]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
[PowerPC] Rename TLS_TPRELGD to TLS_GDIE
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
82704155 2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
5bd4f169
AM
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 5 Largely rewritten by Alan Modra.
5bd4f169 6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 8
ae9a127f
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
5bd4f169 13
ae9a127f
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
5bd4f169 18
4ce794b7
AM
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 22
cd123cb7 23
4ce794b7
AM
24/* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 27
3db64b00 28#include "sysdep.h"
183e98be 29#include <stdarg.h>
5bd4f169 30#include "bfd.h"
5bd4f169
AM
31#include "bfdlink.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
04c9666a 34#include "elf/ppc64.h"
5d1634d7 35#include "elf64-ppc.h"
58d180e8 36#include "dwarf2.h"
5bd4f169 37
805fc799 38static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
5663e321
AM
54static bfd_reloc_status_type ppc64_elf_prefix_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 56static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 58static bfd_vma opd_entry_value
aef36ac1 59 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 60
6d00b590 61#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 62#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 63#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
64#define TARGET_BIG_NAME "elf64-powerpc"
65#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 66#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
67#define ELF_MACHINE_CODE EM_PPC64
68#define ELF_MAXPAGESIZE 0x10000
702d1671
AM
69#define ELF_COMMONPAGESIZE 0x1000
70#define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
ad8e1ba5
AM
71#define elf_info_to_howto ppc64_elf_info_to_howto
72
73#define elf_backend_want_got_sym 0
74#define elf_backend_want_plt_sym 0
75#define elf_backend_plt_alignment 3
76#define elf_backend_plt_not_loaded 1
ad8e1ba5 77#define elf_backend_got_header_size 8
5474d94f 78#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
79#define elf_backend_can_gc_sections 1
80#define elf_backend_can_refcount 1
81#define elf_backend_rela_normal 1
64f52338 82#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 83#define elf_backend_default_execstack 0
ad8e1ba5 84
e717da7e 85#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 86#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 87#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
88#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
89#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
90#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
91#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 92#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 93#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 94#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
95
96#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
97#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
98#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 99#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 100#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 101#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 102#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 103#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 104#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 105#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 106#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 107#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 108#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 109#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 110#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
111#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
112#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 113#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
114#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
115#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 116#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 117#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 118#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
119#define elf_backend_relocate_section ppc64_elf_relocate_section
120#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
121#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
122#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 123#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 124#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 125#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 126#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 127#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 128
5bd4f169
AM
129/* The name of the dynamic interpreter. This is put in the .interp
130 section. */
131#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
132
133/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 134#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 135#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
136
137/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 138#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 139
a078d95a
AM
140/* Offsets to some stack save slots. */
141#define STK_LR 16
142#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 143/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
144 CR save slot. Used only by optimised __tls_get_addr call stub,
145 relying on __tls_get_addr_opt not saving CR.. */
146#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
147
5bd4f169 148/* TOC base pointers offset from start of TOC. */
411e1bfb 149#define TOC_BASE_OFF 0x8000
a27e685f
AM
150/* TOC base alignment. */
151#define TOC_BASE_ALIGN 256
411e1bfb
AM
152
153/* Offset of tp and dtp pointers from start of TLS block. */
154#define TP_OFFSET 0x7000
155#define DTP_OFFSET 0x8000
5bd4f169 156
ad8e1ba5
AM
157/* .plt call stub instructions. The normal stub is like this, but
158 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 159 insert an addi to adjust r11. */
a078d95a 160#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
161#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
162#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
163#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
164#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
165#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
166#define BCTR 0x4e800420 /* bctr */
167
07d6d2b8 168#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
169#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
170#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
171#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
172#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
173
174#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
175#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
176#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
177#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
178#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
179#define BNECTR 0x4ca20420 /* bnectr+ */
180#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 181
71a39c98 182#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 183#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 184#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 185
07d6d2b8
AM
186#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
187#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
188#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 189
04bdff6a 190#define LI_R11_0 0x39600000 /* li %r11,0 */
07d6d2b8 191#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
04bdff6a 192#define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
05d0e962 193#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
194#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
195#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 196#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 197#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962 198#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
04bdff6a 199#define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
05d0e962 200#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 201#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
04bdff6a 202#define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
05d0e962
AM
203#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
204#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
205#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
04bdff6a
AM
206#define PADDI_R12_PC 0x0610000039800000ULL
207#define PLD_R12_PC 0x04100000e5800000ULL
5663e321 208#define PNOP 0x0700000000000000ULL
a345bc8d 209
9e390558
AM
210/* __glink_PLTresolve stub instructions. We enter with the index in R0. */
211#define GLINK_PLTRESOLVE_SIZE(htab) \
407aa07c 212 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
ee4bf8d2
AM
213 /* 0: */
214 /* .quad plt0-1f */
215 /* __glink: */
216#define MFLR_R12 0x7d8802a6 /* mflr %12 */
217#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
218 /* 1: */
219#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 220 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 221#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
222#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
223 /* ld %12,0(%11) */
224 /* ld %2,8(%11) */
225 /* mtctr %12 */
226 /* ld %11,16(%11) */
ee4bf8d2 227 /* bctr */
b9e5796b
AM
228#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
229#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
230#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
231#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
232#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
233
234/* Pad with this. */
235#define NOP 0x60000000
236
721956f4
AM
237/* Some other nops. */
238#define CROR_151515 0x4def7b82
239#define CROR_313131 0x4ffffb82
240
cedb70c5 241/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
242#define LI_R0_0 0x38000000 /* li %r0,0 */
243#define B_DOT 0x48000000 /* b . */
244
245/* After that, we need two instructions to load the index, followed by
246 a branch. */
247#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 248#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 249
deb0e272
AM
250/* Instructions used by the save and restore reg functions. */
251#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
252#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
253#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
254#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
255#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
256#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
257#define LI_R12_0 0x39800000 /* li %r12,0 */
258#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
259#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
260#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
261#define BLR 0x4e800020 /* blr */
262
41bd81ab
AM
263/* Since .opd is an array of descriptors and each entry will end up
264 with identical R_PPC64_RELATIVE relocs, there is really no need to
265 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 266 relocate .opd without reloc entries. */
41bd81ab
AM
267#ifndef NO_OPD_RELOCS
268#define NO_OPD_RELOCS 0
269#endif
810d4e75 270
a4b6fadd
AM
271#ifndef ARRAY_SIZE
272#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
273#endif
274
810d4e75
AM
275static inline int
276abiversion (bfd *abfd)
277{
278 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
279}
280
281static inline void
282set_abiversion (bfd *abfd, int ver)
283{
284 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
285 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
286}
5bd4f169
AM
287\f
288/* Relocation HOWTO's. */
46807bf4
AM
289/* Like other ELF RELA targets that don't apply multiple
290 field-altering relocations to the same localation, src_mask is
291 always zero and pcrel_offset is the same as pc_relative.
292 PowerPC can always use a zero bitpos, even when the field is not at
293 the LSB. For example, a REL24 could use rightshift=2, bisize=24
294 and bitpos=2 which matches the ABI description, or as we do here,
295 rightshift=0, bitsize=26 and bitpos=0. */
296#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
297 complain, special_func) \
298 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
299 complain_overflow_ ## complain, special_func, \
300 #type, FALSE, 0, mask, pc_relative)
301
04c9666a 302static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 303
f3185997
NC
304static reloc_howto_type ppc64_elf_howto_raw[] =
305{
5bd4f169 306 /* This reloc does nothing. */
46807bf4
AM
307 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
308 bfd_elf_generic_reloc),
5bd4f169
AM
309
310 /* A standard 32 bit relocation. */
46807bf4
AM
311 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
312 bfd_elf_generic_reloc),
5bd4f169
AM
313
314 /* An absolute 26 bit branch; the lower two bits must be zero.
315 FIXME: we don't check that, we just clear them. */
46807bf4
AM
316 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
317 bfd_elf_generic_reloc),
5bd4f169
AM
318
319 /* A standard 16 bit relocation. */
46807bf4
AM
320 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
321 bfd_elf_generic_reloc),
5bd4f169
AM
322
323 /* A 16 bit relocation without overflow. */
46807bf4
AM
324 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
325 bfd_elf_generic_reloc),
5bd4f169
AM
326
327 /* Bits 16-31 of an address. */
46807bf4
AM
328 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
329 bfd_elf_generic_reloc),
5bd4f169
AM
330
331 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
332 bits, treated as a signed number, is negative. */
46807bf4
AM
333 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
334 ppc64_elf_ha_reloc),
5bd4f169
AM
335
336 /* An absolute 16 bit branch; the lower two bits must be zero.
337 FIXME: we don't check that, we just clear them. */
46807bf4
AM
338 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
339 ppc64_elf_branch_reloc),
5bd4f169
AM
340
341 /* An absolute 16 bit branch, for which bit 10 should be set to
342 indicate that the branch is expected to be taken. The lower two
343 bits must be zero. */
46807bf4
AM
344 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
345 ppc64_elf_brtaken_reloc),
5bd4f169
AM
346
347 /* An absolute 16 bit branch, for which bit 10 should be set to
348 indicate that the branch is not expected to be taken. The lower
349 two bits must be zero. */
46807bf4
AM
350 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
351 ppc64_elf_brtaken_reloc),
5bd4f169
AM
352
353 /* A relative 26 bit branch; the lower two bits must be zero. */
46807bf4
AM
354 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
355 ppc64_elf_branch_reloc),
5bd4f169 356
05d0e962 357 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
46807bf4
AM
358 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
359 ppc64_elf_branch_reloc),
05d0e962 360
5bd4f169 361 /* A relative 16 bit branch; the lower two bits must be zero. */
46807bf4
AM
362 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
363 ppc64_elf_branch_reloc),
5bd4f169
AM
364
365 /* A relative 16 bit branch. Bit 10 should be set to indicate that
366 the branch is expected to be taken. The lower two bits must be
367 zero. */
46807bf4
AM
368 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
369 ppc64_elf_brtaken_reloc),
5bd4f169
AM
370
371 /* A relative 16 bit branch. Bit 10 should be set to indicate that
372 the branch is not expected to be taken. The lower two bits must
373 be zero. */
46807bf4
AM
374 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
375 ppc64_elf_brtaken_reloc),
5bd4f169
AM
376
377 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
378 symbol. */
46807bf4
AM
379 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
380 ppc64_elf_unhandled_reloc),
5bd4f169
AM
381
382 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
383 the symbol. */
46807bf4
AM
384 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
385 ppc64_elf_unhandled_reloc),
5bd4f169
AM
386
387 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
388 the symbol. */
46807bf4
AM
389 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
390 ppc64_elf_unhandled_reloc),
5bd4f169
AM
391
392 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
393 the symbol. */
46807bf4
AM
394 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
395 ppc64_elf_unhandled_reloc),
5bd4f169
AM
396
397 /* This is used only by the dynamic linker. The symbol should exist
398 both in the object being run and in some shared library. The
399 dynamic linker copies the data addressed by the symbol from the
400 shared library into the object, because the object being
401 run has to have the data at some particular address. */
46807bf4
AM
402 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
403 ppc64_elf_unhandled_reloc),
5bd4f169
AM
404
405 /* Like R_PPC64_ADDR64, but used when setting global offset table
406 entries. */
46807bf4
AM
407 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
408 ppc64_elf_unhandled_reloc),
5bd4f169
AM
409
410 /* Created by the link editor. Marks a procedure linkage table
411 entry for a symbol. */
46807bf4
AM
412 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
413 ppc64_elf_unhandled_reloc),
5bd4f169
AM
414
415 /* Used only by the dynamic linker. When the object is run, this
416 doubleword64 is set to the load address of the object, plus the
417 addend. */
46807bf4
AM
418 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
419 bfd_elf_generic_reloc),
5bd4f169
AM
420
421 /* Like R_PPC64_ADDR32, but may be unaligned. */
46807bf4
AM
422 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
423 bfd_elf_generic_reloc),
5bd4f169
AM
424
425 /* Like R_PPC64_ADDR16, but may be unaligned. */
46807bf4
AM
426 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
427 bfd_elf_generic_reloc),
5bd4f169
AM
428
429 /* 32-bit PC relative. */
46807bf4
AM
430 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431 bfd_elf_generic_reloc),
5bd4f169 432
10ed1bba 433 /* 32-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
434 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
435 ppc64_elf_unhandled_reloc),
5bd4f169
AM
436
437 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
438 FIXME: R_PPC64_PLTREL32 not supported. */
46807bf4
AM
439 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
440 ppc64_elf_unhandled_reloc),
5bd4f169
AM
441
442 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
443 the symbol. */
46807bf4
AM
444 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
445 ppc64_elf_unhandled_reloc),
5bd4f169
AM
446
447 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
448 the symbol. */
46807bf4
AM
449 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
450 ppc64_elf_unhandled_reloc),
5bd4f169
AM
451
452 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
453 the symbol. */
46807bf4
AM
454 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
455 ppc64_elf_unhandled_reloc),
5bd4f169 456
c061c2d8 457 /* 16-bit section relative relocation. */
46807bf4
AM
458 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
459 ppc64_elf_sectoff_reloc),
5bd4f169 460
c061c2d8 461 /* Like R_PPC64_SECTOFF, but no overflow warning. */
46807bf4
AM
462 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
463 ppc64_elf_sectoff_reloc),
5bd4f169
AM
464
465 /* 16-bit upper half section relative relocation. */
46807bf4
AM
466 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
467 ppc64_elf_sectoff_reloc),
5bd4f169
AM
468
469 /* 16-bit upper half adjusted section relative relocation. */
46807bf4
AM
470 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
471 ppc64_elf_sectoff_ha_reloc),
5bd4f169 472
04c9666a 473 /* Like R_PPC64_REL24 without touching the two least significant bits. */
46807bf4
AM
474 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
475 bfd_elf_generic_reloc),
5bd4f169
AM
476
477 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
478
479 /* A standard 64-bit relocation. */
46807bf4
AM
480 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
481 bfd_elf_generic_reloc),
5bd4f169
AM
482
483 /* The bits 32-47 of an address. */
46807bf4
AM
484 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
485 bfd_elf_generic_reloc),
5bd4f169
AM
486
487 /* The bits 32-47 of an address, plus 1 if the contents of the low
488 16 bits, treated as a signed number, is negative. */
46807bf4
AM
489 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
490 ppc64_elf_ha_reloc),
5bd4f169
AM
491
492 /* The bits 48-63 of an address. */
46807bf4
AM
493 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
494 bfd_elf_generic_reloc),
5bd4f169
AM
495
496 /* The bits 48-63 of an address, plus 1 if the contents of the low
497 16 bits, treated as a signed number, is negative. */
46807bf4
AM
498 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
499 ppc64_elf_ha_reloc),
5bd4f169
AM
500
501 /* Like ADDR64, but may be unaligned. */
46807bf4
AM
502 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
503 bfd_elf_generic_reloc),
5bd4f169
AM
504
505 /* 64-bit relative relocation. */
46807bf4
AM
506 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
507 bfd_elf_generic_reloc),
5bd4f169 508
cedb70c5 509 /* 64-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
510 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
511 ppc64_elf_unhandled_reloc),
5bd4f169
AM
512
513 /* 64-bit PC relative relocation to the symbol's procedure linkage
514 table. */
515 /* FIXME: R_PPC64_PLTREL64 not supported. */
46807bf4
AM
516 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
517 ppc64_elf_unhandled_reloc),
5bd4f169
AM
518
519 /* 16 bit TOC-relative relocation. */
5bd4f169 520 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
46807bf4
AM
521 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
522 ppc64_elf_toc_reloc),
5bd4f169
AM
523
524 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 525 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
46807bf4
AM
526 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
527 ppc64_elf_toc_reloc),
5bd4f169
AM
528
529 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 530 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
46807bf4
AM
531 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
532 ppc64_elf_toc_reloc),
5bd4f169
AM
533
534 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
535 contents of the low 16 bits, treated as a signed number, is
536 negative. */
5bd4f169 537 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
46807bf4
AM
538 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
539 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
540
541 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 542 /* R_PPC64_TOC 51 doubleword64 .TOC. */
46807bf4
AM
543 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
544 ppc64_elf_toc64_reloc),
5bd4f169
AM
545
546 /* Like R_PPC64_GOT16, but also informs the link editor that the
547 value to relocate may (!) refer to a PLT entry which the link
548 editor (a) may replace with the symbol value. If the link editor
549 is unable to fully resolve the symbol, it may (b) create a PLT
550 entry and store the address to the new PLT entry in the GOT.
551 This permits lazy resolution of function symbols at run time.
552 The link editor may also skip all of this and just (c) emit a
553 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
554 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
46807bf4
AM
555 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
556 ppc64_elf_unhandled_reloc),
411e1bfb
AM
557
558 /* Like R_PPC64_PLTGOT16, but without overflow. */
559 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
560 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
561 ppc64_elf_unhandled_reloc),
411e1bfb
AM
562
563 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
564 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
46807bf4
AM
565 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
566 ppc64_elf_unhandled_reloc),
411e1bfb
AM
567
568 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
569 1 if the contents of the low 16 bits, treated as a signed number,
570 is negative. */
571 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
46807bf4
AM
572 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
573 ppc64_elf_unhandled_reloc),
411e1bfb
AM
574
575 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
46807bf4
AM
576 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
577 bfd_elf_generic_reloc),
411e1bfb
AM
578
579 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
46807bf4
AM
580 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
581 bfd_elf_generic_reloc),
411e1bfb
AM
582
583 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
46807bf4
AM
584 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
585 ppc64_elf_unhandled_reloc),
411e1bfb
AM
586
587 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
46807bf4
AM
588 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
589 ppc64_elf_unhandled_reloc),
411e1bfb
AM
590
591 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
46807bf4
AM
592 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
593 ppc64_elf_unhandled_reloc),
411e1bfb
AM
594
595 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
46807bf4
AM
596 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
597 ppc64_elf_sectoff_reloc),
411e1bfb
AM
598
599 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
46807bf4
AM
600 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
601 ppc64_elf_sectoff_reloc),
411e1bfb
AM
602
603 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
46807bf4
AM
604 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_toc_reloc),
411e1bfb
AM
606
607 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
46807bf4
AM
608 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
609 ppc64_elf_toc_reloc),
411e1bfb
AM
610
611 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
612 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
46807bf4
AM
613 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
614 ppc64_elf_unhandled_reloc),
411e1bfb
AM
615
616 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
617 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
618 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
619 ppc64_elf_unhandled_reloc),
411e1bfb 620
727fc41e 621 /* Marker relocs for TLS. */
46807bf4
AM
622 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
623 bfd_elf_generic_reloc),
624
625 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
626 bfd_elf_generic_reloc),
627
628 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
629 bfd_elf_generic_reloc),
727fc41e 630
23cedd1d
AM
631 /* Marker reloc for optimizing r2 save in prologue rather than on
632 each plt call stub. */
46807bf4
AM
633 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
634 bfd_elf_generic_reloc),
3b421ab3 635
23cedd1d 636 /* Marker relocs on inline plt call instructions. */
46807bf4
AM
637 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
638 bfd_elf_generic_reloc),
639
640 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
641 bfd_elf_generic_reloc),
23cedd1d 642
411e1bfb
AM
643 /* Computes the load module index of the load module that contains the
644 definition of its TLS sym. */
46807bf4
AM
645 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
646 ppc64_elf_unhandled_reloc),
411e1bfb
AM
647
648 /* Computes a dtv-relative displacement, the difference between the value
649 of sym+add and the base address of the thread-local storage block that
650 contains the definition of sym, minus 0x8000. */
46807bf4
AM
651 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
652 ppc64_elf_unhandled_reloc),
411e1bfb
AM
653
654 /* A 16 bit dtprel reloc. */
46807bf4
AM
655 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
656 ppc64_elf_unhandled_reloc),
411e1bfb
AM
657
658 /* Like DTPREL16, but no overflow. */
46807bf4
AM
659 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
660 ppc64_elf_unhandled_reloc),
411e1bfb
AM
661
662 /* Like DTPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
663 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
664 ppc64_elf_unhandled_reloc),
411e1bfb
AM
665
666 /* Like DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
667 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
668 ppc64_elf_unhandled_reloc),
411e1bfb
AM
669
670 /* Like DTPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
671 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
672 ppc64_elf_unhandled_reloc),
411e1bfb
AM
673
674 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
675 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
676 ppc64_elf_unhandled_reloc),
411e1bfb
AM
677
678 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
679 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
680 ppc64_elf_unhandled_reloc),
411e1bfb
AM
681
682 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
683 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
684 ppc64_elf_unhandled_reloc),
411e1bfb
AM
685
686 /* Like DTPREL16, but for insns with a DS field. */
46807bf4
AM
687 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
688 ppc64_elf_unhandled_reloc),
411e1bfb
AM
689
690 /* Like DTPREL16_DS, but no overflow. */
46807bf4
AM
691 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
692 ppc64_elf_unhandled_reloc),
411e1bfb
AM
693
694 /* Computes a tp-relative displacement, the difference between the value of
695 sym+add and the value of the thread pointer (r13). */
46807bf4
AM
696 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
697 ppc64_elf_unhandled_reloc),
411e1bfb
AM
698
699 /* A 16 bit tprel reloc. */
46807bf4
AM
700 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
701 ppc64_elf_unhandled_reloc),
411e1bfb
AM
702
703 /* Like TPREL16, but no overflow. */
46807bf4
AM
704 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
705 ppc64_elf_unhandled_reloc),
411e1bfb
AM
706
707 /* Like TPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
708 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
709 ppc64_elf_unhandled_reloc),
411e1bfb
AM
710
711 /* Like TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
712 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
713 ppc64_elf_unhandled_reloc),
411e1bfb
AM
714
715 /* Like TPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
716 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
717 ppc64_elf_unhandled_reloc),
411e1bfb
AM
718
719 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
720 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
721 ppc64_elf_unhandled_reloc),
411e1bfb
AM
722
723 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
724 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
725 ppc64_elf_unhandled_reloc),
411e1bfb
AM
726
727 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
728 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
729 ppc64_elf_unhandled_reloc),
411e1bfb
AM
730
731 /* Like TPREL16, but for insns with a DS field. */
46807bf4
AM
732 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
733 ppc64_elf_unhandled_reloc),
411e1bfb
AM
734
735 /* Like TPREL16_DS, but no overflow. */
46807bf4
AM
736 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
737 ppc64_elf_unhandled_reloc),
411e1bfb
AM
738
739 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
740 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
741 to the first entry relative to the TOC base (r2). */
46807bf4
AM
742 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
743 ppc64_elf_unhandled_reloc),
5bd4f169 744
411e1bfb 745 /* Like GOT_TLSGD16, but no overflow. */
46807bf4
AM
746 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
747 ppc64_elf_unhandled_reloc),
5bd4f169 748
411e1bfb 749 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
46807bf4
AM
750 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
751 ppc64_elf_unhandled_reloc),
5bd4f169 752
411e1bfb 753 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
46807bf4
AM
754 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
755 ppc64_elf_unhandled_reloc),
5bd4f169 756
411e1bfb
AM
757 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
758 with values (sym+add)@dtpmod and zero, and computes the offset to the
759 first entry relative to the TOC base (r2). */
46807bf4
AM
760 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
761 ppc64_elf_unhandled_reloc),
5bd4f169 762
411e1bfb 763 /* Like GOT_TLSLD16, but no overflow. */
46807bf4
AM
764 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
765 ppc64_elf_unhandled_reloc),
5bd4f169 766
411e1bfb 767 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
46807bf4
AM
768 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
5bd4f169 770
411e1bfb 771 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
46807bf4
AM
772 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
773 ppc64_elf_unhandled_reloc),
5bd4f169 774
411e1bfb
AM
775 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
776 the offset to the entry relative to the TOC base (r2). */
46807bf4
AM
777 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
778 ppc64_elf_unhandled_reloc),
5bd4f169 779
411e1bfb 780 /* Like GOT_DTPREL16_DS, but no overflow. */
46807bf4
AM
781 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
782 ppc64_elf_unhandled_reloc),
5bd4f169 783
411e1bfb 784 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
785 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
5bd4f169 787
411e1bfb 788 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
789 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
790 ppc64_elf_unhandled_reloc),
411e1bfb
AM
791
792 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
793 offset to the entry relative to the TOC base (r2). */
46807bf4
AM
794 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
795 ppc64_elf_unhandled_reloc),
5bd4f169 796
411e1bfb 797 /* Like GOT_TPREL16_DS, but no overflow. */
46807bf4
AM
798 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
799 ppc64_elf_unhandled_reloc),
5bd4f169 800
411e1bfb 801 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
802 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
803 ppc64_elf_unhandled_reloc),
5bd4f169 804
411e1bfb 805 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
806 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
807 ppc64_elf_unhandled_reloc),
808
809 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
810 ppc64_elf_unhandled_reloc),
811
812 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
813 bfd_elf_generic_reloc),
e054468f 814
25f23106 815 /* A 16 bit relative relocation. */
46807bf4
AM
816 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
817 bfd_elf_generic_reloc),
25f23106
AM
818
819 /* A 16 bit relative relocation without overflow. */
46807bf4
AM
820 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
821 bfd_elf_generic_reloc),
25f23106
AM
822
823 /* The high order 16 bits of a relative address. */
46807bf4
AM
824 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
825 bfd_elf_generic_reloc),
25f23106
AM
826
827 /* The high order 16 bits of a relative address, plus 1 if the contents of
828 the low 16 bits, treated as a signed number, is negative. */
46807bf4
AM
829 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
830 ppc64_elf_ha_reloc),
25f23106 831
4a969973
AM
832 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
833 bfd_elf_generic_reloc),
834
835 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
836 ppc64_elf_ha_reloc),
837
838 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
839 bfd_elf_generic_reloc),
840
841 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
842 ppc64_elf_ha_reloc),
843
844 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
845 bfd_elf_generic_reloc),
846
847 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
848 ppc64_elf_ha_reloc),
849
a680de9a 850 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
46807bf4
AM
851 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
852 ppc64_elf_ha_reloc),
a680de9a 853
7ba71655 854 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
46807bf4
AM
855 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
856 ppc64_elf_ha_reloc),
7ba71655 857
f9c6b907 858 /* Like R_PPC64_ADDR16_HI, but no overflow. */
46807bf4
AM
859 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
860 bfd_elf_generic_reloc),
f9c6b907
AM
861
862 /* Like R_PPC64_ADDR16_HA, but no overflow. */
46807bf4
AM
863 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
864 ppc64_elf_ha_reloc),
f9c6b907
AM
865
866 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
46807bf4
AM
867 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
868 ppc64_elf_unhandled_reloc),
f9c6b907
AM
869
870 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
46807bf4
AM
871 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
872 ppc64_elf_unhandled_reloc),
f9c6b907
AM
873
874 /* Like R_PPC64_TPREL16_HI, but no overflow. */
46807bf4
AM
875 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
876 ppc64_elf_unhandled_reloc),
f9c6b907
AM
877
878 /* Like R_PPC64_TPREL16_HA, but no overflow. */
46807bf4
AM
879 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
880 ppc64_elf_unhandled_reloc),
f9c6b907 881
006589cf 882 /* Marker reloc on ELFv2 large-model function entry. */
46807bf4
AM
883 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
884 bfd_elf_generic_reloc),
006589cf 885
45965137 886 /* Like ADDR64, but use local entry point of function. */
46807bf4
AM
887 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
888 bfd_elf_generic_reloc),
45965137 889
5663e321
AM
890 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
891 bfd_elf_generic_reloc),
892
893 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
894 bfd_elf_generic_reloc),
895
896 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
897 bfd_elf_generic_reloc),
898
899 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
900 ppc64_elf_prefix_reloc),
901
902 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
903 ppc64_elf_prefix_reloc),
904
905 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
906 ppc64_elf_prefix_reloc),
907
908 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
909 ppc64_elf_prefix_reloc),
910
911 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
912 ppc64_elf_prefix_reloc),
913
914 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
915 ppc64_elf_unhandled_reloc),
916
917 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
918 ppc64_elf_unhandled_reloc),
919
920 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
921 ppc64_elf_unhandled_reloc),
922
923 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
924 bfd_elf_generic_reloc),
925
926 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
927 ppc64_elf_ha_reloc),
928
929 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
930 bfd_elf_generic_reloc),
931
932 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
933 ppc64_elf_ha_reloc),
934
935 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
936 bfd_elf_generic_reloc),
937
938 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
939 ppc64_elf_ha_reloc),
940
941 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
942 bfd_elf_generic_reloc),
943
944 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
945 ppc64_elf_ha_reloc),
946
947 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
948 ppc64_elf_prefix_reloc),
949
950 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
951 ppc64_elf_prefix_reloc),
952
5bd4f169 953 /* GNU extension to record C++ vtable hierarchy. */
46807bf4
AM
954 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
955 NULL),
5bd4f169
AM
956
957 /* GNU extension to record C++ vtable member usage. */
46807bf4
AM
958 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
959 NULL),
5bd4f169
AM
960};
961
962\f
963/* Initialize the ppc64_elf_howto_table, so that linear accesses can
964 be done. */
965
966static void
4ce794b7 967ppc_howto_init (void)
5bd4f169
AM
968{
969 unsigned int i, type;
970
a4b6fadd 971 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
972 {
973 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 974 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
975 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
976 }
977}
978
979static reloc_howto_type *
f3185997 980ppc64_elf_reloc_type_lookup (bfd *abfd,
4ce794b7 981 bfd_reloc_code_real_type code)
5bd4f169 982{
411e1bfb 983 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
984
985 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
986 /* Initialize howto table if needed. */
987 ppc_howto_init ();
988
4ce794b7 989 switch (code)
5bd4f169
AM
990 {
991 default:
f3185997 992 /* xgettext:c-format */
2cdcc330
AM
993 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
994 (int) code);
f3185997 995 bfd_set_error (bfd_error_bad_value);
4ce794b7 996 return NULL;
5bd4f169 997
411e1bfb
AM
998 case BFD_RELOC_NONE: r = R_PPC64_NONE;
999 break;
1000 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1001 break;
1002 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1003 break;
1004 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1005 break;
1006 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1007 break;
1008 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1009 break;
f9c6b907
AM
1010 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1011 break;
411e1bfb 1012 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1013 break;
f9c6b907
AM
1014 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1015 break;
411e1bfb 1016 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1017 break;
411e1bfb 1018 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1019 break;
411e1bfb 1020 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1021 break;
411e1bfb 1022 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1023 break;
05d0e962
AM
1024 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1025 break;
411e1bfb 1026 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1027 break;
411e1bfb 1028 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1029 break;
411e1bfb 1030 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1031 break;
411e1bfb 1032 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1033 break;
411e1bfb 1034 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1035 break;
411e1bfb 1036 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1037 break;
411e1bfb 1038 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1039 break;
411e1bfb 1040 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1041 break;
411e1bfb 1042 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1043 break;
411e1bfb 1044 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1045 break;
411e1bfb 1046 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1047 break;
411e1bfb 1048 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1049 break;
411e1bfb 1050 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1051 break;
411e1bfb 1052 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1053 break;
411e1bfb 1054 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1055 break;
411e1bfb 1056 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1057 break;
411e1bfb 1058 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1059 break;
411e1bfb 1060 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1061 break;
411e1bfb 1062 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1063 break;
411e1bfb 1064 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1065 break;
411e1bfb 1066 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1067 break;
411e1bfb 1068 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1069 break;
411e1bfb 1070 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1071 break;
411e1bfb 1072 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1073 break;
411e1bfb 1074 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1075 break;
411e1bfb 1076 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1077 break;
411e1bfb 1078 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1079 break;
411e1bfb 1080 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1081 break;
411e1bfb 1082 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1083 break;
411e1bfb 1084 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1085 break;
411e1bfb 1086 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1087 break;
411e1bfb 1088 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1089 break;
411e1bfb 1090 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1091 break;
411e1bfb 1092 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1093 break;
411e1bfb 1094 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1095 break;
411e1bfb 1096 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1097 break;
411e1bfb 1098 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1099 break;
411e1bfb 1100 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1101 break;
411e1bfb 1102 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1103 break;
411e1bfb 1104 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1105 break;
411e1bfb 1106 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1107 break;
411e1bfb 1108 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1109 break;
411e1bfb 1110 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1111 break;
411e1bfb 1112 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1113 break;
411e1bfb 1114 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1115 break;
411e1bfb 1116 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1117 break;
411e1bfb 1118 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1119 break;
411e1bfb 1120 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1121 break;
411e1bfb 1122 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1123 break;
727fc41e
AM
1124 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1125 break;
1126 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1127 break;
411e1bfb 1128 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1129 break;
411e1bfb 1130 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1131 break;
411e1bfb 1132 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1133 break;
411e1bfb 1134 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1135 break;
f9c6b907
AM
1136 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1137 break;
411e1bfb 1138 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1139 break;
f9c6b907
AM
1140 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1141 break;
411e1bfb 1142 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1143 break;
411e1bfb
AM
1144 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1145 break;
1146 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1147 break;
1148 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1149 break;
f9c6b907
AM
1150 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1151 break;
411e1bfb
AM
1152 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1153 break;
f9c6b907
AM
1154 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1155 break;
411e1bfb
AM
1156 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1157 break;
1158 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1159 break;
1160 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1161 break;
1162 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1163 break;
1164 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1165 break;
1166 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1167 break;
1168 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1169 break;
1170 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1171 break;
1172 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1173 break;
1174 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1175 break;
1176 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1177 break;
1178 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1179 break;
1180 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1181 break;
1182 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1183 break;
1184 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1185 break;
1186 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1187 break;
1188 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1189 break;
1190 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1191 break;
1192 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1193 break;
1194 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1195 break;
1196 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1197 break;
1198 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1199 break;
1200 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1201 break;
1202 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1203 break;
1204 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1205 break;
1206 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1207 break;
1208 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1209 break;
1210 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1211 break;
1212 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1213 break;
25f23106
AM
1214 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1215 break;
1216 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1217 break;
1218 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1219 break;
1220 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1221 break;
4a969973
AM
1222 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1223 break;
1224 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1225 break;
1226 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1227 break;
1228 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1229 break;
1230 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1231 break;
1232 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1233 break;
7ba71655
AM
1234 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1235 break;
a680de9a
PB
1236 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1237 break;
006589cf
AM
1238 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1239 break;
45965137
AM
1240 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1241 break;
5663e321
AM
1242 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1243 break;
1244 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1245 break;
1246 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1247 break;
1248 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1249 break;
1250 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1251 break;
1252 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1253 break;
1254 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1255 break;
1256 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1257 break;
1258 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1259 break;
1260 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1261 break;
1262 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1263 break;
1264 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1265 break;
1266 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1267 break;
1268 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1269 break;
1270 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1271 break;
1272 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1273 break;
1274 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1275 break;
411e1bfb
AM
1276 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1277 break;
1278 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1279 break;
1280 }
1281
4ce794b7 1282 return ppc64_elf_howto_table[r];
5bd4f169
AM
1283};
1284
157090f7
AM
1285static reloc_howto_type *
1286ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1287 const char *r_name)
1288{
1289 unsigned int i;
1290
a4b6fadd 1291 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1292 if (ppc64_elf_howto_raw[i].name != NULL
1293 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1294 return &ppc64_elf_howto_raw[i];
1295
1296 return NULL;
1297}
1298
5bd4f169
AM
1299/* Set the howto pointer for a PowerPC ELF reloc. */
1300
f3185997 1301static bfd_boolean
4aef7643 1302ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1303 Elf_Internal_Rela *dst)
5bd4f169 1304{
65f38f15
AM
1305 unsigned int type;
1306
ef60b7ff 1307 /* Initialize howto table if needed. */
5bd4f169 1308 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1309 ppc_howto_init ();
1310
65f38f15 1311 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1312 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1313 {
695344c0 1314 /* xgettext:c-format */
0aa13fee 1315 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1316 abfd, type);
f3185997
NC
1317 bfd_set_error (bfd_error_bad_value);
1318 return FALSE;
d0fb9a8d 1319 }
65f38f15 1320 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1321 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1322 {
1323 /* xgettext:c-format */
1324 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1325 abfd, type);
1326 bfd_set_error (bfd_error_bad_value);
1327 return FALSE;
1328 }
2cdcc330 1329
f3185997 1330 return TRUE;
5bd4f169
AM
1331}
1332
04c9666a 1333/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1334
1335static bfd_reloc_status_type
4ce794b7
AM
1336ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1337 void *data, asection *input_section,
1338 bfd *output_bfd, char **error_message)
5bd4f169 1339{
a680de9a
PB
1340 enum elf_ppc64_reloc_type r_type;
1341 long insn;
1342 bfd_size_type octets;
3de43e7b 1343 bfd_vma value;
a680de9a 1344
805fc799
AM
1345 /* If this is a relocatable link (output_bfd test tells us), just
1346 call the generic function. Any adjustment will be done at final
1347 link time. */
1348 if (output_bfd != NULL)
cedb70c5 1349 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1350 input_section, output_bfd, error_message);
1351
5663e321
AM
1352 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1353 We won't actually be using the low bits, so trashing them
805fc799 1354 doesn't matter. */
a680de9a 1355 r_type = reloc_entry->howto->type;
5663e321
AM
1356 if (r_type == R_PPC64_ADDR16_HIGHERA34
1357 || r_type == R_PPC64_ADDR16_HIGHESTA34
1358 || r_type == R_PPC64_REL16_HIGHERA34
1359 || r_type == R_PPC64_REL16_HIGHESTA34)
1360 reloc_entry->addend += 1ULL << 33;
1361 else
1362 reloc_entry->addend += 1U << 15;
a680de9a
PB
1363 if (r_type != R_PPC64_REL16DX_HA)
1364 return bfd_reloc_continue;
1365
1366 value = 0;
1367 if (!bfd_is_com_section (symbol->section))
1368 value = symbol->value;
1369 value += (reloc_entry->addend
1370 + symbol->section->output_offset
1371 + symbol->section->output_section->vma);
1372 value -= (reloc_entry->address
1373 + input_section->output_offset
1374 + input_section->output_section->vma);
3de43e7b 1375 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
1376
1377 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1378 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1379 insn &= ~0x1fffc1;
3de43e7b 1380 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1381 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1382 if (value + 0x8000 > 0xffff)
a680de9a
PB
1383 return bfd_reloc_overflow;
1384 return bfd_reloc_ok;
805fc799 1385}
5bd4f169 1386
2441e016
AM
1387static bfd_reloc_status_type
1388ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1389 void *data, asection *input_section,
1390 bfd *output_bfd, char **error_message)
1391{
1392 if (output_bfd != NULL)
1393 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1394 input_section, output_bfd, error_message);
1395
699733f6
AM
1396 if (strcmp (symbol->section->name, ".opd") == 0
1397 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1398 {
1399 bfd_vma dest = opd_entry_value (symbol->section,
1400 symbol->value + reloc_entry->addend,
aef36ac1 1401 NULL, NULL, FALSE);
2441e016
AM
1402 if (dest != (bfd_vma) -1)
1403 reloc_entry->addend = dest - (symbol->value
1404 + symbol->section->output_section->vma
1405 + symbol->section->output_offset);
1406 }
810d4e75
AM
1407 else
1408 {
1409 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1410
1411 if (symbol->section->owner != abfd
9f284bf9 1412 && symbol->section->owner != NULL
810d4e75
AM
1413 && abiversion (symbol->section->owner) >= 2)
1414 {
1415 unsigned int i;
1416
1417 for (i = 0; i < symbol->section->owner->symcount; ++i)
1418 {
1419 asymbol *symdef = symbol->section->owner->outsymbols[i];
1420
1421 if (strcmp (symdef->name, symbol->name) == 0)
1422 {
1423 elfsym = (elf_symbol_type *) symdef;
1424 break;
1425 }
1426 }
1427 }
1428 reloc_entry->addend
1429 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1430 }
2441e016
AM
1431 return bfd_reloc_continue;
1432}
1433
805fc799 1434static bfd_reloc_status_type
4ce794b7
AM
1435ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1436 void *data, asection *input_section,
1437 bfd *output_bfd, char **error_message)
805fc799
AM
1438{
1439 long insn;
04c9666a 1440 enum elf_ppc64_reloc_type r_type;
805fc799 1441 bfd_size_type octets;
794e51c0
AM
1442 /* Assume 'at' branch hints. */
1443 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
1444
1445 /* If this is a relocatable link (output_bfd test tells us), just
1446 call the generic function. Any adjustment will be done at final
1447 link time. */
5bd4f169 1448 if (output_bfd != NULL)
cedb70c5 1449 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1450 input_section, output_bfd, error_message);
1451
1452 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1453 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1454 insn &= ~(0x01 << 21);
4ce794b7 1455 r_type = reloc_entry->howto->type;
805fc799
AM
1456 if (r_type == R_PPC64_ADDR14_BRTAKEN
1457 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1458 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1459
794e51c0 1460 if (is_isa_v2)
5bd4f169 1461 {
805fc799
AM
1462 /* Set 'a' bit. This is 0b00010 in BO field for branch
1463 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1464 for branch on CTR insns (BO == 1a00t or 1a01t). */
1465 if ((insn & (0x14 << 21)) == (0x04 << 21))
1466 insn |= 0x02 << 21;
1467 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1468 insn |= 0x08 << 21;
1469 else
2441e016 1470 goto out;
5bd4f169 1471 }
805fc799
AM
1472 else
1473 {
1474 bfd_vma target = 0;
1475 bfd_vma from;
5bd4f169 1476
805fc799
AM
1477 if (!bfd_is_com_section (symbol->section))
1478 target = symbol->value;
1479 target += symbol->section->output_section->vma;
1480 target += symbol->section->output_offset;
1481 target += reloc_entry->addend;
5bd4f169 1482
805fc799
AM
1483 from = (reloc_entry->address
1484 + input_section->output_offset
1485 + input_section->output_section->vma);
5bd4f169 1486
805fc799
AM
1487 /* Invert 'y' bit if not the default. */
1488 if ((bfd_signed_vma) (target - from) < 0)
1489 insn ^= 0x01 << 21;
1490 }
4ce794b7 1491 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1492 out:
1493 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1494 input_section, output_bfd, error_message);
805fc799 1495}
5bd4f169 1496
805fc799 1497static bfd_reloc_status_type
4ce794b7
AM
1498ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1499 void *data, asection *input_section,
1500 bfd *output_bfd, char **error_message)
805fc799
AM
1501{
1502 /* If this is a relocatable link (output_bfd test tells us), just
1503 call the generic function. Any adjustment will be done at final
1504 link time. */
1505 if (output_bfd != NULL)
cedb70c5 1506 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1507 input_section, output_bfd, error_message);
5bd4f169 1508
805fc799
AM
1509 /* Subtract the symbol section base address. */
1510 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1511 return bfd_reloc_continue;
1512}
1513
805fc799 1514static bfd_reloc_status_type
4ce794b7
AM
1515ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1516 void *data, asection *input_section,
1517 bfd *output_bfd, char **error_message)
805fc799
AM
1518{
1519 /* If this is a relocatable link (output_bfd test tells us), just
1520 call the generic function. Any adjustment will be done at final
1521 link time. */
1522 if (output_bfd != NULL)
cedb70c5 1523 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1524 input_section, output_bfd, error_message);
1525
1526 /* Subtract the symbol section base address. */
1527 reloc_entry->addend -= symbol->section->output_section->vma;
1528
1529 /* Adjust the addend for sign extension of the low 16 bits. */
1530 reloc_entry->addend += 0x8000;
1531 return bfd_reloc_continue;
1532}
1533
1534static bfd_reloc_status_type
4ce794b7
AM
1535ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1536 void *data, asection *input_section,
1537 bfd *output_bfd, char **error_message)
805fc799
AM
1538{
1539 bfd_vma TOCstart;
1540
1541 /* If this is a relocatable link (output_bfd test tells us), just
1542 call the generic function. Any adjustment will be done at final
1543 link time. */
1544 if (output_bfd != NULL)
cedb70c5 1545 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1546 input_section, output_bfd, error_message);
1547
1548 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1549 if (TOCstart == 0)
1c865ab2 1550 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1551
1552 /* Subtract the TOC base address. */
1553 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1554 return bfd_reloc_continue;
1555}
1556
1557static bfd_reloc_status_type
4ce794b7
AM
1558ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1559 void *data, asection *input_section,
1560 bfd *output_bfd, char **error_message)
805fc799
AM
1561{
1562 bfd_vma TOCstart;
1563
1564 /* If this is a relocatable link (output_bfd test tells us), just
1565 call the generic function. Any adjustment will be done at final
1566 link time. */
1567 if (output_bfd != NULL)
cedb70c5 1568 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1569 input_section, output_bfd, error_message);
1570
1571 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1572 if (TOCstart == 0)
1c865ab2 1573 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1574
1575 /* Subtract the TOC base address. */
1576 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1577
1578 /* Adjust the addend for sign extension of the low 16 bits. */
1579 reloc_entry->addend += 0x8000;
1580 return bfd_reloc_continue;
1581}
1582
1583static bfd_reloc_status_type
4ce794b7
AM
1584ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1585 void *data, asection *input_section,
1586 bfd *output_bfd, char **error_message)
805fc799
AM
1587{
1588 bfd_vma TOCstart;
1589 bfd_size_type octets;
1590
1591 /* If this is a relocatable link (output_bfd test tells us), just
1592 call the generic function. Any adjustment will be done at final
1593 link time. */
1594 if (output_bfd != NULL)
cedb70c5 1595 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1596 input_section, output_bfd, error_message);
1597
1598 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1599 if (TOCstart == 0)
1c865ab2 1600 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1601
1602 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1603 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1604 return bfd_reloc_ok;
1605}
1606
5663e321
AM
1607static bfd_reloc_status_type
1608ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1609 void *data, asection *input_section,
1610 bfd *output_bfd, char **error_message)
1611{
1612 uint64_t insn;
1613 bfd_vma targ;
1614
1615 if (output_bfd != NULL)
1616 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1617 input_section, output_bfd, error_message);
1618
1619 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1620 insn <<= 32;
1621 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1622
1623 targ = (symbol->section->output_section->vma
1624 + symbol->section->output_offset
1625 + reloc_entry->addend);
1626 if (!bfd_is_com_section (symbol->section))
1627 targ += symbol->value;
1628 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1629 targ += 1ULL << 33;
1630 if (reloc_entry->howto->pc_relative)
1631 {
1632 bfd_vma from = (reloc_entry->address
1633 + input_section->output_offset
1634 + input_section->output_section->vma);
1635 targ -=from;
1636 }
1637 targ >>= reloc_entry->howto->rightshift;
1638 insn &= ~reloc_entry->howto->dst_mask;
1639 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1640 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1641 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1642 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1643 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1644 >= 1ULL << reloc_entry->howto->bitsize))
1645 return bfd_reloc_overflow;
1646 return bfd_reloc_ok;
1647}
1648
805fc799 1649static bfd_reloc_status_type
4ce794b7
AM
1650ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1651 void *data, asection *input_section,
1652 bfd *output_bfd, char **error_message)
805fc799
AM
1653{
1654 /* If this is a relocatable link (output_bfd test tells us), just
1655 call the generic function. Any adjustment will be done at final
1656 link time. */
1657 if (output_bfd != NULL)
cedb70c5 1658 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1659 input_section, output_bfd, error_message);
1660
1661 if (error_message != NULL)
1662 {
1663 static char buf[60];
1664 sprintf (buf, "generic linker can't handle %s",
1665 reloc_entry->howto->name);
1666 *error_message = buf;
1667 }
1668 return bfd_reloc_dangerous;
1669}
1670
927be08e
AM
1671/* Track GOT entries needed for a given symbol. We might need more
1672 than one got entry per symbol. */
1673struct got_entry
1674{
1675 struct got_entry *next;
1676
1677 /* The symbol addend that we'll be placing in the GOT. */
1678 bfd_vma addend;
1679
1680 /* Unlike other ELF targets, we use separate GOT entries for the same
1681 symbol referenced from different input files. This is to support
1682 automatic multiple TOC/GOT sections, where the TOC base can vary
1683 from one input file to another. After partitioning into TOC groups
1684 we merge entries within the group.
1685
1686 Point to the BFD owning this GOT entry. */
1687 bfd *owner;
1688
1689 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1690 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1691 unsigned char tls_type;
927be08e
AM
1692
1693 /* Non-zero if got.ent points to real entry. */
f961d9dd 1694 unsigned char is_indirect;
927be08e
AM
1695
1696 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1697 union
2cdcc330
AM
1698 {
1699 bfd_signed_vma refcount;
1700 bfd_vma offset;
1701 struct got_entry *ent;
1702 } got;
927be08e
AM
1703};
1704
1705/* The same for PLT. */
1706struct plt_entry
1707{
1708 struct plt_entry *next;
1709
1710 bfd_vma addend;
1711
1712 union
2cdcc330
AM
1713 {
1714 bfd_signed_vma refcount;
1715 bfd_vma offset;
1716 } plt;
927be08e
AM
1717};
1718
e717da7e
AM
1719struct ppc64_elf_obj_tdata
1720{
1721 struct elf_obj_tdata elf;
1722
1723 /* Shortcuts to dynamic linker sections. */
1724 asection *got;
1725 asection *relgot;
1726
b3fac117
AM
1727 /* Used during garbage collection. We attach global symbols defined
1728 on removed .opd entries to this section so that the sym is removed. */
1729 asection *deleted_section;
81688140 1730
927be08e 1731 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1732 sections means we potentially need one of these for each input bfd. */
927be08e 1733 struct got_entry tlsld_got;
8860955f 1734
2cdcc330
AM
1735 union
1736 {
729eabd5
AM
1737 /* A copy of relocs before they are modified for --emit-relocs. */
1738 Elf_Internal_Rela *relocs;
1739
1740 /* Section contents. */
1741 bfd_byte *contents;
1742 } opd;
d77c8a4b
AM
1743
1744 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1745 the reloc to be in the range -32768 to 32767. */
98528052
AM
1746 unsigned int has_small_toc_reloc : 1;
1747
560c8763
AM
1748 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1749 instruction not one we handle. */
1750 unsigned int unexpected_toc_insn : 1;
066f4018
AM
1751
1752 /* Set if got relocs that can be optimised are present in this file. */
1753 unsigned int has_gotrel : 1;
e717da7e
AM
1754};
1755
1756#define ppc64_elf_tdata(bfd) \
1757 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1758
1759#define ppc64_tlsld_got(bfd) \
1760 (&ppc64_elf_tdata (bfd)->tlsld_got)
1761
0c8d6e5c
AM
1762#define is_ppc64_elf(bfd) \
1763 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1764 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1765
e717da7e
AM
1766/* Override the generic function because we store some extras. */
1767
1768static bfd_boolean
1769ppc64_elf_mkobject (bfd *abfd)
1770{
0ffa91dd 1771 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1772 PPC64_ELF_DATA);
e717da7e
AM
1773}
1774
feee612b 1775/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1776 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1777
b34976b6 1778static bfd_boolean
4ce794b7 1779ppc64_elf_object_p (bfd *abfd)
feee612b 1780{
14b57c7c
AM
1781 if (!abfd->arch_info->the_default)
1782 return TRUE;
1783
1784 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1785 {
1786 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1787
1788 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1789 {
1790 /* Relies on arch after 32 bit default being 64 bit default. */
1791 abfd->arch_info = abfd->arch_info->next;
1792 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1793 }
1794 }
14b57c7c 1795 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1796}
1797
d37c89e5
AM
1798/* Support for core dump NOTE sections. */
1799
1800static bfd_boolean
1801ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1802{
eea6121a 1803 size_t offset, size;
d37c89e5
AM
1804
1805 if (note->descsz != 504)
1806 return FALSE;
1807
1808 /* pr_cursig */
228e534f 1809 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1810
1811 /* pr_pid */
228e534f 1812 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1813
1814 /* pr_reg */
1815 offset = 112;
eea6121a 1816 size = 384;
d37c89e5
AM
1817
1818 /* Make a ".reg/999" section. */
1819 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1820 size, note->descpos + offset);
d37c89e5
AM
1821}
1822
1823static bfd_boolean
1824ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1825{
1826 if (note->descsz != 136)
1827 return FALSE;
1828
228e534f 1829 elf_tdata (abfd)->core->pid
bc989cdc 1830 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1831 elf_tdata (abfd)->core->program
d37c89e5 1832 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1833 elf_tdata (abfd)->core->command
d37c89e5
AM
1834 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1835
1836 return TRUE;
1837}
1838
183e98be
AM
1839static char *
1840ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1841 ...)
1842{
1843 switch (note_type)
1844 {
1845 default:
1846 return NULL;
1847
1848 case NT_PRPSINFO:
1849 {
9ef6d1e3 1850 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1851 va_list ap;
1852
1853 va_start (ap, note_type);
75cd47ed 1854 memset (data, 0, sizeof (data));
183e98be 1855 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1856#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1857 DIAGNOSTIC_PUSH;
be3e27bb 1858 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1859 -Wstringop-truncation:
1860 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1861 */
95da9854
L
1862 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1863#endif
183e98be 1864 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1865#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1866 DIAGNOSTIC_POP;
fe75810f 1867#endif
183e98be
AM
1868 va_end (ap);
1869 return elfcore_write_note (abfd, buf, bufsiz,
1870 "CORE", note_type, data, sizeof (data));
1871 }
1872
1873 case NT_PRSTATUS:
1874 {
1875 char data[504];
1876 va_list ap;
1877 long pid;
1878 int cursig;
1879 const void *greg;
1880
1881 va_start (ap, note_type);
1882 memset (data, 0, 112);
1883 pid = va_arg (ap, long);
1884 bfd_put_32 (abfd, pid, data + 32);
1885 cursig = va_arg (ap, int);
1886 bfd_put_16 (abfd, cursig, data + 12);
1887 greg = va_arg (ap, const void *);
1888 memcpy (data + 112, greg, 384);
1889 memset (data + 496, 0, 8);
1890 va_end (ap);
1891 return elfcore_write_note (abfd, buf, bufsiz,
1892 "CORE", note_type, data, sizeof (data));
1893 }
1894 }
1895}
1896
5d35169e
AM
1897/* Add extra PPC sections. */
1898
2cdcc330 1899static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1900{
07d6d2b8
AM
1901 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1902 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1903 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1904 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1905 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1906 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1907 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1908};
1909
7c8fe5c4
AM
1910enum _ppc64_sec_type {
1911 sec_normal = 0,
1912 sec_opd = 1,
1913 sec_toc = 2
1914};
1915
f0abc2a1
AM
1916struct _ppc64_elf_section_data
1917{
1918 struct bfd_elf_section_data elf;
411e1bfb 1919
f0abc2a1
AM
1920 union
1921 {
51aecdc5
AM
1922 /* An array with one entry for each opd function descriptor,
1923 and some spares since opd entries may be either 16 or 24 bytes. */
1924#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1925 struct _opd_sec_data
1926 {
1927 /* Points to the function code section for local opd entries. */
1928 asection **func_sec;
1929
1930 /* After editing .opd, adjust references to opd local syms. */
1931 long *adjust;
1932 } opd;
7c8fe5c4 1933
3a71aa26
AM
1934 /* An array for toc sections, indexed by offset/8. */
1935 struct _toc_sec_data
1936 {
1937 /* Specifies the relocation symbol index used at a given toc offset. */
1938 unsigned *symndx;
1939
1940 /* And the relocation addend. */
1941 bfd_vma *add;
1942 } toc;
7c8fe5c4
AM
1943 } u;
1944
1945 enum _ppc64_sec_type sec_type:2;
411e1bfb 1946
7c8fe5c4
AM
1947 /* Flag set when small branches are detected. Used to
1948 select suitable defaults for the stub group size. */
1949 unsigned int has_14bit_branch:1;
3e04d765
AM
1950
1951 /* Flag set when PLTCALL relocs are detected. */
1952 unsigned int has_pltcall:1;
066f4018
AM
1953
1954 /* Flag set when section has GOT relocations that can be optimised. */
1955 unsigned int has_gotrel:1;
f0abc2a1
AM
1956};
1957
1958#define ppc64_elf_section_data(sec) \
411e1bfb 1959 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
1960
1961static bfd_boolean
4ce794b7 1962ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1963{
f592407e
AM
1964 if (!sec->used_by_bfd)
1965 {
1966 struct _ppc64_elf_section_data *sdata;
1967 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1968
f592407e
AM
1969 sdata = bfd_zalloc (abfd, amt);
1970 if (sdata == NULL)
1971 return FALSE;
1972 sec->used_by_bfd = sdata;
1973 }
f0abc2a1
AM
1974
1975 return _bfd_elf_new_section_hook (abfd, sec);
1976}
4025353c 1977
74f0fb50 1978static struct _opd_sec_data *
4025353c
AM
1979get_opd_info (asection * sec)
1980{
1981 if (sec != NULL
1982 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 1983 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 1984 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
1985 return NULL;
1986}
90e3cdf2
JJ
1987\f
1988/* Parameters for the qsort hook. */
90e3cdf2 1989static bfd_boolean synthetic_relocatable;
cd285db5 1990static asection *synthetic_opd;
90e3cdf2 1991
699733f6 1992/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
1993
1994static int
1995compare_symbols (const void *ap, const void *bp)
1996{
2cdcc330
AM
1997 const asymbol *a = *(const asymbol **) ap;
1998 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 1999
699733f6
AM
2000 /* Section symbols first. */
2001 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2002 return -1;
699733f6 2003 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2004 return 1;
2005
699733f6 2006 /* then .opd symbols. */
cd285db5
AM
2007 if (synthetic_opd != NULL)
2008 {
2009 if (strcmp (a->section->name, ".opd") == 0
2010 && strcmp (b->section->name, ".opd") != 0)
2011 return -1;
2012 if (strcmp (a->section->name, ".opd") != 0
2013 && strcmp (b->section->name, ".opd") == 0)
2014 return 1;
2015 }
90e3cdf2 2016
699733f6 2017 /* then other code symbols. */
2cdcc330
AM
2018 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2019 == (SEC_CODE | SEC_ALLOC))
2020 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2021 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2022 return -1;
2023
2cdcc330
AM
2024 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2025 != (SEC_CODE | SEC_ALLOC))
2026 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2027 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2028 return 1;
2029
2030 if (synthetic_relocatable)
2031 {
2032 if (a->section->id < b->section->id)
2033 return -1;
2034
2035 if (a->section->id > b->section->id)
2036 return 1;
2037 }
2038
2039 if (a->value + a->section->vma < b->value + b->section->vma)
2040 return -1;
2041
2042 if (a->value + a->section->vma > b->value + b->section->vma)
2043 return 1;
2044
4d35a0aa
AM
2045 /* For syms with the same value, prefer strong dynamic global function
2046 syms over other syms. */
2047 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2048 return -1;
2049
2050 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2051 return 1;
2052
2053 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2054 return -1;
2055
2056 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2057 return 1;
2058
2059 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2060 return -1;
2061
2062 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2063 return 1;
2064
2065 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2066 return -1;
2067
2068 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2069 return 1;
2070
aaed6f5b 2071 return a > b;
90e3cdf2
JJ
2072}
2073
699733f6 2074/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2075
699733f6 2076static asymbol *
7292b3ac 2077sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 2078{
699733f6 2079 long mid;
90e3cdf2 2080
7292b3ac 2081 if (id == (unsigned) -1)
699733f6
AM
2082 {
2083 while (lo < hi)
2084 {
2085 mid = (lo + hi) >> 1;
2086 if (syms[mid]->value + syms[mid]->section->vma < value)
2087 lo = mid + 1;
2088 else if (syms[mid]->value + syms[mid]->section->vma > value)
2089 hi = mid;
2090 else
2091 return syms[mid];
2092 }
2093 }
2094 else
2095 {
2096 while (lo < hi)
2097 {
2098 mid = (lo + hi) >> 1;
2099 if (syms[mid]->section->id < id)
2100 lo = mid + 1;
2101 else if (syms[mid]->section->id > id)
2102 hi = mid;
2103 else if (syms[mid]->value < value)
2104 lo = mid + 1;
2105 else if (syms[mid]->value > value)
2106 hi = mid;
2107 else
2108 return syms[mid];
2109 }
2110 }
2111 return NULL;
90e3cdf2
JJ
2112}
2113
468392fb
AM
2114static bfd_boolean
2115section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2116{
2117 bfd_vma vma = *(bfd_vma *) ptr;
2118 return ((section->flags & SEC_ALLOC) != 0
2119 && section->vma <= vma
2120 && vma < section->vma + section->size);
2121}
2122
699733f6 2123/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2124 entry syms. Also generate @plt symbols for the glink branch table.
2125 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2126
2127static long
a7535cf3
AM
2128ppc64_elf_get_synthetic_symtab (bfd *abfd,
2129 long static_count, asymbol **static_syms,
2130 long dyn_count, asymbol **dyn_syms,
c9727e01 2131 asymbol **ret)
90e3cdf2
JJ
2132{
2133 asymbol *s;
0ccf57bd 2134 size_t i, j, count;
90e3cdf2 2135 char *names;
0ccf57bd 2136 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2137 asection *opd = NULL;
90e3cdf2 2138 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2139 asymbol **syms;
ee67d69a 2140 int abi = abiversion (abfd);
90e3cdf2
JJ
2141
2142 *ret = NULL;
2143
ee67d69a
AM
2144 if (abi < 2)
2145 {
2146 opd = bfd_get_section_by_name (abfd, ".opd");
2147 if (opd == NULL && abi == 1)
2148 return 0;
2149 }
90e3cdf2 2150
a5259595
AM
2151 syms = NULL;
2152 codesecsym = 0;
2153 codesecsymend = 0;
2154 secsymend = 0;
2155 opdsymend = 0;
2156 symcount = 0;
2157 if (opd != NULL)
c9727e01 2158 {
a5259595
AM
2159 symcount = static_count;
2160 if (!relocatable)
2161 symcount += dyn_count;
2162 if (symcount == 0)
2163 return 0;
c9727e01 2164
a5259595
AM
2165 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2166 if (syms == NULL)
2167 return -1;
90e3cdf2 2168
a5259595
AM
2169 if (!relocatable && static_count != 0 && dyn_count != 0)
2170 {
2171 /* Use both symbol tables. */
2172 memcpy (syms, static_syms, static_count * sizeof (*syms));
2173 memcpy (syms + static_count, dyn_syms,
2174 (dyn_count + 1) * sizeof (*syms));
2175 }
2176 else if (!relocatable && static_count == 0)
2177 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2178 else
2179 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2180
0ccf57bd
AM
2181 /* Trim uninteresting symbols. Interesting symbols are section,
2182 function, and notype symbols. */
2183 for (i = 0, j = 0; i < symcount; ++i)
2184 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2185 | BSF_RELC | BSF_SRELC)) == 0)
2186 syms[j++] = syms[i];
2187 symcount = j;
2188
a5259595
AM
2189 synthetic_relocatable = relocatable;
2190 synthetic_opd = opd;
2191 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2192
a5259595
AM
2193 if (!relocatable && symcount > 1)
2194 {
bfa5bd2a
PA
2195 /* Trim duplicate syms, since we may have merged the normal
2196 and dynamic symbols. Actually, we only care about syms
2197 that have different values, so trim any with the same
2198 value. Don't consider ifunc and ifunc resolver symbols
2199 duplicates however, because GDB wants to know whether a
2200 text symbol is an ifunc resolver. */
a5259595 2201 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2202 {
2203 const asymbol *s0 = syms[i - 1];
2204 const asymbol *s1 = syms[i];
2205
2206 if ((s0->value + s0->section->vma
2207 != s1->value + s1->section->vma)
2208 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2209 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2210 syms[j++] = syms[i];
2211 }
a5259595
AM
2212 symcount = j;
2213 }
699733f6 2214
a5259595
AM
2215 i = 0;
2216 /* Note that here and in compare_symbols we can't compare opd and
2217 sym->section directly. With separate debug info files, the
2218 symbols will be extracted from the debug file while abfd passed
2219 to this function is the real binary. */
0ccf57bd 2220 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2221 ++i;
2222 codesecsym = i;
2223
2224 for (; i < symcount; ++i)
2225 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2226 | SEC_THREAD_LOCAL))
2227 != (SEC_CODE | SEC_ALLOC))
2228 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2229 break;
2230 codesecsymend = i;
2231
2232 for (; i < symcount; ++i)
2233 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2234 break;
2235 secsymend = i;
2236
2237 for (; i < symcount; ++i)
2238 if (strcmp (syms[i]->section->name, ".opd") != 0)
2239 break;
2240 opdsymend = i;
2241
2242 for (; i < symcount; ++i)
2cdcc330
AM
2243 if (((syms[i]->section->flags
2244 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2245 != (SEC_CODE | SEC_ALLOC))
2246 break;
2247 symcount = i;
2248 }
c9727e01 2249 count = 0;
90e3cdf2 2250
699733f6 2251 if (relocatable)
90e3cdf2 2252 {
699733f6
AM
2253 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2254 arelent *r;
2255 size_t size;
0ccf57bd 2256 size_t relcount;
90e3cdf2 2257
468392fb
AM
2258 if (opdsymend == secsymend)
2259 goto done;
2260
699733f6 2261 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2262 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2263 if (relcount == 0)
c9727e01 2264 goto done;
90e3cdf2 2265
7356fed5
AM
2266 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2267 {
2268 count = -1;
2269 goto done;
2270 }
2271
699733f6 2272 size = 0;
595da8c5 2273 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2274 {
2275 asymbol *sym;
90e3cdf2 2276
595da8c5 2277 while (r < opd->relocation + relcount
699733f6
AM
2278 && r->address < syms[i]->value + opd->vma)
2279 ++r;
90e3cdf2 2280
595da8c5 2281 if (r == opd->relocation + relcount)
699733f6 2282 break;
90e3cdf2 2283
699733f6
AM
2284 if (r->address != syms[i]->value + opd->vma)
2285 continue;
90e3cdf2 2286
699733f6
AM
2287 if (r->howto->type != R_PPC64_ADDR64)
2288 continue;
90e3cdf2 2289
699733f6
AM
2290 sym = *r->sym_ptr_ptr;
2291 if (!sym_exists_at (syms, opdsymend, symcount,
2292 sym->section->id, sym->value + r->addend))
2293 {
2294 ++count;
2295 size += sizeof (asymbol);
2296 size += strlen (syms[i]->name) + 2;
2297 }
2298 }
90e3cdf2 2299
c4b0b099
AM
2300 if (size == 0)
2301 goto done;
699733f6
AM
2302 s = *ret = bfd_malloc (size);
2303 if (s == NULL)
2304 {
7356fed5 2305 count = -1;
c9727e01 2306 goto done;
699733f6 2307 }
90e3cdf2 2308
699733f6 2309 names = (char *) (s + count);
90e3cdf2 2310
595da8c5 2311 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2312 {
699733f6 2313 asymbol *sym;
90e3cdf2 2314
595da8c5 2315 while (r < opd->relocation + relcount
699733f6
AM
2316 && r->address < syms[i]->value + opd->vma)
2317 ++r;
90e3cdf2 2318
595da8c5 2319 if (r == opd->relocation + relcount)
699733f6
AM
2320 break;
2321
2322 if (r->address != syms[i]->value + opd->vma)
2323 continue;
2324
2325 if (r->howto->type != R_PPC64_ADDR64)
2326 continue;
90e3cdf2 2327
699733f6
AM
2328 sym = *r->sym_ptr_ptr;
2329 if (!sym_exists_at (syms, opdsymend, symcount,
2330 sym->section->id, sym->value + r->addend))
2331 {
2332 size_t len;
2333
2334 *s = *syms[i];
6ba2a415 2335 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2336 s->section = sym->section;
2337 s->value = sym->value + r->addend;
2338 s->name = names;
2339 *names++ = '.';
2340 len = strlen (syms[i]->name);
2341 memcpy (names, syms[i]->name, len + 1);
2342 names += len + 1;
6f610d07
UW
2343 /* Have udata.p point back to the original symbol this
2344 synthetic symbol was derived from. */
2345 s->udata.p = syms[i];
699733f6
AM
2346 s++;
2347 }
2348 }
2349 }
2350 else
90e3cdf2 2351 {
468392fb 2352 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2353 bfd_byte *contents = NULL;
699733f6 2354 size_t size;
0ccf57bd 2355 size_t plt_count = 0;
468392fb
AM
2356 bfd_vma glink_vma = 0, resolv_vma = 0;
2357 asection *dynamic, *glink = NULL, *relplt = NULL;
2358 arelent *p;
90e3cdf2 2359
ee67d69a 2360 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2361 {
c4b0b099
AM
2362 free_contents_and_exit_err:
2363 count = -1;
ee67d69a 2364 free_contents_and_exit:
699733f6 2365 if (contents)
ee67d69a 2366 free (contents);
c9727e01 2367 goto done;
699733f6 2368 }
90e3cdf2 2369
699733f6
AM
2370 size = 0;
2371 for (i = secsymend; i < opdsymend; ++i)
2372 {
2373 bfd_vma ent;
90e3cdf2 2374
5ef11c02
AM
2375 /* Ignore bogus symbols. */
2376 if (syms[i]->value > opd->size - 8)
2377 continue;
2378
699733f6
AM
2379 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2380 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2381 {
2382 ++count;
2383 size += sizeof (asymbol);
2384 size += strlen (syms[i]->name) + 2;
2385 }
2386 }
90e3cdf2 2387
468392fb 2388 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2389 if (dyn_count != 0
2390 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2391 {
2392 bfd_byte *dynbuf, *extdyn, *extdynend;
2393 size_t extdynsize;
2394 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2395
2396 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2397 goto free_contents_and_exit_err;
468392fb
AM
2398
2399 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2400 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2401
2402 extdyn = dynbuf;
2403 extdynend = extdyn + dynamic->size;
2404 for (; extdyn < extdynend; extdyn += extdynsize)
2405 {
2406 Elf_Internal_Dyn dyn;
2407 (*swap_dyn_in) (abfd, extdyn, &dyn);
2408
2409 if (dyn.d_tag == DT_NULL)
2410 break;
2411
2412 if (dyn.d_tag == DT_PPC64_GLINK)
2413 {
9e390558
AM
2414 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2415 See comment in ppc64_elf_finish_dynamic_sections. */
2416 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2417 /* The .glink section usually does not survive the final
2418 link; search for the section (usually .text) where the
2419 glink stubs now reside. */
2420 glink = bfd_sections_find_if (abfd, section_covers_vma,
2421 &glink_vma);
2422 break;
2423 }
2424 }
2425
2426 free (dynbuf);
2427 }
2428
2429 if (glink != NULL)
2430 {
2431 /* Determine __glink trampoline by reading the relative branch
2432 from the first glink stub. */
2433 bfd_byte buf[4];
b9e5796b
AM
2434 unsigned int off = 0;
2435
2436 while (bfd_get_section_contents (abfd, glink, buf,
2437 glink_vma + off - glink->vma, 4))
468392fb
AM
2438 {
2439 unsigned int insn = bfd_get_32 (abfd, buf);
2440 insn ^= B_DOT;
2441 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2442 {
2cdcc330
AM
2443 resolv_vma
2444 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2445 break;
2446 }
2447 off += 4;
2448 if (off > 4)
2449 break;
468392fb
AM
2450 }
2451
2452 if (resolv_vma)
2453 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2454
066ee829
AM
2455 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2456 if (relplt != NULL)
2457 {
2458 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2459 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2460 goto free_contents_and_exit_err;
68ffbac6 2461
066ee829
AM
2462 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2463 size += plt_count * sizeof (asymbol);
468392fb 2464
066ee829
AM
2465 p = relplt->relocation;
2466 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2467 {
2468 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2469 if (p->addend != 0)
2470 size += sizeof ("+0x") - 1 + 16;
2471 }
066ee829 2472 }
468392fb
AM
2473 }
2474
c4b0b099
AM
2475 if (size == 0)
2476 goto free_contents_and_exit;
699733f6
AM
2477 s = *ret = bfd_malloc (size);
2478 if (s == NULL)
c4b0b099 2479 goto free_contents_and_exit_err;
90e3cdf2 2480
468392fb 2481 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2482
699733f6 2483 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2484 {
699733f6 2485 bfd_vma ent;
90e3cdf2 2486
5ef11c02
AM
2487 if (syms[i]->value > opd->size - 8)
2488 continue;
2489
699733f6
AM
2490 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2491 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2492 {
0ccf57bd 2493 size_t lo, hi;
699733f6 2494 size_t len;
c9727e01 2495 asection *sec = abfd->sections;
90e3cdf2 2496
699733f6
AM
2497 *s = *syms[i];
2498 lo = codesecsym;
2499 hi = codesecsymend;
2500 while (lo < hi)
2501 {
0ccf57bd 2502 size_t mid = (lo + hi) >> 1;
699733f6
AM
2503 if (syms[mid]->section->vma < ent)
2504 lo = mid + 1;
2505 else if (syms[mid]->section->vma > ent)
2506 hi = mid;
2507 else
c9727e01
AM
2508 {
2509 sec = syms[mid]->section;
2510 break;
2511 }
699733f6
AM
2512 }
2513
c9727e01 2514 if (lo >= hi && lo > codesecsym)
699733f6 2515 sec = syms[lo - 1]->section;
699733f6
AM
2516
2517 for (; sec != NULL; sec = sec->next)
2518 {
2519 if (sec->vma > ent)
2520 break;
63524580
JK
2521 /* SEC_LOAD may not be set if SEC is from a separate debug
2522 info file. */
2523 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2524 break;
2525 if ((sec->flags & SEC_CODE) != 0)
2526 s->section = sec;
2527 }
6ba2a415 2528 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2529 s->value = ent - s->section->vma;
2530 s->name = names;
2531 *names++ = '.';
2532 len = strlen (syms[i]->name);
2533 memcpy (names, syms[i]->name, len + 1);
2534 names += len + 1;
6f610d07
UW
2535 /* Have udata.p point back to the original symbol this
2536 synthetic symbol was derived from. */
2537 s->udata.p = syms[i];
699733f6 2538 s++;
90e3cdf2 2539 }
90e3cdf2 2540 }
699733f6 2541 free (contents);
468392fb
AM
2542
2543 if (glink != NULL && relplt != NULL)
2544 {
2545 if (resolv_vma)
2546 {
2547 /* Add a symbol for the main glink trampoline. */
86a4952b 2548 memset (s, 0, sizeof *s);
468392fb 2549 s->the_bfd = abfd;
6ba2a415 2550 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2551 s->section = glink;
2552 s->value = resolv_vma - glink->vma;
2553 s->name = names;
2cdcc330
AM
2554 memcpy (names, "__glink_PLTresolve",
2555 sizeof ("__glink_PLTresolve"));
468392fb
AM
2556 names += sizeof ("__glink_PLTresolve");
2557 s++;
2558 count++;
2559 }
2560
2561 /* FIXME: It would be very much nicer to put sym@plt on the
2562 stub rather than on the glink branch table entry. The
2563 objdump disassembler would then use a sensible symbol
2564 name on plt calls. The difficulty in doing so is
2565 a) finding the stubs, and,
2566 b) matching stubs against plt entries, and,
2567 c) there can be multiple stubs for a given plt entry.
2568
2569 Solving (a) could be done by code scanning, but older
2570 ppc64 binaries used different stubs to current code.
2571 (b) is the tricky one since you need to known the toc
2572 pointer for at least one function that uses a pic stub to
2573 be able to calculate the plt address referenced.
2574 (c) means gdb would need to set multiple breakpoints (or
2575 find the glink branch itself) when setting breakpoints
2576 for pending shared library loads. */
2577 p = relplt->relocation;
2578 for (i = 0; i < plt_count; i++, p++)
2579 {
2580 size_t len;
2581
2582 *s = **p->sym_ptr_ptr;
2583 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2584 we are defining a symbol, ensure one of them is set. */
2585 if ((s->flags & BSF_LOCAL) == 0)
2586 s->flags |= BSF_GLOBAL;
6ba2a415 2587 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2588 s->section = glink;
2589 s->value = glink_vma - glink->vma;
2590 s->name = names;
2591 s->udata.p = NULL;
2592 len = strlen ((*p->sym_ptr_ptr)->name);
2593 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2594 names += len;
e054468f
AM
2595 if (p->addend != 0)
2596 {
2597 memcpy (names, "+0x", sizeof ("+0x") - 1);
2598 names += sizeof ("+0x") - 1;
2599 bfd_sprintf_vma (abfd, names, p->addend);
2600 names += strlen (names);
2601 }
468392fb
AM
2602 memcpy (names, "@plt", sizeof ("@plt"));
2603 names += sizeof ("@plt");
2604 s++;
b9e5796b
AM
2605 if (abi < 2)
2606 {
2607 glink_vma += 8;
2608 if (i >= 0x8000)
2609 glink_vma += 4;
2610 }
2611 else
468392fb
AM
2612 glink_vma += 4;
2613 }
2614 count += plt_count;
2615 }
90e3cdf2
JJ
2616 }
2617
c9727e01 2618 done:
a7535cf3 2619 free (syms);
90e3cdf2
JJ
2620 return count;
2621}
5bd4f169 2622\f
65f38f15
AM
2623/* The following functions are specific to the ELF linker, while
2624 functions above are used generally. Those named ppc64_elf_* are
2625 called by the main ELF linker code. They appear in this file more
2626 or less in the order in which they are called. eg.
2627 ppc64_elf_check_relocs is called early in the link process,
2628 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2629 called.
2630
2631 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2632 functions have both a function code symbol and a function descriptor
2633 symbol. A call to foo in a relocatable object file looks like:
2634
2635 . .text
2636 . x:
2637 . bl .foo
2638 . nop
2639
2640 The function definition in another object file might be:
2641
2642 . .section .opd
2643 . foo: .quad .foo
2644 . .quad .TOC.@tocbase
2645 . .quad 0
2646 .
2647 . .text
2648 . .foo: blr
2649
2650 When the linker resolves the call during a static link, the branch
2651 unsurprisingly just goes to .foo and the .opd information is unused.
2652 If the function definition is in a shared library, things are a little
2653 different: The call goes via a plt call stub, the opd information gets
2654 copied to the plt, and the linker patches the nop.
2655
2656 . x:
2657 . bl .foo_stub
2658 . ld 2,40(1)
2659 .
2660 .
2661 . .foo_stub:
71a39c98
AM
2662 . std 2,40(1) # in practice, the call stub
2663 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2664 . addi 11,11,Lfoo@toc@l # this is the general idea
2665 . ld 12,0(11)
2666 . ld 2,8(11)
2667 . mtctr 12
2668 . ld 11,16(11)
e86ce104
AM
2669 . bctr
2670 .
2671 . .section .plt
2672 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2673
2674 The "reloc ()" notation is supposed to indicate that the linker emits
2675 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2676 copying.
2677
2678 What are the difficulties here? Well, firstly, the relocations
2679 examined by the linker in check_relocs are against the function code
2680 sym .foo, while the dynamic relocation in the plt is emitted against
2681 the function descriptor symbol, foo. Somewhere along the line, we need
2682 to carefully copy dynamic link information from one symbol to the other.
2683 Secondly, the generic part of the elf linker will make .foo a dynamic
2684 symbol as is normal for most other backends. We need foo dynamic
2685 instead, at least for an application final link. However, when
2686 creating a shared library containing foo, we need to have both symbols
2687 dynamic so that references to .foo are satisfied during the early
2688 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2689 definition from some other object, eg. a static library.
2690
2691 Update: As of August 2004, we support a new convention. Function
2692 calls may use the function descriptor symbol, ie. "bl foo". This
2693 behaves exactly as "bl .foo". */
65f38f15 2694
7c8bbca5
AM
2695/* Of those relocs that might be copied as dynamic relocs, this
2696 function selects those that must be copied when linking a shared
2697 library or PIE, even when the symbol is local. */
65f38f15 2698
1d483afe
AM
2699static int
2700must_be_dyn_reloc (struct bfd_link_info *info,
2701 enum elf_ppc64_reloc_type r_type)
2702{
2703 switch (r_type)
2704 {
2705 default:
7c8bbca5
AM
2706 /* Only relative relocs can be resolved when the object load
2707 address isn't fixed. DTPREL64 is excluded because the
2708 dynamic linker needs to differentiate global dynamic from
2709 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2710 return 1;
2711
2712 case R_PPC64_REL32:
2713 case R_PPC64_REL64:
2714 case R_PPC64_REL30:
2715 return 0;
2716
2717 case R_PPC64_TPREL16:
2718 case R_PPC64_TPREL16_LO:
2719 case R_PPC64_TPREL16_HI:
2720 case R_PPC64_TPREL16_HA:
2721 case R_PPC64_TPREL16_DS:
2722 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2723 case R_PPC64_TPREL16_HIGH:
2724 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2725 case R_PPC64_TPREL16_HIGHER:
2726 case R_PPC64_TPREL16_HIGHERA:
2727 case R_PPC64_TPREL16_HIGHEST:
2728 case R_PPC64_TPREL16_HIGHESTA:
2729 case R_PPC64_TPREL64:
7c8bbca5
AM
2730 /* These relocations are relative but in a shared library the
2731 linker doesn't know the thread pointer base. */
2732 return bfd_link_dll (info);
1d483afe
AM
2733 }
2734}
65f38f15 2735
f4656909
AM
2736/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2737 copying dynamic variables from a shared lib into an app's dynbss
2738 section, and instead use a dynamic relocation to point into the
5d35169e
AM
2739 shared lib. With code that gcc generates, it's vital that this be
2740 enabled; In the PowerPC64 ABI, the address of a function is actually
2741 the address of a function descriptor, which resides in the .opd
2742 section. gcc uses the descriptor directly rather than going via the
2743 GOT as some other ABI's do, which means that initialized function
2744 pointers must reference the descriptor. Thus, a function pointer
2745 initialized to the address of a function in a shared library will
2746 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 2747 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
2748 presents a problem as a plt entry for that function is also
2749 initialized from the function descriptor symbol and the copy reloc
2750 may not be initialized first. */
a23b6845 2751#define ELIMINATE_COPY_RELOCS 1
f4656909 2752
721956f4 2753/* Section name for stubs is the associated section name plus this
29942be8
NC
2754 string. */
2755#define STUB_SUFFIX ".stub"
721956f4
AM
2756
2757/* Linker stubs.
2758 ppc_stub_long_branch:
2759 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2760 destination, but a 24 bit branch in a stub section will reach.
2761 . b dest
2762
2763 ppc_stub_plt_branch:
2764 Similar to the above, but a 24 bit branch in the stub section won't
2765 reach its destination.
71a39c98
AM
2766 . addis %r11,%r2,xxx@toc@ha
2767 . ld %r12,xxx@toc@l(%r11)
2768 . mtctr %r12
721956f4
AM
2769 . bctr
2770
2771 ppc_stub_plt_call:
2c66dc6c
AM
2772 Used to call a function in a shared library. If it so happens that
2773 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2774 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2775 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2776 . addis %r11,%r2,xxx@toc@ha
2777 . ld %r12,xxx+0@toc@l(%r11)
2778 . mtctr %r12
2779 . ld %r2,xxx+8@toc@l(%r11)
2780 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2781 . bctr
ad8e1ba5
AM
2782
2783 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2784 code to adjust the value and save r2 to support multiple toc sections.
2785 A ppc_stub_long_branch with an r2 offset looks like:
2786 . std %r2,40(%r1)
2787 . addis %r2,%r2,off@ha
2788 . addi %r2,%r2,off@l
2789 . b dest
2790
2791 A ppc_stub_plt_branch with an r2 offset looks like:
2792 . std %r2,40(%r1)
71a39c98
AM
2793 . addis %r11,%r2,xxx@toc@ha
2794 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
2795 . addis %r2,%r2,off@ha
2796 . addi %r2,%r2,off@l
71a39c98 2797 . mtctr %r12
ad8e1ba5 2798 . bctr
ac2df442 2799
05d0e962
AM
2800 All of the above stubs are shown as their ELFv1 variants. ELFv2
2801 variants exist too, simpler for plt calls since a new toc pointer
2802 and static chain are not loaded by the stub. In addition, ELFv2
2803 has some more complex stubs to handle calls marked with NOTOC
2804 relocs from functions where r2 is not a valid toc pointer. These
2805 come in two flavours, the ones shown below, and _both variants that
2806 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2807 one call is from a function where r2 is used as the toc pointer but
2808 needs a toc adjusting stub for small-model multi-toc, and another
2809 call is from a function where r2 is not valid.
2810 ppc_stub_long_branch_notoc:
2811 . mflr %r12
2812 . bcl 20,31,1f
2813 . 1:
2814 . mflr %r11
2815 . mtlr %r12
2cdcc330
AM
2816 . addis %r12,%r11,dest-1b@ha
2817 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2818 . b dest
2819
2820 ppc_stub_plt_branch_notoc:
2821 . mflr %r12
2822 . bcl 20,31,1f
2823 . 1:
2824 . mflr %r11
2825 . mtlr %r12
2826 . lis %r12,xxx-1b@highest
f891966f 2827 . ori %r12,%r12,xxx-1b@higher
05d0e962 2828 . sldi %r12,%r12,32
f891966f 2829 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2830 . ori %r12,%r12,xxx-1b@l
2831 . add %r12,%r11,%r12
2832 . mtctr %r12
2833 . bctr
2834
2835 ppc_stub_plt_call_notoc:
2836 . mflr %r12
2837 . bcl 20,31,1f
2838 . 1:
2839 . mflr %r11
2840 . mtlr %r12
2841 . lis %r12,xxx-1b@highest
f891966f 2842 . ori %r12,%r12,xxx-1b@higher
05d0e962 2843 . sldi %r12,%r12,32
f891966f 2844 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2845 . ori %r12,%r12,xxx-1b@l
2846 . ldx %r12,%r11,%r12
2847 . mtctr %r12
2848 . bctr
2849
04bdff6a
AM
2850 There are also ELFv1 powerxx variants of these stubs.
2851 ppc_stub_long_branch_notoc:
d4b87b1e 2852 . pla %r12,dest@pcrel
04bdff6a
AM
2853 . b dest
2854 ppc_stub_plt_branch_notoc:
2855 . lis %r11,(dest-1f)@highesta34
2856 . ori %r11,%r11,(dest-1f)@highera34
2857 . sldi %r11,%r11,34
d4b87b1e 2858 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2859 . add %r12,%r11,%r12
2860 . mtctr %r12
2861 . bctr
2862 ppc_stub_plt_call_notoc:
2863 . lis %r11,(xxx-1f)@highesta34
2864 . ori %r11,%r11,(xxx-1f)@highera34
2865 . sldi %r11,%r11,34
d4b87b1e 2866 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2867 . ldx %r12,%r11,%r12
2868 . mtctr %r12
2869 . bctr
2870
05d0e962
AM
2871 In cases where the high instructions would add zero, they are
2872 omitted and following instructions modified in some cases.
04bdff6a
AM
2873 For example, a powerxx ppc_stub_plt_call_notoc might simplify down
2874 to
2875 . pld %r12,xxx@pcrel
2876 . mtctr %r12
2877 . bctr
05d0e962
AM
2878
2879 For a given stub group (a set of sections all using the same toc
2880 pointer value) there will be just one stub type used for any
2881 particular function symbol. For example, if printf is called from
2882 code with the tocsave optimization (ie. r2 saved in function
2883 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2884 and from other code without the tocsave optimization requiring a
2885 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2886 type will be created. Calls with the tocsave optimization will
2887 enter this stub after the instruction saving r2. A similar
2888 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2889 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2890 to call an external function like printf. If other calls to printf
2891 require a ppc_stub_plt_call linkage stub then a single
2892 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2893 call. If other calls to printf require a ppc_stub_plt_call_r2save
2894 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2895 be created and calls not requiring r2 to be saved will enter the
2896 stub after the r2 save instruction. There is an analogous
2897 hierarchy of long branch and plt branch stubs for local call
2898 linkage. */
721956f4 2899
2cdcc330
AM
2900enum ppc_stub_type
2901{
721956f4
AM
2902 ppc_stub_none,
2903 ppc_stub_long_branch,
ad8e1ba5 2904 ppc_stub_long_branch_r2off,
05d0e962
AM
2905 ppc_stub_long_branch_notoc,
2906 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2907 ppc_stub_plt_branch,
ad8e1ba5 2908 ppc_stub_plt_branch_r2off,
05d0e962
AM
2909 ppc_stub_plt_branch_notoc,
2910 ppc_stub_plt_branch_both,
794e51c0 2911 ppc_stub_plt_call,
7341d5e2 2912 ppc_stub_plt_call_r2save,
05d0e962
AM
2913 ppc_stub_plt_call_notoc,
2914 ppc_stub_plt_call_both,
a4b6fadd
AM
2915 ppc_stub_global_entry,
2916 ppc_stub_save_res
721956f4
AM
2917};
2918
6f20ed8a
AM
2919/* Information on stub grouping. */
2920struct map_stub
2921{
2922 /* The stub section. */
2923 asection *stub_sec;
2924 /* This is the section to which stubs in the group will be attached. */
2925 asection *link_sec;
a4b6fadd
AM
2926 /* Next group. */
2927 struct map_stub *next;
2928 /* Whether to emit a copy of register save/restore functions in this
2929 group. */
2930 int needs_save_res;
df136d64
AM
2931 /* Current offset within stubs after the insn restoring lr in a
2932 _notoc or _both stub using bcl for pc-relative addressing, or
2933 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2934 unsigned int lr_restore;
2935 /* Accumulated size of EH info emitted to describe return address
2936 if stubs modify lr. Does not include 17 byte FDE header. */
2937 unsigned int eh_size;
2938 /* Offset in glink_eh_frame to the start of EH info for this group. */
2939 unsigned int eh_base;
6f20ed8a
AM
2940};
2941
2cdcc330
AM
2942struct ppc_stub_hash_entry
2943{
721956f4
AM
2944 /* Base hash table entry structure. */
2945 struct bfd_hash_entry root;
2946
ad8e1ba5
AM
2947 enum ppc_stub_type stub_type;
2948
6f20ed8a
AM
2949 /* Group information. */
2950 struct map_stub *group;
721956f4
AM
2951
2952 /* Offset within stub_sec of the beginning of this stub. */
2953 bfd_vma stub_offset;
2954
2955 /* Given the symbol's value and its section we can determine its final
2956 value when building the stubs (so the stub knows where to jump. */
2957 bfd_vma target_value;
2958 asection *target_section;
2959
721956f4
AM
2960 /* The symbol table entry, if any, that this was derived from. */
2961 struct ppc_link_hash_entry *h;
e054468f 2962 struct plt_entry *plt_ent;
721956f4 2963
2d7ad24e
AM
2964 /* Symbol type. */
2965 unsigned char symtype;
2966
6911b7dc
AM
2967 /* Symbol st_other. */
2968 unsigned char other;
721956f4
AM
2969};
2970
2cdcc330
AM
2971struct ppc_branch_hash_entry
2972{
721956f4
AM
2973 /* Base hash table entry structure. */
2974 struct bfd_hash_entry root;
2975
c456f082 2976 /* Offset within branch lookup table. */
721956f4
AM
2977 unsigned int offset;
2978
2979 /* Generation marker. */
2980 unsigned int iter;
2981};
65f38f15 2982
19e08130
AM
2983/* Used to track dynamic relocations for local symbols. */
2984struct ppc_dyn_relocs
2985{
2986 struct ppc_dyn_relocs *next;
2987
2988 /* The input section of the reloc. */
2989 asection *sec;
2990
2991 /* Total number of relocs copied for the input section. */
2992 unsigned int count : 31;
2993
2994 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2995 unsigned int ifunc : 1;
2996};
2997
65f38f15
AM
2998struct ppc_link_hash_entry
2999{
3000 struct elf_link_hash_entry elf;
3001
2cdcc330
AM
3002 union
3003 {
b3fac117
AM
3004 /* A pointer to the most recently used stub hash entry against this
3005 symbol. */
3006 struct ppc_stub_hash_entry *stub_cache;
3007
3008 /* A pointer to the next symbol starting with a '.' */
3009 struct ppc_link_hash_entry *next_dot_sym;
3010 } u;
721956f4 3011
65f38f15 3012 /* Track dynamic relocs copied for this symbol. */
6061a67d 3013 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3014
721956f4 3015 /* Link between function code and descriptor symbols. */
34814b9f 3016 struct ppc_link_hash_entry *oh;
721956f4 3017
e86ce104
AM
3018 /* Flag function code and descriptor symbols. */
3019 unsigned int is_func:1;
3020 unsigned int is_func_descriptor:1;
908b32fc 3021 unsigned int fake:1;
411e1bfb 3022
c5614fa4
AM
3023 /* Whether global opd/toc sym has been adjusted or not.
3024 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3025 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3026 unsigned int adjust_done:1;
3027
a4b6fadd
AM
3028 /* Set if this is an out-of-line register save/restore function,
3029 with non-standard calling convention. */
3030 unsigned int save_res:1;
3031
8b5f1ed8
AM
3032 /* Set if a duplicate symbol with non-zero localentry is detected,
3033 even when the duplicate symbol does not provide a definition. */
3034 unsigned int non_zero_localentry:1;
3035
411e1bfb 3036 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3037 Bits are or'd into the mask as the corresponding relocs are
3038 encountered during check_relocs, with TLS_TLS being set when any
3039 of the other TLS bits are set. tls_optimize clears bits when
3040 optimizing to indicate the corresponding GOT entry type is not
3041 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3042 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3043 These flags are also kept for local symbols. */
3044#define TLS_TLS 1 /* Any TLS reloc. */
3045#define TLS_GD 2 /* GD reloc. */
3046#define TLS_LD 4 /* LD reloc. */
3047#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3048#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3049#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3050#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
37da22e5 3051#define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
f961d9dd 3052 unsigned char tls_mask;
37da22e5
AM
3053
3054 /* The above field is also used to mark function symbols. In which
3055 case TLS_TLS will be 0. */
3056#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3057#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3058#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3059};
3060
3061/* ppc64 ELF linker hash table. */
3062
3063struct ppc_link_hash_table
3064{
3065 struct elf_link_hash_table elf;
3066
721956f4
AM
3067 /* The stub hash table. */
3068 struct bfd_hash_table stub_hash_table;
3069
3070 /* Another hash table for plt_branch stubs. */
3071 struct bfd_hash_table branch_hash_table;
3072
3b421ab3
AM
3073 /* Hash table for function prologue tocsave. */
3074 htab_t tocsave_htab;
3075
e7d1c40c
AM
3076 /* Various options and other info passed from the linker. */
3077 struct ppc64_elf_params *params;
721956f4 3078
6f20ed8a
AM
3079 /* The size of sec_info below. */
3080 unsigned int sec_info_arr_size;
3081
3082 /* Per-section array of extra section info. Done this way rather
3083 than as part of ppc64_elf_section_data so we have the info for
3084 non-ppc64 sections. */
3085 struct
3086 {
3087 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3088 bfd_vma toc_off;
6f20ed8a
AM
3089
3090 union
3091 {
3092 /* The section group that this section belongs to. */
3093 struct map_stub *group;
3094 /* A temp section list pointer. */
3095 asection *list;
3096 } u;
3097 } *sec_info;
721956f4 3098
a4b6fadd
AM
3099 /* Linked list of groups. */
3100 struct map_stub *group;
3101
ad8e1ba5
AM
3102 /* Temp used when calculating TOC pointers. */
3103 bfd_vma toc_curr;
bf102f86
AM
3104 bfd *toc_bfd;
3105 asection *toc_first_sec;
ad8e1ba5 3106
b3fac117
AM
3107 /* Used when adding symbols. */
3108 struct ppc_link_hash_entry *dot_syms;
3109
33e44f2e 3110 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3111 asection *glink;
9e390558 3112 asection *global_entry;
82bd7b59 3113 asection *sfpr;
2d7ad24e
AM
3114 asection *pltlocal;
3115 asection *relpltlocal;
4ce794b7
AM
3116 asection *brlt;
3117 asection *relbrlt;
58d180e8 3118 asection *glink_eh_frame;
ec338859 3119
8387904d
AM
3120 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3121 struct ppc_link_hash_entry *tls_get_addr;
3122 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3123
927be08e
AM
3124 /* The size of reliplt used by got entry relocs. */
3125 bfd_size_type got_reli_size;
3126
9b5ecbd0 3127 /* Statistics. */
7341d5e2 3128 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3129
ee75fd95
AM
3130 /* Number of stubs against global syms. */
3131 unsigned long stub_globals;
3132
ee67d69a
AM
3133 /* Set if we're linking code with function descriptors. */
3134 unsigned int opd_abi:1;
3135
4c52953f 3136 /* Support for multiple toc sections. */
33c0ec9d 3137 unsigned int do_multi_toc:1;
4c52953f 3138 unsigned int multi_toc_needed:1;
927be08e 3139 unsigned int second_toc_pass:1;
67f0cbdb 3140 unsigned int do_toc_opt:1;
4c52953f 3141
9a23f96e
AM
3142 /* Set if tls optimization is enabled. */
3143 unsigned int do_tls_opt:1;
3144
3e04d765
AM
3145 /* Set if inline plt calls should be converted to direct calls. */
3146 unsigned int can_convert_all_inline_plt:1;
3147
5d1634d7 3148 /* Set on error. */
99877b66 3149 unsigned int stub_error:1;
721956f4 3150
8c5b4e52
AM
3151 /* Whether func_desc_adjust needs to be run over symbols. */
3152 unsigned int need_func_desc_adj:1;
721956f4 3153
82e66161
AM
3154 /* Whether there exist local gnu indirect function resolvers,
3155 referenced by dynamic relocations. */
3156 unsigned int local_ifunc_resolver:1;
3157 unsigned int maybe_local_ifunc_resolver:1;
3158
f378ab09
AM
3159 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3160 unsigned int has_plt_localentry0:1;
3161
5663e321
AM
3162 /* Whether calls are made via the PLT from NOTOC functions. */
3163 unsigned int notoc_plt:1;
3164
04bdff6a
AM
3165 /* Whether to use powerxx instructions in linkage stubs. */
3166 unsigned int powerxx_stubs:1;
3167
721956f4
AM
3168 /* Incremented every time we size stubs. */
3169 unsigned int stub_iteration;
5d1634d7 3170
87d72d41
AM
3171 /* Small local sym cache. */
3172 struct sym_cache sym_cache;
65f38f15
AM
3173};
3174
4c52953f
AM
3175/* Rename some of the generic section flags to better document how they
3176 are used here. */
b0dddeec
AM
3177
3178/* Nonzero if this section has TLS related relocations. */
3179#define has_tls_reloc sec_flg0
3180
23cedd1d 3181/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
3182#define has_tls_get_addr_call sec_flg1
3183
3184/* Nonzero if this section has any toc or got relocs. */
3185#define has_toc_reloc sec_flg2
3186
3187/* Nonzero if this section has a call to another section that uses
3188 the toc or got. */
d77c8a4b 3189#define makes_toc_func_call sec_flg3
b0dddeec
AM
3190
3191/* Recursion protection when determining above flag. */
d77c8a4b 3192#define call_check_in_progress sec_flg4
70cc837d 3193#define call_check_done sec_flg5
4c52953f 3194
65f38f15
AM
3195/* Get the ppc64 ELF linker hash table from a link_info structure. */
3196
3197#define ppc_hash_table(p) \
4dfe6ac6
NC
3198 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3199 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3200
721956f4
AM
3201#define ppc_stub_hash_lookup(table, string, create, copy) \
3202 ((struct ppc_stub_hash_entry *) \
3203 bfd_hash_lookup ((table), (string), (create), (copy)))
3204
3205#define ppc_branch_hash_lookup(table, string, create, copy) \
3206 ((struct ppc_branch_hash_entry *) \
3207 bfd_hash_lookup ((table), (string), (create), (copy)))
3208
3209/* Create an entry in the stub hash table. */
3210
3211static struct bfd_hash_entry *
4ce794b7
AM
3212stub_hash_newfunc (struct bfd_hash_entry *entry,
3213 struct bfd_hash_table *table,
3214 const char *string)
721956f4
AM
3215{
3216 /* Allocate the structure if it has not already been allocated by a
3217 subclass. */
3218 if (entry == NULL)
3219 {
3220 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3221 if (entry == NULL)
3222 return entry;
3223 }
3224
3225 /* Call the allocation method of the superclass. */
3226 entry = bfd_hash_newfunc (entry, table, string);
3227 if (entry != NULL)
3228 {
3229 struct ppc_stub_hash_entry *eh;
3230
3231 /* Initialize the local fields. */
3232 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3233 eh->stub_type = ppc_stub_none;
6f20ed8a 3234 eh->group = NULL;
721956f4
AM
3235 eh->stub_offset = 0;
3236 eh->target_value = 0;
3237 eh->target_section = NULL;
721956f4 3238 eh->h = NULL;
6911b7dc 3239 eh->plt_ent = NULL;
6911b7dc 3240 eh->other = 0;
721956f4
AM
3241 }
3242
3243 return entry;
3244}
3245
3246/* Create an entry in the branch hash table. */
3247
3248static struct bfd_hash_entry *
4ce794b7
AM
3249branch_hash_newfunc (struct bfd_hash_entry *entry,
3250 struct bfd_hash_table *table,
3251 const char *string)
721956f4
AM
3252{
3253 /* Allocate the structure if it has not already been allocated by a
3254 subclass. */
3255 if (entry == NULL)
3256 {
3257 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3258 if (entry == NULL)
3259 return entry;
3260 }
3261
3262 /* Call the allocation method of the superclass. */
3263 entry = bfd_hash_newfunc (entry, table, string);
3264 if (entry != NULL)
3265 {
3266 struct ppc_branch_hash_entry *eh;
3267
3268 /* Initialize the local fields. */
3269 eh = (struct ppc_branch_hash_entry *) entry;
3270 eh->offset = 0;
3271 eh->iter = 0;
3272 }
3273
3274 return entry;
3275}
3276
65f38f15
AM
3277/* Create an entry in a ppc64 ELF linker hash table. */
3278
3279static struct bfd_hash_entry *
4ce794b7
AM
3280link_hash_newfunc (struct bfd_hash_entry *entry,
3281 struct bfd_hash_table *table,
3282 const char *string)
65f38f15
AM
3283{
3284 /* Allocate the structure if it has not already been allocated by a
3285 subclass. */
3286 if (entry == NULL)
3287 {
3288 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3289 if (entry == NULL)
3290 return entry;
3291 }
3292
3293 /* Call the allocation method of the superclass. */
3294 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3295 if (entry != NULL)
3296 {
3297 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3298
b3fac117 3299 memset (&eh->u.stub_cache, 0,
908b32fc 3300 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3301 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3302
3303 /* When making function calls, old ABI code references function entry
3304 points (dot symbols), while new ABI code references the function
3305 descriptor symbol. We need to make any combination of reference and
3306 definition work together, without breaking archive linking.
3307
3308 For a defined function "foo" and an undefined call to "bar":
3309 An old object defines "foo" and ".foo", references ".bar" (possibly
3310 "bar" too).
3311 A new object defines "foo" and references "bar".
3312
3313 A new object thus has no problem with its undefined symbols being
3314 satisfied by definitions in an old object. On the other hand, the
3315 old object won't have ".bar" satisfied by a new object.
3316
3317 Keep a list of newly added dot-symbols. */
3318
3319 if (string[0] == '.')
3320 {
3321 struct ppc_link_hash_table *htab;
3322
3323 htab = (struct ppc_link_hash_table *) table;
3324 eh->u.next_dot_sym = htab->dot_syms;
3325 htab->dot_syms = eh;
3326 }
65f38f15
AM
3327 }
3328
3329 return entry;
3330}
3331
2cdcc330
AM
3332struct tocsave_entry
3333{
3b421ab3
AM
3334 asection *sec;
3335 bfd_vma offset;
3336};
3337
3338static hashval_t
3339tocsave_htab_hash (const void *p)
3340{
3341 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3342 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3343}
3344
3345static int
3346tocsave_htab_eq (const void *p1, const void *p2)
3347{
3348 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3349 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3350 return e1->sec == e2->sec && e1->offset == e2->offset;
3351}
3352
68faa637
AM
3353/* Destroy a ppc64 ELF linker hash table. */
3354
3355static void
d495ab0d 3356ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3357{
d495ab0d 3358 struct ppc_link_hash_table *htab;
68faa637 3359
d495ab0d 3360 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3361 if (htab->tocsave_htab)
3362 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3363 bfd_hash_table_free (&htab->branch_hash_table);
3364 bfd_hash_table_free (&htab->stub_hash_table);
3365 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3366}
3367
65f38f15
AM
3368/* Create a ppc64 ELF linker hash table. */
3369
3370static struct bfd_link_hash_table *
4ce794b7 3371ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3372{
3373 struct ppc_link_hash_table *htab;
3374 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3375
4ce794b7 3376 htab = bfd_zmalloc (amt);
65f38f15
AM
3377 if (htab == NULL)
3378 return NULL;
3379
66eb6687 3380 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3381 sizeof (struct ppc_link_hash_entry),
3382 PPC64_ELF_DATA))
65f38f15 3383 {
e2d34d7d 3384 free (htab);
65f38f15
AM
3385 return NULL;
3386 }
3387
721956f4 3388 /* Init the stub hash table too. */
66eb6687
AM
3389 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3390 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3391 {
d495ab0d 3392 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3393 return NULL;
3394 }
721956f4
AM
3395
3396 /* And the branch hash table. */
66eb6687
AM
3397 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3398 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3399 {
3400 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3401 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3402 return NULL;
3403 }
721956f4 3404
3b421ab3
AM
3405 htab->tocsave_htab = htab_try_create (1024,
3406 tocsave_htab_hash,
3407 tocsave_htab_eq,
3408 NULL);
3409 if (htab->tocsave_htab == NULL)
2915c55b 3410 {
d495ab0d 3411 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3412 return NULL;
3413 }
d495ab0d 3414 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3415
3254fd24
AM
3416 /* Initializing two fields of the union is just cosmetic. We really
3417 only care about glist, but when compiled on a 32-bit host the
3418 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3419 debugger inspection of these fields look nicer. */
a6aa5195
AM
3420 htab->elf.init_got_refcount.refcount = 0;
3421 htab->elf.init_got_refcount.glist = NULL;
3422 htab->elf.init_plt_refcount.refcount = 0;
3423 htab->elf.init_plt_refcount.glist = NULL;
3424 htab->elf.init_got_offset.offset = 0;
3425 htab->elf.init_got_offset.glist = NULL;
3426 htab->elf.init_plt_offset.offset = 0;
3427 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3428
65f38f15
AM
3429 return &htab->elf.root;
3430}
3431
bfeb4a28
AM
3432/* Create sections for linker generated code. */
3433
3434static bfd_boolean
3435create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3436{
3437 struct ppc_link_hash_table *htab;
3438 flagword flags;
3439
3440 htab = ppc_hash_table (info);
3441
bfeb4a28
AM
3442 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3443 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3444 if (htab->params->save_restore_funcs)
3445 {
3446 /* Create .sfpr for code to save and restore fp regs. */
3447 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3448 flags);
3449 if (htab->sfpr == NULL
2cdcc330 3450 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b32547cd
AM
3451 return FALSE;
3452 }
3453
3454 if (bfd_link_relocatable (info))
3455 return TRUE;
bfeb4a28
AM
3456
3457 /* Create .glink for lazy dynamic linking support. */
3458 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3459 flags);
3460 if (htab->glink == NULL
2cdcc330 3461 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
bfeb4a28
AM
3462 return FALSE;
3463
9e390558
AM
3464 /* The part of .glink used by global entry stubs, separate so that
3465 it can be aligned appropriately without affecting htab->glink. */
3466 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3467 flags);
3468 if (htab->global_entry == NULL
2cdcc330 3469 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
9e390558
AM
3470 return FALSE;
3471
bfeb4a28
AM
3472 if (!info->no_ld_generated_unwind_info)
3473 {
3474 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3475 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3476 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3477 ".eh_frame",
3478 flags);
3479 if (htab->glink_eh_frame == NULL
3480 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3481 return FALSE;
3482 }
3483
3484 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3485 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3486 if (htab->elf.iplt == NULL
2cdcc330 3487 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
3488 return FALSE;
3489
3490 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3491 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3492 htab->elf.irelplt
3493 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3494 if (htab->elf.irelplt == NULL
2cdcc330 3495 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
3496 return FALSE;
3497
3498 /* Create branch lookup table for plt_branch stubs. */
3499 flags = (SEC_ALLOC | SEC_LOAD
3500 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3501 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3502 flags);
3503 if (htab->brlt == NULL
2cdcc330 3504 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
bfeb4a28
AM
3505 return FALSE;
3506
2d7ad24e
AM
3507 /* Local plt entries, put in .branch_lt but a separate section for
3508 convenience. */
3509 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3510 flags);
3511 if (htab->pltlocal == NULL
2cdcc330 3512 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
2d7ad24e
AM
3513 return FALSE;
3514
0e1862bb 3515 if (!bfd_link_pic (info))
bfeb4a28
AM
3516 return TRUE;
3517
3518 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3519 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3520 htab->relbrlt
3521 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3522 if (htab->relbrlt == NULL
2cdcc330 3523 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
bfeb4a28
AM
3524 return FALSE;
3525
2d7ad24e
AM
3526 htab->relpltlocal
3527 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3528 if (htab->relpltlocal == NULL
2cdcc330 3529 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
2d7ad24e
AM
3530 return FALSE;
3531
bfeb4a28
AM
3532 return TRUE;
3533}
3534
e717da7e
AM
3535/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3536
bfeb4a28 3537bfd_boolean
e7d1c40c
AM
3538ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3539 struct ppc64_elf_params *params)
e717da7e
AM
3540{
3541 struct ppc_link_hash_table *htab;
3542
e7d1c40c 3543 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3544
3545/* Always hook our dynamic sections into the first bfd, which is the
3546 linker created stub bfd. This ensures that the GOT header is at
3547 the start of the output TOC section. */
3548 htab = ppc_hash_table (info);
e7d1c40c
AM
3549 htab->elf.dynobj = params->stub_bfd;
3550 htab->params = params;
bfeb4a28 3551
bfeb4a28 3552 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3553}
3554
721956f4
AM
3555/* Build a name for an entry in the stub hash table. */
3556
3557static char *
4ce794b7
AM
3558ppc_stub_name (const asection *input_section,
3559 const asection *sym_sec,
3560 const struct ppc_link_hash_entry *h,
3561 const Elf_Internal_Rela *rel)
721956f4
AM
3562{
3563 char *stub_name;
bcaa2f82 3564 ssize_t len;
721956f4
AM
3565
3566 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3567 offsets from a sym as a branch target? In fact, we could
3568 probably assume the addend is always zero. */
3569 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3570
3571 if (h)
3572 {
3573 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3574 stub_name = bfd_malloc (len);
46de2a7c
AM
3575 if (stub_name == NULL)
3576 return stub_name;
3577
bcaa2f82
AM
3578 len = sprintf (stub_name, "%08x.%s+%x",
3579 input_section->id & 0xffffffff,
3580 h->elf.root.root.string,
3581 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3582 }
3583 else
3584 {
ad8e1ba5 3585 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3586 stub_name = bfd_malloc (len);
46de2a7c
AM
3587 if (stub_name == NULL)
3588 return stub_name;
3589
bcaa2f82
AM
3590 len = sprintf (stub_name, "%08x.%x:%x+%x",
3591 input_section->id & 0xffffffff,
3592 sym_sec->id & 0xffffffff,
3593 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3594 (int) rel->r_addend & 0xffffffff);
721956f4 3595 }
bcaa2f82 3596 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3597 stub_name[len - 2] = 0;
721956f4
AM
3598 return stub_name;
3599}
3600
3601/* Look up an entry in the stub hash. Stub entries are cached because
3602 creating the stub name takes a bit of time. */
3603
3604static struct ppc_stub_hash_entry *
4ce794b7
AM
3605ppc_get_stub_entry (const asection *input_section,
3606 const asection *sym_sec,
039b3fef 3607 struct ppc_link_hash_entry *h,
4ce794b7
AM
3608 const Elf_Internal_Rela *rel,
3609 struct ppc_link_hash_table *htab)
721956f4
AM
3610{
3611 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3612 struct map_stub *group;
721956f4
AM
3613
3614 /* If this input section is part of a group of sections sharing one
3615 stub section, then use the id of the first section in the group.
3616 Stub names need to include a section id, as there may well be
3617 more than one stub used to reach say, printf, and we need to
3618 distinguish between them. */
6f20ed8a 3619 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3620 if (group == NULL)
3621 return NULL;
721956f4 3622
b3fac117
AM
3623 if (h != NULL && h->u.stub_cache != NULL
3624 && h->u.stub_cache->h == h
6f20ed8a 3625 && h->u.stub_cache->group == group)
721956f4 3626 {
b3fac117 3627 stub_entry = h->u.stub_cache;
721956f4
AM
3628 }
3629 else
3630 {
3631 char *stub_name;
3632
6f20ed8a 3633 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3634 if (stub_name == NULL)
3635 return NULL;
3636
3637 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3638 stub_name, FALSE, FALSE);
721956f4 3639 if (h != NULL)
b3fac117 3640 h->u.stub_cache = stub_entry;
721956f4
AM
3641
3642 free (stub_name);
3643 }
3644
3645 return stub_entry;
3646}
3647
3648/* Add a new stub entry to the stub hash. Not all fields of the new
3649 stub entry are initialised. */
3650
3651static struct ppc_stub_hash_entry *
4ce794b7
AM
3652ppc_add_stub (const char *stub_name,
3653 asection *section,
25f53a85 3654 struct bfd_link_info *info)
721956f4 3655{
25f53a85 3656 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3657 struct map_stub *group;
721956f4
AM
3658 asection *link_sec;
3659 asection *stub_sec;
3660 struct ppc_stub_hash_entry *stub_entry;
3661
6f20ed8a
AM
3662 group = htab->sec_info[section->id].u.group;
3663 link_sec = group->link_sec;
3664 stub_sec = group->stub_sec;
721956f4
AM
3665 if (stub_sec == NULL)
3666 {
6f20ed8a
AM
3667 size_t namelen;
3668 bfd_size_type len;
3669 char *s_name;
721956f4 3670
6f20ed8a
AM
3671 namelen = strlen (link_sec->name);
3672 len = namelen + sizeof (STUB_SUFFIX);
3673 s_name = bfd_alloc (htab->params->stub_bfd, len);
3674 if (s_name == NULL)
3675 return NULL;
721956f4 3676
6f20ed8a
AM
3677 memcpy (s_name, link_sec->name, namelen);
3678 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3679 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3680 if (stub_sec == NULL)
3681 return NULL;
3682 group->stub_sec = stub_sec;
721956f4
AM
3683 }
3684
3685 /* Enter this entry into the linker stub hash table. */
3686 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3687 TRUE, FALSE);
721956f4
AM
3688 if (stub_entry == NULL)
3689 {
695344c0 3690 /* xgettext:c-format */
cf97bcb0
AM
3691 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3692 section->owner, stub_name);
721956f4
AM
3693 return NULL;
3694 }
3695
6f20ed8a 3696 stub_entry->group = group;
721956f4 3697 stub_entry->stub_offset = 0;
721956f4
AM
3698 return stub_entry;
3699}
3700
e717da7e
AM
3701/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3702 not already done. */
65f38f15 3703
b34976b6 3704static bfd_boolean
e717da7e 3705create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3706{
e717da7e
AM
3707 asection *got, *relgot;
3708 flagword flags;
3709 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3710
0c8d6e5c 3711 if (!is_ppc64_elf (abfd))
0ffa91dd 3712 return FALSE;
4dfe6ac6
NC
3713 if (htab == NULL)
3714 return FALSE;
0ffa91dd 3715
33e44f2e
AM
3716 if (!htab->elf.sgot
3717 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3718 return FALSE;
e717da7e
AM
3719
3720 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3721 | SEC_LINKER_CREATED);
3722
c456f082 3723 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3724 if (!got
e717da7e
AM
3725 || !bfd_set_section_alignment (abfd, got, 3))
3726 return FALSE;
65f38f15 3727
c456f082
AM
3728 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3729 flags | SEC_READONLY);
e717da7e 3730 if (!relgot
2cdcc330 3731 || !bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 3732 return FALSE;
e717da7e
AM
3733
3734 ppc64_elf_tdata (abfd)->got = got;
3735 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3736 return TRUE;
65f38f15 3737}
5bd4f169 3738
b31867b6
AM
3739/* Follow indirect and warning symbol links. */
3740
3741static inline struct bfd_link_hash_entry *
3742follow_link (struct bfd_link_hash_entry *h)
3743{
3744 while (h->type == bfd_link_hash_indirect
3745 || h->type == bfd_link_hash_warning)
3746 h = h->u.i.link;
3747 return h;
3748}
3749
3750static inline struct elf_link_hash_entry *
3751elf_follow_link (struct elf_link_hash_entry *h)
3752{
3753 return (struct elf_link_hash_entry *) follow_link (&h->root);
3754}
3755
3756static inline struct ppc_link_hash_entry *
3757ppc_follow_link (struct ppc_link_hash_entry *h)
3758{
3759 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3760}
3761
40d16e0b
AM
3762/* Merge PLT info on FROM with that on TO. */
3763
3764static void
3765move_plt_plist (struct ppc_link_hash_entry *from,
3766 struct ppc_link_hash_entry *to)
3767{
3768 if (from->elf.plt.plist != NULL)
3769 {
3770 if (to->elf.plt.plist != NULL)
3771 {
3772 struct plt_entry **entp;
3773 struct plt_entry *ent;
3774
3775 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3776 {
3777 struct plt_entry *dent;
3778
3779 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3780 if (dent->addend == ent->addend)
3781 {
3782 dent->plt.refcount += ent->plt.refcount;
3783 *entp = ent->next;
3784 break;
3785 }
3786 if (dent == NULL)
3787 entp = &ent->next;
3788 }
3789 *entp = to->elf.plt.plist;
3790 }
3791
3792 to->elf.plt.plist = from->elf.plt.plist;
3793 from->elf.plt.plist = NULL;
3794 }
3795}
3796
65f38f15
AM
3797/* Copy the extra info we tack onto an elf_link_hash_entry. */
3798
3799static void
fcfa13d2
AM
3800ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3801 struct elf_link_hash_entry *dir,
3802 struct elf_link_hash_entry *ind)
65f38f15
AM
3803{
3804 struct ppc_link_hash_entry *edir, *eind;
3805
3806 edir = (struct ppc_link_hash_entry *) dir;
3807 eind = (struct ppc_link_hash_entry *) ind;
3808
c79d6685
AM
3809 edir->is_func |= eind->is_func;
3810 edir->is_func_descriptor |= eind->is_func_descriptor;
3811 edir->tls_mask |= eind->tls_mask;
3812 if (eind->oh != NULL)
3813 edir->oh = ppc_follow_link (eind->oh);
3814
474436e6 3815 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3816 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3817 edir->elf.ref_regular |= eind->elf.ref_regular;
3818 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3819 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3820 edir->elf.needs_plt |= eind->elf.needs_plt;
3821 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3822
d311bc8b
AM
3823 /* If we were called to copy over info for a weak sym, don't copy
3824 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3825 in order to simplify readonly_dynrelocs and save a field in the
3826 symbol hash entry, but that means dyn_relocs can't be used in any
3827 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3828 are then tested. */
d311bc8b 3829 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3830 return;
d311bc8b 3831
411e1bfb 3832 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3833 if (eind->dyn_relocs != NULL)
65f38f15 3834 {
bbd7ec4a
AM
3835 if (edir->dyn_relocs != NULL)
3836 {
6061a67d
AM
3837 struct elf_dyn_relocs **pp;
3838 struct elf_dyn_relocs *p;
bbd7ec4a 3839
fcfa13d2 3840 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
3841 list. Merge any entries against the same section. */
3842 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3843 {
6061a67d 3844 struct elf_dyn_relocs *q;
bbd7ec4a
AM
3845
3846 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3847 if (q->sec == p->sec)
3848 {
3849 q->pc_count += p->pc_count;
3850 q->count += p->count;
3851 *pp = p->next;
3852 break;
3853 }
3854 if (q == NULL)
3855 pp = &p->next;
3856 }
3857 *pp = edir->dyn_relocs;
3858 }
3859
65f38f15
AM
3860 edir->dyn_relocs = eind->dyn_relocs;
3861 eind->dyn_relocs = NULL;
3862 }
65f38f15 3863
81848ca0
AM
3864 /* Copy over got entries that we may have already seen to the
3865 symbol which just became indirect. */
411e1bfb
AM
3866 if (eind->elf.got.glist != NULL)
3867 {
3868 if (edir->elf.got.glist != NULL)
3869 {
3870 struct got_entry **entp;
3871 struct got_entry *ent;
3872
3873 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3874 {
3875 struct got_entry *dent;
3876
3877 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3878 if (dent->addend == ent->addend
e717da7e 3879 && dent->owner == ent->owner
411e1bfb
AM
3880 && dent->tls_type == ent->tls_type)
3881 {
3882 dent->got.refcount += ent->got.refcount;
3883 *entp = ent->next;
3884 break;
3885 }
3886 if (dent == NULL)
3887 entp = &ent->next;
3888 }
3889 *entp = edir->elf.got.glist;
3890 }
3891
3892 edir->elf.got.glist = eind->elf.got.glist;
3893 eind->elf.got.glist = NULL;
3894 }
3895
3896 /* And plt entries. */
40d16e0b 3897 move_plt_plist (eind, edir);
411e1bfb 3898
fcfa13d2 3899 if (eind->elf.dynindx != -1)
411e1bfb 3900 {
fcfa13d2
AM
3901 if (edir->elf.dynindx != -1)
3902 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3903 edir->elf.dynstr_index);
411e1bfb
AM
3904 edir->elf.dynindx = eind->elf.dynindx;
3905 edir->elf.dynstr_index = eind->elf.dynstr_index;
3906 eind->elf.dynindx = -1;
3907 eind->elf.dynstr_index = 0;
3908 }
411e1bfb
AM
3909}
3910
8387904d
AM
3911/* Find the function descriptor hash entry from the given function code
3912 hash entry FH. Link the entries via their OH fields. */
3913
3914static struct ppc_link_hash_entry *
b31867b6 3915lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
3916{
3917 struct ppc_link_hash_entry *fdh = fh->oh;
3918
3919 if (fdh == NULL)
3920 {
3921 const char *fd_name = fh->elf.root.root.string + 1;
3922
3923 fdh = (struct ppc_link_hash_entry *)
3924 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
3925 if (fdh == NULL)
3926 return fdh;
3927
3928 fdh->is_func_descriptor = 1;
3929 fdh->oh = fh;
3930 fh->is_func = 1;
3931 fh->oh = fdh;
8387904d
AM
3932 }
3933
8c5b4e52
AM
3934 fdh = ppc_follow_link (fdh);
3935 fdh->is_func_descriptor = 1;
3936 fdh->oh = fh;
3937 return fdh;
8387904d
AM
3938}
3939
8c5b4e52 3940/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
3941
3942static struct ppc_link_hash_entry *
3943make_fdh (struct bfd_link_info *info,
908b32fc 3944 struct ppc_link_hash_entry *fh)
bb700d78 3945{
8c5b4e52
AM
3946 bfd *abfd = fh->elf.root.u.undef.abfd;
3947 struct bfd_link_hash_entry *bh = NULL;
bb700d78 3948 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
3949 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3950 ? BSF_WEAK
3951 : BSF_GLOBAL);
3952
3953 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3954 fh->elf.root.root.string + 1,
3955 flags, bfd_und_section_ptr, 0,
3956 NULL, FALSE, FALSE, &bh))
bb700d78
AM
3957 return NULL;
3958
3959 fdh = (struct ppc_link_hash_entry *) bh;
3960 fdh->elf.non_elf = 0;
908b32fc
AM
3961 fdh->fake = 1;
3962 fdh->is_func_descriptor = 1;
3963 fdh->oh = fh;
3964 fh->is_func = 1;
3965 fh->oh = fdh;
bb700d78
AM
3966 return fdh;
3967}
3968
8387904d
AM
3969/* Fix function descriptor symbols defined in .opd sections to be
3970 function type. */
555cd476
AM
3971
3972static bfd_boolean
c16153ae 3973ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 3974 struct bfd_link_info *info,
555cd476 3975 Elf_Internal_Sym *isym,
6911b7dc 3976 const char **name,
555cd476
AM
3977 flagword *flags ATTRIBUTE_UNUSED,
3978 asection **sec,
b53dfeb2 3979 bfd_vma *value)
555cd476 3980{
b53dfeb2 3981 if (*sec != NULL
f1885d1e 3982 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
3983 {
3984 asection *code_sec;
3985
3986 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3987 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3988 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3989
3990 /* If the symbol is a function defined in .opd, and the function
3991 code is in a discarded group, let it appear to be undefined. */
0e1862bb 3992 if (!bfd_link_relocatable (info)
b53dfeb2
AM
3993 && (*sec)->reloc_count != 0
3994 && opd_entry_value (*sec, *value, &code_sec, NULL,
3995 FALSE) != (bfd_vma) -1
3996 && discarded_section (code_sec))
3997 {
3998 *sec = bfd_und_section_ptr;
3999 isym->st_shndx = SHN_UNDEF;
4000 }
4001 }
dbd1e97e
AM
4002 else if (*sec != NULL
4003 && strcmp ((*sec)->name, ".toc") == 0
4004 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4005 {
4006 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4007 if (htab != NULL)
4008 htab->params->object_in_toc = 1;
4009 }
433817dd 4010
6911b7dc
AM
4011 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4012 {
4013 if (abiversion (ibfd) == 0)
4014 set_abiversion (ibfd, 2);
4015 else if (abiversion (ibfd) == 1)
4016 {
cf97bcb0
AM
4017 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4018 " for ABI version 1"), *name);
6911b7dc
AM
4019 bfd_set_error (bfd_error_bad_value);
4020 return FALSE;
4021 }
4022 }
4023
555cd476
AM
4024 return TRUE;
4025}
4026
6911b7dc
AM
4027/* Merge non-visibility st_other attributes: local entry point. */
4028
4029static void
4030ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4031 const Elf_Internal_Sym *isym,
4032 bfd_boolean definition,
4033 bfd_boolean dynamic)
4034{
f378ab09 4035 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
4036 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4037 | ELF_ST_VISIBILITY (h->other));
4038}
4039
8c5b4e52
AM
4040/* Hook called on merging a symbol. We use this to clear "fake" since
4041 we now have a real symbol. */
4042
4043static bfd_boolean
4044ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4045 const Elf_Internal_Sym *isym,
8c5b4e52
AM
4046 asection **psec ATTRIBUTE_UNUSED,
4047 bfd_boolean newdef ATTRIBUTE_UNUSED,
4048 bfd_boolean olddef ATTRIBUTE_UNUSED,
4049 bfd *oldbfd ATTRIBUTE_UNUSED,
4050 const asection *oldsec ATTRIBUTE_UNUSED)
4051{
4052 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
4053 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4054 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
4055 return TRUE;
4056}
4057
8387904d 4058/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4059 inclusion of a new ABI object archive that defines "bar".
4060 NAME is a symbol defined in an archive. Return a symbol in the hash
4061 table that might be satisfied by the archive symbols. */
8387904d
AM
4062
4063static struct elf_link_hash_entry *
4064ppc64_elf_archive_symbol_lookup (bfd *abfd,
4065 struct bfd_link_info *info,
4066 const char *name)
4067{
4068 struct elf_link_hash_entry *h;
4069 char *dot_name;
4070 size_t len;
4071
4072 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4073 if (h != NULL
4074 /* Don't return this sym if it is a fake function descriptor
4075 created by add_symbol_adjust. */
8c5b4e52 4076 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4077 return h;
4078
4079 if (name[0] == '.')
4080 return h;
4081
4082 len = strlen (name);
4083 dot_name = bfd_alloc (abfd, len + 2);
4084 if (dot_name == NULL)
e99955cd 4085 return (struct elf_link_hash_entry *) -1;
8387904d
AM
4086 dot_name[0] = '.';
4087 memcpy (dot_name + 1, name, len + 1);
4088 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4089 bfd_release (abfd, dot_name);
4090 return h;
4091}
4092
4093/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4094 new ABI object defines "bar". Well, at least, undefined dot symbols
4095 are made weak. This stops later archive searches from including an
4096 object if we already have a function descriptor definition. It also
35b0ce59
AM
4097 prevents the linker complaining about undefined symbols.
4098 We also check and correct mismatched symbol visibility here. The
4099 most restrictive visibility of the function descriptor and the
4100 function entry symbol is used. */
8387904d
AM
4101
4102static bfd_boolean
b3fac117 4103add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4104{
8387904d
AM
4105 struct ppc_link_hash_table *htab;
4106 struct ppc_link_hash_entry *fdh;
4107
b3fac117
AM
4108 if (eh->elf.root.type == bfd_link_hash_warning)
4109 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4110
8400d40d
AM
4111 if (eh->elf.root.type == bfd_link_hash_indirect)
4112 return TRUE;
4113
b3fac117
AM
4114 if (eh->elf.root.root.string[0] != '.')
4115 abort ();
8387904d 4116
b3fac117 4117 htab = ppc_hash_table (info);
4dfe6ac6
NC
4118 if (htab == NULL)
4119 return FALSE;
4120
b31867b6 4121 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4122 if (fdh == NULL
4123 && !bfd_link_relocatable (info)
4124 && (eh->elf.root.type == bfd_link_hash_undefined
4125 || eh->elf.root.type == bfd_link_hash_undefweak)
4126 && eh->elf.ref_regular)
4127 {
4128 /* Make an undefined function descriptor sym, in order to
4129 pull in an --as-needed shared lib. Archives are handled
4130 elsewhere. */
4131 fdh = make_fdh (info, eh);
4132 if (fdh == NULL)
4133 return FALSE;
bb700d78 4134 }
8c5b4e52
AM
4135
4136 if (fdh != NULL)
8387904d 4137 {
35b0ce59
AM
4138 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4139 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4140
4141 /* Make both descriptor and entry symbol have the most
4142 constraining visibility of either symbol. */
35b0ce59
AM
4143 if (entry_vis < descr_vis)
4144 fdh->elf.other += entry_vis - descr_vis;
4145 else if (entry_vis > descr_vis)
4146 eh->elf.other += descr_vis - entry_vis;
4147
8c5b4e52
AM
4148 /* Propagate reference flags from entry symbol to function
4149 descriptor symbol. */
bc4e12de 4150 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4151 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4152 fdh->elf.ref_regular |= eh->elf.ref_regular;
4153 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4154
4155 if (!fdh->elf.forced_local
4156 && fdh->elf.dynindx == -1
4157 && fdh->elf.versioned != versioned_hidden
4158 && (bfd_link_dll (info)
4159 || fdh->elf.def_dynamic
4160 || fdh->elf.ref_dynamic)
4161 && (eh->elf.ref_regular
4162 || eh->elf.def_regular))
4163 {
2cdcc330 4164 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 4165 return FALSE;
35b0ce59 4166 }
8387904d 4167 }
99877b66 4168
8387904d
AM
4169 return TRUE;
4170}
4171
f6c7c3e8
AM
4172/* Set up opd section info and abiversion for IBFD, and process list
4173 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4174
8387904d 4175static bfd_boolean
f6c7c3e8 4176ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4177{
99877b66 4178 struct ppc_link_hash_table *htab;
b3fac117 4179 struct ppc_link_hash_entry **p, *eh;
459609d6 4180 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4181
459609d6 4182 if (opd != NULL && opd->size != 0)
b3fac117 4183 {
b9399fcf
AM
4184 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4185 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4186
459609d6
AM
4187 if (abiversion (ibfd) == 0)
4188 set_abiversion (ibfd, 1);
8a2058b5 4189 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4190 {
695344c0 4191 /* xgettext:c-format */
cf97bcb0
AM
4192 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4193 ibfd, abiversion (ibfd));
459609d6
AM
4194 bfd_set_error (bfd_error_bad_value);
4195 return FALSE;
f6c7c3e8 4196 }
b9399fcf 4197 }
f6c7c3e8 4198
b9399fcf
AM
4199 if (is_ppc64_elf (info->output_bfd))
4200 {
4201 /* For input files without an explicit abiversion in e_flags
4202 we should have flagged any with symbol st_other bits set
4203 as ELFv1 and above flagged those with .opd as ELFv2.
4204 Set the output abiversion if not yet set, and for any input
4205 still ambiguous, take its abiversion from the output.
4206 Differences in ABI are reported later. */
4207 if (abiversion (info->output_bfd) == 0)
4208 set_abiversion (info->output_bfd, abiversion (ibfd));
4209 else if (abiversion (ibfd) == 0)
4210 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4211 }
4212
459609d6
AM
4213 htab = ppc_hash_table (info);
4214 if (htab == NULL)
b9399fcf 4215 return TRUE;
459609d6 4216
b9399fcf
AM
4217 if (opd != NULL && opd->size != 0
4218 && (ibfd->flags & DYNAMIC) == 0
4219 && (opd->flags & SEC_RELOC) != 0
4220 && opd->reloc_count != 0
4221 && !bfd_is_abs_section (opd->output_section)
4222 && info->gc_sections)
4223 {
4224 /* Garbage collection needs some extra help with .opd sections.
4225 We don't want to necessarily keep everything referenced by
4226 relocs in .opd, as that would keep all functions. Instead,
4227 if we reference an .opd symbol (a function descriptor), we
4228 want to keep the function code symbol's section. This is
4229 easy for global symbols, but for local syms we need to keep
4230 information about the associated function section. */
4231 bfd_size_type amt;
4232 asection **opd_sym_map;
4233 Elf_Internal_Shdr *symtab_hdr;
4234 Elf_Internal_Rela *relocs, *rel_end, *rel;
4235
4236 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4237 opd_sym_map = bfd_zalloc (ibfd, amt);
4238 if (opd_sym_map == NULL)
4239 return FALSE;
4240 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4241 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4242 info->keep_memory);
4243 if (relocs == NULL)
4244 return FALSE;
4245 symtab_hdr = &elf_symtab_hdr (ibfd);
4246 rel_end = relocs + opd->reloc_count - 1;
4247 for (rel = relocs; rel < rel_end; rel++)
4248 {
4249 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4250 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4251
4252 if (r_type == R_PPC64_ADDR64
4253 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4254 && r_symndx < symtab_hdr->sh_info)
4255 {
4256 Elf_Internal_Sym *isym;
4257 asection *s;
4258
4259 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4260 if (isym == NULL)
4261 {
4262 if (elf_section_data (opd)->relocs != relocs)
4263 free (relocs);
4264 return FALSE;
4265 }
4266
4267 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4268 if (s != NULL && s != opd)
4269 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4270 }
4271 }
4272 if (elf_section_data (opd)->relocs != relocs)
4273 free (relocs);
4274 }
459609d6
AM
4275
4276 p = &htab->dot_syms;
4277 while ((eh = *p) != NULL)
4278 {
4279 *p = NULL;
4280 if (&eh->elf == htab->elf.hgot)
4281 ;
4282 else if (htab->elf.hgot == NULL
4283 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4284 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4285 else if (abiversion (ibfd) <= 1)
4286 {
4287 htab->need_func_desc_adj = 1;
4288 if (!add_symbol_adjust (eh, info))
4289 return FALSE;
4290 }
459609d6
AM
4291 p = &eh->u.next_dot_sym;
4292 }
b3fac117 4293 return TRUE;
8387904d
AM
4294}
4295
97fed1c9
JJ
4296/* Undo hash table changes when an --as-needed input file is determined
4297 not to be needed. */
4298
4299static bfd_boolean
e5034e59
AM
4300ppc64_elf_notice_as_needed (bfd *ibfd,
4301 struct bfd_link_info *info,
4302 enum notice_asneeded_action act)
97fed1c9 4303{
e5034e59
AM
4304 if (act == notice_not_needed)
4305 {
4306 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4307
e5034e59
AM
4308 if (htab == NULL)
4309 return FALSE;
4dfe6ac6 4310
e5034e59
AM
4311 htab->dot_syms = NULL;
4312 }
4313 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4314}
4315
aa374f67
AM
4316/* If --just-symbols against a final linked binary, then assume we need
4317 toc adjusting stubs when calling functions defined there. */
4318
4319static void
4320ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4321{
4322 if ((sec->flags & SEC_CODE) != 0
4323 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4324 && is_ppc64_elf (sec->owner))
4325 {
2c3f079f
AM
4326 if (abiversion (sec->owner) >= 2
4327 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4328 sec->has_toc_reloc = 1;
4329 }
4330 _bfd_elf_link_just_syms (sec, info);
4331}
4332
e054468f 4333static struct plt_entry **
4ce794b7
AM
4334update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4335 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4336{
4337 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4338 struct plt_entry **local_plt;
f961d9dd 4339 unsigned char *local_got_tls_masks;
411e1bfb
AM
4340
4341 if (local_got_ents == NULL)
4342 {
4343 bfd_size_type size = symtab_hdr->sh_info;
4344
e054468f
AM
4345 size *= (sizeof (*local_got_ents)
4346 + sizeof (*local_plt)
4347 + sizeof (*local_got_tls_masks));
4ce794b7 4348 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4349 if (local_got_ents == NULL)
e054468f 4350 return NULL;
411e1bfb
AM
4351 elf_local_got_ents (abfd) = local_got_ents;
4352 }
4353
37da22e5 4354 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4355 {
4356 struct got_entry *ent;
4357
4358 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4359 if (ent->addend == r_addend
4360 && ent->owner == abfd
4361 && ent->tls_type == tls_type)
411e1bfb
AM
4362 break;
4363 if (ent == NULL)
4364 {
4365 bfd_size_type amt = sizeof (*ent);
4ce794b7 4366 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4367 if (ent == NULL)
4368 return FALSE;
4369 ent->next = local_got_ents[r_symndx];
4370 ent->addend = r_addend;
e717da7e 4371 ent->owner = abfd;
411e1bfb 4372 ent->tls_type = tls_type;
927be08e 4373 ent->is_indirect = FALSE;
411e1bfb
AM
4374 ent->got.refcount = 0;
4375 local_got_ents[r_symndx] = ent;
4376 }
4377 ent->got.refcount += 1;
4378 }
4379
e054468f 4380 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4381 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4382 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4383
4384 return local_plt + r_symndx;
65f38f15
AM
4385}
4386
411e1bfb 4387static bfd_boolean
e054468f 4388update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4389{
411e1bfb 4390 struct plt_entry *ent;
1e2f5b6e 4391
e054468f 4392 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4393 if (ent->addend == addend)
4394 break;
4395 if (ent == NULL)
1e2f5b6e 4396 {
411e1bfb 4397 bfd_size_type amt = sizeof (*ent);
4ce794b7 4398 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4399 if (ent == NULL)
4400 return FALSE;
e054468f 4401 ent->next = *plist;
411e1bfb
AM
4402 ent->addend = addend;
4403 ent->plt.refcount = 0;
e054468f 4404 *plist = ent;
1e2f5b6e 4405 }
411e1bfb 4406 ent->plt.refcount += 1;
b34976b6 4407 return TRUE;
1e2f5b6e
AM
4408}
4409
e054468f
AM
4410static bfd_boolean
4411is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4412{
4413 return (r_type == R_PPC64_REL24
05d0e962 4414 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4415 || r_type == R_PPC64_REL14
4416 || r_type == R_PPC64_REL14_BRTAKEN
4417 || r_type == R_PPC64_REL14_BRNTAKEN
4418 || r_type == R_PPC64_ADDR24
4419 || r_type == R_PPC64_ADDR14
4420 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4421 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4422 || r_type == R_PPC64_PLTCALL
4423 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4424}
4425
4426/* Relocs on inline plt call sequence insns prior to the call. */
4427
4428static bfd_boolean
4429is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4430{
4431 return (r_type == R_PPC64_PLT16_HA
4432 || r_type == R_PPC64_PLT16_HI
4433 || r_type == R_PPC64_PLT16_LO
4434 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4435 || r_type == R_PPC64_PLT_PCREL34
4436 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4437 || r_type == R_PPC64_PLTSEQ
4438 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4439}
4440
5bd4f169 4441/* Look through the relocs for a section during the first phase, and
65f38f15 4442 calculate needed space in the global offset table, procedure
5d1634d7 4443 linkage table, and dynamic reloc sections. */
5bd4f169 4444
b34976b6 4445static bfd_boolean
4ce794b7
AM
4446ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4447 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4448{
65f38f15 4449 struct ppc_link_hash_table *htab;
5bd4f169 4450 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4451 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4452 const Elf_Internal_Rela *rel;
4453 const Elf_Internal_Rela *rel_end;
5bd4f169 4454 asection *sreloc;
3a71aa26 4455 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4456 bfd_boolean is_opd;
5bd4f169 4457
0e1862bb 4458 if (bfd_link_relocatable (info))
b34976b6 4459 return TRUE;
5bd4f169 4460
680a3378
AM
4461 /* Don't do anything special with non-loaded, non-alloced sections.
4462 In particular, any relocs in such sections should not affect GOT
4463 and PLT reference counting (ie. we don't allow them to create GOT
4464 or PLT entries), there's no possibility or desire to optimize TLS
4465 relocs, and there's not much point in propagating relocs to shared
4466 libs that the dynamic linker won't relocate. */
4467 if ((sec->flags & SEC_ALLOC) == 0)
4468 return TRUE;
4469
0c8d6e5c 4470 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4471
65f38f15 4472 htab = ppc_hash_table (info);
4dfe6ac6
NC
4473 if (htab == NULL)
4474 return FALSE;
4475
3a71aa26
AM
4476 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4477 FALSE, FALSE, TRUE);
4478 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4479 FALSE, FALSE, TRUE);
0ffa91dd 4480 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4481 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4482 sreloc = NULL;
b9399fcf 4483 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4484 rel_end = relocs + sec->reloc_count;
4485 for (rel = relocs; rel < rel_end; rel++)
4486 {
4487 unsigned long r_symndx;
4488 struct elf_link_hash_entry *h;
04c9666a 4489 enum elf_ppc64_reloc_type r_type;
727fc41e 4490 int tls_type;
7c8fe5c4 4491 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4492 struct plt_entry **ifunc, **plt_list;
5663e321 4493 bfd_vma sym_addend;
5bd4f169
AM
4494
4495 r_symndx = ELF64_R_SYM (rel->r_info);
4496 if (r_symndx < symtab_hdr->sh_info)
4497 h = NULL;
4498 else
973a3492
L
4499 {
4500 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4501 h = elf_follow_link (h);
1c865ab2
AM
4502
4503 if (h == htab->elf.hgot)
4504 sec->has_toc_reloc = 1;
973a3492 4505 }
5bd4f169 4506
727fc41e 4507 tls_type = 0;
e054468f 4508 ifunc = NULL;
5663e321
AM
4509 r_type = ELF64_R_TYPE (rel->r_info);
4510 switch (r_type)
4511 {
04bdff6a
AM
4512 case R_PPC64_D34:
4513 case R_PPC64_D34_LO:
4514 case R_PPC64_D34_HI30:
4515 case R_PPC64_D34_HA30:
4516 case R_PPC64_D28:
4517 htab->powerxx_stubs = 1;
4518 /* Fall through. */
5663e321
AM
4519 default:
4520 /* Somewhat foolishly, because the ABIs don't specifically
4521 allow it, ppc64 gas and ld support GOT and PLT relocs
4522 with non-zero addends where the addend results in
4523 sym+addend being stored in the GOT or PLT entry. This
4524 can't be supported for pcrel relocs because the addend is
4525 used to specify the pcrel offset. */
4526 sym_addend = rel->r_addend;
4527 break;
04bdff6a
AM
4528
4529 case R_PPC64_PCREL34:
5663e321
AM
4530 case R_PPC64_GOT_PCREL34:
4531 case R_PPC64_PLT_PCREL34:
4532 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a
AM
4533 case R_PPC64_PCREL28:
4534 htab->powerxx_stubs = 1;
5663e321
AM
4535 sym_addend = 0;
4536 break;
4537 }
25f23106
AM
4538 if (h != NULL)
4539 {
4540 if (h->type == STT_GNU_IFUNC)
4541 {
4542 h->needs_plt = 1;
4543 ifunc = &h->plt.plist;
4544 }
4545 }
4546 else
4547 {
4548 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4549 abfd, r_symndx);
4550 if (isym == NULL)
4551 return FALSE;
4552
4553 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4554 {
4555 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4556 sym_addend,
37da22e5 4557 NON_GOT | PLT_IFUNC);
25f23106
AM
4558 if (ifunc == NULL)
4559 return FALSE;
4560 }
4561 }
727fc41e 4562
a33d1f77 4563 switch (r_type)
5bd4f169 4564 {
727fc41e
AM
4565 case R_PPC64_TLSGD:
4566 case R_PPC64_TLSLD:
4567 /* These special tls relocs tie a call to __tls_get_addr with
4568 its parameter symbol. */
37da22e5
AM
4569 if (h != NULL)
4570 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4571 else
4572 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4573 sym_addend,
37da22e5
AM
4574 NON_GOT | TLS_TLS | TLS_MARK))
4575 return FALSE;
4576 sec->has_tls_reloc = 1;
727fc41e
AM
4577 break;
4578
411e1bfb
AM
4579 case R_PPC64_GOT_TLSLD16:
4580 case R_PPC64_GOT_TLSLD16_LO:
4581 case R_PPC64_GOT_TLSLD16_HI:
4582 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 4583 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4584 goto dogottls;
4585
4586 case R_PPC64_GOT_TLSGD16:
4587 case R_PPC64_GOT_TLSGD16_LO:
4588 case R_PPC64_GOT_TLSGD16_HI:
4589 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 4590 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4591 goto dogottls;
4592
4593 case R_PPC64_GOT_TPREL16_DS:
4594 case R_PPC64_GOT_TPREL16_LO_DS:
4595 case R_PPC64_GOT_TPREL16_HI:
4596 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 4597 if (bfd_link_dll (info))
411e1bfb
AM
4598 info->flags |= DF_STATIC_TLS;
4599 tls_type = TLS_TLS | TLS_TPREL;
4600 goto dogottls;
4601
4602 case R_PPC64_GOT_DTPREL16_DS:
4603 case R_PPC64_GOT_DTPREL16_LO_DS:
4604 case R_PPC64_GOT_DTPREL16_HI:
4605 case R_PPC64_GOT_DTPREL16_HA:
4606 tls_type = TLS_TLS | TLS_DTPREL;
4607 dogottls:
4608 sec->has_tls_reloc = 1;
066f4018 4609 goto dogot;
411e1bfb 4610
65f38f15 4611 case R_PPC64_GOT16_HA:
066f4018 4612 case R_PPC64_GOT16_LO_DS:
4a421c53 4613 case R_PPC64_GOT_PCREL34:
066f4018
AM
4614 ppc64_elf_tdata (abfd)->has_gotrel = 1;
4615 ppc64_elf_section_data (sec)->has_gotrel = 1;
4616 /* Fall through. */
4617
bb22a418 4618 case R_PPC64_GOT16_DS:
066f4018 4619 case R_PPC64_GOT16:
65f38f15
AM
4620 case R_PPC64_GOT16_HI:
4621 case R_PPC64_GOT16_LO:
066f4018 4622 dogot:
65f38f15 4623 /* This symbol requires a global offset table entry. */
4c52953f 4624 sec->has_toc_reloc = 1;
33c0ec9d
AM
4625 if (r_type == R_PPC64_GOT_TLSLD16
4626 || r_type == R_PPC64_GOT_TLSGD16
4627 || r_type == R_PPC64_GOT_TPREL16_DS
4628 || r_type == R_PPC64_GOT_DTPREL16_DS
4629 || r_type == R_PPC64_GOT16
4630 || r_type == R_PPC64_GOT16_DS)
4631 {
4632 htab->do_multi_toc = 1;
d77c8a4b 4633 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4634 }
4635
e717da7e
AM
4636 if (ppc64_elf_tdata (abfd)->got == NULL
4637 && !create_got_section (abfd, info))
b34976b6 4638 return FALSE;
5bd4f169
AM
4639
4640 if (h != NULL)
4641 {
411e1bfb
AM
4642 struct ppc_link_hash_entry *eh;
4643 struct got_entry *ent;
65f38f15 4644
411e1bfb
AM
4645 eh = (struct ppc_link_hash_entry *) h;
4646 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5663e321 4647 if (ent->addend == sym_addend
e717da7e 4648 && ent->owner == abfd
411e1bfb
AM
4649 && ent->tls_type == tls_type)
4650 break;
4651 if (ent == NULL)
5bd4f169 4652 {
411e1bfb 4653 bfd_size_type amt = sizeof (*ent);
4ce794b7 4654 ent = bfd_alloc (abfd, amt);
411e1bfb 4655 if (ent == NULL)
b34976b6 4656 return FALSE;
411e1bfb 4657 ent->next = eh->elf.got.glist;
5663e321 4658 ent->addend = sym_addend;
e717da7e 4659 ent->owner = abfd;
411e1bfb 4660 ent->tls_type = tls_type;
927be08e 4661 ent->is_indirect = FALSE;
411e1bfb
AM
4662 ent->got.refcount = 0;
4663 eh->elf.got.glist = ent;
5bd4f169 4664 }
411e1bfb 4665 ent->got.refcount += 1;
e7b938ca 4666 eh->tls_mask |= tls_type;
5bd4f169 4667 }
411e1bfb
AM
4668 else
4669 /* This is a global offset table entry for a local symbol. */
4670 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4671 sym_addend, tls_type))
411e1bfb 4672 return FALSE;
a345bc8d
AM
4673
4674 /* We may also need a plt entry if the symbol turns out to be
4675 an ifunc. */
0e1862bb 4676 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d 4677 {
5663e321 4678 if (!update_plt_info (abfd, &h->plt.plist, sym_addend))
a345bc8d
AM
4679 return FALSE;
4680 }
5bd4f169
AM
4681 break;
4682
5bd4f169 4683 case R_PPC64_PLT16_HA:
65f38f15
AM
4684 case R_PPC64_PLT16_HI:
4685 case R_PPC64_PLT16_LO:
08be3224 4686 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4687 case R_PPC64_PLT_PCREL34:
4688 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4689 case R_PPC64_PLT32:
4690 case R_PPC64_PLT64:
cbf95972
AM
4691 /* This symbol requires a procedure linkage table entry. */
4692 plt_list = ifunc;
4693 if (h != NULL)
e054468f 4694 {
e054468f
AM
4695 h->needs_plt = 1;
4696 if (h->root.root.string[0] == '.'
4697 && h->root.root.string[1] != '\0')
4698 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 4699 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4700 plt_list = &h->plt.plist;
4701 }
4702 if (plt_list == NULL)
2d7ad24e 4703 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4704 sym_addend,
2d7ad24e 4705 NON_GOT | PLT_KEEP);
5663e321 4706 if (!update_plt_info (abfd, plt_list, sym_addend))
cbf95972 4707 return FALSE;
5bd4f169
AM
4708 break;
4709
4710 /* The following relocations don't need to propagate the
4711 relocation if linking a shared object since they are
4712 section relative. */
4713 case R_PPC64_SECTOFF:
4714 case R_PPC64_SECTOFF_LO:
4715 case R_PPC64_SECTOFF_HI:
4716 case R_PPC64_SECTOFF_HA:
4717 case R_PPC64_SECTOFF_DS:
4718 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4719 case R_PPC64_DTPREL16:
4720 case R_PPC64_DTPREL16_LO:
4721 case R_PPC64_DTPREL16_HI:
4722 case R_PPC64_DTPREL16_HA:
4723 case R_PPC64_DTPREL16_DS:
4724 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4725 case R_PPC64_DTPREL16_HIGH:
4726 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4727 case R_PPC64_DTPREL16_HIGHER:
4728 case R_PPC64_DTPREL16_HIGHERA:
4729 case R_PPC64_DTPREL16_HIGHEST:
4730 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4731 break;
4732
ad8e1ba5 4733 /* Nor do these. */
25f23106
AM
4734 case R_PPC64_REL16:
4735 case R_PPC64_REL16_LO:
4736 case R_PPC64_REL16_HI:
4737 case R_PPC64_REL16_HA:
4a969973
AM
4738 case R_PPC64_REL16_HIGH:
4739 case R_PPC64_REL16_HIGHA:
4740 case R_PPC64_REL16_HIGHER:
4741 case R_PPC64_REL16_HIGHERA:
4742 case R_PPC64_REL16_HIGHEST:
4743 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
4744 case R_PPC64_REL16_HIGHER34:
4745 case R_PPC64_REL16_HIGHERA34:
4746 case R_PPC64_REL16_HIGHEST34:
4747 case R_PPC64_REL16_HIGHESTA34:
a680de9a 4748 case R_PPC64_REL16DX_HA:
25f23106
AM
4749 break;
4750
45965137
AM
4751 /* Not supported as a dynamic relocation. */
4752 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4753 if (bfd_link_pic (info))
45965137
AM
4754 {
4755 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4756 ppc_howto_init ();
695344c0 4757 /* xgettext:c-format */
174d0a74 4758 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4759 "in shared libraries and PIEs\n"),
45965137
AM
4760 abfd, sec, rel->r_offset,
4761 ppc64_elf_howto_table[r_type]->name);
4762 bfd_set_error (bfd_error_bad_value);
4763 return FALSE;
4764 }
4765 break;
4766
ad8e1ba5 4767 case R_PPC64_TOC16:
33c0ec9d
AM
4768 case R_PPC64_TOC16_DS:
4769 htab->do_multi_toc = 1;
d77c8a4b 4770 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4771 /* Fall through. */
ad8e1ba5
AM
4772 case R_PPC64_TOC16_LO:
4773 case R_PPC64_TOC16_HI:
4774 case R_PPC64_TOC16_HA:
ad8e1ba5 4775 case R_PPC64_TOC16_LO_DS:
4c52953f 4776 sec->has_toc_reloc = 1;
ad8e1ba5
AM
4777 break;
4778
006589cf
AM
4779 /* Marker reloc. */
4780 case R_PPC64_ENTRY:
4781 break;
4782
5bd4f169
AM
4783 /* This relocation describes the C++ object vtable hierarchy.
4784 Reconstruct it for later use during GC. */
4785 case R_PPC64_GNU_VTINHERIT:
c152c796 4786 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4787 return FALSE;
5bd4f169
AM
4788 break;
4789
4790 /* This relocation describes which C++ vtable entries are actually
4791 used. Record for later use during GC. */
4792 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4793 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4794 return FALSE;
5bd4f169
AM
4795 break;
4796
721956f4
AM
4797 case R_PPC64_REL14:
4798 case R_PPC64_REL14_BRTAKEN:
4799 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4800 {
4801 asection *dest = NULL;
4802
4803 /* Heuristic: If jumping outside our section, chances are
4804 we are going to need a stub. */
4805 if (h != NULL)
4806 {
4807 /* If the sym is weak it may be overridden later, so
4808 don't assume we know where a weak sym lives. */
4809 if (h->root.type == bfd_link_hash_defined)
4810 dest = h->root.u.def.section;
4811 }
4812 else
87d72d41
AM
4813 {
4814 Elf_Internal_Sym *isym;
4815
4816 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4817 abfd, r_symndx);
4818 if (isym == NULL)
4819 return FALSE;
4820
4821 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4822 }
4823
220c76dd 4824 if (dest != sec)
7c8fe5c4 4825 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4826 }
3e04d765
AM
4827 goto rel24;
4828
4829 case R_PPC64_PLTCALL:
5663e321 4830 case R_PPC64_PLTCALL_NOTOC:
3e04d765 4831 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4832 /* Fall through. */
4833
5d1634d7 4834 case R_PPC64_REL24:
05d0e962 4835 case R_PPC64_REL24_NOTOC:
3e04d765 4836 rel24:
cbf95972
AM
4837 plt_list = ifunc;
4838 if (h != NULL)
5d1634d7 4839 {
e054468f
AM
4840 h->needs_plt = 1;
4841 if (h->root.root.string[0] == '.'
4842 && h->root.root.string[1] != '\0')
4843 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 4844
3a71aa26 4845 if (h == tga || h == dottga)
cbf95972
AM
4846 {
4847 sec->has_tls_reloc = 1;
4848 if (rel != relocs
4849 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4850 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4851 /* We have a new-style __tls_get_addr call with
4852 a marker reloc. */
4853 ;
4854 else
4855 /* Mark this section as having an old-style call. */
4856 sec->has_tls_get_addr_call = 1;
4857 }
4858 plt_list = &h->plt.plist;
411e1bfb 4859 }
cbf95972
AM
4860
4861 /* We may need a .plt entry if the function this reloc
4862 refers to is in a shared lib. */
4863 if (plt_list
5663e321 4864 && !update_plt_info (abfd, plt_list, sym_addend))
cbf95972 4865 return FALSE;
411e1bfb
AM
4866 break;
4867
cbf95972
AM
4868 case R_PPC64_ADDR14:
4869 case R_PPC64_ADDR14_BRNTAKEN:
4870 case R_PPC64_ADDR14_BRTAKEN:
4871 case R_PPC64_ADDR24:
4872 goto dodyn;
4873
411e1bfb
AM
4874 case R_PPC64_TPREL64:
4875 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 4876 if (bfd_link_dll (info))
411e1bfb
AM
4877 info->flags |= DF_STATIC_TLS;
4878 goto dotlstoc;
4879
4880 case R_PPC64_DTPMOD64:
4881 if (rel + 1 < rel_end
4882 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4883 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 4884 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 4885 else
951fd09b 4886 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
4887 goto dotlstoc;
4888
4889 case R_PPC64_DTPREL64:
4890 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4891 if (rel != relocs
4892 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4893 && rel[-1].r_offset == rel->r_offset - 8)
4894 /* This is the second reloc of a dtpmod, dtprel pair.
4895 Don't mark with TLS_DTPREL. */
4896 goto dodyn;
4897
4898 dotlstoc:
4899 sec->has_tls_reloc = 1;
4900 if (h != NULL)
4901 {
4902 struct ppc_link_hash_entry *eh;
4903 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 4904 eh->tls_mask |= tls_type;
411e1bfb
AM
4905 }
4906 else
4907 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5663e321 4908 sym_addend, tls_type))
411e1bfb
AM
4909 return FALSE;
4910
7c8fe5c4
AM
4911 ppc64_sec = ppc64_elf_section_data (sec);
4912 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 4913 {
3a71aa26
AM
4914 bfd_size_type amt;
4915
e7b938ca 4916 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
4917 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4918 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4919 if (ppc64_sec->u.toc.symndx == NULL)
4920 return FALSE;
4921 amt = sec->size * sizeof (bfd_vma) / 8;
4922 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4923 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 4924 return FALSE;
7c8fe5c4
AM
4925 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4926 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
4927 }
4928 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 4929 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5663e321 4930 ppc64_sec->u.toc.add[rel->r_offset / 8] = sym_addend;
951fd09b
AM
4931
4932 /* Mark the second slot of a GD or LD entry.
4933 -1 to indicate GD and -2 to indicate LD. */
4934 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 4935 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 4936 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 4937 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
4938 goto dodyn;
4939
4940 case R_PPC64_TPREL16:
4941 case R_PPC64_TPREL16_LO:
4942 case R_PPC64_TPREL16_HI:
4943 case R_PPC64_TPREL16_HA:
4944 case R_PPC64_TPREL16_DS:
4945 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
4946 case R_PPC64_TPREL16_HIGH:
4947 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
4948 case R_PPC64_TPREL16_HIGHER:
4949 case R_PPC64_TPREL16_HIGHERA:
4950 case R_PPC64_TPREL16_HIGHEST:
4951 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
4952 if (bfd_link_dll (info))
4953 info->flags |= DF_STATIC_TLS;
4954 goto dodyn;
5d1634d7 4955
e86ce104 4956 case R_PPC64_ADDR64:
b9399fcf 4957 if (is_opd
1e2f5b6e 4958 && rel + 1 < rel_end
4ce794b7 4959 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 4960 {
8387904d 4961 if (h != NULL)
8c5b4e52 4962 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 4963 }
e86ce104
AM
4964 /* Fall through. */
4965
65f38f15
AM
4966 case R_PPC64_ADDR16:
4967 case R_PPC64_ADDR16_DS:
4968 case R_PPC64_ADDR16_HA:
4969 case R_PPC64_ADDR16_HI:
f9c6b907
AM
4970 case R_PPC64_ADDR16_HIGH:
4971 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
4972 case R_PPC64_ADDR16_HIGHER:
4973 case R_PPC64_ADDR16_HIGHERA:
4974 case R_PPC64_ADDR16_HIGHEST:
4975 case R_PPC64_ADDR16_HIGHESTA:
4976 case R_PPC64_ADDR16_LO:
4977 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
4978 case R_PPC64_D34:
4979 case R_PPC64_D34_LO:
4980 case R_PPC64_D34_HI30:
4981 case R_PPC64_D34_HA30:
4982 case R_PPC64_ADDR16_HIGHER34:
4983 case R_PPC64_ADDR16_HIGHERA34:
4984 case R_PPC64_ADDR16_HIGHEST34:
4985 case R_PPC64_ADDR16_HIGHESTA34:
4986 case R_PPC64_D28:
0e1862bb 4987 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
4988 && rel->r_addend == 0)
4989 {
4990 /* We may need a .plt entry if this reloc refers to a
4991 function in a shared lib. */
5663e321 4992 if (!update_plt_info (abfd, &h->plt.plist, 0))
a345bc8d
AM
4993 return FALSE;
4994 h->pointer_equality_needed = 1;
4995 }
4996 /* Fall through. */
4997
4998 case R_PPC64_REL30:
4999 case R_PPC64_REL32:
5000 case R_PPC64_REL64:
65f38f15 5001 case R_PPC64_ADDR32:
65f38f15
AM
5002 case R_PPC64_UADDR16:
5003 case R_PPC64_UADDR32:
5004 case R_PPC64_UADDR64:
5bd4f169 5005 case R_PPC64_TOC:
0e1862bb 5006 if (h != NULL && !bfd_link_pic (info))
81848ca0 5007 /* We may need a copy reloc. */
f5385ebf 5008 h->non_got_ref = 1;
81848ca0 5009
41bd81ab 5010 /* Don't propagate .opd relocs. */
b9399fcf 5011 if (NO_OPD_RELOCS && is_opd)
e86ce104 5012 break;
e86ce104 5013
65f38f15
AM
5014 /* If we are creating a shared library, and this is a reloc
5015 against a global symbol, or a non PC relative reloc
5016 against a local symbol, then we need to copy the reloc
5017 into the shared library. However, if we are linking with
5018 -Bsymbolic, we do not need to copy a reloc against a
5019 global symbol which is defined in an object we are
5020 including in the link (i.e., DEF_REGULAR is set). At
5021 this point we have not seen all the input files, so it is
5022 possible that DEF_REGULAR is not set now but will be set
5023 later (it is never cleared). In case of a weak definition,
5024 DEF_REGULAR may be cleared later by a strong definition in
5025 a shared library. We account for that possibility below by
f4656909 5026 storing information in the dyn_relocs field of the hash
65f38f15
AM
5027 table entry. A similar situation occurs when creating
5028 shared libraries and symbol visibility changes render the
5029 symbol local.
5030
5031 If on the other hand, we are creating an executable, we
5032 may need to keep relocations for symbols satisfied by a
5033 dynamic library if we manage to avoid copy relocs for the
5034 symbol. */
411e1bfb 5035 dodyn:
0e1862bb 5036 if ((bfd_link_pic (info)
1d483afe 5037 && (must_be_dyn_reloc (info, r_type)
65f38f15 5038 || (h != NULL
198f1157 5039 && (!SYMBOLIC_BIND (info, h)
65f38f15 5040 || h->root.type == bfd_link_hash_defweak
f5385ebf 5041 || !h->def_regular))))
f4656909 5042 || (ELIMINATE_COPY_RELOCS
0e1862bb 5043 && !bfd_link_pic (info)
65f38f15
AM
5044 && h != NULL
5045 && (h->root.type == bfd_link_hash_defweak
25f23106 5046 || !h->def_regular))
0e1862bb 5047 || (!bfd_link_pic (info)
25f23106 5048 && ifunc != NULL))
5bd4f169 5049 {
65f38f15
AM
5050 /* We must copy these reloc types into the output file.
5051 Create a reloc section in dynobj and make room for
5052 this reloc. */
5bd4f169
AM
5053 if (sreloc == NULL)
5054 {
83bac4b0
NC
5055 sreloc = _bfd_elf_make_dynamic_reloc_section
5056 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5057
5bd4f169 5058 if (sreloc == NULL)
83bac4b0 5059 return FALSE;
5bd4f169
AM
5060 }
5061
65f38f15
AM
5062 /* If this is a global symbol, we count the number of
5063 relocations we need for this symbol. */
5064 if (h != NULL)
5065 {
19e08130
AM
5066 struct elf_dyn_relocs *p;
5067 struct elf_dyn_relocs **head;
5068
ec338859 5069 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5070 p = *head;
5071 if (p == NULL || p->sec != sec)
5072 {
5073 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5074 if (p == NULL)
5075 return FALSE;
5076 p->next = *head;
5077 *head = p;
5078 p->sec = sec;
5079 p->count = 0;
5080 p->pc_count = 0;
5081 }
5082 p->count += 1;
5083 if (!must_be_dyn_reloc (info, r_type))
5084 p->pc_count += 1;
65f38f15
AM
5085 }
5086 else
5087 {
ec338859
AM
5088 /* Track dynamic relocs needed for local syms too.
5089 We really need local syms available to do this
5090 easily. Oh well. */
19e08130
AM
5091 struct ppc_dyn_relocs *p;
5092 struct ppc_dyn_relocs **head;
5093 bfd_boolean is_ifunc;
ec338859 5094 asection *s;
6edfbbad 5095 void *vpp;
87d72d41 5096 Elf_Internal_Sym *isym;
6edfbbad 5097
87d72d41
AM
5098 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5099 abfd, r_symndx);
5100 if (isym == NULL)
b34976b6 5101 return FALSE;
ec338859 5102
87d72d41
AM
5103 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5104 if (s == NULL)
5105 s = sec;
5106
6edfbbad 5107 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5108 head = (struct ppc_dyn_relocs **) vpp;
5109 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5110 p = *head;
5111 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5112 p = p->next;
5113 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5114 {
5115 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5116 if (p == NULL)
5117 return FALSE;
5118 p->next = *head;
5119 *head = p;
5120 p->sec = sec;
5121 p->ifunc = is_ifunc;
5122 p->count = 0;
5123 }
5124 p->count += 1;
ec338859 5125 }
65f38f15 5126 }
5bd4f169 5127 break;
65f38f15
AM
5128
5129 default:
96e0dda4 5130 break;
5bd4f169
AM
5131 }
5132 }
5133
b34976b6 5134 return TRUE;
5bd4f169
AM
5135}
5136
ee67d69a
AM
5137/* Merge backend specific data from an object file to the output
5138 object file when linking. */
5139
5140static bfd_boolean
50e03d47 5141ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5142{
50e03d47 5143 bfd *obfd = info->output_bfd;
ee67d69a
AM
5144 unsigned long iflags, oflags;
5145
5146 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5147 return TRUE;
5148
5149 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5150 return TRUE;
5151
50e03d47 5152 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
5153 return FALSE;
5154
5155 iflags = elf_elfheader (ibfd)->e_flags;
5156 oflags = elf_elfheader (obfd)->e_flags;
5157
f6c7c3e8 5158 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5159 {
4eca0228 5160 _bfd_error_handler
695344c0 5161 /* xgettext:c-format */
871b3ab2 5162 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
5163 bfd_set_error (bfd_error_bad_value);
5164 return FALSE;
5165 }
f6c7c3e8 5166 else if (iflags != oflags && iflags != 0)
ee67d69a 5167 {
4eca0228 5168 _bfd_error_handler
695344c0 5169 /* xgettext:c-format */
871b3ab2 5170 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5171 ibfd, iflags, oflags);
5172 bfd_set_error (bfd_error_bad_value);
5173 return FALSE;
5174 }
5175
4a91d0ba
AM
5176 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5177 return FALSE;
005d79fd 5178
ee67d69a 5179 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5180 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5181}
5182
5183static bfd_boolean
5184ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5185{
5186 /* Print normal ELF private data. */
5187 _bfd_elf_print_private_bfd_data (abfd, ptr);
5188
5189 if (elf_elfheader (abfd)->e_flags != 0)
5190 {
5191 FILE *file = ptr;
5192
ee67d69a
AM
5193 fprintf (file, _("private flags = 0x%lx:"),
5194 elf_elfheader (abfd)->e_flags);
5195
5196 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5197 fprintf (file, _(" [abiv%ld]"),
5198 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5199 fputc ('\n', file);
5200 }
5201
5202 return TRUE;
5203}
5204
8387904d 5205/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5206 of the code entry point, and its section, which must be in the same
5207 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5208
5209static bfd_vma
5210opd_entry_value (asection *opd_sec,
5211 bfd_vma offset,
5212 asection **code_sec,
aef36ac1
AM
5213 bfd_vma *code_off,
5214 bfd_boolean in_code_sec)
8387904d
AM
5215{
5216 bfd *opd_bfd = opd_sec->owner;
8860955f 5217 Elf_Internal_Rela *relocs;
8387904d 5218 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5219 bfd_vma val;
8387904d 5220
9f296da3
AM
5221 /* No relocs implies we are linking a --just-symbols object, or looking
5222 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5223 if (opd_sec->reloc_count == 0)
5224 {
729eabd5 5225 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5226
729eabd5
AM
5227 if (contents == NULL)
5228 {
5229 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5230 return (bfd_vma) -1;
5231 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5232 }
ee1e4ede 5233
dbb3fbbb 5234 /* PR 17512: file: 64b9dfbb. */
451dfd38 5235 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5236 return (bfd_vma) -1;
5237
729eabd5 5238 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5239 if (code_sec != NULL)
5240 {
5241 asection *sec, *likely = NULL;
ee1e4ede 5242
aef36ac1 5243 if (in_code_sec)
4b85d634 5244 {
aef36ac1
AM
5245 sec = *code_sec;
5246 if (sec->vma <= val
5247 && val < sec->vma + sec->size)
5248 likely = sec;
5249 else
5250 val = -1;
5251 }
5252 else
5253 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5254 if (sec->vma <= val
5255 && (sec->flags & SEC_LOAD) != 0
5256 && (sec->flags & SEC_ALLOC) != 0)
5257 likely = sec;
5258 if (likely != NULL)
5259 {
5260 *code_sec = likely;
5261 if (code_off != NULL)
5262 *code_off = val - likely->vma;
4b85d634
AM
5263 }
5264 }
aef36ac1 5265 return val;
4b85d634
AM
5266 }
5267
0c8d6e5c 5268 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5269
729eabd5 5270 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5271 if (relocs == NULL)
5272 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5273 /* PR 17512: file: df8e1fd6. */
5274 if (relocs == NULL)
5275 return (bfd_vma) -1;
645ea6a9 5276
8387904d 5277 /* Go find the opd reloc at the sym address. */
8860955f 5278 lo = relocs;
8387904d 5279 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5280 val = (bfd_vma) -1;
8387904d
AM
5281 while (lo < hi)
5282 {
5283 look = lo + (hi - lo) / 2;
5284 if (look->r_offset < offset)
5285 lo = look + 1;
5286 else if (look->r_offset > offset)
5287 hi = look;
5288 else
5289 {
0ffa91dd
NC
5290 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5291
8387904d
AM
5292 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5293 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5294 {
5295 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5296 asection *sec = NULL;
8387904d 5297
b53dfeb2
AM
5298 if (symndx >= symtab_hdr->sh_info
5299 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5300 {
5301 struct elf_link_hash_entry **sym_hashes;
5302 struct elf_link_hash_entry *rh;
5303
5304 sym_hashes = elf_sym_hashes (opd_bfd);
5305 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5306 if (rh != NULL)
5307 {
5308 rh = elf_follow_link (rh);
bb854a36
AM
5309 if (rh->root.type != bfd_link_hash_defined
5310 && rh->root.type != bfd_link_hash_defweak)
5311 break;
5312 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5313 {
bb854a36
AM
5314 val = rh->root.u.def.value;
5315 sec = rh->root.u.def.section;
b53dfeb2
AM
5316 }
5317 }
5318 }
5319
5320 if (sec == NULL)
5321 {
5322 Elf_Internal_Sym *sym;
5323
5324 if (symndx < symtab_hdr->sh_info)
5325 {
5326 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5327 if (sym == NULL)
5328 {
5329 size_t symcnt = symtab_hdr->sh_info;
5330 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5331 symcnt, 0,
5332 NULL, NULL, NULL);
5333 if (sym == NULL)
5334 break;
5335 symtab_hdr->contents = (bfd_byte *) sym;
5336 }
5337 sym += symndx;
128205bb
AM
5338 }
5339 else
5340 {
b53dfeb2
AM
5341 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5342 1, symndx,
5343 NULL, NULL, NULL);
128205bb
AM
5344 if (sym == NULL)
5345 break;
128205bb 5346 }
b53dfeb2
AM
5347 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5348 if (sec == NULL)
5349 break;
5350 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5351 val = sym->st_value;
8387904d 5352 }
b53dfeb2 5353
8387904d
AM
5354 val += look->r_addend;
5355 if (code_off != NULL)
5356 *code_off = val;
5357 if (code_sec != NULL)
aef36ac1
AM
5358 {
5359 if (in_code_sec && *code_sec != sec)
5360 return -1;
5361 else
5362 *code_sec = sec;
5363 }
b53dfeb2 5364 if (sec->output_section != NULL)
8387904d 5365 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5366 }
5367 break;
5368 }
5369 }
645ea6a9 5370
645ea6a9 5371 return val;
8387904d
AM
5372}
5373
aef36ac1
AM
5374/* If the ELF symbol SYM might be a function in SEC, return the
5375 function size and set *CODE_OFF to the function's entry point,
5376 otherwise return zero. */
9f296da3 5377
aef36ac1
AM
5378static bfd_size_type
5379ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5380 bfd_vma *code_off)
9f296da3 5381{
aef36ac1
AM
5382 bfd_size_type size;
5383
5384 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5385 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5386 return 0;
5387
5388 size = 0;
5389 if (!(sym->flags & BSF_SYNTHETIC))
5390 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5391
5392 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5393 {
b07bca4e
AM
5394 struct _opd_sec_data *opd = get_opd_info (sym->section);
5395 bfd_vma symval = sym->value;
5396
5397 if (opd != NULL
5398 && opd->adjust != NULL
5399 && elf_section_data (sym->section)->relocs != NULL)
5400 {
5401 /* opd_entry_value will use cached relocs that have been
5402 adjusted, but with raw symbols. That means both local
5403 and global symbols need adjusting. */
5404 long adjust = opd->adjust[OPD_NDX (symval)];
5405 if (adjust == -1)
5406 return 0;
5407 symval += adjust;
5408 }
5409
5410 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5411 &sec, code_off, TRUE) == (bfd_vma) -1)
5412 return 0;
5413 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5414 symbol. This size has nothing to do with the code size of the
5415 function, which is what we're supposed to return, but the
5416 code size isn't available without looking up the dot-sym.
5417 However, doing that would be a waste of time particularly
5418 since elf_find_function will look at the dot-sym anyway.
5419 Now, elf_find_function will keep the largest size of any
5420 function sym found at the code address of interest, so return
5421 1 here to avoid it incorrectly caching a larger function size
5422 for a small function. This does mean we return the wrong
5423 size for a new-ABI function of size 24, but all that does is
5424 disable caching for such functions. */
5425 if (size == 24)
5426 size = 1;
9f296da3 5427 }
aef36ac1
AM
5428 else
5429 {
5430 if (sym->section != sec)
5431 return 0;
5432 *code_off = sym->value;
5433 }
5434 if (size == 0)
5435 size = 1;
5436 return size;
9f296da3
AM
5437}
5438
f378ab09
AM
5439/* Return true if symbol is a strong function defined in an ELFv2
5440 object with st_other localentry bits of zero, ie. its local entry
5441 point coincides with its global entry point. */
5442
5443static bfd_boolean
5444is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5445{
5446 return (h != NULL
5447 && h->type == STT_FUNC
5448 && h->root.type == bfd_link_hash_defined
5449 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 5450 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
5451 && is_ppc64_elf (h->root.u.def.section->owner)
5452 && abiversion (h->root.u.def.section->owner) >= 2);
5453}
5454
854b41e7
AM
5455/* Return true if symbol is defined in a regular object file. */
5456
5457static bfd_boolean
5458is_static_defined (struct elf_link_hash_entry *h)
5459{
5460 return ((h->root.type == bfd_link_hash_defined
5461 || h->root.type == bfd_link_hash_defweak)
5462 && h->root.u.def.section != NULL
5463 && h->root.u.def.section->output_section != NULL);
5464}
5465
b31867b6
AM
5466/* If FDH is a function descriptor symbol, return the associated code
5467 entry symbol if it is defined. Return NULL otherwise. */
5468
5469static struct ppc_link_hash_entry *
5470defined_code_entry (struct ppc_link_hash_entry *fdh)
5471{
5472 if (fdh->is_func_descriptor)
5473 {
5474 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5475 if (fh->elf.root.type == bfd_link_hash_defined
5476 || fh->elf.root.type == bfd_link_hash_defweak)
5477 return fh;
5478 }
5479 return NULL;
5480}
5481
5482/* If FH is a function code entry symbol, return the associated
5483 function descriptor symbol if it is defined. Return NULL otherwise. */
5484
5485static struct ppc_link_hash_entry *
5486defined_func_desc (struct ppc_link_hash_entry *fh)
5487{
5488 if (fh->oh != NULL
5489 && fh->oh->is_func_descriptor)
5490 {
5491 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5492 if (fdh->elf.root.type == bfd_link_hash_defined
5493 || fdh->elf.root.type == bfd_link_hash_defweak)
5494 return fdh;
5495 }
5496 return NULL;
5497}
5498
8c5b4e52
AM
5499static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5500
5501/* Garbage collect sections, after first dealing with dot-symbols. */
5502
5503static bfd_boolean
5504ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5505{
5506 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5507
5508 if (htab != NULL && htab->need_func_desc_adj)
5509 {
5510 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5511 htab->need_func_desc_adj = 0;
5512 }
5513 return bfd_elf_gc_sections (abfd, info);
5514}
5515
74f0fb50
AM
5516/* Mark all our entry sym sections, both opd and code section. */
5517
5518static void
5519ppc64_elf_gc_keep (struct bfd_link_info *info)
5520{
5521 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5522 struct bfd_sym_chain *sym;
5523
4dfe6ac6
NC
5524 if (htab == NULL)
5525 return;
5526
74f0fb50
AM
5527 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5528 {
b31867b6 5529 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5530 asection *sec;
5531
5532 eh = (struct ppc_link_hash_entry *)
b31867b6 5533 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5534 if (eh == NULL)
5535 continue;
5536 if (eh->elf.root.type != bfd_link_hash_defined
5537 && eh->elf.root.type != bfd_link_hash_defweak)
5538 continue;
5539
b31867b6
AM
5540 fh = defined_code_entry (eh);
5541 if (fh != NULL)
74f0fb50 5542 {
b31867b6 5543 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5544 sec->flags |= SEC_KEEP;
5545 }
5546 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5547 && opd_entry_value (eh->elf.root.u.def.section,
5548 eh->elf.root.u.def.value,
aef36ac1 5549 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5550 sec->flags |= SEC_KEEP;
5551
5552 sec = eh->elf.root.u.def.section;
5553 sec->flags |= SEC_KEEP;
5554 }
5555}
5556
64d03ab5
AM
5557/* Mark sections containing dynamically referenced symbols. When
5558 building shared libraries, we must assume that any visible symbol is
5559 referenced. */
5560
5561static bfd_boolean
5562ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5563{
5564 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5565 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5566 struct ppc_link_hash_entry *fdh;
b407645f 5567 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5568
64d03ab5 5569 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5570 fdh = defined_func_desc (eh);
5571 if (fdh != NULL)
5572 eh = fdh;
64d03ab5
AM
5573
5574 if ((eh->elf.root.type == bfd_link_hash_defined
5575 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5576 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5577 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5578 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5579 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5580 && (!bfd_link_executable (info)
e278ae05 5581 || info->gc_keep_exported
b407645f
AM
5582 || info->export_dynamic
5583 || (eh->elf.dynamic
5584 && d != NULL
2cdcc330
AM
5585 && (*d->match) (&d->head, NULL,
5586 eh->elf.root.root.string)))
e278ae05 5587 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5588 || !bfd_hide_sym_by_version (info->version_info,
5589 eh->elf.root.root.string)))))
64d03ab5
AM
5590 {
5591 asection *code_sec;
b31867b6 5592 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5593
5594 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5595
5596 /* Function descriptor syms cause the associated
5597 function code sym section to be marked. */
b31867b6
AM
5598 fh = defined_code_entry (eh);
5599 if (fh != NULL)
5600 {
5601 code_sec = fh->elf.root.u.def.section;
5602 code_sec->flags |= SEC_KEEP;
5603 }
64d03ab5
AM
5604 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5605 && opd_entry_value (eh->elf.root.u.def.section,
5606 eh->elf.root.u.def.value,
aef36ac1 5607 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5608 code_sec->flags |= SEC_KEEP;
5609 }
5610
5611 return TRUE;
5612}
5613
5bd4f169
AM
5614/* Return the section that should be marked against GC for a given
5615 relocation. */
5616
5617static asection *
4ce794b7 5618ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5619 struct bfd_link_info *info,
4ce794b7
AM
5620 Elf_Internal_Rela *rel,
5621 struct elf_link_hash_entry *h,
5622 Elf_Internal_Sym *sym)
5bd4f169 5623{
ccfa59ea
AM
5624 asection *rsec;
5625
ccfa59ea
AM
5626 /* Syms return NULL if we're marking .opd, so we avoid marking all
5627 function sections, as all functions are referenced in .opd. */
5628 rsec = NULL;
5629 if (get_opd_info (sec) != NULL)
5630 return rsec;
1e2f5b6e 5631
5bd4f169
AM
5632 if (h != NULL)
5633 {
04c9666a 5634 enum elf_ppc64_reloc_type r_type;
b31867b6 5635 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5636
4ce794b7 5637 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5638 switch (r_type)
5bd4f169
AM
5639 {
5640 case R_PPC64_GNU_VTINHERIT:
5641 case R_PPC64_GNU_VTENTRY:
5642 break;
5643
5644 default:
5645 switch (h->root.type)
5646 {
5647 case bfd_link_hash_defined:
5648 case bfd_link_hash_defweak:
ccfa59ea 5649 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5650 fdh = defined_func_desc (eh);
5651 if (fdh != NULL)
8c5b4e52
AM
5652 {
5653 /* -mcall-aixdesc code references the dot-symbol on
5654 a call reloc. Mark the function descriptor too
5655 against garbage collection. */
5656 fdh->elf.mark = 1;
60d67dc8
AM
5657 if (fdh->elf.is_weakalias)
5658 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5659 eh = fdh;
5660 }
1e2f5b6e
AM
5661
5662 /* Function descriptor syms cause the associated
5663 function code sym section to be marked. */
b31867b6
AM
5664 fh = defined_code_entry (eh);
5665 if (fh != NULL)
ccfa59ea
AM
5666 {
5667 /* They also mark their opd section. */
74f0fb50 5668 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5669
b31867b6 5670 rsec = fh->elf.root.u.def.section;
ccfa59ea 5671 }
8387904d
AM
5672 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5673 && opd_entry_value (eh->elf.root.u.def.section,
5674 eh->elf.root.u.def.value,
aef36ac1 5675 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5676 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5677 else
1e2f5b6e
AM
5678 rsec = h->root.u.def.section;
5679 break;
5bd4f169
AM
5680
5681 case bfd_link_hash_common:
1e2f5b6e
AM
5682 rsec = h->root.u.c.p->section;
5683 break;
5bd4f169
AM
5684
5685 default:
fb34365b 5686 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5687 }
5688 }
5689 }
5690 else
5691 {
74f0fb50 5692 struct _opd_sec_data *opd;
1e2f5b6e
AM
5693
5694 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5695 opd = get_opd_info (rsec);
5696 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5697 {
74f0fb50 5698 rsec->gc_mark = 1;
ccfa59ea 5699
51aecdc5 5700 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5701 }
5bd4f169
AM
5702 }
5703
1e2f5b6e 5704 return rsec;
5bd4f169
AM
5705}
5706
deb0e272
AM
5707/* The maximum size of .sfpr. */
5708#define SFPR_MAX (218*4)
5709
5710struct sfpr_def_parms
5711{
699733f6
AM
5712 const char name[12];
5713 unsigned char lo, hi;
2cdcc330
AM
5714 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5715 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5716};
5717
a4b6fadd
AM
5718/* Auto-generate _save*, _rest* functions in .sfpr.
5719 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5720 instead. */
deb0e272 5721
4dfe6ac6 5722static bfd_boolean
a4b6fadd
AM
5723sfpr_define (struct bfd_link_info *info,
5724 const struct sfpr_def_parms *parm,
5725 asection *stub_sec)
deb0e272
AM
5726{
5727 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5728 unsigned int i;
5729 size_t len = strlen (parm->name);
5730 bfd_boolean writing = FALSE;
699733f6 5731 char sym[16];
deb0e272 5732
4dfe6ac6
NC
5733 if (htab == NULL)
5734 return FALSE;
5735
deb0e272
AM
5736 memcpy (sym, parm->name, len);
5737 sym[len + 2] = 0;
5738
5739 for (i = parm->lo; i <= parm->hi; i++)
5740 {
a4b6fadd 5741 struct ppc_link_hash_entry *h;
deb0e272
AM
5742
5743 sym[len + 0] = i / 10 + '0';
5744 sym[len + 1] = i % 10 + '0';
a4b6fadd 5745 h = (struct ppc_link_hash_entry *)
b32547cd 5746 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 5747 if (stub_sec != NULL)
deb0e272 5748 {
a4b6fadd
AM
5749 if (h != NULL
5750 && h->elf.root.type == bfd_link_hash_defined
5751 && h->elf.root.u.def.section == htab->sfpr)
5752 {
5753 struct elf_link_hash_entry *s;
5754 char buf[32];
5755 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5756 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5757 if (s == NULL)
5758 return FALSE;
5759 if (s->root.type == bfd_link_hash_new
5760 || (s->root.type = bfd_link_hash_defined
5761 && s->root.u.def.section == stub_sec))
5762 {
5763 s->root.type = bfd_link_hash_defined;
5764 s->root.u.def.section = stub_sec;
7dda8d3c 5765 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5766 + h->elf.root.u.def.value);
5767 s->ref_regular = 1;
5768 s->def_regular = 1;
5769 s->ref_regular_nonweak = 1;
5770 s->forced_local = 1;
5771 s->non_elf = 0;
5772 s->root.linker_def = 1;
5773 }
5774 }
5775 continue;
5776 }
5777 if (h != NULL)
5778 {
5779 h->save_res = 1;
5780 if (!h->elf.def_regular)
deb0e272 5781 {
a4b6fadd
AM
5782 h->elf.root.type = bfd_link_hash_defined;
5783 h->elf.root.u.def.section = htab->sfpr;
5784 h->elf.root.u.def.value = htab->sfpr->size;
5785 h->elf.type = STT_FUNC;
5786 h->elf.def_regular = 1;
b32547cd 5787 h->elf.non_elf = 0;
a4b6fadd
AM
5788 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5789 writing = TRUE;
deb0e272 5790 if (htab->sfpr->contents == NULL)
a4b6fadd 5791 {
2cdcc330
AM
5792 htab->sfpr->contents
5793 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5794 if (htab->sfpr->contents == NULL)
5795 return FALSE;
5796 }
deb0e272
AM
5797 }
5798 }
5799 if (writing)
5800 {
5801 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5802 if (i != parm->hi)
5803 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5804 else
5805 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5806 htab->sfpr->size = p - htab->sfpr->contents;
5807 }
5808 }
5809
5810 return TRUE;
5811}
5812
5813static bfd_byte *
5814savegpr0 (bfd *abfd, bfd_byte *p, int r)
5815{
5816 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5817 return p + 4;
5818}
5819
5820static bfd_byte *
5821savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5822{
5823 p = savegpr0 (abfd, p, r);
a078d95a 5824 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5825 p = p + 4;
5826 bfd_put_32 (abfd, BLR, p);
5827 return p + 4;
5828}
5829
5830static bfd_byte *
5831restgpr0 (bfd *abfd, bfd_byte *p, int r)
5832{
5833 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5834 return p + 4;
5835}
5836
5837static bfd_byte *
5838restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5839{
a078d95a 5840 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5841 p = p + 4;
5842 p = restgpr0 (abfd, p, r);
5843 bfd_put_32 (abfd, MTLR_R0, p);
5844 p = p + 4;
5845 if (r == 29)
5846 {
5847 p = restgpr0 (abfd, p, 30);
5848 p = restgpr0 (abfd, p, 31);
5849 }
5850 bfd_put_32 (abfd, BLR, p);
5851 return p + 4;
5852}
5853
5854static bfd_byte *
5855savegpr1 (bfd *abfd, bfd_byte *p, int r)
5856{
5857 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5858 return p + 4;
5859}
5860
5861static bfd_byte *
5862savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5863{
5864 p = savegpr1 (abfd, p, r);
5865 bfd_put_32 (abfd, BLR, p);
5866 return p + 4;
5867}
5868
5869static bfd_byte *
5870restgpr1 (bfd *abfd, bfd_byte *p, int r)
5871{
5872 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5873 return p + 4;
5874}
5875
5876static bfd_byte *
5877restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5878{
5879 p = restgpr1 (abfd, p, r);
5880 bfd_put_32 (abfd, BLR, p);
5881 return p + 4;
5882}
5883
5884static bfd_byte *
5885savefpr (bfd *abfd, bfd_byte *p, int r)
5886{
5887 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5888 return p + 4;
5889}
5890
5891static bfd_byte *
5892savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5893{
5894 p = savefpr (abfd, p, r);
a078d95a 5895 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5896 p = p + 4;
5897 bfd_put_32 (abfd, BLR, p);
5898 return p + 4;
5899}
5900
5901static bfd_byte *
5902restfpr (bfd *abfd, bfd_byte *p, int r)
5903{
5904 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5905 return p + 4;
5906}
5907
5908static bfd_byte *
5909restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5910{
a078d95a 5911 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5912 p = p + 4;
5913 p = restfpr (abfd, p, r);
5914 bfd_put_32 (abfd, MTLR_R0, p);
5915 p = p + 4;
5916 if (r == 29)
5917 {
5918 p = restfpr (abfd, p, 30);
5919 p = restfpr (abfd, p, 31);
5920 }
5921 bfd_put_32 (abfd, BLR, p);
5922 return p + 4;
5923}
5924
5925static bfd_byte *
5926savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5927{
5928 p = savefpr (abfd, p, r);
5929 bfd_put_32 (abfd, BLR, p);
5930 return p + 4;
5931}
5932
5933static bfd_byte *
5934restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5935{
5936 p = restfpr (abfd, p, r);
5937 bfd_put_32 (abfd, BLR, p);
5938 return p + 4;
5939}
5940
5941static bfd_byte *
5942savevr (bfd *abfd, bfd_byte *p, int r)
5943{
5944 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5945 p = p + 4;
5946 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5947 return p + 4;
5948}
5949
5950static bfd_byte *
5951savevr_tail (bfd *abfd, bfd_byte *p, int r)
5952{
5953 p = savevr (abfd, p, r);
5954 bfd_put_32 (abfd, BLR, p);
5955 return p + 4;
5956}
5957
5958static bfd_byte *
5959restvr (bfd *abfd, bfd_byte *p, int r)
5960{
5961 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5962 p = p + 4;
5963 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5964 return p + 4;
5965}
5966
5967static bfd_byte *
5968restvr_tail (bfd *abfd, bfd_byte *p, int r)
5969{
5970 p = restvr (abfd, p, r);
5971 bfd_put_32 (abfd, BLR, p);
5972 return p + 4;
5973}
5974
e86ce104
AM
5975/* Called via elf_link_hash_traverse to transfer dynamic linking
5976 information on function code symbol entries to their corresponding
5977 function descriptor symbol entries. */
deb0e272 5978
b34976b6 5979static bfd_boolean
4ce794b7 5980func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 5981{
e86ce104 5982 struct bfd_link_info *info;
65f38f15 5983 struct ppc_link_hash_table *htab;
50bc7936
AM
5984 struct ppc_link_hash_entry *fh;
5985 struct ppc_link_hash_entry *fdh;
5986 bfd_boolean force_local;
5bd4f169 5987
50bc7936
AM
5988 fh = (struct ppc_link_hash_entry *) h;
5989 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 5990 return TRUE;
e86ce104 5991
8c5b4e52
AM
5992 if (!fh->is_func)
5993 return TRUE;
5994
5995 if (fh->elf.root.root.string[0] != '.'
5996 || fh->elf.root.root.string[1] == '\0')
5997 return TRUE;
5998
4ce794b7 5999 info = inf;
65f38f15 6000 htab = ppc_hash_table (info);
4dfe6ac6
NC
6001 if (htab == NULL)
6002 return FALSE;
5bd4f169 6003
8c5b4e52
AM
6004 /* Find the corresponding function descriptor symbol. */
6005 fdh = lookup_fdh (fh, htab);
6006
c09bdfe5
AM
6007 /* Resolve undefined references to dot-symbols as the value
6008 in the function descriptor, if we have one in a regular object.
6009 This is to satisfy cases like ".quad .foo". Calls to functions
6010 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6011 if ((fh->elf.root.type == bfd_link_hash_undefined
6012 || fh->elf.root.type == bfd_link_hash_undefweak)
6013 && (fdh->elf.root.type == bfd_link_hash_defined
6014 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6015 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6016 && opd_entry_value (fdh->elf.root.u.def.section,
6017 fdh->elf.root.u.def.value,
c09bdfe5 6018 &fh->elf.root.u.def.section,
aef36ac1 6019 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6020 {
b31867b6 6021 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6022 fh->elf.forced_local = 1;
b31867b6
AM
6023 fh->elf.def_regular = fdh->elf.def_regular;
6024 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6025 }
6026
8c5b4e52
AM
6027 if (!fh->elf.dynamic)
6028 {
6029 struct plt_entry *ent;
5bd4f169 6030
8c5b4e52
AM
6031 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6032 if (ent->plt.refcount > 0)
6033 break;
6034 if (ent == NULL)
6035 return TRUE;
6036 }
5bd4f169 6037
8c5b4e52 6038 /* Create a descriptor as undefined if necessary. */
50bc7936 6039 if (fdh == NULL
0e1862bb 6040 && !bfd_link_executable (info)
50bc7936
AM
6041 && (fh->elf.root.type == bfd_link_hash_undefined
6042 || fh->elf.root.type == bfd_link_hash_undefweak))
6043 {
908b32fc 6044 fdh = make_fdh (info, fh);
bb700d78
AM
6045 if (fdh == NULL)
6046 return FALSE;
50bc7936 6047 }
648cca2c 6048
8c5b4e52 6049 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6050 if (fdh != NULL
6051 && fdh->fake
8c5b4e52
AM
6052 && (fh->elf.root.type == bfd_link_hash_defined
6053 || fh->elf.root.type == bfd_link_hash_defweak))
6054 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6055
8c5b4e52
AM
6056 /* Transfer dynamic linking information to the function descriptor. */
6057 if (fdh != NULL)
6058 {
f5385ebf
AM
6059 fdh->elf.ref_regular |= fh->elf.ref_regular;
6060 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6061 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6062 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6063 fdh->elf.dynamic |= fh->elf.dynamic;
6064 fdh->elf.needs_plt |= (fh->elf.needs_plt
6065 || fh->elf.type == STT_FUNC
6066 || fh->elf.type == STT_GNU_IFUNC);
6067 move_plt_plist (fh, fdh);
6068
6069 if (!fdh->elf.forced_local
6070 && fh->elf.dynindx != -1)
6071 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6072 return FALSE;
e86ce104
AM
6073 }
6074
50bc7936
AM
6075 /* Now that the info is on the function descriptor, clear the
6076 function code sym info. Any function code syms for which we
6077 don't have a definition in a regular file, we force local.
6078 This prevents a shared library from exporting syms that have
6079 been imported from another library. Function code syms that
6080 are really in the library we must leave global to prevent the
6081 linker dragging in a definition from a static library. */
93f3fa99
AM
6082 force_local = (!fh->elf.def_regular
6083 || fdh == NULL
6084 || !fdh->elf.def_regular
6085 || fdh->elf.forced_local);
50bc7936
AM
6086 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6087
b34976b6 6088 return TRUE;
e86ce104 6089}
40b8271b 6090
a4b6fadd
AM
6091static const struct sfpr_def_parms save_res_funcs[] =
6092 {
6093 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6094 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6095 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6096 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6097 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6098 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6099 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6100 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6101 { "._savef", 14, 31, savefpr, savefpr1_tail },
6102 { "._restf", 14, 31, restfpr, restfpr1_tail },
6103 { "_savevr_", 20, 31, savevr, savevr_tail },
6104 { "_restvr_", 20, 31, restvr, restvr_tail }
6105 };
6106
e86ce104 6107/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6108 this hook to a) provide some gcc support functions, and b) transfer
6109 dynamic linking information gathered so far on function code symbol
6110 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6111
b34976b6 6112static bfd_boolean
4ce794b7
AM
6113ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6114 struct bfd_link_info *info)
e86ce104
AM
6115{
6116 struct ppc_link_hash_table *htab;
6117
6118 htab = ppc_hash_table (info);
4dfe6ac6
NC
6119 if (htab == NULL)
6120 return FALSE;
6121
b32547cd
AM
6122 /* Provide any missing _save* and _rest* functions. */
6123 if (htab->sfpr != NULL)
6124 {
6125 unsigned int i;
6126
6127 htab->sfpr->size = 0;
6128 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6129 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6130 return FALSE;
6131 if (htab->sfpr->size == 0)
6132 htab->sfpr->flags |= SEC_EXCLUDE;
6133 }
6134
6135 if (bfd_link_relocatable (info))
6136 return TRUE;
6137
6138 if (htab->elf.hgot != NULL)
dba6fa9b
AM
6139 {
6140 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6141 /* Make .TOC. defined so as to prevent it being made dynamic.
6142 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6143 if (!htab->elf.hgot->def_regular
6144 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6145 {
6146 htab->elf.hgot->root.type = bfd_link_hash_defined;
6147 htab->elf.hgot->root.u.def.value = 0;
6148 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6149 htab->elf.hgot->def_regular = 1;
6150 htab->elf.hgot->root.linker_def = 1;
6151 }
dba6fa9b 6152 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6153 htab->elf.hgot->other
6154 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6155 }
c66bb0ee 6156
8c5b4e52
AM
6157 if (htab->need_func_desc_adj)
6158 {
6159 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6160 htab->need_func_desc_adj = 0;
6161 }
805fc799 6162
b34976b6 6163 return TRUE;
e86ce104
AM
6164}
6165
98bbb1b8 6166/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 6167
98bbb1b8 6168static asection *
a345bc8d
AM
6169readonly_dynrelocs (struct elf_link_hash_entry *h)
6170{
6171 struct ppc_link_hash_entry *eh;
6172 struct elf_dyn_relocs *p;
6173
6174 eh = (struct ppc_link_hash_entry *) h;
6175 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6176 {
6177 asection *s = p->sec->output_section;
6178
6179 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 6180 return p->sec;
a345bc8d 6181 }
98bbb1b8 6182 return NULL;
a345bc8d
AM
6183}
6184
d311bc8b 6185/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6186 aliases, that apply to read-only sections. Cannot be used after
6187 size_dynamic_sections. */
d311bc8b
AM
6188
6189static bfd_boolean
6190alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6191{
6192 struct ppc_link_hash_entry *eh;
6193
6194 eh = (struct ppc_link_hash_entry *) h;
6195 do
6196 {
6197 if (readonly_dynrelocs (&eh->elf))
6198 return TRUE;
ab2477e1 6199 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
2cdcc330
AM
6200 }
6201 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
6202
6203 return FALSE;
6204}
8a2058b5 6205
8a9e8e72
AM
6206/* Return whether EH has pc-relative dynamic relocs. */
6207
6208static bfd_boolean
6209pc_dynrelocs (struct ppc_link_hash_entry *eh)
6210{
6211 struct elf_dyn_relocs *p;
6212
6213 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6214 if (p->pc_count != 0)
6215 return TRUE;
6216 return FALSE;
6217}
6218
8a2058b5
AM
6219/* Return true if a global entry stub will be created for H. Valid
6220 for ELFv2 before plt entries have been allocated. */
6221
6222static bfd_boolean
6223global_entry_stub (struct elf_link_hash_entry *h)
6224{
6225 struct plt_entry *pent;
6226
6227 if (!h->pointer_equality_needed
6228 || h->def_regular)
6229 return FALSE;
6230
6231 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6232 if (pent->plt.refcount > 0
6233 && pent->addend == 0)
6234 return TRUE;
6235
6236 return FALSE;
6237}
6238
e86ce104
AM
6239/* Adjust a symbol defined by a dynamic object and referenced by a
6240 regular object. The current definition is in some section of the
6241 dynamic object, but we're not including those sections. We have to
6242 change the definition to something the rest of the link can
6243 understand. */
6244
b34976b6 6245static bfd_boolean
4ce794b7
AM
6246ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6247 struct elf_link_hash_entry *h)
e86ce104
AM
6248{
6249 struct ppc_link_hash_table *htab;
5474d94f 6250 asection *s, *srel;
e86ce104
AM
6251
6252 htab = ppc_hash_table (info);
4dfe6ac6
NC
6253 if (htab == NULL)
6254 return FALSE;
e86ce104
AM
6255
6256 /* Deal with function syms. */
6257 if (h->type == STT_FUNC
e054468f 6258 || h->type == STT_GNU_IFUNC
f5385ebf 6259 || h->needs_plt)
e86ce104 6260 {
529fe20e
AM
6261 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6262 || SYMBOL_CALLS_LOCAL (info, h)
6263 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6264 /* Discard dyn_relocs when non-pic if we've decided that a
6265 function symbol is local and not an ifunc. We keep dynamic
6266 relocs for ifuncs when local rather than always emitting a
6267 plt call stub for them and defining the symbol on the call
6268 stub. We can't do that for ELFv1 anyway (a function symbol
6269 is defined on a descriptor, not code) and it can be faster at
6270 run-time due to not needing to bounce through a stub. The
6271 dyn_relocs for ifuncs will be applied even in a static
6272 executable. */
6273 if (!bfd_link_pic (info)
6274 && h->type != STT_GNU_IFUNC
6275 && local)
6276 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6277
e86ce104
AM
6278 /* Clear procedure linkage table information for any symbol that
6279 won't need a .plt entry. */
411e1bfb
AM
6280 struct plt_entry *ent;
6281 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6282 if (ent->plt.refcount > 0)
6283 break;
8387904d 6284 if (ent == NULL
2d7ad24e
AM
6285 || (h->type != STT_GNU_IFUNC
6286 && local
3e04d765
AM
6287 && (htab->can_convert_all_inline_plt
6288 || (((struct ppc_link_hash_entry *) h)->tls_mask
6289 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6290 {
411e1bfb 6291 h->plt.plist = NULL;
f5385ebf 6292 h->needs_plt = 0;
d1eca1e4 6293 h->pointer_equality_needed = 0;
40b8271b 6294 }
8a2058b5 6295 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6296 {
d1eca1e4
AM
6297 /* Taking a function's address in a read/write section
6298 doesn't require us to define the function symbol in the
6299 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6300 be used instead. The reason we prefer a few more dynamic
6301 relocs is that calling via a global entry stub costs a
6302 few more instructions, and pointer_equality_needed causes
6303 extra work in ld.so when resolving these symbols. */
529fe20e 6304 if (global_entry_stub (h))
d1eca1e4 6305 {
ab2477e1 6306 if (!readonly_dynrelocs (h))
529fe20e
AM
6307 {
6308 h->pointer_equality_needed = 0;
04383fd1
AM
6309 /* If we haven't seen a branch reloc and the symbol
6310 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6311 if (!h->needs_plt)
6312 h->plt.plist = NULL;
6313 }
6314 else if (!bfd_link_pic (info))
6315 /* We are going to be defining the function symbol on the
6316 plt stub, so no dyn_relocs needed when non-pic. */
6317 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
6318 }
6319
3988aed5
AM
6320 /* ELFv2 function symbols can't have copy relocs. */
6321 return TRUE;
6322 }
6323 else if (!h->needs_plt
ab2477e1 6324 && !readonly_dynrelocs (h))
3988aed5 6325 {
04383fd1
AM
6326 /* If we haven't seen a branch reloc and the symbol isn't an
6327 ifunc then we don't need a plt entry. */
3988aed5
AM
6328 h->plt.plist = NULL;
6329 h->pointer_equality_needed = 0;
a345bc8d
AM
6330 return TRUE;
6331 }
5bd4f169 6332 }
bbd7ec4a 6333 else
411e1bfb 6334 h->plt.plist = NULL;
5bd4f169
AM
6335
6336 /* If this is a weak symbol, and there is a real definition, the
6337 processor independent code will have arranged for us to see the
6338 real definition first, and we can just use the same value. */
60d67dc8 6339 if (h->is_weakalias)
5bd4f169 6340 {
60d67dc8
AM
6341 struct elf_link_hash_entry *def = weakdef (h);
6342 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6343 h->root.u.def.section = def->root.u.def.section;
6344 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6345 if (def->root.u.def.section == htab->elf.sdynbss
6346 || def->root.u.def.section == htab->elf.sdynrelro)
6347 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 6348 return TRUE;
5bd4f169
AM
6349 }
6350
5bd4f169
AM
6351 /* If we are creating a shared library, we must presume that the
6352 only references to the symbol are via the global offset table.
6353 For such cases we need not do anything here; the relocations will
6354 be handled correctly by relocate_section. */
0e1862bb 6355 if (bfd_link_pic (info))
b34976b6 6356 return TRUE;
5bd4f169 6357
65f38f15
AM
6358 /* If there are no references to this symbol that do not use the
6359 GOT, we don't need to generate a copy reloc. */
f5385ebf 6360 if (!h->non_got_ref)
b34976b6 6361 return TRUE;
65f38f15 6362
b186458a 6363 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6364 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6365
d93d1c80
AM
6366 /* If -z nocopyreloc was given, don't generate them either. */
6367 || info->nocopyreloc
a127494f 6368
dce2246a 6369 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6370 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 6371 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 6372
d93d1c80
AM
6373 /* Protected variables do not work with .dynbss. The copy in
6374 .dynbss won't be used by the shared library with the protected
6375 definition for the variable. Text relocations are preferable
6376 to an incorrect program. */
6377 || h->protected_def)
529fe20e 6378 return TRUE;
a127494f 6379
5d35169e 6380 if (h->plt.plist != NULL)
97b639ba
AM
6381 {
6382 /* We should never get here, but unfortunately there are versions
6383 of gcc out there that improperly (for this ABI) put initialized
6384 function pointers, vtable refs and suchlike in read-only
6385 sections. Allow them to proceed, but warn that this might
6386 break at runtime. */
25f53a85 6387 info->callbacks->einfo
c1c8c1ef 6388 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6389 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6390 h->root.root.string);
6391 }
5d35169e
AM
6392
6393 /* This is a reference to a symbol defined by a dynamic object which
6394 is not a function. */
6395
5bd4f169
AM
6396 /* We must allocate the symbol in our .dynbss section, which will
6397 become part of the .bss section of the executable. There will be
6398 an entry for this symbol in the .dynsym section. The dynamic
6399 object will contain position independent code, so all references
6400 from the dynamic object to this symbol will go through the global
6401 offset table. The dynamic linker will use the .dynsym entry to
6402 determine the address it must put in the global offset table, so
6403 both the dynamic object and the regular object will refer to the
6404 same memory location for the variable. */
5474d94f
AM
6405 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6406 {
6407 s = htab->elf.sdynrelro;
6408 srel = htab->elf.sreldynrelro;
6409 }
6410 else
6411 {
6412 s = htab->elf.sdynbss;
6413 srel = htab->elf.srelbss;
6414 }
1d7e9d18 6415 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6416 {
4a7e5234
AM
6417 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6418 linker to copy the initial value out of the dynamic object
6419 and into the runtime process image. */
5474d94f 6420 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6421 h->needs_copy = 1;
5bd4f169
AM
6422 }
6423
529fe20e
AM
6424 /* We no longer want dyn_relocs. */
6425 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 6426 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6427}
6428
e86ce104
AM
6429/* If given a function descriptor symbol, hide both the function code
6430 sym and the descriptor. */
6431static void
4ce794b7
AM
6432ppc64_elf_hide_symbol (struct bfd_link_info *info,
6433 struct elf_link_hash_entry *h,
6434 bfd_boolean force_local)
e86ce104 6435{
34814b9f 6436 struct ppc_link_hash_entry *eh;
e86ce104
AM
6437 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6438
87469ba2
AM
6439 if (ppc_hash_table (info) == NULL)
6440 return;
6441
34814b9f
AM
6442 eh = (struct ppc_link_hash_entry *) h;
6443 if (eh->is_func_descriptor)
e86ce104 6444 {
34814b9f 6445 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6446
721956f4 6447 if (fh == NULL)
d1329ca3
AM
6448 {
6449 const char *p, *q;
b8ac2841 6450 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6451 char save;
6452
6453 /* We aren't supposed to use alloca in BFD because on
6454 systems which do not have alloca the version in libiberty
6455 calls xmalloc, which might cause the program to crash
6456 when it runs out of memory. This function doesn't have a
6457 return status, so there's no way to gracefully return an
6458 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6459 accessed; It's either a string in an ELF string table,
6460 or allocated in an objalloc structure. */
d1329ca3 6461
34814b9f 6462 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6463 save = *p;
6464 *(char *) p = '.';
34814b9f 6465 fh = (struct ppc_link_hash_entry *)
b8ac2841 6466 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6467 *(char *) p = save;
6468
6469 /* Unfortunately, if it so happens that the string we were
6470 looking for was allocated immediately before this string,
6471 then we overwrote the string terminator. That's the only
6472 reason the lookup should fail. */
6473 if (fh == NULL)
6474 {
34814b9f
AM
6475 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6476 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6477 --q, --p;
34814b9f
AM
6478 if (q < eh->elf.root.root.string && *p == '.')
6479 fh = (struct ppc_link_hash_entry *)
b8ac2841 6480 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6481 }
6482 if (fh != NULL)
6483 {
34814b9f
AM
6484 eh->oh = fh;
6485 fh->oh = eh;
d1329ca3
AM
6486 }
6487 }
e86ce104 6488 if (fh != NULL)
34814b9f 6489 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6490 }
6491}
6492
411e1bfb 6493static bfd_boolean
8843416a
AM
6494get_sym_h (struct elf_link_hash_entry **hp,
6495 Elf_Internal_Sym **symp,
6496 asection **symsecp,
f961d9dd 6497 unsigned char **tls_maskp,
8843416a
AM
6498 Elf_Internal_Sym **locsymsp,
6499 unsigned long r_symndx,
6500 bfd *ibfd)
411e1bfb 6501{
0ffa91dd 6502 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6503
6504 if (r_symndx >= symtab_hdr->sh_info)
6505 {
6506 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6507 struct elf_link_hash_entry *h;
6508
6509 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6510 h = elf_follow_link (h);
411e1bfb
AM
6511
6512 if (hp != NULL)
6513 *hp = h;
6514
6515 if (symp != NULL)
6516 *symp = NULL;
6517
6518 if (symsecp != NULL)
6519 {
6520 asection *symsec = NULL;
6521 if (h->root.type == bfd_link_hash_defined
6522 || h->root.type == bfd_link_hash_defweak)
6523 symsec = h->root.u.def.section;
6524 *symsecp = symsec;
6525 }
6526
e7b938ca 6527 if (tls_maskp != NULL)
411e1bfb
AM
6528 {
6529 struct ppc_link_hash_entry *eh;
6530
6531 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6532 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6533 }
6534 }
6535 else
6536 {
6537 Elf_Internal_Sym *sym;
6538 Elf_Internal_Sym *locsyms = *locsymsp;
6539
6540 if (locsyms == NULL)
6541 {
6542 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6543 if (locsyms == NULL)
6544 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6545 symtab_hdr->sh_info,
6546 0, NULL, NULL, NULL);
6547 if (locsyms == NULL)
6548 return FALSE;
6549 *locsymsp = locsyms;
6550 }
6551 sym = locsyms + r_symndx;
6552
6553 if (hp != NULL)
6554 *hp = NULL;
6555
6556 if (symp != NULL)
6557 *symp = sym;
6558
6559 if (symsecp != NULL)
cb33740c 6560 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6561
e7b938ca 6562 if (tls_maskp != NULL)
411e1bfb
AM
6563 {
6564 struct got_entry **lgot_ents;
f961d9dd 6565 unsigned char *tls_mask;
411e1bfb 6566
e7b938ca 6567 tls_mask = NULL;
411e1bfb
AM
6568 lgot_ents = elf_local_got_ents (ibfd);
6569 if (lgot_ents != NULL)
6570 {
e054468f
AM
6571 struct plt_entry **local_plt = (struct plt_entry **)
6572 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6573 unsigned char *lgot_masks = (unsigned char *)
e054468f 6574 (local_plt + symtab_hdr->sh_info);
e7b938ca 6575 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6576 }
e7b938ca 6577 *tls_maskp = tls_mask;
411e1bfb
AM
6578 }
6579 }
6580 return TRUE;
6581}
6582
e7b938ca 6583/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6584 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6585 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6586
6587static int
f961d9dd 6588get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6589 unsigned long *toc_symndx,
6590 bfd_vma *toc_addend,
0d4792f7 6591 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6592 const Elf_Internal_Rela *rel,
6593 bfd *ibfd)
411e1bfb
AM
6594{
6595 unsigned long r_symndx;
0d4792f7 6596 int next_r;
411e1bfb
AM
6597 struct elf_link_hash_entry *h;
6598 Elf_Internal_Sym *sym;
6599 asection *sec;
6600 bfd_vma off;
6601
6602 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6603 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6604 return 0;
411e1bfb 6605
37da22e5
AM
6606 if ((*tls_maskp != NULL
6607 && (**tls_maskp & TLS_TLS) != 0
6608 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6609 || sec == NULL
6bee8834 6610 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6611 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6612 return 1;
411e1bfb
AM
6613
6614 /* Look inside a TOC section too. */
6615 if (h != NULL)
6616 {
6617 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6618 off = h->root.u.def.value;
6619 }
6620 else
6621 off = sym->st_value;
6622 off += rel->r_addend;
6623 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6624 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6625 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6626 if (toc_symndx != NULL)
6627 *toc_symndx = r_symndx;
3a71aa26
AM
6628 if (toc_addend != NULL)
6629 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6630 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6631 return 0;
854b41e7 6632 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6633 && (next_r == -1 || next_r == -2))
6634 return 1 - next_r;
951fd09b 6635 return 1;
411e1bfb
AM
6636}
6637
3b421ab3
AM
6638/* Find (or create) an entry in the tocsave hash table. */
6639
6640static struct tocsave_entry *
6641tocsave_find (struct ppc_link_hash_table *htab,
6642 enum insert_option insert,
6643 Elf_Internal_Sym **local_syms,
6644 const Elf_Internal_Rela *irela,
6645 bfd *ibfd)
6646{
6647 unsigned long r_indx;
6648 struct elf_link_hash_entry *h;
6649 Elf_Internal_Sym *sym;
6650 struct tocsave_entry ent, *p;
6651 hashval_t hash;
6652 struct tocsave_entry **slot;
6653
6654 r_indx = ELF64_R_SYM (irela->r_info);
6655 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6656 return NULL;
6657 if (ent.sec == NULL || ent.sec->output_section == NULL)
6658 {
4eca0228 6659 _bfd_error_handler
871b3ab2 6660 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6661 return NULL;
6662 }
6663
6664 if (h != NULL)
6665 ent.offset = h->root.u.def.value;
6666 else
6667 ent.offset = sym->st_value;
6668 ent.offset += irela->r_addend;
6669
6670 hash = tocsave_htab_hash (&ent);
6671 slot = ((struct tocsave_entry **)
6672 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6673 if (slot == NULL)
6674 return NULL;
6675
6676 if (*slot == NULL)
6677 {
6678 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6679 if (p == NULL)
6680 return NULL;
6681 *p = ent;
6682 *slot = p;
6683 }
6684 return *slot;
6685}
6686
754021d0 6687/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6688 code for the old ABI, these will already have been done. */
754021d0
AM
6689
6690static bfd_boolean
6691adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6692{
6693 struct ppc_link_hash_entry *eh;
6694 asection *sym_sec;
74f0fb50 6695 struct _opd_sec_data *opd;
754021d0
AM
6696
6697 if (h->root.type == bfd_link_hash_indirect)
6698 return TRUE;
6699
754021d0
AM
6700 if (h->root.type != bfd_link_hash_defined
6701 && h->root.type != bfd_link_hash_defweak)
6702 return TRUE;
6703
6704 eh = (struct ppc_link_hash_entry *) h;
6705 if (eh->adjust_done)
6706 return TRUE;
6707
6708 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6709 opd = get_opd_info (sym_sec);
6710 if (opd != NULL && opd->adjust != NULL)
754021d0 6711 {
51aecdc5 6712 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6713 if (adjust == -1)
6714 {
6715 /* This entry has been deleted. */
b3fac117 6716 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6717 if (dsec == NULL)
6718 {
6719 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6720 if (discarded_section (dsec))
81688140 6721 {
b3fac117 6722 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6723 break;
6724 }
6725 }
4025353c 6726 eh->elf.root.u.def.value = 0;
81688140 6727 eh->elf.root.u.def.section = dsec;
4025353c
AM
6728 }
6729 else
6730 eh->elf.root.u.def.value += adjust;
754021d0
AM
6731 eh->adjust_done = 1;
6732 }
6733 return TRUE;
6734}
6735
8c1d1bb8 6736/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6737 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6738 have already been determined. */
6739
6740static bfd_boolean
6741dec_dynrel_count (bfd_vma r_info,
6742 asection *sec,
6743 struct bfd_link_info *info,
6744 Elf_Internal_Sym **local_syms,
6745 struct elf_link_hash_entry *h,
19e08130 6746 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6747{
6748 enum elf_ppc64_reloc_type r_type;
19e08130 6749 asection *sym_sec = NULL;
8c1d1bb8
AM
6750
6751 /* Can this reloc be dynamic? This switch, and later tests here
6752 should be kept in sync with the code in check_relocs. */
6753 r_type = ELF64_R_TYPE (r_info);
6754 switch (r_type)
6755 {
6756 default:
6757 return TRUE;
6758
6759 case R_PPC64_TPREL16:
6760 case R_PPC64_TPREL16_LO:
6761 case R_PPC64_TPREL16_HI:
6762 case R_PPC64_TPREL16_HA:
6763 case R_PPC64_TPREL16_DS:
6764 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6765 case R_PPC64_TPREL16_HIGH:
6766 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
6767 case R_PPC64_TPREL16_HIGHER:
6768 case R_PPC64_TPREL16_HIGHERA:
6769 case R_PPC64_TPREL16_HIGHEST:
6770 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
6771 case R_PPC64_TPREL64:
6772 case R_PPC64_DTPMOD64:
6773 case R_PPC64_DTPREL64:
6774 case R_PPC64_ADDR64:
6775 case R_PPC64_REL30:
6776 case R_PPC64_REL32:
6777 case R_PPC64_REL64:
6778 case R_PPC64_ADDR14:
6779 case R_PPC64_ADDR14_BRNTAKEN:
6780 case R_PPC64_ADDR14_BRTAKEN:
6781 case R_PPC64_ADDR16:
6782 case R_PPC64_ADDR16_DS:
6783 case R_PPC64_ADDR16_HA:
6784 case R_PPC64_ADDR16_HI:
f9c6b907
AM
6785 case R_PPC64_ADDR16_HIGH:
6786 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
6787 case R_PPC64_ADDR16_HIGHER:
6788 case R_PPC64_ADDR16_HIGHERA:
6789 case R_PPC64_ADDR16_HIGHEST:
6790 case R_PPC64_ADDR16_HIGHESTA:
6791 case R_PPC64_ADDR16_LO:
6792 case R_PPC64_ADDR16_LO_DS:
6793 case R_PPC64_ADDR24:
6794 case R_PPC64_ADDR32:
6795 case R_PPC64_UADDR16:
6796 case R_PPC64_UADDR32:
6797 case R_PPC64_UADDR64:
6798 case R_PPC64_TOC:
5663e321
AM
6799 case R_PPC64_D34:
6800 case R_PPC64_D34_LO:
6801 case R_PPC64_D34_HI30:
6802 case R_PPC64_D34_HA30:
6803 case R_PPC64_ADDR16_HIGHER34:
6804 case R_PPC64_ADDR16_HIGHERA34:
6805 case R_PPC64_ADDR16_HIGHEST34:
6806 case R_PPC64_ADDR16_HIGHESTA34:
6807 case R_PPC64_D28:
8c1d1bb8
AM
6808 break;
6809 }
6810
6811 if (local_syms != NULL)
6812 {
6813 unsigned long r_symndx;
8c1d1bb8
AM
6814 bfd *ibfd = sec->owner;
6815
6816 r_symndx = ELF64_R_SYM (r_info);
6817 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6818 return FALSE;
6819 }
6820
0e1862bb 6821 if ((bfd_link_pic (info)
1d483afe 6822 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 6823 || (h != NULL
198f1157 6824 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
6825 || h->root.type == bfd_link_hash_defweak
6826 || !h->def_regular))))
6827 || (ELIMINATE_COPY_RELOCS
0e1862bb 6828 && !bfd_link_pic (info)
8c1d1bb8
AM
6829 && h != NULL
6830 && (h->root.type == bfd_link_hash_defweak
6831 || !h->def_regular)))
6832 ;
6833 else
6834 return TRUE;
6835
6836 if (h != NULL)
6edfbbad 6837 {
19e08130
AM
6838 struct elf_dyn_relocs *p;
6839 struct elf_dyn_relocs **pp;
6840 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6841
6842 /* elf_gc_sweep may have already removed all dyn relocs associated
6843 with local syms for a given section. Also, symbol flags are
6844 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6845 report a dynreloc miscount. */
6846 if (*pp == NULL && info->gc_sections)
6847 return TRUE;
6848
6849 while ((p = *pp) != NULL)
60124e18 6850 {
19e08130
AM
6851 if (p->sec == sec)
6852 {
6853 if (!must_be_dyn_reloc (info, r_type))
6854 p->pc_count -= 1;
6855 p->count -= 1;
6856 if (p->count == 0)
6857 *pp = p->next;
6858 return TRUE;
6859 }
6860 pp = &p->next;
60124e18 6861 }
6edfbbad 6862 }
19e08130
AM
6863 else
6864 {
6865 struct ppc_dyn_relocs *p;
6866 struct ppc_dyn_relocs **pp;
6867 void *vpp;
6868 bfd_boolean is_ifunc;
8c1d1bb8 6869
19e08130
AM
6870 if (local_syms == NULL)
6871 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6872 if (sym_sec == NULL)
6873 sym_sec = sec;
c57da1a7 6874
19e08130
AM
6875 vpp = &elf_section_data (sym_sec)->local_dynrel;
6876 pp = (struct ppc_dyn_relocs **) vpp;
6877
6878 if (*pp == NULL && info->gc_sections)
6879 return TRUE;
6880
6881 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6882 while ((p = *pp) != NULL)
8c1d1bb8 6883 {
19e08130
AM
6884 if (p->sec == sec && p->ifunc == is_ifunc)
6885 {
6886 p->count -= 1;
6887 if (p->count == 0)
6888 *pp = p->next;
6889 return TRUE;
6890 }
6891 pp = &p->next;
8c1d1bb8 6892 }
8c1d1bb8
AM
6893 }
6894
695344c0 6895 /* xgettext:c-format */
cf97bcb0
AM
6896 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6897 sec->owner, sec);
8c1d1bb8
AM
6898 bfd_set_error (bfd_error_bad_value);
6899 return FALSE;
6900}
6901
754021d0
AM
6902/* Remove unused Official Procedure Descriptor entries. Currently we
6903 only remove those associated with functions in discarded link-once
6904 sections, or weakly defined functions that have been overridden. It
6905 would be possible to remove many more entries for statically linked
6906 applications. */
6907
b34976b6 6908bfd_boolean
e7d1c40c 6909ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
6910{
6911 bfd *ibfd;
754021d0 6912 bfd_boolean some_edited = FALSE;
3f764659 6913 asection *need_pad = NULL;
e7d1c40c
AM
6914 struct ppc_link_hash_table *htab;
6915
6916 htab = ppc_hash_table (info);
6917 if (htab == NULL)
6918 return FALSE;
1e2f5b6e 6919
c72f2fb2 6920 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
6921 {
6922 asection *sec;
6923 Elf_Internal_Rela *relstart, *rel, *relend;
6924 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6925 Elf_Internal_Sym *local_syms;
74f0fb50 6926 struct _opd_sec_data *opd;
51aecdc5 6927 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 6928 bfd_size_type cnt_16b = 0;
1e2f5b6e 6929
854b41e7
AM
6930 if (!is_ppc64_elf (ibfd))
6931 continue;
6932
1e2f5b6e 6933 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6934 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6935 continue;
6936
dbaa2011 6937 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
6938 continue;
6939
1e2f5b6e
AM
6940 if (sec->output_section == bfd_abs_section_ptr)
6941 continue;
6942
6943 /* Look through the section relocs. */
6944 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6945 continue;
6946
6cdc0ccc 6947 local_syms = NULL;
0ffa91dd 6948 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
6949
6950 /* Read the relocations. */
4ce794b7 6951 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6952 info->keep_memory);
1e2f5b6e 6953 if (relstart == NULL)
b34976b6 6954 return FALSE;
1e2f5b6e
AM
6955
6956 /* First run through the relocs to check they are sane, and to
6957 determine whether we need to edit this opd section. */
b34976b6 6958 need_edit = FALSE;
51aecdc5 6959 broken = FALSE;
3f764659 6960 need_pad = sec;
1e2f5b6e 6961 relend = relstart + sec->reloc_count;
50bc7936 6962 for (rel = relstart; rel < relend; )
1e2f5b6e 6963 {
04c9666a 6964 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
6965 unsigned long r_symndx;
6966 asection *sym_sec;
6967 struct elf_link_hash_entry *h;
6968 Elf_Internal_Sym *sym;
51aecdc5 6969 bfd_vma offset;
1e2f5b6e 6970
51aecdc5 6971 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
6972 only interested in the reloc pointing to a function entry
6973 point. */
51aecdc5
AM
6974 offset = rel->r_offset;
6975 if (rel + 1 == relend
6976 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
6977 {
6978 /* If someone messes with .opd alignment then after a
6979 "ld -r" we might have padding in the middle of .opd.
6980 Also, there's nothing to prevent someone putting
6981 something silly in .opd with the assembler. No .opd
b34976b6 6982 optimization for them! */
3f764659 6983 broken_opd:
4eca0228 6984 _bfd_error_handler
871b3ab2 6985 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 6986 broken = TRUE;
1e2f5b6e
AM
6987 break;
6988 }
6989
50bc7936
AM
6990 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6991 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6992 {
4eca0228 6993 _bfd_error_handler
695344c0 6994 /* xgettext:c-format */
871b3ab2 6995 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 6996 ibfd, r_type);
51aecdc5 6997 broken = TRUE;
50bc7936
AM
6998 break;
6999 }
7000
1e2f5b6e 7001 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7002 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7003 r_symndx, ibfd))
50bc7936 7004 goto error_ret;
1e2f5b6e
AM
7005
7006 if (sym_sec == NULL || sym_sec->owner == NULL)
7007 {
411e1bfb
AM
7008 const char *sym_name;
7009 if (h != NULL)
7010 sym_name = h->root.root.string;
7011 else
26c61ae5
L
7012 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7013 sym_sec);
411e1bfb 7014
4eca0228 7015 _bfd_error_handler
695344c0 7016 /* xgettext:c-format */
871b3ab2 7017 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7018 ibfd, sym_name);
51aecdc5 7019 broken = TRUE;
1e2f5b6e
AM
7020 break;
7021 }
7022
51020317
AM
7023 /* opd entries are always for functions defined in the
7024 current input bfd. If the symbol isn't defined in the
7025 input bfd, then we won't be using the function in this
7026 bfd; It must be defined in a linkonce section in another
7027 bfd, or is weak. It's also possible that we are
7028 discarding the function due to a linker script /DISCARD/,
7029 which we test for via the output_section. */
7030 if (sym_sec->owner != ibfd
7031 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7032 need_edit = TRUE;
1e2f5b6e 7033
50bc7936 7034 rel += 2;
51aecdc5
AM
7035 if (rel + 1 == relend
7036 || (rel + 2 < relend
7037 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7038 ++rel;
7039
7040 if (rel == relend)
3f764659
JJ
7041 {
7042 if (sec->size == offset + 24)
7043 {
7044 need_pad = NULL;
7045 break;
7046 }
51aecdc5 7047 if (sec->size == offset + 16)
3f764659
JJ
7048 {
7049 cnt_16b++;
7050 break;
7051 }
7052 goto broken_opd;
7053 }
3f764659
JJ
7054 else if (rel + 1 < relend
7055 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7056 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7057 {
51aecdc5
AM
7058 if (rel[0].r_offset == offset + 16)
7059 cnt_16b++;
7060 else if (rel[0].r_offset != offset + 24)
7061 goto broken_opd;
3f764659
JJ
7062 }
7063 else
7064 goto broken_opd;
1e2f5b6e
AM
7065 }
7066
e7d1c40c 7067 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7068
51aecdc5 7069 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7070 {
7071 Elf_Internal_Rela *write_rel;
d4730f92 7072 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7073 bfd_byte *rptr, *wptr;
983bddc8 7074 bfd_byte *new_contents;
74f0fb50
AM
7075 bfd_size_type amt;
7076
983bddc8 7077 new_contents = NULL;
51aecdc5 7078 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7079 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7080 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7081 if (opd->adjust == NULL)
7082 return FALSE;
1e2f5b6e
AM
7083
7084 /* This seems a waste of time as input .opd sections are all
7085 zeros as generated by gcc, but I suppose there's no reason
7086 this will always be so. We might start putting something in
7087 the third word of .opd entries. */
7088 if ((sec->flags & SEC_IN_MEMORY) == 0)
7089 {
eea6121a
AM
7090 bfd_byte *loc;
7091 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7092 {
eea6121a
AM
7093 if (loc != NULL)
7094 free (loc);
50bc7936 7095 error_ret:
6cdc0ccc
AM
7096 if (local_syms != NULL
7097 && symtab_hdr->contents != (unsigned char *) local_syms)
7098 free (local_syms);
6cdc0ccc
AM
7099 if (elf_section_data (sec)->relocs != relstart)
7100 free (relstart);
b34976b6 7101 return FALSE;
6cdc0ccc 7102 }
1e2f5b6e
AM
7103 sec->contents = loc;
7104 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7105 }
7106
7107 elf_section_data (sec)->relocs = relstart;
7108
3f764659 7109 new_contents = sec->contents;
3f764659
JJ
7110 if (add_aux_fields)
7111 {
7112 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7113 if (new_contents == NULL)
7114 return FALSE;
51aecdc5 7115 need_pad = NULL;
3f764659 7116 }
b4f4e59f
AM
7117 wptr = new_contents;
7118 rptr = sec->contents;
1e2f5b6e 7119 write_rel = relstart;
51aecdc5 7120 for (rel = relstart; rel < relend; )
1e2f5b6e 7121 {
50bc7936
AM
7122 unsigned long r_symndx;
7123 asection *sym_sec;
7124 struct elf_link_hash_entry *h;
51aecdc5 7125 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7126 Elf_Internal_Sym *sym;
51aecdc5
AM
7127 long opd_ent_size;
7128 Elf_Internal_Rela *next_rel;
7129 bfd_boolean skip;
50bc7936
AM
7130
7131 r_symndx = ELF64_R_SYM (rel->r_info);
7132 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7133 r_symndx, ibfd))
50bc7936
AM
7134 goto error_ret;
7135
51aecdc5
AM
7136 next_rel = rel + 2;
7137 if (next_rel + 1 == relend
7138 || (next_rel + 2 < relend
7139 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7140 ++next_rel;
7141
7142 /* See if the .opd entry is full 24 byte or
7143 16 byte (with fd_aux entry overlapped with next
7144 fd_func). */
7145 opd_ent_size = 24;
7146 if (next_rel == relend)
1e2f5b6e 7147 {
51aecdc5 7148 if (sec->size == rel->r_offset + 16)
3f764659 7149 opd_ent_size = 16;
51aecdc5
AM
7150 }
7151 else if (next_rel->r_offset == rel->r_offset + 16)
7152 opd_ent_size = 16;
3f764659 7153
51aecdc5
AM
7154 if (h != NULL
7155 && h->root.root.string[0] == '.')
7156 {
8c5b4e52
AM
7157 fdh = ((struct ppc_link_hash_entry *) h)->oh;
7158 if (fdh != NULL)
7159 {
7160 fdh = ppc_follow_link (fdh);
7161 if (fdh->elf.root.type != bfd_link_hash_defined
7162 && fdh->elf.root.type != bfd_link_hash_defweak)
7163 fdh = NULL;
7164 }
51aecdc5 7165 }
1e2f5b6e 7166
51aecdc5
AM
7167 skip = (sym_sec->owner != ibfd
7168 || sym_sec->output_section == bfd_abs_section_ptr);
7169 if (skip)
7170 {
7171 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7172 {
51aecdc5
AM
7173 /* Arrange for the function descriptor sym
7174 to be dropped. */
7175 fdh->elf.root.u.def.value = 0;
7176 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7177 }
51aecdc5 7178 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7179
0e1862bb 7180 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7181 rel = next_rel;
7182 else
7183 while (1)
7184 {
7185 if (!dec_dynrel_count (rel->r_info, sec, info,
7186 NULL, h, sym))
7187 goto error_ret;
754021d0 7188
51aecdc5
AM
7189 if (++rel == next_rel)
7190 break;
1e2f5b6e 7191
51aecdc5
AM
7192 r_symndx = ELF64_R_SYM (rel->r_info);
7193 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7194 r_symndx, ibfd))
7195 goto error_ret;
7196 }
50bc7936
AM
7197 }
7198 else
1e2f5b6e 7199 {
51aecdc5
AM
7200 /* We'll be keeping this opd entry. */
7201 long adjust;
7202
7203 if (fdh != NULL)
7204 {
7205 /* Redefine the function descriptor symbol to
7206 this location in the opd section. It is
7207 necessary to update the value here rather
7208 than using an array of adjustments as we do
7209 for local symbols, because various places
7210 in the generic ELF code use the value
7211 stored in u.def.value. */
7212 fdh->elf.root.u.def.value = wptr - new_contents;
7213 fdh->adjust_done = 1;
7214 }
7215
7216 /* Local syms are a bit tricky. We could
7217 tweak them as they can be cached, but
7218 we'd need to look through the local syms
7219 for the function descriptor sym which we
7220 don't have at the moment. So keep an
7221 array of adjustments. */
7222 adjust = (wptr - new_contents) - (rptr - sec->contents);
7223 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7224
7225 if (wptr != rptr)
7226 memcpy (wptr, rptr, opd_ent_size);
7227 wptr += opd_ent_size;
7228 if (add_aux_fields && opd_ent_size == 16)
7229 {
7230 memset (wptr, '\0', 8);
7231 wptr += 8;
7232 }
7233
50bc7936 7234 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7235 new opd entries. */
7236 for ( ; rel != next_rel; ++rel)
7237 {
7238 rel->r_offset += adjust;
7239 if (write_rel != rel)
7240 memcpy (write_rel, rel, sizeof (*rel));
7241 ++write_rel;
7242 }
1e2f5b6e 7243 }
51aecdc5
AM
7244
7245 rptr += opd_ent_size;
1e2f5b6e
AM
7246 }
7247
3f764659 7248 sec->size = wptr - new_contents;
1e2f5b6e 7249 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7250 if (add_aux_fields)
7251 {
7252 free (sec->contents);
7253 sec->contents = new_contents;
7254 }
7255
05bf9422 7256 /* Fudge the header size too, as this is used later in
cdcf6e38 7257 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7258 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7259 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7260 some_edited = TRUE;
1e2f5b6e 7261 }
6cdc0ccc 7262 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7263 free (relstart);
6cdc0ccc 7264
411e1bfb
AM
7265 if (local_syms != NULL
7266 && symtab_hdr->contents != (unsigned char *) local_syms)
7267 {
7268 if (!info->keep_memory)
7269 free (local_syms);
7270 else
7271 symtab_hdr->contents = (unsigned char *) local_syms;
7272 }
7273 }
7274
754021d0
AM
7275 if (some_edited)
7276 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7277
3f764659
JJ
7278 /* If we are doing a final link and the last .opd entry is just 16 byte
7279 long, add a 8 byte padding after it. */
0e1862bb 7280 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7281 {
7282 bfd_byte *p;
7283
7284 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7285 {
7286 BFD_ASSERT (need_pad->size > 0);
7287
7288 p = bfd_malloc (need_pad->size + 8);
7289 if (p == NULL)
7290 return FALSE;
699733f6 7291
2cdcc330
AM
7292 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7293 p, 0, need_pad->size))
3f764659
JJ
7294 return FALSE;
7295
7296 need_pad->contents = p;
7297 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7298 }
7299 else
7300 {
7301 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7302 if (p == NULL)
7303 return FALSE;
7304
7305 need_pad->contents = p;
7306 }
7307
7308 memset (need_pad->contents + need_pad->size, 0, 8);
7309 need_pad->size += 8;
7310 }
7311
411e1bfb
AM
7312 return TRUE;
7313}
7314
3e04d765
AM
7315/* Analyze inline PLT call relocations to see whether calls to locally
7316 defined functions can be converted to direct calls. */
7317
7318bfd_boolean
7319ppc64_elf_inline_plt (struct bfd_link_info *info)
7320{
7321 struct ppc_link_hash_table *htab;
7322 bfd *ibfd;
7323 asection *sec;
7324 bfd_vma low_vma, high_vma, limit;
7325
7326 htab = ppc_hash_table (info);
7327 if (htab == NULL)
7328 return FALSE;
7329
7330 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7331 reduced somewhat to cater for possible stubs that might be added
7332 between the call and its destination. */
7333 if (htab->params->group_size < 0)
7334 {
7335 limit = -htab->params->group_size;
7336 if (limit == 1)
7337 limit = 0x1e00000;
7338 }
7339 else
7340 {
7341 limit = htab->params->group_size;
7342 if (limit == 1)
7343 limit = 0x1c00000;
7344 }
7345
7346 low_vma = -1;
7347 high_vma = 0;
7348 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7349 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7350 {
7351 if (low_vma > sec->vma)
7352 low_vma = sec->vma;
7353 if (high_vma < sec->vma + sec->size)
7354 high_vma = sec->vma + sec->size;
7355 }
7356
7357 /* If a "bl" can reach anywhere in local code sections, then we can
7358 convert all inline PLT sequences to direct calls when the symbol
7359 is local. */
7360 if (high_vma - low_vma < limit)
7361 {
7362 htab->can_convert_all_inline_plt = 1;
7363 return TRUE;
7364 }
7365
7366 /* Otherwise, go looking through relocs for cases where a direct
7367 call won't reach. Mark the symbol on any such reloc to disable
7368 the optimization and keep the PLT entry as it seems likely that
7369 this will be better than creating trampolines. Note that this
7370 will disable the optimization for all inline PLT calls to a
7371 particular symbol, not just those that won't reach. The
7372 difficulty in doing a more precise optimization is that the
7373 linker needs to make a decision depending on whether a
7374 particular R_PPC64_PLTCALL insn can be turned into a direct
7375 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7376 the sequence, and there is nothing that ties those relocs
7377 together except their symbol. */
7378
7379 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7380 {
7381 Elf_Internal_Shdr *symtab_hdr;
7382 Elf_Internal_Sym *local_syms;
7383
7384 if (!is_ppc64_elf (ibfd))
7385 continue;
7386
7387 local_syms = NULL;
7388 symtab_hdr = &elf_symtab_hdr (ibfd);
7389
7390 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7391 if (ppc64_elf_section_data (sec)->has_pltcall
7392 && !bfd_is_abs_section (sec->output_section))
7393 {
7394 Elf_Internal_Rela *relstart, *rel, *relend;
7395
7396 /* Read the relocations. */
7397 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7398 info->keep_memory);
7399 if (relstart == NULL)
7400 return FALSE;
7401
7402 relend = relstart + sec->reloc_count;
7403 for (rel = relstart; rel < relend; )
7404 {
7405 enum elf_ppc64_reloc_type r_type;
7406 unsigned long r_symndx;
7407 asection *sym_sec;
7408 struct elf_link_hash_entry *h;
7409 Elf_Internal_Sym *sym;
7410 unsigned char *tls_maskp;
7411
7412 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7413 if (r_type != R_PPC64_PLTCALL
7414 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7415 continue;
7416
7417 r_symndx = ELF64_R_SYM (rel->r_info);
7418 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7419 r_symndx, ibfd))
7420 {
7421 if (elf_section_data (sec)->relocs != relstart)
7422 free (relstart);
7423 if (local_syms != NULL
2cdcc330 7424 && symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7425 free (local_syms);
7426 return FALSE;
7427 }
7428
7429 if (sym_sec != NULL && sym_sec->output_section != NULL)
7430 {
7431 bfd_vma from, to;
7432 if (h != NULL)
7433 to = h->root.u.def.value;
7434 else
7435 to = sym->st_value;
7436 to += (rel->r_addend
7437 + sym_sec->output_offset
7438 + sym_sec->output_section->vma);
7439 from = (rel->r_offset
7440 + sec->output_offset
7441 + sec->output_section->vma);
5663e321
AM
7442 if (to - from + limit < 2 * limit
7443 && !(r_type == R_PPC64_PLTCALL_NOTOC
7444 && (((h ? h->other : sym->st_other)
7445 & STO_PPC64_LOCAL_MASK)
4a4e7361 7446 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7447 *tls_maskp &= ~PLT_KEEP;
7448 }
7449 }
7450 if (elf_section_data (sec)->relocs != relstart)
7451 free (relstart);
7452 }
7453
7454 if (local_syms != NULL
7455 && symtab_hdr->contents != (unsigned char *) local_syms)
7456 {
7457 if (!info->keep_memory)
7458 free (local_syms);
7459 else
7460 symtab_hdr->contents = (unsigned char *) local_syms;
7461 }
7462 }
7463
7464 return TRUE;
7465}
7466
e1918d23 7467/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7468
e1918d23 7469asection *
e7d1c40c 7470ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7471{
411e1bfb
AM
7472 struct ppc_link_hash_table *htab;
7473
411e1bfb 7474 htab = ppc_hash_table (info);
4dfe6ac6
NC
7475 if (htab == NULL)
7476 return NULL;
7477
ee67d69a
AM
7478 if (abiversion (info->output_bfd) == 1)
7479 htab->opd_abi = 1;
7480
e7d1c40c 7481 if (htab->params->no_multi_toc)
33c0ec9d
AM
7482 htab->do_multi_toc = 0;
7483 else if (!htab->do_multi_toc)
e7d1c40c 7484 htab->params->no_multi_toc = 1;
33c0ec9d 7485
8b5f1ed8
AM
7486 /* Default to --no-plt-localentry, as this option can cause problems
7487 with symbol interposition. For example, glibc libpthread.so and
7488 libc.so duplicate many pthread symbols, with a fallback
7489 implementation in libc.so. In some cases the fallback does more
7490 work than the pthread implementation. __pthread_condattr_destroy
7491 is one such symbol: the libpthread.so implementation is
7492 localentry:0 while the libc.so implementation is localentry:8.
7493 An app that "cleverly" uses dlopen to only load necessary
7494 libraries at runtime may omit loading libpthread.so when not
7495 running multi-threaded, which then results in the libc.so
7496 fallback symbols being used and ld.so complaining. Now there
7497 are workarounds in ld (see non_zero_localentry) to detect the
7498 pthread situation, but that may not be the only case where
7499 --plt-localentry can cause trouble. */
f378ab09 7500 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7501 htab->params->plt_localentry0 = 0;
d44c746a
AM
7502 if (htab->params->plt_localentry0
7503 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7504 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7505 _bfd_error_handler
7506 (_("warning: --plt-localentry is especially dangerous without "
7507 "ld.so support to detect ABI violations"));
f378ab09 7508
3a71aa26
AM
7509 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7510 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7511 FALSE, FALSE, TRUE));
a7f2871e
AM
7512 /* Move dynamic linking info to the function descriptor sym. */
7513 if (htab->tls_get_addr != NULL)
7514 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7515 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7516 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7517 FALSE, FALSE, TRUE));
7c9cf415 7518 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
7519 {
7520 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7521
7522 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7523 FALSE, FALSE, TRUE);
7524 if (opt != NULL)
7525 func_desc_adjust (opt, info);
7526 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7527 FALSE, FALSE, TRUE);
7528 if (opt_fd != NULL
7529 && (opt_fd->root.type == bfd_link_hash_defined
7530 || opt_fd->root.type == bfd_link_hash_defweak))
7531 {
7532 /* If glibc supports an optimized __tls_get_addr call stub,
7533 signalled by the presence of __tls_get_addr_opt, and we'll
7534 be calling __tls_get_addr via a plt call stub, then
7535 make __tls_get_addr point to __tls_get_addr_opt. */
7536 tga_fd = &htab->tls_get_addr_fd->elf;
7537 if (htab->elf.dynamic_sections_created
7538 && tga_fd != NULL
7539 && (tga_fd->type == STT_FUNC
7540 || tga_fd->needs_plt)
7541 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 7542 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
7543 {
7544 struct plt_entry *ent;
7545
7546 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7547 if (ent->plt.refcount > 0)
7548 break;
7549 if (ent != NULL)
7550 {
7551 tga_fd->root.type = bfd_link_hash_indirect;
7552 tga_fd->root.u.i.link = &opt_fd->root;
7553 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 7554 opt_fd->mark = 1;
a7f2871e
AM
7555 if (opt_fd->dynindx != -1)
7556 {
7557 /* Use __tls_get_addr_opt in dynamic relocations. */
7558 opt_fd->dynindx = -1;
7559 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7560 opt_fd->dynstr_index);
7561 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7562 return NULL;
a7f2871e 7563 }
2cdcc330
AM
7564 htab->tls_get_addr_fd
7565 = (struct ppc_link_hash_entry *) opt_fd;
a7f2871e
AM
7566 tga = &htab->tls_get_addr->elf;
7567 if (opt != NULL && tga != NULL)
7568 {
7569 tga->root.type = bfd_link_hash_indirect;
7570 tga->root.u.i.link = &opt->root;
7571 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 7572 opt->mark = 1;
a7f2871e
AM
7573 _bfd_elf_link_hash_hide_symbol (info, opt,
7574 tga->forced_local);
7575 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7576 }
7577 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7578 htab->tls_get_addr_fd->is_func_descriptor = 1;
7579 if (htab->tls_get_addr != NULL)
7580 {
7581 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7582 htab->tls_get_addr->is_func = 1;
7583 }
7584 }
7585 }
7586 }
7c9cf415
AM
7587 else if (htab->params->tls_get_addr_opt < 0)
7588 htab->params->tls_get_addr_opt = 0;
a7f2871e 7589 }
33c0ec9d 7590 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7591}
8387904d 7592
3a71aa26
AM
7593/* Return TRUE iff REL is a branch reloc with a global symbol matching
7594 HASH1 or HASH2. */
8387904d 7595
3a71aa26
AM
7596static bfd_boolean
7597branch_reloc_hash_match (const bfd *ibfd,
7598 const Elf_Internal_Rela *rel,
7599 const struct ppc_link_hash_entry *hash1,
7600 const struct ppc_link_hash_entry *hash2)
7601{
7602 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7603 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7604 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7605
e054468f 7606 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7607 {
3a71aa26
AM
7608 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7609 struct elf_link_hash_entry *h;
8387904d 7610
3a71aa26 7611 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7612 h = elf_follow_link (h);
3a71aa26
AM
7613 if (h == &hash1->elf || h == &hash2->elf)
7614 return TRUE;
a48ebf4d 7615 }
3a71aa26 7616 return FALSE;
951fd09b 7617}
411e1bfb 7618
951fd09b
AM
7619/* Run through all the TLS relocs looking for optimization
7620 opportunities. The linker has been hacked (see ppc64elf.em) to do
7621 a preliminary section layout so that we know the TLS segment
7622 offsets. We can't optimize earlier because some optimizations need
7623 to know the tp offset, and we need to optimize before allocating
7624 dynamic relocations. */
7625
7626bfd_boolean
33c0ec9d 7627ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7628{
7629 bfd *ibfd;
7630 asection *sec;
7631 struct ppc_link_hash_table *htab;
663a1470 7632 unsigned char *toc_ref;
102890f0 7633 int pass;
951fd09b 7634
3cbc1e5e 7635 if (!bfd_link_executable (info))
411e1bfb
AM
7636 return TRUE;
7637
951fd09b 7638 htab = ppc_hash_table (info);
4dfe6ac6
NC
7639 if (htab == NULL)
7640 return FALSE;
7641
663a1470
AM
7642 /* Make two passes over the relocs. On the first pass, mark toc
7643 entries involved with tls relocs, and check that tls relocs
7644 involved in setting up a tls_get_addr call are indeed followed by
7645 such a call. If they are not, we can't do any tls optimization.
7646 On the second pass twiddle tls_mask flags to notify
7647 relocate_section that optimization can be done, and adjust got
7648 and plt refcounts. */
7649 toc_ref = NULL;
7650 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7651 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7652 {
7653 Elf_Internal_Sym *locsyms = NULL;
7654 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7655
102890f0
AM
7656 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7657 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7658 {
7659 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7660 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7661
102890f0
AM
7662 /* Read the relocations. */
7663 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7664 info->keep_memory);
7665 if (relstart == NULL)
2915c55b
JK
7666 {
7667 free (toc_ref);
7668 return FALSE;
7669 }
411e1bfb 7670
102890f0
AM
7671 relend = relstart + sec->reloc_count;
7672 for (rel = relstart; rel < relend; rel++)
7673 {
7674 enum elf_ppc64_reloc_type r_type;
7675 unsigned long r_symndx;
7676 struct elf_link_hash_entry *h;
7677 Elf_Internal_Sym *sym;
7678 asection *sym_sec;
f961d9dd
AM
7679 unsigned char *tls_mask;
7680 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7681 bfd_vma value;
7682 bfd_boolean ok_tprel, is_local;
7683 long toc_ref_index = 0;
7684 int expecting_tls_get_addr = 0;
663a1470 7685 bfd_boolean ret = FALSE;
411e1bfb 7686
102890f0
AM
7687 r_symndx = ELF64_R_SYM (rel->r_info);
7688 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7689 r_symndx, ibfd))
7690 {
7691 err_free_rel:
7692 if (elf_section_data (sec)->relocs != relstart)
7693 free (relstart);
7694 if (toc_ref != NULL)
7695 free (toc_ref);
7696 if (locsyms != NULL
0ffa91dd 7697 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7698 != (unsigned char *) locsyms))
7699 free (locsyms);
663a1470 7700 return ret;
102890f0 7701 }
411e1bfb 7702
102890f0
AM
7703 if (h != NULL)
7704 {
766bc656
AM
7705 if (h->root.type == bfd_link_hash_defined
7706 || h->root.type == bfd_link_hash_defweak)
7707 value = h->root.u.def.value;
7708 else if (h->root.type == bfd_link_hash_undefweak)
7709 value = 0;
7710 else
663a1470
AM
7711 {
7712 found_tls_get_addr_arg = 0;
7713 continue;
7714 }
102890f0
AM
7715 }
7716 else
7717 /* Symbols referenced by TLS relocs must be of type
7718 STT_TLS. So no need for .opd local sym adjust. */
7719 value = sym->st_value;
7720
7721 ok_tprel = FALSE;
7722 is_local = FALSE;
7723 if (h == NULL
7724 || !h->def_dynamic)
7725 {
7726 is_local = TRUE;
766bc656
AM
7727 if (h != NULL
7728 && h->root.type == bfd_link_hash_undefweak)
7729 ok_tprel = TRUE;
c27b8c2a
AM
7730 else if (sym_sec != NULL
7731 && sym_sec->output_section != NULL)
766bc656
AM
7732 {
7733 value += sym_sec->output_offset;
7734 value += sym_sec->output_section->vma;
0b147428
AM
7735 value -= htab->elf.tls_sec->vma + TP_OFFSET;
7736 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 7737 }
102890f0 7738 }
951fd09b 7739
102890f0 7740 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7741 /* If this section has old-style __tls_get_addr calls
7742 without marker relocs, then check that each
7743 __tls_get_addr call reloc is preceded by a reloc
7744 that conceivably belongs to the __tls_get_addr arg
7745 setup insn. If we don't find matching arg setup
7746 relocs, don't do any tls optimization. */
7747 if (pass == 0
7748 && sec->has_tls_get_addr_call
7749 && h != NULL
7750 && (h == &htab->tls_get_addr->elf
7751 || h == &htab->tls_get_addr_fd->elf)
7752 && !found_tls_get_addr_arg
7753 && is_branch_reloc (r_type))
7754 {
25f53a85 7755 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7756 "TLS optimization disabled\n"),
7757 ibfd, sec, rel->r_offset);
7758 ret = TRUE;
7759 goto err_free_rel;
7760 }
7761
7762 found_tls_get_addr_arg = 0;
102890f0
AM
7763 switch (r_type)
7764 {
7765 case R_PPC64_GOT_TLSLD16:
7766 case R_PPC64_GOT_TLSLD16_LO:
7767 expecting_tls_get_addr = 1;
663a1470 7768 found_tls_get_addr_arg = 1;
1a0670f3 7769 /* Fall through. */
102890f0
AM
7770
7771 case R_PPC64_GOT_TLSLD16_HI:
7772 case R_PPC64_GOT_TLSLD16_HA:
7773 /* These relocs should never be against a symbol
7774 defined in a shared lib. Leave them alone if
7775 that turns out to be the case. */
7776 if (!is_local)
7777 continue;
411e1bfb 7778
102890f0 7779 /* LD -> LE */
411e1bfb 7780 tls_set = 0;
102890f0
AM
7781 tls_clear = TLS_LD;
7782 tls_type = TLS_TLS | TLS_LD;
7783 break;
411e1bfb 7784
102890f0
AM
7785 case R_PPC64_GOT_TLSGD16:
7786 case R_PPC64_GOT_TLSGD16_LO:
7787 expecting_tls_get_addr = 1;
663a1470 7788 found_tls_get_addr_arg = 1;
1a0670f3 7789 /* Fall through. */
102890f0
AM
7790
7791 case R_PPC64_GOT_TLSGD16_HI:
7792 case R_PPC64_GOT_TLSGD16_HA:
7793 if (ok_tprel)
7794 /* GD -> LE */
411e1bfb 7795 tls_set = 0;
102890f0
AM
7796 else
7797 /* GD -> IE */
b00a0a86 7798 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
7799 tls_clear = TLS_GD;
7800 tls_type = TLS_TLS | TLS_GD;
7801 break;
7802
7803 case R_PPC64_GOT_TPREL16_DS:
7804 case R_PPC64_GOT_TPREL16_LO_DS:
7805 case R_PPC64_GOT_TPREL16_HI:
7806 case R_PPC64_GOT_TPREL16_HA:
7807 if (ok_tprel)
7808 {
7809 /* IE -> LE */
7810 tls_set = 0;
7811 tls_clear = TLS_TPREL;
7812 tls_type = TLS_TLS | TLS_TPREL;
7813 break;
7814 }
411e1bfb
AM
7815 continue;
7816
727fc41e
AM
7817 case R_PPC64_TLSGD:
7818 case R_PPC64_TLSLD:
23cedd1d
AM
7819 if (rel + 1 < relend
7820 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7821 {
7822 if (pass != 0
2cdcc330 7823 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
7824 != R_PPC64_PLTSEQ)
7825 && (ELF64_R_TYPE (rel[1].r_info)
7826 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
7827 {
7828 r_symndx = ELF64_R_SYM (rel[1].r_info);
7829 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 7830 r_symndx, ibfd))
23cedd1d
AM
7831 goto err_free_rel;
7832 if (h != NULL)
7833 {
7834 struct plt_entry *ent = NULL;
7835
7836 for (ent = h->plt.plist;
7837 ent != NULL;
7838 ent = ent->next)
7839 if (ent->addend == rel[1].r_addend)
7840 break;
7841
7842 if (ent != NULL
7843 && ent->plt.refcount > 0)
7844 ent->plt.refcount -= 1;
7845 }
7846 }
7847 continue;
7848 }
663a1470 7849 found_tls_get_addr_arg = 1;
1a0670f3 7850 /* Fall through. */
663a1470
AM
7851
7852 case R_PPC64_TLS:
7853 case R_PPC64_TOC16:
7854 case R_PPC64_TOC16_LO:
102890f0
AM
7855 if (sym_sec == NULL || sym_sec != toc)
7856 continue;
7857
7858 /* Mark this toc entry as referenced by a TLS
7859 code sequence. We can do that now in the
7860 case of R_PPC64_TLS, and after checking for
7861 tls_get_addr for the TOC16 relocs. */
7862 if (toc_ref == NULL)
2cdcc330
AM
7863 toc_ref
7864 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
7865 if (toc_ref == NULL)
7866 goto err_free_rel;
7867
102890f0
AM
7868 if (h != NULL)
7869 value = h->root.u.def.value;
7870 else
7871 value = sym->st_value;
7872 value += rel->r_addend;
73242275
AM
7873 if (value % 8 != 0)
7874 continue;
7875 BFD_ASSERT (value < toc->size
7876 && toc->output_offset % 8 == 0);
663a1470 7877 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7878 if (r_type == R_PPC64_TLS
7879 || r_type == R_PPC64_TLSGD
7880 || r_type == R_PPC64_TLSLD)
102890f0
AM
7881 {
7882 toc_ref[toc_ref_index] = 1;
7883 continue;
7884 }
7885
7886 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7887 continue;
7888
7889 tls_set = 0;
7890 tls_clear = 0;
7891 expecting_tls_get_addr = 2;
7892 break;
7893
7894 case R_PPC64_TPREL64:
7895 if (pass == 0
7896 || sec != toc
7897 || toc_ref == NULL
663a1470 7898 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7899 continue;
7900 if (ok_tprel)
7901 {
7902 /* IE -> LE */
7903 tls_set = TLS_EXPLICIT;
7904 tls_clear = TLS_TPREL;
7905 break;
7906 }
7907 continue;
7908
7909 case R_PPC64_DTPMOD64:
7910 if (pass == 0
7911 || sec != toc
7912 || toc_ref == NULL
663a1470 7913 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7914 continue;
7915 if (rel + 1 < relend
7916 && (rel[1].r_info
7917 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7918 && rel[1].r_offset == rel->r_offset + 8)
7919 {
7920 if (ok_tprel)
7921 /* GD -> LE */
7922 tls_set = TLS_EXPLICIT | TLS_GD;
7923 else
7924 /* GD -> IE */
b00a0a86 7925 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
7926 tls_clear = TLS_GD;
7927 }
7928 else
7929 {
7930 if (!is_local)
7931 continue;
7932
7933 /* LD -> LE */
7934 tls_set = TLS_EXPLICIT;
7935 tls_clear = TLS_LD;
7936 }
7937 break;
7938
7939 default:
7940 continue;
7941 }
7942
7943 if (pass == 0)
7944 {
727fc41e
AM
7945 if (!expecting_tls_get_addr
7946 || !sec->has_tls_get_addr_call)
102890f0
AM
7947 continue;
7948
3a71aa26
AM
7949 if (rel + 1 < relend
7950 && branch_reloc_hash_match (ibfd, rel + 1,
7951 htab->tls_get_addr,
7952 htab->tls_get_addr_fd))
102890f0 7953 {
3a71aa26 7954 if (expecting_tls_get_addr == 2)
102890f0 7955 {
3a71aa26 7956 /* Check for toc tls entries. */
f961d9dd 7957 unsigned char *toc_tls;
3a71aa26
AM
7958 int retval;
7959
7960 retval = get_tls_mask (&toc_tls, NULL, NULL,
7961 &locsyms,
7962 rel, ibfd);
7963 if (retval == 0)
7964 goto err_free_rel;
663a1470
AM
7965 if (toc_tls != NULL)
7966 {
37da22e5
AM
7967 if ((*toc_tls & TLS_TLS) != 0
7968 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
7969 found_tls_get_addr_arg = 1;
7970 if (retval > 1)
7971 toc_ref[toc_ref_index] = 1;
7972 }
102890f0 7973 }
3a71aa26 7974 continue;
102890f0
AM
7975 }
7976
102890f0
AM
7977 /* Uh oh, we didn't find the expected call. We
7978 could just mark this symbol to exclude it
7979 from tls optimization but it's safer to skip
663a1470 7980 the entire optimization. */
695344c0 7981 /* xgettext:c-format */
25f53a85 7982 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
7983 "TLS optimization disabled\n"),
7984 ibfd, sec, rel->r_offset);
7985 ret = TRUE;
7986 goto err_free_rel;
102890f0
AM
7987 }
7988
37da22e5
AM
7989 /* If we don't have old-style __tls_get_addr calls
7990 without TLSGD/TLSLD marker relocs, and we haven't
7991 found a new-style __tls_get_addr call with a
7992 marker for this symbol, then we either have a
7993 broken object file or an -mlongcall style
7994 indirect call to __tls_get_addr without a marker.
7995 Disable optimization in this case. */
7996 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
7997 && (tls_set & TLS_EXPLICIT) == 0
7998 && !sec->has_tls_get_addr_call
7999 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8000 != (TLS_TLS | TLS_MARK)))
8001 continue;
8002
23cedd1d 8003 if (expecting_tls_get_addr)
102890f0 8004 {
23cedd1d
AM
8005 struct plt_entry *ent = NULL;
8006
8007 if (htab->tls_get_addr != NULL)
8008 for (ent = htab->tls_get_addr->elf.plt.plist;
8009 ent != NULL;
8010 ent = ent->next)
8011 if (ent->addend == 0)
102890f0 8012 break;
411e1bfb 8013
23cedd1d
AM
8014 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8015 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8016 ent != NULL;
8017 ent = ent->next)
8018 if (ent->addend == 0)
102890f0 8019 break;
23cedd1d
AM
8020
8021 if (ent != NULL
8022 && ent->plt.refcount > 0)
8023 ent->plt.refcount -= 1;
102890f0 8024 }
411e1bfb 8025
102890f0 8026 if (tls_clear == 0)
30038c59
AM
8027 continue;
8028
102890f0
AM
8029 if ((tls_set & TLS_EXPLICIT) == 0)
8030 {
8031 struct got_entry *ent;
411e1bfb 8032
102890f0
AM
8033 /* Adjust got entry for this reloc. */
8034 if (h != NULL)
8035 ent = h->got.glist;
8036 else
8037 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8038
102890f0
AM
8039 for (; ent != NULL; ent = ent->next)
8040 if (ent->addend == rel->r_addend
8041 && ent->owner == ibfd
8042 && ent->tls_type == tls_type)
8043 break;
8044 if (ent == NULL)
8045 abort ();
411e1bfb 8046
102890f0
AM
8047 if (tls_set == 0)
8048 {
8049 /* We managed to get rid of a got entry. */
8050 if (ent->got.refcount > 0)
8051 ent->got.refcount -= 1;
8052 }
8053 }
8054 else
8055 {
8056 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8057 we'll lose one or two dyn relocs. */
8058 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8059 NULL, h, sym))
102890f0 8060 return FALSE;
411e1bfb 8061
102890f0
AM
8062 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8063 {
8064 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8065 NULL, h, sym))
102890f0
AM
8066 return FALSE;
8067 }
8068 }
411e1bfb 8069
102890f0
AM
8070 *tls_mask |= tls_set;
8071 *tls_mask &= ~tls_clear;
8072 }
8c1d1bb8 8073
102890f0
AM
8074 if (elf_section_data (sec)->relocs != relstart)
8075 free (relstart);
8076 }
411e1bfb 8077
663a1470
AM
8078 if (locsyms != NULL
8079 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8080 {
8081 if (!info->keep_memory)
8082 free (locsyms);
8083 else
8084 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8085 }
8086 }
411e1bfb 8087
663a1470
AM
8088 if (toc_ref != NULL)
8089 free (toc_ref);
9a23f96e 8090 htab->do_tls_opt = 1;
b34976b6 8091 return TRUE;
1e2f5b6e 8092}
b34976b6 8093
c5614fa4
AM
8094/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8095 the values of any global symbols in a toc section that has been
8096 edited. Globals in toc sections should be a rarity, so this function
8097 sets a flag if any are found in toc sections other than the one just
de194d85 8098 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8099
8100struct adjust_toc_info
8101{
8102 asection *toc;
8103 unsigned long *skip;
8104 bfd_boolean global_toc_syms;
8105};
8106
ba761f19
AM
8107enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8108
c5614fa4
AM
8109static bfd_boolean
8110adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8111{
8112 struct ppc_link_hash_entry *eh;
8113 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8114 unsigned long i;
c5614fa4 8115
c5614fa4
AM
8116 if (h->root.type != bfd_link_hash_defined
8117 && h->root.type != bfd_link_hash_defweak)
8118 return TRUE;
8119
8120 eh = (struct ppc_link_hash_entry *) h;
8121 if (eh->adjust_done)
8122 return TRUE;
8123
8124 if (eh->elf.root.u.def.section == toc_inf->toc)
8125 {
854b41e7
AM
8126 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8127 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8128 else
854b41e7
AM
8129 i = eh->elf.root.u.def.value >> 3;
8130
ba761f19 8131 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8132 {
4eca0228 8133 _bfd_error_handler
854b41e7
AM
8134 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8135 do
8136 ++i;
ba761f19 8137 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8138 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8139 }
854b41e7
AM
8140
8141 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8142 eh->adjust_done = 1;
8143 }
8144 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8145 toc_inf->global_toc_syms = TRUE;
8146
8147 return TRUE;
8148}
8149
39eeab25
AM
8150/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8151 on a _LO variety toc/got reloc. */
560c8763
AM
8152
8153static bfd_boolean
39eeab25 8154ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8155{
39eeab25
AM
8156 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8157 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8158 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8159 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8160 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8161 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8162 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8163 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8164 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8165 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8166 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8167 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8168 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8169 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8170 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8171 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8172 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8173 /* Exclude lfqu by testing reloc. If relocs are ever
8174 defined for the reduced D field in psq_lu then those
8175 will need testing too. */
8176 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8177 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8178 && (insn & 1) == 0)
8179 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8180 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8181 /* Exclude stfqu. psq_stu as above for psq_lu. */
8182 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8183 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8184 && (insn & 1) == 0));
560c8763
AM
8185}
8186
4a421c53
AM
8187/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8188 pld ra,symbol@got@pcrel
8189 load/store rt,0(ra)
8190 or
d4b87b1e 8191 pla ra,symbol@pcrel
4a421c53
AM
8192 load/store rt,0(ra)
8193 may be translated to
8194 pload/pstore rt,symbol@pcrel
8195 nop.
8196 This function returns true if the optimization is possible, placing
8197 the prefix insn in *PINSN1 and a NOP in *PINSN2.
8198
8199 On entry to this function, the linker has already determined that
d4b87b1e 8200 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8201 while *PINSN2 is the second instruction. */
8202
8203static bfd_boolean
8204xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2)
8205{
8206 uint32_t insn2 = *pinsn2 >> 32;
8207 uint64_t i1new;
8208
8209 /* Check that regs match. */
8210 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
8211 return FALSE;
8212
8213 switch ((insn2 >> 26) & 63)
8214 {
8215 default:
8216 return FALSE;
8217
8218 case 32: /* lwz */
8219 case 34: /* lbz */
8220 case 36: /* stw */
8221 case 38: /* stb */
8222 case 40: /* lhz */
8223 case 42: /* lha */
8224 case 44: /* sth */
8225 case 48: /* lfs */
8226 case 50: /* lfd */
8227 case 52: /* stfs */
8228 case 54: /* stfd */
8229 /* These are the PMLS cases, where we just need to tack a prefix
8230 on the insn. Check that the D field is zero. */
8231 if ((insn2 & 0xffff) != 0)
8232 return FALSE;
8233 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8234 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8235 break;
8236
8237 case 58: /* lwa, ld */
8238 if ((insn2 & 0xfffd) != 0)
8239 return FALSE;
8240 i1new = ((1ULL << 58) | (1ULL << 52)
8241 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8242 | (insn2 & (31ULL << 21)));
8243 break;
8244
8245 case 57: /* lxsd, lxssp */
8246 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
8247 return FALSE;
8248 i1new = ((1ULL << 58) | (1ULL << 52)
8249 | ((40ULL | (insn2 & 3)) << 26)
8250 | (insn2 & (31ULL << 21)));
8251 break;
8252
8253 case 61: /* stxsd, stxssp, lxv, stxv */
8254 if ((insn2 & 3) == 0)
8255 return FALSE;
8256 else if ((insn2 & 3) >= 2)
8257 {
8258 if ((insn2 & 0xfffc) != 0)
8259 return FALSE;
8260 i1new = ((1ULL << 58) | (1ULL << 52)
8261 | ((44ULL | (insn2 & 3)) << 26)
8262 | (insn2 & (31ULL << 21)));
8263 }
8264 else
8265 {
8266 if ((insn2 & 0xfff0) != 0)
8267 return FALSE;
8268 i1new = ((1ULL << 58) | (1ULL << 52)
8269 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8270 | (insn2 & (31ULL << 21)));
8271 }
8272 break;
8273
8274 case 56: /* lq */
8275 if ((insn2 & 0xffff) != 0)
8276 return FALSE;
8277 i1new = ((1ULL << 58) | (1ULL << 52)
8278 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8279 break;
8280
8281 case 62: /* std, stq */
8282 if ((insn2 & 0xfffd) != 0)
8283 return FALSE;
8284 i1new = ((1ULL << 58) | (1ULL << 52)
8285 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8286 | (insn2 & (31ULL << 21)));
8287 break;
8288 }
8289
8290 *pinsn1 = i1new;
8291 *pinsn2 = (uint64_t) NOP << 32;
8292 return TRUE;
8293}
8294
c5614fa4
AM
8295/* Examine all relocs referencing .toc sections in order to remove
8296 unused .toc entries. */
8297
8298bfd_boolean
33c0ec9d 8299ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8300{
8301 bfd *ibfd;
8302 struct adjust_toc_info toc_inf;
67f0cbdb 8303 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8304
67f0cbdb 8305 htab->do_toc_opt = 1;
c5614fa4 8306 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8307 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8308 {
8309 asection *toc, *sec;
8310 Elf_Internal_Shdr *symtab_hdr;
8311 Elf_Internal_Sym *local_syms;
425b145b 8312 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8313 unsigned long *skip, *drop;
8314 unsigned char *used;
8315 unsigned char *keep, last, some_unused;
8316
854b41e7
AM
8317 if (!is_ppc64_elf (ibfd))
8318 continue;
8319
c5614fa4
AM
8320 toc = bfd_get_section_by_name (ibfd, ".toc");
8321 if (toc == NULL
92b7a70f 8322 || toc->size == 0
dbaa2011
AM
8323 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8324 || discarded_section (toc))
c5614fa4
AM
8325 continue;
8326
425b145b 8327 toc_relocs = NULL;
c5614fa4 8328 local_syms = NULL;
0ffa91dd 8329 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8330
8331 /* Look at sections dropped from the final link. */
8332 skip = NULL;
8333 relstart = NULL;
8334 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8335 {
8336 if (sec->reloc_count == 0
dbaa2011 8337 || !discarded_section (sec)
c5614fa4
AM
8338 || get_opd_info (sec)
8339 || (sec->flags & SEC_ALLOC) == 0
8340 || (sec->flags & SEC_DEBUGGING) != 0)
8341 continue;
8342
8343 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8344 if (relstart == NULL)
8345 goto error_ret;
8346
8347 /* Run through the relocs to see which toc entries might be
8348 unused. */
8349 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8350 {
8351 enum elf_ppc64_reloc_type r_type;
8352 unsigned long r_symndx;
8353 asection *sym_sec;
8354 struct elf_link_hash_entry *h;
8355 Elf_Internal_Sym *sym;
8356 bfd_vma val;
8357
8358 r_type = ELF64_R_TYPE (rel->r_info);
8359 switch (r_type)
8360 {
8361 default:
8362 continue;
8363
8364 case R_PPC64_TOC16:
8365 case R_PPC64_TOC16_LO:
8366 case R_PPC64_TOC16_HI:
8367 case R_PPC64_TOC16_HA:
8368 case R_PPC64_TOC16_DS:
8369 case R_PPC64_TOC16_LO_DS:
8370 break;
8371 }
8372
8373 r_symndx = ELF64_R_SYM (rel->r_info);
8374 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8375 r_symndx, ibfd))
8376 goto error_ret;
8377
8378 if (sym_sec != toc)
8379 continue;
8380
8381 if (h != NULL)
8382 val = h->root.u.def.value;
8383 else
8384 val = sym->st_value;
8385 val += rel->r_addend;
8386
8387 if (val >= toc->size)
8388 continue;
8389
8390 /* Anything in the toc ought to be aligned to 8 bytes.
8391 If not, don't mark as unused. */
8392 if (val & 7)
8393 continue;
8394
8395 if (skip == NULL)
8396 {
854b41e7 8397 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8398 if (skip == NULL)
8399 goto error_ret;
8400 }
8401
ba761f19 8402 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8403 }
8404
8405 if (elf_section_data (sec)->relocs != relstart)
8406 free (relstart);
8407 }
8408
ba761f19
AM
8409 /* For largetoc loads of address constants, we can convert
8410 . addis rx,2,addr@got@ha
8411 . ld ry,addr@got@l(rx)
8412 to
8413 . addis rx,2,addr@toc@ha
8414 . addi ry,rx,addr@toc@l
8415 when addr is within 2G of the toc pointer. This then means
8416 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8417
ba761f19
AM
8418 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8419 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8420 && toc->reloc_count != 0)
8421 {
8422 /* Read toc relocs. */
425b145b
AM
8423 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8424 info->keep_memory);
8425 if (toc_relocs == NULL)
ba761f19
AM
8426 goto error_ret;
8427
425b145b 8428 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8429 {
8430 enum elf_ppc64_reloc_type r_type;
8431 unsigned long r_symndx;
8432 asection *sym_sec;
8433 struct elf_link_hash_entry *h;
8434 Elf_Internal_Sym *sym;
8435 bfd_vma val, addr;
8436
8437 r_type = ELF64_R_TYPE (rel->r_info);
8438 if (r_type != R_PPC64_ADDR64)
8439 continue;
8440
8441 r_symndx = ELF64_R_SYM (rel->r_info);
8442 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8443 r_symndx, ibfd))
8444 goto error_ret;
8445
425b145b 8446 if (sym_sec == NULL
c27b8c2a 8447 || sym_sec->output_section == NULL
dbaa2011 8448 || discarded_section (sym_sec))
425b145b
AM
8449 continue;
8450
afe397ea 8451 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8452 continue;
8453
8454 if (h != NULL)
bddc25c9
AM
8455 {
8456 if (h->type == STT_GNU_IFUNC)
8457 continue;
8458 val = h->root.u.def.value;
8459 }
ba761f19 8460 else
bddc25c9
AM
8461 {
8462 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8463 continue;
8464 val = sym->st_value;
8465 }
ba761f19
AM
8466 val += rel->r_addend;
8467 val += sym_sec->output_section->vma + sym_sec->output_offset;
8468
8469 /* We don't yet know the exact toc pointer value, but we
8470 know it will be somewhere in the toc section. Don't
8471 optimize if the difference from any possible toc
8472 pointer is outside [ff..f80008000, 7fff7fff]. */
8473 addr = toc->output_section->vma + TOC_BASE_OFF;
8474 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8475 continue;
8476
8477 addr = toc->output_section->vma + toc->output_section->rawsize;
8478 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8479 continue;
8480
8481 if (skip == NULL)
8482 {
8483 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8484 if (skip == NULL)
8485 goto error_ret;
8486 }
8487
8488 skip[rel->r_offset >> 3]
425b145b 8489 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8490 }
ba761f19
AM
8491 }
8492
c5614fa4
AM
8493 if (skip == NULL)
8494 continue;
8495
8496 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8497 if (used == NULL)
8498 {
8499 error_ret:
8500 if (local_syms != NULL
8501 && symtab_hdr->contents != (unsigned char *) local_syms)
8502 free (local_syms);
8503 if (sec != NULL
8504 && relstart != NULL
8505 && elf_section_data (sec)->relocs != relstart)
8506 free (relstart);
425b145b
AM
8507 if (toc_relocs != NULL
8508 && elf_section_data (toc)->relocs != toc_relocs)
8509 free (toc_relocs);
c5614fa4
AM
8510 if (skip != NULL)
8511 free (skip);
8512 return FALSE;
8513 }
8514
30038c59
AM
8515 /* Now check all kept sections that might reference the toc.
8516 Check the toc itself last. */
8517 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8518 : ibfd->sections);
c5614fa4 8519 sec != NULL;
c5614fa4 8520 sec = (sec == toc ? NULL
c5614fa4 8521 : sec->next == NULL ? toc
30038c59 8522 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8523 : sec->next))
8524 {
8525 int repeat;
8526
8527 if (sec->reloc_count == 0
dbaa2011 8528 || discarded_section (sec)
c5614fa4
AM
8529 || get_opd_info (sec)
8530 || (sec->flags & SEC_ALLOC) == 0
8531 || (sec->flags & SEC_DEBUGGING) != 0)
8532 continue;
8533
854b41e7
AM
8534 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8535 info->keep_memory);
c5614fa4 8536 if (relstart == NULL)
2915c55b
JK
8537 {
8538 free (used);
8539 goto error_ret;
8540 }
c5614fa4
AM
8541
8542 /* Mark toc entries referenced as used. */
c5614fa4 8543 do
d4f1ee75
AM
8544 {
8545 repeat = 0;
8546 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8547 {
8548 enum elf_ppc64_reloc_type r_type;
8549 unsigned long r_symndx;
8550 asection *sym_sec;
8551 struct elf_link_hash_entry *h;
8552 Elf_Internal_Sym *sym;
8553 bfd_vma val;
8554 enum {no_check, check_lo, check_ha} insn_check;
98528052 8555
d4f1ee75
AM
8556 r_type = ELF64_R_TYPE (rel->r_info);
8557 switch (r_type)
8558 {
8559 default:
8560 insn_check = no_check;
8561 break;
98528052 8562
d4f1ee75
AM
8563 case R_PPC64_GOT_TLSLD16_HA:
8564 case R_PPC64_GOT_TLSGD16_HA:
8565 case R_PPC64_GOT_TPREL16_HA:
8566 case R_PPC64_GOT_DTPREL16_HA:
8567 case R_PPC64_GOT16_HA:
8568 case R_PPC64_TOC16_HA:
8569 insn_check = check_ha;
8570 break;
98528052 8571
d4f1ee75
AM
8572 case R_PPC64_GOT_TLSLD16_LO:
8573 case R_PPC64_GOT_TLSGD16_LO:
8574 case R_PPC64_GOT_TPREL16_LO_DS:
8575 case R_PPC64_GOT_DTPREL16_LO_DS:
8576 case R_PPC64_GOT16_LO:
8577 case R_PPC64_GOT16_LO_DS:
8578 case R_PPC64_TOC16_LO:
8579 case R_PPC64_TOC16_LO_DS:
8580 insn_check = check_lo;
8581 break;
8582 }
560c8763 8583
d4f1ee75
AM
8584 if (insn_check != no_check)
8585 {
8586 bfd_vma off = rel->r_offset & ~3;
8587 unsigned char buf[4];
8588 unsigned int insn;
c5614fa4 8589
d4f1ee75
AM
8590 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8591 {
8592 free (used);
8593 goto error_ret;
8594 }
8595 insn = bfd_get_32 (ibfd, buf);
8596 if (insn_check == check_lo
39eeab25 8597 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
8598 : ((insn & ((0x3f << 26) | 0x1f << 16))
8599 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8600 {
8601 char str[12];
8602
8603 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8604 sprintf (str, "%#08x", insn);
8605 info->callbacks->einfo
695344c0 8606 /* xgettext:c-format */
174d0a74 8607 (_("%H: toc optimization is not supported for"
cf97bcb0 8608 " %s instruction\n"),
d4f1ee75
AM
8609 ibfd, sec, rel->r_offset & ~3, str);
8610 }
8611 }
c5614fa4 8612
d4f1ee75
AM
8613 switch (r_type)
8614 {
8615 case R_PPC64_TOC16:
8616 case R_PPC64_TOC16_LO:
8617 case R_PPC64_TOC16_HI:
8618 case R_PPC64_TOC16_HA:
8619 case R_PPC64_TOC16_DS:
8620 case R_PPC64_TOC16_LO_DS:
8621 /* In case we're taking addresses of toc entries. */
8622 case R_PPC64_ADDR64:
8623 break;
c5614fa4 8624
d4f1ee75
AM
8625 default:
8626 continue;
8627 }
c5614fa4 8628
d4f1ee75
AM
8629 r_symndx = ELF64_R_SYM (rel->r_info);
8630 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8631 r_symndx, ibfd))
8632 {
8633 free (used);
8634 goto error_ret;
8635 }
c5614fa4 8636
d4f1ee75
AM
8637 if (sym_sec != toc)
8638 continue;
c5614fa4 8639
d4f1ee75
AM
8640 if (h != NULL)
8641 val = h->root.u.def.value;
8642 else
8643 val = sym->st_value;
8644 val += rel->r_addend;
ba761f19 8645
d4f1ee75
AM
8646 if (val >= toc->size)
8647 continue;
ba761f19 8648
d4f1ee75
AM
8649 if ((skip[val >> 3] & can_optimize) != 0)
8650 {
8651 bfd_vma off;
8652 unsigned char opc;
8653
8654 switch (r_type)
8655 {
8656 case R_PPC64_TOC16_HA:
ba761f19 8657 break;
ba761f19 8658
d4f1ee75
AM
8659 case R_PPC64_TOC16_LO_DS:
8660 off = rel->r_offset;
8661 off += (bfd_big_endian (ibfd) ? -2 : 3);
8662 if (!bfd_get_section_contents (ibfd, sec, &opc,
8663 off, 1))
8664 {
8665 free (used);
8666 goto error_ret;
8667 }
8668 if ((opc & (0x3f << 2)) == (58u << 2))
8669 break;
1a0670f3 8670 /* Fall through. */
ba761f19 8671
d4f1ee75
AM
8672 default:
8673 /* Wrong sort of reloc, or not a ld. We may
8674 as well clear ref_from_discarded too. */
8675 skip[val >> 3] = 0;
8676 }
8677 }
8678
8679 if (sec != toc)
8680 used[val >> 3] = 1;
8681 /* For the toc section, we only mark as used if this
8682 entry itself isn't unused. */
8683 else if ((used[rel->r_offset >> 3]
8684 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8685 && !used[val >> 3])
8686 {
8687 /* Do all the relocs again, to catch reference
8688 chains. */
8689 repeat = 1;
8690 used[val >> 3] = 1;
8691 }
8692 }
8693 }
c5614fa4 8694 while (repeat);
854b41e7
AM
8695
8696 if (elf_section_data (sec)->relocs != relstart)
8697 free (relstart);
c5614fa4
AM
8698 }
8699
8700 /* Merge the used and skip arrays. Assume that TOC
8701 doublewords not appearing as either used or unused belong
de194d85 8702 to an entry more than one doubleword in size. */
c5614fa4
AM
8703 for (drop = skip, keep = used, last = 0, some_unused = 0;
8704 drop < skip + (toc->size + 7) / 8;
8705 ++drop, ++keep)
8706 {
8707 if (*keep)
8708 {
ba761f19
AM
8709 *drop &= ~ref_from_discarded;
8710 if ((*drop & can_optimize) != 0)
8711 some_unused = 1;
c5614fa4
AM
8712 last = 0;
8713 }
b140b010 8714 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8715 {
8716 some_unused = 1;
ba761f19 8717 last = ref_from_discarded;
c5614fa4
AM
8718 }
8719 else
8720 *drop = last;
8721 }
8722
8723 free (used);
8724
8725 if (some_unused)
8726 {
8727 bfd_byte *contents, *src;
8728 unsigned long off;
d62b3684 8729 Elf_Internal_Sym *sym;
ba761f19 8730 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8731
8732 /* Shuffle the toc contents, and at the same time convert the
8733 skip array from booleans into offsets. */
8734 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8735 goto error_ret;
8736
8737 elf_section_data (toc)->this_hdr.contents = contents;
8738
8739 for (src = contents, off = 0, drop = skip;
8740 src < contents + toc->size;
8741 src += 8, ++drop)
8742 {
ba761f19
AM
8743 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8744 off += 8;
c5614fa4
AM
8745 else if (off != 0)
8746 {
8747 *drop = off;
8748 memcpy (src - off, src, 8);
8749 }
8750 }
854b41e7 8751 *drop = off;
c5614fa4
AM
8752 toc->rawsize = toc->size;
8753 toc->size = src - contents - off;
8754
ba761f19
AM
8755 /* Adjust addends for relocs against the toc section sym,
8756 and optimize any accesses we can. */
c5614fa4
AM
8757 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8758 {
8759 if (sec->reloc_count == 0
dbaa2011 8760 || discarded_section (sec))
c5614fa4
AM
8761 continue;
8762
8763 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8764 info->keep_memory);
c5614fa4
AM
8765 if (relstart == NULL)
8766 goto error_ret;
8767
8768 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8769 {
8770 enum elf_ppc64_reloc_type r_type;
8771 unsigned long r_symndx;
8772 asection *sym_sec;
8773 struct elf_link_hash_entry *h;
854b41e7 8774 bfd_vma val;
c5614fa4
AM
8775
8776 r_type = ELF64_R_TYPE (rel->r_info);
8777 switch (r_type)
8778 {
8779 default:
8780 continue;
8781
8782 case R_PPC64_TOC16:
8783 case R_PPC64_TOC16_LO:
8784 case R_PPC64_TOC16_HI:
8785 case R_PPC64_TOC16_HA:
8786 case R_PPC64_TOC16_DS:
8787 case R_PPC64_TOC16_LO_DS:
8788 case R_PPC64_ADDR64:
8789 break;
8790 }
8791
8792 r_symndx = ELF64_R_SYM (rel->r_info);
8793 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8794 r_symndx, ibfd))
8795 goto error_ret;
8796
ba761f19 8797 if (sym_sec != toc)
c5614fa4
AM
8798 continue;
8799
ba761f19
AM
8800 if (h != NULL)
8801 val = h->root.u.def.value;
8802 else
8803 {
8804 val = sym->st_value;
8805 if (val != 0)
8806 local_toc_syms = TRUE;
8807 }
8808
8809 val += rel->r_addend;
854b41e7
AM
8810
8811 if (val > toc->rawsize)
8812 val = toc->rawsize;
ba761f19
AM
8813 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8814 continue;
8815 else if ((skip[val >> 3] & can_optimize) != 0)
8816 {
8817 Elf_Internal_Rela *tocrel
425b145b 8818 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8819 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8820
8821 switch (r_type)
8822 {
8823 case R_PPC64_TOC16_HA:
8824 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8825 break;
8826
8827 case R_PPC64_TOC16_LO_DS:
8828 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8829 break;
8830
8831 default:
28942f62
AM
8832 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8833 ppc_howto_init ();
b140b010 8834 info->callbacks->einfo
695344c0 8835 /* xgettext:c-format */
174d0a74 8836 (_("%H: %s references "
b140b010
AM
8837 "optimized away TOC entry\n"),
8838 ibfd, sec, rel->r_offset,
8839 ppc64_elf_howto_table[r_type]->name);
8840 bfd_set_error (bfd_error_bad_value);
8841 goto error_ret;
ba761f19
AM
8842 }
8843 rel->r_addend = tocrel->r_addend;
8844 elf_section_data (sec)->relocs = relstart;
8845 continue;
8846 }
8847
8848 if (h != NULL || sym->st_value != 0)
8849 continue;
854b41e7
AM
8850
8851 rel->r_addend -= skip[val >> 3];
8852 elf_section_data (sec)->relocs = relstart;
c5614fa4 8853 }
854b41e7
AM
8854
8855 if (elf_section_data (sec)->relocs != relstart)
8856 free (relstart);
c5614fa4
AM
8857 }
8858
8859 /* We shouldn't have local or global symbols defined in the TOC,
8860 but handle them anyway. */
df22d223
AM
8861 if (local_syms != NULL)
8862 for (sym = local_syms;
8863 sym < local_syms + symtab_hdr->sh_info;
8864 ++sym)
8865 if (sym->st_value != 0
8866 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8867 {
8868 unsigned long i;
854b41e7 8869
df22d223
AM
8870 if (sym->st_value > toc->rawsize)
8871 i = toc->rawsize >> 3;
8872 else
8873 i = sym->st_value >> 3;
854b41e7 8874
df22d223
AM
8875 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8876 {
8877 if (local_toc_syms)
4eca0228 8878 _bfd_error_handler
df22d223
AM
8879 (_("%s defined on removed toc entry"),
8880 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8881 do
8882 ++i;
8883 while ((skip[i] & (ref_from_discarded | can_optimize)));
8884 sym->st_value = (bfd_vma) i << 3;
8885 }
d62b3684 8886
df22d223
AM
8887 sym->st_value -= skip[i];
8888 symtab_hdr->contents = (unsigned char *) local_syms;
8889 }
c5614fa4 8890
854b41e7 8891 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8892 if (toc_inf.global_toc_syms)
8893 {
8894 toc_inf.toc = toc;
8895 toc_inf.skip = skip;
8896 toc_inf.global_toc_syms = FALSE;
8897 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8898 &toc_inf);
8899 }
854b41e7
AM
8900
8901 if (toc->reloc_count != 0)
8902 {
d4730f92 8903 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8904 Elf_Internal_Rela *wrel;
8905 bfd_size_type sz;
8906
854b41e7 8907 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8908 if (toc_relocs == NULL)
8909 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8910 info->keep_memory);
8911 if (toc_relocs == NULL)
8912 goto error_ret;
8913
425b145b
AM
8914 wrel = toc_relocs;
8915 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8916 if ((skip[rel->r_offset >> 3]
8917 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8918 {
8919 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8920 wrel->r_info = rel->r_info;
8921 wrel->r_addend = rel->r_addend;
8922 ++wrel;
8923 }
8924 else if (!dec_dynrel_count (rel->r_info, toc, info,
8925 &local_syms, NULL, NULL))
8926 goto error_ret;
8927
425b145b
AM
8928 elf_section_data (toc)->relocs = toc_relocs;
8929 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8930 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8931 sz = rel_hdr->sh_entsize;
8932 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8933 }
c5614fa4 8934 }
28be611c
AM
8935 else if (toc_relocs != NULL
8936 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8937 free (toc_relocs);
c5614fa4
AM
8938
8939 if (local_syms != NULL
8940 && symtab_hdr->contents != (unsigned char *) local_syms)
8941 {
8942 if (!info->keep_memory)
8943 free (local_syms);
8944 else
8945 symtab_hdr->contents = (unsigned char *) local_syms;
8946 }
8947 free (skip);
8948 }
8949
066f4018 8950 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
8951 a GOT relative or PC relative access, possibly reducing the
8952 number of GOT entries. */
066f4018
AM
8953 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8954 {
8955 asection *sec;
8956 Elf_Internal_Shdr *symtab_hdr;
8957 Elf_Internal_Sym *local_syms;
8958 Elf_Internal_Rela *relstart, *rel;
8959 bfd_vma got;
8960
8961 if (!is_ppc64_elf (ibfd))
8962 continue;
8963
8964 if (!ppc64_elf_tdata (ibfd)->has_gotrel)
8965 continue;
8966
8967 sec = ppc64_elf_tdata (ibfd)->got;
8968 got = sec->output_section->vma + sec->output_offset + 0x8000;
8969
8970 local_syms = NULL;
8971 symtab_hdr = &elf_symtab_hdr (ibfd);
8972
8973 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8974 {
8975 if (sec->reloc_count == 0
8976 || !ppc64_elf_section_data (sec)->has_gotrel
8977 || discarded_section (sec))
8978 continue;
8979
8980 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8981 info->keep_memory);
8982 if (relstart == NULL)
8983 {
8984 got_error_ret:
8985 if (local_syms != NULL
8986 && symtab_hdr->contents != (unsigned char *) local_syms)
8987 free (local_syms);
8988 if (sec != NULL
8989 && relstart != NULL
8990 && elf_section_data (sec)->relocs != relstart)
8991 free (relstart);
8992 return FALSE;
8993 }
8994
8995 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8996 {
8997 enum elf_ppc64_reloc_type r_type;
8998 unsigned long r_symndx;
8999 Elf_Internal_Sym *sym;
9000 asection *sym_sec;
9001 struct elf_link_hash_entry *h;
9002 struct got_entry *ent;
4a421c53
AM
9003 bfd_vma sym_addend, val, pc;
9004 unsigned char buf[8];
066f4018
AM
9005 unsigned int insn;
9006
9007 r_type = ELF64_R_TYPE (rel->r_info);
9008 switch (r_type)
9009 {
bb22a418
AM
9010 /* Note that we don't delete GOT entries for
9011 R_PPC64_GOT16_DS since we'd need a lot more
9012 analysis. For starters, the preliminary layout is
9013 before the GOT, PLT, dynamic sections and stubs are
9014 laid out. Then we'd need to allow for changes in
9015 distance between sections caused by alignment. */
066f4018
AM
9016 default:
9017 continue;
9018
066f4018
AM
9019 case R_PPC64_GOT16_HA:
9020 case R_PPC64_GOT16_LO_DS:
4a421c53
AM
9021 sym_addend = rel->r_addend;
9022 break;
9023
9024 case R_PPC64_GOT_PCREL34:
9025 sym_addend = 0;
066f4018
AM
9026 break;
9027 }
9028
9029 r_symndx = ELF64_R_SYM (rel->r_info);
9030 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9031 r_symndx, ibfd))
9032 goto got_error_ret;
9033
9034 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9035 continue;
9036
9037 if (h != NULL)
9038 val = h->root.u.def.value;
9039 else
9040 val = sym->st_value;
4a421c53 9041 val += sym_addend;
066f4018
AM
9042 val += sym_sec->output_section->vma + sym_sec->output_offset;
9043
bb22a418
AM
9044/* Fudge factor to allow for the fact that the preliminary layout
9045 isn't exact. Reduce limits by this factor. */
9046#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9047
066f4018
AM
9048 switch (r_type)
9049 {
9050 default:
9051 continue;
9052
066f4018 9053 case R_PPC64_GOT16_HA:
bb22a418
AM
9054 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9055 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9056 continue;
9057
9058 if (!bfd_get_section_contents (ibfd, sec, buf,
9059 rel->r_offset & ~3, 4))
9060 goto got_error_ret;
9061 insn = bfd_get_32 (ibfd, buf);
9062 if (((insn & ((0x3f << 26) | 0x1f << 16))
9063 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9064 continue;
9065 break;
9066
9067 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9068 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9069 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9070 continue;
9071 if (!bfd_get_section_contents (ibfd, sec, buf,
9072 rel->r_offset & ~3, 4))
9073 goto got_error_ret;
9074 insn = bfd_get_32 (ibfd, buf);
9075 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9076 continue;
9077 break;
4a421c53
AM
9078
9079 case R_PPC64_GOT_PCREL34:
9080 pc = rel->r_offset;
9081 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9082 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9083 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9084 continue;
9085 if (!bfd_get_section_contents (ibfd, sec, buf,
9086 rel->r_offset & ~3, 8))
9087 goto got_error_ret;
9088 insn = bfd_get_32 (ibfd, buf);
9089 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9090 continue;
9091 insn = bfd_get_32 (ibfd, buf + 4);
9092 if ((insn & (0x3f << 26)) != 57u << 26)
9093 continue;
9094 break;
066f4018 9095 }
bb22a418 9096#undef LIMIT_ADJUST
066f4018
AM
9097
9098 if (h != NULL)
9099 ent = h->got.glist;
9100 else
9101 {
9102 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9103 ent = local_got_ents[r_symndx];
9104 }
9105 for (; ent != NULL; ent = ent->next)
4a421c53 9106 if (ent->addend == sym_addend
066f4018
AM
9107 && ent->owner == ibfd
9108 && ent->tls_type == 0)
9109 break;
9110 BFD_ASSERT (ent && ent->got.refcount > 0);
9111 ent->got.refcount -= 1;
9112 }
9113
9114 if (elf_section_data (sec)->relocs != relstart)
9115 free (relstart);
9116 }
9117
9118 if (local_syms != NULL
9119 && symtab_hdr->contents != (unsigned char *) local_syms)
9120 {
9121 if (!info->keep_memory)
9122 free (local_syms);
9123 else
9124 symtab_hdr->contents = (unsigned char *) local_syms;
9125 }
9126 }
9127
c5614fa4
AM
9128 return TRUE;
9129}
9130
1bbe0902
AM
9131/* Return true iff input section I references the TOC using
9132 instructions limited to +/-32k offsets. */
9133
9134bfd_boolean
9135ppc64_elf_has_small_toc_reloc (asection *i)
9136{
9137 return (is_ppc64_elf (i->owner)
9138 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9139}
9140
927be08e
AM
9141/* Allocate space for one GOT entry. */
9142
9143static void
9144allocate_got (struct elf_link_hash_entry *h,
9145 struct bfd_link_info *info,
9146 struct got_entry *gent)
9147{
9148 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9149 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9150 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9151 ? 16 : 8);
9152 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9153 ? 2 : 1) * sizeof (Elf64_External_Rela);
9154 asection *got = ppc64_elf_tdata (gent->owner)->got;
9155
9156 gent->got.offset = got->size;
9157 got->size += entsize;
9158
19e08130 9159 if (h->type == STT_GNU_IFUNC)
927be08e 9160 {
33e44f2e 9161 htab->elf.irelplt->size += rentsize;
19e08130 9162 htab->got_reli_size += rentsize;
927be08e 9163 }
f15d0b54
AM
9164 else if (((bfd_link_pic (info)
9165 && !((gent->tls_type & TLS_TPREL) != 0
9166 && bfd_link_executable (info)
9167 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9168 || (htab->elf.dynamic_sections_created
9169 && h->dynindx != -1
9170 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9171 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9172 {
19e08130 9173 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9174 relgot->size += rentsize;
927be08e
AM
9175 }
9176}
9177
7865406b
AM
9178/* This function merges got entries in the same toc group. */
9179
9180static void
9181merge_got_entries (struct got_entry **pent)
9182{
9183 struct got_entry *ent, *ent2;
9184
9185 for (ent = *pent; ent != NULL; ent = ent->next)
9186 if (!ent->is_indirect)
9187 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9188 if (!ent2->is_indirect
9189 && ent2->addend == ent->addend
9190 && ent2->tls_type == ent->tls_type
9191 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9192 {
9193 ent2->is_indirect = TRUE;
9194 ent2->got.ent = ent;
9195 }
9196}
9197
46434633 9198/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9199
9200static bfd_boolean
46434633
AM
9201ensure_undef_dynamic (struct bfd_link_info *info,
9202 struct elf_link_hash_entry *h)
f0158f44
AM
9203{
9204 struct elf_link_hash_table *htab = elf_hash_table (info);
9205
9206 if (htab->dynamic_sections_created
46434633
AM
9207 && ((info->dynamic_undefined_weak != 0
9208 && h->root.type == bfd_link_hash_undefweak)
9209 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9210 && h->dynindx == -1
9211 && !h->forced_local
9212 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9213 return bfd_elf_link_record_dynamic_symbol (info, h);
9214 return TRUE;
9215}
9216
65f38f15
AM
9217/* Allocate space in .plt, .got and associated reloc sections for
9218 dynamic relocs. */
5bd4f169 9219
b34976b6 9220static bfd_boolean
4ce794b7 9221allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9222{
65f38f15
AM
9223 struct bfd_link_info *info;
9224 struct ppc_link_hash_table *htab;
5bd4f169 9225 asection *s;
65f38f15 9226 struct ppc_link_hash_entry *eh;
0b8bcf0d 9227 struct got_entry **pgent, *gent;
5bd4f169 9228
e92d460e 9229 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9230 return TRUE;
5bd4f169 9231
65f38f15
AM
9232 info = (struct bfd_link_info *) inf;
9233 htab = ppc_hash_table (info);
4dfe6ac6
NC
9234 if (htab == NULL)
9235 return FALSE;
5bd4f169 9236
951fd09b
AM
9237 eh = (struct ppc_link_hash_entry *) h;
9238 /* Run through the TLS GD got entries first if we're changing them
9239 to TPREL. */
b00a0a86 9240 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9241 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9242 if (gent->got.refcount > 0
9243 && (gent->tls_type & TLS_GD) != 0)
9244 {
9245 /* This was a GD entry that has been converted to TPREL. If
9246 there happens to be a TPREL entry we can use that one. */
9247 struct got_entry *ent;
9248 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9249 if (ent->got.refcount > 0
9250 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9251 && ent->addend == gent->addend
9252 && ent->owner == gent->owner)
951fd09b
AM
9253 {
9254 gent->got.refcount = 0;
9255 break;
9256 }
9257
9258 /* If not, then we'll be using our own TPREL entry. */
9259 if (gent->got.refcount != 0)
9260 gent->tls_type = TLS_TLS | TLS_TPREL;
9261 }
9262
7865406b
AM
9263 /* Remove any list entry that won't generate a word in the GOT before
9264 we call merge_got_entries. Otherwise we risk merging to empty
9265 entries. */
0b8bcf0d
AM
9266 pgent = &h->got.glist;
9267 while ((gent = *pgent) != NULL)
411e1bfb 9268 if (gent->got.refcount > 0)
7865406b
AM
9269 {
9270 if ((gent->tls_type & TLS_LD) != 0
9271 && !h->def_dynamic)
9272 {
9273 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9274 *pgent = gent->next;
9275 }
9276 else
9277 pgent = &gent->next;
9278 }
9279 else
9280 *pgent = gent->next;
9281
9282 if (!htab->do_multi_toc)
9283 merge_got_entries (&h->got.glist);
9284
9285 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9286 if (!gent->is_indirect)
411e1bfb 9287 {
46434633
AM
9288 /* Make sure this symbol is output as a dynamic symbol. */
9289 if (!ensure_undef_dynamic (info, h))
f0158f44 9290 return FALSE;
65f38f15 9291
0c8d6e5c 9292 if (!is_ppc64_elf (gent->owner))
927be08e 9293 abort ();
0ffa91dd 9294
927be08e 9295 allocate_got (h, info, gent);
411e1bfb 9296 }
65f38f15 9297
954b63d4
AM
9298 /* If no dynamic sections we can't have dynamic relocs, except for
9299 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9300 if (!htab->elf.dynamic_sections_created
9301 && h->type != STT_GNU_IFUNC)
9302 eh->dyn_relocs = NULL;
9303
529fe20e
AM
9304 /* Discard relocs on undefined symbols that must be local. */
9305 else if (h->root.type == bfd_link_hash_undefined
9306 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9307 eh->dyn_relocs = NULL;
9308
954b63d4
AM
9309 /* Also discard relocs on undefined weak syms with non-default
9310 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9311 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
9312 eh->dyn_relocs = NULL;
9313
8a2058b5 9314 if (eh->dyn_relocs != NULL)
65f38f15 9315 {
8a2058b5
AM
9316 struct elf_dyn_relocs *p, **pp;
9317
57e7d118
AM
9318 /* In the shared -Bsymbolic case, discard space allocated for
9319 dynamic pc-relative relocs against symbols which turn out to
9320 be defined in regular objects. For the normal shared case,
9321 discard space for relocs that have become local due to symbol
9322 visibility changes. */
9323
9324 if (bfd_link_pic (info))
65f38f15 9325 {
57e7d118
AM
9326 /* Relocs that use pc_count are those that appear on a call
9327 insn, or certain REL relocs (see must_be_dyn_reloc) that
9328 can be generated via assembly. We want calls to
9329 protected symbols to resolve directly to the function
9330 rather than going via the plt. If people want function
9331 pointer comparisons to work as expected then they should
9332 avoid writing weird assembly. */
9333 if (SYMBOL_CALLS_LOCAL (info, h))
9334 {
57e7d118
AM
9335 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9336 {
9337 p->count -= p->pc_count;
9338 p->pc_count = 0;
9339 if (p->count == 0)
9340 *pp = p->next;
9341 else
9342 pp = &p->next;
9343 }
9344 }
65f38f15 9345
954b63d4 9346 if (eh->dyn_relocs != NULL)
5bd4f169 9347 {
46434633
AM
9348 /* Make sure this symbol is output as a dynamic symbol. */
9349 if (!ensure_undef_dynamic (info, h))
f0158f44 9350 return FALSE;
5bd4f169 9351 }
65f38f15 9352 }
529fe20e 9353 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 9354 {
8a2058b5 9355 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9356 symbols which turn out to need copy relocs or are not
9357 dynamic. */
529fe20e
AM
9358 if (h->dynamic_adjusted
9359 && !h->def_regular
9360 && !ELF_COMMON_DEF_P (h))
f0158f44 9361 {
46434633
AM
9362 /* Make sure this symbol is output as a dynamic symbol. */
9363 if (!ensure_undef_dynamic (info, h))
f0158f44 9364 return FALSE;
dfbb6ac9 9365
f0158f44
AM
9366 if (h->dynindx == -1)
9367 eh->dyn_relocs = NULL;
9368 }
9369 else
8a2058b5 9370 eh->dyn_relocs = NULL;
57e7d118
AM
9371 }
9372
9373 /* Finally, allocate space. */
9374 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9375 {
9376 asection *sreloc = elf_section_data (p->sec)->sreloc;
9377 if (eh->elf.type == STT_GNU_IFUNC)
9378 sreloc = htab->elf.irelplt;
9379 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9380 }
65f38f15 9381 }
57e7d118 9382
2d7ad24e
AM
9383 /* We might need a PLT entry when the symbol
9384 a) is dynamic, or
9385 b) is an ifunc, or
9386 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9387 d) has plt16 relocs and we are linking statically. */
9388 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9389 || h->type == STT_GNU_IFUNC
9390 || (h->needs_plt && h->dynamic_adjusted)
9391 || (h->needs_plt
9392 && h->def_regular
9393 && !htab->elf.dynamic_sections_created
3e04d765 9394 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
9395 && (((struct ppc_link_hash_entry *) h)->tls_mask
9396 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9397 {
57e7d118
AM
9398 struct plt_entry *pent;
9399 bfd_boolean doneone = FALSE;
9400 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9401 if (pent->plt.refcount > 0)
9402 {
9403 if (!htab->elf.dynamic_sections_created
9404 || h->dynindx == -1)
9405 {
2d7ad24e
AM
9406 if (h->type == STT_GNU_IFUNC)
9407 {
9408 s = htab->elf.iplt;
9409 pent->plt.offset = s->size;
9410 s->size += PLT_ENTRY_SIZE (htab);
9411 s = htab->elf.irelplt;
9412 }
9413 else
9414 {
9415 s = htab->pltlocal;
9416 pent->plt.offset = s->size;
9417 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9418 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9419 }
57e7d118
AM
9420 }
9421 else
9422 {
9423 /* If this is the first .plt entry, make room for the special
9424 first entry. */
9425 s = htab->elf.splt;
9426 if (s->size == 0)
9427 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9428
57e7d118 9429 pent->plt.offset = s->size;
65f38f15 9430
57e7d118
AM
9431 /* Make room for this entry. */
9432 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9433
57e7d118
AM
9434 /* Make room for the .glink code. */
9435 s = htab->glink;
9436 if (s->size == 0)
9e390558 9437 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9438 if (htab->opd_abi)
9439 {
9440 /* We need bigger stubs past index 32767. */
9e390558 9441 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9442 s->size += 4;
9443 s->size += 2*4;
9444 }
9445 else
9446 s->size += 4;
65f38f15 9447
57e7d118
AM
9448 /* We also need to make an entry in the .rela.plt section. */
9449 s = htab->elf.srelplt;
9450 }
2d7ad24e
AM
9451 if (s != NULL)
9452 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
9453 doneone = TRUE;
9454 }
9455 else
9456 pent->plt.offset = (bfd_vma) -1;
9457 if (!doneone)
9458 {
9459 h->plt.plist = NULL;
9460 h->needs_plt = 0;
9461 }
65f38f15 9462 }
57e7d118 9463 else
65f38f15 9464 {
57e7d118
AM
9465 h->plt.plist = NULL;
9466 h->needs_plt = 0;
65f38f15
AM
9467 }
9468
b34976b6 9469 return TRUE;
65f38f15
AM
9470}
9471
9e390558
AM
9472#define PPC_LO(v) ((v) & 0xffff)
9473#define PPC_HI(v) (((v) >> 16) & 0xffff)
9474#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
9475#define D34(v) \
9476 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9477#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 9478
a345bc8d
AM
9479/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9480 to set up space for global entry stubs. These are put in glink,
9481 after the branch table. */
65f38f15 9482
b34976b6 9483static bfd_boolean
a345bc8d 9484size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9485{
a345bc8d
AM
9486 struct bfd_link_info *info;
9487 struct ppc_link_hash_table *htab;
9488 struct plt_entry *pent;
9e390558 9489 asection *s, *plt;
65f38f15 9490
a345bc8d
AM
9491 if (h->root.type == bfd_link_hash_indirect)
9492 return TRUE;
65f38f15 9493
a345bc8d
AM
9494 if (!h->pointer_equality_needed)
9495 return TRUE;
65f38f15 9496
a345bc8d
AM
9497 if (h->def_regular)
9498 return TRUE;
65f38f15 9499
a345bc8d
AM
9500 info = inf;
9501 htab = ppc_hash_table (info);
9502 if (htab == NULL)
9503 return FALSE;
9504
9e390558
AM
9505 s = htab->global_entry;
9506 plt = htab->elf.splt;
a345bc8d
AM
9507 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9508 if (pent->plt.offset != (bfd_vma) -1
9509 && pent->addend == 0)
9510 {
afe397ea
AM
9511 /* For ELFv2, if this symbol is not defined in a regular file
9512 and we are not generating a shared library or pie, then we
9513 need to define the symbol in the executable on a call stub.
9514 This is to avoid text relocations. */
9e390558
AM
9515 bfd_vma off, stub_align, stub_off, stub_size;
9516 unsigned int align_power;
9517
9518 stub_size = 16;
9519 stub_off = s->size;
9520 if (htab->params->plt_stub_align >= 0)
9521 align_power = htab->params->plt_stub_align;
9522 else
9523 align_power = -htab->params->plt_stub_align;
9524 /* Setting section alignment is delayed until we know it is
9525 non-empty. Otherwise the .text output section will be
9526 aligned at least to plt_stub_align even when no global
9527 entry stubs are needed. */
9528 if (s->alignment_power < align_power)
9529 s->alignment_power = align_power;
9530 stub_align = (bfd_vma) 1 << align_power;
9531 if (htab->params->plt_stub_align >= 0
9532 || ((((stub_off + stub_size - 1) & -stub_align)
9533 - (stub_off & -stub_align))
9534 > ((stub_size - 1) & -stub_align)))
9535 stub_off = (stub_off + stub_align - 1) & -stub_align;
9536 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9537 off -= stub_off + s->output_offset + s->output_section->vma;
9538 /* Note that for --plt-stub-align negative we have a possible
9539 dependency between stub offset and size. Break that
9540 dependency by assuming the max stub size when calculating
9541 the stub offset. */
9542 if (PPC_HA (off) == 0)
9543 stub_size -= 4;
8a2058b5 9544 h->root.type = bfd_link_hash_defined;
afe397ea 9545 h->root.u.def.section = s;
9e390558
AM
9546 h->root.u.def.value = stub_off;
9547 s->size = stub_off + stub_size;
a345bc8d
AM
9548 break;
9549 }
9550 return TRUE;
9551}
9552
9553/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9554 read-only sections. */
9555
9556static bfd_boolean
98bbb1b8 9557maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9558{
98bbb1b8
AM
9559 asection *sec;
9560
a345bc8d
AM
9561 if (h->root.type == bfd_link_hash_indirect)
9562 return TRUE;
9563
98bbb1b8
AM
9564 sec = readonly_dynrelocs (h);
9565 if (sec != NULL)
a345bc8d 9566 {
98bbb1b8
AM
9567 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9568
9569 info->flags |= DF_TEXTREL;
2cdcc330
AM
9570 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9571 " in read-only section `%pA'\n"),
9572 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9573
9574 /* Not an error, just cut short the traversal. */
9575 return FALSE;
65f38f15 9576 }
b34976b6 9577 return TRUE;
65f38f15
AM
9578}
9579
9580/* Set the sizes of the dynamic sections. */
9581
b34976b6 9582static bfd_boolean
ee67d69a 9583ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9584 struct bfd_link_info *info)
65f38f15
AM
9585{
9586 struct ppc_link_hash_table *htab;
9587 bfd *dynobj;
9588 asection *s;
b34976b6 9589 bfd_boolean relocs;
65f38f15 9590 bfd *ibfd;
7865406b 9591 struct got_entry *first_tlsld;
65f38f15
AM
9592
9593 htab = ppc_hash_table (info);
4dfe6ac6
NC
9594 if (htab == NULL)
9595 return FALSE;
9596
65f38f15
AM
9597 dynobj = htab->elf.dynobj;
9598 if (dynobj == NULL)
9599 abort ();
9600
9601 if (htab->elf.dynamic_sections_created)
9602 {
9603 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9604 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9605 {
3d4d4302 9606 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9607 if (s == NULL)
9608 abort ();
eea6121a 9609 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9610 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9611 }
9612 }
9613
9614 /* Set up .got offsets for local syms, and space for local dynamic
9615 relocs. */
c72f2fb2 9616 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9617 {
411e1bfb
AM
9618 struct got_entry **lgot_ents;
9619 struct got_entry **end_lgot_ents;
e054468f
AM
9620 struct plt_entry **local_plt;
9621 struct plt_entry **end_local_plt;
f961d9dd 9622 unsigned char *lgot_masks;
65f38f15
AM
9623 bfd_size_type locsymcount;
9624 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9625
0c8d6e5c 9626 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9627 continue;
9628
9629 for (s = ibfd->sections; s != NULL; s = s->next)
9630 {
19e08130 9631 struct ppc_dyn_relocs *p;
65f38f15 9632
6edfbbad 9633 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9634 {
ec338859
AM
9635 if (!bfd_is_abs_section (p->sec)
9636 && bfd_is_abs_section (p->sec->output_section))
9637 {
9638 /* Input section has been discarded, either because
9639 it is a copy of a linkonce section or due to
9640 linker script /DISCARD/, so we'll be discarding
9641 the relocs too. */
9642 }
248866a8 9643 else if (p->count != 0)
ec338859 9644 {
19e08130
AM
9645 asection *srel = elf_section_data (p->sec)->sreloc;
9646 if (p->ifunc)
33e44f2e 9647 srel = htab->elf.irelplt;
eea6121a 9648 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9649 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9650 info->flags |= DF_TEXTREL;
ec338859 9651 }
65f38f15
AM
9652 }
9653 }
9654
411e1bfb
AM
9655 lgot_ents = elf_local_got_ents (ibfd);
9656 if (!lgot_ents)
65f38f15
AM
9657 continue;
9658
0ffa91dd 9659 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9660 locsymcount = symtab_hdr->sh_info;
411e1bfb 9661 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9662 local_plt = (struct plt_entry **) end_lgot_ents;
9663 end_local_plt = local_plt + locsymcount;
f961d9dd 9664 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9665 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9666 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9667 {
0b8bcf0d 9668 struct got_entry **pent, *ent;
411e1bfb 9669
0b8bcf0d
AM
9670 pent = lgot_ents;
9671 while ((ent = *pent) != NULL)
411e1bfb
AM
9672 if (ent->got.refcount > 0)
9673 {
e7b938ca 9674 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9675 {
927be08e 9676 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9677 *pent = ent->next;
411e1bfb
AM
9678 }
9679 else
9680 {
19e08130
AM
9681 unsigned int ent_size = 8;
9682 unsigned int rel_size = sizeof (Elf64_External_Rela);
9683
eea6121a 9684 ent->got.offset = s->size;
e7b938ca 9685 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9686 {
19e08130
AM
9687 ent_size *= 2;
9688 rel_size *= 2;
9689 }
9690 s->size += ent_size;
37da22e5 9691 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 9692 {
33e44f2e 9693 htab->elf.irelplt->size += rel_size;
19e08130
AM
9694 htab->got_reli_size += rel_size;
9695 }
f15d0b54
AM
9696 else if (bfd_link_pic (info)
9697 && !((ent->tls_type & TLS_TPREL) != 0
9698 && bfd_link_executable (info)))
19e08130
AM
9699 {
9700 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9701 srel->size += rel_size;
927be08e 9702 }
0b8bcf0d 9703 pent = &ent->next;
411e1bfb
AM
9704 }
9705 }
9706 else
0b8bcf0d 9707 *pent = ent->next;
65f38f15 9708 }
e054468f 9709
2d7ad24e
AM
9710 /* Allocate space for plt calls to local syms. */
9711 lgot_masks = (unsigned char *) end_local_plt;
9712 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
9713 {
9714 struct plt_entry *ent;
9715
9716 for (ent = *local_plt; ent != NULL; ent = ent->next)
9717 if (ent->plt.refcount > 0)
9718 {
2d7ad24e
AM
9719 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9720 {
9721 s = htab->elf.iplt;
9722 ent->plt.offset = s->size;
9723 s->size += PLT_ENTRY_SIZE (htab);
9724 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9725 }
3e04d765
AM
9726 else if (htab->can_convert_all_inline_plt
9727 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
9728 ent->plt.offset = (bfd_vma) -1;
9729 else
9730 {
9731 s = htab->pltlocal;
9732 ent->plt.offset = s->size;
9733 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9734 if (bfd_link_pic (info))
9735 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9736 }
e054468f
AM
9737 }
9738 else
9739 ent->plt.offset = (bfd_vma) -1;
9740 }
65f38f15
AM
9741 }
9742
9743 /* Allocate global sym .plt and .got entries, and space for global
9744 sym dynamic relocs. */
4ce794b7 9745 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 9746
0e1862bb 9747 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 9748 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9749
7865406b 9750 first_tlsld = NULL;
c72f2fb2 9751 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9752 {
7865406b
AM
9753 struct got_entry *ent;
9754
0c8d6e5c 9755 if (!is_ppc64_elf (ibfd))
102890f0
AM
9756 continue;
9757
7865406b
AM
9758 ent = ppc64_tlsld_got (ibfd);
9759 if (ent->got.refcount > 0)
102890f0 9760 {
7865406b 9761 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9762 {
7865406b
AM
9763 ent->is_indirect = TRUE;
9764 ent->got.ent = first_tlsld;
9765 }
9766 else
9767 {
9768 if (first_tlsld == NULL)
9769 first_tlsld = ent;
9770 s = ppc64_elf_tdata (ibfd)->got;
9771 ent->got.offset = s->size;
9772 ent->owner = ibfd;
9773 s->size += 16;
0e1862bb 9774 if (bfd_link_pic (info))
7865406b
AM
9775 {
9776 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9777 srel->size += sizeof (Elf64_External_Rela);
9778 }
102890f0
AM
9779 }
9780 }
9781 else
7865406b 9782 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9783 }
9784
65f38f15
AM
9785 /* We now have determined the sizes of the various dynamic sections.
9786 Allocate memory for them. */
b34976b6 9787 relocs = FALSE;
65f38f15
AM
9788 for (s = dynobj->sections; s != NULL; s = s->next)
9789 {
9790 if ((s->flags & SEC_LINKER_CREATED) == 0)
9791 continue;
9792
4ce794b7 9793 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9794 /* These haven't been allocated yet; don't strip. */
9795 continue;
33e44f2e
AM
9796 else if (s == htab->elf.sgot
9797 || s == htab->elf.splt
9798 || s == htab->elf.iplt
2d7ad24e 9799 || s == htab->pltlocal
c456f082 9800 || s == htab->glink
9e390558 9801 || s == htab->global_entry
5474d94f
AM
9802 || s == htab->elf.sdynbss
9803 || s == htab->elf.sdynrelro)
65f38f15
AM
9804 {
9805 /* Strip this section if we don't need it; see the
9806 comment below. */
5bd4f169 9807 }
58d180e8
AM
9808 else if (s == htab->glink_eh_frame)
9809 {
9810 if (!bfd_is_abs_section (s->output_section))
9811 /* Not sized yet. */
9812 continue;
9813 }
70cc837d 9814 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9815 {
c456f082 9816 if (s->size != 0)
5bd4f169 9817 {
33e44f2e 9818 if (s != htab->elf.srelplt)
b34976b6 9819 relocs = TRUE;
5bd4f169
AM
9820
9821 /* We use the reloc_count field as a counter if we need
9822 to copy relocs into the output file. */
9823 s->reloc_count = 0;
9824 }
9825 }
65f38f15 9826 else
5bd4f169
AM
9827 {
9828 /* It's not one of our sections, so don't allocate space. */
9829 continue;
9830 }
9831
eea6121a 9832 if (s->size == 0)
5bd4f169 9833 {
c456f082
AM
9834 /* If we don't need this section, strip it from the
9835 output file. This is mostly to handle .rela.bss and
9836 .rela.plt. We must create both sections in
9837 create_dynamic_sections, because they must be created
9838 before the linker maps input sections to output
9839 sections. The linker does that before
9840 adjust_dynamic_symbol is called, and it is that
9841 function which decides whether anything needs to go
9842 into these sections. */
8423293d 9843 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9844 continue;
9845 }
9846
06bcf541
AM
9847 if (bfd_is_abs_section (s->output_section))
9848 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9849 s->name);
9850
c456f082 9851 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9852 continue;
9853
65f38f15
AM
9854 /* Allocate memory for the section contents. We use bfd_zalloc
9855 here in case unused entries are not reclaimed before the
9856 section's contents are written out. This should not happen,
411e1bfb
AM
9857 but this way if it does we get a R_PPC64_NONE reloc in .rela
9858 sections instead of garbage.
9859 We also rely on the section contents being zero when writing
5474d94f 9860 the GOT and .dynrelro. */
eea6121a 9861 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9862 if (s->contents == NULL)
b34976b6 9863 return FALSE;
5bd4f169
AM
9864 }
9865
c72f2fb2 9866 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 9867 {
0c8d6e5c 9868 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9869 continue;
9870
e717da7e 9871 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 9872 if (s != NULL && s != htab->elf.sgot)
e717da7e 9873 {
eea6121a 9874 if (s->size == 0)
8423293d 9875 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9876 else
9877 {
eea6121a 9878 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9879 if (s->contents == NULL)
9880 return FALSE;
9881 }
9882 }
9883 s = ppc64_elf_tdata (ibfd)->relgot;
9884 if (s != NULL)
9885 {
eea6121a 9886 if (s->size == 0)
8423293d 9887 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9888 else
9889 {
eea6121a 9890 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9891 if (s->contents == NULL)
9892 return FALSE;
9893 relocs = TRUE;
9894 s->reloc_count = 0;
9895 }
9896 }
9897 }
9898
e86ce104 9899 if (htab->elf.dynamic_sections_created)
5bd4f169 9900 {
e8910a83
AM
9901 bfd_boolean tls_opt;
9902
5bd4f169
AM
9903 /* Add some entries to the .dynamic section. We fill in the
9904 values later, in ppc64_elf_finish_dynamic_sections, but we
9905 must add the entries now so that we get the correct size for
9906 the .dynamic section. The DT_DEBUG entry is filled in by the
9907 dynamic linker and used by the debugger. */
dc810e39 9908#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9909 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9910
0e1862bb 9911 if (bfd_link_executable (info))
5bd4f169 9912 {
dc810e39 9913 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9914 return FALSE;
5bd4f169
AM
9915 }
9916
33e44f2e 9917 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 9918 {
dc810e39
AM
9919 if (!add_dynamic_entry (DT_PLTGOT, 0)
9920 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9921 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9922 || !add_dynamic_entry (DT_JMPREL, 0)
9923 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9924 return FALSE;
5bd4f169
AM
9925 }
9926
ee67d69a 9927 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
9928 {
9929 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9930 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9931 return FALSE;
19397422
AM
9932 }
9933
7c9cf415 9934 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
9935 && htab->tls_get_addr_fd != NULL
9936 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9937 if (tls_opt || !htab->opd_abi)
9938 {
9939 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9940 return FALSE;
9941 }
a7f2871e 9942
5bd4f169
AM
9943 if (relocs)
9944 {
dc810e39
AM
9945 if (!add_dynamic_entry (DT_RELA, 0)
9946 || !add_dynamic_entry (DT_RELASZ, 0)
9947 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9948 return FALSE;
5bd4f169 9949
65f38f15
AM
9950 /* If any dynamic relocs apply to a read-only section,
9951 then we need a DT_TEXTREL entry. */
248866a8 9952 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 9953 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 9954
65f38f15 9955 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9956 {
65f38f15 9957 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9958 return FALSE;
5bd4f169 9959 }
5bd4f169 9960 }
5bd4f169 9961 }
65f38f15 9962#undef add_dynamic_entry
5bd4f169 9963
b34976b6 9964 return TRUE;
5bd4f169
AM
9965}
9966
a345bc8d
AM
9967/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9968
9969static bfd_boolean
9970ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9971{
9972 if (h->plt.plist != NULL
9973 && !h->def_regular
9974 && !h->pointer_equality_needed)
9975 return FALSE;
9976
9977 return _bfd_elf_hash_symbol (h);
9978}
9979
721956f4 9980/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9981
4ce794b7
AM
9982static inline enum ppc_stub_type
9983ppc_type_of_stub (asection *input_sec,
9984 const Elf_Internal_Rela *rel,
9985 struct ppc_link_hash_entry **hash,
e054468f 9986 struct plt_entry **plt_ent,
6911b7dc
AM
9987 bfd_vma destination,
9988 unsigned long local_off)
5bd4f169 9989{
721956f4
AM
9990 struct ppc_link_hash_entry *h = *hash;
9991 bfd_vma location;
9992 bfd_vma branch_offset;
9993 bfd_vma max_branch_offset;
4ce794b7 9994 enum elf_ppc64_reloc_type r_type;
5bd4f169 9995
721956f4
AM
9996 if (h != NULL)
9997 {
e054468f 9998 struct plt_entry *ent;
7fe2b9a6 9999 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10000 if (h->oh != NULL
10001 && h->oh->is_func_descriptor)
7b8f6675
AM
10002 {
10003 fdh = ppc_follow_link (h->oh);
10004 *hash = fdh;
10005 }
8387904d 10006
e054468f
AM
10007 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10008 if (ent->addend == rel->r_addend
10009 && ent->plt.offset != (bfd_vma) -1)
10010 {
e054468f
AM
10011 *plt_ent = ent;
10012 return ppc_stub_plt_call;
10013 }
5bd4f169 10014
7fe2b9a6
AM
10015 /* Here, we know we don't have a plt entry. If we don't have a
10016 either a defined function descriptor or a defined entry symbol
10017 in a regular object file, then it is pointless trying to make
10018 any other type of stub. */
854b41e7
AM
10019 if (!is_static_defined (&fdh->elf)
10020 && !is_static_defined (&h->elf))
721956f4 10021 return ppc_stub_none;
5d1634d7 10022 }
e054468f
AM
10023 else if (elf_local_got_ents (input_sec->owner) != NULL)
10024 {
10025 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10026 struct plt_entry **local_plt = (struct plt_entry **)
10027 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10028 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10029
10030 if (local_plt[r_symndx] != NULL)
10031 {
10032 struct plt_entry *ent;
10033
10034 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10035 if (ent->addend == rel->r_addend
10036 && ent->plt.offset != (bfd_vma) -1)
10037 {
10038 *plt_ent = ent;
10039 return ppc_stub_plt_call;
10040 }
10041 }
10042 }
5d1634d7 10043
721956f4
AM
10044 /* Determine where the call point is. */
10045 location = (input_sec->output_offset
10046 + input_sec->output_section->vma
10047 + rel->r_offset);
5d1634d7 10048
721956f4
AM
10049 branch_offset = destination - location;
10050 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10051
721956f4
AM
10052 /* Determine if a long branch stub is needed. */
10053 max_branch_offset = 1 << 25;
23cedd1d
AM
10054 if (r_type == R_PPC64_REL14
10055 || r_type == R_PPC64_REL14_BRTAKEN
10056 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10057 max_branch_offset = 1 << 15;
5d1634d7 10058
6911b7dc 10059 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10060 /* We need a stub. Figure out whether a long_branch or plt_branch
10061 is needed later. */
10062 return ppc_stub_long_branch;
5d1634d7 10063
721956f4 10064 return ppc_stub_none;
5d1634d7
AM
10065}
10066
f891966f
AM
10067/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10068 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10069 . mflr %r12
10070 . bcl 20,31,1f
10071 .1: mflr %r11
10072 . mtlr %r12
05d0e962 10073 . lis %r12,xxx-1b@highest
f891966f 10074 . ori %r12,%r12,xxx-1b@higher
05d0e962 10075 . sldi %r12,%r12,32
f891966f 10076 . oris %r12,%r12,xxx-1b@high
05d0e962 10077 . ori %r12,%r12,xxx-1b@l
f891966f 10078 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10079
10080static bfd_byte *
10081build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10082{
f891966f
AM
10083 bfd_put_32 (abfd, MFLR_R12, p);
10084 p += 4;
10085 bfd_put_32 (abfd, BCL_20_31, p);
10086 p += 4;
10087 bfd_put_32 (abfd, MFLR_R11, p);
10088 p += 4;
10089 bfd_put_32 (abfd, MTLR_R12, p);
10090 p += 4;
05d0e962
AM
10091 if (off + 0x8000 < 0x10000)
10092 {
10093 if (load)
10094 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10095 else
10096 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10097 p += 4;
10098 }
10099 else if (off + 0x80008000ULL < 0x100000000ULL)
10100 {
10101 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10102 p += 4;
10103 if (load)
10104 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10105 else
10106 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10107 p += 4;
10108 }
10109 else
10110 {
10111 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10112 {
10113 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10114 p += 4;
10115 }
10116 else
10117 {
10118 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10119 p += 4;
10120 if (((off >> 32) & 0xffff) != 0)
10121 {
10122 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10123 p += 4;
10124 }
10125 }
10126 if (((off >> 32) & 0xffffffffULL) != 0)
10127 {
10128 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10129 p += 4;
10130 }
10131 if (PPC_HI (off) != 0)
10132 {
10133 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10134 p += 4;
10135 }
10136 if (PPC_LO (off) != 0)
10137 {
10138 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10139 p += 4;
10140 }
10141 if (load)
10142 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10143 else
10144 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10145 p += 4;
10146 }
10147 return p;
10148}
10149
10150static unsigned int
10151size_offset (bfd_vma off)
10152{
10153 unsigned int size;
10154 if (off + 0x8000 < 0x10000)
10155 size = 4;
10156 else if (off + 0x80008000ULL < 0x100000000ULL)
10157 size = 8;
10158 else
10159 {
10160 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10161 size = 4;
10162 else
10163 {
10164 size = 4;
10165 if (((off >> 32) & 0xffff) != 0)
10166 size += 4;
10167 }
10168 if (((off >> 32) & 0xffffffffULL) != 0)
10169 size += 4;
10170 if (PPC_HI (off) != 0)
10171 size += 4;
10172 if (PPC_LO (off) != 0)
10173 size += 4;
10174 size += 4;
10175 }
f891966f 10176 return size + 16;
05d0e962
AM
10177}
10178
3d58e1fc
AM
10179static unsigned int
10180num_relocs_for_offset (bfd_vma off)
10181{
10182 unsigned int num_rel;
10183 if (off + 0x8000 < 0x10000)
10184 num_rel = 1;
10185 else if (off + 0x80008000ULL < 0x100000000ULL)
10186 num_rel = 2;
10187 else
10188 {
10189 num_rel = 1;
10190 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10191 && ((off >> 32) & 0xffff) != 0)
10192 num_rel += 1;
10193 if (PPC_HI (off) != 0)
10194 num_rel += 1;
10195 if (PPC_LO (off) != 0)
10196 num_rel += 1;
10197 }
10198 return num_rel;
10199}
10200
10201static Elf_Internal_Rela *
10202emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10203 bfd_vma roff, bfd_vma targ, bfd_vma off)
10204{
10205 bfd_vma relative_targ = targ - (roff - 8);
10206 if (bfd_big_endian (info->output_bfd))
10207 roff += 2;
10208 r->r_offset = roff;
10209 r->r_addend = relative_targ + roff;
10210 if (off + 0x8000 < 0x10000)
10211 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10212 else if (off + 0x80008000ULL < 0x100000000ULL)
10213 {
10214 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10215 ++r;
10216 roff += 4;
10217 r->r_offset = roff;
10218 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10219 r->r_addend = relative_targ + roff;
10220 }
10221 else
10222 {
10223 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10224 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10225 else
10226 {
10227 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10228 if (((off >> 32) & 0xffff) != 0)
10229 {
10230 ++r;
10231 roff += 4;
10232 r->r_offset = roff;
10233 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10234 r->r_addend = relative_targ + roff;
10235 }
10236 }
10237 if (((off >> 32) & 0xffffffffULL) != 0)
10238 roff += 4;
10239 if (PPC_HI (off) != 0)
10240 {
10241 ++r;
10242 roff += 4;
10243 r->r_offset = roff;
10244 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10245 r->r_addend = relative_targ + roff;
10246 }
10247 if (PPC_LO (off) != 0)
10248 {
10249 ++r;
10250 roff += 4;
10251 r->r_offset = roff;
10252 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10253 r->r_addend = relative_targ + roff;
10254 }
10255 }
10256 return r;
10257}
10258
04bdff6a
AM
10259static bfd_byte *
10260build_powerxx_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10261 bfd_boolean load)
10262{
10263 uint64_t insn;
10264 if (off - odd + (1ULL << 33) < 1ULL << 34)
10265 {
10266 off -= odd;
10267 if (odd)
10268 {
10269 bfd_put_32 (abfd, NOP, p);
10270 p += 4;
10271 }
10272 if (load)
10273 insn = PLD_R12_PC;
10274 else
10275 insn = PADDI_R12_PC;
10276 insn |= D34 (off);
10277 bfd_put_32 (abfd, insn >> 32, p);
10278 p += 4;
10279 bfd_put_32 (abfd, insn, p);
10280 }
10281 /* The minimum value for paddi is -0x200000000. The minimum value
10282 for li is -0x8000, which when shifted by 34 and added gives a
10283 minimum value of -0x2000200000000. The maximum value is
10284 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10285 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10286 {
10287 off -= 8 - odd;
10288 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10289 p += 4;
10290 if (!odd)
10291 {
10292 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10293 p += 4;
10294 }
10295 insn = PADDI_R12_PC | D34 (off);
10296 bfd_put_32 (abfd, insn >> 32, p);
10297 p += 4;
10298 bfd_put_32 (abfd, insn, p);
10299 p += 4;
10300 if (odd)
10301 {
10302 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10303 p += 4;
10304 }
10305 if (load)
10306 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10307 else
10308 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10309 }
10310 else
10311 {
10312 off -= odd + 8;
10313 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10314 p += 4;
10315 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10316 p += 4;
10317 if (odd)
10318 {
10319 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10320 p += 4;
10321 }
10322 insn = PADDI_R12_PC | D34 (off);
10323 bfd_put_32 (abfd, insn >> 32, p);
10324 p += 4;
10325 bfd_put_32 (abfd, insn, p);
10326 p += 4;
10327 if (!odd)
10328 {
10329 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10330 p += 4;
10331 }
10332 if (load)
10333 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10334 else
10335 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10336 }
10337 p += 4;
10338 return p;
10339}
10340
10341static unsigned int
10342size_powerxx_offset (bfd_vma off, int odd)
10343{
10344 if (off - odd + (1ULL << 33) < 1ULL << 34)
10345 return odd + 8;
10346 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10347 return 20;
10348 else
10349 return 24;
10350}
10351
10352static unsigned int
10353num_relocs_for_powerxx_offset (bfd_vma off, int odd)
10354{
10355 if (off - odd + (1ULL << 33) < 1ULL << 34)
10356 return 1;
10357 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10358 return 2;
10359 else
10360 return 3;
10361}
10362
10363static Elf_Internal_Rela *
10364emit_relocs_for_powerxx_offset (struct bfd_link_info *info,
10365 Elf_Internal_Rela *r, bfd_vma roff,
10366 bfd_vma targ, bfd_vma off, int odd)
10367{
10368 if (off - odd + (1ULL << 33) < 1ULL << 34)
10369 roff += odd;
10370 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10371 {
10372 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10373 r->r_offset = roff + d_offset;
10374 r->r_addend = targ + 8 - odd - d_offset;
10375 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10376 ++r;
10377 roff += 8 - odd;
10378 }
10379 else
10380 {
10381 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10382 r->r_offset = roff + d_offset;
10383 r->r_addend = targ + 8 + odd - d_offset;
10384 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10385 ++r;
10386 roff += 4;
10387 r->r_offset = roff + d_offset;
10388 r->r_addend = targ + 4 + odd - d_offset;
10389 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10390 ++r;
10391 roff += 4 + odd;
10392 }
10393 r->r_offset = roff;
10394 r->r_addend = targ;
10395 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10396 return r;
10397}
10398
df136d64
AM
10399/* Emit .eh_frame opcode to advance pc by DELTA. */
10400
10401static bfd_byte *
10402eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10403{
10404 delta /= 4;
10405 if (delta < 64)
10406 *eh++ = DW_CFA_advance_loc + delta;
10407 else if (delta < 256)
10408 {
10409 *eh++ = DW_CFA_advance_loc1;
10410 *eh++ = delta;
10411 }
10412 else if (delta < 65536)
10413 {
10414 *eh++ = DW_CFA_advance_loc2;
10415 bfd_put_16 (abfd, delta, eh);
10416 eh += 2;
10417 }
10418 else
10419 {
10420 *eh++ = DW_CFA_advance_loc4;
10421 bfd_put_32 (abfd, delta, eh);
10422 eh += 4;
10423 }
10424 return eh;
10425}
10426
10427/* Size of required .eh_frame opcode to advance pc by DELTA. */
10428
10429static unsigned int
10430eh_advance_size (unsigned int delta)
10431{
10432 if (delta < 64 * 4)
10433 /* DW_CFA_advance_loc+[1..63]. */
10434 return 1;
10435 if (delta < 256 * 4)
10436 /* DW_CFA_advance_loc1, byte. */
10437 return 2;
10438 if (delta < 65536 * 4)
10439 /* DW_CFA_advance_loc2, 2 bytes. */
10440 return 3;
10441 /* DW_CFA_advance_loc4, 4 bytes. */
10442 return 5;
10443}
10444
794e51c0
AM
10445/* With power7 weakly ordered memory model, it is possible for ld.so
10446 to update a plt entry in one thread and have another thread see a
10447 stale zero toc entry. To avoid this we need some sort of acquire
10448 barrier in the call stub. One solution is to make the load of the
10449 toc word seem to appear to depend on the load of the function entry
10450 word. Another solution is to test for r2 being zero, and branch to
10451 the appropriate glink entry if so.
10452
10453 . fake dep barrier compare
71a39c98
AM
10454 . ld 12,xxx(2) ld 12,xxx(2)
10455 . mtctr 12 mtctr 12
10456 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10457 . add 2,2,11 cmpldi 2,0
10458 . ld 2,xxx+8(2) bnectr+
10459 . bctr b <glink_entry>
10460
10461 The solution involving the compare turns out to be faster, so
10462 that's what we use unless the branch won't reach. */
10463
10464#define ALWAYS_USE_FAKE_DEP 0
10465#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10466
794e51c0
AM
10467static inline unsigned int
10468plt_stub_size (struct ppc_link_hash_table *htab,
10469 struct ppc_stub_hash_entry *stub_entry,
10470 bfd_vma off)
10471{
05d0e962 10472 unsigned size;
b9e5796b 10473
05d0e962
AM
10474 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10475 {
04bdff6a
AM
10476 if (htab->powerxx_stubs)
10477 {
10478 bfd_vma start = (stub_entry->stub_offset
10479 + stub_entry->group->stub_sec->output_offset
10480 + stub_entry->group->stub_sec->output_section->vma);
10481 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10482 start += 4;
10483 size = 8 + size_powerxx_offset (off, start & 4);
10484 }
10485 else
10486 size = 8 + size_offset (off - 8);
05d0e962
AM
10487 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10488 size += 4;
10489 return size;
10490 }
10491
10492 size = 12;
b9e5796b
AM
10493 if (ALWAYS_EMIT_R2SAVE
10494 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10495 size += 4;
10496 if (PPC_HA (off) != 0)
794e51c0 10497 size += 4;
b9e5796b
AM
10498 if (htab->opd_abi)
10499 {
10500 size += 4;
e7d1c40c 10501 if (htab->params->plt_static_chain)
b9e5796b 10502 size += 4;
bd4d2eaa
AM
10503 if (htab->params->plt_thread_safe
10504 && htab->elf.dynamic_sections_created
10505 && stub_entry->h != NULL
10506 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10507 size += 8;
e7d1c40c 10508 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10509 size += 4;
10510 }
794e51c0
AM
10511 if (stub_entry->h != NULL
10512 && (stub_entry->h == htab->tls_get_addr_fd
10513 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10514 && htab->params->tls_get_addr_opt)
f378ab09
AM
10515 {
10516 size += 7 * 4;
e81b4c93 10517 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 10518 size += 6 * 4;
f378ab09 10519 }
794e51c0
AM
10520 return size;
10521}
10522
2420fff6
AM
10523/* Depending on the sign of plt_stub_align:
10524 If positive, return the padding to align to a 2**plt_stub_align
10525 boundary.
10526 If negative, if this stub would cross fewer 2**plt_stub_align
10527 boundaries if we align, then return the padding needed to do so. */
10528
794e51c0
AM
10529static inline unsigned int
10530plt_stub_pad (struct ppc_link_hash_table *htab,
10531 struct ppc_stub_hash_entry *stub_entry,
10532 bfd_vma plt_off)
10533{
2420fff6 10534 int stub_align;
1aa42141 10535 unsigned stub_size;
6f20ed8a 10536 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10537
2420fff6
AM
10538 if (htab->params->plt_stub_align >= 0)
10539 {
10540 stub_align = 1 << htab->params->plt_stub_align;
10541 if ((stub_off & (stub_align - 1)) != 0)
10542 return stub_align - (stub_off & (stub_align - 1));
10543 return 0;
10544 }
10545
10546 stub_align = 1 << -htab->params->plt_stub_align;
1aa42141 10547 stub_size = plt_stub_size (htab, stub_entry, plt_off);
794e51c0 10548 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10549 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10550 return stub_align - (stub_off & (stub_align - 1));
10551 return 0;
10552}
10553
10554/* Build a .plt call stub. */
10555
10556static inline bfd_byte *
10557build_plt_stub (struct ppc_link_hash_table *htab,
10558 struct ppc_stub_hash_entry *stub_entry,
10559 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10560{
e7d1c40c 10561 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10562 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10563 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10564 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10565 && htab->elf.dynamic_sections_created
10566 && stub_entry->h != NULL
10567 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10568 bfd_boolean use_fake_dep = plt_thread_safe;
10569 bfd_vma cmp_branch_off = 0;
10570
10571 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10572 && plt_load_toc
794e51c0 10573 && plt_thread_safe
bd4d2eaa
AM
10574 && !((stub_entry->h == htab->tls_get_addr_fd
10575 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10576 && htab->params->tls_get_addr_opt))
794e51c0
AM
10577 {
10578 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10579 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10580 / PLT_ENTRY_SIZE (htab));
9e390558 10581 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10582 bfd_vma to, from;
10583
68d62958
AM
10584 if (pltindex > 32768)
10585 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10586 to = (glinkoff
10587 + htab->glink->output_offset
10588 + htab->glink->output_section->vma);
6f20ed8a 10589 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10590 + 4 * (ALWAYS_EMIT_R2SAVE
10591 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10592 + 4 * (PPC_HA (offset) != 0)
10593 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10594 != PPC_HA (offset))
10595 + 4 * (plt_static_chain != 0)
10596 + 20
6f20ed8a
AM
10597 + stub_entry->group->stub_sec->output_offset
10598 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10599 cmp_branch_off = to - from;
10600 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10601 }
10602
ac2df442
AM
10603 if (PPC_HA (offset) != 0)
10604 {
176a0d42
AM
10605 if (r != NULL)
10606 {
794e51c0
AM
10607 if (ALWAYS_EMIT_R2SAVE
10608 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10609 r[0].r_offset += 4;
176a0d42 10610 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10611 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10612 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10613 r[1].r_addend = r[0].r_addend;
b9e5796b 10614 if (plt_load_toc)
176a0d42 10615 {
b9e5796b 10616 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10617 {
b9e5796b
AM
10618 r[2].r_offset = r[1].r_offset + 4;
10619 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10620 r[2].r_addend = r[0].r_addend;
10621 }
10622 else
10623 {
10624 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10625 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10626 r[2].r_addend = r[0].r_addend + 8;
10627 if (plt_static_chain)
10628 {
10629 r[3].r_offset = r[2].r_offset + 4;
10630 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10631 r[3].r_addend = r[0].r_addend + 16;
10632 }
c7131b65 10633 }
176a0d42
AM
10634 }
10635 }
794e51c0
AM
10636 if (ALWAYS_EMIT_R2SAVE
10637 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10638 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10639 if (plt_load_toc)
10640 {
10641 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10642 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10643 }
10644 else
10645 {
10646 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10647 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10648 }
b9e5796b
AM
10649 if (plt_load_toc
10650 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10651 {
71a39c98 10652 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10653 offset = 0;
10654 }
71a39c98 10655 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10656 if (plt_load_toc)
794e51c0 10657 {
b9e5796b
AM
10658 if (use_fake_dep)
10659 {
10660 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10661 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10662 }
10663 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10664 if (plt_static_chain)
10665 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10666 }
ac2df442
AM
10667 }
10668 else
10669 {
176a0d42
AM
10670 if (r != NULL)
10671 {
794e51c0
AM
10672 if (ALWAYS_EMIT_R2SAVE
10673 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10674 r[0].r_offset += 4;
176a0d42 10675 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10676 if (plt_load_toc)
176a0d42 10677 {
b9e5796b 10678 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10679 {
b9e5796b
AM
10680 r[1].r_offset = r[0].r_offset + 4;
10681 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10682 r[1].r_addend = r[0].r_addend;
10683 }
10684 else
10685 {
10686 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10687 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10688 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10689 if (plt_static_chain)
10690 {
10691 r[2].r_offset = r[1].r_offset + 4;
10692 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10693 r[2].r_addend = r[0].r_addend + 8;
10694 }
c7131b65 10695 }
176a0d42
AM
10696 }
10697 }
794e51c0
AM
10698 if (ALWAYS_EMIT_R2SAVE
10699 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10700 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10701 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10702 if (plt_load_toc
10703 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10704 {
10705 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10706 offset = 0;
10707 }
71a39c98 10708 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10709 if (plt_load_toc)
794e51c0 10710 {
b9e5796b
AM
10711 if (use_fake_dep)
10712 {
10713 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10714 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10715 }
10716 if (plt_static_chain)
10717 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10718 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10719 }
ac2df442 10720 }
b9e5796b 10721 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10722 {
10723 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10724 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10725 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10726 }
10727 else
407aa07c 10728 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10729 return p;
10730}
10731
a7f2871e
AM
10732/* Build a special .plt call stub for __tls_get_addr. */
10733
10734#define LD_R11_0R3 0xe9630000
10735#define LD_R12_0R3 0xe9830000
10736#define MR_R0_R3 0x7c601b78
10737#define CMPDI_R11_0 0x2c2b0000
10738#define ADD_R3_R12_R13 0x7c6c6a14
10739#define BEQLR 0x4d820020
10740#define MR_R3_R0 0x7c030378
a7f2871e
AM
10741#define STD_R11_0R1 0xf9610000
10742#define BCTRL 0x4e800421
10743#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10744#define MTLR_R11 0x7d6803a6
10745
10746static inline bfd_byte *
794e51c0
AM
10747build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10748 struct ppc_stub_hash_entry *stub_entry,
10749 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10750{
e7d1c40c 10751 bfd *obfd = htab->params->stub_bfd;
df136d64 10752 bfd_byte *loc = p;
794e51c0 10753
a7f2871e
AM
10754 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10755 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10756 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10757 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10758 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10759 bfd_put_32 (obfd, BEQLR, p), p += 4;
10760 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10761 if (r != NULL)
10762 r[0].r_offset += 7 * 4;
e81b4c93 10763 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
f378ab09
AM
10764 return build_plt_stub (htab, stub_entry, p, offset, r);
10765
a7f2871e 10766 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10767 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10768
10769 if (r != NULL)
f378ab09 10770 r[0].r_offset += 2 * 4;
794e51c0 10771 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 10772 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10773
a078d95a 10774 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10775 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10776 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10777 bfd_put_32 (obfd, BLR, p), p += 4;
10778
df136d64
AM
10779 if (htab->glink_eh_frame != NULL
10780 && htab->glink_eh_frame->size != 0)
10781 {
10782 bfd_byte *base, *eh;
10783 unsigned int lr_used, delta;
10784
10785 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10786 eh = base + stub_entry->group->eh_size;
10787 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10788 delta = lr_used - stub_entry->group->lr_restore;
10789 stub_entry->group->lr_restore = lr_used + 16;
10790 eh = eh_advance (htab->elf.dynobj, eh, delta);
10791 *eh++ = DW_CFA_offset_extended_sf;
10792 *eh++ = 65;
10793 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10794 *eh++ = DW_CFA_advance_loc + 4;
10795 *eh++ = DW_CFA_restore_extended;
10796 *eh++ = 65;
10797 stub_entry->group->eh_size = eh - base;
10798 }
a7f2871e
AM
10799 return p;
10800}
10801
176a0d42
AM
10802static Elf_Internal_Rela *
10803get_relocs (asection *sec, int count)
10804{
10805 Elf_Internal_Rela *relocs;
10806 struct bfd_elf_section_data *elfsec_data;
10807
10808 elfsec_data = elf_section_data (sec);
10809 relocs = elfsec_data->relocs;
10810 if (relocs == NULL)
10811 {
10812 bfd_size_type relsize;
10813 relsize = sec->reloc_count * sizeof (*relocs);
10814 relocs = bfd_alloc (sec->owner, relsize);
10815 if (relocs == NULL)
10816 return NULL;
10817 elfsec_data->relocs = relocs;
d4730f92
BS
10818 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10819 sizeof (Elf_Internal_Shdr));
10820 if (elfsec_data->rela.hdr == NULL)
10821 return NULL;
10822 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10823 * sizeof (Elf64_External_Rela));
10824 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10825 sec->reloc_count = 0;
10826 }
10827 relocs += sec->reloc_count;
10828 sec->reloc_count += count;
10829 return relocs;
10830}
10831
3d58e1fc
AM
10832/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10833 forms, to the equivalent relocs against the global symbol given by
10834 STUB_ENTRY->H. */
10835
10836static bfd_boolean
10837use_global_in_relocs (struct ppc_link_hash_table *htab,
10838 struct ppc_stub_hash_entry *stub_entry,
10839 Elf_Internal_Rela *r, unsigned int num_rel)
10840{
10841 struct elf_link_hash_entry **hashes;
10842 unsigned long symndx;
10843 struct ppc_link_hash_entry *h;
10844 bfd_vma symval;
10845
10846 /* Relocs are always against symbols in their own object file. Fake
10847 up global sym hashes for the stub bfd (which has no symbols). */
10848 hashes = elf_sym_hashes (htab->params->stub_bfd);
10849 if (hashes == NULL)
10850 {
10851 bfd_size_type hsize;
10852
10853 /* When called the first time, stub_globals will contain the
10854 total number of symbols seen during stub sizing. After
10855 allocating, stub_globals is used as an index to fill the
10856 hashes array. */
10857 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10858 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10859 if (hashes == NULL)
10860 return FALSE;
10861 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10862 htab->stub_globals = 1;
10863 }
10864 symndx = htab->stub_globals++;
10865 h = stub_entry->h;
10866 hashes[symndx] = &h->elf;
10867 if (h->oh != NULL && h->oh->is_func)
10868 h = ppc_follow_link (h->oh);
10869 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10870 || h->elf.root.type == bfd_link_hash_defweak);
10871 symval = (h->elf.root.u.def.value
10872 + h->elf.root.u.def.section->output_offset
10873 + h->elf.root.u.def.section->output_section->vma);
10874 while (num_rel-- != 0)
10875 {
10876 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10877 if (h->elf.root.u.def.section != stub_entry->target_section)
10878 {
10879 /* H is an opd symbol. The addend must be zero, and the
10880 branch reloc is the only one we can convert. */
10881 r->r_addend = 0;
10882 break;
10883 }
10884 else
10885 r->r_addend -= symval;
10886 --r;
10887 }
10888 return TRUE;
10889}
10890
aa374f67 10891static bfd_vma
25f53a85 10892get_r2off (struct bfd_link_info *info,
aa374f67
AM
10893 struct ppc_stub_hash_entry *stub_entry)
10894{
25f53a85 10895 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10896 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10897
10898 if (r2off == 0)
10899 {
10900 /* Support linking -R objects. Get the toc pointer from the
10901 opd entry. */
10902 char buf[8];
b9e5796b
AM
10903 if (!htab->opd_abi)
10904 return r2off;
aa374f67
AM
10905 asection *opd = stub_entry->h->elf.root.u.def.section;
10906 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10907
10908 if (strcmp (opd->name, ".opd") != 0
10909 || opd->reloc_count != 0)
10910 {
2cdcc330
AM
10911 info->callbacks->einfo
10912 (_("%P: cannot find opd entry toc for `%pT'\n"),
10913 stub_entry->h->elf.root.root.string);
aa374f67 10914 bfd_set_error (bfd_error_bad_value);
a7c49797 10915 return (bfd_vma) -1;
aa374f67
AM
10916 }
10917 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10918 return (bfd_vma) -1;
aa374f67 10919 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10920 r2off -= elf_gp (info->output_bfd);
aa374f67 10921 }
6f20ed8a 10922 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10923 return r2off;
10924}
10925
b34976b6 10926static bfd_boolean
4ce794b7 10927ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10928{
721956f4
AM
10929 struct ppc_stub_hash_entry *stub_entry;
10930 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10931 struct bfd_link_info *info;
10932 struct ppc_link_hash_table *htab;
721956f4 10933 bfd_byte *loc;
3d58e1fc 10934 bfd_byte *p, *relp;
1aa42141 10935 bfd_vma targ, off;
176a0d42 10936 Elf_Internal_Rela *r;
e054468f 10937 asection *plt;
3d58e1fc 10938 int num_rel;
04bdff6a 10939 int odd;
5d1634d7 10940
721956f4
AM
10941 /* Massage our args to the form they really have. */
10942 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10943 info = in_arg;
5d1634d7 10944
5d1634d7 10945 htab = ppc_hash_table (info);
4dfe6ac6
NC
10946 if (htab == NULL)
10947 return FALSE;
5d1634d7 10948
1aa42141 10949 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 10950 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10951
4ce794b7 10952 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10953 switch (stub_entry->stub_type)
5d1634d7 10954 {
721956f4 10955 case ppc_stub_long_branch:
ad8e1ba5 10956 case ppc_stub_long_branch_r2off:
721956f4 10957 /* Branches are relative. This is where we are going to. */
1aa42141 10958 targ = (stub_entry->target_value
6911b7dc
AM
10959 + stub_entry->target_section->output_offset
10960 + stub_entry->target_section->output_section->vma);
1aa42141 10961 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 10962
721956f4 10963 /* And this is where we are coming from. */
1aa42141
AM
10964 off = (stub_entry->stub_offset
10965 + stub_entry->group->stub_sec->output_offset
10966 + stub_entry->group->stub_sec->output_section->vma);
10967 off = targ - off;
e86ce104 10968
9e390558 10969 p = loc;
ac2df442 10970 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10971 {
25f53a85 10972 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10973
a7c49797 10974 if (r2off == (bfd_vma) -1)
aa374f67
AM
10975 {
10976 htab->stub_error = TRUE;
10977 return FALSE;
10978 }
9e390558
AM
10979 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10980 p += 4;
ac2df442
AM
10981 if (PPC_HA (r2off) != 0)
10982 {
e7d1c40c 10983 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10984 ADDIS_R2_R2 | PPC_HA (r2off), p);
10985 p += 4;
a7c49797
AM
10986 }
10987 if (PPC_LO (r2off) != 0)
10988 {
10989 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10990 ADDI_R2_R2 | PPC_LO (r2off), p);
10991 p += 4;
ac2df442 10992 }
9e390558 10993 off -= p - loc;
ad8e1ba5 10994 }
9e390558
AM
10995 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10996 p += 4;
ad8e1ba5 10997
5c3dead3
AM
10998 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10999 {
cf97bcb0
AM
11000 _bfd_error_handler
11001 (_("long branch stub `%s' offset overflow"),
bc30df16 11002 stub_entry->root.string);
5c3dead3
AM
11003 htab->stub_error = TRUE;
11004 return FALSE;
11005 }
ee75fd95
AM
11006
11007 if (info->emitrelocations)
11008 {
6f20ed8a 11009 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11010 if (r == NULL)
11011 return FALSE;
9e390558 11012 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11013 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11014 r->r_addend = targ;
3d58e1fc
AM
11015 if (stub_entry->h != NULL
11016 && !use_global_in_relocs (htab, stub_entry, r, 1))
11017 return FALSE;
ee75fd95 11018 }
721956f4 11019 break;
e86ce104 11020
721956f4 11021 case ppc_stub_plt_branch:
ad8e1ba5 11022 case ppc_stub_plt_branch_r2off:
721956f4
AM
11023 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11024 stub_entry->root.string + 9,
b34976b6 11025 FALSE, FALSE);
721956f4
AM
11026 if (br_entry == NULL)
11027 {
cf97bcb0
AM
11028 _bfd_error_handler (_("can't find branch stub `%s'"),
11029 stub_entry->root.string);
b34976b6
AM
11030 htab->stub_error = TRUE;
11031 return FALSE;
721956f4
AM
11032 }
11033
1aa42141 11034 targ = (stub_entry->target_value
176a0d42
AM
11035 + stub_entry->target_section->output_offset
11036 + stub_entry->target_section->output_section->vma);
6911b7dc 11037 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11038 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11039
1aa42141 11040 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11041 htab->brlt->contents + br_entry->offset);
721956f4 11042
f94498ff 11043 if (br_entry->iter == htab->stub_iteration)
721956f4 11044 {
f94498ff 11045 br_entry->iter = 0;
84f5d08e 11046
f94498ff 11047 if (htab->relbrlt != NULL)
84f5d08e 11048 {
f94498ff
AM
11049 /* Create a reloc for the branch lookup table entry. */
11050 Elf_Internal_Rela rela;
11051 bfd_byte *rl;
11052
11053 rela.r_offset = (br_entry->offset
11054 + htab->brlt->output_offset
11055 + htab->brlt->output_section->vma);
11056 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11057 rela.r_addend = targ;
f94498ff
AM
11058
11059 rl = htab->relbrlt->contents;
11060 rl += (htab->relbrlt->reloc_count++
11061 * sizeof (Elf64_External_Rela));
11062 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11063 }
11064 else if (info->emitrelocations)
11065 {
176a0d42
AM
11066 r = get_relocs (htab->brlt, 1);
11067 if (r == NULL)
11068 return FALSE;
11069 /* brlt, being SEC_LINKER_CREATED does not go through the
11070 normal reloc processing. Symbols and offsets are not
11071 translated from input file to output file form, so
11072 set up the offset per the output file. */
f94498ff
AM
11073 r->r_offset = (br_entry->offset
11074 + htab->brlt->output_offset
11075 + htab->brlt->output_section->vma);
11076 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11077 r->r_addend = targ;
84f5d08e 11078 }
84f5d08e 11079 }
721956f4 11080
1aa42141 11081 targ = (br_entry->offset
176a0d42
AM
11082 + htab->brlt->output_offset
11083 + htab->brlt->output_section->vma);
11084
1aa42141
AM
11085 off = (elf_gp (info->output_bfd)
11086 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11087 off = targ - off;
721956f4 11088
ad8e1ba5 11089 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11090 {
25f53a85 11091 info->callbacks->einfo
c1c8c1ef 11092 (_("%P: linkage table error against `%pT'\n"),
721956f4 11093 stub_entry->root.string);
5d1634d7 11094 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11095 htab->stub_error = TRUE;
11096 return FALSE;
5d1634d7 11097 }
41bd81ab 11098
176a0d42
AM
11099 if (info->emitrelocations)
11100 {
6f20ed8a 11101 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11102 if (r == NULL)
11103 return FALSE;
6f20ed8a 11104 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11105 if (bfd_big_endian (info->output_bfd))
11106 r[0].r_offset += 2;
00f412ee 11107 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11108 r[0].r_offset += 4;
11109 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11110 r[0].r_addend = targ;
176a0d42
AM
11111 if (PPC_HA (off) != 0)
11112 {
11113 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11114 r[1].r_offset = r[0].r_offset + 4;
11115 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11116 r[1].r_addend = r[0].r_addend;
11117 }
11118 }
11119
9e390558 11120 p = loc;
00f412ee 11121 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11122 {
176a0d42 11123 if (PPC_HA (off) != 0)
ac2df442 11124 {
e7d1c40c 11125 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11126 ADDIS_R12_R2 | PPC_HA (off), p);
11127 p += 4;
e7d1c40c 11128 bfd_put_32 (htab->params->stub_bfd,
9e390558 11129 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11130 }
11131 else
9e390558
AM
11132 bfd_put_32 (htab->params->stub_bfd,
11133 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11134 }
11135 else
11136 {
25f53a85 11137 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11138
a7c49797 11139 if (r2off == (bfd_vma) -1)
aa374f67
AM
11140 {
11141 htab->stub_error = TRUE;
11142 return FALSE;
11143 }
ad8e1ba5 11144
9e390558
AM
11145 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11146 p += 4;
176a0d42 11147 if (PPC_HA (off) != 0)
ac2df442 11148 {
e7d1c40c 11149 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11150 ADDIS_R12_R2 | PPC_HA (off), p);
11151 p += 4;
e7d1c40c 11152 bfd_put_32 (htab->params->stub_bfd,
9e390558 11153 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11154 }
11155 else
9e390558 11156 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11157
11158 if (PPC_HA (r2off) != 0)
11159 {
9e390558 11160 p += 4;
e7d1c40c 11161 bfd_put_32 (htab->params->stub_bfd,
9e390558 11162 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11163 }
11164 if (PPC_LO (r2off) != 0)
11165 {
9e390558 11166 p += 4;
e7d1c40c 11167 bfd_put_32 (htab->params->stub_bfd,
9e390558 11168 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11169 }
ad8e1ba5 11170 }
9e390558
AM
11171 p += 4;
11172 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11173 p += 4;
407aa07c
AM
11174 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11175 p += 4;
721956f4 11176 break;
5d1634d7 11177
05d0e962
AM
11178 case ppc_stub_long_branch_notoc:
11179 case ppc_stub_long_branch_both:
11180 case ppc_stub_plt_branch_notoc:
11181 case ppc_stub_plt_branch_both:
11182 case ppc_stub_plt_call_notoc:
11183 case ppc_stub_plt_call_both:
11184 p = loc;
f891966f 11185 off = (stub_entry->stub_offset
05d0e962
AM
11186 + stub_entry->group->stub_sec->output_offset
11187 + stub_entry->group->stub_sec->output_section->vma);
11188 if (stub_entry->stub_type == ppc_stub_long_branch_both
11189 || stub_entry->stub_type == ppc_stub_plt_branch_both
11190 || stub_entry->stub_type == ppc_stub_plt_call_both)
11191 {
11192 off += 4;
11193 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11194 p += 4;
11195 }
11196 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11197 {
11198 targ = stub_entry->plt_ent->plt.offset & ~1;
11199 if (targ >= (bfd_vma) -2)
11200 abort ();
11201
11202 plt = htab->elf.splt;
11203 if (!htab->elf.dynamic_sections_created
11204 || stub_entry->h == NULL
11205 || stub_entry->h->elf.dynindx == -1)
11206 {
11207 if (stub_entry->symtype == STT_GNU_IFUNC)
11208 plt = htab->elf.iplt;
11209 else
11210 plt = htab->pltlocal;
11211 }
11212 targ += plt->output_offset + plt->output_section->vma;
11213 }
11214 else
11215 targ = (stub_entry->target_value
11216 + stub_entry->target_section->output_offset
11217 + stub_entry->target_section->output_section->vma);
04bdff6a 11218 odd = off & 4;
05d0e962 11219 off = targ - off;
f891966f 11220
3d58e1fc
AM
11221 relp = p;
11222 num_rel = 0;
04bdff6a
AM
11223 if (htab->powerxx_stubs)
11224 {
11225 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11226 p = build_powerxx_offset (htab->params->stub_bfd, p, off, odd, load);
11227 }
11228 else
11229 {
11230 /* The notoc stubs calculate their target (either a PLT entry or
11231 the global entry point of a function) relative to the PC
11232 returned by the "bcl" two instructions past the start of the
11233 sequence emitted by build_offset. The offset is therefore 8
11234 less than calculated from the start of the sequence. */
11235 off -= 8;
11236 p = build_offset (htab->params->stub_bfd, p, off,
11237 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11238 }
11239
f891966f 11240 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 11241 {
f891966f 11242 bfd_vma from;
3d58e1fc 11243 num_rel = 1;
f891966f
AM
11244 from = (stub_entry->stub_offset
11245 + stub_entry->group->stub_sec->output_offset
11246 + stub_entry->group->stub_sec->output_section->vma
11247 + (p - loc));
05d0e962 11248 bfd_put_32 (htab->params->stub_bfd,
f891966f 11249 B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11250 }
11251 else
11252 {
11253 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11254 p += 4;
11255 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11256 }
11257 p += 4;
df136d64 11258
3d58e1fc
AM
11259 if (info->emitrelocations)
11260 {
04bdff6a
AM
11261 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11262 if (htab->powerxx_stubs)
11263 num_rel += num_relocs_for_powerxx_offset (off, odd);
11264 else
11265 {
11266 num_rel += num_relocs_for_offset (off);
11267 roff += 16;
11268 }
3d58e1fc
AM
11269 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11270 if (r == NULL)
11271 return FALSE;
04bdff6a
AM
11272 if (htab->powerxx_stubs)
11273 r = emit_relocs_for_powerxx_offset (info, r, roff, targ, off, odd);
11274 else
11275 r = emit_relocs_for_offset (info, r, roff, targ, off);
3d58e1fc
AM
11276 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11277 || stub_entry->stub_type == ppc_stub_long_branch_both)
11278 {
11279 ++r;
11280 roff = p - 4 - stub_entry->group->stub_sec->contents;
11281 r->r_offset = roff;
11282 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11283 r->r_addend = targ;
11284 if (stub_entry->h != NULL
11285 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11286 return FALSE;
11287 }
11288 }
11289
04bdff6a
AM
11290 if (!htab->powerxx_stubs
11291 && htab->glink_eh_frame != NULL
11292 && htab->glink_eh_frame->size != 0)
df136d64
AM
11293 {
11294 bfd_byte *base, *eh;
11295 unsigned int lr_used, delta;
11296
11297 base = (htab->glink_eh_frame->contents
11298 + stub_entry->group->eh_base + 17);
11299 eh = base + stub_entry->group->eh_size;
11300 lr_used = stub_entry->stub_offset + 8;
11301 if (stub_entry->stub_type == ppc_stub_long_branch_both
11302 || stub_entry->stub_type == ppc_stub_plt_branch_both
11303 || stub_entry->stub_type == ppc_stub_plt_call_both)
11304 lr_used += 4;
11305 delta = lr_used - stub_entry->group->lr_restore;
11306 stub_entry->group->lr_restore = lr_used + 8;
11307 eh = eh_advance (htab->elf.dynobj, eh, delta);
11308 *eh++ = DW_CFA_register;
11309 *eh++ = 65;
11310 *eh++ = 12;
11311 *eh++ = DW_CFA_advance_loc + 2;
11312 *eh++ = DW_CFA_restore_extended;
11313 *eh++ = 65;
11314 stub_entry->group->eh_size = eh - base;
11315 }
05d0e962
AM
11316 break;
11317
721956f4 11318 case ppc_stub_plt_call:
794e51c0 11319 case ppc_stub_plt_call_r2save:
e054468f 11320 if (stub_entry->h != NULL
b31867b6
AM
11321 && stub_entry->h->is_func_descriptor
11322 && stub_entry->h->oh != NULL)
c862ae31 11323 {
b31867b6
AM
11324 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11325
11326 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11327 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11328 if (fh->elf.root.type == bfd_link_hash_undefined
11329 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11330 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11331 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11332 }
11333
721956f4 11334 /* Now build the stub. */
1aa42141
AM
11335 targ = stub_entry->plt_ent->plt.offset & ~1;
11336 if (targ >= (bfd_vma) -2)
721956f4
AM
11337 abort ();
11338
33e44f2e 11339 plt = htab->elf.splt;
25f23106
AM
11340 if (!htab->elf.dynamic_sections_created
11341 || stub_entry->h == NULL
11342 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11343 {
11344 if (stub_entry->symtype == STT_GNU_IFUNC)
11345 plt = htab->elf.iplt;
11346 else
11347 plt = htab->pltlocal;
11348 }
1aa42141 11349 targ += plt->output_offset + plt->output_section->vma;
e054468f 11350
1aa42141
AM
11351 off = (elf_gp (info->output_bfd)
11352 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11353 off = targ - off;
721956f4 11354
ad8e1ba5 11355 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11356 {
25f53a85 11357 info->callbacks->einfo
695344c0 11358 /* xgettext:c-format */
c1c8c1ef 11359 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11360 stub_entry->h != NULL
11361 ? stub_entry->h->elf.root.root.string
11362 : "<local sym>");
721956f4 11363 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11364 htab->stub_error = TRUE;
11365 return FALSE;
721956f4
AM
11366 }
11367
176a0d42
AM
11368 r = NULL;
11369 if (info->emitrelocations)
11370 {
6f20ed8a 11371 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11372 ((PPC_HA (off) != 0)
11373 + (htab->opd_abi
e7d1c40c 11374 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11375 && PPC_HA (off + 16) == PPC_HA (off))
11376 : 1)));
176a0d42
AM
11377 if (r == NULL)
11378 return FALSE;
6f20ed8a 11379 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11380 if (bfd_big_endian (info->output_bfd))
11381 r[0].r_offset += 2;
1aa42141 11382 r[0].r_addend = targ;
176a0d42 11383 }
a7f2871e
AM
11384 if (stub_entry->h != NULL
11385 && (stub_entry->h == htab->tls_get_addr_fd
11386 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11387 && htab->params->tls_get_addr_opt)
794e51c0 11388 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11389 else
794e51c0 11390 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11391 break;
11392
a4b6fadd
AM
11393 case ppc_stub_save_res:
11394 return TRUE;
11395
721956f4
AM
11396 default:
11397 BFD_FAIL ();
b34976b6 11398 return FALSE;
721956f4
AM
11399 }
11400
1aa42141 11401 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11402
e7d1c40c 11403 if (htab->params->emit_stub_syms)
97b639ba
AM
11404 {
11405 struct elf_link_hash_entry *h;
ee75fd95
AM
11406 size_t len1, len2;
11407 char *name;
11408 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11409 "long_branch",
11410 "long_branch",
3f6ff479
AM
11411 "long_branch",
11412 "plt_branch",
ee75fd95 11413 "plt_branch",
05d0e962
AM
11414 "plt_branch",
11415 "plt_branch",
11416 "plt_call",
11417 "plt_call",
794e51c0 11418 "plt_call",
ee75fd95
AM
11419 "plt_call" };
11420
11421 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11422 len2 = strlen (stub_entry->root.string);
11423 name = bfd_malloc (len1 + len2 + 2);
11424 if (name == NULL)
11425 return FALSE;
11426 memcpy (name, stub_entry->root.string, 9);
11427 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11428 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11429 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11430 if (h == NULL)
11431 return FALSE;
11432 if (h->root.type == bfd_link_hash_new)
11433 {
11434 h->root.type = bfd_link_hash_defined;
6f20ed8a 11435 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11436 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11437 h->ref_regular = 1;
11438 h->def_regular = 1;
11439 h->ref_regular_nonweak = 1;
11440 h->forced_local = 1;
11441 h->non_elf = 0;
2ec55de3 11442 h->root.linker_def = 1;
97b639ba
AM
11443 }
11444 }
11445
b34976b6 11446 return TRUE;
721956f4
AM
11447}
11448
11449/* As above, but don't actually build the stub. Just bump offset so
11450 we know stub section sizes, and select plt_branch stubs where
11451 long_branch stubs won't do. */
11452
b34976b6 11453static bfd_boolean
4ce794b7 11454ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11455{
11456 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11457 struct bfd_link_info *info;
721956f4 11458 struct ppc_link_hash_table *htab;
f891966f
AM
11459 asection *plt;
11460 bfd_vma targ, off, r2off;
04bdff6a 11461 unsigned int size, extra, lr_used, delta, odd;
721956f4
AM
11462
11463 /* Massage our args to the form they really have. */
11464 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11465 info = in_arg;
11466
11467 htab = ppc_hash_table (info);
4dfe6ac6
NC
11468 if (htab == NULL)
11469 return FALSE;
721956f4 11470
1aa42141
AM
11471 /* Make a note of the offset within the stubs for this entry. */
11472 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11473
a4b6fadd
AM
11474 if (stub_entry->h != NULL
11475 && stub_entry->h->save_res
11476 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11477 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11478 {
11479 /* Don't make stubs to out-of-line register save/restore
11480 functions. Instead, emit copies of the functions. */
11481 stub_entry->group->needs_save_res = 1;
11482 stub_entry->stub_type = ppc_stub_save_res;
11483 return TRUE;
11484 }
11485
f891966f 11486 switch (stub_entry->stub_type)
721956f4 11487 {
f891966f
AM
11488 case ppc_stub_plt_branch:
11489 case ppc_stub_plt_branch_r2off:
11490 /* Reset the stub type from the plt branch variant in case we now
11491 can reach with a shorter stub. */
11492 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11493 /* Fall through. */
11494 case ppc_stub_long_branch:
11495 case ppc_stub_long_branch_r2off:
1aa42141
AM
11496 targ = (stub_entry->target_value
11497 + stub_entry->target_section->output_offset
11498 + stub_entry->target_section->output_section->vma);
f891966f 11499 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
11500 off = (stub_entry->stub_offset
11501 + stub_entry->group->stub_sec->output_offset
11502 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11503
ad8e1ba5 11504 size = 4;
f891966f 11505 r2off = 0;
ad8e1ba5
AM
11506 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11507 {
25f53a85 11508 r2off = get_r2off (info, stub_entry);
a7c49797 11509 if (r2off == (bfd_vma) -1)
aa374f67
AM
11510 {
11511 htab->stub_error = TRUE;
11512 return FALSE;
11513 }
a7c49797 11514 size = 8;
ac2df442 11515 if (PPC_HA (r2off) != 0)
a7c49797
AM
11516 size += 4;
11517 if (PPC_LO (r2off) != 0)
11518 size += 4;
1aa42141 11519 off += size - 4;
ad8e1ba5 11520 }
1aa42141 11521 off = targ - off;
ad8e1ba5 11522
f891966f
AM
11523 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11524 Do the same for -R objects without function descriptors. */
11525 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11526 && r2off == 0
11527 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11528 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 11529 {
f891966f 11530 struct ppc_branch_hash_entry *br_entry;
df136d64 11531
f891966f
AM
11532 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11533 stub_entry->root.string + 9,
11534 TRUE, FALSE);
11535 if (br_entry == NULL)
721956f4 11536 {
f891966f
AM
11537 _bfd_error_handler (_("can't build branch stub `%s'"),
11538 stub_entry->root.string);
11539 htab->stub_error = TRUE;
11540 return FALSE;
721956f4
AM
11541 }
11542
f891966f 11543 if (br_entry->iter != htab->stub_iteration)
721956f4 11544 {
f891966f
AM
11545 br_entry->iter = htab->stub_iteration;
11546 br_entry->offset = htab->brlt->size;
11547 htab->brlt->size += 8;
63bc6f6c 11548
f891966f
AM
11549 if (htab->relbrlt != NULL)
11550 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11551 else if (info->emitrelocations)
84f5d08e 11552 {
f891966f
AM
11553 htab->brlt->reloc_count += 1;
11554 htab->brlt->flags |= SEC_RELOC;
05d0e962 11555 }
f891966f 11556 }
ac2df442 11557
f891966f
AM
11558 targ = (br_entry->offset
11559 + htab->brlt->output_offset
11560 + htab->brlt->output_section->vma);
11561 off = (elf_gp (info->output_bfd)
11562 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11563 off = targ - off;
176a0d42 11564
f891966f
AM
11565 if (info->emitrelocations)
11566 {
11567 stub_entry->group->stub_sec->reloc_count
11568 += 1 + (PPC_HA (off) != 0);
11569 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11570 }
05d0e962 11571
f891966f
AM
11572 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11573 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11574 {
11575 size = 12;
11576 if (PPC_HA (off) != 0)
11577 size = 16;
ac2df442 11578 }
f891966f 11579 else
ac2df442 11580 {
f891966f
AM
11581 size = 16;
11582 if (PPC_HA (off) != 0)
11583 size += 4;
11584
11585 if (PPC_HA (r2off) != 0)
11586 size += 4;
11587 if (PPC_LO (r2off) != 0)
11588 size += 4;
ac2df442 11589 }
721956f4 11590 }
f891966f
AM
11591 else if (info->emitrelocations)
11592 {
11593 stub_entry->group->stub_sec->reloc_count += 1;
11594 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11595 }
11596 break;
11597
11598 case ppc_stub_plt_branch_notoc:
11599 case ppc_stub_plt_branch_both:
11600 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11601 /* Fall through. */
11602 case ppc_stub_long_branch_notoc:
11603 case ppc_stub_long_branch_both:
11604 off = (stub_entry->stub_offset
11605 + stub_entry->group->stub_sec->output_offset
11606 + stub_entry->group->stub_sec->output_section->vma);
11607 size = 0;
11608 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11609 size = 4;
11610 off += size;
11611 targ = (stub_entry->target_value
11612 + stub_entry->target_section->output_offset
11613 + stub_entry->target_section->output_section->vma);
04bdff6a 11614 odd = off & 4;
f891966f
AM
11615 off = targ - off;
11616
3d58e1fc
AM
11617 if (info->emitrelocations)
11618 {
04bdff6a
AM
11619 unsigned int num_rel;
11620 if (htab->powerxx_stubs)
11621 num_rel = num_relocs_for_powerxx_offset (off, odd);
11622 else
11623 num_rel = num_relocs_for_offset (off - 8);
11624 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
11625 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11626 }
11627
04bdff6a
AM
11628 if (htab->powerxx_stubs)
11629 extra = size_powerxx_offset (off, odd);
11630 else
11631 extra = size_offset (off - 8);
f891966f
AM
11632 /* Include branch insn plus those in the offset sequence. */
11633 size += 4 + extra;
11634 /* The branch insn is at the end, or "extra" bytes along. So
11635 its offset will be "extra" bytes less that that already
11636 calculated. */
11637 off -= extra;
11638
04bdff6a
AM
11639 if (!htab->powerxx_stubs)
11640 {
11641 /* After the bcl, lr has been modified so we need to emit
11642 .eh_frame info saying the return address is in r12. */
11643 lr_used = stub_entry->stub_offset + 8;
11644 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11645 lr_used += 4;
11646 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11647 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11648 DW_CFA_restore_extended 65. */
11649 delta = lr_used - stub_entry->group->lr_restore;
11650 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11651 stub_entry->group->lr_restore = lr_used + 8;
11652 }
f891966f
AM
11653
11654 /* If the branch can't reach, use a plt_branch. */
11655 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11656 {
11657 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11658 - ppc_stub_long_branch_notoc);
11659 size += 4;
11660 }
3d58e1fc
AM
11661 else if (info->emitrelocations)
11662 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
11663 break;
11664
11665 case ppc_stub_plt_call_notoc:
11666 case ppc_stub_plt_call_both:
11667 off = (stub_entry->stub_offset
11668 + stub_entry->group->stub_sec->output_offset
11669 + stub_entry->group->stub_sec->output_section->vma);
11670 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11671 off += 4;
11672 targ = stub_entry->plt_ent->plt.offset & ~1;
11673 if (targ >= (bfd_vma) -2)
11674 abort ();
11675
11676 plt = htab->elf.splt;
11677 if (!htab->elf.dynamic_sections_created
11678 || stub_entry->h == NULL
11679 || stub_entry->h->elf.dynindx == -1)
11680 {
11681 if (stub_entry->symtype == STT_GNU_IFUNC)
11682 plt = htab->elf.iplt;
11683 else
11684 plt = htab->pltlocal;
11685 }
11686 targ += plt->output_offset + plt->output_section->vma;
04bdff6a 11687 odd = off & 4;
f891966f
AM
11688 off = targ - off;
11689
11690 if (htab->params->plt_stub_align != 0)
11691 {
11692 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11693
11694 stub_entry->group->stub_sec->size += pad;
11695 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11696 off -= pad;
11697 }
11698
3d58e1fc
AM
11699 if (info->emitrelocations)
11700 {
04bdff6a
AM
11701 unsigned int num_rel;
11702 if (htab->powerxx_stubs)
11703 num_rel = num_relocs_for_powerxx_offset (off, odd);
11704 else
11705 num_rel = num_relocs_for_offset (off - 8);
11706 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
11707 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11708 }
11709
f891966f
AM
11710 size = plt_stub_size (htab, stub_entry, off);
11711
04bdff6a
AM
11712 if (!htab->powerxx_stubs)
11713 {
11714 /* After the bcl, lr has been modified so we need to emit
11715 .eh_frame info saying the return address is in r12. */
11716 lr_used = stub_entry->stub_offset + 8;
11717 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11718 lr_used += 4;
11719 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11720 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11721 DW_CFA_restore_extended 65. */
11722 delta = lr_used - stub_entry->group->lr_restore;
11723 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11724 stub_entry->group->lr_restore = lr_used + 8;
11725 }
f891966f
AM
11726 break;
11727
11728 case ppc_stub_plt_call:
11729 case ppc_stub_plt_call_r2save:
11730 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11731 if (targ >= (bfd_vma) -2)
11732 abort ();
11733 plt = htab->elf.splt;
11734 if (!htab->elf.dynamic_sections_created
11735 || stub_entry->h == NULL
11736 || stub_entry->h->elf.dynindx == -1)
11737 {
11738 if (stub_entry->symtype == STT_GNU_IFUNC)
11739 plt = htab->elf.iplt;
11740 else
11741 plt = htab->pltlocal;
11742 }
11743 targ += plt->output_offset + plt->output_section->vma;
11744
11745 off = (elf_gp (info->output_bfd)
11746 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11747 off = targ - off;
11748
11749 if (htab->params->plt_stub_align != 0)
11750 {
11751 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11752
11753 stub_entry->group->stub_sec->size += pad;
11754 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11755 }
11756
11757 if (info->emitrelocations)
11758 {
11759 stub_entry->group->stub_sec->reloc_count
11760 += ((PPC_HA (off) != 0)
11761 + (htab->opd_abi
11762 ? 2 + (htab->params->plt_static_chain
11763 && PPC_HA (off + 16) == PPC_HA (off))
11764 : 1));
11765 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11766 }
11767
11768 size = plt_stub_size (htab, stub_entry, off);
11769
11770 if (stub_entry->h != NULL
11771 && (stub_entry->h == htab->tls_get_addr_fd
11772 || stub_entry->h == htab->tls_get_addr)
11773 && htab->params->tls_get_addr_opt
11774 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11775 {
11776 /* After the bctrl, lr has been modified so we need to
11777 emit .eh_frame info saying the return address is
11778 on the stack. In fact we put the EH info specifying
11779 that the return address is on the stack *at* the
11780 call rather than after it, because the EH info for a
11781 call needs to be specified by that point.
11782 See libgcc/unwind-dw2.c execute_cfa_program. */
11783 lr_used = stub_entry->stub_offset + size - 20;
11784 /* The eh_frame info will consist of a DW_CFA_advance_loc
11785 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11786 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11787 delta = lr_used - stub_entry->group->lr_restore;
11788 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11789 stub_entry->group->lr_restore = size - 4;
11790 }
11791 break;
11792
11793 default:
11794 BFD_FAIL ();
11795 return FALSE;
721956f4
AM
11796 }
11797
6f20ed8a 11798 stub_entry->group->stub_sec->size += size;
b34976b6 11799 return TRUE;
721956f4
AM
11800}
11801
11802/* Set up various things so that we can make a list of input sections
11803 for each output section included in the link. Returns -1 on error,
cedb70c5 11804 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11805
11806int
e7d1c40c 11807ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11808{
6f20ed8a 11809 unsigned int id;
721956f4
AM
11810 bfd_size_type amt;
11811 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11812
4dfe6ac6
NC
11813 if (htab == NULL)
11814 return -1;
4c52953f 11815
7cf7fcc8 11816 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
11817 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11818 htab->sec_info = bfd_zmalloc (amt);
11819 if (htab->sec_info == NULL)
721956f4
AM
11820 return -1;
11821
3d6f9012
AM
11822 /* Set toc_off for com, und, abs and ind sections. */
11823 for (id = 0; id < 3; id++)
6f20ed8a 11824 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11825
721956f4
AM
11826 return 1;
11827}
11828
927be08e
AM
11829/* Set up for first pass at multitoc partitioning. */
11830
11831void
11832ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11833{
11834 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11835
1c865ab2 11836 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11837 htab->toc_bfd = NULL;
11838 htab->toc_first_sec = NULL;
11839}
11840
e717da7e
AM
11841/* The linker repeatedly calls this function for each TOC input section
11842 and linker generated GOT section. Group input bfds such that the toc
927be08e 11843 within a group is less than 64k in size. */
ad8e1ba5 11844
927be08e 11845bfd_boolean
4ce794b7 11846ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11847{
11848 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11849 bfd_vma addr, off, limit;
ad8e1ba5 11850
4dfe6ac6
NC
11851 if (htab == NULL)
11852 return FALSE;
11853
927be08e 11854 if (!htab->second_toc_pass)
4c52953f 11855 {
927be08e 11856 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11857 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11858
11859 if (new_bfd)
bf102f86
AM
11860 {
11861 htab->toc_bfd = isec->owner;
11862 htab->toc_first_sec = isec;
11863 }
927be08e 11864
bf102f86
AM
11865 addr = isec->output_offset + isec->output_section->vma;
11866 off = addr - htab->toc_curr;
d77c8a4b
AM
11867 limit = 0x80008000;
11868 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11869 limit = 0x10000;
11870 if (off + isec->size > limit)
bf102f86
AM
11871 {
11872 addr = (htab->toc_first_sec->output_offset
11873 + htab->toc_first_sec->output_section->vma);
11874 htab->toc_curr = addr;
a27e685f 11875 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11876 }
99877b66 11877
927be08e
AM
11878 /* toc_curr is the base address of this toc group. Set elf_gp
11879 for the input section to be the offset relative to the
11880 output toc base plus 0x8000. Making the input elf_gp an
11881 offset allows us to move the toc as a whole without
11882 recalculating input elf_gp. */
06bcf541 11883 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11884 off += TOC_BASE_OFF;
11885
11886 /* Die if someone uses a linker script that doesn't keep input
11887 file .toc and .got together. */
a4fd3de5
AM
11888 if (new_bfd
11889 && elf_gp (isec->owner) != 0
927be08e
AM
11890 && elf_gp (isec->owner) != off)
11891 return FALSE;
11892
11893 elf_gp (isec->owner) = off;
11894 return TRUE;
4c52953f 11895 }
927be08e
AM
11896
11897 /* During the second pass toc_first_sec points to the start of
11898 a toc group, and toc_curr is used to track the old elf_gp.
11899 We use toc_bfd to ensure we only look at each bfd once. */
11900 if (htab->toc_bfd == isec->owner)
11901 return TRUE;
11902 htab->toc_bfd = isec->owner;
11903
11904 if (htab->toc_first_sec == NULL
11905 || htab->toc_curr != elf_gp (isec->owner))
11906 {
11907 htab->toc_curr = elf_gp (isec->owner);
11908 htab->toc_first_sec = isec;
11909 }
11910 addr = (htab->toc_first_sec->output_offset
11911 + htab->toc_first_sec->output_section->vma);
06bcf541 11912 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
11913 elf_gp (isec->owner) = off;
11914
11915 return TRUE;
ad8e1ba5
AM
11916}
11917
927be08e
AM
11918/* Called via elf_link_hash_traverse to merge GOT entries for global
11919 symbol H. */
11920
11921static bfd_boolean
11922merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11923{
11924 if (h->root.type == bfd_link_hash_indirect)
11925 return TRUE;
11926
927be08e
AM
11927 merge_got_entries (&h->got.glist);
11928
11929 return TRUE;
11930}
11931
11932/* Called via elf_link_hash_traverse to allocate GOT entries for global
11933 symbol H. */
11934
11935static bfd_boolean
11936reallocate_got (struct elf_link_hash_entry *h, void *inf)
11937{
11938 struct got_entry *gent;
11939
11940 if (h->root.type == bfd_link_hash_indirect)
11941 return TRUE;
11942
927be08e
AM
11943 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11944 if (!gent->is_indirect)
11945 allocate_got (h, (struct bfd_link_info *) inf, gent);
11946 return TRUE;
11947}
11948
11949/* Called on the first multitoc pass after the last call to
11950 ppc64_elf_next_toc_section. This function removes duplicate GOT
11951 entries. */
11952
11953bfd_boolean
11954ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11955{
11956 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11957 struct bfd *ibfd, *ibfd2;
11958 bfd_boolean done_something;
11959
11960 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11961
7865406b
AM
11962 if (!htab->do_multi_toc)
11963 return FALSE;
11964
d0fae19d 11965 /* Merge global sym got entries within a toc group. */
927be08e
AM
11966 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11967
11968 /* And tlsld_got. */
c72f2fb2 11969 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11970 {
11971 struct got_entry *ent, *ent2;
11972
11973 if (!is_ppc64_elf (ibfd))
11974 continue;
11975
11976 ent = ppc64_tlsld_got (ibfd);
11977 if (!ent->is_indirect
11978 && ent->got.offset != (bfd_vma) -1)
11979 {
c72f2fb2 11980 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11981 {
11982 if (!is_ppc64_elf (ibfd2))
11983 continue;
11984
11985 ent2 = ppc64_tlsld_got (ibfd2);
11986 if (!ent2->is_indirect
11987 && ent2->got.offset != (bfd_vma) -1
11988 && elf_gp (ibfd2) == elf_gp (ibfd))
11989 {
11990 ent2->is_indirect = TRUE;
11991 ent2->got.ent = ent;
11992 }
11993 }
11994 }
11995 }
11996
11997 /* Zap sizes of got sections. */
33e44f2e
AM
11998 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11999 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12000 htab->got_reli_size = 0;
12001
c72f2fb2 12002 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12003 {
12004 asection *got, *relgot;
12005
12006 if (!is_ppc64_elf (ibfd))
12007 continue;
12008
12009 got = ppc64_elf_tdata (ibfd)->got;
12010 if (got != NULL)
12011 {
12012 got->rawsize = got->size;
12013 got->size = 0;
12014 relgot = ppc64_elf_tdata (ibfd)->relgot;
12015 relgot->rawsize = relgot->size;
12016 relgot->size = 0;
12017 }
12018 }
12019
12020 /* Now reallocate the got, local syms first. We don't need to
12021 allocate section contents again since we never increase size. */
c72f2fb2 12022 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12023 {
12024 struct got_entry **lgot_ents;
12025 struct got_entry **end_lgot_ents;
12026 struct plt_entry **local_plt;
12027 struct plt_entry **end_local_plt;
f961d9dd 12028 unsigned char *lgot_masks;
927be08e
AM
12029 bfd_size_type locsymcount;
12030 Elf_Internal_Shdr *symtab_hdr;
19e08130 12031 asection *s;
927be08e
AM
12032
12033 if (!is_ppc64_elf (ibfd))
12034 continue;
12035
12036 lgot_ents = elf_local_got_ents (ibfd);
12037 if (!lgot_ents)
12038 continue;
12039
12040 symtab_hdr = &elf_symtab_hdr (ibfd);
12041 locsymcount = symtab_hdr->sh_info;
12042 end_lgot_ents = lgot_ents + locsymcount;
12043 local_plt = (struct plt_entry **) end_lgot_ents;
12044 end_local_plt = local_plt + locsymcount;
f961d9dd 12045 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12046 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12047 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12048 {
12049 struct got_entry *ent;
12050
12051 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12052 {
19e08130
AM
12053 unsigned int ent_size = 8;
12054 unsigned int rel_size = sizeof (Elf64_External_Rela);
12055
d0fae19d
AM
12056 ent->got.offset = s->size;
12057 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12058 {
19e08130
AM
12059 ent_size *= 2;
12060 rel_size *= 2;
12061 }
12062 s->size += ent_size;
37da22e5 12063 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12064 {
33e44f2e 12065 htab->elf.irelplt->size += rel_size;
19e08130
AM
12066 htab->got_reli_size += rel_size;
12067 }
6a3858a6
AM
12068 else if (bfd_link_pic (info)
12069 && !((ent->tls_type & TLS_TPREL) != 0
12070 && bfd_link_executable (info)))
19e08130
AM
12071 {
12072 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12073 srel->size += rel_size;
d0fae19d
AM
12074 }
12075 }
927be08e
AM
12076 }
12077 }
12078
12079 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12080
c72f2fb2 12081 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12082 {
12083 struct got_entry *ent;
12084
12085 if (!is_ppc64_elf (ibfd))
12086 continue;
12087
12088 ent = ppc64_tlsld_got (ibfd);
12089 if (!ent->is_indirect
12090 && ent->got.offset != (bfd_vma) -1)
12091 {
12092 asection *s = ppc64_elf_tdata (ibfd)->got;
12093 ent->got.offset = s->size;
12094 s->size += 16;
0e1862bb 12095 if (bfd_link_pic (info))
927be08e
AM
12096 {
12097 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12098 srel->size += sizeof (Elf64_External_Rela);
12099 }
12100 }
12101 }
12102
33e44f2e 12103 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12104 if (!done_something)
c72f2fb2 12105 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12106 {
12107 asection *got;
12108
12109 if (!is_ppc64_elf (ibfd))
12110 continue;
12111
12112 got = ppc64_elf_tdata (ibfd)->got;
12113 if (got != NULL)
12114 {
12115 done_something = got->rawsize != got->size;
12116 if (done_something)
12117 break;
12118 }
12119 }
12120
12121 if (done_something)
e7d1c40c 12122 (*htab->params->layout_sections_again) ();
927be08e
AM
12123
12124 /* Set up for second pass over toc sections to recalculate elf_gp
12125 on input sections. */
12126 htab->toc_bfd = NULL;
12127 htab->toc_first_sec = NULL;
12128 htab->second_toc_pass = TRUE;
12129 return done_something;
12130}
12131
12132/* Called after second pass of multitoc partitioning. */
12133
12134void
12135ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12136{
12137 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12138
12139 /* After the second pass, toc_curr tracks the TOC offset used
12140 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12141 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12142}
12143
9b5ecbd0
AM
12144/* No toc references were found in ISEC. If the code in ISEC makes no
12145 calls, then there's no need to use toc adjusting stubs when branching
12146 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12147 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12148 needed, and 2 if a cyclical call-graph was found but no other reason
12149 for a stub was detected. If called from the top level, a return of
12150 2 means the same as a return of 0. */
9b5ecbd0
AM
12151
12152static int
4ce794b7 12153toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12154{
9b5ecbd0 12155 int ret;
70cc837d
AM
12156
12157 /* Mark this section as checked. */
12158 isec->call_check_done = 1;
9b5ecbd0 12159
772119ce
AM
12160 /* We know none of our code bearing sections will need toc stubs. */
12161 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12162 return 0;
12163
eea6121a 12164 if (isec->size == 0)
082c50f8
AM
12165 return 0;
12166
4c52953f
AM
12167 if (isec->output_section == NULL)
12168 return 0;
12169
4c52953f 12170 ret = 0;
70cc837d 12171 if (isec->reloc_count != 0)
9b5ecbd0 12172 {
70cc837d
AM
12173 Elf_Internal_Rela *relstart, *rel;
12174 Elf_Internal_Sym *local_syms;
12175 struct ppc_link_hash_table *htab;
2917689a 12176
70cc837d
AM
12177 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12178 info->keep_memory);
12179 if (relstart == NULL)
12180 return -1;
90aecf7a 12181
70cc837d
AM
12182 /* Look for branches to outside of this section. */
12183 local_syms = NULL;
12184 htab = ppc_hash_table (info);
12185 if (htab == NULL)
12186 return -1;
4c52953f 12187
70cc837d 12188 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12189 {
70cc837d
AM
12190 enum elf_ppc64_reloc_type r_type;
12191 unsigned long r_symndx;
12192 struct elf_link_hash_entry *h;
12193 struct ppc_link_hash_entry *eh;
12194 Elf_Internal_Sym *sym;
12195 asection *sym_sec;
12196 struct _opd_sec_data *opd;
12197 bfd_vma sym_value;
12198 bfd_vma dest;
12199
12200 r_type = ELF64_R_TYPE (rel->r_info);
12201 if (r_type != R_PPC64_REL24
05d0e962 12202 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12203 && r_type != R_PPC64_REL14
12204 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12205 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12206 && r_type != R_PPC64_PLTCALL
12207 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12208 continue;
4c52953f 12209
70cc837d
AM
12210 r_symndx = ELF64_R_SYM (rel->r_info);
12211 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12212 isec->owner))
4c52953f 12213 {
70cc837d
AM
12214 ret = -1;
12215 break;
12216 }
4c52953f 12217
70cc837d
AM
12218 /* Calls to dynamic lib functions go through a plt call stub
12219 that uses r2. */
12220 eh = (struct ppc_link_hash_entry *) h;
12221 if (eh != NULL
12222 && (eh->elf.plt.plist != NULL
12223 || (eh->oh != NULL
12224 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12225 {
12226 ret = 1;
12227 break;
4c52953f
AM
12228 }
12229
70cc837d
AM
12230 if (sym_sec == NULL)
12231 /* Ignore other undefined symbols. */
4c52953f 12232 continue;
4c52953f 12233
70cc837d
AM
12234 /* Assume branches to other sections not included in the
12235 link need stubs too, to cover -R and absolute syms. */
12236 if (sym_sec->output_section == NULL)
12237 {
12238 ret = 1;
12239 break;
12240 }
4c52953f 12241
70cc837d
AM
12242 if (h == NULL)
12243 sym_value = sym->st_value;
12244 else
12245 {
12246 if (h->root.type != bfd_link_hash_defined
12247 && h->root.type != bfd_link_hash_defweak)
12248 abort ();
12249 sym_value = h->root.u.def.value;
12250 }
12251 sym_value += rel->r_addend;
4c52953f 12252
70cc837d
AM
12253 /* If this branch reloc uses an opd sym, find the code section. */
12254 opd = get_opd_info (sym_sec);
12255 if (opd != NULL)
12256 {
12257 if (h == NULL && opd->adjust != NULL)
12258 {
12259 long adjust;
4c52953f 12260
92a9c616 12261 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12262 if (adjust == -1)
12263 /* Assume deleted functions won't ever be called. */
12264 continue;
12265 sym_value += adjust;
12266 }
4c52953f 12267
aef36ac1
AM
12268 dest = opd_entry_value (sym_sec, sym_value,
12269 &sym_sec, NULL, FALSE);
70cc837d
AM
12270 if (dest == (bfd_vma) -1)
12271 continue;
12272 }
12273 else
12274 dest = (sym_value
12275 + sym_sec->output_offset
12276 + sym_sec->output_section->vma);
4c52953f 12277
70cc837d
AM
12278 /* Ignore branch to self. */
12279 if (sym_sec == isec)
12280 continue;
4c52953f 12281
70cc837d
AM
12282 /* If the called function uses the toc, we need a stub. */
12283 if (sym_sec->has_toc_reloc
12284 || sym_sec->makes_toc_func_call)
4c52953f 12285 {
70cc837d 12286 ret = 1;
4c52953f
AM
12287 break;
12288 }
70cc837d
AM
12289
12290 /* Assume any branch that needs a long branch stub might in fact
12291 need a plt_branch stub. A plt_branch stub uses r2. */
12292 else if (dest - (isec->output_offset
12293 + isec->output_section->vma
6911b7dc
AM
12294 + rel->r_offset) + (1 << 25)
12295 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12296 ? h->other
12297 : sym->st_other))
4c52953f 12298 {
70cc837d
AM
12299 ret = 1;
12300 break;
12301 }
12302
12303 /* If calling back to a section in the process of being
12304 tested, we can't say for sure that no toc adjusting stubs
12305 are needed, so don't return zero. */
12306 else if (sym_sec->call_check_in_progress)
12307 ret = 2;
12308
12309 /* Branches to another section that itself doesn't have any TOC
12310 references are OK. Recursively call ourselves to check. */
12311 else if (!sym_sec->call_check_done)
12312 {
12313 int recur;
12314
12315 /* Mark current section as indeterminate, so that other
12316 sections that call back to current won't be marked as
12317 known. */
12318 isec->call_check_in_progress = 1;
12319 recur = toc_adjusting_stub_needed (info, sym_sec);
12320 isec->call_check_in_progress = 0;
12321
4c52953f
AM
12322 if (recur != 0)
12323 {
70cc837d
AM
12324 ret = recur;
12325 if (recur != 2)
12326 break;
4c52953f
AM
12327 }
12328 }
4c52953f 12329 }
70cc837d
AM
12330
12331 if (local_syms != NULL
12332 && (elf_symtab_hdr (isec->owner).contents
12333 != (unsigned char *) local_syms))
12334 free (local_syms);
12335 if (elf_section_data (isec)->relocs != relstart)
12336 free (relstart);
9b5ecbd0
AM
12337 }
12338
70cc837d
AM
12339 if ((ret & 1) == 0
12340 && isec->map_head.s != NULL
12341 && (strcmp (isec->output_section->name, ".init") == 0
12342 || strcmp (isec->output_section->name, ".fini") == 0))
12343 {
12344 if (isec->map_head.s->has_toc_reloc
12345 || isec->map_head.s->makes_toc_func_call)
12346 ret = 1;
12347 else if (!isec->map_head.s->call_check_done)
12348 {
12349 int recur;
12350 isec->call_check_in_progress = 1;
12351 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12352 isec->call_check_in_progress = 0;
12353 if (recur != 0)
12354 ret = recur;
12355 }
12356 }
12357
12358 if (ret == 1)
12359 isec->makes_toc_func_call = 1;
4c52953f 12360
9b5ecbd0
AM
12361 return ret;
12362}
12363
721956f4
AM
12364/* The linker repeatedly calls this function for each input section,
12365 in the order that input sections are linked into output sections.
12366 Build lists of input sections to determine groupings between which
12367 we may insert linker stubs. */
12368
9b5ecbd0 12369bfd_boolean
4ce794b7 12370ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12371{
12372 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12373
4dfe6ac6
NC
12374 if (htab == NULL)
12375 return FALSE;
12376
734b6cf9 12377 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12378 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12379 {
3d6f9012
AM
12380 /* This happens to make the list in reverse order,
12381 which is what we want. */
6f20ed8a
AM
12382 htab->sec_info[isec->id].u.list
12383 = htab->sec_info[isec->output_section->id].u.list;
12384 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12385 }
ad8e1ba5 12386
4c52953f 12387 if (htab->multi_toc_needed)
9b5ecbd0 12388 {
8b974ba3
AM
12389 /* Analyse sections that aren't already flagged as needing a
12390 valid toc pointer. Exclude .fixup for the linux kernel.
12391 .fixup contains branches, but only back to the function that
12392 hit an exception. */
12393 if (!(isec->has_toc_reloc
12394 || (isec->flags & SEC_CODE) == 0
12395 || strcmp (isec->name, ".fixup") == 0
12396 || isec->call_check_done))
12397 {
12398 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12399 return FALSE;
8b974ba3
AM
12400 }
12401 /* Make all sections use the TOC assigned for this object file.
12402 This will be wrong for pasted sections; We fix that in
12403 check_pasted_section(). */
12404 if (elf_gp (isec->owner) != 0)
12405 htab->toc_curr = elf_gp (isec->owner);
12406 }
12407
6f20ed8a 12408 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12409 return TRUE;
721956f4
AM
12410}
12411
70cc837d
AM
12412/* Check that all .init and .fini sections use the same toc, if they
12413 have toc relocs. */
12414
12415static bfd_boolean
12416check_pasted_section (struct bfd_link_info *info, const char *name)
12417{
12418 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12419
12420 if (o != NULL)
12421 {
12422 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12423 bfd_vma toc_off = 0;
12424 asection *i;
12425
12426 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12427 if (i->has_toc_reloc)
12428 {
12429 if (toc_off == 0)
6f20ed8a
AM
12430 toc_off = htab->sec_info[i->id].toc_off;
12431 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12432 return FALSE;
12433 }
6683a28d
AM
12434
12435 if (toc_off == 0)
12436 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12437 if (i->makes_toc_func_call)
12438 {
6f20ed8a 12439 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12440 break;
12441 }
12442
70cc837d
AM
12443 /* Make sure the whole pasted function uses the same toc offset. */
12444 if (toc_off != 0)
12445 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12446 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12447 }
12448 return TRUE;
12449}
12450
12451bfd_boolean
12452ppc64_elf_check_init_fini (struct bfd_link_info *info)
12453{
12454 return (check_pasted_section (info, ".init")
12455 & check_pasted_section (info, ".fini"));
12456}
12457
721956f4
AM
12458/* See whether we can group stub sections together. Grouping stub
12459 sections may result in fewer stubs. More importantly, we need to
12460 put all .init* and .fini* stubs at the beginning of the .init or
12461 .fini output sections respectively, because glibc splits the
12462 _init and _fini functions into multiple parts. Putting a stub in
12463 the middle of a function is not a good idea. */
12464
6f20ed8a
AM
12465static bfd_boolean
12466group_sections (struct bfd_link_info *info,
4ce794b7
AM
12467 bfd_size_type stub_group_size,
12468 bfd_boolean stubs_always_before_branch)
721956f4 12469{
6f20ed8a
AM
12470 struct ppc_link_hash_table *htab;
12471 asection *osec;
7c8fe5c4
AM
12472 bfd_boolean suppress_size_errors;
12473
6f20ed8a
AM
12474 htab = ppc_hash_table (info);
12475 if (htab == NULL)
12476 return FALSE;
12477
7c8fe5c4 12478 suppress_size_errors = FALSE;
7c8fe5c4
AM
12479 if (stub_group_size == 1)
12480 {
12481 /* Default values. */
12482 if (stubs_always_before_branch)
09f92717 12483 stub_group_size = 0x1e00000;
7c8fe5c4 12484 else
09f92717 12485 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12486 suppress_size_errors = TRUE;
12487 }
12488
6f20ed8a 12489 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12490 {
6f20ed8a
AM
12491 asection *tail;
12492
12493 if (osec->id >= htab->sec_info_arr_size)
12494 continue;
12495
12496 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12497 while (tail != NULL)
721956f4 12498 {
734b6cf9
AM
12499 asection *curr;
12500 asection *prev;
12501 bfd_size_type total;
12502 bfd_boolean big_sec;
12503 bfd_vma curr_toc;
6f20ed8a 12504 struct map_stub *group;
09f92717 12505 bfd_size_type group_size;
734b6cf9
AM
12506
12507 curr = tail;
eea6121a 12508 total = tail->size;
09f92717
AM
12509 group_size = (ppc64_elf_section_data (tail) != NULL
12510 && ppc64_elf_section_data (tail)->has_14bit_branch
12511 ? stub_group_size >> 10 : stub_group_size);
12512
12513 big_sec = total > group_size;
7c8fe5c4 12514 if (big_sec && !suppress_size_errors)
695344c0 12515 /* xgettext:c-format */
871b3ab2 12516 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12517 tail->owner, tail);
6f20ed8a 12518 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12519
6f20ed8a 12520 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12521 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12522 < (ppc64_elf_section_data (prev) != NULL
12523 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12524 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12525 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12526 curr = prev;
12527
12528 /* OK, the size from the start of CURR to the end is less
09f92717 12529 than group_size and thus can be handled by one stub
734b6cf9 12530 section. (or the tail section is itself larger than
09f92717
AM
12531 group_size, in which case we may be toast.) We should
12532 really be keeping track of the total size of stubs added
12533 here, as stubs contribute to the final output section
12534 size. That's a little tricky, and this way will only
12535 break if stubs added make the total size more than 2^25,
12536 ie. for the default stub_group_size, if stubs total more
12537 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12538 group = bfd_alloc (curr->owner, sizeof (*group));
12539 if (group == NULL)
12540 return FALSE;
12541 group->link_sec = curr;
12542 group->stub_sec = NULL;
a4b6fadd 12543 group->needs_save_res = 0;
df136d64
AM
12544 group->lr_restore = 0;
12545 group->eh_size = 0;
12546 group->eh_base = 0;
a4b6fadd
AM
12547 group->next = htab->group;
12548 htab->group = group;
734b6cf9 12549 do
721956f4 12550 {
6f20ed8a 12551 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12552 /* Set up this stub group. */
6f20ed8a 12553 htab->sec_info[tail->id].u.group = group;
721956f4 12554 }
734b6cf9
AM
12555 while (tail != curr && (tail = prev) != NULL);
12556
09f92717 12557 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12558 bytes before the stub section can be handled by it too.
12559 Don't do this if we have a really large section after the
12560 stubs, as adding more stubs increases the chance that
12561 branches may not reach into the stub section. */
12562 if (!stubs_always_before_branch && !big_sec)
12563 {
12564 total = 0;
12565 while (prev != NULL
12566 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12567 < (ppc64_elf_section_data (prev) != NULL
12568 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
12569 ? (group_size = stub_group_size >> 10)
12570 : group_size))
6f20ed8a 12571 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12572 {
12573 tail = prev;
6f20ed8a
AM
12574 prev = htab->sec_info[tail->id].u.list;
12575 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12576 }
12577 }
12578 tail = prev;
721956f4
AM
12579 }
12580 }
6f20ed8a 12581 return TRUE;
721956f4
AM
12582}
12583
58d180e8
AM
12584static const unsigned char glink_eh_frame_cie[] =
12585{
12586 0, 0, 0, 16, /* length. */
12587 0, 0, 0, 0, /* id. */
12588 1, /* CIE version. */
12589 'z', 'R', 0, /* Augmentation string. */
12590 4, /* Code alignment. */
12591 0x78, /* Data alignment. */
12592 65, /* RA reg. */
12593 1, /* Augmentation size. */
12594 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12595 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12596};
12597
d969d15f
AM
12598/* Stripping output sections is normally done before dynamic section
12599 symbols have been allocated. This function is called later, and
12600 handles cases like htab->brlt which is mapped to its own output
12601 section. */
12602
12603static void
12604maybe_strip_output (struct bfd_link_info *info, asection *isec)
12605{
12606 if (isec->size == 0
12607 && isec->output_section->size == 0
53d8967a 12608 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12609 && !bfd_section_removed_from_list (info->output_bfd,
12610 isec->output_section)
12611 && elf_section_data (isec->output_section)->dynindx == 0)
12612 {
12613 isec->output_section->flags |= SEC_EXCLUDE;
12614 bfd_section_list_remove (info->output_bfd, isec->output_section);
12615 info->output_bfd->section_count--;
12616 }
12617}
12618
721956f4
AM
12619/* Determine and set the size of the stub section for a final link.
12620
12621 The basic idea here is to examine all the relocations looking for
12622 PC-relative calls to a target that is unreachable with a "bl"
12623 instruction. */
12624
b34976b6 12625bfd_boolean
e7d1c40c 12626ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12627{
12628 bfd_size_type stub_group_size;
b34976b6 12629 bfd_boolean stubs_always_before_branch;
721956f4
AM
12630 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12631
4dfe6ac6
NC
12632 if (htab == NULL)
12633 return FALSE;
12634
0e1862bb 12635 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12636 htab->params->plt_thread_safe = 1;
b9e5796b 12637 if (!htab->opd_abi)
e7d1c40c
AM
12638 htab->params->plt_thread_safe = 0;
12639 else if (htab->params->plt_thread_safe == -1)
794e51c0 12640 {
e2458743 12641 static const char *const thread_starter[] =
794e51c0
AM
12642 {
12643 "pthread_create",
12644 /* libstdc++ */
12645 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12646 /* librt */
12647 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12648 "mq_notify", "create_timer",
12649 /* libanl */
12650 "getaddrinfo_a",
12651 /* libgomp */
2300b5a1 12652 "GOMP_parallel",
794e51c0 12653 "GOMP_parallel_start",
2300b5a1 12654 "GOMP_parallel_loop_static",
794e51c0 12655 "GOMP_parallel_loop_static_start",
2300b5a1 12656 "GOMP_parallel_loop_dynamic",
794e51c0 12657 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12658 "GOMP_parallel_loop_guided",
794e51c0 12659 "GOMP_parallel_loop_guided_start",
2300b5a1 12660 "GOMP_parallel_loop_runtime",
794e51c0 12661 "GOMP_parallel_loop_runtime_start",
2300b5a1 12662 "GOMP_parallel_sections",
68ffbac6 12663 "GOMP_parallel_sections_start",
f9dffbf0
AM
12664 /* libgo */
12665 "__go_go",
794e51c0
AM
12666 };
12667 unsigned i;
12668
a4b6fadd 12669 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12670 {
12671 struct elf_link_hash_entry *h;
12672 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12673 FALSE, FALSE, TRUE);
e7d1c40c
AM
12674 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12675 if (htab->params->plt_thread_safe)
794e51c0
AM
12676 break;
12677 }
12678 }
e7d1c40c
AM
12679 stubs_always_before_branch = htab->params->group_size < 0;
12680 if (htab->params->group_size < 0)
12681 stub_group_size = -htab->params->group_size;
721956f4 12682 else
e7d1c40c 12683 stub_group_size = htab->params->group_size;
721956f4 12684
6f20ed8a
AM
12685 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12686 return FALSE;
721956f4 12687
c9301e31
AM
12688#define STUB_SHRINK_ITER 20
12689 /* Loop until no stubs added. After iteration 20 of this loop we may
12690 exit on a stub section shrinking. This is to break out of a
12691 pathological case where adding stubs on one iteration decreases
12692 section gaps (perhaps due to alignment), which then requires
12693 fewer or smaller stubs on the next iteration. */
12694
721956f4
AM
12695 while (1)
12696 {
12697 bfd *input_bfd;
12698 unsigned int bfd_indx;
a4b6fadd 12699 struct map_stub *group;
721956f4
AM
12700
12701 htab->stub_iteration += 1;
721956f4
AM
12702
12703 for (input_bfd = info->input_bfds, bfd_indx = 0;
12704 input_bfd != NULL;
c72f2fb2 12705 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12706 {
12707 Elf_Internal_Shdr *symtab_hdr;
12708 asection *section;
6cdc0ccc 12709 Elf_Internal_Sym *local_syms = NULL;
721956f4 12710
0c8d6e5c 12711 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12712 continue;
12713
721956f4 12714 /* We'll need the symbol table in a second. */
0ffa91dd 12715 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12716 if (symtab_hdr->sh_info == 0)
12717 continue;
12718
721956f4
AM
12719 /* Walk over each section attached to the input bfd. */
12720 for (section = input_bfd->sections;
12721 section != NULL;
12722 section = section->next)
12723 {
721956f4 12724 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12725
12726 /* If there aren't any relocs, then there's nothing more
12727 to do. */
12728 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12729 || (section->flags & SEC_ALLOC) == 0
12730 || (section->flags & SEC_LOAD) == 0
12731 || (section->flags & SEC_CODE) == 0
721956f4
AM
12732 || section->reloc_count == 0)
12733 continue;
12734
12735 /* If this section is a link-once section that will be
12736 discarded, then don't create any stubs. */
12737 if (section->output_section == NULL
927be08e 12738 || section->output_section->owner != info->output_bfd)
721956f4
AM
12739 continue;
12740
1e2f5b6e
AM
12741 /* Get the relocs. */
12742 internal_relocs
4ce794b7 12743 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12744 info->keep_memory);
721956f4 12745 if (internal_relocs == NULL)
1e2f5b6e 12746 goto error_ret_free_local;
721956f4
AM
12747
12748 /* Now examine each relocation. */
12749 irela = internal_relocs;
12750 irelaend = irela + section->reloc_count;
12751 for (; irela < irelaend; irela++)
12752 {
4ce794b7
AM
12753 enum elf_ppc64_reloc_type r_type;
12754 unsigned int r_indx;
721956f4
AM
12755 enum ppc_stub_type stub_type;
12756 struct ppc_stub_hash_entry *stub_entry;
8387904d 12757 asection *sym_sec, *code_sec;
e054468f 12758 bfd_vma sym_value, code_value;
721956f4 12759 bfd_vma destination;
6911b7dc 12760 unsigned long local_off;
8843416a 12761 bfd_boolean ok_dest;
721956f4 12762 struct ppc_link_hash_entry *hash;
8387904d 12763 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12764 struct elf_link_hash_entry *h;
12765 Elf_Internal_Sym *sym;
721956f4
AM
12766 char *stub_name;
12767 const asection *id_sec;
74f0fb50 12768 struct _opd_sec_data *opd;
e054468f 12769 struct plt_entry *plt_ent;
721956f4
AM
12770
12771 r_type = ELF64_R_TYPE (irela->r_info);
12772 r_indx = ELF64_R_SYM (irela->r_info);
12773
4ce794b7 12774 if (r_type >= R_PPC64_max)
721956f4
AM
12775 {
12776 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12777 goto error_ret_free_internal;
721956f4
AM
12778 }
12779
12780 /* Only look for stubs on branch instructions. */
4ce794b7 12781 if (r_type != R_PPC64_REL24
05d0e962 12782 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
12783 && r_type != R_PPC64_REL14
12784 && r_type != R_PPC64_REL14_BRTAKEN
12785 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12786 continue;
12787
12788 /* Now determine the call target, its name, value,
12789 section. */
411e1bfb
AM
12790 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12791 r_indx, input_bfd))
12792 goto error_ret_free_internal;
12793 hash = (struct ppc_link_hash_entry *) h;
12794
8843416a 12795 ok_dest = FALSE;
8387904d 12796 fdh = NULL;
7fe2b9a6 12797 sym_value = 0;
411e1bfb 12798 if (hash == NULL)
721956f4 12799 {
411e1bfb 12800 sym_value = sym->st_value;
c27b8c2a
AM
12801 if (sym_sec != NULL
12802 && sym_sec->output_section != NULL)
12803 ok_dest = TRUE;
721956f4 12804 }
7fe2b9a6
AM
12805 else if (hash->elf.root.type == bfd_link_hash_defined
12806 || hash->elf.root.type == bfd_link_hash_defweak)
12807 {
12808 sym_value = hash->elf.root.u.def.value;
12809 if (sym_sec->output_section != NULL)
12810 ok_dest = TRUE;
12811 }
12812 else if (hash->elf.root.type == bfd_link_hash_undefweak
12813 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12814 {
99877b66 12815 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12816 use the func descriptor sym instead if it is
12817 defined. */
ceb1f1ef 12818 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12819 && hash->oh != NULL)
8387904d 12820 {
8c5b4e52 12821 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12822 if (fdh->elf.root.type == bfd_link_hash_defined
12823 || fdh->elf.root.type == bfd_link_hash_defweak)
12824 {
12825 sym_sec = fdh->elf.root.u.def.section;
12826 sym_value = fdh->elf.root.u.def.value;
12827 if (sym_sec->output_section != NULL)
12828 ok_dest = TRUE;
12829 }
99877b66
AM
12830 else
12831 fdh = NULL;
8387904d 12832 }
7fe2b9a6
AM
12833 }
12834 else
12835 {
12836 bfd_set_error (bfd_error_bad_value);
12837 goto error_ret_free_internal;
721956f4
AM
12838 }
12839
8843416a 12840 destination = 0;
6911b7dc 12841 local_off = 0;
8843416a
AM
12842 if (ok_dest)
12843 {
12844 sym_value += irela->r_addend;
12845 destination = (sym_value
12846 + sym_sec->output_offset
12847 + sym_sec->output_section->vma);
6911b7dc
AM
12848 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12849 ? hash->elf.other
12850 : sym->st_other);
8843416a
AM
12851 }
12852
8387904d 12853 code_sec = sym_sec;
e054468f 12854 code_value = sym_value;
74f0fb50
AM
12855 opd = get_opd_info (sym_sec);
12856 if (opd != NULL)
8387904d
AM
12857 {
12858 bfd_vma dest;
12859
74f0fb50 12860 if (hash == NULL && opd->adjust != NULL)
8387904d 12861 {
51aecdc5 12862 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12863 if (adjust == -1)
12864 continue;
e054468f 12865 code_value += adjust;
8387904d
AM
12866 sym_value += adjust;
12867 }
12868 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12869 &code_sec, &code_value, FALSE);
8387904d
AM
12870 if (dest != (bfd_vma) -1)
12871 {
12872 destination = dest;
12873 if (fdh != NULL)
12874 {
12875 /* Fixup old ABI sym to point at code
12876 entry. */
99877b66 12877 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12878 hash->elf.root.u.def.section = code_sec;
e054468f 12879 hash->elf.root.u.def.value = code_value;
8387904d
AM
12880 }
12881 }
12882 }
12883
721956f4 12884 /* Determine what (if any) linker stub is needed. */
e054468f 12885 plt_ent = NULL;
721956f4 12886 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12887 &plt_ent, destination,
12888 local_off);
ad8e1ba5 12889
05d0e962
AM
12890 if (r_type == R_PPC64_REL24_NOTOC)
12891 {
12892 if (stub_type == ppc_stub_plt_call)
12893 stub_type = ppc_stub_plt_call_notoc;
12894 else if (stub_type == ppc_stub_long_branch
12895 || (code_sec != NULL
12896 && code_sec->output_section != NULL
12897 && (((hash ? hash->elf.other : sym->st_other)
12898 & STO_PPC64_LOCAL_MASK)
4a4e7361 12899 > 1 << STO_PPC64_LOCAL_BIT)))
05d0e962
AM
12900 stub_type = ppc_stub_long_branch_notoc;
12901 }
12902 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
12903 {
12904 /* Check whether we need a TOC adjusting stub.
12905 Since the linker pastes together pieces from
12906 different object files when creating the
12907 _init and _fini functions, it may be that a
12908 call to what looks like a local sym is in
12909 fact a call needing a TOC adjustment. */
33cb30a1
AM
12910 if ((code_sec != NULL
12911 && code_sec->output_section != NULL
12912 && (htab->sec_info[code_sec->id].toc_off
12913 != htab->sec_info[section->id].toc_off)
12914 && (code_sec->has_toc_reloc
12915 || code_sec->makes_toc_func_call))
12916 || (((hash ? hash->elf.other : sym->st_other)
12917 & STO_PPC64_LOCAL_MASK)
12918 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
12919 stub_type = ppc_stub_long_branch_r2off;
12920 }
12921
721956f4
AM
12922 if (stub_type == ppc_stub_none)
12923 continue;
12924
411e1bfb
AM
12925 /* __tls_get_addr calls might be eliminated. */
12926 if (stub_type != ppc_stub_plt_call
05d0e962 12927 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 12928 && hash != NULL
8387904d
AM
12929 && (hash == htab->tls_get_addr
12930 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12931 && section->has_tls_reloc
12932 && irela != internal_relocs)
12933 {
12934 /* Get tls info. */
f961d9dd 12935 unsigned char *tls_mask;
411e1bfb 12936
3a71aa26 12937 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12938 irela - 1, input_bfd))
12939 goto error_ret_free_internal;
37da22e5 12940 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
12941 continue;
12942 }
12943
f378ab09 12944 if (stub_type == ppc_stub_plt_call)
794e51c0 12945 {
6e1816be
AM
12946 if (!htab->opd_abi
12947 && htab->params->plt_localentry0 != 0
12948 && is_elfv2_localentry0 (&hash->elf))
12949 htab->has_plt_localentry0 = 1;
12950 else if (irela + 1 < irelaend
12951 && irela[1].r_offset == irela->r_offset + 4
12952 && (ELF64_R_TYPE (irela[1].r_info)
12953 == R_PPC64_TOCSAVE))
f378ab09
AM
12954 {
12955 if (!tocsave_find (htab, INSERT,
12956 &local_syms, irela + 1, input_bfd))
12957 goto error_ret_free_internal;
12958 }
f378ab09
AM
12959 else
12960 stub_type = ppc_stub_plt_call_r2save;
794e51c0 12961 }
3b421ab3 12962
721956f4 12963 /* Support for grouping stub sections. */
6f20ed8a 12964 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12965
12966 /* Get the name of this stub. */
12967 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12968 if (!stub_name)
12969 goto error_ret_free_internal;
12970
12971 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12972 stub_name, FALSE, FALSE);
721956f4
AM
12973 if (stub_entry != NULL)
12974 {
05d0e962
AM
12975 enum ppc_stub_type old_type;
12976 /* A stub has already been created, but it may
12977 not be the required type. We shouldn't be
12978 transitioning from plt_call to long_branch
12979 stubs or vice versa, but we might be
12980 upgrading from plt_call to plt_call_r2save or
12981 from long_branch to long_branch_r2off. */
721956f4 12982 free (stub_name);
05d0e962
AM
12983 old_type = stub_entry->stub_type;
12984 switch (old_type)
12985 {
12986 default:
12987 abort ();
12988
12989 case ppc_stub_save_res:
12990 continue;
12991
12992 case ppc_stub_plt_call:
12993 case ppc_stub_plt_call_r2save:
12994 case ppc_stub_plt_call_notoc:
12995 case ppc_stub_plt_call_both:
12996 if (stub_type == ppc_stub_plt_call)
12997 continue;
12998 else if (stub_type == ppc_stub_plt_call_r2save)
12999 {
13000 if (old_type == ppc_stub_plt_call_notoc)
13001 stub_type = ppc_stub_plt_call_both;
13002 }
13003 else if (stub_type == ppc_stub_plt_call_notoc)
13004 {
13005 if (old_type == ppc_stub_plt_call_r2save)
13006 stub_type = ppc_stub_plt_call_both;
13007 }
13008 else
13009 abort ();
13010 break;
13011
13012 case ppc_stub_plt_branch:
13013 case ppc_stub_plt_branch_r2off:
13014 case ppc_stub_plt_branch_notoc:
13015 case ppc_stub_plt_branch_both:
13016 old_type += (ppc_stub_long_branch
13017 - ppc_stub_plt_branch);
13018 /* Fall through. */
13019 case ppc_stub_long_branch:
13020 case ppc_stub_long_branch_r2off:
13021 case ppc_stub_long_branch_notoc:
13022 case ppc_stub_long_branch_both:
13023 if (stub_type == ppc_stub_long_branch)
13024 continue;
13025 else if (stub_type == ppc_stub_long_branch_r2off)
13026 {
13027 if (old_type == ppc_stub_long_branch_notoc)
13028 stub_type = ppc_stub_long_branch_both;
13029 }
13030 else if (stub_type == ppc_stub_long_branch_notoc)
13031 {
13032 if (old_type == ppc_stub_long_branch_r2off)
13033 stub_type = ppc_stub_long_branch_both;
13034 }
13035 else
13036 abort ();
13037 break;
13038 }
13039 if (old_type < stub_type)
794e51c0 13040 stub_entry->stub_type = stub_type;
721956f4
AM
13041 continue;
13042 }
13043
25f53a85 13044 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13045 if (stub_entry == NULL)
13046 {
13047 free (stub_name);
6cdc0ccc
AM
13048 error_ret_free_internal:
13049 if (elf_section_data (section)->relocs == NULL)
13050 free (internal_relocs);
13051 error_ret_free_local:
13052 if (local_syms != NULL
13053 && (symtab_hdr->contents
13054 != (unsigned char *) local_syms))
13055 free (local_syms);
b34976b6 13056 return FALSE;
721956f4
AM
13057 }
13058
ad8e1ba5 13059 stub_entry->stub_type = stub_type;
05d0e962
AM
13060 if (stub_type >= ppc_stub_plt_call
13061 && stub_type <= ppc_stub_plt_call_both)
e054468f 13062 {
05d0e962
AM
13063 stub_entry->target_value = sym_value;
13064 stub_entry->target_section = sym_sec;
e054468f
AM
13065 }
13066 else
13067 {
05d0e962
AM
13068 stub_entry->target_value = code_value;
13069 stub_entry->target_section = code_sec;
e054468f 13070 }
721956f4 13071 stub_entry->h = hash;
e054468f 13072 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13073 stub_entry->symtype
13074 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13075 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 13076
3d58e1fc
AM
13077 if (hash != NULL
13078 && (hash->elf.root.type == bfd_link_hash_defined
13079 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 13080 htab->stub_globals += 1;
721956f4
AM
13081 }
13082
13083 /* We're done with the internal relocs, free them. */
6cdc0ccc 13084 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13085 free (internal_relocs);
721956f4 13086 }
6cdc0ccc
AM
13087
13088 if (local_syms != NULL
13089 && symtab_hdr->contents != (unsigned char *) local_syms)
13090 {
13091 if (!info->keep_memory)
13092 free (local_syms);
13093 else
13094 symtab_hdr->contents = (unsigned char *) local_syms;
13095 }
721956f4
AM
13096 }
13097
5c3dead3 13098 /* We may have added some stubs. Find out the new size of the
721956f4 13099 stub sections. */
d4aaa2a0 13100 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13101 {
13102 group->lr_restore = 0;
13103 group->eh_size = 0;
13104 if (group->stub_sec != NULL)
13105 {
13106 asection *stub_sec = group->stub_sec;
13107
13108 if (htab->stub_iteration <= STUB_SHRINK_ITER
13109 || stub_sec->rawsize < stub_sec->size)
13110 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13111 stub_sec->rawsize = stub_sec->size;
13112 stub_sec->size = 0;
13113 stub_sec->reloc_count = 0;
13114 stub_sec->flags &= ~SEC_RELOC;
13115 }
13116 }
eea6121a 13117
ba21f564
AM
13118 if (htab->stub_iteration <= STUB_SHRINK_ITER
13119 || htab->brlt->rawsize < htab->brlt->size)
13120 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13121 htab->brlt->size = 0;
84f5d08e
AM
13122 htab->brlt->reloc_count = 0;
13123 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13124 if (htab->relbrlt != NULL)
eea6121a 13125 htab->relbrlt->size = 0;
721956f4 13126
63bc6f6c 13127 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13128
a4b6fadd
AM
13129 for (group = htab->group; group != NULL; group = group->next)
13130 if (group->needs_save_res)
13131 group->stub_sec->size += htab->sfpr->size;
13132
176a0d42
AM
13133 if (info->emitrelocations
13134 && htab->glink != NULL && htab->glink->size != 0)
13135 {
13136 htab->glink->reloc_count = 1;
13137 htab->glink->flags |= SEC_RELOC;
13138 }
13139
58d180e8
AM
13140 if (htab->glink_eh_frame != NULL
13141 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13142 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13143 {
2e0ce1c8 13144 size_t size = 0, align = 4;
58d180e8 13145
d4aaa2a0 13146 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13147 if (group->eh_size != 0)
13148 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 13149 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13150 size += (24 + align - 1) & -align;
58d180e8 13151 if (size != 0)
2e0ce1c8
AM
13152 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13153 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13154 size = (size + align - 1) & -align;
58d180e8
AM
13155 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13156 htab->glink_eh_frame->size = size;
13157 }
13158
e7d1c40c 13159 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13160 for (group = htab->group; group != NULL; group = group->next)
13161 if (group->stub_sec != NULL)
691d2e9a
AM
13162 {
13163 int align = abs (htab->params->plt_stub_align);
13164 group->stub_sec->size
13165 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13166 }
d4aaa2a0
AM
13167
13168 for (group = htab->group; group != NULL; group = group->next)
13169 if (group->stub_sec != NULL
13170 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13171 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13172 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13173 break;
13174
d4aaa2a0 13175 if (group == NULL
ba21f564
AM
13176 && (htab->brlt->rawsize == htab->brlt->size
13177 || (htab->stub_iteration > STUB_SHRINK_ITER
13178 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
13179 && (htab->glink_eh_frame == NULL
13180 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
13181 break;
13182
721956f4 13183 /* Ask the linker to do its stuff. */
e7d1c40c 13184 (*htab->params->layout_sections_again) ();
721956f4
AM
13185 }
13186
da44f4e5
AM
13187 if (htab->glink_eh_frame != NULL
13188 && htab->glink_eh_frame->size != 0)
13189 {
13190 bfd_vma val;
13191 bfd_byte *p, *last_fde;
13192 size_t last_fde_len, size, align, pad;
d4aaa2a0 13193 struct map_stub *group;
da44f4e5 13194
df136d64
AM
13195 /* It is necessary to at least have a rough outline of the
13196 linker generated CIEs and FDEs written before
13197 bfd_elf_discard_info is run, in order for these FDEs to be
13198 indexed in .eh_frame_hdr. */
da44f4e5
AM
13199 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13200 if (p == NULL)
13201 return FALSE;
13202 htab->glink_eh_frame->contents = p;
13203 last_fde = p;
2e0ce1c8 13204 align = 4;
da44f4e5
AM
13205
13206 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13207 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13208 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13209 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13210 p += last_fde_len + 4;
da44f4e5 13211
d4aaa2a0 13212 for (group = htab->group; group != NULL; group = group->next)
df136d64 13213 if (group->eh_size != 0)
da44f4e5 13214 {
df136d64 13215 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 13216 last_fde = p;
df136d64 13217 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 13218 /* FDE length. */
2e0ce1c8 13219 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13220 p += 4;
13221 /* CIE pointer. */
13222 val = p - htab->glink_eh_frame->contents;
13223 bfd_put_32 (htab->elf.dynobj, val, p);
13224 p += 4;
13225 /* Offset to stub section, written later. */
13226 p += 4;
13227 /* stub section size. */
d4aaa2a0 13228 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13229 p += 4;
13230 /* Augmentation. */
13231 p += 1;
df136d64
AM
13232 /* Make sure we don't have all nops. This is enough for
13233 elf-eh-frame.c to detect the last non-nop opcode. */
13234 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 13235 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13236 }
13237 if (htab->glink != NULL && htab->glink->size != 0)
13238 {
13239 last_fde = p;
2e0ce1c8 13240 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13241 /* FDE length. */
2e0ce1c8 13242 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13243 p += 4;
13244 /* CIE pointer. */
13245 val = p - htab->glink_eh_frame->contents;
13246 bfd_put_32 (htab->elf.dynobj, val, p);
13247 p += 4;
13248 /* Offset to .glink, written later. */
13249 p += 4;
13250 /* .glink size. */
13251 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13252 p += 4;
13253 /* Augmentation. */
13254 p += 1;
13255
13256 *p++ = DW_CFA_advance_loc + 1;
13257 *p++ = DW_CFA_register;
13258 *p++ = 65;
9f08fa5c 13259 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 13260 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
13261 *p++ = DW_CFA_restore_extended;
13262 *p++ = 65;
2e0ce1c8 13263 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13264 }
13265 /* Subsume any padding into the last FDE if user .eh_frame
13266 sections are aligned more than glink_eh_frame. Otherwise any
13267 zero padding will be seen as a terminator. */
2e0ce1c8 13268 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13269 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13270 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13271 htab->glink_eh_frame->size = size + pad;
13272 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13273 }
13274
d969d15f
AM
13275 maybe_strip_output (info, htab->brlt);
13276 if (htab->glink_eh_frame != NULL)
13277 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13278
b34976b6 13279 return TRUE;
721956f4
AM
13280}
13281
13282/* Called after we have determined section placement. If sections
805fc799 13283 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13284
805fc799 13285bfd_vma
1c865ab2 13286ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13287{
805fc799 13288 asection *s;
a27e685f 13289 bfd_vma TOCstart, adjust;
721956f4 13290
43417696
AM
13291 if (info != NULL)
13292 {
13293 struct elf_link_hash_entry *h;
13294 struct elf_link_hash_table *htab = elf_hash_table (info);
13295
13296 if (is_elf_hash_table (htab)
13297 && htab->hgot != NULL)
13298 h = htab->hgot;
13299 else
13300 {
13301 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13302 if (is_elf_hash_table (htab))
13303 htab->hgot = h;
13304 }
13305 if (h != NULL
13306 && h->root.type == bfd_link_hash_defined
13307 && !h->root.linker_def
13308 && (!is_elf_hash_table (htab)
13309 || h->def_regular))
13310 {
13311 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13312 + h->root.u.def.section->output_offset
13313 + h->root.u.def.section->output_section->vma);
13314 _bfd_set_gp_value (obfd, TOCstart);
13315 return TOCstart;
13316 }
13317 }
13318
805fc799
AM
13319 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13320 order. The TOC starts where the first of these sections starts. */
13321 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13322 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13323 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13324 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13325 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13326 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13327 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13328 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13329 {
13330 /* This may happen for
13331 o references to TOC base (SYM@toc / TOC[tc0]) without a
13332 .toc directive
13333 o bad linker script
13334 o --gc-sections and empty TOC sections
13335
13336 FIXME: Warn user? */
13337
13338 /* Look for a likely section. We probably won't even be
13339 using TOCstart. */
13340 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13341 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13342 | SEC_EXCLUDE))
805fc799
AM
13343 == (SEC_ALLOC | SEC_SMALL_DATA))
13344 break;
721956f4 13345 if (s == NULL)
805fc799 13346 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13347 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13348 == (SEC_ALLOC | SEC_SMALL_DATA))
13349 break;
721956f4 13350 if (s == NULL)
805fc799 13351 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13352 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13353 == SEC_ALLOC)
805fc799 13354 break;
721956f4 13355 if (s == NULL)
805fc799 13356 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13357 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
13358 break;
13359 }
721956f4 13360
805fc799
AM
13361 TOCstart = 0;
13362 if (s != NULL)
13363 TOCstart = s->output_section->vma + s->output_offset;
721956f4 13364
a27e685f
AM
13365 /* Force alignment. */
13366 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13367 TOCstart -= adjust;
1c865ab2
AM
13368 _bfd_set_gp_value (obfd, TOCstart);
13369
810d4e75 13370 if (info != NULL && s != NULL)
1c865ab2
AM
13371 {
13372 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13373
810d4e75
AM
13374 if (htab != NULL)
13375 {
13376 if (htab->elf.hgot != NULL)
13377 {
a27e685f 13378 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
13379 htab->elf.hgot->root.u.def.section = s;
13380 }
13381 }
13382 else
1c865ab2 13383 {
810d4e75
AM
13384 struct bfd_link_hash_entry *bh = NULL;
13385 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
13386 s, TOC_BASE_OFF - adjust,
13387 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
13388 }
13389 }
805fc799 13390 return TOCstart;
721956f4
AM
13391}
13392
a345bc8d 13393/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 13394 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
13395
13396static bfd_boolean
49c09209 13397build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
13398{
13399 struct bfd_link_info *info;
13400 struct ppc_link_hash_table *htab;
49c09209 13401 struct plt_entry *ent;
a345bc8d
AM
13402 asection *s;
13403
13404 if (h->root.type == bfd_link_hash_indirect)
13405 return TRUE;
13406
49c09209
AM
13407 info = inf;
13408 htab = ppc_hash_table (info);
13409 if (htab == NULL)
13410 return FALSE;
13411
13412 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13413 if (ent->plt.offset != (bfd_vma) -1)
13414 {
13415 /* This symbol has an entry in the procedure linkage
13416 table. Set it up. */
13417 Elf_Internal_Rela rela;
2d7ad24e 13418 asection *plt, *relplt;
49c09209
AM
13419 bfd_byte *loc;
13420
13421 if (!htab->elf.dynamic_sections_created
13422 || h->dynindx == -1)
13423 {
13424 if (!(h->def_regular
13425 && (h->root.type == bfd_link_hash_defined
13426 || h->root.type == bfd_link_hash_defweak)))
13427 continue;
2d7ad24e
AM
13428 if (h->type == STT_GNU_IFUNC)
13429 {
13430 plt = htab->elf.iplt;
13431 relplt = htab->elf.irelplt;
13432 htab->local_ifunc_resolver = 1;
13433 if (htab->opd_abi)
13434 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13435 else
13436 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13437 }
49c09209 13438 else
2d7ad24e
AM
13439 {
13440 plt = htab->pltlocal;
13441 if (bfd_link_pic (info))
13442 {
13443 relplt = htab->relpltlocal;
13444 if (htab->opd_abi)
13445 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13446 else
13447 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13448 }
13449 else
13450 relplt = NULL;
13451 }
49c09209
AM
13452 rela.r_addend = (h->root.u.def.value
13453 + h->root.u.def.section->output_offset
13454 + h->root.u.def.section->output_section->vma
13455 + ent->addend);
2d7ad24e
AM
13456
13457 if (relplt == NULL)
13458 {
13459 loc = plt->contents + ent->plt.offset;
13460 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13461 if (htab->opd_abi)
13462 {
13463 bfd_vma toc = elf_gp (info->output_bfd);
13464 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13465 bfd_put_64 (info->output_bfd, toc, loc + 8);
13466 }
13467 }
13468 else
13469 {
13470 rela.r_offset = (plt->output_section->vma
13471 + plt->output_offset
13472 + ent->plt.offset);
13473 loc = relplt->contents + (relplt->reloc_count++
13474 * sizeof (Elf64_External_Rela));
13475 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13476 }
49c09209
AM
13477 }
13478 else
13479 {
13480 rela.r_offset = (htab->elf.splt->output_section->vma
13481 + htab->elf.splt->output_offset
13482 + ent->plt.offset);
13483 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13484 rela.r_addend = ent->addend;
13485 loc = (htab->elf.srelplt->contents
13486 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13487 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13488 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13489 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 13490 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 13491 }
49c09209
AM
13492 }
13493
a345bc8d
AM
13494 if (!h->pointer_equality_needed)
13495 return TRUE;
13496
13497 if (h->def_regular)
13498 return TRUE;
13499
9e390558 13500 s = htab->global_entry;
49c09209
AM
13501 if (s == NULL || s->size == 0)
13502 return TRUE;
13503
13504 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13505 if (ent->plt.offset != (bfd_vma) -1
13506 && ent->addend == 0)
a345bc8d
AM
13507 {
13508 bfd_byte *p;
13509 asection *plt;
13510 bfd_vma off;
13511
a345bc8d 13512 p = s->contents + h->root.u.def.value;
33e44f2e 13513 plt = htab->elf.splt;
a345bc8d
AM
13514 if (!htab->elf.dynamic_sections_created
13515 || h->dynindx == -1)
2d7ad24e
AM
13516 {
13517 if (h->type == STT_GNU_IFUNC)
13518 plt = htab->elf.iplt;
13519 else
13520 plt = htab->pltlocal;
13521 }
49c09209 13522 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
13523 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13524
13525 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13526 {
13527 info->callbacks->einfo
c1c8c1ef 13528 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13529 h->root.root.string);
13530 bfd_set_error (bfd_error_bad_value);
13531 htab->stub_error = TRUE;
13532 }
13533
7341d5e2
AM
13534 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13535 if (htab->params->emit_stub_syms)
13536 {
13537 size_t len = strlen (h->root.root.string);
13538 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13539
13540 if (name == NULL)
13541 return FALSE;
13542
13543 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13544 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13545 if (h == NULL)
13546 return FALSE;
13547 if (h->root.type == bfd_link_hash_new)
13548 {
13549 h->root.type = bfd_link_hash_defined;
13550 h->root.u.def.section = s;
13551 h->root.u.def.value = p - s->contents;
13552 h->ref_regular = 1;
13553 h->def_regular = 1;
13554 h->ref_regular_nonweak = 1;
13555 h->forced_local = 1;
13556 h->non_elf = 0;
2ec55de3 13557 h->root.linker_def = 1;
7341d5e2
AM
13558 }
13559 }
13560
a345bc8d
AM
13561 if (PPC_HA (off) != 0)
13562 {
13563 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13564 p += 4;
13565 }
13566 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13567 p += 4;
13568 bfd_put_32 (s->owner, MTCTR_R12, p);
13569 p += 4;
407aa07c 13570 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13571 break;
13572 }
13573 return TRUE;
13574}
13575
49c09209
AM
13576/* Write PLT relocs for locals. */
13577
13578static bfd_boolean
13579write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13580{
13581 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13582 bfd *ibfd;
13583
13584 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13585 {
13586 struct got_entry **lgot_ents, **end_lgot_ents;
13587 struct plt_entry **local_plt, **lplt, **end_local_plt;
13588 Elf_Internal_Shdr *symtab_hdr;
13589 bfd_size_type locsymcount;
13590 Elf_Internal_Sym *local_syms = NULL;
13591 struct plt_entry *ent;
13592
13593 if (!is_ppc64_elf (ibfd))
13594 continue;
13595
13596 lgot_ents = elf_local_got_ents (ibfd);
13597 if (!lgot_ents)
13598 continue;
13599
13600 symtab_hdr = &elf_symtab_hdr (ibfd);
13601 locsymcount = symtab_hdr->sh_info;
13602 end_lgot_ents = lgot_ents + locsymcount;
13603 local_plt = (struct plt_entry **) end_lgot_ents;
13604 end_local_plt = local_plt + locsymcount;
13605 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13606 for (ent = *lplt; ent != NULL; ent = ent->next)
13607 if (ent->plt.offset != (bfd_vma) -1)
13608 {
13609 Elf_Internal_Sym *sym;
13610 asection *sym_sec;
13611 asection *plt, *relplt;
13612 bfd_byte *loc;
13613 bfd_vma val;
49c09209
AM
13614
13615 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13616 lplt - local_plt, ibfd))
13617 {
13618 if (local_syms != NULL
13619 && symtab_hdr->contents != (unsigned char *) local_syms)
13620 free (local_syms);
13621 return FALSE;
13622 }
13623
13624 val = sym->st_value + ent->addend;
ba85c15d
AM
13625 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13626 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
49c09209
AM
13627 if (sym_sec != NULL && sym_sec->output_section != NULL)
13628 val += sym_sec->output_offset + sym_sec->output_section->vma;
13629
2d7ad24e
AM
13630 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13631 {
13632 htab->local_ifunc_resolver = 1;
13633 plt = htab->elf.iplt;
13634 relplt = htab->elf.irelplt;
13635 }
13636 else
13637 {
13638 plt = htab->pltlocal;
13639 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13640 }
49c09209 13641
2d7ad24e
AM
13642 if (relplt == NULL)
13643 {
13644 loc = plt->contents + ent->plt.offset;
13645 bfd_put_64 (info->output_bfd, val, loc);
13646 if (htab->opd_abi)
13647 {
13648 bfd_vma toc = elf_gp (ibfd);
13649 bfd_put_64 (info->output_bfd, toc, loc + 8);
13650 }
13651 }
49c09209 13652 else
2d7ad24e
AM
13653 {
13654 Elf_Internal_Rela rela;
13655 rela.r_offset = (ent->plt.offset
13656 + plt->output_offset
13657 + plt->output_section->vma);
13658 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13659 {
13660 if (htab->opd_abi)
13661 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13662 else
13663 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13664 }
13665 else
13666 {
13667 if (htab->opd_abi)
13668 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13669 else
13670 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13671 }
13672 rela.r_addend = val;
13673 loc = relplt->contents + (relplt->reloc_count++
13674 * sizeof (Elf64_External_Rela));
13675 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13676 }
49c09209
AM
13677 }
13678
13679 if (local_syms != NULL
13680 && symtab_hdr->contents != (unsigned char *) local_syms)
13681 {
13682 if (!info->keep_memory)
13683 free (local_syms);
13684 else
13685 symtab_hdr->contents = (unsigned char *) local_syms;
13686 }
13687 }
13688 return TRUE;
13689}
13690
721956f4
AM
13691/* Build all the stubs associated with the current output file.
13692 The stubs are kept in a hash table attached to the main linker
13693 hash table. This function is called via gldelf64ppc_finish. */
13694
b34976b6 13695bfd_boolean
e7d1c40c 13696ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13697 char **stats)
5d1634d7
AM
13698{
13699 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13700 struct map_stub *group;
721956f4 13701 asection *stub_sec;
5d1634d7 13702 bfd_byte *p;
e717da7e 13703 int stub_sec_count = 0;
5d1634d7 13704
4dfe6ac6
NC
13705 if (htab == NULL)
13706 return FALSE;
13707
eea6121a 13708 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 13709 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13710 {
13711 group->eh_size = 0;
13712 group->lr_restore = 0;
13713 if ((stub_sec = group->stub_sec) != NULL
13714 && stub_sec->size != 0)
13715 {
13716 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13717 stub_sec->size);
13718 if (stub_sec->contents == NULL)
13719 return FALSE;
13720 stub_sec->size = 0;
13721 }
13722 }
5d1634d7 13723
23eb7e01 13724 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13725 {
9f951329 13726 unsigned int indx;
ad8e1ba5 13727 bfd_vma plt0;
9f951329 13728
721956f4 13729 /* Build the .glink plt call stub. */
e7d1c40c 13730 if (htab->params->emit_stub_syms)
97b639ba
AM
13731 {
13732 struct elf_link_hash_entry *h;
468392fb
AM
13733 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13734 TRUE, FALSE, FALSE);
97b639ba
AM
13735 if (h == NULL)
13736 return FALSE;
13737 if (h->root.type == bfd_link_hash_new)
13738 {
13739 h->root.type = bfd_link_hash_defined;
13740 h->root.u.def.section = htab->glink;
ee4bf8d2 13741 h->root.u.def.value = 8;
f5385ebf
AM
13742 h->ref_regular = 1;
13743 h->def_regular = 1;
13744 h->ref_regular_nonweak = 1;
13745 h->forced_local = 1;
13746 h->non_elf = 0;
2ec55de3 13747 h->root.linker_def = 1;
97b639ba
AM
13748 }
13749 }
33e44f2e
AM
13750 plt0 = (htab->elf.splt->output_section->vma
13751 + htab->elf.splt->output_offset
13752 - 16);
176a0d42
AM
13753 if (info->emitrelocations)
13754 {
13755 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13756 if (r == NULL)
13757 return FALSE;
13758 r->r_offset = (htab->glink->output_offset
13759 + htab->glink->output_section->vma);
13760 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13761 r->r_addend = plt0;
13762 }
4ce794b7 13763 p = htab->glink->contents;
176a0d42 13764 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13765 bfd_put_64 (htab->glink->owner, plt0, p);
13766 p += 8;
b9e5796b
AM
13767 if (htab->opd_abi)
13768 {
13769 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13770 p += 4;
13771 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13772 p += 4;
13773 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13774 p += 4;
13775 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13776 p += 4;
13777 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13778 p += 4;
13779 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13780 p += 4;
13781 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13782 p += 4;
13783 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13784 p += 4;
13785 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13786 p += 4;
13787 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13788 p += 4;
13789 }
13790 else
13791 {
13792 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13793 p += 4;
13794 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13795 p += 4;
13796 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13797 p += 4;
f378ab09
AM
13798 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13799 p += 4;
b9e5796b
AM
13800 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13801 p += 4;
13802 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13803 p += 4;
13804 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13805 p += 4;
13806 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13807 p += 4;
13808 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13809 p += 4;
13810 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13811 p += 4;
13812 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13813 p += 4;
13814 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13815 p += 4;
13816 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13817 p += 4;
13818 }
407aa07c
AM
13819 bfd_put_32 (htab->glink->owner, BCTR, p);
13820 p += 4;
c75bc4f7 13821 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13822
9f951329
AM
13823 /* Build the .glink lazy link call stubs. */
13824 indx = 0;
9e390558 13825 while (p < htab->glink->contents + htab->glink->size)
9f951329 13826 {
b9e5796b 13827 if (htab->opd_abi)
9f951329 13828 {
b9e5796b
AM
13829 if (indx < 0x8000)
13830 {
13831 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13832 p += 4;
13833 }
13834 else
13835 {
13836 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13837 p += 4;
13838 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13839 p);
13840 p += 4;
13841 }
9f951329 13842 }
4ce794b7 13843 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13844 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13845 indx++;
9f951329
AM
13846 p += 4;
13847 }
5d1634d7 13848 }
5d1634d7 13849
49c09209
AM
13850 /* Build .glink global entry stubs, and PLT relocs for globals. */
13851 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13852
13853 if (!write_plt_relocs_for_local_syms (info))
13854 return FALSE;
9e390558 13855
7341d5e2 13856 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13857 {
4ce794b7 13858 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13859 htab->brlt->size);
4ce794b7 13860 if (htab->brlt->contents == NULL)
b34976b6 13861 return FALSE;
721956f4 13862 }
ee75fd95 13863 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13864 {
13865 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13866 htab->relbrlt->size);
63bc6f6c
AM
13867 if (htab->relbrlt->contents == NULL)
13868 return FALSE;
13869 }
5d1634d7 13870
721956f4
AM
13871 /* Build the stubs as directed by the stub hash table. */
13872 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13873
a4b6fadd
AM
13874 for (group = htab->group; group != NULL; group = group->next)
13875 if (group->needs_save_res)
7dda8d3c 13876 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 13877
aa8a7074
AM
13878 if (htab->relbrlt != NULL)
13879 htab->relbrlt->reloc_count = 0;
13880
e7d1c40c 13881 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13882 for (group = htab->group; group != NULL; group = group->next)
13883 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13884 {
13885 int align = abs (htab->params->plt_stub_align);
13886 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13887 }
794e51c0 13888
7dda8d3c
AM
13889 for (group = htab->group; group != NULL; group = group->next)
13890 if (group->needs_save_res)
13891 {
13892 stub_sec = group->stub_sec;
13893 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13894 htab->sfpr->contents, htab->sfpr->size);
13895 if (htab->params->emit_stub_syms)
13896 {
13897 unsigned int i;
13898
13899 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13900 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13901 return FALSE;
13902 }
13903 }
13904
df136d64
AM
13905 if (htab->glink_eh_frame != NULL
13906 && htab->glink_eh_frame->size != 0)
13907 {
13908 bfd_vma val;
13909 size_t align = 4;
13910
13911 p = htab->glink_eh_frame->contents;
13912 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13913
13914 for (group = htab->group; group != NULL; group = group->next)
13915 if (group->eh_size != 0)
13916 {
13917 /* Offset to stub section. */
13918 val = (group->stub_sec->output_section->vma
13919 + group->stub_sec->output_offset);
13920 val -= (htab->glink_eh_frame->output_section->vma
13921 + htab->glink_eh_frame->output_offset
13922 + (p + 8 - htab->glink_eh_frame->contents));
13923 if (val + 0x80000000 > 0xffffffff)
13924 {
13925 _bfd_error_handler
13926 (_("%s offset too large for .eh_frame sdata4 encoding"),
13927 group->stub_sec->name);
13928 return FALSE;
13929 }
13930 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13931 p += (group->eh_size + 17 + 3) & -4;
13932 }
13933 if (htab->glink != NULL && htab->glink->size != 0)
13934 {
13935 /* Offset to .glink. */
13936 val = (htab->glink->output_section->vma
13937 + htab->glink->output_offset
13938 + 8);
13939 val -= (htab->glink_eh_frame->output_section->vma
13940 + htab->glink_eh_frame->output_offset
13941 + (p + 8 - htab->glink_eh_frame->contents));
13942 if (val + 0x80000000 > 0xffffffff)
13943 {
13944 _bfd_error_handler
13945 (_("%s offset too large for .eh_frame sdata4 encoding"),
13946 htab->glink->name);
13947 return FALSE;
13948 }
13949 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13950 p += (24 + align - 1) & -align;
13951 }
13952 }
13953
d4aaa2a0
AM
13954 for (group = htab->group; group != NULL; group = group->next)
13955 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
13956 {
13957 stub_sec_count += 1;
c9301e31
AM
13958 if (stub_sec->rawsize != stub_sec->size
13959 && (htab->stub_iteration <= STUB_SHRINK_ITER
13960 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13961 break;
13962 }
5d1634d7 13963
25516cc5 13964 if (group != NULL)
5d1634d7 13965 {
b34976b6 13966 htab->stub_error = TRUE;
cf97bcb0 13967 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 13968 }
721956f4 13969
d2a300cf
AM
13970 if (htab->stub_error)
13971 return FALSE;
13972
13973 if (stats != NULL)
13974 {
db84fff3 13975 size_t len;
d2a300cf
AM
13976 *stats = bfd_malloc (500);
13977 if (*stats == NULL)
13978 return FALSE;
13979
db84fff3
AM
13980 len = sprintf (*stats,
13981 ngettext ("linker stubs in %u group\n",
13982 "linker stubs in %u groups\n",
13983 stub_sec_count),
13984 stub_sec_count);
05d0e962
AM
13985 sprintf (*stats + len, _(" branch %lu\n"
13986 " branch toc adj %lu\n"
13987 " branch notoc %lu\n"
13988 " branch both %lu\n"
13989 " long branch %lu\n"
13990 " long toc adj %lu\n"
13991 " long notoc %lu\n"
13992 " long both %lu\n"
13993 " plt call %lu\n"
13994 " plt call save %lu\n"
13995 " plt call notoc %lu\n"
13996 " plt call both %lu\n"
13997 " global entry %lu"),
4ce794b7
AM
13998 htab->stub_count[ppc_stub_long_branch - 1],
13999 htab->stub_count[ppc_stub_long_branch_r2off - 1],
05d0e962
AM
14000 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14001 htab->stub_count[ppc_stub_long_branch_both - 1],
4ce794b7
AM
14002 htab->stub_count[ppc_stub_plt_branch - 1],
14003 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
05d0e962
AM
14004 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14005 htab->stub_count[ppc_stub_plt_branch_both - 1],
794e51c0 14006 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2 14007 htab->stub_count[ppc_stub_plt_call_r2save - 1],
05d0e962
AM
14008 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14009 htab->stub_count[ppc_stub_plt_call_both - 1],
7341d5e2 14010 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
14011 }
14012 return TRUE;
5bd4f169
AM
14013}
14014
60124e18
AM
14015/* What to do when ld finds relocations against symbols defined in
14016 discarded sections. */
14017
14018static unsigned int
14019ppc64_elf_action_discarded (asection *sec)
14020{
14021 if (strcmp (".opd", sec->name) == 0)
14022 return 0;
14023
14024 if (strcmp (".toc", sec->name) == 0)
14025 return 0;
14026
bce50a28
JJ
14027 if (strcmp (".toc1", sec->name) == 0)
14028 return 0;
14029
60124e18
AM
14030 return _bfd_elf_default_action_discarded (sec);
14031}
14032
5bd4f169
AM
14033/* The RELOCATE_SECTION function is called by the ELF backend linker
14034 to handle the relocations for a section.
14035
14036 The relocs are always passed as Rela structures; if the section
14037 actually uses Rel structures, the r_addend field will always be
14038 zero.
14039
14040 This function is responsible for adjust the section contents as
14041 necessary, and (if using Rela relocs and generating a
1049f94e 14042 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14043 necessary.
14044
14045 This function does not have to worry about setting the reloc
14046 address or the reloc symbol index.
14047
14048 LOCAL_SYMS is a pointer to the swapped in local symbols.
14049
14050 LOCAL_SECTIONS is an array giving the section in the input file
14051 corresponding to the st_shndx field of each local symbol.
14052
14053 The global hash table entry for the global symbols can be found
14054 via elf_sym_hashes (input_bfd).
14055
1049f94e 14056 When generating relocatable output, this function must handle
5bd4f169
AM
14057 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14058 going to be the section symbol corresponding to the output
14059 section, which means that the addend must be adjusted
14060 accordingly. */
14061
b34976b6 14062static bfd_boolean
4ce794b7
AM
14063ppc64_elf_relocate_section (bfd *output_bfd,
14064 struct bfd_link_info *info,
14065 bfd *input_bfd,
14066 asection *input_section,
14067 bfd_byte *contents,
14068 Elf_Internal_Rela *relocs,
14069 Elf_Internal_Sym *local_syms,
14070 asection **local_sections)
5bd4f169 14071{
65f38f15 14072 struct ppc_link_hash_table *htab;
5bd4f169
AM
14073 Elf_Internal_Shdr *symtab_hdr;
14074 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14075 Elf_Internal_Rela *rel;
c316a17c 14076 Elf_Internal_Rela *wrel;
5bd4f169 14077 Elf_Internal_Rela *relend;
411e1bfb
AM
14078 Elf_Internal_Rela outrel;
14079 bfd_byte *loc;
411e1bfb 14080 struct got_entry **local_got_ents;
5bd4f169 14081 bfd_vma TOCstart;
b34976b6
AM
14082 bfd_boolean ret = TRUE;
14083 bfd_boolean is_opd;
794e51c0
AM
14084 /* Assume 'at' branch hints. */
14085 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 14086 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14087
65f38f15 14088 /* Initialize howto table if needed. */
5bd4f169 14089 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14090 ppc_howto_init ();
14091
65f38f15 14092 htab = ppc_hash_table (info);
4dfe6ac6
NC
14093 if (htab == NULL)
14094 return FALSE;
ee75fd95
AM
14095
14096 /* Don't relocate stub sections. */
e7d1c40c 14097 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
14098 return TRUE;
14099
7af5d5c4
AM
14100 if (!is_ppc64_elf (input_bfd))
14101 {
14102 bfd_set_error (bfd_error_wrong_format);
14103 return FALSE;
14104 }
0ffa91dd 14105
411e1bfb 14106 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14107 TOCstart = elf_gp (output_bfd);
0ffa91dd 14108 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14109 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14110 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14111
c316a17c 14112 rel = wrel = relocs;
5bd4f169 14113 relend = relocs + input_section->reloc_count;
c316a17c 14114 for (; rel < relend; wrel++, rel++)
5bd4f169 14115 {
04c9666a 14116 enum elf_ppc64_reloc_type r_type;
31c76678 14117 bfd_vma addend;
5bd4f169
AM
14118 bfd_reloc_status_type r;
14119 Elf_Internal_Sym *sym;
14120 asection *sec;
039b3fef
AM
14121 struct elf_link_hash_entry *h_elf;
14122 struct ppc_link_hash_entry *h;
14123 struct ppc_link_hash_entry *fdh;
5bd4f169 14124 const char *sym_name;
0d4792f7 14125 unsigned long r_symndx, toc_symndx;
3a71aa26 14126 bfd_vma toc_addend;
f961d9dd
AM
14127 unsigned char tls_mask, tls_gd, tls_type;
14128 unsigned char sym_type;
5bd4f169 14129 bfd_vma relocation;
23cedd1d 14130 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 14131 bfd_boolean warned;
bc30df16 14132 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14133 unsigned int insn;
e11840f9 14134 unsigned int mask;
721956f4
AM
14135 struct ppc_stub_hash_entry *stub_entry;
14136 bfd_vma max_br_offset;
14137 bfd_vma from;
c316a17c 14138 Elf_Internal_Rela orig_rel;
b80eed39
AM
14139 reloc_howto_type *howto;
14140 struct reloc_howto_struct alt_howto;
4a421c53
AM
14141 uint64_t pinsn;
14142 bfd_vma offset;
5bd4f169 14143
c316a17c
AM
14144 again:
14145 orig_rel = *rel;
14146
4ce794b7 14147 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 14148 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
14149
14150 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14151 symbol of the previous ADDR64 reloc. The symbol gives us the
14152 proper TOC base to use. */
14153 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
14154 && wrel != relocs
14155 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 14156 && is_opd)
c316a17c 14157 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 14158
4ce794b7
AM
14159 sym = NULL;
14160 sec = NULL;
039b3fef 14161 h_elf = NULL;
4ce794b7 14162 sym_name = NULL;
b34976b6
AM
14163 unresolved_reloc = FALSE;
14164 warned = FALSE;
65f38f15 14165
0b13192e 14166 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
14167 {
14168 /* It's a local symbol. */
74f0fb50 14169 struct _opd_sec_data *opd;
4025353c 14170
5bd4f169
AM
14171 sym = local_syms + r_symndx;
14172 sec = local_sections[r_symndx];
26c61ae5 14173 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 14174 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 14175 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
14176 opd = get_opd_info (sec);
14177 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 14178 {
51aecdc5
AM
14179 long adjust = opd->adjust[OPD_NDX (sym->st_value
14180 + rel->r_addend)];
4025353c
AM
14181 if (adjust == -1)
14182 relocation = 0;
14183 else
4cc603a5
AM
14184 {
14185 /* If this is a relocation against the opd section sym
14186 and we have edited .opd, adjust the reloc addend so
14187 that ld -r and ld --emit-relocs output is correct.
14188 If it is a reloc against some other .opd symbol,
14189 then the symbol value will be adjusted later. */
14190 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14191 rel->r_addend += adjust;
14192 else
14193 relocation += adjust;
14194 }
1e2f5b6e 14195 }
5bd4f169
AM
14196 }
14197 else
14198 {
62d887d4
L
14199 bfd_boolean ignored;
14200
b2a8e766
AM
14201 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14202 r_symndx, symtab_hdr, sym_hashes,
039b3fef 14203 h_elf, sec, relocation,
62d887d4 14204 unresolved_reloc, warned, ignored);
039b3fef
AM
14205 sym_name = h_elf->root.root.string;
14206 sym_type = h_elf->type;
b69fdb4e
AM
14207 if (sec != NULL
14208 && sec->owner == output_bfd
14209 && strcmp (sec->name, ".opd") == 0)
14210 {
14211 /* This is a symbol defined in a linker script. All
14212 such are defined in output sections, even those
14213 defined by simple assignment from a symbol defined in
14214 an input section. Transfer the symbol to an
14215 appropriate input .opd section, so that a branch to
14216 this symbol will be mapped to the location specified
14217 by the opd entry. */
14218 struct bfd_link_order *lo;
14219 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14220 if (lo->type == bfd_indirect_link_order)
14221 {
14222 asection *isec = lo->u.indirect.section;
14223 if (h_elf->root.u.def.value >= isec->output_offset
14224 && h_elf->root.u.def.value < (isec->output_offset
14225 + isec->size))
14226 {
14227 h_elf->root.u.def.value -= isec->output_offset;
14228 h_elf->root.u.def.section = isec;
14229 sec = isec;
14230 break;
14231 }
14232 }
14233 }
5bd4f169 14234 }
039b3fef 14235 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 14236
dbaa2011 14237 if (sec != NULL && discarded_section (sec))
c316a17c
AM
14238 {
14239 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14240 input_bfd, input_section,
0930cb30 14241 contents, rel->r_offset);
c316a17c
AM
14242 wrel->r_offset = rel->r_offset;
14243 wrel->r_info = 0;
14244 wrel->r_addend = 0;
14245
14246 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 14247 symbols defined in discarded sections. Not done for
c316a17c
AM
14248 non-debug to preserve relocs in .eh_frame which the
14249 eh_frame editing code expects to be present. */
14250 if (bfd_link_relocatable (info)
14251 && (input_section->flags & SEC_DEBUGGING))
14252 wrel--;
14253
14254 continue;
14255 }
ab96bf03 14256
0e1862bb 14257 if (bfd_link_relocatable (info))
c316a17c 14258 goto copy_reloc;
ab96bf03 14259
f40da81b
AM
14260 if (h != NULL && &h->elf == htab->elf.hgot)
14261 {
6f20ed8a 14262 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
14263 sec = bfd_abs_section_ptr;
14264 unresolved_reloc = FALSE;
14265 }
14266
951fd09b
AM
14267 /* TLS optimizations. Replace instruction sequences and relocs
14268 based on information we collected in tls_optimize. We edit
14269 RELOCS so that --emit-relocs will output something sensible
14270 for the final instruction stream. */
14271 tls_mask = 0;
14272 tls_gd = 0;
0d4792f7 14273 toc_symndx = 0;
727fc41e
AM
14274 if (h != NULL)
14275 tls_mask = h->tls_mask;
14276 else if (local_got_ents != NULL)
411e1bfb 14277 {
e054468f
AM
14278 struct plt_entry **local_plt = (struct plt_entry **)
14279 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 14280 unsigned char *lgot_masks = (unsigned char *)
e054468f 14281 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
14282 tls_mask = lgot_masks[r_symndx];
14283 }
37da22e5 14284 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
14285 && (r_type == R_PPC64_TLS
14286 || r_type == R_PPC64_TLSGD
14287 || r_type == R_PPC64_TLSLD))
14288 {
14289 /* Check for toc tls entries. */
f961d9dd 14290 unsigned char *toc_tls;
0d4792f7 14291
727fc41e
AM
14292 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14293 &local_syms, rel, input_bfd))
14294 return FALSE;
0d4792f7 14295
727fc41e
AM
14296 if (toc_tls)
14297 tls_mask = *toc_tls;
0d4792f7
AM
14298 }
14299
14300 /* Check that tls relocs are used with tls syms, and non-tls
14301 relocs are used with non-tls syms. */
cf35638d 14302 if (r_symndx != STN_UNDEF
0d4792f7
AM
14303 && r_type != R_PPC64_NONE
14304 && (h == NULL
039b3fef
AM
14305 || h->elf.root.type == bfd_link_hash_defined
14306 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
14307 && (IS_PPC64_TLS_RELOC (r_type)
14308 != (sym_type == STT_TLS
14309 || (sym_type == STT_SECTION
14310 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 14311 {
37da22e5 14312 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
14313 && (r_type == R_PPC64_TLS
14314 || r_type == R_PPC64_TLSGD
14315 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
14316 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14317 ;
14318 else
25f53a85 14319 info->callbacks->einfo
1d483afe 14320 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 14321 /* xgettext:c-format */
c1c8c1ef 14322 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 14323 /* xgettext:c-format */
c1c8c1ef 14324 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 14325 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
14326 ppc64_elf_howto_table[r_type]->name,
14327 sym_name);
411e1bfb
AM
14328 }
14329
14330 /* Ensure reloc mapping code below stays sane. */
14331 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14332 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14333 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14334 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14335 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14336 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14337 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14338 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14339 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14340 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14341 abort ();
0d4792f7 14342
411e1bfb
AM
14343 switch (r_type)
14344 {
14345 default:
411e1bfb
AM
14346 break;
14347
ba761f19 14348 case R_PPC64_LO_DS_OPT:
95f0d0d2 14349 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
14350 if ((insn & (0x3f << 26)) != 58u << 26)
14351 abort ();
14352 insn += (14u << 26) - (58u << 26);
95f0d0d2 14353 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
14354 r_type = R_PPC64_TOC16_LO;
14355 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14356 break;
14357
411e1bfb
AM
14358 case R_PPC64_TOC16:
14359 case R_PPC64_TOC16_LO:
14360 case R_PPC64_TOC16_DS:
14361 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
14362 {
14363 /* Check for toc tls entries. */
f961d9dd 14364 unsigned char *toc_tls;
951fd09b 14365 int retval;
411e1bfb 14366
3a71aa26
AM
14367 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14368 &local_syms, rel, input_bfd);
951fd09b 14369 if (retval == 0)
411e1bfb
AM
14370 return FALSE;
14371
14372 if (toc_tls)
14373 {
951fd09b 14374 tls_mask = *toc_tls;
411e1bfb
AM
14375 if (r_type == R_PPC64_TOC16_DS
14376 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 14377 {
37da22e5 14378 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
14379 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14380 goto toctprel;
14381 }
411e1bfb 14382 else
951fd09b
AM
14383 {
14384 /* If we found a GD reloc pair, then we might be
14385 doing a GD->IE transition. */
14386 if (retval == 2)
14387 {
b00a0a86 14388 tls_gd = TLS_GDIE;
37da22e5
AM
14389 if ((tls_mask & TLS_TLS) != 0
14390 && (tls_mask & TLS_GD) == 0)
102890f0 14391 goto tls_ldgd_opt;
951fd09b
AM
14392 }
14393 else if (retval == 3)
14394 {
37da22e5
AM
14395 if ((tls_mask & TLS_TLS) != 0
14396 && (tls_mask & TLS_LD) == 0)
102890f0 14397 goto tls_ldgd_opt;
951fd09b
AM
14398 }
14399 }
411e1bfb
AM
14400 }
14401 }
14402 break;
14403
9d6ded02
AM
14404 case R_PPC64_GOT_TPREL16_HI:
14405 case R_PPC64_GOT_TPREL16_HA:
37da22e5 14406 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
14407 && (tls_mask & TLS_TPREL) == 0)
14408 {
14409 rel->r_offset -= d_offset;
95f0d0d2 14410 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
14411 r_type = R_PPC64_NONE;
14412 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14413 }
14414 break;
14415
411e1bfb
AM
14416 case R_PPC64_GOT_TPREL16_DS:
14417 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 14418 if ((tls_mask & TLS_TLS) != 0
951fd09b 14419 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14420 {
81407a69 14421 toctprel:
95f0d0d2 14422 insn = bfd_get_32 (input_bfd,
c316a17c 14423 contents + rel->r_offset - d_offset);
411e1bfb
AM
14424 insn &= 31 << 21;
14425 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 14426 bfd_put_32 (input_bfd, insn,
c316a17c 14427 contents + rel->r_offset - d_offset);
411e1bfb 14428 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
14429 if (toc_symndx != 0)
14430 {
14431 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14432 rel->r_addend = toc_addend;
0d4792f7
AM
14433 /* We changed the symbol. Start over in order to
14434 get h, sym, sec etc. right. */
c316a17c 14435 goto again;
0d4792f7
AM
14436 }
14437 else
14438 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14439 }
14440 break;
14441
14442 case R_PPC64_TLS:
37da22e5 14443 if ((tls_mask & TLS_TLS) != 0
951fd09b 14444 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14445 {
95f0d0d2 14446 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
14447 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14448 if (insn == 0)
411e1bfb 14449 abort ();
95f0d0d2 14450 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 14451 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
14452 PPC64_TPREL16_LO which is at low-order half-word. */
14453 rel->r_offset += d_offset;
0d4792f7
AM
14454 r_type = R_PPC64_TPREL16_LO;
14455 if (toc_symndx != 0)
14456 {
14457 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14458 rel->r_addend = toc_addend;
0d4792f7
AM
14459 /* We changed the symbol. Start over in order to
14460 get h, sym, sec etc. right. */
c316a17c 14461 goto again;
0d4792f7
AM
14462 }
14463 else
14464 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14465 }
14466 break;
14467
411e1bfb
AM
14468 case R_PPC64_GOT_TLSGD16_HI:
14469 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 14470 tls_gd = TLS_GDIE;
37da22e5 14471 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
14472 goto tls_gdld_hi;
14473 break;
14474
411e1bfb
AM
14475 case R_PPC64_GOT_TLSLD16_HI:
14476 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 14477 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 14478 {
951fd09b
AM
14479 tls_gdld_hi:
14480 if ((tls_mask & tls_gd) != 0)
14481 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14482 + R_PPC64_GOT_TPREL16_DS);
14483 else
411e1bfb 14484 {
4fe5ca5b 14485 rel->r_offset -= d_offset;
95f0d0d2 14486 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 14487 r_type = R_PPC64_NONE;
411e1bfb 14488 }
951fd09b 14489 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14490 }
14491 break;
14492
951fd09b
AM
14493 case R_PPC64_GOT_TLSGD16:
14494 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 14495 tls_gd = TLS_GDIE;
37da22e5 14496 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 14497 goto tls_ldgd_opt;
951fd09b 14498 break;
411e1bfb 14499
951fd09b
AM
14500 case R_PPC64_GOT_TLSLD16:
14501 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 14502 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 14503 {
b9f04fe0 14504 unsigned int insn1, insn2;
102890f0
AM
14505
14506 tls_ldgd_opt:
727fc41e
AM
14507 offset = (bfd_vma) -1;
14508 /* If not using the newer R_PPC64_TLSGD/LD to mark
14509 __tls_get_addr calls, we must trust that the call
14510 stays with its arg setup insns, ie. that the next
14511 reloc is the __tls_get_addr call associated with
14512 the current reloc. Edit both insns. */
14513 if (input_section->has_tls_get_addr_call
14514 && rel + 1 < relend
14515 && branch_reloc_hash_match (input_bfd, rel + 1,
14516 htab->tls_get_addr,
14517 htab->tls_get_addr_fd))
14518 offset = rel[1].r_offset;
b86ac8e3
AM
14519 /* We read the low GOT_TLS (or TOC16) insn because we
14520 need to keep the destination reg. It may be
14521 something other than the usual r3, and moved to r3
14522 before the call by intervening code. */
95f0d0d2 14523 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 14524 contents + rel->r_offset - d_offset);
102890f0 14525 if ((tls_mask & tls_gd) != 0)
411e1bfb 14526 {
102890f0 14527 /* IE */
b86ac8e3 14528 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
14529 insn1 |= 58 << 26; /* ld */
14530 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 14531 if (offset != (bfd_vma) -1)
f58d5a2d 14532 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
14533 if ((tls_mask & TLS_EXPLICIT) == 0)
14534 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14535 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 14536 else
102890f0
AM
14537 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14538 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14539 }
14540 else
14541 {
14542 /* LE */
b86ac8e3
AM
14543 insn1 &= 0x1f << 21;
14544 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
14545 insn2 = 0x38630000; /* addi 3,3,0 */
14546 if (tls_gd == 0)
951fd09b 14547 {
102890f0 14548 /* Was an LD reloc. */
1d483afe
AM
14549 if (toc_symndx)
14550 sec = local_sections[toc_symndx];
14551 for (r_symndx = 0;
14552 r_symndx < symtab_hdr->sh_info;
14553 r_symndx++)
14554 if (local_sections[r_symndx] == sec)
14555 break;
14556 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14557 r_symndx = STN_UNDEF;
102890f0 14558 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14559 if (r_symndx != STN_UNDEF)
1d483afe
AM
14560 rel->r_addend -= (local_syms[r_symndx].st_value
14561 + sec->output_offset
14562 + sec->output_section->vma);
951fd09b 14563 }
102890f0 14564 else if (toc_symndx != 0)
3a71aa26
AM
14565 {
14566 r_symndx = toc_symndx;
14567 rel->r_addend = toc_addend;
14568 }
102890f0
AM
14569 r_type = R_PPC64_TPREL16_HA;
14570 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14571 if (offset != (bfd_vma) -1)
14572 {
14573 rel[1].r_info = ELF64_R_INFO (r_symndx,
14574 R_PPC64_TPREL16_LO);
14575 rel[1].r_offset = offset + d_offset;
14576 rel[1].r_addend = rel->r_addend;
14577 }
102890f0 14578 }
95f0d0d2 14579 bfd_put_32 (input_bfd, insn1,
3a71aa26 14580 contents + rel->r_offset - d_offset);
727fc41e 14581 if (offset != (bfd_vma) -1)
c96e0573
AM
14582 {
14583 bfd_put_32 (input_bfd, insn2, contents + offset);
14584 if (offset + 8 <= input_section->size)
14585 {
14586 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14587 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14588 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14589 }
14590 }
727fc41e
AM
14591 if ((tls_mask & tls_gd) == 0
14592 && (tls_gd == 0 || toc_symndx != 0))
14593 {
14594 /* We changed the symbol. Start over in order
14595 to get h, sym, sec etc. right. */
c316a17c 14596 goto again;
727fc41e
AM
14597 }
14598 }
14599 break;
14600
14601 case R_PPC64_TLSGD:
37da22e5 14602 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 14603 && rel + 1 < relend)
727fc41e 14604 {
b9f04fe0 14605 unsigned int insn2;
5663e321 14606 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14607
4a421c53 14608 offset = rel->r_offset;
5663e321 14609 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14610 {
14611 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14612 if (r_type1 == R_PPC64_PLT_PCREL34
14613 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14614 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14615 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14616 break;
14617 }
14618
14619 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14620 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14621
b00a0a86 14622 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
14623 {
14624 /* IE */
14625 r_type = R_PPC64_NONE;
14626 insn2 = 0x7c636a14; /* add 3,3,13 */
14627 }
14628 else
14629 {
14630 /* LE */
14631 if (toc_symndx != 0)
14632 {
14633 r_symndx = toc_symndx;
14634 rel->r_addend = toc_addend;
14635 }
14636 r_type = R_PPC64_TPREL16_LO;
14637 rel->r_offset = offset + d_offset;
14638 insn2 = 0x38630000; /* addi 3,3,0 */
14639 }
14640 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14641 /* Zap the reloc on the _tls_get_addr call too. */
14642 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14643 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14644 bfd_put_32 (input_bfd, insn2, contents + offset);
b00a0a86 14645 if ((tls_mask & TLS_GDIE) == 0 && toc_symndx != 0)
c316a17c 14646 goto again;
411e1bfb 14647 }
411e1bfb
AM
14648 break;
14649
727fc41e 14650 case R_PPC64_TLSLD:
37da22e5 14651 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 14652 && rel + 1 < relend)
727fc41e 14653 {
b9f04fe0 14654 unsigned int insn2;
5663e321 14655 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 14656
4a421c53 14657 offset = rel->r_offset;
5663e321 14658 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
14659 {
14660 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
14661 if (r_type1 == R_PPC64_PLT_PCREL34
14662 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14663 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
14664 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14665 break;
14666 }
14667
14668 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14669 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14670
727fc41e
AM
14671 if (toc_symndx)
14672 sec = local_sections[toc_symndx];
14673 for (r_symndx = 0;
14674 r_symndx < symtab_hdr->sh_info;
14675 r_symndx++)
14676 if (local_sections[r_symndx] == sec)
14677 break;
14678 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14679 r_symndx = STN_UNDEF;
727fc41e 14680 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14681 if (r_symndx != STN_UNDEF)
727fc41e
AM
14682 rel->r_addend -= (local_syms[r_symndx].st_value
14683 + sec->output_offset
14684 + sec->output_section->vma);
14685
14686 r_type = R_PPC64_TPREL16_LO;
14687 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14688 rel->r_offset = offset + d_offset;
14689 /* Zap the reloc on the _tls_get_addr call too. */
14690 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14691 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 14692 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 14693 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 14694 goto again;
727fc41e
AM
14695 }
14696 break;
14697
411e1bfb 14698 case R_PPC64_DTPMOD64:
951fd09b
AM
14699 if (rel + 1 < relend
14700 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14701 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 14702 {
951fd09b
AM
14703 if ((tls_mask & TLS_GD) == 0)
14704 {
14705 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 14706 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
14707 r_type = R_PPC64_TPREL64;
14708 else
14709 {
4ce794b7 14710 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
14711 r_type = R_PPC64_NONE;
14712 }
14713 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14714 }
14715 }
14716 else
14717 {
14718 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14719 {
4ce794b7 14720 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14721 r_type = R_PPC64_NONE;
951fd09b 14722 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14723 }
411e1bfb
AM
14724 }
14725 break;
14726
14727 case R_PPC64_TPREL64:
951fd09b 14728 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14729 {
14730 r_type = R_PPC64_NONE;
14731 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14732 }
14733 break;
52a82034 14734
006589cf
AM
14735 case R_PPC64_ENTRY:
14736 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14737 if (!bfd_link_pic (info)
14738 && !info->traditional_format
14739 && relocation + 0x80008000 <= 0xffffffff)
14740 {
14741 unsigned int insn1, insn2;
14742
14743 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14744 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14745 if ((insn1 & ~0xfffc) == LD_R2_0R12
14746 && insn2 == ADD_R2_R2_R12)
14747 {
95f0d0d2 14748 bfd_put_32 (input_bfd,
006589cf
AM
14749 LIS_R2 + PPC_HA (relocation),
14750 contents + rel->r_offset);
95f0d0d2 14751 bfd_put_32 (input_bfd,
006589cf
AM
14752 ADDI_R2_R2 + PPC_LO (relocation),
14753 contents + rel->r_offset + 4);
14754 }
14755 }
14756 else
14757 {
14758 relocation -= (rel->r_offset
14759 + input_section->output_offset
14760 + input_section->output_section->vma);
14761 if (relocation + 0x80008000 <= 0xffffffff)
14762 {
14763 unsigned int insn1, insn2;
14764
14765 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14766 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14767 if ((insn1 & ~0xfffc) == LD_R2_0R12
14768 && insn2 == ADD_R2_R2_R12)
14769 {
95f0d0d2 14770 bfd_put_32 (input_bfd,
006589cf
AM
14771 ADDIS_R2_R12 + PPC_HA (relocation),
14772 contents + rel->r_offset);
95f0d0d2 14773 bfd_put_32 (input_bfd,
006589cf
AM
14774 ADDI_R2_R2 + PPC_LO (relocation),
14775 contents + rel->r_offset + 4);
14776 }
14777 }
14778 }
14779 break;
14780
52a82034
AM
14781 case R_PPC64_REL16_HA:
14782 /* If we are generating a non-PIC executable, edit
14783 . 0: addis 2,12,.TOC.-0b@ha
14784 . addi 2,2,.TOC.-0b@l
14785 used by ELFv2 global entry points to set up r2, to
14786 . lis 2,.TOC.@ha
14787 . addi 2,2,.TOC.@l
14788 if .TOC. is in range. */
0e1862bb 14789 if (!bfd_link_pic (info)
810d4e75 14790 && !info->traditional_format
006589cf 14791 && !htab->opd_abi
4f038ee5 14792 && rel->r_addend == d_offset
52a82034
AM
14793 && h != NULL && &h->elf == htab->elf.hgot
14794 && rel + 1 < relend
14795 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14796 && rel[1].r_offset == rel->r_offset + 4
14797 && rel[1].r_addend == rel->r_addend + 4
14798 && relocation + 0x80008000 <= 0xffffffff)
14799 {
14800 unsigned int insn1, insn2;
4a421c53 14801 offset = rel->r_offset - d_offset;
95f0d0d2
AM
14802 insn1 = bfd_get_32 (input_bfd, contents + offset);
14803 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14804 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14805 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14806 {
14807 r_type = R_PPC64_ADDR16_HA;
14808 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14809 rel->r_addend -= d_offset;
14810 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14811 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14812 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14813 }
14814 }
14815 break;
411e1bfb
AM
14816 }
14817
14818 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14819 insn = 0;
b25116a9
AM
14820 max_br_offset = 1 << 25;
14821 addend = rel->r_addend;
bc30df16 14822 reloc_dest = DEST_NORMAL;
65f38f15 14823 switch (r_type)
5bd4f169
AM
14824 {
14825 default:
65f38f15 14826 break;
5bd4f169 14827
3b421ab3
AM
14828 case R_PPC64_TOCSAVE:
14829 if (relocation + addend == (rel->r_offset
14830 + input_section->output_offset
14831 + input_section->output_section->vma)
14832 && tocsave_find (htab, NO_INSERT,
14833 &local_syms, rel, input_bfd))
14834 {
14835 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14836 if (insn == NOP
14837 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14838 bfd_put_32 (input_bfd,
14839 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14840 contents + rel->r_offset);
14841 }
14842 break;
14843
65f38f15
AM
14844 /* Branch taken prediction relocations. */
14845 case R_PPC64_ADDR14_BRTAKEN:
14846 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14847 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14848 /* Fall through. */
65f38f15 14849
86c76c7b 14850 /* Branch not taken prediction relocations. */
65f38f15
AM
14851 case R_PPC64_ADDR14_BRNTAKEN:
14852 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14853 insn |= bfd_get_32 (input_bfd,
411e1bfb 14854 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14855 /* Fall through. */
86c76c7b 14856
b25116a9
AM
14857 case R_PPC64_REL14:
14858 max_br_offset = 1 << 15;
1a0670f3 14859 /* Fall through. */
5bd4f169 14860
65f38f15 14861 case R_PPC64_REL24:
05d0e962 14862 case R_PPC64_REL24_NOTOC:
23cedd1d 14863 case R_PPC64_PLTCALL:
5663e321 14864 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
14865 /* Calls to functions with a different TOC, such as calls to
14866 shared objects, need to alter the TOC pointer. This is
14867 done using a linkage stub. A REL24 branching to these
14868 linkage stubs needs to be followed by a nop, as the nop
14869 will be replaced with an instruction to restore the TOC
14870 base pointer. */
8387904d 14871 fdh = h;
b31867b6
AM
14872 if (h != NULL
14873 && h->oh != NULL
14874 && h->oh->is_func_descriptor)
14875 fdh = ppc_follow_link (h->oh);
31c76678
DK
14876 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14877 htab);
5663e321
AM
14878 if ((r_type == R_PPC64_PLTCALL
14879 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 14880 && stub_entry != NULL
05d0e962
AM
14881 && stub_entry->stub_type >= ppc_stub_plt_call
14882 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
14883 stub_entry = NULL;
14884
6abec6bc 14885 if (stub_entry != NULL
d0abeec8
AM
14886 && ((stub_entry->stub_type >= ppc_stub_plt_call
14887 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 14888 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
14889 || stub_entry->stub_type == ppc_stub_plt_branch_both
14890 || stub_entry->stub_type == ppc_stub_long_branch_r2off
14891 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 14892 {
b25116a9 14893 bfd_boolean can_plt_call = FALSE;
721956f4 14894
6e1816be
AM
14895 if (stub_entry->stub_type == ppc_stub_plt_call
14896 && !htab->opd_abi
14897 && htab->params->plt_localentry0 != 0
14898 && is_elfv2_localentry0 (&h->elf))
14899 {
14900 /* The function doesn't use or change r2. */
14901 can_plt_call = TRUE;
14902 }
05d0e962
AM
14903 else if (r_type == R_PPC64_REL24_NOTOC)
14904 {
14905 /* NOTOC calls don't need to restore r2. */
14906 can_plt_call = TRUE;
14907 }
6e1816be 14908
f378ab09 14909 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
14910 branch and link followed by a nop. The nop is
14911 replaced by an insn to restore r2. */
6e1816be 14912 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14913 {
ba8ca3e7
AM
14914 unsigned long br;
14915
14916 br = bfd_get_32 (input_bfd,
14917 contents + rel->r_offset);
14918 if ((br & 1) != 0)
41bd81ab 14919 {
ba8ca3e7
AM
14920 unsigned long nop;
14921
14922 nop = bfd_get_32 (input_bfd,
14923 contents + rel->r_offset + 4);
23cedd1d
AM
14924 if (nop == LD_R2_0R1 + STK_TOC (htab))
14925 can_plt_call = TRUE;
14926 else if (nop == NOP
14927 || nop == CROR_151515
14928 || nop == CROR_313131)
a7f2871e 14929 {
ba8ca3e7
AM
14930 if (h != NULL
14931 && (h == htab->tls_get_addr_fd
14932 || h == htab->tls_get_addr)
7c9cf415 14933 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14934 {
14935 /* Special stub used, leave nop alone. */
14936 }
14937 else
a078d95a
AM
14938 bfd_put_32 (input_bfd,
14939 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14940 contents + rel->r_offset + 4);
14941 can_plt_call = TRUE;
a7f2871e 14942 }
41bd81ab 14943 }
5bd4f169 14944 }
721956f4 14945
ba8ca3e7 14946 if (!can_plt_call && h != NULL)
721956f4 14947 {
ba8ca3e7
AM
14948 const char *name = h->elf.root.root.string;
14949
14950 if (*name == '.')
14951 ++name;
14952
14953 if (strncmp (name, "__libc_start_main", 17) == 0
14954 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14955 {
ba8ca3e7
AM
14956 /* Allow crt1 branch to go via a toc adjusting
14957 stub. Other calls that never return could do
14958 the same, if we could detect such. */
b25116a9 14959 can_plt_call = TRUE;
6ab189d5 14960 }
ba8ca3e7
AM
14961 }
14962
14963 if (!can_plt_call)
14964 {
14965 /* g++ as of 20130507 emits self-calls without a
14966 following nop. This is arguably wrong since we
14967 have conflicting information. On the one hand a
14968 global symbol and on the other a local call
14969 sequence, but don't error for this special case.
14970 It isn't possible to cheaply verify we have
14971 exactly such a call. Allow all calls to the same
14972 section. */
14973 asection *code_sec = sec;
14974
14975 if (get_opd_info (sec) != NULL)
ad8e1ba5 14976 {
ba8ca3e7
AM
14977 bfd_vma off = (relocation + addend
14978 - sec->output_section->vma
14979 - sec->output_offset);
bc30df16 14980
ba8ca3e7 14981 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14982 }
ba8ca3e7
AM
14983 if (code_sec == input_section)
14984 can_plt_call = TRUE;
14985 }
14986
14987 if (!can_plt_call)
14988 {
05d0e962
AM
14989 if (stub_entry->stub_type >= ppc_stub_plt_call
14990 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 14991 info->callbacks->einfo
695344c0 14992 /* xgettext:c-format */
c1c8c1ef 14993 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 14994 "(plt call stub)\n"),
4805fc55
AM
14995 input_bfd, input_section, rel->r_offset, sym_name);
14996 else
14997 info->callbacks->einfo
695344c0 14998 /* xgettext:c-format */
c1c8c1ef 14999 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15000 "(toc save/adjust stub)\n"),
4805fc55 15001 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15002
15003 bfd_set_error (bfd_error_bad_value);
15004 ret = FALSE;
721956f4
AM
15005 }
15006
b25116a9 15007 if (can_plt_call
05d0e962
AM
15008 && stub_entry->stub_type >= ppc_stub_plt_call
15009 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
15010 unresolved_reloc = FALSE;
15011 }
15012
6abec6bc
AM
15013 if ((stub_entry == NULL
15014 || stub_entry->stub_type == ppc_stub_long_branch
15015 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15016 && get_opd_info (sec) != NULL)
15017 {
15018 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15019 bfd_vma off = (relocation + addend
15020 - sec->output_section->vma
15021 - sec->output_offset);
aef36ac1 15022 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
15023 if (dest != (bfd_vma) -1)
15024 {
15025 relocation = dest;
15026 addend = 0;
bc30df16 15027 reloc_dest = DEST_OPD;
8387904d
AM
15028 }
15029 }
15030
b25116a9
AM
15031 /* If the branch is out of reach we ought to have a long
15032 branch stub. */
15033 from = (rel->r_offset
15034 + input_section->output_offset
15035 + input_section->output_section->vma);
15036
6911b7dc
AM
15037 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15038 ? fdh->elf.other
15039 : sym->st_other);
15040
6abec6bc
AM
15041 if (stub_entry != NULL
15042 && (stub_entry->stub_type == ppc_stub_long_branch
15043 || stub_entry->stub_type == ppc_stub_plt_branch)
15044 && (r_type == R_PPC64_ADDR14_BRTAKEN
15045 || r_type == R_PPC64_ADDR14_BRNTAKEN
15046 || (relocation + addend - from + max_br_offset
15047 < 2 * max_br_offset)))
15048 /* Don't use the stub if this branch is in range. */
15049 stub_entry = NULL;
b25116a9 15050
05d0e962
AM
15051 if (stub_entry != NULL
15052 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15053 || stub_entry->stub_type == ppc_stub_long_branch_both
15054 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15055 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15056 && (r_type != R_PPC64_REL24_NOTOC
15057 || ((fdh ? fdh->elf.other : sym->st_other)
4a4e7361 15058 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
05d0e962
AM
15059 && (relocation + addend - from + max_br_offset
15060 < 2 * max_br_offset))
15061 stub_entry = NULL;
15062
15063 if (stub_entry != NULL
15064 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15065 || stub_entry->stub_type == ppc_stub_long_branch_both
15066 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15067 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15068 && r_type == R_PPC64_REL24_NOTOC
15069 && (relocation + addend - from + max_br_offset
15070 < 2 * max_br_offset))
15071 stub_entry = NULL;
15072
b25116a9
AM
15073 if (stub_entry != NULL)
15074 {
15075 /* Munge up the value and addend so that we call the stub
15076 rather than the procedure directly. */
a4b6fadd
AM
15077 asection *stub_sec = stub_entry->group->stub_sec;
15078
15079 if (stub_entry->stub_type == ppc_stub_save_res)
15080 relocation += (stub_sec->output_offset
15081 + stub_sec->output_section->vma
15082 + stub_sec->size - htab->sfpr->size
15083 - htab->sfpr->output_offset
15084 - htab->sfpr->output_section->vma);
15085 else
15086 relocation = (stub_entry->stub_offset
15087 + stub_sec->output_offset
15088 + stub_sec->output_section->vma);
b25116a9 15089 addend = 0;
bc30df16 15090 reloc_dest = DEST_STUB;
3b421ab3 15091
05d0e962
AM
15092 if (((stub_entry->stub_type == ppc_stub_plt_call
15093 && ALWAYS_EMIT_R2SAVE)
15094 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15095 || stub_entry->stub_type == ppc_stub_plt_call_both)
e81b4c93
AM
15096 && !(h != NULL
15097 && (h == htab->tls_get_addr_fd
15098 || h == htab->tls_get_addr)
15099 && htab->params->tls_get_addr_opt)
3b421ab3
AM
15100 && rel + 1 < relend
15101 && rel[1].r_offset == rel->r_offset + 4
15102 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15103 relocation += 4;
05d0e962
AM
15104 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15105 || stub_entry->stub_type == ppc_stub_plt_branch_both
15106 || stub_entry->stub_type == ppc_stub_plt_call_both)
15107 && r_type == R_PPC64_REL24_NOTOC)
15108 relocation += 4;
5663e321
AM
15109
15110 if (r_type == R_PPC64_REL24_NOTOC
15111 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
15112 || stub_entry->stub_type == ppc_stub_plt_call_both))
15113 htab->notoc_plt = 1;
b25116a9
AM
15114 }
15115
15116 if (insn != 0)
15117 {
794e51c0 15118 if (is_isa_v2)
721956f4 15119 {
b25116a9
AM
15120 /* Set 'a' bit. This is 0b00010 in BO field for branch
15121 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15122 for branch on CTR insns (BO == 1a00t or 1a01t). */
15123 if ((insn & (0x14 << 21)) == (0x04 << 21))
15124 insn |= 0x02 << 21;
15125 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15126 insn |= 0x08 << 21;
15127 else
15128 break;
15129 }
15130 else
15131 {
15132 /* Invert 'y' bit if not the default. */
4cc603a5 15133 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 15134 insn ^= 0x01 << 21;
721956f4 15135 }
b25116a9 15136
95f0d0d2 15137 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 15138 }
e86ce104 15139
06da1e8e
AM
15140 /* NOP out calls to undefined weak functions.
15141 We can thus call a weak function without first
15142 checking whether the function is defined. */
b25116a9 15143 else if (h != NULL
039b3fef 15144 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 15145 && h->elf.dynindx == -1
05d0e962
AM
15146 && (r_type == R_PPC64_REL24
15147 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 15148 && relocation == 0
4cc603a5 15149 && addend == 0)
e86ce104 15150 {
95f0d0d2 15151 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 15152 goto copy_reloc;
e86ce104 15153 }
65f38f15 15154 break;
066f4018
AM
15155
15156 case R_PPC64_GOT16_DS:
15157 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15158 if (relocation + addend - from + 0x8000 < 0x10000
15159 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15160 {
15161 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15162 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15163 {
15164 insn += (14u << 26) - (58u << 26);
15165 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15166 r_type = R_PPC64_TOC16;
15167 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15168 }
15169 }
15170 break;
15171
15172 case R_PPC64_GOT16_LO_DS:
15173 case R_PPC64_GOT16_HA:
15174 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15175 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
15176 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15177 {
15178 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15179 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15180 {
15181 insn += (14u << 26) - (58u << 26);
15182 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15183 r_type = R_PPC64_TOC16_LO;
15184 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15185 }
15186 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
15187 {
15188 r_type = R_PPC64_TOC16_HA;
15189 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15190 }
15191 }
15192 break;
4a421c53
AM
15193
15194 case R_PPC64_GOT_PCREL34:
15195 from = (rel->r_offset
15196 + input_section->output_section->vma
15197 + input_section->output_offset);
15198 if (relocation - from + (1ULL << 33) < 1ULL << 34
15199 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15200 {
15201 offset = rel->r_offset;
15202 pinsn = bfd_get_32 (input_bfd, contents + offset);
15203 pinsn <<= 32;
15204 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15205 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15206 == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
15207 {
15208 /* Replace with paddi. */
15209 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
15210 r_type = R_PPC64_PCREL34;
15211 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15212 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
15213 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
15214 goto pcrelopt;
15215 }
15216 }
15217 break;
15218
15219 case R_PPC64_PCREL34:
15220 if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15221 {
15222 offset = rel->r_offset;
15223 pinsn = bfd_get_32 (input_bfd, contents + offset);
15224 pinsn <<= 32;
15225 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15226 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15227 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
15228 | (14ULL << 26) /* paddi */))
15229 {
15230 pcrelopt:
15231 if (rel + 1 < relend
15232 && rel[1].r_offset == offset
15233 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
15234 {
15235 bfd_vma off2 = rel[1].r_addend;
15236 if (off2 == 0)
15237 /* zero means next insn. */
15238 off2 = 8;
15239 off2 += offset;
15240 if (off2 + 4 <= input_section->size)
15241 {
15242 uint64_t pinsn2;
15243 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15244 pinsn2 <<= 32;
15245 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15246 break;
15247 if (xlate_pcrel_opt (&pinsn, &pinsn2))
15248 {
15249 bfd_put_32 (input_bfd, pinsn >> 32,
15250 contents + offset);
15251 bfd_put_32 (input_bfd, pinsn,
15252 contents + offset + 4);
15253 bfd_put_32 (input_bfd, pinsn2 >> 32,
15254 contents + off2);
15255 }
15256 }
15257 }
15258 }
15259 }
15260 break;
65f38f15 15261 }
5bd4f169 15262
65f38f15 15263 /* Set `addend'. */
411e1bfb 15264 tls_type = 0;
23cedd1d 15265 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
15266 switch (r_type)
15267 {
15268 default:
cf97bcb0
AM
15269 /* xgettext:c-format */
15270 _bfd_error_handler (_("%pB: %s unsupported"),
15271 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 15272
65f38f15 15273 bfd_set_error (bfd_error_bad_value);
b34976b6 15274 ret = FALSE;
c316a17c 15275 goto copy_reloc;
5bd4f169 15276
65f38f15 15277 case R_PPC64_NONE:
411e1bfb 15278 case R_PPC64_TLS:
727fc41e
AM
15279 case R_PPC64_TLSGD:
15280 case R_PPC64_TLSLD:
3b421ab3 15281 case R_PPC64_TOCSAVE:
04c9666a
AM
15282 case R_PPC64_GNU_VTINHERIT:
15283 case R_PPC64_GNU_VTENTRY:
006589cf 15284 case R_PPC64_ENTRY:
4a421c53 15285 case R_PPC64_PCREL_OPT:
c316a17c 15286 goto copy_reloc;
5bd4f169
AM
15287
15288 /* GOT16 relocations. Like an ADDR16 using the symbol's
15289 address in the GOT as relocation value instead of the
411e1bfb 15290 symbol's value itself. Also, create a GOT entry for the
5bd4f169 15291 symbol and put the symbol value there. */
411e1bfb
AM
15292 case R_PPC64_GOT_TLSGD16:
15293 case R_PPC64_GOT_TLSGD16_LO:
15294 case R_PPC64_GOT_TLSGD16_HI:
15295 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 15296 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
15297 goto dogot;
15298
15299 case R_PPC64_GOT_TLSLD16:
15300 case R_PPC64_GOT_TLSLD16_LO:
15301 case R_PPC64_GOT_TLSLD16_HI:
15302 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 15303 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
15304 goto dogot;
15305
15306 case R_PPC64_GOT_TPREL16_DS:
15307 case R_PPC64_GOT_TPREL16_LO_DS:
15308 case R_PPC64_GOT_TPREL16_HI:
15309 case R_PPC64_GOT_TPREL16_HA:
15310 tls_type = TLS_TLS | TLS_TPREL;
15311 goto dogot;
15312
15313 case R_PPC64_GOT_DTPREL16_DS:
15314 case R_PPC64_GOT_DTPREL16_LO_DS:
15315 case R_PPC64_GOT_DTPREL16_HI:
15316 case R_PPC64_GOT_DTPREL16_HA:
15317 tls_type = TLS_TLS | TLS_DTPREL;
15318 goto dogot;
15319
65f38f15
AM
15320 case R_PPC64_GOT16:
15321 case R_PPC64_GOT16_LO:
15322 case R_PPC64_GOT16_HI:
15323 case R_PPC64_GOT16_HA:
15324 case R_PPC64_GOT16_DS:
15325 case R_PPC64_GOT16_LO_DS:
5663e321 15326 case R_PPC64_GOT_PCREL34:
411e1bfb 15327 dogot:
5bd4f169
AM
15328 {
15329 /* Relocation is to the entry for this symbol in the global
15330 offset table. */
e717da7e 15331 asection *got;
d881513a 15332 bfd_vma *offp;
5bd4f169 15333 bfd_vma off;
d881513a 15334 unsigned long indx = 0;
927be08e 15335 struct got_entry *ent;
5663e321
AM
15336 bfd_vma sym_addend = orig_rel.r_addend;
15337
15338 if (r_type == R_PPC64_GOT_PCREL34)
15339 sym_addend = 0;
65f38f15 15340
d881513a
AM
15341 if (tls_type == (TLS_TLS | TLS_LD)
15342 && (h == NULL
f5385ebf 15343 || !h->elf.def_dynamic))
927be08e 15344 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 15345 else
5bd4f169 15346 {
d881513a
AM
15347 if (h != NULL)
15348 {
f0158f44
AM
15349 if (!htab->elf.dynamic_sections_created
15350 || h->elf.dynindx == -1
15351 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 15352 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
15353 /* This is actually a static link, or it is a
15354 -Bsymbolic link and the symbol is defined
15355 locally, or the symbol was forced to be local
15356 because of a version file. */
15357 ;
15358 else
15359 {
039b3fef 15360 indx = h->elf.dynindx;
d881513a
AM
15361 unresolved_reloc = FALSE;
15362 }
039b3fef 15363 ent = h->elf.got.glist;
d881513a 15364 }
411e1bfb 15365 else
5bd4f169 15366 {
d881513a
AM
15367 if (local_got_ents == NULL)
15368 abort ();
15369 ent = local_got_ents[r_symndx];
5bd4f169 15370 }
d881513a
AM
15371
15372 for (; ent != NULL; ent = ent->next)
5663e321 15373 if (ent->addend == sym_addend
e717da7e 15374 && ent->owner == input_bfd
d881513a
AM
15375 && ent->tls_type == tls_type)
15376 break;
5bd4f169 15377 }
411e1bfb 15378
927be08e
AM
15379 if (ent == NULL)
15380 abort ();
15381 if (ent->is_indirect)
15382 ent = ent->got.ent;
15383 offp = &ent->got.offset;
15384 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
15385 if (got == NULL)
15386 abort ();
15387
411e1bfb
AM
15388 /* The offset must always be a multiple of 8. We use the
15389 least significant bit to record whether we have already
15390 processed this entry. */
d881513a 15391 off = *offp;
411e1bfb
AM
15392 if ((off & 1) != 0)
15393 off &= ~1;
5bd4f169
AM
15394 else
15395 {
411e1bfb
AM
15396 /* Generate relocs for the dynamic linker, except in
15397 the case of TLSLD where we'll use one entry per
15398 module. */
25f23106
AM
15399 asection *relgot;
15400 bfd_boolean ifunc;
e717da7e 15401
d881513a 15402 *offp = off | 1;
25f23106
AM
15403 relgot = NULL;
15404 ifunc = (h != NULL
15405 ? h->elf.type == STT_GNU_IFUNC
15406 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 15407 if (ifunc)
82e66161
AM
15408 {
15409 relgot = htab->elf.irelplt;
15410 if (indx == 0)
15411 htab->local_ifunc_resolver = 1;
15412 else if (is_static_defined (&h->elf))
15413 htab->maybe_local_ifunc_resolver = 1;
15414 }
f0158f44
AM
15415 else if (indx != 0
15416 || (bfd_link_pic (info)
15417 && (h == NULL
21d68fcd 15418 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 15419 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
15420 && !h->elf.def_dynamic))
15421 && !(tls_type == (TLS_TLS | TLS_TPREL)
15422 && bfd_link_executable (info)
15423 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 15424 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 15425 if (relgot != NULL)
5bd4f169 15426 {
e717da7e
AM
15427 outrel.r_offset = (got->output_section->vma
15428 + got->output_offset
411e1bfb 15429 + off);
5663e321 15430 outrel.r_addend = sym_addend;
d881513a 15431 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 15432 {
411e1bfb 15433 outrel.r_addend = 0;
e515b051 15434 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
15435 if (tls_type == (TLS_TLS | TLS_GD))
15436 {
e717da7e
AM
15437 loc = relgot->contents;
15438 loc += (relgot->reloc_count++
d881513a
AM
15439 * sizeof (Elf64_External_Rela));
15440 bfd_elf64_swap_reloca_out (output_bfd,
15441 &outrel, loc);
e515b051 15442 outrel.r_offset += 8;
5663e321 15443 outrel.r_addend = sym_addend;
d881513a
AM
15444 outrel.r_info
15445 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 15446 }
411e1bfb 15447 }
951fd09b 15448 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 15449 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 15450 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 15451 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
15452 else if (indx != 0)
15453 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15454 else
81407a69 15455 {
25f23106
AM
15456 if (ifunc)
15457 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15458 else
15459 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
15460
15461 /* Write the .got section contents for the sake
15462 of prelink. */
e717da7e 15463 loc = got->contents + off;
23fbd6fa
JJ
15464 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15465 loc);
81407a69 15466 }
81407a69
AM
15467
15468 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
15469 {
15470 outrel.r_addend += relocation;
15471 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
15472 {
15473 if (htab->elf.tls_sec == NULL)
15474 outrel.r_addend = 0;
15475 else
15476 outrel.r_addend -= htab->elf.tls_sec->vma;
15477 }
e515b051 15478 }
e717da7e
AM
15479 loc = relgot->contents;
15480 loc += (relgot->reloc_count++
411e1bfb
AM
15481 * sizeof (Elf64_External_Rela));
15482 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15483 }
15484
ad8e1ba5 15485 /* Init the .got section contents here if we're not
81407a69 15486 emitting a reloc. */
d881513a 15487 else
411e1bfb 15488 {
5663e321 15489 relocation += sym_addend;
f0158f44 15490 if (tls_type != 0)
411e1bfb 15491 {
989f9879
AM
15492 if (htab->elf.tls_sec == NULL)
15493 relocation = 0;
15494 else
15495 {
f0158f44
AM
15496 if (tls_type & TLS_LD)
15497 relocation = 0;
15498 else
15499 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 15500 if (tls_type & TLS_TPREL)
989f9879
AM
15501 relocation += DTP_OFFSET - TP_OFFSET;
15502 }
5bd4f169 15503
f0158f44 15504 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
15505 {
15506 bfd_put_64 (output_bfd, relocation,
e717da7e 15507 got->contents + off + 8);
676ee2b5 15508 relocation = 1;
7b609f53 15509 }
411e1bfb
AM
15510 }
15511 bfd_put_64 (output_bfd, relocation,
e717da7e 15512 got->contents + off);
5bd4f169
AM
15513 }
15514 }
15515
65f38f15
AM
15516 if (off >= (bfd_vma) -2)
15517 abort ();
15518
bf102f86 15519 relocation = got->output_section->vma + got->output_offset + off;
5663e321
AM
15520 if (r_type != R_PPC64_GOT_PCREL34)
15521 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 15522 }
65f38f15
AM
15523 break;
15524
15525 case R_PPC64_PLT16_HA:
15526 case R_PPC64_PLT16_HI:
15527 case R_PPC64_PLT16_LO:
08be3224 15528 case R_PPC64_PLT16_LO_DS:
5663e321
AM
15529 case R_PPC64_PLT_PCREL34:
15530 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
15531 case R_PPC64_PLT32:
15532 case R_PPC64_PLT64:
23cedd1d 15533 case R_PPC64_PLTSEQ:
5663e321 15534 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 15535 case R_PPC64_PLTCALL:
5663e321 15536 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
15537 /* Relocation is to the entry for this symbol in the
15538 procedure linkage table. */
23cedd1d 15539 unresolved_reloc = TRUE;
cbf95972
AM
15540 {
15541 struct plt_entry **plt_list = NULL;
15542 if (h != NULL)
15543 plt_list = &h->elf.plt.plist;
15544 else if (local_got_ents != NULL)
15545 {
15546 struct plt_entry **local_plt = (struct plt_entry **)
15547 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 15548 plt_list = local_plt + r_symndx;
cbf95972
AM
15549 }
15550 if (plt_list)
15551 {
15552 struct plt_entry *ent;
5663e321
AM
15553 bfd_vma sym_addend = orig_rel.r_addend;
15554
15555 if (r_type == R_PPC64_PLT_PCREL34
15556 || r_type == R_PPC64_PLT_PCREL34_NOTOC)
15557 sym_addend = 0;
65f38f15 15558
cbf95972
AM
15559 for (ent = *plt_list; ent != NULL; ent = ent->next)
15560 if (ent->plt.offset != (bfd_vma) -1
5663e321 15561 && ent->addend == sym_addend)
cbf95972
AM
15562 {
15563 asection *plt;
08be3224 15564 bfd_vma got;
cbf95972
AM
15565
15566 plt = htab->elf.splt;
15567 if (!htab->elf.dynamic_sections_created
15568 || h == NULL
15569 || h->elf.dynindx == -1)
2d7ad24e
AM
15570 {
15571 if (h != NULL
15572 ? h->elf.type == STT_GNU_IFUNC
15573 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15574 plt = htab->elf.iplt;
15575 else
15576 plt = htab->pltlocal;
15577 }
15578 relocation = (plt->output_section->vma
15579 + plt->output_offset
15580 + ent->plt.offset);
08be3224 15581 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
15582 || r_type == R_PPC64_PLT16_HI
15583 || r_type == R_PPC64_PLT16_LO
15584 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
15585 {
15586 got = (elf_gp (output_bfd)
15587 + htab->sec_info[input_section->id].toc_off);
15588 relocation -= got;
15589 }
5663e321
AM
15590 if (r_type != R_PPC64_PLT_PCREL34
15591 && r_type != R_PPC64_PLT_PCREL34_NOTOC)
15592 addend = 0;
cbf95972
AM
15593 unresolved_reloc = FALSE;
15594 break;
15595 }
15596 }
15597 }
65f38f15 15598 break;
5bd4f169 15599
0b13192e
AM
15600 case R_PPC64_TOC:
15601 /* Relocation value is TOC base. */
15602 relocation = TOCstart;
cf35638d 15603 if (r_symndx == STN_UNDEF)
6f20ed8a 15604 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
15605 else if (unresolved_reloc)
15606 ;
6f20ed8a
AM
15607 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15608 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
15609 else
15610 unresolved_reloc = TRUE;
ab96bf03 15611 goto dodyn;
0b13192e 15612
5bd4f169
AM
15613 /* TOC16 relocs. We want the offset relative to the TOC base,
15614 which is the address of the start of the TOC plus 0x8000.
15615 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15616 in this order. */
65f38f15
AM
15617 case R_PPC64_TOC16:
15618 case R_PPC64_TOC16_LO:
15619 case R_PPC64_TOC16_HI:
15620 case R_PPC64_TOC16_DS:
15621 case R_PPC64_TOC16_LO_DS:
15622 case R_PPC64_TOC16_HA:
6f20ed8a 15623 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
15624 break;
15625
15626 /* Relocate against the beginning of the section. */
65f38f15
AM
15627 case R_PPC64_SECTOFF:
15628 case R_PPC64_SECTOFF_LO:
15629 case R_PPC64_SECTOFF_HI:
15630 case R_PPC64_SECTOFF_DS:
15631 case R_PPC64_SECTOFF_LO_DS:
15632 case R_PPC64_SECTOFF_HA:
4ce794b7 15633 if (sec != NULL)
65f38f15 15634 addend -= sec->output_section->vma;
5bd4f169
AM
15635 break;
15636
25f23106
AM
15637 case R_PPC64_REL16:
15638 case R_PPC64_REL16_LO:
15639 case R_PPC64_REL16_HI:
15640 case R_PPC64_REL16_HA:
4a969973
AM
15641 case R_PPC64_REL16_HIGH:
15642 case R_PPC64_REL16_HIGHA:
15643 case R_PPC64_REL16_HIGHER:
15644 case R_PPC64_REL16_HIGHERA:
15645 case R_PPC64_REL16_HIGHEST:
15646 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
15647 case R_PPC64_REL16_HIGHER34:
15648 case R_PPC64_REL16_HIGHERA34:
15649 case R_PPC64_REL16_HIGHEST34:
15650 case R_PPC64_REL16_HIGHESTA34:
a680de9a 15651 case R_PPC64_REL16DX_HA:
721956f4
AM
15652 case R_PPC64_REL14:
15653 case R_PPC64_REL14_BRNTAKEN:
15654 case R_PPC64_REL14_BRTAKEN:
5d1634d7 15655 case R_PPC64_REL24:
05d0e962 15656 case R_PPC64_REL24_NOTOC:
5663e321
AM
15657 case R_PPC64_PCREL34:
15658 case R_PPC64_PCREL28:
5d1634d7
AM
15659 break;
15660
411e1bfb
AM
15661 case R_PPC64_TPREL16:
15662 case R_PPC64_TPREL16_LO:
15663 case R_PPC64_TPREL16_HI:
15664 case R_PPC64_TPREL16_HA:
15665 case R_PPC64_TPREL16_DS:
15666 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
15667 case R_PPC64_TPREL16_HIGH:
15668 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
15669 case R_PPC64_TPREL16_HIGHER:
15670 case R_PPC64_TPREL16_HIGHERA:
15671 case R_PPC64_TPREL16_HIGHEST:
15672 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
15673 if (h != NULL
15674 && h->elf.root.type == bfd_link_hash_undefweak
15675 && h->elf.dynindx == -1)
15676 {
15677 /* Make this relocation against an undefined weak symbol
15678 resolve to zero. This is really just a tweak, since
15679 code using weak externs ought to check that they are
15680 defined before using them. */
15681 bfd_byte *p = contents + rel->r_offset - d_offset;
15682
95f0d0d2 15683 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
15684 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15685 if (insn != 0)
95f0d0d2 15686 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
15687 break;
15688 }
989f9879
AM
15689 if (htab->elf.tls_sec != NULL)
15690 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
15691 /* The TPREL16 relocs shouldn't really be used in shared
15692 libs or with non-local symbols as that will result in
15693 DT_TEXTREL being set, but support them anyway. */
15694 goto dodyn;
411e1bfb
AM
15695
15696 case R_PPC64_DTPREL16:
15697 case R_PPC64_DTPREL16_LO:
15698 case R_PPC64_DTPREL16_HI:
15699 case R_PPC64_DTPREL16_HA:
15700 case R_PPC64_DTPREL16_DS:
15701 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
15702 case R_PPC64_DTPREL16_HIGH:
15703 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
15704 case R_PPC64_DTPREL16_HIGHER:
15705 case R_PPC64_DTPREL16_HIGHERA:
15706 case R_PPC64_DTPREL16_HIGHEST:
15707 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
15708 if (htab->elf.tls_sec != NULL)
15709 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
15710 break;
15711
45965137
AM
15712 case R_PPC64_ADDR64_LOCAL:
15713 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15714 ? h->elf.other
15715 : sym->st_other);
15716 break;
15717
e515b051
AM
15718 case R_PPC64_DTPMOD64:
15719 relocation = 1;
15720 addend = 0;
15721 goto dodyn;
15722
411e1bfb 15723 case R_PPC64_TPREL64:
989f9879
AM
15724 if (htab->elf.tls_sec != NULL)
15725 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
15726 goto dodyn;
15727
15728 case R_PPC64_DTPREL64:
989f9879
AM
15729 if (htab->elf.tls_sec != NULL)
15730 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 15731 /* Fall through. */
411e1bfb 15732
65f38f15
AM
15733 /* Relocations that may need to be propagated if this is a
15734 dynamic object. */
04c9666a 15735 case R_PPC64_REL30:
65f38f15
AM
15736 case R_PPC64_REL32:
15737 case R_PPC64_REL64:
15738 case R_PPC64_ADDR14:
15739 case R_PPC64_ADDR14_BRNTAKEN:
15740 case R_PPC64_ADDR14_BRTAKEN:
15741 case R_PPC64_ADDR16:
15742 case R_PPC64_ADDR16_DS:
15743 case R_PPC64_ADDR16_HA:
15744 case R_PPC64_ADDR16_HI:
f9c6b907
AM
15745 case R_PPC64_ADDR16_HIGH:
15746 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15747 case R_PPC64_ADDR16_HIGHER:
15748 case R_PPC64_ADDR16_HIGHERA:
15749 case R_PPC64_ADDR16_HIGHEST:
15750 case R_PPC64_ADDR16_HIGHESTA:
15751 case R_PPC64_ADDR16_LO:
15752 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
15753 case R_PPC64_ADDR16_HIGHER34:
15754 case R_PPC64_ADDR16_HIGHERA34:
15755 case R_PPC64_ADDR16_HIGHEST34:
15756 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 15757 case R_PPC64_ADDR24:
65f38f15
AM
15758 case R_PPC64_ADDR32:
15759 case R_PPC64_ADDR64:
15760 case R_PPC64_UADDR16:
15761 case R_PPC64_UADDR32:
15762 case R_PPC64_UADDR64:
5663e321
AM
15763 case R_PPC64_D34:
15764 case R_PPC64_D34_LO:
15765 case R_PPC64_D34_HI30:
15766 case R_PPC64_D34_HA30:
15767 case R_PPC64_D28:
411e1bfb 15768 dodyn:
5d1634d7 15769 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
15770 break;
15771
41bd81ab
AM
15772 if (NO_OPD_RELOCS && is_opd)
15773 break;
15774
8a9e8e72 15775 if (bfd_link_pic (info)
b1b07054
AM
15776 ? ((h == NULL
15777 || h->dyn_relocs != NULL)
15778 && ((h != NULL && pc_dynrelocs (h))
15779 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
15780 : (h != NULL
15781 ? h->dyn_relocs != NULL
d311bc8b 15782 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 15783 {
b34976b6 15784 bfd_boolean skip, relocate;
65f38f15 15785 asection *sreloc;
1cf1f670 15786 bfd_vma out_off;
82e66161 15787 long indx = 0;
65f38f15
AM
15788
15789 /* When generating a dynamic object, these relocations
15790 are copied into the output file to be resolved at run
15791 time. */
15792
b34976b6
AM
15793 skip = FALSE;
15794 relocate = FALSE;
65f38f15 15795
1cf1f670
AM
15796 out_off = _bfd_elf_section_offset (output_bfd, info,
15797 input_section, rel->r_offset);
15798 if (out_off == (bfd_vma) -1)
b34976b6 15799 skip = TRUE;
1cf1f670 15800 else if (out_off == (bfd_vma) -2)
b34976b6 15801 skip = TRUE, relocate = TRUE;
1cf1f670
AM
15802 out_off += (input_section->output_section->vma
15803 + input_section->output_offset);
15804 outrel.r_offset = out_off;
411e1bfb 15805 outrel.r_addend = rel->r_addend;
65f38f15 15806
1cf1f670
AM
15807 /* Optimize unaligned reloc use. */
15808 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15809 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15810 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15811 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15812 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15813 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15814 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15815 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15816 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15817
65f38f15 15818 if (skip)
0bb2d96a 15819 memset (&outrel, 0, sizeof outrel);
afe397ea 15820 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
15821 && !is_opd
15822 && r_type != R_PPC64_TOC)
14acf4dc 15823 {
82e66161
AM
15824 indx = h->elf.dynindx;
15825 BFD_ASSERT (indx != -1);
15826 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 15827 }
65f38f15
AM
15828 else
15829 {
41bd81ab
AM
15830 /* This symbol is local, or marked to become local,
15831 or this is an opd section reloc which must point
15832 at a local function. */
65f38f15 15833 outrel.r_addend += relocation;
e86ce104 15834 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 15835 {
3fad3c7c 15836 if (is_opd && h != NULL)
afbe61cf
AM
15837 {
15838 /* Lie about opd entries. This case occurs
15839 when building shared libraries and we
15840 reference a function in another shared
3fad3c7c
AM
15841 lib. The same thing happens for a weak
15842 definition in an application that's
15843 overridden by a strong definition in a
15844 shared lib. (I believe this is a generic
15845 bug in binutils handling of weak syms.)
15846 In these cases we won't use the opd
1e2f5b6e 15847 entry in this lib. */
b34976b6 15848 unresolved_reloc = FALSE;
afbe61cf 15849 }
25f23106
AM
15850 if (!is_opd
15851 && r_type == R_PPC64_ADDR64
15852 && (h != NULL
15853 ? h->elf.type == STT_GNU_IFUNC
15854 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15855 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15856 else
15857 {
15858 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 15859
25f23106
AM
15860 /* We need to relocate .opd contents for ld.so.
15861 Prelink also wants simple and consistent rules
15862 for relocs. This make all RELATIVE relocs have
15863 *r_offset equal to r_addend. */
15864 relocate = TRUE;
15865 }
65f38f15
AM
15866 }
15867 else
15868 {
25f23106
AM
15869 if (h != NULL
15870 ? h->elf.type == STT_GNU_IFUNC
15871 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15872 {
25f53a85 15873 info->callbacks->einfo
695344c0 15874 /* xgettext:c-format */
174d0a74 15875 (_("%H: %s for indirect "
c1c8c1ef 15876 "function `%pT' unsupported\n"),
25f53a85 15877 input_bfd, input_section, rel->r_offset,
25f23106
AM
15878 ppc64_elf_howto_table[r_type]->name,
15879 sym_name);
15880 ret = FALSE;
15881 }
cf35638d 15882 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
15883 ;
15884 else if (sec == NULL || sec->owner == NULL)
15885 {
15886 bfd_set_error (bfd_error_bad_value);
b34976b6 15887 return FALSE;
65f38f15
AM
15888 }
15889 else
15890 {
f26a3287 15891 asection *osec = sec->output_section;
65f38f15 15892
f26a3287
AM
15893 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
15894 {
15895 /* TLS symbol values are relative to the
15896 TLS segment. Dynamic relocations for
15897 local TLS symbols therefore can't be
15898 reduced to a relocation against their
15899 section symbol because it holds the
15900 address of the section, not a value
15901 relative to the TLS segment. We could
15902 change the .tdata dynamic section symbol
15903 to be zero value but STN_UNDEF works
15904 and is used elsewhere, eg. for TPREL64
15905 GOT relocs against local TLS symbols. */
15906 osec = htab->elf.tls_sec;
15907 indx = 0;
15908 }
15909 else
74541ad4 15910 {
74541ad4 15911 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
15912 if (indx == 0)
15913 {
15914 if ((osec->flags & SEC_READONLY) == 0
15915 && htab->elf.data_index_section != NULL)
15916 osec = htab->elf.data_index_section;
15917 else
15918 osec = htab->elf.text_index_section;
15919 indx = elf_section_data (osec)->dynindx;
15920 }
15921 BFD_ASSERT (indx != 0);
74541ad4 15922 }
74541ad4 15923
65f38f15
AM
15924 /* We are turning this relocation into one
15925 against a section symbol, so subtract out
15926 the output section's address but not the
15927 offset of the input section in the output
15928 section. */
15929 outrel.r_addend -= osec->vma;
15930 }
15931
15932 outrel.r_info = ELF64_R_INFO (indx, r_type);
15933 }
15934 }
15935
15936 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
15937 if (h != NULL
15938 ? h->elf.type == STT_GNU_IFUNC
15939 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
15940 {
15941 sreloc = htab->elf.irelplt;
15942 if (indx == 0)
15943 htab->local_ifunc_resolver = 1;
15944 else if (is_static_defined (&h->elf))
15945 htab->maybe_local_ifunc_resolver = 1;
15946 }
65f38f15
AM
15947 if (sreloc == NULL)
15948 abort ();
15949
dfbb6ac9
AM
15950 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15951 >= sreloc->size)
15952 abort ();
947216bf
AM
15953 loc = sreloc->contents;
15954 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
15955 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15956
15957 /* If this reloc is against an external symbol, it will
15958 be computed at runtime, so there's no need to do
81407a69
AM
15959 anything now. However, for the sake of prelink ensure
15960 that the section contents are a known value. */
2cdcc330 15961 if (!relocate)
81407a69
AM
15962 {
15963 unresolved_reloc = FALSE;
15964 /* The value chosen here is quite arbitrary as ld.so
15965 ignores section contents except for the special
15966 case of .opd where the contents might be accessed
15967 before relocation. Choose zero, as that won't
15968 cause reloc overflow. */
15969 relocation = 0;
15970 addend = 0;
15971 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15972 to improve backward compatibility with older
15973 versions of ld. */
15974 if (r_type == R_PPC64_ADDR64)
15975 addend = outrel.r_addend;
15976 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 15977 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 15978 addend = outrel.r_offset;
81407a69 15979 }
65f38f15 15980 }
5bd4f169
AM
15981 break;
15982
65f38f15
AM
15983 case R_PPC64_COPY:
15984 case R_PPC64_GLOB_DAT:
15985 case R_PPC64_JMP_SLOT:
25f23106 15986 case R_PPC64_JMP_IREL:
65f38f15
AM
15987 case R_PPC64_RELATIVE:
15988 /* We shouldn't ever see these dynamic relocs in relocatable
15989 files. */
ae9a127f 15990 /* Fall through. */
65f38f15
AM
15991
15992 case R_PPC64_PLTGOT16:
15993 case R_PPC64_PLTGOT16_DS:
15994 case R_PPC64_PLTGOT16_HA:
15995 case R_PPC64_PLTGOT16_HI:
15996 case R_PPC64_PLTGOT16_LO:
15997 case R_PPC64_PLTGOT16_LO_DS:
15998 case R_PPC64_PLTREL32:
15999 case R_PPC64_PLTREL64:
16000 /* These ones haven't been implemented yet. */
16001
25f53a85 16002 info->callbacks->einfo
695344c0 16003 /* xgettext:c-format */
c1c8c1ef 16004 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16005 input_bfd,
4ce794b7 16006 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16007
16008 bfd_set_error (bfd_error_invalid_operation);
b34976b6 16009 ret = FALSE;
c316a17c 16010 goto copy_reloc;
65f38f15 16011 }
5bd4f169 16012
67f0cbdb
AM
16013 /* Multi-instruction sequences that access the TOC can be
16014 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16015 to nop; addi rb,r2,x; */
67f0cbdb
AM
16016 switch (r_type)
16017 {
16018 default:
16019 break;
16020
16021 case R_PPC64_GOT_TLSLD16_HI:
16022 case R_PPC64_GOT_TLSGD16_HI:
16023 case R_PPC64_GOT_TPREL16_HI:
16024 case R_PPC64_GOT_DTPREL16_HI:
16025 case R_PPC64_GOT16_HI:
16026 case R_PPC64_TOC16_HI:
16027 /* These relocs would only be useful if building up an
16028 offset to later add to r2, perhaps in an indexed
16029 addressing mode instruction. Don't try to optimize.
16030 Unfortunately, the possibility of someone building up an
16031 offset like this or even with the HA relocs, means that
16032 we need to check the high insn when optimizing the low
16033 insn. */
16034 break;
16035
5663e321
AM
16036 case R_PPC64_PLTCALL_NOTOC:
16037 if (!unresolved_reloc)
16038 htab->notoc_plt = 1;
16039 /* Fall through. */
23cedd1d
AM
16040 case R_PPC64_PLTCALL:
16041 if (unresolved_reloc)
16042 {
16043 /* No plt entry. Make this into a direct call. */
16044 bfd_byte *p = contents + rel->r_offset;
16045 insn = bfd_get_32 (input_bfd, p);
16046 insn &= 1;
16047 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
16048 if (r_type == R_PPC64_PLTCALL)
16049 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
16050 unresolved_reloc = save_unresolved_reloc;
16051 r_type = R_PPC64_REL24;
16052 }
16053 break;
16054
5663e321 16055 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
16056 case R_PPC64_PLTSEQ:
16057 if (unresolved_reloc)
16058 {
16059 unresolved_reloc = FALSE;
16060 goto nop_it;
16061 }
16062 break;
16063
5663e321
AM
16064 case R_PPC64_PLT_PCREL34_NOTOC:
16065 if (!unresolved_reloc)
16066 htab->notoc_plt = 1;
16067 /* Fall through. */
16068 case R_PPC64_PLT_PCREL34:
16069 if (unresolved_reloc)
16070 {
16071 bfd_byte *p = contents + rel->r_offset;
16072 bfd_put_32 (input_bfd, PNOP >> 32, p);
16073 bfd_put_32 (input_bfd, PNOP, p + 4);
16074 unresolved_reloc = FALSE;
16075 goto copy_reloc;
16076 }
16077 break;
16078
23cedd1d
AM
16079 case R_PPC64_PLT16_HA:
16080 if (unresolved_reloc)
16081 {
16082 unresolved_reloc = FALSE;
16083 goto nop_it;
16084 }
16085 /* Fall through. */
67f0cbdb
AM
16086 case R_PPC64_GOT_TLSLD16_HA:
16087 case R_PPC64_GOT_TLSGD16_HA:
16088 case R_PPC64_GOT_TPREL16_HA:
16089 case R_PPC64_GOT_DTPREL16_HA:
16090 case R_PPC64_GOT16_HA:
16091 case R_PPC64_TOC16_HA:
98528052 16092 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16093 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 16094 {
23cedd1d
AM
16095 bfd_byte *p;
16096 nop_it:
16097 p = contents + (rel->r_offset & ~3);
98528052 16098 bfd_put_32 (input_bfd, NOP, p);
d830549d 16099 goto copy_reloc;
98528052 16100 }
67f0cbdb
AM
16101 break;
16102
23cedd1d
AM
16103 case R_PPC64_PLT16_LO:
16104 case R_PPC64_PLT16_LO_DS:
16105 if (unresolved_reloc)
16106 {
16107 unresolved_reloc = FALSE;
16108 goto nop_it;
16109 }
16110 /* Fall through. */
67f0cbdb
AM
16111 case R_PPC64_GOT_TLSLD16_LO:
16112 case R_PPC64_GOT_TLSGD16_LO:
16113 case R_PPC64_GOT_TPREL16_LO_DS:
16114 case R_PPC64_GOT_DTPREL16_LO_DS:
16115 case R_PPC64_GOT16_LO:
16116 case R_PPC64_GOT16_LO_DS:
16117 case R_PPC64_TOC16_LO:
16118 case R_PPC64_TOC16_LO_DS:
98528052 16119 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 16120 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
16121 {
16122 bfd_byte *p = contents + (rel->r_offset & ~3);
16123 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
16124 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16125 {
16126 /* Transform addic to addi when we change reg. */
16127 insn &= ~((0x3f << 26) | (0x1f << 16));
16128 insn |= (14u << 26) | (2 << 16);
16129 }
16130 else
67f0cbdb 16131 {
98528052
AM
16132 insn &= ~(0x1f << 16);
16133 insn |= 2 << 16;
67f0cbdb 16134 }
560c8763 16135 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
16136 }
16137 break;
9a23f96e
AM
16138
16139 case R_PPC64_TPREL16_HA:
16140 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16141 {
16142 bfd_byte *p = contents + (rel->r_offset & ~3);
16143 insn = bfd_get_32 (input_bfd, p);
16144 if ((insn & ((0x3f << 26) | 0x1f << 16))
16145 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16146 /* xgettext:c-format */
16147 info->callbacks->minfo
16148 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
16149 input_bfd, input_section, rel->r_offset,
16150 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 16151 else
d830549d
AM
16152 {
16153 bfd_put_32 (input_bfd, NOP, p);
16154 goto copy_reloc;
16155 }
9a23f96e
AM
16156 }
16157 break;
16158
16159 case R_PPC64_TPREL16_LO:
16160 case R_PPC64_TPREL16_LO_DS:
16161 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16162 {
16163 bfd_byte *p = contents + (rel->r_offset & ~3);
16164 insn = bfd_get_32 (input_bfd, p);
16165 insn &= ~(0x1f << 16);
16166 insn |= 13 << 16;
16167 bfd_put_32 (input_bfd, insn, p);
16168 }
16169 break;
67f0cbdb
AM
16170 }
16171
65f38f15
AM
16172 /* Do any further special processing. */
16173 switch (r_type)
16174 {
16175 default:
16176 break;
16177
25f23106 16178 case R_PPC64_REL16_HA:
4a969973
AM
16179 case R_PPC64_REL16_HIGHA:
16180 case R_PPC64_REL16_HIGHERA:
16181 case R_PPC64_REL16_HIGHESTA:
a680de9a 16182 case R_PPC64_REL16DX_HA:
f9c6b907
AM
16183 case R_PPC64_ADDR16_HA:
16184 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16185 case R_PPC64_ADDR16_HIGHERA:
16186 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
16187 case R_PPC64_TOC16_HA:
16188 case R_PPC64_SECTOFF_HA:
411e1bfb 16189 case R_PPC64_TPREL16_HA:
f9c6b907 16190 case R_PPC64_TPREL16_HIGHA:
411e1bfb 16191 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 16192 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
16193 case R_PPC64_DTPREL16_HA:
16194 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 16195 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 16196 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
16197 /* It's just possible that this symbol is a weak symbol
16198 that's not actually defined anywhere. In that case,
16199 'sec' would be NULL, and we should leave the symbol
16200 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
16201 if (sec == NULL)
16202 break;
1a0670f3 16203 /* Fall through. */
5c5f6e17
AM
16204
16205 case R_PPC64_GOT16_HA:
16206 case R_PPC64_PLTGOT16_HA:
16207 case R_PPC64_PLT16_HA:
16208 case R_PPC64_GOT_TLSGD16_HA:
16209 case R_PPC64_GOT_TLSLD16_HA:
16210 case R_PPC64_GOT_TPREL16_HA:
16211 case R_PPC64_GOT_DTPREL16_HA:
16212 /* Add 0x10000 if sign bit in 0:15 is set.
16213 Bits 0:15 are not used. */
16214 addend += 0x8000;
65f38f15
AM
16215 break;
16216
5663e321
AM
16217 case R_PPC64_D34_HA30:
16218 case R_PPC64_ADDR16_HIGHERA34:
16219 case R_PPC64_ADDR16_HIGHESTA34:
16220 case R_PPC64_REL16_HIGHERA34:
16221 case R_PPC64_REL16_HIGHESTA34:
16222 if (sec != NULL)
16223 addend += 1ULL << 33;
16224 break;
16225
65f38f15
AM
16226 case R_PPC64_ADDR16_DS:
16227 case R_PPC64_ADDR16_LO_DS:
16228 case R_PPC64_GOT16_DS:
16229 case R_PPC64_GOT16_LO_DS:
16230 case R_PPC64_PLT16_LO_DS:
16231 case R_PPC64_SECTOFF_DS:
16232 case R_PPC64_SECTOFF_LO_DS:
16233 case R_PPC64_TOC16_DS:
16234 case R_PPC64_TOC16_LO_DS:
16235 case R_PPC64_PLTGOT16_DS:
16236 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
16237 case R_PPC64_GOT_TPREL16_DS:
16238 case R_PPC64_GOT_TPREL16_LO_DS:
16239 case R_PPC64_GOT_DTPREL16_DS:
16240 case R_PPC64_GOT_DTPREL16_LO_DS:
16241 case R_PPC64_TPREL16_DS:
16242 case R_PPC64_TPREL16_LO_DS:
16243 case R_PPC64_DTPREL16_DS:
16244 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
16245 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16246 mask = 3;
a680de9a
PB
16247 /* If this reloc is against an lq, lxv, or stxv insn, then
16248 the value must be a multiple of 16. This is somewhat of
16249 a hack, but the "correct" way to do this by defining _DQ
16250 forms of all the _DS relocs bloats all reloc switches in
16251 this file. It doesn't make much sense to use these
16252 relocs in data, so testing the insn should be safe. */
16253 if ((insn & (0x3f << 26)) == (56u << 26)
16254 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 16255 mask = 15;
a680de9a
PB
16256 relocation += addend;
16257 addend = insn & (mask ^ 3);
16258 if ((relocation & mask) != 0)
65f38f15 16259 {
a680de9a 16260 relocation ^= relocation & mask;
25f53a85 16261 info->callbacks->einfo
695344c0 16262 /* xgettext:c-format */
174d0a74 16263 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 16264 input_bfd, input_section, rel->r_offset,
d830549d 16265 ppc64_elf_howto_table[r_type]->name,
adadcc0c 16266 mask + 1);
65f38f15 16267 bfd_set_error (bfd_error_bad_value);
b34976b6 16268 ret = FALSE;
c316a17c 16269 goto copy_reloc;
65f38f15
AM
16270 }
16271 break;
5bd4f169
AM
16272 }
16273
239e1f3a
AM
16274 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16275 because such sections are not SEC_ALLOC and thus ld.so will
16276 not process them. */
d830549d 16277 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 16278 if (unresolved_reloc
239e1f3a 16279 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
16280 && h->elf.def_dynamic)
16281 && _bfd_elf_section_offset (output_bfd, info, input_section,
16282 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 16283 {
25f53a85 16284 info->callbacks->einfo
695344c0 16285 /* xgettext:c-format */
c1c8c1ef 16286 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 16287 input_bfd, input_section, rel->r_offset,
b80eed39 16288 howto->name,
039b3fef 16289 h->elf.root.root.string);
b34976b6 16290 ret = FALSE;
9c07fe7c 16291 }
5bd4f169 16292
b80eed39
AM
16293 /* 16-bit fields in insns mostly have signed values, but a
16294 few insns have 16-bit unsigned values. Really, we should
16295 have different reloc types. */
16296 if (howto->complain_on_overflow != complain_overflow_dont
16297 && howto->dst_mask == 0xffff
16298 && (input_section->flags & SEC_CODE) != 0)
16299 {
16300 enum complain_overflow complain = complain_overflow_signed;
16301
16302 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
16303 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16304 complain = complain_overflow_bitfield;
16305 else if (howto->rightshift == 0
16306 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16307 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16308 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16309 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16310 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16311 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
16312 complain = complain_overflow_unsigned;
16313 if (howto->complain_on_overflow != complain)
16314 {
16315 alt_howto = *howto;
16316 alt_howto.complain_on_overflow = complain;
16317 howto = &alt_howto;
16318 }
16319 }
16320
5663e321 16321 switch (r_type)
a680de9a 16322 {
5663e321
AM
16323 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
16324 case R_PPC64_D34:
16325 case R_PPC64_D34_LO:
16326 case R_PPC64_D34_HI30:
16327 case R_PPC64_D34_HA30:
16328 case R_PPC64_PCREL34:
16329 case R_PPC64_GOT_PCREL34:
16330 case R_PPC64_PLT_PCREL34:
16331 case R_PPC64_PLT_PCREL34_NOTOC:
16332 case R_PPC64_D28:
16333 case R_PPC64_PCREL28:
16334 if (rel->r_offset + 8 > input_section->size)
16335 r = bfd_reloc_outofrange;
16336 else
16337 {
5663e321
AM
16338 relocation += addend;
16339 if (howto->pc_relative)
16340 relocation -= (rel->r_offset
16341 + input_section->output_offset
16342 + input_section->output_section->vma);
16343 relocation >>= howto->rightshift;
16344
16345 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16346 pinsn <<= 32;
16347 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16348
16349 pinsn &= ~howto->dst_mask;
16350 pinsn |= (((relocation << 16) | (relocation & 0xffff))
16351 & howto->dst_mask);
16352 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16353 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16354 r = bfd_reloc_ok;
16355 if (howto->complain_on_overflow == complain_overflow_signed
16356 && (relocation + (1ULL << (howto->bitsize - 1))
16357 >= 1ULL << howto->bitsize))
16358 r = bfd_reloc_overflow;
16359 }
16360 break;
16361
16362 case R_PPC64_REL16DX_HA:
a680de9a
PB
16363 if (rel->r_offset + 4 > input_section->size)
16364 r = bfd_reloc_outofrange;
16365 else
16366 {
16367 relocation += addend;
16368 relocation -= (rel->r_offset
16369 + input_section->output_offset
16370 + input_section->output_section->vma);
3de43e7b 16371 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
16372 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16373 insn &= ~0x1fffc1;
3de43e7b 16374 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
16375 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16376 r = bfd_reloc_ok;
3de43e7b 16377 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
16378 r = bfd_reloc_overflow;
16379 }
5663e321
AM
16380 break;
16381
16382 default:
16383 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16384 contents, rel->r_offset,
16385 relocation, addend);
a680de9a 16386 }
5bd4f169 16387
ef60b7ff 16388 if (r != bfd_reloc_ok)
5bd4f169 16389 {
bc30df16 16390 char *more_info = NULL;
b80eed39 16391 const char *reloc_name = howto->name;
bc30df16
AM
16392
16393 if (reloc_dest != DEST_NORMAL)
16394 {
16395 more_info = bfd_malloc (strlen (reloc_name) + 8);
16396 if (more_info != NULL)
16397 {
16398 strcpy (more_info, reloc_name);
16399 strcat (more_info, (reloc_dest == DEST_OPD
16400 ? " (OPD)" : " (stub)"));
16401 reloc_name = more_info;
16402 }
16403 }
16404
cd27b276 16405 if (r == bfd_reloc_overflow)
5bd4f169 16406 {
8131c122
AM
16407 /* On code like "if (foo) foo();" don't report overflow
16408 on a branch to zero when foo is undefined. */
16409 if (!warned
16410 && (reloc_dest == DEST_STUB
16411 || !(h != NULL
16412 && (h->elf.root.type == bfd_link_hash_undefweak
16413 || h->elf.root.type == bfd_link_hash_undefined)
16414 && is_branch_reloc (r_type))))
1a72702b
AM
16415 info->callbacks->reloc_overflow (info, &h->elf.root,
16416 sym_name, reloc_name,
16417 orig_rel.r_addend,
16418 input_bfd, input_section,
16419 rel->r_offset);
ef60b7ff
AM
16420 }
16421 else
16422 {
25f53a85 16423 info->callbacks->einfo
695344c0 16424 /* xgettext:c-format */
c1c8c1ef 16425 (_("%H: %s against `%pT': error %d\n"),
25f53a85 16426 input_bfd, input_section, rel->r_offset,
bc30df16 16427 reloc_name, sym_name, (int) r);
b34976b6 16428 ret = FALSE;
ef60b7ff 16429 }
bc30df16
AM
16430 if (more_info != NULL)
16431 free (more_info);
5bd4f169 16432 }
c316a17c
AM
16433 copy_reloc:
16434 if (wrel != rel)
16435 *wrel = *rel;
16436 }
16437
16438 if (wrel != rel)
16439 {
16440 Elf_Internal_Shdr *rel_hdr;
16441 size_t deleted = rel - wrel;
16442
16443 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16444 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16445 if (rel_hdr->sh_size == 0)
16446 {
16447 /* It is too late to remove an empty reloc section. Leave
16448 one NONE reloc.
16449 ??? What is wrong with an empty section??? */
16450 rel_hdr->sh_size = rel_hdr->sh_entsize;
16451 deleted -= 1;
16452 }
16453 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16454 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16455 input_section->reloc_count -= deleted;
5bd4f169
AM
16456 }
16457
645ea6a9
AM
16458 /* If we're emitting relocations, then shortly after this function
16459 returns, reloc offsets and addends for this section will be
16460 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
16461 file rather than the input. Save a copy of the relocs for
16462 opd_entry_value. */
0e1862bb 16463 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
16464 {
16465 bfd_size_type amt;
16466 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16467 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
16468 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16469 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
16470 if (rel == NULL)
16471 return FALSE;
16472 memcpy (rel, relocs, amt);
16473 }
5bd4f169
AM
16474 return ret;
16475}
16476
754021d0
AM
16477/* Adjust the value of any local symbols in opd sections. */
16478
6e0b88f1 16479static int
754021d0
AM
16480ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16481 const char *name ATTRIBUTE_UNUSED,
16482 Elf_Internal_Sym *elfsym,
16483 asection *input_sec,
16484 struct elf_link_hash_entry *h)
16485{
74f0fb50
AM
16486 struct _opd_sec_data *opd;
16487 long adjust;
754021d0
AM
16488 bfd_vma value;
16489
4025353c 16490 if (h != NULL)
6e0b88f1 16491 return 1;
4025353c 16492
74f0fb50
AM
16493 opd = get_opd_info (input_sec);
16494 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 16495 return 1;
754021d0
AM
16496
16497 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 16498 if (!bfd_link_relocatable (info))
754021d0
AM
16499 value -= input_sec->output_section->vma;
16500
51aecdc5 16501 adjust = opd->adjust[OPD_NDX (value)];
4025353c 16502 if (adjust == -1)
6e0b88f1
AM
16503 return 2;
16504
16505 elfsym->st_value += adjust;
16506 return 1;
754021d0
AM
16507}
16508
5bd4f169
AM
16509/* Finish up dynamic symbol handling. We set the contents of various
16510 dynamic sections here. */
16511
b34976b6 16512static bfd_boolean
4ce794b7
AM
16513ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16514 struct bfd_link_info *info,
16515 struct elf_link_hash_entry *h,
4aef7643 16516 Elf_Internal_Sym *sym)
5bd4f169 16517{
65f38f15 16518 struct ppc_link_hash_table *htab;
8387904d 16519 struct plt_entry *ent;
5bd4f169 16520
65f38f15 16521 htab = ppc_hash_table (info);
4dfe6ac6
NC
16522 if (htab == NULL)
16523 return FALSE;
5bd4f169 16524
49c09209
AM
16525 if (!htab->opd_abi && !h->def_regular)
16526 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16527 if (ent->plt.offset != (bfd_vma) -1)
16528 {
16529 /* Mark the symbol as undefined, rather than as
16530 defined in glink. Leave the value if there were
16531 any relocations where pointer equality matters
16532 (this is a clue for the dynamic linker, to make
16533 function pointer comparisons work between an
16534 application and shared library), otherwise set it
16535 to zero. */
16536 sym->st_shndx = SHN_UNDEF;
16537 if (!h->pointer_equality_needed)
16538 sym->st_value = 0;
16539 else if (!h->ref_regular_nonweak)
16540 {
16541 /* This breaks function pointer comparisons, but
16542 that is better than breaking tests for a NULL
16543 function pointer. */
16544 sym->st_value = 0;
16545 }
16546 break;
16547 }
5bd4f169 16548
f5385ebf 16549 if (h->needs_copy)
5bd4f169 16550 {
65f38f15 16551 /* This symbol needs a copy reloc. Set it up. */
49c09209 16552 Elf_Internal_Rela rela;
5474d94f 16553 asection *srel;
49c09209 16554 bfd_byte *loc;
5bd4f169 16555
65f38f15
AM
16556 if (h->dynindx == -1
16557 || (h->root.type != bfd_link_hash_defined
16558 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
16559 || htab->elf.srelbss == NULL
16560 || htab->elf.sreldynrelro == NULL)
65f38f15 16561 abort ();
5bd4f169
AM
16562
16563 rela.r_offset = (h->root.u.def.value
16564 + h->root.u.def.section->output_section->vma
16565 + h->root.u.def.section->output_offset);
16566 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16567 rela.r_addend = 0;
afbf7e8e 16568 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
16569 srel = htab->elf.sreldynrelro;
16570 else
16571 srel = htab->elf.srelbss;
16572 loc = srel->contents;
16573 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 16574 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
16575 }
16576
b34976b6 16577 return TRUE;
5bd4f169
AM
16578}
16579
65f38f15
AM
16580/* Used to decide how to sort relocs in an optimal manner for the
16581 dynamic linker, before writing them out. */
16582
16583static enum elf_reloc_type_class
7e612e98
AM
16584ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16585 const asection *rel_sec,
16586 const Elf_Internal_Rela *rela)
65f38f15 16587{
04c9666a 16588 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
16589 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16590
33e44f2e 16591 if (rel_sec == htab->elf.irelplt)
7e612e98 16592 return reloc_class_ifunc;
a33d1f77 16593
4ce794b7 16594 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 16595 switch (r_type)
65f38f15
AM
16596 {
16597 case R_PPC64_RELATIVE:
16598 return reloc_class_relative;
16599 case R_PPC64_JMP_SLOT:
16600 return reloc_class_plt;
16601 case R_PPC64_COPY:
16602 return reloc_class_copy;
16603 default:
16604 return reloc_class_normal;
16605 }
16606}
16607
5bd4f169
AM
16608/* Finish up the dynamic sections. */
16609
b34976b6 16610static bfd_boolean
4ce794b7
AM
16611ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16612 struct bfd_link_info *info)
5bd4f169 16613{
65f38f15
AM
16614 struct ppc_link_hash_table *htab;
16615 bfd *dynobj;
5bd4f169 16616 asection *sdyn;
5bd4f169 16617
65f38f15 16618 htab = ppc_hash_table (info);
4dfe6ac6
NC
16619 if (htab == NULL)
16620 return FALSE;
16621
65f38f15 16622 dynobj = htab->elf.dynobj;
3d4d4302 16623 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 16624
65f38f15 16625 if (htab->elf.dynamic_sections_created)
5bd4f169 16626 {
5bd4f169
AM
16627 Elf64_External_Dyn *dyncon, *dynconend;
16628
33e44f2e 16629 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 16630 abort ();
5bd4f169
AM
16631
16632 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 16633 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
16634 for (; dyncon < dynconend; dyncon++)
16635 {
16636 Elf_Internal_Dyn dyn;
19397422 16637 asection *s;
5bd4f169
AM
16638
16639 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16640
16641 switch (dyn.d_tag)
16642 {
65f38f15
AM
16643 default:
16644 continue;
5bd4f169 16645
5d1634d7 16646 case DT_PPC64_GLINK:
4ce794b7 16647 s = htab->glink;
6348e046 16648 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
16649 /* We stupidly defined DT_PPC64_GLINK to be the start
16650 of glink rather than the first entry point, which is
16651 what ld.so needs, and now have a bigger stub to
16652 support automatic multiple TOCs. */
9e390558 16653 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
16654 break;
16655
19397422
AM
16656 case DT_PPC64_OPD:
16657 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16658 if (s == NULL)
16659 continue;
16660 dyn.d_un.d_ptr = s->vma;
19397422
AM
16661 break;
16662
e8910a83 16663 case DT_PPC64_OPT:
5663e321
AM
16664 if ((htab->do_multi_toc && htab->multi_toc_needed)
16665 || htab->notoc_plt)
e8910a83 16666 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
16667 if (htab->has_plt_localentry0)
16668 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
16669 break;
16670
19397422
AM
16671 case DT_PPC64_OPDSZ:
16672 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16673 if (s == NULL)
16674 continue;
eea6121a 16675 dyn.d_un.d_val = s->size;
19397422
AM
16676 break;
16677
65f38f15 16678 case DT_PLTGOT:
33e44f2e 16679 s = htab->elf.splt;
6348e046 16680 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
16681 break;
16682
16683 case DT_JMPREL:
33e44f2e 16684 s = htab->elf.srelplt;
6348e046 16685 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 16686 break;
5bd4f169 16687
65f38f15 16688 case DT_PLTRELSZ:
33e44f2e 16689 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 16690 break;
82e66161
AM
16691
16692 case DT_TEXTREL:
16693 if (htab->local_ifunc_resolver)
16694 info->callbacks->einfo
16695 (_("%X%P: text relocations and GNU indirect "
16696 "functions will result in a segfault at runtime\n"));
16697 else if (htab->maybe_local_ifunc_resolver)
16698 info->callbacks->einfo
16699 (_("%P: warning: text relocations and GNU indirect "
16700 "functions may result in a segfault at runtime\n"));
16701 continue;
5bd4f169 16702 }
5bd4f169 16703
65f38f15 16704 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 16705 }
5bd4f169
AM
16706 }
16707
6528b6eb
AM
16708 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16709 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16710 {
16711 /* Fill in the first entry in the global offset table.
16712 We use it to hold the link-time TOCbase. */
16713 bfd_put_64 (output_bfd,
60ee0d4a 16714 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 16715 htab->elf.sgot->contents);
5d1634d7
AM
16716
16717 /* Set .got entry size. */
2cdcc330
AM
16718 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16719 = 8;
5d1634d7
AM
16720 }
16721
6528b6eb
AM
16722 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16723 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16724 {
16725 /* Set .plt entry size. */
33e44f2e 16726 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 16727 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
16728 }
16729
84f5d08e
AM
16730 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16731 brlt ourselves if emitrelocations. */
16732 if (htab->brlt != NULL
16733 && htab->brlt->reloc_count != 0
16734 && !_bfd_elf_link_output_relocs (output_bfd,
16735 htab->brlt,
d4730f92 16736 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
16737 elf_section_data (htab->brlt)->relocs,
16738 NULL))
16739 return FALSE;
16740
176a0d42
AM
16741 if (htab->glink != NULL
16742 && htab->glink->reloc_count != 0
16743 && !_bfd_elf_link_output_relocs (output_bfd,
16744 htab->glink,
d4730f92 16745 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
16746 elf_section_data (htab->glink)->relocs,
16747 NULL))
16748 return FALSE;
16749
da44f4e5 16750
df136d64
AM
16751 if (htab->glink_eh_frame != NULL
16752 && htab->glink_eh_frame->size != 0
16753 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16754 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16755 htab->glink_eh_frame,
16756 htab->glink_eh_frame->contents))
16757 return FALSE;
58d180e8 16758
e717da7e 16759 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
16760 since we didn't add them to DYNOBJ. We know dynobj is the first
16761 bfd. */
c72f2fb2 16762 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
16763 {
16764 asection *s;
7b53ace3 16765
0c8d6e5c 16766 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
16767 continue;
16768
e717da7e
AM
16769 s = ppc64_elf_tdata (dynobj)->got;
16770 if (s != NULL
eea6121a 16771 && s->size != 0
e717da7e
AM
16772 && s->output_section != bfd_abs_section_ptr
16773 && !bfd_set_section_contents (output_bfd, s->output_section,
16774 s->contents, s->output_offset,
eea6121a 16775 s->size))
e717da7e
AM
16776 return FALSE;
16777 s = ppc64_elf_tdata (dynobj)->relgot;
16778 if (s != NULL
eea6121a 16779 && s->size != 0
e717da7e
AM
16780 && s->output_section != bfd_abs_section_ptr
16781 && !bfd_set_section_contents (output_bfd, s->output_section,
16782 s->contents, s->output_offset,
eea6121a 16783 s->size))
e717da7e
AM
16784 return FALSE;
16785 }
f6c52c13 16786
b34976b6 16787 return TRUE;
5bd4f169
AM
16788}
16789
5bd4f169 16790#include "elf64-target.h"
7b8e7dad
AM
16791
16792/* FreeBSD support */
16793
16794#undef TARGET_LITTLE_SYM
16795#undef TARGET_LITTLE_NAME
16796
16797#undef TARGET_BIG_SYM
6d00b590 16798#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
16799#undef TARGET_BIG_NAME
16800#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16801
16802#undef ELF_OSABI
16803#define ELF_OSABI ELFOSABI_FREEBSD
16804
16805#undef elf64_bed
16806#define elf64_bed elf64_powerpc_fbsd_bed
16807
16808#include "elf64-target.h"