]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
219d1afa 2 Copyright (C) 1999-2018 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 **);
805fc799 54static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 56static bfd_vma opd_entry_value
aef36ac1 57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 58
6d00b590 59#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 60#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 61#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
62#define TARGET_BIG_NAME "elf64-powerpc"
63#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 64#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
65#define ELF_MACHINE_CODE EM_PPC64
66#define ELF_MAXPAGESIZE 0x10000
702d1671
AM
67#define ELF_COMMONPAGESIZE 0x1000
68#define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
ad8e1ba5
AM
69#define elf_info_to_howto ppc64_elf_info_to_howto
70
71#define elf_backend_want_got_sym 0
72#define elf_backend_want_plt_sym 0
73#define elf_backend_plt_alignment 3
74#define elf_backend_plt_not_loaded 1
ad8e1ba5 75#define elf_backend_got_header_size 8
5474d94f 76#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
77#define elf_backend_can_gc_sections 1
78#define elf_backend_can_refcount 1
79#define elf_backend_rela_normal 1
64f52338 80#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 81#define elf_backend_default_execstack 0
ad8e1ba5 82
e717da7e 83#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 84#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 85#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
86#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
87#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
88#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
89#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 90#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 91#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 92#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
93
94#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
95#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
96#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 97#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 98#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 99#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 100#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 101#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 102#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 103#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 104#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 105#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 106#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 107#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 108#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
109#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
110#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 111#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
112#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
113#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 114#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 115#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 116#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
117#define elf_backend_relocate_section ppc64_elf_relocate_section
118#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
119#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
120#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 121#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 122#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 123#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 124#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 125#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 126
5bd4f169
AM
127/* The name of the dynamic interpreter. This is put in the .interp
128 section. */
129#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
130
131/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 132#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 133#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
134
135/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 136#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 137
a078d95a
AM
138/* Offsets to some stack save slots. */
139#define STK_LR 16
140#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 141/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
142 CR save slot. Used only by optimised __tls_get_addr call stub,
143 relying on __tls_get_addr_opt not saving CR.. */
144#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
145
5bd4f169 146/* TOC base pointers offset from start of TOC. */
411e1bfb 147#define TOC_BASE_OFF 0x8000
a27e685f
AM
148/* TOC base alignment. */
149#define TOC_BASE_ALIGN 256
411e1bfb
AM
150
151/* Offset of tp and dtp pointers from start of TLS block. */
152#define TP_OFFSET 0x7000
153#define DTP_OFFSET 0x8000
5bd4f169 154
ad8e1ba5
AM
155/* .plt call stub instructions. The normal stub is like this, but
156 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 157 insert an addi to adjust r11. */
a078d95a 158#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
159#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
160#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
161#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
162#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
163#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
164#define BCTR 0x4e800420 /* bctr */
165
07d6d2b8 166#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
167#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
168#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
169#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
170#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
171
172#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
173#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
174#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
175#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
176#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
177#define BNECTR 0x4ca20420 /* bnectr+ */
178#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 179
71a39c98 180#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 181#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 182#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 183
07d6d2b8
AM
184#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
185#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
186#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 187
07d6d2b8 188#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
05d0e962 189#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
190#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
191#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 192#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 193#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962
AM
194#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
195#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 196#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
05d0e962
AM
197#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
198#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
199#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
a345bc8d 200
9e390558
AM
201/* __glink_PLTresolve stub instructions. We enter with the index in R0. */
202#define GLINK_PLTRESOLVE_SIZE(htab) \
407aa07c 203 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
ee4bf8d2
AM
204 /* 0: */
205 /* .quad plt0-1f */
206 /* __glink: */
207#define MFLR_R12 0x7d8802a6 /* mflr %12 */
208#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
209 /* 1: */
210#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 211 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 212#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
213#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
214 /* ld %12,0(%11) */
215 /* ld %2,8(%11) */
216 /* mtctr %12 */
217 /* ld %11,16(%11) */
ee4bf8d2 218 /* bctr */
b9e5796b
AM
219#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
220#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
221#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
222#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
223#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
224
225/* Pad with this. */
226#define NOP 0x60000000
227
721956f4
AM
228/* Some other nops. */
229#define CROR_151515 0x4def7b82
230#define CROR_313131 0x4ffffb82
231
cedb70c5 232/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
233#define LI_R0_0 0x38000000 /* li %r0,0 */
234#define B_DOT 0x48000000 /* b . */
235
236/* After that, we need two instructions to load the index, followed by
237 a branch. */
238#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 239#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 240
deb0e272
AM
241/* Instructions used by the save and restore reg functions. */
242#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
243#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
244#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
245#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
246#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
247#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
248#define LI_R12_0 0x39800000 /* li %r12,0 */
249#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
250#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
251#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
252#define BLR 0x4e800020 /* blr */
253
41bd81ab
AM
254/* Since .opd is an array of descriptors and each entry will end up
255 with identical R_PPC64_RELATIVE relocs, there is really no need to
256 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 257 relocate .opd without reloc entries. */
41bd81ab
AM
258#ifndef NO_OPD_RELOCS
259#define NO_OPD_RELOCS 0
260#endif
810d4e75 261
a4b6fadd
AM
262#ifndef ARRAY_SIZE
263#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
264#endif
265
810d4e75
AM
266static inline int
267abiversion (bfd *abfd)
268{
269 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
270}
271
272static inline void
273set_abiversion (bfd *abfd, int ver)
274{
275 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
276 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
277}
5bd4f169
AM
278\f
279/* Relocation HOWTO's. */
46807bf4
AM
280/* Like other ELF RELA targets that don't apply multiple
281 field-altering relocations to the same localation, src_mask is
282 always zero and pcrel_offset is the same as pc_relative.
283 PowerPC can always use a zero bitpos, even when the field is not at
284 the LSB. For example, a REL24 could use rightshift=2, bisize=24
285 and bitpos=2 which matches the ABI description, or as we do here,
286 rightshift=0, bitsize=26 and bitpos=0. */
287#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
288 complain, special_func) \
289 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
290 complain_overflow_ ## complain, special_func, \
291 #type, FALSE, 0, mask, pc_relative)
292
04c9666a 293static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 294
f3185997
NC
295static reloc_howto_type ppc64_elf_howto_raw[] =
296{
5bd4f169 297 /* This reloc does nothing. */
46807bf4
AM
298 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
299 bfd_elf_generic_reloc),
5bd4f169
AM
300
301 /* A standard 32 bit relocation. */
46807bf4
AM
302 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
303 bfd_elf_generic_reloc),
5bd4f169
AM
304
305 /* An absolute 26 bit branch; the lower two bits must be zero.
306 FIXME: we don't check that, we just clear them. */
46807bf4
AM
307 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
308 bfd_elf_generic_reloc),
5bd4f169
AM
309
310 /* A standard 16 bit relocation. */
46807bf4
AM
311 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
312 bfd_elf_generic_reloc),
5bd4f169
AM
313
314 /* A 16 bit relocation without overflow. */
46807bf4
AM
315 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
316 bfd_elf_generic_reloc),
5bd4f169
AM
317
318 /* Bits 16-31 of an address. */
46807bf4
AM
319 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
320 bfd_elf_generic_reloc),
5bd4f169
AM
321
322 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
323 bits, treated as a signed number, is negative. */
46807bf4
AM
324 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
325 ppc64_elf_ha_reloc),
5bd4f169
AM
326
327 /* An absolute 16 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
46807bf4
AM
329 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
330 ppc64_elf_branch_reloc),
5bd4f169
AM
331
332 /* An absolute 16 bit branch, for which bit 10 should be set to
333 indicate that the branch is expected to be taken. The lower two
334 bits must be zero. */
46807bf4
AM
335 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
336 ppc64_elf_brtaken_reloc),
5bd4f169
AM
337
338 /* An absolute 16 bit branch, for which bit 10 should be set to
339 indicate that the branch is not expected to be taken. The lower
340 two bits must be zero. */
46807bf4
AM
341 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
342 ppc64_elf_brtaken_reloc),
5bd4f169
AM
343
344 /* A relative 26 bit branch; the lower two bits must be zero. */
46807bf4
AM
345 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
346 ppc64_elf_branch_reloc),
5bd4f169 347
05d0e962 348 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
46807bf4
AM
349 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
350 ppc64_elf_branch_reloc),
05d0e962 351
5bd4f169 352 /* A relative 16 bit branch; the lower two bits must be zero. */
46807bf4
AM
353 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
354 ppc64_elf_branch_reloc),
5bd4f169
AM
355
356 /* A relative 16 bit branch. Bit 10 should be set to indicate that
357 the branch is expected to be taken. The lower two bits must be
358 zero. */
46807bf4
AM
359 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
360 ppc64_elf_brtaken_reloc),
5bd4f169
AM
361
362 /* A relative 16 bit branch. Bit 10 should be set to indicate that
363 the branch is not expected to be taken. The lower two bits must
364 be zero. */
46807bf4
AM
365 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
366 ppc64_elf_brtaken_reloc),
5bd4f169
AM
367
368 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
369 symbol. */
46807bf4
AM
370 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
371 ppc64_elf_unhandled_reloc),
5bd4f169
AM
372
373 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
374 the symbol. */
46807bf4
AM
375 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
376 ppc64_elf_unhandled_reloc),
5bd4f169
AM
377
378 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
379 the symbol. */
46807bf4
AM
380 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
381 ppc64_elf_unhandled_reloc),
5bd4f169
AM
382
383 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
384 the symbol. */
46807bf4
AM
385 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
386 ppc64_elf_unhandled_reloc),
5bd4f169
AM
387
388 /* This is used only by the dynamic linker. The symbol should exist
389 both in the object being run and in some shared library. The
390 dynamic linker copies the data addressed by the symbol from the
391 shared library into the object, because the object being
392 run has to have the data at some particular address. */
46807bf4
AM
393 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
394 ppc64_elf_unhandled_reloc),
5bd4f169
AM
395
396 /* Like R_PPC64_ADDR64, but used when setting global offset table
397 entries. */
46807bf4
AM
398 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
399 ppc64_elf_unhandled_reloc),
5bd4f169
AM
400
401 /* Created by the link editor. Marks a procedure linkage table
402 entry for a symbol. */
46807bf4
AM
403 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
404 ppc64_elf_unhandled_reloc),
5bd4f169
AM
405
406 /* Used only by the dynamic linker. When the object is run, this
407 doubleword64 is set to the load address of the object, plus the
408 addend. */
46807bf4
AM
409 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
410 bfd_elf_generic_reloc),
5bd4f169
AM
411
412 /* Like R_PPC64_ADDR32, but may be unaligned. */
46807bf4
AM
413 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
414 bfd_elf_generic_reloc),
5bd4f169
AM
415
416 /* Like R_PPC64_ADDR16, but may be unaligned. */
46807bf4
AM
417 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
418 bfd_elf_generic_reloc),
5bd4f169
AM
419
420 /* 32-bit PC relative. */
46807bf4
AM
421 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
422 bfd_elf_generic_reloc),
5bd4f169 423
10ed1bba 424 /* 32-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
425 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
426 ppc64_elf_unhandled_reloc),
5bd4f169
AM
427
428 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
429 FIXME: R_PPC64_PLTREL32 not supported. */
46807bf4
AM
430 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431 ppc64_elf_unhandled_reloc),
5bd4f169
AM
432
433 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
434 the symbol. */
46807bf4
AM
435 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
436 ppc64_elf_unhandled_reloc),
5bd4f169
AM
437
438 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
439 the symbol. */
46807bf4
AM
440 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
441 ppc64_elf_unhandled_reloc),
5bd4f169
AM
442
443 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
444 the symbol. */
46807bf4
AM
445 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
446 ppc64_elf_unhandled_reloc),
5bd4f169 447
c061c2d8 448 /* 16-bit section relative relocation. */
46807bf4
AM
449 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
450 ppc64_elf_sectoff_reloc),
5bd4f169 451
c061c2d8 452 /* Like R_PPC64_SECTOFF, but no overflow warning. */
46807bf4
AM
453 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
454 ppc64_elf_sectoff_reloc),
5bd4f169
AM
455
456 /* 16-bit upper half section relative relocation. */
46807bf4
AM
457 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
458 ppc64_elf_sectoff_reloc),
5bd4f169
AM
459
460 /* 16-bit upper half adjusted section relative relocation. */
46807bf4
AM
461 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
462 ppc64_elf_sectoff_ha_reloc),
5bd4f169 463
04c9666a 464 /* Like R_PPC64_REL24 without touching the two least significant bits. */
46807bf4
AM
465 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
466 bfd_elf_generic_reloc),
5bd4f169
AM
467
468 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
469
470 /* A standard 64-bit relocation. */
46807bf4
AM
471 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
472 bfd_elf_generic_reloc),
5bd4f169
AM
473
474 /* The bits 32-47 of an address. */
46807bf4
AM
475 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
476 bfd_elf_generic_reloc),
5bd4f169
AM
477
478 /* The bits 32-47 of an address, plus 1 if the contents of the low
479 16 bits, treated as a signed number, is negative. */
46807bf4
AM
480 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
481 ppc64_elf_ha_reloc),
5bd4f169
AM
482
483 /* The bits 48-63 of an address. */
46807bf4
AM
484 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
485 bfd_elf_generic_reloc),
5bd4f169
AM
486
487 /* The bits 48-63 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_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
490 ppc64_elf_ha_reloc),
5bd4f169
AM
491
492 /* Like ADDR64, but may be unaligned. */
46807bf4
AM
493 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
494 bfd_elf_generic_reloc),
5bd4f169
AM
495
496 /* 64-bit relative relocation. */
46807bf4
AM
497 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
498 bfd_elf_generic_reloc),
5bd4f169 499
cedb70c5 500 /* 64-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
501 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
502 ppc64_elf_unhandled_reloc),
5bd4f169
AM
503
504 /* 64-bit PC relative relocation to the symbol's procedure linkage
505 table. */
506 /* FIXME: R_PPC64_PLTREL64 not supported. */
46807bf4
AM
507 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
508 ppc64_elf_unhandled_reloc),
5bd4f169
AM
509
510 /* 16 bit TOC-relative relocation. */
5bd4f169 511 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
46807bf4
AM
512 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
513 ppc64_elf_toc_reloc),
5bd4f169
AM
514
515 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 516 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
46807bf4
AM
517 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
518 ppc64_elf_toc_reloc),
5bd4f169
AM
519
520 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 521 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
46807bf4
AM
522 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
523 ppc64_elf_toc_reloc),
5bd4f169
AM
524
525 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
526 contents of the low 16 bits, treated as a signed number, is
527 negative. */
5bd4f169 528 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
46807bf4
AM
529 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
530 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
531
532 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 533 /* R_PPC64_TOC 51 doubleword64 .TOC. */
46807bf4
AM
534 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
535 ppc64_elf_toc64_reloc),
5bd4f169
AM
536
537 /* Like R_PPC64_GOT16, but also informs the link editor that the
538 value to relocate may (!) refer to a PLT entry which the link
539 editor (a) may replace with the symbol value. If the link editor
540 is unable to fully resolve the symbol, it may (b) create a PLT
541 entry and store the address to the new PLT entry in the GOT.
542 This permits lazy resolution of function symbols at run time.
543 The link editor may also skip all of this and just (c) emit a
544 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
545 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
46807bf4
AM
546 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
547 ppc64_elf_unhandled_reloc),
411e1bfb
AM
548
549 /* Like R_PPC64_PLTGOT16, but without overflow. */
550 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
551 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
552 ppc64_elf_unhandled_reloc),
411e1bfb
AM
553
554 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
555 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
46807bf4
AM
556 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
557 ppc64_elf_unhandled_reloc),
411e1bfb
AM
558
559 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
560 1 if the contents of the low 16 bits, treated as a signed number,
561 is negative. */
562 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
46807bf4
AM
563 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
564 ppc64_elf_unhandled_reloc),
411e1bfb
AM
565
566 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
46807bf4
AM
567 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
568 bfd_elf_generic_reloc),
411e1bfb
AM
569
570 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
46807bf4
AM
571 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
572 bfd_elf_generic_reloc),
411e1bfb
AM
573
574 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
46807bf4
AM
575 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
576 ppc64_elf_unhandled_reloc),
411e1bfb
AM
577
578 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
46807bf4
AM
579 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
580 ppc64_elf_unhandled_reloc),
411e1bfb
AM
581
582 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
46807bf4
AM
583 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
584 ppc64_elf_unhandled_reloc),
411e1bfb
AM
585
586 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
46807bf4
AM
587 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
588 ppc64_elf_sectoff_reloc),
411e1bfb
AM
589
590 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
46807bf4
AM
591 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
592 ppc64_elf_sectoff_reloc),
411e1bfb
AM
593
594 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
46807bf4
AM
595 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
596 ppc64_elf_toc_reloc),
411e1bfb
AM
597
598 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
46807bf4
AM
599 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
600 ppc64_elf_toc_reloc),
411e1bfb
AM
601
602 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
603 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
46807bf4
AM
604 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_unhandled_reloc),
411e1bfb
AM
606
607 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
608 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
609 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
610 ppc64_elf_unhandled_reloc),
411e1bfb 611
727fc41e 612 /* Marker relocs for TLS. */
46807bf4
AM
613 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
614 bfd_elf_generic_reloc),
615
616 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
617 bfd_elf_generic_reloc),
618
619 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
620 bfd_elf_generic_reloc),
727fc41e 621
23cedd1d
AM
622 /* Marker reloc for optimizing r2 save in prologue rather than on
623 each plt call stub. */
46807bf4
AM
624 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
625 bfd_elf_generic_reloc),
3b421ab3 626
23cedd1d 627 /* Marker relocs on inline plt call instructions. */
46807bf4
AM
628 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
629 bfd_elf_generic_reloc),
630
631 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
632 bfd_elf_generic_reloc),
23cedd1d 633
411e1bfb
AM
634 /* Computes the load module index of the load module that contains the
635 definition of its TLS sym. */
46807bf4
AM
636 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
637 ppc64_elf_unhandled_reloc),
411e1bfb
AM
638
639 /* Computes a dtv-relative displacement, the difference between the value
640 of sym+add and the base address of the thread-local storage block that
641 contains the definition of sym, minus 0x8000. */
46807bf4
AM
642 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
643 ppc64_elf_unhandled_reloc),
411e1bfb
AM
644
645 /* A 16 bit dtprel reloc. */
46807bf4
AM
646 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
647 ppc64_elf_unhandled_reloc),
411e1bfb
AM
648
649 /* Like DTPREL16, but no overflow. */
46807bf4
AM
650 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
651 ppc64_elf_unhandled_reloc),
411e1bfb
AM
652
653 /* Like DTPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
654 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
655 ppc64_elf_unhandled_reloc),
411e1bfb
AM
656
657 /* Like DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
658 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
659 ppc64_elf_unhandled_reloc),
411e1bfb
AM
660
661 /* Like DTPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
662 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
663 ppc64_elf_unhandled_reloc),
411e1bfb
AM
664
665 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
666 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
667 ppc64_elf_unhandled_reloc),
411e1bfb
AM
668
669 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
670 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
671 ppc64_elf_unhandled_reloc),
411e1bfb
AM
672
673 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
674 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
675 ppc64_elf_unhandled_reloc),
411e1bfb
AM
676
677 /* Like DTPREL16, but for insns with a DS field. */
46807bf4
AM
678 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
679 ppc64_elf_unhandled_reloc),
411e1bfb
AM
680
681 /* Like DTPREL16_DS, but no overflow. */
46807bf4
AM
682 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
683 ppc64_elf_unhandled_reloc),
411e1bfb
AM
684
685 /* Computes a tp-relative displacement, the difference between the value of
686 sym+add and the value of the thread pointer (r13). */
46807bf4
AM
687 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
688 ppc64_elf_unhandled_reloc),
411e1bfb
AM
689
690 /* A 16 bit tprel reloc. */
46807bf4
AM
691 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
692 ppc64_elf_unhandled_reloc),
411e1bfb
AM
693
694 /* Like TPREL16, but no overflow. */
46807bf4
AM
695 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
696 ppc64_elf_unhandled_reloc),
411e1bfb
AM
697
698 /* Like TPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
699 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
700 ppc64_elf_unhandled_reloc),
411e1bfb
AM
701
702 /* Like TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
703 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
704 ppc64_elf_unhandled_reloc),
411e1bfb
AM
705
706 /* Like TPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
707 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
708 ppc64_elf_unhandled_reloc),
411e1bfb
AM
709
710 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
711 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
712 ppc64_elf_unhandled_reloc),
411e1bfb
AM
713
714 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
715 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
716 ppc64_elf_unhandled_reloc),
411e1bfb
AM
717
718 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
719 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
720 ppc64_elf_unhandled_reloc),
411e1bfb
AM
721
722 /* Like TPREL16, but for insns with a DS field. */
46807bf4
AM
723 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
724 ppc64_elf_unhandled_reloc),
411e1bfb
AM
725
726 /* Like TPREL16_DS, but no overflow. */
46807bf4
AM
727 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
728 ppc64_elf_unhandled_reloc),
411e1bfb
AM
729
730 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
731 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
732 to the first entry relative to the TOC base (r2). */
46807bf4
AM
733 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
734 ppc64_elf_unhandled_reloc),
5bd4f169 735
411e1bfb 736 /* Like GOT_TLSGD16, but no overflow. */
46807bf4
AM
737 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
738 ppc64_elf_unhandled_reloc),
5bd4f169 739
411e1bfb 740 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
46807bf4
AM
741 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
742 ppc64_elf_unhandled_reloc),
5bd4f169 743
411e1bfb 744 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
46807bf4
AM
745 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
746 ppc64_elf_unhandled_reloc),
5bd4f169 747
411e1bfb
AM
748 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
749 with values (sym+add)@dtpmod and zero, and computes the offset to the
750 first entry relative to the TOC base (r2). */
46807bf4
AM
751 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
752 ppc64_elf_unhandled_reloc),
5bd4f169 753
411e1bfb 754 /* Like GOT_TLSLD16, but no overflow. */
46807bf4
AM
755 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
756 ppc64_elf_unhandled_reloc),
5bd4f169 757
411e1bfb 758 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
46807bf4
AM
759 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
760 ppc64_elf_unhandled_reloc),
5bd4f169 761
411e1bfb 762 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
46807bf4
AM
763 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
764 ppc64_elf_unhandled_reloc),
5bd4f169 765
411e1bfb
AM
766 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
767 the offset to the entry relative to the TOC base (r2). */
46807bf4
AM
768 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
5bd4f169 770
411e1bfb 771 /* Like GOT_DTPREL16_DS, but no overflow. */
46807bf4
AM
772 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
773 ppc64_elf_unhandled_reloc),
5bd4f169 774
411e1bfb 775 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
776 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
777 ppc64_elf_unhandled_reloc),
5bd4f169 778
411e1bfb 779 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
780 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
781 ppc64_elf_unhandled_reloc),
411e1bfb
AM
782
783 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
784 offset to the entry relative to the TOC base (r2). */
46807bf4
AM
785 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
5bd4f169 787
411e1bfb 788 /* Like GOT_TPREL16_DS, but no overflow. */
46807bf4
AM
789 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
790 ppc64_elf_unhandled_reloc),
5bd4f169 791
411e1bfb 792 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
793 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
794 ppc64_elf_unhandled_reloc),
5bd4f169 795
411e1bfb 796 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
797 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
798 ppc64_elf_unhandled_reloc),
799
800 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
801 ppc64_elf_unhandled_reloc),
802
803 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
804 bfd_elf_generic_reloc),
e054468f 805
25f23106 806 /* A 16 bit relative relocation. */
46807bf4
AM
807 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
808 bfd_elf_generic_reloc),
25f23106
AM
809
810 /* A 16 bit relative relocation without overflow. */
46807bf4
AM
811 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
812 bfd_elf_generic_reloc),
25f23106
AM
813
814 /* The high order 16 bits of a relative address. */
46807bf4
AM
815 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
816 bfd_elf_generic_reloc),
25f23106
AM
817
818 /* The high order 16 bits of a relative address, plus 1 if the contents of
819 the low 16 bits, treated as a signed number, is negative. */
46807bf4
AM
820 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
821 ppc64_elf_ha_reloc),
25f23106 822
a680de9a 823 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
46807bf4
AM
824 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
825 ppc64_elf_ha_reloc),
a680de9a 826
7ba71655 827 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
46807bf4
AM
828 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
829 ppc64_elf_ha_reloc),
7ba71655 830
f9c6b907 831 /* Like R_PPC64_ADDR16_HI, but no overflow. */
46807bf4
AM
832 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
833 bfd_elf_generic_reloc),
f9c6b907
AM
834
835 /* Like R_PPC64_ADDR16_HA, but no overflow. */
46807bf4
AM
836 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
837 ppc64_elf_ha_reloc),
f9c6b907
AM
838
839 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
46807bf4
AM
840 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
841 ppc64_elf_unhandled_reloc),
f9c6b907
AM
842
843 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
46807bf4
AM
844 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
845 ppc64_elf_unhandled_reloc),
f9c6b907
AM
846
847 /* Like R_PPC64_TPREL16_HI, but no overflow. */
46807bf4
AM
848 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
849 ppc64_elf_unhandled_reloc),
f9c6b907
AM
850
851 /* Like R_PPC64_TPREL16_HA, but no overflow. */
46807bf4
AM
852 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
853 ppc64_elf_unhandled_reloc),
f9c6b907 854
006589cf 855 /* Marker reloc on ELFv2 large-model function entry. */
46807bf4
AM
856 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
857 bfd_elf_generic_reloc),
006589cf 858
45965137 859 /* Like ADDR64, but use local entry point of function. */
46807bf4
AM
860 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
861 bfd_elf_generic_reloc),
45965137 862
5bd4f169 863 /* GNU extension to record C++ vtable hierarchy. */
46807bf4
AM
864 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
865 NULL),
5bd4f169
AM
866
867 /* GNU extension to record C++ vtable member usage. */
46807bf4
AM
868 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
869 NULL),
5bd4f169
AM
870};
871
872\f
873/* Initialize the ppc64_elf_howto_table, so that linear accesses can
874 be done. */
875
876static void
4ce794b7 877ppc_howto_init (void)
5bd4f169
AM
878{
879 unsigned int i, type;
880
a4b6fadd 881 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
882 {
883 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 884 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
885 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
886 }
887}
888
889static reloc_howto_type *
f3185997 890ppc64_elf_reloc_type_lookup (bfd *abfd,
4ce794b7 891 bfd_reloc_code_real_type code)
5bd4f169 892{
411e1bfb 893 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
894
895 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
896 /* Initialize howto table if needed. */
897 ppc_howto_init ();
898
4ce794b7 899 switch (code)
5bd4f169
AM
900 {
901 default:
f3185997 902 /* xgettext:c-format */
2cdcc330
AM
903 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
904 (int) code);
f3185997 905 bfd_set_error (bfd_error_bad_value);
4ce794b7 906 return NULL;
5bd4f169 907
411e1bfb
AM
908 case BFD_RELOC_NONE: r = R_PPC64_NONE;
909 break;
910 case BFD_RELOC_32: r = R_PPC64_ADDR32;
911 break;
912 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
913 break;
914 case BFD_RELOC_16: r = R_PPC64_ADDR16;
915 break;
916 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
917 break;
918 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
919 break;
f9c6b907
AM
920 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
921 break;
411e1bfb 922 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 923 break;
f9c6b907
AM
924 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
925 break;
411e1bfb 926 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 927 break;
411e1bfb 928 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 929 break;
411e1bfb 930 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 931 break;
411e1bfb 932 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 933 break;
05d0e962
AM
934 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
935 break;
411e1bfb 936 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 937 break;
411e1bfb 938 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 939 break;
411e1bfb 940 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 941 break;
411e1bfb 942 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 943 break;
411e1bfb 944 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 945 break;
411e1bfb 946 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 947 break;
411e1bfb 948 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 949 break;
411e1bfb 950 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 951 break;
411e1bfb 952 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 953 break;
411e1bfb 954 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 955 break;
411e1bfb 956 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 957 break;
411e1bfb 958 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 959 break;
411e1bfb 960 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 961 break;
411e1bfb 962 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 963 break;
411e1bfb 964 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 965 break;
411e1bfb 966 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 967 break;
411e1bfb 968 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 969 break;
411e1bfb 970 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 971 break;
411e1bfb 972 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 973 break;
411e1bfb 974 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 975 break;
411e1bfb 976 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 977 break;
411e1bfb 978 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 979 break;
411e1bfb 980 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 981 break;
411e1bfb 982 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 983 break;
411e1bfb 984 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 985 break;
411e1bfb 986 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 987 break;
411e1bfb 988 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 989 break;
411e1bfb 990 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 991 break;
411e1bfb 992 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 993 break;
411e1bfb 994 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 995 break;
411e1bfb 996 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 997 break;
411e1bfb 998 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 999 break;
411e1bfb 1000 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1001 break;
411e1bfb 1002 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1003 break;
411e1bfb 1004 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1005 break;
411e1bfb 1006 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1007 break;
411e1bfb 1008 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1009 break;
411e1bfb 1010 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1011 break;
411e1bfb 1012 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1013 break;
411e1bfb 1014 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1015 break;
411e1bfb 1016 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1017 break;
411e1bfb 1018 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1019 break;
411e1bfb 1020 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1021 break;
411e1bfb 1022 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1023 break;
411e1bfb 1024 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1025 break;
411e1bfb 1026 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1027 break;
411e1bfb 1028 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1029 break;
411e1bfb 1030 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1031 break;
411e1bfb 1032 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1033 break;
727fc41e
AM
1034 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1035 break;
1036 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1037 break;
411e1bfb 1038 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1039 break;
411e1bfb 1040 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1041 break;
411e1bfb 1042 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1043 break;
411e1bfb 1044 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1045 break;
f9c6b907
AM
1046 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1047 break;
411e1bfb 1048 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1049 break;
f9c6b907
AM
1050 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1051 break;
411e1bfb 1052 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1053 break;
411e1bfb
AM
1054 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1055 break;
1056 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1057 break;
1058 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1059 break;
f9c6b907
AM
1060 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1061 break;
411e1bfb
AM
1062 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1063 break;
f9c6b907
AM
1064 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1065 break;
411e1bfb
AM
1066 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1067 break;
1068 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1069 break;
1070 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1071 break;
1072 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1073 break;
1074 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1075 break;
1076 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1077 break;
1078 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1079 break;
1080 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1081 break;
1082 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1083 break;
1084 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1085 break;
1086 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1087 break;
1088 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1089 break;
1090 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1091 break;
1092 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1093 break;
1094 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1095 break;
1096 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1097 break;
1098 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1099 break;
1100 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1101 break;
1102 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1103 break;
1104 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1105 break;
1106 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1107 break;
1108 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1109 break;
1110 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1111 break;
1112 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1113 break;
1114 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1115 break;
1116 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1117 break;
1118 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1119 break;
1120 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1121 break;
1122 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1123 break;
25f23106
AM
1124 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1125 break;
1126 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1127 break;
1128 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1129 break;
1130 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1131 break;
7ba71655
AM
1132 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1133 break;
a680de9a
PB
1134 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1135 break;
006589cf
AM
1136 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1137 break;
45965137
AM
1138 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1139 break;
411e1bfb
AM
1140 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1141 break;
1142 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1143 break;
1144 }
1145
4ce794b7 1146 return ppc64_elf_howto_table[r];
5bd4f169
AM
1147};
1148
157090f7
AM
1149static reloc_howto_type *
1150ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1151 const char *r_name)
1152{
1153 unsigned int i;
1154
a4b6fadd 1155 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1156 if (ppc64_elf_howto_raw[i].name != NULL
1157 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1158 return &ppc64_elf_howto_raw[i];
1159
1160 return NULL;
1161}
1162
5bd4f169
AM
1163/* Set the howto pointer for a PowerPC ELF reloc. */
1164
f3185997 1165static bfd_boolean
4aef7643 1166ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1167 Elf_Internal_Rela *dst)
5bd4f169 1168{
65f38f15
AM
1169 unsigned int type;
1170
ef60b7ff 1171 /* Initialize howto table if needed. */
5bd4f169 1172 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1173 ppc_howto_init ();
1174
65f38f15 1175 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1176 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1177 {
695344c0 1178 /* xgettext:c-format */
0aa13fee 1179 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1180 abfd, type);
f3185997
NC
1181 bfd_set_error (bfd_error_bad_value);
1182 return FALSE;
d0fb9a8d 1183 }
65f38f15 1184 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1185 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1186 {
1187 /* xgettext:c-format */
1188 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1189 abfd, type);
1190 bfd_set_error (bfd_error_bad_value);
1191 return FALSE;
1192 }
2cdcc330 1193
f3185997 1194 return TRUE;
5bd4f169
AM
1195}
1196
04c9666a 1197/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1198
1199static bfd_reloc_status_type
4ce794b7
AM
1200ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1201 void *data, asection *input_section,
1202 bfd *output_bfd, char **error_message)
5bd4f169 1203{
a680de9a
PB
1204 enum elf_ppc64_reloc_type r_type;
1205 long insn;
1206 bfd_size_type octets;
3de43e7b 1207 bfd_vma value;
a680de9a 1208
805fc799
AM
1209 /* If this is a relocatable link (output_bfd test tells us), just
1210 call the generic function. Any adjustment will be done at final
1211 link time. */
1212 if (output_bfd != NULL)
cedb70c5 1213 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1214 input_section, output_bfd, error_message);
1215
1216 /* Adjust the addend for sign extension of the low 16 bits.
1217 We won't actually be using the low 16 bits, so trashing them
1218 doesn't matter. */
1219 reloc_entry->addend += 0x8000;
a680de9a
PB
1220 r_type = reloc_entry->howto->type;
1221 if (r_type != R_PPC64_REL16DX_HA)
1222 return bfd_reloc_continue;
1223
1224 value = 0;
1225 if (!bfd_is_com_section (symbol->section))
1226 value = symbol->value;
1227 value += (reloc_entry->addend
1228 + symbol->section->output_offset
1229 + symbol->section->output_section->vma);
1230 value -= (reloc_entry->address
1231 + input_section->output_offset
1232 + input_section->output_section->vma);
3de43e7b 1233 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
1234
1235 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1236 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1237 insn &= ~0x1fffc1;
3de43e7b 1238 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1239 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1240 if (value + 0x8000 > 0xffff)
a680de9a
PB
1241 return bfd_reloc_overflow;
1242 return bfd_reloc_ok;
805fc799 1243}
5bd4f169 1244
2441e016
AM
1245static bfd_reloc_status_type
1246ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1247 void *data, asection *input_section,
1248 bfd *output_bfd, char **error_message)
1249{
1250 if (output_bfd != NULL)
1251 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1252 input_section, output_bfd, error_message);
1253
699733f6
AM
1254 if (strcmp (symbol->section->name, ".opd") == 0
1255 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1256 {
1257 bfd_vma dest = opd_entry_value (symbol->section,
1258 symbol->value + reloc_entry->addend,
aef36ac1 1259 NULL, NULL, FALSE);
2441e016
AM
1260 if (dest != (bfd_vma) -1)
1261 reloc_entry->addend = dest - (symbol->value
1262 + symbol->section->output_section->vma
1263 + symbol->section->output_offset);
1264 }
810d4e75
AM
1265 else
1266 {
1267 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1268
1269 if (symbol->section->owner != abfd
9f284bf9 1270 && symbol->section->owner != NULL
810d4e75
AM
1271 && abiversion (symbol->section->owner) >= 2)
1272 {
1273 unsigned int i;
1274
1275 for (i = 0; i < symbol->section->owner->symcount; ++i)
1276 {
1277 asymbol *symdef = symbol->section->owner->outsymbols[i];
1278
1279 if (strcmp (symdef->name, symbol->name) == 0)
1280 {
1281 elfsym = (elf_symbol_type *) symdef;
1282 break;
1283 }
1284 }
1285 }
1286 reloc_entry->addend
1287 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1288 }
2441e016
AM
1289 return bfd_reloc_continue;
1290}
1291
805fc799 1292static bfd_reloc_status_type
4ce794b7
AM
1293ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1294 void *data, asection *input_section,
1295 bfd *output_bfd, char **error_message)
805fc799
AM
1296{
1297 long insn;
04c9666a 1298 enum elf_ppc64_reloc_type r_type;
805fc799 1299 bfd_size_type octets;
794e51c0
AM
1300 /* Assume 'at' branch hints. */
1301 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
1302
1303 /* If this is a relocatable link (output_bfd test tells us), just
1304 call the generic function. Any adjustment will be done at final
1305 link time. */
5bd4f169 1306 if (output_bfd != NULL)
cedb70c5 1307 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1308 input_section, output_bfd, error_message);
1309
1310 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1311 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1312 insn &= ~(0x01 << 21);
4ce794b7 1313 r_type = reloc_entry->howto->type;
805fc799
AM
1314 if (r_type == R_PPC64_ADDR14_BRTAKEN
1315 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1316 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1317
794e51c0 1318 if (is_isa_v2)
5bd4f169 1319 {
805fc799
AM
1320 /* Set 'a' bit. This is 0b00010 in BO field for branch
1321 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1322 for branch on CTR insns (BO == 1a00t or 1a01t). */
1323 if ((insn & (0x14 << 21)) == (0x04 << 21))
1324 insn |= 0x02 << 21;
1325 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1326 insn |= 0x08 << 21;
1327 else
2441e016 1328 goto out;
5bd4f169 1329 }
805fc799
AM
1330 else
1331 {
1332 bfd_vma target = 0;
1333 bfd_vma from;
5bd4f169 1334
805fc799
AM
1335 if (!bfd_is_com_section (symbol->section))
1336 target = symbol->value;
1337 target += symbol->section->output_section->vma;
1338 target += symbol->section->output_offset;
1339 target += reloc_entry->addend;
5bd4f169 1340
805fc799
AM
1341 from = (reloc_entry->address
1342 + input_section->output_offset
1343 + input_section->output_section->vma);
5bd4f169 1344
805fc799
AM
1345 /* Invert 'y' bit if not the default. */
1346 if ((bfd_signed_vma) (target - from) < 0)
1347 insn ^= 0x01 << 21;
1348 }
4ce794b7 1349 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1350 out:
1351 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1352 input_section, output_bfd, error_message);
805fc799 1353}
5bd4f169 1354
805fc799 1355static bfd_reloc_status_type
4ce794b7
AM
1356ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1357 void *data, asection *input_section,
1358 bfd *output_bfd, char **error_message)
805fc799
AM
1359{
1360 /* If this is a relocatable link (output_bfd test tells us), just
1361 call the generic function. Any adjustment will be done at final
1362 link time. */
1363 if (output_bfd != NULL)
cedb70c5 1364 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1365 input_section, output_bfd, error_message);
5bd4f169 1366
805fc799
AM
1367 /* Subtract the symbol section base address. */
1368 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1369 return bfd_reloc_continue;
1370}
1371
805fc799 1372static bfd_reloc_status_type
4ce794b7
AM
1373ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1374 void *data, asection *input_section,
1375 bfd *output_bfd, char **error_message)
805fc799
AM
1376{
1377 /* If this is a relocatable link (output_bfd test tells us), just
1378 call the generic function. Any adjustment will be done at final
1379 link time. */
1380 if (output_bfd != NULL)
cedb70c5 1381 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1382 input_section, output_bfd, error_message);
1383
1384 /* Subtract the symbol section base address. */
1385 reloc_entry->addend -= symbol->section->output_section->vma;
1386
1387 /* Adjust the addend for sign extension of the low 16 bits. */
1388 reloc_entry->addend += 0x8000;
1389 return bfd_reloc_continue;
1390}
1391
1392static bfd_reloc_status_type
4ce794b7
AM
1393ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1394 void *data, asection *input_section,
1395 bfd *output_bfd, char **error_message)
805fc799
AM
1396{
1397 bfd_vma TOCstart;
1398
1399 /* If this is a relocatable link (output_bfd test tells us), just
1400 call the generic function. Any adjustment will be done at final
1401 link time. */
1402 if (output_bfd != NULL)
cedb70c5 1403 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1404 input_section, output_bfd, error_message);
1405
1406 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1407 if (TOCstart == 0)
1c865ab2 1408 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1409
1410 /* Subtract the TOC base address. */
1411 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1412 return bfd_reloc_continue;
1413}
1414
1415static bfd_reloc_status_type
4ce794b7
AM
1416ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1417 void *data, asection *input_section,
1418 bfd *output_bfd, char **error_message)
805fc799
AM
1419{
1420 bfd_vma TOCstart;
1421
1422 /* If this is a relocatable link (output_bfd test tells us), just
1423 call the generic function. Any adjustment will be done at final
1424 link time. */
1425 if (output_bfd != NULL)
cedb70c5 1426 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1427 input_section, output_bfd, error_message);
1428
1429 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1430 if (TOCstart == 0)
1c865ab2 1431 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1432
1433 /* Subtract the TOC base address. */
1434 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1435
1436 /* Adjust the addend for sign extension of the low 16 bits. */
1437 reloc_entry->addend += 0x8000;
1438 return bfd_reloc_continue;
1439}
1440
1441static bfd_reloc_status_type
4ce794b7
AM
1442ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1443 void *data, asection *input_section,
1444 bfd *output_bfd, char **error_message)
805fc799
AM
1445{
1446 bfd_vma TOCstart;
1447 bfd_size_type octets;
1448
1449 /* If this is a relocatable link (output_bfd test tells us), just
1450 call the generic function. Any adjustment will be done at final
1451 link time. */
1452 if (output_bfd != NULL)
cedb70c5 1453 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1454 input_section, output_bfd, error_message);
1455
1456 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1457 if (TOCstart == 0)
1c865ab2 1458 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1459
1460 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1461 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1462 return bfd_reloc_ok;
1463}
1464
1465static bfd_reloc_status_type
4ce794b7
AM
1466ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1467 void *data, asection *input_section,
1468 bfd *output_bfd, char **error_message)
805fc799
AM
1469{
1470 /* If this is a relocatable link (output_bfd test tells us), just
1471 call the generic function. Any adjustment will be done at final
1472 link time. */
1473 if (output_bfd != NULL)
cedb70c5 1474 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1475 input_section, output_bfd, error_message);
1476
1477 if (error_message != NULL)
1478 {
1479 static char buf[60];
1480 sprintf (buf, "generic linker can't handle %s",
1481 reloc_entry->howto->name);
1482 *error_message = buf;
1483 }
1484 return bfd_reloc_dangerous;
1485}
1486
927be08e
AM
1487/* Track GOT entries needed for a given symbol. We might need more
1488 than one got entry per symbol. */
1489struct got_entry
1490{
1491 struct got_entry *next;
1492
1493 /* The symbol addend that we'll be placing in the GOT. */
1494 bfd_vma addend;
1495
1496 /* Unlike other ELF targets, we use separate GOT entries for the same
1497 symbol referenced from different input files. This is to support
1498 automatic multiple TOC/GOT sections, where the TOC base can vary
1499 from one input file to another. After partitioning into TOC groups
1500 we merge entries within the group.
1501
1502 Point to the BFD owning this GOT entry. */
1503 bfd *owner;
1504
1505 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1506 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1507 unsigned char tls_type;
927be08e
AM
1508
1509 /* Non-zero if got.ent points to real entry. */
f961d9dd 1510 unsigned char is_indirect;
927be08e
AM
1511
1512 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1513 union
2cdcc330
AM
1514 {
1515 bfd_signed_vma refcount;
1516 bfd_vma offset;
1517 struct got_entry *ent;
1518 } got;
927be08e
AM
1519};
1520
1521/* The same for PLT. */
1522struct plt_entry
1523{
1524 struct plt_entry *next;
1525
1526 bfd_vma addend;
1527
1528 union
2cdcc330
AM
1529 {
1530 bfd_signed_vma refcount;
1531 bfd_vma offset;
1532 } plt;
927be08e
AM
1533};
1534
e717da7e
AM
1535struct ppc64_elf_obj_tdata
1536{
1537 struct elf_obj_tdata elf;
1538
1539 /* Shortcuts to dynamic linker sections. */
1540 asection *got;
1541 asection *relgot;
1542
b3fac117
AM
1543 /* Used during garbage collection. We attach global symbols defined
1544 on removed .opd entries to this section so that the sym is removed. */
1545 asection *deleted_section;
81688140 1546
927be08e 1547 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1548 sections means we potentially need one of these for each input bfd. */
927be08e 1549 struct got_entry tlsld_got;
8860955f 1550
2cdcc330
AM
1551 union
1552 {
729eabd5
AM
1553 /* A copy of relocs before they are modified for --emit-relocs. */
1554 Elf_Internal_Rela *relocs;
1555
1556 /* Section contents. */
1557 bfd_byte *contents;
1558 } opd;
d77c8a4b
AM
1559
1560 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1561 the reloc to be in the range -32768 to 32767. */
98528052
AM
1562 unsigned int has_small_toc_reloc : 1;
1563
560c8763
AM
1564 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1565 instruction not one we handle. */
1566 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
1567};
1568
1569#define ppc64_elf_tdata(bfd) \
1570 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1571
1572#define ppc64_tlsld_got(bfd) \
1573 (&ppc64_elf_tdata (bfd)->tlsld_got)
1574
0c8d6e5c
AM
1575#define is_ppc64_elf(bfd) \
1576 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1577 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1578
e717da7e
AM
1579/* Override the generic function because we store some extras. */
1580
1581static bfd_boolean
1582ppc64_elf_mkobject (bfd *abfd)
1583{
0ffa91dd 1584 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1585 PPC64_ELF_DATA);
e717da7e
AM
1586}
1587
feee612b 1588/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1589 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1590
b34976b6 1591static bfd_boolean
4ce794b7 1592ppc64_elf_object_p (bfd *abfd)
feee612b 1593{
14b57c7c
AM
1594 if (!abfd->arch_info->the_default)
1595 return TRUE;
1596
1597 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1598 {
1599 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1600
1601 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1602 {
1603 /* Relies on arch after 32 bit default being 64 bit default. */
1604 abfd->arch_info = abfd->arch_info->next;
1605 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1606 }
1607 }
14b57c7c 1608 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1609}
1610
d37c89e5
AM
1611/* Support for core dump NOTE sections. */
1612
1613static bfd_boolean
1614ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1615{
eea6121a 1616 size_t offset, size;
d37c89e5
AM
1617
1618 if (note->descsz != 504)
1619 return FALSE;
1620
1621 /* pr_cursig */
228e534f 1622 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1623
1624 /* pr_pid */
228e534f 1625 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1626
1627 /* pr_reg */
1628 offset = 112;
eea6121a 1629 size = 384;
d37c89e5
AM
1630
1631 /* Make a ".reg/999" section. */
1632 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1633 size, note->descpos + offset);
d37c89e5
AM
1634}
1635
1636static bfd_boolean
1637ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1638{
1639 if (note->descsz != 136)
1640 return FALSE;
1641
228e534f 1642 elf_tdata (abfd)->core->pid
bc989cdc 1643 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1644 elf_tdata (abfd)->core->program
d37c89e5 1645 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1646 elf_tdata (abfd)->core->command
d37c89e5
AM
1647 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1648
1649 return TRUE;
1650}
1651
183e98be
AM
1652static char *
1653ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1654 ...)
1655{
1656 switch (note_type)
1657 {
1658 default:
1659 return NULL;
1660
1661 case NT_PRPSINFO:
1662 {
9ef6d1e3 1663 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1664 va_list ap;
1665
1666 va_start (ap, note_type);
75cd47ed 1667 memset (data, 0, sizeof (data));
183e98be 1668 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1669#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1670 DIAGNOSTIC_PUSH;
be3e27bb 1671 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1672 -Wstringop-truncation:
1673 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1674 */
95da9854
L
1675 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1676#endif
183e98be 1677 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1678#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1679 DIAGNOSTIC_POP;
fe75810f 1680#endif
183e98be
AM
1681 va_end (ap);
1682 return elfcore_write_note (abfd, buf, bufsiz,
1683 "CORE", note_type, data, sizeof (data));
1684 }
1685
1686 case NT_PRSTATUS:
1687 {
1688 char data[504];
1689 va_list ap;
1690 long pid;
1691 int cursig;
1692 const void *greg;
1693
1694 va_start (ap, note_type);
1695 memset (data, 0, 112);
1696 pid = va_arg (ap, long);
1697 bfd_put_32 (abfd, pid, data + 32);
1698 cursig = va_arg (ap, int);
1699 bfd_put_16 (abfd, cursig, data + 12);
1700 greg = va_arg (ap, const void *);
1701 memcpy (data + 112, greg, 384);
1702 memset (data + 496, 0, 8);
1703 va_end (ap);
1704 return elfcore_write_note (abfd, buf, bufsiz,
1705 "CORE", note_type, data, sizeof (data));
1706 }
1707 }
1708}
1709
5d35169e
AM
1710/* Add extra PPC sections. */
1711
2cdcc330 1712static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1713{
07d6d2b8
AM
1714 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1715 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1716 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1717 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1718 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1719 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1720 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1721};
1722
7c8fe5c4
AM
1723enum _ppc64_sec_type {
1724 sec_normal = 0,
1725 sec_opd = 1,
1726 sec_toc = 2
1727};
1728
f0abc2a1
AM
1729struct _ppc64_elf_section_data
1730{
1731 struct bfd_elf_section_data elf;
411e1bfb 1732
f0abc2a1
AM
1733 union
1734 {
51aecdc5
AM
1735 /* An array with one entry for each opd function descriptor,
1736 and some spares since opd entries may be either 16 or 24 bytes. */
1737#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1738 struct _opd_sec_data
1739 {
1740 /* Points to the function code section for local opd entries. */
1741 asection **func_sec;
1742
1743 /* After editing .opd, adjust references to opd local syms. */
1744 long *adjust;
1745 } opd;
7c8fe5c4 1746
3a71aa26
AM
1747 /* An array for toc sections, indexed by offset/8. */
1748 struct _toc_sec_data
1749 {
1750 /* Specifies the relocation symbol index used at a given toc offset. */
1751 unsigned *symndx;
1752
1753 /* And the relocation addend. */
1754 bfd_vma *add;
1755 } toc;
7c8fe5c4
AM
1756 } u;
1757
1758 enum _ppc64_sec_type sec_type:2;
411e1bfb 1759
7c8fe5c4
AM
1760 /* Flag set when small branches are detected. Used to
1761 select suitable defaults for the stub group size. */
1762 unsigned int has_14bit_branch:1;
3e04d765
AM
1763
1764 /* Flag set when PLTCALL relocs are detected. */
1765 unsigned int has_pltcall:1;
f0abc2a1
AM
1766};
1767
1768#define ppc64_elf_section_data(sec) \
411e1bfb 1769 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
1770
1771static bfd_boolean
4ce794b7 1772ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1773{
f592407e
AM
1774 if (!sec->used_by_bfd)
1775 {
1776 struct _ppc64_elf_section_data *sdata;
1777 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1778
f592407e
AM
1779 sdata = bfd_zalloc (abfd, amt);
1780 if (sdata == NULL)
1781 return FALSE;
1782 sec->used_by_bfd = sdata;
1783 }
f0abc2a1
AM
1784
1785 return _bfd_elf_new_section_hook (abfd, sec);
1786}
4025353c 1787
74f0fb50 1788static struct _opd_sec_data *
4025353c
AM
1789get_opd_info (asection * sec)
1790{
1791 if (sec != NULL
1792 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 1793 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 1794 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
1795 return NULL;
1796}
90e3cdf2
JJ
1797\f
1798/* Parameters for the qsort hook. */
90e3cdf2 1799static bfd_boolean synthetic_relocatable;
cd285db5 1800static asection *synthetic_opd;
90e3cdf2 1801
699733f6 1802/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
1803
1804static int
1805compare_symbols (const void *ap, const void *bp)
1806{
2cdcc330
AM
1807 const asymbol *a = *(const asymbol **) ap;
1808 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 1809
699733f6
AM
1810 /* Section symbols first. */
1811 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 1812 return -1;
699733f6 1813 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
1814 return 1;
1815
699733f6 1816 /* then .opd symbols. */
cd285db5
AM
1817 if (synthetic_opd != NULL)
1818 {
1819 if (strcmp (a->section->name, ".opd") == 0
1820 && strcmp (b->section->name, ".opd") != 0)
1821 return -1;
1822 if (strcmp (a->section->name, ".opd") != 0
1823 && strcmp (b->section->name, ".opd") == 0)
1824 return 1;
1825 }
90e3cdf2 1826
699733f6 1827 /* then other code symbols. */
2cdcc330
AM
1828 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1829 == (SEC_CODE | SEC_ALLOC))
1830 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1831 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
1832 return -1;
1833
2cdcc330
AM
1834 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1835 != (SEC_CODE | SEC_ALLOC))
1836 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1837 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
1838 return 1;
1839
1840 if (synthetic_relocatable)
1841 {
1842 if (a->section->id < b->section->id)
1843 return -1;
1844
1845 if (a->section->id > b->section->id)
1846 return 1;
1847 }
1848
1849 if (a->value + a->section->vma < b->value + b->section->vma)
1850 return -1;
1851
1852 if (a->value + a->section->vma > b->value + b->section->vma)
1853 return 1;
1854
4d35a0aa
AM
1855 /* For syms with the same value, prefer strong dynamic global function
1856 syms over other syms. */
1857 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
1858 return -1;
1859
1860 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
1861 return 1;
1862
1863 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
1864 return -1;
1865
1866 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
1867 return 1;
1868
1869 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
1870 return -1;
1871
1872 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
1873 return 1;
1874
1875 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
1876 return -1;
1877
1878 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
1879 return 1;
1880
aaed6f5b 1881 return a > b;
90e3cdf2
JJ
1882}
1883
699733f6 1884/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 1885
699733f6 1886static asymbol *
7292b3ac 1887sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 1888{
699733f6 1889 long mid;
90e3cdf2 1890
7292b3ac 1891 if (id == (unsigned) -1)
699733f6
AM
1892 {
1893 while (lo < hi)
1894 {
1895 mid = (lo + hi) >> 1;
1896 if (syms[mid]->value + syms[mid]->section->vma < value)
1897 lo = mid + 1;
1898 else if (syms[mid]->value + syms[mid]->section->vma > value)
1899 hi = mid;
1900 else
1901 return syms[mid];
1902 }
1903 }
1904 else
1905 {
1906 while (lo < hi)
1907 {
1908 mid = (lo + hi) >> 1;
1909 if (syms[mid]->section->id < id)
1910 lo = mid + 1;
1911 else if (syms[mid]->section->id > id)
1912 hi = mid;
1913 else if (syms[mid]->value < value)
1914 lo = mid + 1;
1915 else if (syms[mid]->value > value)
1916 hi = mid;
1917 else
1918 return syms[mid];
1919 }
1920 }
1921 return NULL;
90e3cdf2
JJ
1922}
1923
468392fb
AM
1924static bfd_boolean
1925section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
1926{
1927 bfd_vma vma = *(bfd_vma *) ptr;
1928 return ((section->flags & SEC_ALLOC) != 0
1929 && section->vma <= vma
1930 && vma < section->vma + section->size);
1931}
1932
699733f6 1933/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
1934 entry syms. Also generate @plt symbols for the glink branch table.
1935 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
1936
1937static long
a7535cf3
AM
1938ppc64_elf_get_synthetic_symtab (bfd *abfd,
1939 long static_count, asymbol **static_syms,
1940 long dyn_count, asymbol **dyn_syms,
c9727e01 1941 asymbol **ret)
90e3cdf2
JJ
1942{
1943 asymbol *s;
0ccf57bd 1944 size_t i, j, count;
90e3cdf2 1945 char *names;
0ccf57bd 1946 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 1947 asection *opd = NULL;
90e3cdf2 1948 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 1949 asymbol **syms;
ee67d69a 1950 int abi = abiversion (abfd);
90e3cdf2
JJ
1951
1952 *ret = NULL;
1953
ee67d69a
AM
1954 if (abi < 2)
1955 {
1956 opd = bfd_get_section_by_name (abfd, ".opd");
1957 if (opd == NULL && abi == 1)
1958 return 0;
1959 }
90e3cdf2 1960
a5259595
AM
1961 syms = NULL;
1962 codesecsym = 0;
1963 codesecsymend = 0;
1964 secsymend = 0;
1965 opdsymend = 0;
1966 symcount = 0;
1967 if (opd != NULL)
c9727e01 1968 {
a5259595
AM
1969 symcount = static_count;
1970 if (!relocatable)
1971 symcount += dyn_count;
1972 if (symcount == 0)
1973 return 0;
c9727e01 1974
a5259595
AM
1975 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
1976 if (syms == NULL)
1977 return -1;
90e3cdf2 1978
a5259595
AM
1979 if (!relocatable && static_count != 0 && dyn_count != 0)
1980 {
1981 /* Use both symbol tables. */
1982 memcpy (syms, static_syms, static_count * sizeof (*syms));
1983 memcpy (syms + static_count, dyn_syms,
1984 (dyn_count + 1) * sizeof (*syms));
1985 }
1986 else if (!relocatable && static_count == 0)
1987 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
1988 else
1989 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 1990
0ccf57bd
AM
1991 /* Trim uninteresting symbols. Interesting symbols are section,
1992 function, and notype symbols. */
1993 for (i = 0, j = 0; i < symcount; ++i)
1994 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
1995 | BSF_RELC | BSF_SRELC)) == 0)
1996 syms[j++] = syms[i];
1997 symcount = j;
1998
a5259595
AM
1999 synthetic_relocatable = relocatable;
2000 synthetic_opd = opd;
2001 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2002
a5259595
AM
2003 if (!relocatable && symcount > 1)
2004 {
bfa5bd2a
PA
2005 /* Trim duplicate syms, since we may have merged the normal
2006 and dynamic symbols. Actually, we only care about syms
2007 that have different values, so trim any with the same
2008 value. Don't consider ifunc and ifunc resolver symbols
2009 duplicates however, because GDB wants to know whether a
2010 text symbol is an ifunc resolver. */
a5259595 2011 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2012 {
2013 const asymbol *s0 = syms[i - 1];
2014 const asymbol *s1 = syms[i];
2015
2016 if ((s0->value + s0->section->vma
2017 != s1->value + s1->section->vma)
2018 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2019 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2020 syms[j++] = syms[i];
2021 }
a5259595
AM
2022 symcount = j;
2023 }
699733f6 2024
a5259595
AM
2025 i = 0;
2026 /* Note that here and in compare_symbols we can't compare opd and
2027 sym->section directly. With separate debug info files, the
2028 symbols will be extracted from the debug file while abfd passed
2029 to this function is the real binary. */
0ccf57bd 2030 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2031 ++i;
2032 codesecsym = i;
2033
2034 for (; i < symcount; ++i)
2035 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2036 | SEC_THREAD_LOCAL))
2037 != (SEC_CODE | SEC_ALLOC))
2038 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2039 break;
2040 codesecsymend = i;
2041
2042 for (; i < symcount; ++i)
2043 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2044 break;
2045 secsymend = i;
2046
2047 for (; i < symcount; ++i)
2048 if (strcmp (syms[i]->section->name, ".opd") != 0)
2049 break;
2050 opdsymend = i;
2051
2052 for (; i < symcount; ++i)
2cdcc330
AM
2053 if (((syms[i]->section->flags
2054 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2055 != (SEC_CODE | SEC_ALLOC))
2056 break;
2057 symcount = i;
2058 }
c9727e01 2059 count = 0;
90e3cdf2 2060
699733f6 2061 if (relocatable)
90e3cdf2 2062 {
699733f6
AM
2063 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2064 arelent *r;
2065 size_t size;
0ccf57bd 2066 size_t relcount;
90e3cdf2 2067
468392fb
AM
2068 if (opdsymend == secsymend)
2069 goto done;
2070
699733f6 2071 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2072 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2073 if (relcount == 0)
c9727e01 2074 goto done;
90e3cdf2 2075
7356fed5
AM
2076 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2077 {
2078 count = -1;
2079 goto done;
2080 }
2081
699733f6 2082 size = 0;
595da8c5 2083 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2084 {
2085 asymbol *sym;
90e3cdf2 2086
595da8c5 2087 while (r < opd->relocation + relcount
699733f6
AM
2088 && r->address < syms[i]->value + opd->vma)
2089 ++r;
90e3cdf2 2090
595da8c5 2091 if (r == opd->relocation + relcount)
699733f6 2092 break;
90e3cdf2 2093
699733f6
AM
2094 if (r->address != syms[i]->value + opd->vma)
2095 continue;
90e3cdf2 2096
699733f6
AM
2097 if (r->howto->type != R_PPC64_ADDR64)
2098 continue;
90e3cdf2 2099
699733f6
AM
2100 sym = *r->sym_ptr_ptr;
2101 if (!sym_exists_at (syms, opdsymend, symcount,
2102 sym->section->id, sym->value + r->addend))
2103 {
2104 ++count;
2105 size += sizeof (asymbol);
2106 size += strlen (syms[i]->name) + 2;
2107 }
2108 }
90e3cdf2 2109
c4b0b099
AM
2110 if (size == 0)
2111 goto done;
699733f6
AM
2112 s = *ret = bfd_malloc (size);
2113 if (s == NULL)
2114 {
7356fed5 2115 count = -1;
c9727e01 2116 goto done;
699733f6 2117 }
90e3cdf2 2118
699733f6 2119 names = (char *) (s + count);
90e3cdf2 2120
595da8c5 2121 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2122 {
699733f6 2123 asymbol *sym;
90e3cdf2 2124
595da8c5 2125 while (r < opd->relocation + relcount
699733f6
AM
2126 && r->address < syms[i]->value + opd->vma)
2127 ++r;
90e3cdf2 2128
595da8c5 2129 if (r == opd->relocation + relcount)
699733f6
AM
2130 break;
2131
2132 if (r->address != syms[i]->value + opd->vma)
2133 continue;
2134
2135 if (r->howto->type != R_PPC64_ADDR64)
2136 continue;
90e3cdf2 2137
699733f6
AM
2138 sym = *r->sym_ptr_ptr;
2139 if (!sym_exists_at (syms, opdsymend, symcount,
2140 sym->section->id, sym->value + r->addend))
2141 {
2142 size_t len;
2143
2144 *s = *syms[i];
6ba2a415 2145 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2146 s->section = sym->section;
2147 s->value = sym->value + r->addend;
2148 s->name = names;
2149 *names++ = '.';
2150 len = strlen (syms[i]->name);
2151 memcpy (names, syms[i]->name, len + 1);
2152 names += len + 1;
6f610d07
UW
2153 /* Have udata.p point back to the original symbol this
2154 synthetic symbol was derived from. */
2155 s->udata.p = syms[i];
699733f6
AM
2156 s++;
2157 }
2158 }
2159 }
2160 else
90e3cdf2 2161 {
468392fb 2162 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 2163 bfd_byte *contents = NULL;
699733f6 2164 size_t size;
0ccf57bd 2165 size_t plt_count = 0;
468392fb
AM
2166 bfd_vma glink_vma = 0, resolv_vma = 0;
2167 asection *dynamic, *glink = NULL, *relplt = NULL;
2168 arelent *p;
90e3cdf2 2169
ee67d69a 2170 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2171 {
c4b0b099
AM
2172 free_contents_and_exit_err:
2173 count = -1;
ee67d69a 2174 free_contents_and_exit:
699733f6 2175 if (contents)
ee67d69a 2176 free (contents);
c9727e01 2177 goto done;
699733f6 2178 }
90e3cdf2 2179
699733f6
AM
2180 size = 0;
2181 for (i = secsymend; i < opdsymend; ++i)
2182 {
2183 bfd_vma ent;
90e3cdf2 2184
5ef11c02
AM
2185 /* Ignore bogus symbols. */
2186 if (syms[i]->value > opd->size - 8)
2187 continue;
2188
699733f6
AM
2189 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2190 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2191 {
2192 ++count;
2193 size += sizeof (asymbol);
2194 size += strlen (syms[i]->name) + 2;
2195 }
2196 }
90e3cdf2 2197
468392fb 2198 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2199 if (dyn_count != 0
2200 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2201 {
2202 bfd_byte *dynbuf, *extdyn, *extdynend;
2203 size_t extdynsize;
2204 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2205
2206 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2207 goto free_contents_and_exit_err;
468392fb
AM
2208
2209 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2210 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2211
2212 extdyn = dynbuf;
2213 extdynend = extdyn + dynamic->size;
2214 for (; extdyn < extdynend; extdyn += extdynsize)
2215 {
2216 Elf_Internal_Dyn dyn;
2217 (*swap_dyn_in) (abfd, extdyn, &dyn);
2218
2219 if (dyn.d_tag == DT_NULL)
2220 break;
2221
2222 if (dyn.d_tag == DT_PPC64_GLINK)
2223 {
9e390558
AM
2224 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2225 See comment in ppc64_elf_finish_dynamic_sections. */
2226 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2227 /* The .glink section usually does not survive the final
2228 link; search for the section (usually .text) where the
2229 glink stubs now reside. */
2230 glink = bfd_sections_find_if (abfd, section_covers_vma,
2231 &glink_vma);
2232 break;
2233 }
2234 }
2235
2236 free (dynbuf);
2237 }
2238
2239 if (glink != NULL)
2240 {
2241 /* Determine __glink trampoline by reading the relative branch
2242 from the first glink stub. */
2243 bfd_byte buf[4];
b9e5796b
AM
2244 unsigned int off = 0;
2245
2246 while (bfd_get_section_contents (abfd, glink, buf,
2247 glink_vma + off - glink->vma, 4))
468392fb
AM
2248 {
2249 unsigned int insn = bfd_get_32 (abfd, buf);
2250 insn ^= B_DOT;
2251 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2252 {
2cdcc330
AM
2253 resolv_vma
2254 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2255 break;
2256 }
2257 off += 4;
2258 if (off > 4)
2259 break;
468392fb
AM
2260 }
2261
2262 if (resolv_vma)
2263 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2264
066ee829
AM
2265 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2266 if (relplt != NULL)
2267 {
2268 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2cdcc330 2269 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 2270 goto free_contents_and_exit_err;
68ffbac6 2271
066ee829
AM
2272 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2273 size += plt_count * sizeof (asymbol);
468392fb 2274
066ee829
AM
2275 p = relplt->relocation;
2276 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2277 {
2278 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2279 if (p->addend != 0)
2280 size += sizeof ("+0x") - 1 + 16;
2281 }
066ee829 2282 }
468392fb
AM
2283 }
2284
c4b0b099
AM
2285 if (size == 0)
2286 goto free_contents_and_exit;
699733f6
AM
2287 s = *ret = bfd_malloc (size);
2288 if (s == NULL)
c4b0b099 2289 goto free_contents_and_exit_err;
90e3cdf2 2290
468392fb 2291 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2292
699733f6 2293 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2294 {
699733f6 2295 bfd_vma ent;
90e3cdf2 2296
5ef11c02
AM
2297 if (syms[i]->value > opd->size - 8)
2298 continue;
2299
699733f6
AM
2300 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2301 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2302 {
0ccf57bd 2303 size_t lo, hi;
699733f6 2304 size_t len;
c9727e01 2305 asection *sec = abfd->sections;
90e3cdf2 2306
699733f6
AM
2307 *s = *syms[i];
2308 lo = codesecsym;
2309 hi = codesecsymend;
2310 while (lo < hi)
2311 {
0ccf57bd 2312 size_t mid = (lo + hi) >> 1;
699733f6
AM
2313 if (syms[mid]->section->vma < ent)
2314 lo = mid + 1;
2315 else if (syms[mid]->section->vma > ent)
2316 hi = mid;
2317 else
c9727e01
AM
2318 {
2319 sec = syms[mid]->section;
2320 break;
2321 }
699733f6
AM
2322 }
2323
c9727e01 2324 if (lo >= hi && lo > codesecsym)
699733f6 2325 sec = syms[lo - 1]->section;
699733f6
AM
2326
2327 for (; sec != NULL; sec = sec->next)
2328 {
2329 if (sec->vma > ent)
2330 break;
63524580
JK
2331 /* SEC_LOAD may not be set if SEC is from a separate debug
2332 info file. */
2333 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2334 break;
2335 if ((sec->flags & SEC_CODE) != 0)
2336 s->section = sec;
2337 }
6ba2a415 2338 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2339 s->value = ent - s->section->vma;
2340 s->name = names;
2341 *names++ = '.';
2342 len = strlen (syms[i]->name);
2343 memcpy (names, syms[i]->name, len + 1);
2344 names += len + 1;
6f610d07
UW
2345 /* Have udata.p point back to the original symbol this
2346 synthetic symbol was derived from. */
2347 s->udata.p = syms[i];
699733f6 2348 s++;
90e3cdf2 2349 }
90e3cdf2 2350 }
699733f6 2351 free (contents);
468392fb
AM
2352
2353 if (glink != NULL && relplt != NULL)
2354 {
2355 if (resolv_vma)
2356 {
2357 /* Add a symbol for the main glink trampoline. */
86a4952b 2358 memset (s, 0, sizeof *s);
468392fb 2359 s->the_bfd = abfd;
6ba2a415 2360 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2361 s->section = glink;
2362 s->value = resolv_vma - glink->vma;
2363 s->name = names;
2cdcc330
AM
2364 memcpy (names, "__glink_PLTresolve",
2365 sizeof ("__glink_PLTresolve"));
468392fb
AM
2366 names += sizeof ("__glink_PLTresolve");
2367 s++;
2368 count++;
2369 }
2370
2371 /* FIXME: It would be very much nicer to put sym@plt on the
2372 stub rather than on the glink branch table entry. The
2373 objdump disassembler would then use a sensible symbol
2374 name on plt calls. The difficulty in doing so is
2375 a) finding the stubs, and,
2376 b) matching stubs against plt entries, and,
2377 c) there can be multiple stubs for a given plt entry.
2378
2379 Solving (a) could be done by code scanning, but older
2380 ppc64 binaries used different stubs to current code.
2381 (b) is the tricky one since you need to known the toc
2382 pointer for at least one function that uses a pic stub to
2383 be able to calculate the plt address referenced.
2384 (c) means gdb would need to set multiple breakpoints (or
2385 find the glink branch itself) when setting breakpoints
2386 for pending shared library loads. */
2387 p = relplt->relocation;
2388 for (i = 0; i < plt_count; i++, p++)
2389 {
2390 size_t len;
2391
2392 *s = **p->sym_ptr_ptr;
2393 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2394 we are defining a symbol, ensure one of them is set. */
2395 if ((s->flags & BSF_LOCAL) == 0)
2396 s->flags |= BSF_GLOBAL;
6ba2a415 2397 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2398 s->section = glink;
2399 s->value = glink_vma - glink->vma;
2400 s->name = names;
2401 s->udata.p = NULL;
2402 len = strlen ((*p->sym_ptr_ptr)->name);
2403 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2404 names += len;
e054468f
AM
2405 if (p->addend != 0)
2406 {
2407 memcpy (names, "+0x", sizeof ("+0x") - 1);
2408 names += sizeof ("+0x") - 1;
2409 bfd_sprintf_vma (abfd, names, p->addend);
2410 names += strlen (names);
2411 }
468392fb
AM
2412 memcpy (names, "@plt", sizeof ("@plt"));
2413 names += sizeof ("@plt");
2414 s++;
b9e5796b
AM
2415 if (abi < 2)
2416 {
2417 glink_vma += 8;
2418 if (i >= 0x8000)
2419 glink_vma += 4;
2420 }
2421 else
468392fb
AM
2422 glink_vma += 4;
2423 }
2424 count += plt_count;
2425 }
90e3cdf2
JJ
2426 }
2427
c9727e01 2428 done:
a7535cf3 2429 free (syms);
90e3cdf2
JJ
2430 return count;
2431}
5bd4f169 2432\f
65f38f15
AM
2433/* The following functions are specific to the ELF linker, while
2434 functions above are used generally. Those named ppc64_elf_* are
2435 called by the main ELF linker code. They appear in this file more
2436 or less in the order in which they are called. eg.
2437 ppc64_elf_check_relocs is called early in the link process,
2438 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2439 called.
2440
2441 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2442 functions have both a function code symbol and a function descriptor
2443 symbol. A call to foo in a relocatable object file looks like:
2444
2445 . .text
2446 . x:
2447 . bl .foo
2448 . nop
2449
2450 The function definition in another object file might be:
2451
2452 . .section .opd
2453 . foo: .quad .foo
2454 . .quad .TOC.@tocbase
2455 . .quad 0
2456 .
2457 . .text
2458 . .foo: blr
2459
2460 When the linker resolves the call during a static link, the branch
2461 unsurprisingly just goes to .foo and the .opd information is unused.
2462 If the function definition is in a shared library, things are a little
2463 different: The call goes via a plt call stub, the opd information gets
2464 copied to the plt, and the linker patches the nop.
2465
2466 . x:
2467 . bl .foo_stub
2468 . ld 2,40(1)
2469 .
2470 .
2471 . .foo_stub:
71a39c98
AM
2472 . std 2,40(1) # in practice, the call stub
2473 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2474 . addi 11,11,Lfoo@toc@l # this is the general idea
2475 . ld 12,0(11)
2476 . ld 2,8(11)
2477 . mtctr 12
2478 . ld 11,16(11)
e86ce104
AM
2479 . bctr
2480 .
2481 . .section .plt
2482 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2483
2484 The "reloc ()" notation is supposed to indicate that the linker emits
2485 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2486 copying.
2487
2488 What are the difficulties here? Well, firstly, the relocations
2489 examined by the linker in check_relocs are against the function code
2490 sym .foo, while the dynamic relocation in the plt is emitted against
2491 the function descriptor symbol, foo. Somewhere along the line, we need
2492 to carefully copy dynamic link information from one symbol to the other.
2493 Secondly, the generic part of the elf linker will make .foo a dynamic
2494 symbol as is normal for most other backends. We need foo dynamic
2495 instead, at least for an application final link. However, when
2496 creating a shared library containing foo, we need to have both symbols
2497 dynamic so that references to .foo are satisfied during the early
2498 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2499 definition from some other object, eg. a static library.
2500
2501 Update: As of August 2004, we support a new convention. Function
2502 calls may use the function descriptor symbol, ie. "bl foo". This
2503 behaves exactly as "bl .foo". */
65f38f15 2504
7c8bbca5
AM
2505/* Of those relocs that might be copied as dynamic relocs, this
2506 function selects those that must be copied when linking a shared
2507 library or PIE, even when the symbol is local. */
65f38f15 2508
1d483afe
AM
2509static int
2510must_be_dyn_reloc (struct bfd_link_info *info,
2511 enum elf_ppc64_reloc_type r_type)
2512{
2513 switch (r_type)
2514 {
2515 default:
7c8bbca5
AM
2516 /* Only relative relocs can be resolved when the object load
2517 address isn't fixed. DTPREL64 is excluded because the
2518 dynamic linker needs to differentiate global dynamic from
2519 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2520 return 1;
2521
2522 case R_PPC64_REL32:
2523 case R_PPC64_REL64:
2524 case R_PPC64_REL30:
2525 return 0;
2526
2527 case R_PPC64_TPREL16:
2528 case R_PPC64_TPREL16_LO:
2529 case R_PPC64_TPREL16_HI:
2530 case R_PPC64_TPREL16_HA:
2531 case R_PPC64_TPREL16_DS:
2532 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2533 case R_PPC64_TPREL16_HIGH:
2534 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2535 case R_PPC64_TPREL16_HIGHER:
2536 case R_PPC64_TPREL16_HIGHERA:
2537 case R_PPC64_TPREL16_HIGHEST:
2538 case R_PPC64_TPREL16_HIGHESTA:
2539 case R_PPC64_TPREL64:
7c8bbca5
AM
2540 /* These relocations are relative but in a shared library the
2541 linker doesn't know the thread pointer base. */
2542 return bfd_link_dll (info);
1d483afe
AM
2543 }
2544}
65f38f15 2545
f4656909
AM
2546/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2547 copying dynamic variables from a shared lib into an app's dynbss
2548 section, and instead use a dynamic relocation to point into the
5d35169e
AM
2549 shared lib. With code that gcc generates, it's vital that this be
2550 enabled; In the PowerPC64 ABI, the address of a function is actually
2551 the address of a function descriptor, which resides in the .opd
2552 section. gcc uses the descriptor directly rather than going via the
2553 GOT as some other ABI's do, which means that initialized function
2554 pointers must reference the descriptor. Thus, a function pointer
2555 initialized to the address of a function in a shared library will
2556 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 2557 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
2558 presents a problem as a plt entry for that function is also
2559 initialized from the function descriptor symbol and the copy reloc
2560 may not be initialized first. */
a23b6845 2561#define ELIMINATE_COPY_RELOCS 1
f4656909 2562
721956f4 2563/* Section name for stubs is the associated section name plus this
29942be8
NC
2564 string. */
2565#define STUB_SUFFIX ".stub"
721956f4
AM
2566
2567/* Linker stubs.
2568 ppc_stub_long_branch:
2569 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2570 destination, but a 24 bit branch in a stub section will reach.
2571 . b dest
2572
2573 ppc_stub_plt_branch:
2574 Similar to the above, but a 24 bit branch in the stub section won't
2575 reach its destination.
71a39c98
AM
2576 . addis %r11,%r2,xxx@toc@ha
2577 . ld %r12,xxx@toc@l(%r11)
2578 . mtctr %r12
721956f4
AM
2579 . bctr
2580
2581 ppc_stub_plt_call:
2c66dc6c
AM
2582 Used to call a function in a shared library. If it so happens that
2583 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2584 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2585 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2586 . addis %r11,%r2,xxx@toc@ha
2587 . ld %r12,xxx+0@toc@l(%r11)
2588 . mtctr %r12
2589 . ld %r2,xxx+8@toc@l(%r11)
2590 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2591 . bctr
ad8e1ba5
AM
2592
2593 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2594 code to adjust the value and save r2 to support multiple toc sections.
2595 A ppc_stub_long_branch with an r2 offset looks like:
2596 . std %r2,40(%r1)
2597 . addis %r2,%r2,off@ha
2598 . addi %r2,%r2,off@l
2599 . b dest
2600
2601 A ppc_stub_plt_branch with an r2 offset looks like:
2602 . std %r2,40(%r1)
71a39c98
AM
2603 . addis %r11,%r2,xxx@toc@ha
2604 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
2605 . addis %r2,%r2,off@ha
2606 . addi %r2,%r2,off@l
71a39c98 2607 . mtctr %r12
ad8e1ba5 2608 . bctr
ac2df442 2609
05d0e962
AM
2610 All of the above stubs are shown as their ELFv1 variants. ELFv2
2611 variants exist too, simpler for plt calls since a new toc pointer
2612 and static chain are not loaded by the stub. In addition, ELFv2
2613 has some more complex stubs to handle calls marked with NOTOC
2614 relocs from functions where r2 is not a valid toc pointer. These
2615 come in two flavours, the ones shown below, and _both variants that
2616 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2617 one call is from a function where r2 is used as the toc pointer but
2618 needs a toc adjusting stub for small-model multi-toc, and another
2619 call is from a function where r2 is not valid.
2620 ppc_stub_long_branch_notoc:
2621 . mflr %r12
2622 . bcl 20,31,1f
2623 . 1:
2624 . mflr %r11
2625 . mtlr %r12
2cdcc330
AM
2626 . addis %r12,%r11,dest-1b@ha
2627 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2628 . b dest
2629
2630 ppc_stub_plt_branch_notoc:
2631 . mflr %r12
2632 . bcl 20,31,1f
2633 . 1:
2634 . mflr %r11
2635 . mtlr %r12
2636 . lis %r12,xxx-1b@highest
2637 . ori %r12,xxx-1b@higher
2638 . sldi %r12,%r12,32
2639 . oris %r12,%r12,xxx-1b@hi
2640 . ori %r12,%r12,xxx-1b@l
2641 . add %r12,%r11,%r12
2642 . mtctr %r12
2643 . bctr
2644
2645 ppc_stub_plt_call_notoc:
2646 . mflr %r12
2647 . bcl 20,31,1f
2648 . 1:
2649 . mflr %r11
2650 . mtlr %r12
2651 . lis %r12,xxx-1b@highest
2652 . ori %r12,xxx-1b@higher
2653 . sldi %r12,%r12,32
2654 . oris %r12,%r12,xxx-1b@hi
2655 . ori %r12,%r12,xxx-1b@l
2656 . ldx %r12,%r11,%r12
2657 . mtctr %r12
2658 . bctr
2659
2660 In cases where the high instructions would add zero, they are
2661 omitted and following instructions modified in some cases.
2662
2663 For a given stub group (a set of sections all using the same toc
2664 pointer value) there will be just one stub type used for any
2665 particular function symbol. For example, if printf is called from
2666 code with the tocsave optimization (ie. r2 saved in function
2667 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2668 and from other code without the tocsave optimization requiring a
2669 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2670 type will be created. Calls with the tocsave optimization will
2671 enter this stub after the instruction saving r2. A similar
2672 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2673 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2674 to call an external function like printf. If other calls to printf
2675 require a ppc_stub_plt_call linkage stub then a single
2676 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2677 call. If other calls to printf require a ppc_stub_plt_call_r2save
2678 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2679 be created and calls not requiring r2 to be saved will enter the
2680 stub after the r2 save instruction. There is an analogous
2681 hierarchy of long branch and plt branch stubs for local call
2682 linkage. */
721956f4 2683
2cdcc330
AM
2684enum ppc_stub_type
2685{
721956f4
AM
2686 ppc_stub_none,
2687 ppc_stub_long_branch,
ad8e1ba5 2688 ppc_stub_long_branch_r2off,
05d0e962
AM
2689 ppc_stub_long_branch_notoc,
2690 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2691 ppc_stub_plt_branch,
ad8e1ba5 2692 ppc_stub_plt_branch_r2off,
05d0e962
AM
2693 ppc_stub_plt_branch_notoc,
2694 ppc_stub_plt_branch_both,
794e51c0 2695 ppc_stub_plt_call,
7341d5e2 2696 ppc_stub_plt_call_r2save,
05d0e962
AM
2697 ppc_stub_plt_call_notoc,
2698 ppc_stub_plt_call_both,
a4b6fadd
AM
2699 ppc_stub_global_entry,
2700 ppc_stub_save_res
721956f4
AM
2701};
2702
6f20ed8a
AM
2703/* Information on stub grouping. */
2704struct map_stub
2705{
2706 /* The stub section. */
2707 asection *stub_sec;
2708 /* This is the section to which stubs in the group will be attached. */
2709 asection *link_sec;
a4b6fadd
AM
2710 /* Next group. */
2711 struct map_stub *next;
2712 /* Whether to emit a copy of register save/restore functions in this
2713 group. */
2714 int needs_save_res;
df136d64
AM
2715 /* Current offset within stubs after the insn restoring lr in a
2716 _notoc or _both stub using bcl for pc-relative addressing, or
2717 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2718 unsigned int lr_restore;
2719 /* Accumulated size of EH info emitted to describe return address
2720 if stubs modify lr. Does not include 17 byte FDE header. */
2721 unsigned int eh_size;
2722 /* Offset in glink_eh_frame to the start of EH info for this group. */
2723 unsigned int eh_base;
6f20ed8a
AM
2724};
2725
2cdcc330
AM
2726struct ppc_stub_hash_entry
2727{
721956f4
AM
2728 /* Base hash table entry structure. */
2729 struct bfd_hash_entry root;
2730
ad8e1ba5
AM
2731 enum ppc_stub_type stub_type;
2732
6f20ed8a
AM
2733 /* Group information. */
2734 struct map_stub *group;
721956f4
AM
2735
2736 /* Offset within stub_sec of the beginning of this stub. */
2737 bfd_vma stub_offset;
2738
2739 /* Given the symbol's value and its section we can determine its final
2740 value when building the stubs (so the stub knows where to jump. */
2741 bfd_vma target_value;
2742 asection *target_section;
2743
721956f4
AM
2744 /* The symbol table entry, if any, that this was derived from. */
2745 struct ppc_link_hash_entry *h;
e054468f 2746 struct plt_entry *plt_ent;
721956f4 2747
2d7ad24e
AM
2748 /* Symbol type. */
2749 unsigned char symtype;
2750
6911b7dc
AM
2751 /* Symbol st_other. */
2752 unsigned char other;
721956f4
AM
2753};
2754
2cdcc330
AM
2755struct ppc_branch_hash_entry
2756{
721956f4
AM
2757 /* Base hash table entry structure. */
2758 struct bfd_hash_entry root;
2759
c456f082 2760 /* Offset within branch lookup table. */
721956f4
AM
2761 unsigned int offset;
2762
2763 /* Generation marker. */
2764 unsigned int iter;
2765};
65f38f15 2766
19e08130
AM
2767/* Used to track dynamic relocations for local symbols. */
2768struct ppc_dyn_relocs
2769{
2770 struct ppc_dyn_relocs *next;
2771
2772 /* The input section of the reloc. */
2773 asection *sec;
2774
2775 /* Total number of relocs copied for the input section. */
2776 unsigned int count : 31;
2777
2778 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2779 unsigned int ifunc : 1;
2780};
2781
65f38f15
AM
2782struct ppc_link_hash_entry
2783{
2784 struct elf_link_hash_entry elf;
2785
2cdcc330
AM
2786 union
2787 {
b3fac117
AM
2788 /* A pointer to the most recently used stub hash entry against this
2789 symbol. */
2790 struct ppc_stub_hash_entry *stub_cache;
2791
2792 /* A pointer to the next symbol starting with a '.' */
2793 struct ppc_link_hash_entry *next_dot_sym;
2794 } u;
721956f4 2795
65f38f15 2796 /* Track dynamic relocs copied for this symbol. */
6061a67d 2797 struct elf_dyn_relocs *dyn_relocs;
e86ce104 2798
721956f4 2799 /* Link between function code and descriptor symbols. */
34814b9f 2800 struct ppc_link_hash_entry *oh;
721956f4 2801
e86ce104
AM
2802 /* Flag function code and descriptor symbols. */
2803 unsigned int is_func:1;
2804 unsigned int is_func_descriptor:1;
908b32fc 2805 unsigned int fake:1;
411e1bfb 2806
c5614fa4
AM
2807 /* Whether global opd/toc sym has been adjusted or not.
2808 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
2809 should be set for all globals defined in any opd/toc section. */
754021d0
AM
2810 unsigned int adjust_done:1;
2811
a4b6fadd
AM
2812 /* Set if this is an out-of-line register save/restore function,
2813 with non-standard calling convention. */
2814 unsigned int save_res:1;
2815
8b5f1ed8
AM
2816 /* Set if a duplicate symbol with non-zero localentry is detected,
2817 even when the duplicate symbol does not provide a definition. */
2818 unsigned int non_zero_localentry:1;
2819
411e1bfb 2820 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
2821 Bits are or'd into the mask as the corresponding relocs are
2822 encountered during check_relocs, with TLS_TLS being set when any
2823 of the other TLS bits are set. tls_optimize clears bits when
2824 optimizing to indicate the corresponding GOT entry type is not
2825 needed. If set, TLS_TLS is never cleared. tls_optimize may also
2826 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
2827 separate flag rather than setting TPREL just for convenience in
2828 distinguishing the two cases.
2829 These flags are also kept for local symbols. */
2830#define TLS_TLS 1 /* Any TLS reloc. */
2831#define TLS_GD 2 /* GD reloc. */
2832#define TLS_LD 4 /* LD reloc. */
2833#define TLS_TPREL 8 /* TPREL reloc, => IE. */
2834#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
2835#define TLS_MARK 32 /* __tls_get_addr call marked. */
e7b938ca 2836#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
37da22e5 2837#define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
f961d9dd 2838 unsigned char tls_mask;
37da22e5
AM
2839
2840 /* The above field is also used to mark function symbols. In which
2841 case TLS_TLS will be 0. */
2842#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 2843#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 2844#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
2845};
2846
2847/* ppc64 ELF linker hash table. */
2848
2849struct ppc_link_hash_table
2850{
2851 struct elf_link_hash_table elf;
2852
721956f4
AM
2853 /* The stub hash table. */
2854 struct bfd_hash_table stub_hash_table;
2855
2856 /* Another hash table for plt_branch stubs. */
2857 struct bfd_hash_table branch_hash_table;
2858
3b421ab3
AM
2859 /* Hash table for function prologue tocsave. */
2860 htab_t tocsave_htab;
2861
e7d1c40c
AM
2862 /* Various options and other info passed from the linker. */
2863 struct ppc64_elf_params *params;
721956f4 2864
6f20ed8a
AM
2865 /* The size of sec_info below. */
2866 unsigned int sec_info_arr_size;
2867
2868 /* Per-section array of extra section info. Done this way rather
2869 than as part of ppc64_elf_section_data so we have the info for
2870 non-ppc64 sections. */
2871 struct
2872 {
2873 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 2874 bfd_vma toc_off;
6f20ed8a
AM
2875
2876 union
2877 {
2878 /* The section group that this section belongs to. */
2879 struct map_stub *group;
2880 /* A temp section list pointer. */
2881 asection *list;
2882 } u;
2883 } *sec_info;
721956f4 2884
a4b6fadd
AM
2885 /* Linked list of groups. */
2886 struct map_stub *group;
2887
ad8e1ba5
AM
2888 /* Temp used when calculating TOC pointers. */
2889 bfd_vma toc_curr;
bf102f86
AM
2890 bfd *toc_bfd;
2891 asection *toc_first_sec;
ad8e1ba5 2892
b3fac117
AM
2893 /* Used when adding symbols. */
2894 struct ppc_link_hash_entry *dot_syms;
2895
33e44f2e 2896 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 2897 asection *glink;
9e390558 2898 asection *global_entry;
82bd7b59 2899 asection *sfpr;
2d7ad24e
AM
2900 asection *pltlocal;
2901 asection *relpltlocal;
4ce794b7
AM
2902 asection *brlt;
2903 asection *relbrlt;
58d180e8 2904 asection *glink_eh_frame;
ec338859 2905
8387904d
AM
2906 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
2907 struct ppc_link_hash_entry *tls_get_addr;
2908 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 2909
927be08e
AM
2910 /* The size of reliplt used by got entry relocs. */
2911 bfd_size_type got_reli_size;
2912
9b5ecbd0 2913 /* Statistics. */
7341d5e2 2914 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 2915
ee75fd95
AM
2916 /* Number of stubs against global syms. */
2917 unsigned long stub_globals;
2918
ee67d69a
AM
2919 /* Set if we're linking code with function descriptors. */
2920 unsigned int opd_abi:1;
2921
4c52953f 2922 /* Support for multiple toc sections. */
33c0ec9d 2923 unsigned int do_multi_toc:1;
4c52953f 2924 unsigned int multi_toc_needed:1;
927be08e 2925 unsigned int second_toc_pass:1;
67f0cbdb 2926 unsigned int do_toc_opt:1;
4c52953f 2927
9a23f96e
AM
2928 /* Set if tls optimization is enabled. */
2929 unsigned int do_tls_opt:1;
2930
3e04d765
AM
2931 /* Set if inline plt calls should be converted to direct calls. */
2932 unsigned int can_convert_all_inline_plt:1;
2933
5d1634d7 2934 /* Set on error. */
99877b66 2935 unsigned int stub_error:1;
721956f4 2936
8c5b4e52
AM
2937 /* Whether func_desc_adjust needs to be run over symbols. */
2938 unsigned int need_func_desc_adj:1;
721956f4 2939
82e66161
AM
2940 /* Whether there exist local gnu indirect function resolvers,
2941 referenced by dynamic relocations. */
2942 unsigned int local_ifunc_resolver:1;
2943 unsigned int maybe_local_ifunc_resolver:1;
2944
f378ab09
AM
2945 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
2946 unsigned int has_plt_localentry0:1;
2947
721956f4
AM
2948 /* Incremented every time we size stubs. */
2949 unsigned int stub_iteration;
5d1634d7 2950
87d72d41
AM
2951 /* Small local sym cache. */
2952 struct sym_cache sym_cache;
65f38f15
AM
2953};
2954
4c52953f
AM
2955/* Rename some of the generic section flags to better document how they
2956 are used here. */
b0dddeec
AM
2957
2958/* Nonzero if this section has TLS related relocations. */
2959#define has_tls_reloc sec_flg0
2960
23cedd1d 2961/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
2962#define has_tls_get_addr_call sec_flg1
2963
2964/* Nonzero if this section has any toc or got relocs. */
2965#define has_toc_reloc sec_flg2
2966
2967/* Nonzero if this section has a call to another section that uses
2968 the toc or got. */
d77c8a4b 2969#define makes_toc_func_call sec_flg3
b0dddeec
AM
2970
2971/* Recursion protection when determining above flag. */
d77c8a4b 2972#define call_check_in_progress sec_flg4
70cc837d 2973#define call_check_done sec_flg5
4c52953f 2974
65f38f15
AM
2975/* Get the ppc64 ELF linker hash table from a link_info structure. */
2976
2977#define ppc_hash_table(p) \
4dfe6ac6
NC
2978 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2979 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 2980
721956f4
AM
2981#define ppc_stub_hash_lookup(table, string, create, copy) \
2982 ((struct ppc_stub_hash_entry *) \
2983 bfd_hash_lookup ((table), (string), (create), (copy)))
2984
2985#define ppc_branch_hash_lookup(table, string, create, copy) \
2986 ((struct ppc_branch_hash_entry *) \
2987 bfd_hash_lookup ((table), (string), (create), (copy)))
2988
2989/* Create an entry in the stub hash table. */
2990
2991static struct bfd_hash_entry *
4ce794b7
AM
2992stub_hash_newfunc (struct bfd_hash_entry *entry,
2993 struct bfd_hash_table *table,
2994 const char *string)
721956f4
AM
2995{
2996 /* Allocate the structure if it has not already been allocated by a
2997 subclass. */
2998 if (entry == NULL)
2999 {
3000 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3001 if (entry == NULL)
3002 return entry;
3003 }
3004
3005 /* Call the allocation method of the superclass. */
3006 entry = bfd_hash_newfunc (entry, table, string);
3007 if (entry != NULL)
3008 {
3009 struct ppc_stub_hash_entry *eh;
3010
3011 /* Initialize the local fields. */
3012 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3013 eh->stub_type = ppc_stub_none;
6f20ed8a 3014 eh->group = NULL;
721956f4
AM
3015 eh->stub_offset = 0;
3016 eh->target_value = 0;
3017 eh->target_section = NULL;
721956f4 3018 eh->h = NULL;
6911b7dc 3019 eh->plt_ent = NULL;
6911b7dc 3020 eh->other = 0;
721956f4
AM
3021 }
3022
3023 return entry;
3024}
3025
3026/* Create an entry in the branch hash table. */
3027
3028static struct bfd_hash_entry *
4ce794b7
AM
3029branch_hash_newfunc (struct bfd_hash_entry *entry,
3030 struct bfd_hash_table *table,
3031 const char *string)
721956f4
AM
3032{
3033 /* Allocate the structure if it has not already been allocated by a
3034 subclass. */
3035 if (entry == NULL)
3036 {
3037 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3038 if (entry == NULL)
3039 return entry;
3040 }
3041
3042 /* Call the allocation method of the superclass. */
3043 entry = bfd_hash_newfunc (entry, table, string);
3044 if (entry != NULL)
3045 {
3046 struct ppc_branch_hash_entry *eh;
3047
3048 /* Initialize the local fields. */
3049 eh = (struct ppc_branch_hash_entry *) entry;
3050 eh->offset = 0;
3051 eh->iter = 0;
3052 }
3053
3054 return entry;
3055}
3056
65f38f15
AM
3057/* Create an entry in a ppc64 ELF linker hash table. */
3058
3059static struct bfd_hash_entry *
4ce794b7
AM
3060link_hash_newfunc (struct bfd_hash_entry *entry,
3061 struct bfd_hash_table *table,
3062 const char *string)
65f38f15
AM
3063{
3064 /* Allocate the structure if it has not already been allocated by a
3065 subclass. */
3066 if (entry == NULL)
3067 {
3068 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3069 if (entry == NULL)
3070 return entry;
3071 }
3072
3073 /* Call the allocation method of the superclass. */
3074 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3075 if (entry != NULL)
3076 {
3077 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3078
b3fac117 3079 memset (&eh->u.stub_cache, 0,
908b32fc 3080 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3081 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3082
3083 /* When making function calls, old ABI code references function entry
3084 points (dot symbols), while new ABI code references the function
3085 descriptor symbol. We need to make any combination of reference and
3086 definition work together, without breaking archive linking.
3087
3088 For a defined function "foo" and an undefined call to "bar":
3089 An old object defines "foo" and ".foo", references ".bar" (possibly
3090 "bar" too).
3091 A new object defines "foo" and references "bar".
3092
3093 A new object thus has no problem with its undefined symbols being
3094 satisfied by definitions in an old object. On the other hand, the
3095 old object won't have ".bar" satisfied by a new object.
3096
3097 Keep a list of newly added dot-symbols. */
3098
3099 if (string[0] == '.')
3100 {
3101 struct ppc_link_hash_table *htab;
3102
3103 htab = (struct ppc_link_hash_table *) table;
3104 eh->u.next_dot_sym = htab->dot_syms;
3105 htab->dot_syms = eh;
3106 }
65f38f15
AM
3107 }
3108
3109 return entry;
3110}
3111
2cdcc330
AM
3112struct tocsave_entry
3113{
3b421ab3
AM
3114 asection *sec;
3115 bfd_vma offset;
3116};
3117
3118static hashval_t
3119tocsave_htab_hash (const void *p)
3120{
3121 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3122 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3123}
3124
3125static int
3126tocsave_htab_eq (const void *p1, const void *p2)
3127{
3128 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3129 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3130 return e1->sec == e2->sec && e1->offset == e2->offset;
3131}
3132
68faa637
AM
3133/* Destroy a ppc64 ELF linker hash table. */
3134
3135static void
d495ab0d 3136ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3137{
d495ab0d 3138 struct ppc_link_hash_table *htab;
68faa637 3139
d495ab0d 3140 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3141 if (htab->tocsave_htab)
3142 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3143 bfd_hash_table_free (&htab->branch_hash_table);
3144 bfd_hash_table_free (&htab->stub_hash_table);
3145 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3146}
3147
65f38f15
AM
3148/* Create a ppc64 ELF linker hash table. */
3149
3150static struct bfd_link_hash_table *
4ce794b7 3151ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3152{
3153 struct ppc_link_hash_table *htab;
3154 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3155
4ce794b7 3156 htab = bfd_zmalloc (amt);
65f38f15
AM
3157 if (htab == NULL)
3158 return NULL;
3159
66eb6687 3160 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3161 sizeof (struct ppc_link_hash_entry),
3162 PPC64_ELF_DATA))
65f38f15 3163 {
e2d34d7d 3164 free (htab);
65f38f15
AM
3165 return NULL;
3166 }
3167
721956f4 3168 /* Init the stub hash table too. */
66eb6687
AM
3169 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3170 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3171 {
d495ab0d 3172 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3173 return NULL;
3174 }
721956f4
AM
3175
3176 /* And the branch hash table. */
66eb6687
AM
3177 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3178 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3179 {
3180 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3181 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3182 return NULL;
3183 }
721956f4 3184
3b421ab3
AM
3185 htab->tocsave_htab = htab_try_create (1024,
3186 tocsave_htab_hash,
3187 tocsave_htab_eq,
3188 NULL);
3189 if (htab->tocsave_htab == NULL)
2915c55b 3190 {
d495ab0d 3191 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3192 return NULL;
3193 }
d495ab0d 3194 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3195
3254fd24
AM
3196 /* Initializing two fields of the union is just cosmetic. We really
3197 only care about glist, but when compiled on a 32-bit host the
3198 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3199 debugger inspection of these fields look nicer. */
a6aa5195
AM
3200 htab->elf.init_got_refcount.refcount = 0;
3201 htab->elf.init_got_refcount.glist = NULL;
3202 htab->elf.init_plt_refcount.refcount = 0;
3203 htab->elf.init_plt_refcount.glist = NULL;
3204 htab->elf.init_got_offset.offset = 0;
3205 htab->elf.init_got_offset.glist = NULL;
3206 htab->elf.init_plt_offset.offset = 0;
3207 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3208
65f38f15
AM
3209 return &htab->elf.root;
3210}
3211
bfeb4a28
AM
3212/* Create sections for linker generated code. */
3213
3214static bfd_boolean
3215create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3216{
3217 struct ppc_link_hash_table *htab;
3218 flagword flags;
3219
3220 htab = ppc_hash_table (info);
3221
bfeb4a28
AM
3222 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3223 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3224 if (htab->params->save_restore_funcs)
3225 {
3226 /* Create .sfpr for code to save and restore fp regs. */
3227 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3228 flags);
3229 if (htab->sfpr == NULL
2cdcc330 3230 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b32547cd
AM
3231 return FALSE;
3232 }
3233
3234 if (bfd_link_relocatable (info))
3235 return TRUE;
bfeb4a28
AM
3236
3237 /* Create .glink for lazy dynamic linking support. */
3238 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3239 flags);
3240 if (htab->glink == NULL
2cdcc330 3241 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
bfeb4a28
AM
3242 return FALSE;
3243
9e390558
AM
3244 /* The part of .glink used by global entry stubs, separate so that
3245 it can be aligned appropriately without affecting htab->glink. */
3246 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3247 flags);
3248 if (htab->global_entry == NULL
2cdcc330 3249 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
9e390558
AM
3250 return FALSE;
3251
bfeb4a28
AM
3252 if (!info->no_ld_generated_unwind_info)
3253 {
3254 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3255 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3256 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3257 ".eh_frame",
3258 flags);
3259 if (htab->glink_eh_frame == NULL
3260 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3261 return FALSE;
3262 }
3263
3264 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3265 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3266 if (htab->elf.iplt == NULL
2cdcc330 3267 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
3268 return FALSE;
3269
3270 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3271 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3272 htab->elf.irelplt
3273 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3274 if (htab->elf.irelplt == NULL
2cdcc330 3275 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
3276 return FALSE;
3277
3278 /* Create branch lookup table for plt_branch stubs. */
3279 flags = (SEC_ALLOC | SEC_LOAD
3280 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3281 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3282 flags);
3283 if (htab->brlt == NULL
2cdcc330 3284 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
bfeb4a28
AM
3285 return FALSE;
3286
2d7ad24e
AM
3287 /* Local plt entries, put in .branch_lt but a separate section for
3288 convenience. */
3289 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3290 flags);
3291 if (htab->pltlocal == NULL
2cdcc330 3292 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
2d7ad24e
AM
3293 return FALSE;
3294
0e1862bb 3295 if (!bfd_link_pic (info))
bfeb4a28
AM
3296 return TRUE;
3297
3298 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3299 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3300 htab->relbrlt
3301 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3302 if (htab->relbrlt == NULL
2cdcc330 3303 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
bfeb4a28
AM
3304 return FALSE;
3305
2d7ad24e
AM
3306 htab->relpltlocal
3307 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3308 if (htab->relpltlocal == NULL
2cdcc330 3309 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
2d7ad24e
AM
3310 return FALSE;
3311
bfeb4a28
AM
3312 return TRUE;
3313}
3314
e717da7e
AM
3315/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3316
bfeb4a28 3317bfd_boolean
e7d1c40c
AM
3318ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3319 struct ppc64_elf_params *params)
e717da7e
AM
3320{
3321 struct ppc_link_hash_table *htab;
3322
e7d1c40c 3323 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3324
3325/* Always hook our dynamic sections into the first bfd, which is the
3326 linker created stub bfd. This ensures that the GOT header is at
3327 the start of the output TOC section. */
3328 htab = ppc_hash_table (info);
e7d1c40c
AM
3329 htab->elf.dynobj = params->stub_bfd;
3330 htab->params = params;
bfeb4a28 3331
bfeb4a28 3332 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3333}
3334
721956f4
AM
3335/* Build a name for an entry in the stub hash table. */
3336
3337static char *
4ce794b7
AM
3338ppc_stub_name (const asection *input_section,
3339 const asection *sym_sec,
3340 const struct ppc_link_hash_entry *h,
3341 const Elf_Internal_Rela *rel)
721956f4
AM
3342{
3343 char *stub_name;
bcaa2f82 3344 ssize_t len;
721956f4
AM
3345
3346 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3347 offsets from a sym as a branch target? In fact, we could
3348 probably assume the addend is always zero. */
3349 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3350
3351 if (h)
3352 {
3353 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3354 stub_name = bfd_malloc (len);
46de2a7c
AM
3355 if (stub_name == NULL)
3356 return stub_name;
3357
bcaa2f82
AM
3358 len = sprintf (stub_name, "%08x.%s+%x",
3359 input_section->id & 0xffffffff,
3360 h->elf.root.root.string,
3361 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3362 }
3363 else
3364 {
ad8e1ba5 3365 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3366 stub_name = bfd_malloc (len);
46de2a7c
AM
3367 if (stub_name == NULL)
3368 return stub_name;
3369
bcaa2f82
AM
3370 len = sprintf (stub_name, "%08x.%x:%x+%x",
3371 input_section->id & 0xffffffff,
3372 sym_sec->id & 0xffffffff,
3373 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3374 (int) rel->r_addend & 0xffffffff);
721956f4 3375 }
bcaa2f82 3376 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3377 stub_name[len - 2] = 0;
721956f4
AM
3378 return stub_name;
3379}
3380
3381/* Look up an entry in the stub hash. Stub entries are cached because
3382 creating the stub name takes a bit of time. */
3383
3384static struct ppc_stub_hash_entry *
4ce794b7
AM
3385ppc_get_stub_entry (const asection *input_section,
3386 const asection *sym_sec,
039b3fef 3387 struct ppc_link_hash_entry *h,
4ce794b7
AM
3388 const Elf_Internal_Rela *rel,
3389 struct ppc_link_hash_table *htab)
721956f4
AM
3390{
3391 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3392 struct map_stub *group;
721956f4
AM
3393
3394 /* If this input section is part of a group of sections sharing one
3395 stub section, then use the id of the first section in the group.
3396 Stub names need to include a section id, as there may well be
3397 more than one stub used to reach say, printf, and we need to
3398 distinguish between them. */
6f20ed8a 3399 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3400 if (group == NULL)
3401 return NULL;
721956f4 3402
b3fac117
AM
3403 if (h != NULL && h->u.stub_cache != NULL
3404 && h->u.stub_cache->h == h
6f20ed8a 3405 && h->u.stub_cache->group == group)
721956f4 3406 {
b3fac117 3407 stub_entry = h->u.stub_cache;
721956f4
AM
3408 }
3409 else
3410 {
3411 char *stub_name;
3412
6f20ed8a 3413 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3414 if (stub_name == NULL)
3415 return NULL;
3416
3417 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3418 stub_name, FALSE, FALSE);
721956f4 3419 if (h != NULL)
b3fac117 3420 h->u.stub_cache = stub_entry;
721956f4
AM
3421
3422 free (stub_name);
3423 }
3424
3425 return stub_entry;
3426}
3427
3428/* Add a new stub entry to the stub hash. Not all fields of the new
3429 stub entry are initialised. */
3430
3431static struct ppc_stub_hash_entry *
4ce794b7
AM
3432ppc_add_stub (const char *stub_name,
3433 asection *section,
25f53a85 3434 struct bfd_link_info *info)
721956f4 3435{
25f53a85 3436 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3437 struct map_stub *group;
721956f4
AM
3438 asection *link_sec;
3439 asection *stub_sec;
3440 struct ppc_stub_hash_entry *stub_entry;
3441
6f20ed8a
AM
3442 group = htab->sec_info[section->id].u.group;
3443 link_sec = group->link_sec;
3444 stub_sec = group->stub_sec;
721956f4
AM
3445 if (stub_sec == NULL)
3446 {
6f20ed8a
AM
3447 size_t namelen;
3448 bfd_size_type len;
3449 char *s_name;
721956f4 3450
6f20ed8a
AM
3451 namelen = strlen (link_sec->name);
3452 len = namelen + sizeof (STUB_SUFFIX);
3453 s_name = bfd_alloc (htab->params->stub_bfd, len);
3454 if (s_name == NULL)
3455 return NULL;
721956f4 3456
6f20ed8a
AM
3457 memcpy (s_name, link_sec->name, namelen);
3458 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3459 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3460 if (stub_sec == NULL)
3461 return NULL;
3462 group->stub_sec = stub_sec;
721956f4
AM
3463 }
3464
3465 /* Enter this entry into the linker stub hash table. */
3466 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3467 TRUE, FALSE);
721956f4
AM
3468 if (stub_entry == NULL)
3469 {
695344c0 3470 /* xgettext:c-format */
cf97bcb0
AM
3471 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3472 section->owner, stub_name);
721956f4
AM
3473 return NULL;
3474 }
3475
6f20ed8a 3476 stub_entry->group = group;
721956f4 3477 stub_entry->stub_offset = 0;
721956f4
AM
3478 return stub_entry;
3479}
3480
e717da7e
AM
3481/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3482 not already done. */
65f38f15 3483
b34976b6 3484static bfd_boolean
e717da7e 3485create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3486{
e717da7e
AM
3487 asection *got, *relgot;
3488 flagword flags;
3489 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3490
0c8d6e5c 3491 if (!is_ppc64_elf (abfd))
0ffa91dd 3492 return FALSE;
4dfe6ac6
NC
3493 if (htab == NULL)
3494 return FALSE;
0ffa91dd 3495
33e44f2e
AM
3496 if (!htab->elf.sgot
3497 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3498 return FALSE;
e717da7e
AM
3499
3500 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3501 | SEC_LINKER_CREATED);
3502
c456f082 3503 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3504 if (!got
e717da7e
AM
3505 || !bfd_set_section_alignment (abfd, got, 3))
3506 return FALSE;
65f38f15 3507
c456f082
AM
3508 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3509 flags | SEC_READONLY);
e717da7e 3510 if (!relgot
2cdcc330 3511 || !bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 3512 return FALSE;
e717da7e
AM
3513
3514 ppc64_elf_tdata (abfd)->got = got;
3515 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3516 return TRUE;
65f38f15 3517}
5bd4f169 3518
b31867b6
AM
3519/* Follow indirect and warning symbol links. */
3520
3521static inline struct bfd_link_hash_entry *
3522follow_link (struct bfd_link_hash_entry *h)
3523{
3524 while (h->type == bfd_link_hash_indirect
3525 || h->type == bfd_link_hash_warning)
3526 h = h->u.i.link;
3527 return h;
3528}
3529
3530static inline struct elf_link_hash_entry *
3531elf_follow_link (struct elf_link_hash_entry *h)
3532{
3533 return (struct elf_link_hash_entry *) follow_link (&h->root);
3534}
3535
3536static inline struct ppc_link_hash_entry *
3537ppc_follow_link (struct ppc_link_hash_entry *h)
3538{
3539 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3540}
3541
40d16e0b
AM
3542/* Merge PLT info on FROM with that on TO. */
3543
3544static void
3545move_plt_plist (struct ppc_link_hash_entry *from,
3546 struct ppc_link_hash_entry *to)
3547{
3548 if (from->elf.plt.plist != NULL)
3549 {
3550 if (to->elf.plt.plist != NULL)
3551 {
3552 struct plt_entry **entp;
3553 struct plt_entry *ent;
3554
3555 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3556 {
3557 struct plt_entry *dent;
3558
3559 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3560 if (dent->addend == ent->addend)
3561 {
3562 dent->plt.refcount += ent->plt.refcount;
3563 *entp = ent->next;
3564 break;
3565 }
3566 if (dent == NULL)
3567 entp = &ent->next;
3568 }
3569 *entp = to->elf.plt.plist;
3570 }
3571
3572 to->elf.plt.plist = from->elf.plt.plist;
3573 from->elf.plt.plist = NULL;
3574 }
3575}
3576
65f38f15
AM
3577/* Copy the extra info we tack onto an elf_link_hash_entry. */
3578
3579static void
fcfa13d2
AM
3580ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3581 struct elf_link_hash_entry *dir,
3582 struct elf_link_hash_entry *ind)
65f38f15
AM
3583{
3584 struct ppc_link_hash_entry *edir, *eind;
3585
3586 edir = (struct ppc_link_hash_entry *) dir;
3587 eind = (struct ppc_link_hash_entry *) ind;
3588
c79d6685
AM
3589 edir->is_func |= eind->is_func;
3590 edir->is_func_descriptor |= eind->is_func_descriptor;
3591 edir->tls_mask |= eind->tls_mask;
3592 if (eind->oh != NULL)
3593 edir->oh = ppc_follow_link (eind->oh);
3594
474436e6 3595 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3596 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3597 edir->elf.ref_regular |= eind->elf.ref_regular;
3598 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3599 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3600 edir->elf.needs_plt |= eind->elf.needs_plt;
3601 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3602
d311bc8b
AM
3603 /* If we were called to copy over info for a weak sym, don't copy
3604 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3605 in order to simplify readonly_dynrelocs and save a field in the
3606 symbol hash entry, but that means dyn_relocs can't be used in any
3607 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3608 are then tested. */
d311bc8b 3609 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3610 return;
d311bc8b 3611
411e1bfb 3612 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3613 if (eind->dyn_relocs != NULL)
65f38f15 3614 {
bbd7ec4a
AM
3615 if (edir->dyn_relocs != NULL)
3616 {
6061a67d
AM
3617 struct elf_dyn_relocs **pp;
3618 struct elf_dyn_relocs *p;
bbd7ec4a 3619
fcfa13d2 3620 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
3621 list. Merge any entries against the same section. */
3622 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3623 {
6061a67d 3624 struct elf_dyn_relocs *q;
bbd7ec4a
AM
3625
3626 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3627 if (q->sec == p->sec)
3628 {
3629 q->pc_count += p->pc_count;
3630 q->count += p->count;
3631 *pp = p->next;
3632 break;
3633 }
3634 if (q == NULL)
3635 pp = &p->next;
3636 }
3637 *pp = edir->dyn_relocs;
3638 }
3639
65f38f15
AM
3640 edir->dyn_relocs = eind->dyn_relocs;
3641 eind->dyn_relocs = NULL;
3642 }
65f38f15 3643
81848ca0
AM
3644 /* Copy over got entries that we may have already seen to the
3645 symbol which just became indirect. */
411e1bfb
AM
3646 if (eind->elf.got.glist != NULL)
3647 {
3648 if (edir->elf.got.glist != NULL)
3649 {
3650 struct got_entry **entp;
3651 struct got_entry *ent;
3652
3653 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3654 {
3655 struct got_entry *dent;
3656
3657 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3658 if (dent->addend == ent->addend
e717da7e 3659 && dent->owner == ent->owner
411e1bfb
AM
3660 && dent->tls_type == ent->tls_type)
3661 {
3662 dent->got.refcount += ent->got.refcount;
3663 *entp = ent->next;
3664 break;
3665 }
3666 if (dent == NULL)
3667 entp = &ent->next;
3668 }
3669 *entp = edir->elf.got.glist;
3670 }
3671
3672 edir->elf.got.glist = eind->elf.got.glist;
3673 eind->elf.got.glist = NULL;
3674 }
3675
3676 /* And plt entries. */
40d16e0b 3677 move_plt_plist (eind, edir);
411e1bfb 3678
fcfa13d2 3679 if (eind->elf.dynindx != -1)
411e1bfb 3680 {
fcfa13d2
AM
3681 if (edir->elf.dynindx != -1)
3682 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3683 edir->elf.dynstr_index);
411e1bfb
AM
3684 edir->elf.dynindx = eind->elf.dynindx;
3685 edir->elf.dynstr_index = eind->elf.dynstr_index;
3686 eind->elf.dynindx = -1;
3687 eind->elf.dynstr_index = 0;
3688 }
411e1bfb
AM
3689}
3690
8387904d
AM
3691/* Find the function descriptor hash entry from the given function code
3692 hash entry FH. Link the entries via their OH fields. */
3693
3694static struct ppc_link_hash_entry *
b31867b6 3695lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
3696{
3697 struct ppc_link_hash_entry *fdh = fh->oh;
3698
3699 if (fdh == NULL)
3700 {
3701 const char *fd_name = fh->elf.root.root.string + 1;
3702
3703 fdh = (struct ppc_link_hash_entry *)
3704 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
3705 if (fdh == NULL)
3706 return fdh;
3707
3708 fdh->is_func_descriptor = 1;
3709 fdh->oh = fh;
3710 fh->is_func = 1;
3711 fh->oh = fdh;
8387904d
AM
3712 }
3713
8c5b4e52
AM
3714 fdh = ppc_follow_link (fdh);
3715 fdh->is_func_descriptor = 1;
3716 fdh->oh = fh;
3717 return fdh;
8387904d
AM
3718}
3719
8c5b4e52 3720/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
3721
3722static struct ppc_link_hash_entry *
3723make_fdh (struct bfd_link_info *info,
908b32fc 3724 struct ppc_link_hash_entry *fh)
bb700d78 3725{
8c5b4e52
AM
3726 bfd *abfd = fh->elf.root.u.undef.abfd;
3727 struct bfd_link_hash_entry *bh = NULL;
bb700d78 3728 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
3729 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3730 ? BSF_WEAK
3731 : BSF_GLOBAL);
3732
3733 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3734 fh->elf.root.root.string + 1,
3735 flags, bfd_und_section_ptr, 0,
3736 NULL, FALSE, FALSE, &bh))
bb700d78
AM
3737 return NULL;
3738
3739 fdh = (struct ppc_link_hash_entry *) bh;
3740 fdh->elf.non_elf = 0;
908b32fc
AM
3741 fdh->fake = 1;
3742 fdh->is_func_descriptor = 1;
3743 fdh->oh = fh;
3744 fh->is_func = 1;
3745 fh->oh = fdh;
bb700d78
AM
3746 return fdh;
3747}
3748
8387904d
AM
3749/* Fix function descriptor symbols defined in .opd sections to be
3750 function type. */
555cd476
AM
3751
3752static bfd_boolean
c16153ae 3753ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 3754 struct bfd_link_info *info,
555cd476 3755 Elf_Internal_Sym *isym,
6911b7dc 3756 const char **name,
555cd476
AM
3757 flagword *flags ATTRIBUTE_UNUSED,
3758 asection **sec,
b53dfeb2 3759 bfd_vma *value)
555cd476 3760{
b53dfeb2 3761 if (*sec != NULL
f1885d1e 3762 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
3763 {
3764 asection *code_sec;
3765
3766 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3767 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3768 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3769
3770 /* If the symbol is a function defined in .opd, and the function
3771 code is in a discarded group, let it appear to be undefined. */
0e1862bb 3772 if (!bfd_link_relocatable (info)
b53dfeb2
AM
3773 && (*sec)->reloc_count != 0
3774 && opd_entry_value (*sec, *value, &code_sec, NULL,
3775 FALSE) != (bfd_vma) -1
3776 && discarded_section (code_sec))
3777 {
3778 *sec = bfd_und_section_ptr;
3779 isym->st_shndx = SHN_UNDEF;
3780 }
3781 }
dbd1e97e
AM
3782 else if (*sec != NULL
3783 && strcmp ((*sec)->name, ".toc") == 0
3784 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
3785 {
3786 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3787 if (htab != NULL)
3788 htab->params->object_in_toc = 1;
3789 }
433817dd 3790
6911b7dc
AM
3791 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3792 {
3793 if (abiversion (ibfd) == 0)
3794 set_abiversion (ibfd, 2);
3795 else if (abiversion (ibfd) == 1)
3796 {
cf97bcb0
AM
3797 _bfd_error_handler (_("symbol '%s' has invalid st_other"
3798 " for ABI version 1"), *name);
6911b7dc
AM
3799 bfd_set_error (bfd_error_bad_value);
3800 return FALSE;
3801 }
3802 }
3803
555cd476
AM
3804 return TRUE;
3805}
3806
6911b7dc
AM
3807/* Merge non-visibility st_other attributes: local entry point. */
3808
3809static void
3810ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3811 const Elf_Internal_Sym *isym,
3812 bfd_boolean definition,
3813 bfd_boolean dynamic)
3814{
f378ab09 3815 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
3816 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
3817 | ELF_ST_VISIBILITY (h->other));
3818}
3819
8c5b4e52
AM
3820/* Hook called on merging a symbol. We use this to clear "fake" since
3821 we now have a real symbol. */
3822
3823static bfd_boolean
3824ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 3825 const Elf_Internal_Sym *isym,
8c5b4e52
AM
3826 asection **psec ATTRIBUTE_UNUSED,
3827 bfd_boolean newdef ATTRIBUTE_UNUSED,
3828 bfd_boolean olddef ATTRIBUTE_UNUSED,
3829 bfd *oldbfd ATTRIBUTE_UNUSED,
3830 const asection *oldsec ATTRIBUTE_UNUSED)
3831{
3832 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
3833 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3834 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
3835 return TRUE;
3836}
3837
8387904d 3838/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
3839 inclusion of a new ABI object archive that defines "bar".
3840 NAME is a symbol defined in an archive. Return a symbol in the hash
3841 table that might be satisfied by the archive symbols. */
8387904d
AM
3842
3843static struct elf_link_hash_entry *
3844ppc64_elf_archive_symbol_lookup (bfd *abfd,
3845 struct bfd_link_info *info,
3846 const char *name)
3847{
3848 struct elf_link_hash_entry *h;
3849 char *dot_name;
3850 size_t len;
3851
3852 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
3853 if (h != NULL
3854 /* Don't return this sym if it is a fake function descriptor
3855 created by add_symbol_adjust. */
8c5b4e52 3856 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
3857 return h;
3858
3859 if (name[0] == '.')
3860 return h;
3861
3862 len = strlen (name);
3863 dot_name = bfd_alloc (abfd, len + 2);
3864 if (dot_name == NULL)
e99955cd 3865 return (struct elf_link_hash_entry *) -1;
8387904d
AM
3866 dot_name[0] = '.';
3867 memcpy (dot_name + 1, name, len + 1);
3868 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
3869 bfd_release (abfd, dot_name);
3870 return h;
3871}
3872
3873/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
3874 new ABI object defines "bar". Well, at least, undefined dot symbols
3875 are made weak. This stops later archive searches from including an
3876 object if we already have a function descriptor definition. It also
35b0ce59
AM
3877 prevents the linker complaining about undefined symbols.
3878 We also check and correct mismatched symbol visibility here. The
3879 most restrictive visibility of the function descriptor and the
3880 function entry symbol is used. */
8387904d
AM
3881
3882static bfd_boolean
b3fac117 3883add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 3884{
8387904d
AM
3885 struct ppc_link_hash_table *htab;
3886 struct ppc_link_hash_entry *fdh;
3887
b3fac117
AM
3888 if (eh->elf.root.type == bfd_link_hash_warning)
3889 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 3890
8400d40d
AM
3891 if (eh->elf.root.type == bfd_link_hash_indirect)
3892 return TRUE;
3893
b3fac117
AM
3894 if (eh->elf.root.root.string[0] != '.')
3895 abort ();
8387904d 3896
b3fac117 3897 htab = ppc_hash_table (info);
4dfe6ac6
NC
3898 if (htab == NULL)
3899 return FALSE;
3900
b31867b6 3901 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
3902 if (fdh == NULL
3903 && !bfd_link_relocatable (info)
3904 && (eh->elf.root.type == bfd_link_hash_undefined
3905 || eh->elf.root.type == bfd_link_hash_undefweak)
3906 && eh->elf.ref_regular)
3907 {
3908 /* Make an undefined function descriptor sym, in order to
3909 pull in an --as-needed shared lib. Archives are handled
3910 elsewhere. */
3911 fdh = make_fdh (info, eh);
3912 if (fdh == NULL)
3913 return FALSE;
bb700d78 3914 }
8c5b4e52
AM
3915
3916 if (fdh != NULL)
8387904d 3917 {
35b0ce59
AM
3918 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
3919 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
3920
3921 /* Make both descriptor and entry symbol have the most
3922 constraining visibility of either symbol. */
35b0ce59
AM
3923 if (entry_vis < descr_vis)
3924 fdh->elf.other += entry_vis - descr_vis;
3925 else if (entry_vis > descr_vis)
3926 eh->elf.other += descr_vis - entry_vis;
3927
8c5b4e52
AM
3928 /* Propagate reference flags from entry symbol to function
3929 descriptor symbol. */
bc4e12de 3930 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 3931 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
3932 fdh->elf.ref_regular |= eh->elf.ref_regular;
3933 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
3934
3935 if (!fdh->elf.forced_local
3936 && fdh->elf.dynindx == -1
3937 && fdh->elf.versioned != versioned_hidden
3938 && (bfd_link_dll (info)
3939 || fdh->elf.def_dynamic
3940 || fdh->elf.ref_dynamic)
3941 && (eh->elf.ref_regular
3942 || eh->elf.def_regular))
3943 {
2cdcc330 3944 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 3945 return FALSE;
35b0ce59 3946 }
8387904d 3947 }
99877b66 3948
8387904d
AM
3949 return TRUE;
3950}
3951
f6c7c3e8
AM
3952/* Set up opd section info and abiversion for IBFD, and process list
3953 of dot-symbols we made in link_hash_newfunc. */
b3fac117 3954
8387904d 3955static bfd_boolean
f6c7c3e8 3956ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 3957{
99877b66 3958 struct ppc_link_hash_table *htab;
b3fac117 3959 struct ppc_link_hash_entry **p, *eh;
459609d6 3960 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 3961
459609d6 3962 if (opd != NULL && opd->size != 0)
b3fac117 3963 {
b9399fcf
AM
3964 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
3965 ppc64_elf_section_data (opd)->sec_type = sec_opd;
3966
459609d6
AM
3967 if (abiversion (ibfd) == 0)
3968 set_abiversion (ibfd, 1);
8a2058b5 3969 else if (abiversion (ibfd) >= 2)
f6c7c3e8 3970 {
695344c0 3971 /* xgettext:c-format */
cf97bcb0
AM
3972 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
3973 ibfd, abiversion (ibfd));
459609d6
AM
3974 bfd_set_error (bfd_error_bad_value);
3975 return FALSE;
f6c7c3e8 3976 }
b9399fcf 3977 }
f6c7c3e8 3978
b9399fcf
AM
3979 if (is_ppc64_elf (info->output_bfd))
3980 {
3981 /* For input files without an explicit abiversion in e_flags
3982 we should have flagged any with symbol st_other bits set
3983 as ELFv1 and above flagged those with .opd as ELFv2.
3984 Set the output abiversion if not yet set, and for any input
3985 still ambiguous, take its abiversion from the output.
3986 Differences in ABI are reported later. */
3987 if (abiversion (info->output_bfd) == 0)
3988 set_abiversion (info->output_bfd, abiversion (ibfd));
3989 else if (abiversion (ibfd) == 0)
3990 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
3991 }
3992
459609d6
AM
3993 htab = ppc_hash_table (info);
3994 if (htab == NULL)
b9399fcf 3995 return TRUE;
459609d6 3996
b9399fcf
AM
3997 if (opd != NULL && opd->size != 0
3998 && (ibfd->flags & DYNAMIC) == 0
3999 && (opd->flags & SEC_RELOC) != 0
4000 && opd->reloc_count != 0
4001 && !bfd_is_abs_section (opd->output_section)
4002 && info->gc_sections)
4003 {
4004 /* Garbage collection needs some extra help with .opd sections.
4005 We don't want to necessarily keep everything referenced by
4006 relocs in .opd, as that would keep all functions. Instead,
4007 if we reference an .opd symbol (a function descriptor), we
4008 want to keep the function code symbol's section. This is
4009 easy for global symbols, but for local syms we need to keep
4010 information about the associated function section. */
4011 bfd_size_type amt;
4012 asection **opd_sym_map;
4013 Elf_Internal_Shdr *symtab_hdr;
4014 Elf_Internal_Rela *relocs, *rel_end, *rel;
4015
4016 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4017 opd_sym_map = bfd_zalloc (ibfd, amt);
4018 if (opd_sym_map == NULL)
4019 return FALSE;
4020 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4021 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4022 info->keep_memory);
4023 if (relocs == NULL)
4024 return FALSE;
4025 symtab_hdr = &elf_symtab_hdr (ibfd);
4026 rel_end = relocs + opd->reloc_count - 1;
4027 for (rel = relocs; rel < rel_end; rel++)
4028 {
4029 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4030 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4031
4032 if (r_type == R_PPC64_ADDR64
4033 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4034 && r_symndx < symtab_hdr->sh_info)
4035 {
4036 Elf_Internal_Sym *isym;
4037 asection *s;
4038
4039 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4040 if (isym == NULL)
4041 {
4042 if (elf_section_data (opd)->relocs != relocs)
4043 free (relocs);
4044 return FALSE;
4045 }
4046
4047 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4048 if (s != NULL && s != opd)
4049 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4050 }
4051 }
4052 if (elf_section_data (opd)->relocs != relocs)
4053 free (relocs);
4054 }
459609d6
AM
4055
4056 p = &htab->dot_syms;
4057 while ((eh = *p) != NULL)
4058 {
4059 *p = NULL;
4060 if (&eh->elf == htab->elf.hgot)
4061 ;
4062 else if (htab->elf.hgot == NULL
4063 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4064 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4065 else if (abiversion (ibfd) <= 1)
4066 {
4067 htab->need_func_desc_adj = 1;
4068 if (!add_symbol_adjust (eh, info))
4069 return FALSE;
4070 }
459609d6
AM
4071 p = &eh->u.next_dot_sym;
4072 }
b3fac117 4073 return TRUE;
8387904d
AM
4074}
4075
97fed1c9
JJ
4076/* Undo hash table changes when an --as-needed input file is determined
4077 not to be needed. */
4078
4079static bfd_boolean
e5034e59
AM
4080ppc64_elf_notice_as_needed (bfd *ibfd,
4081 struct bfd_link_info *info,
4082 enum notice_asneeded_action act)
97fed1c9 4083{
e5034e59
AM
4084 if (act == notice_not_needed)
4085 {
4086 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4087
e5034e59
AM
4088 if (htab == NULL)
4089 return FALSE;
4dfe6ac6 4090
e5034e59
AM
4091 htab->dot_syms = NULL;
4092 }
4093 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4094}
4095
aa374f67
AM
4096/* If --just-symbols against a final linked binary, then assume we need
4097 toc adjusting stubs when calling functions defined there. */
4098
4099static void
4100ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4101{
4102 if ((sec->flags & SEC_CODE) != 0
4103 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4104 && is_ppc64_elf (sec->owner))
4105 {
2c3f079f
AM
4106 if (abiversion (sec->owner) >= 2
4107 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4108 sec->has_toc_reloc = 1;
4109 }
4110 _bfd_elf_link_just_syms (sec, info);
4111}
4112
e054468f 4113static struct plt_entry **
4ce794b7
AM
4114update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4115 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4116{
4117 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4118 struct plt_entry **local_plt;
f961d9dd 4119 unsigned char *local_got_tls_masks;
411e1bfb
AM
4120
4121 if (local_got_ents == NULL)
4122 {
4123 bfd_size_type size = symtab_hdr->sh_info;
4124
e054468f
AM
4125 size *= (sizeof (*local_got_ents)
4126 + sizeof (*local_plt)
4127 + sizeof (*local_got_tls_masks));
4ce794b7 4128 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4129 if (local_got_ents == NULL)
e054468f 4130 return NULL;
411e1bfb
AM
4131 elf_local_got_ents (abfd) = local_got_ents;
4132 }
4133
37da22e5 4134 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4135 {
4136 struct got_entry *ent;
4137
4138 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4139 if (ent->addend == r_addend
4140 && ent->owner == abfd
4141 && ent->tls_type == tls_type)
411e1bfb
AM
4142 break;
4143 if (ent == NULL)
4144 {
4145 bfd_size_type amt = sizeof (*ent);
4ce794b7 4146 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4147 if (ent == NULL)
4148 return FALSE;
4149 ent->next = local_got_ents[r_symndx];
4150 ent->addend = r_addend;
e717da7e 4151 ent->owner = abfd;
411e1bfb 4152 ent->tls_type = tls_type;
927be08e 4153 ent->is_indirect = FALSE;
411e1bfb
AM
4154 ent->got.refcount = 0;
4155 local_got_ents[r_symndx] = ent;
4156 }
4157 ent->got.refcount += 1;
4158 }
4159
e054468f 4160 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4161 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4162 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4163
4164 return local_plt + r_symndx;
65f38f15
AM
4165}
4166
411e1bfb 4167static bfd_boolean
e054468f 4168update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4169{
411e1bfb 4170 struct plt_entry *ent;
1e2f5b6e 4171
e054468f 4172 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4173 if (ent->addend == addend)
4174 break;
4175 if (ent == NULL)
1e2f5b6e 4176 {
411e1bfb 4177 bfd_size_type amt = sizeof (*ent);
4ce794b7 4178 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4179 if (ent == NULL)
4180 return FALSE;
e054468f 4181 ent->next = *plist;
411e1bfb
AM
4182 ent->addend = addend;
4183 ent->plt.refcount = 0;
e054468f 4184 *plist = ent;
1e2f5b6e 4185 }
411e1bfb 4186 ent->plt.refcount += 1;
b34976b6 4187 return TRUE;
1e2f5b6e
AM
4188}
4189
e054468f
AM
4190static bfd_boolean
4191is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4192{
4193 return (r_type == R_PPC64_REL24
05d0e962 4194 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4195 || r_type == R_PPC64_REL14
4196 || r_type == R_PPC64_REL14_BRTAKEN
4197 || r_type == R_PPC64_REL14_BRNTAKEN
4198 || r_type == R_PPC64_ADDR24
4199 || r_type == R_PPC64_ADDR14
4200 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d
AM
4201 || r_type == R_PPC64_ADDR14_BRNTAKEN
4202 || r_type == R_PPC64_PLTCALL);
4203}
4204
4205/* Relocs on inline plt call sequence insns prior to the call. */
4206
4207static bfd_boolean
4208is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4209{
4210 return (r_type == R_PPC64_PLT16_HA
4211 || r_type == R_PPC64_PLT16_HI
4212 || r_type == R_PPC64_PLT16_LO
4213 || r_type == R_PPC64_PLT16_LO_DS
4214 || r_type == R_PPC64_PLTSEQ);
e054468f
AM
4215}
4216
5bd4f169 4217/* Look through the relocs for a section during the first phase, and
65f38f15 4218 calculate needed space in the global offset table, procedure
5d1634d7 4219 linkage table, and dynamic reloc sections. */
5bd4f169 4220
b34976b6 4221static bfd_boolean
4ce794b7
AM
4222ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4223 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4224{
65f38f15 4225 struct ppc_link_hash_table *htab;
5bd4f169 4226 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4227 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4228 const Elf_Internal_Rela *rel;
4229 const Elf_Internal_Rela *rel_end;
5bd4f169 4230 asection *sreloc;
3a71aa26 4231 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4232 bfd_boolean is_opd;
5bd4f169 4233
0e1862bb 4234 if (bfd_link_relocatable (info))
b34976b6 4235 return TRUE;
5bd4f169 4236
680a3378
AM
4237 /* Don't do anything special with non-loaded, non-alloced sections.
4238 In particular, any relocs in such sections should not affect GOT
4239 and PLT reference counting (ie. we don't allow them to create GOT
4240 or PLT entries), there's no possibility or desire to optimize TLS
4241 relocs, and there's not much point in propagating relocs to shared
4242 libs that the dynamic linker won't relocate. */
4243 if ((sec->flags & SEC_ALLOC) == 0)
4244 return TRUE;
4245
0c8d6e5c 4246 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4247
65f38f15 4248 htab = ppc_hash_table (info);
4dfe6ac6
NC
4249 if (htab == NULL)
4250 return FALSE;
4251
3a71aa26
AM
4252 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4253 FALSE, FALSE, TRUE);
4254 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4255 FALSE, FALSE, TRUE);
0ffa91dd 4256 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4257 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4258 sreloc = NULL;
b9399fcf 4259 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4260 rel_end = relocs + sec->reloc_count;
4261 for (rel = relocs; rel < rel_end; rel++)
4262 {
4263 unsigned long r_symndx;
4264 struct elf_link_hash_entry *h;
04c9666a 4265 enum elf_ppc64_reloc_type r_type;
727fc41e 4266 int tls_type;
7c8fe5c4 4267 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4268 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4269
4270 r_symndx = ELF64_R_SYM (rel->r_info);
4271 if (r_symndx < symtab_hdr->sh_info)
4272 h = NULL;
4273 else
973a3492
L
4274 {
4275 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4276 h = elf_follow_link (h);
1c865ab2
AM
4277
4278 if (h == htab->elf.hgot)
4279 sec->has_toc_reloc = 1;
973a3492 4280 }
5bd4f169 4281
727fc41e 4282 tls_type = 0;
e054468f 4283 ifunc = NULL;
25f23106
AM
4284 if (h != NULL)
4285 {
4286 if (h->type == STT_GNU_IFUNC)
4287 {
4288 h->needs_plt = 1;
4289 ifunc = &h->plt.plist;
4290 }
4291 }
4292 else
4293 {
4294 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4295 abfd, r_symndx);
4296 if (isym == NULL)
4297 return FALSE;
4298
4299 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4300 {
4301 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
37da22e5
AM
4302 rel->r_addend,
4303 NON_GOT | PLT_IFUNC);
25f23106
AM
4304 if (ifunc == NULL)
4305 return FALSE;
4306 }
4307 }
727fc41e 4308
cbf95972 4309 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 4310 switch (r_type)
5bd4f169 4311 {
727fc41e
AM
4312 case R_PPC64_TLSGD:
4313 case R_PPC64_TLSLD:
4314 /* These special tls relocs tie a call to __tls_get_addr with
4315 its parameter symbol. */
37da22e5
AM
4316 if (h != NULL)
4317 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4318 else
4319 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4320 rel->r_addend,
4321 NON_GOT | TLS_TLS | TLS_MARK))
4322 return FALSE;
4323 sec->has_tls_reloc = 1;
727fc41e
AM
4324 break;
4325
411e1bfb
AM
4326 case R_PPC64_GOT_TLSLD16:
4327 case R_PPC64_GOT_TLSLD16_LO:
4328 case R_PPC64_GOT_TLSLD16_HI:
4329 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 4330 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4331 goto dogottls;
4332
4333 case R_PPC64_GOT_TLSGD16:
4334 case R_PPC64_GOT_TLSGD16_LO:
4335 case R_PPC64_GOT_TLSGD16_HI:
4336 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 4337 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4338 goto dogottls;
4339
4340 case R_PPC64_GOT_TPREL16_DS:
4341 case R_PPC64_GOT_TPREL16_LO_DS:
4342 case R_PPC64_GOT_TPREL16_HI:
4343 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 4344 if (bfd_link_dll (info))
411e1bfb
AM
4345 info->flags |= DF_STATIC_TLS;
4346 tls_type = TLS_TLS | TLS_TPREL;
4347 goto dogottls;
4348
4349 case R_PPC64_GOT_DTPREL16_DS:
4350 case R_PPC64_GOT_DTPREL16_LO_DS:
4351 case R_PPC64_GOT_DTPREL16_HI:
4352 case R_PPC64_GOT_DTPREL16_HA:
4353 tls_type = TLS_TLS | TLS_DTPREL;
4354 dogottls:
4355 sec->has_tls_reloc = 1;
1a0670f3 4356 /* Fall through */
411e1bfb 4357
5bd4f169 4358 case R_PPC64_GOT16:
5bd4f169 4359 case R_PPC64_GOT16_DS:
65f38f15
AM
4360 case R_PPC64_GOT16_HA:
4361 case R_PPC64_GOT16_HI:
4362 case R_PPC64_GOT16_LO:
5bd4f169 4363 case R_PPC64_GOT16_LO_DS:
65f38f15 4364 /* This symbol requires a global offset table entry. */
4c52953f 4365 sec->has_toc_reloc = 1;
33c0ec9d
AM
4366 if (r_type == R_PPC64_GOT_TLSLD16
4367 || r_type == R_PPC64_GOT_TLSGD16
4368 || r_type == R_PPC64_GOT_TPREL16_DS
4369 || r_type == R_PPC64_GOT_DTPREL16_DS
4370 || r_type == R_PPC64_GOT16
4371 || r_type == R_PPC64_GOT16_DS)
4372 {
4373 htab->do_multi_toc = 1;
d77c8a4b 4374 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4375 }
4376
e717da7e
AM
4377 if (ppc64_elf_tdata (abfd)->got == NULL
4378 && !create_got_section (abfd, info))
b34976b6 4379 return FALSE;
5bd4f169
AM
4380
4381 if (h != NULL)
4382 {
411e1bfb
AM
4383 struct ppc_link_hash_entry *eh;
4384 struct got_entry *ent;
65f38f15 4385
411e1bfb
AM
4386 eh = (struct ppc_link_hash_entry *) h;
4387 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4388 if (ent->addend == rel->r_addend
e717da7e 4389 && ent->owner == abfd
411e1bfb
AM
4390 && ent->tls_type == tls_type)
4391 break;
4392 if (ent == NULL)
5bd4f169 4393 {
411e1bfb 4394 bfd_size_type amt = sizeof (*ent);
4ce794b7 4395 ent = bfd_alloc (abfd, amt);
411e1bfb 4396 if (ent == NULL)
b34976b6 4397 return FALSE;
411e1bfb
AM
4398 ent->next = eh->elf.got.glist;
4399 ent->addend = rel->r_addend;
e717da7e 4400 ent->owner = abfd;
411e1bfb 4401 ent->tls_type = tls_type;
927be08e 4402 ent->is_indirect = FALSE;
411e1bfb
AM
4403 ent->got.refcount = 0;
4404 eh->elf.got.glist = ent;
5bd4f169 4405 }
411e1bfb 4406 ent->got.refcount += 1;
e7b938ca 4407 eh->tls_mask |= tls_type;
5bd4f169 4408 }
411e1bfb
AM
4409 else
4410 /* This is a global offset table entry for a local symbol. */
4411 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4412 rel->r_addend, tls_type))
4413 return FALSE;
a345bc8d
AM
4414
4415 /* We may also need a plt entry if the symbol turns out to be
4416 an ifunc. */
0e1862bb 4417 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
4418 {
4419 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4420 return FALSE;
4421 }
5bd4f169
AM
4422 break;
4423
5bd4f169 4424 case R_PPC64_PLT16_HA:
65f38f15
AM
4425 case R_PPC64_PLT16_HI:
4426 case R_PPC64_PLT16_LO:
08be3224 4427 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
4428 case R_PPC64_PLT32:
4429 case R_PPC64_PLT64:
cbf95972
AM
4430 /* This symbol requires a procedure linkage table entry. */
4431 plt_list = ifunc;
4432 if (h != NULL)
e054468f 4433 {
e054468f
AM
4434 h->needs_plt = 1;
4435 if (h->root.root.string[0] == '.'
4436 && h->root.root.string[1] != '\0')
4437 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 4438 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4439 plt_list = &h->plt.plist;
4440 }
4441 if (plt_list == NULL)
2d7ad24e
AM
4442 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4443 rel->r_addend,
4444 NON_GOT | PLT_KEEP);
cbf95972
AM
4445 if (!update_plt_info (abfd, plt_list, rel->r_addend))
4446 return FALSE;
5bd4f169
AM
4447 break;
4448
4449 /* The following relocations don't need to propagate the
4450 relocation if linking a shared object since they are
4451 section relative. */
4452 case R_PPC64_SECTOFF:
4453 case R_PPC64_SECTOFF_LO:
4454 case R_PPC64_SECTOFF_HI:
4455 case R_PPC64_SECTOFF_HA:
4456 case R_PPC64_SECTOFF_DS:
4457 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4458 case R_PPC64_DTPREL16:
4459 case R_PPC64_DTPREL16_LO:
4460 case R_PPC64_DTPREL16_HI:
4461 case R_PPC64_DTPREL16_HA:
4462 case R_PPC64_DTPREL16_DS:
4463 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4464 case R_PPC64_DTPREL16_HIGH:
4465 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4466 case R_PPC64_DTPREL16_HIGHER:
4467 case R_PPC64_DTPREL16_HIGHERA:
4468 case R_PPC64_DTPREL16_HIGHEST:
4469 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4470 break;
4471
ad8e1ba5 4472 /* Nor do these. */
25f23106
AM
4473 case R_PPC64_REL16:
4474 case R_PPC64_REL16_LO:
4475 case R_PPC64_REL16_HI:
4476 case R_PPC64_REL16_HA:
a680de9a 4477 case R_PPC64_REL16DX_HA:
25f23106
AM
4478 break;
4479
45965137
AM
4480 /* Not supported as a dynamic relocation. */
4481 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4482 if (bfd_link_pic (info))
45965137
AM
4483 {
4484 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4485 ppc_howto_init ();
695344c0 4486 /* xgettext:c-format */
174d0a74 4487 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4488 "in shared libraries and PIEs\n"),
45965137
AM
4489 abfd, sec, rel->r_offset,
4490 ppc64_elf_howto_table[r_type]->name);
4491 bfd_set_error (bfd_error_bad_value);
4492 return FALSE;
4493 }
4494 break;
4495
ad8e1ba5 4496 case R_PPC64_TOC16:
33c0ec9d
AM
4497 case R_PPC64_TOC16_DS:
4498 htab->do_multi_toc = 1;
d77c8a4b 4499 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4500 /* Fall through. */
ad8e1ba5
AM
4501 case R_PPC64_TOC16_LO:
4502 case R_PPC64_TOC16_HI:
4503 case R_PPC64_TOC16_HA:
ad8e1ba5 4504 case R_PPC64_TOC16_LO_DS:
4c52953f 4505 sec->has_toc_reloc = 1;
ad8e1ba5
AM
4506 break;
4507
006589cf
AM
4508 /* Marker reloc. */
4509 case R_PPC64_ENTRY:
4510 break;
4511
5bd4f169
AM
4512 /* This relocation describes the C++ object vtable hierarchy.
4513 Reconstruct it for later use during GC. */
4514 case R_PPC64_GNU_VTINHERIT:
c152c796 4515 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4516 return FALSE;
5bd4f169
AM
4517 break;
4518
4519 /* This relocation describes which C++ vtable entries are actually
4520 used. Record for later use during GC. */
4521 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
4522 BFD_ASSERT (h != NULL);
4523 if (h != NULL
4524 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4525 return FALSE;
5bd4f169
AM
4526 break;
4527
721956f4
AM
4528 case R_PPC64_REL14:
4529 case R_PPC64_REL14_BRTAKEN:
4530 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4531 {
4532 asection *dest = NULL;
4533
4534 /* Heuristic: If jumping outside our section, chances are
4535 we are going to need a stub. */
4536 if (h != NULL)
4537 {
4538 /* If the sym is weak it may be overridden later, so
4539 don't assume we know where a weak sym lives. */
4540 if (h->root.type == bfd_link_hash_defined)
4541 dest = h->root.u.def.section;
4542 }
4543 else
87d72d41
AM
4544 {
4545 Elf_Internal_Sym *isym;
4546
4547 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4548 abfd, r_symndx);
4549 if (isym == NULL)
4550 return FALSE;
4551
4552 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4553 }
4554
220c76dd 4555 if (dest != sec)
7c8fe5c4 4556 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4557 }
3e04d765
AM
4558 goto rel24;
4559
4560 case R_PPC64_PLTCALL:
4561 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4562 /* Fall through. */
4563
5d1634d7 4564 case R_PPC64_REL24:
05d0e962 4565 case R_PPC64_REL24_NOTOC:
3e04d765 4566 rel24:
cbf95972
AM
4567 plt_list = ifunc;
4568 if (h != NULL)
5d1634d7 4569 {
e054468f
AM
4570 h->needs_plt = 1;
4571 if (h->root.root.string[0] == '.'
4572 && h->root.root.string[1] != '\0')
4573 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 4574
3a71aa26 4575 if (h == tga || h == dottga)
cbf95972
AM
4576 {
4577 sec->has_tls_reloc = 1;
4578 if (rel != relocs
4579 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4580 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4581 /* We have a new-style __tls_get_addr call with
4582 a marker reloc. */
4583 ;
4584 else
4585 /* Mark this section as having an old-style call. */
4586 sec->has_tls_get_addr_call = 1;
4587 }
4588 plt_list = &h->plt.plist;
411e1bfb 4589 }
cbf95972
AM
4590
4591 /* We may need a .plt entry if the function this reloc
4592 refers to is in a shared lib. */
4593 if (plt_list
4594 && !update_plt_info (abfd, plt_list, rel->r_addend))
4595 return FALSE;
411e1bfb
AM
4596 break;
4597
cbf95972
AM
4598 case R_PPC64_ADDR14:
4599 case R_PPC64_ADDR14_BRNTAKEN:
4600 case R_PPC64_ADDR14_BRTAKEN:
4601 case R_PPC64_ADDR24:
4602 goto dodyn;
4603
411e1bfb
AM
4604 case R_PPC64_TPREL64:
4605 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 4606 if (bfd_link_dll (info))
411e1bfb
AM
4607 info->flags |= DF_STATIC_TLS;
4608 goto dotlstoc;
4609
4610 case R_PPC64_DTPMOD64:
4611 if (rel + 1 < rel_end
4612 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4613 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 4614 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 4615 else
951fd09b 4616 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
4617 goto dotlstoc;
4618
4619 case R_PPC64_DTPREL64:
4620 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4621 if (rel != relocs
4622 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4623 && rel[-1].r_offset == rel->r_offset - 8)
4624 /* This is the second reloc of a dtpmod, dtprel pair.
4625 Don't mark with TLS_DTPREL. */
4626 goto dodyn;
4627
4628 dotlstoc:
4629 sec->has_tls_reloc = 1;
4630 if (h != NULL)
4631 {
4632 struct ppc_link_hash_entry *eh;
4633 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 4634 eh->tls_mask |= tls_type;
411e1bfb
AM
4635 }
4636 else
4637 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4638 rel->r_addend, tls_type))
4639 return FALSE;
4640
7c8fe5c4
AM
4641 ppc64_sec = ppc64_elf_section_data (sec);
4642 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 4643 {
3a71aa26
AM
4644 bfd_size_type amt;
4645
e7b938ca 4646 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
4647 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4648 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4649 if (ppc64_sec->u.toc.symndx == NULL)
4650 return FALSE;
4651 amt = sec->size * sizeof (bfd_vma) / 8;
4652 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4653 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 4654 return FALSE;
7c8fe5c4
AM
4655 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4656 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
4657 }
4658 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
4659 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4660 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
4661
4662 /* Mark the second slot of a GD or LD entry.
4663 -1 to indicate GD and -2 to indicate LD. */
4664 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 4665 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 4666 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 4667 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
4668 goto dodyn;
4669
4670 case R_PPC64_TPREL16:
4671 case R_PPC64_TPREL16_LO:
4672 case R_PPC64_TPREL16_HI:
4673 case R_PPC64_TPREL16_HA:
4674 case R_PPC64_TPREL16_DS:
4675 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
4676 case R_PPC64_TPREL16_HIGH:
4677 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
4678 case R_PPC64_TPREL16_HIGHER:
4679 case R_PPC64_TPREL16_HIGHERA:
4680 case R_PPC64_TPREL16_HIGHEST:
4681 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
4682 if (bfd_link_dll (info))
4683 info->flags |= DF_STATIC_TLS;
4684 goto dodyn;
5d1634d7 4685
e86ce104 4686 case R_PPC64_ADDR64:
b9399fcf 4687 if (is_opd
1e2f5b6e 4688 && rel + 1 < rel_end
4ce794b7 4689 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 4690 {
8387904d 4691 if (h != NULL)
8c5b4e52 4692 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 4693 }
e86ce104
AM
4694 /* Fall through. */
4695
65f38f15
AM
4696 case R_PPC64_ADDR16:
4697 case R_PPC64_ADDR16_DS:
4698 case R_PPC64_ADDR16_HA:
4699 case R_PPC64_ADDR16_HI:
f9c6b907
AM
4700 case R_PPC64_ADDR16_HIGH:
4701 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
4702 case R_PPC64_ADDR16_HIGHER:
4703 case R_PPC64_ADDR16_HIGHERA:
4704 case R_PPC64_ADDR16_HIGHEST:
4705 case R_PPC64_ADDR16_HIGHESTA:
4706 case R_PPC64_ADDR16_LO:
4707 case R_PPC64_ADDR16_LO_DS:
0e1862bb 4708 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
4709 && rel->r_addend == 0)
4710 {
4711 /* We may need a .plt entry if this reloc refers to a
4712 function in a shared lib. */
4713 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4714 return FALSE;
4715 h->pointer_equality_needed = 1;
4716 }
4717 /* Fall through. */
4718
4719 case R_PPC64_REL30:
4720 case R_PPC64_REL32:
4721 case R_PPC64_REL64:
65f38f15 4722 case R_PPC64_ADDR32:
65f38f15
AM
4723 case R_PPC64_UADDR16:
4724 case R_PPC64_UADDR32:
4725 case R_PPC64_UADDR64:
5bd4f169 4726 case R_PPC64_TOC:
0e1862bb 4727 if (h != NULL && !bfd_link_pic (info))
81848ca0 4728 /* We may need a copy reloc. */
f5385ebf 4729 h->non_got_ref = 1;
81848ca0 4730
41bd81ab 4731 /* Don't propagate .opd relocs. */
b9399fcf 4732 if (NO_OPD_RELOCS && is_opd)
e86ce104 4733 break;
e86ce104 4734
65f38f15
AM
4735 /* If we are creating a shared library, and this is a reloc
4736 against a global symbol, or a non PC relative reloc
4737 against a local symbol, then we need to copy the reloc
4738 into the shared library. However, if we are linking with
4739 -Bsymbolic, we do not need to copy a reloc against a
4740 global symbol which is defined in an object we are
4741 including in the link (i.e., DEF_REGULAR is set). At
4742 this point we have not seen all the input files, so it is
4743 possible that DEF_REGULAR is not set now but will be set
4744 later (it is never cleared). In case of a weak definition,
4745 DEF_REGULAR may be cleared later by a strong definition in
4746 a shared library. We account for that possibility below by
f4656909 4747 storing information in the dyn_relocs field of the hash
65f38f15
AM
4748 table entry. A similar situation occurs when creating
4749 shared libraries and symbol visibility changes render the
4750 symbol local.
4751
4752 If on the other hand, we are creating an executable, we
4753 may need to keep relocations for symbols satisfied by a
4754 dynamic library if we manage to avoid copy relocs for the
4755 symbol. */
411e1bfb 4756 dodyn:
0e1862bb 4757 if ((bfd_link_pic (info)
1d483afe 4758 && (must_be_dyn_reloc (info, r_type)
65f38f15 4759 || (h != NULL
198f1157 4760 && (!SYMBOLIC_BIND (info, h)
65f38f15 4761 || h->root.type == bfd_link_hash_defweak
f5385ebf 4762 || !h->def_regular))))
f4656909 4763 || (ELIMINATE_COPY_RELOCS
0e1862bb 4764 && !bfd_link_pic (info)
65f38f15
AM
4765 && h != NULL
4766 && (h->root.type == bfd_link_hash_defweak
25f23106 4767 || !h->def_regular))
0e1862bb 4768 || (!bfd_link_pic (info)
25f23106 4769 && ifunc != NULL))
5bd4f169 4770 {
65f38f15
AM
4771 /* We must copy these reloc types into the output file.
4772 Create a reloc section in dynobj and make room for
4773 this reloc. */
5bd4f169
AM
4774 if (sreloc == NULL)
4775 {
83bac4b0
NC
4776 sreloc = _bfd_elf_make_dynamic_reloc_section
4777 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 4778
5bd4f169 4779 if (sreloc == NULL)
83bac4b0 4780 return FALSE;
5bd4f169
AM
4781 }
4782
65f38f15
AM
4783 /* If this is a global symbol, we count the number of
4784 relocations we need for this symbol. */
4785 if (h != NULL)
4786 {
19e08130
AM
4787 struct elf_dyn_relocs *p;
4788 struct elf_dyn_relocs **head;
4789
ec338859 4790 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
4791 p = *head;
4792 if (p == NULL || p->sec != sec)
4793 {
4794 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4795 if (p == NULL)
4796 return FALSE;
4797 p->next = *head;
4798 *head = p;
4799 p->sec = sec;
4800 p->count = 0;
4801 p->pc_count = 0;
4802 }
4803 p->count += 1;
4804 if (!must_be_dyn_reloc (info, r_type))
4805 p->pc_count += 1;
65f38f15
AM
4806 }
4807 else
4808 {
ec338859
AM
4809 /* Track dynamic relocs needed for local syms too.
4810 We really need local syms available to do this
4811 easily. Oh well. */
19e08130
AM
4812 struct ppc_dyn_relocs *p;
4813 struct ppc_dyn_relocs **head;
4814 bfd_boolean is_ifunc;
ec338859 4815 asection *s;
6edfbbad 4816 void *vpp;
87d72d41 4817 Elf_Internal_Sym *isym;
6edfbbad 4818
87d72d41
AM
4819 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4820 abfd, r_symndx);
4821 if (isym == NULL)
b34976b6 4822 return FALSE;
ec338859 4823
87d72d41
AM
4824 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4825 if (s == NULL)
4826 s = sec;
4827
6edfbbad 4828 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
4829 head = (struct ppc_dyn_relocs **) vpp;
4830 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
4831 p = *head;
4832 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
4833 p = p->next;
4834 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
4835 {
4836 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4837 if (p == NULL)
4838 return FALSE;
4839 p->next = *head;
4840 *head = p;
4841 p->sec = sec;
4842 p->ifunc = is_ifunc;
4843 p->count = 0;
4844 }
4845 p->count += 1;
ec338859 4846 }
65f38f15 4847 }
5bd4f169 4848 break;
65f38f15
AM
4849
4850 default:
96e0dda4 4851 break;
5bd4f169
AM
4852 }
4853 }
4854
b34976b6 4855 return TRUE;
5bd4f169
AM
4856}
4857
ee67d69a
AM
4858/* Merge backend specific data from an object file to the output
4859 object file when linking. */
4860
4861static bfd_boolean
50e03d47 4862ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 4863{
50e03d47 4864 bfd *obfd = info->output_bfd;
ee67d69a
AM
4865 unsigned long iflags, oflags;
4866
4867 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
4868 return TRUE;
4869
4870 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
4871 return TRUE;
4872
50e03d47 4873 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
4874 return FALSE;
4875
4876 iflags = elf_elfheader (ibfd)->e_flags;
4877 oflags = elf_elfheader (obfd)->e_flags;
4878
f6c7c3e8 4879 if (iflags & ~EF_PPC64_ABI)
ee67d69a 4880 {
4eca0228 4881 _bfd_error_handler
695344c0 4882 /* xgettext:c-format */
871b3ab2 4883 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
4884 bfd_set_error (bfd_error_bad_value);
4885 return FALSE;
4886 }
f6c7c3e8 4887 else if (iflags != oflags && iflags != 0)
ee67d69a 4888 {
4eca0228 4889 _bfd_error_handler
695344c0 4890 /* xgettext:c-format */
871b3ab2 4891 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
4892 ibfd, iflags, oflags);
4893 bfd_set_error (bfd_error_bad_value);
4894 return FALSE;
4895 }
4896
4a91d0ba
AM
4897 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
4898 return FALSE;
005d79fd 4899
ee67d69a 4900 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 4901 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
4902}
4903
4904static bfd_boolean
4905ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
4906{
4907 /* Print normal ELF private data. */
4908 _bfd_elf_print_private_bfd_data (abfd, ptr);
4909
4910 if (elf_elfheader (abfd)->e_flags != 0)
4911 {
4912 FILE *file = ptr;
4913
ee67d69a
AM
4914 fprintf (file, _("private flags = 0x%lx:"),
4915 elf_elfheader (abfd)->e_flags);
4916
4917 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
4918 fprintf (file, _(" [abiv%ld]"),
4919 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
4920 fputc ('\n', file);
4921 }
4922
4923 return TRUE;
4924}
4925
8387904d 4926/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
4927 of the code entry point, and its section, which must be in the same
4928 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
4929
4930static bfd_vma
4931opd_entry_value (asection *opd_sec,
4932 bfd_vma offset,
4933 asection **code_sec,
aef36ac1
AM
4934 bfd_vma *code_off,
4935 bfd_boolean in_code_sec)
8387904d
AM
4936{
4937 bfd *opd_bfd = opd_sec->owner;
8860955f 4938 Elf_Internal_Rela *relocs;
8387904d 4939 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 4940 bfd_vma val;
8387904d 4941
9f296da3
AM
4942 /* No relocs implies we are linking a --just-symbols object, or looking
4943 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
4944 if (opd_sec->reloc_count == 0)
4945 {
729eabd5 4946 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 4947
729eabd5
AM
4948 if (contents == NULL)
4949 {
4950 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
4951 return (bfd_vma) -1;
4952 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
4953 }
ee1e4ede 4954
dbb3fbbb 4955 /* PR 17512: file: 64b9dfbb. */
451dfd38 4956 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
4957 return (bfd_vma) -1;
4958
729eabd5 4959 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
4960 if (code_sec != NULL)
4961 {
4962 asection *sec, *likely = NULL;
ee1e4ede 4963
aef36ac1 4964 if (in_code_sec)
4b85d634 4965 {
aef36ac1
AM
4966 sec = *code_sec;
4967 if (sec->vma <= val
4968 && val < sec->vma + sec->size)
4969 likely = sec;
4970 else
4971 val = -1;
4972 }
4973 else
4974 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
4975 if (sec->vma <= val
4976 && (sec->flags & SEC_LOAD) != 0
4977 && (sec->flags & SEC_ALLOC) != 0)
4978 likely = sec;
4979 if (likely != NULL)
4980 {
4981 *code_sec = likely;
4982 if (code_off != NULL)
4983 *code_off = val - likely->vma;
4b85d634
AM
4984 }
4985 }
aef36ac1 4986 return val;
4b85d634
AM
4987 }
4988
0c8d6e5c 4989 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 4990
729eabd5 4991 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
4992 if (relocs == NULL)
4993 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
4994 /* PR 17512: file: df8e1fd6. */
4995 if (relocs == NULL)
4996 return (bfd_vma) -1;
645ea6a9 4997
8387904d 4998 /* Go find the opd reloc at the sym address. */
8860955f 4999 lo = relocs;
8387904d 5000 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5001 val = (bfd_vma) -1;
8387904d
AM
5002 while (lo < hi)
5003 {
5004 look = lo + (hi - lo) / 2;
5005 if (look->r_offset < offset)
5006 lo = look + 1;
5007 else if (look->r_offset > offset)
5008 hi = look;
5009 else
5010 {
0ffa91dd
NC
5011 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5012
8387904d
AM
5013 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5014 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5015 {
5016 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5017 asection *sec = NULL;
8387904d 5018
b53dfeb2
AM
5019 if (symndx >= symtab_hdr->sh_info
5020 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5021 {
5022 struct elf_link_hash_entry **sym_hashes;
5023 struct elf_link_hash_entry *rh;
5024
5025 sym_hashes = elf_sym_hashes (opd_bfd);
5026 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5027 if (rh != NULL)
5028 {
5029 rh = elf_follow_link (rh);
bb854a36
AM
5030 if (rh->root.type != bfd_link_hash_defined
5031 && rh->root.type != bfd_link_hash_defweak)
5032 break;
5033 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5034 {
bb854a36
AM
5035 val = rh->root.u.def.value;
5036 sec = rh->root.u.def.section;
b53dfeb2
AM
5037 }
5038 }
5039 }
5040
5041 if (sec == NULL)
5042 {
5043 Elf_Internal_Sym *sym;
5044
5045 if (symndx < symtab_hdr->sh_info)
5046 {
5047 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5048 if (sym == NULL)
5049 {
5050 size_t symcnt = symtab_hdr->sh_info;
5051 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5052 symcnt, 0,
5053 NULL, NULL, NULL);
5054 if (sym == NULL)
5055 break;
5056 symtab_hdr->contents = (bfd_byte *) sym;
5057 }
5058 sym += symndx;
128205bb
AM
5059 }
5060 else
5061 {
b53dfeb2
AM
5062 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5063 1, symndx,
5064 NULL, NULL, NULL);
128205bb
AM
5065 if (sym == NULL)
5066 break;
128205bb 5067 }
b53dfeb2
AM
5068 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5069 if (sec == NULL)
5070 break;
5071 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5072 val = sym->st_value;
8387904d 5073 }
b53dfeb2 5074
8387904d
AM
5075 val += look->r_addend;
5076 if (code_off != NULL)
5077 *code_off = val;
5078 if (code_sec != NULL)
aef36ac1
AM
5079 {
5080 if (in_code_sec && *code_sec != sec)
5081 return -1;
5082 else
5083 *code_sec = sec;
5084 }
b53dfeb2 5085 if (sec->output_section != NULL)
8387904d 5086 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5087 }
5088 break;
5089 }
5090 }
645ea6a9 5091
645ea6a9 5092 return val;
8387904d
AM
5093}
5094
aef36ac1
AM
5095/* If the ELF symbol SYM might be a function in SEC, return the
5096 function size and set *CODE_OFF to the function's entry point,
5097 otherwise return zero. */
9f296da3 5098
aef36ac1
AM
5099static bfd_size_type
5100ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5101 bfd_vma *code_off)
9f296da3 5102{
aef36ac1
AM
5103 bfd_size_type size;
5104
5105 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5106 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5107 return 0;
5108
5109 size = 0;
5110 if (!(sym->flags & BSF_SYNTHETIC))
5111 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5112
5113 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5114 {
b07bca4e
AM
5115 struct _opd_sec_data *opd = get_opd_info (sym->section);
5116 bfd_vma symval = sym->value;
5117
5118 if (opd != NULL
5119 && opd->adjust != NULL
5120 && elf_section_data (sym->section)->relocs != NULL)
5121 {
5122 /* opd_entry_value will use cached relocs that have been
5123 adjusted, but with raw symbols. That means both local
5124 and global symbols need adjusting. */
5125 long adjust = opd->adjust[OPD_NDX (symval)];
5126 if (adjust == -1)
5127 return 0;
5128 symval += adjust;
5129 }
5130
5131 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5132 &sec, code_off, TRUE) == (bfd_vma) -1)
5133 return 0;
5134 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5135 symbol. This size has nothing to do with the code size of the
5136 function, which is what we're supposed to return, but the
5137 code size isn't available without looking up the dot-sym.
5138 However, doing that would be a waste of time particularly
5139 since elf_find_function will look at the dot-sym anyway.
5140 Now, elf_find_function will keep the largest size of any
5141 function sym found at the code address of interest, so return
5142 1 here to avoid it incorrectly caching a larger function size
5143 for a small function. This does mean we return the wrong
5144 size for a new-ABI function of size 24, but all that does is
5145 disable caching for such functions. */
5146 if (size == 24)
5147 size = 1;
9f296da3 5148 }
aef36ac1
AM
5149 else
5150 {
5151 if (sym->section != sec)
5152 return 0;
5153 *code_off = sym->value;
5154 }
5155 if (size == 0)
5156 size = 1;
5157 return size;
9f296da3
AM
5158}
5159
f378ab09
AM
5160/* Return true if symbol is a strong function defined in an ELFv2
5161 object with st_other localentry bits of zero, ie. its local entry
5162 point coincides with its global entry point. */
5163
5164static bfd_boolean
5165is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5166{
5167 return (h != NULL
5168 && h->type == STT_FUNC
5169 && h->root.type == bfd_link_hash_defined
5170 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 5171 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
5172 && is_ppc64_elf (h->root.u.def.section->owner)
5173 && abiversion (h->root.u.def.section->owner) >= 2);
5174}
5175
854b41e7
AM
5176/* Return true if symbol is defined in a regular object file. */
5177
5178static bfd_boolean
5179is_static_defined (struct elf_link_hash_entry *h)
5180{
5181 return ((h->root.type == bfd_link_hash_defined
5182 || h->root.type == bfd_link_hash_defweak)
5183 && h->root.u.def.section != NULL
5184 && h->root.u.def.section->output_section != NULL);
5185}
5186
b31867b6
AM
5187/* If FDH is a function descriptor symbol, return the associated code
5188 entry symbol if it is defined. Return NULL otherwise. */
5189
5190static struct ppc_link_hash_entry *
5191defined_code_entry (struct ppc_link_hash_entry *fdh)
5192{
5193 if (fdh->is_func_descriptor)
5194 {
5195 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5196 if (fh->elf.root.type == bfd_link_hash_defined
5197 || fh->elf.root.type == bfd_link_hash_defweak)
5198 return fh;
5199 }
5200 return NULL;
5201}
5202
5203/* If FH is a function code entry symbol, return the associated
5204 function descriptor symbol if it is defined. Return NULL otherwise. */
5205
5206static struct ppc_link_hash_entry *
5207defined_func_desc (struct ppc_link_hash_entry *fh)
5208{
5209 if (fh->oh != NULL
5210 && fh->oh->is_func_descriptor)
5211 {
5212 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5213 if (fdh->elf.root.type == bfd_link_hash_defined
5214 || fdh->elf.root.type == bfd_link_hash_defweak)
5215 return fdh;
5216 }
5217 return NULL;
5218}
5219
8c5b4e52
AM
5220static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5221
5222/* Garbage collect sections, after first dealing with dot-symbols. */
5223
5224static bfd_boolean
5225ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5226{
5227 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5228
5229 if (htab != NULL && htab->need_func_desc_adj)
5230 {
5231 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5232 htab->need_func_desc_adj = 0;
5233 }
5234 return bfd_elf_gc_sections (abfd, info);
5235}
5236
74f0fb50
AM
5237/* Mark all our entry sym sections, both opd and code section. */
5238
5239static void
5240ppc64_elf_gc_keep (struct bfd_link_info *info)
5241{
5242 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5243 struct bfd_sym_chain *sym;
5244
4dfe6ac6
NC
5245 if (htab == NULL)
5246 return;
5247
74f0fb50
AM
5248 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5249 {
b31867b6 5250 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5251 asection *sec;
5252
5253 eh = (struct ppc_link_hash_entry *)
b31867b6 5254 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5255 if (eh == NULL)
5256 continue;
5257 if (eh->elf.root.type != bfd_link_hash_defined
5258 && eh->elf.root.type != bfd_link_hash_defweak)
5259 continue;
5260
b31867b6
AM
5261 fh = defined_code_entry (eh);
5262 if (fh != NULL)
74f0fb50 5263 {
b31867b6 5264 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5265 sec->flags |= SEC_KEEP;
5266 }
5267 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5268 && opd_entry_value (eh->elf.root.u.def.section,
5269 eh->elf.root.u.def.value,
aef36ac1 5270 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5271 sec->flags |= SEC_KEEP;
5272
5273 sec = eh->elf.root.u.def.section;
5274 sec->flags |= SEC_KEEP;
5275 }
5276}
5277
64d03ab5
AM
5278/* Mark sections containing dynamically referenced symbols. When
5279 building shared libraries, we must assume that any visible symbol is
5280 referenced. */
5281
5282static bfd_boolean
5283ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5284{
5285 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5286 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5287 struct ppc_link_hash_entry *fdh;
b407645f 5288 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5289
64d03ab5 5290 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5291 fdh = defined_func_desc (eh);
5292 if (fdh != NULL)
5293 eh = fdh;
64d03ab5
AM
5294
5295 if ((eh->elf.root.type == bfd_link_hash_defined
5296 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5297 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5298 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5299 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5300 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5301 && (!bfd_link_executable (info)
e278ae05 5302 || info->gc_keep_exported
b407645f
AM
5303 || info->export_dynamic
5304 || (eh->elf.dynamic
5305 && d != NULL
2cdcc330
AM
5306 && (*d->match) (&d->head, NULL,
5307 eh->elf.root.root.string)))
e278ae05 5308 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5309 || !bfd_hide_sym_by_version (info->version_info,
5310 eh->elf.root.root.string)))))
64d03ab5
AM
5311 {
5312 asection *code_sec;
b31867b6 5313 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5314
5315 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5316
5317 /* Function descriptor syms cause the associated
5318 function code sym section to be marked. */
b31867b6
AM
5319 fh = defined_code_entry (eh);
5320 if (fh != NULL)
5321 {
5322 code_sec = fh->elf.root.u.def.section;
5323 code_sec->flags |= SEC_KEEP;
5324 }
64d03ab5
AM
5325 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5326 && opd_entry_value (eh->elf.root.u.def.section,
5327 eh->elf.root.u.def.value,
aef36ac1 5328 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5329 code_sec->flags |= SEC_KEEP;
5330 }
5331
5332 return TRUE;
5333}
5334
5bd4f169
AM
5335/* Return the section that should be marked against GC for a given
5336 relocation. */
5337
5338static asection *
4ce794b7 5339ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5340 struct bfd_link_info *info,
4ce794b7
AM
5341 Elf_Internal_Rela *rel,
5342 struct elf_link_hash_entry *h,
5343 Elf_Internal_Sym *sym)
5bd4f169 5344{
ccfa59ea
AM
5345 asection *rsec;
5346
ccfa59ea
AM
5347 /* Syms return NULL if we're marking .opd, so we avoid marking all
5348 function sections, as all functions are referenced in .opd. */
5349 rsec = NULL;
5350 if (get_opd_info (sec) != NULL)
5351 return rsec;
1e2f5b6e 5352
5bd4f169
AM
5353 if (h != NULL)
5354 {
04c9666a 5355 enum elf_ppc64_reloc_type r_type;
b31867b6 5356 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5357
4ce794b7 5358 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5359 switch (r_type)
5bd4f169
AM
5360 {
5361 case R_PPC64_GNU_VTINHERIT:
5362 case R_PPC64_GNU_VTENTRY:
5363 break;
5364
5365 default:
5366 switch (h->root.type)
5367 {
5368 case bfd_link_hash_defined:
5369 case bfd_link_hash_defweak:
ccfa59ea 5370 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5371 fdh = defined_func_desc (eh);
5372 if (fdh != NULL)
8c5b4e52
AM
5373 {
5374 /* -mcall-aixdesc code references the dot-symbol on
5375 a call reloc. Mark the function descriptor too
5376 against garbage collection. */
5377 fdh->elf.mark = 1;
60d67dc8
AM
5378 if (fdh->elf.is_weakalias)
5379 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5380 eh = fdh;
5381 }
1e2f5b6e
AM
5382
5383 /* Function descriptor syms cause the associated
5384 function code sym section to be marked. */
b31867b6
AM
5385 fh = defined_code_entry (eh);
5386 if (fh != NULL)
ccfa59ea
AM
5387 {
5388 /* They also mark their opd section. */
74f0fb50 5389 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5390
b31867b6 5391 rsec = fh->elf.root.u.def.section;
ccfa59ea 5392 }
8387904d
AM
5393 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5394 && opd_entry_value (eh->elf.root.u.def.section,
5395 eh->elf.root.u.def.value,
aef36ac1 5396 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5397 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5398 else
1e2f5b6e
AM
5399 rsec = h->root.u.def.section;
5400 break;
5bd4f169
AM
5401
5402 case bfd_link_hash_common:
1e2f5b6e
AM
5403 rsec = h->root.u.c.p->section;
5404 break;
5bd4f169
AM
5405
5406 default:
fb34365b 5407 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5408 }
5409 }
5410 }
5411 else
5412 {
74f0fb50 5413 struct _opd_sec_data *opd;
1e2f5b6e
AM
5414
5415 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5416 opd = get_opd_info (rsec);
5417 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5418 {
74f0fb50 5419 rsec->gc_mark = 1;
ccfa59ea 5420
51aecdc5 5421 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5422 }
5bd4f169
AM
5423 }
5424
1e2f5b6e 5425 return rsec;
5bd4f169
AM
5426}
5427
deb0e272
AM
5428/* The maximum size of .sfpr. */
5429#define SFPR_MAX (218*4)
5430
5431struct sfpr_def_parms
5432{
699733f6
AM
5433 const char name[12];
5434 unsigned char lo, hi;
2cdcc330
AM
5435 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5436 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5437};
5438
a4b6fadd
AM
5439/* Auto-generate _save*, _rest* functions in .sfpr.
5440 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5441 instead. */
deb0e272 5442
4dfe6ac6 5443static bfd_boolean
a4b6fadd
AM
5444sfpr_define (struct bfd_link_info *info,
5445 const struct sfpr_def_parms *parm,
5446 asection *stub_sec)
deb0e272
AM
5447{
5448 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5449 unsigned int i;
5450 size_t len = strlen (parm->name);
5451 bfd_boolean writing = FALSE;
699733f6 5452 char sym[16];
deb0e272 5453
4dfe6ac6
NC
5454 if (htab == NULL)
5455 return FALSE;
5456
deb0e272
AM
5457 memcpy (sym, parm->name, len);
5458 sym[len + 2] = 0;
5459
5460 for (i = parm->lo; i <= parm->hi; i++)
5461 {
a4b6fadd 5462 struct ppc_link_hash_entry *h;
deb0e272
AM
5463
5464 sym[len + 0] = i / 10 + '0';
5465 sym[len + 1] = i % 10 + '0';
a4b6fadd 5466 h = (struct ppc_link_hash_entry *)
b32547cd 5467 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 5468 if (stub_sec != NULL)
deb0e272 5469 {
a4b6fadd
AM
5470 if (h != NULL
5471 && h->elf.root.type == bfd_link_hash_defined
5472 && h->elf.root.u.def.section == htab->sfpr)
5473 {
5474 struct elf_link_hash_entry *s;
5475 char buf[32];
5476 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5477 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5478 if (s == NULL)
5479 return FALSE;
5480 if (s->root.type == bfd_link_hash_new
5481 || (s->root.type = bfd_link_hash_defined
5482 && s->root.u.def.section == stub_sec))
5483 {
5484 s->root.type = bfd_link_hash_defined;
5485 s->root.u.def.section = stub_sec;
7dda8d3c 5486 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5487 + h->elf.root.u.def.value);
5488 s->ref_regular = 1;
5489 s->def_regular = 1;
5490 s->ref_regular_nonweak = 1;
5491 s->forced_local = 1;
5492 s->non_elf = 0;
5493 s->root.linker_def = 1;
5494 }
5495 }
5496 continue;
5497 }
5498 if (h != NULL)
5499 {
5500 h->save_res = 1;
5501 if (!h->elf.def_regular)
deb0e272 5502 {
a4b6fadd
AM
5503 h->elf.root.type = bfd_link_hash_defined;
5504 h->elf.root.u.def.section = htab->sfpr;
5505 h->elf.root.u.def.value = htab->sfpr->size;
5506 h->elf.type = STT_FUNC;
5507 h->elf.def_regular = 1;
b32547cd 5508 h->elf.non_elf = 0;
a4b6fadd
AM
5509 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5510 writing = TRUE;
deb0e272 5511 if (htab->sfpr->contents == NULL)
a4b6fadd 5512 {
2cdcc330
AM
5513 htab->sfpr->contents
5514 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5515 if (htab->sfpr->contents == NULL)
5516 return FALSE;
5517 }
deb0e272
AM
5518 }
5519 }
5520 if (writing)
5521 {
5522 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5523 if (i != parm->hi)
5524 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5525 else
5526 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5527 htab->sfpr->size = p - htab->sfpr->contents;
5528 }
5529 }
5530
5531 return TRUE;
5532}
5533
5534static bfd_byte *
5535savegpr0 (bfd *abfd, bfd_byte *p, int r)
5536{
5537 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5538 return p + 4;
5539}
5540
5541static bfd_byte *
5542savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5543{
5544 p = savegpr0 (abfd, p, r);
a078d95a 5545 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5546 p = p + 4;
5547 bfd_put_32 (abfd, BLR, p);
5548 return p + 4;
5549}
5550
5551static bfd_byte *
5552restgpr0 (bfd *abfd, bfd_byte *p, int r)
5553{
5554 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5555 return p + 4;
5556}
5557
5558static bfd_byte *
5559restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5560{
a078d95a 5561 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5562 p = p + 4;
5563 p = restgpr0 (abfd, p, r);
5564 bfd_put_32 (abfd, MTLR_R0, p);
5565 p = p + 4;
5566 if (r == 29)
5567 {
5568 p = restgpr0 (abfd, p, 30);
5569 p = restgpr0 (abfd, p, 31);
5570 }
5571 bfd_put_32 (abfd, BLR, p);
5572 return p + 4;
5573}
5574
5575static bfd_byte *
5576savegpr1 (bfd *abfd, bfd_byte *p, int r)
5577{
5578 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5579 return p + 4;
5580}
5581
5582static bfd_byte *
5583savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5584{
5585 p = savegpr1 (abfd, p, r);
5586 bfd_put_32 (abfd, BLR, p);
5587 return p + 4;
5588}
5589
5590static bfd_byte *
5591restgpr1 (bfd *abfd, bfd_byte *p, int r)
5592{
5593 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5594 return p + 4;
5595}
5596
5597static bfd_byte *
5598restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5599{
5600 p = restgpr1 (abfd, p, r);
5601 bfd_put_32 (abfd, BLR, p);
5602 return p + 4;
5603}
5604
5605static bfd_byte *
5606savefpr (bfd *abfd, bfd_byte *p, int r)
5607{
5608 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5609 return p + 4;
5610}
5611
5612static bfd_byte *
5613savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5614{
5615 p = savefpr (abfd, p, r);
a078d95a 5616 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5617 p = p + 4;
5618 bfd_put_32 (abfd, BLR, p);
5619 return p + 4;
5620}
5621
5622static bfd_byte *
5623restfpr (bfd *abfd, bfd_byte *p, int r)
5624{
5625 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5626 return p + 4;
5627}
5628
5629static bfd_byte *
5630restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5631{
a078d95a 5632 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
5633 p = p + 4;
5634 p = restfpr (abfd, p, r);
5635 bfd_put_32 (abfd, MTLR_R0, p);
5636 p = p + 4;
5637 if (r == 29)
5638 {
5639 p = restfpr (abfd, p, 30);
5640 p = restfpr (abfd, p, 31);
5641 }
5642 bfd_put_32 (abfd, BLR, p);
5643 return p + 4;
5644}
5645
5646static bfd_byte *
5647savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5648{
5649 p = savefpr (abfd, p, r);
5650 bfd_put_32 (abfd, BLR, p);
5651 return p + 4;
5652}
5653
5654static bfd_byte *
5655restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5656{
5657 p = restfpr (abfd, p, r);
5658 bfd_put_32 (abfd, BLR, p);
5659 return p + 4;
5660}
5661
5662static bfd_byte *
5663savevr (bfd *abfd, bfd_byte *p, int r)
5664{
5665 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5666 p = p + 4;
5667 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5668 return p + 4;
5669}
5670
5671static bfd_byte *
5672savevr_tail (bfd *abfd, bfd_byte *p, int r)
5673{
5674 p = savevr (abfd, p, r);
5675 bfd_put_32 (abfd, BLR, p);
5676 return p + 4;
5677}
5678
5679static bfd_byte *
5680restvr (bfd *abfd, bfd_byte *p, int r)
5681{
5682 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5683 p = p + 4;
5684 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5685 return p + 4;
5686}
5687
5688static bfd_byte *
5689restvr_tail (bfd *abfd, bfd_byte *p, int r)
5690{
5691 p = restvr (abfd, p, r);
5692 bfd_put_32 (abfd, BLR, p);
5693 return p + 4;
5694}
5695
e86ce104
AM
5696/* Called via elf_link_hash_traverse to transfer dynamic linking
5697 information on function code symbol entries to their corresponding
5698 function descriptor symbol entries. */
deb0e272 5699
b34976b6 5700static bfd_boolean
4ce794b7 5701func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 5702{
e86ce104 5703 struct bfd_link_info *info;
65f38f15 5704 struct ppc_link_hash_table *htab;
50bc7936
AM
5705 struct ppc_link_hash_entry *fh;
5706 struct ppc_link_hash_entry *fdh;
5707 bfd_boolean force_local;
5bd4f169 5708
50bc7936
AM
5709 fh = (struct ppc_link_hash_entry *) h;
5710 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 5711 return TRUE;
e86ce104 5712
8c5b4e52
AM
5713 if (!fh->is_func)
5714 return TRUE;
5715
5716 if (fh->elf.root.root.string[0] != '.'
5717 || fh->elf.root.root.string[1] == '\0')
5718 return TRUE;
5719
4ce794b7 5720 info = inf;
65f38f15 5721 htab = ppc_hash_table (info);
4dfe6ac6
NC
5722 if (htab == NULL)
5723 return FALSE;
5bd4f169 5724
8c5b4e52
AM
5725 /* Find the corresponding function descriptor symbol. */
5726 fdh = lookup_fdh (fh, htab);
5727
c09bdfe5
AM
5728 /* Resolve undefined references to dot-symbols as the value
5729 in the function descriptor, if we have one in a regular object.
5730 This is to satisfy cases like ".quad .foo". Calls to functions
5731 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
5732 if ((fh->elf.root.type == bfd_link_hash_undefined
5733 || fh->elf.root.type == bfd_link_hash_undefweak)
5734 && (fdh->elf.root.type == bfd_link_hash_defined
5735 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
5736 && get_opd_info (fdh->elf.root.u.def.section) != NULL
5737 && opd_entry_value (fdh->elf.root.u.def.section,
5738 fdh->elf.root.u.def.value,
c09bdfe5 5739 &fh->elf.root.u.def.section,
aef36ac1 5740 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 5741 {
b31867b6 5742 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 5743 fh->elf.forced_local = 1;
b31867b6
AM
5744 fh->elf.def_regular = fdh->elf.def_regular;
5745 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
5746 }
5747
8c5b4e52
AM
5748 if (!fh->elf.dynamic)
5749 {
5750 struct plt_entry *ent;
5bd4f169 5751
8c5b4e52
AM
5752 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5753 if (ent->plt.refcount > 0)
5754 break;
5755 if (ent == NULL)
5756 return TRUE;
5757 }
5bd4f169 5758
8c5b4e52 5759 /* Create a descriptor as undefined if necessary. */
50bc7936 5760 if (fdh == NULL
0e1862bb 5761 && !bfd_link_executable (info)
50bc7936
AM
5762 && (fh->elf.root.type == bfd_link_hash_undefined
5763 || fh->elf.root.type == bfd_link_hash_undefweak))
5764 {
908b32fc 5765 fdh = make_fdh (info, fh);
bb700d78
AM
5766 if (fdh == NULL)
5767 return FALSE;
50bc7936 5768 }
648cca2c 5769
8c5b4e52 5770 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
5771 if (fdh != NULL
5772 && fdh->fake
8c5b4e52
AM
5773 && (fh->elf.root.type == bfd_link_hash_defined
5774 || fh->elf.root.type == bfd_link_hash_defweak))
5775 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 5776
8c5b4e52
AM
5777 /* Transfer dynamic linking information to the function descriptor. */
5778 if (fdh != NULL)
5779 {
f5385ebf
AM
5780 fdh->elf.ref_regular |= fh->elf.ref_regular;
5781 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5782 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5783 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
5784 fdh->elf.dynamic |= fh->elf.dynamic;
5785 fdh->elf.needs_plt |= (fh->elf.needs_plt
5786 || fh->elf.type == STT_FUNC
5787 || fh->elf.type == STT_GNU_IFUNC);
5788 move_plt_plist (fh, fdh);
5789
5790 if (!fdh->elf.forced_local
5791 && fh->elf.dynindx != -1)
5792 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5793 return FALSE;
e86ce104
AM
5794 }
5795
50bc7936
AM
5796 /* Now that the info is on the function descriptor, clear the
5797 function code sym info. Any function code syms for which we
5798 don't have a definition in a regular file, we force local.
5799 This prevents a shared library from exporting syms that have
5800 been imported from another library. Function code syms that
5801 are really in the library we must leave global to prevent the
5802 linker dragging in a definition from a static library. */
93f3fa99
AM
5803 force_local = (!fh->elf.def_regular
5804 || fdh == NULL
5805 || !fdh->elf.def_regular
5806 || fdh->elf.forced_local);
50bc7936
AM
5807 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5808
b34976b6 5809 return TRUE;
e86ce104 5810}
40b8271b 5811
a4b6fadd
AM
5812static const struct sfpr_def_parms save_res_funcs[] =
5813 {
5814 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5815 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5816 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5817 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5818 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5819 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5820 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5821 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5822 { "._savef", 14, 31, savefpr, savefpr1_tail },
5823 { "._restf", 14, 31, restfpr, restfpr1_tail },
5824 { "_savevr_", 20, 31, savevr, savevr_tail },
5825 { "_restvr_", 20, 31, restvr, restvr_tail }
5826 };
5827
e86ce104 5828/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
5829 this hook to a) provide some gcc support functions, and b) transfer
5830 dynamic linking information gathered so far on function code symbol
5831 entries, to their corresponding function descriptor symbol entries. */
deb0e272 5832
b34976b6 5833static bfd_boolean
4ce794b7
AM
5834ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5835 struct bfd_link_info *info)
e86ce104
AM
5836{
5837 struct ppc_link_hash_table *htab;
5838
5839 htab = ppc_hash_table (info);
4dfe6ac6
NC
5840 if (htab == NULL)
5841 return FALSE;
5842
b32547cd
AM
5843 /* Provide any missing _save* and _rest* functions. */
5844 if (htab->sfpr != NULL)
5845 {
5846 unsigned int i;
5847
5848 htab->sfpr->size = 0;
5849 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
5850 if (!sfpr_define (info, &save_res_funcs[i], NULL))
5851 return FALSE;
5852 if (htab->sfpr->size == 0)
5853 htab->sfpr->flags |= SEC_EXCLUDE;
5854 }
5855
5856 if (bfd_link_relocatable (info))
5857 return TRUE;
5858
5859 if (htab->elf.hgot != NULL)
dba6fa9b
AM
5860 {
5861 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
5862 /* Make .TOC. defined so as to prevent it being made dynamic.
5863 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
5864 if (!htab->elf.hgot->def_regular
5865 || htab->elf.hgot->root.type != bfd_link_hash_defined)
5866 {
5867 htab->elf.hgot->root.type = bfd_link_hash_defined;
5868 htab->elf.hgot->root.u.def.value = 0;
5869 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
5870 htab->elf.hgot->def_regular = 1;
5871 htab->elf.hgot->root.linker_def = 1;
5872 }
dba6fa9b 5873 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
5874 htab->elf.hgot->other
5875 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 5876 }
c66bb0ee 5877
8c5b4e52
AM
5878 if (htab->need_func_desc_adj)
5879 {
5880 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5881 htab->need_func_desc_adj = 0;
5882 }
805fc799 5883
b34976b6 5884 return TRUE;
e86ce104
AM
5885}
5886
98bbb1b8 5887/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 5888
98bbb1b8 5889static asection *
a345bc8d
AM
5890readonly_dynrelocs (struct elf_link_hash_entry *h)
5891{
5892 struct ppc_link_hash_entry *eh;
5893 struct elf_dyn_relocs *p;
5894
5895 eh = (struct ppc_link_hash_entry *) h;
5896 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5897 {
5898 asection *s = p->sec->output_section;
5899
5900 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 5901 return p->sec;
a345bc8d 5902 }
98bbb1b8 5903 return NULL;
a345bc8d
AM
5904}
5905
d311bc8b 5906/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
5907 aliases, that apply to read-only sections. Cannot be used after
5908 size_dynamic_sections. */
d311bc8b
AM
5909
5910static bfd_boolean
5911alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
5912{
5913 struct ppc_link_hash_entry *eh;
5914
5915 eh = (struct ppc_link_hash_entry *) h;
5916 do
5917 {
5918 if (readonly_dynrelocs (&eh->elf))
5919 return TRUE;
ab2477e1 5920 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
2cdcc330
AM
5921 }
5922 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
5923
5924 return FALSE;
5925}
8a2058b5 5926
8a9e8e72
AM
5927/* Return whether EH has pc-relative dynamic relocs. */
5928
5929static bfd_boolean
5930pc_dynrelocs (struct ppc_link_hash_entry *eh)
5931{
5932 struct elf_dyn_relocs *p;
5933
5934 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5935 if (p->pc_count != 0)
5936 return TRUE;
5937 return FALSE;
5938}
5939
8a2058b5
AM
5940/* Return true if a global entry stub will be created for H. Valid
5941 for ELFv2 before plt entries have been allocated. */
5942
5943static bfd_boolean
5944global_entry_stub (struct elf_link_hash_entry *h)
5945{
5946 struct plt_entry *pent;
5947
5948 if (!h->pointer_equality_needed
5949 || h->def_regular)
5950 return FALSE;
5951
5952 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5953 if (pent->plt.refcount > 0
5954 && pent->addend == 0)
5955 return TRUE;
5956
5957 return FALSE;
5958}
5959
e86ce104
AM
5960/* Adjust a symbol defined by a dynamic object and referenced by a
5961 regular object. The current definition is in some section of the
5962 dynamic object, but we're not including those sections. We have to
5963 change the definition to something the rest of the link can
5964 understand. */
5965
b34976b6 5966static bfd_boolean
4ce794b7
AM
5967ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
5968 struct elf_link_hash_entry *h)
e86ce104
AM
5969{
5970 struct ppc_link_hash_table *htab;
5474d94f 5971 asection *s, *srel;
e86ce104
AM
5972
5973 htab = ppc_hash_table (info);
4dfe6ac6
NC
5974 if (htab == NULL)
5975 return FALSE;
e86ce104
AM
5976
5977 /* Deal with function syms. */
5978 if (h->type == STT_FUNC
e054468f 5979 || h->type == STT_GNU_IFUNC
f5385ebf 5980 || h->needs_plt)
e86ce104 5981 {
529fe20e
AM
5982 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
5983 || SYMBOL_CALLS_LOCAL (info, h)
5984 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
5985 /* Discard dyn_relocs when non-pic if we've decided that a
5986 function symbol is local and not an ifunc. We keep dynamic
5987 relocs for ifuncs when local rather than always emitting a
5988 plt call stub for them and defining the symbol on the call
5989 stub. We can't do that for ELFv1 anyway (a function symbol
5990 is defined on a descriptor, not code) and it can be faster at
5991 run-time due to not needing to bounce through a stub. The
5992 dyn_relocs for ifuncs will be applied even in a static
5993 executable. */
5994 if (!bfd_link_pic (info)
5995 && h->type != STT_GNU_IFUNC
5996 && local)
5997 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
5998
e86ce104
AM
5999 /* Clear procedure linkage table information for any symbol that
6000 won't need a .plt entry. */
411e1bfb
AM
6001 struct plt_entry *ent;
6002 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6003 if (ent->plt.refcount > 0)
6004 break;
8387904d 6005 if (ent == NULL
2d7ad24e
AM
6006 || (h->type != STT_GNU_IFUNC
6007 && local
3e04d765
AM
6008 && (htab->can_convert_all_inline_plt
6009 || (((struct ppc_link_hash_entry *) h)->tls_mask
6010 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6011 {
411e1bfb 6012 h->plt.plist = NULL;
f5385ebf 6013 h->needs_plt = 0;
d1eca1e4 6014 h->pointer_equality_needed = 0;
40b8271b 6015 }
8a2058b5 6016 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6017 {
d1eca1e4
AM
6018 /* Taking a function's address in a read/write section
6019 doesn't require us to define the function symbol in the
6020 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6021 be used instead. The reason we prefer a few more dynamic
6022 relocs is that calling via a global entry stub costs a
6023 few more instructions, and pointer_equality_needed causes
6024 extra work in ld.so when resolving these symbols. */
529fe20e 6025 if (global_entry_stub (h))
d1eca1e4 6026 {
ab2477e1 6027 if (!readonly_dynrelocs (h))
529fe20e
AM
6028 {
6029 h->pointer_equality_needed = 0;
04383fd1
AM
6030 /* If we haven't seen a branch reloc and the symbol
6031 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6032 if (!h->needs_plt)
6033 h->plt.plist = NULL;
6034 }
6035 else if (!bfd_link_pic (info))
6036 /* We are going to be defining the function symbol on the
6037 plt stub, so no dyn_relocs needed when non-pic. */
6038 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
6039 }
6040
3988aed5
AM
6041 /* ELFv2 function symbols can't have copy relocs. */
6042 return TRUE;
6043 }
6044 else if (!h->needs_plt
ab2477e1 6045 && !readonly_dynrelocs (h))
3988aed5 6046 {
04383fd1
AM
6047 /* If we haven't seen a branch reloc and the symbol isn't an
6048 ifunc then we don't need a plt entry. */
3988aed5
AM
6049 h->plt.plist = NULL;
6050 h->pointer_equality_needed = 0;
a345bc8d
AM
6051 return TRUE;
6052 }
5bd4f169 6053 }
bbd7ec4a 6054 else
411e1bfb 6055 h->plt.plist = NULL;
5bd4f169
AM
6056
6057 /* If this is a weak symbol, and there is a real definition, the
6058 processor independent code will have arranged for us to see the
6059 real definition first, and we can just use the same value. */
60d67dc8 6060 if (h->is_weakalias)
5bd4f169 6061 {
60d67dc8
AM
6062 struct elf_link_hash_entry *def = weakdef (h);
6063 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6064 h->root.u.def.section = def->root.u.def.section;
6065 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6066 if (def->root.u.def.section == htab->elf.sdynbss
6067 || def->root.u.def.section == htab->elf.sdynrelro)
6068 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 6069 return TRUE;
5bd4f169
AM
6070 }
6071
5bd4f169
AM
6072 /* If we are creating a shared library, we must presume that the
6073 only references to the symbol are via the global offset table.
6074 For such cases we need not do anything here; the relocations will
6075 be handled correctly by relocate_section. */
0e1862bb 6076 if (bfd_link_pic (info))
b34976b6 6077 return TRUE;
5bd4f169 6078
65f38f15
AM
6079 /* If there are no references to this symbol that do not use the
6080 GOT, we don't need to generate a copy reloc. */
f5385ebf 6081 if (!h->non_got_ref)
b34976b6 6082 return TRUE;
65f38f15 6083
b186458a 6084 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6085 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6086
d93d1c80
AM
6087 /* If -z nocopyreloc was given, don't generate them either. */
6088 || info->nocopyreloc
a127494f 6089
dce2246a 6090 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6091 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 6092 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 6093
d93d1c80
AM
6094 /* Protected variables do not work with .dynbss. The copy in
6095 .dynbss won't be used by the shared library with the protected
6096 definition for the variable. Text relocations are preferable
6097 to an incorrect program. */
6098 || h->protected_def)
529fe20e 6099 return TRUE;
a127494f 6100
5d35169e 6101 if (h->plt.plist != NULL)
97b639ba
AM
6102 {
6103 /* We should never get here, but unfortunately there are versions
6104 of gcc out there that improperly (for this ABI) put initialized
6105 function pointers, vtable refs and suchlike in read-only
6106 sections. Allow them to proceed, but warn that this might
6107 break at runtime. */
25f53a85 6108 info->callbacks->einfo
c1c8c1ef 6109 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6110 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6111 h->root.root.string);
6112 }
5d35169e
AM
6113
6114 /* This is a reference to a symbol defined by a dynamic object which
6115 is not a function. */
6116
5bd4f169
AM
6117 /* We must allocate the symbol in our .dynbss section, which will
6118 become part of the .bss section of the executable. There will be
6119 an entry for this symbol in the .dynsym section. The dynamic
6120 object will contain position independent code, so all references
6121 from the dynamic object to this symbol will go through the global
6122 offset table. The dynamic linker will use the .dynsym entry to
6123 determine the address it must put in the global offset table, so
6124 both the dynamic object and the regular object will refer to the
6125 same memory location for the variable. */
5474d94f
AM
6126 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6127 {
6128 s = htab->elf.sdynrelro;
6129 srel = htab->elf.sreldynrelro;
6130 }
6131 else
6132 {
6133 s = htab->elf.sdynbss;
6134 srel = htab->elf.srelbss;
6135 }
1d7e9d18 6136 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6137 {
4a7e5234
AM
6138 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6139 linker to copy the initial value out of the dynamic object
6140 and into the runtime process image. */
5474d94f 6141 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6142 h->needs_copy = 1;
5bd4f169
AM
6143 }
6144
529fe20e
AM
6145 /* We no longer want dyn_relocs. */
6146 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 6147 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6148}
6149
e86ce104
AM
6150/* If given a function descriptor symbol, hide both the function code
6151 sym and the descriptor. */
6152static void
4ce794b7
AM
6153ppc64_elf_hide_symbol (struct bfd_link_info *info,
6154 struct elf_link_hash_entry *h,
6155 bfd_boolean force_local)
e86ce104 6156{
34814b9f 6157 struct ppc_link_hash_entry *eh;
e86ce104
AM
6158 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6159
34814b9f
AM
6160 eh = (struct ppc_link_hash_entry *) h;
6161 if (eh->is_func_descriptor)
e86ce104 6162 {
34814b9f 6163 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6164
721956f4 6165 if (fh == NULL)
d1329ca3
AM
6166 {
6167 const char *p, *q;
b8ac2841 6168 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6169 char save;
6170
6171 /* We aren't supposed to use alloca in BFD because on
6172 systems which do not have alloca the version in libiberty
6173 calls xmalloc, which might cause the program to crash
6174 when it runs out of memory. This function doesn't have a
6175 return status, so there's no way to gracefully return an
6176 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6177 accessed; It's either a string in an ELF string table,
6178 or allocated in an objalloc structure. */
d1329ca3 6179
34814b9f 6180 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6181 save = *p;
6182 *(char *) p = '.';
34814b9f 6183 fh = (struct ppc_link_hash_entry *)
b8ac2841 6184 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6185 *(char *) p = save;
6186
6187 /* Unfortunately, if it so happens that the string we were
6188 looking for was allocated immediately before this string,
6189 then we overwrote the string terminator. That's the only
6190 reason the lookup should fail. */
6191 if (fh == NULL)
6192 {
34814b9f
AM
6193 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6194 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6195 --q, --p;
34814b9f
AM
6196 if (q < eh->elf.root.root.string && *p == '.')
6197 fh = (struct ppc_link_hash_entry *)
b8ac2841 6198 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6199 }
6200 if (fh != NULL)
6201 {
34814b9f
AM
6202 eh->oh = fh;
6203 fh->oh = eh;
d1329ca3
AM
6204 }
6205 }
e86ce104 6206 if (fh != NULL)
34814b9f 6207 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6208 }
6209}
6210
411e1bfb 6211static bfd_boolean
8843416a
AM
6212get_sym_h (struct elf_link_hash_entry **hp,
6213 Elf_Internal_Sym **symp,
6214 asection **symsecp,
f961d9dd 6215 unsigned char **tls_maskp,
8843416a
AM
6216 Elf_Internal_Sym **locsymsp,
6217 unsigned long r_symndx,
6218 bfd *ibfd)
411e1bfb 6219{
0ffa91dd 6220 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6221
6222 if (r_symndx >= symtab_hdr->sh_info)
6223 {
6224 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6225 struct elf_link_hash_entry *h;
6226
6227 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6228 h = elf_follow_link (h);
411e1bfb
AM
6229
6230 if (hp != NULL)
6231 *hp = h;
6232
6233 if (symp != NULL)
6234 *symp = NULL;
6235
6236 if (symsecp != NULL)
6237 {
6238 asection *symsec = NULL;
6239 if (h->root.type == bfd_link_hash_defined
6240 || h->root.type == bfd_link_hash_defweak)
6241 symsec = h->root.u.def.section;
6242 *symsecp = symsec;
6243 }
6244
e7b938ca 6245 if (tls_maskp != NULL)
411e1bfb
AM
6246 {
6247 struct ppc_link_hash_entry *eh;
6248
6249 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6250 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6251 }
6252 }
6253 else
6254 {
6255 Elf_Internal_Sym *sym;
6256 Elf_Internal_Sym *locsyms = *locsymsp;
6257
6258 if (locsyms == NULL)
6259 {
6260 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6261 if (locsyms == NULL)
6262 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6263 symtab_hdr->sh_info,
6264 0, NULL, NULL, NULL);
6265 if (locsyms == NULL)
6266 return FALSE;
6267 *locsymsp = locsyms;
6268 }
6269 sym = locsyms + r_symndx;
6270
6271 if (hp != NULL)
6272 *hp = NULL;
6273
6274 if (symp != NULL)
6275 *symp = sym;
6276
6277 if (symsecp != NULL)
cb33740c 6278 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6279
e7b938ca 6280 if (tls_maskp != NULL)
411e1bfb
AM
6281 {
6282 struct got_entry **lgot_ents;
f961d9dd 6283 unsigned char *tls_mask;
411e1bfb 6284
e7b938ca 6285 tls_mask = NULL;
411e1bfb
AM
6286 lgot_ents = elf_local_got_ents (ibfd);
6287 if (lgot_ents != NULL)
6288 {
e054468f
AM
6289 struct plt_entry **local_plt = (struct plt_entry **)
6290 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6291 unsigned char *lgot_masks = (unsigned char *)
e054468f 6292 (local_plt + symtab_hdr->sh_info);
e7b938ca 6293 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6294 }
e7b938ca 6295 *tls_maskp = tls_mask;
411e1bfb
AM
6296 }
6297 }
6298 return TRUE;
6299}
6300
e7b938ca 6301/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6302 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6303 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6304
6305static int
f961d9dd 6306get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6307 unsigned long *toc_symndx,
6308 bfd_vma *toc_addend,
0d4792f7 6309 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6310 const Elf_Internal_Rela *rel,
6311 bfd *ibfd)
411e1bfb
AM
6312{
6313 unsigned long r_symndx;
0d4792f7 6314 int next_r;
411e1bfb
AM
6315 struct elf_link_hash_entry *h;
6316 Elf_Internal_Sym *sym;
6317 asection *sec;
6318 bfd_vma off;
6319
6320 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6321 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6322 return 0;
411e1bfb 6323
37da22e5
AM
6324 if ((*tls_maskp != NULL
6325 && (**tls_maskp & TLS_TLS) != 0
6326 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6327 || sec == NULL
6bee8834 6328 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6329 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6330 return 1;
411e1bfb
AM
6331
6332 /* Look inside a TOC section too. */
6333 if (h != NULL)
6334 {
6335 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6336 off = h->root.u.def.value;
6337 }
6338 else
6339 off = sym->st_value;
6340 off += rel->r_addend;
6341 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6342 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6343 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6344 if (toc_symndx != NULL)
6345 *toc_symndx = r_symndx;
3a71aa26
AM
6346 if (toc_addend != NULL)
6347 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6348 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6349 return 0;
854b41e7 6350 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6351 && (next_r == -1 || next_r == -2))
6352 return 1 - next_r;
951fd09b 6353 return 1;
411e1bfb
AM
6354}
6355
3b421ab3
AM
6356/* Find (or create) an entry in the tocsave hash table. */
6357
6358static struct tocsave_entry *
6359tocsave_find (struct ppc_link_hash_table *htab,
6360 enum insert_option insert,
6361 Elf_Internal_Sym **local_syms,
6362 const Elf_Internal_Rela *irela,
6363 bfd *ibfd)
6364{
6365 unsigned long r_indx;
6366 struct elf_link_hash_entry *h;
6367 Elf_Internal_Sym *sym;
6368 struct tocsave_entry ent, *p;
6369 hashval_t hash;
6370 struct tocsave_entry **slot;
6371
6372 r_indx = ELF64_R_SYM (irela->r_info);
6373 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6374 return NULL;
6375 if (ent.sec == NULL || ent.sec->output_section == NULL)
6376 {
4eca0228 6377 _bfd_error_handler
871b3ab2 6378 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6379 return NULL;
6380 }
6381
6382 if (h != NULL)
6383 ent.offset = h->root.u.def.value;
6384 else
6385 ent.offset = sym->st_value;
6386 ent.offset += irela->r_addend;
6387
6388 hash = tocsave_htab_hash (&ent);
6389 slot = ((struct tocsave_entry **)
6390 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6391 if (slot == NULL)
6392 return NULL;
6393
6394 if (*slot == NULL)
6395 {
6396 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6397 if (p == NULL)
6398 return NULL;
6399 *p = ent;
6400 *slot = p;
6401 }
6402 return *slot;
6403}
6404
754021d0 6405/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6406 code for the old ABI, these will already have been done. */
754021d0
AM
6407
6408static bfd_boolean
6409adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6410{
6411 struct ppc_link_hash_entry *eh;
6412 asection *sym_sec;
74f0fb50 6413 struct _opd_sec_data *opd;
754021d0
AM
6414
6415 if (h->root.type == bfd_link_hash_indirect)
6416 return TRUE;
6417
754021d0
AM
6418 if (h->root.type != bfd_link_hash_defined
6419 && h->root.type != bfd_link_hash_defweak)
6420 return TRUE;
6421
6422 eh = (struct ppc_link_hash_entry *) h;
6423 if (eh->adjust_done)
6424 return TRUE;
6425
6426 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6427 opd = get_opd_info (sym_sec);
6428 if (opd != NULL && opd->adjust != NULL)
754021d0 6429 {
51aecdc5 6430 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6431 if (adjust == -1)
6432 {
6433 /* This entry has been deleted. */
b3fac117 6434 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6435 if (dsec == NULL)
6436 {
6437 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6438 if (discarded_section (dsec))
81688140 6439 {
b3fac117 6440 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6441 break;
6442 }
6443 }
4025353c 6444 eh->elf.root.u.def.value = 0;
81688140 6445 eh->elf.root.u.def.section = dsec;
4025353c
AM
6446 }
6447 else
6448 eh->elf.root.u.def.value += adjust;
754021d0
AM
6449 eh->adjust_done = 1;
6450 }
6451 return TRUE;
6452}
6453
8c1d1bb8 6454/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6455 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6456 have already been determined. */
6457
6458static bfd_boolean
6459dec_dynrel_count (bfd_vma r_info,
6460 asection *sec,
6461 struct bfd_link_info *info,
6462 Elf_Internal_Sym **local_syms,
6463 struct elf_link_hash_entry *h,
19e08130 6464 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6465{
6466 enum elf_ppc64_reloc_type r_type;
19e08130 6467 asection *sym_sec = NULL;
8c1d1bb8
AM
6468
6469 /* Can this reloc be dynamic? This switch, and later tests here
6470 should be kept in sync with the code in check_relocs. */
6471 r_type = ELF64_R_TYPE (r_info);
6472 switch (r_type)
6473 {
6474 default:
6475 return TRUE;
6476
6477 case R_PPC64_TPREL16:
6478 case R_PPC64_TPREL16_LO:
6479 case R_PPC64_TPREL16_HI:
6480 case R_PPC64_TPREL16_HA:
6481 case R_PPC64_TPREL16_DS:
6482 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6483 case R_PPC64_TPREL16_HIGH:
6484 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
6485 case R_PPC64_TPREL16_HIGHER:
6486 case R_PPC64_TPREL16_HIGHERA:
6487 case R_PPC64_TPREL16_HIGHEST:
6488 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
6489 case R_PPC64_TPREL64:
6490 case R_PPC64_DTPMOD64:
6491 case R_PPC64_DTPREL64:
6492 case R_PPC64_ADDR64:
6493 case R_PPC64_REL30:
6494 case R_PPC64_REL32:
6495 case R_PPC64_REL64:
6496 case R_PPC64_ADDR14:
6497 case R_PPC64_ADDR14_BRNTAKEN:
6498 case R_PPC64_ADDR14_BRTAKEN:
6499 case R_PPC64_ADDR16:
6500 case R_PPC64_ADDR16_DS:
6501 case R_PPC64_ADDR16_HA:
6502 case R_PPC64_ADDR16_HI:
f9c6b907
AM
6503 case R_PPC64_ADDR16_HIGH:
6504 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
6505 case R_PPC64_ADDR16_HIGHER:
6506 case R_PPC64_ADDR16_HIGHERA:
6507 case R_PPC64_ADDR16_HIGHEST:
6508 case R_PPC64_ADDR16_HIGHESTA:
6509 case R_PPC64_ADDR16_LO:
6510 case R_PPC64_ADDR16_LO_DS:
6511 case R_PPC64_ADDR24:
6512 case R_PPC64_ADDR32:
6513 case R_PPC64_UADDR16:
6514 case R_PPC64_UADDR32:
6515 case R_PPC64_UADDR64:
6516 case R_PPC64_TOC:
6517 break;
6518 }
6519
6520 if (local_syms != NULL)
6521 {
6522 unsigned long r_symndx;
8c1d1bb8
AM
6523 bfd *ibfd = sec->owner;
6524
6525 r_symndx = ELF64_R_SYM (r_info);
6526 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6527 return FALSE;
6528 }
6529
0e1862bb 6530 if ((bfd_link_pic (info)
1d483afe 6531 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 6532 || (h != NULL
198f1157 6533 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
6534 || h->root.type == bfd_link_hash_defweak
6535 || !h->def_regular))))
6536 || (ELIMINATE_COPY_RELOCS
0e1862bb 6537 && !bfd_link_pic (info)
8c1d1bb8
AM
6538 && h != NULL
6539 && (h->root.type == bfd_link_hash_defweak
6540 || !h->def_regular)))
6541 ;
6542 else
6543 return TRUE;
6544
6545 if (h != NULL)
6edfbbad 6546 {
19e08130
AM
6547 struct elf_dyn_relocs *p;
6548 struct elf_dyn_relocs **pp;
6549 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6550
6551 /* elf_gc_sweep may have already removed all dyn relocs associated
6552 with local syms for a given section. Also, symbol flags are
6553 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6554 report a dynreloc miscount. */
6555 if (*pp == NULL && info->gc_sections)
6556 return TRUE;
6557
6558 while ((p = *pp) != NULL)
60124e18 6559 {
19e08130
AM
6560 if (p->sec == sec)
6561 {
6562 if (!must_be_dyn_reloc (info, r_type))
6563 p->pc_count -= 1;
6564 p->count -= 1;
6565 if (p->count == 0)
6566 *pp = p->next;
6567 return TRUE;
6568 }
6569 pp = &p->next;
60124e18 6570 }
6edfbbad 6571 }
19e08130
AM
6572 else
6573 {
6574 struct ppc_dyn_relocs *p;
6575 struct ppc_dyn_relocs **pp;
6576 void *vpp;
6577 bfd_boolean is_ifunc;
8c1d1bb8 6578
19e08130
AM
6579 if (local_syms == NULL)
6580 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6581 if (sym_sec == NULL)
6582 sym_sec = sec;
c57da1a7 6583
19e08130
AM
6584 vpp = &elf_section_data (sym_sec)->local_dynrel;
6585 pp = (struct ppc_dyn_relocs **) vpp;
6586
6587 if (*pp == NULL && info->gc_sections)
6588 return TRUE;
6589
6590 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6591 while ((p = *pp) != NULL)
8c1d1bb8 6592 {
19e08130
AM
6593 if (p->sec == sec && p->ifunc == is_ifunc)
6594 {
6595 p->count -= 1;
6596 if (p->count == 0)
6597 *pp = p->next;
6598 return TRUE;
6599 }
6600 pp = &p->next;
8c1d1bb8 6601 }
8c1d1bb8
AM
6602 }
6603
695344c0 6604 /* xgettext:c-format */
cf97bcb0
AM
6605 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6606 sec->owner, sec);
8c1d1bb8
AM
6607 bfd_set_error (bfd_error_bad_value);
6608 return FALSE;
6609}
6610
754021d0
AM
6611/* Remove unused Official Procedure Descriptor entries. Currently we
6612 only remove those associated with functions in discarded link-once
6613 sections, or weakly defined functions that have been overridden. It
6614 would be possible to remove many more entries for statically linked
6615 applications. */
6616
b34976b6 6617bfd_boolean
e7d1c40c 6618ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
6619{
6620 bfd *ibfd;
754021d0 6621 bfd_boolean some_edited = FALSE;
3f764659 6622 asection *need_pad = NULL;
e7d1c40c
AM
6623 struct ppc_link_hash_table *htab;
6624
6625 htab = ppc_hash_table (info);
6626 if (htab == NULL)
6627 return FALSE;
1e2f5b6e 6628
c72f2fb2 6629 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
6630 {
6631 asection *sec;
6632 Elf_Internal_Rela *relstart, *rel, *relend;
6633 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6634 Elf_Internal_Sym *local_syms;
74f0fb50 6635 struct _opd_sec_data *opd;
51aecdc5 6636 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 6637 bfd_size_type cnt_16b = 0;
1e2f5b6e 6638
854b41e7
AM
6639 if (!is_ppc64_elf (ibfd))
6640 continue;
6641
1e2f5b6e 6642 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6643 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6644 continue;
6645
dbaa2011 6646 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
6647 continue;
6648
1e2f5b6e
AM
6649 if (sec->output_section == bfd_abs_section_ptr)
6650 continue;
6651
6652 /* Look through the section relocs. */
6653 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6654 continue;
6655
6cdc0ccc 6656 local_syms = NULL;
0ffa91dd 6657 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
6658
6659 /* Read the relocations. */
4ce794b7 6660 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6661 info->keep_memory);
1e2f5b6e 6662 if (relstart == NULL)
b34976b6 6663 return FALSE;
1e2f5b6e
AM
6664
6665 /* First run through the relocs to check they are sane, and to
6666 determine whether we need to edit this opd section. */
b34976b6 6667 need_edit = FALSE;
51aecdc5 6668 broken = FALSE;
3f764659 6669 need_pad = sec;
1e2f5b6e 6670 relend = relstart + sec->reloc_count;
50bc7936 6671 for (rel = relstart; rel < relend; )
1e2f5b6e 6672 {
04c9666a 6673 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
6674 unsigned long r_symndx;
6675 asection *sym_sec;
6676 struct elf_link_hash_entry *h;
6677 Elf_Internal_Sym *sym;
51aecdc5 6678 bfd_vma offset;
1e2f5b6e 6679
51aecdc5 6680 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
6681 only interested in the reloc pointing to a function entry
6682 point. */
51aecdc5
AM
6683 offset = rel->r_offset;
6684 if (rel + 1 == relend
6685 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
6686 {
6687 /* If someone messes with .opd alignment then after a
6688 "ld -r" we might have padding in the middle of .opd.
6689 Also, there's nothing to prevent someone putting
6690 something silly in .opd with the assembler. No .opd
b34976b6 6691 optimization for them! */
3f764659 6692 broken_opd:
4eca0228 6693 _bfd_error_handler
871b3ab2 6694 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 6695 broken = TRUE;
1e2f5b6e
AM
6696 break;
6697 }
6698
50bc7936
AM
6699 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6700 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6701 {
4eca0228 6702 _bfd_error_handler
695344c0 6703 /* xgettext:c-format */
871b3ab2 6704 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 6705 ibfd, r_type);
51aecdc5 6706 broken = TRUE;
50bc7936
AM
6707 break;
6708 }
6709
1e2f5b6e 6710 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
6711 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6712 r_symndx, ibfd))
50bc7936 6713 goto error_ret;
1e2f5b6e
AM
6714
6715 if (sym_sec == NULL || sym_sec->owner == NULL)
6716 {
411e1bfb
AM
6717 const char *sym_name;
6718 if (h != NULL)
6719 sym_name = h->root.root.string;
6720 else
26c61ae5
L
6721 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6722 sym_sec);
411e1bfb 6723
4eca0228 6724 _bfd_error_handler
695344c0 6725 /* xgettext:c-format */
871b3ab2 6726 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 6727 ibfd, sym_name);
51aecdc5 6728 broken = TRUE;
1e2f5b6e
AM
6729 break;
6730 }
6731
51020317
AM
6732 /* opd entries are always for functions defined in the
6733 current input bfd. If the symbol isn't defined in the
6734 input bfd, then we won't be using the function in this
6735 bfd; It must be defined in a linkonce section in another
6736 bfd, or is weak. It's also possible that we are
6737 discarding the function due to a linker script /DISCARD/,
6738 which we test for via the output_section. */
6739 if (sym_sec->owner != ibfd
6740 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 6741 need_edit = TRUE;
1e2f5b6e 6742
50bc7936 6743 rel += 2;
51aecdc5
AM
6744 if (rel + 1 == relend
6745 || (rel + 2 < relend
6746 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
6747 ++rel;
6748
6749 if (rel == relend)
3f764659
JJ
6750 {
6751 if (sec->size == offset + 24)
6752 {
6753 need_pad = NULL;
6754 break;
6755 }
51aecdc5 6756 if (sec->size == offset + 16)
3f764659
JJ
6757 {
6758 cnt_16b++;
6759 break;
6760 }
6761 goto broken_opd;
6762 }
3f764659
JJ
6763 else if (rel + 1 < relend
6764 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6765 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6766 {
51aecdc5
AM
6767 if (rel[0].r_offset == offset + 16)
6768 cnt_16b++;
6769 else if (rel[0].r_offset != offset + 24)
6770 goto broken_opd;
3f764659
JJ
6771 }
6772 else
6773 goto broken_opd;
1e2f5b6e
AM
6774 }
6775
e7d1c40c 6776 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 6777
51aecdc5 6778 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
6779 {
6780 Elf_Internal_Rela *write_rel;
d4730f92 6781 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 6782 bfd_byte *rptr, *wptr;
983bddc8 6783 bfd_byte *new_contents;
74f0fb50
AM
6784 bfd_size_type amt;
6785
983bddc8 6786 new_contents = NULL;
51aecdc5 6787 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 6788 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 6789 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
6790 if (opd->adjust == NULL)
6791 return FALSE;
1e2f5b6e
AM
6792
6793 /* This seems a waste of time as input .opd sections are all
6794 zeros as generated by gcc, but I suppose there's no reason
6795 this will always be so. We might start putting something in
6796 the third word of .opd entries. */
6797 if ((sec->flags & SEC_IN_MEMORY) == 0)
6798 {
eea6121a
AM
6799 bfd_byte *loc;
6800 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 6801 {
eea6121a
AM
6802 if (loc != NULL)
6803 free (loc);
50bc7936 6804 error_ret:
6cdc0ccc
AM
6805 if (local_syms != NULL
6806 && symtab_hdr->contents != (unsigned char *) local_syms)
6807 free (local_syms);
6cdc0ccc
AM
6808 if (elf_section_data (sec)->relocs != relstart)
6809 free (relstart);
b34976b6 6810 return FALSE;
6cdc0ccc 6811 }
1e2f5b6e
AM
6812 sec->contents = loc;
6813 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6814 }
6815
6816 elf_section_data (sec)->relocs = relstart;
6817
3f764659 6818 new_contents = sec->contents;
3f764659
JJ
6819 if (add_aux_fields)
6820 {
6821 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6822 if (new_contents == NULL)
6823 return FALSE;
51aecdc5 6824 need_pad = NULL;
3f764659 6825 }
b4f4e59f
AM
6826 wptr = new_contents;
6827 rptr = sec->contents;
1e2f5b6e 6828 write_rel = relstart;
51aecdc5 6829 for (rel = relstart; rel < relend; )
1e2f5b6e 6830 {
50bc7936
AM
6831 unsigned long r_symndx;
6832 asection *sym_sec;
6833 struct elf_link_hash_entry *h;
51aecdc5 6834 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 6835 Elf_Internal_Sym *sym;
51aecdc5
AM
6836 long opd_ent_size;
6837 Elf_Internal_Rela *next_rel;
6838 bfd_boolean skip;
50bc7936
AM
6839
6840 r_symndx = ELF64_R_SYM (rel->r_info);
6841 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 6842 r_symndx, ibfd))
50bc7936
AM
6843 goto error_ret;
6844
51aecdc5
AM
6845 next_rel = rel + 2;
6846 if (next_rel + 1 == relend
6847 || (next_rel + 2 < relend
6848 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
6849 ++next_rel;
6850
6851 /* See if the .opd entry is full 24 byte or
6852 16 byte (with fd_aux entry overlapped with next
6853 fd_func). */
6854 opd_ent_size = 24;
6855 if (next_rel == relend)
1e2f5b6e 6856 {
51aecdc5 6857 if (sec->size == rel->r_offset + 16)
3f764659 6858 opd_ent_size = 16;
51aecdc5
AM
6859 }
6860 else if (next_rel->r_offset == rel->r_offset + 16)
6861 opd_ent_size = 16;
3f764659 6862
51aecdc5
AM
6863 if (h != NULL
6864 && h->root.root.string[0] == '.')
6865 {
8c5b4e52
AM
6866 fdh = ((struct ppc_link_hash_entry *) h)->oh;
6867 if (fdh != NULL)
6868 {
6869 fdh = ppc_follow_link (fdh);
6870 if (fdh->elf.root.type != bfd_link_hash_defined
6871 && fdh->elf.root.type != bfd_link_hash_defweak)
6872 fdh = NULL;
6873 }
51aecdc5 6874 }
1e2f5b6e 6875
51aecdc5
AM
6876 skip = (sym_sec->owner != ibfd
6877 || sym_sec->output_section == bfd_abs_section_ptr);
6878 if (skip)
6879 {
6880 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 6881 {
51aecdc5
AM
6882 /* Arrange for the function descriptor sym
6883 to be dropped. */
6884 fdh->elf.root.u.def.value = 0;
6885 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 6886 }
51aecdc5 6887 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 6888
0e1862bb 6889 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
6890 rel = next_rel;
6891 else
6892 while (1)
6893 {
6894 if (!dec_dynrel_count (rel->r_info, sec, info,
6895 NULL, h, sym))
6896 goto error_ret;
754021d0 6897
51aecdc5
AM
6898 if (++rel == next_rel)
6899 break;
1e2f5b6e 6900
51aecdc5
AM
6901 r_symndx = ELF64_R_SYM (rel->r_info);
6902 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6903 r_symndx, ibfd))
6904 goto error_ret;
6905 }
50bc7936
AM
6906 }
6907 else
1e2f5b6e 6908 {
51aecdc5
AM
6909 /* We'll be keeping this opd entry. */
6910 long adjust;
6911
6912 if (fdh != NULL)
6913 {
6914 /* Redefine the function descriptor symbol to
6915 this location in the opd section. It is
6916 necessary to update the value here rather
6917 than using an array of adjustments as we do
6918 for local symbols, because various places
6919 in the generic ELF code use the value
6920 stored in u.def.value. */
6921 fdh->elf.root.u.def.value = wptr - new_contents;
6922 fdh->adjust_done = 1;
6923 }
6924
6925 /* Local syms are a bit tricky. We could
6926 tweak them as they can be cached, but
6927 we'd need to look through the local syms
6928 for the function descriptor sym which we
6929 don't have at the moment. So keep an
6930 array of adjustments. */
6931 adjust = (wptr - new_contents) - (rptr - sec->contents);
6932 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
6933
6934 if (wptr != rptr)
6935 memcpy (wptr, rptr, opd_ent_size);
6936 wptr += opd_ent_size;
6937 if (add_aux_fields && opd_ent_size == 16)
6938 {
6939 memset (wptr, '\0', 8);
6940 wptr += 8;
6941 }
6942
50bc7936 6943 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
6944 new opd entries. */
6945 for ( ; rel != next_rel; ++rel)
6946 {
6947 rel->r_offset += adjust;
6948 if (write_rel != rel)
6949 memcpy (write_rel, rel, sizeof (*rel));
6950 ++write_rel;
6951 }
1e2f5b6e 6952 }
51aecdc5
AM
6953
6954 rptr += opd_ent_size;
1e2f5b6e
AM
6955 }
6956
3f764659 6957 sec->size = wptr - new_contents;
1e2f5b6e 6958 sec->reloc_count = write_rel - relstart;
3f764659
JJ
6959 if (add_aux_fields)
6960 {
6961 free (sec->contents);
6962 sec->contents = new_contents;
6963 }
6964
05bf9422 6965 /* Fudge the header size too, as this is used later in
cdcf6e38 6966 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
6967 rel_hdr = _bfd_elf_single_rel_hdr (sec);
6968 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 6969 some_edited = TRUE;
1e2f5b6e 6970 }
6cdc0ccc 6971 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 6972 free (relstart);
6cdc0ccc 6973
411e1bfb
AM
6974 if (local_syms != NULL
6975 && symtab_hdr->contents != (unsigned char *) local_syms)
6976 {
6977 if (!info->keep_memory)
6978 free (local_syms);
6979 else
6980 symtab_hdr->contents = (unsigned char *) local_syms;
6981 }
6982 }
6983
754021d0
AM
6984 if (some_edited)
6985 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
6986
3f764659
JJ
6987 /* If we are doing a final link and the last .opd entry is just 16 byte
6988 long, add a 8 byte padding after it. */
0e1862bb 6989 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
6990 {
6991 bfd_byte *p;
6992
6993 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
6994 {
6995 BFD_ASSERT (need_pad->size > 0);
6996
6997 p = bfd_malloc (need_pad->size + 8);
6998 if (p == NULL)
6999 return FALSE;
699733f6 7000
2cdcc330
AM
7001 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7002 p, 0, need_pad->size))
3f764659
JJ
7003 return FALSE;
7004
7005 need_pad->contents = p;
7006 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7007 }
7008 else
7009 {
7010 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7011 if (p == NULL)
7012 return FALSE;
7013
7014 need_pad->contents = p;
7015 }
7016
7017 memset (need_pad->contents + need_pad->size, 0, 8);
7018 need_pad->size += 8;
7019 }
7020
411e1bfb
AM
7021 return TRUE;
7022}
7023
3e04d765
AM
7024/* Analyze inline PLT call relocations to see whether calls to locally
7025 defined functions can be converted to direct calls. */
7026
7027bfd_boolean
7028ppc64_elf_inline_plt (struct bfd_link_info *info)
7029{
7030 struct ppc_link_hash_table *htab;
7031 bfd *ibfd;
7032 asection *sec;
7033 bfd_vma low_vma, high_vma, limit;
7034
7035 htab = ppc_hash_table (info);
7036 if (htab == NULL)
7037 return FALSE;
7038
7039 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7040 reduced somewhat to cater for possible stubs that might be added
7041 between the call and its destination. */
7042 if (htab->params->group_size < 0)
7043 {
7044 limit = -htab->params->group_size;
7045 if (limit == 1)
7046 limit = 0x1e00000;
7047 }
7048 else
7049 {
7050 limit = htab->params->group_size;
7051 if (limit == 1)
7052 limit = 0x1c00000;
7053 }
7054
7055 low_vma = -1;
7056 high_vma = 0;
7057 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7058 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7059 {
7060 if (low_vma > sec->vma)
7061 low_vma = sec->vma;
7062 if (high_vma < sec->vma + sec->size)
7063 high_vma = sec->vma + sec->size;
7064 }
7065
7066 /* If a "bl" can reach anywhere in local code sections, then we can
7067 convert all inline PLT sequences to direct calls when the symbol
7068 is local. */
7069 if (high_vma - low_vma < limit)
7070 {
7071 htab->can_convert_all_inline_plt = 1;
7072 return TRUE;
7073 }
7074
7075 /* Otherwise, go looking through relocs for cases where a direct
7076 call won't reach. Mark the symbol on any such reloc to disable
7077 the optimization and keep the PLT entry as it seems likely that
7078 this will be better than creating trampolines. Note that this
7079 will disable the optimization for all inline PLT calls to a
7080 particular symbol, not just those that won't reach. The
7081 difficulty in doing a more precise optimization is that the
7082 linker needs to make a decision depending on whether a
7083 particular R_PPC64_PLTCALL insn can be turned into a direct
7084 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7085 the sequence, and there is nothing that ties those relocs
7086 together except their symbol. */
7087
7088 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7089 {
7090 Elf_Internal_Shdr *symtab_hdr;
7091 Elf_Internal_Sym *local_syms;
7092
7093 if (!is_ppc64_elf (ibfd))
7094 continue;
7095
7096 local_syms = NULL;
7097 symtab_hdr = &elf_symtab_hdr (ibfd);
7098
7099 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7100 if (ppc64_elf_section_data (sec)->has_pltcall
7101 && !bfd_is_abs_section (sec->output_section))
7102 {
7103 Elf_Internal_Rela *relstart, *rel, *relend;
7104
7105 /* Read the relocations. */
7106 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7107 info->keep_memory);
7108 if (relstart == NULL)
7109 return FALSE;
7110
7111 relend = relstart + sec->reloc_count;
7112 for (rel = relstart; rel < relend; )
7113 {
7114 enum elf_ppc64_reloc_type r_type;
7115 unsigned long r_symndx;
7116 asection *sym_sec;
7117 struct elf_link_hash_entry *h;
7118 Elf_Internal_Sym *sym;
7119 unsigned char *tls_maskp;
7120
7121 r_type = ELF64_R_TYPE (rel->r_info);
7122 if (r_type != R_PPC64_PLTCALL)
7123 continue;
7124
7125 r_symndx = ELF64_R_SYM (rel->r_info);
7126 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7127 r_symndx, ibfd))
7128 {
7129 if (elf_section_data (sec)->relocs != relstart)
7130 free (relstart);
7131 if (local_syms != NULL
2cdcc330 7132 && symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7133 free (local_syms);
7134 return FALSE;
7135 }
7136
7137 if (sym_sec != NULL && sym_sec->output_section != NULL)
7138 {
7139 bfd_vma from, to;
7140 if (h != NULL)
7141 to = h->root.u.def.value;
7142 else
7143 to = sym->st_value;
7144 to += (rel->r_addend
7145 + sym_sec->output_offset
7146 + sym_sec->output_section->vma);
7147 from = (rel->r_offset
7148 + sec->output_offset
7149 + sec->output_section->vma);
7150 if (to - from + limit < 2 * limit)
7151 *tls_maskp &= ~PLT_KEEP;
7152 }
7153 }
7154 if (elf_section_data (sec)->relocs != relstart)
7155 free (relstart);
7156 }
7157
7158 if (local_syms != NULL
7159 && symtab_hdr->contents != (unsigned char *) local_syms)
7160 {
7161 if (!info->keep_memory)
7162 free (local_syms);
7163 else
7164 symtab_hdr->contents = (unsigned char *) local_syms;
7165 }
7166 }
7167
7168 return TRUE;
7169}
7170
e1918d23 7171/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7172
e1918d23 7173asection *
e7d1c40c 7174ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7175{
411e1bfb
AM
7176 struct ppc_link_hash_table *htab;
7177
411e1bfb 7178 htab = ppc_hash_table (info);
4dfe6ac6
NC
7179 if (htab == NULL)
7180 return NULL;
7181
ee67d69a
AM
7182 if (abiversion (info->output_bfd) == 1)
7183 htab->opd_abi = 1;
7184
e7d1c40c 7185 if (htab->params->no_multi_toc)
33c0ec9d
AM
7186 htab->do_multi_toc = 0;
7187 else if (!htab->do_multi_toc)
e7d1c40c 7188 htab->params->no_multi_toc = 1;
33c0ec9d 7189
8b5f1ed8
AM
7190 /* Default to --no-plt-localentry, as this option can cause problems
7191 with symbol interposition. For example, glibc libpthread.so and
7192 libc.so duplicate many pthread symbols, with a fallback
7193 implementation in libc.so. In some cases the fallback does more
7194 work than the pthread implementation. __pthread_condattr_destroy
7195 is one such symbol: the libpthread.so implementation is
7196 localentry:0 while the libc.so implementation is localentry:8.
7197 An app that "cleverly" uses dlopen to only load necessary
7198 libraries at runtime may omit loading libpthread.so when not
7199 running multi-threaded, which then results in the libc.so
7200 fallback symbols being used and ld.so complaining. Now there
7201 are workarounds in ld (see non_zero_localentry) to detect the
7202 pthread situation, but that may not be the only case where
7203 --plt-localentry can cause trouble. */
f378ab09 7204 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7205 htab->params->plt_localentry0 = 0;
d44c746a
AM
7206 if (htab->params->plt_localentry0
7207 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7208 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7209 _bfd_error_handler
7210 (_("warning: --plt-localentry is especially dangerous without "
7211 "ld.so support to detect ABI violations"));
f378ab09 7212
3a71aa26
AM
7213 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7214 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7215 FALSE, FALSE, TRUE));
a7f2871e
AM
7216 /* Move dynamic linking info to the function descriptor sym. */
7217 if (htab->tls_get_addr != NULL)
7218 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7219 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7220 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7221 FALSE, FALSE, TRUE));
7c9cf415 7222 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
7223 {
7224 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7225
7226 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7227 FALSE, FALSE, TRUE);
7228 if (opt != NULL)
7229 func_desc_adjust (opt, info);
7230 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7231 FALSE, FALSE, TRUE);
7232 if (opt_fd != NULL
7233 && (opt_fd->root.type == bfd_link_hash_defined
7234 || opt_fd->root.type == bfd_link_hash_defweak))
7235 {
7236 /* If glibc supports an optimized __tls_get_addr call stub,
7237 signalled by the presence of __tls_get_addr_opt, and we'll
7238 be calling __tls_get_addr via a plt call stub, then
7239 make __tls_get_addr point to __tls_get_addr_opt. */
7240 tga_fd = &htab->tls_get_addr_fd->elf;
7241 if (htab->elf.dynamic_sections_created
7242 && tga_fd != NULL
7243 && (tga_fd->type == STT_FUNC
7244 || tga_fd->needs_plt)
7245 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 7246 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
7247 {
7248 struct plt_entry *ent;
7249
7250 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7251 if (ent->plt.refcount > 0)
7252 break;
7253 if (ent != NULL)
7254 {
7255 tga_fd->root.type = bfd_link_hash_indirect;
7256 tga_fd->root.u.i.link = &opt_fd->root;
7257 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 7258 opt_fd->mark = 1;
a7f2871e
AM
7259 if (opt_fd->dynindx != -1)
7260 {
7261 /* Use __tls_get_addr_opt in dynamic relocations. */
7262 opt_fd->dynindx = -1;
7263 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7264 opt_fd->dynstr_index);
7265 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7266 return NULL;
a7f2871e 7267 }
2cdcc330
AM
7268 htab->tls_get_addr_fd
7269 = (struct ppc_link_hash_entry *) opt_fd;
a7f2871e
AM
7270 tga = &htab->tls_get_addr->elf;
7271 if (opt != NULL && tga != NULL)
7272 {
7273 tga->root.type = bfd_link_hash_indirect;
7274 tga->root.u.i.link = &opt->root;
7275 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 7276 opt->mark = 1;
a7f2871e
AM
7277 _bfd_elf_link_hash_hide_symbol (info, opt,
7278 tga->forced_local);
7279 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7280 }
7281 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7282 htab->tls_get_addr_fd->is_func_descriptor = 1;
7283 if (htab->tls_get_addr != NULL)
7284 {
7285 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7286 htab->tls_get_addr->is_func = 1;
7287 }
7288 }
7289 }
7290 }
7c9cf415
AM
7291 else if (htab->params->tls_get_addr_opt < 0)
7292 htab->params->tls_get_addr_opt = 0;
a7f2871e 7293 }
33c0ec9d 7294 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7295}
8387904d 7296
3a71aa26
AM
7297/* Return TRUE iff REL is a branch reloc with a global symbol matching
7298 HASH1 or HASH2. */
8387904d 7299
3a71aa26
AM
7300static bfd_boolean
7301branch_reloc_hash_match (const bfd *ibfd,
7302 const Elf_Internal_Rela *rel,
7303 const struct ppc_link_hash_entry *hash1,
7304 const struct ppc_link_hash_entry *hash2)
7305{
7306 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7307 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7308 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7309
e054468f 7310 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7311 {
3a71aa26
AM
7312 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7313 struct elf_link_hash_entry *h;
8387904d 7314
3a71aa26 7315 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7316 h = elf_follow_link (h);
3a71aa26
AM
7317 if (h == &hash1->elf || h == &hash2->elf)
7318 return TRUE;
a48ebf4d 7319 }
3a71aa26 7320 return FALSE;
951fd09b 7321}
411e1bfb 7322
951fd09b
AM
7323/* Run through all the TLS relocs looking for optimization
7324 opportunities. The linker has been hacked (see ppc64elf.em) to do
7325 a preliminary section layout so that we know the TLS segment
7326 offsets. We can't optimize earlier because some optimizations need
7327 to know the tp offset, and we need to optimize before allocating
7328 dynamic relocations. */
7329
7330bfd_boolean
33c0ec9d 7331ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7332{
7333 bfd *ibfd;
7334 asection *sec;
7335 struct ppc_link_hash_table *htab;
663a1470 7336 unsigned char *toc_ref;
102890f0 7337 int pass;
951fd09b 7338
3cbc1e5e 7339 if (!bfd_link_executable (info))
411e1bfb
AM
7340 return TRUE;
7341
951fd09b 7342 htab = ppc_hash_table (info);
4dfe6ac6
NC
7343 if (htab == NULL)
7344 return FALSE;
7345
663a1470
AM
7346 /* Make two passes over the relocs. On the first pass, mark toc
7347 entries involved with tls relocs, and check that tls relocs
7348 involved in setting up a tls_get_addr call are indeed followed by
7349 such a call. If they are not, we can't do any tls optimization.
7350 On the second pass twiddle tls_mask flags to notify
7351 relocate_section that optimization can be done, and adjust got
7352 and plt refcounts. */
7353 toc_ref = NULL;
7354 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7355 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7356 {
7357 Elf_Internal_Sym *locsyms = NULL;
7358 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7359
102890f0
AM
7360 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7361 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7362 {
7363 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7364 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7365
102890f0
AM
7366 /* Read the relocations. */
7367 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7368 info->keep_memory);
7369 if (relstart == NULL)
2915c55b
JK
7370 {
7371 free (toc_ref);
7372 return FALSE;
7373 }
411e1bfb 7374
102890f0
AM
7375 relend = relstart + sec->reloc_count;
7376 for (rel = relstart; rel < relend; rel++)
7377 {
7378 enum elf_ppc64_reloc_type r_type;
7379 unsigned long r_symndx;
7380 struct elf_link_hash_entry *h;
7381 Elf_Internal_Sym *sym;
7382 asection *sym_sec;
f961d9dd
AM
7383 unsigned char *tls_mask;
7384 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7385 bfd_vma value;
7386 bfd_boolean ok_tprel, is_local;
7387 long toc_ref_index = 0;
7388 int expecting_tls_get_addr = 0;
663a1470 7389 bfd_boolean ret = FALSE;
411e1bfb 7390
102890f0
AM
7391 r_symndx = ELF64_R_SYM (rel->r_info);
7392 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7393 r_symndx, ibfd))
7394 {
7395 err_free_rel:
7396 if (elf_section_data (sec)->relocs != relstart)
7397 free (relstart);
7398 if (toc_ref != NULL)
7399 free (toc_ref);
7400 if (locsyms != NULL
0ffa91dd 7401 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7402 != (unsigned char *) locsyms))
7403 free (locsyms);
663a1470 7404 return ret;
102890f0 7405 }
411e1bfb 7406
102890f0
AM
7407 if (h != NULL)
7408 {
766bc656
AM
7409 if (h->root.type == bfd_link_hash_defined
7410 || h->root.type == bfd_link_hash_defweak)
7411 value = h->root.u.def.value;
7412 else if (h->root.type == bfd_link_hash_undefweak)
7413 value = 0;
7414 else
663a1470
AM
7415 {
7416 found_tls_get_addr_arg = 0;
7417 continue;
7418 }
102890f0
AM
7419 }
7420 else
7421 /* Symbols referenced by TLS relocs must be of type
7422 STT_TLS. So no need for .opd local sym adjust. */
7423 value = sym->st_value;
7424
7425 ok_tprel = FALSE;
7426 is_local = FALSE;
7427 if (h == NULL
7428 || !h->def_dynamic)
7429 {
7430 is_local = TRUE;
766bc656
AM
7431 if (h != NULL
7432 && h->root.type == bfd_link_hash_undefweak)
7433 ok_tprel = TRUE;
c27b8c2a
AM
7434 else if (sym_sec != NULL
7435 && sym_sec->output_section != NULL)
766bc656
AM
7436 {
7437 value += sym_sec->output_offset;
7438 value += sym_sec->output_section->vma;
7439 value -= htab->elf.tls_sec->vma;
7440 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7441 < (bfd_vma) 1 << 32);
7442 }
102890f0 7443 }
951fd09b 7444
102890f0 7445 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7446 /* If this section has old-style __tls_get_addr calls
7447 without marker relocs, then check that each
7448 __tls_get_addr call reloc is preceded by a reloc
7449 that conceivably belongs to the __tls_get_addr arg
7450 setup insn. If we don't find matching arg setup
7451 relocs, don't do any tls optimization. */
7452 if (pass == 0
7453 && sec->has_tls_get_addr_call
7454 && h != NULL
7455 && (h == &htab->tls_get_addr->elf
7456 || h == &htab->tls_get_addr_fd->elf)
7457 && !found_tls_get_addr_arg
7458 && is_branch_reloc (r_type))
7459 {
25f53a85 7460 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7461 "TLS optimization disabled\n"),
7462 ibfd, sec, rel->r_offset);
7463 ret = TRUE;
7464 goto err_free_rel;
7465 }
7466
7467 found_tls_get_addr_arg = 0;
102890f0
AM
7468 switch (r_type)
7469 {
7470 case R_PPC64_GOT_TLSLD16:
7471 case R_PPC64_GOT_TLSLD16_LO:
7472 expecting_tls_get_addr = 1;
663a1470 7473 found_tls_get_addr_arg = 1;
1a0670f3 7474 /* Fall through. */
102890f0
AM
7475
7476 case R_PPC64_GOT_TLSLD16_HI:
7477 case R_PPC64_GOT_TLSLD16_HA:
7478 /* These relocs should never be against a symbol
7479 defined in a shared lib. Leave them alone if
7480 that turns out to be the case. */
7481 if (!is_local)
7482 continue;
411e1bfb 7483
102890f0 7484 /* LD -> LE */
411e1bfb 7485 tls_set = 0;
102890f0
AM
7486 tls_clear = TLS_LD;
7487 tls_type = TLS_TLS | TLS_LD;
7488 break;
411e1bfb 7489
102890f0
AM
7490 case R_PPC64_GOT_TLSGD16:
7491 case R_PPC64_GOT_TLSGD16_LO:
7492 expecting_tls_get_addr = 1;
663a1470 7493 found_tls_get_addr_arg = 1;
1a0670f3 7494 /* Fall through. */
102890f0
AM
7495
7496 case R_PPC64_GOT_TLSGD16_HI:
7497 case R_PPC64_GOT_TLSGD16_HA:
7498 if (ok_tprel)
7499 /* GD -> LE */
411e1bfb 7500 tls_set = 0;
102890f0
AM
7501 else
7502 /* GD -> IE */
7503 tls_set = TLS_TLS | TLS_TPRELGD;
7504 tls_clear = TLS_GD;
7505 tls_type = TLS_TLS | TLS_GD;
7506 break;
7507
7508 case R_PPC64_GOT_TPREL16_DS:
7509 case R_PPC64_GOT_TPREL16_LO_DS:
7510 case R_PPC64_GOT_TPREL16_HI:
7511 case R_PPC64_GOT_TPREL16_HA:
7512 if (ok_tprel)
7513 {
7514 /* IE -> LE */
7515 tls_set = 0;
7516 tls_clear = TLS_TPREL;
7517 tls_type = TLS_TLS | TLS_TPREL;
7518 break;
7519 }
411e1bfb
AM
7520 continue;
7521
727fc41e
AM
7522 case R_PPC64_TLSGD:
7523 case R_PPC64_TLSLD:
23cedd1d
AM
7524 if (rel + 1 < relend
7525 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7526 {
7527 if (pass != 0
2cdcc330
AM
7528 && (ELF64_R_TYPE (rel[1].r_info)
7529 != R_PPC64_PLTSEQ))
23cedd1d
AM
7530 {
7531 r_symndx = ELF64_R_SYM (rel[1].r_info);
7532 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 7533 r_symndx, ibfd))
23cedd1d
AM
7534 goto err_free_rel;
7535 if (h != NULL)
7536 {
7537 struct plt_entry *ent = NULL;
7538
7539 for (ent = h->plt.plist;
7540 ent != NULL;
7541 ent = ent->next)
7542 if (ent->addend == rel[1].r_addend)
7543 break;
7544
7545 if (ent != NULL
7546 && ent->plt.refcount > 0)
7547 ent->plt.refcount -= 1;
7548 }
7549 }
7550 continue;
7551 }
663a1470 7552 found_tls_get_addr_arg = 1;
1a0670f3 7553 /* Fall through. */
663a1470
AM
7554
7555 case R_PPC64_TLS:
7556 case R_PPC64_TOC16:
7557 case R_PPC64_TOC16_LO:
102890f0
AM
7558 if (sym_sec == NULL || sym_sec != toc)
7559 continue;
7560
7561 /* Mark this toc entry as referenced by a TLS
7562 code sequence. We can do that now in the
7563 case of R_PPC64_TLS, and after checking for
7564 tls_get_addr for the TOC16 relocs. */
7565 if (toc_ref == NULL)
2cdcc330
AM
7566 toc_ref
7567 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
7568 if (toc_ref == NULL)
7569 goto err_free_rel;
7570
102890f0
AM
7571 if (h != NULL)
7572 value = h->root.u.def.value;
7573 else
7574 value = sym->st_value;
7575 value += rel->r_addend;
73242275
AM
7576 if (value % 8 != 0)
7577 continue;
7578 BFD_ASSERT (value < toc->size
7579 && toc->output_offset % 8 == 0);
663a1470 7580 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7581 if (r_type == R_PPC64_TLS
7582 || r_type == R_PPC64_TLSGD
7583 || r_type == R_PPC64_TLSLD)
102890f0
AM
7584 {
7585 toc_ref[toc_ref_index] = 1;
7586 continue;
7587 }
7588
7589 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7590 continue;
7591
7592 tls_set = 0;
7593 tls_clear = 0;
7594 expecting_tls_get_addr = 2;
7595 break;
7596
7597 case R_PPC64_TPREL64:
7598 if (pass == 0
7599 || sec != toc
7600 || toc_ref == NULL
663a1470 7601 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7602 continue;
7603 if (ok_tprel)
7604 {
7605 /* IE -> LE */
7606 tls_set = TLS_EXPLICIT;
7607 tls_clear = TLS_TPREL;
7608 break;
7609 }
7610 continue;
7611
7612 case R_PPC64_DTPMOD64:
7613 if (pass == 0
7614 || sec != toc
7615 || toc_ref == NULL
663a1470 7616 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7617 continue;
7618 if (rel + 1 < relend
7619 && (rel[1].r_info
7620 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7621 && rel[1].r_offset == rel->r_offset + 8)
7622 {
7623 if (ok_tprel)
7624 /* GD -> LE */
7625 tls_set = TLS_EXPLICIT | TLS_GD;
7626 else
7627 /* GD -> IE */
7628 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7629 tls_clear = TLS_GD;
7630 }
7631 else
7632 {
7633 if (!is_local)
7634 continue;
7635
7636 /* LD -> LE */
7637 tls_set = TLS_EXPLICIT;
7638 tls_clear = TLS_LD;
7639 }
7640 break;
7641
7642 default:
7643 continue;
7644 }
7645
7646 if (pass == 0)
7647 {
727fc41e
AM
7648 if (!expecting_tls_get_addr
7649 || !sec->has_tls_get_addr_call)
102890f0
AM
7650 continue;
7651
3a71aa26
AM
7652 if (rel + 1 < relend
7653 && branch_reloc_hash_match (ibfd, rel + 1,
7654 htab->tls_get_addr,
7655 htab->tls_get_addr_fd))
102890f0 7656 {
3a71aa26 7657 if (expecting_tls_get_addr == 2)
102890f0 7658 {
3a71aa26 7659 /* Check for toc tls entries. */
f961d9dd 7660 unsigned char *toc_tls;
3a71aa26
AM
7661 int retval;
7662
7663 retval = get_tls_mask (&toc_tls, NULL, NULL,
7664 &locsyms,
7665 rel, ibfd);
7666 if (retval == 0)
7667 goto err_free_rel;
663a1470
AM
7668 if (toc_tls != NULL)
7669 {
37da22e5
AM
7670 if ((*toc_tls & TLS_TLS) != 0
7671 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
7672 found_tls_get_addr_arg = 1;
7673 if (retval > 1)
7674 toc_ref[toc_ref_index] = 1;
7675 }
102890f0 7676 }
3a71aa26 7677 continue;
102890f0
AM
7678 }
7679
102890f0
AM
7680 /* Uh oh, we didn't find the expected call. We
7681 could just mark this symbol to exclude it
7682 from tls optimization but it's safer to skip
663a1470 7683 the entire optimization. */
695344c0 7684 /* xgettext:c-format */
25f53a85 7685 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
7686 "TLS optimization disabled\n"),
7687 ibfd, sec, rel->r_offset);
7688 ret = TRUE;
7689 goto err_free_rel;
102890f0
AM
7690 }
7691
37da22e5
AM
7692 /* If we don't have old-style __tls_get_addr calls
7693 without TLSGD/TLSLD marker relocs, and we haven't
7694 found a new-style __tls_get_addr call with a
7695 marker for this symbol, then we either have a
7696 broken object file or an -mlongcall style
7697 indirect call to __tls_get_addr without a marker.
7698 Disable optimization in this case. */
7699 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
7700 && (tls_set & TLS_EXPLICIT) == 0
7701 && !sec->has_tls_get_addr_call
7702 && ((*tls_mask & (TLS_TLS | TLS_MARK))
7703 != (TLS_TLS | TLS_MARK)))
7704 continue;
7705
23cedd1d 7706 if (expecting_tls_get_addr)
102890f0 7707 {
23cedd1d
AM
7708 struct plt_entry *ent = NULL;
7709
7710 if (htab->tls_get_addr != NULL)
7711 for (ent = htab->tls_get_addr->elf.plt.plist;
7712 ent != NULL;
7713 ent = ent->next)
7714 if (ent->addend == 0)
102890f0 7715 break;
411e1bfb 7716
23cedd1d
AM
7717 if (ent == NULL && htab->tls_get_addr_fd != NULL)
7718 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7719 ent != NULL;
7720 ent = ent->next)
7721 if (ent->addend == 0)
102890f0 7722 break;
23cedd1d
AM
7723
7724 if (ent != NULL
7725 && ent->plt.refcount > 0)
7726 ent->plt.refcount -= 1;
102890f0 7727 }
411e1bfb 7728
102890f0 7729 if (tls_clear == 0)
30038c59
AM
7730 continue;
7731
102890f0
AM
7732 if ((tls_set & TLS_EXPLICIT) == 0)
7733 {
7734 struct got_entry *ent;
411e1bfb 7735
102890f0
AM
7736 /* Adjust got entry for this reloc. */
7737 if (h != NULL)
7738 ent = h->got.glist;
7739 else
7740 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 7741
102890f0
AM
7742 for (; ent != NULL; ent = ent->next)
7743 if (ent->addend == rel->r_addend
7744 && ent->owner == ibfd
7745 && ent->tls_type == tls_type)
7746 break;
7747 if (ent == NULL)
7748 abort ();
411e1bfb 7749
102890f0
AM
7750 if (tls_set == 0)
7751 {
7752 /* We managed to get rid of a got entry. */
7753 if (ent->got.refcount > 0)
7754 ent->got.refcount -= 1;
7755 }
7756 }
7757 else
7758 {
7759 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7760 we'll lose one or two dyn relocs. */
7761 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 7762 NULL, h, sym))
102890f0 7763 return FALSE;
411e1bfb 7764
102890f0
AM
7765 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7766 {
7767 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 7768 NULL, h, sym))
102890f0
AM
7769 return FALSE;
7770 }
7771 }
411e1bfb 7772
102890f0
AM
7773 *tls_mask |= tls_set;
7774 *tls_mask &= ~tls_clear;
7775 }
8c1d1bb8 7776
102890f0
AM
7777 if (elf_section_data (sec)->relocs != relstart)
7778 free (relstart);
7779 }
411e1bfb 7780
663a1470
AM
7781 if (locsyms != NULL
7782 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7783 {
7784 if (!info->keep_memory)
7785 free (locsyms);
7786 else
7787 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7788 }
7789 }
411e1bfb 7790
663a1470
AM
7791 if (toc_ref != NULL)
7792 free (toc_ref);
9a23f96e 7793 htab->do_tls_opt = 1;
b34976b6 7794 return TRUE;
1e2f5b6e 7795}
b34976b6 7796
c5614fa4
AM
7797/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7798 the values of any global symbols in a toc section that has been
7799 edited. Globals in toc sections should be a rarity, so this function
7800 sets a flag if any are found in toc sections other than the one just
de194d85 7801 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
7802
7803struct adjust_toc_info
7804{
7805 asection *toc;
7806 unsigned long *skip;
7807 bfd_boolean global_toc_syms;
7808};
7809
ba761f19
AM
7810enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7811
c5614fa4
AM
7812static bfd_boolean
7813adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7814{
7815 struct ppc_link_hash_entry *eh;
7816 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 7817 unsigned long i;
c5614fa4 7818
c5614fa4
AM
7819 if (h->root.type != bfd_link_hash_defined
7820 && h->root.type != bfd_link_hash_defweak)
7821 return TRUE;
7822
7823 eh = (struct ppc_link_hash_entry *) h;
7824 if (eh->adjust_done)
7825 return TRUE;
7826
7827 if (eh->elf.root.u.def.section == toc_inf->toc)
7828 {
854b41e7
AM
7829 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7830 i = toc_inf->toc->rawsize >> 3;
c5614fa4 7831 else
854b41e7
AM
7832 i = eh->elf.root.u.def.value >> 3;
7833
ba761f19 7834 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 7835 {
4eca0228 7836 _bfd_error_handler
854b41e7
AM
7837 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7838 do
7839 ++i;
ba761f19 7840 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 7841 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 7842 }
854b41e7
AM
7843
7844 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
7845 eh->adjust_done = 1;
7846 }
7847 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7848 toc_inf->global_toc_syms = TRUE;
7849
7850 return TRUE;
7851}
7852
39eeab25
AM
7853/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
7854 on a _LO variety toc/got reloc. */
560c8763
AM
7855
7856static bfd_boolean
39eeab25 7857ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 7858{
39eeab25
AM
7859 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7860 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
7861 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7862 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7863 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7864 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7865 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7866 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7867 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7868 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7869 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7870 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7871 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7872 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7873 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
7874 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7875 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7876 /* Exclude lfqu by testing reloc. If relocs are ever
7877 defined for the reduced D field in psq_lu then those
7878 will need testing too. */
7879 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7880 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7881 && (insn & 1) == 0)
7882 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7883 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7884 /* Exclude stfqu. psq_stu as above for psq_lu. */
7885 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7886 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7887 && (insn & 1) == 0));
560c8763
AM
7888}
7889
c5614fa4
AM
7890/* Examine all relocs referencing .toc sections in order to remove
7891 unused .toc entries. */
7892
7893bfd_boolean
33c0ec9d 7894ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
7895{
7896 bfd *ibfd;
7897 struct adjust_toc_info toc_inf;
67f0cbdb 7898 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 7899
67f0cbdb 7900 htab->do_toc_opt = 1;
c5614fa4 7901 toc_inf.global_toc_syms = TRUE;
c72f2fb2 7902 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
7903 {
7904 asection *toc, *sec;
7905 Elf_Internal_Shdr *symtab_hdr;
7906 Elf_Internal_Sym *local_syms;
425b145b 7907 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
7908 unsigned long *skip, *drop;
7909 unsigned char *used;
7910 unsigned char *keep, last, some_unused;
7911
854b41e7
AM
7912 if (!is_ppc64_elf (ibfd))
7913 continue;
7914
c5614fa4
AM
7915 toc = bfd_get_section_by_name (ibfd, ".toc");
7916 if (toc == NULL
92b7a70f 7917 || toc->size == 0
dbaa2011
AM
7918 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
7919 || discarded_section (toc))
c5614fa4
AM
7920 continue;
7921
425b145b 7922 toc_relocs = NULL;
c5614fa4 7923 local_syms = NULL;
0ffa91dd 7924 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
7925
7926 /* Look at sections dropped from the final link. */
7927 skip = NULL;
7928 relstart = NULL;
7929 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7930 {
7931 if (sec->reloc_count == 0
dbaa2011 7932 || !discarded_section (sec)
c5614fa4
AM
7933 || get_opd_info (sec)
7934 || (sec->flags & SEC_ALLOC) == 0
7935 || (sec->flags & SEC_DEBUGGING) != 0)
7936 continue;
7937
7938 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7939 if (relstart == NULL)
7940 goto error_ret;
7941
7942 /* Run through the relocs to see which toc entries might be
7943 unused. */
7944 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7945 {
7946 enum elf_ppc64_reloc_type r_type;
7947 unsigned long r_symndx;
7948 asection *sym_sec;
7949 struct elf_link_hash_entry *h;
7950 Elf_Internal_Sym *sym;
7951 bfd_vma val;
7952
7953 r_type = ELF64_R_TYPE (rel->r_info);
7954 switch (r_type)
7955 {
7956 default:
7957 continue;
7958
7959 case R_PPC64_TOC16:
7960 case R_PPC64_TOC16_LO:
7961 case R_PPC64_TOC16_HI:
7962 case R_PPC64_TOC16_HA:
7963 case R_PPC64_TOC16_DS:
7964 case R_PPC64_TOC16_LO_DS:
7965 break;
7966 }
7967
7968 r_symndx = ELF64_R_SYM (rel->r_info);
7969 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7970 r_symndx, ibfd))
7971 goto error_ret;
7972
7973 if (sym_sec != toc)
7974 continue;
7975
7976 if (h != NULL)
7977 val = h->root.u.def.value;
7978 else
7979 val = sym->st_value;
7980 val += rel->r_addend;
7981
7982 if (val >= toc->size)
7983 continue;
7984
7985 /* Anything in the toc ought to be aligned to 8 bytes.
7986 If not, don't mark as unused. */
7987 if (val & 7)
7988 continue;
7989
7990 if (skip == NULL)
7991 {
854b41e7 7992 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
7993 if (skip == NULL)
7994 goto error_ret;
7995 }
7996
ba761f19 7997 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
7998 }
7999
8000 if (elf_section_data (sec)->relocs != relstart)
8001 free (relstart);
8002 }
8003
ba761f19
AM
8004 /* For largetoc loads of address constants, we can convert
8005 . addis rx,2,addr@got@ha
8006 . ld ry,addr@got@l(rx)
8007 to
8008 . addis rx,2,addr@toc@ha
8009 . addi ry,rx,addr@toc@l
8010 when addr is within 2G of the toc pointer. This then means
8011 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8012
ba761f19
AM
8013 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8014 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8015 && toc->reloc_count != 0)
8016 {
8017 /* Read toc relocs. */
425b145b
AM
8018 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8019 info->keep_memory);
8020 if (toc_relocs == NULL)
ba761f19
AM
8021 goto error_ret;
8022
425b145b 8023 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8024 {
8025 enum elf_ppc64_reloc_type r_type;
8026 unsigned long r_symndx;
8027 asection *sym_sec;
8028 struct elf_link_hash_entry *h;
8029 Elf_Internal_Sym *sym;
8030 bfd_vma val, addr;
8031
8032 r_type = ELF64_R_TYPE (rel->r_info);
8033 if (r_type != R_PPC64_ADDR64)
8034 continue;
8035
8036 r_symndx = ELF64_R_SYM (rel->r_info);
8037 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8038 r_symndx, ibfd))
8039 goto error_ret;
8040
425b145b 8041 if (sym_sec == NULL
c27b8c2a 8042 || sym_sec->output_section == NULL
dbaa2011 8043 || discarded_section (sym_sec))
425b145b
AM
8044 continue;
8045
afe397ea 8046 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8047 continue;
8048
8049 if (h != NULL)
bddc25c9
AM
8050 {
8051 if (h->type == STT_GNU_IFUNC)
8052 continue;
8053 val = h->root.u.def.value;
8054 }
ba761f19 8055 else
bddc25c9
AM
8056 {
8057 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8058 continue;
8059 val = sym->st_value;
8060 }
ba761f19
AM
8061 val += rel->r_addend;
8062 val += sym_sec->output_section->vma + sym_sec->output_offset;
8063
8064 /* We don't yet know the exact toc pointer value, but we
8065 know it will be somewhere in the toc section. Don't
8066 optimize if the difference from any possible toc
8067 pointer is outside [ff..f80008000, 7fff7fff]. */
8068 addr = toc->output_section->vma + TOC_BASE_OFF;
8069 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8070 continue;
8071
8072 addr = toc->output_section->vma + toc->output_section->rawsize;
8073 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8074 continue;
8075
8076 if (skip == NULL)
8077 {
8078 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8079 if (skip == NULL)
8080 goto error_ret;
8081 }
8082
8083 skip[rel->r_offset >> 3]
425b145b 8084 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8085 }
ba761f19
AM
8086 }
8087
c5614fa4
AM
8088 if (skip == NULL)
8089 continue;
8090
8091 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8092 if (used == NULL)
8093 {
8094 error_ret:
8095 if (local_syms != NULL
8096 && symtab_hdr->contents != (unsigned char *) local_syms)
8097 free (local_syms);
8098 if (sec != NULL
8099 && relstart != NULL
8100 && elf_section_data (sec)->relocs != relstart)
8101 free (relstart);
425b145b
AM
8102 if (toc_relocs != NULL
8103 && elf_section_data (toc)->relocs != toc_relocs)
8104 free (toc_relocs);
c5614fa4
AM
8105 if (skip != NULL)
8106 free (skip);
8107 return FALSE;
8108 }
8109
30038c59
AM
8110 /* Now check all kept sections that might reference the toc.
8111 Check the toc itself last. */
8112 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8113 : ibfd->sections);
c5614fa4 8114 sec != NULL;
c5614fa4 8115 sec = (sec == toc ? NULL
c5614fa4 8116 : sec->next == NULL ? toc
30038c59 8117 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8118 : sec->next))
8119 {
8120 int repeat;
8121
8122 if (sec->reloc_count == 0
dbaa2011 8123 || discarded_section (sec)
c5614fa4
AM
8124 || get_opd_info (sec)
8125 || (sec->flags & SEC_ALLOC) == 0
8126 || (sec->flags & SEC_DEBUGGING) != 0)
8127 continue;
8128
854b41e7
AM
8129 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8130 info->keep_memory);
c5614fa4 8131 if (relstart == NULL)
2915c55b
JK
8132 {
8133 free (used);
8134 goto error_ret;
8135 }
c5614fa4
AM
8136
8137 /* Mark toc entries referenced as used. */
c5614fa4 8138 do
d4f1ee75
AM
8139 {
8140 repeat = 0;
8141 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8142 {
8143 enum elf_ppc64_reloc_type r_type;
8144 unsigned long r_symndx;
8145 asection *sym_sec;
8146 struct elf_link_hash_entry *h;
8147 Elf_Internal_Sym *sym;
8148 bfd_vma val;
8149 enum {no_check, check_lo, check_ha} insn_check;
98528052 8150
d4f1ee75
AM
8151 r_type = ELF64_R_TYPE (rel->r_info);
8152 switch (r_type)
8153 {
8154 default:
8155 insn_check = no_check;
8156 break;
98528052 8157
d4f1ee75
AM
8158 case R_PPC64_GOT_TLSLD16_HA:
8159 case R_PPC64_GOT_TLSGD16_HA:
8160 case R_PPC64_GOT_TPREL16_HA:
8161 case R_PPC64_GOT_DTPREL16_HA:
8162 case R_PPC64_GOT16_HA:
8163 case R_PPC64_TOC16_HA:
8164 insn_check = check_ha;
8165 break;
98528052 8166
d4f1ee75
AM
8167 case R_PPC64_GOT_TLSLD16_LO:
8168 case R_PPC64_GOT_TLSGD16_LO:
8169 case R_PPC64_GOT_TPREL16_LO_DS:
8170 case R_PPC64_GOT_DTPREL16_LO_DS:
8171 case R_PPC64_GOT16_LO:
8172 case R_PPC64_GOT16_LO_DS:
8173 case R_PPC64_TOC16_LO:
8174 case R_PPC64_TOC16_LO_DS:
8175 insn_check = check_lo;
8176 break;
8177 }
560c8763 8178
d4f1ee75
AM
8179 if (insn_check != no_check)
8180 {
8181 bfd_vma off = rel->r_offset & ~3;
8182 unsigned char buf[4];
8183 unsigned int insn;
c5614fa4 8184
d4f1ee75
AM
8185 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8186 {
8187 free (used);
8188 goto error_ret;
8189 }
8190 insn = bfd_get_32 (ibfd, buf);
8191 if (insn_check == check_lo
39eeab25 8192 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
8193 : ((insn & ((0x3f << 26) | 0x1f << 16))
8194 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8195 {
8196 char str[12];
8197
8198 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8199 sprintf (str, "%#08x", insn);
8200 info->callbacks->einfo
695344c0 8201 /* xgettext:c-format */
174d0a74 8202 (_("%H: toc optimization is not supported for"
cf97bcb0 8203 " %s instruction\n"),
d4f1ee75
AM
8204 ibfd, sec, rel->r_offset & ~3, str);
8205 }
8206 }
c5614fa4 8207
d4f1ee75
AM
8208 switch (r_type)
8209 {
8210 case R_PPC64_TOC16:
8211 case R_PPC64_TOC16_LO:
8212 case R_PPC64_TOC16_HI:
8213 case R_PPC64_TOC16_HA:
8214 case R_PPC64_TOC16_DS:
8215 case R_PPC64_TOC16_LO_DS:
8216 /* In case we're taking addresses of toc entries. */
8217 case R_PPC64_ADDR64:
8218 break;
c5614fa4 8219
d4f1ee75
AM
8220 default:
8221 continue;
8222 }
c5614fa4 8223
d4f1ee75
AM
8224 r_symndx = ELF64_R_SYM (rel->r_info);
8225 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8226 r_symndx, ibfd))
8227 {
8228 free (used);
8229 goto error_ret;
8230 }
c5614fa4 8231
d4f1ee75
AM
8232 if (sym_sec != toc)
8233 continue;
c5614fa4 8234
d4f1ee75
AM
8235 if (h != NULL)
8236 val = h->root.u.def.value;
8237 else
8238 val = sym->st_value;
8239 val += rel->r_addend;
ba761f19 8240
d4f1ee75
AM
8241 if (val >= toc->size)
8242 continue;
ba761f19 8243
d4f1ee75
AM
8244 if ((skip[val >> 3] & can_optimize) != 0)
8245 {
8246 bfd_vma off;
8247 unsigned char opc;
8248
8249 switch (r_type)
8250 {
8251 case R_PPC64_TOC16_HA:
ba761f19 8252 break;
ba761f19 8253
d4f1ee75
AM
8254 case R_PPC64_TOC16_LO_DS:
8255 off = rel->r_offset;
8256 off += (bfd_big_endian (ibfd) ? -2 : 3);
8257 if (!bfd_get_section_contents (ibfd, sec, &opc,
8258 off, 1))
8259 {
8260 free (used);
8261 goto error_ret;
8262 }
8263 if ((opc & (0x3f << 2)) == (58u << 2))
8264 break;
1a0670f3 8265 /* Fall through. */
ba761f19 8266
d4f1ee75
AM
8267 default:
8268 /* Wrong sort of reloc, or not a ld. We may
8269 as well clear ref_from_discarded too. */
8270 skip[val >> 3] = 0;
8271 }
8272 }
8273
8274 if (sec != toc)
8275 used[val >> 3] = 1;
8276 /* For the toc section, we only mark as used if this
8277 entry itself isn't unused. */
8278 else if ((used[rel->r_offset >> 3]
8279 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8280 && !used[val >> 3])
8281 {
8282 /* Do all the relocs again, to catch reference
8283 chains. */
8284 repeat = 1;
8285 used[val >> 3] = 1;
8286 }
8287 }
8288 }
c5614fa4 8289 while (repeat);
854b41e7
AM
8290
8291 if (elf_section_data (sec)->relocs != relstart)
8292 free (relstart);
c5614fa4
AM
8293 }
8294
8295 /* Merge the used and skip arrays. Assume that TOC
8296 doublewords not appearing as either used or unused belong
de194d85 8297 to an entry more than one doubleword in size. */
c5614fa4
AM
8298 for (drop = skip, keep = used, last = 0, some_unused = 0;
8299 drop < skip + (toc->size + 7) / 8;
8300 ++drop, ++keep)
8301 {
8302 if (*keep)
8303 {
ba761f19
AM
8304 *drop &= ~ref_from_discarded;
8305 if ((*drop & can_optimize) != 0)
8306 some_unused = 1;
c5614fa4
AM
8307 last = 0;
8308 }
b140b010 8309 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8310 {
8311 some_unused = 1;
ba761f19 8312 last = ref_from_discarded;
c5614fa4
AM
8313 }
8314 else
8315 *drop = last;
8316 }
8317
8318 free (used);
8319
8320 if (some_unused)
8321 {
8322 bfd_byte *contents, *src;
8323 unsigned long off;
d62b3684 8324 Elf_Internal_Sym *sym;
ba761f19 8325 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8326
8327 /* Shuffle the toc contents, and at the same time convert the
8328 skip array from booleans into offsets. */
8329 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8330 goto error_ret;
8331
8332 elf_section_data (toc)->this_hdr.contents = contents;
8333
8334 for (src = contents, off = 0, drop = skip;
8335 src < contents + toc->size;
8336 src += 8, ++drop)
8337 {
ba761f19
AM
8338 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8339 off += 8;
c5614fa4
AM
8340 else if (off != 0)
8341 {
8342 *drop = off;
8343 memcpy (src - off, src, 8);
8344 }
8345 }
854b41e7 8346 *drop = off;
c5614fa4
AM
8347 toc->rawsize = toc->size;
8348 toc->size = src - contents - off;
8349
ba761f19
AM
8350 /* Adjust addends for relocs against the toc section sym,
8351 and optimize any accesses we can. */
c5614fa4
AM
8352 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8353 {
8354 if (sec->reloc_count == 0
dbaa2011 8355 || discarded_section (sec))
c5614fa4
AM
8356 continue;
8357
8358 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8359 info->keep_memory);
c5614fa4
AM
8360 if (relstart == NULL)
8361 goto error_ret;
8362
8363 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8364 {
8365 enum elf_ppc64_reloc_type r_type;
8366 unsigned long r_symndx;
8367 asection *sym_sec;
8368 struct elf_link_hash_entry *h;
854b41e7 8369 bfd_vma val;
c5614fa4
AM
8370
8371 r_type = ELF64_R_TYPE (rel->r_info);
8372 switch (r_type)
8373 {
8374 default:
8375 continue;
8376
8377 case R_PPC64_TOC16:
8378 case R_PPC64_TOC16_LO:
8379 case R_PPC64_TOC16_HI:
8380 case R_PPC64_TOC16_HA:
8381 case R_PPC64_TOC16_DS:
8382 case R_PPC64_TOC16_LO_DS:
8383 case R_PPC64_ADDR64:
8384 break;
8385 }
8386
8387 r_symndx = ELF64_R_SYM (rel->r_info);
8388 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8389 r_symndx, ibfd))
8390 goto error_ret;
8391
ba761f19 8392 if (sym_sec != toc)
c5614fa4
AM
8393 continue;
8394
ba761f19
AM
8395 if (h != NULL)
8396 val = h->root.u.def.value;
8397 else
8398 {
8399 val = sym->st_value;
8400 if (val != 0)
8401 local_toc_syms = TRUE;
8402 }
8403
8404 val += rel->r_addend;
854b41e7
AM
8405
8406 if (val > toc->rawsize)
8407 val = toc->rawsize;
ba761f19
AM
8408 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8409 continue;
8410 else if ((skip[val >> 3] & can_optimize) != 0)
8411 {
8412 Elf_Internal_Rela *tocrel
425b145b 8413 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8414 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8415
8416 switch (r_type)
8417 {
8418 case R_PPC64_TOC16_HA:
8419 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8420 break;
8421
8422 case R_PPC64_TOC16_LO_DS:
8423 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8424 break;
8425
8426 default:
28942f62
AM
8427 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8428 ppc_howto_init ();
b140b010 8429 info->callbacks->einfo
695344c0 8430 /* xgettext:c-format */
174d0a74 8431 (_("%H: %s references "
b140b010
AM
8432 "optimized away TOC entry\n"),
8433 ibfd, sec, rel->r_offset,
8434 ppc64_elf_howto_table[r_type]->name);
8435 bfd_set_error (bfd_error_bad_value);
8436 goto error_ret;
ba761f19
AM
8437 }
8438 rel->r_addend = tocrel->r_addend;
8439 elf_section_data (sec)->relocs = relstart;
8440 continue;
8441 }
8442
8443 if (h != NULL || sym->st_value != 0)
8444 continue;
854b41e7
AM
8445
8446 rel->r_addend -= skip[val >> 3];
8447 elf_section_data (sec)->relocs = relstart;
c5614fa4 8448 }
854b41e7
AM
8449
8450 if (elf_section_data (sec)->relocs != relstart)
8451 free (relstart);
c5614fa4
AM
8452 }
8453
8454 /* We shouldn't have local or global symbols defined in the TOC,
8455 but handle them anyway. */
df22d223
AM
8456 if (local_syms != NULL)
8457 for (sym = local_syms;
8458 sym < local_syms + symtab_hdr->sh_info;
8459 ++sym)
8460 if (sym->st_value != 0
8461 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8462 {
8463 unsigned long i;
854b41e7 8464
df22d223
AM
8465 if (sym->st_value > toc->rawsize)
8466 i = toc->rawsize >> 3;
8467 else
8468 i = sym->st_value >> 3;
854b41e7 8469
df22d223
AM
8470 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8471 {
8472 if (local_toc_syms)
4eca0228 8473 _bfd_error_handler
df22d223
AM
8474 (_("%s defined on removed toc entry"),
8475 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8476 do
8477 ++i;
8478 while ((skip[i] & (ref_from_discarded | can_optimize)));
8479 sym->st_value = (bfd_vma) i << 3;
8480 }
d62b3684 8481
df22d223
AM
8482 sym->st_value -= skip[i];
8483 symtab_hdr->contents = (unsigned char *) local_syms;
8484 }
c5614fa4 8485
854b41e7 8486 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8487 if (toc_inf.global_toc_syms)
8488 {
8489 toc_inf.toc = toc;
8490 toc_inf.skip = skip;
8491 toc_inf.global_toc_syms = FALSE;
8492 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8493 &toc_inf);
8494 }
854b41e7
AM
8495
8496 if (toc->reloc_count != 0)
8497 {
d4730f92 8498 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8499 Elf_Internal_Rela *wrel;
8500 bfd_size_type sz;
8501
854b41e7 8502 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8503 if (toc_relocs == NULL)
8504 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8505 info->keep_memory);
8506 if (toc_relocs == NULL)
8507 goto error_ret;
8508
425b145b
AM
8509 wrel = toc_relocs;
8510 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8511 if ((skip[rel->r_offset >> 3]
8512 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8513 {
8514 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8515 wrel->r_info = rel->r_info;
8516 wrel->r_addend = rel->r_addend;
8517 ++wrel;
8518 }
8519 else if (!dec_dynrel_count (rel->r_info, toc, info,
8520 &local_syms, NULL, NULL))
8521 goto error_ret;
8522
425b145b
AM
8523 elf_section_data (toc)->relocs = toc_relocs;
8524 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8525 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8526 sz = rel_hdr->sh_entsize;
8527 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8528 }
c5614fa4 8529 }
28be611c
AM
8530 else if (toc_relocs != NULL
8531 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8532 free (toc_relocs);
c5614fa4
AM
8533
8534 if (local_syms != NULL
8535 && symtab_hdr->contents != (unsigned char *) local_syms)
8536 {
8537 if (!info->keep_memory)
8538 free (local_syms);
8539 else
8540 symtab_hdr->contents = (unsigned char *) local_syms;
8541 }
8542 free (skip);
8543 }
8544
8545 return TRUE;
8546}
8547
1bbe0902
AM
8548/* Return true iff input section I references the TOC using
8549 instructions limited to +/-32k offsets. */
8550
8551bfd_boolean
8552ppc64_elf_has_small_toc_reloc (asection *i)
8553{
8554 return (is_ppc64_elf (i->owner)
8555 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8556}
8557
927be08e
AM
8558/* Allocate space for one GOT entry. */
8559
8560static void
8561allocate_got (struct elf_link_hash_entry *h,
8562 struct bfd_link_info *info,
8563 struct got_entry *gent)
8564{
8565 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
8566 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8567 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8568 ? 16 : 8);
8569 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8570 ? 2 : 1) * sizeof (Elf64_External_Rela);
8571 asection *got = ppc64_elf_tdata (gent->owner)->got;
8572
8573 gent->got.offset = got->size;
8574 got->size += entsize;
8575
19e08130 8576 if (h->type == STT_GNU_IFUNC)
927be08e 8577 {
33e44f2e 8578 htab->elf.irelplt->size += rentsize;
19e08130 8579 htab->got_reli_size += rentsize;
927be08e 8580 }
f15d0b54
AM
8581 else if (((bfd_link_pic (info)
8582 && !((gent->tls_type & TLS_TPREL) != 0
8583 && bfd_link_executable (info)
8584 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
8585 || (htab->elf.dynamic_sections_created
8586 && h->dynindx != -1
8587 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 8588 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 8589 {
19e08130 8590 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 8591 relgot->size += rentsize;
927be08e
AM
8592 }
8593}
8594
7865406b
AM
8595/* This function merges got entries in the same toc group. */
8596
8597static void
8598merge_got_entries (struct got_entry **pent)
8599{
8600 struct got_entry *ent, *ent2;
8601
8602 for (ent = *pent; ent != NULL; ent = ent->next)
8603 if (!ent->is_indirect)
8604 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8605 if (!ent2->is_indirect
8606 && ent2->addend == ent->addend
8607 && ent2->tls_type == ent->tls_type
8608 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8609 {
8610 ent2->is_indirect = TRUE;
8611 ent2->got.ent = ent;
8612 }
8613}
8614
46434633 8615/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
8616
8617static bfd_boolean
46434633
AM
8618ensure_undef_dynamic (struct bfd_link_info *info,
8619 struct elf_link_hash_entry *h)
f0158f44
AM
8620{
8621 struct elf_link_hash_table *htab = elf_hash_table (info);
8622
8623 if (htab->dynamic_sections_created
46434633
AM
8624 && ((info->dynamic_undefined_weak != 0
8625 && h->root.type == bfd_link_hash_undefweak)
8626 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
8627 && h->dynindx == -1
8628 && !h->forced_local
8629 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
8630 return bfd_elf_link_record_dynamic_symbol (info, h);
8631 return TRUE;
8632}
8633
65f38f15
AM
8634/* Allocate space in .plt, .got and associated reloc sections for
8635 dynamic relocs. */
5bd4f169 8636
b34976b6 8637static bfd_boolean
4ce794b7 8638allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 8639{
65f38f15
AM
8640 struct bfd_link_info *info;
8641 struct ppc_link_hash_table *htab;
5bd4f169 8642 asection *s;
65f38f15 8643 struct ppc_link_hash_entry *eh;
0b8bcf0d 8644 struct got_entry **pgent, *gent;
5bd4f169 8645
e92d460e 8646 if (h->root.type == bfd_link_hash_indirect)
b34976b6 8647 return TRUE;
5bd4f169 8648
65f38f15
AM
8649 info = (struct bfd_link_info *) inf;
8650 htab = ppc_hash_table (info);
4dfe6ac6
NC
8651 if (htab == NULL)
8652 return FALSE;
5bd4f169 8653
951fd09b
AM
8654 eh = (struct ppc_link_hash_entry *) h;
8655 /* Run through the TLS GD got entries first if we're changing them
8656 to TPREL. */
37da22e5 8657 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
951fd09b
AM
8658 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8659 if (gent->got.refcount > 0
8660 && (gent->tls_type & TLS_GD) != 0)
8661 {
8662 /* This was a GD entry that has been converted to TPREL. If
8663 there happens to be a TPREL entry we can use that one. */
8664 struct got_entry *ent;
8665 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8666 if (ent->got.refcount > 0
8667 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
8668 && ent->addend == gent->addend
8669 && ent->owner == gent->owner)
951fd09b
AM
8670 {
8671 gent->got.refcount = 0;
8672 break;
8673 }
8674
8675 /* If not, then we'll be using our own TPREL entry. */
8676 if (gent->got.refcount != 0)
8677 gent->tls_type = TLS_TLS | TLS_TPREL;
8678 }
8679
7865406b
AM
8680 /* Remove any list entry that won't generate a word in the GOT before
8681 we call merge_got_entries. Otherwise we risk merging to empty
8682 entries. */
0b8bcf0d
AM
8683 pgent = &h->got.glist;
8684 while ((gent = *pgent) != NULL)
411e1bfb 8685 if (gent->got.refcount > 0)
7865406b
AM
8686 {
8687 if ((gent->tls_type & TLS_LD) != 0
8688 && !h->def_dynamic)
8689 {
8690 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8691 *pgent = gent->next;
8692 }
8693 else
8694 pgent = &gent->next;
8695 }
8696 else
8697 *pgent = gent->next;
8698
8699 if (!htab->do_multi_toc)
8700 merge_got_entries (&h->got.glist);
8701
8702 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8703 if (!gent->is_indirect)
411e1bfb 8704 {
46434633
AM
8705 /* Make sure this symbol is output as a dynamic symbol. */
8706 if (!ensure_undef_dynamic (info, h))
f0158f44 8707 return FALSE;
65f38f15 8708
0c8d6e5c 8709 if (!is_ppc64_elf (gent->owner))
927be08e 8710 abort ();
0ffa91dd 8711
927be08e 8712 allocate_got (h, info, gent);
411e1bfb 8713 }
65f38f15 8714
954b63d4
AM
8715 /* If no dynamic sections we can't have dynamic relocs, except for
8716 IFUNCs which are handled even in static executables. */
8a2058b5
AM
8717 if (!htab->elf.dynamic_sections_created
8718 && h->type != STT_GNU_IFUNC)
8719 eh->dyn_relocs = NULL;
8720
529fe20e
AM
8721 /* Discard relocs on undefined symbols that must be local. */
8722 else if (h->root.type == bfd_link_hash_undefined
8723 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8724 eh->dyn_relocs = NULL;
8725
954b63d4
AM
8726 /* Also discard relocs on undefined weak syms with non-default
8727 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 8728 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
8729 eh->dyn_relocs = NULL;
8730
8a2058b5 8731 if (eh->dyn_relocs != NULL)
65f38f15 8732 {
8a2058b5
AM
8733 struct elf_dyn_relocs *p, **pp;
8734
57e7d118
AM
8735 /* In the shared -Bsymbolic case, discard space allocated for
8736 dynamic pc-relative relocs against symbols which turn out to
8737 be defined in regular objects. For the normal shared case,
8738 discard space for relocs that have become local due to symbol
8739 visibility changes. */
8740
8741 if (bfd_link_pic (info))
65f38f15 8742 {
57e7d118
AM
8743 /* Relocs that use pc_count are those that appear on a call
8744 insn, or certain REL relocs (see must_be_dyn_reloc) that
8745 can be generated via assembly. We want calls to
8746 protected symbols to resolve directly to the function
8747 rather than going via the plt. If people want function
8748 pointer comparisons to work as expected then they should
8749 avoid writing weird assembly. */
8750 if (SYMBOL_CALLS_LOCAL (info, h))
8751 {
57e7d118
AM
8752 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8753 {
8754 p->count -= p->pc_count;
8755 p->pc_count = 0;
8756 if (p->count == 0)
8757 *pp = p->next;
8758 else
8759 pp = &p->next;
8760 }
8761 }
65f38f15 8762
954b63d4 8763 if (eh->dyn_relocs != NULL)
5bd4f169 8764 {
46434633
AM
8765 /* Make sure this symbol is output as a dynamic symbol. */
8766 if (!ensure_undef_dynamic (info, h))
f0158f44 8767 return FALSE;
5bd4f169 8768 }
65f38f15 8769 }
529fe20e 8770 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 8771 {
8a2058b5 8772 /* For the non-pic case, discard space for relocs against
57e7d118
AM
8773 symbols which turn out to need copy relocs or are not
8774 dynamic. */
529fe20e
AM
8775 if (h->dynamic_adjusted
8776 && !h->def_regular
8777 && !ELF_COMMON_DEF_P (h))
f0158f44 8778 {
46434633
AM
8779 /* Make sure this symbol is output as a dynamic symbol. */
8780 if (!ensure_undef_dynamic (info, h))
f0158f44 8781 return FALSE;
dfbb6ac9 8782
f0158f44
AM
8783 if (h->dynindx == -1)
8784 eh->dyn_relocs = NULL;
8785 }
8786 else
8a2058b5 8787 eh->dyn_relocs = NULL;
57e7d118
AM
8788 }
8789
8790 /* Finally, allocate space. */
8791 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8792 {
8793 asection *sreloc = elf_section_data (p->sec)->sreloc;
8794 if (eh->elf.type == STT_GNU_IFUNC)
8795 sreloc = htab->elf.irelplt;
8796 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 8797 }
65f38f15 8798 }
57e7d118 8799
2d7ad24e
AM
8800 /* We might need a PLT entry when the symbol
8801 a) is dynamic, or
8802 b) is an ifunc, or
8803 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
8804 d) has plt16 relocs and we are linking statically. */
8805 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
8806 || h->type == STT_GNU_IFUNC
8807 || (h->needs_plt && h->dynamic_adjusted)
8808 || (h->needs_plt
8809 && h->def_regular
8810 && !htab->elf.dynamic_sections_created
3e04d765 8811 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
8812 && (((struct ppc_link_hash_entry *) h)->tls_mask
8813 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 8814 {
57e7d118
AM
8815 struct plt_entry *pent;
8816 bfd_boolean doneone = FALSE;
8817 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8818 if (pent->plt.refcount > 0)
8819 {
8820 if (!htab->elf.dynamic_sections_created
8821 || h->dynindx == -1)
8822 {
2d7ad24e
AM
8823 if (h->type == STT_GNU_IFUNC)
8824 {
8825 s = htab->elf.iplt;
8826 pent->plt.offset = s->size;
8827 s->size += PLT_ENTRY_SIZE (htab);
8828 s = htab->elf.irelplt;
8829 }
8830 else
8831 {
8832 s = htab->pltlocal;
8833 pent->plt.offset = s->size;
8834 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
8835 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
8836 }
57e7d118
AM
8837 }
8838 else
8839 {
8840 /* If this is the first .plt entry, make room for the special
8841 first entry. */
8842 s = htab->elf.splt;
8843 if (s->size == 0)
8844 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 8845
57e7d118 8846 pent->plt.offset = s->size;
65f38f15 8847
57e7d118
AM
8848 /* Make room for this entry. */
8849 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 8850
57e7d118
AM
8851 /* Make room for the .glink code. */
8852 s = htab->glink;
8853 if (s->size == 0)
9e390558 8854 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
8855 if (htab->opd_abi)
8856 {
8857 /* We need bigger stubs past index 32767. */
9e390558 8858 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
8859 s->size += 4;
8860 s->size += 2*4;
8861 }
8862 else
8863 s->size += 4;
65f38f15 8864
57e7d118
AM
8865 /* We also need to make an entry in the .rela.plt section. */
8866 s = htab->elf.srelplt;
8867 }
2d7ad24e
AM
8868 if (s != NULL)
8869 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
8870 doneone = TRUE;
8871 }
8872 else
8873 pent->plt.offset = (bfd_vma) -1;
8874 if (!doneone)
8875 {
8876 h->plt.plist = NULL;
8877 h->needs_plt = 0;
8878 }
65f38f15 8879 }
57e7d118 8880 else
65f38f15 8881 {
57e7d118
AM
8882 h->plt.plist = NULL;
8883 h->needs_plt = 0;
65f38f15
AM
8884 }
8885
b34976b6 8886 return TRUE;
65f38f15
AM
8887}
8888
9e390558
AM
8889#define PPC_LO(v) ((v) & 0xffff)
8890#define PPC_HI(v) (((v) >> 16) & 0xffff)
8891#define PPC_HA(v) PPC_HI ((v) + 0x8000)
8892
a345bc8d
AM
8893/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
8894 to set up space for global entry stubs. These are put in glink,
8895 after the branch table. */
65f38f15 8896
b34976b6 8897static bfd_boolean
a345bc8d 8898size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 8899{
a345bc8d
AM
8900 struct bfd_link_info *info;
8901 struct ppc_link_hash_table *htab;
8902 struct plt_entry *pent;
9e390558 8903 asection *s, *plt;
65f38f15 8904
a345bc8d
AM
8905 if (h->root.type == bfd_link_hash_indirect)
8906 return TRUE;
65f38f15 8907
a345bc8d
AM
8908 if (!h->pointer_equality_needed)
8909 return TRUE;
65f38f15 8910
a345bc8d
AM
8911 if (h->def_regular)
8912 return TRUE;
65f38f15 8913
a345bc8d
AM
8914 info = inf;
8915 htab = ppc_hash_table (info);
8916 if (htab == NULL)
8917 return FALSE;
8918
9e390558
AM
8919 s = htab->global_entry;
8920 plt = htab->elf.splt;
a345bc8d
AM
8921 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8922 if (pent->plt.offset != (bfd_vma) -1
8923 && pent->addend == 0)
8924 {
afe397ea
AM
8925 /* For ELFv2, if this symbol is not defined in a regular file
8926 and we are not generating a shared library or pie, then we
8927 need to define the symbol in the executable on a call stub.
8928 This is to avoid text relocations. */
9e390558
AM
8929 bfd_vma off, stub_align, stub_off, stub_size;
8930 unsigned int align_power;
8931
8932 stub_size = 16;
8933 stub_off = s->size;
8934 if (htab->params->plt_stub_align >= 0)
8935 align_power = htab->params->plt_stub_align;
8936 else
8937 align_power = -htab->params->plt_stub_align;
8938 /* Setting section alignment is delayed until we know it is
8939 non-empty. Otherwise the .text output section will be
8940 aligned at least to plt_stub_align even when no global
8941 entry stubs are needed. */
8942 if (s->alignment_power < align_power)
8943 s->alignment_power = align_power;
8944 stub_align = (bfd_vma) 1 << align_power;
8945 if (htab->params->plt_stub_align >= 0
8946 || ((((stub_off + stub_size - 1) & -stub_align)
8947 - (stub_off & -stub_align))
8948 > ((stub_size - 1) & -stub_align)))
8949 stub_off = (stub_off + stub_align - 1) & -stub_align;
8950 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
8951 off -= stub_off + s->output_offset + s->output_section->vma;
8952 /* Note that for --plt-stub-align negative we have a possible
8953 dependency between stub offset and size. Break that
8954 dependency by assuming the max stub size when calculating
8955 the stub offset. */
8956 if (PPC_HA (off) == 0)
8957 stub_size -= 4;
8a2058b5 8958 h->root.type = bfd_link_hash_defined;
afe397ea 8959 h->root.u.def.section = s;
9e390558
AM
8960 h->root.u.def.value = stub_off;
8961 s->size = stub_off + stub_size;
a345bc8d
AM
8962 break;
8963 }
8964 return TRUE;
8965}
8966
8967/* Set DF_TEXTREL if we find any dynamic relocs that apply to
8968 read-only sections. */
8969
8970static bfd_boolean
98bbb1b8 8971maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 8972{
98bbb1b8
AM
8973 asection *sec;
8974
a345bc8d
AM
8975 if (h->root.type == bfd_link_hash_indirect)
8976 return TRUE;
8977
98bbb1b8
AM
8978 sec = readonly_dynrelocs (h);
8979 if (sec != NULL)
a345bc8d 8980 {
98bbb1b8
AM
8981 struct bfd_link_info *info = (struct bfd_link_info *) inf;
8982
8983 info->flags |= DF_TEXTREL;
2cdcc330
AM
8984 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
8985 " in read-only section `%pA'\n"),
8986 sec->owner, h->root.root.string, sec);
a345bc8d
AM
8987
8988 /* Not an error, just cut short the traversal. */
8989 return FALSE;
65f38f15 8990 }
b34976b6 8991 return TRUE;
65f38f15
AM
8992}
8993
8994/* Set the sizes of the dynamic sections. */
8995
b34976b6 8996static bfd_boolean
ee67d69a 8997ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 8998 struct bfd_link_info *info)
65f38f15
AM
8999{
9000 struct ppc_link_hash_table *htab;
9001 bfd *dynobj;
9002 asection *s;
b34976b6 9003 bfd_boolean relocs;
65f38f15 9004 bfd *ibfd;
7865406b 9005 struct got_entry *first_tlsld;
65f38f15
AM
9006
9007 htab = ppc_hash_table (info);
4dfe6ac6
NC
9008 if (htab == NULL)
9009 return FALSE;
9010
65f38f15
AM
9011 dynobj = htab->elf.dynobj;
9012 if (dynobj == NULL)
9013 abort ();
9014
9015 if (htab->elf.dynamic_sections_created)
9016 {
9017 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9018 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9019 {
3d4d4302 9020 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9021 if (s == NULL)
9022 abort ();
eea6121a 9023 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9024 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9025 }
9026 }
9027
9028 /* Set up .got offsets for local syms, and space for local dynamic
9029 relocs. */
c72f2fb2 9030 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9031 {
411e1bfb
AM
9032 struct got_entry **lgot_ents;
9033 struct got_entry **end_lgot_ents;
e054468f
AM
9034 struct plt_entry **local_plt;
9035 struct plt_entry **end_local_plt;
f961d9dd 9036 unsigned char *lgot_masks;
65f38f15
AM
9037 bfd_size_type locsymcount;
9038 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9039
0c8d6e5c 9040 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9041 continue;
9042
9043 for (s = ibfd->sections; s != NULL; s = s->next)
9044 {
19e08130 9045 struct ppc_dyn_relocs *p;
65f38f15 9046
6edfbbad 9047 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9048 {
ec338859
AM
9049 if (!bfd_is_abs_section (p->sec)
9050 && bfd_is_abs_section (p->sec->output_section))
9051 {
9052 /* Input section has been discarded, either because
9053 it is a copy of a linkonce section or due to
9054 linker script /DISCARD/, so we'll be discarding
9055 the relocs too. */
9056 }
248866a8 9057 else if (p->count != 0)
ec338859 9058 {
19e08130
AM
9059 asection *srel = elf_section_data (p->sec)->sreloc;
9060 if (p->ifunc)
33e44f2e 9061 srel = htab->elf.irelplt;
eea6121a 9062 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9063 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9064 info->flags |= DF_TEXTREL;
ec338859 9065 }
65f38f15
AM
9066 }
9067 }
9068
411e1bfb
AM
9069 lgot_ents = elf_local_got_ents (ibfd);
9070 if (!lgot_ents)
65f38f15
AM
9071 continue;
9072
0ffa91dd 9073 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9074 locsymcount = symtab_hdr->sh_info;
411e1bfb 9075 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9076 local_plt = (struct plt_entry **) end_lgot_ents;
9077 end_local_plt = local_plt + locsymcount;
f961d9dd 9078 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9079 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9080 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9081 {
0b8bcf0d 9082 struct got_entry **pent, *ent;
411e1bfb 9083
0b8bcf0d
AM
9084 pent = lgot_ents;
9085 while ((ent = *pent) != NULL)
411e1bfb
AM
9086 if (ent->got.refcount > 0)
9087 {
e7b938ca 9088 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9089 {
927be08e 9090 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9091 *pent = ent->next;
411e1bfb
AM
9092 }
9093 else
9094 {
19e08130
AM
9095 unsigned int ent_size = 8;
9096 unsigned int rel_size = sizeof (Elf64_External_Rela);
9097
eea6121a 9098 ent->got.offset = s->size;
e7b938ca 9099 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9100 {
19e08130
AM
9101 ent_size *= 2;
9102 rel_size *= 2;
9103 }
9104 s->size += ent_size;
37da22e5 9105 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 9106 {
33e44f2e 9107 htab->elf.irelplt->size += rel_size;
19e08130
AM
9108 htab->got_reli_size += rel_size;
9109 }
f15d0b54
AM
9110 else if (bfd_link_pic (info)
9111 && !((ent->tls_type & TLS_TPREL) != 0
9112 && bfd_link_executable (info)))
19e08130
AM
9113 {
9114 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9115 srel->size += rel_size;
927be08e 9116 }
0b8bcf0d 9117 pent = &ent->next;
411e1bfb
AM
9118 }
9119 }
9120 else
0b8bcf0d 9121 *pent = ent->next;
65f38f15 9122 }
e054468f 9123
2d7ad24e
AM
9124 /* Allocate space for plt calls to local syms. */
9125 lgot_masks = (unsigned char *) end_local_plt;
9126 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
9127 {
9128 struct plt_entry *ent;
9129
9130 for (ent = *local_plt; ent != NULL; ent = ent->next)
9131 if (ent->plt.refcount > 0)
9132 {
2d7ad24e
AM
9133 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9134 {
9135 s = htab->elf.iplt;
9136 ent->plt.offset = s->size;
9137 s->size += PLT_ENTRY_SIZE (htab);
9138 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9139 }
3e04d765
AM
9140 else if (htab->can_convert_all_inline_plt
9141 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
9142 ent->plt.offset = (bfd_vma) -1;
9143 else
9144 {
9145 s = htab->pltlocal;
9146 ent->plt.offset = s->size;
9147 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9148 if (bfd_link_pic (info))
9149 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9150 }
e054468f
AM
9151 }
9152 else
9153 ent->plt.offset = (bfd_vma) -1;
9154 }
65f38f15
AM
9155 }
9156
9157 /* Allocate global sym .plt and .got entries, and space for global
9158 sym dynamic relocs. */
4ce794b7 9159 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 9160
0e1862bb 9161 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 9162 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 9163
7865406b 9164 first_tlsld = NULL;
c72f2fb2 9165 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 9166 {
7865406b
AM
9167 struct got_entry *ent;
9168
0c8d6e5c 9169 if (!is_ppc64_elf (ibfd))
102890f0
AM
9170 continue;
9171
7865406b
AM
9172 ent = ppc64_tlsld_got (ibfd);
9173 if (ent->got.refcount > 0)
102890f0 9174 {
7865406b 9175 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9176 {
7865406b
AM
9177 ent->is_indirect = TRUE;
9178 ent->got.ent = first_tlsld;
9179 }
9180 else
9181 {
9182 if (first_tlsld == NULL)
9183 first_tlsld = ent;
9184 s = ppc64_elf_tdata (ibfd)->got;
9185 ent->got.offset = s->size;
9186 ent->owner = ibfd;
9187 s->size += 16;
0e1862bb 9188 if (bfd_link_pic (info))
7865406b
AM
9189 {
9190 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9191 srel->size += sizeof (Elf64_External_Rela);
9192 }
102890f0
AM
9193 }
9194 }
9195 else
7865406b 9196 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9197 }
9198
65f38f15
AM
9199 /* We now have determined the sizes of the various dynamic sections.
9200 Allocate memory for them. */
b34976b6 9201 relocs = FALSE;
65f38f15
AM
9202 for (s = dynobj->sections; s != NULL; s = s->next)
9203 {
9204 if ((s->flags & SEC_LINKER_CREATED) == 0)
9205 continue;
9206
4ce794b7 9207 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9208 /* These haven't been allocated yet; don't strip. */
9209 continue;
33e44f2e
AM
9210 else if (s == htab->elf.sgot
9211 || s == htab->elf.splt
9212 || s == htab->elf.iplt
2d7ad24e 9213 || s == htab->pltlocal
c456f082 9214 || s == htab->glink
9e390558 9215 || s == htab->global_entry
5474d94f
AM
9216 || s == htab->elf.sdynbss
9217 || s == htab->elf.sdynrelro)
65f38f15
AM
9218 {
9219 /* Strip this section if we don't need it; see the
9220 comment below. */
5bd4f169 9221 }
58d180e8
AM
9222 else if (s == htab->glink_eh_frame)
9223 {
9224 if (!bfd_is_abs_section (s->output_section))
9225 /* Not sized yet. */
9226 continue;
9227 }
70cc837d 9228 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9229 {
c456f082 9230 if (s->size != 0)
5bd4f169 9231 {
33e44f2e 9232 if (s != htab->elf.srelplt)
b34976b6 9233 relocs = TRUE;
5bd4f169
AM
9234
9235 /* We use the reloc_count field as a counter if we need
9236 to copy relocs into the output file. */
9237 s->reloc_count = 0;
9238 }
9239 }
65f38f15 9240 else
5bd4f169
AM
9241 {
9242 /* It's not one of our sections, so don't allocate space. */
9243 continue;
9244 }
9245
eea6121a 9246 if (s->size == 0)
5bd4f169 9247 {
c456f082
AM
9248 /* If we don't need this section, strip it from the
9249 output file. This is mostly to handle .rela.bss and
9250 .rela.plt. We must create both sections in
9251 create_dynamic_sections, because they must be created
9252 before the linker maps input sections to output
9253 sections. The linker does that before
9254 adjust_dynamic_symbol is called, and it is that
9255 function which decides whether anything needs to go
9256 into these sections. */
8423293d 9257 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9258 continue;
9259 }
9260
06bcf541
AM
9261 if (bfd_is_abs_section (s->output_section))
9262 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9263 s->name);
9264
c456f082 9265 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9266 continue;
9267
65f38f15
AM
9268 /* Allocate memory for the section contents. We use bfd_zalloc
9269 here in case unused entries are not reclaimed before the
9270 section's contents are written out. This should not happen,
411e1bfb
AM
9271 but this way if it does we get a R_PPC64_NONE reloc in .rela
9272 sections instead of garbage.
9273 We also rely on the section contents being zero when writing
5474d94f 9274 the GOT and .dynrelro. */
eea6121a 9275 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9276 if (s->contents == NULL)
b34976b6 9277 return FALSE;
5bd4f169
AM
9278 }
9279
c72f2fb2 9280 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 9281 {
0c8d6e5c 9282 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9283 continue;
9284
e717da7e 9285 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 9286 if (s != NULL && s != htab->elf.sgot)
e717da7e 9287 {
eea6121a 9288 if (s->size == 0)
8423293d 9289 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9290 else
9291 {
eea6121a 9292 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9293 if (s->contents == NULL)
9294 return FALSE;
9295 }
9296 }
9297 s = ppc64_elf_tdata (ibfd)->relgot;
9298 if (s != NULL)
9299 {
eea6121a 9300 if (s->size == 0)
8423293d 9301 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9302 else
9303 {
eea6121a 9304 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9305 if (s->contents == NULL)
9306 return FALSE;
9307 relocs = TRUE;
9308 s->reloc_count = 0;
9309 }
9310 }
9311 }
9312
e86ce104 9313 if (htab->elf.dynamic_sections_created)
5bd4f169 9314 {
e8910a83
AM
9315 bfd_boolean tls_opt;
9316
5bd4f169
AM
9317 /* Add some entries to the .dynamic section. We fill in the
9318 values later, in ppc64_elf_finish_dynamic_sections, but we
9319 must add the entries now so that we get the correct size for
9320 the .dynamic section. The DT_DEBUG entry is filled in by the
9321 dynamic linker and used by the debugger. */
dc810e39 9322#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9323 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9324
0e1862bb 9325 if (bfd_link_executable (info))
5bd4f169 9326 {
dc810e39 9327 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9328 return FALSE;
5bd4f169
AM
9329 }
9330
33e44f2e 9331 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 9332 {
dc810e39
AM
9333 if (!add_dynamic_entry (DT_PLTGOT, 0)
9334 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9335 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9336 || !add_dynamic_entry (DT_JMPREL, 0)
9337 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9338 return FALSE;
5bd4f169
AM
9339 }
9340
ee67d69a 9341 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
9342 {
9343 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9344 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9345 return FALSE;
19397422
AM
9346 }
9347
7c9cf415 9348 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
9349 && htab->tls_get_addr_fd != NULL
9350 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9351 if (tls_opt || !htab->opd_abi)
9352 {
9353 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9354 return FALSE;
9355 }
a7f2871e 9356
5bd4f169
AM
9357 if (relocs)
9358 {
dc810e39
AM
9359 if (!add_dynamic_entry (DT_RELA, 0)
9360 || !add_dynamic_entry (DT_RELASZ, 0)
9361 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9362 return FALSE;
5bd4f169 9363
65f38f15
AM
9364 /* If any dynamic relocs apply to a read-only section,
9365 then we need a DT_TEXTREL entry. */
248866a8 9366 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 9367 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 9368
65f38f15 9369 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9370 {
65f38f15 9371 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9372 return FALSE;
5bd4f169 9373 }
5bd4f169 9374 }
5bd4f169 9375 }
65f38f15 9376#undef add_dynamic_entry
5bd4f169 9377
b34976b6 9378 return TRUE;
5bd4f169
AM
9379}
9380
a345bc8d
AM
9381/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9382
9383static bfd_boolean
9384ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9385{
9386 if (h->plt.plist != NULL
9387 && !h->def_regular
9388 && !h->pointer_equality_needed)
9389 return FALSE;
9390
9391 return _bfd_elf_hash_symbol (h);
9392}
9393
721956f4 9394/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9395
4ce794b7
AM
9396static inline enum ppc_stub_type
9397ppc_type_of_stub (asection *input_sec,
9398 const Elf_Internal_Rela *rel,
9399 struct ppc_link_hash_entry **hash,
e054468f 9400 struct plt_entry **plt_ent,
6911b7dc
AM
9401 bfd_vma destination,
9402 unsigned long local_off)
5bd4f169 9403{
721956f4
AM
9404 struct ppc_link_hash_entry *h = *hash;
9405 bfd_vma location;
9406 bfd_vma branch_offset;
9407 bfd_vma max_branch_offset;
4ce794b7 9408 enum elf_ppc64_reloc_type r_type;
5bd4f169 9409
721956f4
AM
9410 if (h != NULL)
9411 {
e054468f 9412 struct plt_entry *ent;
7fe2b9a6 9413 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9414 if (h->oh != NULL
9415 && h->oh->is_func_descriptor)
7b8f6675
AM
9416 {
9417 fdh = ppc_follow_link (h->oh);
9418 *hash = fdh;
9419 }
8387904d 9420
e054468f
AM
9421 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9422 if (ent->addend == rel->r_addend
9423 && ent->plt.offset != (bfd_vma) -1)
9424 {
e054468f
AM
9425 *plt_ent = ent;
9426 return ppc_stub_plt_call;
9427 }
5bd4f169 9428
7fe2b9a6
AM
9429 /* Here, we know we don't have a plt entry. If we don't have a
9430 either a defined function descriptor or a defined entry symbol
9431 in a regular object file, then it is pointless trying to make
9432 any other type of stub. */
854b41e7
AM
9433 if (!is_static_defined (&fdh->elf)
9434 && !is_static_defined (&h->elf))
721956f4 9435 return ppc_stub_none;
5d1634d7 9436 }
e054468f
AM
9437 else if (elf_local_got_ents (input_sec->owner) != NULL)
9438 {
9439 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9440 struct plt_entry **local_plt = (struct plt_entry **)
9441 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9442 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9443
9444 if (local_plt[r_symndx] != NULL)
9445 {
9446 struct plt_entry *ent;
9447
9448 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9449 if (ent->addend == rel->r_addend
9450 && ent->plt.offset != (bfd_vma) -1)
9451 {
9452 *plt_ent = ent;
9453 return ppc_stub_plt_call;
9454 }
9455 }
9456 }
5d1634d7 9457
721956f4
AM
9458 /* Determine where the call point is. */
9459 location = (input_sec->output_offset
9460 + input_sec->output_section->vma
9461 + rel->r_offset);
5d1634d7 9462
721956f4
AM
9463 branch_offset = destination - location;
9464 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9465
721956f4
AM
9466 /* Determine if a long branch stub is needed. */
9467 max_branch_offset = 1 << 25;
23cedd1d
AM
9468 if (r_type == R_PPC64_REL14
9469 || r_type == R_PPC64_REL14_BRTAKEN
9470 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 9471 max_branch_offset = 1 << 15;
5d1634d7 9472
6911b7dc 9473 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
9474 /* We need a stub. Figure out whether a long_branch or plt_branch
9475 is needed later. */
9476 return ppc_stub_long_branch;
5d1634d7 9477
721956f4 9478 return ppc_stub_none;
5d1634d7
AM
9479}
9480
05d0e962
AM
9481/* Builds a 64-bit offset in r12 then adds it to r11 (LOAD false) or
9482 loads r12 from r11+r12 (LOAD true).
9483 . lis %r12,xxx-1b@highest
9484 . ori %r12,xxx-1b@higher
9485 . sldi %r12,%r12,32
9486 . oris %r12,%r12,xxx-1b@hi
9487 . ori %r12,%r12,xxx-1b@l
9488 . add %r12,%r11,%r12 */
9489
9490static bfd_byte *
9491build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
9492{
9493 if (off + 0x8000 < 0x10000)
9494 {
9495 if (load)
9496 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
9497 else
9498 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
9499 p += 4;
9500 }
9501 else if (off + 0x80008000ULL < 0x100000000ULL)
9502 {
9503 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
9504 p += 4;
9505 if (load)
9506 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
9507 else
9508 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
9509 p += 4;
9510 }
9511 else
9512 {
9513 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9514 {
9515 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
9516 p += 4;
9517 }
9518 else
9519 {
9520 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
9521 p += 4;
9522 if (((off >> 32) & 0xffff) != 0)
9523 {
9524 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
9525 p += 4;
9526 }
9527 }
9528 if (((off >> 32) & 0xffffffffULL) != 0)
9529 {
9530 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
9531 p += 4;
9532 }
9533 if (PPC_HI (off) != 0)
9534 {
9535 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
9536 p += 4;
9537 }
9538 if (PPC_LO (off) != 0)
9539 {
9540 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
9541 p += 4;
9542 }
9543 if (load)
9544 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
9545 else
9546 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
9547 p += 4;
9548 }
9549 return p;
9550}
9551
9552static unsigned int
9553size_offset (bfd_vma off)
9554{
9555 unsigned int size;
9556 if (off + 0x8000 < 0x10000)
9557 size = 4;
9558 else if (off + 0x80008000ULL < 0x100000000ULL)
9559 size = 8;
9560 else
9561 {
9562 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9563 size = 4;
9564 else
9565 {
9566 size = 4;
9567 if (((off >> 32) & 0xffff) != 0)
9568 size += 4;
9569 }
9570 if (((off >> 32) & 0xffffffffULL) != 0)
9571 size += 4;
9572 if (PPC_HI (off) != 0)
9573 size += 4;
9574 if (PPC_LO (off) != 0)
9575 size += 4;
9576 size += 4;
9577 }
9578 return size;
9579}
9580
df136d64
AM
9581/* Emit .eh_frame opcode to advance pc by DELTA. */
9582
9583static bfd_byte *
9584eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
9585{
9586 delta /= 4;
9587 if (delta < 64)
9588 *eh++ = DW_CFA_advance_loc + delta;
9589 else if (delta < 256)
9590 {
9591 *eh++ = DW_CFA_advance_loc1;
9592 *eh++ = delta;
9593 }
9594 else if (delta < 65536)
9595 {
9596 *eh++ = DW_CFA_advance_loc2;
9597 bfd_put_16 (abfd, delta, eh);
9598 eh += 2;
9599 }
9600 else
9601 {
9602 *eh++ = DW_CFA_advance_loc4;
9603 bfd_put_32 (abfd, delta, eh);
9604 eh += 4;
9605 }
9606 return eh;
9607}
9608
9609/* Size of required .eh_frame opcode to advance pc by DELTA. */
9610
9611static unsigned int
9612eh_advance_size (unsigned int delta)
9613{
9614 if (delta < 64 * 4)
9615 /* DW_CFA_advance_loc+[1..63]. */
9616 return 1;
9617 if (delta < 256 * 4)
9618 /* DW_CFA_advance_loc1, byte. */
9619 return 2;
9620 if (delta < 65536 * 4)
9621 /* DW_CFA_advance_loc2, 2 bytes. */
9622 return 3;
9623 /* DW_CFA_advance_loc4, 4 bytes. */
9624 return 5;
9625}
9626
794e51c0
AM
9627/* With power7 weakly ordered memory model, it is possible for ld.so
9628 to update a plt entry in one thread and have another thread see a
9629 stale zero toc entry. To avoid this we need some sort of acquire
9630 barrier in the call stub. One solution is to make the load of the
9631 toc word seem to appear to depend on the load of the function entry
9632 word. Another solution is to test for r2 being zero, and branch to
9633 the appropriate glink entry if so.
9634
9635 . fake dep barrier compare
71a39c98
AM
9636 . ld 12,xxx(2) ld 12,xxx(2)
9637 . mtctr 12 mtctr 12
9638 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
9639 . add 2,2,11 cmpldi 2,0
9640 . ld 2,xxx+8(2) bnectr+
9641 . bctr b <glink_entry>
9642
9643 The solution involving the compare turns out to be faster, so
9644 that's what we use unless the branch won't reach. */
9645
9646#define ALWAYS_USE_FAKE_DEP 0
9647#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 9648
794e51c0
AM
9649static inline unsigned int
9650plt_stub_size (struct ppc_link_hash_table *htab,
9651 struct ppc_stub_hash_entry *stub_entry,
9652 bfd_vma off)
9653{
05d0e962 9654 unsigned size;
b9e5796b 9655
05d0e962
AM
9656 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
9657 {
9658 size = 24 + size_offset (off);
9659 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
9660 size += 4;
9661 return size;
9662 }
9663
9664 size = 12;
b9e5796b
AM
9665 if (ALWAYS_EMIT_R2SAVE
9666 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9667 size += 4;
9668 if (PPC_HA (off) != 0)
794e51c0 9669 size += 4;
b9e5796b
AM
9670 if (htab->opd_abi)
9671 {
9672 size += 4;
e7d1c40c 9673 if (htab->params->plt_static_chain)
b9e5796b 9674 size += 4;
bd4d2eaa
AM
9675 if (htab->params->plt_thread_safe
9676 && htab->elf.dynamic_sections_created
9677 && stub_entry->h != NULL
9678 && stub_entry->h->elf.dynindx != -1)
b9e5796b 9679 size += 8;
e7d1c40c 9680 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
9681 size += 4;
9682 }
794e51c0
AM
9683 if (stub_entry->h != NULL
9684 && (stub_entry->h == htab->tls_get_addr_fd
9685 || stub_entry->h == htab->tls_get_addr)
7c9cf415 9686 && htab->params->tls_get_addr_opt)
f378ab09
AM
9687 {
9688 size += 7 * 4;
e81b4c93 9689 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 9690 size += 6 * 4;
f378ab09 9691 }
794e51c0
AM
9692 return size;
9693}
9694
2420fff6
AM
9695/* Depending on the sign of plt_stub_align:
9696 If positive, return the padding to align to a 2**plt_stub_align
9697 boundary.
9698 If negative, if this stub would cross fewer 2**plt_stub_align
9699 boundaries if we align, then return the padding needed to do so. */
9700
794e51c0
AM
9701static inline unsigned int
9702plt_stub_pad (struct ppc_link_hash_table *htab,
9703 struct ppc_stub_hash_entry *stub_entry,
9704 bfd_vma plt_off)
9705{
2420fff6 9706 int stub_align;
1aa42141 9707 unsigned stub_size;
6f20ed8a 9708 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 9709
2420fff6
AM
9710 if (htab->params->plt_stub_align >= 0)
9711 {
9712 stub_align = 1 << htab->params->plt_stub_align;
9713 if ((stub_off & (stub_align - 1)) != 0)
9714 return stub_align - (stub_off & (stub_align - 1));
9715 return 0;
9716 }
9717
9718 stub_align = 1 << -htab->params->plt_stub_align;
1aa42141 9719 stub_size = plt_stub_size (htab, stub_entry, plt_off);
794e51c0 9720 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 9721 > ((stub_size - 1) & -stub_align))
794e51c0
AM
9722 return stub_align - (stub_off & (stub_align - 1));
9723 return 0;
9724}
9725
9726/* Build a .plt call stub. */
9727
9728static inline bfd_byte *
9729build_plt_stub (struct ppc_link_hash_table *htab,
9730 struct ppc_stub_hash_entry *stub_entry,
9731 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9732{
e7d1c40c 9733 bfd *obfd = htab->params->stub_bfd;
b9e5796b 9734 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 9735 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
9736 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
9737 && htab->elf.dynamic_sections_created
9738 && stub_entry->h != NULL
9739 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
9740 bfd_boolean use_fake_dep = plt_thread_safe;
9741 bfd_vma cmp_branch_off = 0;
9742
9743 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 9744 && plt_load_toc
794e51c0 9745 && plt_thread_safe
bd4d2eaa
AM
9746 && !((stub_entry->h == htab->tls_get_addr_fd
9747 || stub_entry->h == htab->tls_get_addr)
7c9cf415 9748 && htab->params->tls_get_addr_opt))
794e51c0
AM
9749 {
9750 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
9751 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
9752 / PLT_ENTRY_SIZE (htab));
9e390558 9753 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
9754 bfd_vma to, from;
9755
68d62958
AM
9756 if (pltindex > 32768)
9757 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
9758 to = (glinkoff
9759 + htab->glink->output_offset
9760 + htab->glink->output_section->vma);
6f20ed8a 9761 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
9762 + 4 * (ALWAYS_EMIT_R2SAVE
9763 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9764 + 4 * (PPC_HA (offset) != 0)
9765 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
9766 != PPC_HA (offset))
9767 + 4 * (plt_static_chain != 0)
9768 + 20
6f20ed8a
AM
9769 + stub_entry->group->stub_sec->output_offset
9770 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
9771 cmp_branch_off = to - from;
9772 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
9773 }
9774
ac2df442
AM
9775 if (PPC_HA (offset) != 0)
9776 {
176a0d42
AM
9777 if (r != NULL)
9778 {
794e51c0
AM
9779 if (ALWAYS_EMIT_R2SAVE
9780 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9781 r[0].r_offset += 4;
176a0d42 9782 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 9783 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
9784 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9785 r[1].r_addend = r[0].r_addend;
b9e5796b 9786 if (plt_load_toc)
176a0d42 9787 {
b9e5796b 9788 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 9789 {
b9e5796b
AM
9790 r[2].r_offset = r[1].r_offset + 4;
9791 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9792 r[2].r_addend = r[0].r_addend;
9793 }
9794 else
9795 {
9796 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
9797 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9798 r[2].r_addend = r[0].r_addend + 8;
9799 if (plt_static_chain)
9800 {
9801 r[3].r_offset = r[2].r_offset + 4;
9802 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9803 r[3].r_addend = r[0].r_addend + 16;
9804 }
c7131b65 9805 }
176a0d42
AM
9806 }
9807 }
794e51c0
AM
9808 if (ALWAYS_EMIT_R2SAVE
9809 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 9810 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
9811 if (plt_load_toc)
9812 {
9813 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
9814 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
9815 }
9816 else
9817 {
9818 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9819 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
9820 }
b9e5796b
AM
9821 if (plt_load_toc
9822 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 9823 {
71a39c98 9824 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
9825 offset = 0;
9826 }
71a39c98 9827 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 9828 if (plt_load_toc)
794e51c0 9829 {
b9e5796b
AM
9830 if (use_fake_dep)
9831 {
9832 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
9833 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
9834 }
9835 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
9836 if (plt_static_chain)
9837 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 9838 }
ac2df442
AM
9839 }
9840 else
9841 {
176a0d42
AM
9842 if (r != NULL)
9843 {
794e51c0
AM
9844 if (ALWAYS_EMIT_R2SAVE
9845 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9846 r[0].r_offset += 4;
176a0d42 9847 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 9848 if (plt_load_toc)
176a0d42 9849 {
b9e5796b 9850 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 9851 {
b9e5796b
AM
9852 r[1].r_offset = r[0].r_offset + 4;
9853 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9854 r[1].r_addend = r[0].r_addend;
9855 }
9856 else
9857 {
9858 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
9859 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9860 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9861 if (plt_static_chain)
9862 {
9863 r[2].r_offset = r[1].r_offset + 4;
9864 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9865 r[2].r_addend = r[0].r_addend + 8;
9866 }
c7131b65 9867 }
176a0d42
AM
9868 }
9869 }
794e51c0
AM
9870 if (ALWAYS_EMIT_R2SAVE
9871 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 9872 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 9873 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
9874 if (plt_load_toc
9875 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
9876 {
9877 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9878 offset = 0;
9879 }
71a39c98 9880 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 9881 if (plt_load_toc)
794e51c0 9882 {
b9e5796b
AM
9883 if (use_fake_dep)
9884 {
9885 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
9886 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
9887 }
9888 if (plt_static_chain)
9889 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9890 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 9891 }
ac2df442 9892 }
b9e5796b 9893 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
9894 {
9895 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
9896 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 9897 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
9898 }
9899 else
407aa07c 9900 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
9901 return p;
9902}
9903
a7f2871e
AM
9904/* Build a special .plt call stub for __tls_get_addr. */
9905
9906#define LD_R11_0R3 0xe9630000
9907#define LD_R12_0R3 0xe9830000
9908#define MR_R0_R3 0x7c601b78
9909#define CMPDI_R11_0 0x2c2b0000
9910#define ADD_R3_R12_R13 0x7c6c6a14
9911#define BEQLR 0x4d820020
9912#define MR_R3_R0 0x7c030378
a7f2871e
AM
9913#define STD_R11_0R1 0xf9610000
9914#define BCTRL 0x4e800421
9915#define LD_R11_0R1 0xe9610000
a7f2871e
AM
9916#define MTLR_R11 0x7d6803a6
9917
9918static inline bfd_byte *
794e51c0
AM
9919build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
9920 struct ppc_stub_hash_entry *stub_entry,
9921 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 9922{
e7d1c40c 9923 bfd *obfd = htab->params->stub_bfd;
df136d64 9924 bfd_byte *loc = p;
794e51c0 9925
a7f2871e
AM
9926 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9927 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9928 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9929 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9930 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9931 bfd_put_32 (obfd, BEQLR, p), p += 4;
9932 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
9933 if (r != NULL)
9934 r[0].r_offset += 7 * 4;
e81b4c93 9935 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
f378ab09
AM
9936 return build_plt_stub (htab, stub_entry, p, offset, r);
9937
a7f2871e 9938 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 9939 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
9940
9941 if (r != NULL)
f378ab09 9942 r[0].r_offset += 2 * 4;
794e51c0 9943 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 9944 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 9945
a078d95a 9946 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 9947 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
9948 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9949 bfd_put_32 (obfd, BLR, p), p += 4;
9950
df136d64
AM
9951 if (htab->glink_eh_frame != NULL
9952 && htab->glink_eh_frame->size != 0)
9953 {
9954 bfd_byte *base, *eh;
9955 unsigned int lr_used, delta;
9956
9957 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
9958 eh = base + stub_entry->group->eh_size;
9959 lr_used = stub_entry->stub_offset + (p - 20 - loc);
9960 delta = lr_used - stub_entry->group->lr_restore;
9961 stub_entry->group->lr_restore = lr_used + 16;
9962 eh = eh_advance (htab->elf.dynobj, eh, delta);
9963 *eh++ = DW_CFA_offset_extended_sf;
9964 *eh++ = 65;
9965 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
9966 *eh++ = DW_CFA_advance_loc + 4;
9967 *eh++ = DW_CFA_restore_extended;
9968 *eh++ = 65;
9969 stub_entry->group->eh_size = eh - base;
9970 }
a7f2871e
AM
9971 return p;
9972}
9973
176a0d42
AM
9974static Elf_Internal_Rela *
9975get_relocs (asection *sec, int count)
9976{
9977 Elf_Internal_Rela *relocs;
9978 struct bfd_elf_section_data *elfsec_data;
9979
9980 elfsec_data = elf_section_data (sec);
9981 relocs = elfsec_data->relocs;
9982 if (relocs == NULL)
9983 {
9984 bfd_size_type relsize;
9985 relsize = sec->reloc_count * sizeof (*relocs);
9986 relocs = bfd_alloc (sec->owner, relsize);
9987 if (relocs == NULL)
9988 return NULL;
9989 elfsec_data->relocs = relocs;
d4730f92
BS
9990 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9991 sizeof (Elf_Internal_Shdr));
9992 if (elfsec_data->rela.hdr == NULL)
9993 return NULL;
9994 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9995 * sizeof (Elf64_External_Rela));
9996 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
9997 sec->reloc_count = 0;
9998 }
9999 relocs += sec->reloc_count;
10000 sec->reloc_count += count;
10001 return relocs;
10002}
10003
aa374f67 10004static bfd_vma
25f53a85 10005get_r2off (struct bfd_link_info *info,
aa374f67
AM
10006 struct ppc_stub_hash_entry *stub_entry)
10007{
25f53a85 10008 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10009 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10010
10011 if (r2off == 0)
10012 {
10013 /* Support linking -R objects. Get the toc pointer from the
10014 opd entry. */
10015 char buf[8];
b9e5796b
AM
10016 if (!htab->opd_abi)
10017 return r2off;
aa374f67
AM
10018 asection *opd = stub_entry->h->elf.root.u.def.section;
10019 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10020
10021 if (strcmp (opd->name, ".opd") != 0
10022 || opd->reloc_count != 0)
10023 {
2cdcc330
AM
10024 info->callbacks->einfo
10025 (_("%P: cannot find opd entry toc for `%pT'\n"),
10026 stub_entry->h->elf.root.root.string);
aa374f67 10027 bfd_set_error (bfd_error_bad_value);
a7c49797 10028 return (bfd_vma) -1;
aa374f67
AM
10029 }
10030 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10031 return (bfd_vma) -1;
aa374f67 10032 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10033 r2off -= elf_gp (info->output_bfd);
aa374f67 10034 }
6f20ed8a 10035 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10036 return r2off;
10037}
10038
b34976b6 10039static bfd_boolean
4ce794b7 10040ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10041{
721956f4
AM
10042 struct ppc_stub_hash_entry *stub_entry;
10043 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10044 struct bfd_link_info *info;
10045 struct ppc_link_hash_table *htab;
721956f4
AM
10046 bfd_byte *loc;
10047 bfd_byte *p;
1aa42141 10048 bfd_vma targ, off;
176a0d42 10049 Elf_Internal_Rela *r;
e054468f 10050 asection *plt;
5d1634d7 10051
721956f4
AM
10052 /* Massage our args to the form they really have. */
10053 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10054 info = in_arg;
5d1634d7 10055
5d1634d7 10056 htab = ppc_hash_table (info);
4dfe6ac6
NC
10057 if (htab == NULL)
10058 return FALSE;
5d1634d7 10059
1aa42141 10060 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 10061 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10062
4ce794b7 10063 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10064 switch (stub_entry->stub_type)
5d1634d7 10065 {
721956f4 10066 case ppc_stub_long_branch:
ad8e1ba5 10067 case ppc_stub_long_branch_r2off:
721956f4 10068 /* Branches are relative. This is where we are going to. */
1aa42141 10069 targ = (stub_entry->target_value
6911b7dc
AM
10070 + stub_entry->target_section->output_offset
10071 + stub_entry->target_section->output_section->vma);
1aa42141 10072 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 10073
721956f4 10074 /* And this is where we are coming from. */
1aa42141
AM
10075 off = (stub_entry->stub_offset
10076 + stub_entry->group->stub_sec->output_offset
10077 + stub_entry->group->stub_sec->output_section->vma);
10078 off = targ - off;
e86ce104 10079
9e390558 10080 p = loc;
ac2df442 10081 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10082 {
25f53a85 10083 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10084
a7c49797 10085 if (r2off == (bfd_vma) -1)
aa374f67
AM
10086 {
10087 htab->stub_error = TRUE;
10088 return FALSE;
10089 }
9e390558
AM
10090 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10091 p += 4;
ac2df442
AM
10092 if (PPC_HA (r2off) != 0)
10093 {
e7d1c40c 10094 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10095 ADDIS_R2_R2 | PPC_HA (r2off), p);
10096 p += 4;
a7c49797
AM
10097 }
10098 if (PPC_LO (r2off) != 0)
10099 {
10100 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10101 ADDI_R2_R2 | PPC_LO (r2off), p);
10102 p += 4;
ac2df442 10103 }
9e390558 10104 off -= p - loc;
ad8e1ba5 10105 }
9e390558
AM
10106 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10107 p += 4;
ad8e1ba5 10108
5c3dead3
AM
10109 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10110 {
cf97bcb0
AM
10111 _bfd_error_handler
10112 (_("long branch stub `%s' offset overflow"),
bc30df16 10113 stub_entry->root.string);
5c3dead3
AM
10114 htab->stub_error = TRUE;
10115 return FALSE;
10116 }
ee75fd95
AM
10117
10118 if (info->emitrelocations)
10119 {
6f20ed8a 10120 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10121 if (r == NULL)
10122 return FALSE;
9e390558 10123 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 10124 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 10125 r->r_addend = targ;
ee75fd95
AM
10126 if (stub_entry->h != NULL)
10127 {
10128 struct elf_link_hash_entry **hashes;
10129 unsigned long symndx;
10130 struct ppc_link_hash_entry *h;
10131
e7d1c40c 10132 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10133 if (hashes == NULL)
10134 {
10135 bfd_size_type hsize;
10136
10137 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10138 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10139 if (hashes == NULL)
10140 return FALSE;
e7d1c40c 10141 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10142 htab->stub_globals = 1;
10143 }
10144 symndx = htab->stub_globals++;
10145 h = stub_entry->h;
10146 hashes[symndx] = &h->elf;
10147 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10148 if (h->oh != NULL && h->oh->is_func)
b31867b6 10149 h = ppc_follow_link (h->oh);
ee75fd95
AM
10150 if (h->elf.root.u.def.section != stub_entry->target_section)
10151 /* H is an opd symbol. The addend must be zero. */
10152 r->r_addend = 0;
10153 else
10154 {
10155 off = (h->elf.root.u.def.value
10156 + h->elf.root.u.def.section->output_offset
10157 + h->elf.root.u.def.section->output_section->vma);
10158 r->r_addend -= off;
10159 }
10160 }
10161 }
721956f4 10162 break;
e86ce104 10163
721956f4 10164 case ppc_stub_plt_branch:
ad8e1ba5 10165 case ppc_stub_plt_branch_r2off:
721956f4
AM
10166 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10167 stub_entry->root.string + 9,
b34976b6 10168 FALSE, FALSE);
721956f4
AM
10169 if (br_entry == NULL)
10170 {
cf97bcb0
AM
10171 _bfd_error_handler (_("can't find branch stub `%s'"),
10172 stub_entry->root.string);
b34976b6
AM
10173 htab->stub_error = TRUE;
10174 return FALSE;
721956f4
AM
10175 }
10176
1aa42141 10177 targ = (stub_entry->target_value
176a0d42
AM
10178 + stub_entry->target_section->output_offset
10179 + stub_entry->target_section->output_section->vma);
6911b7dc 10180 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 10181 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10182
1aa42141 10183 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 10184 htab->brlt->contents + br_entry->offset);
721956f4 10185
f94498ff 10186 if (br_entry->iter == htab->stub_iteration)
721956f4 10187 {
f94498ff 10188 br_entry->iter = 0;
84f5d08e 10189
f94498ff 10190 if (htab->relbrlt != NULL)
84f5d08e 10191 {
f94498ff
AM
10192 /* Create a reloc for the branch lookup table entry. */
10193 Elf_Internal_Rela rela;
10194 bfd_byte *rl;
10195
10196 rela.r_offset = (br_entry->offset
10197 + htab->brlt->output_offset
10198 + htab->brlt->output_section->vma);
10199 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 10200 rela.r_addend = targ;
f94498ff
AM
10201
10202 rl = htab->relbrlt->contents;
10203 rl += (htab->relbrlt->reloc_count++
10204 * sizeof (Elf64_External_Rela));
10205 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10206 }
10207 else if (info->emitrelocations)
10208 {
176a0d42
AM
10209 r = get_relocs (htab->brlt, 1);
10210 if (r == NULL)
10211 return FALSE;
10212 /* brlt, being SEC_LINKER_CREATED does not go through the
10213 normal reloc processing. Symbols and offsets are not
10214 translated from input file to output file form, so
10215 set up the offset per the output file. */
f94498ff
AM
10216 r->r_offset = (br_entry->offset
10217 + htab->brlt->output_offset
10218 + htab->brlt->output_section->vma);
10219 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 10220 r->r_addend = targ;
84f5d08e 10221 }
84f5d08e 10222 }
721956f4 10223
1aa42141 10224 targ = (br_entry->offset
176a0d42
AM
10225 + htab->brlt->output_offset
10226 + htab->brlt->output_section->vma);
10227
1aa42141
AM
10228 off = (elf_gp (info->output_bfd)
10229 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10230 off = targ - off;
721956f4 10231
ad8e1ba5 10232 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10233 {
25f53a85 10234 info->callbacks->einfo
c1c8c1ef 10235 (_("%P: linkage table error against `%pT'\n"),
721956f4 10236 stub_entry->root.string);
5d1634d7 10237 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10238 htab->stub_error = TRUE;
10239 return FALSE;
5d1634d7 10240 }
41bd81ab 10241
176a0d42
AM
10242 if (info->emitrelocations)
10243 {
6f20ed8a 10244 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
10245 if (r == NULL)
10246 return FALSE;
6f20ed8a 10247 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
10248 if (bfd_big_endian (info->output_bfd))
10249 r[0].r_offset += 2;
00f412ee 10250 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
10251 r[0].r_offset += 4;
10252 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 10253 r[0].r_addend = targ;
176a0d42
AM
10254 if (PPC_HA (off) != 0)
10255 {
10256 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10257 r[1].r_offset = r[0].r_offset + 4;
10258 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10259 r[1].r_addend = r[0].r_addend;
10260 }
10261 }
10262
9e390558 10263 p = loc;
00f412ee 10264 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 10265 {
176a0d42 10266 if (PPC_HA (off) != 0)
ac2df442 10267 {
e7d1c40c 10268 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10269 ADDIS_R12_R2 | PPC_HA (off), p);
10270 p += 4;
e7d1c40c 10271 bfd_put_32 (htab->params->stub_bfd,
9e390558 10272 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
10273 }
10274 else
9e390558
AM
10275 bfd_put_32 (htab->params->stub_bfd,
10276 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
10277 }
10278 else
10279 {
25f53a85 10280 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 10281
a7c49797 10282 if (r2off == (bfd_vma) -1)
aa374f67
AM
10283 {
10284 htab->stub_error = TRUE;
10285 return FALSE;
10286 }
ad8e1ba5 10287
9e390558
AM
10288 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10289 p += 4;
176a0d42 10290 if (PPC_HA (off) != 0)
ac2df442 10291 {
e7d1c40c 10292 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10293 ADDIS_R12_R2 | PPC_HA (off), p);
10294 p += 4;
e7d1c40c 10295 bfd_put_32 (htab->params->stub_bfd,
9e390558 10296 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
10297 }
10298 else
9e390558 10299 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
10300
10301 if (PPC_HA (r2off) != 0)
10302 {
9e390558 10303 p += 4;
e7d1c40c 10304 bfd_put_32 (htab->params->stub_bfd,
9e390558 10305 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
10306 }
10307 if (PPC_LO (r2off) != 0)
10308 {
9e390558 10309 p += 4;
e7d1c40c 10310 bfd_put_32 (htab->params->stub_bfd,
9e390558 10311 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 10312 }
ad8e1ba5 10313 }
9e390558
AM
10314 p += 4;
10315 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10316 p += 4;
407aa07c
AM
10317 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10318 p += 4;
721956f4 10319 break;
5d1634d7 10320
05d0e962
AM
10321 case ppc_stub_long_branch_notoc:
10322 case ppc_stub_long_branch_both:
10323 case ppc_stub_plt_branch_notoc:
10324 case ppc_stub_plt_branch_both:
10325 case ppc_stub_plt_call_notoc:
10326 case ppc_stub_plt_call_both:
10327 p = loc;
10328 off = (8 + stub_entry->stub_offset
10329 + stub_entry->group->stub_sec->output_offset
10330 + stub_entry->group->stub_sec->output_section->vma);
10331 if (stub_entry->stub_type == ppc_stub_long_branch_both
10332 || stub_entry->stub_type == ppc_stub_plt_branch_both
10333 || stub_entry->stub_type == ppc_stub_plt_call_both)
10334 {
10335 off += 4;
10336 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10337 p += 4;
10338 }
10339 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10340 {
10341 targ = stub_entry->plt_ent->plt.offset & ~1;
10342 if (targ >= (bfd_vma) -2)
10343 abort ();
10344
10345 plt = htab->elf.splt;
10346 if (!htab->elf.dynamic_sections_created
10347 || stub_entry->h == NULL
10348 || stub_entry->h->elf.dynindx == -1)
10349 {
10350 if (stub_entry->symtype == STT_GNU_IFUNC)
10351 plt = htab->elf.iplt;
10352 else
10353 plt = htab->pltlocal;
10354 }
10355 targ += plt->output_offset + plt->output_section->vma;
10356 }
10357 else
10358 targ = (stub_entry->target_value
10359 + stub_entry->target_section->output_offset
10360 + stub_entry->target_section->output_section->vma);
10361 off = targ - off;
10362 bfd_put_32 (htab->params->stub_bfd, MFLR_R12, p);
10363 p += 4;
10364 bfd_put_32 (htab->params->stub_bfd, BCL_20_31, p);
10365 p += 4;
10366 bfd_put_32 (htab->params->stub_bfd, MFLR_R11, p);
10367 p += 4;
10368 bfd_put_32 (htab->params->stub_bfd, MTLR_R12, p);
10369 p += 4;
10370 p = build_offset (htab->params->stub_bfd, p, off,
10371 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
10372 if (stub_entry->stub_type == ppc_stub_long_branch_notoc)
10373 {
10374 off += 8;
10375 bfd_put_32 (htab->params->stub_bfd,
10376 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
10377 }
10378 else if (stub_entry->stub_type == ppc_stub_long_branch_both)
10379 {
10380 off += 12;
10381 bfd_put_32 (htab->params->stub_bfd,
10382 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
10383 }
10384 else
10385 {
10386 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10387 p += 4;
10388 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10389 }
10390 p += 4;
df136d64
AM
10391
10392 if (htab->glink_eh_frame != NULL
10393 && htab->glink_eh_frame->size != 0)
10394 {
10395 bfd_byte *base, *eh;
10396 unsigned int lr_used, delta;
10397
10398 base = (htab->glink_eh_frame->contents
10399 + stub_entry->group->eh_base + 17);
10400 eh = base + stub_entry->group->eh_size;
10401 lr_used = stub_entry->stub_offset + 8;
10402 if (stub_entry->stub_type == ppc_stub_long_branch_both
10403 || stub_entry->stub_type == ppc_stub_plt_branch_both
10404 || stub_entry->stub_type == ppc_stub_plt_call_both)
10405 lr_used += 4;
10406 delta = lr_used - stub_entry->group->lr_restore;
10407 stub_entry->group->lr_restore = lr_used + 8;
10408 eh = eh_advance (htab->elf.dynobj, eh, delta);
10409 *eh++ = DW_CFA_register;
10410 *eh++ = 65;
10411 *eh++ = 12;
10412 *eh++ = DW_CFA_advance_loc + 2;
10413 *eh++ = DW_CFA_restore_extended;
10414 *eh++ = 65;
10415 stub_entry->group->eh_size = eh - base;
10416 }
05d0e962
AM
10417 break;
10418
721956f4 10419 case ppc_stub_plt_call:
794e51c0 10420 case ppc_stub_plt_call_r2save:
e054468f 10421 if (stub_entry->h != NULL
b31867b6
AM
10422 && stub_entry->h->is_func_descriptor
10423 && stub_entry->h->oh != NULL)
c862ae31 10424 {
b31867b6
AM
10425 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10426
10427 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 10428 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
10429 if (fh->elf.root.type == bfd_link_hash_undefined
10430 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
10431 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 10432 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
10433 }
10434
721956f4 10435 /* Now build the stub. */
1aa42141
AM
10436 targ = stub_entry->plt_ent->plt.offset & ~1;
10437 if (targ >= (bfd_vma) -2)
721956f4
AM
10438 abort ();
10439
33e44f2e 10440 plt = htab->elf.splt;
25f23106
AM
10441 if (!htab->elf.dynamic_sections_created
10442 || stub_entry->h == NULL
10443 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
10444 {
10445 if (stub_entry->symtype == STT_GNU_IFUNC)
10446 plt = htab->elf.iplt;
10447 else
10448 plt = htab->pltlocal;
10449 }
1aa42141 10450 targ += plt->output_offset + plt->output_section->vma;
e054468f 10451
1aa42141
AM
10452 off = (elf_gp (info->output_bfd)
10453 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10454 off = targ - off;
721956f4 10455
ad8e1ba5 10456 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 10457 {
25f53a85 10458 info->callbacks->einfo
695344c0 10459 /* xgettext:c-format */
c1c8c1ef 10460 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
10461 stub_entry->h != NULL
10462 ? stub_entry->h->elf.root.root.string
10463 : "<local sym>");
721956f4 10464 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10465 htab->stub_error = TRUE;
10466 return FALSE;
721956f4
AM
10467 }
10468
176a0d42
AM
10469 r = NULL;
10470 if (info->emitrelocations)
10471 {
6f20ed8a 10472 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
10473 ((PPC_HA (off) != 0)
10474 + (htab->opd_abi
e7d1c40c 10475 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
10476 && PPC_HA (off + 16) == PPC_HA (off))
10477 : 1)));
176a0d42
AM
10478 if (r == NULL)
10479 return FALSE;
6f20ed8a 10480 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
10481 if (bfd_big_endian (info->output_bfd))
10482 r[0].r_offset += 2;
1aa42141 10483 r[0].r_addend = targ;
176a0d42 10484 }
a7f2871e
AM
10485 if (stub_entry->h != NULL
10486 && (stub_entry->h == htab->tls_get_addr_fd
10487 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10488 && htab->params->tls_get_addr_opt)
794e51c0 10489 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 10490 else
794e51c0 10491 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
10492 break;
10493
a4b6fadd
AM
10494 case ppc_stub_save_res:
10495 return TRUE;
10496
721956f4
AM
10497 default:
10498 BFD_FAIL ();
b34976b6 10499 return FALSE;
721956f4
AM
10500 }
10501
1aa42141 10502 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 10503
e7d1c40c 10504 if (htab->params->emit_stub_syms)
97b639ba
AM
10505 {
10506 struct elf_link_hash_entry *h;
ee75fd95
AM
10507 size_t len1, len2;
10508 char *name;
10509 const char *const stub_str[] = { "long_branch",
05d0e962
AM
10510 "long_branch",
10511 "long_branch",
3f6ff479
AM
10512 "long_branch",
10513 "plt_branch",
ee75fd95 10514 "plt_branch",
05d0e962
AM
10515 "plt_branch",
10516 "plt_branch",
10517 "plt_call",
10518 "plt_call",
794e51c0 10519 "plt_call",
ee75fd95
AM
10520 "plt_call" };
10521
10522 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10523 len2 = strlen (stub_entry->root.string);
10524 name = bfd_malloc (len1 + len2 + 2);
10525 if (name == NULL)
10526 return FALSE;
10527 memcpy (name, stub_entry->root.string, 9);
10528 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10529 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10530 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
10531 if (h == NULL)
10532 return FALSE;
10533 if (h->root.type == bfd_link_hash_new)
10534 {
10535 h->root.type = bfd_link_hash_defined;
6f20ed8a 10536 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 10537 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
10538 h->ref_regular = 1;
10539 h->def_regular = 1;
10540 h->ref_regular_nonweak = 1;
10541 h->forced_local = 1;
10542 h->non_elf = 0;
2ec55de3 10543 h->root.linker_def = 1;
97b639ba
AM
10544 }
10545 }
10546
b34976b6 10547 return TRUE;
721956f4
AM
10548}
10549
10550/* As above, but don't actually build the stub. Just bump offset so
10551 we know stub section sizes, and select plt_branch stubs where
10552 long_branch stubs won't do. */
10553
b34976b6 10554static bfd_boolean
4ce794b7 10555ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
10556{
10557 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 10558 struct bfd_link_info *info;
721956f4 10559 struct ppc_link_hash_table *htab;
1aa42141 10560 bfd_vma targ, off;
721956f4
AM
10561 int size;
10562
10563 /* Massage our args to the form they really have. */
10564 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
10565 info = in_arg;
10566
10567 htab = ppc_hash_table (info);
4dfe6ac6
NC
10568 if (htab == NULL)
10569 return FALSE;
721956f4 10570
1aa42141
AM
10571 /* Make a note of the offset within the stubs for this entry. */
10572 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10573
a4b6fadd
AM
10574 if (stub_entry->h != NULL
10575 && stub_entry->h->save_res
10576 && stub_entry->h->elf.root.type == bfd_link_hash_defined
10577 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
10578 {
10579 /* Don't make stubs to out-of-line register save/restore
10580 functions. Instead, emit copies of the functions. */
10581 stub_entry->group->needs_save_res = 1;
10582 stub_entry->stub_type = ppc_stub_save_res;
10583 return TRUE;
10584 }
10585
05d0e962
AM
10586 if (stub_entry->stub_type >= ppc_stub_plt_call
10587 && stub_entry->stub_type <= ppc_stub_plt_call_both)
721956f4 10588 {
e054468f 10589 asection *plt;
1aa42141
AM
10590 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10591 if (targ >= (bfd_vma) -2)
411e1bfb 10592 abort ();
33e44f2e 10593 plt = htab->elf.splt;
25f23106
AM
10594 if (!htab->elf.dynamic_sections_created
10595 || stub_entry->h == NULL
10596 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
10597 {
10598 if (stub_entry->symtype == STT_GNU_IFUNC)
10599 plt = htab->elf.iplt;
10600 else
10601 plt = htab->pltlocal;
10602 }
1aa42141
AM
10603 targ += plt->output_offset + plt->output_section->vma;
10604
05d0e962
AM
10605 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10606 {
10607 off = (8 + stub_entry->stub_offset
10608 + stub_entry->group->stub_sec->output_offset
10609 + stub_entry->group->stub_sec->output_section->vma);
10610 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10611 off += 4;
10612 }
10613 else
10614 off = (elf_gp (info->output_bfd)
10615 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
1aa42141
AM
10616
10617 if (htab->params->plt_stub_align != 0)
10618 {
05d0e962 10619 unsigned pad = plt_stub_pad (htab, stub_entry, targ - off);
1aa42141
AM
10620
10621 stub_entry->group->stub_sec->size += pad;
10622 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
05d0e962
AM
10623 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10624 off += pad;
1aa42141 10625 }
721956f4 10626
05d0e962 10627 off = targ - off;
794e51c0 10628 size = plt_stub_size (htab, stub_entry, off);
1aa42141 10629
df136d64
AM
10630 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10631 {
10632 /* After the bcl, lr has been modified so we need to emit
10633 .eh_frame info saying the return address is in r12. */
10634 unsigned int lr_used = stub_entry->stub_offset + 8;
10635 unsigned int delta;
10636 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10637 lr_used += 4;
10638 /* The eh_frame info will consist of a DW_CFA_advance_loc or
10639 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
10640 DW_CFA_restore_extended 65. */
10641 delta = lr_used - stub_entry->group->lr_restore;
10642 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10643 stub_entry->group->lr_restore = lr_used + 8;
10644 }
10645 else
176a0d42 10646 {
05d0e962
AM
10647 if (stub_entry->h != NULL
10648 && (stub_entry->h == htab->tls_get_addr_fd
10649 || stub_entry->h == htab->tls_get_addr)
10650 && htab->params->tls_get_addr_opt
e81b4c93 10651 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
df136d64
AM
10652 {
10653 /* After the bctrl, lr has been modified so we need to
10654 emit .eh_frame info saying the return address is
10655 on the stack. In fact we put the EH info specifying
10656 that the return address is on the stack *at* the
10657 call rather than after it, because the EH info for a
10658 call needs to be specified by that point.
10659 See libgcc/unwind-dw2.c execute_cfa_program. */
10660 unsigned int lr_used = stub_entry->stub_offset + size - 20;
10661 unsigned int delta;
10662 /* The eh_frame info will consist of a DW_CFA_advance_loc
10663 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
10664 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
10665 delta = lr_used - stub_entry->group->lr_restore;
10666 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10667 stub_entry->group->lr_restore = size - 4;
10668 }
05d0e962
AM
10669
10670 if (info->emitrelocations)
10671 {
10672 stub_entry->group->stub_sec->reloc_count
10673 += ((PPC_HA (off) != 0)
10674 + (htab->opd_abi
10675 ? 2 + (htab->params->plt_static_chain
10676 && PPC_HA (off + 16) == PPC_HA (off))
10677 : 1));
10678 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10679 }
176a0d42 10680 }
721956f4
AM
10681 }
10682 else
10683 {
ad8e1ba5
AM
10684 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10685 variants. */
ac2df442 10686 bfd_vma r2off = 0;
6911b7dc 10687 bfd_vma local_off = 0;
ac2df442 10688
1aa42141
AM
10689 targ = (stub_entry->target_value
10690 + stub_entry->target_section->output_offset
10691 + stub_entry->target_section->output_section->vma);
10692 off = (stub_entry->stub_offset
10693 + stub_entry->group->stub_sec->output_offset
10694 + stub_entry->group->stub_sec->output_section->vma);
721956f4 10695
ad8e1ba5
AM
10696 /* Reset the stub type from the plt variant in case we now
10697 can reach with a shorter stub. */
10698 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10699 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10700
10701 size = 4;
10702 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10703 {
25f53a85 10704 r2off = get_r2off (info, stub_entry);
a7c49797 10705 if (r2off == (bfd_vma) -1)
aa374f67
AM
10706 {
10707 htab->stub_error = TRUE;
10708 return FALSE;
10709 }
a7c49797 10710 size = 8;
ac2df442 10711 if (PPC_HA (r2off) != 0)
a7c49797
AM
10712 size += 4;
10713 if (PPC_LO (r2off) != 0)
10714 size += 4;
1aa42141 10715 off += size - 4;
ad8e1ba5 10716 }
05d0e962
AM
10717 else if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
10718 {
10719 size = 20 + size_offset (targ - (off + 8));
10720 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
10721 size += 4;
10722 off += size - 4;
10723 }
1aa42141 10724 off = targ - off;
ad8e1ba5 10725
05d0e962 10726 if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
721956f4 10727 {
df136d64
AM
10728 /* After the bcl, lr has been modified so we need to emit
10729 .eh_frame info saying the return address is in r12. */
10730 unsigned int lr_used = stub_entry->stub_offset + 8;
10731 unsigned int delta;
10732 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
10733 lr_used += 4;
10734 /* The eh_frame info will consist of a DW_CFA_advance_loc or
10735 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
10736 DW_CFA_restore_extended 65. */
10737 delta = lr_used - stub_entry->group->lr_restore;
10738 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10739 stub_entry->group->lr_restore = lr_used + 8;
10740
05d0e962 10741 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 10742 {
05d0e962
AM
10743 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
10744 - ppc_stub_long_branch_notoc);
10745 size += 4;
721956f4 10746 }
05d0e962
AM
10747 }
10748 else
10749 {
10750 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10751
05d0e962
AM
10752 /* If the branch offset is too big, use a ppc_stub_plt_branch.
10753 Do the same for -R objects without function descriptors. */
10754 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
10755 && r2off == 0
10756 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
10757 || off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off)
721956f4 10758 {
05d0e962 10759 struct ppc_branch_hash_entry *br_entry;
63bc6f6c 10760
05d0e962
AM
10761 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10762 stub_entry->root.string + 9,
10763 TRUE, FALSE);
10764 if (br_entry == NULL)
84f5d08e 10765 {
05d0e962
AM
10766 _bfd_error_handler (_("can't build branch stub `%s'"),
10767 stub_entry->root.string);
10768 htab->stub_error = TRUE;
10769 return FALSE;
84f5d08e 10770 }
ad8e1ba5 10771
05d0e962
AM
10772 if (br_entry->iter != htab->stub_iteration)
10773 {
10774 br_entry->iter = htab->stub_iteration;
10775 br_entry->offset = htab->brlt->size;
10776 htab->brlt->size += 8;
10777
10778 if (htab->relbrlt != NULL)
10779 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10780 else if (info->emitrelocations)
10781 {
10782 htab->brlt->reloc_count += 1;
10783 htab->brlt->flags |= SEC_RELOC;
10784 }
10785 }
ac2df442 10786
05d0e962
AM
10787 targ = (br_entry->offset
10788 + htab->brlt->output_offset
10789 + htab->brlt->output_section->vma);
10790 off = (elf_gp (info->output_bfd)
10791 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10792 off = targ - off;
176a0d42 10793
05d0e962
AM
10794 if (info->emitrelocations)
10795 {
10796 stub_entry->group->stub_sec->reloc_count
10797 += 1 + (PPC_HA (off) != 0);
10798 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10799 }
10800
10801 stub_entry->stub_type
10802 += ppc_stub_plt_branch - ppc_stub_long_branch;
10803 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10804 {
10805 size = 12;
10806 if (PPC_HA (off) != 0)
10807 size = 16;
10808 }
10809 else
10810 {
10811 size = 16;
10812 if (PPC_HA (off) != 0)
10813 size += 4;
10814
10815 if (PPC_HA (r2off) != 0)
10816 size += 4;
10817 if (PPC_LO (r2off) != 0)
10818 size += 4;
10819 }
ac2df442 10820 }
05d0e962 10821 else if (info->emitrelocations)
ac2df442 10822 {
05d0e962
AM
10823 stub_entry->group->stub_sec->reloc_count += 1;
10824 stub_entry->group->stub_sec->flags |= SEC_RELOC;
ac2df442 10825 }
721956f4
AM
10826 }
10827 }
10828
6f20ed8a 10829 stub_entry->group->stub_sec->size += size;
b34976b6 10830 return TRUE;
721956f4
AM
10831}
10832
10833/* Set up various things so that we can make a list of input sections
10834 for each output section included in the link. Returns -1 on error,
cedb70c5 10835 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
10836
10837int
e7d1c40c 10838ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 10839{
6f20ed8a 10840 unsigned int id;
721956f4
AM
10841 bfd_size_type amt;
10842 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10843
4dfe6ac6
NC
10844 if (htab == NULL)
10845 return -1;
4c52953f 10846
7cf7fcc8 10847 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
10848 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
10849 htab->sec_info = bfd_zmalloc (amt);
10850 if (htab->sec_info == NULL)
721956f4
AM
10851 return -1;
10852
3d6f9012
AM
10853 /* Set toc_off for com, und, abs and ind sections. */
10854 for (id = 0; id < 3; id++)
6f20ed8a 10855 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 10856
721956f4
AM
10857 return 1;
10858}
10859
927be08e
AM
10860/* Set up for first pass at multitoc partitioning. */
10861
10862void
10863ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10864{
10865 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10866
1c865ab2 10867 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
10868 htab->toc_bfd = NULL;
10869 htab->toc_first_sec = NULL;
10870}
10871
e717da7e
AM
10872/* The linker repeatedly calls this function for each TOC input section
10873 and linker generated GOT section. Group input bfds such that the toc
927be08e 10874 within a group is less than 64k in size. */
ad8e1ba5 10875
927be08e 10876bfd_boolean
4ce794b7 10877ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
10878{
10879 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 10880 bfd_vma addr, off, limit;
ad8e1ba5 10881
4dfe6ac6
NC
10882 if (htab == NULL)
10883 return FALSE;
10884
927be08e 10885 if (!htab->second_toc_pass)
4c52953f 10886 {
927be08e 10887 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
10888 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10889
10890 if (new_bfd)
bf102f86
AM
10891 {
10892 htab->toc_bfd = isec->owner;
10893 htab->toc_first_sec = isec;
10894 }
927be08e 10895
bf102f86
AM
10896 addr = isec->output_offset + isec->output_section->vma;
10897 off = addr - htab->toc_curr;
d77c8a4b
AM
10898 limit = 0x80008000;
10899 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10900 limit = 0x10000;
10901 if (off + isec->size > limit)
bf102f86
AM
10902 {
10903 addr = (htab->toc_first_sec->output_offset
10904 + htab->toc_first_sec->output_section->vma);
10905 htab->toc_curr = addr;
a27e685f 10906 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 10907 }
99877b66 10908
927be08e
AM
10909 /* toc_curr is the base address of this toc group. Set elf_gp
10910 for the input section to be the offset relative to the
10911 output toc base plus 0x8000. Making the input elf_gp an
10912 offset allows us to move the toc as a whole without
10913 recalculating input elf_gp. */
06bcf541 10914 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
10915 off += TOC_BASE_OFF;
10916
10917 /* Die if someone uses a linker script that doesn't keep input
10918 file .toc and .got together. */
a4fd3de5
AM
10919 if (new_bfd
10920 && elf_gp (isec->owner) != 0
927be08e
AM
10921 && elf_gp (isec->owner) != off)
10922 return FALSE;
10923
10924 elf_gp (isec->owner) = off;
10925 return TRUE;
4c52953f 10926 }
927be08e
AM
10927
10928 /* During the second pass toc_first_sec points to the start of
10929 a toc group, and toc_curr is used to track the old elf_gp.
10930 We use toc_bfd to ensure we only look at each bfd once. */
10931 if (htab->toc_bfd == isec->owner)
10932 return TRUE;
10933 htab->toc_bfd = isec->owner;
10934
10935 if (htab->toc_first_sec == NULL
10936 || htab->toc_curr != elf_gp (isec->owner))
10937 {
10938 htab->toc_curr = elf_gp (isec->owner);
10939 htab->toc_first_sec = isec;
10940 }
10941 addr = (htab->toc_first_sec->output_offset
10942 + htab->toc_first_sec->output_section->vma);
06bcf541 10943 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
10944 elf_gp (isec->owner) = off;
10945
10946 return TRUE;
ad8e1ba5
AM
10947}
10948
927be08e
AM
10949/* Called via elf_link_hash_traverse to merge GOT entries for global
10950 symbol H. */
10951
10952static bfd_boolean
10953merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10954{
10955 if (h->root.type == bfd_link_hash_indirect)
10956 return TRUE;
10957
927be08e
AM
10958 merge_got_entries (&h->got.glist);
10959
10960 return TRUE;
10961}
10962
10963/* Called via elf_link_hash_traverse to allocate GOT entries for global
10964 symbol H. */
10965
10966static bfd_boolean
10967reallocate_got (struct elf_link_hash_entry *h, void *inf)
10968{
10969 struct got_entry *gent;
10970
10971 if (h->root.type == bfd_link_hash_indirect)
10972 return TRUE;
10973
927be08e
AM
10974 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10975 if (!gent->is_indirect)
10976 allocate_got (h, (struct bfd_link_info *) inf, gent);
10977 return TRUE;
10978}
10979
10980/* Called on the first multitoc pass after the last call to
10981 ppc64_elf_next_toc_section. This function removes duplicate GOT
10982 entries. */
10983
10984bfd_boolean
10985ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
10986{
10987 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
10988 struct bfd *ibfd, *ibfd2;
10989 bfd_boolean done_something;
10990
10991 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 10992
7865406b
AM
10993 if (!htab->do_multi_toc)
10994 return FALSE;
10995
d0fae19d 10996 /* Merge global sym got entries within a toc group. */
927be08e
AM
10997 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10998
10999 /* And tlsld_got. */
c72f2fb2 11000 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11001 {
11002 struct got_entry *ent, *ent2;
11003
11004 if (!is_ppc64_elf (ibfd))
11005 continue;
11006
11007 ent = ppc64_tlsld_got (ibfd);
11008 if (!ent->is_indirect
11009 && ent->got.offset != (bfd_vma) -1)
11010 {
c72f2fb2 11011 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11012 {
11013 if (!is_ppc64_elf (ibfd2))
11014 continue;
11015
11016 ent2 = ppc64_tlsld_got (ibfd2);
11017 if (!ent2->is_indirect
11018 && ent2->got.offset != (bfd_vma) -1
11019 && elf_gp (ibfd2) == elf_gp (ibfd))
11020 {
11021 ent2->is_indirect = TRUE;
11022 ent2->got.ent = ent;
11023 }
11024 }
11025 }
11026 }
11027
11028 /* Zap sizes of got sections. */
33e44f2e
AM
11029 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11030 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11031 htab->got_reli_size = 0;
11032
c72f2fb2 11033 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11034 {
11035 asection *got, *relgot;
11036
11037 if (!is_ppc64_elf (ibfd))
11038 continue;
11039
11040 got = ppc64_elf_tdata (ibfd)->got;
11041 if (got != NULL)
11042 {
11043 got->rawsize = got->size;
11044 got->size = 0;
11045 relgot = ppc64_elf_tdata (ibfd)->relgot;
11046 relgot->rawsize = relgot->size;
11047 relgot->size = 0;
11048 }
11049 }
11050
11051 /* Now reallocate the got, local syms first. We don't need to
11052 allocate section contents again since we never increase size. */
c72f2fb2 11053 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11054 {
11055 struct got_entry **lgot_ents;
11056 struct got_entry **end_lgot_ents;
11057 struct plt_entry **local_plt;
11058 struct plt_entry **end_local_plt;
f961d9dd 11059 unsigned char *lgot_masks;
927be08e
AM
11060 bfd_size_type locsymcount;
11061 Elf_Internal_Shdr *symtab_hdr;
19e08130 11062 asection *s;
927be08e
AM
11063
11064 if (!is_ppc64_elf (ibfd))
11065 continue;
11066
11067 lgot_ents = elf_local_got_ents (ibfd);
11068 if (!lgot_ents)
11069 continue;
11070
11071 symtab_hdr = &elf_symtab_hdr (ibfd);
11072 locsymcount = symtab_hdr->sh_info;
11073 end_lgot_ents = lgot_ents + locsymcount;
11074 local_plt = (struct plt_entry **) end_lgot_ents;
11075 end_local_plt = local_plt + locsymcount;
f961d9dd 11076 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11077 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11078 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11079 {
11080 struct got_entry *ent;
11081
11082 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11083 {
19e08130
AM
11084 unsigned int ent_size = 8;
11085 unsigned int rel_size = sizeof (Elf64_External_Rela);
11086
d0fae19d
AM
11087 ent->got.offset = s->size;
11088 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11089 {
19e08130
AM
11090 ent_size *= 2;
11091 rel_size *= 2;
11092 }
11093 s->size += ent_size;
37da22e5 11094 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 11095 {
33e44f2e 11096 htab->elf.irelplt->size += rel_size;
19e08130
AM
11097 htab->got_reli_size += rel_size;
11098 }
6a3858a6
AM
11099 else if (bfd_link_pic (info)
11100 && !((ent->tls_type & TLS_TPREL) != 0
11101 && bfd_link_executable (info)))
19e08130
AM
11102 {
11103 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11104 srel->size += rel_size;
d0fae19d
AM
11105 }
11106 }
927be08e
AM
11107 }
11108 }
11109
11110 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11111
c72f2fb2 11112 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11113 {
11114 struct got_entry *ent;
11115
11116 if (!is_ppc64_elf (ibfd))
11117 continue;
11118
11119 ent = ppc64_tlsld_got (ibfd);
11120 if (!ent->is_indirect
11121 && ent->got.offset != (bfd_vma) -1)
11122 {
11123 asection *s = ppc64_elf_tdata (ibfd)->got;
11124 ent->got.offset = s->size;
11125 s->size += 16;
0e1862bb 11126 if (bfd_link_pic (info))
927be08e
AM
11127 {
11128 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11129 srel->size += sizeof (Elf64_External_Rela);
11130 }
11131 }
11132 }
11133
33e44f2e 11134 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11135 if (!done_something)
c72f2fb2 11136 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11137 {
11138 asection *got;
11139
11140 if (!is_ppc64_elf (ibfd))
11141 continue;
11142
11143 got = ppc64_elf_tdata (ibfd)->got;
11144 if (got != NULL)
11145 {
11146 done_something = got->rawsize != got->size;
11147 if (done_something)
11148 break;
11149 }
11150 }
11151
11152 if (done_something)
e7d1c40c 11153 (*htab->params->layout_sections_again) ();
927be08e
AM
11154
11155 /* Set up for second pass over toc sections to recalculate elf_gp
11156 on input sections. */
11157 htab->toc_bfd = NULL;
11158 htab->toc_first_sec = NULL;
11159 htab->second_toc_pass = TRUE;
11160 return done_something;
11161}
11162
11163/* Called after second pass of multitoc partitioning. */
11164
11165void
11166ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11167{
11168 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11169
11170 /* After the second pass, toc_curr tracks the TOC offset used
11171 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11172 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11173}
11174
9b5ecbd0
AM
11175/* No toc references were found in ISEC. If the code in ISEC makes no
11176 calls, then there's no need to use toc adjusting stubs when branching
11177 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11178 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11179 needed, and 2 if a cyclical call-graph was found but no other reason
11180 for a stub was detected. If called from the top level, a return of
11181 2 means the same as a return of 0. */
9b5ecbd0
AM
11182
11183static int
4ce794b7 11184toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11185{
9b5ecbd0 11186 int ret;
70cc837d
AM
11187
11188 /* Mark this section as checked. */
11189 isec->call_check_done = 1;
9b5ecbd0 11190
772119ce
AM
11191 /* We know none of our code bearing sections will need toc stubs. */
11192 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11193 return 0;
11194
eea6121a 11195 if (isec->size == 0)
082c50f8
AM
11196 return 0;
11197
4c52953f
AM
11198 if (isec->output_section == NULL)
11199 return 0;
11200
4c52953f 11201 ret = 0;
70cc837d 11202 if (isec->reloc_count != 0)
9b5ecbd0 11203 {
70cc837d
AM
11204 Elf_Internal_Rela *relstart, *rel;
11205 Elf_Internal_Sym *local_syms;
11206 struct ppc_link_hash_table *htab;
2917689a 11207
70cc837d
AM
11208 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11209 info->keep_memory);
11210 if (relstart == NULL)
11211 return -1;
90aecf7a 11212
70cc837d
AM
11213 /* Look for branches to outside of this section. */
11214 local_syms = NULL;
11215 htab = ppc_hash_table (info);
11216 if (htab == NULL)
11217 return -1;
4c52953f 11218
70cc837d 11219 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11220 {
70cc837d
AM
11221 enum elf_ppc64_reloc_type r_type;
11222 unsigned long r_symndx;
11223 struct elf_link_hash_entry *h;
11224 struct ppc_link_hash_entry *eh;
11225 Elf_Internal_Sym *sym;
11226 asection *sym_sec;
11227 struct _opd_sec_data *opd;
11228 bfd_vma sym_value;
11229 bfd_vma dest;
11230
11231 r_type = ELF64_R_TYPE (rel->r_info);
11232 if (r_type != R_PPC64_REL24
05d0e962 11233 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
11234 && r_type != R_PPC64_REL14
11235 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d
AM
11236 && r_type != R_PPC64_REL14_BRNTAKEN
11237 && r_type != R_PPC64_PLTCALL)
70cc837d 11238 continue;
4c52953f 11239
70cc837d
AM
11240 r_symndx = ELF64_R_SYM (rel->r_info);
11241 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11242 isec->owner))
4c52953f 11243 {
70cc837d
AM
11244 ret = -1;
11245 break;
11246 }
4c52953f 11247
70cc837d
AM
11248 /* Calls to dynamic lib functions go through a plt call stub
11249 that uses r2. */
11250 eh = (struct ppc_link_hash_entry *) h;
11251 if (eh != NULL
11252 && (eh->elf.plt.plist != NULL
11253 || (eh->oh != NULL
11254 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11255 {
11256 ret = 1;
11257 break;
4c52953f
AM
11258 }
11259
70cc837d
AM
11260 if (sym_sec == NULL)
11261 /* Ignore other undefined symbols. */
4c52953f 11262 continue;
4c52953f 11263
70cc837d
AM
11264 /* Assume branches to other sections not included in the
11265 link need stubs too, to cover -R and absolute syms. */
11266 if (sym_sec->output_section == NULL)
11267 {
11268 ret = 1;
11269 break;
11270 }
4c52953f 11271
70cc837d
AM
11272 if (h == NULL)
11273 sym_value = sym->st_value;
11274 else
11275 {
11276 if (h->root.type != bfd_link_hash_defined
11277 && h->root.type != bfd_link_hash_defweak)
11278 abort ();
11279 sym_value = h->root.u.def.value;
11280 }
11281 sym_value += rel->r_addend;
4c52953f 11282
70cc837d
AM
11283 /* If this branch reloc uses an opd sym, find the code section. */
11284 opd = get_opd_info (sym_sec);
11285 if (opd != NULL)
11286 {
11287 if (h == NULL && opd->adjust != NULL)
11288 {
11289 long adjust;
4c52953f 11290
92a9c616 11291 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11292 if (adjust == -1)
11293 /* Assume deleted functions won't ever be called. */
11294 continue;
11295 sym_value += adjust;
11296 }
4c52953f 11297
aef36ac1
AM
11298 dest = opd_entry_value (sym_sec, sym_value,
11299 &sym_sec, NULL, FALSE);
70cc837d
AM
11300 if (dest == (bfd_vma) -1)
11301 continue;
11302 }
11303 else
11304 dest = (sym_value
11305 + sym_sec->output_offset
11306 + sym_sec->output_section->vma);
4c52953f 11307
70cc837d
AM
11308 /* Ignore branch to self. */
11309 if (sym_sec == isec)
11310 continue;
4c52953f 11311
70cc837d
AM
11312 /* If the called function uses the toc, we need a stub. */
11313 if (sym_sec->has_toc_reloc
11314 || sym_sec->makes_toc_func_call)
4c52953f 11315 {
70cc837d 11316 ret = 1;
4c52953f
AM
11317 break;
11318 }
70cc837d
AM
11319
11320 /* Assume any branch that needs a long branch stub might in fact
11321 need a plt_branch stub. A plt_branch stub uses r2. */
11322 else if (dest - (isec->output_offset
11323 + isec->output_section->vma
6911b7dc
AM
11324 + rel->r_offset) + (1 << 25)
11325 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11326 ? h->other
11327 : sym->st_other))
4c52953f 11328 {
70cc837d
AM
11329 ret = 1;
11330 break;
11331 }
11332
11333 /* If calling back to a section in the process of being
11334 tested, we can't say for sure that no toc adjusting stubs
11335 are needed, so don't return zero. */
11336 else if (sym_sec->call_check_in_progress)
11337 ret = 2;
11338
11339 /* Branches to another section that itself doesn't have any TOC
11340 references are OK. Recursively call ourselves to check. */
11341 else if (!sym_sec->call_check_done)
11342 {
11343 int recur;
11344
11345 /* Mark current section as indeterminate, so that other
11346 sections that call back to current won't be marked as
11347 known. */
11348 isec->call_check_in_progress = 1;
11349 recur = toc_adjusting_stub_needed (info, sym_sec);
11350 isec->call_check_in_progress = 0;
11351
4c52953f
AM
11352 if (recur != 0)
11353 {
70cc837d
AM
11354 ret = recur;
11355 if (recur != 2)
11356 break;
4c52953f
AM
11357 }
11358 }
4c52953f 11359 }
70cc837d
AM
11360
11361 if (local_syms != NULL
11362 && (elf_symtab_hdr (isec->owner).contents
11363 != (unsigned char *) local_syms))
11364 free (local_syms);
11365 if (elf_section_data (isec)->relocs != relstart)
11366 free (relstart);
9b5ecbd0
AM
11367 }
11368
70cc837d
AM
11369 if ((ret & 1) == 0
11370 && isec->map_head.s != NULL
11371 && (strcmp (isec->output_section->name, ".init") == 0
11372 || strcmp (isec->output_section->name, ".fini") == 0))
11373 {
11374 if (isec->map_head.s->has_toc_reloc
11375 || isec->map_head.s->makes_toc_func_call)
11376 ret = 1;
11377 else if (!isec->map_head.s->call_check_done)
11378 {
11379 int recur;
11380 isec->call_check_in_progress = 1;
11381 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11382 isec->call_check_in_progress = 0;
11383 if (recur != 0)
11384 ret = recur;
11385 }
11386 }
11387
11388 if (ret == 1)
11389 isec->makes_toc_func_call = 1;
4c52953f 11390
9b5ecbd0
AM
11391 return ret;
11392}
11393
721956f4
AM
11394/* The linker repeatedly calls this function for each input section,
11395 in the order that input sections are linked into output sections.
11396 Build lists of input sections to determine groupings between which
11397 we may insert linker stubs. */
11398
9b5ecbd0 11399bfd_boolean
4ce794b7 11400ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11401{
11402 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11403
4dfe6ac6
NC
11404 if (htab == NULL)
11405 return FALSE;
11406
734b6cf9 11407 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 11408 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 11409 {
3d6f9012
AM
11410 /* This happens to make the list in reverse order,
11411 which is what we want. */
6f20ed8a
AM
11412 htab->sec_info[isec->id].u.list
11413 = htab->sec_info[isec->output_section->id].u.list;
11414 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 11415 }
ad8e1ba5 11416
4c52953f 11417 if (htab->multi_toc_needed)
9b5ecbd0 11418 {
8b974ba3
AM
11419 /* Analyse sections that aren't already flagged as needing a
11420 valid toc pointer. Exclude .fixup for the linux kernel.
11421 .fixup contains branches, but only back to the function that
11422 hit an exception. */
11423 if (!(isec->has_toc_reloc
11424 || (isec->flags & SEC_CODE) == 0
11425 || strcmp (isec->name, ".fixup") == 0
11426 || isec->call_check_done))
11427 {
11428 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 11429 return FALSE;
8b974ba3
AM
11430 }
11431 /* Make all sections use the TOC assigned for this object file.
11432 This will be wrong for pasted sections; We fix that in
11433 check_pasted_section(). */
11434 if (elf_gp (isec->owner) != 0)
11435 htab->toc_curr = elf_gp (isec->owner);
11436 }
11437
6f20ed8a 11438 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 11439 return TRUE;
721956f4
AM
11440}
11441
70cc837d
AM
11442/* Check that all .init and .fini sections use the same toc, if they
11443 have toc relocs. */
11444
11445static bfd_boolean
11446check_pasted_section (struct bfd_link_info *info, const char *name)
11447{
11448 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11449
11450 if (o != NULL)
11451 {
11452 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11453 bfd_vma toc_off = 0;
11454 asection *i;
11455
11456 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11457 if (i->has_toc_reloc)
11458 {
11459 if (toc_off == 0)
6f20ed8a
AM
11460 toc_off = htab->sec_info[i->id].toc_off;
11461 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
11462 return FALSE;
11463 }
6683a28d
AM
11464
11465 if (toc_off == 0)
11466 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11467 if (i->makes_toc_func_call)
11468 {
6f20ed8a 11469 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
11470 break;
11471 }
11472
70cc837d
AM
11473 /* Make sure the whole pasted function uses the same toc offset. */
11474 if (toc_off != 0)
11475 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 11476 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
11477 }
11478 return TRUE;
11479}
11480
11481bfd_boolean
11482ppc64_elf_check_init_fini (struct bfd_link_info *info)
11483{
11484 return (check_pasted_section (info, ".init")
11485 & check_pasted_section (info, ".fini"));
11486}
11487
721956f4
AM
11488/* See whether we can group stub sections together. Grouping stub
11489 sections may result in fewer stubs. More importantly, we need to
11490 put all .init* and .fini* stubs at the beginning of the .init or
11491 .fini output sections respectively, because glibc splits the
11492 _init and _fini functions into multiple parts. Putting a stub in
11493 the middle of a function is not a good idea. */
11494
6f20ed8a
AM
11495static bfd_boolean
11496group_sections (struct bfd_link_info *info,
4ce794b7
AM
11497 bfd_size_type stub_group_size,
11498 bfd_boolean stubs_always_before_branch)
721956f4 11499{
6f20ed8a
AM
11500 struct ppc_link_hash_table *htab;
11501 asection *osec;
7c8fe5c4
AM
11502 bfd_boolean suppress_size_errors;
11503
6f20ed8a
AM
11504 htab = ppc_hash_table (info);
11505 if (htab == NULL)
11506 return FALSE;
11507
7c8fe5c4 11508 suppress_size_errors = FALSE;
7c8fe5c4
AM
11509 if (stub_group_size == 1)
11510 {
11511 /* Default values. */
11512 if (stubs_always_before_branch)
09f92717 11513 stub_group_size = 0x1e00000;
7c8fe5c4 11514 else
09f92717 11515 stub_group_size = 0x1c00000;
7c8fe5c4
AM
11516 suppress_size_errors = TRUE;
11517 }
11518
6f20ed8a 11519 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 11520 {
6f20ed8a
AM
11521 asection *tail;
11522
11523 if (osec->id >= htab->sec_info_arr_size)
11524 continue;
11525
11526 tail = htab->sec_info[osec->id].u.list;
734b6cf9 11527 while (tail != NULL)
721956f4 11528 {
734b6cf9
AM
11529 asection *curr;
11530 asection *prev;
11531 bfd_size_type total;
11532 bfd_boolean big_sec;
11533 bfd_vma curr_toc;
6f20ed8a 11534 struct map_stub *group;
09f92717 11535 bfd_size_type group_size;
734b6cf9
AM
11536
11537 curr = tail;
eea6121a 11538 total = tail->size;
09f92717
AM
11539 group_size = (ppc64_elf_section_data (tail) != NULL
11540 && ppc64_elf_section_data (tail)->has_14bit_branch
11541 ? stub_group_size >> 10 : stub_group_size);
11542
11543 big_sec = total > group_size;
7c8fe5c4 11544 if (big_sec && !suppress_size_errors)
695344c0 11545 /* xgettext:c-format */
871b3ab2 11546 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 11547 tail->owner, tail);
6f20ed8a 11548 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 11549
6f20ed8a 11550 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 11551 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
11552 < (ppc64_elf_section_data (prev) != NULL
11553 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 11554 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 11555 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
11556 curr = prev;
11557
11558 /* OK, the size from the start of CURR to the end is less
09f92717 11559 than group_size and thus can be handled by one stub
734b6cf9 11560 section. (or the tail section is itself larger than
09f92717
AM
11561 group_size, in which case we may be toast.) We should
11562 really be keeping track of the total size of stubs added
11563 here, as stubs contribute to the final output section
11564 size. That's a little tricky, and this way will only
11565 break if stubs added make the total size more than 2^25,
11566 ie. for the default stub_group_size, if stubs total more
11567 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
11568 group = bfd_alloc (curr->owner, sizeof (*group));
11569 if (group == NULL)
11570 return FALSE;
11571 group->link_sec = curr;
11572 group->stub_sec = NULL;
a4b6fadd 11573 group->needs_save_res = 0;
df136d64
AM
11574 group->lr_restore = 0;
11575 group->eh_size = 0;
11576 group->eh_base = 0;
a4b6fadd
AM
11577 group->next = htab->group;
11578 htab->group = group;
734b6cf9 11579 do
721956f4 11580 {
6f20ed8a 11581 prev = htab->sec_info[tail->id].u.list;
734b6cf9 11582 /* Set up this stub group. */
6f20ed8a 11583 htab->sec_info[tail->id].u.group = group;
721956f4 11584 }
734b6cf9
AM
11585 while (tail != curr && (tail = prev) != NULL);
11586
09f92717 11587 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
11588 bytes before the stub section can be handled by it too.
11589 Don't do this if we have a really large section after the
11590 stubs, as adding more stubs increases the chance that
11591 branches may not reach into the stub section. */
11592 if (!stubs_always_before_branch && !big_sec)
11593 {
11594 total = 0;
11595 while (prev != NULL
11596 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
11597 < (ppc64_elf_section_data (prev) != NULL
11598 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
11599 ? (group_size = stub_group_size >> 10)
11600 : group_size))
6f20ed8a 11601 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
11602 {
11603 tail = prev;
6f20ed8a
AM
11604 prev = htab->sec_info[tail->id].u.list;
11605 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
11606 }
11607 }
11608 tail = prev;
721956f4
AM
11609 }
11610 }
6f20ed8a 11611 return TRUE;
721956f4
AM
11612}
11613
58d180e8
AM
11614static const unsigned char glink_eh_frame_cie[] =
11615{
11616 0, 0, 0, 16, /* length. */
11617 0, 0, 0, 0, /* id. */
11618 1, /* CIE version. */
11619 'z', 'R', 0, /* Augmentation string. */
11620 4, /* Code alignment. */
11621 0x78, /* Data alignment. */
11622 65, /* RA reg. */
11623 1, /* Augmentation size. */
11624 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 11625 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
11626};
11627
d969d15f
AM
11628/* Stripping output sections is normally done before dynamic section
11629 symbols have been allocated. This function is called later, and
11630 handles cases like htab->brlt which is mapped to its own output
11631 section. */
11632
11633static void
11634maybe_strip_output (struct bfd_link_info *info, asection *isec)
11635{
11636 if (isec->size == 0
11637 && isec->output_section->size == 0
53d8967a 11638 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
11639 && !bfd_section_removed_from_list (info->output_bfd,
11640 isec->output_section)
11641 && elf_section_data (isec->output_section)->dynindx == 0)
11642 {
11643 isec->output_section->flags |= SEC_EXCLUDE;
11644 bfd_section_list_remove (info->output_bfd, isec->output_section);
11645 info->output_bfd->section_count--;
11646 }
11647}
11648
721956f4
AM
11649/* Determine and set the size of the stub section for a final link.
11650
11651 The basic idea here is to examine all the relocations looking for
11652 PC-relative calls to a target that is unreachable with a "bl"
11653 instruction. */
11654
b34976b6 11655bfd_boolean
e7d1c40c 11656ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
11657{
11658 bfd_size_type stub_group_size;
b34976b6 11659 bfd_boolean stubs_always_before_branch;
721956f4
AM
11660 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11661
4dfe6ac6
NC
11662 if (htab == NULL)
11663 return FALSE;
11664
0e1862bb 11665 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 11666 htab->params->plt_thread_safe = 1;
b9e5796b 11667 if (!htab->opd_abi)
e7d1c40c
AM
11668 htab->params->plt_thread_safe = 0;
11669 else if (htab->params->plt_thread_safe == -1)
794e51c0 11670 {
e2458743 11671 static const char *const thread_starter[] =
794e51c0
AM
11672 {
11673 "pthread_create",
11674 /* libstdc++ */
11675 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11676 /* librt */
11677 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11678 "mq_notify", "create_timer",
11679 /* libanl */
11680 "getaddrinfo_a",
11681 /* libgomp */
2300b5a1 11682 "GOMP_parallel",
794e51c0 11683 "GOMP_parallel_start",
2300b5a1 11684 "GOMP_parallel_loop_static",
794e51c0 11685 "GOMP_parallel_loop_static_start",
2300b5a1 11686 "GOMP_parallel_loop_dynamic",
794e51c0 11687 "GOMP_parallel_loop_dynamic_start",
2300b5a1 11688 "GOMP_parallel_loop_guided",
794e51c0 11689 "GOMP_parallel_loop_guided_start",
2300b5a1 11690 "GOMP_parallel_loop_runtime",
794e51c0 11691 "GOMP_parallel_loop_runtime_start",
2300b5a1 11692 "GOMP_parallel_sections",
68ffbac6 11693 "GOMP_parallel_sections_start",
f9dffbf0
AM
11694 /* libgo */
11695 "__go_go",
794e51c0
AM
11696 };
11697 unsigned i;
11698
a4b6fadd 11699 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
11700 {
11701 struct elf_link_hash_entry *h;
11702 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11703 FALSE, FALSE, TRUE);
e7d1c40c
AM
11704 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
11705 if (htab->params->plt_thread_safe)
794e51c0
AM
11706 break;
11707 }
11708 }
e7d1c40c
AM
11709 stubs_always_before_branch = htab->params->group_size < 0;
11710 if (htab->params->group_size < 0)
11711 stub_group_size = -htab->params->group_size;
721956f4 11712 else
e7d1c40c 11713 stub_group_size = htab->params->group_size;
721956f4 11714
6f20ed8a
AM
11715 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
11716 return FALSE;
721956f4 11717
c9301e31
AM
11718#define STUB_SHRINK_ITER 20
11719 /* Loop until no stubs added. After iteration 20 of this loop we may
11720 exit on a stub section shrinking. This is to break out of a
11721 pathological case where adding stubs on one iteration decreases
11722 section gaps (perhaps due to alignment), which then requires
11723 fewer or smaller stubs on the next iteration. */
11724
721956f4
AM
11725 while (1)
11726 {
11727 bfd *input_bfd;
11728 unsigned int bfd_indx;
a4b6fadd 11729 struct map_stub *group;
721956f4
AM
11730
11731 htab->stub_iteration += 1;
721956f4
AM
11732
11733 for (input_bfd = info->input_bfds, bfd_indx = 0;
11734 input_bfd != NULL;
c72f2fb2 11735 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
11736 {
11737 Elf_Internal_Shdr *symtab_hdr;
11738 asection *section;
6cdc0ccc 11739 Elf_Internal_Sym *local_syms = NULL;
721956f4 11740
0c8d6e5c 11741 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
11742 continue;
11743
721956f4 11744 /* We'll need the symbol table in a second. */
0ffa91dd 11745 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
11746 if (symtab_hdr->sh_info == 0)
11747 continue;
11748
721956f4
AM
11749 /* Walk over each section attached to the input bfd. */
11750 for (section = input_bfd->sections;
11751 section != NULL;
11752 section = section->next)
11753 {
721956f4 11754 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
11755
11756 /* If there aren't any relocs, then there's nothing more
11757 to do. */
11758 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
11759 || (section->flags & SEC_ALLOC) == 0
11760 || (section->flags & SEC_LOAD) == 0
11761 || (section->flags & SEC_CODE) == 0
721956f4
AM
11762 || section->reloc_count == 0)
11763 continue;
11764
11765 /* If this section is a link-once section that will be
11766 discarded, then don't create any stubs. */
11767 if (section->output_section == NULL
927be08e 11768 || section->output_section->owner != info->output_bfd)
721956f4
AM
11769 continue;
11770
1e2f5b6e
AM
11771 /* Get the relocs. */
11772 internal_relocs
4ce794b7 11773 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 11774 info->keep_memory);
721956f4 11775 if (internal_relocs == NULL)
1e2f5b6e 11776 goto error_ret_free_local;
721956f4
AM
11777
11778 /* Now examine each relocation. */
11779 irela = internal_relocs;
11780 irelaend = irela + section->reloc_count;
11781 for (; irela < irelaend; irela++)
11782 {
4ce794b7
AM
11783 enum elf_ppc64_reloc_type r_type;
11784 unsigned int r_indx;
721956f4
AM
11785 enum ppc_stub_type stub_type;
11786 struct ppc_stub_hash_entry *stub_entry;
8387904d 11787 asection *sym_sec, *code_sec;
e054468f 11788 bfd_vma sym_value, code_value;
721956f4 11789 bfd_vma destination;
6911b7dc 11790 unsigned long local_off;
8843416a 11791 bfd_boolean ok_dest;
721956f4 11792 struct ppc_link_hash_entry *hash;
8387904d 11793 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
11794 struct elf_link_hash_entry *h;
11795 Elf_Internal_Sym *sym;
721956f4
AM
11796 char *stub_name;
11797 const asection *id_sec;
74f0fb50 11798 struct _opd_sec_data *opd;
e054468f 11799 struct plt_entry *plt_ent;
721956f4
AM
11800
11801 r_type = ELF64_R_TYPE (irela->r_info);
11802 r_indx = ELF64_R_SYM (irela->r_info);
11803
4ce794b7 11804 if (r_type >= R_PPC64_max)
721956f4
AM
11805 {
11806 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 11807 goto error_ret_free_internal;
721956f4
AM
11808 }
11809
11810 /* Only look for stubs on branch instructions. */
4ce794b7 11811 if (r_type != R_PPC64_REL24
05d0e962 11812 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
11813 && r_type != R_PPC64_REL14
11814 && r_type != R_PPC64_REL14_BRTAKEN
11815 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
11816 continue;
11817
11818 /* Now determine the call target, its name, value,
11819 section. */
411e1bfb
AM
11820 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11821 r_indx, input_bfd))
11822 goto error_ret_free_internal;
11823 hash = (struct ppc_link_hash_entry *) h;
11824
8843416a 11825 ok_dest = FALSE;
8387904d 11826 fdh = NULL;
7fe2b9a6 11827 sym_value = 0;
411e1bfb 11828 if (hash == NULL)
721956f4 11829 {
411e1bfb 11830 sym_value = sym->st_value;
c27b8c2a
AM
11831 if (sym_sec != NULL
11832 && sym_sec->output_section != NULL)
11833 ok_dest = TRUE;
721956f4 11834 }
7fe2b9a6
AM
11835 else if (hash->elf.root.type == bfd_link_hash_defined
11836 || hash->elf.root.type == bfd_link_hash_defweak)
11837 {
11838 sym_value = hash->elf.root.u.def.value;
11839 if (sym_sec->output_section != NULL)
11840 ok_dest = TRUE;
11841 }
11842 else if (hash->elf.root.type == bfd_link_hash_undefweak
11843 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 11844 {
99877b66 11845 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
11846 use the func descriptor sym instead if it is
11847 defined. */
ceb1f1ef 11848 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 11849 && hash->oh != NULL)
8387904d 11850 {
8c5b4e52 11851 fdh = ppc_follow_link (hash->oh);
8387904d
AM
11852 if (fdh->elf.root.type == bfd_link_hash_defined
11853 || fdh->elf.root.type == bfd_link_hash_defweak)
11854 {
11855 sym_sec = fdh->elf.root.u.def.section;
11856 sym_value = fdh->elf.root.u.def.value;
11857 if (sym_sec->output_section != NULL)
11858 ok_dest = TRUE;
11859 }
99877b66
AM
11860 else
11861 fdh = NULL;
8387904d 11862 }
7fe2b9a6
AM
11863 }
11864 else
11865 {
11866 bfd_set_error (bfd_error_bad_value);
11867 goto error_ret_free_internal;
721956f4
AM
11868 }
11869
8843416a 11870 destination = 0;
6911b7dc 11871 local_off = 0;
8843416a
AM
11872 if (ok_dest)
11873 {
11874 sym_value += irela->r_addend;
11875 destination = (sym_value
11876 + sym_sec->output_offset
11877 + sym_sec->output_section->vma);
6911b7dc
AM
11878 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
11879 ? hash->elf.other
11880 : sym->st_other);
8843416a
AM
11881 }
11882
8387904d 11883 code_sec = sym_sec;
e054468f 11884 code_value = sym_value;
74f0fb50
AM
11885 opd = get_opd_info (sym_sec);
11886 if (opd != NULL)
8387904d
AM
11887 {
11888 bfd_vma dest;
11889
74f0fb50 11890 if (hash == NULL && opd->adjust != NULL)
8387904d 11891 {
51aecdc5 11892 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
11893 if (adjust == -1)
11894 continue;
e054468f 11895 code_value += adjust;
8387904d
AM
11896 sym_value += adjust;
11897 }
11898 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 11899 &code_sec, &code_value, FALSE);
8387904d
AM
11900 if (dest != (bfd_vma) -1)
11901 {
11902 destination = dest;
11903 if (fdh != NULL)
11904 {
11905 /* Fixup old ABI sym to point at code
11906 entry. */
99877b66 11907 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 11908 hash->elf.root.u.def.section = code_sec;
e054468f 11909 hash->elf.root.u.def.value = code_value;
8387904d
AM
11910 }
11911 }
11912 }
11913
721956f4 11914 /* Determine what (if any) linker stub is needed. */
e054468f 11915 plt_ent = NULL;
721956f4 11916 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
11917 &plt_ent, destination,
11918 local_off);
ad8e1ba5 11919
05d0e962
AM
11920 if (r_type == R_PPC64_REL24_NOTOC)
11921 {
11922 if (stub_type == ppc_stub_plt_call)
11923 stub_type = ppc_stub_plt_call_notoc;
11924 else if (stub_type == ppc_stub_long_branch
11925 || (code_sec != NULL
11926 && code_sec->output_section != NULL
11927 && (((hash ? hash->elf.other : sym->st_other)
11928 & STO_PPC64_LOCAL_MASK)
11929 != 1 << STO_PPC64_LOCAL_BIT)))
11930 stub_type = ppc_stub_long_branch_notoc;
11931 }
11932 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
11933 {
11934 /* Check whether we need a TOC adjusting stub.
11935 Since the linker pastes together pieces from
11936 different object files when creating the
11937 _init and _fini functions, it may be that a
11938 call to what looks like a local sym is in
11939 fact a call needing a TOC adjustment. */
33cb30a1
AM
11940 if ((code_sec != NULL
11941 && code_sec->output_section != NULL
11942 && (htab->sec_info[code_sec->id].toc_off
11943 != htab->sec_info[section->id].toc_off)
11944 && (code_sec->has_toc_reloc
11945 || code_sec->makes_toc_func_call))
11946 || (((hash ? hash->elf.other : sym->st_other)
11947 & STO_PPC64_LOCAL_MASK)
11948 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
11949 stub_type = ppc_stub_long_branch_r2off;
11950 }
11951
721956f4
AM
11952 if (stub_type == ppc_stub_none)
11953 continue;
11954
411e1bfb
AM
11955 /* __tls_get_addr calls might be eliminated. */
11956 if (stub_type != ppc_stub_plt_call
05d0e962 11957 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 11958 && hash != NULL
8387904d
AM
11959 && (hash == htab->tls_get_addr
11960 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
11961 && section->has_tls_reloc
11962 && irela != internal_relocs)
11963 {
11964 /* Get tls info. */
f961d9dd 11965 unsigned char *tls_mask;
411e1bfb 11966
3a71aa26 11967 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
11968 irela - 1, input_bfd))
11969 goto error_ret_free_internal;
37da22e5 11970 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
11971 continue;
11972 }
11973
f378ab09 11974 if (stub_type == ppc_stub_plt_call)
794e51c0 11975 {
6e1816be
AM
11976 if (!htab->opd_abi
11977 && htab->params->plt_localentry0 != 0
11978 && is_elfv2_localentry0 (&hash->elf))
11979 htab->has_plt_localentry0 = 1;
11980 else if (irela + 1 < irelaend
11981 && irela[1].r_offset == irela->r_offset + 4
11982 && (ELF64_R_TYPE (irela[1].r_info)
11983 == R_PPC64_TOCSAVE))
f378ab09
AM
11984 {
11985 if (!tocsave_find (htab, INSERT,
11986 &local_syms, irela + 1, input_bfd))
11987 goto error_ret_free_internal;
11988 }
f378ab09
AM
11989 else
11990 stub_type = ppc_stub_plt_call_r2save;
794e51c0 11991 }
3b421ab3 11992
721956f4 11993 /* Support for grouping stub sections. */
6f20ed8a 11994 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
11995
11996 /* Get the name of this stub. */
11997 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11998 if (!stub_name)
11999 goto error_ret_free_internal;
12000
12001 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12002 stub_name, FALSE, FALSE);
721956f4
AM
12003 if (stub_entry != NULL)
12004 {
05d0e962
AM
12005 enum ppc_stub_type old_type;
12006 /* A stub has already been created, but it may
12007 not be the required type. We shouldn't be
12008 transitioning from plt_call to long_branch
12009 stubs or vice versa, but we might be
12010 upgrading from plt_call to plt_call_r2save or
12011 from long_branch to long_branch_r2off. */
721956f4 12012 free (stub_name);
05d0e962
AM
12013 old_type = stub_entry->stub_type;
12014 switch (old_type)
12015 {
12016 default:
12017 abort ();
12018
12019 case ppc_stub_save_res:
12020 continue;
12021
12022 case ppc_stub_plt_call:
12023 case ppc_stub_plt_call_r2save:
12024 case ppc_stub_plt_call_notoc:
12025 case ppc_stub_plt_call_both:
12026 if (stub_type == ppc_stub_plt_call)
12027 continue;
12028 else if (stub_type == ppc_stub_plt_call_r2save)
12029 {
12030 if (old_type == ppc_stub_plt_call_notoc)
12031 stub_type = ppc_stub_plt_call_both;
12032 }
12033 else if (stub_type == ppc_stub_plt_call_notoc)
12034 {
12035 if (old_type == ppc_stub_plt_call_r2save)
12036 stub_type = ppc_stub_plt_call_both;
12037 }
12038 else
12039 abort ();
12040 break;
12041
12042 case ppc_stub_plt_branch:
12043 case ppc_stub_plt_branch_r2off:
12044 case ppc_stub_plt_branch_notoc:
12045 case ppc_stub_plt_branch_both:
12046 old_type += (ppc_stub_long_branch
12047 - ppc_stub_plt_branch);
12048 /* Fall through. */
12049 case ppc_stub_long_branch:
12050 case ppc_stub_long_branch_r2off:
12051 case ppc_stub_long_branch_notoc:
12052 case ppc_stub_long_branch_both:
12053 if (stub_type == ppc_stub_long_branch)
12054 continue;
12055 else if (stub_type == ppc_stub_long_branch_r2off)
12056 {
12057 if (old_type == ppc_stub_long_branch_notoc)
12058 stub_type = ppc_stub_long_branch_both;
12059 }
12060 else if (stub_type == ppc_stub_long_branch_notoc)
12061 {
12062 if (old_type == ppc_stub_long_branch_r2off)
12063 stub_type = ppc_stub_long_branch_both;
12064 }
12065 else
12066 abort ();
12067 break;
12068 }
12069 if (old_type < stub_type)
794e51c0 12070 stub_entry->stub_type = stub_type;
721956f4
AM
12071 continue;
12072 }
12073
25f53a85 12074 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12075 if (stub_entry == NULL)
12076 {
12077 free (stub_name);
6cdc0ccc
AM
12078 error_ret_free_internal:
12079 if (elf_section_data (section)->relocs == NULL)
12080 free (internal_relocs);
12081 error_ret_free_local:
12082 if (local_syms != NULL
12083 && (symtab_hdr->contents
12084 != (unsigned char *) local_syms))
12085 free (local_syms);
b34976b6 12086 return FALSE;
721956f4
AM
12087 }
12088
ad8e1ba5 12089 stub_entry->stub_type = stub_type;
05d0e962
AM
12090 if (stub_type >= ppc_stub_plt_call
12091 && stub_type <= ppc_stub_plt_call_both)
e054468f 12092 {
05d0e962
AM
12093 stub_entry->target_value = sym_value;
12094 stub_entry->target_section = sym_sec;
e054468f
AM
12095 }
12096 else
12097 {
05d0e962
AM
12098 stub_entry->target_value = code_value;
12099 stub_entry->target_section = code_sec;
e054468f 12100 }
721956f4 12101 stub_entry->h = hash;
e054468f 12102 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
12103 stub_entry->symtype
12104 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 12105 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12106
12107 if (stub_entry->h != NULL)
12108 htab->stub_globals += 1;
721956f4
AM
12109 }
12110
12111 /* We're done with the internal relocs, free them. */
6cdc0ccc 12112 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12113 free (internal_relocs);
721956f4 12114 }
6cdc0ccc
AM
12115
12116 if (local_syms != NULL
12117 && symtab_hdr->contents != (unsigned char *) local_syms)
12118 {
12119 if (!info->keep_memory)
12120 free (local_syms);
12121 else
12122 symtab_hdr->contents = (unsigned char *) local_syms;
12123 }
721956f4
AM
12124 }
12125
5c3dead3 12126 /* We may have added some stubs. Find out the new size of the
721956f4 12127 stub sections. */
d4aaa2a0 12128 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
12129 {
12130 group->lr_restore = 0;
12131 group->eh_size = 0;
12132 if (group->stub_sec != NULL)
12133 {
12134 asection *stub_sec = group->stub_sec;
12135
12136 if (htab->stub_iteration <= STUB_SHRINK_ITER
12137 || stub_sec->rawsize < stub_sec->size)
12138 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12139 stub_sec->rawsize = stub_sec->size;
12140 stub_sec->size = 0;
12141 stub_sec->reloc_count = 0;
12142 stub_sec->flags &= ~SEC_RELOC;
12143 }
12144 }
eea6121a 12145
ba21f564
AM
12146 if (htab->stub_iteration <= STUB_SHRINK_ITER
12147 || htab->brlt->rawsize < htab->brlt->size)
12148 htab->brlt->rawsize = htab->brlt->size;
eea6121a 12149 htab->brlt->size = 0;
84f5d08e
AM
12150 htab->brlt->reloc_count = 0;
12151 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12152 if (htab->relbrlt != NULL)
eea6121a 12153 htab->relbrlt->size = 0;
721956f4 12154
63bc6f6c 12155 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12156
a4b6fadd
AM
12157 for (group = htab->group; group != NULL; group = group->next)
12158 if (group->needs_save_res)
12159 group->stub_sec->size += htab->sfpr->size;
12160
176a0d42
AM
12161 if (info->emitrelocations
12162 && htab->glink != NULL && htab->glink->size != 0)
12163 {
12164 htab->glink->reloc_count = 1;
12165 htab->glink->flags |= SEC_RELOC;
12166 }
12167
58d180e8
AM
12168 if (htab->glink_eh_frame != NULL
12169 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 12170 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 12171 {
2e0ce1c8 12172 size_t size = 0, align = 4;
58d180e8 12173
d4aaa2a0 12174 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
12175 if (group->eh_size != 0)
12176 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 12177 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 12178 size += (24 + align - 1) & -align;
58d180e8 12179 if (size != 0)
2e0ce1c8
AM
12180 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12181 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12182 size = (size + align - 1) & -align;
58d180e8
AM
12183 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12184 htab->glink_eh_frame->size = size;
12185 }
12186
e7d1c40c 12187 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
12188 for (group = htab->group; group != NULL; group = group->next)
12189 if (group->stub_sec != NULL)
691d2e9a
AM
12190 {
12191 int align = abs (htab->params->plt_stub_align);
12192 group->stub_sec->size
12193 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12194 }
d4aaa2a0
AM
12195
12196 for (group = htab->group; group != NULL; group = group->next)
12197 if (group->stub_sec != NULL
12198 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 12199 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 12200 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
12201 break;
12202
d4aaa2a0 12203 if (group == NULL
ba21f564
AM
12204 && (htab->brlt->rawsize == htab->brlt->size
12205 || (htab->stub_iteration > STUB_SHRINK_ITER
12206 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
12207 && (htab->glink_eh_frame == NULL
12208 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12209 break;
12210
721956f4 12211 /* Ask the linker to do its stuff. */
e7d1c40c 12212 (*htab->params->layout_sections_again) ();
721956f4
AM
12213 }
12214
da44f4e5
AM
12215 if (htab->glink_eh_frame != NULL
12216 && htab->glink_eh_frame->size != 0)
12217 {
12218 bfd_vma val;
12219 bfd_byte *p, *last_fde;
12220 size_t last_fde_len, size, align, pad;
d4aaa2a0 12221 struct map_stub *group;
da44f4e5 12222
df136d64
AM
12223 /* It is necessary to at least have a rough outline of the
12224 linker generated CIEs and FDEs written before
12225 bfd_elf_discard_info is run, in order for these FDEs to be
12226 indexed in .eh_frame_hdr. */
da44f4e5
AM
12227 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12228 if (p == NULL)
12229 return FALSE;
12230 htab->glink_eh_frame->contents = p;
12231 last_fde = p;
2e0ce1c8 12232 align = 4;
da44f4e5
AM
12233
12234 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12235 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 12236 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 12237 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 12238 p += last_fde_len + 4;
da44f4e5 12239
d4aaa2a0 12240 for (group = htab->group; group != NULL; group = group->next)
df136d64 12241 if (group->eh_size != 0)
da44f4e5 12242 {
df136d64 12243 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 12244 last_fde = p;
df136d64 12245 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 12246 /* FDE length. */
2e0ce1c8 12247 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12248 p += 4;
12249 /* CIE pointer. */
12250 val = p - htab->glink_eh_frame->contents;
12251 bfd_put_32 (htab->elf.dynobj, val, p);
12252 p += 4;
12253 /* Offset to stub section, written later. */
12254 p += 4;
12255 /* stub section size. */
d4aaa2a0 12256 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
12257 p += 4;
12258 /* Augmentation. */
12259 p += 1;
df136d64
AM
12260 /* Make sure we don't have all nops. This is enough for
12261 elf-eh-frame.c to detect the last non-nop opcode. */
12262 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 12263 p = last_fde + last_fde_len + 4;
da44f4e5
AM
12264 }
12265 if (htab->glink != NULL && htab->glink->size != 0)
12266 {
12267 last_fde = p;
2e0ce1c8 12268 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 12269 /* FDE length. */
2e0ce1c8 12270 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12271 p += 4;
12272 /* CIE pointer. */
12273 val = p - htab->glink_eh_frame->contents;
12274 bfd_put_32 (htab->elf.dynobj, val, p);
12275 p += 4;
12276 /* Offset to .glink, written later. */
12277 p += 4;
12278 /* .glink size. */
12279 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12280 p += 4;
12281 /* Augmentation. */
12282 p += 1;
12283
12284 *p++ = DW_CFA_advance_loc + 1;
12285 *p++ = DW_CFA_register;
12286 *p++ = 65;
9f08fa5c 12287 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 12288 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
12289 *p++ = DW_CFA_restore_extended;
12290 *p++ = 65;
2e0ce1c8 12291 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
12292 }
12293 /* Subsume any padding into the last FDE if user .eh_frame
12294 sections are aligned more than glink_eh_frame. Otherwise any
12295 zero padding will be seen as a terminator. */
2e0ce1c8 12296 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 12297 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 12298 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
12299 htab->glink_eh_frame->size = size + pad;
12300 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12301 }
12302
d969d15f
AM
12303 maybe_strip_output (info, htab->brlt);
12304 if (htab->glink_eh_frame != NULL)
12305 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12306
b34976b6 12307 return TRUE;
721956f4
AM
12308}
12309
12310/* Called after we have determined section placement. If sections
805fc799 12311 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12312
805fc799 12313bfd_vma
1c865ab2 12314ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12315{
805fc799 12316 asection *s;
a27e685f 12317 bfd_vma TOCstart, adjust;
721956f4 12318
43417696
AM
12319 if (info != NULL)
12320 {
12321 struct elf_link_hash_entry *h;
12322 struct elf_link_hash_table *htab = elf_hash_table (info);
12323
12324 if (is_elf_hash_table (htab)
12325 && htab->hgot != NULL)
12326 h = htab->hgot;
12327 else
12328 {
12329 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12330 if (is_elf_hash_table (htab))
12331 htab->hgot = h;
12332 }
12333 if (h != NULL
12334 && h->root.type == bfd_link_hash_defined
12335 && !h->root.linker_def
12336 && (!is_elf_hash_table (htab)
12337 || h->def_regular))
12338 {
12339 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12340 + h->root.u.def.section->output_offset
12341 + h->root.u.def.section->output_section->vma);
12342 _bfd_set_gp_value (obfd, TOCstart);
12343 return TOCstart;
12344 }
12345 }
12346
805fc799
AM
12347 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12348 order. The TOC starts where the first of these sections starts. */
12349 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12350 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12351 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12352 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12353 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12354 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12355 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12356 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12357 {
12358 /* This may happen for
12359 o references to TOC base (SYM@toc / TOC[tc0]) without a
12360 .toc directive
12361 o bad linker script
12362 o --gc-sections and empty TOC sections
12363
12364 FIXME: Warn user? */
12365
12366 /* Look for a likely section. We probably won't even be
12367 using TOCstart. */
12368 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12369 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12370 | SEC_EXCLUDE))
805fc799
AM
12371 == (SEC_ALLOC | SEC_SMALL_DATA))
12372 break;
721956f4 12373 if (s == NULL)
805fc799 12374 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12375 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12376 == (SEC_ALLOC | SEC_SMALL_DATA))
12377 break;
721956f4 12378 if (s == NULL)
805fc799 12379 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12380 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12381 == SEC_ALLOC)
805fc799 12382 break;
721956f4 12383 if (s == NULL)
805fc799 12384 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12385 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12386 break;
12387 }
721956f4 12388
805fc799
AM
12389 TOCstart = 0;
12390 if (s != NULL)
12391 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12392
a27e685f
AM
12393 /* Force alignment. */
12394 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12395 TOCstart -= adjust;
1c865ab2
AM
12396 _bfd_set_gp_value (obfd, TOCstart);
12397
810d4e75 12398 if (info != NULL && s != NULL)
1c865ab2
AM
12399 {
12400 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12401
810d4e75
AM
12402 if (htab != NULL)
12403 {
12404 if (htab->elf.hgot != NULL)
12405 {
a27e685f 12406 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12407 htab->elf.hgot->root.u.def.section = s;
12408 }
12409 }
12410 else
1c865ab2 12411 {
810d4e75
AM
12412 struct bfd_link_hash_entry *bh = NULL;
12413 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12414 s, TOC_BASE_OFF - adjust,
12415 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12416 }
12417 }
805fc799 12418 return TOCstart;
721956f4
AM
12419}
12420
a345bc8d 12421/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 12422 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
12423
12424static bfd_boolean
49c09209 12425build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
12426{
12427 struct bfd_link_info *info;
12428 struct ppc_link_hash_table *htab;
49c09209 12429 struct plt_entry *ent;
a345bc8d
AM
12430 asection *s;
12431
12432 if (h->root.type == bfd_link_hash_indirect)
12433 return TRUE;
12434
49c09209
AM
12435 info = inf;
12436 htab = ppc_hash_table (info);
12437 if (htab == NULL)
12438 return FALSE;
12439
12440 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12441 if (ent->plt.offset != (bfd_vma) -1)
12442 {
12443 /* This symbol has an entry in the procedure linkage
12444 table. Set it up. */
12445 Elf_Internal_Rela rela;
2d7ad24e 12446 asection *plt, *relplt;
49c09209
AM
12447 bfd_byte *loc;
12448
12449 if (!htab->elf.dynamic_sections_created
12450 || h->dynindx == -1)
12451 {
12452 if (!(h->def_regular
12453 && (h->root.type == bfd_link_hash_defined
12454 || h->root.type == bfd_link_hash_defweak)))
12455 continue;
2d7ad24e
AM
12456 if (h->type == STT_GNU_IFUNC)
12457 {
12458 plt = htab->elf.iplt;
12459 relplt = htab->elf.irelplt;
12460 htab->local_ifunc_resolver = 1;
12461 if (htab->opd_abi)
12462 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12463 else
12464 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12465 }
49c09209 12466 else
2d7ad24e
AM
12467 {
12468 plt = htab->pltlocal;
12469 if (bfd_link_pic (info))
12470 {
12471 relplt = htab->relpltlocal;
12472 if (htab->opd_abi)
12473 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12474 else
12475 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12476 }
12477 else
12478 relplt = NULL;
12479 }
49c09209
AM
12480 rela.r_addend = (h->root.u.def.value
12481 + h->root.u.def.section->output_offset
12482 + h->root.u.def.section->output_section->vma
12483 + ent->addend);
2d7ad24e
AM
12484
12485 if (relplt == NULL)
12486 {
12487 loc = plt->contents + ent->plt.offset;
12488 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
12489 if (htab->opd_abi)
12490 {
12491 bfd_vma toc = elf_gp (info->output_bfd);
12492 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
12493 bfd_put_64 (info->output_bfd, toc, loc + 8);
12494 }
12495 }
12496 else
12497 {
12498 rela.r_offset = (plt->output_section->vma
12499 + plt->output_offset
12500 + ent->plt.offset);
12501 loc = relplt->contents + (relplt->reloc_count++
12502 * sizeof (Elf64_External_Rela));
12503 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12504 }
49c09209
AM
12505 }
12506 else
12507 {
12508 rela.r_offset = (htab->elf.splt->output_section->vma
12509 + htab->elf.splt->output_offset
12510 + ent->plt.offset);
12511 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
12512 rela.r_addend = ent->addend;
12513 loc = (htab->elf.srelplt->contents
12514 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
12515 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
12516 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
12517 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 12518 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 12519 }
49c09209
AM
12520 }
12521
a345bc8d
AM
12522 if (!h->pointer_equality_needed)
12523 return TRUE;
12524
12525 if (h->def_regular)
12526 return TRUE;
12527
9e390558 12528 s = htab->global_entry;
49c09209
AM
12529 if (s == NULL || s->size == 0)
12530 return TRUE;
12531
12532 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12533 if (ent->plt.offset != (bfd_vma) -1
12534 && ent->addend == 0)
a345bc8d
AM
12535 {
12536 bfd_byte *p;
12537 asection *plt;
12538 bfd_vma off;
12539
a345bc8d 12540 p = s->contents + h->root.u.def.value;
33e44f2e 12541 plt = htab->elf.splt;
a345bc8d
AM
12542 if (!htab->elf.dynamic_sections_created
12543 || h->dynindx == -1)
2d7ad24e
AM
12544 {
12545 if (h->type == STT_GNU_IFUNC)
12546 plt = htab->elf.iplt;
12547 else
12548 plt = htab->pltlocal;
12549 }
49c09209 12550 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
12551 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12552
12553 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12554 {
12555 info->callbacks->einfo
c1c8c1ef 12556 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
12557 h->root.root.string);
12558 bfd_set_error (bfd_error_bad_value);
12559 htab->stub_error = TRUE;
12560 }
12561
7341d5e2
AM
12562 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12563 if (htab->params->emit_stub_syms)
12564 {
12565 size_t len = strlen (h->root.root.string);
12566 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12567
12568 if (name == NULL)
12569 return FALSE;
12570
12571 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12572 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12573 if (h == NULL)
12574 return FALSE;
12575 if (h->root.type == bfd_link_hash_new)
12576 {
12577 h->root.type = bfd_link_hash_defined;
12578 h->root.u.def.section = s;
12579 h->root.u.def.value = p - s->contents;
12580 h->ref_regular = 1;
12581 h->def_regular = 1;
12582 h->ref_regular_nonweak = 1;
12583 h->forced_local = 1;
12584 h->non_elf = 0;
2ec55de3 12585 h->root.linker_def = 1;
7341d5e2
AM
12586 }
12587 }
12588
a345bc8d
AM
12589 if (PPC_HA (off) != 0)
12590 {
12591 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12592 p += 4;
12593 }
12594 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12595 p += 4;
12596 bfd_put_32 (s->owner, MTCTR_R12, p);
12597 p += 4;
407aa07c 12598 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
12599 break;
12600 }
12601 return TRUE;
12602}
12603
49c09209
AM
12604/* Write PLT relocs for locals. */
12605
12606static bfd_boolean
12607write_plt_relocs_for_local_syms (struct bfd_link_info *info)
12608{
12609 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12610 bfd *ibfd;
12611
12612 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12613 {
12614 struct got_entry **lgot_ents, **end_lgot_ents;
12615 struct plt_entry **local_plt, **lplt, **end_local_plt;
12616 Elf_Internal_Shdr *symtab_hdr;
12617 bfd_size_type locsymcount;
12618 Elf_Internal_Sym *local_syms = NULL;
12619 struct plt_entry *ent;
12620
12621 if (!is_ppc64_elf (ibfd))
12622 continue;
12623
12624 lgot_ents = elf_local_got_ents (ibfd);
12625 if (!lgot_ents)
12626 continue;
12627
12628 symtab_hdr = &elf_symtab_hdr (ibfd);
12629 locsymcount = symtab_hdr->sh_info;
12630 end_lgot_ents = lgot_ents + locsymcount;
12631 local_plt = (struct plt_entry **) end_lgot_ents;
12632 end_local_plt = local_plt + locsymcount;
12633 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
12634 for (ent = *lplt; ent != NULL; ent = ent->next)
12635 if (ent->plt.offset != (bfd_vma) -1)
12636 {
12637 Elf_Internal_Sym *sym;
12638 asection *sym_sec;
12639 asection *plt, *relplt;
12640 bfd_byte *loc;
12641 bfd_vma val;
49c09209
AM
12642
12643 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
12644 lplt - local_plt, ibfd))
12645 {
12646 if (local_syms != NULL
12647 && symtab_hdr->contents != (unsigned char *) local_syms)
12648 free (local_syms);
12649 return FALSE;
12650 }
12651
12652 val = sym->st_value + ent->addend;
12653 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
12654 if (sym_sec != NULL && sym_sec->output_section != NULL)
12655 val += sym_sec->output_offset + sym_sec->output_section->vma;
12656
2d7ad24e
AM
12657 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12658 {
12659 htab->local_ifunc_resolver = 1;
12660 plt = htab->elf.iplt;
12661 relplt = htab->elf.irelplt;
12662 }
12663 else
12664 {
12665 plt = htab->pltlocal;
12666 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
12667 }
49c09209 12668
2d7ad24e
AM
12669 if (relplt == NULL)
12670 {
12671 loc = plt->contents + ent->plt.offset;
12672 bfd_put_64 (info->output_bfd, val, loc);
12673 if (htab->opd_abi)
12674 {
12675 bfd_vma toc = elf_gp (ibfd);
12676 bfd_put_64 (info->output_bfd, toc, loc + 8);
12677 }
12678 }
49c09209 12679 else
2d7ad24e
AM
12680 {
12681 Elf_Internal_Rela rela;
12682 rela.r_offset = (ent->plt.offset
12683 + plt->output_offset
12684 + plt->output_section->vma);
12685 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12686 {
12687 if (htab->opd_abi)
12688 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12689 else
12690 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12691 }
12692 else
12693 {
12694 if (htab->opd_abi)
12695 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12696 else
12697 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12698 }
12699 rela.r_addend = val;
12700 loc = relplt->contents + (relplt->reloc_count++
12701 * sizeof (Elf64_External_Rela));
12702 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12703 }
49c09209
AM
12704 }
12705
12706 if (local_syms != NULL
12707 && symtab_hdr->contents != (unsigned char *) local_syms)
12708 {
12709 if (!info->keep_memory)
12710 free (local_syms);
12711 else
12712 symtab_hdr->contents = (unsigned char *) local_syms;
12713 }
12714 }
12715 return TRUE;
12716}
12717
721956f4
AM
12718/* Build all the stubs associated with the current output file.
12719 The stubs are kept in a hash table attached to the main linker
12720 hash table. This function is called via gldelf64ppc_finish. */
12721
b34976b6 12722bfd_boolean
e7d1c40c 12723ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 12724 char **stats)
5d1634d7
AM
12725{
12726 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 12727 struct map_stub *group;
721956f4 12728 asection *stub_sec;
5d1634d7 12729 bfd_byte *p;
e717da7e 12730 int stub_sec_count = 0;
5d1634d7 12731
4dfe6ac6
NC
12732 if (htab == NULL)
12733 return FALSE;
12734
eea6121a 12735 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 12736 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
12737 {
12738 group->eh_size = 0;
12739 group->lr_restore = 0;
12740 if ((stub_sec = group->stub_sec) != NULL
12741 && stub_sec->size != 0)
12742 {
12743 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
12744 stub_sec->size);
12745 if (stub_sec->contents == NULL)
12746 return FALSE;
12747 stub_sec->size = 0;
12748 }
12749 }
5d1634d7 12750
23eb7e01 12751 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 12752 {
9f951329 12753 unsigned int indx;
ad8e1ba5 12754 bfd_vma plt0;
9f951329 12755
721956f4 12756 /* Build the .glink plt call stub. */
e7d1c40c 12757 if (htab->params->emit_stub_syms)
97b639ba
AM
12758 {
12759 struct elf_link_hash_entry *h;
468392fb
AM
12760 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12761 TRUE, FALSE, FALSE);
97b639ba
AM
12762 if (h == NULL)
12763 return FALSE;
12764 if (h->root.type == bfd_link_hash_new)
12765 {
12766 h->root.type = bfd_link_hash_defined;
12767 h->root.u.def.section = htab->glink;
ee4bf8d2 12768 h->root.u.def.value = 8;
f5385ebf
AM
12769 h->ref_regular = 1;
12770 h->def_regular = 1;
12771 h->ref_regular_nonweak = 1;
12772 h->forced_local = 1;
12773 h->non_elf = 0;
2ec55de3 12774 h->root.linker_def = 1;
97b639ba
AM
12775 }
12776 }
33e44f2e
AM
12777 plt0 = (htab->elf.splt->output_section->vma
12778 + htab->elf.splt->output_offset
12779 - 16);
176a0d42
AM
12780 if (info->emitrelocations)
12781 {
12782 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12783 if (r == NULL)
12784 return FALSE;
12785 r->r_offset = (htab->glink->output_offset
12786 + htab->glink->output_section->vma);
12787 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12788 r->r_addend = plt0;
12789 }
4ce794b7 12790 p = htab->glink->contents;
176a0d42 12791 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
12792 bfd_put_64 (htab->glink->owner, plt0, p);
12793 p += 8;
b9e5796b
AM
12794 if (htab->opd_abi)
12795 {
12796 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12797 p += 4;
12798 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12799 p += 4;
12800 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12801 p += 4;
12802 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12803 p += 4;
12804 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12805 p += 4;
12806 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12807 p += 4;
12808 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12809 p += 4;
12810 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12811 p += 4;
12812 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12813 p += 4;
12814 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12815 p += 4;
12816 }
12817 else
12818 {
12819 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12820 p += 4;
12821 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12822 p += 4;
12823 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12824 p += 4;
f378ab09
AM
12825 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
12826 p += 4;
b9e5796b
AM
12827 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12828 p += 4;
12829 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12830 p += 4;
12831 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12832 p += 4;
12833 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12834 p += 4;
12835 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12836 p += 4;
12837 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12838 p += 4;
12839 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12840 p += 4;
12841 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12842 p += 4;
12843 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12844 p += 4;
12845 }
407aa07c
AM
12846 bfd_put_32 (htab->glink->owner, BCTR, p);
12847 p += 4;
c75bc4f7 12848 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 12849
9f951329
AM
12850 /* Build the .glink lazy link call stubs. */
12851 indx = 0;
9e390558 12852 while (p < htab->glink->contents + htab->glink->size)
9f951329 12853 {
b9e5796b 12854 if (htab->opd_abi)
9f951329 12855 {
b9e5796b
AM
12856 if (indx < 0x8000)
12857 {
12858 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12859 p += 4;
12860 }
12861 else
12862 {
12863 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12864 p += 4;
12865 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12866 p);
12867 p += 4;
12868 }
9f951329 12869 }
4ce794b7 12870 bfd_put_32 (htab->glink->owner,
ee4bf8d2 12871 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 12872 indx++;
9f951329
AM
12873 p += 4;
12874 }
5d1634d7 12875 }
5d1634d7 12876
49c09209
AM
12877 /* Build .glink global entry stubs, and PLT relocs for globals. */
12878 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
12879
12880 if (!write_plt_relocs_for_local_syms (info))
12881 return FALSE;
9e390558 12882
7341d5e2 12883 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 12884 {
4ce794b7 12885 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 12886 htab->brlt->size);
4ce794b7 12887 if (htab->brlt->contents == NULL)
b34976b6 12888 return FALSE;
721956f4 12889 }
ee75fd95 12890 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
12891 {
12892 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 12893 htab->relbrlt->size);
63bc6f6c
AM
12894 if (htab->relbrlt->contents == NULL)
12895 return FALSE;
12896 }
5d1634d7 12897
721956f4
AM
12898 /* Build the stubs as directed by the stub hash table. */
12899 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 12900
a4b6fadd
AM
12901 for (group = htab->group; group != NULL; group = group->next)
12902 if (group->needs_save_res)
7dda8d3c 12903 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 12904
aa8a7074
AM
12905 if (htab->relbrlt != NULL)
12906 htab->relbrlt->reloc_count = 0;
12907
e7d1c40c 12908 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
12909 for (group = htab->group; group != NULL; group = group->next)
12910 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
12911 {
12912 int align = abs (htab->params->plt_stub_align);
12913 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
12914 }
794e51c0 12915
7dda8d3c
AM
12916 for (group = htab->group; group != NULL; group = group->next)
12917 if (group->needs_save_res)
12918 {
12919 stub_sec = group->stub_sec;
12920 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
12921 htab->sfpr->contents, htab->sfpr->size);
12922 if (htab->params->emit_stub_syms)
12923 {
12924 unsigned int i;
12925
12926 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
12927 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
12928 return FALSE;
12929 }
12930 }
12931
df136d64
AM
12932 if (htab->glink_eh_frame != NULL
12933 && htab->glink_eh_frame->size != 0)
12934 {
12935 bfd_vma val;
12936 size_t align = 4;
12937
12938 p = htab->glink_eh_frame->contents;
12939 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12940
12941 for (group = htab->group; group != NULL; group = group->next)
12942 if (group->eh_size != 0)
12943 {
12944 /* Offset to stub section. */
12945 val = (group->stub_sec->output_section->vma
12946 + group->stub_sec->output_offset);
12947 val -= (htab->glink_eh_frame->output_section->vma
12948 + htab->glink_eh_frame->output_offset
12949 + (p + 8 - htab->glink_eh_frame->contents));
12950 if (val + 0x80000000 > 0xffffffff)
12951 {
12952 _bfd_error_handler
12953 (_("%s offset too large for .eh_frame sdata4 encoding"),
12954 group->stub_sec->name);
12955 return FALSE;
12956 }
12957 bfd_put_32 (htab->elf.dynobj, val, p + 8);
12958 p += (group->eh_size + 17 + 3) & -4;
12959 }
12960 if (htab->glink != NULL && htab->glink->size != 0)
12961 {
12962 /* Offset to .glink. */
12963 val = (htab->glink->output_section->vma
12964 + htab->glink->output_offset
12965 + 8);
12966 val -= (htab->glink_eh_frame->output_section->vma
12967 + htab->glink_eh_frame->output_offset
12968 + (p + 8 - htab->glink_eh_frame->contents));
12969 if (val + 0x80000000 > 0xffffffff)
12970 {
12971 _bfd_error_handler
12972 (_("%s offset too large for .eh_frame sdata4 encoding"),
12973 htab->glink->name);
12974 return FALSE;
12975 }
12976 bfd_put_32 (htab->elf.dynobj, val, p + 8);
12977 p += (24 + align - 1) & -align;
12978 }
12979 }
12980
d4aaa2a0
AM
12981 for (group = htab->group; group != NULL; group = group->next)
12982 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
12983 {
12984 stub_sec_count += 1;
c9301e31
AM
12985 if (stub_sec->rawsize != stub_sec->size
12986 && (htab->stub_iteration <= STUB_SHRINK_ITER
12987 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
12988 break;
12989 }
5d1634d7 12990
25516cc5 12991 if (group != NULL)
5d1634d7 12992 {
b34976b6 12993 htab->stub_error = TRUE;
cf97bcb0 12994 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 12995 }
721956f4 12996
d2a300cf
AM
12997 if (htab->stub_error)
12998 return FALSE;
12999
13000 if (stats != NULL)
13001 {
db84fff3 13002 size_t len;
d2a300cf
AM
13003 *stats = bfd_malloc (500);
13004 if (*stats == NULL)
13005 return FALSE;
13006
db84fff3
AM
13007 len = sprintf (*stats,
13008 ngettext ("linker stubs in %u group\n",
13009 "linker stubs in %u groups\n",
13010 stub_sec_count),
13011 stub_sec_count);
05d0e962
AM
13012 sprintf (*stats + len, _(" branch %lu\n"
13013 " branch toc adj %lu\n"
13014 " branch notoc %lu\n"
13015 " branch both %lu\n"
13016 " long branch %lu\n"
13017 " long toc adj %lu\n"
13018 " long notoc %lu\n"
13019 " long both %lu\n"
13020 " plt call %lu\n"
13021 " plt call save %lu\n"
13022 " plt call notoc %lu\n"
13023 " plt call both %lu\n"
13024 " global entry %lu"),
4ce794b7
AM
13025 htab->stub_count[ppc_stub_long_branch - 1],
13026 htab->stub_count[ppc_stub_long_branch_r2off - 1],
05d0e962
AM
13027 htab->stub_count[ppc_stub_long_branch_notoc - 1],
13028 htab->stub_count[ppc_stub_long_branch_both - 1],
4ce794b7
AM
13029 htab->stub_count[ppc_stub_plt_branch - 1],
13030 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
05d0e962
AM
13031 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13032 htab->stub_count[ppc_stub_plt_branch_both - 1],
794e51c0 13033 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2 13034 htab->stub_count[ppc_stub_plt_call_r2save - 1],
05d0e962
AM
13035 htab->stub_count[ppc_stub_plt_call_notoc - 1],
13036 htab->stub_count[ppc_stub_plt_call_both - 1],
7341d5e2 13037 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13038 }
13039 return TRUE;
5bd4f169
AM
13040}
13041
60124e18
AM
13042/* What to do when ld finds relocations against symbols defined in
13043 discarded sections. */
13044
13045static unsigned int
13046ppc64_elf_action_discarded (asection *sec)
13047{
13048 if (strcmp (".opd", sec->name) == 0)
13049 return 0;
13050
13051 if (strcmp (".toc", sec->name) == 0)
13052 return 0;
13053
bce50a28
JJ
13054 if (strcmp (".toc1", sec->name) == 0)
13055 return 0;
13056
60124e18
AM
13057 return _bfd_elf_default_action_discarded (sec);
13058}
13059
5bd4f169
AM
13060/* The RELOCATE_SECTION function is called by the ELF backend linker
13061 to handle the relocations for a section.
13062
13063 The relocs are always passed as Rela structures; if the section
13064 actually uses Rel structures, the r_addend field will always be
13065 zero.
13066
13067 This function is responsible for adjust the section contents as
13068 necessary, and (if using Rela relocs and generating a
1049f94e 13069 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13070 necessary.
13071
13072 This function does not have to worry about setting the reloc
13073 address or the reloc symbol index.
13074
13075 LOCAL_SYMS is a pointer to the swapped in local symbols.
13076
13077 LOCAL_SECTIONS is an array giving the section in the input file
13078 corresponding to the st_shndx field of each local symbol.
13079
13080 The global hash table entry for the global symbols can be found
13081 via elf_sym_hashes (input_bfd).
13082
1049f94e 13083 When generating relocatable output, this function must handle
5bd4f169
AM
13084 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13085 going to be the section symbol corresponding to the output
13086 section, which means that the addend must be adjusted
13087 accordingly. */
13088
b34976b6 13089static bfd_boolean
4ce794b7
AM
13090ppc64_elf_relocate_section (bfd *output_bfd,
13091 struct bfd_link_info *info,
13092 bfd *input_bfd,
13093 asection *input_section,
13094 bfd_byte *contents,
13095 Elf_Internal_Rela *relocs,
13096 Elf_Internal_Sym *local_syms,
13097 asection **local_sections)
5bd4f169 13098{
65f38f15 13099 struct ppc_link_hash_table *htab;
5bd4f169
AM
13100 Elf_Internal_Shdr *symtab_hdr;
13101 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13102 Elf_Internal_Rela *rel;
c316a17c 13103 Elf_Internal_Rela *wrel;
5bd4f169 13104 Elf_Internal_Rela *relend;
411e1bfb
AM
13105 Elf_Internal_Rela outrel;
13106 bfd_byte *loc;
411e1bfb 13107 struct got_entry **local_got_ents;
5bd4f169 13108 bfd_vma TOCstart;
b34976b6
AM
13109 bfd_boolean ret = TRUE;
13110 bfd_boolean is_opd;
794e51c0
AM
13111 /* Assume 'at' branch hints. */
13112 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13113 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13114
65f38f15 13115 /* Initialize howto table if needed. */
5bd4f169 13116 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13117 ppc_howto_init ();
13118
65f38f15 13119 htab = ppc_hash_table (info);
4dfe6ac6
NC
13120 if (htab == NULL)
13121 return FALSE;
ee75fd95
AM
13122
13123 /* Don't relocate stub sections. */
e7d1c40c 13124 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13125 return TRUE;
13126
0c8d6e5c 13127 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13128
411e1bfb 13129 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13130 TOCstart = elf_gp (output_bfd);
0ffa91dd 13131 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13132 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13133 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13134
c316a17c 13135 rel = wrel = relocs;
5bd4f169 13136 relend = relocs + input_section->reloc_count;
c316a17c 13137 for (; rel < relend; wrel++, rel++)
5bd4f169 13138 {
04c9666a 13139 enum elf_ppc64_reloc_type r_type;
31c76678 13140 bfd_vma addend;
5bd4f169
AM
13141 bfd_reloc_status_type r;
13142 Elf_Internal_Sym *sym;
13143 asection *sec;
039b3fef
AM
13144 struct elf_link_hash_entry *h_elf;
13145 struct ppc_link_hash_entry *h;
13146 struct ppc_link_hash_entry *fdh;
5bd4f169 13147 const char *sym_name;
0d4792f7 13148 unsigned long r_symndx, toc_symndx;
3a71aa26 13149 bfd_vma toc_addend;
f961d9dd
AM
13150 unsigned char tls_mask, tls_gd, tls_type;
13151 unsigned char sym_type;
5bd4f169 13152 bfd_vma relocation;
23cedd1d 13153 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 13154 bfd_boolean warned;
bc30df16 13155 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13156 unsigned int insn;
e11840f9 13157 unsigned int mask;
721956f4
AM
13158 struct ppc_stub_hash_entry *stub_entry;
13159 bfd_vma max_br_offset;
13160 bfd_vma from;
c316a17c 13161 Elf_Internal_Rela orig_rel;
b80eed39
AM
13162 reloc_howto_type *howto;
13163 struct reloc_howto_struct alt_howto;
5bd4f169 13164
c316a17c
AM
13165 again:
13166 orig_rel = *rel;
13167
4ce794b7 13168 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13169 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13170
13171 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13172 symbol of the previous ADDR64 reloc. The symbol gives us the
13173 proper TOC base to use. */
13174 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13175 && wrel != relocs
13176 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13177 && is_opd)
c316a17c 13178 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13179
4ce794b7
AM
13180 sym = NULL;
13181 sec = NULL;
039b3fef 13182 h_elf = NULL;
4ce794b7 13183 sym_name = NULL;
b34976b6
AM
13184 unresolved_reloc = FALSE;
13185 warned = FALSE;
65f38f15 13186
0b13192e 13187 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13188 {
13189 /* It's a local symbol. */
74f0fb50 13190 struct _opd_sec_data *opd;
4025353c 13191
5bd4f169
AM
13192 sym = local_syms + r_symndx;
13193 sec = local_sections[r_symndx];
26c61ae5 13194 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13195 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13196 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13197 opd = get_opd_info (sec);
13198 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13199 {
51aecdc5
AM
13200 long adjust = opd->adjust[OPD_NDX (sym->st_value
13201 + rel->r_addend)];
4025353c
AM
13202 if (adjust == -1)
13203 relocation = 0;
13204 else
4cc603a5
AM
13205 {
13206 /* If this is a relocation against the opd section sym
13207 and we have edited .opd, adjust the reloc addend so
13208 that ld -r and ld --emit-relocs output is correct.
13209 If it is a reloc against some other .opd symbol,
13210 then the symbol value will be adjusted later. */
13211 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13212 rel->r_addend += adjust;
13213 else
13214 relocation += adjust;
13215 }
1e2f5b6e 13216 }
5bd4f169
AM
13217 }
13218 else
13219 {
62d887d4
L
13220 bfd_boolean ignored;
13221
b2a8e766
AM
13222 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13223 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13224 h_elf, sec, relocation,
62d887d4 13225 unresolved_reloc, warned, ignored);
039b3fef
AM
13226 sym_name = h_elf->root.root.string;
13227 sym_type = h_elf->type;
b69fdb4e
AM
13228 if (sec != NULL
13229 && sec->owner == output_bfd
13230 && strcmp (sec->name, ".opd") == 0)
13231 {
13232 /* This is a symbol defined in a linker script. All
13233 such are defined in output sections, even those
13234 defined by simple assignment from a symbol defined in
13235 an input section. Transfer the symbol to an
13236 appropriate input .opd section, so that a branch to
13237 this symbol will be mapped to the location specified
13238 by the opd entry. */
13239 struct bfd_link_order *lo;
13240 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13241 if (lo->type == bfd_indirect_link_order)
13242 {
13243 asection *isec = lo->u.indirect.section;
13244 if (h_elf->root.u.def.value >= isec->output_offset
13245 && h_elf->root.u.def.value < (isec->output_offset
13246 + isec->size))
13247 {
13248 h_elf->root.u.def.value -= isec->output_offset;
13249 h_elf->root.u.def.section = isec;
13250 sec = isec;
13251 break;
13252 }
13253 }
13254 }
5bd4f169 13255 }
039b3fef 13256 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13257
dbaa2011 13258 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13259 {
13260 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13261 input_bfd, input_section,
13262 contents + rel->r_offset);
13263 wrel->r_offset = rel->r_offset;
13264 wrel->r_info = 0;
13265 wrel->r_addend = 0;
13266
13267 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 13268 symbols defined in discarded sections. Not done for
c316a17c
AM
13269 non-debug to preserve relocs in .eh_frame which the
13270 eh_frame editing code expects to be present. */
13271 if (bfd_link_relocatable (info)
13272 && (input_section->flags & SEC_DEBUGGING))
13273 wrel--;
13274
13275 continue;
13276 }
ab96bf03 13277
0e1862bb 13278 if (bfd_link_relocatable (info))
c316a17c 13279 goto copy_reloc;
ab96bf03 13280
f40da81b
AM
13281 if (h != NULL && &h->elf == htab->elf.hgot)
13282 {
6f20ed8a 13283 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13284 sec = bfd_abs_section_ptr;
13285 unresolved_reloc = FALSE;
13286 }
13287
951fd09b
AM
13288 /* TLS optimizations. Replace instruction sequences and relocs
13289 based on information we collected in tls_optimize. We edit
13290 RELOCS so that --emit-relocs will output something sensible
13291 for the final instruction stream. */
13292 tls_mask = 0;
13293 tls_gd = 0;
0d4792f7 13294 toc_symndx = 0;
727fc41e
AM
13295 if (h != NULL)
13296 tls_mask = h->tls_mask;
13297 else if (local_got_ents != NULL)
411e1bfb 13298 {
e054468f
AM
13299 struct plt_entry **local_plt = (struct plt_entry **)
13300 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13301 unsigned char *lgot_masks = (unsigned char *)
e054468f 13302 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13303 tls_mask = lgot_masks[r_symndx];
13304 }
37da22e5 13305 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
13306 && (r_type == R_PPC64_TLS
13307 || r_type == R_PPC64_TLSGD
13308 || r_type == R_PPC64_TLSLD))
13309 {
13310 /* Check for toc tls entries. */
f961d9dd 13311 unsigned char *toc_tls;
0d4792f7 13312
727fc41e
AM
13313 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13314 &local_syms, rel, input_bfd))
13315 return FALSE;
0d4792f7 13316
727fc41e
AM
13317 if (toc_tls)
13318 tls_mask = *toc_tls;
0d4792f7
AM
13319 }
13320
13321 /* Check that tls relocs are used with tls syms, and non-tls
13322 relocs are used with non-tls syms. */
cf35638d 13323 if (r_symndx != STN_UNDEF
0d4792f7
AM
13324 && r_type != R_PPC64_NONE
13325 && (h == NULL
039b3fef
AM
13326 || h->elf.root.type == bfd_link_hash_defined
13327 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13328 && (IS_PPC64_TLS_RELOC (r_type)
13329 != (sym_type == STT_TLS
13330 || (sym_type == STT_SECTION
13331 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13332 {
37da22e5 13333 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
13334 && (r_type == R_PPC64_TLS
13335 || r_type == R_PPC64_TLSGD
13336 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13337 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13338 ;
13339 else
25f53a85 13340 info->callbacks->einfo
1d483afe 13341 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 13342 /* xgettext:c-format */
c1c8c1ef 13343 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 13344 /* xgettext:c-format */
c1c8c1ef 13345 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 13346 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13347 ppc64_elf_howto_table[r_type]->name,
13348 sym_name);
411e1bfb
AM
13349 }
13350
13351 /* Ensure reloc mapping code below stays sane. */
13352 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13353 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13354 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13355 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13356 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13357 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13358 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13359 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13360 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13361 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13362 abort ();
0d4792f7 13363
411e1bfb
AM
13364 switch (r_type)
13365 {
13366 default:
411e1bfb
AM
13367 break;
13368
ba761f19 13369 case R_PPC64_LO_DS_OPT:
95f0d0d2 13370 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
13371 if ((insn & (0x3f << 26)) != 58u << 26)
13372 abort ();
13373 insn += (14u << 26) - (58u << 26);
95f0d0d2 13374 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
13375 r_type = R_PPC64_TOC16_LO;
13376 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13377 break;
13378
411e1bfb
AM
13379 case R_PPC64_TOC16:
13380 case R_PPC64_TOC16_LO:
13381 case R_PPC64_TOC16_DS:
13382 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13383 {
13384 /* Check for toc tls entries. */
f961d9dd 13385 unsigned char *toc_tls;
951fd09b 13386 int retval;
411e1bfb 13387
3a71aa26
AM
13388 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13389 &local_syms, rel, input_bfd);
951fd09b 13390 if (retval == 0)
411e1bfb
AM
13391 return FALSE;
13392
13393 if (toc_tls)
13394 {
951fd09b 13395 tls_mask = *toc_tls;
411e1bfb
AM
13396 if (r_type == R_PPC64_TOC16_DS
13397 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 13398 {
37da22e5 13399 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
13400 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13401 goto toctprel;
13402 }
411e1bfb 13403 else
951fd09b
AM
13404 {
13405 /* If we found a GD reloc pair, then we might be
13406 doing a GD->IE transition. */
13407 if (retval == 2)
13408 {
13409 tls_gd = TLS_TPRELGD;
37da22e5
AM
13410 if ((tls_mask & TLS_TLS) != 0
13411 && (tls_mask & TLS_GD) == 0)
102890f0 13412 goto tls_ldgd_opt;
951fd09b
AM
13413 }
13414 else if (retval == 3)
13415 {
37da22e5
AM
13416 if ((tls_mask & TLS_TLS) != 0
13417 && (tls_mask & TLS_LD) == 0)
102890f0 13418 goto tls_ldgd_opt;
951fd09b
AM
13419 }
13420 }
411e1bfb
AM
13421 }
13422 }
13423 break;
13424
9d6ded02
AM
13425 case R_PPC64_GOT_TPREL16_HI:
13426 case R_PPC64_GOT_TPREL16_HA:
37da22e5 13427 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
13428 && (tls_mask & TLS_TPREL) == 0)
13429 {
13430 rel->r_offset -= d_offset;
95f0d0d2 13431 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
13432 r_type = R_PPC64_NONE;
13433 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13434 }
13435 break;
13436
411e1bfb
AM
13437 case R_PPC64_GOT_TPREL16_DS:
13438 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 13439 if ((tls_mask & TLS_TLS) != 0
951fd09b 13440 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13441 {
81407a69 13442 toctprel:
95f0d0d2 13443 insn = bfd_get_32 (input_bfd,
c316a17c 13444 contents + rel->r_offset - d_offset);
411e1bfb
AM
13445 insn &= 31 << 21;
13446 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 13447 bfd_put_32 (input_bfd, insn,
c316a17c 13448 contents + rel->r_offset - d_offset);
411e1bfb 13449 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13450 if (toc_symndx != 0)
13451 {
13452 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13453 rel->r_addend = toc_addend;
0d4792f7
AM
13454 /* We changed the symbol. Start over in order to
13455 get h, sym, sec etc. right. */
c316a17c 13456 goto again;
0d4792f7
AM
13457 }
13458 else
13459 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13460 }
13461 break;
13462
13463 case R_PPC64_TLS:
37da22e5 13464 if ((tls_mask & TLS_TLS) != 0
951fd09b 13465 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13466 {
95f0d0d2 13467 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
13468 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13469 if (insn == 0)
411e1bfb 13470 abort ();
95f0d0d2 13471 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 13472 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13473 PPC64_TPREL16_LO which is at low-order half-word. */
13474 rel->r_offset += d_offset;
0d4792f7
AM
13475 r_type = R_PPC64_TPREL16_LO;
13476 if (toc_symndx != 0)
13477 {
13478 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13479 rel->r_addend = toc_addend;
0d4792f7
AM
13480 /* We changed the symbol. Start over in order to
13481 get h, sym, sec etc. right. */
c316a17c 13482 goto again;
0d4792f7
AM
13483 }
13484 else
13485 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13486 }
13487 break;
13488
411e1bfb
AM
13489 case R_PPC64_GOT_TLSGD16_HI:
13490 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 13491 tls_gd = TLS_TPRELGD;
37da22e5 13492 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
13493 goto tls_gdld_hi;
13494 break;
13495
411e1bfb
AM
13496 case R_PPC64_GOT_TLSLD16_HI:
13497 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 13498 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13499 {
951fd09b
AM
13500 tls_gdld_hi:
13501 if ((tls_mask & tls_gd) != 0)
13502 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13503 + R_PPC64_GOT_TPREL16_DS);
13504 else
411e1bfb 13505 {
4fe5ca5b 13506 rel->r_offset -= d_offset;
95f0d0d2 13507 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 13508 r_type = R_PPC64_NONE;
411e1bfb 13509 }
951fd09b 13510 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13511 }
13512 break;
13513
951fd09b
AM
13514 case R_PPC64_GOT_TLSGD16:
13515 case R_PPC64_GOT_TLSGD16_LO:
13516 tls_gd = TLS_TPRELGD;
37da22e5 13517 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13518 goto tls_ldgd_opt;
951fd09b 13519 break;
411e1bfb 13520
951fd09b
AM
13521 case R_PPC64_GOT_TLSLD16:
13522 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 13523 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 13524 {
b9f04fe0 13525 unsigned int insn1, insn2;
102890f0
AM
13526 bfd_vma offset;
13527
13528 tls_ldgd_opt:
727fc41e
AM
13529 offset = (bfd_vma) -1;
13530 /* If not using the newer R_PPC64_TLSGD/LD to mark
13531 __tls_get_addr calls, we must trust that the call
13532 stays with its arg setup insns, ie. that the next
13533 reloc is the __tls_get_addr call associated with
13534 the current reloc. Edit both insns. */
13535 if (input_section->has_tls_get_addr_call
13536 && rel + 1 < relend
13537 && branch_reloc_hash_match (input_bfd, rel + 1,
13538 htab->tls_get_addr,
13539 htab->tls_get_addr_fd))
13540 offset = rel[1].r_offset;
b86ac8e3
AM
13541 /* We read the low GOT_TLS (or TOC16) insn because we
13542 need to keep the destination reg. It may be
13543 something other than the usual r3, and moved to r3
13544 before the call by intervening code. */
95f0d0d2 13545 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 13546 contents + rel->r_offset - d_offset);
102890f0 13547 if ((tls_mask & tls_gd) != 0)
411e1bfb 13548 {
102890f0 13549 /* IE */
b86ac8e3 13550 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13551 insn1 |= 58 << 26; /* ld */
13552 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13553 if (offset != (bfd_vma) -1)
f58d5a2d 13554 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13555 if ((tls_mask & TLS_EXPLICIT) == 0)
13556 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13557 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13558 else
102890f0
AM
13559 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13560 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13561 }
13562 else
13563 {
13564 /* LE */
b86ac8e3
AM
13565 insn1 &= 0x1f << 21;
13566 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13567 insn2 = 0x38630000; /* addi 3,3,0 */
13568 if (tls_gd == 0)
951fd09b 13569 {
102890f0 13570 /* Was an LD reloc. */
1d483afe
AM
13571 if (toc_symndx)
13572 sec = local_sections[toc_symndx];
13573 for (r_symndx = 0;
13574 r_symndx < symtab_hdr->sh_info;
13575 r_symndx++)
13576 if (local_sections[r_symndx] == sec)
13577 break;
13578 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13579 r_symndx = STN_UNDEF;
102890f0 13580 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13581 if (r_symndx != STN_UNDEF)
1d483afe
AM
13582 rel->r_addend -= (local_syms[r_symndx].st_value
13583 + sec->output_offset
13584 + sec->output_section->vma);
951fd09b 13585 }
102890f0 13586 else if (toc_symndx != 0)
3a71aa26
AM
13587 {
13588 r_symndx = toc_symndx;
13589 rel->r_addend = toc_addend;
13590 }
102890f0
AM
13591 r_type = R_PPC64_TPREL16_HA;
13592 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13593 if (offset != (bfd_vma) -1)
13594 {
13595 rel[1].r_info = ELF64_R_INFO (r_symndx,
13596 R_PPC64_TPREL16_LO);
13597 rel[1].r_offset = offset + d_offset;
13598 rel[1].r_addend = rel->r_addend;
13599 }
102890f0 13600 }
95f0d0d2 13601 bfd_put_32 (input_bfd, insn1,
3a71aa26 13602 contents + rel->r_offset - d_offset);
727fc41e 13603 if (offset != (bfd_vma) -1)
b9f04fe0 13604 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
13605 if ((tls_mask & tls_gd) == 0
13606 && (tls_gd == 0 || toc_symndx != 0))
13607 {
13608 /* We changed the symbol. Start over in order
13609 to get h, sym, sec etc. right. */
c316a17c 13610 goto again;
727fc41e
AM
13611 }
13612 }
13613 break;
13614
13615 case R_PPC64_TLSGD:
37da22e5 13616 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 13617 && rel + 1 < relend)
727fc41e 13618 {
b9f04fe0 13619 unsigned int insn2;
727fc41e
AM
13620 bfd_vma offset = rel->r_offset;
13621
23cedd1d
AM
13622 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
13623 {
13624 bfd_put_32 (output_bfd, NOP, contents + offset);
13625 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13626 break;
13627 }
13628
13629 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
13630 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
13631
727fc41e
AM
13632 if ((tls_mask & TLS_TPRELGD) != 0)
13633 {
13634 /* IE */
13635 r_type = R_PPC64_NONE;
13636 insn2 = 0x7c636a14; /* add 3,3,13 */
13637 }
13638 else
13639 {
13640 /* LE */
13641 if (toc_symndx != 0)
13642 {
13643 r_symndx = toc_symndx;
13644 rel->r_addend = toc_addend;
13645 }
13646 r_type = R_PPC64_TPREL16_LO;
13647 rel->r_offset = offset + d_offset;
13648 insn2 = 0x38630000; /* addi 3,3,0 */
13649 }
13650 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13651 /* Zap the reloc on the _tls_get_addr call too. */
13652 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13653 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 13654 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 13655 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13656 goto again;
411e1bfb 13657 }
411e1bfb
AM
13658 break;
13659
727fc41e 13660 case R_PPC64_TLSLD:
37da22e5 13661 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 13662 && rel + 1 < relend)
727fc41e 13663 {
b9f04fe0 13664 unsigned int insn2;
727fc41e
AM
13665 bfd_vma offset = rel->r_offset;
13666
23cedd1d
AM
13667 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
13668 {
13669 bfd_put_32 (output_bfd, NOP, contents + offset);
13670 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13671 break;
13672 }
13673
13674 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
13675 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
13676
727fc41e
AM
13677 if (toc_symndx)
13678 sec = local_sections[toc_symndx];
13679 for (r_symndx = 0;
13680 r_symndx < symtab_hdr->sh_info;
13681 r_symndx++)
13682 if (local_sections[r_symndx] == sec)
13683 break;
13684 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13685 r_symndx = STN_UNDEF;
727fc41e 13686 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13687 if (r_symndx != STN_UNDEF)
727fc41e
AM
13688 rel->r_addend -= (local_syms[r_symndx].st_value
13689 + sec->output_offset
13690 + sec->output_section->vma);
13691
13692 r_type = R_PPC64_TPREL16_LO;
13693 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13694 rel->r_offset = offset + d_offset;
13695 /* Zap the reloc on the _tls_get_addr call too. */
13696 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13697 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 13698 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 13699 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 13700 goto again;
727fc41e
AM
13701 }
13702 break;
13703
411e1bfb 13704 case R_PPC64_DTPMOD64:
951fd09b
AM
13705 if (rel + 1 < relend
13706 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13707 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13708 {
951fd09b
AM
13709 if ((tls_mask & TLS_GD) == 0)
13710 {
13711 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13712 if ((tls_mask & TLS_TPRELGD) != 0)
13713 r_type = R_PPC64_TPREL64;
13714 else
13715 {
4ce794b7 13716 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13717 r_type = R_PPC64_NONE;
13718 }
13719 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13720 }
13721 }
13722 else
13723 {
13724 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13725 {
4ce794b7 13726 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13727 r_type = R_PPC64_NONE;
951fd09b 13728 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13729 }
411e1bfb
AM
13730 }
13731 break;
13732
13733 case R_PPC64_TPREL64:
951fd09b 13734 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13735 {
13736 r_type = R_PPC64_NONE;
13737 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13738 }
13739 break;
52a82034 13740
006589cf
AM
13741 case R_PPC64_ENTRY:
13742 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13743 if (!bfd_link_pic (info)
13744 && !info->traditional_format
13745 && relocation + 0x80008000 <= 0xffffffff)
13746 {
13747 unsigned int insn1, insn2;
13748
13749 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13750 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13751 if ((insn1 & ~0xfffc) == LD_R2_0R12
13752 && insn2 == ADD_R2_R2_R12)
13753 {
95f0d0d2 13754 bfd_put_32 (input_bfd,
006589cf
AM
13755 LIS_R2 + PPC_HA (relocation),
13756 contents + rel->r_offset);
95f0d0d2 13757 bfd_put_32 (input_bfd,
006589cf
AM
13758 ADDI_R2_R2 + PPC_LO (relocation),
13759 contents + rel->r_offset + 4);
13760 }
13761 }
13762 else
13763 {
13764 relocation -= (rel->r_offset
13765 + input_section->output_offset
13766 + input_section->output_section->vma);
13767 if (relocation + 0x80008000 <= 0xffffffff)
13768 {
13769 unsigned int insn1, insn2;
13770
13771 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13772 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13773 if ((insn1 & ~0xfffc) == LD_R2_0R12
13774 && insn2 == ADD_R2_R2_R12)
13775 {
95f0d0d2 13776 bfd_put_32 (input_bfd,
006589cf
AM
13777 ADDIS_R2_R12 + PPC_HA (relocation),
13778 contents + rel->r_offset);
95f0d0d2 13779 bfd_put_32 (input_bfd,
006589cf
AM
13780 ADDI_R2_R2 + PPC_LO (relocation),
13781 contents + rel->r_offset + 4);
13782 }
13783 }
13784 }
13785 break;
13786
52a82034
AM
13787 case R_PPC64_REL16_HA:
13788 /* If we are generating a non-PIC executable, edit
13789 . 0: addis 2,12,.TOC.-0b@ha
13790 . addi 2,2,.TOC.-0b@l
13791 used by ELFv2 global entry points to set up r2, to
13792 . lis 2,.TOC.@ha
13793 . addi 2,2,.TOC.@l
13794 if .TOC. is in range. */
0e1862bb 13795 if (!bfd_link_pic (info)
810d4e75 13796 && !info->traditional_format
006589cf 13797 && !htab->opd_abi
4f038ee5 13798 && rel->r_addend == d_offset
52a82034
AM
13799 && h != NULL && &h->elf == htab->elf.hgot
13800 && rel + 1 < relend
13801 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
13802 && rel[1].r_offset == rel->r_offset + 4
13803 && rel[1].r_addend == rel->r_addend + 4
13804 && relocation + 0x80008000 <= 0xffffffff)
13805 {
13806 unsigned int insn1, insn2;
13807 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
13808 insn1 = bfd_get_32 (input_bfd, contents + offset);
13809 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
13810 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
13811 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
13812 {
13813 r_type = R_PPC64_ADDR16_HA;
13814 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13815 rel->r_addend -= d_offset;
13816 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
13817 rel[1].r_addend -= d_offset + 4;
95f0d0d2 13818 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
13819 }
13820 }
13821 break;
411e1bfb
AM
13822 }
13823
13824 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 13825 insn = 0;
b25116a9
AM
13826 max_br_offset = 1 << 25;
13827 addend = rel->r_addend;
bc30df16 13828 reloc_dest = DEST_NORMAL;
65f38f15 13829 switch (r_type)
5bd4f169
AM
13830 {
13831 default:
65f38f15 13832 break;
5bd4f169 13833
3b421ab3
AM
13834 case R_PPC64_TOCSAVE:
13835 if (relocation + addend == (rel->r_offset
13836 + input_section->output_offset
13837 + input_section->output_section->vma)
13838 && tocsave_find (htab, NO_INSERT,
13839 &local_syms, rel, input_bfd))
13840 {
13841 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13842 if (insn == NOP
13843 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
13844 bfd_put_32 (input_bfd,
13845 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
13846 contents + rel->r_offset);
13847 }
13848 break;
13849
65f38f15
AM
13850 /* Branch taken prediction relocations. */
13851 case R_PPC64_ADDR14_BRTAKEN:
13852 case R_PPC64_REL14_BRTAKEN:
cedb70c5 13853 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 13854 /* Fall through. */
65f38f15 13855
86c76c7b 13856 /* Branch not taken prediction relocations. */
65f38f15
AM
13857 case R_PPC64_ADDR14_BRNTAKEN:
13858 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 13859 insn |= bfd_get_32 (input_bfd,
411e1bfb 13860 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 13861 /* Fall through. */
86c76c7b 13862
b25116a9
AM
13863 case R_PPC64_REL14:
13864 max_br_offset = 1 << 15;
1a0670f3 13865 /* Fall through. */
5bd4f169 13866
65f38f15 13867 case R_PPC64_REL24:
05d0e962 13868 case R_PPC64_REL24_NOTOC:
23cedd1d 13869 case R_PPC64_PLTCALL:
ad8e1ba5
AM
13870 /* Calls to functions with a different TOC, such as calls to
13871 shared objects, need to alter the TOC pointer. This is
13872 done using a linkage stub. A REL24 branching to these
13873 linkage stubs needs to be followed by a nop, as the nop
13874 will be replaced with an instruction to restore the TOC
13875 base pointer. */
8387904d 13876 fdh = h;
b31867b6
AM
13877 if (h != NULL
13878 && h->oh != NULL
13879 && h->oh->is_func_descriptor)
13880 fdh = ppc_follow_link (h->oh);
31c76678
DK
13881 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13882 htab);
23cedd1d
AM
13883 if (r_type == R_PPC64_PLTCALL
13884 && stub_entry != NULL
05d0e962
AM
13885 && stub_entry->stub_type >= ppc_stub_plt_call
13886 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
13887 stub_entry = NULL;
13888
6abec6bc 13889 if (stub_entry != NULL
ad8e1ba5 13890 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 13891 || stub_entry->stub_type == ppc_stub_plt_call_r2save
05d0e962 13892 || stub_entry->stub_type == ppc_stub_plt_call_both
ad8e1ba5 13893 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
13894 || stub_entry->stub_type == ppc_stub_plt_branch_both
13895 || stub_entry->stub_type == ppc_stub_long_branch_r2off
13896 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 13897 {
b25116a9 13898 bfd_boolean can_plt_call = FALSE;
721956f4 13899
6e1816be
AM
13900 if (stub_entry->stub_type == ppc_stub_plt_call
13901 && !htab->opd_abi
13902 && htab->params->plt_localentry0 != 0
13903 && is_elfv2_localentry0 (&h->elf))
13904 {
13905 /* The function doesn't use or change r2. */
13906 can_plt_call = TRUE;
13907 }
05d0e962
AM
13908 else if (r_type == R_PPC64_REL24_NOTOC)
13909 {
13910 /* NOTOC calls don't need to restore r2. */
13911 can_plt_call = TRUE;
13912 }
6e1816be 13913
f378ab09 13914 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
13915 branch and link followed by a nop. The nop is
13916 replaced by an insn to restore r2. */
6e1816be 13917 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 13918 {
ba8ca3e7
AM
13919 unsigned long br;
13920
13921 br = bfd_get_32 (input_bfd,
13922 contents + rel->r_offset);
13923 if ((br & 1) != 0)
41bd81ab 13924 {
ba8ca3e7
AM
13925 unsigned long nop;
13926
13927 nop = bfd_get_32 (input_bfd,
13928 contents + rel->r_offset + 4);
23cedd1d
AM
13929 if (nop == LD_R2_0R1 + STK_TOC (htab))
13930 can_plt_call = TRUE;
13931 else if (nop == NOP
13932 || nop == CROR_151515
13933 || nop == CROR_313131)
a7f2871e 13934 {
ba8ca3e7
AM
13935 if (h != NULL
13936 && (h == htab->tls_get_addr_fd
13937 || h == htab->tls_get_addr)
7c9cf415 13938 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
13939 {
13940 /* Special stub used, leave nop alone. */
13941 }
13942 else
a078d95a
AM
13943 bfd_put_32 (input_bfd,
13944 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
13945 contents + rel->r_offset + 4);
13946 can_plt_call = TRUE;
a7f2871e 13947 }
41bd81ab 13948 }
5bd4f169 13949 }
721956f4 13950
ba8ca3e7 13951 if (!can_plt_call && h != NULL)
721956f4 13952 {
ba8ca3e7
AM
13953 const char *name = h->elf.root.root.string;
13954
13955 if (*name == '.')
13956 ++name;
13957
13958 if (strncmp (name, "__libc_start_main", 17) == 0
13959 && (name[17] == 0 || name[17] == '@'))
6ab189d5 13960 {
ba8ca3e7
AM
13961 /* Allow crt1 branch to go via a toc adjusting
13962 stub. Other calls that never return could do
13963 the same, if we could detect such. */
b25116a9 13964 can_plt_call = TRUE;
6ab189d5 13965 }
ba8ca3e7
AM
13966 }
13967
13968 if (!can_plt_call)
13969 {
13970 /* g++ as of 20130507 emits self-calls without a
13971 following nop. This is arguably wrong since we
13972 have conflicting information. On the one hand a
13973 global symbol and on the other a local call
13974 sequence, but don't error for this special case.
13975 It isn't possible to cheaply verify we have
13976 exactly such a call. Allow all calls to the same
13977 section. */
13978 asection *code_sec = sec;
13979
13980 if (get_opd_info (sec) != NULL)
ad8e1ba5 13981 {
ba8ca3e7
AM
13982 bfd_vma off = (relocation + addend
13983 - sec->output_section->vma
13984 - sec->output_offset);
bc30df16 13985
ba8ca3e7 13986 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 13987 }
ba8ca3e7
AM
13988 if (code_sec == input_section)
13989 can_plt_call = TRUE;
13990 }
13991
13992 if (!can_plt_call)
13993 {
05d0e962
AM
13994 if (stub_entry->stub_type >= ppc_stub_plt_call
13995 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 13996 info->callbacks->einfo
695344c0 13997 /* xgettext:c-format */
c1c8c1ef 13998 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
13999 "recompile with -fPIC\n"),
14000 input_bfd, input_section, rel->r_offset, sym_name);
14001 else
14002 info->callbacks->einfo
695344c0 14003 /* xgettext:c-format */
c1c8c1ef 14004 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
14005 "(-mcmodel=small toc adjust stub)\n"),
14006 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14007
14008 bfd_set_error (bfd_error_bad_value);
14009 ret = FALSE;
721956f4
AM
14010 }
14011
b25116a9 14012 if (can_plt_call
05d0e962
AM
14013 && stub_entry->stub_type >= ppc_stub_plt_call
14014 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
14015 unresolved_reloc = FALSE;
14016 }
14017
6abec6bc
AM
14018 if ((stub_entry == NULL
14019 || stub_entry->stub_type == ppc_stub_long_branch
14020 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14021 && get_opd_info (sec) != NULL)
14022 {
14023 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14024 bfd_vma off = (relocation + addend
14025 - sec->output_section->vma
14026 - sec->output_offset);
aef36ac1 14027 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14028 if (dest != (bfd_vma) -1)
14029 {
14030 relocation = dest;
14031 addend = 0;
bc30df16 14032 reloc_dest = DEST_OPD;
8387904d
AM
14033 }
14034 }
14035
b25116a9
AM
14036 /* If the branch is out of reach we ought to have a long
14037 branch stub. */
14038 from = (rel->r_offset
14039 + input_section->output_offset
14040 + input_section->output_section->vma);
14041
6911b7dc
AM
14042 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14043 ? fdh->elf.other
14044 : sym->st_other);
14045
6abec6bc
AM
14046 if (stub_entry != NULL
14047 && (stub_entry->stub_type == ppc_stub_long_branch
14048 || stub_entry->stub_type == ppc_stub_plt_branch)
14049 && (r_type == R_PPC64_ADDR14_BRTAKEN
14050 || r_type == R_PPC64_ADDR14_BRNTAKEN
14051 || (relocation + addend - from + max_br_offset
14052 < 2 * max_br_offset)))
14053 /* Don't use the stub if this branch is in range. */
14054 stub_entry = NULL;
b25116a9 14055
05d0e962
AM
14056 if (stub_entry != NULL
14057 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14058 || stub_entry->stub_type == ppc_stub_long_branch_both
14059 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14060 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14061 && (r_type != R_PPC64_REL24_NOTOC
14062 || ((fdh ? fdh->elf.other : sym->st_other)
14063 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14064 && (relocation + addend - from + max_br_offset
14065 < 2 * max_br_offset))
14066 stub_entry = NULL;
14067
14068 if (stub_entry != NULL
14069 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14070 || stub_entry->stub_type == ppc_stub_long_branch_both
14071 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14072 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14073 && r_type == R_PPC64_REL24_NOTOC
14074 && (relocation + addend - from + max_br_offset
14075 < 2 * max_br_offset))
14076 stub_entry = NULL;
14077
b25116a9
AM
14078 if (stub_entry != NULL)
14079 {
14080 /* Munge up the value and addend so that we call the stub
14081 rather than the procedure directly. */
a4b6fadd
AM
14082 asection *stub_sec = stub_entry->group->stub_sec;
14083
14084 if (stub_entry->stub_type == ppc_stub_save_res)
14085 relocation += (stub_sec->output_offset
14086 + stub_sec->output_section->vma
14087 + stub_sec->size - htab->sfpr->size
14088 - htab->sfpr->output_offset
14089 - htab->sfpr->output_section->vma);
14090 else
14091 relocation = (stub_entry->stub_offset
14092 + stub_sec->output_offset
14093 + stub_sec->output_section->vma);
b25116a9 14094 addend = 0;
bc30df16 14095 reloc_dest = DEST_STUB;
3b421ab3 14096
05d0e962
AM
14097 if (((stub_entry->stub_type == ppc_stub_plt_call
14098 && ALWAYS_EMIT_R2SAVE)
14099 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14100 || stub_entry->stub_type == ppc_stub_plt_call_both)
e81b4c93
AM
14101 && !(h != NULL
14102 && (h == htab->tls_get_addr_fd
14103 || h == htab->tls_get_addr)
14104 && htab->params->tls_get_addr_opt)
3b421ab3
AM
14105 && rel + 1 < relend
14106 && rel[1].r_offset == rel->r_offset + 4
14107 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14108 relocation += 4;
05d0e962
AM
14109 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14110 || stub_entry->stub_type == ppc_stub_plt_branch_both
14111 || stub_entry->stub_type == ppc_stub_plt_call_both)
14112 && r_type == R_PPC64_REL24_NOTOC)
14113 relocation += 4;
b25116a9
AM
14114 }
14115
14116 if (insn != 0)
14117 {
794e51c0 14118 if (is_isa_v2)
721956f4 14119 {
b25116a9
AM
14120 /* Set 'a' bit. This is 0b00010 in BO field for branch
14121 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14122 for branch on CTR insns (BO == 1a00t or 1a01t). */
14123 if ((insn & (0x14 << 21)) == (0x04 << 21))
14124 insn |= 0x02 << 21;
14125 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14126 insn |= 0x08 << 21;
14127 else
14128 break;
14129 }
14130 else
14131 {
14132 /* Invert 'y' bit if not the default. */
4cc603a5 14133 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14134 insn ^= 0x01 << 21;
721956f4 14135 }
b25116a9 14136
95f0d0d2 14137 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14138 }
e86ce104 14139
06da1e8e
AM
14140 /* NOP out calls to undefined weak functions.
14141 We can thus call a weak function without first
14142 checking whether the function is defined. */
b25116a9 14143 else if (h != NULL
039b3fef 14144 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14145 && h->elf.dynindx == -1
05d0e962
AM
14146 && (r_type == R_PPC64_REL24
14147 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 14148 && relocation == 0
4cc603a5 14149 && addend == 0)
e86ce104 14150 {
95f0d0d2 14151 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14152 goto copy_reloc;
e86ce104 14153 }
65f38f15
AM
14154 break;
14155 }
5bd4f169 14156
65f38f15 14157 /* Set `addend'. */
411e1bfb 14158 tls_type = 0;
23cedd1d 14159 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
14160 switch (r_type)
14161 {
14162 default:
cf97bcb0
AM
14163 /* xgettext:c-format */
14164 _bfd_error_handler (_("%pB: %s unsupported"),
14165 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 14166
65f38f15 14167 bfd_set_error (bfd_error_bad_value);
b34976b6 14168 ret = FALSE;
c316a17c 14169 goto copy_reloc;
5bd4f169 14170
65f38f15 14171 case R_PPC64_NONE:
411e1bfb 14172 case R_PPC64_TLS:
727fc41e
AM
14173 case R_PPC64_TLSGD:
14174 case R_PPC64_TLSLD:
3b421ab3 14175 case R_PPC64_TOCSAVE:
04c9666a
AM
14176 case R_PPC64_GNU_VTINHERIT:
14177 case R_PPC64_GNU_VTENTRY:
006589cf 14178 case R_PPC64_ENTRY:
c316a17c 14179 goto copy_reloc;
5bd4f169
AM
14180
14181 /* GOT16 relocations. Like an ADDR16 using the symbol's
14182 address in the GOT as relocation value instead of the
411e1bfb 14183 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14184 symbol and put the symbol value there. */
411e1bfb
AM
14185 case R_PPC64_GOT_TLSGD16:
14186 case R_PPC64_GOT_TLSGD16_LO:
14187 case R_PPC64_GOT_TLSGD16_HI:
14188 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14189 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14190 goto dogot;
14191
14192 case R_PPC64_GOT_TLSLD16:
14193 case R_PPC64_GOT_TLSLD16_LO:
14194 case R_PPC64_GOT_TLSLD16_HI:
14195 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14196 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14197 goto dogot;
14198
14199 case R_PPC64_GOT_TPREL16_DS:
14200 case R_PPC64_GOT_TPREL16_LO_DS:
14201 case R_PPC64_GOT_TPREL16_HI:
14202 case R_PPC64_GOT_TPREL16_HA:
14203 tls_type = TLS_TLS | TLS_TPREL;
14204 goto dogot;
14205
14206 case R_PPC64_GOT_DTPREL16_DS:
14207 case R_PPC64_GOT_DTPREL16_LO_DS:
14208 case R_PPC64_GOT_DTPREL16_HI:
14209 case R_PPC64_GOT_DTPREL16_HA:
14210 tls_type = TLS_TLS | TLS_DTPREL;
14211 goto dogot;
14212
65f38f15
AM
14213 case R_PPC64_GOT16:
14214 case R_PPC64_GOT16_LO:
14215 case R_PPC64_GOT16_HI:
14216 case R_PPC64_GOT16_HA:
14217 case R_PPC64_GOT16_DS:
14218 case R_PPC64_GOT16_LO_DS:
411e1bfb 14219 dogot:
5bd4f169
AM
14220 {
14221 /* Relocation is to the entry for this symbol in the global
14222 offset table. */
e717da7e 14223 asection *got;
d881513a 14224 bfd_vma *offp;
5bd4f169 14225 bfd_vma off;
d881513a 14226 unsigned long indx = 0;
927be08e 14227 struct got_entry *ent;
65f38f15 14228
d881513a
AM
14229 if (tls_type == (TLS_TLS | TLS_LD)
14230 && (h == NULL
f5385ebf 14231 || !h->elf.def_dynamic))
927be08e 14232 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14233 else
5bd4f169 14234 {
d881513a
AM
14235 if (h != NULL)
14236 {
f0158f44
AM
14237 if (!htab->elf.dynamic_sections_created
14238 || h->elf.dynindx == -1
14239 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 14240 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
14241 /* This is actually a static link, or it is a
14242 -Bsymbolic link and the symbol is defined
14243 locally, or the symbol was forced to be local
14244 because of a version file. */
14245 ;
14246 else
14247 {
039b3fef 14248 indx = h->elf.dynindx;
d881513a
AM
14249 unresolved_reloc = FALSE;
14250 }
039b3fef 14251 ent = h->elf.got.glist;
d881513a 14252 }
411e1bfb 14253 else
5bd4f169 14254 {
d881513a
AM
14255 if (local_got_ents == NULL)
14256 abort ();
14257 ent = local_got_ents[r_symndx];
5bd4f169 14258 }
d881513a
AM
14259
14260 for (; ent != NULL; ent = ent->next)
31c76678 14261 if (ent->addend == orig_rel.r_addend
e717da7e 14262 && ent->owner == input_bfd
d881513a
AM
14263 && ent->tls_type == tls_type)
14264 break;
5bd4f169 14265 }
411e1bfb 14266
927be08e
AM
14267 if (ent == NULL)
14268 abort ();
14269 if (ent->is_indirect)
14270 ent = ent->got.ent;
14271 offp = &ent->got.offset;
14272 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14273 if (got == NULL)
14274 abort ();
14275
411e1bfb
AM
14276 /* The offset must always be a multiple of 8. We use the
14277 least significant bit to record whether we have already
14278 processed this entry. */
d881513a 14279 off = *offp;
411e1bfb
AM
14280 if ((off & 1) != 0)
14281 off &= ~1;
5bd4f169
AM
14282 else
14283 {
411e1bfb
AM
14284 /* Generate relocs for the dynamic linker, except in
14285 the case of TLSLD where we'll use one entry per
14286 module. */
25f23106
AM
14287 asection *relgot;
14288 bfd_boolean ifunc;
e717da7e 14289
d881513a 14290 *offp = off | 1;
25f23106
AM
14291 relgot = NULL;
14292 ifunc = (h != NULL
14293 ? h->elf.type == STT_GNU_IFUNC
14294 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14295 if (ifunc)
82e66161
AM
14296 {
14297 relgot = htab->elf.irelplt;
14298 if (indx == 0)
14299 htab->local_ifunc_resolver = 1;
14300 else if (is_static_defined (&h->elf))
14301 htab->maybe_local_ifunc_resolver = 1;
14302 }
f0158f44
AM
14303 else if (indx != 0
14304 || (bfd_link_pic (info)
14305 && (h == NULL
21d68fcd 14306 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 14307 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
14308 && !h->elf.def_dynamic))
14309 && !(tls_type == (TLS_TLS | TLS_TPREL)
14310 && bfd_link_executable (info)
14311 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 14312 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14313 if (relgot != NULL)
5bd4f169 14314 {
e717da7e
AM
14315 outrel.r_offset = (got->output_section->vma
14316 + got->output_offset
411e1bfb 14317 + off);
4cc603a5 14318 outrel.r_addend = addend;
d881513a 14319 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14320 {
411e1bfb 14321 outrel.r_addend = 0;
e515b051 14322 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14323 if (tls_type == (TLS_TLS | TLS_GD))
14324 {
e717da7e
AM
14325 loc = relgot->contents;
14326 loc += (relgot->reloc_count++
d881513a
AM
14327 * sizeof (Elf64_External_Rela));
14328 bfd_elf64_swap_reloca_out (output_bfd,
14329 &outrel, loc);
e515b051 14330 outrel.r_offset += 8;
4cc603a5 14331 outrel.r_addend = addend;
d881513a
AM
14332 outrel.r_info
14333 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14334 }
411e1bfb 14335 }
951fd09b 14336 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14337 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14338 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14339 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14340 else if (indx != 0)
14341 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14342 else
81407a69 14343 {
25f23106
AM
14344 if (ifunc)
14345 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14346 else
14347 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14348
14349 /* Write the .got section contents for the sake
14350 of prelink. */
e717da7e 14351 loc = got->contents + off;
23fbd6fa
JJ
14352 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14353 loc);
81407a69 14354 }
81407a69
AM
14355
14356 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14357 {
14358 outrel.r_addend += relocation;
14359 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14360 {
14361 if (htab->elf.tls_sec == NULL)
14362 outrel.r_addend = 0;
14363 else
14364 outrel.r_addend -= htab->elf.tls_sec->vma;
14365 }
e515b051 14366 }
e717da7e
AM
14367 loc = relgot->contents;
14368 loc += (relgot->reloc_count++
411e1bfb
AM
14369 * sizeof (Elf64_External_Rela));
14370 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14371 }
14372
ad8e1ba5 14373 /* Init the .got section contents here if we're not
81407a69 14374 emitting a reloc. */
d881513a 14375 else
411e1bfb 14376 {
4cc603a5 14377 relocation += addend;
f0158f44 14378 if (tls_type != 0)
411e1bfb 14379 {
989f9879
AM
14380 if (htab->elf.tls_sec == NULL)
14381 relocation = 0;
14382 else
14383 {
f0158f44
AM
14384 if (tls_type & TLS_LD)
14385 relocation = 0;
14386 else
14387 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 14388 if (tls_type & TLS_TPREL)
989f9879
AM
14389 relocation += DTP_OFFSET - TP_OFFSET;
14390 }
5bd4f169 14391
f0158f44 14392 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
14393 {
14394 bfd_put_64 (output_bfd, relocation,
e717da7e 14395 got->contents + off + 8);
676ee2b5 14396 relocation = 1;
7b609f53 14397 }
411e1bfb
AM
14398 }
14399 bfd_put_64 (output_bfd, relocation,
e717da7e 14400 got->contents + off);
5bd4f169
AM
14401 }
14402 }
14403
65f38f15
AM
14404 if (off >= (bfd_vma) -2)
14405 abort ();
14406
bf102f86 14407 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14408 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14409 }
65f38f15
AM
14410 break;
14411
14412 case R_PPC64_PLT16_HA:
14413 case R_PPC64_PLT16_HI:
14414 case R_PPC64_PLT16_LO:
08be3224 14415 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
14416 case R_PPC64_PLT32:
14417 case R_PPC64_PLT64:
23cedd1d
AM
14418 case R_PPC64_PLTSEQ:
14419 case R_PPC64_PLTCALL:
65f38f15
AM
14420 /* Relocation is to the entry for this symbol in the
14421 procedure linkage table. */
23cedd1d 14422 unresolved_reloc = TRUE;
cbf95972
AM
14423 {
14424 struct plt_entry **plt_list = NULL;
14425 if (h != NULL)
14426 plt_list = &h->elf.plt.plist;
14427 else if (local_got_ents != NULL)
14428 {
14429 struct plt_entry **local_plt = (struct plt_entry **)
14430 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 14431 plt_list = local_plt + r_symndx;
cbf95972
AM
14432 }
14433 if (plt_list)
14434 {
14435 struct plt_entry *ent;
65f38f15 14436
cbf95972
AM
14437 for (ent = *plt_list; ent != NULL; ent = ent->next)
14438 if (ent->plt.offset != (bfd_vma) -1
14439 && ent->addend == orig_rel.r_addend)
14440 {
14441 asection *plt;
08be3224 14442 bfd_vma got;
cbf95972
AM
14443
14444 plt = htab->elf.splt;
14445 if (!htab->elf.dynamic_sections_created
14446 || h == NULL
14447 || h->elf.dynindx == -1)
2d7ad24e
AM
14448 {
14449 if (h != NULL
14450 ? h->elf.type == STT_GNU_IFUNC
14451 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14452 plt = htab->elf.iplt;
14453 else
14454 plt = htab->pltlocal;
14455 }
14456 relocation = (plt->output_section->vma
14457 + plt->output_offset
14458 + ent->plt.offset);
08be3224 14459 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
14460 || r_type == R_PPC64_PLT16_HI
14461 || r_type == R_PPC64_PLT16_LO
14462 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
14463 {
14464 got = (elf_gp (output_bfd)
14465 + htab->sec_info[input_section->id].toc_off);
14466 relocation -= got;
14467 }
cbf95972
AM
14468 addend = 0;
14469 unresolved_reloc = FALSE;
14470 break;
14471 }
14472 }
14473 }
65f38f15 14474 break;
5bd4f169 14475
0b13192e
AM
14476 case R_PPC64_TOC:
14477 /* Relocation value is TOC base. */
14478 relocation = TOCstart;
cf35638d 14479 if (r_symndx == STN_UNDEF)
6f20ed8a 14480 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14481 else if (unresolved_reloc)
14482 ;
6f20ed8a
AM
14483 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14484 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14485 else
14486 unresolved_reloc = TRUE;
ab96bf03 14487 goto dodyn;
0b13192e 14488
5bd4f169
AM
14489 /* TOC16 relocs. We want the offset relative to the TOC base,
14490 which is the address of the start of the TOC plus 0x8000.
14491 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14492 in this order. */
65f38f15
AM
14493 case R_PPC64_TOC16:
14494 case R_PPC64_TOC16_LO:
14495 case R_PPC64_TOC16_HI:
14496 case R_PPC64_TOC16_DS:
14497 case R_PPC64_TOC16_LO_DS:
14498 case R_PPC64_TOC16_HA:
6f20ed8a 14499 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14500 break;
14501
14502 /* Relocate against the beginning of the section. */
65f38f15
AM
14503 case R_PPC64_SECTOFF:
14504 case R_PPC64_SECTOFF_LO:
14505 case R_PPC64_SECTOFF_HI:
14506 case R_PPC64_SECTOFF_DS:
14507 case R_PPC64_SECTOFF_LO_DS:
14508 case R_PPC64_SECTOFF_HA:
4ce794b7 14509 if (sec != NULL)
65f38f15 14510 addend -= sec->output_section->vma;
5bd4f169
AM
14511 break;
14512
25f23106
AM
14513 case R_PPC64_REL16:
14514 case R_PPC64_REL16_LO:
14515 case R_PPC64_REL16_HI:
14516 case R_PPC64_REL16_HA:
a680de9a 14517 case R_PPC64_REL16DX_HA:
25f23106
AM
14518 break;
14519
721956f4
AM
14520 case R_PPC64_REL14:
14521 case R_PPC64_REL14_BRNTAKEN:
14522 case R_PPC64_REL14_BRTAKEN:
5d1634d7 14523 case R_PPC64_REL24:
05d0e962 14524 case R_PPC64_REL24_NOTOC:
5d1634d7
AM
14525 break;
14526
411e1bfb
AM
14527 case R_PPC64_TPREL16:
14528 case R_PPC64_TPREL16_LO:
14529 case R_PPC64_TPREL16_HI:
14530 case R_PPC64_TPREL16_HA:
14531 case R_PPC64_TPREL16_DS:
14532 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14533 case R_PPC64_TPREL16_HIGH:
14534 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14535 case R_PPC64_TPREL16_HIGHER:
14536 case R_PPC64_TPREL16_HIGHERA:
14537 case R_PPC64_TPREL16_HIGHEST:
14538 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14539 if (h != NULL
14540 && h->elf.root.type == bfd_link_hash_undefweak
14541 && h->elf.dynindx == -1)
14542 {
14543 /* Make this relocation against an undefined weak symbol
14544 resolve to zero. This is really just a tweak, since
14545 code using weak externs ought to check that they are
14546 defined before using them. */
14547 bfd_byte *p = contents + rel->r_offset - d_offset;
14548
95f0d0d2 14549 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
14550 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14551 if (insn != 0)
95f0d0d2 14552 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
14553 break;
14554 }
989f9879
AM
14555 if (htab->elf.tls_sec != NULL)
14556 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
14557 /* The TPREL16 relocs shouldn't really be used in shared
14558 libs or with non-local symbols as that will result in
14559 DT_TEXTREL being set, but support them anyway. */
14560 goto dodyn;
411e1bfb
AM
14561
14562 case R_PPC64_DTPREL16:
14563 case R_PPC64_DTPREL16_LO:
14564 case R_PPC64_DTPREL16_HI:
14565 case R_PPC64_DTPREL16_HA:
14566 case R_PPC64_DTPREL16_DS:
14567 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14568 case R_PPC64_DTPREL16_HIGH:
14569 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14570 case R_PPC64_DTPREL16_HIGHER:
14571 case R_PPC64_DTPREL16_HIGHERA:
14572 case R_PPC64_DTPREL16_HIGHEST:
14573 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14574 if (htab->elf.tls_sec != NULL)
14575 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14576 break;
14577
45965137
AM
14578 case R_PPC64_ADDR64_LOCAL:
14579 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14580 ? h->elf.other
14581 : sym->st_other);
14582 break;
14583
e515b051
AM
14584 case R_PPC64_DTPMOD64:
14585 relocation = 1;
14586 addend = 0;
14587 goto dodyn;
14588
411e1bfb 14589 case R_PPC64_TPREL64:
989f9879
AM
14590 if (htab->elf.tls_sec != NULL)
14591 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14592 goto dodyn;
14593
14594 case R_PPC64_DTPREL64:
989f9879
AM
14595 if (htab->elf.tls_sec != NULL)
14596 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 14597 /* Fall through. */
411e1bfb 14598
65f38f15
AM
14599 /* Relocations that may need to be propagated if this is a
14600 dynamic object. */
04c9666a 14601 case R_PPC64_REL30:
65f38f15
AM
14602 case R_PPC64_REL32:
14603 case R_PPC64_REL64:
14604 case R_PPC64_ADDR14:
14605 case R_PPC64_ADDR14_BRNTAKEN:
14606 case R_PPC64_ADDR14_BRTAKEN:
14607 case R_PPC64_ADDR16:
14608 case R_PPC64_ADDR16_DS:
14609 case R_PPC64_ADDR16_HA:
14610 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14611 case R_PPC64_ADDR16_HIGH:
14612 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14613 case R_PPC64_ADDR16_HIGHER:
14614 case R_PPC64_ADDR16_HIGHERA:
14615 case R_PPC64_ADDR16_HIGHEST:
14616 case R_PPC64_ADDR16_HIGHESTA:
14617 case R_PPC64_ADDR16_LO:
14618 case R_PPC64_ADDR16_LO_DS:
14619 case R_PPC64_ADDR24:
65f38f15
AM
14620 case R_PPC64_ADDR32:
14621 case R_PPC64_ADDR64:
14622 case R_PPC64_UADDR16:
14623 case R_PPC64_UADDR32:
14624 case R_PPC64_UADDR64:
411e1bfb 14625 dodyn:
5d1634d7 14626 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14627 break;
14628
41bd81ab
AM
14629 if (NO_OPD_RELOCS && is_opd)
14630 break;
14631
8a9e8e72 14632 if (bfd_link_pic (info)
b1b07054
AM
14633 ? ((h == NULL
14634 || h->dyn_relocs != NULL)
14635 && ((h != NULL && pc_dynrelocs (h))
14636 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
14637 : (h != NULL
14638 ? h->dyn_relocs != NULL
d311bc8b 14639 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 14640 {
b34976b6 14641 bfd_boolean skip, relocate;
65f38f15 14642 asection *sreloc;
1cf1f670 14643 bfd_vma out_off;
82e66161 14644 long indx = 0;
65f38f15
AM
14645
14646 /* When generating a dynamic object, these relocations
14647 are copied into the output file to be resolved at run
14648 time. */
14649
b34976b6
AM
14650 skip = FALSE;
14651 relocate = FALSE;
65f38f15 14652
1cf1f670
AM
14653 out_off = _bfd_elf_section_offset (output_bfd, info,
14654 input_section, rel->r_offset);
14655 if (out_off == (bfd_vma) -1)
b34976b6 14656 skip = TRUE;
1cf1f670 14657 else if (out_off == (bfd_vma) -2)
b34976b6 14658 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14659 out_off += (input_section->output_section->vma
14660 + input_section->output_offset);
14661 outrel.r_offset = out_off;
411e1bfb 14662 outrel.r_addend = rel->r_addend;
65f38f15 14663
1cf1f670
AM
14664 /* Optimize unaligned reloc use. */
14665 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14666 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14667 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14668 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14669 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14670 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14671 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14672 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14673 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14674
65f38f15 14675 if (skip)
0bb2d96a 14676 memset (&outrel, 0, sizeof outrel);
afe397ea 14677 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14678 && !is_opd
14679 && r_type != R_PPC64_TOC)
14acf4dc 14680 {
82e66161
AM
14681 indx = h->elf.dynindx;
14682 BFD_ASSERT (indx != -1);
14683 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 14684 }
65f38f15
AM
14685 else
14686 {
41bd81ab
AM
14687 /* This symbol is local, or marked to become local,
14688 or this is an opd section reloc which must point
14689 at a local function. */
65f38f15 14690 outrel.r_addend += relocation;
e86ce104 14691 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14692 {
3fad3c7c 14693 if (is_opd && h != NULL)
afbe61cf
AM
14694 {
14695 /* Lie about opd entries. This case occurs
14696 when building shared libraries and we
14697 reference a function in another shared
3fad3c7c
AM
14698 lib. The same thing happens for a weak
14699 definition in an application that's
14700 overridden by a strong definition in a
14701 shared lib. (I believe this is a generic
14702 bug in binutils handling of weak syms.)
14703 In these cases we won't use the opd
1e2f5b6e 14704 entry in this lib. */
b34976b6 14705 unresolved_reloc = FALSE;
afbe61cf 14706 }
25f23106
AM
14707 if (!is_opd
14708 && r_type == R_PPC64_ADDR64
14709 && (h != NULL
14710 ? h->elf.type == STT_GNU_IFUNC
14711 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14712 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14713 else
14714 {
14715 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14716
25f23106
AM
14717 /* We need to relocate .opd contents for ld.so.
14718 Prelink also wants simple and consistent rules
14719 for relocs. This make all RELATIVE relocs have
14720 *r_offset equal to r_addend. */
14721 relocate = TRUE;
14722 }
65f38f15
AM
14723 }
14724 else
14725 {
25f23106
AM
14726 if (h != NULL
14727 ? h->elf.type == STT_GNU_IFUNC
14728 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14729 {
25f53a85 14730 info->callbacks->einfo
695344c0 14731 /* xgettext:c-format */
174d0a74 14732 (_("%H: %s for indirect "
c1c8c1ef 14733 "function `%pT' unsupported\n"),
25f53a85 14734 input_bfd, input_section, rel->r_offset,
25f23106
AM
14735 ppc64_elf_howto_table[r_type]->name,
14736 sym_name);
14737 ret = FALSE;
14738 }
cf35638d 14739 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14740 ;
14741 else if (sec == NULL || sec->owner == NULL)
14742 {
14743 bfd_set_error (bfd_error_bad_value);
b34976b6 14744 return FALSE;
65f38f15
AM
14745 }
14746 else
14747 {
14748 asection *osec;
14749
14750 osec = sec->output_section;
14751 indx = elf_section_data (osec)->dynindx;
14752
74541ad4
AM
14753 if (indx == 0)
14754 {
14755 if ((osec->flags & SEC_READONLY) == 0
14756 && htab->elf.data_index_section != NULL)
14757 osec = htab->elf.data_index_section;
14758 else
14759 osec = htab->elf.text_index_section;
14760 indx = elf_section_data (osec)->dynindx;
14761 }
14762 BFD_ASSERT (indx != 0);
14763
65f38f15
AM
14764 /* We are turning this relocation into one
14765 against a section symbol, so subtract out
14766 the output section's address but not the
14767 offset of the input section in the output
14768 section. */
14769 outrel.r_addend -= osec->vma;
14770 }
14771
14772 outrel.r_info = ELF64_R_INFO (indx, r_type);
14773 }
14774 }
14775
14776 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14777 if (h != NULL
14778 ? h->elf.type == STT_GNU_IFUNC
14779 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
14780 {
14781 sreloc = htab->elf.irelplt;
14782 if (indx == 0)
14783 htab->local_ifunc_resolver = 1;
14784 else if (is_static_defined (&h->elf))
14785 htab->maybe_local_ifunc_resolver = 1;
14786 }
65f38f15
AM
14787 if (sreloc == NULL)
14788 abort ();
14789
dfbb6ac9
AM
14790 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14791 >= sreloc->size)
14792 abort ();
947216bf
AM
14793 loc = sreloc->contents;
14794 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14795 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14796
14797 /* If this reloc is against an external symbol, it will
14798 be computed at runtime, so there's no need to do
81407a69
AM
14799 anything now. However, for the sake of prelink ensure
14800 that the section contents are a known value. */
2cdcc330 14801 if (!relocate)
81407a69
AM
14802 {
14803 unresolved_reloc = FALSE;
14804 /* The value chosen here is quite arbitrary as ld.so
14805 ignores section contents except for the special
14806 case of .opd where the contents might be accessed
14807 before relocation. Choose zero, as that won't
14808 cause reloc overflow. */
14809 relocation = 0;
14810 addend = 0;
14811 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14812 to improve backward compatibility with older
14813 versions of ld. */
14814 if (r_type == R_PPC64_ADDR64)
14815 addend = outrel.r_addend;
14816 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 14817 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 14818 addend = outrel.r_offset;
81407a69 14819 }
65f38f15 14820 }
5bd4f169
AM
14821 break;
14822
65f38f15
AM
14823 case R_PPC64_COPY:
14824 case R_PPC64_GLOB_DAT:
14825 case R_PPC64_JMP_SLOT:
25f23106 14826 case R_PPC64_JMP_IREL:
65f38f15
AM
14827 case R_PPC64_RELATIVE:
14828 /* We shouldn't ever see these dynamic relocs in relocatable
14829 files. */
ae9a127f 14830 /* Fall through. */
65f38f15
AM
14831
14832 case R_PPC64_PLTGOT16:
14833 case R_PPC64_PLTGOT16_DS:
14834 case R_PPC64_PLTGOT16_HA:
14835 case R_PPC64_PLTGOT16_HI:
14836 case R_PPC64_PLTGOT16_LO:
14837 case R_PPC64_PLTGOT16_LO_DS:
14838 case R_PPC64_PLTREL32:
14839 case R_PPC64_PLTREL64:
14840 /* These ones haven't been implemented yet. */
14841
25f53a85 14842 info->callbacks->einfo
695344c0 14843 /* xgettext:c-format */
c1c8c1ef 14844 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 14845 input_bfd,
4ce794b7 14846 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
14847
14848 bfd_set_error (bfd_error_invalid_operation);
b34976b6 14849 ret = FALSE;
c316a17c 14850 goto copy_reloc;
65f38f15 14851 }
5bd4f169 14852
67f0cbdb
AM
14853 /* Multi-instruction sequences that access the TOC can be
14854 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 14855 to nop; addi rb,r2,x; */
67f0cbdb
AM
14856 switch (r_type)
14857 {
14858 default:
14859 break;
14860
14861 case R_PPC64_GOT_TLSLD16_HI:
14862 case R_PPC64_GOT_TLSGD16_HI:
14863 case R_PPC64_GOT_TPREL16_HI:
14864 case R_PPC64_GOT_DTPREL16_HI:
14865 case R_PPC64_GOT16_HI:
14866 case R_PPC64_TOC16_HI:
14867 /* These relocs would only be useful if building up an
14868 offset to later add to r2, perhaps in an indexed
14869 addressing mode instruction. Don't try to optimize.
14870 Unfortunately, the possibility of someone building up an
14871 offset like this or even with the HA relocs, means that
14872 we need to check the high insn when optimizing the low
14873 insn. */
14874 break;
14875
23cedd1d
AM
14876 case R_PPC64_PLTCALL:
14877 if (unresolved_reloc)
14878 {
14879 /* No plt entry. Make this into a direct call. */
14880 bfd_byte *p = contents + rel->r_offset;
14881 insn = bfd_get_32 (input_bfd, p);
14882 insn &= 1;
14883 bfd_put_32 (input_bfd, B_DOT | insn, p);
14884 bfd_put_32 (input_bfd, NOP, p + 4);
14885 unresolved_reloc = save_unresolved_reloc;
14886 r_type = R_PPC64_REL24;
14887 }
14888 break;
14889
14890 case R_PPC64_PLTSEQ:
14891 if (unresolved_reloc)
14892 {
14893 unresolved_reloc = FALSE;
14894 goto nop_it;
14895 }
14896 break;
14897
14898 case R_PPC64_PLT16_HA:
14899 if (unresolved_reloc)
14900 {
14901 unresolved_reloc = FALSE;
14902 goto nop_it;
14903 }
14904 /* Fall through. */
67f0cbdb
AM
14905 case R_PPC64_GOT_TLSLD16_HA:
14906 case R_PPC64_GOT_TLSGD16_HA:
14907 case R_PPC64_GOT_TPREL16_HA:
14908 case R_PPC64_GOT_DTPREL16_HA:
14909 case R_PPC64_GOT16_HA:
14910 case R_PPC64_TOC16_HA:
98528052 14911 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14912 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 14913 {
23cedd1d
AM
14914 bfd_byte *p;
14915 nop_it:
14916 p = contents + (rel->r_offset & ~3);
98528052 14917 bfd_put_32 (input_bfd, NOP, p);
d830549d 14918 goto copy_reloc;
98528052 14919 }
67f0cbdb
AM
14920 break;
14921
23cedd1d
AM
14922 case R_PPC64_PLT16_LO:
14923 case R_PPC64_PLT16_LO_DS:
14924 if (unresolved_reloc)
14925 {
14926 unresolved_reloc = FALSE;
14927 goto nop_it;
14928 }
14929 /* Fall through. */
67f0cbdb
AM
14930 case R_PPC64_GOT_TLSLD16_LO:
14931 case R_PPC64_GOT_TLSGD16_LO:
14932 case R_PPC64_GOT_TPREL16_LO_DS:
14933 case R_PPC64_GOT_DTPREL16_LO_DS:
14934 case R_PPC64_GOT16_LO:
14935 case R_PPC64_GOT16_LO_DS:
14936 case R_PPC64_TOC16_LO:
14937 case R_PPC64_TOC16_LO_DS:
98528052 14938 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14939 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
14940 {
14941 bfd_byte *p = contents + (rel->r_offset & ~3);
14942 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
14943 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14944 {
14945 /* Transform addic to addi when we change reg. */
14946 insn &= ~((0x3f << 26) | (0x1f << 16));
14947 insn |= (14u << 26) | (2 << 16);
14948 }
14949 else
67f0cbdb 14950 {
98528052
AM
14951 insn &= ~(0x1f << 16);
14952 insn |= 2 << 16;
67f0cbdb 14953 }
560c8763 14954 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
14955 }
14956 break;
9a23f96e
AM
14957
14958 case R_PPC64_TPREL16_HA:
14959 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
14960 {
14961 bfd_byte *p = contents + (rel->r_offset & ~3);
14962 insn = bfd_get_32 (input_bfd, p);
14963 if ((insn & ((0x3f << 26) | 0x1f << 16))
14964 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
14965 /* xgettext:c-format */
14966 info->callbacks->minfo
14967 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
14968 input_bfd, input_section, rel->r_offset,
14969 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 14970 else
d830549d
AM
14971 {
14972 bfd_put_32 (input_bfd, NOP, p);
14973 goto copy_reloc;
14974 }
9a23f96e
AM
14975 }
14976 break;
14977
14978 case R_PPC64_TPREL16_LO:
14979 case R_PPC64_TPREL16_LO_DS:
14980 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
14981 {
14982 bfd_byte *p = contents + (rel->r_offset & ~3);
14983 insn = bfd_get_32 (input_bfd, p);
14984 insn &= ~(0x1f << 16);
14985 insn |= 13 << 16;
14986 bfd_put_32 (input_bfd, insn, p);
14987 }
14988 break;
67f0cbdb
AM
14989 }
14990
65f38f15
AM
14991 /* Do any further special processing. */
14992 switch (r_type)
14993 {
14994 default:
14995 break;
14996
25f23106 14997 case R_PPC64_REL16_HA:
a680de9a 14998 case R_PPC64_REL16DX_HA:
f9c6b907
AM
14999 case R_PPC64_ADDR16_HA:
15000 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15001 case R_PPC64_ADDR16_HIGHERA:
15002 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15003 case R_PPC64_TOC16_HA:
15004 case R_PPC64_SECTOFF_HA:
411e1bfb 15005 case R_PPC64_TPREL16_HA:
f9c6b907 15006 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15007 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15008 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15009 case R_PPC64_DTPREL16_HA:
15010 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15011 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15012 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15013 /* It's just possible that this symbol is a weak symbol
15014 that's not actually defined anywhere. In that case,
15015 'sec' would be NULL, and we should leave the symbol
15016 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15017 if (sec == NULL)
15018 break;
1a0670f3 15019 /* Fall through. */
5c5f6e17
AM
15020
15021 case R_PPC64_GOT16_HA:
15022 case R_PPC64_PLTGOT16_HA:
15023 case R_PPC64_PLT16_HA:
15024 case R_PPC64_GOT_TLSGD16_HA:
15025 case R_PPC64_GOT_TLSLD16_HA:
15026 case R_PPC64_GOT_TPREL16_HA:
15027 case R_PPC64_GOT_DTPREL16_HA:
15028 /* Add 0x10000 if sign bit in 0:15 is set.
15029 Bits 0:15 are not used. */
15030 addend += 0x8000;
65f38f15
AM
15031 break;
15032
15033 case R_PPC64_ADDR16_DS:
15034 case R_PPC64_ADDR16_LO_DS:
15035 case R_PPC64_GOT16_DS:
15036 case R_PPC64_GOT16_LO_DS:
15037 case R_PPC64_PLT16_LO_DS:
15038 case R_PPC64_SECTOFF_DS:
15039 case R_PPC64_SECTOFF_LO_DS:
15040 case R_PPC64_TOC16_DS:
15041 case R_PPC64_TOC16_LO_DS:
15042 case R_PPC64_PLTGOT16_DS:
15043 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15044 case R_PPC64_GOT_TPREL16_DS:
15045 case R_PPC64_GOT_TPREL16_LO_DS:
15046 case R_PPC64_GOT_DTPREL16_DS:
15047 case R_PPC64_GOT_DTPREL16_LO_DS:
15048 case R_PPC64_TPREL16_DS:
15049 case R_PPC64_TPREL16_LO_DS:
15050 case R_PPC64_DTPREL16_DS:
15051 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15052 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15053 mask = 3;
a680de9a
PB
15054 /* If this reloc is against an lq, lxv, or stxv insn, then
15055 the value must be a multiple of 16. This is somewhat of
15056 a hack, but the "correct" way to do this by defining _DQ
15057 forms of all the _DS relocs bloats all reloc switches in
15058 this file. It doesn't make much sense to use these
15059 relocs in data, so testing the insn should be safe. */
15060 if ((insn & (0x3f << 26)) == (56u << 26)
15061 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15062 mask = 15;
a680de9a
PB
15063 relocation += addend;
15064 addend = insn & (mask ^ 3);
15065 if ((relocation & mask) != 0)
65f38f15 15066 {
a680de9a 15067 relocation ^= relocation & mask;
25f53a85 15068 info->callbacks->einfo
695344c0 15069 /* xgettext:c-format */
174d0a74 15070 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 15071 input_bfd, input_section, rel->r_offset,
d830549d 15072 ppc64_elf_howto_table[r_type]->name,
adadcc0c 15073 mask + 1);
65f38f15 15074 bfd_set_error (bfd_error_bad_value);
b34976b6 15075 ret = FALSE;
c316a17c 15076 goto copy_reloc;
65f38f15
AM
15077 }
15078 break;
5bd4f169
AM
15079 }
15080
239e1f3a
AM
15081 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15082 because such sections are not SEC_ALLOC and thus ld.so will
15083 not process them. */
d830549d 15084 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 15085 if (unresolved_reloc
239e1f3a 15086 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15087 && h->elf.def_dynamic)
15088 && _bfd_elf_section_offset (output_bfd, info, input_section,
15089 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15090 {
25f53a85 15091 info->callbacks->einfo
695344c0 15092 /* xgettext:c-format */
c1c8c1ef 15093 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 15094 input_bfd, input_section, rel->r_offset,
b80eed39 15095 howto->name,
039b3fef 15096 h->elf.root.root.string);
b34976b6 15097 ret = FALSE;
9c07fe7c 15098 }
5bd4f169 15099
b80eed39
AM
15100 /* 16-bit fields in insns mostly have signed values, but a
15101 few insns have 16-bit unsigned values. Really, we should
15102 have different reloc types. */
15103 if (howto->complain_on_overflow != complain_overflow_dont
15104 && howto->dst_mask == 0xffff
15105 && (input_section->flags & SEC_CODE) != 0)
15106 {
15107 enum complain_overflow complain = complain_overflow_signed;
15108
15109 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15110 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15111 complain = complain_overflow_bitfield;
15112 else if (howto->rightshift == 0
15113 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15114 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15115 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15116 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15117 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15118 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15119 complain = complain_overflow_unsigned;
15120 if (howto->complain_on_overflow != complain)
15121 {
15122 alt_howto = *howto;
15123 alt_howto.complain_on_overflow = complain;
15124 howto = &alt_howto;
15125 }
15126 }
15127
a680de9a
PB
15128 if (r_type == R_PPC64_REL16DX_HA)
15129 {
15130 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15131 if (rel->r_offset + 4 > input_section->size)
15132 r = bfd_reloc_outofrange;
15133 else
15134 {
15135 relocation += addend;
15136 relocation -= (rel->r_offset
15137 + input_section->output_offset
15138 + input_section->output_section->vma);
3de43e7b 15139 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
15140 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15141 insn &= ~0x1fffc1;
3de43e7b 15142 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
15143 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15144 r = bfd_reloc_ok;
3de43e7b 15145 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
15146 r = bfd_reloc_overflow;
15147 }
15148 }
15149 else
15150 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15151 rel->r_offset, relocation, addend);
5bd4f169 15152
ef60b7ff 15153 if (r != bfd_reloc_ok)
5bd4f169 15154 {
bc30df16 15155 char *more_info = NULL;
b80eed39 15156 const char *reloc_name = howto->name;
bc30df16
AM
15157
15158 if (reloc_dest != DEST_NORMAL)
15159 {
15160 more_info = bfd_malloc (strlen (reloc_name) + 8);
15161 if (more_info != NULL)
15162 {
15163 strcpy (more_info, reloc_name);
15164 strcat (more_info, (reloc_dest == DEST_OPD
15165 ? " (OPD)" : " (stub)"));
15166 reloc_name = more_info;
15167 }
15168 }
15169
cd27b276 15170 if (r == bfd_reloc_overflow)
5bd4f169 15171 {
8131c122
AM
15172 /* On code like "if (foo) foo();" don't report overflow
15173 on a branch to zero when foo is undefined. */
15174 if (!warned
15175 && (reloc_dest == DEST_STUB
15176 || !(h != NULL
15177 && (h->elf.root.type == bfd_link_hash_undefweak
15178 || h->elf.root.type == bfd_link_hash_undefined)
15179 && is_branch_reloc (r_type))))
1a72702b
AM
15180 info->callbacks->reloc_overflow (info, &h->elf.root,
15181 sym_name, reloc_name,
15182 orig_rel.r_addend,
15183 input_bfd, input_section,
15184 rel->r_offset);
ef60b7ff
AM
15185 }
15186 else
15187 {
25f53a85 15188 info->callbacks->einfo
695344c0 15189 /* xgettext:c-format */
c1c8c1ef 15190 (_("%H: %s against `%pT': error %d\n"),
25f53a85 15191 input_bfd, input_section, rel->r_offset,
bc30df16 15192 reloc_name, sym_name, (int) r);
b34976b6 15193 ret = FALSE;
ef60b7ff 15194 }
bc30df16
AM
15195 if (more_info != NULL)
15196 free (more_info);
5bd4f169 15197 }
c316a17c
AM
15198 copy_reloc:
15199 if (wrel != rel)
15200 *wrel = *rel;
15201 }
15202
15203 if (wrel != rel)
15204 {
15205 Elf_Internal_Shdr *rel_hdr;
15206 size_t deleted = rel - wrel;
15207
15208 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15209 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15210 if (rel_hdr->sh_size == 0)
15211 {
15212 /* It is too late to remove an empty reloc section. Leave
15213 one NONE reloc.
15214 ??? What is wrong with an empty section??? */
15215 rel_hdr->sh_size = rel_hdr->sh_entsize;
15216 deleted -= 1;
15217 }
15218 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15219 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15220 input_section->reloc_count -= deleted;
5bd4f169
AM
15221 }
15222
645ea6a9
AM
15223 /* If we're emitting relocations, then shortly after this function
15224 returns, reloc offsets and addends for this section will be
15225 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15226 file rather than the input. Save a copy of the relocs for
15227 opd_entry_value. */
0e1862bb 15228 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15229 {
15230 bfd_size_type amt;
15231 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15232 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15233 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15234 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15235 if (rel == NULL)
15236 return FALSE;
15237 memcpy (rel, relocs, amt);
15238 }
5bd4f169
AM
15239 return ret;
15240}
15241
754021d0
AM
15242/* Adjust the value of any local symbols in opd sections. */
15243
6e0b88f1 15244static int
754021d0
AM
15245ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15246 const char *name ATTRIBUTE_UNUSED,
15247 Elf_Internal_Sym *elfsym,
15248 asection *input_sec,
15249 struct elf_link_hash_entry *h)
15250{
74f0fb50
AM
15251 struct _opd_sec_data *opd;
15252 long adjust;
754021d0
AM
15253 bfd_vma value;
15254
4025353c 15255 if (h != NULL)
6e0b88f1 15256 return 1;
4025353c 15257
74f0fb50
AM
15258 opd = get_opd_info (input_sec);
15259 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15260 return 1;
754021d0
AM
15261
15262 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15263 if (!bfd_link_relocatable (info))
754021d0
AM
15264 value -= input_sec->output_section->vma;
15265
51aecdc5 15266 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15267 if (adjust == -1)
6e0b88f1
AM
15268 return 2;
15269
15270 elfsym->st_value += adjust;
15271 return 1;
754021d0
AM
15272}
15273
5bd4f169
AM
15274/* Finish up dynamic symbol handling. We set the contents of various
15275 dynamic sections here. */
15276
b34976b6 15277static bfd_boolean
4ce794b7
AM
15278ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15279 struct bfd_link_info *info,
15280 struct elf_link_hash_entry *h,
4aef7643 15281 Elf_Internal_Sym *sym)
5bd4f169 15282{
65f38f15 15283 struct ppc_link_hash_table *htab;
8387904d 15284 struct plt_entry *ent;
5bd4f169 15285
65f38f15 15286 htab = ppc_hash_table (info);
4dfe6ac6
NC
15287 if (htab == NULL)
15288 return FALSE;
5bd4f169 15289
49c09209
AM
15290 if (!htab->opd_abi && !h->def_regular)
15291 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15292 if (ent->plt.offset != (bfd_vma) -1)
15293 {
15294 /* Mark the symbol as undefined, rather than as
15295 defined in glink. Leave the value if there were
15296 any relocations where pointer equality matters
15297 (this is a clue for the dynamic linker, to make
15298 function pointer comparisons work between an
15299 application and shared library), otherwise set it
15300 to zero. */
15301 sym->st_shndx = SHN_UNDEF;
15302 if (!h->pointer_equality_needed)
15303 sym->st_value = 0;
15304 else if (!h->ref_regular_nonweak)
15305 {
15306 /* This breaks function pointer comparisons, but
15307 that is better than breaking tests for a NULL
15308 function pointer. */
15309 sym->st_value = 0;
15310 }
15311 break;
15312 }
5bd4f169 15313
f5385ebf 15314 if (h->needs_copy)
5bd4f169 15315 {
65f38f15 15316 /* This symbol needs a copy reloc. Set it up. */
49c09209 15317 Elf_Internal_Rela rela;
5474d94f 15318 asection *srel;
49c09209 15319 bfd_byte *loc;
5bd4f169 15320
65f38f15
AM
15321 if (h->dynindx == -1
15322 || (h->root.type != bfd_link_hash_defined
15323 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
15324 || htab->elf.srelbss == NULL
15325 || htab->elf.sreldynrelro == NULL)
65f38f15 15326 abort ();
5bd4f169
AM
15327
15328 rela.r_offset = (h->root.u.def.value
15329 + h->root.u.def.section->output_section->vma
15330 + h->root.u.def.section->output_offset);
15331 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15332 rela.r_addend = 0;
afbf7e8e 15333 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
15334 srel = htab->elf.sreldynrelro;
15335 else
15336 srel = htab->elf.srelbss;
15337 loc = srel->contents;
15338 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15339 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15340 }
15341
b34976b6 15342 return TRUE;
5bd4f169
AM
15343}
15344
65f38f15
AM
15345/* Used to decide how to sort relocs in an optimal manner for the
15346 dynamic linker, before writing them out. */
15347
15348static enum elf_reloc_type_class
7e612e98
AM
15349ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15350 const asection *rel_sec,
15351 const Elf_Internal_Rela *rela)
65f38f15 15352{
04c9666a 15353 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15354 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15355
33e44f2e 15356 if (rel_sec == htab->elf.irelplt)
7e612e98 15357 return reloc_class_ifunc;
a33d1f77 15358
4ce794b7 15359 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15360 switch (r_type)
65f38f15
AM
15361 {
15362 case R_PPC64_RELATIVE:
15363 return reloc_class_relative;
15364 case R_PPC64_JMP_SLOT:
15365 return reloc_class_plt;
15366 case R_PPC64_COPY:
15367 return reloc_class_copy;
15368 default:
15369 return reloc_class_normal;
15370 }
15371}
15372
5bd4f169
AM
15373/* Finish up the dynamic sections. */
15374
b34976b6 15375static bfd_boolean
4ce794b7
AM
15376ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15377 struct bfd_link_info *info)
5bd4f169 15378{
65f38f15
AM
15379 struct ppc_link_hash_table *htab;
15380 bfd *dynobj;
5bd4f169 15381 asection *sdyn;
5bd4f169 15382
65f38f15 15383 htab = ppc_hash_table (info);
4dfe6ac6
NC
15384 if (htab == NULL)
15385 return FALSE;
15386
65f38f15 15387 dynobj = htab->elf.dynobj;
3d4d4302 15388 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15389
65f38f15 15390 if (htab->elf.dynamic_sections_created)
5bd4f169 15391 {
5bd4f169
AM
15392 Elf64_External_Dyn *dyncon, *dynconend;
15393
33e44f2e 15394 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15395 abort ();
5bd4f169
AM
15396
15397 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15398 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15399 for (; dyncon < dynconend; dyncon++)
15400 {
15401 Elf_Internal_Dyn dyn;
19397422 15402 asection *s;
5bd4f169
AM
15403
15404 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15405
15406 switch (dyn.d_tag)
15407 {
65f38f15
AM
15408 default:
15409 continue;
5bd4f169 15410
5d1634d7 15411 case DT_PPC64_GLINK:
4ce794b7 15412 s = htab->glink;
6348e046 15413 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15414 /* We stupidly defined DT_PPC64_GLINK to be the start
15415 of glink rather than the first entry point, which is
15416 what ld.so needs, and now have a bigger stub to
15417 support automatic multiple TOCs. */
9e390558 15418 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
15419 break;
15420
19397422
AM
15421 case DT_PPC64_OPD:
15422 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15423 if (s == NULL)
15424 continue;
15425 dyn.d_un.d_ptr = s->vma;
19397422
AM
15426 break;
15427
e8910a83
AM
15428 case DT_PPC64_OPT:
15429 if (htab->do_multi_toc && htab->multi_toc_needed)
15430 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
15431 if (htab->has_plt_localentry0)
15432 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
15433 break;
15434
19397422
AM
15435 case DT_PPC64_OPDSZ:
15436 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15437 if (s == NULL)
15438 continue;
eea6121a 15439 dyn.d_un.d_val = s->size;
19397422
AM
15440 break;
15441
65f38f15 15442 case DT_PLTGOT:
33e44f2e 15443 s = htab->elf.splt;
6348e046 15444 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15445 break;
15446
15447 case DT_JMPREL:
33e44f2e 15448 s = htab->elf.srelplt;
6348e046 15449 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15450 break;
5bd4f169 15451
65f38f15 15452 case DT_PLTRELSZ:
33e44f2e 15453 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 15454 break;
82e66161
AM
15455
15456 case DT_TEXTREL:
15457 if (htab->local_ifunc_resolver)
15458 info->callbacks->einfo
15459 (_("%X%P: text relocations and GNU indirect "
15460 "functions will result in a segfault at runtime\n"));
15461 else if (htab->maybe_local_ifunc_resolver)
15462 info->callbacks->einfo
15463 (_("%P: warning: text relocations and GNU indirect "
15464 "functions may result in a segfault at runtime\n"));
15465 continue;
5bd4f169 15466 }
5bd4f169 15467
65f38f15 15468 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15469 }
5bd4f169
AM
15470 }
15471
6528b6eb
AM
15472 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15473 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15474 {
15475 /* Fill in the first entry in the global offset table.
15476 We use it to hold the link-time TOCbase. */
15477 bfd_put_64 (output_bfd,
60ee0d4a 15478 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15479 htab->elf.sgot->contents);
5d1634d7
AM
15480
15481 /* Set .got entry size. */
2cdcc330
AM
15482 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
15483 = 8;
5d1634d7
AM
15484 }
15485
6528b6eb
AM
15486 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15487 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15488 {
15489 /* Set .plt entry size. */
33e44f2e 15490 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15491 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15492 }
15493
84f5d08e
AM
15494 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15495 brlt ourselves if emitrelocations. */
15496 if (htab->brlt != NULL
15497 && htab->brlt->reloc_count != 0
15498 && !_bfd_elf_link_output_relocs (output_bfd,
15499 htab->brlt,
d4730f92 15500 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15501 elf_section_data (htab->brlt)->relocs,
15502 NULL))
15503 return FALSE;
15504
176a0d42
AM
15505 if (htab->glink != NULL
15506 && htab->glink->reloc_count != 0
15507 && !_bfd_elf_link_output_relocs (output_bfd,
15508 htab->glink,
d4730f92 15509 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15510 elf_section_data (htab->glink)->relocs,
15511 NULL))
15512 return FALSE;
15513
da44f4e5 15514
df136d64
AM
15515 if (htab->glink_eh_frame != NULL
15516 && htab->glink_eh_frame->size != 0
15517 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15518 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15519 htab->glink_eh_frame,
15520 htab->glink_eh_frame->contents))
15521 return FALSE;
58d180e8 15522
e717da7e 15523 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15524 since we didn't add them to DYNOBJ. We know dynobj is the first
15525 bfd. */
c72f2fb2 15526 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15527 {
15528 asection *s;
7b53ace3 15529
0c8d6e5c 15530 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15531 continue;
15532
e717da7e
AM
15533 s = ppc64_elf_tdata (dynobj)->got;
15534 if (s != NULL
eea6121a 15535 && s->size != 0
e717da7e
AM
15536 && s->output_section != bfd_abs_section_ptr
15537 && !bfd_set_section_contents (output_bfd, s->output_section,
15538 s->contents, s->output_offset,
eea6121a 15539 s->size))
e717da7e
AM
15540 return FALSE;
15541 s = ppc64_elf_tdata (dynobj)->relgot;
15542 if (s != NULL
eea6121a 15543 && s->size != 0
e717da7e
AM
15544 && s->output_section != bfd_abs_section_ptr
15545 && !bfd_set_section_contents (output_bfd, s->output_section,
15546 s->contents, s->output_offset,
eea6121a 15547 s->size))
e717da7e
AM
15548 return FALSE;
15549 }
f6c52c13 15550
b34976b6 15551 return TRUE;
5bd4f169
AM
15552}
15553
5bd4f169 15554#include "elf64-target.h"
7b8e7dad
AM
15555
15556/* FreeBSD support */
15557
15558#undef TARGET_LITTLE_SYM
15559#undef TARGET_LITTLE_NAME
15560
15561#undef TARGET_BIG_SYM
6d00b590 15562#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15563#undef TARGET_BIG_NAME
15564#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15565
15566#undef ELF_OSABI
15567#define ELF_OSABI ELFOSABI_FREEBSD
15568
15569#undef elf64_bed
15570#define elf64_bed elf64_powerpc_fbsd_bed
15571
15572#include "elf64-target.h"