[GOLD] PowerPC .gnu.attributes support
[deliverable/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
AM
166#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
167#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
168#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
169
170#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
171#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
172#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
173#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
174#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
175#define BNECTR 0x4ca20420 /* bnectr+ */
176#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 177
71a39c98 178#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 179#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 180#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 181
07d6d2b8
AM
182#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
183#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
184#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 185
07d6d2b8 186#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
006589cf
AM
187#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
188#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
a345bc8d 189#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
07d6d2b8 190#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
a345bc8d 191
9e390558
AM
192/* __glink_PLTresolve stub instructions. We enter with the index in R0. */
193#define GLINK_PLTRESOLVE_SIZE(htab) \
407aa07c 194 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
ee4bf8d2
AM
195 /* 0: */
196 /* .quad plt0-1f */
197 /* __glink: */
198#define MFLR_R12 0x7d8802a6 /* mflr %12 */
199#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
200 /* 1: */
201#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 202 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 203#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
204#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
205 /* ld %12,0(%11) */
206 /* ld %2,8(%11) */
207 /* mtctr %12 */
208 /* ld %11,16(%11) */
ee4bf8d2 209 /* bctr */
b9e5796b
AM
210#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
211#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
212#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
213#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
214#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
215
216/* Pad with this. */
217#define NOP 0x60000000
218
721956f4
AM
219/* Some other nops. */
220#define CROR_151515 0x4def7b82
221#define CROR_313131 0x4ffffb82
222
cedb70c5 223/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
224#define LI_R0_0 0x38000000 /* li %r0,0 */
225#define B_DOT 0x48000000 /* b . */
226
227/* After that, we need two instructions to load the index, followed by
228 a branch. */
229#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 230#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 231
deb0e272
AM
232/* Instructions used by the save and restore reg functions. */
233#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
234#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
235#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
236#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
237#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
238#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
239#define LI_R12_0 0x39800000 /* li %r12,0 */
240#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
241#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
242#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
243#define BLR 0x4e800020 /* blr */
244
41bd81ab
AM
245/* Since .opd is an array of descriptors and each entry will end up
246 with identical R_PPC64_RELATIVE relocs, there is really no need to
247 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 248 relocate .opd without reloc entries. */
41bd81ab
AM
249#ifndef NO_OPD_RELOCS
250#define NO_OPD_RELOCS 0
251#endif
810d4e75 252
a4b6fadd
AM
253#ifndef ARRAY_SIZE
254#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
255#endif
256
810d4e75
AM
257static inline int
258abiversion (bfd *abfd)
259{
260 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
261}
262
263static inline void
264set_abiversion (bfd *abfd, int ver)
265{
266 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
267 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
268}
5bd4f169 269\f
f5e87a1d 270#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 271
5bd4f169 272/* Relocation HOWTO's. */
04c9666a 273static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 274
f3185997
NC
275static reloc_howto_type ppc64_elf_howto_raw[] =
276{
5bd4f169
AM
277 /* This reloc does nothing. */
278 HOWTO (R_PPC64_NONE, /* type */
279 0, /* rightshift */
6346d5ca
AM
280 3, /* size (0 = byte, 1 = short, 2 = long) */
281 0, /* bitsize */
b34976b6 282 FALSE, /* pc_relative */
5bd4f169 283 0, /* bitpos */
f5e87a1d 284 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
285 bfd_elf_generic_reloc, /* special_function */
286 "R_PPC64_NONE", /* name */
b34976b6 287 FALSE, /* partial_inplace */
d006db6c 288 0, /* src_mask */
5bd4f169 289 0, /* dst_mask */
b34976b6 290 FALSE), /* pcrel_offset */
5bd4f169
AM
291
292 /* A standard 32 bit relocation. */
293 HOWTO (R_PPC64_ADDR32, /* type */
294 0, /* rightshift */
295 2, /* size (0 = byte, 1 = short, 2 = long) */
296 32, /* bitsize */
b34976b6 297 FALSE, /* pc_relative */
5bd4f169
AM
298 0, /* bitpos */
299 complain_overflow_bitfield, /* complain_on_overflow */
300 bfd_elf_generic_reloc, /* special_function */
301 "R_PPC64_ADDR32", /* name */
b34976b6 302 FALSE, /* partial_inplace */
5bd4f169
AM
303 0, /* src_mask */
304 0xffffffff, /* dst_mask */
b34976b6 305 FALSE), /* pcrel_offset */
5bd4f169
AM
306
307 /* An absolute 26 bit branch; the lower two bits must be zero.
308 FIXME: we don't check that, we just clear them. */
309 HOWTO (R_PPC64_ADDR24, /* type */
310 0, /* rightshift */
311 2, /* size (0 = byte, 1 = short, 2 = long) */
312 26, /* bitsize */
b34976b6 313 FALSE, /* pc_relative */
5bd4f169
AM
314 0, /* bitpos */
315 complain_overflow_bitfield, /* complain_on_overflow */
316 bfd_elf_generic_reloc, /* special_function */
317 "R_PPC64_ADDR24", /* name */
b34976b6 318 FALSE, /* partial_inplace */
d006db6c 319 0, /* src_mask */
f5e87a1d 320 0x03fffffc, /* dst_mask */
b34976b6 321 FALSE), /* pcrel_offset */
5bd4f169
AM
322
323 /* A standard 16 bit relocation. */
324 HOWTO (R_PPC64_ADDR16, /* type */
325 0, /* rightshift */
326 1, /* size (0 = byte, 1 = short, 2 = long) */
327 16, /* bitsize */
b34976b6 328 FALSE, /* pc_relative */
5bd4f169
AM
329 0, /* bitpos */
330 complain_overflow_bitfield, /* complain_on_overflow */
331 bfd_elf_generic_reloc, /* special_function */
332 "R_PPC64_ADDR16", /* name */
b34976b6 333 FALSE, /* partial_inplace */
5bd4f169
AM
334 0, /* src_mask */
335 0xffff, /* dst_mask */
b34976b6 336 FALSE), /* pcrel_offset */
5bd4f169
AM
337
338 /* A 16 bit relocation without overflow. */
339 HOWTO (R_PPC64_ADDR16_LO, /* type */
340 0, /* rightshift */
341 1, /* size (0 = byte, 1 = short, 2 = long) */
342 16, /* bitsize */
b34976b6 343 FALSE, /* pc_relative */
5bd4f169
AM
344 0, /* bitpos */
345 complain_overflow_dont,/* complain_on_overflow */
346 bfd_elf_generic_reloc, /* special_function */
347 "R_PPC64_ADDR16_LO", /* name */
b34976b6 348 FALSE, /* partial_inplace */
5bd4f169
AM
349 0, /* src_mask */
350 0xffff, /* dst_mask */
b34976b6 351 FALSE), /* pcrel_offset */
5bd4f169
AM
352
353 /* Bits 16-31 of an address. */
354 HOWTO (R_PPC64_ADDR16_HI, /* type */
355 16, /* rightshift */
356 1, /* size (0 = byte, 1 = short, 2 = long) */
357 16, /* bitsize */
b34976b6 358 FALSE, /* pc_relative */
5bd4f169 359 0, /* bitpos */
f9c6b907 360 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
361 bfd_elf_generic_reloc, /* special_function */
362 "R_PPC64_ADDR16_HI", /* name */
b34976b6 363 FALSE, /* partial_inplace */
5bd4f169
AM
364 0, /* src_mask */
365 0xffff, /* dst_mask */
b34976b6 366 FALSE), /* pcrel_offset */
5bd4f169
AM
367
368 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
369 bits, treated as a signed number, is negative. */
370 HOWTO (R_PPC64_ADDR16_HA, /* type */
371 16, /* rightshift */
372 1, /* size (0 = byte, 1 = short, 2 = long) */
373 16, /* bitsize */
b34976b6 374 FALSE, /* pc_relative */
5bd4f169 375 0, /* bitpos */
f9c6b907 376 complain_overflow_signed, /* complain_on_overflow */
805fc799 377 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 378 "R_PPC64_ADDR16_HA", /* name */
b34976b6 379 FALSE, /* partial_inplace */
5bd4f169
AM
380 0, /* src_mask */
381 0xffff, /* dst_mask */
b34976b6 382 FALSE), /* pcrel_offset */
5bd4f169
AM
383
384 /* An absolute 16 bit branch; the lower two bits must be zero.
385 FIXME: we don't check that, we just clear them. */
386 HOWTO (R_PPC64_ADDR14, /* type */
387 0, /* rightshift */
388 2, /* size (0 = byte, 1 = short, 2 = long) */
389 16, /* bitsize */
b34976b6 390 FALSE, /* pc_relative */
5bd4f169 391 0, /* bitpos */
b80eed39 392 complain_overflow_signed, /* complain_on_overflow */
2441e016 393 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 394 "R_PPC64_ADDR14", /* name */
b34976b6 395 FALSE, /* partial_inplace */
d006db6c 396 0, /* src_mask */
f5e87a1d 397 0x0000fffc, /* dst_mask */
b34976b6 398 FALSE), /* pcrel_offset */
5bd4f169
AM
399
400 /* An absolute 16 bit branch, for which bit 10 should be set to
401 indicate that the branch is expected to be taken. The lower two
402 bits must be zero. */
403 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
404 0, /* rightshift */
405 2, /* size (0 = byte, 1 = short, 2 = long) */
406 16, /* bitsize */
b34976b6 407 FALSE, /* pc_relative */
5bd4f169 408 0, /* bitpos */
b80eed39 409 complain_overflow_signed, /* complain_on_overflow */
805fc799 410 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 411 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 412 FALSE, /* partial_inplace */
d006db6c 413 0, /* src_mask */
f5e87a1d 414 0x0000fffc, /* dst_mask */
b34976b6 415 FALSE), /* pcrel_offset */
5bd4f169
AM
416
417 /* An absolute 16 bit branch, for which bit 10 should be set to
418 indicate that the branch is not expected to be taken. The lower
419 two bits must be zero. */
420 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
421 0, /* rightshift */
422 2, /* size (0 = byte, 1 = short, 2 = long) */
423 16, /* bitsize */
b34976b6 424 FALSE, /* pc_relative */
5bd4f169 425 0, /* bitpos */
b80eed39 426 complain_overflow_signed, /* complain_on_overflow */
805fc799 427 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 428 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 429 FALSE, /* partial_inplace */
d006db6c 430 0, /* src_mask */
f5e87a1d 431 0x0000fffc, /* dst_mask */
b34976b6 432 FALSE), /* pcrel_offset */
5bd4f169
AM
433
434 /* A relative 26 bit branch; the lower two bits must be zero. */
435 HOWTO (R_PPC64_REL24, /* type */
436 0, /* rightshift */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
438 26, /* bitsize */
b34976b6 439 TRUE, /* pc_relative */
5bd4f169
AM
440 0, /* bitpos */
441 complain_overflow_signed, /* complain_on_overflow */
2441e016 442 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 443 "R_PPC64_REL24", /* name */
b34976b6 444 FALSE, /* partial_inplace */
d006db6c 445 0, /* src_mask */
f5e87a1d 446 0x03fffffc, /* dst_mask */
b34976b6 447 TRUE), /* pcrel_offset */
5bd4f169
AM
448
449 /* A relative 16 bit branch; the lower two bits must be zero. */
450 HOWTO (R_PPC64_REL14, /* type */
451 0, /* rightshift */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
453 16, /* bitsize */
b34976b6 454 TRUE, /* pc_relative */
5bd4f169
AM
455 0, /* bitpos */
456 complain_overflow_signed, /* complain_on_overflow */
2441e016 457 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 458 "R_PPC64_REL14", /* name */
b34976b6 459 FALSE, /* partial_inplace */
d006db6c 460 0, /* src_mask */
f5e87a1d 461 0x0000fffc, /* dst_mask */
b34976b6 462 TRUE), /* pcrel_offset */
5bd4f169
AM
463
464 /* A relative 16 bit branch. Bit 10 should be set to indicate that
465 the branch is expected to be taken. The lower two bits must be
466 zero. */
467 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
468 0, /* rightshift */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
470 16, /* bitsize */
b34976b6 471 TRUE, /* pc_relative */
5bd4f169
AM
472 0, /* bitpos */
473 complain_overflow_signed, /* complain_on_overflow */
805fc799 474 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 475 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 476 FALSE, /* partial_inplace */
d006db6c 477 0, /* src_mask */
f5e87a1d 478 0x0000fffc, /* dst_mask */
b34976b6 479 TRUE), /* pcrel_offset */
5bd4f169
AM
480
481 /* A relative 16 bit branch. Bit 10 should be set to indicate that
482 the branch is not expected to be taken. The lower two bits must
483 be zero. */
484 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
485 0, /* rightshift */
486 2, /* size (0 = byte, 1 = short, 2 = long) */
487 16, /* bitsize */
b34976b6 488 TRUE, /* pc_relative */
5bd4f169
AM
489 0, /* bitpos */
490 complain_overflow_signed, /* complain_on_overflow */
805fc799 491 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 492 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 493 FALSE, /* partial_inplace */
d006db6c 494 0, /* src_mask */
f5e87a1d 495 0x0000fffc, /* dst_mask */
b34976b6 496 TRUE), /* pcrel_offset */
5bd4f169
AM
497
498 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
499 symbol. */
500 HOWTO (R_PPC64_GOT16, /* type */
501 0, /* rightshift */
502 1, /* size (0 = byte, 1 = short, 2 = long) */
503 16, /* bitsize */
b34976b6 504 FALSE, /* pc_relative */
5bd4f169
AM
505 0, /* bitpos */
506 complain_overflow_signed, /* complain_on_overflow */
805fc799 507 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 508 "R_PPC64_GOT16", /* name */
b34976b6 509 FALSE, /* partial_inplace */
5bd4f169
AM
510 0, /* src_mask */
511 0xffff, /* dst_mask */
b34976b6 512 FALSE), /* pcrel_offset */
5bd4f169
AM
513
514 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
515 the symbol. */
516 HOWTO (R_PPC64_GOT16_LO, /* type */
517 0, /* rightshift */
518 1, /* size (0 = byte, 1 = short, 2 = long) */
519 16, /* bitsize */
b34976b6 520 FALSE, /* pc_relative */
5bd4f169
AM
521 0, /* bitpos */
522 complain_overflow_dont, /* complain_on_overflow */
805fc799 523 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 524 "R_PPC64_GOT16_LO", /* name */
b34976b6 525 FALSE, /* partial_inplace */
5bd4f169
AM
526 0, /* src_mask */
527 0xffff, /* dst_mask */
b34976b6 528 FALSE), /* pcrel_offset */
5bd4f169
AM
529
530 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
531 the symbol. */
532 HOWTO (R_PPC64_GOT16_HI, /* type */
533 16, /* rightshift */
534 1, /* size (0 = byte, 1 = short, 2 = long) */
535 16, /* bitsize */
b34976b6 536 FALSE, /* pc_relative */
5bd4f169 537 0, /* bitpos */
f9c6b907 538 complain_overflow_signed,/* complain_on_overflow */
805fc799 539 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 540 "R_PPC64_GOT16_HI", /* name */
b34976b6 541 FALSE, /* partial_inplace */
5bd4f169
AM
542 0, /* src_mask */
543 0xffff, /* dst_mask */
b34976b6 544 FALSE), /* pcrel_offset */
5bd4f169
AM
545
546 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
547 the symbol. */
548 HOWTO (R_PPC64_GOT16_HA, /* type */
549 16, /* rightshift */
550 1, /* size (0 = byte, 1 = short, 2 = long) */
551 16, /* bitsize */
b34976b6 552 FALSE, /* pc_relative */
5bd4f169 553 0, /* bitpos */
f9c6b907 554 complain_overflow_signed,/* complain_on_overflow */
805fc799 555 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 556 "R_PPC64_GOT16_HA", /* name */
b34976b6 557 FALSE, /* partial_inplace */
5bd4f169
AM
558 0, /* src_mask */
559 0xffff, /* dst_mask */
b34976b6 560 FALSE), /* pcrel_offset */
5bd4f169
AM
561
562 /* This is used only by the dynamic linker. The symbol should exist
563 both in the object being run and in some shared library. The
564 dynamic linker copies the data addressed by the symbol from the
565 shared library into the object, because the object being
566 run has to have the data at some particular address. */
567 HOWTO (R_PPC64_COPY, /* type */
568 0, /* rightshift */
f5e87a1d
AM
569 0, /* this one is variable size */
570 0, /* bitsize */
b34976b6 571 FALSE, /* pc_relative */
5bd4f169 572 0, /* bitpos */
f5e87a1d
AM
573 complain_overflow_dont, /* complain_on_overflow */
574 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 575 "R_PPC64_COPY", /* name */
b34976b6 576 FALSE, /* partial_inplace */
5bd4f169
AM
577 0, /* src_mask */
578 0, /* dst_mask */
b34976b6 579 FALSE), /* pcrel_offset */
5bd4f169
AM
580
581 /* Like R_PPC64_ADDR64, but used when setting global offset table
582 entries. */
583 HOWTO (R_PPC64_GLOB_DAT, /* type */
584 0, /* rightshift */
585 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
586 64, /* bitsize */
b34976b6 587 FALSE, /* pc_relative */
5bd4f169
AM
588 0, /* bitpos */
589 complain_overflow_dont, /* complain_on_overflow */
805fc799 590 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 591 "R_PPC64_GLOB_DAT", /* name */
b34976b6 592 FALSE, /* partial_inplace */
5bd4f169 593 0, /* src_mask */
f5e87a1d 594 ONES (64), /* dst_mask */
b34976b6 595 FALSE), /* pcrel_offset */
5bd4f169
AM
596
597 /* Created by the link editor. Marks a procedure linkage table
598 entry for a symbol. */
599 HOWTO (R_PPC64_JMP_SLOT, /* type */
600 0, /* rightshift */
601 0, /* size (0 = byte, 1 = short, 2 = long) */
602 0, /* bitsize */
b34976b6 603 FALSE, /* pc_relative */
5bd4f169
AM
604 0, /* bitpos */
605 complain_overflow_dont, /* complain_on_overflow */
805fc799 606 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 607 "R_PPC64_JMP_SLOT", /* name */
b34976b6 608 FALSE, /* partial_inplace */
5bd4f169
AM
609 0, /* src_mask */
610 0, /* dst_mask */
b34976b6 611 FALSE), /* pcrel_offset */
5bd4f169
AM
612
613 /* Used only by the dynamic linker. When the object is run, this
614 doubleword64 is set to the load address of the object, plus the
615 addend. */
616 HOWTO (R_PPC64_RELATIVE, /* type */
617 0, /* rightshift */
618 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
619 64, /* bitsize */
b34976b6 620 FALSE, /* pc_relative */
5bd4f169
AM
621 0, /* bitpos */
622 complain_overflow_dont, /* complain_on_overflow */
623 bfd_elf_generic_reloc, /* special_function */
624 "R_PPC64_RELATIVE", /* name */
b34976b6 625 FALSE, /* partial_inplace */
5bd4f169 626 0, /* src_mask */
f5e87a1d 627 ONES (64), /* dst_mask */
b34976b6 628 FALSE), /* pcrel_offset */
5bd4f169
AM
629
630 /* Like R_PPC64_ADDR32, but may be unaligned. */
631 HOWTO (R_PPC64_UADDR32, /* type */
632 0, /* rightshift */
633 2, /* size (0 = byte, 1 = short, 2 = long) */
634 32, /* bitsize */
b34976b6 635 FALSE, /* pc_relative */
5bd4f169
AM
636 0, /* bitpos */
637 complain_overflow_bitfield, /* complain_on_overflow */
638 bfd_elf_generic_reloc, /* special_function */
639 "R_PPC64_UADDR32", /* name */
b34976b6 640 FALSE, /* partial_inplace */
5bd4f169
AM
641 0, /* src_mask */
642 0xffffffff, /* dst_mask */
b34976b6 643 FALSE), /* pcrel_offset */
5bd4f169
AM
644
645 /* Like R_PPC64_ADDR16, but may be unaligned. */
646 HOWTO (R_PPC64_UADDR16, /* type */
647 0, /* rightshift */
648 1, /* size (0 = byte, 1 = short, 2 = long) */
649 16, /* bitsize */
b34976b6 650 FALSE, /* pc_relative */
5bd4f169
AM
651 0, /* bitpos */
652 complain_overflow_bitfield, /* complain_on_overflow */
653 bfd_elf_generic_reloc, /* special_function */
654 "R_PPC64_UADDR16", /* name */
b34976b6 655 FALSE, /* partial_inplace */
5bd4f169
AM
656 0, /* src_mask */
657 0xffff, /* dst_mask */
b34976b6 658 FALSE), /* pcrel_offset */
5bd4f169
AM
659
660 /* 32-bit PC relative. */
661 HOWTO (R_PPC64_REL32, /* type */
662 0, /* rightshift */
663 2, /* size (0 = byte, 1 = short, 2 = long) */
664 32, /* bitsize */
b34976b6 665 TRUE, /* pc_relative */
5bd4f169 666 0, /* bitpos */
5bd4f169
AM
667 complain_overflow_signed, /* complain_on_overflow */
668 bfd_elf_generic_reloc, /* special_function */
669 "R_PPC64_REL32", /* name */
b34976b6 670 FALSE, /* partial_inplace */
5bd4f169
AM
671 0, /* src_mask */
672 0xffffffff, /* dst_mask */
b34976b6 673 TRUE), /* pcrel_offset */
5bd4f169 674
10ed1bba 675 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
676 HOWTO (R_PPC64_PLT32, /* type */
677 0, /* rightshift */
678 2, /* size (0 = byte, 1 = short, 2 = long) */
679 32, /* bitsize */
b34976b6 680 FALSE, /* pc_relative */
5bd4f169
AM
681 0, /* bitpos */
682 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 683 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 684 "R_PPC64_PLT32", /* name */
b34976b6 685 FALSE, /* partial_inplace */
5bd4f169 686 0, /* src_mask */
f5e87a1d 687 0xffffffff, /* dst_mask */
b34976b6 688 FALSE), /* pcrel_offset */
5bd4f169
AM
689
690 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
691 FIXME: R_PPC64_PLTREL32 not supported. */
692 HOWTO (R_PPC64_PLTREL32, /* type */
693 0, /* rightshift */
694 2, /* size (0 = byte, 1 = short, 2 = long) */
695 32, /* bitsize */
b34976b6 696 TRUE, /* pc_relative */
5bd4f169
AM
697 0, /* bitpos */
698 complain_overflow_signed, /* complain_on_overflow */
3ce51288 699 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 700 "R_PPC64_PLTREL32", /* name */
b34976b6 701 FALSE, /* partial_inplace */
5bd4f169 702 0, /* src_mask */
f5e87a1d 703 0xffffffff, /* dst_mask */
b34976b6 704 TRUE), /* pcrel_offset */
5bd4f169
AM
705
706 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
707 the symbol. */
708 HOWTO (R_PPC64_PLT16_LO, /* type */
709 0, /* rightshift */
710 1, /* size (0 = byte, 1 = short, 2 = long) */
711 16, /* bitsize */
b34976b6 712 FALSE, /* pc_relative */
5bd4f169
AM
713 0, /* bitpos */
714 complain_overflow_dont, /* complain_on_overflow */
805fc799 715 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 716 "R_PPC64_PLT16_LO", /* name */
b34976b6 717 FALSE, /* partial_inplace */
5bd4f169
AM
718 0, /* src_mask */
719 0xffff, /* dst_mask */
b34976b6 720 FALSE), /* pcrel_offset */
5bd4f169
AM
721
722 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
723 the symbol. */
724 HOWTO (R_PPC64_PLT16_HI, /* type */
725 16, /* rightshift */
726 1, /* size (0 = byte, 1 = short, 2 = long) */
727 16, /* bitsize */
b34976b6 728 FALSE, /* pc_relative */
5bd4f169 729 0, /* bitpos */
f9c6b907 730 complain_overflow_signed, /* complain_on_overflow */
805fc799 731 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 732 "R_PPC64_PLT16_HI", /* name */
b34976b6 733 FALSE, /* partial_inplace */
5bd4f169
AM
734 0, /* src_mask */
735 0xffff, /* dst_mask */
b34976b6 736 FALSE), /* pcrel_offset */
5bd4f169
AM
737
738 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
739 the symbol. */
740 HOWTO (R_PPC64_PLT16_HA, /* type */
741 16, /* rightshift */
742 1, /* size (0 = byte, 1 = short, 2 = long) */
743 16, /* bitsize */
b34976b6 744 FALSE, /* pc_relative */
5bd4f169 745 0, /* bitpos */
f9c6b907 746 complain_overflow_signed, /* complain_on_overflow */
805fc799 747 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 748 "R_PPC64_PLT16_HA", /* name */
b34976b6 749 FALSE, /* partial_inplace */
5bd4f169
AM
750 0, /* src_mask */
751 0xffff, /* dst_mask */
b34976b6 752 FALSE), /* pcrel_offset */
5bd4f169 753
c061c2d8 754 /* 16-bit section relative relocation. */
5bd4f169
AM
755 HOWTO (R_PPC64_SECTOFF, /* type */
756 0, /* rightshift */
c061c2d8
AM
757 1, /* size (0 = byte, 1 = short, 2 = long) */
758 16, /* bitsize */
b34976b6 759 FALSE, /* pc_relative */
5bd4f169 760 0, /* bitpos */
b80eed39 761 complain_overflow_signed, /* complain_on_overflow */
805fc799 762 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 763 "R_PPC64_SECTOFF", /* name */
b34976b6 764 FALSE, /* partial_inplace */
5bd4f169 765 0, /* src_mask */
c061c2d8 766 0xffff, /* dst_mask */
b34976b6 767 FALSE), /* pcrel_offset */
5bd4f169 768
c061c2d8 769 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
770 HOWTO (R_PPC64_SECTOFF_LO, /* type */
771 0, /* rightshift */
772 1, /* size (0 = byte, 1 = short, 2 = long) */
773 16, /* bitsize */
b34976b6 774 FALSE, /* pc_relative */
5bd4f169
AM
775 0, /* bitpos */
776 complain_overflow_dont, /* complain_on_overflow */
805fc799 777 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 778 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 779 FALSE, /* partial_inplace */
5bd4f169
AM
780 0, /* src_mask */
781 0xffff, /* dst_mask */
b34976b6 782 FALSE), /* pcrel_offset */
5bd4f169
AM
783
784 /* 16-bit upper half section relative relocation. */
785 HOWTO (R_PPC64_SECTOFF_HI, /* type */
786 16, /* rightshift */
787 1, /* size (0 = byte, 1 = short, 2 = long) */
788 16, /* bitsize */
b34976b6 789 FALSE, /* pc_relative */
5bd4f169 790 0, /* bitpos */
f9c6b907 791 complain_overflow_signed, /* complain_on_overflow */
805fc799 792 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 793 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 794 FALSE, /* partial_inplace */
5bd4f169
AM
795 0, /* src_mask */
796 0xffff, /* dst_mask */
b34976b6 797 FALSE), /* pcrel_offset */
5bd4f169
AM
798
799 /* 16-bit upper half adjusted section relative relocation. */
800 HOWTO (R_PPC64_SECTOFF_HA, /* type */
801 16, /* rightshift */
802 1, /* size (0 = byte, 1 = short, 2 = long) */
803 16, /* bitsize */
b34976b6 804 FALSE, /* pc_relative */
5bd4f169 805 0, /* bitpos */
f9c6b907 806 complain_overflow_signed, /* complain_on_overflow */
805fc799 807 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 808 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 809 FALSE, /* partial_inplace */
5bd4f169
AM
810 0, /* src_mask */
811 0xffff, /* dst_mask */
b34976b6 812 FALSE), /* pcrel_offset */
5bd4f169 813
04c9666a
AM
814 /* Like R_PPC64_REL24 without touching the two least significant bits. */
815 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
816 2, /* rightshift */
817 2, /* size (0 = byte, 1 = short, 2 = long) */
818 30, /* bitsize */
b34976b6 819 TRUE, /* pc_relative */
5bd4f169
AM
820 0, /* bitpos */
821 complain_overflow_dont, /* complain_on_overflow */
822 bfd_elf_generic_reloc, /* special_function */
04c9666a 823 "R_PPC64_REL30", /* name */
b34976b6 824 FALSE, /* partial_inplace */
d006db6c 825 0, /* src_mask */
5bd4f169 826 0xfffffffc, /* dst_mask */
b34976b6 827 TRUE), /* pcrel_offset */
5bd4f169
AM
828
829 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
830
831 /* A standard 64-bit relocation. */
832 HOWTO (R_PPC64_ADDR64, /* type */
833 0, /* rightshift */
834 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
835 64, /* bitsize */
b34976b6 836 FALSE, /* pc_relative */
5bd4f169
AM
837 0, /* bitpos */
838 complain_overflow_dont, /* complain_on_overflow */
839 bfd_elf_generic_reloc, /* special_function */
840 "R_PPC64_ADDR64", /* name */
b34976b6 841 FALSE, /* partial_inplace */
5bd4f169 842 0, /* src_mask */
f5e87a1d 843 ONES (64), /* dst_mask */
b34976b6 844 FALSE), /* pcrel_offset */
5bd4f169
AM
845
846 /* The bits 32-47 of an address. */
847 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
848 32, /* rightshift */
849 1, /* size (0 = byte, 1 = short, 2 = long) */
850 16, /* bitsize */
b34976b6 851 FALSE, /* pc_relative */
5bd4f169
AM
852 0, /* bitpos */
853 complain_overflow_dont, /* complain_on_overflow */
854 bfd_elf_generic_reloc, /* special_function */
855 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 856 FALSE, /* partial_inplace */
5bd4f169
AM
857 0, /* src_mask */
858 0xffff, /* dst_mask */
b34976b6 859 FALSE), /* pcrel_offset */
5bd4f169
AM
860
861 /* The bits 32-47 of an address, plus 1 if the contents of the low
862 16 bits, treated as a signed number, is negative. */
863 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
864 32, /* rightshift */
865 1, /* size (0 = byte, 1 = short, 2 = long) */
866 16, /* bitsize */
b34976b6 867 FALSE, /* pc_relative */
5bd4f169
AM
868 0, /* bitpos */
869 complain_overflow_dont, /* complain_on_overflow */
805fc799 870 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 871 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 872 FALSE, /* partial_inplace */
5bd4f169
AM
873 0, /* src_mask */
874 0xffff, /* dst_mask */
b34976b6 875 FALSE), /* pcrel_offset */
5bd4f169
AM
876
877 /* The bits 48-63 of an address. */
878 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
879 48, /* rightshift */
880 1, /* size (0 = byte, 1 = short, 2 = long) */
881 16, /* bitsize */
b34976b6 882 FALSE, /* pc_relative */
5bd4f169
AM
883 0, /* bitpos */
884 complain_overflow_dont, /* complain_on_overflow */
885 bfd_elf_generic_reloc, /* special_function */
886 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 887 FALSE, /* partial_inplace */
5bd4f169
AM
888 0, /* src_mask */
889 0xffff, /* dst_mask */
b34976b6 890 FALSE), /* pcrel_offset */
5bd4f169
AM
891
892 /* The bits 48-63 of an address, plus 1 if the contents of the low
893 16 bits, treated as a signed number, is negative. */
894 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
895 48, /* rightshift */
896 1, /* size (0 = byte, 1 = short, 2 = long) */
897 16, /* bitsize */
b34976b6 898 FALSE, /* pc_relative */
5bd4f169
AM
899 0, /* bitpos */
900 complain_overflow_dont, /* complain_on_overflow */
805fc799 901 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 902 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 903 FALSE, /* partial_inplace */
5bd4f169
AM
904 0, /* src_mask */
905 0xffff, /* dst_mask */
b34976b6 906 FALSE), /* pcrel_offset */
5bd4f169
AM
907
908 /* Like ADDR64, but may be unaligned. */
909 HOWTO (R_PPC64_UADDR64, /* type */
910 0, /* rightshift */
911 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
912 64, /* bitsize */
b34976b6 913 FALSE, /* pc_relative */
5bd4f169
AM
914 0, /* bitpos */
915 complain_overflow_dont, /* complain_on_overflow */
916 bfd_elf_generic_reloc, /* special_function */
917 "R_PPC64_UADDR64", /* name */
b34976b6 918 FALSE, /* partial_inplace */
5bd4f169 919 0, /* src_mask */
f5e87a1d 920 ONES (64), /* dst_mask */
b34976b6 921 FALSE), /* pcrel_offset */
5bd4f169
AM
922
923 /* 64-bit relative relocation. */
924 HOWTO (R_PPC64_REL64, /* type */
925 0, /* rightshift */
926 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
927 64, /* bitsize */
b34976b6 928 TRUE, /* pc_relative */
5bd4f169
AM
929 0, /* bitpos */
930 complain_overflow_dont, /* complain_on_overflow */
931 bfd_elf_generic_reloc, /* special_function */
932 "R_PPC64_REL64", /* name */
b34976b6 933 FALSE, /* partial_inplace */
5bd4f169 934 0, /* src_mask */
f5e87a1d 935 ONES (64), /* dst_mask */
b34976b6 936 TRUE), /* pcrel_offset */
5bd4f169 937
cedb70c5 938 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
939 HOWTO (R_PPC64_PLT64, /* type */
940 0, /* rightshift */
941 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
942 64, /* bitsize */
b34976b6 943 FALSE, /* pc_relative */
5bd4f169
AM
944 0, /* bitpos */
945 complain_overflow_dont, /* complain_on_overflow */
805fc799 946 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 947 "R_PPC64_PLT64", /* name */
b34976b6 948 FALSE, /* partial_inplace */
5bd4f169 949 0, /* src_mask */
f5e87a1d 950 ONES (64), /* dst_mask */
b34976b6 951 FALSE), /* pcrel_offset */
5bd4f169
AM
952
953 /* 64-bit PC relative relocation to the symbol's procedure linkage
954 table. */
955 /* FIXME: R_PPC64_PLTREL64 not supported. */
956 HOWTO (R_PPC64_PLTREL64, /* type */
957 0, /* rightshift */
958 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
959 64, /* bitsize */
b34976b6 960 TRUE, /* pc_relative */
5bd4f169
AM
961 0, /* bitpos */
962 complain_overflow_dont, /* complain_on_overflow */
805fc799 963 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 964 "R_PPC64_PLTREL64", /* name */
b34976b6 965 FALSE, /* partial_inplace */
5bd4f169 966 0, /* src_mask */
f5e87a1d 967 ONES (64), /* dst_mask */
b34976b6 968 TRUE), /* pcrel_offset */
5bd4f169
AM
969
970 /* 16 bit TOC-relative relocation. */
971
972 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
973 HOWTO (R_PPC64_TOC16, /* type */
974 0, /* rightshift */
975 1, /* size (0 = byte, 1 = short, 2 = long) */
976 16, /* bitsize */
b34976b6 977 FALSE, /* pc_relative */
5bd4f169
AM
978 0, /* bitpos */
979 complain_overflow_signed, /* complain_on_overflow */
805fc799 980 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 981 "R_PPC64_TOC16", /* name */
b34976b6 982 FALSE, /* partial_inplace */
5bd4f169
AM
983 0, /* src_mask */
984 0xffff, /* dst_mask */
b34976b6 985 FALSE), /* pcrel_offset */
5bd4f169
AM
986
987 /* 16 bit TOC-relative relocation without overflow. */
988
989 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
990 HOWTO (R_PPC64_TOC16_LO, /* type */
991 0, /* rightshift */
992 1, /* size (0 = byte, 1 = short, 2 = long) */
993 16, /* bitsize */
b34976b6 994 FALSE, /* pc_relative */
5bd4f169
AM
995 0, /* bitpos */
996 complain_overflow_dont, /* complain_on_overflow */
805fc799 997 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 998 "R_PPC64_TOC16_LO", /* name */
b34976b6 999 FALSE, /* partial_inplace */
5bd4f169
AM
1000 0, /* src_mask */
1001 0xffff, /* dst_mask */
b34976b6 1002 FALSE), /* pcrel_offset */
5bd4f169
AM
1003
1004 /* 16 bit TOC-relative relocation, high 16 bits. */
1005
1006 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1007 HOWTO (R_PPC64_TOC16_HI, /* type */
1008 16, /* rightshift */
1009 1, /* size (0 = byte, 1 = short, 2 = long) */
1010 16, /* bitsize */
b34976b6 1011 FALSE, /* pc_relative */
5bd4f169 1012 0, /* bitpos */
f9c6b907 1013 complain_overflow_signed, /* complain_on_overflow */
805fc799 1014 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 1015 "R_PPC64_TOC16_HI", /* name */
b34976b6 1016 FALSE, /* partial_inplace */
5bd4f169
AM
1017 0, /* src_mask */
1018 0xffff, /* dst_mask */
b34976b6 1019 FALSE), /* pcrel_offset */
5bd4f169
AM
1020
1021 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1022 contents of the low 16 bits, treated as a signed number, is
1023 negative. */
1024
1025 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1026 HOWTO (R_PPC64_TOC16_HA, /* type */
1027 16, /* rightshift */
1028 1, /* size (0 = byte, 1 = short, 2 = long) */
1029 16, /* bitsize */
b34976b6 1030 FALSE, /* pc_relative */
5bd4f169 1031 0, /* bitpos */
f9c6b907 1032 complain_overflow_signed, /* complain_on_overflow */
805fc799 1033 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 1034 "R_PPC64_TOC16_HA", /* name */
b34976b6 1035 FALSE, /* partial_inplace */
5bd4f169
AM
1036 0, /* src_mask */
1037 0xffff, /* dst_mask */
b34976b6 1038 FALSE), /* pcrel_offset */
5bd4f169
AM
1039
1040 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1041
1042 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1043 HOWTO (R_PPC64_TOC, /* type */
1044 0, /* rightshift */
1045 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1046 64, /* bitsize */
b34976b6 1047 FALSE, /* pc_relative */
5bd4f169 1048 0, /* bitpos */
b80eed39 1049 complain_overflow_dont, /* complain_on_overflow */
805fc799 1050 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1051 "R_PPC64_TOC", /* name */
b34976b6 1052 FALSE, /* partial_inplace */
5bd4f169 1053 0, /* src_mask */
f5e87a1d 1054 ONES (64), /* dst_mask */
b34976b6 1055 FALSE), /* pcrel_offset */
5bd4f169
AM
1056
1057 /* Like R_PPC64_GOT16, but also informs the link editor that the
1058 value to relocate may (!) refer to a PLT entry which the link
1059 editor (a) may replace with the symbol value. If the link editor
1060 is unable to fully resolve the symbol, it may (b) create a PLT
1061 entry and store the address to the new PLT entry in the GOT.
1062 This permits lazy resolution of function symbols at run time.
1063 The link editor may also skip all of this and just (c) emit a
1064 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1065 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1066 HOWTO (R_PPC64_PLTGOT16, /* type */
1067 0, /* rightshift */
1068 1, /* size (0 = byte, 1 = short, 2 = long) */
1069 16, /* bitsize */
b34976b6 1070 FALSE, /* pc_relative */
5bd4f169
AM
1071 0, /* bitpos */
1072 complain_overflow_signed, /* complain_on_overflow */
805fc799 1073 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1074 "R_PPC64_PLTGOT16", /* name */
1075 FALSE, /* partial_inplace */
1076 0, /* src_mask */
1077 0xffff, /* dst_mask */
1078 FALSE), /* pcrel_offset */
1079
1080 /* Like R_PPC64_PLTGOT16, but without overflow. */
1081 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1082 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1083 0, /* rightshift */
1084 1, /* size (0 = byte, 1 = short, 2 = long) */
1085 16, /* bitsize */
1086 FALSE, /* pc_relative */
1087 0, /* bitpos */
1088 complain_overflow_dont, /* complain_on_overflow */
1089 ppc64_elf_unhandled_reloc, /* special_function */
1090 "R_PPC64_PLTGOT16_LO", /* name */
1091 FALSE, /* partial_inplace */
1092 0, /* src_mask */
1093 0xffff, /* dst_mask */
1094 FALSE), /* pcrel_offset */
1095
1096 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1097 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1098 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1099 16, /* rightshift */
1100 1, /* size (0 = byte, 1 = short, 2 = long) */
1101 16, /* bitsize */
1102 FALSE, /* pc_relative */
1103 0, /* bitpos */
f9c6b907 1104 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1105 ppc64_elf_unhandled_reloc, /* special_function */
1106 "R_PPC64_PLTGOT16_HI", /* name */
1107 FALSE, /* partial_inplace */
1108 0, /* src_mask */
1109 0xffff, /* dst_mask */
1110 FALSE), /* pcrel_offset */
1111
1112 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1113 1 if the contents of the low 16 bits, treated as a signed number,
1114 is negative. */
1115 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1116 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1117 16, /* rightshift */
1118 1, /* size (0 = byte, 1 = short, 2 = long) */
1119 16, /* bitsize */
1120 FALSE, /* pc_relative */
1121 0, /* bitpos */
f9c6b907 1122 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1123 ppc64_elf_unhandled_reloc, /* special_function */
1124 "R_PPC64_PLTGOT16_HA", /* name */
1125 FALSE, /* partial_inplace */
1126 0, /* src_mask */
1127 0xffff, /* dst_mask */
1128 FALSE), /* pcrel_offset */
1129
1130 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1131 HOWTO (R_PPC64_ADDR16_DS, /* type */
1132 0, /* rightshift */
1133 1, /* size (0 = byte, 1 = short, 2 = long) */
1134 16, /* bitsize */
1135 FALSE, /* pc_relative */
1136 0, /* bitpos */
b80eed39 1137 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1138 bfd_elf_generic_reloc, /* special_function */
1139 "R_PPC64_ADDR16_DS", /* name */
1140 FALSE, /* partial_inplace */
1141 0, /* src_mask */
1142 0xfffc, /* dst_mask */
1143 FALSE), /* pcrel_offset */
1144
1145 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1146 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1147 0, /* rightshift */
1148 1, /* size (0 = byte, 1 = short, 2 = long) */
1149 16, /* bitsize */
1150 FALSE, /* pc_relative */
1151 0, /* bitpos */
1152 complain_overflow_dont,/* complain_on_overflow */
1153 bfd_elf_generic_reloc, /* special_function */
1154 "R_PPC64_ADDR16_LO_DS",/* name */
1155 FALSE, /* partial_inplace */
1156 0, /* src_mask */
1157 0xfffc, /* dst_mask */
1158 FALSE), /* pcrel_offset */
1159
1160 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1161 HOWTO (R_PPC64_GOT16_DS, /* type */
1162 0, /* rightshift */
1163 1, /* size (0 = byte, 1 = short, 2 = long) */
1164 16, /* bitsize */
1165 FALSE, /* pc_relative */
1166 0, /* bitpos */
1167 complain_overflow_signed, /* complain_on_overflow */
1168 ppc64_elf_unhandled_reloc, /* special_function */
1169 "R_PPC64_GOT16_DS", /* name */
1170 FALSE, /* partial_inplace */
1171 0, /* src_mask */
1172 0xfffc, /* dst_mask */
1173 FALSE), /* pcrel_offset */
1174
1175 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1176 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1177 0, /* rightshift */
1178 1, /* size (0 = byte, 1 = short, 2 = long) */
1179 16, /* bitsize */
1180 FALSE, /* pc_relative */
1181 0, /* bitpos */
1182 complain_overflow_dont, /* complain_on_overflow */
1183 ppc64_elf_unhandled_reloc, /* special_function */
1184 "R_PPC64_GOT16_LO_DS", /* name */
1185 FALSE, /* partial_inplace */
1186 0, /* src_mask */
1187 0xfffc, /* dst_mask */
1188 FALSE), /* pcrel_offset */
1189
1190 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1191 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1192 0, /* rightshift */
1193 1, /* size (0 = byte, 1 = short, 2 = long) */
1194 16, /* bitsize */
1195 FALSE, /* pc_relative */
1196 0, /* bitpos */
1197 complain_overflow_dont, /* complain_on_overflow */
1198 ppc64_elf_unhandled_reloc, /* special_function */
1199 "R_PPC64_PLT16_LO_DS", /* name */
1200 FALSE, /* partial_inplace */
1201 0, /* src_mask */
1202 0xfffc, /* dst_mask */
1203 FALSE), /* pcrel_offset */
1204
1205 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1206 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1207 0, /* rightshift */
1208 1, /* size (0 = byte, 1 = short, 2 = long) */
1209 16, /* bitsize */
1210 FALSE, /* pc_relative */
1211 0, /* bitpos */
b80eed39 1212 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1213 ppc64_elf_sectoff_reloc, /* special_function */
1214 "R_PPC64_SECTOFF_DS", /* name */
1215 FALSE, /* partial_inplace */
1216 0, /* src_mask */
1217 0xfffc, /* dst_mask */
1218 FALSE), /* pcrel_offset */
1219
1220 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1221 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1222 0, /* rightshift */
1223 1, /* size (0 = byte, 1 = short, 2 = long) */
1224 16, /* bitsize */
1225 FALSE, /* pc_relative */
1226 0, /* bitpos */
1227 complain_overflow_dont, /* complain_on_overflow */
1228 ppc64_elf_sectoff_reloc, /* special_function */
1229 "R_PPC64_SECTOFF_LO_DS",/* name */
1230 FALSE, /* partial_inplace */
1231 0, /* src_mask */
1232 0xfffc, /* dst_mask */
1233 FALSE), /* pcrel_offset */
1234
1235 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1236 HOWTO (R_PPC64_TOC16_DS, /* type */
1237 0, /* rightshift */
1238 1, /* size (0 = byte, 1 = short, 2 = long) */
1239 16, /* bitsize */
1240 FALSE, /* pc_relative */
1241 0, /* bitpos */
1242 complain_overflow_signed, /* complain_on_overflow */
1243 ppc64_elf_toc_reloc, /* special_function */
1244 "R_PPC64_TOC16_DS", /* name */
1245 FALSE, /* partial_inplace */
1246 0, /* src_mask */
1247 0xfffc, /* dst_mask */
1248 FALSE), /* pcrel_offset */
1249
1250 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1251 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1252 0, /* rightshift */
1253 1, /* size (0 = byte, 1 = short, 2 = long) */
1254 16, /* bitsize */
1255 FALSE, /* pc_relative */
1256 0, /* bitpos */
1257 complain_overflow_dont, /* complain_on_overflow */
1258 ppc64_elf_toc_reloc, /* special_function */
1259 "R_PPC64_TOC16_LO_DS", /* name */
1260 FALSE, /* partial_inplace */
1261 0, /* src_mask */
1262 0xfffc, /* dst_mask */
1263 FALSE), /* pcrel_offset */
1264
1265 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1266 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1267 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1268 0, /* rightshift */
1269 1, /* size (0 = byte, 1 = short, 2 = long) */
1270 16, /* bitsize */
1271 FALSE, /* pc_relative */
1272 0, /* bitpos */
1273 complain_overflow_signed, /* complain_on_overflow */
1274 ppc64_elf_unhandled_reloc, /* special_function */
1275 "R_PPC64_PLTGOT16_DS", /* name */
1276 FALSE, /* partial_inplace */
1277 0, /* src_mask */
1278 0xfffc, /* dst_mask */
1279 FALSE), /* pcrel_offset */
1280
1281 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1282 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1283 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1284 0, /* rightshift */
1285 1, /* size (0 = byte, 1 = short, 2 = long) */
1286 16, /* bitsize */
1287 FALSE, /* pc_relative */
1288 0, /* bitpos */
1289 complain_overflow_dont, /* complain_on_overflow */
1290 ppc64_elf_unhandled_reloc, /* special_function */
1291 "R_PPC64_PLTGOT16_LO_DS",/* name */
1292 FALSE, /* partial_inplace */
1293 0, /* src_mask */
1294 0xfffc, /* dst_mask */
1295 FALSE), /* pcrel_offset */
1296
727fc41e 1297 /* Marker relocs for TLS. */
411e1bfb
AM
1298 HOWTO (R_PPC64_TLS,
1299 0, /* rightshift */
1300 2, /* size (0 = byte, 1 = short, 2 = long) */
1301 32, /* bitsize */
1302 FALSE, /* pc_relative */
1303 0, /* bitpos */
1304 complain_overflow_dont, /* complain_on_overflow */
1305 bfd_elf_generic_reloc, /* special_function */
1306 "R_PPC64_TLS", /* name */
1307 FALSE, /* partial_inplace */
1308 0, /* src_mask */
1309 0, /* dst_mask */
1310 FALSE), /* pcrel_offset */
1311
727fc41e
AM
1312 HOWTO (R_PPC64_TLSGD,
1313 0, /* rightshift */
1314 2, /* size (0 = byte, 1 = short, 2 = long) */
1315 32, /* bitsize */
1316 FALSE, /* pc_relative */
1317 0, /* bitpos */
1318 complain_overflow_dont, /* complain_on_overflow */
1319 bfd_elf_generic_reloc, /* special_function */
1320 "R_PPC64_TLSGD", /* name */
1321 FALSE, /* partial_inplace */
1322 0, /* src_mask */
1323 0, /* dst_mask */
1324 FALSE), /* pcrel_offset */
1325
1326 HOWTO (R_PPC64_TLSLD,
1327 0, /* rightshift */
1328 2, /* size (0 = byte, 1 = short, 2 = long) */
1329 32, /* bitsize */
1330 FALSE, /* pc_relative */
1331 0, /* bitpos */
1332 complain_overflow_dont, /* complain_on_overflow */
1333 bfd_elf_generic_reloc, /* special_function */
1334 "R_PPC64_TLSLD", /* name */
1335 FALSE, /* partial_inplace */
1336 0, /* src_mask */
1337 0, /* dst_mask */
1338 FALSE), /* pcrel_offset */
1339
23cedd1d
AM
1340 /* Marker reloc for optimizing r2 save in prologue rather than on
1341 each plt call stub. */
3b421ab3
AM
1342 HOWTO (R_PPC64_TOCSAVE,
1343 0, /* rightshift */
1344 2, /* size (0 = byte, 1 = short, 2 = long) */
1345 32, /* bitsize */
1346 FALSE, /* pc_relative */
1347 0, /* bitpos */
1348 complain_overflow_dont, /* complain_on_overflow */
1349 bfd_elf_generic_reloc, /* special_function */
1350 "R_PPC64_TOCSAVE", /* name */
1351 FALSE, /* partial_inplace */
1352 0, /* src_mask */
1353 0, /* dst_mask */
1354 FALSE), /* pcrel_offset */
1355
23cedd1d
AM
1356 /* Marker relocs on inline plt call instructions. */
1357 HOWTO (R_PPC64_PLTSEQ,
1358 0, /* rightshift */
1359 2, /* size (0 = byte, 1 = short, 2 = long) */
1360 32, /* bitsize */
1361 FALSE, /* pc_relative */
1362 0, /* bitpos */
1363 complain_overflow_dont, /* complain_on_overflow */
1364 bfd_elf_generic_reloc, /* special_function */
1365 "R_PPC64_PLTSEQ", /* name */
1366 FALSE, /* partial_inplace */
1367 0, /* src_mask */
1368 0, /* dst_mask */
1369 FALSE), /* pcrel_offset */
1370
1371 HOWTO (R_PPC64_PLTCALL,
1372 0, /* rightshift */
1373 2, /* size (0 = byte, 1 = short, 2 = long) */
1374 32, /* bitsize */
1375 FALSE, /* pc_relative */
1376 0, /* bitpos */
1377 complain_overflow_dont, /* complain_on_overflow */
1378 bfd_elf_generic_reloc, /* special_function */
1379 "R_PPC64_PLTCALL", /* name */
1380 FALSE, /* partial_inplace */
1381 0, /* src_mask */
1382 0, /* dst_mask */
1383 FALSE), /* pcrel_offset */
1384
411e1bfb
AM
1385 /* Computes the load module index of the load module that contains the
1386 definition of its TLS sym. */
1387 HOWTO (R_PPC64_DTPMOD64,
1388 0, /* rightshift */
1389 4, /* size (0 = byte, 1 = short, 2 = long) */
1390 64, /* bitsize */
1391 FALSE, /* pc_relative */
1392 0, /* bitpos */
1393 complain_overflow_dont, /* complain_on_overflow */
1394 ppc64_elf_unhandled_reloc, /* special_function */
1395 "R_PPC64_DTPMOD64", /* name */
1396 FALSE, /* partial_inplace */
1397 0, /* src_mask */
1398 ONES (64), /* dst_mask */
1399 FALSE), /* pcrel_offset */
1400
1401 /* Computes a dtv-relative displacement, the difference between the value
1402 of sym+add and the base address of the thread-local storage block that
1403 contains the definition of sym, minus 0x8000. */
1404 HOWTO (R_PPC64_DTPREL64,
1405 0, /* rightshift */
1406 4, /* size (0 = byte, 1 = short, 2 = long) */
1407 64, /* bitsize */
1408 FALSE, /* pc_relative */
1409 0, /* bitpos */
1410 complain_overflow_dont, /* complain_on_overflow */
1411 ppc64_elf_unhandled_reloc, /* special_function */
1412 "R_PPC64_DTPREL64", /* name */
1413 FALSE, /* partial_inplace */
1414 0, /* src_mask */
1415 ONES (64), /* dst_mask */
1416 FALSE), /* pcrel_offset */
1417
1418 /* A 16 bit dtprel reloc. */
1419 HOWTO (R_PPC64_DTPREL16,
1420 0, /* rightshift */
1421 1, /* size (0 = byte, 1 = short, 2 = long) */
1422 16, /* bitsize */
1423 FALSE, /* pc_relative */
1424 0, /* bitpos */
1425 complain_overflow_signed, /* complain_on_overflow */
1426 ppc64_elf_unhandled_reloc, /* special_function */
1427 "R_PPC64_DTPREL16", /* name */
1428 FALSE, /* partial_inplace */
1429 0, /* src_mask */
1430 0xffff, /* dst_mask */
1431 FALSE), /* pcrel_offset */
1432
1433 /* Like DTPREL16, but no overflow. */
1434 HOWTO (R_PPC64_DTPREL16_LO,
1435 0, /* rightshift */
1436 1, /* size (0 = byte, 1 = short, 2 = long) */
1437 16, /* bitsize */
1438 FALSE, /* pc_relative */
1439 0, /* bitpos */
1440 complain_overflow_dont, /* complain_on_overflow */
1441 ppc64_elf_unhandled_reloc, /* special_function */
1442 "R_PPC64_DTPREL16_LO", /* name */
1443 FALSE, /* partial_inplace */
1444 0, /* src_mask */
1445 0xffff, /* dst_mask */
1446 FALSE), /* pcrel_offset */
1447
1448 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1449 HOWTO (R_PPC64_DTPREL16_HI,
1450 16, /* rightshift */
1451 1, /* size (0 = byte, 1 = short, 2 = long) */
1452 16, /* bitsize */
1453 FALSE, /* pc_relative */
1454 0, /* bitpos */
f9c6b907 1455 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1456 ppc64_elf_unhandled_reloc, /* special_function */
1457 "R_PPC64_DTPREL16_HI", /* name */
1458 FALSE, /* partial_inplace */
1459 0, /* src_mask */
1460 0xffff, /* dst_mask */
1461 FALSE), /* pcrel_offset */
1462
1463 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1464 HOWTO (R_PPC64_DTPREL16_HA,
1465 16, /* rightshift */
1466 1, /* size (0 = byte, 1 = short, 2 = long) */
1467 16, /* bitsize */
1468 FALSE, /* pc_relative */
1469 0, /* bitpos */
f9c6b907 1470 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1471 ppc64_elf_unhandled_reloc, /* special_function */
1472 "R_PPC64_DTPREL16_HA", /* name */
1473 FALSE, /* partial_inplace */
1474 0, /* src_mask */
1475 0xffff, /* dst_mask */
1476 FALSE), /* pcrel_offset */
1477
1478 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1479 HOWTO (R_PPC64_DTPREL16_HIGHER,
1480 32, /* rightshift */
1481 1, /* size (0 = byte, 1 = short, 2 = long) */
1482 16, /* bitsize */
1483 FALSE, /* pc_relative */
1484 0, /* bitpos */
1485 complain_overflow_dont, /* complain_on_overflow */
1486 ppc64_elf_unhandled_reloc, /* special_function */
1487 "R_PPC64_DTPREL16_HIGHER", /* name */
1488 FALSE, /* partial_inplace */
1489 0, /* src_mask */
1490 0xffff, /* dst_mask */
1491 FALSE), /* pcrel_offset */
1492
1493 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1494 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1495 32, /* rightshift */
1496 1, /* size (0 = byte, 1 = short, 2 = long) */
1497 16, /* bitsize */
1498 FALSE, /* pc_relative */
1499 0, /* bitpos */
1500 complain_overflow_dont, /* complain_on_overflow */
1501 ppc64_elf_unhandled_reloc, /* special_function */
1502 "R_PPC64_DTPREL16_HIGHERA", /* name */
1503 FALSE, /* partial_inplace */
1504 0, /* src_mask */
1505 0xffff, /* dst_mask */
1506 FALSE), /* pcrel_offset */
1507
1508 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1509 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1510 48, /* rightshift */
1511 1, /* size (0 = byte, 1 = short, 2 = long) */
1512 16, /* bitsize */
1513 FALSE, /* pc_relative */
1514 0, /* bitpos */
1515 complain_overflow_dont, /* complain_on_overflow */
1516 ppc64_elf_unhandled_reloc, /* special_function */
1517 "R_PPC64_DTPREL16_HIGHEST", /* name */
1518 FALSE, /* partial_inplace */
1519 0, /* src_mask */
1520 0xffff, /* dst_mask */
1521 FALSE), /* pcrel_offset */
1522
1523 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1524 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1525 48, /* rightshift */
1526 1, /* size (0 = byte, 1 = short, 2 = long) */
1527 16, /* bitsize */
1528 FALSE, /* pc_relative */
1529 0, /* bitpos */
1530 complain_overflow_dont, /* complain_on_overflow */
1531 ppc64_elf_unhandled_reloc, /* special_function */
1532 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1533 FALSE, /* partial_inplace */
1534 0, /* src_mask */
1535 0xffff, /* dst_mask */
1536 FALSE), /* pcrel_offset */
1537
1538 /* Like DTPREL16, but for insns with a DS field. */
1539 HOWTO (R_PPC64_DTPREL16_DS,
1540 0, /* rightshift */
1541 1, /* size (0 = byte, 1 = short, 2 = long) */
1542 16, /* bitsize */
1543 FALSE, /* pc_relative */
1544 0, /* bitpos */
1545 complain_overflow_signed, /* complain_on_overflow */
1546 ppc64_elf_unhandled_reloc, /* special_function */
1547 "R_PPC64_DTPREL16_DS", /* name */
1548 FALSE, /* partial_inplace */
1549 0, /* src_mask */
1550 0xfffc, /* dst_mask */
1551 FALSE), /* pcrel_offset */
1552
1553 /* Like DTPREL16_DS, but no overflow. */
1554 HOWTO (R_PPC64_DTPREL16_LO_DS,
1555 0, /* rightshift */
1556 1, /* size (0 = byte, 1 = short, 2 = long) */
1557 16, /* bitsize */
1558 FALSE, /* pc_relative */
1559 0, /* bitpos */
1560 complain_overflow_dont, /* complain_on_overflow */
1561 ppc64_elf_unhandled_reloc, /* special_function */
1562 "R_PPC64_DTPREL16_LO_DS", /* name */
1563 FALSE, /* partial_inplace */
1564 0, /* src_mask */
1565 0xfffc, /* dst_mask */
1566 FALSE), /* pcrel_offset */
1567
1568 /* Computes a tp-relative displacement, the difference between the value of
1569 sym+add and the value of the thread pointer (r13). */
1570 HOWTO (R_PPC64_TPREL64,
1571 0, /* rightshift */
1572 4, /* size (0 = byte, 1 = short, 2 = long) */
1573 64, /* bitsize */
1574 FALSE, /* pc_relative */
1575 0, /* bitpos */
1576 complain_overflow_dont, /* complain_on_overflow */
1577 ppc64_elf_unhandled_reloc, /* special_function */
1578 "R_PPC64_TPREL64", /* name */
1579 FALSE, /* partial_inplace */
1580 0, /* src_mask */
1581 ONES (64), /* dst_mask */
1582 FALSE), /* pcrel_offset */
1583
1584 /* A 16 bit tprel reloc. */
1585 HOWTO (R_PPC64_TPREL16,
1586 0, /* rightshift */
1587 1, /* size (0 = byte, 1 = short, 2 = long) */
1588 16, /* bitsize */
1589 FALSE, /* pc_relative */
1590 0, /* bitpos */
1591 complain_overflow_signed, /* complain_on_overflow */
1592 ppc64_elf_unhandled_reloc, /* special_function */
1593 "R_PPC64_TPREL16", /* name */
1594 FALSE, /* partial_inplace */
1595 0, /* src_mask */
1596 0xffff, /* dst_mask */
1597 FALSE), /* pcrel_offset */
1598
1599 /* Like TPREL16, but no overflow. */
1600 HOWTO (R_PPC64_TPREL16_LO,
1601 0, /* rightshift */
1602 1, /* size (0 = byte, 1 = short, 2 = long) */
1603 16, /* bitsize */
1604 FALSE, /* pc_relative */
1605 0, /* bitpos */
1606 complain_overflow_dont, /* complain_on_overflow */
1607 ppc64_elf_unhandled_reloc, /* special_function */
1608 "R_PPC64_TPREL16_LO", /* name */
1609 FALSE, /* partial_inplace */
1610 0, /* src_mask */
1611 0xffff, /* dst_mask */
1612 FALSE), /* pcrel_offset */
1613
1614 /* Like TPREL16_LO, but next higher group of 16 bits. */
1615 HOWTO (R_PPC64_TPREL16_HI,
1616 16, /* rightshift */
1617 1, /* size (0 = byte, 1 = short, 2 = long) */
1618 16, /* bitsize */
1619 FALSE, /* pc_relative */
1620 0, /* bitpos */
f9c6b907 1621 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1622 ppc64_elf_unhandled_reloc, /* special_function */
1623 "R_PPC64_TPREL16_HI", /* name */
1624 FALSE, /* partial_inplace */
1625 0, /* src_mask */
1626 0xffff, /* dst_mask */
1627 FALSE), /* pcrel_offset */
1628
1629 /* Like TPREL16_HI, but adjust for low 16 bits. */
1630 HOWTO (R_PPC64_TPREL16_HA,
1631 16, /* rightshift */
1632 1, /* size (0 = byte, 1 = short, 2 = long) */
1633 16, /* bitsize */
1634 FALSE, /* pc_relative */
1635 0, /* bitpos */
f9c6b907 1636 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1637 ppc64_elf_unhandled_reloc, /* special_function */
1638 "R_PPC64_TPREL16_HA", /* name */
1639 FALSE, /* partial_inplace */
1640 0, /* src_mask */
1641 0xffff, /* dst_mask */
1642 FALSE), /* pcrel_offset */
1643
1644 /* Like TPREL16_HI, but next higher group of 16 bits. */
1645 HOWTO (R_PPC64_TPREL16_HIGHER,
1646 32, /* rightshift */
1647 1, /* size (0 = byte, 1 = short, 2 = long) */
1648 16, /* bitsize */
1649 FALSE, /* pc_relative */
1650 0, /* bitpos */
1651 complain_overflow_dont, /* complain_on_overflow */
1652 ppc64_elf_unhandled_reloc, /* special_function */
1653 "R_PPC64_TPREL16_HIGHER", /* name */
1654 FALSE, /* partial_inplace */
1655 0, /* src_mask */
1656 0xffff, /* dst_mask */
1657 FALSE), /* pcrel_offset */
1658
1659 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1660 HOWTO (R_PPC64_TPREL16_HIGHERA,
1661 32, /* rightshift */
1662 1, /* size (0 = byte, 1 = short, 2 = long) */
1663 16, /* bitsize */
1664 FALSE, /* pc_relative */
1665 0, /* bitpos */
1666 complain_overflow_dont, /* complain_on_overflow */
1667 ppc64_elf_unhandled_reloc, /* special_function */
1668 "R_PPC64_TPREL16_HIGHERA", /* name */
1669 FALSE, /* partial_inplace */
1670 0, /* src_mask */
1671 0xffff, /* dst_mask */
1672 FALSE), /* pcrel_offset */
1673
1674 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1675 HOWTO (R_PPC64_TPREL16_HIGHEST,
1676 48, /* rightshift */
1677 1, /* size (0 = byte, 1 = short, 2 = long) */
1678 16, /* bitsize */
1679 FALSE, /* pc_relative */
1680 0, /* bitpos */
1681 complain_overflow_dont, /* complain_on_overflow */
1682 ppc64_elf_unhandled_reloc, /* special_function */
1683 "R_PPC64_TPREL16_HIGHEST", /* name */
1684 FALSE, /* partial_inplace */
1685 0, /* src_mask */
1686 0xffff, /* dst_mask */
1687 FALSE), /* pcrel_offset */
1688
1689 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1690 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1691 48, /* rightshift */
1692 1, /* size (0 = byte, 1 = short, 2 = long) */
1693 16, /* bitsize */
1694 FALSE, /* pc_relative */
1695 0, /* bitpos */
1696 complain_overflow_dont, /* complain_on_overflow */
1697 ppc64_elf_unhandled_reloc, /* special_function */
1698 "R_PPC64_TPREL16_HIGHESTA", /* name */
1699 FALSE, /* partial_inplace */
1700 0, /* src_mask */
1701 0xffff, /* dst_mask */
1702 FALSE), /* pcrel_offset */
1703
1704 /* Like TPREL16, but for insns with a DS field. */
1705 HOWTO (R_PPC64_TPREL16_DS,
1706 0, /* rightshift */
1707 1, /* size (0 = byte, 1 = short, 2 = long) */
1708 16, /* bitsize */
1709 FALSE, /* pc_relative */
1710 0, /* bitpos */
1711 complain_overflow_signed, /* complain_on_overflow */
1712 ppc64_elf_unhandled_reloc, /* special_function */
1713 "R_PPC64_TPREL16_DS", /* name */
1714 FALSE, /* partial_inplace */
1715 0, /* src_mask */
1716 0xfffc, /* dst_mask */
1717 FALSE), /* pcrel_offset */
1718
1719 /* Like TPREL16_DS, but no overflow. */
1720 HOWTO (R_PPC64_TPREL16_LO_DS,
1721 0, /* rightshift */
1722 1, /* size (0 = byte, 1 = short, 2 = long) */
1723 16, /* bitsize */
1724 FALSE, /* pc_relative */
1725 0, /* bitpos */
1726 complain_overflow_dont, /* complain_on_overflow */
1727 ppc64_elf_unhandled_reloc, /* special_function */
1728 "R_PPC64_TPREL16_LO_DS", /* name */
1729 FALSE, /* partial_inplace */
1730 0, /* src_mask */
1731 0xfffc, /* dst_mask */
1732 FALSE), /* pcrel_offset */
1733
1734 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1735 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1736 to the first entry relative to the TOC base (r2). */
1737 HOWTO (R_PPC64_GOT_TLSGD16,
1738 0, /* rightshift */
1739 1, /* size (0 = byte, 1 = short, 2 = long) */
1740 16, /* bitsize */
1741 FALSE, /* pc_relative */
1742 0, /* bitpos */
1743 complain_overflow_signed, /* complain_on_overflow */
1744 ppc64_elf_unhandled_reloc, /* special_function */
1745 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1746 FALSE, /* partial_inplace */
5bd4f169
AM
1747 0, /* src_mask */
1748 0xffff, /* dst_mask */
b34976b6 1749 FALSE), /* pcrel_offset */
5bd4f169 1750
411e1bfb
AM
1751 /* Like GOT_TLSGD16, but no overflow. */
1752 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1753 0, /* rightshift */
1754 1, /* size (0 = byte, 1 = short, 2 = long) */
1755 16, /* bitsize */
b34976b6 1756 FALSE, /* pc_relative */
5bd4f169
AM
1757 0, /* bitpos */
1758 complain_overflow_dont, /* complain_on_overflow */
805fc799 1759 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1760 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1761 FALSE, /* partial_inplace */
5bd4f169
AM
1762 0, /* src_mask */
1763 0xffff, /* dst_mask */
b34976b6 1764 FALSE), /* pcrel_offset */
5bd4f169 1765
411e1bfb
AM
1766 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1767 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1768 16, /* rightshift */
1769 1, /* size (0 = byte, 1 = short, 2 = long) */
1770 16, /* bitsize */
b34976b6 1771 FALSE, /* pc_relative */
5bd4f169 1772 0, /* bitpos */
f9c6b907 1773 complain_overflow_signed, /* complain_on_overflow */
805fc799 1774 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1775 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1776 FALSE, /* partial_inplace */
5bd4f169
AM
1777 0, /* src_mask */
1778 0xffff, /* dst_mask */
b34976b6 1779 FALSE), /* pcrel_offset */
5bd4f169 1780
411e1bfb
AM
1781 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1782 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1783 16, /* rightshift */
1784 1, /* size (0 = byte, 1 = short, 2 = long) */
1785 16, /* bitsize */
b34976b6 1786 FALSE, /* pc_relative */
5bd4f169 1787 0, /* bitpos */
f9c6b907 1788 complain_overflow_signed, /* complain_on_overflow */
805fc799 1789 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1790 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1791 FALSE, /* partial_inplace */
5bd4f169
AM
1792 0, /* src_mask */
1793 0xffff, /* dst_mask */
b34976b6 1794 FALSE), /* pcrel_offset */
5bd4f169 1795
411e1bfb
AM
1796 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1797 with values (sym+add)@dtpmod and zero, and computes the offset to the
1798 first entry relative to the TOC base (r2). */
1799 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1800 0, /* rightshift */
1801 1, /* size (0 = byte, 1 = short, 2 = long) */
1802 16, /* bitsize */
b34976b6 1803 FALSE, /* pc_relative */
5bd4f169 1804 0, /* bitpos */
411e1bfb
AM
1805 complain_overflow_signed, /* complain_on_overflow */
1806 ppc64_elf_unhandled_reloc, /* special_function */
1807 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1808 FALSE, /* partial_inplace */
d006db6c 1809 0, /* src_mask */
411e1bfb 1810 0xffff, /* dst_mask */
b34976b6 1811 FALSE), /* pcrel_offset */
5bd4f169 1812
411e1bfb
AM
1813 /* Like GOT_TLSLD16, but no overflow. */
1814 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1815 0, /* rightshift */
1816 1, /* size (0 = byte, 1 = short, 2 = long) */
1817 16, /* bitsize */
b34976b6 1818 FALSE, /* pc_relative */
5bd4f169 1819 0, /* bitpos */
411e1bfb
AM
1820 complain_overflow_dont, /* complain_on_overflow */
1821 ppc64_elf_unhandled_reloc, /* special_function */
1822 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1823 FALSE, /* partial_inplace */
d006db6c 1824 0, /* src_mask */
411e1bfb 1825 0xffff, /* dst_mask */
b34976b6 1826 FALSE), /* pcrel_offset */
5bd4f169 1827
411e1bfb
AM
1828 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1829 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1830 16, /* rightshift */
5bd4f169
AM
1831 1, /* size (0 = byte, 1 = short, 2 = long) */
1832 16, /* bitsize */
b34976b6 1833 FALSE, /* pc_relative */
5bd4f169 1834 0, /* bitpos */
f9c6b907 1835 complain_overflow_signed, /* complain_on_overflow */
805fc799 1836 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1837 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1838 FALSE, /* partial_inplace */
d006db6c 1839 0, /* src_mask */
411e1bfb 1840 0xffff, /* dst_mask */
b34976b6 1841 FALSE), /* pcrel_offset */
5bd4f169 1842
411e1bfb
AM
1843 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1844 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1845 16, /* rightshift */
5bd4f169
AM
1846 1, /* size (0 = byte, 1 = short, 2 = long) */
1847 16, /* bitsize */
b34976b6 1848 FALSE, /* pc_relative */
5bd4f169 1849 0, /* bitpos */
f9c6b907 1850 complain_overflow_signed, /* complain_on_overflow */
805fc799 1851 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1852 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1853 FALSE, /* partial_inplace */
d006db6c 1854 0, /* src_mask */
411e1bfb 1855 0xffff, /* dst_mask */
b34976b6 1856 FALSE), /* pcrel_offset */
5bd4f169 1857
411e1bfb
AM
1858 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1859 the offset to the entry relative to the TOC base (r2). */
1860 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1861 0, /* rightshift */
1862 1, /* size (0 = byte, 1 = short, 2 = long) */
1863 16, /* bitsize */
b34976b6 1864 FALSE, /* pc_relative */
5bd4f169 1865 0, /* bitpos */
411e1bfb 1866 complain_overflow_signed, /* complain_on_overflow */
805fc799 1867 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1868 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1869 FALSE, /* partial_inplace */
d006db6c 1870 0, /* src_mask */
5bd4f169 1871 0xfffc, /* dst_mask */
b34976b6 1872 FALSE), /* pcrel_offset */
5bd4f169 1873
411e1bfb
AM
1874 /* Like GOT_DTPREL16_DS, but no overflow. */
1875 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1876 0, /* rightshift */
c061c2d8
AM
1877 1, /* size (0 = byte, 1 = short, 2 = long) */
1878 16, /* bitsize */
b34976b6 1879 FALSE, /* pc_relative */
5bd4f169 1880 0, /* bitpos */
411e1bfb
AM
1881 complain_overflow_dont, /* complain_on_overflow */
1882 ppc64_elf_unhandled_reloc, /* special_function */
1883 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1884 FALSE, /* partial_inplace */
d006db6c 1885 0, /* src_mask */
c061c2d8 1886 0xfffc, /* dst_mask */
b34976b6 1887 FALSE), /* pcrel_offset */
5bd4f169 1888
411e1bfb
AM
1889 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1890 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1891 16, /* rightshift */
5bd4f169
AM
1892 1, /* size (0 = byte, 1 = short, 2 = long) */
1893 16, /* bitsize */
b34976b6 1894 FALSE, /* pc_relative */
5bd4f169 1895 0, /* bitpos */
f9c6b907 1896 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1897 ppc64_elf_unhandled_reloc, /* special_function */
1898 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1899 FALSE, /* partial_inplace */
d006db6c 1900 0, /* src_mask */
411e1bfb 1901 0xffff, /* dst_mask */
b34976b6 1902 FALSE), /* pcrel_offset */
5bd4f169 1903
411e1bfb
AM
1904 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1905 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1906 16, /* rightshift */
1907 1, /* size (0 = byte, 1 = short, 2 = long) */
1908 16, /* bitsize */
1909 FALSE, /* pc_relative */
1910 0, /* bitpos */
f9c6b907 1911 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1912 ppc64_elf_unhandled_reloc, /* special_function */
1913 "R_PPC64_GOT_DTPREL16_HA", /* name */
1914 FALSE, /* partial_inplace */
1915 0, /* src_mask */
1916 0xffff, /* dst_mask */
1917 FALSE), /* pcrel_offset */
1918
1919 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1920 offset to the entry relative to the TOC base (r2). */
1921 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1922 0, /* rightshift */
1923 1, /* size (0 = byte, 1 = short, 2 = long) */
1924 16, /* bitsize */
b34976b6 1925 FALSE, /* pc_relative */
5bd4f169
AM
1926 0, /* bitpos */
1927 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1928 ppc64_elf_unhandled_reloc, /* special_function */
1929 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1930 FALSE, /* partial_inplace */
d006db6c 1931 0, /* src_mask */
ad8e1ba5 1932 0xfffc, /* dst_mask */
b34976b6 1933 FALSE), /* pcrel_offset */
5bd4f169 1934
411e1bfb
AM
1935 /* Like GOT_TPREL16_DS, but no overflow. */
1936 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1937 0, /* rightshift */
1938 1, /* size (0 = byte, 1 = short, 2 = long) */
1939 16, /* bitsize */
b34976b6 1940 FALSE, /* pc_relative */
5bd4f169
AM
1941 0, /* bitpos */
1942 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1943 ppc64_elf_unhandled_reloc, /* special_function */
1944 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1945 FALSE, /* partial_inplace */
d006db6c 1946 0, /* src_mask */
ad8e1ba5 1947 0xfffc, /* dst_mask */
b34976b6 1948 FALSE), /* pcrel_offset */
5bd4f169 1949
411e1bfb
AM
1950 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1951 HOWTO (R_PPC64_GOT_TPREL16_HI,
1952 16, /* rightshift */
5bd4f169
AM
1953 1, /* size (0 = byte, 1 = short, 2 = long) */
1954 16, /* bitsize */
b34976b6 1955 FALSE, /* pc_relative */
5bd4f169 1956 0, /* bitpos */
f9c6b907 1957 complain_overflow_signed, /* complain_on_overflow */
805fc799 1958 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1959 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1960 FALSE, /* partial_inplace */
d006db6c 1961 0, /* src_mask */
411e1bfb 1962 0xffff, /* dst_mask */
b34976b6 1963 FALSE), /* pcrel_offset */
5bd4f169 1964
411e1bfb
AM
1965 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1966 HOWTO (R_PPC64_GOT_TPREL16_HA,
1967 16, /* rightshift */
5bd4f169
AM
1968 1, /* size (0 = byte, 1 = short, 2 = long) */
1969 16, /* bitsize */
b34976b6 1970 FALSE, /* pc_relative */
5bd4f169 1971 0, /* bitpos */
f9c6b907 1972 complain_overflow_signed, /* complain_on_overflow */
805fc799 1973 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1974 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1975 FALSE, /* partial_inplace */
d006db6c 1976 0, /* src_mask */
411e1bfb 1977 0xffff, /* dst_mask */
b34976b6 1978 FALSE), /* pcrel_offset */
5bd4f169 1979
25f23106
AM
1980 HOWTO (R_PPC64_JMP_IREL, /* type */
1981 0, /* rightshift */
1982 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1983 0, /* bitsize */
1984 FALSE, /* pc_relative */
1985 0, /* bitpos */
1986 complain_overflow_dont, /* complain_on_overflow */
1987 ppc64_elf_unhandled_reloc, /* special_function */
1988 "R_PPC64_JMP_IREL", /* name */
1989 FALSE, /* partial_inplace */
1990 0, /* src_mask */
1991 0, /* dst_mask */
1992 FALSE), /* pcrel_offset */
1993
e054468f
AM
1994 HOWTO (R_PPC64_IRELATIVE, /* type */
1995 0, /* rightshift */
1996 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1997 64, /* bitsize */
1998 FALSE, /* pc_relative */
1999 0, /* bitpos */
2000 complain_overflow_dont, /* complain_on_overflow */
2001 bfd_elf_generic_reloc, /* special_function */
2002 "R_PPC64_IRELATIVE", /* name */
2003 FALSE, /* partial_inplace */
2004 0, /* src_mask */
2005 ONES (64), /* dst_mask */
2006 FALSE), /* pcrel_offset */
2007
25f23106
AM
2008 /* A 16 bit relative relocation. */
2009 HOWTO (R_PPC64_REL16, /* type */
2010 0, /* rightshift */
2011 1, /* size (0 = byte, 1 = short, 2 = long) */
2012 16, /* bitsize */
2013 TRUE, /* pc_relative */
2014 0, /* bitpos */
b80eed39 2015 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2016 bfd_elf_generic_reloc, /* special_function */
2017 "R_PPC64_REL16", /* name */
2018 FALSE, /* partial_inplace */
2019 0, /* src_mask */
2020 0xffff, /* dst_mask */
2021 TRUE), /* pcrel_offset */
2022
2023 /* A 16 bit relative relocation without overflow. */
2024 HOWTO (R_PPC64_REL16_LO, /* type */
2025 0, /* rightshift */
2026 1, /* size (0 = byte, 1 = short, 2 = long) */
2027 16, /* bitsize */
2028 TRUE, /* pc_relative */
2029 0, /* bitpos */
2030 complain_overflow_dont,/* complain_on_overflow */
2031 bfd_elf_generic_reloc, /* special_function */
2032 "R_PPC64_REL16_LO", /* name */
2033 FALSE, /* partial_inplace */
2034 0, /* src_mask */
2035 0xffff, /* dst_mask */
2036 TRUE), /* pcrel_offset */
2037
2038 /* The high order 16 bits of a relative address. */
2039 HOWTO (R_PPC64_REL16_HI, /* type */
2040 16, /* rightshift */
2041 1, /* size (0 = byte, 1 = short, 2 = long) */
2042 16, /* bitsize */
2043 TRUE, /* pc_relative */
2044 0, /* bitpos */
f9c6b907 2045 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2046 bfd_elf_generic_reloc, /* special_function */
2047 "R_PPC64_REL16_HI", /* name */
2048 FALSE, /* partial_inplace */
2049 0, /* src_mask */
2050 0xffff, /* dst_mask */
2051 TRUE), /* pcrel_offset */
2052
2053 /* The high order 16 bits of a relative address, plus 1 if the contents of
2054 the low 16 bits, treated as a signed number, is negative. */
2055 HOWTO (R_PPC64_REL16_HA, /* type */
2056 16, /* rightshift */
2057 1, /* size (0 = byte, 1 = short, 2 = long) */
2058 16, /* bitsize */
2059 TRUE, /* pc_relative */
2060 0, /* bitpos */
f9c6b907 2061 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2062 ppc64_elf_ha_reloc, /* special_function */
2063 "R_PPC64_REL16_HA", /* name */
2064 FALSE, /* partial_inplace */
2065 0, /* src_mask */
2066 0xffff, /* dst_mask */
2067 TRUE), /* pcrel_offset */
2068
a680de9a
PB
2069 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2070 HOWTO (R_PPC64_REL16DX_HA, /* type */
2071 16, /* rightshift */
2072 2, /* size (0 = byte, 1 = short, 2 = long) */
2073 16, /* bitsize */
2074 TRUE, /* pc_relative */
2075 0, /* bitpos */
2076 complain_overflow_signed, /* complain_on_overflow */
2077 ppc64_elf_ha_reloc, /* special_function */
2078 "R_PPC64_REL16DX_HA", /* name */
2079 FALSE, /* partial_inplace */
2080 0, /* src_mask */
2081 0x1fffc1, /* dst_mask */
2082 TRUE), /* pcrel_offset */
2083
7ba71655
AM
2084 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2085 HOWTO (R_PPC64_16DX_HA, /* type */
2086 16, /* rightshift */
2087 2, /* size (0 = byte, 1 = short, 2 = long) */
2088 16, /* bitsize */
2089 FALSE, /* pc_relative */
2090 0, /* bitpos */
2091 complain_overflow_signed, /* complain_on_overflow */
2092 ppc64_elf_ha_reloc, /* special_function */
2093 "R_PPC64_16DX_HA", /* name */
2094 FALSE, /* partial_inplace */
2095 0, /* src_mask */
2096 0x1fffc1, /* dst_mask */
2097 FALSE), /* pcrel_offset */
2098
f9c6b907
AM
2099 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2100 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2101 16, /* rightshift */
2102 1, /* size (0 = byte, 1 = short, 2 = long) */
2103 16, /* bitsize */
2104 FALSE, /* pc_relative */
2105 0, /* bitpos */
2106 complain_overflow_dont, /* complain_on_overflow */
2107 bfd_elf_generic_reloc, /* special_function */
2108 "R_PPC64_ADDR16_HIGH", /* name */
2109 FALSE, /* partial_inplace */
2110 0, /* src_mask */
2111 0xffff, /* dst_mask */
2112 FALSE), /* pcrel_offset */
2113
2114 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2115 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2116 16, /* rightshift */
2117 1, /* size (0 = byte, 1 = short, 2 = long) */
2118 16, /* bitsize */
2119 FALSE, /* pc_relative */
2120 0, /* bitpos */
2121 complain_overflow_dont, /* complain_on_overflow */
2122 ppc64_elf_ha_reloc, /* special_function */
2123 "R_PPC64_ADDR16_HIGHA", /* name */
2124 FALSE, /* partial_inplace */
2125 0, /* src_mask */
2126 0xffff, /* dst_mask */
2127 FALSE), /* pcrel_offset */
2128
2129 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2130 HOWTO (R_PPC64_DTPREL16_HIGH,
2131 16, /* rightshift */
2132 1, /* size (0 = byte, 1 = short, 2 = long) */
2133 16, /* bitsize */
2134 FALSE, /* pc_relative */
2135 0, /* bitpos */
2136 complain_overflow_dont, /* complain_on_overflow */
2137 ppc64_elf_unhandled_reloc, /* special_function */
2138 "R_PPC64_DTPREL16_HIGH", /* name */
2139 FALSE, /* partial_inplace */
2140 0, /* src_mask */
2141 0xffff, /* dst_mask */
2142 FALSE), /* pcrel_offset */
2143
2144 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2145 HOWTO (R_PPC64_DTPREL16_HIGHA,
2146 16, /* rightshift */
2147 1, /* size (0 = byte, 1 = short, 2 = long) */
2148 16, /* bitsize */
2149 FALSE, /* pc_relative */
2150 0, /* bitpos */
2151 complain_overflow_dont, /* complain_on_overflow */
2152 ppc64_elf_unhandled_reloc, /* special_function */
2153 "R_PPC64_DTPREL16_HIGHA", /* name */
2154 FALSE, /* partial_inplace */
2155 0, /* src_mask */
2156 0xffff, /* dst_mask */
2157 FALSE), /* pcrel_offset */
2158
2159 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2160 HOWTO (R_PPC64_TPREL16_HIGH,
2161 16, /* rightshift */
2162 1, /* size (0 = byte, 1 = short, 2 = long) */
2163 16, /* bitsize */
2164 FALSE, /* pc_relative */
2165 0, /* bitpos */
2166 complain_overflow_dont, /* complain_on_overflow */
2167 ppc64_elf_unhandled_reloc, /* special_function */
2168 "R_PPC64_TPREL16_HIGH", /* name */
2169 FALSE, /* partial_inplace */
2170 0, /* src_mask */
2171 0xffff, /* dst_mask */
2172 FALSE), /* pcrel_offset */
2173
2174 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2175 HOWTO (R_PPC64_TPREL16_HIGHA,
2176 16, /* rightshift */
2177 1, /* size (0 = byte, 1 = short, 2 = long) */
2178 16, /* bitsize */
2179 FALSE, /* pc_relative */
2180 0, /* bitpos */
2181 complain_overflow_dont, /* complain_on_overflow */
2182 ppc64_elf_unhandled_reloc, /* special_function */
2183 "R_PPC64_TPREL16_HIGHA", /* name */
2184 FALSE, /* partial_inplace */
2185 0, /* src_mask */
2186 0xffff, /* dst_mask */
2187 FALSE), /* pcrel_offset */
2188
006589cf
AM
2189 /* Marker reloc on ELFv2 large-model function entry. */
2190 HOWTO (R_PPC64_ENTRY,
2191 0, /* rightshift */
2192 2, /* size (0 = byte, 1 = short, 2 = long) */
2193 32, /* bitsize */
2194 FALSE, /* pc_relative */
2195 0, /* bitpos */
2196 complain_overflow_dont, /* complain_on_overflow */
2197 bfd_elf_generic_reloc, /* special_function */
2198 "R_PPC64_ENTRY", /* name */
2199 FALSE, /* partial_inplace */
2200 0, /* src_mask */
2201 0, /* dst_mask */
2202 FALSE), /* pcrel_offset */
2203
45965137
AM
2204 /* Like ADDR64, but use local entry point of function. */
2205 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2206 0, /* rightshift */
2207 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2208 64, /* bitsize */
2209 FALSE, /* pc_relative */
2210 0, /* bitpos */
2211 complain_overflow_dont, /* complain_on_overflow */
2212 bfd_elf_generic_reloc, /* special_function */
2213 "R_PPC64_ADDR64_LOCAL", /* name */
2214 FALSE, /* partial_inplace */
2215 0, /* src_mask */
2216 ONES (64), /* dst_mask */
2217 FALSE), /* pcrel_offset */
2218
5bd4f169
AM
2219 /* GNU extension to record C++ vtable hierarchy. */
2220 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2221 0, /* rightshift */
2222 0, /* size (0 = byte, 1 = short, 2 = long) */
2223 0, /* bitsize */
b34976b6 2224 FALSE, /* pc_relative */
5bd4f169
AM
2225 0, /* bitpos */
2226 complain_overflow_dont, /* complain_on_overflow */
2227 NULL, /* special_function */
2228 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2229 FALSE, /* partial_inplace */
5bd4f169
AM
2230 0, /* src_mask */
2231 0, /* dst_mask */
b34976b6 2232 FALSE), /* pcrel_offset */
5bd4f169
AM
2233
2234 /* GNU extension to record C++ vtable member usage. */
2235 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2236 0, /* rightshift */
2237 0, /* size (0 = byte, 1 = short, 2 = long) */
2238 0, /* bitsize */
b34976b6 2239 FALSE, /* pc_relative */
5bd4f169
AM
2240 0, /* bitpos */
2241 complain_overflow_dont, /* complain_on_overflow */
2242 NULL, /* special_function */
2243 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2244 FALSE, /* partial_inplace */
5bd4f169
AM
2245 0, /* src_mask */
2246 0, /* dst_mask */
b34976b6 2247 FALSE), /* pcrel_offset */
5bd4f169
AM
2248};
2249
2250\f
2251/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2252 be done. */
2253
2254static void
4ce794b7 2255ppc_howto_init (void)
5bd4f169
AM
2256{
2257 unsigned int i, type;
2258
a4b6fadd 2259 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
2260 {
2261 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 2262 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
2263 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2264 }
2265}
2266
2267static reloc_howto_type *
f3185997 2268ppc64_elf_reloc_type_lookup (bfd *abfd,
4ce794b7 2269 bfd_reloc_code_real_type code)
5bd4f169 2270{
411e1bfb 2271 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2272
2273 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2274 /* Initialize howto table if needed. */
2275 ppc_howto_init ();
2276
4ce794b7 2277 switch (code)
5bd4f169
AM
2278 {
2279 default:
f3185997 2280 /* xgettext:c-format */
e8f5af78 2281 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
f3185997 2282 bfd_set_error (bfd_error_bad_value);
4ce794b7 2283 return NULL;
5bd4f169 2284
411e1bfb
AM
2285 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2286 break;
2287 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2288 break;
2289 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2290 break;
2291 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2292 break;
2293 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2294 break;
2295 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2296 break;
f9c6b907
AM
2297 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2298 break;
411e1bfb 2299 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2300 break;
f9c6b907
AM
2301 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2302 break;
411e1bfb 2303 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2304 break;
411e1bfb 2305 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2306 break;
411e1bfb 2307 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2308 break;
411e1bfb 2309 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2310 break;
411e1bfb 2311 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2312 break;
411e1bfb 2313 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2314 break;
411e1bfb 2315 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2316 break;
411e1bfb 2317 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2318 break;
411e1bfb 2319 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2320 break;
411e1bfb 2321 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2322 break;
411e1bfb 2323 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2324 break;
411e1bfb 2325 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2326 break;
411e1bfb 2327 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2328 break;
411e1bfb 2329 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2330 break;
411e1bfb 2331 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2332 break;
411e1bfb 2333 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2334 break;
411e1bfb 2335 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2336 break;
411e1bfb 2337 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2338 break;
411e1bfb 2339 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2340 break;
411e1bfb 2341 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2342 break;
411e1bfb 2343 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2344 break;
411e1bfb 2345 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2346 break;
411e1bfb 2347 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2348 break;
411e1bfb 2349 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2350 break;
411e1bfb 2351 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2352 break;
411e1bfb 2353 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2354 break;
411e1bfb 2355 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2356 break;
411e1bfb 2357 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2358 break;
411e1bfb 2359 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2360 break;
411e1bfb 2361 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2362 break;
411e1bfb 2363 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2364 break;
411e1bfb 2365 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2366 break;
411e1bfb 2367 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2368 break;
411e1bfb 2369 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2370 break;
411e1bfb 2371 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2372 break;
411e1bfb 2373 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2374 break;
411e1bfb 2375 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2376 break;
411e1bfb 2377 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2378 break;
411e1bfb 2379 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2380 break;
411e1bfb 2381 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2382 break;
411e1bfb 2383 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2384 break;
411e1bfb 2385 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2386 break;
411e1bfb 2387 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2388 break;
411e1bfb 2389 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2390 break;
411e1bfb 2391 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2392 break;
411e1bfb 2393 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2394 break;
411e1bfb 2395 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2396 break;
411e1bfb 2397 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2398 break;
411e1bfb 2399 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2400 break;
411e1bfb 2401 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2402 break;
411e1bfb 2403 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2404 break;
411e1bfb 2405 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2406 break;
411e1bfb 2407 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2408 break;
727fc41e
AM
2409 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2410 break;
2411 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2412 break;
411e1bfb 2413 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2414 break;
411e1bfb 2415 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2416 break;
411e1bfb 2417 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2418 break;
411e1bfb 2419 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2420 break;
f9c6b907
AM
2421 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2422 break;
411e1bfb 2423 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2424 break;
f9c6b907
AM
2425 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2426 break;
411e1bfb 2427 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2428 break;
411e1bfb
AM
2429 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2430 break;
2431 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2432 break;
2433 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2434 break;
f9c6b907
AM
2435 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2436 break;
411e1bfb
AM
2437 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2438 break;
f9c6b907
AM
2439 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2440 break;
411e1bfb
AM
2441 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2442 break;
2443 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2444 break;
2445 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2446 break;
2447 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2448 break;
2449 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2450 break;
2451 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2452 break;
2453 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2454 break;
2455 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2456 break;
2457 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2458 break;
2459 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2460 break;
2461 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2462 break;
2463 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2464 break;
2465 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2466 break;
2467 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2468 break;
2469 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2470 break;
2471 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2472 break;
2473 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2474 break;
2475 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2476 break;
2477 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2478 break;
2479 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2480 break;
2481 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2482 break;
2483 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2484 break;
2485 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2486 break;
2487 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2488 break;
2489 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2490 break;
2491 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2492 break;
2493 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2494 break;
2495 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2496 break;
2497 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2498 break;
25f23106
AM
2499 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2500 break;
2501 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2502 break;
2503 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2504 break;
2505 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2506 break;
7ba71655
AM
2507 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2508 break;
a680de9a
PB
2509 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2510 break;
006589cf
AM
2511 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2512 break;
45965137
AM
2513 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2514 break;
411e1bfb
AM
2515 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2516 break;
2517 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2518 break;
2519 }
2520
4ce794b7 2521 return ppc64_elf_howto_table[r];
5bd4f169
AM
2522};
2523
157090f7
AM
2524static reloc_howto_type *
2525ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2526 const char *r_name)
2527{
2528 unsigned int i;
2529
a4b6fadd 2530 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
2531 if (ppc64_elf_howto_raw[i].name != NULL
2532 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2533 return &ppc64_elf_howto_raw[i];
2534
f3185997 2535
157090f7
AM
2536 return NULL;
2537}
2538
5bd4f169
AM
2539/* Set the howto pointer for a PowerPC ELF reloc. */
2540
f3185997 2541static bfd_boolean
4aef7643 2542ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 2543 Elf_Internal_Rela *dst)
5bd4f169 2544{
65f38f15
AM
2545 unsigned int type;
2546
ef60b7ff 2547 /* Initialize howto table if needed. */
5bd4f169 2548 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2549 ppc_howto_init ();
2550
65f38f15 2551 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 2552 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 2553 {
695344c0 2554 /* xgettext:c-format */
0aa13fee 2555 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 2556 abfd, type);
f3185997
NC
2557 bfd_set_error (bfd_error_bad_value);
2558 return FALSE;
d0fb9a8d 2559 }
65f38f15 2560 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
2561 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
2562 {
2563 /* xgettext:c-format */
2564 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2565 abfd, type);
2566 bfd_set_error (bfd_error_bad_value);
2567 return FALSE;
2568 }
2569
2570 return TRUE;
5bd4f169
AM
2571}
2572
04c9666a 2573/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2574
2575static bfd_reloc_status_type
4ce794b7
AM
2576ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2577 void *data, asection *input_section,
2578 bfd *output_bfd, char **error_message)
5bd4f169 2579{
a680de9a
PB
2580 enum elf_ppc64_reloc_type r_type;
2581 long insn;
2582 bfd_size_type octets;
3de43e7b 2583 bfd_vma value;
a680de9a 2584
805fc799
AM
2585 /* If this is a relocatable link (output_bfd test tells us), just
2586 call the generic function. Any adjustment will be done at final
2587 link time. */
2588 if (output_bfd != NULL)
cedb70c5 2589 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2590 input_section, output_bfd, error_message);
2591
2592 /* Adjust the addend for sign extension of the low 16 bits.
2593 We won't actually be using the low 16 bits, so trashing them
2594 doesn't matter. */
2595 reloc_entry->addend += 0x8000;
a680de9a
PB
2596 r_type = reloc_entry->howto->type;
2597 if (r_type != R_PPC64_REL16DX_HA)
2598 return bfd_reloc_continue;
2599
2600 value = 0;
2601 if (!bfd_is_com_section (symbol->section))
2602 value = symbol->value;
2603 value += (reloc_entry->addend
2604 + symbol->section->output_offset
2605 + symbol->section->output_section->vma);
2606 value -= (reloc_entry->address
2607 + input_section->output_offset
2608 + input_section->output_section->vma);
3de43e7b 2609 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
2610
2611 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2612 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2613 insn &= ~0x1fffc1;
3de43e7b 2614 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 2615 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 2616 if (value + 0x8000 > 0xffff)
a680de9a
PB
2617 return bfd_reloc_overflow;
2618 return bfd_reloc_ok;
805fc799 2619}
5bd4f169 2620
2441e016
AM
2621static bfd_reloc_status_type
2622ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2623 void *data, asection *input_section,
2624 bfd *output_bfd, char **error_message)
2625{
2626 if (output_bfd != NULL)
2627 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2628 input_section, output_bfd, error_message);
2629
699733f6
AM
2630 if (strcmp (symbol->section->name, ".opd") == 0
2631 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2632 {
2633 bfd_vma dest = opd_entry_value (symbol->section,
2634 symbol->value + reloc_entry->addend,
aef36ac1 2635 NULL, NULL, FALSE);
2441e016
AM
2636 if (dest != (bfd_vma) -1)
2637 reloc_entry->addend = dest - (symbol->value
2638 + symbol->section->output_section->vma
2639 + symbol->section->output_offset);
2640 }
810d4e75
AM
2641 else
2642 {
2643 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2644
2645 if (symbol->section->owner != abfd
9f284bf9 2646 && symbol->section->owner != NULL
810d4e75
AM
2647 && abiversion (symbol->section->owner) >= 2)
2648 {
2649 unsigned int i;
2650
2651 for (i = 0; i < symbol->section->owner->symcount; ++i)
2652 {
2653 asymbol *symdef = symbol->section->owner->outsymbols[i];
2654
2655 if (strcmp (symdef->name, symbol->name) == 0)
2656 {
2657 elfsym = (elf_symbol_type *) symdef;
2658 break;
2659 }
2660 }
2661 }
2662 reloc_entry->addend
2663 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2664 }
2441e016
AM
2665 return bfd_reloc_continue;
2666}
2667
805fc799 2668static bfd_reloc_status_type
4ce794b7
AM
2669ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2670 void *data, asection *input_section,
2671 bfd *output_bfd, char **error_message)
805fc799
AM
2672{
2673 long insn;
04c9666a 2674 enum elf_ppc64_reloc_type r_type;
805fc799 2675 bfd_size_type octets;
794e51c0
AM
2676 /* Assume 'at' branch hints. */
2677 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2678
2679 /* If this is a relocatable link (output_bfd test tells us), just
2680 call the generic function. Any adjustment will be done at final
2681 link time. */
5bd4f169 2682 if (output_bfd != NULL)
cedb70c5 2683 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2684 input_section, output_bfd, error_message);
2685
2686 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2687 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2688 insn &= ~(0x01 << 21);
4ce794b7 2689 r_type = reloc_entry->howto->type;
805fc799
AM
2690 if (r_type == R_PPC64_ADDR14_BRTAKEN
2691 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2692 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2693
794e51c0 2694 if (is_isa_v2)
5bd4f169 2695 {
805fc799
AM
2696 /* Set 'a' bit. This is 0b00010 in BO field for branch
2697 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2698 for branch on CTR insns (BO == 1a00t or 1a01t). */
2699 if ((insn & (0x14 << 21)) == (0x04 << 21))
2700 insn |= 0x02 << 21;
2701 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2702 insn |= 0x08 << 21;
2703 else
2441e016 2704 goto out;
5bd4f169 2705 }
805fc799
AM
2706 else
2707 {
2708 bfd_vma target = 0;
2709 bfd_vma from;
5bd4f169 2710
805fc799
AM
2711 if (!bfd_is_com_section (symbol->section))
2712 target = symbol->value;
2713 target += symbol->section->output_section->vma;
2714 target += symbol->section->output_offset;
2715 target += reloc_entry->addend;
5bd4f169 2716
805fc799
AM
2717 from = (reloc_entry->address
2718 + input_section->output_offset
2719 + input_section->output_section->vma);
5bd4f169 2720
805fc799
AM
2721 /* Invert 'y' bit if not the default. */
2722 if ((bfd_signed_vma) (target - from) < 0)
2723 insn ^= 0x01 << 21;
2724 }
4ce794b7 2725 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2726 out:
2727 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2728 input_section, output_bfd, error_message);
805fc799 2729}
5bd4f169 2730
805fc799 2731static bfd_reloc_status_type
4ce794b7
AM
2732ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2733 void *data, asection *input_section,
2734 bfd *output_bfd, char **error_message)
805fc799
AM
2735{
2736 /* If this is a relocatable link (output_bfd test tells us), just
2737 call the generic function. Any adjustment will be done at final
2738 link time. */
2739 if (output_bfd != NULL)
cedb70c5 2740 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2741 input_section, output_bfd, error_message);
5bd4f169 2742
805fc799
AM
2743 /* Subtract the symbol section base address. */
2744 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2745 return bfd_reloc_continue;
2746}
2747
805fc799 2748static bfd_reloc_status_type
4ce794b7
AM
2749ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2750 void *data, asection *input_section,
2751 bfd *output_bfd, char **error_message)
805fc799
AM
2752{
2753 /* If this is a relocatable link (output_bfd test tells us), just
2754 call the generic function. Any adjustment will be done at final
2755 link time. */
2756 if (output_bfd != NULL)
cedb70c5 2757 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2758 input_section, output_bfd, error_message);
2759
2760 /* Subtract the symbol section base address. */
2761 reloc_entry->addend -= symbol->section->output_section->vma;
2762
2763 /* Adjust the addend for sign extension of the low 16 bits. */
2764 reloc_entry->addend += 0x8000;
2765 return bfd_reloc_continue;
2766}
2767
2768static bfd_reloc_status_type
4ce794b7
AM
2769ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2770 void *data, asection *input_section,
2771 bfd *output_bfd, char **error_message)
805fc799
AM
2772{
2773 bfd_vma TOCstart;
2774
2775 /* If this is a relocatable link (output_bfd test tells us), just
2776 call the generic function. Any adjustment will be done at final
2777 link time. */
2778 if (output_bfd != NULL)
cedb70c5 2779 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2780 input_section, output_bfd, error_message);
2781
2782 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2783 if (TOCstart == 0)
1c865ab2 2784 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2785
2786 /* Subtract the TOC base address. */
2787 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2788 return bfd_reloc_continue;
2789}
2790
2791static bfd_reloc_status_type
4ce794b7
AM
2792ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2793 void *data, asection *input_section,
2794 bfd *output_bfd, char **error_message)
805fc799
AM
2795{
2796 bfd_vma TOCstart;
2797
2798 /* If this is a relocatable link (output_bfd test tells us), just
2799 call the generic function. Any adjustment will be done at final
2800 link time. */
2801 if (output_bfd != NULL)
cedb70c5 2802 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2803 input_section, output_bfd, error_message);
2804
2805 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2806 if (TOCstart == 0)
1c865ab2 2807 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2808
2809 /* Subtract the TOC base address. */
2810 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2811
2812 /* Adjust the addend for sign extension of the low 16 bits. */
2813 reloc_entry->addend += 0x8000;
2814 return bfd_reloc_continue;
2815}
2816
2817static bfd_reloc_status_type
4ce794b7
AM
2818ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2819 void *data, asection *input_section,
2820 bfd *output_bfd, char **error_message)
805fc799
AM
2821{
2822 bfd_vma TOCstart;
2823 bfd_size_type octets;
2824
2825 /* If this is a relocatable link (output_bfd test tells us), just
2826 call the generic function. Any adjustment will be done at final
2827 link time. */
2828 if (output_bfd != NULL)
cedb70c5 2829 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2830 input_section, output_bfd, error_message);
2831
2832 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2833 if (TOCstart == 0)
1c865ab2 2834 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2835
2836 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2837 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2838 return bfd_reloc_ok;
2839}
2840
2841static bfd_reloc_status_type
4ce794b7
AM
2842ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2843 void *data, asection *input_section,
2844 bfd *output_bfd, char **error_message)
805fc799
AM
2845{
2846 /* If this is a relocatable link (output_bfd test tells us), just
2847 call the generic function. Any adjustment will be done at final
2848 link time. */
2849 if (output_bfd != NULL)
cedb70c5 2850 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2851 input_section, output_bfd, error_message);
2852
2853 if (error_message != NULL)
2854 {
2855 static char buf[60];
2856 sprintf (buf, "generic linker can't handle %s",
2857 reloc_entry->howto->name);
2858 *error_message = buf;
2859 }
2860 return bfd_reloc_dangerous;
2861}
2862
927be08e
AM
2863/* Track GOT entries needed for a given symbol. We might need more
2864 than one got entry per symbol. */
2865struct got_entry
2866{
2867 struct got_entry *next;
2868
2869 /* The symbol addend that we'll be placing in the GOT. */
2870 bfd_vma addend;
2871
2872 /* Unlike other ELF targets, we use separate GOT entries for the same
2873 symbol referenced from different input files. This is to support
2874 automatic multiple TOC/GOT sections, where the TOC base can vary
2875 from one input file to another. After partitioning into TOC groups
2876 we merge entries within the group.
2877
2878 Point to the BFD owning this GOT entry. */
2879 bfd *owner;
2880
2881 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2882 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2883 unsigned char tls_type;
927be08e
AM
2884
2885 /* Non-zero if got.ent points to real entry. */
f961d9dd 2886 unsigned char is_indirect;
927be08e
AM
2887
2888 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2889 union
2890 {
2891 bfd_signed_vma refcount;
2892 bfd_vma offset;
2893 struct got_entry *ent;
2894 } got;
2895};
2896
2897/* The same for PLT. */
2898struct plt_entry
2899{
2900 struct plt_entry *next;
2901
2902 bfd_vma addend;
2903
2904 union
2905 {
2906 bfd_signed_vma refcount;
2907 bfd_vma offset;
2908 } plt;
2909};
2910
e717da7e
AM
2911struct ppc64_elf_obj_tdata
2912{
2913 struct elf_obj_tdata elf;
2914
2915 /* Shortcuts to dynamic linker sections. */
2916 asection *got;
2917 asection *relgot;
2918
b3fac117
AM
2919 /* Used during garbage collection. We attach global symbols defined
2920 on removed .opd entries to this section so that the sym is removed. */
2921 asection *deleted_section;
81688140 2922
927be08e 2923 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2924 sections means we potentially need one of these for each input bfd. */
927be08e 2925 struct got_entry tlsld_got;
8860955f 2926
729eabd5
AM
2927 union {
2928 /* A copy of relocs before they are modified for --emit-relocs. */
2929 Elf_Internal_Rela *relocs;
2930
2931 /* Section contents. */
2932 bfd_byte *contents;
2933 } opd;
d77c8a4b
AM
2934
2935 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2936 the reloc to be in the range -32768 to 32767. */
98528052
AM
2937 unsigned int has_small_toc_reloc : 1;
2938
560c8763
AM
2939 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2940 instruction not one we handle. */
2941 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2942};
2943
2944#define ppc64_elf_tdata(bfd) \
2945 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2946
2947#define ppc64_tlsld_got(bfd) \
2948 (&ppc64_elf_tdata (bfd)->tlsld_got)
2949
0c8d6e5c
AM
2950#define is_ppc64_elf(bfd) \
2951 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2952 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2953
e717da7e
AM
2954/* Override the generic function because we store some extras. */
2955
2956static bfd_boolean
2957ppc64_elf_mkobject (bfd *abfd)
2958{
0ffa91dd 2959 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2960 PPC64_ELF_DATA);
e717da7e
AM
2961}
2962
feee612b 2963/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 2964 default is 32 bit. Also select arch based on apuinfo. */
feee612b 2965
b34976b6 2966static bfd_boolean
4ce794b7 2967ppc64_elf_object_p (bfd *abfd)
feee612b 2968{
14b57c7c
AM
2969 if (!abfd->arch_info->the_default)
2970 return TRUE;
2971
2972 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
2973 {
2974 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2975
2976 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2977 {
2978 /* Relies on arch after 32 bit default being 64 bit default. */
2979 abfd->arch_info = abfd->arch_info->next;
2980 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2981 }
2982 }
14b57c7c 2983 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
2984}
2985
d37c89e5
AM
2986/* Support for core dump NOTE sections. */
2987
2988static bfd_boolean
2989ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2990{
eea6121a 2991 size_t offset, size;
d37c89e5
AM
2992
2993 if (note->descsz != 504)
2994 return FALSE;
2995
2996 /* pr_cursig */
228e534f 2997 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2998
2999 /* pr_pid */
228e534f 3000 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
3001
3002 /* pr_reg */
3003 offset = 112;
eea6121a 3004 size = 384;
d37c89e5
AM
3005
3006 /* Make a ".reg/999" section. */
3007 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 3008 size, note->descpos + offset);
d37c89e5
AM
3009}
3010
3011static bfd_boolean
3012ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3013{
3014 if (note->descsz != 136)
3015 return FALSE;
3016
228e534f 3017 elf_tdata (abfd)->core->pid
bc989cdc 3018 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 3019 elf_tdata (abfd)->core->program
d37c89e5 3020 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 3021 elf_tdata (abfd)->core->command
d37c89e5
AM
3022 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
3023
3024 return TRUE;
3025}
3026
183e98be
AM
3027static char *
3028ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
3029 ...)
3030{
3031 switch (note_type)
3032 {
3033 default:
3034 return NULL;
3035
3036 case NT_PRPSINFO:
3037 {
9ef6d1e3 3038 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
3039 va_list ap;
3040
3041 va_start (ap, note_type);
75cd47ed 3042 memset (data, 0, sizeof (data));
183e98be 3043 strncpy (data + 40, va_arg (ap, const char *), 16);
95da9854
L
3044 DIAGNOSTIC_PUSH;
3045 /* GCC 8.1 warns about 80 equals destination size with
3046 -Wstringop-truncation:
3047 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
3048 */
3049#if GCC_VERSION == 8001
3050 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
3051#endif
183e98be 3052 strncpy (data + 56, va_arg (ap, const char *), 80);
95da9854 3053 DIAGNOSTIC_POP;
183e98be
AM
3054 va_end (ap);
3055 return elfcore_write_note (abfd, buf, bufsiz,
3056 "CORE", note_type, data, sizeof (data));
3057 }
3058
3059 case NT_PRSTATUS:
3060 {
3061 char data[504];
3062 va_list ap;
3063 long pid;
3064 int cursig;
3065 const void *greg;
3066
3067 va_start (ap, note_type);
3068 memset (data, 0, 112);
3069 pid = va_arg (ap, long);
3070 bfd_put_32 (abfd, pid, data + 32);
3071 cursig = va_arg (ap, int);
3072 bfd_put_16 (abfd, cursig, data + 12);
3073 greg = va_arg (ap, const void *);
3074 memcpy (data + 112, greg, 384);
3075 memset (data + 496, 0, 8);
3076 va_end (ap);
3077 return elfcore_write_note (abfd, buf, bufsiz,
3078 "CORE", note_type, data, sizeof (data));
3079 }
3080 }
3081}
3082
5d35169e
AM
3083/* Add extra PPC sections. */
3084
b35d266b 3085static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 3086{
07d6d2b8
AM
3087 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3088 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3089 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
3090 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3091 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3092 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 3093 { NULL, 0, 0, 0, 0 }
5d35169e
AM
3094};
3095
7c8fe5c4
AM
3096enum _ppc64_sec_type {
3097 sec_normal = 0,
3098 sec_opd = 1,
3099 sec_toc = 2
3100};
3101
f0abc2a1
AM
3102struct _ppc64_elf_section_data
3103{
3104 struct bfd_elf_section_data elf;
411e1bfb 3105
f0abc2a1
AM
3106 union
3107 {
51aecdc5
AM
3108 /* An array with one entry for each opd function descriptor,
3109 and some spares since opd entries may be either 16 or 24 bytes. */
3110#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
3111 struct _opd_sec_data
3112 {
3113 /* Points to the function code section for local opd entries. */
3114 asection **func_sec;
3115
3116 /* After editing .opd, adjust references to opd local syms. */
3117 long *adjust;
3118 } opd;
7c8fe5c4 3119
3a71aa26
AM
3120 /* An array for toc sections, indexed by offset/8. */
3121 struct _toc_sec_data
3122 {
3123 /* Specifies the relocation symbol index used at a given toc offset. */
3124 unsigned *symndx;
3125
3126 /* And the relocation addend. */
3127 bfd_vma *add;
3128 } toc;
7c8fe5c4
AM
3129 } u;
3130
3131 enum _ppc64_sec_type sec_type:2;
411e1bfb 3132
7c8fe5c4
AM
3133 /* Flag set when small branches are detected. Used to
3134 select suitable defaults for the stub group size. */
3135 unsigned int has_14bit_branch:1;
3e04d765
AM
3136
3137 /* Flag set when PLTCALL relocs are detected. */
3138 unsigned int has_pltcall:1;
f0abc2a1
AM
3139};
3140
3141#define ppc64_elf_section_data(sec) \
411e1bfb 3142 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
3143
3144static bfd_boolean
4ce794b7 3145ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 3146{
f592407e
AM
3147 if (!sec->used_by_bfd)
3148 {
3149 struct _ppc64_elf_section_data *sdata;
3150 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 3151
f592407e
AM
3152 sdata = bfd_zalloc (abfd, amt);
3153 if (sdata == NULL)
3154 return FALSE;
3155 sec->used_by_bfd = sdata;
3156 }
f0abc2a1
AM
3157
3158 return _bfd_elf_new_section_hook (abfd, sec);
3159}
4025353c 3160
74f0fb50 3161static struct _opd_sec_data *
4025353c
AM
3162get_opd_info (asection * sec)
3163{
3164 if (sec != NULL
3165 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3166 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3167 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3168 return NULL;
3169}
90e3cdf2
JJ
3170\f
3171/* Parameters for the qsort hook. */
90e3cdf2 3172static bfd_boolean synthetic_relocatable;
cd285db5 3173static asection *synthetic_opd;
90e3cdf2 3174
699733f6 3175/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3176
3177static int
3178compare_symbols (const void *ap, const void *bp)
3179{
3180 const asymbol *a = * (const asymbol **) ap;
3181 const asymbol *b = * (const asymbol **) bp;
3182
699733f6
AM
3183 /* Section symbols first. */
3184 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3185 return -1;
699733f6 3186 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3187 return 1;
3188
699733f6 3189 /* then .opd symbols. */
cd285db5
AM
3190 if (synthetic_opd != NULL)
3191 {
3192 if (strcmp (a->section->name, ".opd") == 0
3193 && strcmp (b->section->name, ".opd") != 0)
3194 return -1;
3195 if (strcmp (a->section->name, ".opd") != 0
3196 && strcmp (b->section->name, ".opd") == 0)
3197 return 1;
3198 }
90e3cdf2 3199
699733f6 3200 /* then other code symbols. */
90e3cdf2
JJ
3201 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3202 == (SEC_CODE | SEC_ALLOC)
3203 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3204 != (SEC_CODE | SEC_ALLOC))
3205 return -1;
3206
3207 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3208 != (SEC_CODE | SEC_ALLOC)
3209 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3210 == (SEC_CODE | SEC_ALLOC))
3211 return 1;
3212
3213 if (synthetic_relocatable)
3214 {
3215 if (a->section->id < b->section->id)
3216 return -1;
3217
3218 if (a->section->id > b->section->id)
3219 return 1;
3220 }
3221
3222 if (a->value + a->section->vma < b->value + b->section->vma)
3223 return -1;
3224
3225 if (a->value + a->section->vma > b->value + b->section->vma)
3226 return 1;
3227
4d35a0aa
AM
3228 /* For syms with the same value, prefer strong dynamic global function
3229 syms over other syms. */
3230 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3231 return -1;
3232
3233 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3234 return 1;
3235
3236 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3237 return -1;
3238
3239 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3240 return 1;
3241
3242 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3243 return -1;
3244
3245 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3246 return 1;
3247
3248 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3249 return -1;
3250
3251 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3252 return 1;
3253
aaed6f5b 3254 return a > b;
90e3cdf2
JJ
3255}
3256
699733f6 3257/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3258
699733f6 3259static asymbol *
7292b3ac 3260sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3261{
699733f6 3262 long mid;
90e3cdf2 3263
7292b3ac 3264 if (id == (unsigned) -1)
699733f6
AM
3265 {
3266 while (lo < hi)
3267 {
3268 mid = (lo + hi) >> 1;
3269 if (syms[mid]->value + syms[mid]->section->vma < value)
3270 lo = mid + 1;
3271 else if (syms[mid]->value + syms[mid]->section->vma > value)
3272 hi = mid;
3273 else
3274 return syms[mid];
3275 }
3276 }
3277 else
3278 {
3279 while (lo < hi)
3280 {
3281 mid = (lo + hi) >> 1;
3282 if (syms[mid]->section->id < id)
3283 lo = mid + 1;
3284 else if (syms[mid]->section->id > id)
3285 hi = mid;
3286 else if (syms[mid]->value < value)
3287 lo = mid + 1;
3288 else if (syms[mid]->value > value)
3289 hi = mid;
3290 else
3291 return syms[mid];
3292 }
3293 }
3294 return NULL;
90e3cdf2
JJ
3295}
3296
468392fb
AM
3297static bfd_boolean
3298section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3299{
3300 bfd_vma vma = *(bfd_vma *) ptr;
3301 return ((section->flags & SEC_ALLOC) != 0
3302 && section->vma <= vma
3303 && vma < section->vma + section->size);
3304}
3305
699733f6 3306/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3307 entry syms. Also generate @plt symbols for the glink branch table.
3308 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3309
3310static long
a7535cf3
AM
3311ppc64_elf_get_synthetic_symtab (bfd *abfd,
3312 long static_count, asymbol **static_syms,
3313 long dyn_count, asymbol **dyn_syms,
c9727e01 3314 asymbol **ret)
90e3cdf2
JJ
3315{
3316 asymbol *s;
0ccf57bd 3317 size_t i, j, count;
90e3cdf2 3318 char *names;
0ccf57bd 3319 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3320 asection *opd = NULL;
90e3cdf2 3321 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3322 asymbol **syms;
ee67d69a 3323 int abi = abiversion (abfd);
90e3cdf2
JJ
3324
3325 *ret = NULL;
3326
ee67d69a
AM
3327 if (abi < 2)
3328 {
3329 opd = bfd_get_section_by_name (abfd, ".opd");
3330 if (opd == NULL && abi == 1)
3331 return 0;
3332 }
90e3cdf2 3333
a5259595
AM
3334 syms = NULL;
3335 codesecsym = 0;
3336 codesecsymend = 0;
3337 secsymend = 0;
3338 opdsymend = 0;
3339 symcount = 0;
3340 if (opd != NULL)
c9727e01 3341 {
a5259595
AM
3342 symcount = static_count;
3343 if (!relocatable)
3344 symcount += dyn_count;
3345 if (symcount == 0)
3346 return 0;
c9727e01 3347
a5259595
AM
3348 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3349 if (syms == NULL)
3350 return -1;
90e3cdf2 3351
a5259595
AM
3352 if (!relocatable && static_count != 0 && dyn_count != 0)
3353 {
3354 /* Use both symbol tables. */
3355 memcpy (syms, static_syms, static_count * sizeof (*syms));
3356 memcpy (syms + static_count, dyn_syms,
3357 (dyn_count + 1) * sizeof (*syms));
3358 }
3359 else if (!relocatable && static_count == 0)
3360 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3361 else
3362 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 3363
0ccf57bd
AM
3364 /* Trim uninteresting symbols. Interesting symbols are section,
3365 function, and notype symbols. */
3366 for (i = 0, j = 0; i < symcount; ++i)
3367 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3368 | BSF_RELC | BSF_SRELC)) == 0)
3369 syms[j++] = syms[i];
3370 symcount = j;
3371
a5259595
AM
3372 synthetic_relocatable = relocatable;
3373 synthetic_opd = opd;
3374 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3375
a5259595
AM
3376 if (!relocatable && symcount > 1)
3377 {
bfa5bd2a
PA
3378 /* Trim duplicate syms, since we may have merged the normal
3379 and dynamic symbols. Actually, we only care about syms
3380 that have different values, so trim any with the same
3381 value. Don't consider ifunc and ifunc resolver symbols
3382 duplicates however, because GDB wants to know whether a
3383 text symbol is an ifunc resolver. */
a5259595 3384 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
3385 {
3386 const asymbol *s0 = syms[i - 1];
3387 const asymbol *s1 = syms[i];
3388
3389 if ((s0->value + s0->section->vma
3390 != s1->value + s1->section->vma)
3391 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
3392 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
3393 syms[j++] = syms[i];
3394 }
a5259595
AM
3395 symcount = j;
3396 }
699733f6 3397
a5259595
AM
3398 i = 0;
3399 /* Note that here and in compare_symbols we can't compare opd and
3400 sym->section directly. With separate debug info files, the
3401 symbols will be extracted from the debug file while abfd passed
3402 to this function is the real binary. */
0ccf57bd 3403 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
3404 ++i;
3405 codesecsym = i;
3406
3407 for (; i < symcount; ++i)
3408 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3409 | SEC_THREAD_LOCAL))
3410 != (SEC_CODE | SEC_ALLOC))
3411 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3412 break;
3413 codesecsymend = i;
3414
3415 for (; i < symcount; ++i)
3416 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3417 break;
3418 secsymend = i;
3419
3420 for (; i < symcount; ++i)
3421 if (strcmp (syms[i]->section->name, ".opd") != 0)
3422 break;
3423 opdsymend = i;
3424
3425 for (; i < symcount; ++i)
3426 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3427 != (SEC_CODE | SEC_ALLOC))
3428 break;
3429 symcount = i;
3430 }
c9727e01 3431 count = 0;
90e3cdf2 3432
699733f6 3433 if (relocatable)
90e3cdf2 3434 {
699733f6
AM
3435 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3436 arelent *r;
3437 size_t size;
0ccf57bd 3438 size_t relcount;
90e3cdf2 3439
468392fb
AM
3440 if (opdsymend == secsymend)
3441 goto done;
3442
699733f6 3443 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3444 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3445 if (relcount == 0)
c9727e01 3446 goto done;
90e3cdf2 3447
7356fed5
AM
3448 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3449 {
3450 count = -1;
3451 goto done;
3452 }
3453
699733f6 3454 size = 0;
595da8c5 3455 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3456 {
3457 asymbol *sym;
90e3cdf2 3458
595da8c5 3459 while (r < opd->relocation + relcount
699733f6
AM
3460 && r->address < syms[i]->value + opd->vma)
3461 ++r;
90e3cdf2 3462
595da8c5 3463 if (r == opd->relocation + relcount)
699733f6 3464 break;
90e3cdf2 3465
699733f6
AM
3466 if (r->address != syms[i]->value + opd->vma)
3467 continue;
90e3cdf2 3468
699733f6
AM
3469 if (r->howto->type != R_PPC64_ADDR64)
3470 continue;
90e3cdf2 3471
699733f6
AM
3472 sym = *r->sym_ptr_ptr;
3473 if (!sym_exists_at (syms, opdsymend, symcount,
3474 sym->section->id, sym->value + r->addend))
3475 {
3476 ++count;
3477 size += sizeof (asymbol);
3478 size += strlen (syms[i]->name) + 2;
3479 }
3480 }
90e3cdf2 3481
c4b0b099
AM
3482 if (size == 0)
3483 goto done;
699733f6
AM
3484 s = *ret = bfd_malloc (size);
3485 if (s == NULL)
3486 {
7356fed5 3487 count = -1;
c9727e01 3488 goto done;
699733f6 3489 }
90e3cdf2 3490
699733f6 3491 names = (char *) (s + count);
90e3cdf2 3492
595da8c5 3493 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3494 {
699733f6 3495 asymbol *sym;
90e3cdf2 3496
595da8c5 3497 while (r < opd->relocation + relcount
699733f6
AM
3498 && r->address < syms[i]->value + opd->vma)
3499 ++r;
90e3cdf2 3500
595da8c5 3501 if (r == opd->relocation + relcount)
699733f6
AM
3502 break;
3503
3504 if (r->address != syms[i]->value + opd->vma)
3505 continue;
3506
3507 if (r->howto->type != R_PPC64_ADDR64)
3508 continue;
90e3cdf2 3509
699733f6
AM
3510 sym = *r->sym_ptr_ptr;
3511 if (!sym_exists_at (syms, opdsymend, symcount,
3512 sym->section->id, sym->value + r->addend))
3513 {
3514 size_t len;
3515
3516 *s = *syms[i];
6ba2a415 3517 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3518 s->section = sym->section;
3519 s->value = sym->value + r->addend;
3520 s->name = names;
3521 *names++ = '.';
3522 len = strlen (syms[i]->name);
3523 memcpy (names, syms[i]->name, len + 1);
3524 names += len + 1;
6f610d07
UW
3525 /* Have udata.p point back to the original symbol this
3526 synthetic symbol was derived from. */
3527 s->udata.p = syms[i];
699733f6
AM
3528 s++;
3529 }
3530 }
3531 }
3532 else
90e3cdf2 3533 {
468392fb 3534 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3535 bfd_byte *contents = NULL;
699733f6 3536 size_t size;
0ccf57bd 3537 size_t plt_count = 0;
468392fb
AM
3538 bfd_vma glink_vma = 0, resolv_vma = 0;
3539 asection *dynamic, *glink = NULL, *relplt = NULL;
3540 arelent *p;
90e3cdf2 3541
ee67d69a 3542 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3543 {
c4b0b099
AM
3544 free_contents_and_exit_err:
3545 count = -1;
ee67d69a 3546 free_contents_and_exit:
699733f6 3547 if (contents)
ee67d69a 3548 free (contents);
c9727e01 3549 goto done;
699733f6 3550 }
90e3cdf2 3551
699733f6
AM
3552 size = 0;
3553 for (i = secsymend; i < opdsymend; ++i)
3554 {
3555 bfd_vma ent;
90e3cdf2 3556
5ef11c02
AM
3557 /* Ignore bogus symbols. */
3558 if (syms[i]->value > opd->size - 8)
3559 continue;
3560
699733f6
AM
3561 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3562 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3563 {
3564 ++count;
3565 size += sizeof (asymbol);
3566 size += strlen (syms[i]->name) + 2;
3567 }
3568 }
90e3cdf2 3569
468392fb 3570 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3571 if (dyn_count != 0
3572 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3573 {
3574 bfd_byte *dynbuf, *extdyn, *extdynend;
3575 size_t extdynsize;
3576 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3577
3578 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3579 goto free_contents_and_exit_err;
468392fb
AM
3580
3581 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3582 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3583
3584 extdyn = dynbuf;
3585 extdynend = extdyn + dynamic->size;
3586 for (; extdyn < extdynend; extdyn += extdynsize)
3587 {
3588 Elf_Internal_Dyn dyn;
3589 (*swap_dyn_in) (abfd, extdyn, &dyn);
3590
3591 if (dyn.d_tag == DT_NULL)
3592 break;
3593
3594 if (dyn.d_tag == DT_PPC64_GLINK)
3595 {
9e390558
AM
3596 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3597 See comment in ppc64_elf_finish_dynamic_sections. */
3598 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
3599 /* The .glink section usually does not survive the final
3600 link; search for the section (usually .text) where the
3601 glink stubs now reside. */
3602 glink = bfd_sections_find_if (abfd, section_covers_vma,
3603 &glink_vma);
3604 break;
3605 }
3606 }
3607
3608 free (dynbuf);
3609 }
3610
3611 if (glink != NULL)
3612 {
3613 /* Determine __glink trampoline by reading the relative branch
3614 from the first glink stub. */
3615 bfd_byte buf[4];
b9e5796b
AM
3616 unsigned int off = 0;
3617
3618 while (bfd_get_section_contents (abfd, glink, buf,
3619 glink_vma + off - glink->vma, 4))
468392fb
AM
3620 {
3621 unsigned int insn = bfd_get_32 (abfd, buf);
3622 insn ^= B_DOT;
3623 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3624 {
3625 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3626 break;
3627 }
3628 off += 4;
3629 if (off > 4)
3630 break;
468392fb
AM
3631 }
3632
3633 if (resolv_vma)
3634 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3635
066ee829
AM
3636 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3637 if (relplt != NULL)
3638 {
3639 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3640 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3641 goto free_contents_and_exit_err;
68ffbac6 3642
066ee829
AM
3643 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3644 size += plt_count * sizeof (asymbol);
468392fb 3645
066ee829
AM
3646 p = relplt->relocation;
3647 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3648 {
3649 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3650 if (p->addend != 0)
3651 size += sizeof ("+0x") - 1 + 16;
3652 }
066ee829 3653 }
468392fb
AM
3654 }
3655
c4b0b099
AM
3656 if (size == 0)
3657 goto free_contents_and_exit;
699733f6
AM
3658 s = *ret = bfd_malloc (size);
3659 if (s == NULL)
c4b0b099 3660 goto free_contents_and_exit_err;
90e3cdf2 3661
468392fb 3662 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3663
699733f6 3664 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3665 {
699733f6 3666 bfd_vma ent;
90e3cdf2 3667
5ef11c02
AM
3668 if (syms[i]->value > opd->size - 8)
3669 continue;
3670
699733f6
AM
3671 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3672 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3673 {
0ccf57bd 3674 size_t lo, hi;
699733f6 3675 size_t len;
c9727e01 3676 asection *sec = abfd->sections;
90e3cdf2 3677
699733f6
AM
3678 *s = *syms[i];
3679 lo = codesecsym;
3680 hi = codesecsymend;
3681 while (lo < hi)
3682 {
0ccf57bd 3683 size_t mid = (lo + hi) >> 1;
699733f6
AM
3684 if (syms[mid]->section->vma < ent)
3685 lo = mid + 1;
3686 else if (syms[mid]->section->vma > ent)
3687 hi = mid;
3688 else
c9727e01
AM
3689 {
3690 sec = syms[mid]->section;
3691 break;
3692 }
699733f6
AM
3693 }
3694
c9727e01 3695 if (lo >= hi && lo > codesecsym)
699733f6 3696 sec = syms[lo - 1]->section;
699733f6
AM
3697
3698 for (; sec != NULL; sec = sec->next)
3699 {
3700 if (sec->vma > ent)
3701 break;
63524580
JK
3702 /* SEC_LOAD may not be set if SEC is from a separate debug
3703 info file. */
3704 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3705 break;
3706 if ((sec->flags & SEC_CODE) != 0)
3707 s->section = sec;
3708 }
6ba2a415 3709 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3710 s->value = ent - s->section->vma;
3711 s->name = names;
3712 *names++ = '.';
3713 len = strlen (syms[i]->name);
3714 memcpy (names, syms[i]->name, len + 1);
3715 names += len + 1;
6f610d07
UW
3716 /* Have udata.p point back to the original symbol this
3717 synthetic symbol was derived from. */
3718 s->udata.p = syms[i];
699733f6 3719 s++;
90e3cdf2 3720 }
90e3cdf2 3721 }
699733f6 3722 free (contents);
468392fb
AM
3723
3724 if (glink != NULL && relplt != NULL)
3725 {
3726 if (resolv_vma)
3727 {
3728 /* Add a symbol for the main glink trampoline. */
86a4952b 3729 memset (s, 0, sizeof *s);
468392fb 3730 s->the_bfd = abfd;
6ba2a415 3731 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3732 s->section = glink;
3733 s->value = resolv_vma - glink->vma;
3734 s->name = names;
3735 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3736 names += sizeof ("__glink_PLTresolve");
3737 s++;
3738 count++;
3739 }
3740
3741 /* FIXME: It would be very much nicer to put sym@plt on the
3742 stub rather than on the glink branch table entry. The
3743 objdump disassembler would then use a sensible symbol
3744 name on plt calls. The difficulty in doing so is
3745 a) finding the stubs, and,
3746 b) matching stubs against plt entries, and,
3747 c) there can be multiple stubs for a given plt entry.
3748
3749 Solving (a) could be done by code scanning, but older
3750 ppc64 binaries used different stubs to current code.
3751 (b) is the tricky one since you need to known the toc
3752 pointer for at least one function that uses a pic stub to
3753 be able to calculate the plt address referenced.
3754 (c) means gdb would need to set multiple breakpoints (or
3755 find the glink branch itself) when setting breakpoints
3756 for pending shared library loads. */
3757 p = relplt->relocation;
3758 for (i = 0; i < plt_count; i++, p++)
3759 {
3760 size_t len;
3761
3762 *s = **p->sym_ptr_ptr;
3763 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3764 we are defining a symbol, ensure one of them is set. */
3765 if ((s->flags & BSF_LOCAL) == 0)
3766 s->flags |= BSF_GLOBAL;
6ba2a415 3767 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3768 s->section = glink;
3769 s->value = glink_vma - glink->vma;
3770 s->name = names;
3771 s->udata.p = NULL;
3772 len = strlen ((*p->sym_ptr_ptr)->name);
3773 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3774 names += len;
e054468f
AM
3775 if (p->addend != 0)
3776 {
3777 memcpy (names, "+0x", sizeof ("+0x") - 1);
3778 names += sizeof ("+0x") - 1;
3779 bfd_sprintf_vma (abfd, names, p->addend);
3780 names += strlen (names);
3781 }
468392fb
AM
3782 memcpy (names, "@plt", sizeof ("@plt"));
3783 names += sizeof ("@plt");
3784 s++;
b9e5796b
AM
3785 if (abi < 2)
3786 {
3787 glink_vma += 8;
3788 if (i >= 0x8000)
3789 glink_vma += 4;
3790 }
3791 else
468392fb
AM
3792 glink_vma += 4;
3793 }
3794 count += plt_count;
3795 }
90e3cdf2
JJ
3796 }
3797
c9727e01 3798 done:
a7535cf3 3799 free (syms);
90e3cdf2
JJ
3800 return count;
3801}
5bd4f169 3802\f
65f38f15
AM
3803/* The following functions are specific to the ELF linker, while
3804 functions above are used generally. Those named ppc64_elf_* are
3805 called by the main ELF linker code. They appear in this file more
3806 or less in the order in which they are called. eg.
3807 ppc64_elf_check_relocs is called early in the link process,
3808 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3809 called.
3810
3811 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3812 functions have both a function code symbol and a function descriptor
3813 symbol. A call to foo in a relocatable object file looks like:
3814
3815 . .text
3816 . x:
3817 . bl .foo
3818 . nop
3819
3820 The function definition in another object file might be:
3821
3822 . .section .opd
3823 . foo: .quad .foo
3824 . .quad .TOC.@tocbase
3825 . .quad 0
3826 .
3827 . .text
3828 . .foo: blr
3829
3830 When the linker resolves the call during a static link, the branch
3831 unsurprisingly just goes to .foo and the .opd information is unused.
3832 If the function definition is in a shared library, things are a little
3833 different: The call goes via a plt call stub, the opd information gets
3834 copied to the plt, and the linker patches the nop.
3835
3836 . x:
3837 . bl .foo_stub
3838 . ld 2,40(1)
3839 .
3840 .
3841 . .foo_stub:
71a39c98
AM
3842 . std 2,40(1) # in practice, the call stub
3843 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3844 . addi 11,11,Lfoo@toc@l # this is the general idea
3845 . ld 12,0(11)
3846 . ld 2,8(11)
3847 . mtctr 12
3848 . ld 11,16(11)
e86ce104
AM
3849 . bctr
3850 .
3851 . .section .plt
3852 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3853
3854 The "reloc ()" notation is supposed to indicate that the linker emits
3855 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3856 copying.
3857
3858 What are the difficulties here? Well, firstly, the relocations
3859 examined by the linker in check_relocs are against the function code
3860 sym .foo, while the dynamic relocation in the plt is emitted against
3861 the function descriptor symbol, foo. Somewhere along the line, we need
3862 to carefully copy dynamic link information from one symbol to the other.
3863 Secondly, the generic part of the elf linker will make .foo a dynamic
3864 symbol as is normal for most other backends. We need foo dynamic
3865 instead, at least for an application final link. However, when
3866 creating a shared library containing foo, we need to have both symbols
3867 dynamic so that references to .foo are satisfied during the early
3868 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3869 definition from some other object, eg. a static library.
3870
3871 Update: As of August 2004, we support a new convention. Function
3872 calls may use the function descriptor symbol, ie. "bl foo". This
3873 behaves exactly as "bl .foo". */
65f38f15 3874
7c8bbca5
AM
3875/* Of those relocs that might be copied as dynamic relocs, this
3876 function selects those that must be copied when linking a shared
3877 library or PIE, even when the symbol is local. */
65f38f15 3878
1d483afe
AM
3879static int
3880must_be_dyn_reloc (struct bfd_link_info *info,
3881 enum elf_ppc64_reloc_type r_type)
3882{
3883 switch (r_type)
3884 {
3885 default:
7c8bbca5
AM
3886 /* Only relative relocs can be resolved when the object load
3887 address isn't fixed. DTPREL64 is excluded because the
3888 dynamic linker needs to differentiate global dynamic from
3889 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
3890 return 1;
3891
3892 case R_PPC64_REL32:
3893 case R_PPC64_REL64:
3894 case R_PPC64_REL30:
3895 return 0;
3896
3897 case R_PPC64_TPREL16:
3898 case R_PPC64_TPREL16_LO:
3899 case R_PPC64_TPREL16_HI:
3900 case R_PPC64_TPREL16_HA:
3901 case R_PPC64_TPREL16_DS:
3902 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3903 case R_PPC64_TPREL16_HIGH:
3904 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3905 case R_PPC64_TPREL16_HIGHER:
3906 case R_PPC64_TPREL16_HIGHERA:
3907 case R_PPC64_TPREL16_HIGHEST:
3908 case R_PPC64_TPREL16_HIGHESTA:
3909 case R_PPC64_TPREL64:
7c8bbca5
AM
3910 /* These relocations are relative but in a shared library the
3911 linker doesn't know the thread pointer base. */
3912 return bfd_link_dll (info);
1d483afe
AM
3913 }
3914}
65f38f15 3915
f4656909
AM
3916/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3917 copying dynamic variables from a shared lib into an app's dynbss
3918 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3919 shared lib. With code that gcc generates, it's vital that this be
3920 enabled; In the PowerPC64 ABI, the address of a function is actually
3921 the address of a function descriptor, which resides in the .opd
3922 section. gcc uses the descriptor directly rather than going via the
3923 GOT as some other ABI's do, which means that initialized function
3924 pointers must reference the descriptor. Thus, a function pointer
3925 initialized to the address of a function in a shared library will
3926 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3927 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3928 presents a problem as a plt entry for that function is also
3929 initialized from the function descriptor symbol and the copy reloc
3930 may not be initialized first. */
a23b6845 3931#define ELIMINATE_COPY_RELOCS 1
f4656909 3932
721956f4 3933/* Section name for stubs is the associated section name plus this
29942be8
NC
3934 string. */
3935#define STUB_SUFFIX ".stub"
721956f4
AM
3936
3937/* Linker stubs.
3938 ppc_stub_long_branch:
3939 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3940 destination, but a 24 bit branch in a stub section will reach.
3941 . b dest
3942
3943 ppc_stub_plt_branch:
3944 Similar to the above, but a 24 bit branch in the stub section won't
3945 reach its destination.
71a39c98
AM
3946 . addis %r11,%r2,xxx@toc@ha
3947 . ld %r12,xxx@toc@l(%r11)
3948 . mtctr %r12
721956f4
AM
3949 . bctr
3950
3951 ppc_stub_plt_call:
2c66dc6c
AM
3952 Used to call a function in a shared library. If it so happens that
3953 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3954 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3955 . std %r2,40(%r1)
71a39c98
AM
3956 . addis %r11,%r2,xxx@toc@ha
3957 . ld %r12,xxx+0@toc@l(%r11)
3958 . mtctr %r12
3959 . ld %r2,xxx+8@toc@l(%r11)
3960 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3961 . bctr
ad8e1ba5
AM
3962
3963 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3964 code to adjust the value and save r2 to support multiple toc sections.
3965 A ppc_stub_long_branch with an r2 offset looks like:
3966 . std %r2,40(%r1)
3967 . addis %r2,%r2,off@ha
3968 . addi %r2,%r2,off@l
3969 . b dest
3970
3971 A ppc_stub_plt_branch with an r2 offset looks like:
3972 . std %r2,40(%r1)
71a39c98
AM
3973 . addis %r11,%r2,xxx@toc@ha
3974 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3975 . addis %r2,%r2,off@ha
3976 . addi %r2,%r2,off@l
71a39c98 3977 . mtctr %r12
ad8e1ba5 3978 . bctr
ac2df442
AM
3979
3980 In cases where the "addis" instruction would add zero, the "addis" is
3981 omitted and following instructions modified slightly in some cases.
721956f4
AM
3982*/
3983
3984enum ppc_stub_type {
3985 ppc_stub_none,
3986 ppc_stub_long_branch,
ad8e1ba5 3987 ppc_stub_long_branch_r2off,
721956f4 3988 ppc_stub_plt_branch,
ad8e1ba5 3989 ppc_stub_plt_branch_r2off,
794e51c0 3990 ppc_stub_plt_call,
7341d5e2 3991 ppc_stub_plt_call_r2save,
a4b6fadd
AM
3992 ppc_stub_global_entry,
3993 ppc_stub_save_res
721956f4
AM
3994};
3995
6f20ed8a
AM
3996/* Information on stub grouping. */
3997struct map_stub
3998{
3999 /* The stub section. */
4000 asection *stub_sec;
4001 /* This is the section to which stubs in the group will be attached. */
4002 asection *link_sec;
a4b6fadd
AM
4003 /* Next group. */
4004 struct map_stub *next;
4005 /* Whether to emit a copy of register save/restore functions in this
4006 group. */
4007 int needs_save_res;
d4aaa2a0
AM
4008 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
4009 or -1u if no such stub with bctrl exists. */
4010 unsigned int tls_get_addr_opt_bctrl;
6f20ed8a
AM
4011};
4012
721956f4
AM
4013struct ppc_stub_hash_entry {
4014
4015 /* Base hash table entry structure. */
4016 struct bfd_hash_entry root;
4017
ad8e1ba5
AM
4018 enum ppc_stub_type stub_type;
4019
6f20ed8a
AM
4020 /* Group information. */
4021 struct map_stub *group;
721956f4
AM
4022
4023 /* Offset within stub_sec of the beginning of this stub. */
4024 bfd_vma stub_offset;
4025
4026 /* Given the symbol's value and its section we can determine its final
4027 value when building the stubs (so the stub knows where to jump. */
4028 bfd_vma target_value;
4029 asection *target_section;
4030
721956f4
AM
4031 /* The symbol table entry, if any, that this was derived from. */
4032 struct ppc_link_hash_entry *h;
e054468f 4033 struct plt_entry *plt_ent;
721956f4 4034
2d7ad24e
AM
4035 /* Symbol type. */
4036 unsigned char symtype;
4037
6911b7dc
AM
4038 /* Symbol st_other. */
4039 unsigned char other;
721956f4
AM
4040};
4041
4042struct ppc_branch_hash_entry {
4043
4044 /* Base hash table entry structure. */
4045 struct bfd_hash_entry root;
4046
c456f082 4047 /* Offset within branch lookup table. */
721956f4
AM
4048 unsigned int offset;
4049
4050 /* Generation marker. */
4051 unsigned int iter;
4052};
65f38f15 4053
19e08130
AM
4054/* Used to track dynamic relocations for local symbols. */
4055struct ppc_dyn_relocs
4056{
4057 struct ppc_dyn_relocs *next;
4058
4059 /* The input section of the reloc. */
4060 asection *sec;
4061
4062 /* Total number of relocs copied for the input section. */
4063 unsigned int count : 31;
4064
4065 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4066 unsigned int ifunc : 1;
4067};
4068
65f38f15
AM
4069struct ppc_link_hash_entry
4070{
4071 struct elf_link_hash_entry elf;
4072
b3fac117
AM
4073 union {
4074 /* A pointer to the most recently used stub hash entry against this
4075 symbol. */
4076 struct ppc_stub_hash_entry *stub_cache;
4077
4078 /* A pointer to the next symbol starting with a '.' */
4079 struct ppc_link_hash_entry *next_dot_sym;
4080 } u;
721956f4 4081
65f38f15 4082 /* Track dynamic relocs copied for this symbol. */
6061a67d 4083 struct elf_dyn_relocs *dyn_relocs;
e86ce104 4084
721956f4 4085 /* Link between function code and descriptor symbols. */
34814b9f 4086 struct ppc_link_hash_entry *oh;
721956f4 4087
e86ce104
AM
4088 /* Flag function code and descriptor symbols. */
4089 unsigned int is_func:1;
4090 unsigned int is_func_descriptor:1;
908b32fc 4091 unsigned int fake:1;
411e1bfb 4092
c5614fa4
AM
4093 /* Whether global opd/toc sym has been adjusted or not.
4094 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4095 should be set for all globals defined in any opd/toc section. */
754021d0
AM
4096 unsigned int adjust_done:1;
4097
a4b6fadd
AM
4098 /* Set if this is an out-of-line register save/restore function,
4099 with non-standard calling convention. */
4100 unsigned int save_res:1;
4101
8b5f1ed8
AM
4102 /* Set if a duplicate symbol with non-zero localentry is detected,
4103 even when the duplicate symbol does not provide a definition. */
4104 unsigned int non_zero_localentry:1;
4105
411e1bfb 4106 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
4107 Bits are or'd into the mask as the corresponding relocs are
4108 encountered during check_relocs, with TLS_TLS being set when any
4109 of the other TLS bits are set. tls_optimize clears bits when
4110 optimizing to indicate the corresponding GOT entry type is not
4111 needed. If set, TLS_TLS is never cleared. tls_optimize may also
4112 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
4113 separate flag rather than setting TPREL just for convenience in
4114 distinguishing the two cases.
4115 These flags are also kept for local symbols. */
4116#define TLS_TLS 1 /* Any TLS reloc. */
4117#define TLS_GD 2 /* GD reloc. */
4118#define TLS_LD 4 /* LD reloc. */
4119#define TLS_TPREL 8 /* TPREL reloc, => IE. */
4120#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
4121#define TLS_MARK 32 /* __tls_get_addr call marked. */
e7b938ca 4122#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
37da22e5 4123#define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
f961d9dd 4124 unsigned char tls_mask;
37da22e5
AM
4125
4126 /* The above field is also used to mark function symbols. In which
4127 case TLS_TLS will be 0. */
4128#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 4129#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 4130#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
4131};
4132
4133/* ppc64 ELF linker hash table. */
4134
4135struct ppc_link_hash_table
4136{
4137 struct elf_link_hash_table elf;
4138
721956f4
AM
4139 /* The stub hash table. */
4140 struct bfd_hash_table stub_hash_table;
4141
4142 /* Another hash table for plt_branch stubs. */
4143 struct bfd_hash_table branch_hash_table;
4144
3b421ab3
AM
4145 /* Hash table for function prologue tocsave. */
4146 htab_t tocsave_htab;
4147
e7d1c40c
AM
4148 /* Various options and other info passed from the linker. */
4149 struct ppc64_elf_params *params;
721956f4 4150
6f20ed8a
AM
4151 /* The size of sec_info below. */
4152 unsigned int sec_info_arr_size;
4153
4154 /* Per-section array of extra section info. Done this way rather
4155 than as part of ppc64_elf_section_data so we have the info for
4156 non-ppc64 sections. */
4157 struct
4158 {
4159 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 4160 bfd_vma toc_off;
6f20ed8a
AM
4161
4162 union
4163 {
4164 /* The section group that this section belongs to. */
4165 struct map_stub *group;
4166 /* A temp section list pointer. */
4167 asection *list;
4168 } u;
4169 } *sec_info;
721956f4 4170
a4b6fadd
AM
4171 /* Linked list of groups. */
4172 struct map_stub *group;
4173
ad8e1ba5
AM
4174 /* Temp used when calculating TOC pointers. */
4175 bfd_vma toc_curr;
bf102f86
AM
4176 bfd *toc_bfd;
4177 asection *toc_first_sec;
ad8e1ba5 4178
b3fac117
AM
4179 /* Used when adding symbols. */
4180 struct ppc_link_hash_entry *dot_syms;
4181
33e44f2e 4182 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 4183 asection *glink;
9e390558 4184 asection *global_entry;
82bd7b59 4185 asection *sfpr;
2d7ad24e
AM
4186 asection *pltlocal;
4187 asection *relpltlocal;
4ce794b7
AM
4188 asection *brlt;
4189 asection *relbrlt;
58d180e8 4190 asection *glink_eh_frame;
ec338859 4191
8387904d
AM
4192 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4193 struct ppc_link_hash_entry *tls_get_addr;
4194 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4195
927be08e
AM
4196 /* The size of reliplt used by got entry relocs. */
4197 bfd_size_type got_reli_size;
4198
9b5ecbd0 4199 /* Statistics. */
7341d5e2 4200 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4201
ee75fd95
AM
4202 /* Number of stubs against global syms. */
4203 unsigned long stub_globals;
4204
ee67d69a
AM
4205 /* Set if we're linking code with function descriptors. */
4206 unsigned int opd_abi:1;
4207
4c52953f 4208 /* Support for multiple toc sections. */
33c0ec9d 4209 unsigned int do_multi_toc:1;
4c52953f 4210 unsigned int multi_toc_needed:1;
927be08e 4211 unsigned int second_toc_pass:1;
67f0cbdb 4212 unsigned int do_toc_opt:1;
4c52953f 4213
9a23f96e
AM
4214 /* Set if tls optimization is enabled. */
4215 unsigned int do_tls_opt:1;
4216
3e04d765
AM
4217 /* Set if inline plt calls should be converted to direct calls. */
4218 unsigned int can_convert_all_inline_plt:1;
4219
5d1634d7 4220 /* Set on error. */
99877b66 4221 unsigned int stub_error:1;
721956f4 4222
8c5b4e52
AM
4223 /* Whether func_desc_adjust needs to be run over symbols. */
4224 unsigned int need_func_desc_adj:1;
721956f4 4225
82e66161
AM
4226 /* Whether there exist local gnu indirect function resolvers,
4227 referenced by dynamic relocations. */
4228 unsigned int local_ifunc_resolver:1;
4229 unsigned int maybe_local_ifunc_resolver:1;
4230
f378ab09
AM
4231 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4232 unsigned int has_plt_localentry0:1;
4233
721956f4
AM
4234 /* Incremented every time we size stubs. */
4235 unsigned int stub_iteration;
5d1634d7 4236
87d72d41
AM
4237 /* Small local sym cache. */
4238 struct sym_cache sym_cache;
65f38f15
AM
4239};
4240
4c52953f
AM
4241/* Rename some of the generic section flags to better document how they
4242 are used here. */
b0dddeec
AM
4243
4244/* Nonzero if this section has TLS related relocations. */
4245#define has_tls_reloc sec_flg0
4246
23cedd1d 4247/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
4248#define has_tls_get_addr_call sec_flg1
4249
4250/* Nonzero if this section has any toc or got relocs. */
4251#define has_toc_reloc sec_flg2
4252
4253/* Nonzero if this section has a call to another section that uses
4254 the toc or got. */
d77c8a4b 4255#define makes_toc_func_call sec_flg3
b0dddeec
AM
4256
4257/* Recursion protection when determining above flag. */
d77c8a4b 4258#define call_check_in_progress sec_flg4
70cc837d 4259#define call_check_done sec_flg5
4c52953f 4260
65f38f15
AM
4261/* Get the ppc64 ELF linker hash table from a link_info structure. */
4262
4263#define ppc_hash_table(p) \
4dfe6ac6
NC
4264 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4265 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4266
721956f4
AM
4267#define ppc_stub_hash_lookup(table, string, create, copy) \
4268 ((struct ppc_stub_hash_entry *) \
4269 bfd_hash_lookup ((table), (string), (create), (copy)))
4270
4271#define ppc_branch_hash_lookup(table, string, create, copy) \
4272 ((struct ppc_branch_hash_entry *) \
4273 bfd_hash_lookup ((table), (string), (create), (copy)))
4274
4275/* Create an entry in the stub hash table. */
4276
4277static struct bfd_hash_entry *
4ce794b7
AM
4278stub_hash_newfunc (struct bfd_hash_entry *entry,
4279 struct bfd_hash_table *table,
4280 const char *string)
721956f4
AM
4281{
4282 /* Allocate the structure if it has not already been allocated by a
4283 subclass. */
4284 if (entry == NULL)
4285 {
4286 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4287 if (entry == NULL)
4288 return entry;
4289 }
4290
4291 /* Call the allocation method of the superclass. */
4292 entry = bfd_hash_newfunc (entry, table, string);
4293 if (entry != NULL)
4294 {
4295 struct ppc_stub_hash_entry *eh;
4296
4297 /* Initialize the local fields. */
4298 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4299 eh->stub_type = ppc_stub_none;
6f20ed8a 4300 eh->group = NULL;
721956f4
AM
4301 eh->stub_offset = 0;
4302 eh->target_value = 0;
4303 eh->target_section = NULL;
721956f4 4304 eh->h = NULL;
6911b7dc 4305 eh->plt_ent = NULL;
6911b7dc 4306 eh->other = 0;
721956f4
AM
4307 }
4308
4309 return entry;
4310}
4311
4312/* Create an entry in the branch hash table. */
4313
4314static struct bfd_hash_entry *
4ce794b7
AM
4315branch_hash_newfunc (struct bfd_hash_entry *entry,
4316 struct bfd_hash_table *table,
4317 const char *string)
721956f4
AM
4318{
4319 /* Allocate the structure if it has not already been allocated by a
4320 subclass. */
4321 if (entry == NULL)
4322 {
4323 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4324 if (entry == NULL)
4325 return entry;
4326 }
4327
4328 /* Call the allocation method of the superclass. */
4329 entry = bfd_hash_newfunc (entry, table, string);
4330 if (entry != NULL)
4331 {
4332 struct ppc_branch_hash_entry *eh;
4333
4334 /* Initialize the local fields. */
4335 eh = (struct ppc_branch_hash_entry *) entry;
4336 eh->offset = 0;
4337 eh->iter = 0;
4338 }
4339
4340 return entry;
4341}
4342
65f38f15
AM
4343/* Create an entry in a ppc64 ELF linker hash table. */
4344
4345static struct bfd_hash_entry *
4ce794b7
AM
4346link_hash_newfunc (struct bfd_hash_entry *entry,
4347 struct bfd_hash_table *table,
4348 const char *string)
65f38f15
AM
4349{
4350 /* Allocate the structure if it has not already been allocated by a
4351 subclass. */
4352 if (entry == NULL)
4353 {
4354 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4355 if (entry == NULL)
4356 return entry;
4357 }
4358
4359 /* Call the allocation method of the superclass. */
4360 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4361 if (entry != NULL)
4362 {
4363 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4364
b3fac117 4365 memset (&eh->u.stub_cache, 0,
908b32fc 4366 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4367 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4368
4369 /* When making function calls, old ABI code references function entry
4370 points (dot symbols), while new ABI code references the function
4371 descriptor symbol. We need to make any combination of reference and
4372 definition work together, without breaking archive linking.
4373
4374 For a defined function "foo" and an undefined call to "bar":
4375 An old object defines "foo" and ".foo", references ".bar" (possibly
4376 "bar" too).
4377 A new object defines "foo" and references "bar".
4378
4379 A new object thus has no problem with its undefined symbols being
4380 satisfied by definitions in an old object. On the other hand, the
4381 old object won't have ".bar" satisfied by a new object.
4382
4383 Keep a list of newly added dot-symbols. */
4384
4385 if (string[0] == '.')
4386 {
4387 struct ppc_link_hash_table *htab;
4388
4389 htab = (struct ppc_link_hash_table *) table;
4390 eh->u.next_dot_sym = htab->dot_syms;
4391 htab->dot_syms = eh;
4392 }
65f38f15
AM
4393 }
4394
4395 return entry;
4396}
4397
3b421ab3
AM
4398struct tocsave_entry {
4399 asection *sec;
4400 bfd_vma offset;
4401};
4402
4403static hashval_t
4404tocsave_htab_hash (const void *p)
4405{
4406 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 4407 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
4408}
4409
4410static int
4411tocsave_htab_eq (const void *p1, const void *p2)
4412{
4413 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4414 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4415 return e1->sec == e2->sec && e1->offset == e2->offset;
4416}
4417
68faa637
AM
4418/* Destroy a ppc64 ELF linker hash table. */
4419
4420static void
d495ab0d 4421ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4422{
d495ab0d 4423 struct ppc_link_hash_table *htab;
68faa637 4424
d495ab0d 4425 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4426 if (htab->tocsave_htab)
4427 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4428 bfd_hash_table_free (&htab->branch_hash_table);
4429 bfd_hash_table_free (&htab->stub_hash_table);
4430 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4431}
4432
65f38f15
AM
4433/* Create a ppc64 ELF linker hash table. */
4434
4435static struct bfd_link_hash_table *
4ce794b7 4436ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4437{
4438 struct ppc_link_hash_table *htab;
4439 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4440
4ce794b7 4441 htab = bfd_zmalloc (amt);
65f38f15
AM
4442 if (htab == NULL)
4443 return NULL;
4444
66eb6687 4445 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4446 sizeof (struct ppc_link_hash_entry),
4447 PPC64_ELF_DATA))
65f38f15 4448 {
e2d34d7d 4449 free (htab);
65f38f15
AM
4450 return NULL;
4451 }
4452
721956f4 4453 /* Init the stub hash table too. */
66eb6687
AM
4454 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4455 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4456 {
d495ab0d 4457 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4458 return NULL;
4459 }
721956f4
AM
4460
4461 /* And the branch hash table. */
66eb6687
AM
4462 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4463 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4464 {
4465 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4466 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4467 return NULL;
4468 }
721956f4 4469
3b421ab3
AM
4470 htab->tocsave_htab = htab_try_create (1024,
4471 tocsave_htab_hash,
4472 tocsave_htab_eq,
4473 NULL);
4474 if (htab->tocsave_htab == NULL)
2915c55b 4475 {
d495ab0d 4476 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4477 return NULL;
4478 }
d495ab0d 4479 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4480
3254fd24
AM
4481 /* Initializing two fields of the union is just cosmetic. We really
4482 only care about glist, but when compiled on a 32-bit host the
4483 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4484 debugger inspection of these fields look nicer. */
a6aa5195
AM
4485 htab->elf.init_got_refcount.refcount = 0;
4486 htab->elf.init_got_refcount.glist = NULL;
4487 htab->elf.init_plt_refcount.refcount = 0;
4488 htab->elf.init_plt_refcount.glist = NULL;
4489 htab->elf.init_got_offset.offset = 0;
4490 htab->elf.init_got_offset.glist = NULL;
4491 htab->elf.init_plt_offset.offset = 0;
4492 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4493
65f38f15
AM
4494 return &htab->elf.root;
4495}
4496
bfeb4a28
AM
4497/* Create sections for linker generated code. */
4498
4499static bfd_boolean
4500create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4501{
4502 struct ppc_link_hash_table *htab;
4503 flagword flags;
4504
4505 htab = ppc_hash_table (info);
4506
bfeb4a28
AM
4507 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4508 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4509 if (htab->params->save_restore_funcs)
4510 {
4511 /* Create .sfpr for code to save and restore fp regs. */
4512 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4513 flags);
4514 if (htab->sfpr == NULL
4515 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4516 return FALSE;
4517 }
4518
4519 if (bfd_link_relocatable (info))
4520 return TRUE;
bfeb4a28
AM
4521
4522 /* Create .glink for lazy dynamic linking support. */
4523 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4524 flags);
4525 if (htab->glink == NULL
4526 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4527 return FALSE;
4528
9e390558
AM
4529 /* The part of .glink used by global entry stubs, separate so that
4530 it can be aligned appropriately without affecting htab->glink. */
4531 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4532 flags);
4533 if (htab->global_entry == NULL
4534 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4535 return FALSE;
4536
bfeb4a28
AM
4537 if (!info->no_ld_generated_unwind_info)
4538 {
4539 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4540 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4541 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4542 ".eh_frame",
4543 flags);
4544 if (htab->glink_eh_frame == NULL
4545 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4546 return FALSE;
4547 }
4548
4549 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4550 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4551 if (htab->elf.iplt == NULL
4552 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4553 return FALSE;
4554
4555 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4556 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4557 htab->elf.irelplt
4558 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4559 if (htab->elf.irelplt == NULL
4560 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4561 return FALSE;
4562
4563 /* Create branch lookup table for plt_branch stubs. */
4564 flags = (SEC_ALLOC | SEC_LOAD
4565 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4566 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4567 flags);
4568 if (htab->brlt == NULL
4569 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4570 return FALSE;
4571
2d7ad24e
AM
4572 /* Local plt entries, put in .branch_lt but a separate section for
4573 convenience. */
4574 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4575 flags);
4576 if (htab->pltlocal == NULL
4577 || ! bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
4578 return FALSE;
4579
0e1862bb 4580 if (!bfd_link_pic (info))
bfeb4a28
AM
4581 return TRUE;
4582
4583 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4584 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
4585 htab->relbrlt
4586 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28
AM
4587 if (htab->relbrlt == NULL
4588 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4589 return FALSE;
4590
2d7ad24e
AM
4591 htab->relpltlocal
4592 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4593 if (htab->relpltlocal == NULL
4594 || ! bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
4595 return FALSE;
4596
bfeb4a28
AM
4597 return TRUE;
4598}
4599
e717da7e
AM
4600/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4601
bfeb4a28 4602bfd_boolean
e7d1c40c
AM
4603ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4604 struct ppc64_elf_params *params)
e717da7e
AM
4605{
4606 struct ppc_link_hash_table *htab;
4607
e7d1c40c 4608 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4609
4610/* Always hook our dynamic sections into the first bfd, which is the
4611 linker created stub bfd. This ensures that the GOT header is at
4612 the start of the output TOC section. */
4613 htab = ppc_hash_table (info);
e7d1c40c
AM
4614 htab->elf.dynobj = params->stub_bfd;
4615 htab->params = params;
bfeb4a28 4616
bfeb4a28 4617 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4618}
4619
721956f4
AM
4620/* Build a name for an entry in the stub hash table. */
4621
4622static char *
4ce794b7
AM
4623ppc_stub_name (const asection *input_section,
4624 const asection *sym_sec,
4625 const struct ppc_link_hash_entry *h,
4626 const Elf_Internal_Rela *rel)
721956f4
AM
4627{
4628 char *stub_name;
bcaa2f82 4629 ssize_t len;
721956f4
AM
4630
4631 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4632 offsets from a sym as a branch target? In fact, we could
4633 probably assume the addend is always zero. */
4634 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4635
4636 if (h)
4637 {
4638 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4639 stub_name = bfd_malloc (len);
46de2a7c
AM
4640 if (stub_name == NULL)
4641 return stub_name;
4642
bcaa2f82
AM
4643 len = sprintf (stub_name, "%08x.%s+%x",
4644 input_section->id & 0xffffffff,
4645 h->elf.root.root.string,
4646 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4647 }
4648 else
4649 {
ad8e1ba5 4650 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4651 stub_name = bfd_malloc (len);
46de2a7c
AM
4652 if (stub_name == NULL)
4653 return stub_name;
4654
bcaa2f82
AM
4655 len = sprintf (stub_name, "%08x.%x:%x+%x",
4656 input_section->id & 0xffffffff,
4657 sym_sec->id & 0xffffffff,
4658 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4659 (int) rel->r_addend & 0xffffffff);
721956f4 4660 }
bcaa2f82 4661 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4662 stub_name[len - 2] = 0;
721956f4
AM
4663 return stub_name;
4664}
4665
4666/* Look up an entry in the stub hash. Stub entries are cached because
4667 creating the stub name takes a bit of time. */
4668
4669static struct ppc_stub_hash_entry *
4ce794b7
AM
4670ppc_get_stub_entry (const asection *input_section,
4671 const asection *sym_sec,
039b3fef 4672 struct ppc_link_hash_entry *h,
4ce794b7
AM
4673 const Elf_Internal_Rela *rel,
4674 struct ppc_link_hash_table *htab)
721956f4
AM
4675{
4676 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4677 struct map_stub *group;
721956f4
AM
4678
4679 /* If this input section is part of a group of sections sharing one
4680 stub section, then use the id of the first section in the group.
4681 Stub names need to include a section id, as there may well be
4682 more than one stub used to reach say, printf, and we need to
4683 distinguish between them. */
6f20ed8a 4684 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4685 if (group == NULL)
4686 return NULL;
721956f4 4687
b3fac117
AM
4688 if (h != NULL && h->u.stub_cache != NULL
4689 && h->u.stub_cache->h == h
6f20ed8a 4690 && h->u.stub_cache->group == group)
721956f4 4691 {
b3fac117 4692 stub_entry = h->u.stub_cache;
721956f4
AM
4693 }
4694 else
4695 {
4696 char *stub_name;
4697
6f20ed8a 4698 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4699 if (stub_name == NULL)
4700 return NULL;
4701
4702 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4703 stub_name, FALSE, FALSE);
721956f4 4704 if (h != NULL)
b3fac117 4705 h->u.stub_cache = stub_entry;
721956f4
AM
4706
4707 free (stub_name);
4708 }
4709
4710 return stub_entry;
4711}
4712
4713/* Add a new stub entry to the stub hash. Not all fields of the new
4714 stub entry are initialised. */
4715
4716static struct ppc_stub_hash_entry *
4ce794b7
AM
4717ppc_add_stub (const char *stub_name,
4718 asection *section,
25f53a85 4719 struct bfd_link_info *info)
721956f4 4720{
25f53a85 4721 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4722 struct map_stub *group;
721956f4
AM
4723 asection *link_sec;
4724 asection *stub_sec;
4725 struct ppc_stub_hash_entry *stub_entry;
4726
6f20ed8a
AM
4727 group = htab->sec_info[section->id].u.group;
4728 link_sec = group->link_sec;
4729 stub_sec = group->stub_sec;
721956f4
AM
4730 if (stub_sec == NULL)
4731 {
6f20ed8a
AM
4732 size_t namelen;
4733 bfd_size_type len;
4734 char *s_name;
721956f4 4735
6f20ed8a
AM
4736 namelen = strlen (link_sec->name);
4737 len = namelen + sizeof (STUB_SUFFIX);
4738 s_name = bfd_alloc (htab->params->stub_bfd, len);
4739 if (s_name == NULL)
4740 return NULL;
721956f4 4741
6f20ed8a
AM
4742 memcpy (s_name, link_sec->name, namelen);
4743 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4744 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4745 if (stub_sec == NULL)
4746 return NULL;
4747 group->stub_sec = stub_sec;
721956f4
AM
4748 }
4749
4750 /* Enter this entry into the linker stub hash table. */
4751 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4752 TRUE, FALSE);
721956f4
AM
4753 if (stub_entry == NULL)
4754 {
695344c0 4755 /* xgettext:c-format */
cf97bcb0
AM
4756 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4757 section->owner, stub_name);
721956f4
AM
4758 return NULL;
4759 }
4760
6f20ed8a 4761 stub_entry->group = group;
721956f4 4762 stub_entry->stub_offset = 0;
721956f4
AM
4763 return stub_entry;
4764}
4765
e717da7e
AM
4766/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4767 not already done. */
65f38f15 4768
b34976b6 4769static bfd_boolean
e717da7e 4770create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4771{
e717da7e
AM
4772 asection *got, *relgot;
4773 flagword flags;
4774 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4775
0c8d6e5c 4776 if (!is_ppc64_elf (abfd))
0ffa91dd 4777 return FALSE;
4dfe6ac6
NC
4778 if (htab == NULL)
4779 return FALSE;
0ffa91dd 4780
33e44f2e
AM
4781 if (!htab->elf.sgot
4782 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4783 return FALSE;
e717da7e
AM
4784
4785 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4786 | SEC_LINKER_CREATED);
4787
c456f082 4788 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4789 if (!got
e717da7e
AM
4790 || !bfd_set_section_alignment (abfd, got, 3))
4791 return FALSE;
65f38f15 4792
c456f082
AM
4793 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4794 flags | SEC_READONLY);
e717da7e 4795 if (!relgot
e717da7e 4796 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4797 return FALSE;
e717da7e
AM
4798
4799 ppc64_elf_tdata (abfd)->got = got;
4800 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4801 return TRUE;
65f38f15 4802}
5bd4f169 4803
b31867b6
AM
4804/* Follow indirect and warning symbol links. */
4805
4806static inline struct bfd_link_hash_entry *
4807follow_link (struct bfd_link_hash_entry *h)
4808{
4809 while (h->type == bfd_link_hash_indirect
4810 || h->type == bfd_link_hash_warning)
4811 h = h->u.i.link;
4812 return h;
4813}
4814
4815static inline struct elf_link_hash_entry *
4816elf_follow_link (struct elf_link_hash_entry *h)
4817{
4818 return (struct elf_link_hash_entry *) follow_link (&h->root);
4819}
4820
4821static inline struct ppc_link_hash_entry *
4822ppc_follow_link (struct ppc_link_hash_entry *h)
4823{
4824 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4825}
4826
40d16e0b
AM
4827/* Merge PLT info on FROM with that on TO. */
4828
4829static void
4830move_plt_plist (struct ppc_link_hash_entry *from,
4831 struct ppc_link_hash_entry *to)
4832{
4833 if (from->elf.plt.plist != NULL)
4834 {
4835 if (to->elf.plt.plist != NULL)
4836 {
4837 struct plt_entry **entp;
4838 struct plt_entry *ent;
4839
4840 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4841 {
4842 struct plt_entry *dent;
4843
4844 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4845 if (dent->addend == ent->addend)
4846 {
4847 dent->plt.refcount += ent->plt.refcount;
4848 *entp = ent->next;
4849 break;
4850 }
4851 if (dent == NULL)
4852 entp = &ent->next;
4853 }
4854 *entp = to->elf.plt.plist;
4855 }
4856
4857 to->elf.plt.plist = from->elf.plt.plist;
4858 from->elf.plt.plist = NULL;
4859 }
4860}
4861
65f38f15
AM
4862/* Copy the extra info we tack onto an elf_link_hash_entry. */
4863
4864static void
fcfa13d2
AM
4865ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4866 struct elf_link_hash_entry *dir,
4867 struct elf_link_hash_entry *ind)
65f38f15
AM
4868{
4869 struct ppc_link_hash_entry *edir, *eind;
4870
4871 edir = (struct ppc_link_hash_entry *) dir;
4872 eind = (struct ppc_link_hash_entry *) ind;
4873
c79d6685
AM
4874 edir->is_func |= eind->is_func;
4875 edir->is_func_descriptor |= eind->is_func_descriptor;
4876 edir->tls_mask |= eind->tls_mask;
4877 if (eind->oh != NULL)
4878 edir->oh = ppc_follow_link (eind->oh);
4879
474436e6 4880 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4881 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4882 edir->elf.ref_regular |= eind->elf.ref_regular;
4883 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 4884 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
4885 edir->elf.needs_plt |= eind->elf.needs_plt;
4886 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4887
d311bc8b
AM
4888 /* If we were called to copy over info for a weak sym, don't copy
4889 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4890 in order to simplify readonly_dynrelocs and save a field in the
4891 symbol hash entry, but that means dyn_relocs can't be used in any
4892 tests about a specific symbol, or affect other symbol flags which
ab2477e1 4893 are then tested. */
d311bc8b 4894 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 4895 return;
d311bc8b 4896
411e1bfb 4897 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4898 if (eind->dyn_relocs != NULL)
65f38f15 4899 {
bbd7ec4a
AM
4900 if (edir->dyn_relocs != NULL)
4901 {
6061a67d
AM
4902 struct elf_dyn_relocs **pp;
4903 struct elf_dyn_relocs *p;
bbd7ec4a 4904
fcfa13d2 4905 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4906 list. Merge any entries against the same section. */
4907 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4908 {
6061a67d 4909 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4910
4911 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4912 if (q->sec == p->sec)
4913 {
4914 q->pc_count += p->pc_count;
4915 q->count += p->count;
4916 *pp = p->next;
4917 break;
4918 }
4919 if (q == NULL)
4920 pp = &p->next;
4921 }
4922 *pp = edir->dyn_relocs;
4923 }
4924
65f38f15
AM
4925 edir->dyn_relocs = eind->dyn_relocs;
4926 eind->dyn_relocs = NULL;
4927 }
65f38f15 4928
81848ca0
AM
4929 /* Copy over got entries that we may have already seen to the
4930 symbol which just became indirect. */
411e1bfb
AM
4931 if (eind->elf.got.glist != NULL)
4932 {
4933 if (edir->elf.got.glist != NULL)
4934 {
4935 struct got_entry **entp;
4936 struct got_entry *ent;
4937
4938 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4939 {
4940 struct got_entry *dent;
4941
4942 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4943 if (dent->addend == ent->addend
e717da7e 4944 && dent->owner == ent->owner
411e1bfb
AM
4945 && dent->tls_type == ent->tls_type)
4946 {
4947 dent->got.refcount += ent->got.refcount;
4948 *entp = ent->next;
4949 break;
4950 }
4951 if (dent == NULL)
4952 entp = &ent->next;
4953 }
4954 *entp = edir->elf.got.glist;
4955 }
4956
4957 edir->elf.got.glist = eind->elf.got.glist;
4958 eind->elf.got.glist = NULL;
4959 }
4960
4961 /* And plt entries. */
40d16e0b 4962 move_plt_plist (eind, edir);
411e1bfb 4963
fcfa13d2 4964 if (eind->elf.dynindx != -1)
411e1bfb 4965 {
fcfa13d2
AM
4966 if (edir->elf.dynindx != -1)
4967 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4968 edir->elf.dynstr_index);
411e1bfb
AM
4969 edir->elf.dynindx = eind->elf.dynindx;
4970 edir->elf.dynstr_index = eind->elf.dynstr_index;
4971 eind->elf.dynindx = -1;
4972 eind->elf.dynstr_index = 0;
4973 }
411e1bfb
AM
4974}
4975
8387904d
AM
4976/* Find the function descriptor hash entry from the given function code
4977 hash entry FH. Link the entries via their OH fields. */
4978
4979static struct ppc_link_hash_entry *
b31867b6 4980lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4981{
4982 struct ppc_link_hash_entry *fdh = fh->oh;
4983
4984 if (fdh == NULL)
4985 {
4986 const char *fd_name = fh->elf.root.root.string + 1;
4987
4988 fdh = (struct ppc_link_hash_entry *)
4989 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4990 if (fdh == NULL)
4991 return fdh;
4992
4993 fdh->is_func_descriptor = 1;
4994 fdh->oh = fh;
4995 fh->is_func = 1;
4996 fh->oh = fdh;
8387904d
AM
4997 }
4998
8c5b4e52
AM
4999 fdh = ppc_follow_link (fdh);
5000 fdh->is_func_descriptor = 1;
5001 fdh->oh = fh;
5002 return fdh;
8387904d
AM
5003}
5004
8c5b4e52 5005/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
5006
5007static struct ppc_link_hash_entry *
5008make_fdh (struct bfd_link_info *info,
908b32fc 5009 struct ppc_link_hash_entry *fh)
bb700d78 5010{
8c5b4e52
AM
5011 bfd *abfd = fh->elf.root.u.undef.abfd;
5012 struct bfd_link_hash_entry *bh = NULL;
bb700d78 5013 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
5014 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
5015 ? BSF_WEAK
5016 : BSF_GLOBAL);
5017
5018 if (!_bfd_generic_link_add_one_symbol (info, abfd,
5019 fh->elf.root.root.string + 1,
5020 flags, bfd_und_section_ptr, 0,
5021 NULL, FALSE, FALSE, &bh))
bb700d78
AM
5022 return NULL;
5023
5024 fdh = (struct ppc_link_hash_entry *) bh;
5025 fdh->elf.non_elf = 0;
908b32fc
AM
5026 fdh->fake = 1;
5027 fdh->is_func_descriptor = 1;
5028 fdh->oh = fh;
5029 fh->is_func = 1;
5030 fh->oh = fdh;
bb700d78
AM
5031 return fdh;
5032}
5033
8387904d
AM
5034/* Fix function descriptor symbols defined in .opd sections to be
5035 function type. */
555cd476
AM
5036
5037static bfd_boolean
c16153ae 5038ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 5039 struct bfd_link_info *info,
555cd476 5040 Elf_Internal_Sym *isym,
6911b7dc 5041 const char **name,
555cd476
AM
5042 flagword *flags ATTRIBUTE_UNUSED,
5043 asection **sec,
b53dfeb2 5044 bfd_vma *value)
555cd476 5045{
b53dfeb2 5046 if (*sec != NULL
f1885d1e 5047 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
5048 {
5049 asection *code_sec;
5050
5051 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5052 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5053 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5054
5055 /* If the symbol is a function defined in .opd, and the function
5056 code is in a discarded group, let it appear to be undefined. */
0e1862bb 5057 if (!bfd_link_relocatable (info)
b53dfeb2
AM
5058 && (*sec)->reloc_count != 0
5059 && opd_entry_value (*sec, *value, &code_sec, NULL,
5060 FALSE) != (bfd_vma) -1
5061 && discarded_section (code_sec))
5062 {
5063 *sec = bfd_und_section_ptr;
5064 isym->st_shndx = SHN_UNDEF;
5065 }
5066 }
dbd1e97e
AM
5067 else if (*sec != NULL
5068 && strcmp ((*sec)->name, ".toc") == 0
5069 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5070 {
5071 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5072 if (htab != NULL)
5073 htab->params->object_in_toc = 1;
5074 }
433817dd 5075
6911b7dc
AM
5076 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5077 {
5078 if (abiversion (ibfd) == 0)
5079 set_abiversion (ibfd, 2);
5080 else if (abiversion (ibfd) == 1)
5081 {
cf97bcb0
AM
5082 _bfd_error_handler (_("symbol '%s' has invalid st_other"
5083 " for ABI version 1"), *name);
6911b7dc
AM
5084 bfd_set_error (bfd_error_bad_value);
5085 return FALSE;
5086 }
5087 }
5088
555cd476
AM
5089 return TRUE;
5090}
5091
6911b7dc
AM
5092/* Merge non-visibility st_other attributes: local entry point. */
5093
5094static void
5095ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5096 const Elf_Internal_Sym *isym,
5097 bfd_boolean definition,
5098 bfd_boolean dynamic)
5099{
f378ab09 5100 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
5101 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5102 | ELF_ST_VISIBILITY (h->other));
5103}
5104
8c5b4e52
AM
5105/* Hook called on merging a symbol. We use this to clear "fake" since
5106 we now have a real symbol. */
5107
5108static bfd_boolean
5109ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 5110 const Elf_Internal_Sym *isym,
8c5b4e52
AM
5111 asection **psec ATTRIBUTE_UNUSED,
5112 bfd_boolean newdef ATTRIBUTE_UNUSED,
5113 bfd_boolean olddef ATTRIBUTE_UNUSED,
5114 bfd *oldbfd ATTRIBUTE_UNUSED,
5115 const asection *oldsec ATTRIBUTE_UNUSED)
5116{
5117 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
5118 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5119 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
5120 return TRUE;
5121}
5122
8387904d 5123/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5124 inclusion of a new ABI object archive that defines "bar".
5125 NAME is a symbol defined in an archive. Return a symbol in the hash
5126 table that might be satisfied by the archive symbols. */
8387904d
AM
5127
5128static struct elf_link_hash_entry *
5129ppc64_elf_archive_symbol_lookup (bfd *abfd,
5130 struct bfd_link_info *info,
5131 const char *name)
5132{
5133 struct elf_link_hash_entry *h;
5134 char *dot_name;
5135 size_t len;
5136
5137 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5138 if (h != NULL
5139 /* Don't return this sym if it is a fake function descriptor
5140 created by add_symbol_adjust. */
8c5b4e52 5141 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
5142 return h;
5143
5144 if (name[0] == '.')
5145 return h;
5146
5147 len = strlen (name);
5148 dot_name = bfd_alloc (abfd, len + 2);
5149 if (dot_name == NULL)
e99955cd 5150 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5151 dot_name[0] = '.';
5152 memcpy (dot_name + 1, name, len + 1);
5153 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5154 bfd_release (abfd, dot_name);
5155 return h;
5156}
5157
5158/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5159 new ABI object defines "bar". Well, at least, undefined dot symbols
5160 are made weak. This stops later archive searches from including an
5161 object if we already have a function descriptor definition. It also
35b0ce59
AM
5162 prevents the linker complaining about undefined symbols.
5163 We also check and correct mismatched symbol visibility here. The
5164 most restrictive visibility of the function descriptor and the
5165 function entry symbol is used. */
8387904d
AM
5166
5167static bfd_boolean
b3fac117 5168add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5169{
8387904d
AM
5170 struct ppc_link_hash_table *htab;
5171 struct ppc_link_hash_entry *fdh;
5172
b3fac117
AM
5173 if (eh->elf.root.type == bfd_link_hash_warning)
5174 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5175
8400d40d
AM
5176 if (eh->elf.root.type == bfd_link_hash_indirect)
5177 return TRUE;
5178
b3fac117
AM
5179 if (eh->elf.root.root.string[0] != '.')
5180 abort ();
8387904d 5181
b3fac117 5182 htab = ppc_hash_table (info);
4dfe6ac6
NC
5183 if (htab == NULL)
5184 return FALSE;
5185
b31867b6 5186 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
5187 if (fdh == NULL
5188 && !bfd_link_relocatable (info)
5189 && (eh->elf.root.type == bfd_link_hash_undefined
5190 || eh->elf.root.type == bfd_link_hash_undefweak)
5191 && eh->elf.ref_regular)
5192 {
5193 /* Make an undefined function descriptor sym, in order to
5194 pull in an --as-needed shared lib. Archives are handled
5195 elsewhere. */
5196 fdh = make_fdh (info, eh);
5197 if (fdh == NULL)
5198 return FALSE;
bb700d78 5199 }
8c5b4e52
AM
5200
5201 if (fdh != NULL)
8387904d 5202 {
35b0ce59
AM
5203 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5204 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
5205
5206 /* Make both descriptor and entry symbol have the most
5207 constraining visibility of either symbol. */
35b0ce59
AM
5208 if (entry_vis < descr_vis)
5209 fdh->elf.other += entry_vis - descr_vis;
5210 else if (entry_vis > descr_vis)
5211 eh->elf.other += descr_vis - entry_vis;
5212
8c5b4e52
AM
5213 /* Propagate reference flags from entry symbol to function
5214 descriptor symbol. */
bc4e12de 5215 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 5216 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
5217 fdh->elf.ref_regular |= eh->elf.ref_regular;
5218 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5219
5220 if (!fdh->elf.forced_local
5221 && fdh->elf.dynindx == -1
5222 && fdh->elf.versioned != versioned_hidden
5223 && (bfd_link_dll (info)
5224 || fdh->elf.def_dynamic
5225 || fdh->elf.ref_dynamic)
5226 && (eh->elf.ref_regular
5227 || eh->elf.def_regular))
5228 {
5229 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5230 return FALSE;
35b0ce59 5231 }
8387904d 5232 }
99877b66 5233
8387904d
AM
5234 return TRUE;
5235}
5236
f6c7c3e8
AM
5237/* Set up opd section info and abiversion for IBFD, and process list
5238 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5239
8387904d 5240static bfd_boolean
f6c7c3e8 5241ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5242{
99877b66 5243 struct ppc_link_hash_table *htab;
b3fac117 5244 struct ppc_link_hash_entry **p, *eh;
459609d6 5245 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5246
459609d6 5247 if (opd != NULL && opd->size != 0)
b3fac117 5248 {
b9399fcf
AM
5249 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5250 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5251
459609d6
AM
5252 if (abiversion (ibfd) == 0)
5253 set_abiversion (ibfd, 1);
8a2058b5 5254 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5255 {
695344c0 5256 /* xgettext:c-format */
cf97bcb0
AM
5257 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5258 ibfd, abiversion (ibfd));
459609d6
AM
5259 bfd_set_error (bfd_error_bad_value);
5260 return FALSE;
f6c7c3e8 5261 }
b9399fcf 5262 }
f6c7c3e8 5263
b9399fcf
AM
5264 if (is_ppc64_elf (info->output_bfd))
5265 {
5266 /* For input files without an explicit abiversion in e_flags
5267 we should have flagged any with symbol st_other bits set
5268 as ELFv1 and above flagged those with .opd as ELFv2.
5269 Set the output abiversion if not yet set, and for any input
5270 still ambiguous, take its abiversion from the output.
5271 Differences in ABI are reported later. */
5272 if (abiversion (info->output_bfd) == 0)
5273 set_abiversion (info->output_bfd, abiversion (ibfd));
5274 else if (abiversion (ibfd) == 0)
5275 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
5276 }
5277
459609d6
AM
5278 htab = ppc_hash_table (info);
5279 if (htab == NULL)
b9399fcf 5280 return TRUE;
459609d6 5281
b9399fcf
AM
5282 if (opd != NULL && opd->size != 0
5283 && (ibfd->flags & DYNAMIC) == 0
5284 && (opd->flags & SEC_RELOC) != 0
5285 && opd->reloc_count != 0
5286 && !bfd_is_abs_section (opd->output_section)
5287 && info->gc_sections)
5288 {
5289 /* Garbage collection needs some extra help with .opd sections.
5290 We don't want to necessarily keep everything referenced by
5291 relocs in .opd, as that would keep all functions. Instead,
5292 if we reference an .opd symbol (a function descriptor), we
5293 want to keep the function code symbol's section. This is
5294 easy for global symbols, but for local syms we need to keep
5295 information about the associated function section. */
5296 bfd_size_type amt;
5297 asection **opd_sym_map;
5298 Elf_Internal_Shdr *symtab_hdr;
5299 Elf_Internal_Rela *relocs, *rel_end, *rel;
5300
5301 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5302 opd_sym_map = bfd_zalloc (ibfd, amt);
5303 if (opd_sym_map == NULL)
5304 return FALSE;
5305 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5306 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5307 info->keep_memory);
5308 if (relocs == NULL)
5309 return FALSE;
5310 symtab_hdr = &elf_symtab_hdr (ibfd);
5311 rel_end = relocs + opd->reloc_count - 1;
5312 for (rel = relocs; rel < rel_end; rel++)
5313 {
5314 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5315 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5316
5317 if (r_type == R_PPC64_ADDR64
5318 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5319 && r_symndx < symtab_hdr->sh_info)
5320 {
5321 Elf_Internal_Sym *isym;
5322 asection *s;
5323
5324 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5325 if (isym == NULL)
5326 {
5327 if (elf_section_data (opd)->relocs != relocs)
5328 free (relocs);
5329 return FALSE;
5330 }
5331
5332 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5333 if (s != NULL && s != opd)
5334 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5335 }
5336 }
5337 if (elf_section_data (opd)->relocs != relocs)
5338 free (relocs);
5339 }
459609d6
AM
5340
5341 p = &htab->dot_syms;
5342 while ((eh = *p) != NULL)
5343 {
5344 *p = NULL;
5345 if (&eh->elf == htab->elf.hgot)
5346 ;
5347 else if (htab->elf.hgot == NULL
5348 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5349 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
5350 else if (abiversion (ibfd) <= 1)
5351 {
5352 htab->need_func_desc_adj = 1;
5353 if (!add_symbol_adjust (eh, info))
5354 return FALSE;
5355 }
459609d6
AM
5356 p = &eh->u.next_dot_sym;
5357 }
b3fac117 5358 return TRUE;
8387904d
AM
5359}
5360
97fed1c9
JJ
5361/* Undo hash table changes when an --as-needed input file is determined
5362 not to be needed. */
5363
5364static bfd_boolean
e5034e59
AM
5365ppc64_elf_notice_as_needed (bfd *ibfd,
5366 struct bfd_link_info *info,
5367 enum notice_asneeded_action act)
97fed1c9 5368{
e5034e59
AM
5369 if (act == notice_not_needed)
5370 {
5371 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5372
e5034e59
AM
5373 if (htab == NULL)
5374 return FALSE;
4dfe6ac6 5375
e5034e59
AM
5376 htab->dot_syms = NULL;
5377 }
5378 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5379}
5380
aa374f67
AM
5381/* If --just-symbols against a final linked binary, then assume we need
5382 toc adjusting stubs when calling functions defined there. */
5383
5384static void
5385ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5386{
5387 if ((sec->flags & SEC_CODE) != 0
5388 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5389 && is_ppc64_elf (sec->owner))
5390 {
2c3f079f
AM
5391 if (abiversion (sec->owner) >= 2
5392 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5393 sec->has_toc_reloc = 1;
5394 }
5395 _bfd_elf_link_just_syms (sec, info);
5396}
5397
e054468f 5398static struct plt_entry **
4ce794b7
AM
5399update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5400 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5401{
5402 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5403 struct plt_entry **local_plt;
f961d9dd 5404 unsigned char *local_got_tls_masks;
411e1bfb
AM
5405
5406 if (local_got_ents == NULL)
5407 {
5408 bfd_size_type size = symtab_hdr->sh_info;
5409
e054468f
AM
5410 size *= (sizeof (*local_got_ents)
5411 + sizeof (*local_plt)
5412 + sizeof (*local_got_tls_masks));
4ce794b7 5413 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5414 if (local_got_ents == NULL)
e054468f 5415 return NULL;
411e1bfb
AM
5416 elf_local_got_ents (abfd) = local_got_ents;
5417 }
5418
37da22e5 5419 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5420 {
5421 struct got_entry *ent;
5422
5423 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5424 if (ent->addend == r_addend
5425 && ent->owner == abfd
5426 && ent->tls_type == tls_type)
411e1bfb
AM
5427 break;
5428 if (ent == NULL)
5429 {
5430 bfd_size_type amt = sizeof (*ent);
4ce794b7 5431 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5432 if (ent == NULL)
5433 return FALSE;
5434 ent->next = local_got_ents[r_symndx];
5435 ent->addend = r_addend;
e717da7e 5436 ent->owner = abfd;
411e1bfb 5437 ent->tls_type = tls_type;
927be08e 5438 ent->is_indirect = FALSE;
411e1bfb
AM
5439 ent->got.refcount = 0;
5440 local_got_ents[r_symndx] = ent;
5441 }
5442 ent->got.refcount += 1;
5443 }
5444
e054468f 5445 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5446 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 5447 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
5448
5449 return local_plt + r_symndx;
65f38f15
AM
5450}
5451
411e1bfb 5452static bfd_boolean
e054468f 5453update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5454{
411e1bfb 5455 struct plt_entry *ent;
1e2f5b6e 5456
e054468f 5457 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5458 if (ent->addend == addend)
5459 break;
5460 if (ent == NULL)
1e2f5b6e 5461 {
411e1bfb 5462 bfd_size_type amt = sizeof (*ent);
4ce794b7 5463 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5464 if (ent == NULL)
5465 return FALSE;
e054468f 5466 ent->next = *plist;
411e1bfb
AM
5467 ent->addend = addend;
5468 ent->plt.refcount = 0;
e054468f 5469 *plist = ent;
1e2f5b6e 5470 }
411e1bfb 5471 ent->plt.refcount += 1;
b34976b6 5472 return TRUE;
1e2f5b6e
AM
5473}
5474
e054468f
AM
5475static bfd_boolean
5476is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5477{
5478 return (r_type == R_PPC64_REL24
5479 || r_type == R_PPC64_REL14
5480 || r_type == R_PPC64_REL14_BRTAKEN
5481 || r_type == R_PPC64_REL14_BRNTAKEN
5482 || r_type == R_PPC64_ADDR24
5483 || r_type == R_PPC64_ADDR14
5484 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d
AM
5485 || r_type == R_PPC64_ADDR14_BRNTAKEN
5486 || r_type == R_PPC64_PLTCALL);
5487}
5488
5489/* Relocs on inline plt call sequence insns prior to the call. */
5490
5491static bfd_boolean
5492is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
5493{
5494 return (r_type == R_PPC64_PLT16_HA
5495 || r_type == R_PPC64_PLT16_HI
5496 || r_type == R_PPC64_PLT16_LO
5497 || r_type == R_PPC64_PLT16_LO_DS
5498 || r_type == R_PPC64_PLTSEQ);
e054468f
AM
5499}
5500
5bd4f169 5501/* Look through the relocs for a section during the first phase, and
65f38f15 5502 calculate needed space in the global offset table, procedure
5d1634d7 5503 linkage table, and dynamic reloc sections. */
5bd4f169 5504
b34976b6 5505static bfd_boolean
4ce794b7
AM
5506ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5507 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5508{
65f38f15 5509 struct ppc_link_hash_table *htab;
5bd4f169 5510 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5511 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5512 const Elf_Internal_Rela *rel;
5513 const Elf_Internal_Rela *rel_end;
5bd4f169 5514 asection *sreloc;
3a71aa26 5515 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 5516 bfd_boolean is_opd;
5bd4f169 5517
0e1862bb 5518 if (bfd_link_relocatable (info))
b34976b6 5519 return TRUE;
5bd4f169 5520
680a3378
AM
5521 /* Don't do anything special with non-loaded, non-alloced sections.
5522 In particular, any relocs in such sections should not affect GOT
5523 and PLT reference counting (ie. we don't allow them to create GOT
5524 or PLT entries), there's no possibility or desire to optimize TLS
5525 relocs, and there's not much point in propagating relocs to shared
5526 libs that the dynamic linker won't relocate. */
5527 if ((sec->flags & SEC_ALLOC) == 0)
5528 return TRUE;
5529
0c8d6e5c 5530 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5531
65f38f15 5532 htab = ppc_hash_table (info);
4dfe6ac6
NC
5533 if (htab == NULL)
5534 return FALSE;
5535
3a71aa26
AM
5536 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5537 FALSE, FALSE, TRUE);
5538 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5539 FALSE, FALSE, TRUE);
0ffa91dd 5540 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5541 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5542 sreloc = NULL;
b9399fcf 5543 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
5544 rel_end = relocs + sec->reloc_count;
5545 for (rel = relocs; rel < rel_end; rel++)
5546 {
5547 unsigned long r_symndx;
5548 struct elf_link_hash_entry *h;
04c9666a 5549 enum elf_ppc64_reloc_type r_type;
727fc41e 5550 int tls_type;
7c8fe5c4 5551 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5552 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5553
5554 r_symndx = ELF64_R_SYM (rel->r_info);
5555 if (r_symndx < symtab_hdr->sh_info)
5556 h = NULL;
5557 else
973a3492
L
5558 {
5559 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5560 h = elf_follow_link (h);
1c865ab2
AM
5561
5562 if (h == htab->elf.hgot)
5563 sec->has_toc_reloc = 1;
973a3492 5564 }
5bd4f169 5565
727fc41e 5566 tls_type = 0;
e054468f 5567 ifunc = NULL;
25f23106
AM
5568 if (h != NULL)
5569 {
5570 if (h->type == STT_GNU_IFUNC)
5571 {
5572 h->needs_plt = 1;
5573 ifunc = &h->plt.plist;
5574 }
5575 }
5576 else
5577 {
5578 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5579 abfd, r_symndx);
5580 if (isym == NULL)
5581 return FALSE;
5582
5583 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5584 {
5585 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
37da22e5
AM
5586 rel->r_addend,
5587 NON_GOT | PLT_IFUNC);
25f23106
AM
5588 if (ifunc == NULL)
5589 return FALSE;
5590 }
5591 }
727fc41e 5592
cbf95972 5593 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5594 switch (r_type)
5bd4f169 5595 {
727fc41e
AM
5596 case R_PPC64_TLSGD:
5597 case R_PPC64_TLSLD:
5598 /* These special tls relocs tie a call to __tls_get_addr with
5599 its parameter symbol. */
37da22e5
AM
5600 if (h != NULL)
5601 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5602 else
5603 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5604 rel->r_addend,
5605 NON_GOT | TLS_TLS | TLS_MARK))
5606 return FALSE;
5607 sec->has_tls_reloc = 1;
727fc41e
AM
5608 break;
5609
411e1bfb
AM
5610 case R_PPC64_GOT_TLSLD16:
5611 case R_PPC64_GOT_TLSLD16_LO:
5612 case R_PPC64_GOT_TLSLD16_HI:
5613 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5614 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5615 goto dogottls;
5616
5617 case R_PPC64_GOT_TLSGD16:
5618 case R_PPC64_GOT_TLSGD16_LO:
5619 case R_PPC64_GOT_TLSGD16_HI:
5620 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5621 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5622 goto dogottls;
5623
5624 case R_PPC64_GOT_TPREL16_DS:
5625 case R_PPC64_GOT_TPREL16_LO_DS:
5626 case R_PPC64_GOT_TPREL16_HI:
5627 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 5628 if (bfd_link_dll (info))
411e1bfb
AM
5629 info->flags |= DF_STATIC_TLS;
5630 tls_type = TLS_TLS | TLS_TPREL;
5631 goto dogottls;
5632
5633 case R_PPC64_GOT_DTPREL16_DS:
5634 case R_PPC64_GOT_DTPREL16_LO_DS:
5635 case R_PPC64_GOT_DTPREL16_HI:
5636 case R_PPC64_GOT_DTPREL16_HA:
5637 tls_type = TLS_TLS | TLS_DTPREL;
5638 dogottls:
5639 sec->has_tls_reloc = 1;
1a0670f3 5640 /* Fall through */
411e1bfb 5641
5bd4f169 5642 case R_PPC64_GOT16:
5bd4f169 5643 case R_PPC64_GOT16_DS:
65f38f15
AM
5644 case R_PPC64_GOT16_HA:
5645 case R_PPC64_GOT16_HI:
5646 case R_PPC64_GOT16_LO:
5bd4f169 5647 case R_PPC64_GOT16_LO_DS:
65f38f15 5648 /* This symbol requires a global offset table entry. */
4c52953f 5649 sec->has_toc_reloc = 1;
33c0ec9d
AM
5650 if (r_type == R_PPC64_GOT_TLSLD16
5651 || r_type == R_PPC64_GOT_TLSGD16
5652 || r_type == R_PPC64_GOT_TPREL16_DS
5653 || r_type == R_PPC64_GOT_DTPREL16_DS
5654 || r_type == R_PPC64_GOT16
5655 || r_type == R_PPC64_GOT16_DS)
5656 {
5657 htab->do_multi_toc = 1;
d77c8a4b 5658 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5659 }
5660
e717da7e
AM
5661 if (ppc64_elf_tdata (abfd)->got == NULL
5662 && !create_got_section (abfd, info))
b34976b6 5663 return FALSE;
5bd4f169
AM
5664
5665 if (h != NULL)
5666 {
411e1bfb
AM
5667 struct ppc_link_hash_entry *eh;
5668 struct got_entry *ent;
65f38f15 5669
411e1bfb
AM
5670 eh = (struct ppc_link_hash_entry *) h;
5671 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5672 if (ent->addend == rel->r_addend
e717da7e 5673 && ent->owner == abfd
411e1bfb
AM
5674 && ent->tls_type == tls_type)
5675 break;
5676 if (ent == NULL)
5bd4f169 5677 {
411e1bfb 5678 bfd_size_type amt = sizeof (*ent);
4ce794b7 5679 ent = bfd_alloc (abfd, amt);
411e1bfb 5680 if (ent == NULL)
b34976b6 5681 return FALSE;
411e1bfb
AM
5682 ent->next = eh->elf.got.glist;
5683 ent->addend = rel->r_addend;
e717da7e 5684 ent->owner = abfd;
411e1bfb 5685 ent->tls_type = tls_type;
927be08e 5686 ent->is_indirect = FALSE;
411e1bfb
AM
5687 ent->got.refcount = 0;
5688 eh->elf.got.glist = ent;
5bd4f169 5689 }
411e1bfb 5690 ent->got.refcount += 1;
e7b938ca 5691 eh->tls_mask |= tls_type;
5bd4f169 5692 }
411e1bfb
AM
5693 else
5694 /* This is a global offset table entry for a local symbol. */
5695 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5696 rel->r_addend, tls_type))
5697 return FALSE;
a345bc8d
AM
5698
5699 /* We may also need a plt entry if the symbol turns out to be
5700 an ifunc. */
0e1862bb 5701 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5702 {
5703 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5704 return FALSE;
5705 }
5bd4f169
AM
5706 break;
5707
5bd4f169 5708 case R_PPC64_PLT16_HA:
65f38f15
AM
5709 case R_PPC64_PLT16_HI:
5710 case R_PPC64_PLT16_LO:
08be3224 5711 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
5712 case R_PPC64_PLT32:
5713 case R_PPC64_PLT64:
cbf95972
AM
5714 /* This symbol requires a procedure linkage table entry. */
5715 plt_list = ifunc;
5716 if (h != NULL)
e054468f 5717 {
e054468f
AM
5718 h->needs_plt = 1;
5719 if (h->root.root.string[0] == '.'
5720 && h->root.root.string[1] != '\0')
5721 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 5722 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
5723 plt_list = &h->plt.plist;
5724 }
5725 if (plt_list == NULL)
2d7ad24e
AM
5726 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5727 rel->r_addend,
5728 NON_GOT | PLT_KEEP);
cbf95972
AM
5729 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5730 return FALSE;
5bd4f169
AM
5731 break;
5732
5733 /* The following relocations don't need to propagate the
5734 relocation if linking a shared object since they are
5735 section relative. */
5736 case R_PPC64_SECTOFF:
5737 case R_PPC64_SECTOFF_LO:
5738 case R_PPC64_SECTOFF_HI:
5739 case R_PPC64_SECTOFF_HA:
5740 case R_PPC64_SECTOFF_DS:
5741 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5742 case R_PPC64_DTPREL16:
5743 case R_PPC64_DTPREL16_LO:
5744 case R_PPC64_DTPREL16_HI:
5745 case R_PPC64_DTPREL16_HA:
5746 case R_PPC64_DTPREL16_DS:
5747 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5748 case R_PPC64_DTPREL16_HIGH:
5749 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5750 case R_PPC64_DTPREL16_HIGHER:
5751 case R_PPC64_DTPREL16_HIGHERA:
5752 case R_PPC64_DTPREL16_HIGHEST:
5753 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5754 break;
5755
ad8e1ba5 5756 /* Nor do these. */
25f23106
AM
5757 case R_PPC64_REL16:
5758 case R_PPC64_REL16_LO:
5759 case R_PPC64_REL16_HI:
5760 case R_PPC64_REL16_HA:
a680de9a 5761 case R_PPC64_REL16DX_HA:
25f23106
AM
5762 break;
5763
45965137
AM
5764 /* Not supported as a dynamic relocation. */
5765 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5766 if (bfd_link_pic (info))
45965137
AM
5767 {
5768 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5769 ppc_howto_init ();
695344c0 5770 /* xgettext:c-format */
174d0a74 5771 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 5772 "in shared libraries and PIEs\n"),
45965137
AM
5773 abfd, sec, rel->r_offset,
5774 ppc64_elf_howto_table[r_type]->name);
5775 bfd_set_error (bfd_error_bad_value);
5776 return FALSE;
5777 }
5778 break;
5779
ad8e1ba5 5780 case R_PPC64_TOC16:
33c0ec9d
AM
5781 case R_PPC64_TOC16_DS:
5782 htab->do_multi_toc = 1;
d77c8a4b 5783 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5784 /* Fall through. */
ad8e1ba5
AM
5785 case R_PPC64_TOC16_LO:
5786 case R_PPC64_TOC16_HI:
5787 case R_PPC64_TOC16_HA:
ad8e1ba5 5788 case R_PPC64_TOC16_LO_DS:
4c52953f 5789 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5790 break;
5791
006589cf
AM
5792 /* Marker reloc. */
5793 case R_PPC64_ENTRY:
5794 break;
5795
5bd4f169
AM
5796 /* This relocation describes the C++ object vtable hierarchy.
5797 Reconstruct it for later use during GC. */
5798 case R_PPC64_GNU_VTINHERIT:
c152c796 5799 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5800 return FALSE;
5bd4f169
AM
5801 break;
5802
5803 /* This relocation describes which C++ vtable entries are actually
5804 used. Record for later use during GC. */
5805 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5806 BFD_ASSERT (h != NULL);
5807 if (h != NULL
5808 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5809 return FALSE;
5bd4f169
AM
5810 break;
5811
721956f4
AM
5812 case R_PPC64_REL14:
5813 case R_PPC64_REL14_BRTAKEN:
5814 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5815 {
5816 asection *dest = NULL;
5817
5818 /* Heuristic: If jumping outside our section, chances are
5819 we are going to need a stub. */
5820 if (h != NULL)
5821 {
5822 /* If the sym is weak it may be overridden later, so
5823 don't assume we know where a weak sym lives. */
5824 if (h->root.type == bfd_link_hash_defined)
5825 dest = h->root.u.def.section;
5826 }
5827 else
87d72d41
AM
5828 {
5829 Elf_Internal_Sym *isym;
5830
5831 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5832 abfd, r_symndx);
5833 if (isym == NULL)
5834 return FALSE;
5835
5836 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5837 }
5838
220c76dd 5839 if (dest != sec)
7c8fe5c4 5840 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5841 }
3e04d765
AM
5842 goto rel24;
5843
5844 case R_PPC64_PLTCALL:
5845 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
5846 /* Fall through. */
5847
5d1634d7 5848 case R_PPC64_REL24:
3e04d765 5849 rel24:
cbf95972
AM
5850 plt_list = ifunc;
5851 if (h != NULL)
5d1634d7 5852 {
e054468f
AM
5853 h->needs_plt = 1;
5854 if (h->root.root.string[0] == '.'
5855 && h->root.root.string[1] != '\0')
5856 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5857
3a71aa26 5858 if (h == tga || h == dottga)
cbf95972
AM
5859 {
5860 sec->has_tls_reloc = 1;
5861 if (rel != relocs
5862 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5863 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5864 /* We have a new-style __tls_get_addr call with
5865 a marker reloc. */
5866 ;
5867 else
5868 /* Mark this section as having an old-style call. */
5869 sec->has_tls_get_addr_call = 1;
5870 }
5871 plt_list = &h->plt.plist;
411e1bfb 5872 }
cbf95972
AM
5873
5874 /* We may need a .plt entry if the function this reloc
5875 refers to is in a shared lib. */
5876 if (plt_list
5877 && !update_plt_info (abfd, plt_list, rel->r_addend))
5878 return FALSE;
411e1bfb
AM
5879 break;
5880
cbf95972
AM
5881 case R_PPC64_ADDR14:
5882 case R_PPC64_ADDR14_BRNTAKEN:
5883 case R_PPC64_ADDR14_BRTAKEN:
5884 case R_PPC64_ADDR24:
5885 goto dodyn;
5886
411e1bfb
AM
5887 case R_PPC64_TPREL64:
5888 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5889 if (bfd_link_dll (info))
411e1bfb
AM
5890 info->flags |= DF_STATIC_TLS;
5891 goto dotlstoc;
5892
5893 case R_PPC64_DTPMOD64:
5894 if (rel + 1 < rel_end
5895 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5896 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5897 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5898 else
951fd09b 5899 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5900 goto dotlstoc;
5901
5902 case R_PPC64_DTPREL64:
5903 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5904 if (rel != relocs
5905 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5906 && rel[-1].r_offset == rel->r_offset - 8)
5907 /* This is the second reloc of a dtpmod, dtprel pair.
5908 Don't mark with TLS_DTPREL. */
5909 goto dodyn;
5910
5911 dotlstoc:
5912 sec->has_tls_reloc = 1;
5913 if (h != NULL)
5914 {
5915 struct ppc_link_hash_entry *eh;
5916 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5917 eh->tls_mask |= tls_type;
411e1bfb
AM
5918 }
5919 else
5920 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5921 rel->r_addend, tls_type))
5922 return FALSE;
5923
7c8fe5c4
AM
5924 ppc64_sec = ppc64_elf_section_data (sec);
5925 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5926 {
3a71aa26
AM
5927 bfd_size_type amt;
5928
e7b938ca 5929 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5930 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5931 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5932 if (ppc64_sec->u.toc.symndx == NULL)
5933 return FALSE;
5934 amt = sec->size * sizeof (bfd_vma) / 8;
5935 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5936 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5937 return FALSE;
7c8fe5c4
AM
5938 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5939 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5940 }
5941 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5942 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5943 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5944
5945 /* Mark the second slot of a GD or LD entry.
5946 -1 to indicate GD and -2 to indicate LD. */
5947 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5948 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5949 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5950 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5951 goto dodyn;
5952
5953 case R_PPC64_TPREL16:
5954 case R_PPC64_TPREL16_LO:
5955 case R_PPC64_TPREL16_HI:
5956 case R_PPC64_TPREL16_HA:
5957 case R_PPC64_TPREL16_DS:
5958 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5959 case R_PPC64_TPREL16_HIGH:
5960 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5961 case R_PPC64_TPREL16_HIGHER:
5962 case R_PPC64_TPREL16_HIGHERA:
5963 case R_PPC64_TPREL16_HIGHEST:
5964 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
5965 if (bfd_link_dll (info))
5966 info->flags |= DF_STATIC_TLS;
5967 goto dodyn;
5d1634d7 5968
e86ce104 5969 case R_PPC64_ADDR64:
b9399fcf 5970 if (is_opd
1e2f5b6e 5971 && rel + 1 < rel_end
4ce794b7 5972 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5973 {
8387904d 5974 if (h != NULL)
8c5b4e52 5975 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 5976 }
e86ce104
AM
5977 /* Fall through. */
5978
65f38f15
AM
5979 case R_PPC64_ADDR16:
5980 case R_PPC64_ADDR16_DS:
5981 case R_PPC64_ADDR16_HA:
5982 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5983 case R_PPC64_ADDR16_HIGH:
5984 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5985 case R_PPC64_ADDR16_HIGHER:
5986 case R_PPC64_ADDR16_HIGHERA:
5987 case R_PPC64_ADDR16_HIGHEST:
5988 case R_PPC64_ADDR16_HIGHESTA:
5989 case R_PPC64_ADDR16_LO:
5990 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5991 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5992 && rel->r_addend == 0)
5993 {
5994 /* We may need a .plt entry if this reloc refers to a
5995 function in a shared lib. */
5996 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5997 return FALSE;
5998 h->pointer_equality_needed = 1;
5999 }
6000 /* Fall through. */
6001
6002 case R_PPC64_REL30:
6003 case R_PPC64_REL32:
6004 case R_PPC64_REL64:
65f38f15 6005 case R_PPC64_ADDR32:
65f38f15
AM
6006 case R_PPC64_UADDR16:
6007 case R_PPC64_UADDR32:
6008 case R_PPC64_UADDR64:
5bd4f169 6009 case R_PPC64_TOC:
0e1862bb 6010 if (h != NULL && !bfd_link_pic (info))
81848ca0 6011 /* We may need a copy reloc. */
f5385ebf 6012 h->non_got_ref = 1;
81848ca0 6013
41bd81ab 6014 /* Don't propagate .opd relocs. */
b9399fcf 6015 if (NO_OPD_RELOCS && is_opd)
e86ce104 6016 break;
e86ce104 6017
65f38f15
AM
6018 /* If we are creating a shared library, and this is a reloc
6019 against a global symbol, or a non PC relative reloc
6020 against a local symbol, then we need to copy the reloc
6021 into the shared library. However, if we are linking with
6022 -Bsymbolic, we do not need to copy a reloc against a
6023 global symbol which is defined in an object we are
6024 including in the link (i.e., DEF_REGULAR is set). At
6025 this point we have not seen all the input files, so it is
6026 possible that DEF_REGULAR is not set now but will be set
6027 later (it is never cleared). In case of a weak definition,
6028 DEF_REGULAR may be cleared later by a strong definition in
6029 a shared library. We account for that possibility below by
f4656909 6030 storing information in the dyn_relocs field of the hash
65f38f15
AM
6031 table entry. A similar situation occurs when creating
6032 shared libraries and symbol visibility changes render the
6033 symbol local.
6034
6035 If on the other hand, we are creating an executable, we
6036 may need to keep relocations for symbols satisfied by a
6037 dynamic library if we manage to avoid copy relocs for the
6038 symbol. */
411e1bfb 6039 dodyn:
0e1862bb 6040 if ((bfd_link_pic (info)
1d483afe 6041 && (must_be_dyn_reloc (info, r_type)
65f38f15 6042 || (h != NULL
198f1157 6043 && (!SYMBOLIC_BIND (info, h)
65f38f15 6044 || h->root.type == bfd_link_hash_defweak
f5385ebf 6045 || !h->def_regular))))
f4656909 6046 || (ELIMINATE_COPY_RELOCS
0e1862bb 6047 && !bfd_link_pic (info)
65f38f15
AM
6048 && h != NULL
6049 && (h->root.type == bfd_link_hash_defweak
25f23106 6050 || !h->def_regular))
0e1862bb 6051 || (!bfd_link_pic (info)
25f23106 6052 && ifunc != NULL))
5bd4f169 6053 {
65f38f15
AM
6054 /* We must copy these reloc types into the output file.
6055 Create a reloc section in dynobj and make room for
6056 this reloc. */
5bd4f169
AM
6057 if (sreloc == NULL)
6058 {
83bac4b0
NC
6059 sreloc = _bfd_elf_make_dynamic_reloc_section
6060 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 6061
5bd4f169 6062 if (sreloc == NULL)
83bac4b0 6063 return FALSE;
5bd4f169
AM
6064 }
6065
65f38f15
AM
6066 /* If this is a global symbol, we count the number of
6067 relocations we need for this symbol. */
6068 if (h != NULL)
6069 {
19e08130
AM
6070 struct elf_dyn_relocs *p;
6071 struct elf_dyn_relocs **head;
6072
ec338859 6073 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
6074 p = *head;
6075 if (p == NULL || p->sec != sec)
6076 {
6077 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6078 if (p == NULL)
6079 return FALSE;
6080 p->next = *head;
6081 *head = p;
6082 p->sec = sec;
6083 p->count = 0;
6084 p->pc_count = 0;
6085 }
6086 p->count += 1;
6087 if (!must_be_dyn_reloc (info, r_type))
6088 p->pc_count += 1;
65f38f15
AM
6089 }
6090 else
6091 {
ec338859
AM
6092 /* Track dynamic relocs needed for local syms too.
6093 We really need local syms available to do this
6094 easily. Oh well. */
19e08130
AM
6095 struct ppc_dyn_relocs *p;
6096 struct ppc_dyn_relocs **head;
6097 bfd_boolean is_ifunc;
ec338859 6098 asection *s;
6edfbbad 6099 void *vpp;
87d72d41 6100 Elf_Internal_Sym *isym;
6edfbbad 6101
87d72d41
AM
6102 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6103 abfd, r_symndx);
6104 if (isym == NULL)
b34976b6 6105 return FALSE;
ec338859 6106
87d72d41
AM
6107 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6108 if (s == NULL)
6109 s = sec;
6110
6edfbbad 6111 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
6112 head = (struct ppc_dyn_relocs **) vpp;
6113 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6114 p = *head;
6115 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6116 p = p->next;
6117 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6118 {
6119 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6120 if (p == NULL)
6121 return FALSE;
6122 p->next = *head;
6123 *head = p;
6124 p->sec = sec;
6125 p->ifunc = is_ifunc;
6126 p->count = 0;
6127 }
6128 p->count += 1;
ec338859 6129 }
65f38f15 6130 }
5bd4f169 6131 break;
65f38f15
AM
6132
6133 default:
96e0dda4 6134 break;
5bd4f169
AM
6135 }
6136 }
6137
b34976b6 6138 return TRUE;
5bd4f169
AM
6139}
6140
ee67d69a
AM
6141/* Merge backend specific data from an object file to the output
6142 object file when linking. */
6143
6144static bfd_boolean
50e03d47 6145ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 6146{
50e03d47 6147 bfd *obfd = info->output_bfd;
ee67d69a
AM
6148 unsigned long iflags, oflags;
6149
6150 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6151 return TRUE;
6152
6153 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6154 return TRUE;
6155
50e03d47 6156 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6157 return FALSE;
6158
6159 iflags = elf_elfheader (ibfd)->e_flags;
6160 oflags = elf_elfheader (obfd)->e_flags;
6161
f6c7c3e8 6162 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6163 {
4eca0228 6164 _bfd_error_handler
695344c0 6165 /* xgettext:c-format */
871b3ab2 6166 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
6167 bfd_set_error (bfd_error_bad_value);
6168 return FALSE;
6169 }
f6c7c3e8 6170 else if (iflags != oflags && iflags != 0)
ee67d69a 6171 {
4eca0228 6172 _bfd_error_handler
695344c0 6173 /* xgettext:c-format */
871b3ab2 6174 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
6175 ibfd, iflags, oflags);
6176 bfd_set_error (bfd_error_bad_value);
6177 return FALSE;
6178 }
6179
4a91d0ba
AM
6180 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
6181 return FALSE;
005d79fd 6182
ee67d69a 6183 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 6184 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6185}
6186
6187static bfd_boolean
6188ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6189{
6190 /* Print normal ELF private data. */
6191 _bfd_elf_print_private_bfd_data (abfd, ptr);
6192
6193 if (elf_elfheader (abfd)->e_flags != 0)
6194 {
6195 FILE *file = ptr;
6196
ee67d69a
AM
6197 fprintf (file, _("private flags = 0x%lx:"),
6198 elf_elfheader (abfd)->e_flags);
6199
6200 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6201 fprintf (file, _(" [abiv%ld]"),
6202 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6203 fputc ('\n', file);
6204 }
6205
6206 return TRUE;
6207}
6208
8387904d 6209/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6210 of the code entry point, and its section, which must be in the same
6211 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6212
6213static bfd_vma
6214opd_entry_value (asection *opd_sec,
6215 bfd_vma offset,
6216 asection **code_sec,
aef36ac1
AM
6217 bfd_vma *code_off,
6218 bfd_boolean in_code_sec)
8387904d
AM
6219{
6220 bfd *opd_bfd = opd_sec->owner;
8860955f 6221 Elf_Internal_Rela *relocs;
8387904d 6222 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6223 bfd_vma val;
8387904d 6224
9f296da3
AM
6225 /* No relocs implies we are linking a --just-symbols object, or looking
6226 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6227 if (opd_sec->reloc_count == 0)
6228 {
729eabd5 6229 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6230
729eabd5
AM
6231 if (contents == NULL)
6232 {
6233 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6234 return (bfd_vma) -1;
6235 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6236 }
ee1e4ede 6237
dbb3fbbb 6238 /* PR 17512: file: 64b9dfbb. */
451dfd38 6239 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6240 return (bfd_vma) -1;
6241
729eabd5 6242 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6243 if (code_sec != NULL)
6244 {
6245 asection *sec, *likely = NULL;
ee1e4ede 6246
aef36ac1 6247 if (in_code_sec)
4b85d634 6248 {
aef36ac1
AM
6249 sec = *code_sec;
6250 if (sec->vma <= val
6251 && val < sec->vma + sec->size)
6252 likely = sec;
6253 else
6254 val = -1;
6255 }
6256 else
6257 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6258 if (sec->vma <= val
6259 && (sec->flags & SEC_LOAD) != 0
6260 && (sec->flags & SEC_ALLOC) != 0)
6261 likely = sec;
6262 if (likely != NULL)
6263 {
6264 *code_sec = likely;
6265 if (code_off != NULL)
6266 *code_off = val - likely->vma;
4b85d634
AM
6267 }
6268 }
aef36ac1 6269 return val;
4b85d634
AM
6270 }
6271
0c8d6e5c 6272 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6273
729eabd5 6274 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6275 if (relocs == NULL)
6276 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6277 /* PR 17512: file: df8e1fd6. */
6278 if (relocs == NULL)
6279 return (bfd_vma) -1;
645ea6a9 6280
8387904d 6281 /* Go find the opd reloc at the sym address. */
8860955f 6282 lo = relocs;
8387904d 6283 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6284 val = (bfd_vma) -1;
8387904d
AM
6285 while (lo < hi)
6286 {
6287 look = lo + (hi - lo) / 2;
6288 if (look->r_offset < offset)
6289 lo = look + 1;
6290 else if (look->r_offset > offset)
6291 hi = look;
6292 else
6293 {
0ffa91dd
NC
6294 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6295
8387904d
AM
6296 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6297 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6298 {
6299 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6300 asection *sec = NULL;
8387904d 6301
b53dfeb2
AM
6302 if (symndx >= symtab_hdr->sh_info
6303 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6304 {
6305 struct elf_link_hash_entry **sym_hashes;
6306 struct elf_link_hash_entry *rh;
6307
6308 sym_hashes = elf_sym_hashes (opd_bfd);
6309 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6310 if (rh != NULL)
6311 {
6312 rh = elf_follow_link (rh);
bb854a36
AM
6313 if (rh->root.type != bfd_link_hash_defined
6314 && rh->root.type != bfd_link_hash_defweak)
6315 break;
6316 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6317 {
bb854a36
AM
6318 val = rh->root.u.def.value;
6319 sec = rh->root.u.def.section;
b53dfeb2
AM
6320 }
6321 }
6322 }
6323
6324 if (sec == NULL)
6325 {
6326 Elf_Internal_Sym *sym;
6327
6328 if (symndx < symtab_hdr->sh_info)
6329 {
6330 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6331 if (sym == NULL)
6332 {
6333 size_t symcnt = symtab_hdr->sh_info;
6334 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6335 symcnt, 0,
6336 NULL, NULL, NULL);
6337 if (sym == NULL)
6338 break;
6339 symtab_hdr->contents = (bfd_byte *) sym;
6340 }
6341 sym += symndx;
128205bb
AM
6342 }
6343 else
6344 {
b53dfeb2
AM
6345 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6346 1, symndx,
6347 NULL, NULL, NULL);
128205bb
AM
6348 if (sym == NULL)
6349 break;
128205bb 6350 }
b53dfeb2
AM
6351 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6352 if (sec == NULL)
6353 break;
6354 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6355 val = sym->st_value;
8387904d 6356 }
b53dfeb2 6357
8387904d
AM
6358 val += look->r_addend;
6359 if (code_off != NULL)
6360 *code_off = val;
6361 if (code_sec != NULL)
aef36ac1
AM
6362 {
6363 if (in_code_sec && *code_sec != sec)
6364 return -1;
6365 else
6366 *code_sec = sec;
6367 }
b53dfeb2 6368 if (sec->output_section != NULL)
8387904d 6369 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6370 }
6371 break;
6372 }
6373 }
645ea6a9 6374
645ea6a9 6375 return val;
8387904d
AM
6376}
6377
aef36ac1
AM
6378/* If the ELF symbol SYM might be a function in SEC, return the
6379 function size and set *CODE_OFF to the function's entry point,
6380 otherwise return zero. */
9f296da3 6381
aef36ac1
AM
6382static bfd_size_type
6383ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6384 bfd_vma *code_off)
9f296da3 6385{
aef36ac1
AM
6386 bfd_size_type size;
6387
6388 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6389 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6390 return 0;
6391
6392 size = 0;
6393 if (!(sym->flags & BSF_SYNTHETIC))
6394 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6395
6396 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6397 {
b07bca4e
AM
6398 struct _opd_sec_data *opd = get_opd_info (sym->section);
6399 bfd_vma symval = sym->value;
6400
6401 if (opd != NULL
6402 && opd->adjust != NULL
6403 && elf_section_data (sym->section)->relocs != NULL)
6404 {
6405 /* opd_entry_value will use cached relocs that have been
6406 adjusted, but with raw symbols. That means both local
6407 and global symbols need adjusting. */
6408 long adjust = opd->adjust[OPD_NDX (symval)];
6409 if (adjust == -1)
6410 return 0;
6411 symval += adjust;
6412 }
6413
6414 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6415 &sec, code_off, TRUE) == (bfd_vma) -1)
6416 return 0;
6417 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6418 symbol. This size has nothing to do with the code size of the
6419 function, which is what we're supposed to return, but the
6420 code size isn't available without looking up the dot-sym.
6421 However, doing that would be a waste of time particularly
6422 since elf_find_function will look at the dot-sym anyway.
6423 Now, elf_find_function will keep the largest size of any
6424 function sym found at the code address of interest, so return
6425 1 here to avoid it incorrectly caching a larger function size
6426 for a small function. This does mean we return the wrong
6427 size for a new-ABI function of size 24, but all that does is
6428 disable caching for such functions. */
6429 if (size == 24)
6430 size = 1;
9f296da3 6431 }
aef36ac1
AM
6432 else
6433 {
6434 if (sym->section != sec)
6435 return 0;
6436 *code_off = sym->value;
6437 }
6438 if (size == 0)
6439 size = 1;
6440 return size;
9f296da3
AM
6441}
6442
f378ab09
AM
6443/* Return true if symbol is a strong function defined in an ELFv2
6444 object with st_other localentry bits of zero, ie. its local entry
6445 point coincides with its global entry point. */
6446
6447static bfd_boolean
6448is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6449{
6450 return (h != NULL
6451 && h->type == STT_FUNC
6452 && h->root.type == bfd_link_hash_defined
6453 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 6454 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
6455 && is_ppc64_elf (h->root.u.def.section->owner)
6456 && abiversion (h->root.u.def.section->owner) >= 2);
6457}
6458
854b41e7
AM
6459/* Return true if symbol is defined in a regular object file. */
6460
6461static bfd_boolean
6462is_static_defined (struct elf_link_hash_entry *h)
6463{
6464 return ((h->root.type == bfd_link_hash_defined
6465 || h->root.type == bfd_link_hash_defweak)
6466 && h->root.u.def.section != NULL
6467 && h->root.u.def.section->output_section != NULL);
6468}
6469
b31867b6
AM
6470/* If FDH is a function descriptor symbol, return the associated code
6471 entry symbol if it is defined. Return NULL otherwise. */
6472
6473static struct ppc_link_hash_entry *
6474defined_code_entry (struct ppc_link_hash_entry *fdh)
6475{
6476 if (fdh->is_func_descriptor)
6477 {
6478 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6479 if (fh->elf.root.type == bfd_link_hash_defined
6480 || fh->elf.root.type == bfd_link_hash_defweak)
6481 return fh;
6482 }
6483 return NULL;
6484}
6485
6486/* If FH is a function code entry symbol, return the associated
6487 function descriptor symbol if it is defined. Return NULL otherwise. */
6488
6489static struct ppc_link_hash_entry *
6490defined_func_desc (struct ppc_link_hash_entry *fh)
6491{
6492 if (fh->oh != NULL
6493 && fh->oh->is_func_descriptor)
6494 {
6495 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6496 if (fdh->elf.root.type == bfd_link_hash_defined
6497 || fdh->elf.root.type == bfd_link_hash_defweak)
6498 return fdh;
6499 }
6500 return NULL;
6501}
6502
8c5b4e52
AM
6503static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6504
6505/* Garbage collect sections, after first dealing with dot-symbols. */
6506
6507static bfd_boolean
6508ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6509{
6510 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6511
6512 if (htab != NULL && htab->need_func_desc_adj)
6513 {
6514 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6515 htab->need_func_desc_adj = 0;
6516 }
6517 return bfd_elf_gc_sections (abfd, info);
6518}
6519
74f0fb50
AM
6520/* Mark all our entry sym sections, both opd and code section. */
6521
6522static void
6523ppc64_elf_gc_keep (struct bfd_link_info *info)
6524{
6525 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6526 struct bfd_sym_chain *sym;
6527
4dfe6ac6
NC
6528 if (htab == NULL)
6529 return;
6530
74f0fb50
AM
6531 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6532 {
b31867b6 6533 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6534 asection *sec;
6535
6536 eh = (struct ppc_link_hash_entry *)
b31867b6 6537 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6538 if (eh == NULL)
6539 continue;
6540 if (eh->elf.root.type != bfd_link_hash_defined
6541 && eh->elf.root.type != bfd_link_hash_defweak)
6542 continue;
6543
b31867b6
AM
6544 fh = defined_code_entry (eh);
6545 if (fh != NULL)
74f0fb50 6546 {
b31867b6 6547 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6548 sec->flags |= SEC_KEEP;
6549 }
6550 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6551 && opd_entry_value (eh->elf.root.u.def.section,
6552 eh->elf.root.u.def.value,
aef36ac1 6553 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6554 sec->flags |= SEC_KEEP;
6555
6556 sec = eh->elf.root.u.def.section;
6557 sec->flags |= SEC_KEEP;
6558 }
6559}
6560
64d03ab5
AM
6561/* Mark sections containing dynamically referenced symbols. When
6562 building shared libraries, we must assume that any visible symbol is
6563 referenced. */
6564
6565static bfd_boolean
6566ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6567{
6568 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6569 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6570 struct ppc_link_hash_entry *fdh;
b407645f 6571 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6572
64d03ab5 6573 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6574 fdh = defined_func_desc (eh);
6575 if (fdh != NULL)
6576 eh = fdh;
64d03ab5
AM
6577
6578 if ((eh->elf.root.type == bfd_link_hash_defined
6579 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 6580 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 6581 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6582 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6583 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6584 && (!bfd_link_executable (info)
e278ae05 6585 || info->gc_keep_exported
b407645f
AM
6586 || info->export_dynamic
6587 || (eh->elf.dynamic
6588 && d != NULL
6589 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
e278ae05 6590 && (eh->elf.versioned >= versioned
4c58e0d8
AM
6591 || !bfd_hide_sym_by_version (info->version_info,
6592 eh->elf.root.root.string)))))
64d03ab5
AM
6593 {
6594 asection *code_sec;
b31867b6 6595 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6596
6597 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6598
6599 /* Function descriptor syms cause the associated
6600 function code sym section to be marked. */
b31867b6
AM
6601 fh = defined_code_entry (eh);
6602 if (fh != NULL)
6603 {
6604 code_sec = fh->elf.root.u.def.section;
6605 code_sec->flags |= SEC_KEEP;
6606 }
64d03ab5
AM
6607 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6608 && opd_entry_value (eh->elf.root.u.def.section,
6609 eh->elf.root.u.def.value,
aef36ac1 6610 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6611 code_sec->flags |= SEC_KEEP;
6612 }
6613
6614 return TRUE;
6615}
6616
5bd4f169
AM
6617/* Return the section that should be marked against GC for a given
6618 relocation. */
6619
6620static asection *
4ce794b7 6621ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6622 struct bfd_link_info *info,
4ce794b7
AM
6623 Elf_Internal_Rela *rel,
6624 struct elf_link_hash_entry *h,
6625 Elf_Internal_Sym *sym)
5bd4f169 6626{
ccfa59ea
AM
6627 asection *rsec;
6628
ccfa59ea
AM
6629 /* Syms return NULL if we're marking .opd, so we avoid marking all
6630 function sections, as all functions are referenced in .opd. */
6631 rsec = NULL;
6632 if (get_opd_info (sec) != NULL)
6633 return rsec;
1e2f5b6e 6634
5bd4f169
AM
6635 if (h != NULL)
6636 {
04c9666a 6637 enum elf_ppc64_reloc_type r_type;
b31867b6 6638 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6639
4ce794b7 6640 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6641 switch (r_type)
5bd4f169
AM
6642 {
6643 case R_PPC64_GNU_VTINHERIT:
6644 case R_PPC64_GNU_VTENTRY:
6645 break;
6646
6647 default:
6648 switch (h->root.type)
6649 {
6650 case bfd_link_hash_defined:
6651 case bfd_link_hash_defweak:
ccfa59ea 6652 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6653 fdh = defined_func_desc (eh);
6654 if (fdh != NULL)
8c5b4e52
AM
6655 {
6656 /* -mcall-aixdesc code references the dot-symbol on
6657 a call reloc. Mark the function descriptor too
6658 against garbage collection. */
6659 fdh->elf.mark = 1;
60d67dc8
AM
6660 if (fdh->elf.is_weakalias)
6661 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6662 eh = fdh;
6663 }
1e2f5b6e
AM
6664
6665 /* Function descriptor syms cause the associated
6666 function code sym section to be marked. */
b31867b6
AM
6667 fh = defined_code_entry (eh);
6668 if (fh != NULL)
ccfa59ea
AM
6669 {
6670 /* They also mark their opd section. */
74f0fb50 6671 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6672
b31867b6 6673 rsec = fh->elf.root.u.def.section;
ccfa59ea 6674 }
8387904d
AM
6675 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6676 && opd_entry_value (eh->elf.root.u.def.section,
6677 eh->elf.root.u.def.value,
aef36ac1 6678 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6679 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6680 else
1e2f5b6e
AM
6681 rsec = h->root.u.def.section;
6682 break;
5bd4f169
AM
6683
6684 case bfd_link_hash_common:
1e2f5b6e
AM
6685 rsec = h->root.u.c.p->section;
6686 break;
5bd4f169
AM
6687
6688 default:
fb34365b 6689 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6690 }
6691 }
6692 }
6693 else
6694 {
74f0fb50 6695 struct _opd_sec_data *opd;
1e2f5b6e
AM
6696
6697 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6698 opd = get_opd_info (rsec);
6699 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6700 {
74f0fb50 6701 rsec->gc_mark = 1;
ccfa59ea 6702
51aecdc5 6703 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6704 }
5bd4f169
AM
6705 }
6706
1e2f5b6e 6707 return rsec;
5bd4f169
AM
6708}
6709
deb0e272
AM
6710/* The maximum size of .sfpr. */
6711#define SFPR_MAX (218*4)
6712
6713struct sfpr_def_parms
6714{
699733f6
AM
6715 const char name[12];
6716 unsigned char lo, hi;
deb0e272
AM
6717 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6718 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6719};
6720
a4b6fadd
AM
6721/* Auto-generate _save*, _rest* functions in .sfpr.
6722 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6723 instead. */
deb0e272 6724
4dfe6ac6 6725static bfd_boolean
a4b6fadd
AM
6726sfpr_define (struct bfd_link_info *info,
6727 const struct sfpr_def_parms *parm,
6728 asection *stub_sec)
deb0e272
AM
6729{
6730 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6731 unsigned int i;
6732 size_t len = strlen (parm->name);
6733 bfd_boolean writing = FALSE;
699733f6 6734 char sym[16];
deb0e272 6735
4dfe6ac6
NC
6736 if (htab == NULL)
6737 return FALSE;
6738
deb0e272
AM
6739 memcpy (sym, parm->name, len);
6740 sym[len + 2] = 0;
6741
6742 for (i = parm->lo; i <= parm->hi; i++)
6743 {
a4b6fadd 6744 struct ppc_link_hash_entry *h;
deb0e272
AM
6745
6746 sym[len + 0] = i / 10 + '0';
6747 sym[len + 1] = i % 10 + '0';
a4b6fadd 6748 h = (struct ppc_link_hash_entry *)
b32547cd 6749 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6750 if (stub_sec != NULL)
deb0e272 6751 {
a4b6fadd
AM
6752 if (h != NULL
6753 && h->elf.root.type == bfd_link_hash_defined
6754 && h->elf.root.u.def.section == htab->sfpr)
6755 {
6756 struct elf_link_hash_entry *s;
6757 char buf[32];
6758 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6759 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6760 if (s == NULL)
6761 return FALSE;
6762 if (s->root.type == bfd_link_hash_new
6763 || (s->root.type = bfd_link_hash_defined
6764 && s->root.u.def.section == stub_sec))
6765 {
6766 s->root.type = bfd_link_hash_defined;
6767 s->root.u.def.section = stub_sec;
7dda8d3c 6768 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
6769 + h->elf.root.u.def.value);
6770 s->ref_regular = 1;
6771 s->def_regular = 1;
6772 s->ref_regular_nonweak = 1;
6773 s->forced_local = 1;
6774 s->non_elf = 0;
6775 s->root.linker_def = 1;
6776 }
6777 }
6778 continue;
6779 }
6780 if (h != NULL)
6781 {
6782 h->save_res = 1;
6783 if (!h->elf.def_regular)
deb0e272 6784 {
a4b6fadd
AM
6785 h->elf.root.type = bfd_link_hash_defined;
6786 h->elf.root.u.def.section = htab->sfpr;
6787 h->elf.root.u.def.value = htab->sfpr->size;
6788 h->elf.type = STT_FUNC;
6789 h->elf.def_regular = 1;
b32547cd 6790 h->elf.non_elf = 0;
a4b6fadd
AM
6791 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6792 writing = TRUE;
deb0e272 6793 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6794 {
6795 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6796 if (htab->sfpr->contents == NULL)
6797 return FALSE;
6798 }
deb0e272
AM
6799 }
6800 }
6801 if (writing)
6802 {
6803 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6804 if (i != parm->hi)
6805 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6806 else
6807 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6808 htab->sfpr->size = p - htab->sfpr->contents;
6809 }
6810 }
6811
6812 return TRUE;
6813}
6814
6815static bfd_byte *
6816savegpr0 (bfd *abfd, bfd_byte *p, int r)
6817{
6818 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6819 return p + 4;
6820}
6821
6822static bfd_byte *
6823savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6824{
6825 p = savegpr0 (abfd, p, r);
a078d95a 6826 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6827 p = p + 4;
6828 bfd_put_32 (abfd, BLR, p);
6829 return p + 4;
6830}
6831
6832static bfd_byte *
6833restgpr0 (bfd *abfd, bfd_byte *p, int r)
6834{
6835 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6836 return p + 4;
6837}
6838
6839static bfd_byte *
6840restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6841{
a078d95a 6842 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6843 p = p + 4;
6844 p = restgpr0 (abfd, p, r);
6845 bfd_put_32 (abfd, MTLR_R0, p);
6846 p = p + 4;
6847 if (r == 29)
6848 {
6849 p = restgpr0 (abfd, p, 30);
6850 p = restgpr0 (abfd, p, 31);
6851 }
6852 bfd_put_32 (abfd, BLR, p);
6853 return p + 4;
6854}
6855
6856static bfd_byte *
6857savegpr1 (bfd *abfd, bfd_byte *p, int r)
6858{
6859 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6860 return p + 4;
6861}
6862
6863static bfd_byte *
6864savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6865{
6866 p = savegpr1 (abfd, p, r);
6867 bfd_put_32 (abfd, BLR, p);
6868 return p + 4;
6869}
6870
6871static bfd_byte *
6872restgpr1 (bfd *abfd, bfd_byte *p, int r)
6873{
6874 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6875 return p + 4;
6876}
6877
6878static bfd_byte *
6879restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6880{
6881 p = restgpr1 (abfd, p, r);
6882 bfd_put_32 (abfd, BLR, p);
6883 return p + 4;
6884}
6885
6886static bfd_byte *
6887savefpr (bfd *abfd, bfd_byte *p, int r)
6888{
6889 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6890 return p + 4;
6891}
6892
6893static bfd_byte *
6894savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6895{
6896 p = savefpr (abfd, p, r);
a078d95a 6897 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6898 p = p + 4;
6899 bfd_put_32 (abfd, BLR, p);
6900 return p + 4;
6901}
6902
6903static bfd_byte *
6904restfpr (bfd *abfd, bfd_byte *p, int r)
6905{
6906 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6907 return p + 4;
6908}
6909
6910static bfd_byte *
6911restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6912{
a078d95a 6913 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6914 p = p + 4;
6915 p = restfpr (abfd, p, r);
6916 bfd_put_32 (abfd, MTLR_R0, p);
6917 p = p + 4;
6918 if (r == 29)
6919 {
6920 p = restfpr (abfd, p, 30);
6921 p = restfpr (abfd, p, 31);
6922 }
6923 bfd_put_32 (abfd, BLR, p);
6924 return p + 4;
6925}
6926
6927static bfd_byte *
6928savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6929{
6930 p = savefpr (abfd, p, r);
6931 bfd_put_32 (abfd, BLR, p);
6932 return p + 4;
6933}
6934
6935static bfd_byte *
6936restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6937{
6938 p = restfpr (abfd, p, r);
6939 bfd_put_32 (abfd, BLR, p);
6940 return p + 4;
6941}
6942
6943static bfd_byte *
6944savevr (bfd *abfd, bfd_byte *p, int r)
6945{
6946 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6947 p = p + 4;
6948 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6949 return p + 4;
6950}
6951
6952static bfd_byte *
6953savevr_tail (bfd *abfd, bfd_byte *p, int r)
6954{
6955 p = savevr (abfd, p, r);
6956 bfd_put_32 (abfd, BLR, p);
6957 return p + 4;
6958}
6959
6960static bfd_byte *
6961restvr (bfd *abfd, bfd_byte *p, int r)
6962{
6963 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6964 p = p + 4;
6965 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6966 return p + 4;
6967}
6968
6969static bfd_byte *
6970restvr_tail (bfd *abfd, bfd_byte *p, int r)
6971{
6972 p = restvr (abfd, p, r);
6973 bfd_put_32 (abfd, BLR, p);
6974 return p + 4;
6975}
6976
e86ce104
AM
6977/* Called via elf_link_hash_traverse to transfer dynamic linking
6978 information on function code symbol entries to their corresponding
6979 function descriptor symbol entries. */
deb0e272 6980
b34976b6 6981static bfd_boolean
4ce794b7 6982func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6983{
e86ce104 6984 struct bfd_link_info *info;
65f38f15 6985 struct ppc_link_hash_table *htab;
50bc7936
AM
6986 struct ppc_link_hash_entry *fh;
6987 struct ppc_link_hash_entry *fdh;
6988 bfd_boolean force_local;
5bd4f169 6989
50bc7936
AM
6990 fh = (struct ppc_link_hash_entry *) h;
6991 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6992 return TRUE;
e86ce104 6993
8c5b4e52
AM
6994 if (!fh->is_func)
6995 return TRUE;
6996
6997 if (fh->elf.root.root.string[0] != '.'
6998 || fh->elf.root.root.string[1] == '\0')
6999 return TRUE;
7000
4ce794b7 7001 info = inf;
65f38f15 7002 htab = ppc_hash_table (info);
4dfe6ac6
NC
7003 if (htab == NULL)
7004 return FALSE;
5bd4f169 7005
8c5b4e52
AM
7006 /* Find the corresponding function descriptor symbol. */
7007 fdh = lookup_fdh (fh, htab);
7008
c09bdfe5
AM
7009 /* Resolve undefined references to dot-symbols as the value
7010 in the function descriptor, if we have one in a regular object.
7011 This is to satisfy cases like ".quad .foo". Calls to functions
7012 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
7013 if ((fh->elf.root.type == bfd_link_hash_undefined
7014 || fh->elf.root.type == bfd_link_hash_undefweak)
7015 && (fdh->elf.root.type == bfd_link_hash_defined
7016 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
7017 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7018 && opd_entry_value (fdh->elf.root.u.def.section,
7019 fdh->elf.root.u.def.value,
c09bdfe5 7020 &fh->elf.root.u.def.section,
aef36ac1 7021 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 7022 {
b31867b6 7023 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 7024 fh->elf.forced_local = 1;
b31867b6
AM
7025 fh->elf.def_regular = fdh->elf.def_regular;
7026 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
7027 }
7028
8c5b4e52
AM
7029 if (!fh->elf.dynamic)
7030 {
7031 struct plt_entry *ent;
5bd4f169 7032
8c5b4e52
AM
7033 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7034 if (ent->plt.refcount > 0)
7035 break;
7036 if (ent == NULL)
7037 return TRUE;
7038 }
5bd4f169 7039
8c5b4e52 7040 /* Create a descriptor as undefined if necessary. */
50bc7936 7041 if (fdh == NULL
0e1862bb 7042 && !bfd_link_executable (info)
50bc7936
AM
7043 && (fh->elf.root.type == bfd_link_hash_undefined
7044 || fh->elf.root.type == bfd_link_hash_undefweak))
7045 {
908b32fc 7046 fdh = make_fdh (info, fh);
bb700d78
AM
7047 if (fdh == NULL)
7048 return FALSE;
50bc7936 7049 }
648cca2c 7050
8c5b4e52 7051 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
7052 if (fdh != NULL
7053 && fdh->fake
8c5b4e52
AM
7054 && (fh->elf.root.type == bfd_link_hash_defined
7055 || fh->elf.root.type == bfd_link_hash_defweak))
7056 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 7057
8c5b4e52
AM
7058 /* Transfer dynamic linking information to the function descriptor. */
7059 if (fdh != NULL)
7060 {
f5385ebf
AM
7061 fdh->elf.ref_regular |= fh->elf.ref_regular;
7062 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7063 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7064 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
7065 fdh->elf.dynamic |= fh->elf.dynamic;
7066 fdh->elf.needs_plt |= (fh->elf.needs_plt
7067 || fh->elf.type == STT_FUNC
7068 || fh->elf.type == STT_GNU_IFUNC);
7069 move_plt_plist (fh, fdh);
7070
7071 if (!fdh->elf.forced_local
7072 && fh->elf.dynindx != -1)
7073 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7074 return FALSE;
e86ce104
AM
7075 }
7076
50bc7936
AM
7077 /* Now that the info is on the function descriptor, clear the
7078 function code sym info. Any function code syms for which we
7079 don't have a definition in a regular file, we force local.
7080 This prevents a shared library from exporting syms that have
7081 been imported from another library. Function code syms that
7082 are really in the library we must leave global to prevent the
7083 linker dragging in a definition from a static library. */
93f3fa99
AM
7084 force_local = (!fh->elf.def_regular
7085 || fdh == NULL
7086 || !fdh->elf.def_regular
7087 || fdh->elf.forced_local);
50bc7936
AM
7088 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7089
b34976b6 7090 return TRUE;
e86ce104 7091}
40b8271b 7092
a4b6fadd
AM
7093static const struct sfpr_def_parms save_res_funcs[] =
7094 {
7095 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7096 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7097 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7098 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7099 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7100 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7101 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7102 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7103 { "._savef", 14, 31, savefpr, savefpr1_tail },
7104 { "._restf", 14, 31, restfpr, restfpr1_tail },
7105 { "_savevr_", 20, 31, savevr, savevr_tail },
7106 { "_restvr_", 20, 31, restvr, restvr_tail }
7107 };
7108
e86ce104 7109/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
7110 this hook to a) provide some gcc support functions, and b) transfer
7111 dynamic linking information gathered so far on function code symbol
7112 entries, to their corresponding function descriptor symbol entries. */
deb0e272 7113
b34976b6 7114static bfd_boolean
4ce794b7
AM
7115ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7116 struct bfd_link_info *info)
e86ce104
AM
7117{
7118 struct ppc_link_hash_table *htab;
7119
7120 htab = ppc_hash_table (info);
4dfe6ac6
NC
7121 if (htab == NULL)
7122 return FALSE;
7123
b32547cd
AM
7124 /* Provide any missing _save* and _rest* functions. */
7125 if (htab->sfpr != NULL)
7126 {
7127 unsigned int i;
7128
7129 htab->sfpr->size = 0;
7130 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7131 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7132 return FALSE;
7133 if (htab->sfpr->size == 0)
7134 htab->sfpr->flags |= SEC_EXCLUDE;
7135 }
7136
7137 if (bfd_link_relocatable (info))
7138 return TRUE;
7139
7140 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7141 {
7142 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7143 /* Make .TOC. defined so as to prevent it being made dynamic.
7144 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7145 if (!htab->elf.hgot->def_regular
7146 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7147 {
7148 htab->elf.hgot->root.type = bfd_link_hash_defined;
7149 htab->elf.hgot->root.u.def.value = 0;
7150 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7151 htab->elf.hgot->def_regular = 1;
7152 htab->elf.hgot->root.linker_def = 1;
7153 }
dba6fa9b 7154 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7155 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7156 | STV_HIDDEN);
7157 }
c66bb0ee 7158
8c5b4e52
AM
7159 if (htab->need_func_desc_adj)
7160 {
7161 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7162 htab->need_func_desc_adj = 0;
7163 }
805fc799 7164
b34976b6 7165 return TRUE;
e86ce104
AM
7166}
7167
98bbb1b8 7168/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 7169
98bbb1b8 7170static asection *
a345bc8d
AM
7171readonly_dynrelocs (struct elf_link_hash_entry *h)
7172{
7173 struct ppc_link_hash_entry *eh;
7174 struct elf_dyn_relocs *p;
7175
7176 eh = (struct ppc_link_hash_entry *) h;
7177 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7178 {
7179 asection *s = p->sec->output_section;
7180
7181 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 7182 return p->sec;
a345bc8d 7183 }
98bbb1b8 7184 return NULL;
a345bc8d
AM
7185}
7186
d311bc8b 7187/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
7188 aliases, that apply to read-only sections. Cannot be used after
7189 size_dynamic_sections. */
d311bc8b
AM
7190
7191static bfd_boolean
7192alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7193{
7194 struct ppc_link_hash_entry *eh;
7195
7196 eh = (struct ppc_link_hash_entry *) h;
7197 do
7198 {
7199 if (readonly_dynrelocs (&eh->elf))
7200 return TRUE;
ab2477e1 7201 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
d311bc8b
AM
7202 } while (eh != NULL && &eh->elf != h);
7203
7204 return FALSE;
7205}
8a2058b5 7206
8a9e8e72
AM
7207/* Return whether EH has pc-relative dynamic relocs. */
7208
7209static bfd_boolean
7210pc_dynrelocs (struct ppc_link_hash_entry *eh)
7211{
7212 struct elf_dyn_relocs *p;
7213
7214 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7215 if (p->pc_count != 0)
7216 return TRUE;
7217 return FALSE;
7218}
7219
8a2058b5
AM
7220/* Return true if a global entry stub will be created for H. Valid
7221 for ELFv2 before plt entries have been allocated. */
7222
7223static bfd_boolean
7224global_entry_stub (struct elf_link_hash_entry *h)
7225{
7226 struct plt_entry *pent;
7227
7228 if (!h->pointer_equality_needed
7229 || h->def_regular)
7230 return FALSE;
7231
7232 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7233 if (pent->plt.refcount > 0
7234 && pent->addend == 0)
7235 return TRUE;
7236
7237 return FALSE;
7238}
7239
e86ce104
AM
7240/* Adjust a symbol defined by a dynamic object and referenced by a
7241 regular object. The current definition is in some section of the
7242 dynamic object, but we're not including those sections. We have to
7243 change the definition to something the rest of the link can
7244 understand. */
7245
b34976b6 7246static bfd_boolean
4ce794b7
AM
7247ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7248 struct elf_link_hash_entry *h)
e86ce104
AM
7249{
7250 struct ppc_link_hash_table *htab;
5474d94f 7251 asection *s, *srel;
e86ce104
AM
7252
7253 htab = ppc_hash_table (info);
4dfe6ac6
NC
7254 if (htab == NULL)
7255 return FALSE;
e86ce104
AM
7256
7257 /* Deal with function syms. */
7258 if (h->type == STT_FUNC
e054468f 7259 || h->type == STT_GNU_IFUNC
f5385ebf 7260 || h->needs_plt)
e86ce104 7261 {
529fe20e
AM
7262 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7263 || SYMBOL_CALLS_LOCAL (info, h)
7264 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7265 /* Discard dyn_relocs when non-pic if we've decided that a
7266 function symbol is local and not an ifunc. We keep dynamic
7267 relocs for ifuncs when local rather than always emitting a
7268 plt call stub for them and defining the symbol on the call
7269 stub. We can't do that for ELFv1 anyway (a function symbol
7270 is defined on a descriptor, not code) and it can be faster at
7271 run-time due to not needing to bounce through a stub. The
7272 dyn_relocs for ifuncs will be applied even in a static
7273 executable. */
7274 if (!bfd_link_pic (info)
7275 && h->type != STT_GNU_IFUNC
7276 && local)
7277 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7278
e86ce104
AM
7279 /* Clear procedure linkage table information for any symbol that
7280 won't need a .plt entry. */
411e1bfb
AM
7281 struct plt_entry *ent;
7282 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7283 if (ent->plt.refcount > 0)
7284 break;
8387904d 7285 if (ent == NULL
2d7ad24e
AM
7286 || (h->type != STT_GNU_IFUNC
7287 && local
3e04d765
AM
7288 && (htab->can_convert_all_inline_plt
7289 || (((struct ppc_link_hash_entry *) h)->tls_mask
7290 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 7291 {
411e1bfb 7292 h->plt.plist = NULL;
f5385ebf 7293 h->needs_plt = 0;
d1eca1e4 7294 h->pointer_equality_needed = 0;
40b8271b 7295 }
8a2058b5 7296 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7297 {
d1eca1e4
AM
7298 /* Taking a function's address in a read/write section
7299 doesn't require us to define the function symbol in the
7300 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7301 be used instead. The reason we prefer a few more dynamic
7302 relocs is that calling via a global entry stub costs a
7303 few more instructions, and pointer_equality_needed causes
7304 extra work in ld.so when resolving these symbols. */
529fe20e 7305 if (global_entry_stub (h))
d1eca1e4 7306 {
ab2477e1 7307 if (!readonly_dynrelocs (h))
529fe20e
AM
7308 {
7309 h->pointer_equality_needed = 0;
04383fd1
AM
7310 /* If we haven't seen a branch reloc and the symbol
7311 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
7312 if (!h->needs_plt)
7313 h->plt.plist = NULL;
7314 }
7315 else if (!bfd_link_pic (info))
7316 /* We are going to be defining the function symbol on the
7317 plt stub, so no dyn_relocs needed when non-pic. */
7318 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
7319 }
7320
3988aed5
AM
7321 /* ELFv2 function symbols can't have copy relocs. */
7322 return TRUE;
7323 }
7324 else if (!h->needs_plt
ab2477e1 7325 && !readonly_dynrelocs (h))
3988aed5 7326 {
04383fd1
AM
7327 /* If we haven't seen a branch reloc and the symbol isn't an
7328 ifunc then we don't need a plt entry. */
3988aed5
AM
7329 h->plt.plist = NULL;
7330 h->pointer_equality_needed = 0;
a345bc8d
AM
7331 return TRUE;
7332 }
5bd4f169 7333 }
bbd7ec4a 7334 else
411e1bfb 7335 h->plt.plist = NULL;
5bd4f169
AM
7336
7337 /* If this is a weak symbol, and there is a real definition, the
7338 processor independent code will have arranged for us to see the
7339 real definition first, and we can just use the same value. */
60d67dc8 7340 if (h->is_weakalias)
5bd4f169 7341 {
60d67dc8
AM
7342 struct elf_link_hash_entry *def = weakdef (h);
7343 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7344 h->root.u.def.section = def->root.u.def.section;
7345 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
7346 if (def->root.u.def.section == htab->elf.sdynbss
7347 || def->root.u.def.section == htab->elf.sdynrelro)
7348 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 7349 return TRUE;
5bd4f169
AM
7350 }
7351
5bd4f169
AM
7352 /* If we are creating a shared library, we must presume that the
7353 only references to the symbol are via the global offset table.
7354 For such cases we need not do anything here; the relocations will
7355 be handled correctly by relocate_section. */
0e1862bb 7356 if (bfd_link_pic (info))
b34976b6 7357 return TRUE;
5bd4f169 7358
65f38f15
AM
7359 /* If there are no references to this symbol that do not use the
7360 GOT, we don't need to generate a copy reloc. */
f5385ebf 7361 if (!h->non_got_ref)
b34976b6 7362 return TRUE;
65f38f15 7363
b186458a 7364 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7365 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7366
d93d1c80
AM
7367 /* If -z nocopyreloc was given, don't generate them either. */
7368 || info->nocopyreloc
a127494f 7369
dce2246a 7370 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 7371 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7372 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7373
d93d1c80
AM
7374 /* Protected variables do not work with .dynbss. The copy in
7375 .dynbss won't be used by the shared library with the protected
7376 definition for the variable. Text relocations are preferable
7377 to an incorrect program. */
7378 || h->protected_def)
529fe20e 7379 return TRUE;
a127494f 7380
5d35169e 7381 if (h->plt.plist != NULL)
97b639ba
AM
7382 {
7383 /* We should never get here, but unfortunately there are versions
7384 of gcc out there that improperly (for this ABI) put initialized
7385 function pointers, vtable refs and suchlike in read-only
7386 sections. Allow them to proceed, but warn that this might
7387 break at runtime. */
25f53a85 7388 info->callbacks->einfo
c1c8c1ef 7389 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 7390 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7391 h->root.root.string);
7392 }
5d35169e
AM
7393
7394 /* This is a reference to a symbol defined by a dynamic object which
7395 is not a function. */
7396
5bd4f169
AM
7397 /* We must allocate the symbol in our .dynbss section, which will
7398 become part of the .bss section of the executable. There will be
7399 an entry for this symbol in the .dynsym section. The dynamic
7400 object will contain position independent code, so all references
7401 from the dynamic object to this symbol will go through the global
7402 offset table. The dynamic linker will use the .dynsym entry to
7403 determine the address it must put in the global offset table, so
7404 both the dynamic object and the regular object will refer to the
7405 same memory location for the variable. */
5474d94f
AM
7406 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7407 {
7408 s = htab->elf.sdynrelro;
7409 srel = htab->elf.sreldynrelro;
7410 }
7411 else
7412 {
7413 s = htab->elf.sdynbss;
7414 srel = htab->elf.srelbss;
7415 }
1d7e9d18 7416 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7417 {
4a7e5234
AM
7418 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7419 linker to copy the initial value out of the dynamic object
7420 and into the runtime process image. */
5474d94f 7421 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 7422 h->needs_copy = 1;
5bd4f169
AM
7423 }
7424
529fe20e
AM
7425 /* We no longer want dyn_relocs. */
7426 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 7427 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7428}
7429
e86ce104
AM
7430/* If given a function descriptor symbol, hide both the function code
7431 sym and the descriptor. */
7432static void
4ce794b7
AM
7433ppc64_elf_hide_symbol (struct bfd_link_info *info,
7434 struct elf_link_hash_entry *h,
7435 bfd_boolean force_local)
e86ce104 7436{
34814b9f 7437 struct ppc_link_hash_entry *eh;
e86ce104
AM
7438 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7439
34814b9f
AM
7440 eh = (struct ppc_link_hash_entry *) h;
7441 if (eh->is_func_descriptor)
e86ce104 7442 {
34814b9f 7443 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7444
721956f4 7445 if (fh == NULL)
d1329ca3
AM
7446 {
7447 const char *p, *q;
b8ac2841 7448 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
7449 char save;
7450
7451 /* We aren't supposed to use alloca in BFD because on
7452 systems which do not have alloca the version in libiberty
7453 calls xmalloc, which might cause the program to crash
7454 when it runs out of memory. This function doesn't have a
7455 return status, so there's no way to gracefully return an
7456 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7457 accessed; It's either a string in an ELF string table,
7458 or allocated in an objalloc structure. */
d1329ca3 7459
34814b9f 7460 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7461 save = *p;
7462 *(char *) p = '.';
34814b9f 7463 fh = (struct ppc_link_hash_entry *)
b8ac2841 7464 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7465 *(char *) p = save;
7466
7467 /* Unfortunately, if it so happens that the string we were
7468 looking for was allocated immediately before this string,
7469 then we overwrote the string terminator. That's the only
7470 reason the lookup should fail. */
7471 if (fh == NULL)
7472 {
34814b9f
AM
7473 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7474 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7475 --q, --p;
34814b9f
AM
7476 if (q < eh->elf.root.root.string && *p == '.')
7477 fh = (struct ppc_link_hash_entry *)
b8ac2841 7478 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7479 }
7480 if (fh != NULL)
7481 {
34814b9f
AM
7482 eh->oh = fh;
7483 fh->oh = eh;
d1329ca3
AM
7484 }
7485 }
e86ce104 7486 if (fh != NULL)
34814b9f 7487 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7488 }
7489}
7490
411e1bfb 7491static bfd_boolean
8843416a
AM
7492get_sym_h (struct elf_link_hash_entry **hp,
7493 Elf_Internal_Sym **symp,
7494 asection **symsecp,
f961d9dd 7495 unsigned char **tls_maskp,
8843416a
AM
7496 Elf_Internal_Sym **locsymsp,
7497 unsigned long r_symndx,
7498 bfd *ibfd)
411e1bfb 7499{
0ffa91dd 7500 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7501
7502 if (r_symndx >= symtab_hdr->sh_info)
7503 {
7504 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7505 struct elf_link_hash_entry *h;
7506
7507 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7508 h = elf_follow_link (h);
411e1bfb
AM
7509
7510 if (hp != NULL)
7511 *hp = h;
7512
7513 if (symp != NULL)
7514 *symp = NULL;
7515
7516 if (symsecp != NULL)
7517 {
7518 asection *symsec = NULL;
7519 if (h->root.type == bfd_link_hash_defined
7520 || h->root.type == bfd_link_hash_defweak)
7521 symsec = h->root.u.def.section;
7522 *symsecp = symsec;
7523 }
7524
e7b938ca 7525 if (tls_maskp != NULL)
411e1bfb
AM
7526 {
7527 struct ppc_link_hash_entry *eh;
7528
7529 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7530 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7531 }
7532 }
7533 else
7534 {
7535 Elf_Internal_Sym *sym;
7536 Elf_Internal_Sym *locsyms = *locsymsp;
7537
7538 if (locsyms == NULL)
7539 {
7540 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7541 if (locsyms == NULL)
7542 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7543 symtab_hdr->sh_info,
7544 0, NULL, NULL, NULL);
7545 if (locsyms == NULL)
7546 return FALSE;
7547 *locsymsp = locsyms;
7548 }
7549 sym = locsyms + r_symndx;
7550
7551 if (hp != NULL)
7552 *hp = NULL;
7553
7554 if (symp != NULL)
7555 *symp = sym;
7556
7557 if (symsecp != NULL)
cb33740c 7558 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7559
e7b938ca 7560 if (tls_maskp != NULL)
411e1bfb
AM
7561 {
7562 struct got_entry **lgot_ents;
f961d9dd 7563 unsigned char *tls_mask;
411e1bfb 7564
e7b938ca 7565 tls_mask = NULL;
411e1bfb
AM
7566 lgot_ents = elf_local_got_ents (ibfd);
7567 if (lgot_ents != NULL)
7568 {
e054468f
AM
7569 struct plt_entry **local_plt = (struct plt_entry **)
7570 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7571 unsigned char *lgot_masks = (unsigned char *)
e054468f 7572 (local_plt + symtab_hdr->sh_info);
e7b938ca 7573 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7574 }
e7b938ca 7575 *tls_maskp = tls_mask;
411e1bfb
AM
7576 }
7577 }
7578 return TRUE;
7579}
7580
e7b938ca 7581/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7582 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7583 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7584
7585static int
f961d9dd 7586get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7587 unsigned long *toc_symndx,
7588 bfd_vma *toc_addend,
0d4792f7 7589 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7590 const Elf_Internal_Rela *rel,
7591 bfd *ibfd)
411e1bfb
AM
7592{
7593 unsigned long r_symndx;
0d4792f7 7594 int next_r;
411e1bfb
AM
7595 struct elf_link_hash_entry *h;
7596 Elf_Internal_Sym *sym;
7597 asection *sec;
7598 bfd_vma off;
7599
7600 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7601 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7602 return 0;
411e1bfb 7603
37da22e5
AM
7604 if ((*tls_maskp != NULL
7605 && (**tls_maskp & TLS_TLS) != 0
7606 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 7607 || sec == NULL
6bee8834 7608 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7609 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7610 return 1;
411e1bfb
AM
7611
7612 /* Look inside a TOC section too. */
7613 if (h != NULL)
7614 {
7615 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7616 off = h->root.u.def.value;
7617 }
7618 else
7619 off = sym->st_value;
7620 off += rel->r_addend;
7621 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7622 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7623 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7624 if (toc_symndx != NULL)
7625 *toc_symndx = r_symndx;
3a71aa26
AM
7626 if (toc_addend != NULL)
7627 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7628 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7629 return 0;
854b41e7 7630 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7631 && (next_r == -1 || next_r == -2))
7632 return 1 - next_r;
951fd09b 7633 return 1;
411e1bfb
AM
7634}
7635
3b421ab3
AM
7636/* Find (or create) an entry in the tocsave hash table. */
7637
7638static struct tocsave_entry *
7639tocsave_find (struct ppc_link_hash_table *htab,
7640 enum insert_option insert,
7641 Elf_Internal_Sym **local_syms,
7642 const Elf_Internal_Rela *irela,
7643 bfd *ibfd)
7644{
7645 unsigned long r_indx;
7646 struct elf_link_hash_entry *h;
7647 Elf_Internal_Sym *sym;
7648 struct tocsave_entry ent, *p;
7649 hashval_t hash;
7650 struct tocsave_entry **slot;
7651
7652 r_indx = ELF64_R_SYM (irela->r_info);
7653 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7654 return NULL;
7655 if (ent.sec == NULL || ent.sec->output_section == NULL)
7656 {
4eca0228 7657 _bfd_error_handler
871b3ab2 7658 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7659 return NULL;
7660 }
7661
7662 if (h != NULL)
7663 ent.offset = h->root.u.def.value;
7664 else
7665 ent.offset = sym->st_value;
7666 ent.offset += irela->r_addend;
7667
7668 hash = tocsave_htab_hash (&ent);
7669 slot = ((struct tocsave_entry **)
7670 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7671 if (slot == NULL)
7672 return NULL;
7673
7674 if (*slot == NULL)
7675 {
7676 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7677 if (p == NULL)
7678 return NULL;
7679 *p = ent;
7680 *slot = p;
7681 }
7682 return *slot;
7683}
7684
754021d0 7685/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7686 code for the old ABI, these will already have been done. */
754021d0
AM
7687
7688static bfd_boolean
7689adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7690{
7691 struct ppc_link_hash_entry *eh;
7692 asection *sym_sec;
74f0fb50 7693 struct _opd_sec_data *opd;
754021d0
AM
7694
7695 if (h->root.type == bfd_link_hash_indirect)
7696 return TRUE;
7697
754021d0
AM
7698 if (h->root.type != bfd_link_hash_defined
7699 && h->root.type != bfd_link_hash_defweak)
7700 return TRUE;
7701
7702 eh = (struct ppc_link_hash_entry *) h;
7703 if (eh->adjust_done)
7704 return TRUE;
7705
7706 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7707 opd = get_opd_info (sym_sec);
7708 if (opd != NULL && opd->adjust != NULL)
754021d0 7709 {
51aecdc5 7710 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7711 if (adjust == -1)
7712 {
7713 /* This entry has been deleted. */
b3fac117 7714 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7715 if (dsec == NULL)
7716 {
7717 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7718 if (discarded_section (dsec))
81688140 7719 {
b3fac117 7720 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7721 break;
7722 }
7723 }
4025353c 7724 eh->elf.root.u.def.value = 0;
81688140 7725 eh->elf.root.u.def.section = dsec;
4025353c
AM
7726 }
7727 else
7728 eh->elf.root.u.def.value += adjust;
754021d0
AM
7729 eh->adjust_done = 1;
7730 }
7731 return TRUE;
7732}
7733
8c1d1bb8 7734/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7735 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7736 have already been determined. */
7737
7738static bfd_boolean
7739dec_dynrel_count (bfd_vma r_info,
7740 asection *sec,
7741 struct bfd_link_info *info,
7742 Elf_Internal_Sym **local_syms,
7743 struct elf_link_hash_entry *h,
19e08130 7744 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7745{
7746 enum elf_ppc64_reloc_type r_type;
19e08130 7747 asection *sym_sec = NULL;
8c1d1bb8
AM
7748
7749 /* Can this reloc be dynamic? This switch, and later tests here
7750 should be kept in sync with the code in check_relocs. */
7751 r_type = ELF64_R_TYPE (r_info);
7752 switch (r_type)
7753 {
7754 default:
7755 return TRUE;
7756
7757 case R_PPC64_TPREL16:
7758 case R_PPC64_TPREL16_LO:
7759 case R_PPC64_TPREL16_HI:
7760 case R_PPC64_TPREL16_HA:
7761 case R_PPC64_TPREL16_DS:
7762 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7763 case R_PPC64_TPREL16_HIGH:
7764 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7765 case R_PPC64_TPREL16_HIGHER:
7766 case R_PPC64_TPREL16_HIGHERA:
7767 case R_PPC64_TPREL16_HIGHEST:
7768 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
7769 case R_PPC64_TPREL64:
7770 case R_PPC64_DTPMOD64:
7771 case R_PPC64_DTPREL64:
7772 case R_PPC64_ADDR64:
7773 case R_PPC64_REL30:
7774 case R_PPC64_REL32:
7775 case R_PPC64_REL64:
7776 case R_PPC64_ADDR14:
7777 case R_PPC64_ADDR14_BRNTAKEN:
7778 case R_PPC64_ADDR14_BRTAKEN:
7779 case R_PPC64_ADDR16:
7780 case R_PPC64_ADDR16_DS:
7781 case R_PPC64_ADDR16_HA:
7782 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7783 case R_PPC64_ADDR16_HIGH:
7784 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7785 case R_PPC64_ADDR16_HIGHER:
7786 case R_PPC64_ADDR16_HIGHERA:
7787 case R_PPC64_ADDR16_HIGHEST:
7788 case R_PPC64_ADDR16_HIGHESTA:
7789 case R_PPC64_ADDR16_LO:
7790 case R_PPC64_ADDR16_LO_DS:
7791 case R_PPC64_ADDR24:
7792 case R_PPC64_ADDR32:
7793 case R_PPC64_UADDR16:
7794 case R_PPC64_UADDR32:
7795 case R_PPC64_UADDR64:
7796 case R_PPC64_TOC:
7797 break;
7798 }
7799
7800 if (local_syms != NULL)
7801 {
7802 unsigned long r_symndx;
8c1d1bb8
AM
7803 bfd *ibfd = sec->owner;
7804
7805 r_symndx = ELF64_R_SYM (r_info);
7806 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7807 return FALSE;
7808 }
7809
0e1862bb 7810 if ((bfd_link_pic (info)
1d483afe 7811 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7812 || (h != NULL
198f1157 7813 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7814 || h->root.type == bfd_link_hash_defweak
7815 || !h->def_regular))))
7816 || (ELIMINATE_COPY_RELOCS
0e1862bb 7817 && !bfd_link_pic (info)
8c1d1bb8
AM
7818 && h != NULL
7819 && (h->root.type == bfd_link_hash_defweak
7820 || !h->def_regular)))
7821 ;
7822 else
7823 return TRUE;
7824
7825 if (h != NULL)
6edfbbad 7826 {
19e08130
AM
7827 struct elf_dyn_relocs *p;
7828 struct elf_dyn_relocs **pp;
7829 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7830
7831 /* elf_gc_sweep may have already removed all dyn relocs associated
7832 with local syms for a given section. Also, symbol flags are
7833 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7834 report a dynreloc miscount. */
7835 if (*pp == NULL && info->gc_sections)
7836 return TRUE;
7837
7838 while ((p = *pp) != NULL)
60124e18 7839 {
19e08130
AM
7840 if (p->sec == sec)
7841 {
7842 if (!must_be_dyn_reloc (info, r_type))
7843 p->pc_count -= 1;
7844 p->count -= 1;
7845 if (p->count == 0)
7846 *pp = p->next;
7847 return TRUE;
7848 }
7849 pp = &p->next;
60124e18 7850 }
6edfbbad 7851 }
19e08130
AM
7852 else
7853 {
7854 struct ppc_dyn_relocs *p;
7855 struct ppc_dyn_relocs **pp;
7856 void *vpp;
7857 bfd_boolean is_ifunc;
8c1d1bb8 7858
19e08130
AM
7859 if (local_syms == NULL)
7860 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7861 if (sym_sec == NULL)
7862 sym_sec = sec;
c57da1a7 7863
19e08130
AM
7864 vpp = &elf_section_data (sym_sec)->local_dynrel;
7865 pp = (struct ppc_dyn_relocs **) vpp;
7866
7867 if (*pp == NULL && info->gc_sections)
7868 return TRUE;
7869
7870 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7871 while ((p = *pp) != NULL)
8c1d1bb8 7872 {
19e08130
AM
7873 if (p->sec == sec && p->ifunc == is_ifunc)
7874 {
7875 p->count -= 1;
7876 if (p->count == 0)
7877 *pp = p->next;
7878 return TRUE;
7879 }
7880 pp = &p->next;
8c1d1bb8 7881 }
8c1d1bb8
AM
7882 }
7883
695344c0 7884 /* xgettext:c-format */
cf97bcb0
AM
7885 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7886 sec->owner, sec);
8c1d1bb8
AM
7887 bfd_set_error (bfd_error_bad_value);
7888 return FALSE;
7889}
7890
754021d0
AM
7891/* Remove unused Official Procedure Descriptor entries. Currently we
7892 only remove those associated with functions in discarded link-once
7893 sections, or weakly defined functions that have been overridden. It
7894 would be possible to remove many more entries for statically linked
7895 applications. */
7896
b34976b6 7897bfd_boolean
e7d1c40c 7898ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7899{
7900 bfd *ibfd;
754021d0 7901 bfd_boolean some_edited = FALSE;
3f764659 7902 asection *need_pad = NULL;
e7d1c40c
AM
7903 struct ppc_link_hash_table *htab;
7904
7905 htab = ppc_hash_table (info);
7906 if (htab == NULL)
7907 return FALSE;
1e2f5b6e 7908
c72f2fb2 7909 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7910 {
7911 asection *sec;
7912 Elf_Internal_Rela *relstart, *rel, *relend;
7913 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7914 Elf_Internal_Sym *local_syms;
74f0fb50 7915 struct _opd_sec_data *opd;
51aecdc5 7916 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7917 bfd_size_type cnt_16b = 0;
1e2f5b6e 7918
854b41e7
AM
7919 if (!is_ppc64_elf (ibfd))
7920 continue;
7921
1e2f5b6e 7922 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7923 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7924 continue;
7925
dbaa2011 7926 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7927 continue;
7928
1e2f5b6e
AM
7929 if (sec->output_section == bfd_abs_section_ptr)
7930 continue;
7931
7932 /* Look through the section relocs. */
7933 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7934 continue;
7935
6cdc0ccc 7936 local_syms = NULL;
0ffa91dd 7937 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7938
7939 /* Read the relocations. */
4ce794b7 7940 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7941 info->keep_memory);
1e2f5b6e 7942 if (relstart == NULL)
b34976b6 7943 return FALSE;
1e2f5b6e
AM
7944
7945 /* First run through the relocs to check they are sane, and to
7946 determine whether we need to edit this opd section. */
b34976b6 7947 need_edit = FALSE;
51aecdc5 7948 broken = FALSE;
3f764659 7949 need_pad = sec;
1e2f5b6e 7950 relend = relstart + sec->reloc_count;
50bc7936 7951 for (rel = relstart; rel < relend; )
1e2f5b6e 7952 {
04c9666a 7953 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7954 unsigned long r_symndx;
7955 asection *sym_sec;
7956 struct elf_link_hash_entry *h;
7957 Elf_Internal_Sym *sym;
51aecdc5 7958 bfd_vma offset;
1e2f5b6e 7959
51aecdc5 7960 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7961 only interested in the reloc pointing to a function entry
7962 point. */
51aecdc5
AM
7963 offset = rel->r_offset;
7964 if (rel + 1 == relend
7965 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7966 {
7967 /* If someone messes with .opd alignment then after a
7968 "ld -r" we might have padding in the middle of .opd.
7969 Also, there's nothing to prevent someone putting
7970 something silly in .opd with the assembler. No .opd
b34976b6 7971 optimization for them! */
3f764659 7972 broken_opd:
4eca0228 7973 _bfd_error_handler
871b3ab2 7974 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7975 broken = TRUE;
1e2f5b6e
AM
7976 break;
7977 }
7978
50bc7936
AM
7979 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7980 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7981 {
4eca0228 7982 _bfd_error_handler
695344c0 7983 /* xgettext:c-format */
871b3ab2 7984 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7985 ibfd, r_type);
51aecdc5 7986 broken = TRUE;
50bc7936
AM
7987 break;
7988 }
7989
1e2f5b6e 7990 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7991 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7992 r_symndx, ibfd))
50bc7936 7993 goto error_ret;
1e2f5b6e
AM
7994
7995 if (sym_sec == NULL || sym_sec->owner == NULL)
7996 {
411e1bfb
AM
7997 const char *sym_name;
7998 if (h != NULL)
7999 sym_name = h->root.root.string;
8000 else
26c61ae5
L
8001 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
8002 sym_sec);
411e1bfb 8003
4eca0228 8004 _bfd_error_handler
695344c0 8005 /* xgettext:c-format */
871b3ab2 8006 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 8007 ibfd, sym_name);
51aecdc5 8008 broken = TRUE;
1e2f5b6e
AM
8009 break;
8010 }
8011
51020317
AM
8012 /* opd entries are always for functions defined in the
8013 current input bfd. If the symbol isn't defined in the
8014 input bfd, then we won't be using the function in this
8015 bfd; It must be defined in a linkonce section in another
8016 bfd, or is weak. It's also possible that we are
8017 discarding the function due to a linker script /DISCARD/,
8018 which we test for via the output_section. */
8019 if (sym_sec->owner != ibfd
8020 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 8021 need_edit = TRUE;
1e2f5b6e 8022
50bc7936 8023 rel += 2;
51aecdc5
AM
8024 if (rel + 1 == relend
8025 || (rel + 2 < relend
8026 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8027 ++rel;
8028
8029 if (rel == relend)
3f764659
JJ
8030 {
8031 if (sec->size == offset + 24)
8032 {
8033 need_pad = NULL;
8034 break;
8035 }
51aecdc5 8036 if (sec->size == offset + 16)
3f764659
JJ
8037 {
8038 cnt_16b++;
8039 break;
8040 }
8041 goto broken_opd;
8042 }
3f764659
JJ
8043 else if (rel + 1 < relend
8044 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8045 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8046 {
51aecdc5
AM
8047 if (rel[0].r_offset == offset + 16)
8048 cnt_16b++;
8049 else if (rel[0].r_offset != offset + 24)
8050 goto broken_opd;
3f764659
JJ
8051 }
8052 else
8053 goto broken_opd;
1e2f5b6e
AM
8054 }
8055
e7d1c40c 8056 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 8057
51aecdc5 8058 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
8059 {
8060 Elf_Internal_Rela *write_rel;
d4730f92 8061 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 8062 bfd_byte *rptr, *wptr;
983bddc8 8063 bfd_byte *new_contents;
74f0fb50
AM
8064 bfd_size_type amt;
8065
983bddc8 8066 new_contents = NULL;
51aecdc5 8067 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 8068 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 8069 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
8070 if (opd->adjust == NULL)
8071 return FALSE;
1e2f5b6e
AM
8072
8073 /* This seems a waste of time as input .opd sections are all
8074 zeros as generated by gcc, but I suppose there's no reason
8075 this will always be so. We might start putting something in
8076 the third word of .opd entries. */
8077 if ((sec->flags & SEC_IN_MEMORY) == 0)
8078 {
eea6121a
AM
8079 bfd_byte *loc;
8080 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 8081 {
eea6121a
AM
8082 if (loc != NULL)
8083 free (loc);
50bc7936 8084 error_ret:
6cdc0ccc
AM
8085 if (local_syms != NULL
8086 && symtab_hdr->contents != (unsigned char *) local_syms)
8087 free (local_syms);
6cdc0ccc
AM
8088 if (elf_section_data (sec)->relocs != relstart)
8089 free (relstart);
b34976b6 8090 return FALSE;
6cdc0ccc 8091 }
1e2f5b6e
AM
8092 sec->contents = loc;
8093 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8094 }
8095
8096 elf_section_data (sec)->relocs = relstart;
8097
3f764659 8098 new_contents = sec->contents;
3f764659
JJ
8099 if (add_aux_fields)
8100 {
8101 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8102 if (new_contents == NULL)
8103 return FALSE;
51aecdc5 8104 need_pad = NULL;
3f764659 8105 }
b4f4e59f
AM
8106 wptr = new_contents;
8107 rptr = sec->contents;
1e2f5b6e 8108 write_rel = relstart;
51aecdc5 8109 for (rel = relstart; rel < relend; )
1e2f5b6e 8110 {
50bc7936
AM
8111 unsigned long r_symndx;
8112 asection *sym_sec;
8113 struct elf_link_hash_entry *h;
51aecdc5 8114 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 8115 Elf_Internal_Sym *sym;
51aecdc5
AM
8116 long opd_ent_size;
8117 Elf_Internal_Rela *next_rel;
8118 bfd_boolean skip;
50bc7936
AM
8119
8120 r_symndx = ELF64_R_SYM (rel->r_info);
8121 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 8122 r_symndx, ibfd))
50bc7936
AM
8123 goto error_ret;
8124
51aecdc5
AM
8125 next_rel = rel + 2;
8126 if (next_rel + 1 == relend
8127 || (next_rel + 2 < relend
8128 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8129 ++next_rel;
8130
8131 /* See if the .opd entry is full 24 byte or
8132 16 byte (with fd_aux entry overlapped with next
8133 fd_func). */
8134 opd_ent_size = 24;
8135 if (next_rel == relend)
1e2f5b6e 8136 {
51aecdc5 8137 if (sec->size == rel->r_offset + 16)
3f764659 8138 opd_ent_size = 16;
51aecdc5
AM
8139 }
8140 else if (next_rel->r_offset == rel->r_offset + 16)
8141 opd_ent_size = 16;
3f764659 8142
51aecdc5
AM
8143 if (h != NULL
8144 && h->root.root.string[0] == '.')
8145 {
8c5b4e52
AM
8146 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8147 if (fdh != NULL)
8148 {
8149 fdh = ppc_follow_link (fdh);
8150 if (fdh->elf.root.type != bfd_link_hash_defined
8151 && fdh->elf.root.type != bfd_link_hash_defweak)
8152 fdh = NULL;
8153 }
51aecdc5 8154 }
1e2f5b6e 8155
51aecdc5
AM
8156 skip = (sym_sec->owner != ibfd
8157 || sym_sec->output_section == bfd_abs_section_ptr);
8158 if (skip)
8159 {
8160 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8161 {
51aecdc5
AM
8162 /* Arrange for the function descriptor sym
8163 to be dropped. */
8164 fdh->elf.root.u.def.value = 0;
8165 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8166 }
51aecdc5 8167 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8168
0e1862bb 8169 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8170 rel = next_rel;
8171 else
8172 while (1)
8173 {
8174 if (!dec_dynrel_count (rel->r_info, sec, info,
8175 NULL, h, sym))
8176 goto error_ret;
754021d0 8177
51aecdc5
AM
8178 if (++rel == next_rel)
8179 break;
1e2f5b6e 8180
51aecdc5
AM
8181 r_symndx = ELF64_R_SYM (rel->r_info);
8182 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8183 r_symndx, ibfd))
8184 goto error_ret;
8185 }
50bc7936
AM
8186 }
8187 else
1e2f5b6e 8188 {
51aecdc5
AM
8189 /* We'll be keeping this opd entry. */
8190 long adjust;
8191
8192 if (fdh != NULL)
8193 {
8194 /* Redefine the function descriptor symbol to
8195 this location in the opd section. It is
8196 necessary to update the value here rather
8197 than using an array of adjustments as we do
8198 for local symbols, because various places
8199 in the generic ELF code use the value
8200 stored in u.def.value. */
8201 fdh->elf.root.u.def.value = wptr - new_contents;
8202 fdh->adjust_done = 1;
8203 }
8204
8205 /* Local syms are a bit tricky. We could
8206 tweak them as they can be cached, but
8207 we'd need to look through the local syms
8208 for the function descriptor sym which we
8209 don't have at the moment. So keep an
8210 array of adjustments. */
8211 adjust = (wptr - new_contents) - (rptr - sec->contents);
8212 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8213
8214 if (wptr != rptr)
8215 memcpy (wptr, rptr, opd_ent_size);
8216 wptr += opd_ent_size;
8217 if (add_aux_fields && opd_ent_size == 16)
8218 {
8219 memset (wptr, '\0', 8);
8220 wptr += 8;
8221 }
8222
50bc7936 8223 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8224 new opd entries. */
8225 for ( ; rel != next_rel; ++rel)
8226 {
8227 rel->r_offset += adjust;
8228 if (write_rel != rel)
8229 memcpy (write_rel, rel, sizeof (*rel));
8230 ++write_rel;
8231 }
1e2f5b6e 8232 }
51aecdc5
AM
8233
8234 rptr += opd_ent_size;
1e2f5b6e
AM
8235 }
8236
3f764659 8237 sec->size = wptr - new_contents;
1e2f5b6e 8238 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8239 if (add_aux_fields)
8240 {
8241 free (sec->contents);
8242 sec->contents = new_contents;
8243 }
8244
05bf9422 8245 /* Fudge the header size too, as this is used later in
cdcf6e38 8246 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8247 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8248 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8249 some_edited = TRUE;
1e2f5b6e 8250 }
6cdc0ccc 8251 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8252 free (relstart);
6cdc0ccc 8253
411e1bfb
AM
8254 if (local_syms != NULL
8255 && symtab_hdr->contents != (unsigned char *) local_syms)
8256 {
8257 if (!info->keep_memory)
8258 free (local_syms);
8259 else
8260 symtab_hdr->contents = (unsigned char *) local_syms;
8261 }
8262 }
8263
754021d0
AM
8264 if (some_edited)
8265 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8266
3f764659
JJ
8267 /* If we are doing a final link and the last .opd entry is just 16 byte
8268 long, add a 8 byte padding after it. */
0e1862bb 8269 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8270 {
8271 bfd_byte *p;
8272
8273 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8274 {
8275 BFD_ASSERT (need_pad->size > 0);
8276
8277 p = bfd_malloc (need_pad->size + 8);
8278 if (p == NULL)
8279 return FALSE;
699733f6 8280
3f764659
JJ
8281 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8282 p, 0, need_pad->size))
8283 return FALSE;
8284
8285 need_pad->contents = p;
8286 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8287 }
8288 else
8289 {
8290 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8291 if (p == NULL)
8292 return FALSE;
8293
8294 need_pad->contents = p;
8295 }
8296
8297 memset (need_pad->contents + need_pad->size, 0, 8);
8298 need_pad->size += 8;
8299 }
8300
411e1bfb
AM
8301 return TRUE;
8302}
8303
3e04d765
AM
8304/* Analyze inline PLT call relocations to see whether calls to locally
8305 defined functions can be converted to direct calls. */
8306
8307bfd_boolean
8308ppc64_elf_inline_plt (struct bfd_link_info *info)
8309{
8310 struct ppc_link_hash_table *htab;
8311 bfd *ibfd;
8312 asection *sec;
8313 bfd_vma low_vma, high_vma, limit;
8314
8315 htab = ppc_hash_table (info);
8316 if (htab == NULL)
8317 return FALSE;
8318
8319 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
8320 reduced somewhat to cater for possible stubs that might be added
8321 between the call and its destination. */
8322 if (htab->params->group_size < 0)
8323 {
8324 limit = -htab->params->group_size;
8325 if (limit == 1)
8326 limit = 0x1e00000;
8327 }
8328 else
8329 {
8330 limit = htab->params->group_size;
8331 if (limit == 1)
8332 limit = 0x1c00000;
8333 }
8334
8335 low_vma = -1;
8336 high_vma = 0;
8337 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
8338 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
8339 {
8340 if (low_vma > sec->vma)
8341 low_vma = sec->vma;
8342 if (high_vma < sec->vma + sec->size)
8343 high_vma = sec->vma + sec->size;
8344 }
8345
8346 /* If a "bl" can reach anywhere in local code sections, then we can
8347 convert all inline PLT sequences to direct calls when the symbol
8348 is local. */
8349 if (high_vma - low_vma < limit)
8350 {
8351 htab->can_convert_all_inline_plt = 1;
8352 return TRUE;
8353 }
8354
8355 /* Otherwise, go looking through relocs for cases where a direct
8356 call won't reach. Mark the symbol on any such reloc to disable
8357 the optimization and keep the PLT entry as it seems likely that
8358 this will be better than creating trampolines. Note that this
8359 will disable the optimization for all inline PLT calls to a
8360 particular symbol, not just those that won't reach. The
8361 difficulty in doing a more precise optimization is that the
8362 linker needs to make a decision depending on whether a
8363 particular R_PPC64_PLTCALL insn can be turned into a direct
8364 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
8365 the sequence, and there is nothing that ties those relocs
8366 together except their symbol. */
8367
8368 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8369 {
8370 Elf_Internal_Shdr *symtab_hdr;
8371 Elf_Internal_Sym *local_syms;
8372
8373 if (!is_ppc64_elf (ibfd))
8374 continue;
8375
8376 local_syms = NULL;
8377 symtab_hdr = &elf_symtab_hdr (ibfd);
8378
8379 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8380 if (ppc64_elf_section_data (sec)->has_pltcall
8381 && !bfd_is_abs_section (sec->output_section))
8382 {
8383 Elf_Internal_Rela *relstart, *rel, *relend;
8384
8385 /* Read the relocations. */
8386 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8387 info->keep_memory);
8388 if (relstart == NULL)
8389 return FALSE;
8390
8391 relend = relstart + sec->reloc_count;
8392 for (rel = relstart; rel < relend; )
8393 {
8394 enum elf_ppc64_reloc_type r_type;
8395 unsigned long r_symndx;
8396 asection *sym_sec;
8397 struct elf_link_hash_entry *h;
8398 Elf_Internal_Sym *sym;
8399 unsigned char *tls_maskp;
8400
8401 r_type = ELF64_R_TYPE (rel->r_info);
8402 if (r_type != R_PPC64_PLTCALL)
8403 continue;
8404
8405 r_symndx = ELF64_R_SYM (rel->r_info);
8406 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
8407 r_symndx, ibfd))
8408 {
8409 if (elf_section_data (sec)->relocs != relstart)
8410 free (relstart);
8411 if (local_syms != NULL
8412 && symtab_hdr->contents != (unsigned char *) local_syms)
8413 free (local_syms);
8414 return FALSE;
8415 }
8416
8417 if (sym_sec != NULL && sym_sec->output_section != NULL)
8418 {
8419 bfd_vma from, to;
8420 if (h != NULL)
8421 to = h->root.u.def.value;
8422 else
8423 to = sym->st_value;
8424 to += (rel->r_addend
8425 + sym_sec->output_offset
8426 + sym_sec->output_section->vma);
8427 from = (rel->r_offset
8428 + sec->output_offset
8429 + sec->output_section->vma);
8430 if (to - from + limit < 2 * limit)
8431 *tls_maskp &= ~PLT_KEEP;
8432 }
8433 }
8434 if (elf_section_data (sec)->relocs != relstart)
8435 free (relstart);
8436 }
8437
8438 if (local_syms != NULL
8439 && symtab_hdr->contents != (unsigned char *) local_syms)
8440 {
8441 if (!info->keep_memory)
8442 free (local_syms);
8443 else
8444 symtab_hdr->contents = (unsigned char *) local_syms;
8445 }
8446 }
8447
8448 return TRUE;
8449}
8450
e1918d23 8451/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8452
e1918d23 8453asection *
e7d1c40c 8454ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8455{
411e1bfb
AM
8456 struct ppc_link_hash_table *htab;
8457
411e1bfb 8458 htab = ppc_hash_table (info);
4dfe6ac6
NC
8459 if (htab == NULL)
8460 return NULL;
8461
ee67d69a
AM
8462 if (abiversion (info->output_bfd) == 1)
8463 htab->opd_abi = 1;
8464
e7d1c40c 8465 if (htab->params->no_multi_toc)
33c0ec9d
AM
8466 htab->do_multi_toc = 0;
8467 else if (!htab->do_multi_toc)
e7d1c40c 8468 htab->params->no_multi_toc = 1;
33c0ec9d 8469
8b5f1ed8
AM
8470 /* Default to --no-plt-localentry, as this option can cause problems
8471 with symbol interposition. For example, glibc libpthread.so and
8472 libc.so duplicate many pthread symbols, with a fallback
8473 implementation in libc.so. In some cases the fallback does more
8474 work than the pthread implementation. __pthread_condattr_destroy
8475 is one such symbol: the libpthread.so implementation is
8476 localentry:0 while the libc.so implementation is localentry:8.
8477 An app that "cleverly" uses dlopen to only load necessary
8478 libraries at runtime may omit loading libpthread.so when not
8479 running multi-threaded, which then results in the libc.so
8480 fallback symbols being used and ld.so complaining. Now there
8481 are workarounds in ld (see non_zero_localentry) to detect the
8482 pthread situation, but that may not be the only case where
8483 --plt-localentry can cause trouble. */
f378ab09 8484 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 8485 htab->params->plt_localentry0 = 0;
d44c746a
AM
8486 if (htab->params->plt_localentry0
8487 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8488 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
8489 _bfd_error_handler
8490 (_("warning: --plt-localentry is especially dangerous without "
8491 "ld.so support to detect ABI violations"));
f378ab09 8492
3a71aa26
AM
8493 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8494 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8495 FALSE, FALSE, TRUE));
a7f2871e
AM
8496 /* Move dynamic linking info to the function descriptor sym. */
8497 if (htab->tls_get_addr != NULL)
8498 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8499 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8500 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8501 FALSE, FALSE, TRUE));
7c9cf415 8502 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8503 {
8504 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8505
8506 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8507 FALSE, FALSE, TRUE);
8508 if (opt != NULL)
8509 func_desc_adjust (opt, info);
8510 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8511 FALSE, FALSE, TRUE);
8512 if (opt_fd != NULL
8513 && (opt_fd->root.type == bfd_link_hash_defined
8514 || opt_fd->root.type == bfd_link_hash_defweak))
8515 {
8516 /* If glibc supports an optimized __tls_get_addr call stub,
8517 signalled by the presence of __tls_get_addr_opt, and we'll
8518 be calling __tls_get_addr via a plt call stub, then
8519 make __tls_get_addr point to __tls_get_addr_opt. */
8520 tga_fd = &htab->tls_get_addr_fd->elf;
8521 if (htab->elf.dynamic_sections_created
8522 && tga_fd != NULL
8523 && (tga_fd->type == STT_FUNC
8524 || tga_fd->needs_plt)
8525 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 8526 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
8527 {
8528 struct plt_entry *ent;
8529
8530 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8531 if (ent->plt.refcount > 0)
8532 break;
8533 if (ent != NULL)
8534 {
8535 tga_fd->root.type = bfd_link_hash_indirect;
8536 tga_fd->root.u.i.link = &opt_fd->root;
8537 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 8538 opt_fd->mark = 1;
a7f2871e
AM
8539 if (opt_fd->dynindx != -1)
8540 {
8541 /* Use __tls_get_addr_opt in dynamic relocations. */
8542 opt_fd->dynindx = -1;
8543 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8544 opt_fd->dynstr_index);
8545 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8546 return NULL;
a7f2871e
AM
8547 }
8548 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8549 tga = &htab->tls_get_addr->elf;
8550 if (opt != NULL && tga != NULL)
8551 {
8552 tga->root.type = bfd_link_hash_indirect;
8553 tga->root.u.i.link = &opt->root;
8554 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 8555 opt->mark = 1;
a7f2871e
AM
8556 _bfd_elf_link_hash_hide_symbol (info, opt,
8557 tga->forced_local);
8558 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8559 }
8560 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8561 htab->tls_get_addr_fd->is_func_descriptor = 1;
8562 if (htab->tls_get_addr != NULL)
8563 {
8564 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8565 htab->tls_get_addr->is_func = 1;
8566 }
8567 }
8568 }
8569 }
7c9cf415
AM
8570 else if (htab->params->tls_get_addr_opt < 0)
8571 htab->params->tls_get_addr_opt = 0;
a7f2871e 8572 }
33c0ec9d 8573 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8574}
8387904d 8575
3a71aa26
AM
8576/* Return TRUE iff REL is a branch reloc with a global symbol matching
8577 HASH1 or HASH2. */
8387904d 8578
3a71aa26
AM
8579static bfd_boolean
8580branch_reloc_hash_match (const bfd *ibfd,
8581 const Elf_Internal_Rela *rel,
8582 const struct ppc_link_hash_entry *hash1,
8583 const struct ppc_link_hash_entry *hash2)
8584{
8585 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8586 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8587 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8588
e054468f 8589 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8590 {
3a71aa26
AM
8591 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8592 struct elf_link_hash_entry *h;
8387904d 8593
3a71aa26 8594 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8595 h = elf_follow_link (h);
3a71aa26
AM
8596 if (h == &hash1->elf || h == &hash2->elf)
8597 return TRUE;
a48ebf4d 8598 }
3a71aa26 8599 return FALSE;
951fd09b 8600}
411e1bfb 8601
951fd09b
AM
8602/* Run through all the TLS relocs looking for optimization
8603 opportunities. The linker has been hacked (see ppc64elf.em) to do
8604 a preliminary section layout so that we know the TLS segment
8605 offsets. We can't optimize earlier because some optimizations need
8606 to know the tp offset, and we need to optimize before allocating
8607 dynamic relocations. */
8608
8609bfd_boolean
33c0ec9d 8610ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8611{
8612 bfd *ibfd;
8613 asection *sec;
8614 struct ppc_link_hash_table *htab;
663a1470 8615 unsigned char *toc_ref;
102890f0 8616 int pass;
951fd09b 8617
3cbc1e5e 8618 if (!bfd_link_executable (info))
411e1bfb
AM
8619 return TRUE;
8620
951fd09b 8621 htab = ppc_hash_table (info);
4dfe6ac6
NC
8622 if (htab == NULL)
8623 return FALSE;
8624
663a1470
AM
8625 /* Make two passes over the relocs. On the first pass, mark toc
8626 entries involved with tls relocs, and check that tls relocs
8627 involved in setting up a tls_get_addr call are indeed followed by
8628 such a call. If they are not, we can't do any tls optimization.
8629 On the second pass twiddle tls_mask flags to notify
8630 relocate_section that optimization can be done, and adjust got
8631 and plt refcounts. */
8632 toc_ref = NULL;
8633 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8634 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8635 {
8636 Elf_Internal_Sym *locsyms = NULL;
8637 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8638
102890f0
AM
8639 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8640 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8641 {
8642 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8643 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8644
102890f0
AM
8645 /* Read the relocations. */
8646 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8647 info->keep_memory);
8648 if (relstart == NULL)
2915c55b
JK
8649 {
8650 free (toc_ref);
8651 return FALSE;
8652 }
411e1bfb 8653
102890f0
AM
8654 relend = relstart + sec->reloc_count;
8655 for (rel = relstart; rel < relend; rel++)
8656 {
8657 enum elf_ppc64_reloc_type r_type;
8658 unsigned long r_symndx;
8659 struct elf_link_hash_entry *h;
8660 Elf_Internal_Sym *sym;
8661 asection *sym_sec;
f961d9dd
AM
8662 unsigned char *tls_mask;
8663 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8664 bfd_vma value;
8665 bfd_boolean ok_tprel, is_local;
8666 long toc_ref_index = 0;
8667 int expecting_tls_get_addr = 0;
663a1470 8668 bfd_boolean ret = FALSE;
411e1bfb 8669
102890f0
AM
8670 r_symndx = ELF64_R_SYM (rel->r_info);
8671 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8672 r_symndx, ibfd))
8673 {
8674 err_free_rel:
8675 if (elf_section_data (sec)->relocs != relstart)
8676 free (relstart);
8677 if (toc_ref != NULL)
8678 free (toc_ref);
8679 if (locsyms != NULL
0ffa91dd 8680 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8681 != (unsigned char *) locsyms))
8682 free (locsyms);
663a1470 8683 return ret;
102890f0 8684 }
411e1bfb 8685
102890f0
AM
8686 if (h != NULL)
8687 {
766bc656
AM
8688 if (h->root.type == bfd_link_hash_defined
8689 || h->root.type == bfd_link_hash_defweak)
8690 value = h->root.u.def.value;
8691 else if (h->root.type == bfd_link_hash_undefweak)
8692 value = 0;
8693 else
663a1470
AM
8694 {
8695 found_tls_get_addr_arg = 0;
8696 continue;
8697 }
102890f0
AM
8698 }
8699 else
8700 /* Symbols referenced by TLS relocs must be of type
8701 STT_TLS. So no need for .opd local sym adjust. */
8702 value = sym->st_value;
8703
8704 ok_tprel = FALSE;
8705 is_local = FALSE;
8706 if (h == NULL
8707 || !h->def_dynamic)
8708 {
8709 is_local = TRUE;
766bc656
AM
8710 if (h != NULL
8711 && h->root.type == bfd_link_hash_undefweak)
8712 ok_tprel = TRUE;
c27b8c2a
AM
8713 else if (sym_sec != NULL
8714 && sym_sec->output_section != NULL)
766bc656
AM
8715 {
8716 value += sym_sec->output_offset;
8717 value += sym_sec->output_section->vma;
8718 value -= htab->elf.tls_sec->vma;
8719 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8720 < (bfd_vma) 1 << 32);
8721 }
102890f0 8722 }
951fd09b 8723
102890f0 8724 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8725 /* If this section has old-style __tls_get_addr calls
8726 without marker relocs, then check that each
8727 __tls_get_addr call reloc is preceded by a reloc
8728 that conceivably belongs to the __tls_get_addr arg
8729 setup insn. If we don't find matching arg setup
8730 relocs, don't do any tls optimization. */
8731 if (pass == 0
8732 && sec->has_tls_get_addr_call
8733 && h != NULL
8734 && (h == &htab->tls_get_addr->elf
8735 || h == &htab->tls_get_addr_fd->elf)
8736 && !found_tls_get_addr_arg
8737 && is_branch_reloc (r_type))
8738 {
25f53a85 8739 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8740 "TLS optimization disabled\n"),
8741 ibfd, sec, rel->r_offset);
8742 ret = TRUE;
8743 goto err_free_rel;
8744 }
8745
8746 found_tls_get_addr_arg = 0;
102890f0
AM
8747 switch (r_type)
8748 {
8749 case R_PPC64_GOT_TLSLD16:
8750 case R_PPC64_GOT_TLSLD16_LO:
8751 expecting_tls_get_addr = 1;
663a1470 8752 found_tls_get_addr_arg = 1;
1a0670f3 8753 /* Fall through. */
102890f0
AM
8754
8755 case R_PPC64_GOT_TLSLD16_HI:
8756 case R_PPC64_GOT_TLSLD16_HA:
8757 /* These relocs should never be against a symbol
8758 defined in a shared lib. Leave them alone if
8759 that turns out to be the case. */
8760 if (!is_local)
8761 continue;
411e1bfb 8762
102890f0 8763 /* LD -> LE */
411e1bfb 8764 tls_set = 0;
102890f0
AM
8765 tls_clear = TLS_LD;
8766 tls_type = TLS_TLS | TLS_LD;
8767 break;
411e1bfb 8768
102890f0
AM
8769 case R_PPC64_GOT_TLSGD16:
8770 case R_PPC64_GOT_TLSGD16_LO:
8771 expecting_tls_get_addr = 1;
663a1470 8772 found_tls_get_addr_arg = 1;
1a0670f3 8773 /* Fall through. */
102890f0
AM
8774
8775 case R_PPC64_GOT_TLSGD16_HI:
8776 case R_PPC64_GOT_TLSGD16_HA:
8777 if (ok_tprel)
8778 /* GD -> LE */
411e1bfb 8779 tls_set = 0;
102890f0
AM
8780 else
8781 /* GD -> IE */
8782 tls_set = TLS_TLS | TLS_TPRELGD;
8783 tls_clear = TLS_GD;
8784 tls_type = TLS_TLS | TLS_GD;
8785 break;
8786
8787 case R_PPC64_GOT_TPREL16_DS:
8788 case R_PPC64_GOT_TPREL16_LO_DS:
8789 case R_PPC64_GOT_TPREL16_HI:
8790 case R_PPC64_GOT_TPREL16_HA:
8791 if (ok_tprel)
8792 {
8793 /* IE -> LE */
8794 tls_set = 0;
8795 tls_clear = TLS_TPREL;
8796 tls_type = TLS_TLS | TLS_TPREL;
8797 break;
8798 }
411e1bfb
AM
8799 continue;
8800
727fc41e
AM
8801 case R_PPC64_TLSGD:
8802 case R_PPC64_TLSLD:
23cedd1d
AM
8803 if (rel + 1 < relend
8804 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8805 {
8806 if (pass != 0
8807 && ELF64_R_TYPE (rel[1].r_info) != R_PPC64_PLTSEQ)
8808 {
8809 r_symndx = ELF64_R_SYM (rel[1].r_info);
8810 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8811 r_symndx, ibfd))
8812 goto err_free_rel;
8813 if (h != NULL)
8814 {
8815 struct plt_entry *ent = NULL;
8816
8817 for (ent = h->plt.plist;
8818 ent != NULL;
8819 ent = ent->next)
8820 if (ent->addend == rel[1].r_addend)
8821 break;
8822
8823 if (ent != NULL
8824 && ent->plt.refcount > 0)
8825 ent->plt.refcount -= 1;
8826 }
8827 }
8828 continue;
8829 }
663a1470 8830 found_tls_get_addr_arg = 1;
1a0670f3 8831 /* Fall through. */
663a1470
AM
8832
8833 case R_PPC64_TLS:
8834 case R_PPC64_TOC16:
8835 case R_PPC64_TOC16_LO:
102890f0
AM
8836 if (sym_sec == NULL || sym_sec != toc)
8837 continue;
8838
8839 /* Mark this toc entry as referenced by a TLS
8840 code sequence. We can do that now in the
8841 case of R_PPC64_TLS, and after checking for
8842 tls_get_addr for the TOC16 relocs. */
8843 if (toc_ref == NULL)
663a1470
AM
8844 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8845 if (toc_ref == NULL)
8846 goto err_free_rel;
8847
102890f0
AM
8848 if (h != NULL)
8849 value = h->root.u.def.value;
8850 else
8851 value = sym->st_value;
8852 value += rel->r_addend;
73242275
AM
8853 if (value % 8 != 0)
8854 continue;
8855 BFD_ASSERT (value < toc->size
8856 && toc->output_offset % 8 == 0);
663a1470 8857 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8858 if (r_type == R_PPC64_TLS
8859 || r_type == R_PPC64_TLSGD
8860 || r_type == R_PPC64_TLSLD)
102890f0
AM
8861 {
8862 toc_ref[toc_ref_index] = 1;
8863 continue;
8864 }
8865
8866 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8867 continue;
8868
8869 tls_set = 0;
8870 tls_clear = 0;
8871 expecting_tls_get_addr = 2;
8872 break;
8873
8874 case R_PPC64_TPREL64:
8875 if (pass == 0
8876 || sec != toc
8877 || toc_ref == NULL
663a1470 8878 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8879 continue;
8880 if (ok_tprel)
8881 {
8882 /* IE -> LE */
8883 tls_set = TLS_EXPLICIT;
8884 tls_clear = TLS_TPREL;
8885 break;
8886 }
8887 continue;
8888
8889 case R_PPC64_DTPMOD64:
8890 if (pass == 0
8891 || sec != toc
8892 || toc_ref == NULL
663a1470 8893 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8894 continue;
8895 if (rel + 1 < relend
8896 && (rel[1].r_info
8897 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8898 && rel[1].r_offset == rel->r_offset + 8)
8899 {
8900 if (ok_tprel)
8901 /* GD -> LE */
8902 tls_set = TLS_EXPLICIT | TLS_GD;
8903 else
8904 /* GD -> IE */
8905 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8906 tls_clear = TLS_GD;
8907 }
8908 else
8909 {
8910 if (!is_local)
8911 continue;
8912
8913 /* LD -> LE */
8914 tls_set = TLS_EXPLICIT;
8915 tls_clear = TLS_LD;
8916 }
8917 break;
8918
8919 default:
8920 continue;
8921 }
8922
8923 if (pass == 0)
8924 {
727fc41e
AM
8925 if (!expecting_tls_get_addr
8926 || !sec->has_tls_get_addr_call)
102890f0
AM
8927 continue;
8928
3a71aa26
AM
8929 if (rel + 1 < relend
8930 && branch_reloc_hash_match (ibfd, rel + 1,
8931 htab->tls_get_addr,
8932 htab->tls_get_addr_fd))
102890f0 8933 {
3a71aa26 8934 if (expecting_tls_get_addr == 2)
102890f0 8935 {
3a71aa26 8936 /* Check for toc tls entries. */
f961d9dd 8937 unsigned char *toc_tls;
3a71aa26
AM
8938 int retval;
8939
8940 retval = get_tls_mask (&toc_tls, NULL, NULL,
8941 &locsyms,
8942 rel, ibfd);
8943 if (retval == 0)
8944 goto err_free_rel;
663a1470
AM
8945 if (toc_tls != NULL)
8946 {
37da22e5
AM
8947 if ((*toc_tls & TLS_TLS) != 0
8948 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8949 found_tls_get_addr_arg = 1;
8950 if (retval > 1)
8951 toc_ref[toc_ref_index] = 1;
8952 }
102890f0 8953 }
3a71aa26 8954 continue;
102890f0
AM
8955 }
8956
102890f0
AM
8957 /* Uh oh, we didn't find the expected call. We
8958 could just mark this symbol to exclude it
8959 from tls optimization but it's safer to skip
663a1470 8960 the entire optimization. */
695344c0 8961 /* xgettext:c-format */
25f53a85 8962 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8963 "TLS optimization disabled\n"),
8964 ibfd, sec, rel->r_offset);
8965 ret = TRUE;
8966 goto err_free_rel;
102890f0
AM
8967 }
8968
37da22e5
AM
8969 /* If we don't have old-style __tls_get_addr calls
8970 without TLSGD/TLSLD marker relocs, and we haven't
8971 found a new-style __tls_get_addr call with a
8972 marker for this symbol, then we either have a
8973 broken object file or an -mlongcall style
8974 indirect call to __tls_get_addr without a marker.
8975 Disable optimization in this case. */
8976 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8977 && (tls_set & TLS_EXPLICIT) == 0
8978 && !sec->has_tls_get_addr_call
8979 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8980 != (TLS_TLS | TLS_MARK)))
8981 continue;
8982
23cedd1d 8983 if (expecting_tls_get_addr)
102890f0 8984 {
23cedd1d
AM
8985 struct plt_entry *ent = NULL;
8986
8987 if (htab->tls_get_addr != NULL)
8988 for (ent = htab->tls_get_addr->elf.plt.plist;
8989 ent != NULL;
8990 ent = ent->next)
8991 if (ent->addend == 0)
102890f0 8992 break;
411e1bfb 8993
23cedd1d
AM
8994 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8995 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8996 ent != NULL;
8997 ent = ent->next)
8998 if (ent->addend == 0)
102890f0 8999 break;
23cedd1d
AM
9000
9001 if (ent != NULL
9002 && ent->plt.refcount > 0)
9003 ent->plt.refcount -= 1;
102890f0 9004 }
411e1bfb 9005
102890f0 9006 if (tls_clear == 0)
30038c59
AM
9007 continue;
9008
102890f0
AM
9009 if ((tls_set & TLS_EXPLICIT) == 0)
9010 {
9011 struct got_entry *ent;
411e1bfb 9012
102890f0
AM
9013 /* Adjust got entry for this reloc. */
9014 if (h != NULL)
9015 ent = h->got.glist;
9016 else
9017 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 9018
102890f0
AM
9019 for (; ent != NULL; ent = ent->next)
9020 if (ent->addend == rel->r_addend
9021 && ent->owner == ibfd
9022 && ent->tls_type == tls_type)
9023 break;
9024 if (ent == NULL)
9025 abort ();
411e1bfb 9026
102890f0
AM
9027 if (tls_set == 0)
9028 {
9029 /* We managed to get rid of a got entry. */
9030 if (ent->got.refcount > 0)
9031 ent->got.refcount -= 1;
9032 }
9033 }
9034 else
9035 {
9036 /* If we got rid of a DTPMOD/DTPREL reloc pair then
9037 we'll lose one or two dyn relocs. */
9038 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 9039 NULL, h, sym))
102890f0 9040 return FALSE;
411e1bfb 9041
102890f0
AM
9042 if (tls_set == (TLS_EXPLICIT | TLS_GD))
9043 {
9044 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 9045 NULL, h, sym))
102890f0
AM
9046 return FALSE;
9047 }
9048 }
411e1bfb 9049
102890f0
AM
9050 *tls_mask |= tls_set;
9051 *tls_mask &= ~tls_clear;
9052 }
8c1d1bb8 9053
102890f0
AM
9054 if (elf_section_data (sec)->relocs != relstart)
9055 free (relstart);
9056 }
411e1bfb 9057
663a1470
AM
9058 if (locsyms != NULL
9059 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
9060 {
9061 if (!info->keep_memory)
9062 free (locsyms);
9063 else
9064 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
9065 }
9066 }
411e1bfb 9067
663a1470
AM
9068 if (toc_ref != NULL)
9069 free (toc_ref);
9a23f96e 9070 htab->do_tls_opt = 1;
b34976b6 9071 return TRUE;
1e2f5b6e 9072}
b34976b6 9073
c5614fa4
AM
9074/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
9075 the values of any global symbols in a toc section that has been
9076 edited. Globals in toc sections should be a rarity, so this function
9077 sets a flag if any are found in toc sections other than the one just
de194d85 9078 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
9079
9080struct adjust_toc_info
9081{
9082 asection *toc;
9083 unsigned long *skip;
9084 bfd_boolean global_toc_syms;
9085};
9086
ba761f19
AM
9087enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
9088
c5614fa4
AM
9089static bfd_boolean
9090adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
9091{
9092 struct ppc_link_hash_entry *eh;
9093 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 9094 unsigned long i;
c5614fa4 9095
c5614fa4
AM
9096 if (h->root.type != bfd_link_hash_defined
9097 && h->root.type != bfd_link_hash_defweak)
9098 return TRUE;
9099
9100 eh = (struct ppc_link_hash_entry *) h;
9101 if (eh->adjust_done)
9102 return TRUE;
9103
9104 if (eh->elf.root.u.def.section == toc_inf->toc)
9105 {
854b41e7
AM
9106 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
9107 i = toc_inf->toc->rawsize >> 3;
c5614fa4 9108 else
854b41e7
AM
9109 i = eh->elf.root.u.def.value >> 3;
9110
ba761f19 9111 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 9112 {
4eca0228 9113 _bfd_error_handler
854b41e7
AM
9114 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
9115 do
9116 ++i;
ba761f19 9117 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 9118 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 9119 }
854b41e7
AM
9120
9121 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
9122 eh->adjust_done = 1;
9123 }
9124 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
9125 toc_inf->global_toc_syms = TRUE;
9126
9127 return TRUE;
9128}
9129
39eeab25
AM
9130/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9131 on a _LO variety toc/got reloc. */
560c8763
AM
9132
9133static bfd_boolean
39eeab25 9134ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 9135{
39eeab25
AM
9136 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9137 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
9138 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9139 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9140 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9141 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9142 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9143 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9144 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9145 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9146 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9147 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9148 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9149 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9150 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
9151 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9152 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9153 /* Exclude lfqu by testing reloc. If relocs are ever
9154 defined for the reduced D field in psq_lu then those
9155 will need testing too. */
9156 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9157 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9158 && (insn & 1) == 0)
9159 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9160 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9161 /* Exclude stfqu. psq_stu as above for psq_lu. */
9162 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9163 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9164 && (insn & 1) == 0));
560c8763
AM
9165}
9166
c5614fa4
AM
9167/* Examine all relocs referencing .toc sections in order to remove
9168 unused .toc entries. */
9169
9170bfd_boolean
33c0ec9d 9171ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
9172{
9173 bfd *ibfd;
9174 struct adjust_toc_info toc_inf;
67f0cbdb 9175 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 9176
67f0cbdb 9177 htab->do_toc_opt = 1;
c5614fa4 9178 toc_inf.global_toc_syms = TRUE;
c72f2fb2 9179 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
9180 {
9181 asection *toc, *sec;
9182 Elf_Internal_Shdr *symtab_hdr;
9183 Elf_Internal_Sym *local_syms;
425b145b 9184 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
9185 unsigned long *skip, *drop;
9186 unsigned char *used;
9187 unsigned char *keep, last, some_unused;
9188
854b41e7
AM
9189 if (!is_ppc64_elf (ibfd))
9190 continue;
9191
c5614fa4
AM
9192 toc = bfd_get_section_by_name (ibfd, ".toc");
9193 if (toc == NULL
92b7a70f 9194 || toc->size == 0
dbaa2011
AM
9195 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9196 || discarded_section (toc))
c5614fa4
AM
9197 continue;
9198
425b145b 9199 toc_relocs = NULL;
c5614fa4 9200 local_syms = NULL;
0ffa91dd 9201 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
9202
9203 /* Look at sections dropped from the final link. */
9204 skip = NULL;
9205 relstart = NULL;
9206 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9207 {
9208 if (sec->reloc_count == 0
dbaa2011 9209 || !discarded_section (sec)
c5614fa4
AM
9210 || get_opd_info (sec)
9211 || (sec->flags & SEC_ALLOC) == 0
9212 || (sec->flags & SEC_DEBUGGING) != 0)
9213 continue;
9214
9215 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9216 if (relstart == NULL)
9217 goto error_ret;
9218
9219 /* Run through the relocs to see which toc entries might be
9220 unused. */
9221 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9222 {
9223 enum elf_ppc64_reloc_type r_type;
9224 unsigned long r_symndx;
9225 asection *sym_sec;
9226 struct elf_link_hash_entry *h;
9227 Elf_Internal_Sym *sym;
9228 bfd_vma val;
9229
9230 r_type = ELF64_R_TYPE (rel->r_info);
9231 switch (r_type)
9232 {
9233 default:
9234 continue;
9235
9236 case R_PPC64_TOC16:
9237 case R_PPC64_TOC16_LO:
9238 case R_PPC64_TOC16_HI:
9239 case R_PPC64_TOC16_HA:
9240 case R_PPC64_TOC16_DS:
9241 case R_PPC64_TOC16_LO_DS:
9242 break;
9243 }
9244
9245 r_symndx = ELF64_R_SYM (rel->r_info);
9246 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9247 r_symndx, ibfd))
9248 goto error_ret;
9249
9250 if (sym_sec != toc)
9251 continue;
9252
9253 if (h != NULL)
9254 val = h->root.u.def.value;
9255 else
9256 val = sym->st_value;
9257 val += rel->r_addend;
9258
9259 if (val >= toc->size)
9260 continue;
9261
9262 /* Anything in the toc ought to be aligned to 8 bytes.
9263 If not, don't mark as unused. */
9264 if (val & 7)
9265 continue;
9266
9267 if (skip == NULL)
9268 {
854b41e7 9269 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9270 if (skip == NULL)
9271 goto error_ret;
9272 }
9273
ba761f19 9274 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9275 }
9276
9277 if (elf_section_data (sec)->relocs != relstart)
9278 free (relstart);
9279 }
9280
ba761f19
AM
9281 /* For largetoc loads of address constants, we can convert
9282 . addis rx,2,addr@got@ha
9283 . ld ry,addr@got@l(rx)
9284 to
9285 . addis rx,2,addr@toc@ha
9286 . addi ry,rx,addr@toc@l
9287 when addr is within 2G of the toc pointer. This then means
9288 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9289
ba761f19
AM
9290 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9291 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9292 && toc->reloc_count != 0)
9293 {
9294 /* Read toc relocs. */
425b145b
AM
9295 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9296 info->keep_memory);
9297 if (toc_relocs == NULL)
ba761f19
AM
9298 goto error_ret;
9299
425b145b 9300 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9301 {
9302 enum elf_ppc64_reloc_type r_type;
9303 unsigned long r_symndx;
9304 asection *sym_sec;
9305 struct elf_link_hash_entry *h;
9306 Elf_Internal_Sym *sym;
9307 bfd_vma val, addr;
9308
9309 r_type = ELF64_R_TYPE (rel->r_info);
9310 if (r_type != R_PPC64_ADDR64)
9311 continue;
9312
9313 r_symndx = ELF64_R_SYM (rel->r_info);
9314 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9315 r_symndx, ibfd))
9316 goto error_ret;
9317
425b145b 9318 if (sym_sec == NULL
c27b8c2a 9319 || sym_sec->output_section == NULL
dbaa2011 9320 || discarded_section (sym_sec))
425b145b
AM
9321 continue;
9322
afe397ea 9323 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9324 continue;
9325
9326 if (h != NULL)
bddc25c9
AM
9327 {
9328 if (h->type == STT_GNU_IFUNC)
9329 continue;
9330 val = h->root.u.def.value;
9331 }
ba761f19 9332 else
bddc25c9
AM
9333 {
9334 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9335 continue;
9336 val = sym->st_value;
9337 }
ba761f19
AM
9338 val += rel->r_addend;
9339 val += sym_sec->output_section->vma + sym_sec->output_offset;
9340
9341 /* We don't yet know the exact toc pointer value, but we
9342 know it will be somewhere in the toc section. Don't
9343 optimize if the difference from any possible toc
9344 pointer is outside [ff..f80008000, 7fff7fff]. */
9345 addr = toc->output_section->vma + TOC_BASE_OFF;
9346 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9347 continue;
9348
9349 addr = toc->output_section->vma + toc->output_section->rawsize;
9350 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9351 continue;
9352
9353 if (skip == NULL)
9354 {
9355 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9356 if (skip == NULL)
9357 goto error_ret;
9358 }
9359
9360 skip[rel->r_offset >> 3]
425b145b 9361 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9362 }
ba761f19
AM
9363 }
9364
c5614fa4
AM
9365 if (skip == NULL)
9366 continue;
9367
9368 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9369 if (used == NULL)
9370 {
9371 error_ret:
9372 if (local_syms != NULL
9373 && symtab_hdr->contents != (unsigned char *) local_syms)
9374 free (local_syms);
9375 if (sec != NULL
9376 && relstart != NULL
9377 && elf_section_data (sec)->relocs != relstart)
9378 free (relstart);
425b145b
AM
9379 if (toc_relocs != NULL
9380 && elf_section_data (toc)->relocs != toc_relocs)
9381 free (toc_relocs);
c5614fa4
AM
9382 if (skip != NULL)
9383 free (skip);
9384 return FALSE;
9385 }
9386
30038c59
AM
9387 /* Now check all kept sections that might reference the toc.
9388 Check the toc itself last. */
9389 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9390 : ibfd->sections);
c5614fa4 9391 sec != NULL;
c5614fa4 9392 sec = (sec == toc ? NULL
c5614fa4 9393 : sec->next == NULL ? toc
30038c59 9394 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9395 : sec->next))
9396 {
9397 int repeat;
9398
9399 if (sec->reloc_count == 0
dbaa2011 9400 || discarded_section (sec)
c5614fa4
AM
9401 || get_opd_info (sec)
9402 || (sec->flags & SEC_ALLOC) == 0
9403 || (sec->flags & SEC_DEBUGGING) != 0)
9404 continue;
9405
854b41e7
AM
9406 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9407 info->keep_memory);
c5614fa4 9408 if (relstart == NULL)
2915c55b
JK
9409 {
9410 free (used);
9411 goto error_ret;
9412 }
c5614fa4
AM
9413
9414 /* Mark toc entries referenced as used. */
c5614fa4 9415 do
d4f1ee75
AM
9416 {
9417 repeat = 0;
9418 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9419 {
9420 enum elf_ppc64_reloc_type r_type;
9421 unsigned long r_symndx;
9422 asection *sym_sec;
9423 struct elf_link_hash_entry *h;
9424 Elf_Internal_Sym *sym;
9425 bfd_vma val;
9426 enum {no_check, check_lo, check_ha} insn_check;
98528052 9427
d4f1ee75
AM
9428 r_type = ELF64_R_TYPE (rel->r_info);
9429 switch (r_type)
9430 {
9431 default:
9432 insn_check = no_check;
9433 break;
98528052 9434
d4f1ee75
AM
9435 case R_PPC64_GOT_TLSLD16_HA:
9436 case R_PPC64_GOT_TLSGD16_HA:
9437 case R_PPC64_GOT_TPREL16_HA:
9438 case R_PPC64_GOT_DTPREL16_HA:
9439 case R_PPC64_GOT16_HA:
9440 case R_PPC64_TOC16_HA:
9441 insn_check = check_ha;
9442 break;
98528052 9443
d4f1ee75
AM
9444 case R_PPC64_GOT_TLSLD16_LO:
9445 case R_PPC64_GOT_TLSGD16_LO:
9446 case R_PPC64_GOT_TPREL16_LO_DS:
9447 case R_PPC64_GOT_DTPREL16_LO_DS:
9448 case R_PPC64_GOT16_LO:
9449 case R_PPC64_GOT16_LO_DS:
9450 case R_PPC64_TOC16_LO:
9451 case R_PPC64_TOC16_LO_DS:
9452 insn_check = check_lo;
9453 break;
9454 }
560c8763 9455
d4f1ee75
AM
9456 if (insn_check != no_check)
9457 {
9458 bfd_vma off = rel->r_offset & ~3;
9459 unsigned char buf[4];
9460 unsigned int insn;
c5614fa4 9461
d4f1ee75
AM
9462 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9463 {
9464 free (used);
9465 goto error_ret;
9466 }
9467 insn = bfd_get_32 (ibfd, buf);
9468 if (insn_check == check_lo
39eeab25 9469 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
9470 : ((insn & ((0x3f << 26) | 0x1f << 16))
9471 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9472 {
9473 char str[12];
9474
9475 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9476 sprintf (str, "%#08x", insn);
9477 info->callbacks->einfo
695344c0 9478 /* xgettext:c-format */
174d0a74 9479 (_("%H: toc optimization is not supported for"
cf97bcb0 9480 " %s instruction\n"),
d4f1ee75
AM
9481 ibfd, sec, rel->r_offset & ~3, str);
9482 }
9483 }
c5614fa4 9484
d4f1ee75
AM
9485 switch (r_type)
9486 {
9487 case R_PPC64_TOC16:
9488 case R_PPC64_TOC16_LO:
9489 case R_PPC64_TOC16_HI:
9490 case R_PPC64_TOC16_HA:
9491 case R_PPC64_TOC16_DS:
9492 case R_PPC64_TOC16_LO_DS:
9493 /* In case we're taking addresses of toc entries. */
9494 case R_PPC64_ADDR64:
9495 break;
c5614fa4 9496
d4f1ee75
AM
9497 default:
9498 continue;
9499 }
c5614fa4 9500
d4f1ee75
AM
9501 r_symndx = ELF64_R_SYM (rel->r_info);
9502 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9503 r_symndx, ibfd))
9504 {
9505 free (used);
9506 goto error_ret;
9507 }
c5614fa4 9508
d4f1ee75
AM
9509 if (sym_sec != toc)
9510 continue;
c5614fa4 9511
d4f1ee75
AM
9512 if (h != NULL)
9513 val = h->root.u.def.value;
9514 else
9515 val = sym->st_value;
9516 val += rel->r_addend;
ba761f19 9517
d4f1ee75
AM
9518 if (val >= toc->size)
9519 continue;
ba761f19 9520
d4f1ee75
AM
9521 if ((skip[val >> 3] & can_optimize) != 0)
9522 {
9523 bfd_vma off;
9524 unsigned char opc;
9525
9526 switch (r_type)
9527 {
9528 case R_PPC64_TOC16_HA:
ba761f19 9529 break;
ba761f19 9530
d4f1ee75
AM
9531 case R_PPC64_TOC16_LO_DS:
9532 off = rel->r_offset;
9533 off += (bfd_big_endian (ibfd) ? -2 : 3);
9534 if (!bfd_get_section_contents (ibfd, sec, &opc,
9535 off, 1))
9536 {
9537 free (used);
9538 goto error_ret;
9539 }
9540 if ((opc & (0x3f << 2)) == (58u << 2))
9541 break;
1a0670f3 9542 /* Fall through. */
ba761f19 9543
d4f1ee75
AM
9544 default:
9545 /* Wrong sort of reloc, or not a ld. We may
9546 as well clear ref_from_discarded too. */
9547 skip[val >> 3] = 0;
9548 }
9549 }
9550
9551 if (sec != toc)
9552 used[val >> 3] = 1;
9553 /* For the toc section, we only mark as used if this
9554 entry itself isn't unused. */
9555 else if ((used[rel->r_offset >> 3]
9556 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9557 && !used[val >> 3])
9558 {
9559 /* Do all the relocs again, to catch reference
9560 chains. */
9561 repeat = 1;
9562 used[val >> 3] = 1;
9563 }
9564 }
9565 }
c5614fa4 9566 while (repeat);
854b41e7
AM
9567
9568 if (elf_section_data (sec)->relocs != relstart)
9569 free (relstart);
c5614fa4
AM
9570 }
9571
9572 /* Merge the used and skip arrays. Assume that TOC
9573 doublewords not appearing as either used or unused belong
de194d85 9574 to an entry more than one doubleword in size. */
c5614fa4
AM
9575 for (drop = skip, keep = used, last = 0, some_unused = 0;
9576 drop < skip + (toc->size + 7) / 8;
9577 ++drop, ++keep)
9578 {
9579 if (*keep)
9580 {
ba761f19
AM
9581 *drop &= ~ref_from_discarded;
9582 if ((*drop & can_optimize) != 0)
9583 some_unused = 1;
c5614fa4
AM
9584 last = 0;
9585 }
b140b010 9586 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9587 {
9588 some_unused = 1;
ba761f19 9589 last = ref_from_discarded;
c5614fa4
AM
9590 }
9591 else
9592 *drop = last;
9593 }
9594
9595 free (used);
9596
9597 if (some_unused)
9598 {
9599 bfd_byte *contents, *src;
9600 unsigned long off;
d62b3684 9601 Elf_Internal_Sym *sym;
ba761f19 9602 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9603
9604 /* Shuffle the toc contents, and at the same time convert the
9605 skip array from booleans into offsets. */
9606 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9607 goto error_ret;
9608
9609 elf_section_data (toc)->this_hdr.contents = contents;
9610
9611 for (src = contents, off = 0, drop = skip;
9612 src < contents + toc->size;
9613 src += 8, ++drop)
9614 {
ba761f19
AM
9615 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9616 off += 8;
c5614fa4
AM
9617 else if (off != 0)
9618 {
9619 *drop = off;
9620 memcpy (src - off, src, 8);
9621 }
9622 }
854b41e7 9623 *drop = off;
c5614fa4
AM
9624 toc->rawsize = toc->size;
9625 toc->size = src - contents - off;
9626
ba761f19
AM
9627 /* Adjust addends for relocs against the toc section sym,
9628 and optimize any accesses we can. */
c5614fa4
AM
9629 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9630 {
9631 if (sec->reloc_count == 0
dbaa2011 9632 || discarded_section (sec))
c5614fa4
AM
9633 continue;
9634
9635 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9636 info->keep_memory);
c5614fa4
AM
9637 if (relstart == NULL)
9638 goto error_ret;
9639
9640 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9641 {
9642 enum elf_ppc64_reloc_type r_type;
9643 unsigned long r_symndx;
9644 asection *sym_sec;
9645 struct elf_link_hash_entry *h;
854b41e7 9646 bfd_vma val;
c5614fa4
AM
9647
9648 r_type = ELF64_R_TYPE (rel->r_info);
9649 switch (r_type)
9650 {
9651 default:
9652 continue;
9653
9654 case R_PPC64_TOC16:
9655 case R_PPC64_TOC16_LO:
9656 case R_PPC64_TOC16_HI:
9657 case R_PPC64_TOC16_HA:
9658 case R_PPC64_TOC16_DS:
9659 case R_PPC64_TOC16_LO_DS:
9660 case R_PPC64_ADDR64:
9661 break;
9662 }
9663
9664 r_symndx = ELF64_R_SYM (rel->r_info);
9665 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9666 r_symndx, ibfd))
9667 goto error_ret;
9668
ba761f19 9669 if (sym_sec != toc)
c5614fa4
AM
9670 continue;
9671
ba761f19
AM
9672 if (h != NULL)
9673 val = h->root.u.def.value;
9674 else
9675 {
9676 val = sym->st_value;
9677 if (val != 0)
9678 local_toc_syms = TRUE;
9679 }
9680
9681 val += rel->r_addend;
854b41e7
AM
9682
9683 if (val > toc->rawsize)
9684 val = toc->rawsize;
ba761f19
AM
9685 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9686 continue;
9687 else if ((skip[val >> 3] & can_optimize) != 0)
9688 {
9689 Elf_Internal_Rela *tocrel
425b145b 9690 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9691 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9692
9693 switch (r_type)
9694 {
9695 case R_PPC64_TOC16_HA:
9696 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9697 break;
9698
9699 case R_PPC64_TOC16_LO_DS:
9700 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9701 break;
9702
9703 default:
28942f62
AM
9704 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9705 ppc_howto_init ();
b140b010 9706 info->callbacks->einfo
695344c0 9707 /* xgettext:c-format */
174d0a74 9708 (_("%H: %s references "
b140b010
AM
9709 "optimized away TOC entry\n"),
9710 ibfd, sec, rel->r_offset,
9711 ppc64_elf_howto_table[r_type]->name);
9712 bfd_set_error (bfd_error_bad_value);
9713 goto error_ret;
ba761f19
AM
9714 }
9715 rel->r_addend = tocrel->r_addend;
9716 elf_section_data (sec)->relocs = relstart;
9717 continue;
9718 }
9719
9720 if (h != NULL || sym->st_value != 0)
9721 continue;
854b41e7
AM
9722
9723 rel->r_addend -= skip[val >> 3];
9724 elf_section_data (sec)->relocs = relstart;
c5614fa4 9725 }
854b41e7
AM
9726
9727 if (elf_section_data (sec)->relocs != relstart)
9728 free (relstart);
c5614fa4
AM
9729 }
9730
9731 /* We shouldn't have local or global symbols defined in the TOC,
9732 but handle them anyway. */
df22d223
AM
9733 if (local_syms != NULL)
9734 for (sym = local_syms;
9735 sym < local_syms + symtab_hdr->sh_info;
9736 ++sym)
9737 if (sym->st_value != 0
9738 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9739 {
9740 unsigned long i;
854b41e7 9741
df22d223
AM
9742 if (sym->st_value > toc->rawsize)
9743 i = toc->rawsize >> 3;
9744 else
9745 i = sym->st_value >> 3;
854b41e7 9746
df22d223
AM
9747 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9748 {
9749 if (local_toc_syms)
4eca0228 9750 _bfd_error_handler
df22d223
AM
9751 (_("%s defined on removed toc entry"),
9752 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9753 do
9754 ++i;
9755 while ((skip[i] & (ref_from_discarded | can_optimize)));
9756 sym->st_value = (bfd_vma) i << 3;
9757 }
d62b3684 9758
df22d223
AM
9759 sym->st_value -= skip[i];
9760 symtab_hdr->contents = (unsigned char *) local_syms;
9761 }
c5614fa4 9762
854b41e7 9763 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9764 if (toc_inf.global_toc_syms)
9765 {
9766 toc_inf.toc = toc;
9767 toc_inf.skip = skip;
9768 toc_inf.global_toc_syms = FALSE;
9769 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9770 &toc_inf);
9771 }
854b41e7
AM
9772
9773 if (toc->reloc_count != 0)
9774 {
d4730f92 9775 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9776 Elf_Internal_Rela *wrel;
9777 bfd_size_type sz;
9778
854b41e7 9779 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9780 if (toc_relocs == NULL)
9781 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9782 info->keep_memory);
9783 if (toc_relocs == NULL)
9784 goto error_ret;
9785
425b145b
AM
9786 wrel = toc_relocs;
9787 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9788 if ((skip[rel->r_offset >> 3]
9789 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9790 {
9791 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9792 wrel->r_info = rel->r_info;
9793 wrel->r_addend = rel->r_addend;
9794 ++wrel;
9795 }
9796 else if (!dec_dynrel_count (rel->r_info, toc, info,
9797 &local_syms, NULL, NULL))
9798 goto error_ret;
9799
425b145b
AM
9800 elf_section_data (toc)->relocs = toc_relocs;
9801 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9802 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9803 sz = rel_hdr->sh_entsize;
9804 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9805 }
c5614fa4 9806 }
28be611c
AM
9807 else if (toc_relocs != NULL
9808 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9809 free (toc_relocs);
c5614fa4
AM
9810
9811 if (local_syms != NULL
9812 && symtab_hdr->contents != (unsigned char *) local_syms)
9813 {
9814 if (!info->keep_memory)
9815 free (local_syms);
9816 else
9817 symtab_hdr->contents = (unsigned char *) local_syms;
9818 }
9819 free (skip);
9820 }
9821
9822 return TRUE;
9823}
9824
1bbe0902
AM
9825/* Return true iff input section I references the TOC using
9826 instructions limited to +/-32k offsets. */
9827
9828bfd_boolean
9829ppc64_elf_has_small_toc_reloc (asection *i)
9830{
9831 return (is_ppc64_elf (i->owner)
9832 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9833}
9834
927be08e
AM
9835/* Allocate space for one GOT entry. */
9836
9837static void
9838allocate_got (struct elf_link_hash_entry *h,
9839 struct bfd_link_info *info,
9840 struct got_entry *gent)
9841{
9842 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9843 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9844 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9845 ? 16 : 8);
9846 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9847 ? 2 : 1) * sizeof (Elf64_External_Rela);
9848 asection *got = ppc64_elf_tdata (gent->owner)->got;
9849
9850 gent->got.offset = got->size;
9851 got->size += entsize;
9852
19e08130 9853 if (h->type == STT_GNU_IFUNC)
927be08e 9854 {
33e44f2e 9855 htab->elf.irelplt->size += rentsize;
19e08130 9856 htab->got_reli_size += rentsize;
927be08e 9857 }
f15d0b54
AM
9858 else if (((bfd_link_pic (info)
9859 && !((gent->tls_type & TLS_TPREL) != 0
9860 && bfd_link_executable (info)
9861 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9862 || (htab->elf.dynamic_sections_created
9863 && h->dynindx != -1
9864 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9865 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9866 {
19e08130 9867 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9868 relgot->size += rentsize;
927be08e
AM
9869 }
9870}
9871
7865406b
AM
9872/* This function merges got entries in the same toc group. */
9873
9874static void
9875merge_got_entries (struct got_entry **pent)
9876{
9877 struct got_entry *ent, *ent2;
9878
9879 for (ent = *pent; ent != NULL; ent = ent->next)
9880 if (!ent->is_indirect)
9881 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9882 if (!ent2->is_indirect
9883 && ent2->addend == ent->addend
9884 && ent2->tls_type == ent->tls_type
9885 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9886 {
9887 ent2->is_indirect = TRUE;
9888 ent2->got.ent = ent;
9889 }
9890}
9891
46434633 9892/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9893
9894static bfd_boolean
46434633
AM
9895ensure_undef_dynamic (struct bfd_link_info *info,
9896 struct elf_link_hash_entry *h)
f0158f44
AM
9897{
9898 struct elf_link_hash_table *htab = elf_hash_table (info);
9899
9900 if (htab->dynamic_sections_created
46434633
AM
9901 && ((info->dynamic_undefined_weak != 0
9902 && h->root.type == bfd_link_hash_undefweak)
9903 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9904 && h->dynindx == -1
9905 && !h->forced_local
9906 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9907 return bfd_elf_link_record_dynamic_symbol (info, h);
9908 return TRUE;
9909}
9910
65f38f15
AM
9911/* Allocate space in .plt, .got and associated reloc sections for
9912 dynamic relocs. */
5bd4f169 9913
b34976b6 9914static bfd_boolean
4ce794b7 9915allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9916{
65f38f15
AM
9917 struct bfd_link_info *info;
9918 struct ppc_link_hash_table *htab;
5bd4f169 9919 asection *s;
65f38f15 9920 struct ppc_link_hash_entry *eh;
0b8bcf0d 9921 struct got_entry **pgent, *gent;
5bd4f169 9922
e92d460e 9923 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9924 return TRUE;
5bd4f169 9925
65f38f15
AM
9926 info = (struct bfd_link_info *) inf;
9927 htab = ppc_hash_table (info);
4dfe6ac6
NC
9928 if (htab == NULL)
9929 return FALSE;
5bd4f169 9930
951fd09b
AM
9931 eh = (struct ppc_link_hash_entry *) h;
9932 /* Run through the TLS GD got entries first if we're changing them
9933 to TPREL. */
37da22e5 9934 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
951fd09b
AM
9935 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9936 if (gent->got.refcount > 0
9937 && (gent->tls_type & TLS_GD) != 0)
9938 {
9939 /* This was a GD entry that has been converted to TPREL. If
9940 there happens to be a TPREL entry we can use that one. */
9941 struct got_entry *ent;
9942 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9943 if (ent->got.refcount > 0
9944 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9945 && ent->addend == gent->addend
9946 && ent->owner == gent->owner)
951fd09b
AM
9947 {
9948 gent->got.refcount = 0;
9949 break;
9950 }
9951
9952 /* If not, then we'll be using our own TPREL entry. */
9953 if (gent->got.refcount != 0)
9954 gent->tls_type = TLS_TLS | TLS_TPREL;
9955 }
9956
7865406b
AM
9957 /* Remove any list entry that won't generate a word in the GOT before
9958 we call merge_got_entries. Otherwise we risk merging to empty
9959 entries. */
0b8bcf0d
AM
9960 pgent = &h->got.glist;
9961 while ((gent = *pgent) != NULL)
411e1bfb 9962 if (gent->got.refcount > 0)
7865406b
AM
9963 {
9964 if ((gent->tls_type & TLS_LD) != 0
9965 && !h->def_dynamic)
9966 {
9967 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9968 *pgent = gent->next;
9969 }
9970 else
9971 pgent = &gent->next;
9972 }
9973 else
9974 *pgent = gent->next;
9975
9976 if (!htab->do_multi_toc)
9977 merge_got_entries (&h->got.glist);
9978
9979 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9980 if (!gent->is_indirect)
411e1bfb 9981 {
46434633
AM
9982 /* Make sure this symbol is output as a dynamic symbol. */
9983 if (!ensure_undef_dynamic (info, h))
f0158f44 9984 return FALSE;
65f38f15 9985
0c8d6e5c 9986 if (!is_ppc64_elf (gent->owner))
927be08e 9987 abort ();
0ffa91dd 9988
927be08e 9989 allocate_got (h, info, gent);
411e1bfb 9990 }
65f38f15 9991
954b63d4
AM
9992 /* If no dynamic sections we can't have dynamic relocs, except for
9993 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9994 if (!htab->elf.dynamic_sections_created
9995 && h->type != STT_GNU_IFUNC)
9996 eh->dyn_relocs = NULL;
9997
529fe20e
AM
9998 /* Discard relocs on undefined symbols that must be local. */
9999 else if (h->root.type == bfd_link_hash_undefined
10000 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
10001 eh->dyn_relocs = NULL;
10002
954b63d4
AM
10003 /* Also discard relocs on undefined weak syms with non-default
10004 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 10005 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
10006 eh->dyn_relocs = NULL;
10007
8a2058b5 10008 if (eh->dyn_relocs != NULL)
65f38f15 10009 {
8a2058b5
AM
10010 struct elf_dyn_relocs *p, **pp;
10011
57e7d118
AM
10012 /* In the shared -Bsymbolic case, discard space allocated for
10013 dynamic pc-relative relocs against symbols which turn out to
10014 be defined in regular objects. For the normal shared case,
10015 discard space for relocs that have become local due to symbol
10016 visibility changes. */
10017
10018 if (bfd_link_pic (info))
65f38f15 10019 {
57e7d118
AM
10020 /* Relocs that use pc_count are those that appear on a call
10021 insn, or certain REL relocs (see must_be_dyn_reloc) that
10022 can be generated via assembly. We want calls to
10023 protected symbols to resolve directly to the function
10024 rather than going via the plt. If people want function
10025 pointer comparisons to work as expected then they should
10026 avoid writing weird assembly. */
10027 if (SYMBOL_CALLS_LOCAL (info, h))
10028 {
57e7d118
AM
10029 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
10030 {
10031 p->count -= p->pc_count;
10032 p->pc_count = 0;
10033 if (p->count == 0)
10034 *pp = p->next;
10035 else
10036 pp = &p->next;
10037 }
10038 }
65f38f15 10039
954b63d4 10040 if (eh->dyn_relocs != NULL)
5bd4f169 10041 {
46434633
AM
10042 /* Make sure this symbol is output as a dynamic symbol. */
10043 if (!ensure_undef_dynamic (info, h))
f0158f44 10044 return FALSE;
5bd4f169 10045 }
65f38f15 10046 }
529fe20e 10047 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 10048 {
8a2058b5 10049 /* For the non-pic case, discard space for relocs against
57e7d118
AM
10050 symbols which turn out to need copy relocs or are not
10051 dynamic. */
529fe20e
AM
10052 if (h->dynamic_adjusted
10053 && !h->def_regular
10054 && !ELF_COMMON_DEF_P (h))
f0158f44 10055 {
46434633
AM
10056 /* Make sure this symbol is output as a dynamic symbol. */
10057 if (!ensure_undef_dynamic (info, h))
f0158f44 10058 return FALSE;
dfbb6ac9 10059
f0158f44
AM
10060 if (h->dynindx == -1)
10061 eh->dyn_relocs = NULL;
10062 }
10063 else
8a2058b5 10064 eh->dyn_relocs = NULL;
57e7d118
AM
10065 }
10066
10067 /* Finally, allocate space. */
10068 for (p = eh->dyn_relocs; p != NULL; p = p->next)
10069 {
10070 asection *sreloc = elf_section_data (p->sec)->sreloc;
10071 if (eh->elf.type == STT_GNU_IFUNC)
10072 sreloc = htab->elf.irelplt;
10073 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 10074 }
65f38f15 10075 }
57e7d118 10076
2d7ad24e
AM
10077 /* We might need a PLT entry when the symbol
10078 a) is dynamic, or
10079 b) is an ifunc, or
10080 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10081 d) has plt16 relocs and we are linking statically. */
10082 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10083 || h->type == STT_GNU_IFUNC
10084 || (h->needs_plt && h->dynamic_adjusted)
10085 || (h->needs_plt
10086 && h->def_regular
10087 && !htab->elf.dynamic_sections_created
3e04d765 10088 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
10089 && (((struct ppc_link_hash_entry *) h)->tls_mask
10090 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 10091 {
57e7d118
AM
10092 struct plt_entry *pent;
10093 bfd_boolean doneone = FALSE;
10094 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10095 if (pent->plt.refcount > 0)
10096 {
10097 if (!htab->elf.dynamic_sections_created
10098 || h->dynindx == -1)
10099 {
2d7ad24e
AM
10100 if (h->type == STT_GNU_IFUNC)
10101 {
10102 s = htab->elf.iplt;
10103 pent->plt.offset = s->size;
10104 s->size += PLT_ENTRY_SIZE (htab);
10105 s = htab->elf.irelplt;
10106 }
10107 else
10108 {
10109 s = htab->pltlocal;
10110 pent->plt.offset = s->size;
10111 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10112 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
10113 }
57e7d118
AM
10114 }
10115 else
10116 {
10117 /* If this is the first .plt entry, make room for the special
10118 first entry. */
10119 s = htab->elf.splt;
10120 if (s->size == 0)
10121 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 10122
57e7d118 10123 pent->plt.offset = s->size;
65f38f15 10124
57e7d118
AM
10125 /* Make room for this entry. */
10126 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 10127
57e7d118
AM
10128 /* Make room for the .glink code. */
10129 s = htab->glink;
10130 if (s->size == 0)
9e390558 10131 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
10132 if (htab->opd_abi)
10133 {
10134 /* We need bigger stubs past index 32767. */
9e390558 10135 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
10136 s->size += 4;
10137 s->size += 2*4;
10138 }
10139 else
10140 s->size += 4;
65f38f15 10141
57e7d118
AM
10142 /* We also need to make an entry in the .rela.plt section. */
10143 s = htab->elf.srelplt;
10144 }
2d7ad24e
AM
10145 if (s != NULL)
10146 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
10147 doneone = TRUE;
10148 }
10149 else
10150 pent->plt.offset = (bfd_vma) -1;
10151 if (!doneone)
10152 {
10153 h->plt.plist = NULL;
10154 h->needs_plt = 0;
10155 }
65f38f15 10156 }
57e7d118 10157 else
65f38f15 10158 {
57e7d118
AM
10159 h->plt.plist = NULL;
10160 h->needs_plt = 0;
65f38f15
AM
10161 }
10162
b34976b6 10163 return TRUE;
65f38f15
AM
10164}
10165
9e390558
AM
10166#define PPC_LO(v) ((v) & 0xffff)
10167#define PPC_HI(v) (((v) >> 16) & 0xffff)
10168#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10169
a345bc8d
AM
10170/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10171 to set up space for global entry stubs. These are put in glink,
10172 after the branch table. */
65f38f15 10173
b34976b6 10174static bfd_boolean
a345bc8d 10175size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 10176{
a345bc8d
AM
10177 struct bfd_link_info *info;
10178 struct ppc_link_hash_table *htab;
10179 struct plt_entry *pent;
9e390558 10180 asection *s, *plt;
65f38f15 10181
a345bc8d
AM
10182 if (h->root.type == bfd_link_hash_indirect)
10183 return TRUE;
65f38f15 10184
a345bc8d
AM
10185 if (!h->pointer_equality_needed)
10186 return TRUE;
65f38f15 10187
a345bc8d
AM
10188 if (h->def_regular)
10189 return TRUE;
65f38f15 10190
a345bc8d
AM
10191 info = inf;
10192 htab = ppc_hash_table (info);
10193 if (htab == NULL)
10194 return FALSE;
10195
9e390558
AM
10196 s = htab->global_entry;
10197 plt = htab->elf.splt;
a345bc8d
AM
10198 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10199 if (pent->plt.offset != (bfd_vma) -1
10200 && pent->addend == 0)
10201 {
afe397ea
AM
10202 /* For ELFv2, if this symbol is not defined in a regular file
10203 and we are not generating a shared library or pie, then we
10204 need to define the symbol in the executable on a call stub.
10205 This is to avoid text relocations. */
9e390558
AM
10206 bfd_vma off, stub_align, stub_off, stub_size;
10207 unsigned int align_power;
10208
10209 stub_size = 16;
10210 stub_off = s->size;
10211 if (htab->params->plt_stub_align >= 0)
10212 align_power = htab->params->plt_stub_align;
10213 else
10214 align_power = -htab->params->plt_stub_align;
10215 /* Setting section alignment is delayed until we know it is
10216 non-empty. Otherwise the .text output section will be
10217 aligned at least to plt_stub_align even when no global
10218 entry stubs are needed. */
10219 if (s->alignment_power < align_power)
10220 s->alignment_power = align_power;
10221 stub_align = (bfd_vma) 1 << align_power;
10222 if (htab->params->plt_stub_align >= 0
10223 || ((((stub_off + stub_size - 1) & -stub_align)
10224 - (stub_off & -stub_align))
10225 > ((stub_size - 1) & -stub_align)))
10226 stub_off = (stub_off + stub_align - 1) & -stub_align;
10227 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10228 off -= stub_off + s->output_offset + s->output_section->vma;
10229 /* Note that for --plt-stub-align negative we have a possible
10230 dependency between stub offset and size. Break that
10231 dependency by assuming the max stub size when calculating
10232 the stub offset. */
10233 if (PPC_HA (off) == 0)
10234 stub_size -= 4;
8a2058b5 10235 h->root.type = bfd_link_hash_defined;
afe397ea 10236 h->root.u.def.section = s;
9e390558
AM
10237 h->root.u.def.value = stub_off;
10238 s->size = stub_off + stub_size;
a345bc8d
AM
10239 break;
10240 }
10241 return TRUE;
10242}
10243
10244/* Set DF_TEXTREL if we find any dynamic relocs that apply to
10245 read-only sections. */
10246
10247static bfd_boolean
98bbb1b8 10248maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 10249{
98bbb1b8
AM
10250 asection *sec;
10251
a345bc8d
AM
10252 if (h->root.type == bfd_link_hash_indirect)
10253 return TRUE;
10254
98bbb1b8
AM
10255 sec = readonly_dynrelocs (h);
10256 if (sec != NULL)
a345bc8d 10257 {
98bbb1b8
AM
10258 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10259
10260 info->flags |= DF_TEXTREL;
10261 info->callbacks->minfo
c1c8c1ef 10262 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
63c1f59d 10263 sec->owner, h->root.root.string, sec);
a345bc8d
AM
10264
10265 /* Not an error, just cut short the traversal. */
10266 return FALSE;
65f38f15 10267 }
b34976b6 10268 return TRUE;
65f38f15
AM
10269}
10270
10271/* Set the sizes of the dynamic sections. */
10272
b34976b6 10273static bfd_boolean
ee67d69a 10274ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 10275 struct bfd_link_info *info)
65f38f15
AM
10276{
10277 struct ppc_link_hash_table *htab;
10278 bfd *dynobj;
10279 asection *s;
b34976b6 10280 bfd_boolean relocs;
65f38f15 10281 bfd *ibfd;
7865406b 10282 struct got_entry *first_tlsld;
65f38f15
AM
10283
10284 htab = ppc_hash_table (info);
4dfe6ac6
NC
10285 if (htab == NULL)
10286 return FALSE;
10287
65f38f15
AM
10288 dynobj = htab->elf.dynobj;
10289 if (dynobj == NULL)
10290 abort ();
10291
10292 if (htab->elf.dynamic_sections_created)
10293 {
10294 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10295 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10296 {
3d4d4302 10297 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10298 if (s == NULL)
10299 abort ();
eea6121a 10300 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10301 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10302 }
10303 }
10304
10305 /* Set up .got offsets for local syms, and space for local dynamic
10306 relocs. */
c72f2fb2 10307 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10308 {
411e1bfb
AM
10309 struct got_entry **lgot_ents;
10310 struct got_entry **end_lgot_ents;
e054468f
AM
10311 struct plt_entry **local_plt;
10312 struct plt_entry **end_local_plt;
f961d9dd 10313 unsigned char *lgot_masks;
65f38f15
AM
10314 bfd_size_type locsymcount;
10315 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10316
0c8d6e5c 10317 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10318 continue;
10319
10320 for (s = ibfd->sections; s != NULL; s = s->next)
10321 {
19e08130 10322 struct ppc_dyn_relocs *p;
65f38f15 10323
6edfbbad 10324 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10325 {
ec338859
AM
10326 if (!bfd_is_abs_section (p->sec)
10327 && bfd_is_abs_section (p->sec->output_section))
10328 {
10329 /* Input section has been discarded, either because
10330 it is a copy of a linkonce section or due to
10331 linker script /DISCARD/, so we'll be discarding
10332 the relocs too. */
10333 }
248866a8 10334 else if (p->count != 0)
ec338859 10335 {
19e08130
AM
10336 asection *srel = elf_section_data (p->sec)->sreloc;
10337 if (p->ifunc)
33e44f2e 10338 srel = htab->elf.irelplt;
eea6121a 10339 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10340 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10341 info->flags |= DF_TEXTREL;
ec338859 10342 }
65f38f15
AM
10343 }
10344 }
10345
411e1bfb
AM
10346 lgot_ents = elf_local_got_ents (ibfd);
10347 if (!lgot_ents)
65f38f15
AM
10348 continue;
10349
0ffa91dd 10350 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10351 locsymcount = symtab_hdr->sh_info;
411e1bfb 10352 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10353 local_plt = (struct plt_entry **) end_lgot_ents;
10354 end_local_plt = local_plt + locsymcount;
f961d9dd 10355 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10356 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10357 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10358 {
0b8bcf0d 10359 struct got_entry **pent, *ent;
411e1bfb 10360
0b8bcf0d
AM
10361 pent = lgot_ents;
10362 while ((ent = *pent) != NULL)
411e1bfb
AM
10363 if (ent->got.refcount > 0)
10364 {
e7b938ca 10365 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10366 {
927be08e 10367 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10368 *pent = ent->next;
411e1bfb
AM
10369 }
10370 else
10371 {
19e08130
AM
10372 unsigned int ent_size = 8;
10373 unsigned int rel_size = sizeof (Elf64_External_Rela);
10374
eea6121a 10375 ent->got.offset = s->size;
e7b938ca 10376 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10377 {
19e08130
AM
10378 ent_size *= 2;
10379 rel_size *= 2;
10380 }
10381 s->size += ent_size;
37da22e5 10382 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10383 {
33e44f2e 10384 htab->elf.irelplt->size += rel_size;
19e08130
AM
10385 htab->got_reli_size += rel_size;
10386 }
f15d0b54
AM
10387 else if (bfd_link_pic (info)
10388 && !((ent->tls_type & TLS_TPREL) != 0
10389 && bfd_link_executable (info)))
19e08130
AM
10390 {
10391 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10392 srel->size += rel_size;
927be08e 10393 }
0b8bcf0d 10394 pent = &ent->next;
411e1bfb
AM
10395 }
10396 }
10397 else
0b8bcf0d 10398 *pent = ent->next;
65f38f15 10399 }
e054468f 10400
2d7ad24e
AM
10401 /* Allocate space for plt calls to local syms. */
10402 lgot_masks = (unsigned char *) end_local_plt;
10403 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10404 {
10405 struct plt_entry *ent;
10406
10407 for (ent = *local_plt; ent != NULL; ent = ent->next)
10408 if (ent->plt.refcount > 0)
10409 {
2d7ad24e
AM
10410 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10411 {
10412 s = htab->elf.iplt;
10413 ent->plt.offset = s->size;
10414 s->size += PLT_ENTRY_SIZE (htab);
10415 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10416 }
3e04d765
AM
10417 else if (htab->can_convert_all_inline_plt
10418 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10419 ent->plt.offset = (bfd_vma) -1;
10420 else
10421 {
10422 s = htab->pltlocal;
10423 ent->plt.offset = s->size;
10424 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10425 if (bfd_link_pic (info))
10426 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10427 }
e054468f
AM
10428 }
10429 else
10430 ent->plt.offset = (bfd_vma) -1;
10431 }
65f38f15
AM
10432 }
10433
10434 /* Allocate global sym .plt and .got entries, and space for global
10435 sym dynamic relocs. */
4ce794b7 10436 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10437
0e1862bb 10438 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10439 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10440
7865406b 10441 first_tlsld = NULL;
c72f2fb2 10442 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10443 {
7865406b
AM
10444 struct got_entry *ent;
10445
0c8d6e5c 10446 if (!is_ppc64_elf (ibfd))
102890f0
AM
10447 continue;
10448
7865406b
AM
10449 ent = ppc64_tlsld_got (ibfd);
10450 if (ent->got.refcount > 0)
102890f0 10451 {
7865406b 10452 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10453 {
7865406b
AM
10454 ent->is_indirect = TRUE;
10455 ent->got.ent = first_tlsld;
10456 }
10457 else
10458 {
10459 if (first_tlsld == NULL)
10460 first_tlsld = ent;
10461 s = ppc64_elf_tdata (ibfd)->got;
10462 ent->got.offset = s->size;
10463 ent->owner = ibfd;
10464 s->size += 16;
0e1862bb 10465 if (bfd_link_pic (info))
7865406b
AM
10466 {
10467 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10468 srel->size += sizeof (Elf64_External_Rela);
10469 }
102890f0
AM
10470 }
10471 }
10472 else
7865406b 10473 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10474 }
10475
65f38f15
AM
10476 /* We now have determined the sizes of the various dynamic sections.
10477 Allocate memory for them. */
b34976b6 10478 relocs = FALSE;
65f38f15
AM
10479 for (s = dynobj->sections; s != NULL; s = s->next)
10480 {
10481 if ((s->flags & SEC_LINKER_CREATED) == 0)
10482 continue;
10483
4ce794b7 10484 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10485 /* These haven't been allocated yet; don't strip. */
10486 continue;
33e44f2e
AM
10487 else if (s == htab->elf.sgot
10488 || s == htab->elf.splt
10489 || s == htab->elf.iplt
2d7ad24e 10490 || s == htab->pltlocal
c456f082 10491 || s == htab->glink
9e390558 10492 || s == htab->global_entry
5474d94f
AM
10493 || s == htab->elf.sdynbss
10494 || s == htab->elf.sdynrelro)
65f38f15
AM
10495 {
10496 /* Strip this section if we don't need it; see the
10497 comment below. */
5bd4f169 10498 }
58d180e8
AM
10499 else if (s == htab->glink_eh_frame)
10500 {
10501 if (!bfd_is_abs_section (s->output_section))
10502 /* Not sized yet. */
10503 continue;
10504 }
70cc837d 10505 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10506 {
c456f082 10507 if (s->size != 0)
5bd4f169 10508 {
33e44f2e 10509 if (s != htab->elf.srelplt)
b34976b6 10510 relocs = TRUE;
5bd4f169
AM
10511
10512 /* We use the reloc_count field as a counter if we need
10513 to copy relocs into the output file. */
10514 s->reloc_count = 0;
10515 }
10516 }
65f38f15 10517 else
5bd4f169
AM
10518 {
10519 /* It's not one of our sections, so don't allocate space. */
10520 continue;
10521 }
10522
eea6121a 10523 if (s->size == 0)
5bd4f169 10524 {
c456f082
AM
10525 /* If we don't need this section, strip it from the
10526 output file. This is mostly to handle .rela.bss and
10527 .rela.plt. We must create both sections in
10528 create_dynamic_sections, because they must be created
10529 before the linker maps input sections to output
10530 sections. The linker does that before
10531 adjust_dynamic_symbol is called, and it is that
10532 function which decides whether anything needs to go
10533 into these sections. */
8423293d 10534 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10535 continue;
10536 }
10537
06bcf541
AM
10538 if (bfd_is_abs_section (s->output_section))
10539 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10540 s->name);
10541
c456f082 10542 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10543 continue;
10544
65f38f15
AM
10545 /* Allocate memory for the section contents. We use bfd_zalloc
10546 here in case unused entries are not reclaimed before the
10547 section's contents are written out. This should not happen,
411e1bfb
AM
10548 but this way if it does we get a R_PPC64_NONE reloc in .rela
10549 sections instead of garbage.
10550 We also rely on the section contents being zero when writing
5474d94f 10551 the GOT and .dynrelro. */
eea6121a 10552 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10553 if (s->contents == NULL)
b34976b6 10554 return FALSE;
5bd4f169
AM
10555 }
10556
c72f2fb2 10557 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10558 {
0c8d6e5c 10559 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10560 continue;
10561
e717da7e 10562 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10563 if (s != NULL && s != htab->elf.sgot)
e717da7e 10564 {
eea6121a 10565 if (s->size == 0)
8423293d 10566 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10567 else
10568 {
eea6121a 10569 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10570 if (s->contents == NULL)
10571 return FALSE;
10572 }
10573 }
10574 s = ppc64_elf_tdata (ibfd)->relgot;
10575 if (s != NULL)
10576 {
eea6121a 10577 if (s->size == 0)
8423293d 10578 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10579 else
10580 {
eea6121a 10581 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10582 if (s->contents == NULL)
10583 return FALSE;
10584 relocs = TRUE;
10585 s->reloc_count = 0;
10586 }
10587 }
10588 }
10589
e86ce104 10590 if (htab->elf.dynamic_sections_created)
5bd4f169 10591 {
e8910a83
AM
10592 bfd_boolean tls_opt;
10593
5bd4f169
AM
10594 /* Add some entries to the .dynamic section. We fill in the
10595 values later, in ppc64_elf_finish_dynamic_sections, but we
10596 must add the entries now so that we get the correct size for
10597 the .dynamic section. The DT_DEBUG entry is filled in by the
10598 dynamic linker and used by the debugger. */
dc810e39 10599#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10600 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10601
0e1862bb 10602 if (bfd_link_executable (info))
5bd4f169 10603 {
dc810e39 10604 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10605 return FALSE;
5bd4f169
AM
10606 }
10607
33e44f2e 10608 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10609 {
dc810e39
AM
10610 if (!add_dynamic_entry (DT_PLTGOT, 0)
10611 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10612 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10613 || !add_dynamic_entry (DT_JMPREL, 0)
10614 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10615 return FALSE;
5bd4f169
AM
10616 }
10617
ee67d69a 10618 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10619 {
10620 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10621 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10622 return FALSE;
19397422
AM
10623 }
10624
7c9cf415 10625 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10626 && htab->tls_get_addr_fd != NULL
10627 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10628 if (tls_opt || !htab->opd_abi)
10629 {
10630 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10631 return FALSE;
10632 }
a7f2871e 10633
5bd4f169
AM
10634 if (relocs)
10635 {
dc810e39
AM
10636 if (!add_dynamic_entry (DT_RELA, 0)
10637 || !add_dynamic_entry (DT_RELASZ, 0)
10638 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10639 return FALSE;
5bd4f169 10640
65f38f15
AM
10641 /* If any dynamic relocs apply to a read-only section,
10642 then we need a DT_TEXTREL entry. */
248866a8 10643 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10644 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10645
65f38f15 10646 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10647 {
65f38f15 10648 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10649 return FALSE;
5bd4f169 10650 }
5bd4f169 10651 }
5bd4f169 10652 }
65f38f15 10653#undef add_dynamic_entry
5bd4f169 10654
b34976b6 10655 return TRUE;
5bd4f169
AM
10656}
10657
a345bc8d
AM
10658/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10659
10660static bfd_boolean
10661ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10662{
10663 if (h->plt.plist != NULL
10664 && !h->def_regular
10665 && !h->pointer_equality_needed)
10666 return FALSE;
10667
10668 return _bfd_elf_hash_symbol (h);
10669}
10670
721956f4 10671/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10672
4ce794b7
AM
10673static inline enum ppc_stub_type
10674ppc_type_of_stub (asection *input_sec,
10675 const Elf_Internal_Rela *rel,
10676 struct ppc_link_hash_entry **hash,
e054468f 10677 struct plt_entry **plt_ent,
6911b7dc
AM
10678 bfd_vma destination,
10679 unsigned long local_off)
5bd4f169 10680{
721956f4
AM
10681 struct ppc_link_hash_entry *h = *hash;
10682 bfd_vma location;
10683 bfd_vma branch_offset;
10684 bfd_vma max_branch_offset;
4ce794b7 10685 enum elf_ppc64_reloc_type r_type;
5bd4f169 10686
721956f4
AM
10687 if (h != NULL)
10688 {
e054468f 10689 struct plt_entry *ent;
7fe2b9a6 10690 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10691 if (h->oh != NULL
10692 && h->oh->is_func_descriptor)
7b8f6675
AM
10693 {
10694 fdh = ppc_follow_link (h->oh);
10695 *hash = fdh;
10696 }
8387904d 10697
e054468f
AM
10698 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10699 if (ent->addend == rel->r_addend
10700 && ent->plt.offset != (bfd_vma) -1)
10701 {
e054468f
AM
10702 *plt_ent = ent;
10703 return ppc_stub_plt_call;
10704 }
5bd4f169 10705
7fe2b9a6
AM
10706 /* Here, we know we don't have a plt entry. If we don't have a
10707 either a defined function descriptor or a defined entry symbol
10708 in a regular object file, then it is pointless trying to make
10709 any other type of stub. */
854b41e7
AM
10710 if (!is_static_defined (&fdh->elf)
10711 && !is_static_defined (&h->elf))
721956f4 10712 return ppc_stub_none;
5d1634d7 10713 }
e054468f
AM
10714 else if (elf_local_got_ents (input_sec->owner) != NULL)
10715 {
10716 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10717 struct plt_entry **local_plt = (struct plt_entry **)
10718 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10719 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10720
10721 if (local_plt[r_symndx] != NULL)
10722 {
10723 struct plt_entry *ent;
10724
10725 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10726 if (ent->addend == rel->r_addend
10727 && ent->plt.offset != (bfd_vma) -1)
10728 {
10729 *plt_ent = ent;
10730 return ppc_stub_plt_call;
10731 }
10732 }
10733 }
5d1634d7 10734
721956f4
AM
10735 /* Determine where the call point is. */
10736 location = (input_sec->output_offset
10737 + input_sec->output_section->vma
10738 + rel->r_offset);
5d1634d7 10739
721956f4
AM
10740 branch_offset = destination - location;
10741 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10742
721956f4
AM
10743 /* Determine if a long branch stub is needed. */
10744 max_branch_offset = 1 << 25;
23cedd1d
AM
10745 if (r_type == R_PPC64_REL14
10746 || r_type == R_PPC64_REL14_BRTAKEN
10747 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10748 max_branch_offset = 1 << 15;
5d1634d7 10749
6911b7dc 10750 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10751 /* We need a stub. Figure out whether a long_branch or plt_branch
10752 is needed later. */
10753 return ppc_stub_long_branch;
5d1634d7 10754
721956f4 10755 return ppc_stub_none;
5d1634d7
AM
10756}
10757
794e51c0
AM
10758/* With power7 weakly ordered memory model, it is possible for ld.so
10759 to update a plt entry in one thread and have another thread see a
10760 stale zero toc entry. To avoid this we need some sort of acquire
10761 barrier in the call stub. One solution is to make the load of the
10762 toc word seem to appear to depend on the load of the function entry
10763 word. Another solution is to test for r2 being zero, and branch to
10764 the appropriate glink entry if so.
10765
10766 . fake dep barrier compare
71a39c98
AM
10767 . ld 12,xxx(2) ld 12,xxx(2)
10768 . mtctr 12 mtctr 12
10769 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10770 . add 2,2,11 cmpldi 2,0
10771 . ld 2,xxx+8(2) bnectr+
10772 . bctr b <glink_entry>
10773
10774 The solution involving the compare turns out to be faster, so
10775 that's what we use unless the branch won't reach. */
10776
10777#define ALWAYS_USE_FAKE_DEP 0
10778#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10779
794e51c0
AM
10780static inline unsigned int
10781plt_stub_size (struct ppc_link_hash_table *htab,
10782 struct ppc_stub_hash_entry *stub_entry,
10783 bfd_vma off)
10784{
b9e5796b
AM
10785 unsigned size = 12;
10786
10787 if (ALWAYS_EMIT_R2SAVE
10788 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10789 size += 4;
10790 if (PPC_HA (off) != 0)
794e51c0 10791 size += 4;
b9e5796b
AM
10792 if (htab->opd_abi)
10793 {
10794 size += 4;
e7d1c40c 10795 if (htab->params->plt_static_chain)
b9e5796b 10796 size += 4;
bd4d2eaa
AM
10797 if (htab->params->plt_thread_safe
10798 && htab->elf.dynamic_sections_created
10799 && stub_entry->h != NULL
10800 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10801 size += 8;
e7d1c40c 10802 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10803 size += 4;
10804 }
794e51c0
AM
10805 if (stub_entry->h != NULL
10806 && (stub_entry->h == htab->tls_get_addr_fd
10807 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10808 && htab->params->tls_get_addr_opt)
f378ab09
AM
10809 {
10810 size += 7 * 4;
10811 if (ALWAYS_EMIT_R2SAVE
10812 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 10813 size += 6 * 4;
f378ab09 10814 }
794e51c0
AM
10815 return size;
10816}
10817
2420fff6
AM
10818/* Depending on the sign of plt_stub_align:
10819 If positive, return the padding to align to a 2**plt_stub_align
10820 boundary.
10821 If negative, if this stub would cross fewer 2**plt_stub_align
10822 boundaries if we align, then return the padding needed to do so. */
10823
794e51c0
AM
10824static inline unsigned int
10825plt_stub_pad (struct ppc_link_hash_table *htab,
10826 struct ppc_stub_hash_entry *stub_entry,
10827 bfd_vma plt_off)
10828{
2420fff6 10829 int stub_align;
794e51c0 10830 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10831 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10832
2420fff6
AM
10833 if (htab->params->plt_stub_align >= 0)
10834 {
10835 stub_align = 1 << htab->params->plt_stub_align;
10836 if ((stub_off & (stub_align - 1)) != 0)
10837 return stub_align - (stub_off & (stub_align - 1));
10838 return 0;
10839 }
10840
10841 stub_align = 1 << -htab->params->plt_stub_align;
794e51c0 10842 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10843 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10844 return stub_align - (stub_off & (stub_align - 1));
10845 return 0;
10846}
10847
10848/* Build a .plt call stub. */
10849
10850static inline bfd_byte *
10851build_plt_stub (struct ppc_link_hash_table *htab,
10852 struct ppc_stub_hash_entry *stub_entry,
10853 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10854{
e7d1c40c 10855 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10856 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10857 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10858 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10859 && htab->elf.dynamic_sections_created
10860 && stub_entry->h != NULL
10861 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10862 bfd_boolean use_fake_dep = plt_thread_safe;
10863 bfd_vma cmp_branch_off = 0;
10864
10865 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10866 && plt_load_toc
794e51c0 10867 && plt_thread_safe
bd4d2eaa
AM
10868 && !((stub_entry->h == htab->tls_get_addr_fd
10869 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10870 && htab->params->tls_get_addr_opt))
794e51c0
AM
10871 {
10872 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10873 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10874 / PLT_ENTRY_SIZE (htab));
9e390558 10875 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10876 bfd_vma to, from;
10877
68d62958
AM
10878 if (pltindex > 32768)
10879 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10880 to = (glinkoff
10881 + htab->glink->output_offset
10882 + htab->glink->output_section->vma);
6f20ed8a 10883 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10884 + 4 * (ALWAYS_EMIT_R2SAVE
10885 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10886 + 4 * (PPC_HA (offset) != 0)
10887 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10888 != PPC_HA (offset))
10889 + 4 * (plt_static_chain != 0)
10890 + 20
6f20ed8a
AM
10891 + stub_entry->group->stub_sec->output_offset
10892 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10893 cmp_branch_off = to - from;
10894 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10895 }
10896
ac2df442
AM
10897 if (PPC_HA (offset) != 0)
10898 {
176a0d42
AM
10899 if (r != NULL)
10900 {
794e51c0
AM
10901 if (ALWAYS_EMIT_R2SAVE
10902 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10903 r[0].r_offset += 4;
176a0d42 10904 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10905 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10906 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10907 r[1].r_addend = r[0].r_addend;
b9e5796b 10908 if (plt_load_toc)
176a0d42 10909 {
b9e5796b 10910 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10911 {
b9e5796b
AM
10912 r[2].r_offset = r[1].r_offset + 4;
10913 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10914 r[2].r_addend = r[0].r_addend;
10915 }
10916 else
10917 {
10918 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10919 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10920 r[2].r_addend = r[0].r_addend + 8;
10921 if (plt_static_chain)
10922 {
10923 r[3].r_offset = r[2].r_offset + 4;
10924 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10925 r[3].r_addend = r[0].r_addend + 16;
10926 }
c7131b65 10927 }
176a0d42
AM
10928 }
10929 }
794e51c0
AM
10930 if (ALWAYS_EMIT_R2SAVE
10931 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10932 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10933 if (plt_load_toc)
10934 {
10935 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10936 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10937 }
10938 else
10939 {
10940 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10941 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10942 }
b9e5796b
AM
10943 if (plt_load_toc
10944 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10945 {
71a39c98 10946 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10947 offset = 0;
10948 }
71a39c98 10949 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10950 if (plt_load_toc)
794e51c0 10951 {
b9e5796b
AM
10952 if (use_fake_dep)
10953 {
10954 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10955 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10956 }
10957 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10958 if (plt_static_chain)
10959 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10960 }
ac2df442
AM
10961 }
10962 else
10963 {
176a0d42
AM
10964 if (r != NULL)
10965 {
794e51c0
AM
10966 if (ALWAYS_EMIT_R2SAVE
10967 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10968 r[0].r_offset += 4;
176a0d42 10969 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10970 if (plt_load_toc)
176a0d42 10971 {
b9e5796b 10972 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10973 {
b9e5796b
AM
10974 r[1].r_offset = r[0].r_offset + 4;
10975 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10976 r[1].r_addend = r[0].r_addend;
10977 }
10978 else
10979 {
10980 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10981 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10982 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10983 if (plt_static_chain)
10984 {
10985 r[2].r_offset = r[1].r_offset + 4;
10986 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10987 r[2].r_addend = r[0].r_addend + 8;
10988 }
c7131b65 10989 }
176a0d42
AM
10990 }
10991 }
794e51c0
AM
10992 if (ALWAYS_EMIT_R2SAVE
10993 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10994 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10995 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10996 if (plt_load_toc
10997 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10998 {
10999 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11000 offset = 0;
11001 }
71a39c98 11002 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11003 if (plt_load_toc)
794e51c0 11004 {
b9e5796b
AM
11005 if (use_fake_dep)
11006 {
11007 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11008 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11009 }
11010 if (plt_static_chain)
11011 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11012 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11013 }
ac2df442 11014 }
b9e5796b 11015 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11016 {
11017 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11018 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11019 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11020 }
11021 else
407aa07c 11022 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11023 return p;
11024}
11025
a7f2871e
AM
11026/* Build a special .plt call stub for __tls_get_addr. */
11027
11028#define LD_R11_0R3 0xe9630000
11029#define LD_R12_0R3 0xe9830000
11030#define MR_R0_R3 0x7c601b78
11031#define CMPDI_R11_0 0x2c2b0000
11032#define ADD_R3_R12_R13 0x7c6c6a14
11033#define BEQLR 0x4d820020
11034#define MR_R3_R0 0x7c030378
a7f2871e
AM
11035#define STD_R11_0R1 0xf9610000
11036#define BCTRL 0x4e800421
11037#define LD_R11_0R1 0xe9610000
a7f2871e
AM
11038#define MTLR_R11 0x7d6803a6
11039
11040static inline bfd_byte *
794e51c0
AM
11041build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11042 struct ppc_stub_hash_entry *stub_entry,
11043 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 11044{
e7d1c40c 11045 bfd *obfd = htab->params->stub_bfd;
794e51c0 11046
a7f2871e
AM
11047 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
11048 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11049 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11050 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
11051 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11052 bfd_put_32 (obfd, BEQLR, p), p += 4;
11053 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
11054 if (r != NULL)
11055 r[0].r_offset += 7 * 4;
11056 if (!ALWAYS_EMIT_R2SAVE
11057 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
11058 return build_plt_stub (htab, stub_entry, p, offset, r);
11059
a7f2871e 11060 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 11061 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
11062
11063 if (r != NULL)
f378ab09 11064 r[0].r_offset += 2 * 4;
794e51c0 11065 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 11066 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 11067
a078d95a 11068 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 11069 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
11070 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
11071 bfd_put_32 (obfd, BLR, p), p += 4;
11072
11073 return p;
11074}
11075
176a0d42
AM
11076static Elf_Internal_Rela *
11077get_relocs (asection *sec, int count)
11078{
11079 Elf_Internal_Rela *relocs;
11080 struct bfd_elf_section_data *elfsec_data;
11081
11082 elfsec_data = elf_section_data (sec);
11083 relocs = elfsec_data->relocs;
11084 if (relocs == NULL)
11085 {
11086 bfd_size_type relsize;
11087 relsize = sec->reloc_count * sizeof (*relocs);
11088 relocs = bfd_alloc (sec->owner, relsize);
11089 if (relocs == NULL)
11090 return NULL;
11091 elfsec_data->relocs = relocs;
d4730f92
BS
11092 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11093 sizeof (Elf_Internal_Shdr));
11094 if (elfsec_data->rela.hdr == NULL)
11095 return NULL;
11096 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11097 * sizeof (Elf64_External_Rela));
11098 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11099 sec->reloc_count = 0;
11100 }
11101 relocs += sec->reloc_count;
11102 sec->reloc_count += count;
11103 return relocs;
11104}
11105
aa374f67 11106static bfd_vma
25f53a85 11107get_r2off (struct bfd_link_info *info,
aa374f67
AM
11108 struct ppc_stub_hash_entry *stub_entry)
11109{
25f53a85 11110 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11111 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11112
11113 if (r2off == 0)
11114 {
11115 /* Support linking -R objects. Get the toc pointer from the
11116 opd entry. */
11117 char buf[8];
b9e5796b
AM
11118 if (!htab->opd_abi)
11119 return r2off;
aa374f67
AM
11120 asection *opd = stub_entry->h->elf.root.u.def.section;
11121 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11122
11123 if (strcmp (opd->name, ".opd") != 0
11124 || opd->reloc_count != 0)
11125 {
c1c8c1ef 11126 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
25f53a85 11127 stub_entry->h->elf.root.root.string);
aa374f67 11128 bfd_set_error (bfd_error_bad_value);
a7c49797 11129 return (bfd_vma) -1;
aa374f67
AM
11130 }
11131 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11132 return (bfd_vma) -1;
aa374f67 11133 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11134 r2off -= elf_gp (info->output_bfd);
aa374f67 11135 }
6f20ed8a 11136 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11137 return r2off;
11138}
11139
b34976b6 11140static bfd_boolean
4ce794b7 11141ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11142{
721956f4
AM
11143 struct ppc_stub_hash_entry *stub_entry;
11144 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11145 struct bfd_link_info *info;
11146 struct ppc_link_hash_table *htab;
721956f4
AM
11147 bfd_byte *loc;
11148 bfd_byte *p;
ee75fd95 11149 bfd_vma dest, off;
176a0d42 11150 Elf_Internal_Rela *r;
e054468f 11151 asection *plt;
5d1634d7 11152
721956f4
AM
11153 /* Massage our args to the form they really have. */
11154 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11155 info = in_arg;
5d1634d7 11156
5d1634d7 11157 htab = ppc_hash_table (info);
4dfe6ac6
NC
11158 if (htab == NULL)
11159 return FALSE;
5d1634d7 11160
721956f4 11161 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
11162 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11163 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11164
4ce794b7 11165 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11166 switch (stub_entry->stub_type)
5d1634d7 11167 {
721956f4 11168 case ppc_stub_long_branch:
ad8e1ba5 11169 case ppc_stub_long_branch_r2off:
721956f4 11170 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
11171 dest = (stub_entry->target_value
11172 + stub_entry->target_section->output_offset
11173 + stub_entry->target_section->output_section->vma);
11174 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11175 off = dest;
5d1634d7 11176
721956f4
AM
11177 /* And this is where we are coming from. */
11178 off -= (stub_entry->stub_offset
6f20ed8a
AM
11179 + stub_entry->group->stub_sec->output_offset
11180 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 11181
9e390558 11182 p = loc;
ac2df442 11183 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11184 {
25f53a85 11185 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11186
a7c49797 11187 if (r2off == (bfd_vma) -1)
aa374f67
AM
11188 {
11189 htab->stub_error = TRUE;
11190 return FALSE;
11191 }
9e390558
AM
11192 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11193 p += 4;
ac2df442
AM
11194 if (PPC_HA (r2off) != 0)
11195 {
e7d1c40c 11196 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11197 ADDIS_R2_R2 | PPC_HA (r2off), p);
11198 p += 4;
a7c49797
AM
11199 }
11200 if (PPC_LO (r2off) != 0)
11201 {
11202 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11203 ADDI_R2_R2 | PPC_LO (r2off), p);
11204 p += 4;
ac2df442 11205 }
9e390558 11206 off -= p - loc;
ad8e1ba5 11207 }
9e390558
AM
11208 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11209 p += 4;
ad8e1ba5 11210
5c3dead3
AM
11211 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11212 {
cf97bcb0
AM
11213 _bfd_error_handler
11214 (_("long branch stub `%s' offset overflow"),
bc30df16 11215 stub_entry->root.string);
5c3dead3
AM
11216 htab->stub_error = TRUE;
11217 return FALSE;
11218 }
ee75fd95
AM
11219
11220 if (info->emitrelocations)
11221 {
6f20ed8a 11222 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11223 if (r == NULL)
11224 return FALSE;
9e390558 11225 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95
AM
11226 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11227 r->r_addend = dest;
11228 if (stub_entry->h != NULL)
11229 {
11230 struct elf_link_hash_entry **hashes;
11231 unsigned long symndx;
11232 struct ppc_link_hash_entry *h;
11233
e7d1c40c 11234 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
11235 if (hashes == NULL)
11236 {
11237 bfd_size_type hsize;
11238
11239 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 11240 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
11241 if (hashes == NULL)
11242 return FALSE;
e7d1c40c 11243 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
11244 htab->stub_globals = 1;
11245 }
11246 symndx = htab->stub_globals++;
11247 h = stub_entry->h;
11248 hashes[symndx] = &h->elf;
11249 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11250 if (h->oh != NULL && h->oh->is_func)
b31867b6 11251 h = ppc_follow_link (h->oh);
ee75fd95
AM
11252 if (h->elf.root.u.def.section != stub_entry->target_section)
11253 /* H is an opd symbol. The addend must be zero. */
11254 r->r_addend = 0;
11255 else
11256 {
11257 off = (h->elf.root.u.def.value
11258 + h->elf.root.u.def.section->output_offset
11259 + h->elf.root.u.def.section->output_section->vma);
11260 r->r_addend -= off;
11261 }
11262 }
11263 }
721956f4 11264 break;
e86ce104 11265
721956f4 11266 case ppc_stub_plt_branch:
ad8e1ba5 11267 case ppc_stub_plt_branch_r2off:
721956f4
AM
11268 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11269 stub_entry->root.string + 9,
b34976b6 11270 FALSE, FALSE);
721956f4
AM
11271 if (br_entry == NULL)
11272 {
cf97bcb0
AM
11273 _bfd_error_handler (_("can't find branch stub `%s'"),
11274 stub_entry->root.string);
b34976b6
AM
11275 htab->stub_error = TRUE;
11276 return FALSE;
721956f4
AM
11277 }
11278
176a0d42
AM
11279 dest = (stub_entry->target_value
11280 + stub_entry->target_section->output_offset
11281 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
11282 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11283 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11284
176a0d42 11285 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 11286 htab->brlt->contents + br_entry->offset);
721956f4 11287
f94498ff 11288 if (br_entry->iter == htab->stub_iteration)
721956f4 11289 {
f94498ff 11290 br_entry->iter = 0;
84f5d08e 11291
f94498ff 11292 if (htab->relbrlt != NULL)
84f5d08e 11293 {
f94498ff
AM
11294 /* Create a reloc for the branch lookup table entry. */
11295 Elf_Internal_Rela rela;
11296 bfd_byte *rl;
11297
11298 rela.r_offset = (br_entry->offset
11299 + htab->brlt->output_offset
11300 + htab->brlt->output_section->vma);
11301 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 11302 rela.r_addend = dest;
f94498ff
AM
11303
11304 rl = htab->relbrlt->contents;
11305 rl += (htab->relbrlt->reloc_count++
11306 * sizeof (Elf64_External_Rela));
11307 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11308 }
11309 else if (info->emitrelocations)
11310 {
176a0d42
AM
11311 r = get_relocs (htab->brlt, 1);
11312 if (r == NULL)
11313 return FALSE;
11314 /* brlt, being SEC_LINKER_CREATED does not go through the
11315 normal reloc processing. Symbols and offsets are not
11316 translated from input file to output file form, so
11317 set up the offset per the output file. */
f94498ff
AM
11318 r->r_offset = (br_entry->offset
11319 + htab->brlt->output_offset
11320 + htab->brlt->output_section->vma);
11321 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 11322 r->r_addend = dest;
84f5d08e 11323 }
84f5d08e 11324 }
721956f4 11325
176a0d42
AM
11326 dest = (br_entry->offset
11327 + htab->brlt->output_offset
11328 + htab->brlt->output_section->vma);
11329
11330 off = (dest
06bcf541 11331 - elf_gp (info->output_bfd)
6f20ed8a 11332 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11333
ad8e1ba5 11334 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11335 {
25f53a85 11336 info->callbacks->einfo
c1c8c1ef 11337 (_("%P: linkage table error against `%pT'\n"),
721956f4 11338 stub_entry->root.string);
5d1634d7 11339 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11340 htab->stub_error = TRUE;
11341 return FALSE;
5d1634d7 11342 }
41bd81ab 11343
176a0d42
AM
11344 if (info->emitrelocations)
11345 {
6f20ed8a 11346 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11347 if (r == NULL)
11348 return FALSE;
6f20ed8a 11349 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11350 if (bfd_big_endian (info->output_bfd))
11351 r[0].r_offset += 2;
00f412ee 11352 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11353 r[0].r_offset += 4;
11354 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11355 r[0].r_addend = dest;
11356 if (PPC_HA (off) != 0)
11357 {
11358 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11359 r[1].r_offset = r[0].r_offset + 4;
11360 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11361 r[1].r_addend = r[0].r_addend;
11362 }
11363 }
11364
9e390558 11365 p = loc;
00f412ee 11366 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11367 {
176a0d42 11368 if (PPC_HA (off) != 0)
ac2df442 11369 {
e7d1c40c 11370 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11371 ADDIS_R12_R2 | PPC_HA (off), p);
11372 p += 4;
e7d1c40c 11373 bfd_put_32 (htab->params->stub_bfd,
9e390558 11374 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11375 }
11376 else
9e390558
AM
11377 bfd_put_32 (htab->params->stub_bfd,
11378 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11379 }
11380 else
11381 {
25f53a85 11382 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11383
a7c49797 11384 if (r2off == (bfd_vma) -1)
aa374f67
AM
11385 {
11386 htab->stub_error = TRUE;
11387 return FALSE;
11388 }
ad8e1ba5 11389
9e390558
AM
11390 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11391 p += 4;
176a0d42 11392 if (PPC_HA (off) != 0)
ac2df442 11393 {
e7d1c40c 11394 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11395 ADDIS_R12_R2 | PPC_HA (off), p);
11396 p += 4;
e7d1c40c 11397 bfd_put_32 (htab->params->stub_bfd,
9e390558 11398 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11399 }
11400 else
9e390558 11401 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11402
11403 if (PPC_HA (r2off) != 0)
11404 {
9e390558 11405 p += 4;
e7d1c40c 11406 bfd_put_32 (htab->params->stub_bfd,
9e390558 11407 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11408 }
11409 if (PPC_LO (r2off) != 0)
11410 {
9e390558 11411 p += 4;
e7d1c40c 11412 bfd_put_32 (htab->params->stub_bfd,
9e390558 11413 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11414 }
ad8e1ba5 11415 }
9e390558
AM
11416 p += 4;
11417 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11418 p += 4;
407aa07c
AM
11419 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11420 p += 4;
721956f4 11421 break;
5d1634d7 11422
721956f4 11423 case ppc_stub_plt_call:
794e51c0 11424 case ppc_stub_plt_call_r2save:
e054468f 11425 if (stub_entry->h != NULL
b31867b6
AM
11426 && stub_entry->h->is_func_descriptor
11427 && stub_entry->h->oh != NULL)
c862ae31 11428 {
b31867b6
AM
11429 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11430
11431 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11432 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11433 if (fh->elf.root.type == bfd_link_hash_undefined
11434 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11435 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11436 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11437 }
11438
721956f4 11439 /* Now build the stub. */
e054468f 11440 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 11441 if (dest >= (bfd_vma) -2)
721956f4
AM
11442 abort ();
11443
33e44f2e 11444 plt = htab->elf.splt;
25f23106
AM
11445 if (!htab->elf.dynamic_sections_created
11446 || stub_entry->h == NULL
11447 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11448 {
11449 if (stub_entry->symtype == STT_GNU_IFUNC)
11450 plt = htab->elf.iplt;
11451 else
11452 plt = htab->pltlocal;
11453 }
e054468f
AM
11454
11455 dest += plt->output_offset + plt->output_section->vma;
11456
176a0d42 11457 off = (dest
06bcf541 11458 - elf_gp (info->output_bfd)
6f20ed8a 11459 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11460
ad8e1ba5 11461 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11462 {
25f53a85 11463 info->callbacks->einfo
695344c0 11464 /* xgettext:c-format */
c1c8c1ef 11465 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11466 stub_entry->h != NULL
11467 ? stub_entry->h->elf.root.root.string
11468 : "<local sym>");
721956f4 11469 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11470 htab->stub_error = TRUE;
11471 return FALSE;
721956f4
AM
11472 }
11473
e7d1c40c 11474 if (htab->params->plt_stub_align != 0)
794e51c0
AM
11475 {
11476 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11477
6f20ed8a
AM
11478 stub_entry->group->stub_sec->size += pad;
11479 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
11480 loc += pad;
11481 }
11482
176a0d42
AM
11483 r = NULL;
11484 if (info->emitrelocations)
11485 {
6f20ed8a 11486 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11487 ((PPC_HA (off) != 0)
11488 + (htab->opd_abi
e7d1c40c 11489 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11490 && PPC_HA (off + 16) == PPC_HA (off))
11491 : 1)));
176a0d42
AM
11492 if (r == NULL)
11493 return FALSE;
6f20ed8a 11494 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11495 if (bfd_big_endian (info->output_bfd))
11496 r[0].r_offset += 2;
176a0d42
AM
11497 r[0].r_addend = dest;
11498 }
a7f2871e
AM
11499 if (stub_entry->h != NULL
11500 && (stub_entry->h == htab->tls_get_addr_fd
11501 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11502 && htab->params->tls_get_addr_opt)
794e51c0 11503 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11504 else
794e51c0 11505 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11506 break;
11507
a4b6fadd
AM
11508 case ppc_stub_save_res:
11509 return TRUE;
11510
721956f4
AM
11511 default:
11512 BFD_FAIL ();
b34976b6 11513 return FALSE;
721956f4
AM
11514 }
11515
9e390558 11516 stub_entry->group->stub_sec->size += p - loc;
97b639ba 11517
e7d1c40c 11518 if (htab->params->emit_stub_syms)
97b639ba
AM
11519 {
11520 struct elf_link_hash_entry *h;
ee75fd95
AM
11521 size_t len1, len2;
11522 char *name;
11523 const char *const stub_str[] = { "long_branch",
11524 "long_branch_r2off",
11525 "plt_branch",
11526 "plt_branch_r2off",
794e51c0 11527 "plt_call",
ee75fd95
AM
11528 "plt_call" };
11529
11530 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11531 len2 = strlen (stub_entry->root.string);
11532 name = bfd_malloc (len1 + len2 + 2);
11533 if (name == NULL)
11534 return FALSE;
11535 memcpy (name, stub_entry->root.string, 9);
11536 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11537 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11538 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11539 if (h == NULL)
11540 return FALSE;
11541 if (h->root.type == bfd_link_hash_new)
11542 {
11543 h->root.type = bfd_link_hash_defined;
6f20ed8a 11544 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11545 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11546 h->ref_regular = 1;
11547 h->def_regular = 1;
11548 h->ref_regular_nonweak = 1;
11549 h->forced_local = 1;
11550 h->non_elf = 0;
2ec55de3 11551 h->root.linker_def = 1;
97b639ba
AM
11552 }
11553 }
11554
b34976b6 11555 return TRUE;
721956f4
AM
11556}
11557
11558/* As above, but don't actually build the stub. Just bump offset so
11559 we know stub section sizes, and select plt_branch stubs where
11560 long_branch stubs won't do. */
11561
b34976b6 11562static bfd_boolean
4ce794b7 11563ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11564{
11565 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11566 struct bfd_link_info *info;
721956f4
AM
11567 struct ppc_link_hash_table *htab;
11568 bfd_vma off;
11569 int size;
11570
11571 /* Massage our args to the form they really have. */
11572 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11573 info = in_arg;
11574
11575 htab = ppc_hash_table (info);
4dfe6ac6
NC
11576 if (htab == NULL)
11577 return FALSE;
721956f4 11578
a4b6fadd
AM
11579 if (stub_entry->h != NULL
11580 && stub_entry->h->save_res
11581 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11582 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11583 {
11584 /* Don't make stubs to out-of-line register save/restore
11585 functions. Instead, emit copies of the functions. */
11586 stub_entry->group->needs_save_res = 1;
11587 stub_entry->stub_type = ppc_stub_save_res;
11588 return TRUE;
11589 }
11590
794e51c0
AM
11591 if (stub_entry->stub_type == ppc_stub_plt_call
11592 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11593 {
e054468f
AM
11594 asection *plt;
11595 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11596 if (off >= (bfd_vma) -2)
411e1bfb 11597 abort ();
33e44f2e 11598 plt = htab->elf.splt;
25f23106
AM
11599 if (!htab->elf.dynamic_sections_created
11600 || stub_entry->h == NULL
11601 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11602 {
11603 if (stub_entry->symtype == STT_GNU_IFUNC)
11604 plt = htab->elf.iplt;
11605 else
11606 plt = htab->pltlocal;
11607 }
e054468f
AM
11608 off += (plt->output_offset
11609 + plt->output_section->vma
06bcf541 11610 - elf_gp (info->output_bfd)
6f20ed8a 11611 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11612
794e51c0 11613 size = plt_stub_size (htab, stub_entry, off);
d4aaa2a0
AM
11614 if (stub_entry->h != NULL
11615 && (stub_entry->h == htab->tls_get_addr_fd
11616 || stub_entry->h == htab->tls_get_addr)
11617 && htab->params->tls_get_addr_opt
11618 && (ALWAYS_EMIT_R2SAVE
11619 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11620 stub_entry->group->tls_get_addr_opt_bctrl
11621 = stub_entry->group->stub_sec->size + size - 5 * 4;
11622
e7d1c40c 11623 if (htab->params->plt_stub_align)
794e51c0 11624 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11625 if (info->emitrelocations)
11626 {
6f20ed8a 11627 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11628 += ((PPC_HA (off) != 0)
11629 + (htab->opd_abi
e7d1c40c 11630 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11631 && PPC_HA (off + 16) == PPC_HA (off))
11632 : 1));
6f20ed8a 11633 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11634 }
721956f4
AM
11635 }
11636 else
11637 {
ad8e1ba5
AM
11638 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11639 variants. */
ac2df442 11640 bfd_vma r2off = 0;
6911b7dc 11641 bfd_vma local_off = 0;
ac2df442 11642
721956f4
AM
11643 off = (stub_entry->target_value
11644 + stub_entry->target_section->output_offset
11645 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11646 off -= (stub_entry->group->stub_sec->size
11647 + stub_entry->group->stub_sec->output_offset
11648 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11649
ad8e1ba5
AM
11650 /* Reset the stub type from the plt variant in case we now
11651 can reach with a shorter stub. */
11652 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11653 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11654
11655 size = 4;
11656 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11657 {
25f53a85 11658 r2off = get_r2off (info, stub_entry);
a7c49797 11659 if (r2off == (bfd_vma) -1)
aa374f67
AM
11660 {
11661 htab->stub_error = TRUE;
11662 return FALSE;
11663 }
a7c49797 11664 size = 8;
ac2df442 11665 if (PPC_HA (r2off) != 0)
a7c49797
AM
11666 size += 4;
11667 if (PPC_LO (r2off) != 0)
11668 size += 4;
ac2df442 11669 off -= size - 4;
ad8e1ba5
AM
11670 }
11671
6911b7dc
AM
11672 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11673
6a3858a6 11674 /* If the branch offset is too big, use a ppc_stub_plt_branch.
b9e5796b
AM
11675 Do the same for -R objects without function descriptors. */
11676 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11677 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11678 && r2off == 0
11679 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11680 {
11681 struct ppc_branch_hash_entry *br_entry;
11682
11683 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11684 stub_entry->root.string + 9,
b34976b6 11685 TRUE, FALSE);
721956f4
AM
11686 if (br_entry == NULL)
11687 {
cf97bcb0
AM
11688 _bfd_error_handler (_("can't build branch stub `%s'"),
11689 stub_entry->root.string);
b34976b6
AM
11690 htab->stub_error = TRUE;
11691 return FALSE;
721956f4
AM
11692 }
11693
11694 if (br_entry->iter != htab->stub_iteration)
11695 {
11696 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11697 br_entry->offset = htab->brlt->size;
11698 htab->brlt->size += 8;
63bc6f6c 11699
ee75fd95 11700 if (htab->relbrlt != NULL)
eea6121a 11701 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11702 else if (info->emitrelocations)
11703 {
11704 htab->brlt->reloc_count += 1;
11705 htab->brlt->flags |= SEC_RELOC;
11706 }
721956f4 11707 }
ad8e1ba5
AM
11708
11709 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11710 off = (br_entry->offset
11711 + htab->brlt->output_offset
11712 + htab->brlt->output_section->vma
06bcf541 11713 - elf_gp (info->output_bfd)
6f20ed8a 11714 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11715
176a0d42
AM
11716 if (info->emitrelocations)
11717 {
6f20ed8a
AM
11718 stub_entry->group->stub_sec->reloc_count
11719 += 1 + (PPC_HA (off) != 0);
11720 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11721 }
11722
00f412ee 11723 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11724 {
11725 size = 12;
176a0d42 11726 if (PPC_HA (off) != 0)
ac2df442
AM
11727 size = 16;
11728 }
11729 else
11730 {
00f412ee 11731 size = 16;
176a0d42 11732 if (PPC_HA (off) != 0)
ac2df442
AM
11733 size += 4;
11734
11735 if (PPC_HA (r2off) != 0)
11736 size += 4;
00f412ee
AM
11737 if (PPC_LO (r2off) != 0)
11738 size += 4;
ac2df442 11739 }
721956f4 11740 }
84f5d08e
AM
11741 else if (info->emitrelocations)
11742 {
6f20ed8a
AM
11743 stub_entry->group->stub_sec->reloc_count += 1;
11744 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11745 }
721956f4
AM
11746 }
11747
6f20ed8a 11748 stub_entry->group->stub_sec->size += size;
b34976b6 11749 return TRUE;
721956f4
AM
11750}
11751
11752/* Set up various things so that we can make a list of input sections
11753 for each output section included in the link. Returns -1 on error,
cedb70c5 11754 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11755
11756int
e7d1c40c 11757ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11758{
6f20ed8a 11759 unsigned int id;
721956f4
AM
11760 bfd_size_type amt;
11761 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11762
4dfe6ac6
NC
11763 if (htab == NULL)
11764 return -1;
4c52953f 11765
7cf7fcc8 11766 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
11767 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11768 htab->sec_info = bfd_zmalloc (amt);
11769 if (htab->sec_info == NULL)
721956f4
AM
11770 return -1;
11771
3d6f9012
AM
11772 /* Set toc_off for com, und, abs and ind sections. */
11773 for (id = 0; id < 3; id++)
6f20ed8a 11774 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11775
721956f4
AM
11776 return 1;
11777}
11778
927be08e
AM
11779/* Set up for first pass at multitoc partitioning. */
11780
11781void
11782ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11783{
11784 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11785
1c865ab2 11786 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11787 htab->toc_bfd = NULL;
11788 htab->toc_first_sec = NULL;
11789}
11790
e717da7e
AM
11791/* The linker repeatedly calls this function for each TOC input section
11792 and linker generated GOT section. Group input bfds such that the toc
927be08e 11793 within a group is less than 64k in size. */
ad8e1ba5 11794
927be08e 11795bfd_boolean
4ce794b7 11796ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11797{
11798 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11799 bfd_vma addr, off, limit;
ad8e1ba5 11800
4dfe6ac6
NC
11801 if (htab == NULL)
11802 return FALSE;
11803
927be08e 11804 if (!htab->second_toc_pass)
4c52953f 11805 {
927be08e 11806 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11807 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11808
11809 if (new_bfd)
bf102f86
AM
11810 {
11811 htab->toc_bfd = isec->owner;
11812 htab->toc_first_sec = isec;
11813 }
927be08e 11814
bf102f86
AM
11815 addr = isec->output_offset + isec->output_section->vma;
11816 off = addr - htab->toc_curr;
d77c8a4b
AM
11817 limit = 0x80008000;
11818 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11819 limit = 0x10000;
11820 if (off + isec->size > limit)
bf102f86
AM
11821 {
11822 addr = (htab->toc_first_sec->output_offset
11823 + htab->toc_first_sec->output_section->vma);
11824 htab->toc_curr = addr;
a27e685f 11825 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11826 }
99877b66 11827
927be08e
AM
11828 /* toc_curr is the base address of this toc group. Set elf_gp
11829 for the input section to be the offset relative to the
11830 output toc base plus 0x8000. Making the input elf_gp an
11831 offset allows us to move the toc as a whole without
11832 recalculating input elf_gp. */
06bcf541 11833 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11834 off += TOC_BASE_OFF;
11835
11836 /* Die if someone uses a linker script that doesn't keep input
11837 file .toc and .got together. */
a4fd3de5
AM
11838 if (new_bfd
11839 && elf_gp (isec->owner) != 0
927be08e
AM
11840 && elf_gp (isec->owner) != off)
11841 return FALSE;
11842
11843 elf_gp (isec->owner) = off;
11844 return TRUE;
4c52953f 11845 }
927be08e
AM
11846
11847 /* During the second pass toc_first_sec points to the start of
11848 a toc group, and toc_curr is used to track the old elf_gp.
11849 We use toc_bfd to ensure we only look at each bfd once. */
11850 if (htab->toc_bfd == isec->owner)
11851 return TRUE;
11852 htab->toc_bfd = isec->owner;
11853
11854 if (htab->toc_first_sec == NULL
11855 || htab->toc_curr != elf_gp (isec->owner))
11856 {
11857 htab->toc_curr = elf_gp (isec->owner);
11858 htab->toc_first_sec = isec;
11859 }
11860 addr = (htab->toc_first_sec->output_offset
11861 + htab->toc_first_sec->output_section->vma);
06bcf541 11862 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
11863 elf_gp (isec->owner) = off;
11864
11865 return TRUE;
ad8e1ba5
AM
11866}
11867
927be08e
AM
11868/* Called via elf_link_hash_traverse to merge GOT entries for global
11869 symbol H. */
11870
11871static bfd_boolean
11872merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11873{
11874 if (h->root.type == bfd_link_hash_indirect)
11875 return TRUE;
11876
927be08e
AM
11877 merge_got_entries (&h->got.glist);
11878
11879 return TRUE;
11880}
11881
11882/* Called via elf_link_hash_traverse to allocate GOT entries for global
11883 symbol H. */
11884
11885static bfd_boolean
11886reallocate_got (struct elf_link_hash_entry *h, void *inf)
11887{
11888 struct got_entry *gent;
11889
11890 if (h->root.type == bfd_link_hash_indirect)
11891 return TRUE;
11892
927be08e
AM
11893 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11894 if (!gent->is_indirect)
11895 allocate_got (h, (struct bfd_link_info *) inf, gent);
11896 return TRUE;
11897}
11898
11899/* Called on the first multitoc pass after the last call to
11900 ppc64_elf_next_toc_section. This function removes duplicate GOT
11901 entries. */
11902
11903bfd_boolean
11904ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11905{
11906 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11907 struct bfd *ibfd, *ibfd2;
11908 bfd_boolean done_something;
11909
11910 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11911
7865406b
AM
11912 if (!htab->do_multi_toc)
11913 return FALSE;
11914
d0fae19d 11915 /* Merge global sym got entries within a toc group. */
927be08e
AM
11916 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11917
11918 /* And tlsld_got. */
c72f2fb2 11919 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11920 {
11921 struct got_entry *ent, *ent2;
11922
11923 if (!is_ppc64_elf (ibfd))
11924 continue;
11925
11926 ent = ppc64_tlsld_got (ibfd);
11927 if (!ent->is_indirect
11928 && ent->got.offset != (bfd_vma) -1)
11929 {
c72f2fb2 11930 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11931 {
11932 if (!is_ppc64_elf (ibfd2))
11933 continue;
11934
11935 ent2 = ppc64_tlsld_got (ibfd2);
11936 if (!ent2->is_indirect
11937 && ent2->got.offset != (bfd_vma) -1
11938 && elf_gp (ibfd2) == elf_gp (ibfd))
11939 {
11940 ent2->is_indirect = TRUE;
11941 ent2->got.ent = ent;
11942 }
11943 }
11944 }
11945 }
11946
11947 /* Zap sizes of got sections. */
33e44f2e
AM
11948 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11949 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11950 htab->got_reli_size = 0;
11951
c72f2fb2 11952 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11953 {
11954 asection *got, *relgot;
11955
11956 if (!is_ppc64_elf (ibfd))
11957 continue;
11958
11959 got = ppc64_elf_tdata (ibfd)->got;
11960 if (got != NULL)
11961 {
11962 got->rawsize = got->size;
11963 got->size = 0;
11964 relgot = ppc64_elf_tdata (ibfd)->relgot;
11965 relgot->rawsize = relgot->size;
11966 relgot->size = 0;
11967 }
11968 }
11969
11970 /* Now reallocate the got, local syms first. We don't need to
11971 allocate section contents again since we never increase size. */
c72f2fb2 11972 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11973 {
11974 struct got_entry **lgot_ents;
11975 struct got_entry **end_lgot_ents;
11976 struct plt_entry **local_plt;
11977 struct plt_entry **end_local_plt;
f961d9dd 11978 unsigned char *lgot_masks;
927be08e
AM
11979 bfd_size_type locsymcount;
11980 Elf_Internal_Shdr *symtab_hdr;
19e08130 11981 asection *s;
927be08e
AM
11982
11983 if (!is_ppc64_elf (ibfd))
11984 continue;
11985
11986 lgot_ents = elf_local_got_ents (ibfd);
11987 if (!lgot_ents)
11988 continue;
11989
11990 symtab_hdr = &elf_symtab_hdr (ibfd);
11991 locsymcount = symtab_hdr->sh_info;
11992 end_lgot_ents = lgot_ents + locsymcount;
11993 local_plt = (struct plt_entry **) end_lgot_ents;
11994 end_local_plt = local_plt + locsymcount;
f961d9dd 11995 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11996 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11997 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11998 {
11999 struct got_entry *ent;
12000
12001 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12002 {
19e08130
AM
12003 unsigned int ent_size = 8;
12004 unsigned int rel_size = sizeof (Elf64_External_Rela);
12005
d0fae19d
AM
12006 ent->got.offset = s->size;
12007 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12008 {
19e08130
AM
12009 ent_size *= 2;
12010 rel_size *= 2;
12011 }
12012 s->size += ent_size;
37da22e5 12013 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12014 {
33e44f2e 12015 htab->elf.irelplt->size += rel_size;
19e08130
AM
12016 htab->got_reli_size += rel_size;
12017 }
6a3858a6
AM
12018 else if (bfd_link_pic (info)
12019 && !((ent->tls_type & TLS_TPREL) != 0
12020 && bfd_link_executable (info)))
19e08130
AM
12021 {
12022 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12023 srel->size += rel_size;
d0fae19d
AM
12024 }
12025 }
927be08e
AM
12026 }
12027 }
12028
12029 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12030
c72f2fb2 12031 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12032 {
12033 struct got_entry *ent;
12034
12035 if (!is_ppc64_elf (ibfd))
12036 continue;
12037
12038 ent = ppc64_tlsld_got (ibfd);
12039 if (!ent->is_indirect
12040 && ent->got.offset != (bfd_vma) -1)
12041 {
12042 asection *s = ppc64_elf_tdata (ibfd)->got;
12043 ent->got.offset = s->size;
12044 s->size += 16;
0e1862bb 12045 if (bfd_link_pic (info))
927be08e
AM
12046 {
12047 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12048 srel->size += sizeof (Elf64_External_Rela);
12049 }
12050 }
12051 }
12052
33e44f2e 12053 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12054 if (!done_something)
c72f2fb2 12055 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12056 {
12057 asection *got;
12058
12059 if (!is_ppc64_elf (ibfd))
12060 continue;
12061
12062 got = ppc64_elf_tdata (ibfd)->got;
12063 if (got != NULL)
12064 {
12065 done_something = got->rawsize != got->size;
12066 if (done_something)
12067 break;
12068 }
12069 }
12070
12071 if (done_something)
e7d1c40c 12072 (*htab->params->layout_sections_again) ();
927be08e
AM
12073
12074 /* Set up for second pass over toc sections to recalculate elf_gp
12075 on input sections. */
12076 htab->toc_bfd = NULL;
12077 htab->toc_first_sec = NULL;
12078 htab->second_toc_pass = TRUE;
12079 return done_something;
12080}
12081
12082/* Called after second pass of multitoc partitioning. */
12083
12084void
12085ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12086{
12087 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12088
12089 /* After the second pass, toc_curr tracks the TOC offset used
12090 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12091 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12092}
12093
9b5ecbd0
AM
12094/* No toc references were found in ISEC. If the code in ISEC makes no
12095 calls, then there's no need to use toc adjusting stubs when branching
12096 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12097 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12098 needed, and 2 if a cyclical call-graph was found but no other reason
12099 for a stub was detected. If called from the top level, a return of
12100 2 means the same as a return of 0. */
9b5ecbd0
AM
12101
12102static int
4ce794b7 12103toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12104{
9b5ecbd0 12105 int ret;
70cc837d
AM
12106
12107 /* Mark this section as checked. */
12108 isec->call_check_done = 1;
9b5ecbd0 12109
772119ce
AM
12110 /* We know none of our code bearing sections will need toc stubs. */
12111 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12112 return 0;
12113
eea6121a 12114 if (isec->size == 0)
082c50f8
AM
12115 return 0;
12116
4c52953f
AM
12117 if (isec->output_section == NULL)
12118 return 0;
12119
4c52953f 12120 ret = 0;
70cc837d 12121 if (isec->reloc_count != 0)
9b5ecbd0 12122 {
70cc837d
AM
12123 Elf_Internal_Rela *relstart, *rel;
12124 Elf_Internal_Sym *local_syms;
12125 struct ppc_link_hash_table *htab;
2917689a 12126
70cc837d
AM
12127 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12128 info->keep_memory);
12129 if (relstart == NULL)
12130 return -1;
90aecf7a 12131
70cc837d
AM
12132 /* Look for branches to outside of this section. */
12133 local_syms = NULL;
12134 htab = ppc_hash_table (info);
12135 if (htab == NULL)
12136 return -1;
4c52953f 12137
70cc837d 12138 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12139 {
70cc837d
AM
12140 enum elf_ppc64_reloc_type r_type;
12141 unsigned long r_symndx;
12142 struct elf_link_hash_entry *h;
12143 struct ppc_link_hash_entry *eh;
12144 Elf_Internal_Sym *sym;
12145 asection *sym_sec;
12146 struct _opd_sec_data *opd;
12147 bfd_vma sym_value;
12148 bfd_vma dest;
12149
12150 r_type = ELF64_R_TYPE (rel->r_info);
12151 if (r_type != R_PPC64_REL24
12152 && r_type != R_PPC64_REL14
12153 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d
AM
12154 && r_type != R_PPC64_REL14_BRNTAKEN
12155 && r_type != R_PPC64_PLTCALL)
70cc837d 12156 continue;
4c52953f 12157
70cc837d
AM
12158 r_symndx = ELF64_R_SYM (rel->r_info);
12159 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12160 isec->owner))
4c52953f 12161 {
70cc837d
AM
12162 ret = -1;
12163 break;
12164 }
4c52953f 12165
70cc837d
AM
12166 /* Calls to dynamic lib functions go through a plt call stub
12167 that uses r2. */
12168 eh = (struct ppc_link_hash_entry *) h;
12169 if (eh != NULL
12170 && (eh->elf.plt.plist != NULL
12171 || (eh->oh != NULL
12172 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12173 {
12174 ret = 1;
12175 break;
4c52953f
AM
12176 }
12177
70cc837d
AM
12178 if (sym_sec == NULL)
12179 /* Ignore other undefined symbols. */
4c52953f 12180 continue;
4c52953f 12181
70cc837d
AM
12182 /* Assume branches to other sections not included in the
12183 link need stubs too, to cover -R and absolute syms. */
12184 if (sym_sec->output_section == NULL)
12185 {
12186 ret = 1;
12187 break;
12188 }
4c52953f 12189
70cc837d
AM
12190 if (h == NULL)
12191 sym_value = sym->st_value;
12192 else
12193 {
12194 if (h->root.type != bfd_link_hash_defined
12195 && h->root.type != bfd_link_hash_defweak)
12196 abort ();
12197 sym_value = h->root.u.def.value;
12198 }
12199 sym_value += rel->r_addend;
4c52953f 12200
70cc837d
AM
12201 /* If this branch reloc uses an opd sym, find the code section. */
12202 opd = get_opd_info (sym_sec);
12203 if (opd != NULL)
12204 {
12205 if (h == NULL && opd->adjust != NULL)
12206 {
12207 long adjust;
4c52953f 12208
92a9c616 12209 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12210 if (adjust == -1)
12211 /* Assume deleted functions won't ever be called. */
12212 continue;
12213 sym_value += adjust;
12214 }
4c52953f 12215
aef36ac1
AM
12216 dest = opd_entry_value (sym_sec, sym_value,
12217 &sym_sec, NULL, FALSE);
70cc837d
AM
12218 if (dest == (bfd_vma) -1)
12219 continue;
12220 }
12221 else
12222 dest = (sym_value
12223 + sym_sec->output_offset
12224 + sym_sec->output_section->vma);
4c52953f 12225
70cc837d
AM
12226 /* Ignore branch to self. */
12227 if (sym_sec == isec)
12228 continue;
4c52953f 12229
70cc837d
AM
12230 /* If the called function uses the toc, we need a stub. */
12231 if (sym_sec->has_toc_reloc
12232 || sym_sec->makes_toc_func_call)
4c52953f 12233 {
70cc837d 12234 ret = 1;
4c52953f
AM
12235 break;
12236 }
70cc837d
AM
12237
12238 /* Assume any branch that needs a long branch stub might in fact
12239 need a plt_branch stub. A plt_branch stub uses r2. */
12240 else if (dest - (isec->output_offset
12241 + isec->output_section->vma
6911b7dc
AM
12242 + rel->r_offset) + (1 << 25)
12243 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12244 ? h->other
12245 : sym->st_other))
4c52953f 12246 {
70cc837d
AM
12247 ret = 1;
12248 break;
12249 }
12250
12251 /* If calling back to a section in the process of being
12252 tested, we can't say for sure that no toc adjusting stubs
12253 are needed, so don't return zero. */
12254 else if (sym_sec->call_check_in_progress)
12255 ret = 2;
12256
12257 /* Branches to another section that itself doesn't have any TOC
12258 references are OK. Recursively call ourselves to check. */
12259 else if (!sym_sec->call_check_done)
12260 {
12261 int recur;
12262
12263 /* Mark current section as indeterminate, so that other
12264 sections that call back to current won't be marked as
12265 known. */
12266 isec->call_check_in_progress = 1;
12267 recur = toc_adjusting_stub_needed (info, sym_sec);
12268 isec->call_check_in_progress = 0;
12269
4c52953f
AM
12270 if (recur != 0)
12271 {
70cc837d
AM
12272 ret = recur;
12273 if (recur != 2)
12274 break;
4c52953f
AM
12275 }
12276 }
4c52953f 12277 }
70cc837d
AM
12278
12279 if (local_syms != NULL
12280 && (elf_symtab_hdr (isec->owner).contents
12281 != (unsigned char *) local_syms))
12282 free (local_syms);
12283 if (elf_section_data (isec)->relocs != relstart)
12284 free (relstart);
9b5ecbd0
AM
12285 }
12286
70cc837d
AM
12287 if ((ret & 1) == 0
12288 && isec->map_head.s != NULL
12289 && (strcmp (isec->output_section->name, ".init") == 0
12290 || strcmp (isec->output_section->name, ".fini") == 0))
12291 {
12292 if (isec->map_head.s->has_toc_reloc
12293 || isec->map_head.s->makes_toc_func_call)
12294 ret = 1;
12295 else if (!isec->map_head.s->call_check_done)
12296 {
12297 int recur;
12298 isec->call_check_in_progress = 1;
12299 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12300 isec->call_check_in_progress = 0;
12301 if (recur != 0)
12302 ret = recur;
12303 }
12304 }
12305
12306 if (ret == 1)
12307 isec->makes_toc_func_call = 1;
4c52953f 12308
9b5ecbd0
AM
12309 return ret;
12310}
12311
721956f4
AM
12312/* The linker repeatedly calls this function for each input section,
12313 in the order that input sections are linked into output sections.
12314 Build lists of input sections to determine groupings between which
12315 we may insert linker stubs. */
12316
9b5ecbd0 12317bfd_boolean
4ce794b7 12318ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12319{
12320 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12321
4dfe6ac6
NC
12322 if (htab == NULL)
12323 return FALSE;
12324
734b6cf9 12325 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12326 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12327 {
3d6f9012
AM
12328 /* This happens to make the list in reverse order,
12329 which is what we want. */
6f20ed8a
AM
12330 htab->sec_info[isec->id].u.list
12331 = htab->sec_info[isec->output_section->id].u.list;
12332 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12333 }
ad8e1ba5 12334
4c52953f 12335 if (htab->multi_toc_needed)
9b5ecbd0 12336 {
8b974ba3
AM
12337 /* Analyse sections that aren't already flagged as needing a
12338 valid toc pointer. Exclude .fixup for the linux kernel.
12339 .fixup contains branches, but only back to the function that
12340 hit an exception. */
12341 if (!(isec->has_toc_reloc
12342 || (isec->flags & SEC_CODE) == 0
12343 || strcmp (isec->name, ".fixup") == 0
12344 || isec->call_check_done))
12345 {
12346 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12347 return FALSE;
8b974ba3
AM
12348 }
12349 /* Make all sections use the TOC assigned for this object file.
12350 This will be wrong for pasted sections; We fix that in
12351 check_pasted_section(). */
12352 if (elf_gp (isec->owner) != 0)
12353 htab->toc_curr = elf_gp (isec->owner);
12354 }
12355
6f20ed8a 12356 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12357 return TRUE;
721956f4
AM
12358}
12359
70cc837d
AM
12360/* Check that all .init and .fini sections use the same toc, if they
12361 have toc relocs. */
12362
12363static bfd_boolean
12364check_pasted_section (struct bfd_link_info *info, const char *name)
12365{
12366 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12367
12368 if (o != NULL)
12369 {
12370 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12371 bfd_vma toc_off = 0;
12372 asection *i;
12373
12374 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12375 if (i->has_toc_reloc)
12376 {
12377 if (toc_off == 0)
6f20ed8a
AM
12378 toc_off = htab->sec_info[i->id].toc_off;
12379 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12380 return FALSE;
12381 }
6683a28d
AM
12382
12383 if (toc_off == 0)
12384 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12385 if (i->makes_toc_func_call)
12386 {
6f20ed8a 12387 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12388 break;
12389 }
12390
70cc837d
AM
12391 /* Make sure the whole pasted function uses the same toc offset. */
12392 if (toc_off != 0)
12393 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12394 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12395 }
12396 return TRUE;
12397}
12398
12399bfd_boolean
12400ppc64_elf_check_init_fini (struct bfd_link_info *info)
12401{
12402 return (check_pasted_section (info, ".init")
12403 & check_pasted_section (info, ".fini"));
12404}
12405
721956f4
AM
12406/* See whether we can group stub sections together. Grouping stub
12407 sections may result in fewer stubs. More importantly, we need to
12408 put all .init* and .fini* stubs at the beginning of the .init or
12409 .fini output sections respectively, because glibc splits the
12410 _init and _fini functions into multiple parts. Putting a stub in
12411 the middle of a function is not a good idea. */
12412
6f20ed8a
AM
12413static bfd_boolean
12414group_sections (struct bfd_link_info *info,
4ce794b7
AM
12415 bfd_size_type stub_group_size,
12416 bfd_boolean stubs_always_before_branch)
721956f4 12417{
6f20ed8a
AM
12418 struct ppc_link_hash_table *htab;
12419 asection *osec;
7c8fe5c4
AM
12420 bfd_boolean suppress_size_errors;
12421
6f20ed8a
AM
12422 htab = ppc_hash_table (info);
12423 if (htab == NULL)
12424 return FALSE;
12425
7c8fe5c4 12426 suppress_size_errors = FALSE;
7c8fe5c4
AM
12427 if (stub_group_size == 1)
12428 {
12429 /* Default values. */
12430 if (stubs_always_before_branch)
09f92717 12431 stub_group_size = 0x1e00000;
7c8fe5c4 12432 else
09f92717 12433 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12434 suppress_size_errors = TRUE;
12435 }
12436
6f20ed8a 12437 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12438 {
6f20ed8a
AM
12439 asection *tail;
12440
12441 if (osec->id >= htab->sec_info_arr_size)
12442 continue;
12443
12444 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12445 while (tail != NULL)
721956f4 12446 {
734b6cf9
AM
12447 asection *curr;
12448 asection *prev;
12449 bfd_size_type total;
12450 bfd_boolean big_sec;
12451 bfd_vma curr_toc;
6f20ed8a 12452 struct map_stub *group;
09f92717 12453 bfd_size_type group_size;
734b6cf9
AM
12454
12455 curr = tail;
eea6121a 12456 total = tail->size;
09f92717
AM
12457 group_size = (ppc64_elf_section_data (tail) != NULL
12458 && ppc64_elf_section_data (tail)->has_14bit_branch
12459 ? stub_group_size >> 10 : stub_group_size);
12460
12461 big_sec = total > group_size;
7c8fe5c4 12462 if (big_sec && !suppress_size_errors)
695344c0 12463 /* xgettext:c-format */
871b3ab2 12464 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12465 tail->owner, tail);
6f20ed8a 12466 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12467
6f20ed8a 12468 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12469 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12470 < (ppc64_elf_section_data (prev) != NULL
12471 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12472 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12473 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12474 curr = prev;
12475
12476 /* OK, the size from the start of CURR to the end is less
09f92717 12477 than group_size and thus can be handled by one stub
734b6cf9 12478 section. (or the tail section is itself larger than
09f92717
AM
12479 group_size, in which case we may be toast.) We should
12480 really be keeping track of the total size of stubs added
12481 here, as stubs contribute to the final output section
12482 size. That's a little tricky, and this way will only
12483 break if stubs added make the total size more than 2^25,
12484 ie. for the default stub_group_size, if stubs total more
12485 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12486 group = bfd_alloc (curr->owner, sizeof (*group));
12487 if (group == NULL)
12488 return FALSE;
12489 group->link_sec = curr;
12490 group->stub_sec = NULL;
a4b6fadd 12491 group->needs_save_res = 0;
d4aaa2a0 12492 group->tls_get_addr_opt_bctrl = -1u;
a4b6fadd
AM
12493 group->next = htab->group;
12494 htab->group = group;
734b6cf9 12495 do
721956f4 12496 {
6f20ed8a 12497 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12498 /* Set up this stub group. */
6f20ed8a 12499 htab->sec_info[tail->id].u.group = group;
721956f4 12500 }
734b6cf9
AM
12501 while (tail != curr && (tail = prev) != NULL);
12502
09f92717 12503 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12504 bytes before the stub section can be handled by it too.
12505 Don't do this if we have a really large section after the
12506 stubs, as adding more stubs increases the chance that
12507 branches may not reach into the stub section. */
12508 if (!stubs_always_before_branch && !big_sec)
12509 {
12510 total = 0;
12511 while (prev != NULL
12512 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12513 < (ppc64_elf_section_data (prev) != NULL
12514 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12515 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12516 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12517 {
12518 tail = prev;
6f20ed8a
AM
12519 prev = htab->sec_info[tail->id].u.list;
12520 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12521 }
12522 }
12523 tail = prev;
721956f4
AM
12524 }
12525 }
6f20ed8a 12526 return TRUE;
721956f4
AM
12527}
12528
58d180e8
AM
12529static const unsigned char glink_eh_frame_cie[] =
12530{
12531 0, 0, 0, 16, /* length. */
12532 0, 0, 0, 0, /* id. */
12533 1, /* CIE version. */
12534 'z', 'R', 0, /* Augmentation string. */
12535 4, /* Code alignment. */
12536 0x78, /* Data alignment. */
12537 65, /* RA reg. */
12538 1, /* Augmentation size. */
12539 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12540 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12541};
12542
d4aaa2a0
AM
12543static size_t
12544stub_eh_frame_size (struct map_stub *group, size_t align)
12545{
12546 size_t this_size = 17;
12547 if (group->tls_get_addr_opt_bctrl != -1u)
12548 {
12549 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12550 if (to_bctrl < 64)
12551 this_size += 1;
12552 else if (to_bctrl < 256)
12553 this_size += 2;
12554 else if (to_bctrl < 65536)
12555 this_size += 3;
12556 else
12557 this_size += 5;
12558 this_size += 6;
12559 }
12560 this_size = (this_size + align - 1) & -align;
12561 return this_size;
12562}
12563
d969d15f
AM
12564/* Stripping output sections is normally done before dynamic section
12565 symbols have been allocated. This function is called later, and
12566 handles cases like htab->brlt which is mapped to its own output
12567 section. */
12568
12569static void
12570maybe_strip_output (struct bfd_link_info *info, asection *isec)
12571{
12572 if (isec->size == 0
12573 && isec->output_section->size == 0
53d8967a 12574 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12575 && !bfd_section_removed_from_list (info->output_bfd,
12576 isec->output_section)
12577 && elf_section_data (isec->output_section)->dynindx == 0)
12578 {
12579 isec->output_section->flags |= SEC_EXCLUDE;
12580 bfd_section_list_remove (info->output_bfd, isec->output_section);
12581 info->output_bfd->section_count--;
12582 }
12583}
12584
721956f4
AM
12585/* Determine and set the size of the stub section for a final link.
12586
12587 The basic idea here is to examine all the relocations looking for
12588 PC-relative calls to a target that is unreachable with a "bl"
12589 instruction. */
12590
b34976b6 12591bfd_boolean
e7d1c40c 12592ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12593{
12594 bfd_size_type stub_group_size;
b34976b6 12595 bfd_boolean stubs_always_before_branch;
721956f4
AM
12596 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12597
4dfe6ac6
NC
12598 if (htab == NULL)
12599 return FALSE;
12600
0e1862bb 12601 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12602 htab->params->plt_thread_safe = 1;
b9e5796b 12603 if (!htab->opd_abi)
e7d1c40c
AM
12604 htab->params->plt_thread_safe = 0;
12605 else if (htab->params->plt_thread_safe == -1)
794e51c0 12606 {
e2458743 12607 static const char *const thread_starter[] =
794e51c0
AM
12608 {
12609 "pthread_create",
12610 /* libstdc++ */
12611 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12612 /* librt */
12613 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12614 "mq_notify", "create_timer",
12615 /* libanl */
12616 "getaddrinfo_a",
12617 /* libgomp */
2300b5a1 12618 "GOMP_parallel",
794e51c0 12619 "GOMP_parallel_start",
2300b5a1 12620 "GOMP_parallel_loop_static",
794e51c0 12621 "GOMP_parallel_loop_static_start",
2300b5a1 12622 "GOMP_parallel_loop_dynamic",
794e51c0 12623 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12624 "GOMP_parallel_loop_guided",
794e51c0 12625 "GOMP_parallel_loop_guided_start",
2300b5a1 12626 "GOMP_parallel_loop_runtime",
794e51c0 12627 "GOMP_parallel_loop_runtime_start",
2300b5a1 12628 "GOMP_parallel_sections",
68ffbac6 12629 "GOMP_parallel_sections_start",
f9dffbf0
AM
12630 /* libgo */
12631 "__go_go",
794e51c0
AM
12632 };
12633 unsigned i;
12634
a4b6fadd 12635 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12636 {
12637 struct elf_link_hash_entry *h;
12638 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12639 FALSE, FALSE, TRUE);
e7d1c40c
AM
12640 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12641 if (htab->params->plt_thread_safe)
794e51c0
AM
12642 break;
12643 }
12644 }
e7d1c40c
AM
12645 stubs_always_before_branch = htab->params->group_size < 0;
12646 if (htab->params->group_size < 0)
12647 stub_group_size = -htab->params->group_size;
721956f4 12648 else
e7d1c40c 12649 stub_group_size = htab->params->group_size;
721956f4 12650
6f20ed8a
AM
12651 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12652 return FALSE;
721956f4 12653
c9301e31
AM
12654#define STUB_SHRINK_ITER 20
12655 /* Loop until no stubs added. After iteration 20 of this loop we may
12656 exit on a stub section shrinking. This is to break out of a
12657 pathological case where adding stubs on one iteration decreases
12658 section gaps (perhaps due to alignment), which then requires
12659 fewer or smaller stubs on the next iteration. */
12660
721956f4
AM
12661 while (1)
12662 {
12663 bfd *input_bfd;
12664 unsigned int bfd_indx;
a4b6fadd 12665 struct map_stub *group;
721956f4
AM
12666
12667 htab->stub_iteration += 1;
721956f4
AM
12668
12669 for (input_bfd = info->input_bfds, bfd_indx = 0;
12670 input_bfd != NULL;
c72f2fb2 12671 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12672 {
12673 Elf_Internal_Shdr *symtab_hdr;
12674 asection *section;
6cdc0ccc 12675 Elf_Internal_Sym *local_syms = NULL;
721956f4 12676
0c8d6e5c 12677 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12678 continue;
12679
721956f4 12680 /* We'll need the symbol table in a second. */
0ffa91dd 12681 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12682 if (symtab_hdr->sh_info == 0)
12683 continue;
12684
721956f4
AM
12685 /* Walk over each section attached to the input bfd. */
12686 for (section = input_bfd->sections;
12687 section != NULL;
12688 section = section->next)
12689 {
721956f4 12690 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12691
12692 /* If there aren't any relocs, then there's nothing more
12693 to do. */
12694 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12695 || (section->flags & SEC_ALLOC) == 0
12696 || (section->flags & SEC_LOAD) == 0
12697 || (section->flags & SEC_CODE) == 0
721956f4
AM
12698 || section->reloc_count == 0)
12699 continue;
12700
12701 /* If this section is a link-once section that will be
12702 discarded, then don't create any stubs. */
12703 if (section->output_section == NULL
927be08e 12704 || section->output_section->owner != info->output_bfd)
721956f4
AM
12705 continue;
12706
1e2f5b6e
AM
12707 /* Get the relocs. */
12708 internal_relocs
4ce794b7 12709 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12710 info->keep_memory);
721956f4 12711 if (internal_relocs == NULL)
1e2f5b6e 12712 goto error_ret_free_local;
721956f4
AM
12713
12714 /* Now examine each relocation. */
12715 irela = internal_relocs;
12716 irelaend = irela + section->reloc_count;
12717 for (; irela < irelaend; irela++)
12718 {
4ce794b7
AM
12719 enum elf_ppc64_reloc_type r_type;
12720 unsigned int r_indx;
721956f4
AM
12721 enum ppc_stub_type stub_type;
12722 struct ppc_stub_hash_entry *stub_entry;
8387904d 12723 asection *sym_sec, *code_sec;
e054468f 12724 bfd_vma sym_value, code_value;
721956f4 12725 bfd_vma destination;
6911b7dc 12726 unsigned long local_off;
8843416a 12727 bfd_boolean ok_dest;
721956f4 12728 struct ppc_link_hash_entry *hash;
8387904d 12729 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12730 struct elf_link_hash_entry *h;
12731 Elf_Internal_Sym *sym;
721956f4
AM
12732 char *stub_name;
12733 const asection *id_sec;
74f0fb50 12734 struct _opd_sec_data *opd;
e054468f 12735 struct plt_entry *plt_ent;
721956f4
AM
12736
12737 r_type = ELF64_R_TYPE (irela->r_info);
12738 r_indx = ELF64_R_SYM (irela->r_info);
12739
4ce794b7 12740 if (r_type >= R_PPC64_max)
721956f4
AM
12741 {
12742 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12743 goto error_ret_free_internal;
721956f4
AM
12744 }
12745
12746 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12747 if (r_type != R_PPC64_REL24
12748 && r_type != R_PPC64_REL14
12749 && r_type != R_PPC64_REL14_BRTAKEN
12750 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12751 continue;
12752
12753 /* Now determine the call target, its name, value,
12754 section. */
411e1bfb
AM
12755 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12756 r_indx, input_bfd))
12757 goto error_ret_free_internal;
12758 hash = (struct ppc_link_hash_entry *) h;
12759
8843416a 12760 ok_dest = FALSE;
8387904d 12761 fdh = NULL;
7fe2b9a6 12762 sym_value = 0;
411e1bfb 12763 if (hash == NULL)
721956f4 12764 {
411e1bfb 12765 sym_value = sym->st_value;
c27b8c2a
AM
12766 if (sym_sec != NULL
12767 && sym_sec->output_section != NULL)
12768 ok_dest = TRUE;
721956f4 12769 }
7fe2b9a6
AM
12770 else if (hash->elf.root.type == bfd_link_hash_defined
12771 || hash->elf.root.type == bfd_link_hash_defweak)
12772 {
12773 sym_value = hash->elf.root.u.def.value;
12774 if (sym_sec->output_section != NULL)
12775 ok_dest = TRUE;
12776 }
12777 else if (hash->elf.root.type == bfd_link_hash_undefweak
12778 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12779 {
99877b66 12780 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12781 use the func descriptor sym instead if it is
12782 defined. */
ceb1f1ef 12783 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12784 && hash->oh != NULL)
8387904d 12785 {
8c5b4e52 12786 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12787 if (fdh->elf.root.type == bfd_link_hash_defined
12788 || fdh->elf.root.type == bfd_link_hash_defweak)
12789 {
12790 sym_sec = fdh->elf.root.u.def.section;
12791 sym_value = fdh->elf.root.u.def.value;
12792 if (sym_sec->output_section != NULL)
12793 ok_dest = TRUE;
12794 }
99877b66
AM
12795 else
12796 fdh = NULL;
8387904d 12797 }
7fe2b9a6
AM
12798 }
12799 else
12800 {
12801 bfd_set_error (bfd_error_bad_value);
12802 goto error_ret_free_internal;
721956f4
AM
12803 }
12804
8843416a 12805 destination = 0;
6911b7dc 12806 local_off = 0;
8843416a
AM
12807 if (ok_dest)
12808 {
12809 sym_value += irela->r_addend;
12810 destination = (sym_value
12811 + sym_sec->output_offset
12812 + sym_sec->output_section->vma);
6911b7dc
AM
12813 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12814 ? hash->elf.other
12815 : sym->st_other);
8843416a
AM
12816 }
12817
8387904d 12818 code_sec = sym_sec;
e054468f 12819 code_value = sym_value;
74f0fb50
AM
12820 opd = get_opd_info (sym_sec);
12821 if (opd != NULL)
8387904d
AM
12822 {
12823 bfd_vma dest;
12824
74f0fb50 12825 if (hash == NULL && opd->adjust != NULL)
8387904d 12826 {
51aecdc5 12827 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12828 if (adjust == -1)
12829 continue;
e054468f 12830 code_value += adjust;
8387904d
AM
12831 sym_value += adjust;
12832 }
12833 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12834 &code_sec, &code_value, FALSE);
8387904d
AM
12835 if (dest != (bfd_vma) -1)
12836 {
12837 destination = dest;
12838 if (fdh != NULL)
12839 {
12840 /* Fixup old ABI sym to point at code
12841 entry. */
99877b66 12842 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12843 hash->elf.root.u.def.section = code_sec;
e054468f 12844 hash->elf.root.u.def.value = code_value;
8387904d
AM
12845 }
12846 }
12847 }
12848
721956f4 12849 /* Determine what (if any) linker stub is needed. */
e054468f 12850 plt_ent = NULL;
721956f4 12851 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12852 &plt_ent, destination,
12853 local_off);
ad8e1ba5
AM
12854
12855 if (stub_type != ppc_stub_plt_call)
12856 {
12857 /* Check whether we need a TOC adjusting stub.
12858 Since the linker pastes together pieces from
12859 different object files when creating the
12860 _init and _fini functions, it may be that a
12861 call to what looks like a local sym is in
12862 fact a call needing a TOC adjustment. */
8387904d
AM
12863 if (code_sec != NULL
12864 && code_sec->output_section != NULL
6f20ed8a
AM
12865 && (htab->sec_info[code_sec->id].toc_off
12866 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12867 && (code_sec->has_toc_reloc
12868 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12869 stub_type = ppc_stub_long_branch_r2off;
12870 }
12871
721956f4
AM
12872 if (stub_type == ppc_stub_none)
12873 continue;
12874
411e1bfb
AM
12875 /* __tls_get_addr calls might be eliminated. */
12876 if (stub_type != ppc_stub_plt_call
12877 && hash != NULL
8387904d
AM
12878 && (hash == htab->tls_get_addr
12879 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12880 && section->has_tls_reloc
12881 && irela != internal_relocs)
12882 {
12883 /* Get tls info. */
f961d9dd 12884 unsigned char *tls_mask;
411e1bfb 12885
3a71aa26 12886 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12887 irela - 1, input_bfd))
12888 goto error_ret_free_internal;
37da22e5 12889 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
12890 continue;
12891 }
12892
f378ab09 12893 if (stub_type == ppc_stub_plt_call)
794e51c0 12894 {
6e1816be
AM
12895 if (!htab->opd_abi
12896 && htab->params->plt_localentry0 != 0
12897 && is_elfv2_localentry0 (&hash->elf))
12898 htab->has_plt_localentry0 = 1;
12899 else if (irela + 1 < irelaend
12900 && irela[1].r_offset == irela->r_offset + 4
12901 && (ELF64_R_TYPE (irela[1].r_info)
12902 == R_PPC64_TOCSAVE))
f378ab09
AM
12903 {
12904 if (!tocsave_find (htab, INSERT,
12905 &local_syms, irela + 1, input_bfd))
12906 goto error_ret_free_internal;
12907 }
f378ab09
AM
12908 else
12909 stub_type = ppc_stub_plt_call_r2save;
794e51c0 12910 }
3b421ab3 12911
721956f4 12912 /* Support for grouping stub sections. */
6f20ed8a 12913 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12914
12915 /* Get the name of this stub. */
12916 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12917 if (!stub_name)
12918 goto error_ret_free_internal;
12919
12920 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12921 stub_name, FALSE, FALSE);
721956f4
AM
12922 if (stub_entry != NULL)
12923 {
12924 /* The proper stub has already been created. */
12925 free (stub_name);
794e51c0
AM
12926 if (stub_type == ppc_stub_plt_call_r2save)
12927 stub_entry->stub_type = stub_type;
721956f4
AM
12928 continue;
12929 }
12930
25f53a85 12931 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12932 if (stub_entry == NULL)
12933 {
12934 free (stub_name);
6cdc0ccc
AM
12935 error_ret_free_internal:
12936 if (elf_section_data (section)->relocs == NULL)
12937 free (internal_relocs);
12938 error_ret_free_local:
12939 if (local_syms != NULL
12940 && (symtab_hdr->contents
12941 != (unsigned char *) local_syms))
12942 free (local_syms);
b34976b6 12943 return FALSE;
721956f4
AM
12944 }
12945
ad8e1ba5 12946 stub_entry->stub_type = stub_type;
794e51c0
AM
12947 if (stub_type != ppc_stub_plt_call
12948 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12949 {
12950 stub_entry->target_value = code_value;
12951 stub_entry->target_section = code_sec;
12952 }
12953 else
12954 {
12955 stub_entry->target_value = sym_value;
12956 stub_entry->target_section = sym_sec;
12957 }
721956f4 12958 stub_entry->h = hash;
e054468f 12959 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
12960 stub_entry->symtype
12961 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 12962 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12963
12964 if (stub_entry->h != NULL)
12965 htab->stub_globals += 1;
721956f4
AM
12966 }
12967
12968 /* We're done with the internal relocs, free them. */
6cdc0ccc 12969 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12970 free (internal_relocs);
721956f4 12971 }
6cdc0ccc
AM
12972
12973 if (local_syms != NULL
12974 && symtab_hdr->contents != (unsigned char *) local_syms)
12975 {
12976 if (!info->keep_memory)
12977 free (local_syms);
12978 else
12979 symtab_hdr->contents = (unsigned char *) local_syms;
12980 }
721956f4
AM
12981 }
12982
5c3dead3 12983 /* We may have added some stubs. Find out the new size of the
721956f4 12984 stub sections. */
d4aaa2a0
AM
12985 for (group = htab->group; group != NULL; group = group->next)
12986 if (group->stub_sec != NULL)
ee75fd95 12987 {
d4aaa2a0
AM
12988 asection *stub_sec = group->stub_sec;
12989
ea3d7d1c
AM
12990 if (htab->stub_iteration <= STUB_SHRINK_ITER
12991 || stub_sec->rawsize < stub_sec->size)
12992 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12993 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12994 stub_sec->size = 0;
12995 stub_sec->reloc_count = 0;
84f5d08e 12996 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12997 }
eea6121a 12998
ba21f564
AM
12999 if (htab->stub_iteration <= STUB_SHRINK_ITER
13000 || htab->brlt->rawsize < htab->brlt->size)
13001 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13002 htab->brlt->size = 0;
84f5d08e
AM
13003 htab->brlt->reloc_count = 0;
13004 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13005 if (htab->relbrlt != NULL)
eea6121a 13006 htab->relbrlt->size = 0;
721956f4 13007
63bc6f6c 13008 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13009
a4b6fadd
AM
13010 for (group = htab->group; group != NULL; group = group->next)
13011 if (group->needs_save_res)
13012 group->stub_sec->size += htab->sfpr->size;
13013
176a0d42
AM
13014 if (info->emitrelocations
13015 && htab->glink != NULL && htab->glink->size != 0)
13016 {
13017 htab->glink->reloc_count = 1;
13018 htab->glink->flags |= SEC_RELOC;
13019 }
13020
58d180e8
AM
13021 if (htab->glink_eh_frame != NULL
13022 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13023 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13024 {
2e0ce1c8 13025 size_t size = 0, align = 4;
58d180e8 13026
d4aaa2a0
AM
13027 for (group = htab->group; group != NULL; group = group->next)
13028 if (group->stub_sec != NULL)
13029 size += stub_eh_frame_size (group, align);
58d180e8 13030 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13031 size += (24 + align - 1) & -align;
58d180e8 13032 if (size != 0)
2e0ce1c8
AM
13033 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13034 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13035 size = (size + align - 1) & -align;
58d180e8
AM
13036 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13037 htab->glink_eh_frame->size = size;
13038 }
13039
e7d1c40c 13040 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13041 for (group = htab->group; group != NULL; group = group->next)
13042 if (group->stub_sec != NULL)
691d2e9a
AM
13043 {
13044 int align = abs (htab->params->plt_stub_align);
13045 group->stub_sec->size
13046 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13047 }
d4aaa2a0
AM
13048
13049 for (group = htab->group; group != NULL; group = group->next)
13050 if (group->stub_sec != NULL
13051 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13052 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13053 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13054 break;
13055
d4aaa2a0 13056 if (group == NULL
ba21f564
AM
13057 && (htab->brlt->rawsize == htab->brlt->size
13058 || (htab->stub_iteration > STUB_SHRINK_ITER
13059 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
13060 && (htab->glink_eh_frame == NULL
13061 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
13062 break;
13063
721956f4 13064 /* Ask the linker to do its stuff. */
e7d1c40c 13065 (*htab->params->layout_sections_again) ();
721956f4
AM
13066 }
13067
da44f4e5
AM
13068 if (htab->glink_eh_frame != NULL
13069 && htab->glink_eh_frame->size != 0)
13070 {
13071 bfd_vma val;
13072 bfd_byte *p, *last_fde;
13073 size_t last_fde_len, size, align, pad;
d4aaa2a0 13074 struct map_stub *group;
da44f4e5
AM
13075
13076 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13077 if (p == NULL)
13078 return FALSE;
13079 htab->glink_eh_frame->contents = p;
13080 last_fde = p;
2e0ce1c8 13081 align = 4;
da44f4e5
AM
13082
13083 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13084 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13085 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13086 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13087 p += last_fde_len + 4;
da44f4e5 13088
d4aaa2a0
AM
13089 for (group = htab->group; group != NULL; group = group->next)
13090 if (group->stub_sec != NULL)
da44f4e5
AM
13091 {
13092 last_fde = p;
d4aaa2a0 13093 last_fde_len = stub_eh_frame_size (group, align) - 4;
da44f4e5 13094 /* FDE length. */
2e0ce1c8 13095 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13096 p += 4;
13097 /* CIE pointer. */
13098 val = p - htab->glink_eh_frame->contents;
13099 bfd_put_32 (htab->elf.dynobj, val, p);
13100 p += 4;
13101 /* Offset to stub section, written later. */
13102 p += 4;
13103 /* stub section size. */
d4aaa2a0 13104 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13105 p += 4;
13106 /* Augmentation. */
13107 p += 1;
d4aaa2a0
AM
13108 if (group->tls_get_addr_opt_bctrl != -1u)
13109 {
13110 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
13111
13112 /* This FDE needs more than just the default.
13113 Describe __tls_get_addr_opt stub LR. */
13114 if (to_bctrl < 64)
13115 *p++ = DW_CFA_advance_loc + to_bctrl;
13116 else if (to_bctrl < 256)
13117 {
13118 *p++ = DW_CFA_advance_loc1;
13119 *p++ = to_bctrl;
13120 }
13121 else if (to_bctrl < 65536)
13122 {
13123 *p++ = DW_CFA_advance_loc2;
13124 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
13125 p += 2;
13126 }
13127 else
13128 {
13129 *p++ = DW_CFA_advance_loc4;
13130 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
13131 p += 4;
13132 }
13133 *p++ = DW_CFA_offset_extended_sf;
13134 *p++ = 65;
13135 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
13136 *p++ = DW_CFA_advance_loc + 4;
13137 *p++ = DW_CFA_restore_extended;
13138 *p++ = 65;
13139 }
da44f4e5 13140 /* Pad. */
d4aaa2a0 13141 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13142 }
13143 if (htab->glink != NULL && htab->glink->size != 0)
13144 {
13145 last_fde = p;
2e0ce1c8 13146 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13147 /* FDE length. */
2e0ce1c8 13148 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13149 p += 4;
13150 /* CIE pointer. */
13151 val = p - htab->glink_eh_frame->contents;
13152 bfd_put_32 (htab->elf.dynobj, val, p);
13153 p += 4;
13154 /* Offset to .glink, written later. */
13155 p += 4;
13156 /* .glink size. */
13157 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13158 p += 4;
13159 /* Augmentation. */
13160 p += 1;
13161
13162 *p++ = DW_CFA_advance_loc + 1;
13163 *p++ = DW_CFA_register;
13164 *p++ = 65;
9f08fa5c 13165 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 13166 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
13167 *p++ = DW_CFA_restore_extended;
13168 *p++ = 65;
2e0ce1c8 13169 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13170 }
13171 /* Subsume any padding into the last FDE if user .eh_frame
13172 sections are aligned more than glink_eh_frame. Otherwise any
13173 zero padding will be seen as a terminator. */
2e0ce1c8 13174 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13175 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13176 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13177 htab->glink_eh_frame->size = size + pad;
13178 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13179 }
13180
d969d15f
AM
13181 maybe_strip_output (info, htab->brlt);
13182 if (htab->glink_eh_frame != NULL)
13183 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13184
b34976b6 13185 return TRUE;
721956f4
AM
13186}
13187
13188/* Called after we have determined section placement. If sections
805fc799 13189 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13190
805fc799 13191bfd_vma
1c865ab2 13192ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13193{
805fc799 13194 asection *s;
a27e685f 13195 bfd_vma TOCstart, adjust;
721956f4 13196
43417696
AM
13197 if (info != NULL)
13198 {
13199 struct elf_link_hash_entry *h;
13200 struct elf_link_hash_table *htab = elf_hash_table (info);
13201
13202 if (is_elf_hash_table (htab)
13203 && htab->hgot != NULL)
13204 h = htab->hgot;
13205 else
13206 {
13207 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13208 if (is_elf_hash_table (htab))
13209 htab->hgot = h;
13210 }
13211 if (h != NULL
13212 && h->root.type == bfd_link_hash_defined
13213 && !h->root.linker_def
13214 && (!is_elf_hash_table (htab)
13215 || h->def_regular))
13216 {
13217 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13218 + h->root.u.def.section->output_offset
13219 + h->root.u.def.section->output_section->vma);
13220 _bfd_set_gp_value (obfd, TOCstart);
13221 return TOCstart;
13222 }
13223 }
13224
805fc799
AM
13225 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13226 order. The TOC starts where the first of these sections starts. */
13227 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13228 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13229 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13230 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13231 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13232 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13233 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13234 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13235 {
13236 /* This may happen for
13237 o references to TOC base (SYM@toc / TOC[tc0]) without a
13238 .toc directive
13239 o bad linker script
13240 o --gc-sections and empty TOC sections
13241
13242 FIXME: Warn user? */
13243
13244 /* Look for a likely section. We probably won't even be
13245 using TOCstart. */
13246 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13247 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13248 | SEC_EXCLUDE))
805fc799
AM
13249 == (SEC_ALLOC | SEC_SMALL_DATA))
13250 break;
721956f4 13251 if (s == NULL)
805fc799 13252 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13253 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13254 == (SEC_ALLOC | SEC_SMALL_DATA))
13255 break;
721956f4 13256 if (s == NULL)
805fc799 13257 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13258 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13259 == SEC_ALLOC)
805fc799 13260 break;
721956f4 13261 if (s == NULL)
805fc799 13262 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13263 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
13264 break;
13265 }
721956f4 13266
805fc799
AM
13267 TOCstart = 0;
13268 if (s != NULL)
13269 TOCstart = s->output_section->vma + s->output_offset;
721956f4 13270
a27e685f
AM
13271 /* Force alignment. */
13272 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13273 TOCstart -= adjust;
1c865ab2
AM
13274 _bfd_set_gp_value (obfd, TOCstart);
13275
810d4e75 13276 if (info != NULL && s != NULL)
1c865ab2
AM
13277 {
13278 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13279
810d4e75
AM
13280 if (htab != NULL)
13281 {
13282 if (htab->elf.hgot != NULL)
13283 {
a27e685f 13284 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
13285 htab->elf.hgot->root.u.def.section = s;
13286 }
13287 }
13288 else
1c865ab2 13289 {
810d4e75
AM
13290 struct bfd_link_hash_entry *bh = NULL;
13291 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
13292 s, TOC_BASE_OFF - adjust,
13293 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
13294 }
13295 }
805fc799 13296 return TOCstart;
721956f4
AM
13297}
13298
a345bc8d 13299/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 13300 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
13301
13302static bfd_boolean
49c09209 13303build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
13304{
13305 struct bfd_link_info *info;
13306 struct ppc_link_hash_table *htab;
49c09209 13307 struct plt_entry *ent;
a345bc8d
AM
13308 asection *s;
13309
13310 if (h->root.type == bfd_link_hash_indirect)
13311 return TRUE;
13312
49c09209
AM
13313 info = inf;
13314 htab = ppc_hash_table (info);
13315 if (htab == NULL)
13316 return FALSE;
13317
13318 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13319 if (ent->plt.offset != (bfd_vma) -1)
13320 {
13321 /* This symbol has an entry in the procedure linkage
13322 table. Set it up. */
13323 Elf_Internal_Rela rela;
2d7ad24e 13324 asection *plt, *relplt;
49c09209
AM
13325 bfd_byte *loc;
13326
13327 if (!htab->elf.dynamic_sections_created
13328 || h->dynindx == -1)
13329 {
13330 if (!(h->def_regular
13331 && (h->root.type == bfd_link_hash_defined
13332 || h->root.type == bfd_link_hash_defweak)))
13333 continue;
2d7ad24e
AM
13334 if (h->type == STT_GNU_IFUNC)
13335 {
13336 plt = htab->elf.iplt;
13337 relplt = htab->elf.irelplt;
13338 htab->local_ifunc_resolver = 1;
13339 if (htab->opd_abi)
13340 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13341 else
13342 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13343 }
49c09209 13344 else
2d7ad24e
AM
13345 {
13346 plt = htab->pltlocal;
13347 if (bfd_link_pic (info))
13348 {
13349 relplt = htab->relpltlocal;
13350 if (htab->opd_abi)
13351 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13352 else
13353 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13354 }
13355 else
13356 relplt = NULL;
13357 }
49c09209
AM
13358 rela.r_addend = (h->root.u.def.value
13359 + h->root.u.def.section->output_offset
13360 + h->root.u.def.section->output_section->vma
13361 + ent->addend);
2d7ad24e
AM
13362
13363 if (relplt == NULL)
13364 {
13365 loc = plt->contents + ent->plt.offset;
13366 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13367 if (htab->opd_abi)
13368 {
13369 bfd_vma toc = elf_gp (info->output_bfd);
13370 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13371 bfd_put_64 (info->output_bfd, toc, loc + 8);
13372 }
13373 }
13374 else
13375 {
13376 rela.r_offset = (plt->output_section->vma
13377 + plt->output_offset
13378 + ent->plt.offset);
13379 loc = relplt->contents + (relplt->reloc_count++
13380 * sizeof (Elf64_External_Rela));
13381 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13382 }
49c09209
AM
13383 }
13384 else
13385 {
13386 rela.r_offset = (htab->elf.splt->output_section->vma
13387 + htab->elf.splt->output_offset
13388 + ent->plt.offset);
13389 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13390 rela.r_addend = ent->addend;
13391 loc = (htab->elf.srelplt->contents
13392 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13393 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13394 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13395 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 13396 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 13397 }
49c09209
AM
13398 }
13399
a345bc8d
AM
13400 if (!h->pointer_equality_needed)
13401 return TRUE;
13402
13403 if (h->def_regular)
13404 return TRUE;
13405
9e390558 13406 s = htab->global_entry;
49c09209
AM
13407 if (s == NULL || s->size == 0)
13408 return TRUE;
13409
13410 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13411 if (ent->plt.offset != (bfd_vma) -1
13412 && ent->addend == 0)
a345bc8d
AM
13413 {
13414 bfd_byte *p;
13415 asection *plt;
13416 bfd_vma off;
13417
a345bc8d 13418 p = s->contents + h->root.u.def.value;
33e44f2e 13419 plt = htab->elf.splt;
a345bc8d
AM
13420 if (!htab->elf.dynamic_sections_created
13421 || h->dynindx == -1)
2d7ad24e
AM
13422 {
13423 if (h->type == STT_GNU_IFUNC)
13424 plt = htab->elf.iplt;
13425 else
13426 plt = htab->pltlocal;
13427 }
49c09209 13428 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
13429 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13430
13431 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13432 {
13433 info->callbacks->einfo
c1c8c1ef 13434 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13435 h->root.root.string);
13436 bfd_set_error (bfd_error_bad_value);
13437 htab->stub_error = TRUE;
13438 }
13439
7341d5e2
AM
13440 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13441 if (htab->params->emit_stub_syms)
13442 {
13443 size_t len = strlen (h->root.root.string);
13444 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13445
13446 if (name == NULL)
13447 return FALSE;
13448
13449 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13450 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13451 if (h == NULL)
13452 return FALSE;
13453 if (h->root.type == bfd_link_hash_new)
13454 {
13455 h->root.type = bfd_link_hash_defined;
13456 h->root.u.def.section = s;
13457 h->root.u.def.value = p - s->contents;
13458 h->ref_regular = 1;
13459 h->def_regular = 1;
13460 h->ref_regular_nonweak = 1;
13461 h->forced_local = 1;
13462 h->non_elf = 0;
2ec55de3 13463 h->root.linker_def = 1;
7341d5e2
AM
13464 }
13465 }
13466
a345bc8d
AM
13467 if (PPC_HA (off) != 0)
13468 {
13469 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13470 p += 4;
13471 }
13472 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13473 p += 4;
13474 bfd_put_32 (s->owner, MTCTR_R12, p);
13475 p += 4;
407aa07c 13476 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13477 break;
13478 }
13479 return TRUE;
13480}
13481
49c09209
AM
13482/* Write PLT relocs for locals. */
13483
13484static bfd_boolean
13485write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13486{
13487 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13488 bfd *ibfd;
13489
13490 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13491 {
13492 struct got_entry **lgot_ents, **end_lgot_ents;
13493 struct plt_entry **local_plt, **lplt, **end_local_plt;
13494 Elf_Internal_Shdr *symtab_hdr;
13495 bfd_size_type locsymcount;
13496 Elf_Internal_Sym *local_syms = NULL;
13497 struct plt_entry *ent;
13498
13499 if (!is_ppc64_elf (ibfd))
13500 continue;
13501
13502 lgot_ents = elf_local_got_ents (ibfd);
13503 if (!lgot_ents)
13504 continue;
13505
13506 symtab_hdr = &elf_symtab_hdr (ibfd);
13507 locsymcount = symtab_hdr->sh_info;
13508 end_lgot_ents = lgot_ents + locsymcount;
13509 local_plt = (struct plt_entry **) end_lgot_ents;
13510 end_local_plt = local_plt + locsymcount;
13511 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13512 for (ent = *lplt; ent != NULL; ent = ent->next)
13513 if (ent->plt.offset != (bfd_vma) -1)
13514 {
13515 Elf_Internal_Sym *sym;
13516 asection *sym_sec;
13517 asection *plt, *relplt;
13518 bfd_byte *loc;
13519 bfd_vma val;
49c09209
AM
13520
13521 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13522 lplt - local_plt, ibfd))
13523 {
13524 if (local_syms != NULL
13525 && symtab_hdr->contents != (unsigned char *) local_syms)
13526 free (local_syms);
13527 return FALSE;
13528 }
13529
13530 val = sym->st_value + ent->addend;
13531 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13532 if (sym_sec != NULL && sym_sec->output_section != NULL)
13533 val += sym_sec->output_offset + sym_sec->output_section->vma;
13534
2d7ad24e
AM
13535 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13536 {
13537 htab->local_ifunc_resolver = 1;
13538 plt = htab->elf.iplt;
13539 relplt = htab->elf.irelplt;
13540 }
13541 else
13542 {
13543 plt = htab->pltlocal;
13544 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13545 }
49c09209 13546
2d7ad24e
AM
13547 if (relplt == NULL)
13548 {
13549 loc = plt->contents + ent->plt.offset;
13550 bfd_put_64 (info->output_bfd, val, loc);
13551 if (htab->opd_abi)
13552 {
13553 bfd_vma toc = elf_gp (ibfd);
13554 bfd_put_64 (info->output_bfd, toc, loc + 8);
13555 }
13556 }
49c09209 13557 else
2d7ad24e
AM
13558 {
13559 Elf_Internal_Rela rela;
13560 rela.r_offset = (ent->plt.offset
13561 + plt->output_offset
13562 + plt->output_section->vma);
13563 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13564 {
13565 if (htab->opd_abi)
13566 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13567 else
13568 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13569 }
13570 else
13571 {
13572 if (htab->opd_abi)
13573 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13574 else
13575 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13576 }
13577 rela.r_addend = val;
13578 loc = relplt->contents + (relplt->reloc_count++
13579 * sizeof (Elf64_External_Rela));
13580 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13581 }
49c09209
AM
13582 }
13583
13584 if (local_syms != NULL
13585 && symtab_hdr->contents != (unsigned char *) local_syms)
13586 {
13587 if (!info->keep_memory)
13588 free (local_syms);
13589 else
13590 symtab_hdr->contents = (unsigned char *) local_syms;
13591 }
13592 }
13593 return TRUE;
13594}
13595
721956f4
AM
13596/* Build all the stubs associated with the current output file.
13597 The stubs are kept in a hash table attached to the main linker
13598 hash table. This function is called via gldelf64ppc_finish. */
13599
b34976b6 13600bfd_boolean
e7d1c40c 13601ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13602 char **stats)
5d1634d7
AM
13603{
13604 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13605 struct map_stub *group;
721956f4 13606 asection *stub_sec;
5d1634d7 13607 bfd_byte *p;
e717da7e 13608 int stub_sec_count = 0;
5d1634d7 13609
4dfe6ac6
NC
13610 if (htab == NULL)
13611 return FALSE;
13612
eea6121a 13613 /* Allocate memory to hold the linker stubs. */
d4aaa2a0
AM
13614 for (group = htab->group; group != NULL; group = group->next)
13615 if ((stub_sec = group->stub_sec) != NULL
eea6121a 13616 && stub_sec->size != 0)
e717da7e 13617 {
e7d1c40c 13618 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
13619 if (stub_sec->contents == NULL)
13620 return FALSE;
eea6121a 13621 stub_sec->size = 0;
e717da7e 13622 }
5d1634d7 13623
23eb7e01 13624 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13625 {
9f951329 13626 unsigned int indx;
ad8e1ba5 13627 bfd_vma plt0;
9f951329 13628
721956f4 13629 /* Build the .glink plt call stub. */
e7d1c40c 13630 if (htab->params->emit_stub_syms)
97b639ba
AM
13631 {
13632 struct elf_link_hash_entry *h;
468392fb
AM
13633 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13634 TRUE, FALSE, FALSE);
97b639ba
AM
13635 if (h == NULL)
13636 return FALSE;
13637 if (h->root.type == bfd_link_hash_new)
13638 {
13639 h->root.type = bfd_link_hash_defined;
13640 h->root.u.def.section = htab->glink;
ee4bf8d2 13641 h->root.u.def.value = 8;
f5385ebf
AM
13642 h->ref_regular = 1;
13643 h->def_regular = 1;
13644 h->ref_regular_nonweak = 1;
13645 h->forced_local = 1;
13646 h->non_elf = 0;
2ec55de3 13647 h->root.linker_def = 1;
97b639ba
AM
13648 }
13649 }
33e44f2e
AM
13650 plt0 = (htab->elf.splt->output_section->vma
13651 + htab->elf.splt->output_offset
13652 - 16);
176a0d42
AM
13653 if (info->emitrelocations)
13654 {
13655 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13656 if (r == NULL)
13657 return FALSE;
13658 r->r_offset = (htab->glink->output_offset
13659 + htab->glink->output_section->vma);
13660 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13661 r->r_addend = plt0;
13662 }
4ce794b7 13663 p = htab->glink->contents;
176a0d42 13664 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13665 bfd_put_64 (htab->glink->owner, plt0, p);
13666 p += 8;
b9e5796b
AM
13667 if (htab->opd_abi)
13668 {
13669 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13670 p += 4;
13671 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13672 p += 4;
13673 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13674 p += 4;
13675 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13676 p += 4;
13677 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13678 p += 4;
13679 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13680 p += 4;
13681 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13682 p += 4;
13683 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13684 p += 4;
13685 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13686 p += 4;
13687 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13688 p += 4;
13689 }
13690 else
13691 {
13692 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13693 p += 4;
13694 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13695 p += 4;
13696 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13697 p += 4;
f378ab09
AM
13698 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13699 p += 4;
b9e5796b
AM
13700 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13701 p += 4;
13702 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13703 p += 4;
13704 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13705 p += 4;
13706 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13707 p += 4;
13708 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13709 p += 4;
13710 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13711 p += 4;
13712 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13713 p += 4;
13714 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13715 p += 4;
13716 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13717 p += 4;
13718 }
407aa07c
AM
13719 bfd_put_32 (htab->glink->owner, BCTR, p);
13720 p += 4;
c75bc4f7 13721 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13722
9f951329
AM
13723 /* Build the .glink lazy link call stubs. */
13724 indx = 0;
9e390558 13725 while (p < htab->glink->contents + htab->glink->size)
9f951329 13726 {
b9e5796b 13727 if (htab->opd_abi)
9f951329 13728 {
b9e5796b
AM
13729 if (indx < 0x8000)
13730 {
13731 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13732 p += 4;
13733 }
13734 else
13735 {
13736 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13737 p += 4;
13738 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13739 p);
13740 p += 4;
13741 }
9f951329 13742 }
4ce794b7 13743 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13744 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13745 indx++;
9f951329
AM
13746 p += 4;
13747 }
5d1634d7 13748 }
5d1634d7 13749
49c09209
AM
13750 /* Build .glink global entry stubs, and PLT relocs for globals. */
13751 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13752
13753 if (!write_plt_relocs_for_local_syms (info))
13754 return FALSE;
9e390558 13755
7341d5e2 13756 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13757 {
4ce794b7 13758 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13759 htab->brlt->size);
4ce794b7 13760 if (htab->brlt->contents == NULL)
b34976b6 13761 return FALSE;
721956f4 13762 }
ee75fd95 13763 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13764 {
13765 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13766 htab->relbrlt->size);
63bc6f6c
AM
13767 if (htab->relbrlt->contents == NULL)
13768 return FALSE;
13769 }
5d1634d7 13770
721956f4
AM
13771 /* Build the stubs as directed by the stub hash table. */
13772 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13773
a4b6fadd
AM
13774 for (group = htab->group; group != NULL; group = group->next)
13775 if (group->needs_save_res)
7dda8d3c 13776 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 13777
aa8a7074
AM
13778 if (htab->relbrlt != NULL)
13779 htab->relbrlt->reloc_count = 0;
13780
e7d1c40c 13781 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13782 for (group = htab->group; group != NULL; group = group->next)
13783 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13784 {
13785 int align = abs (htab->params->plt_stub_align);
13786 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13787 }
794e51c0 13788
7dda8d3c
AM
13789 for (group = htab->group; group != NULL; group = group->next)
13790 if (group->needs_save_res)
13791 {
13792 stub_sec = group->stub_sec;
13793 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13794 htab->sfpr->contents, htab->sfpr->size);
13795 if (htab->params->emit_stub_syms)
13796 {
13797 unsigned int i;
13798
13799 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13800 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13801 return FALSE;
13802 }
13803 }
13804
d4aaa2a0
AM
13805 for (group = htab->group; group != NULL; group = group->next)
13806 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
13807 {
13808 stub_sec_count += 1;
c9301e31
AM
13809 if (stub_sec->rawsize != stub_sec->size
13810 && (htab->stub_iteration <= STUB_SHRINK_ITER
13811 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13812 break;
13813 }
5d1634d7 13814
25516cc5 13815 if (group != NULL)
5d1634d7 13816 {
b34976b6 13817 htab->stub_error = TRUE;
cf97bcb0 13818 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 13819 }
721956f4 13820
d2a300cf
AM
13821 if (htab->stub_error)
13822 return FALSE;
13823
13824 if (stats != NULL)
13825 {
db84fff3 13826 size_t len;
d2a300cf
AM
13827 *stats = bfd_malloc (500);
13828 if (*stats == NULL)
13829 return FALSE;
13830
db84fff3
AM
13831 len = sprintf (*stats,
13832 ngettext ("linker stubs in %u group\n",
13833 "linker stubs in %u groups\n",
13834 stub_sec_count),
13835 stub_sec_count);
13836 sprintf (*stats + len, _(" branch %lu\n"
13837 " toc adjust %lu\n"
13838 " long branch %lu\n"
13839 " long toc adj %lu\n"
13840 " plt call %lu\n"
13841 " plt call toc %lu\n"
13842 " global entry %lu"),
4ce794b7
AM
13843 htab->stub_count[ppc_stub_long_branch - 1],
13844 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13845 htab->stub_count[ppc_stub_plt_branch - 1],
13846 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13847 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13848 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13849 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13850 }
13851 return TRUE;
5bd4f169
AM
13852}
13853
60124e18
AM
13854/* What to do when ld finds relocations against symbols defined in
13855 discarded sections. */
13856
13857static unsigned int
13858ppc64_elf_action_discarded (asection *sec)
13859{
13860 if (strcmp (".opd", sec->name) == 0)
13861 return 0;
13862
13863 if (strcmp (".toc", sec->name) == 0)
13864 return 0;
13865
bce50a28
JJ
13866 if (strcmp (".toc1", sec->name) == 0)
13867 return 0;
13868
60124e18
AM
13869 return _bfd_elf_default_action_discarded (sec);
13870}
13871
5bd4f169
AM
13872/* The RELOCATE_SECTION function is called by the ELF backend linker
13873 to handle the relocations for a section.
13874
13875 The relocs are always passed as Rela structures; if the section
13876 actually uses Rel structures, the r_addend field will always be
13877 zero.
13878
13879 This function is responsible for adjust the section contents as
13880 necessary, and (if using Rela relocs and generating a
1049f94e 13881 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13882 necessary.
13883
13884 This function does not have to worry about setting the reloc
13885 address or the reloc symbol index.
13886
13887 LOCAL_SYMS is a pointer to the swapped in local symbols.
13888
13889 LOCAL_SECTIONS is an array giving the section in the input file
13890 corresponding to the st_shndx field of each local symbol.
13891
13892 The global hash table entry for the global symbols can be found
13893 via elf_sym_hashes (input_bfd).
13894
1049f94e 13895 When generating relocatable output, this function must handle
5bd4f169
AM
13896 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13897 going to be the section symbol corresponding to the output
13898 section, which means that the addend must be adjusted
13899 accordingly. */
13900
b34976b6 13901static bfd_boolean
4ce794b7
AM
13902ppc64_elf_relocate_section (bfd *output_bfd,
13903 struct bfd_link_info *info,
13904 bfd *input_bfd,
13905 asection *input_section,
13906 bfd_byte *contents,
13907 Elf_Internal_Rela *relocs,
13908 Elf_Internal_Sym *local_syms,
13909 asection **local_sections)
5bd4f169 13910{
65f38f15 13911 struct ppc_link_hash_table *htab;
5bd4f169
AM
13912 Elf_Internal_Shdr *symtab_hdr;
13913 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13914 Elf_Internal_Rela *rel;
c316a17c 13915 Elf_Internal_Rela *wrel;
5bd4f169 13916 Elf_Internal_Rela *relend;
411e1bfb
AM
13917 Elf_Internal_Rela outrel;
13918 bfd_byte *loc;
411e1bfb 13919 struct got_entry **local_got_ents;
5bd4f169 13920 bfd_vma TOCstart;
b34976b6
AM
13921 bfd_boolean ret = TRUE;
13922 bfd_boolean is_opd;
794e51c0
AM
13923 /* Assume 'at' branch hints. */
13924 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13925 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13926
65f38f15 13927 /* Initialize howto table if needed. */
5bd4f169 13928 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13929 ppc_howto_init ();
13930
65f38f15 13931 htab = ppc_hash_table (info);
4dfe6ac6
NC
13932 if (htab == NULL)
13933 return FALSE;
ee75fd95
AM
13934
13935 /* Don't relocate stub sections. */
e7d1c40c 13936 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13937 return TRUE;
13938
0c8d6e5c 13939 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13940
411e1bfb 13941 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13942 TOCstart = elf_gp (output_bfd);
0ffa91dd 13943 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13944 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13945 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13946
c316a17c 13947 rel = wrel = relocs;
5bd4f169 13948 relend = relocs + input_section->reloc_count;
c316a17c 13949 for (; rel < relend; wrel++, rel++)
5bd4f169 13950 {
04c9666a 13951 enum elf_ppc64_reloc_type r_type;
31c76678 13952 bfd_vma addend;
5bd4f169
AM
13953 bfd_reloc_status_type r;
13954 Elf_Internal_Sym *sym;
13955 asection *sec;
039b3fef
AM
13956 struct elf_link_hash_entry *h_elf;
13957 struct ppc_link_hash_entry *h;
13958 struct ppc_link_hash_entry *fdh;
5bd4f169 13959 const char *sym_name;
0d4792f7 13960 unsigned long r_symndx, toc_symndx;
3a71aa26 13961 bfd_vma toc_addend;
f961d9dd
AM
13962 unsigned char tls_mask, tls_gd, tls_type;
13963 unsigned char sym_type;
5bd4f169 13964 bfd_vma relocation;
23cedd1d 13965 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 13966 bfd_boolean warned;
bc30df16 13967 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13968 unsigned int insn;
e11840f9 13969 unsigned int mask;
721956f4
AM
13970 struct ppc_stub_hash_entry *stub_entry;
13971 bfd_vma max_br_offset;
13972 bfd_vma from;
c316a17c 13973 Elf_Internal_Rela orig_rel;
b80eed39
AM
13974 reloc_howto_type *howto;
13975 struct reloc_howto_struct alt_howto;
5bd4f169 13976
c316a17c
AM
13977 again:
13978 orig_rel = *rel;
13979
4ce794b7 13980 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13981 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13982
13983 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13984 symbol of the previous ADDR64 reloc. The symbol gives us the
13985 proper TOC base to use. */
13986 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13987 && wrel != relocs
13988 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13989 && is_opd)
c316a17c 13990 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13991
4ce794b7
AM
13992 sym = NULL;
13993 sec = NULL;
039b3fef 13994 h_elf = NULL;
4ce794b7 13995 sym_name = NULL;
b34976b6
AM
13996 unresolved_reloc = FALSE;
13997 warned = FALSE;
65f38f15 13998
0b13192e 13999 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
14000 {
14001 /* It's a local symbol. */
74f0fb50 14002 struct _opd_sec_data *opd;
4025353c 14003
5bd4f169
AM
14004 sym = local_syms + r_symndx;
14005 sec = local_sections[r_symndx];
26c61ae5 14006 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 14007 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 14008 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
14009 opd = get_opd_info (sec);
14010 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 14011 {
51aecdc5
AM
14012 long adjust = opd->adjust[OPD_NDX (sym->st_value
14013 + rel->r_addend)];
4025353c
AM
14014 if (adjust == -1)
14015 relocation = 0;
14016 else
4cc603a5
AM
14017 {
14018 /* If this is a relocation against the opd section sym
14019 and we have edited .opd, adjust the reloc addend so
14020 that ld -r and ld --emit-relocs output is correct.
14021 If it is a reloc against some other .opd symbol,
14022 then the symbol value will be adjusted later. */
14023 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14024 rel->r_addend += adjust;
14025 else
14026 relocation += adjust;
14027 }
1e2f5b6e 14028 }
5bd4f169
AM
14029 }
14030 else
14031 {
62d887d4
L
14032 bfd_boolean ignored;
14033
b2a8e766
AM
14034 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14035 r_symndx, symtab_hdr, sym_hashes,
039b3fef 14036 h_elf, sec, relocation,
62d887d4 14037 unresolved_reloc, warned, ignored);
039b3fef
AM
14038 sym_name = h_elf->root.root.string;
14039 sym_type = h_elf->type;
b69fdb4e
AM
14040 if (sec != NULL
14041 && sec->owner == output_bfd
14042 && strcmp (sec->name, ".opd") == 0)
14043 {
14044 /* This is a symbol defined in a linker script. All
14045 such are defined in output sections, even those
14046 defined by simple assignment from a symbol defined in
14047 an input section. Transfer the symbol to an
14048 appropriate input .opd section, so that a branch to
14049 this symbol will be mapped to the location specified
14050 by the opd entry. */
14051 struct bfd_link_order *lo;
14052 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14053 if (lo->type == bfd_indirect_link_order)
14054 {
14055 asection *isec = lo->u.indirect.section;
14056 if (h_elf->root.u.def.value >= isec->output_offset
14057 && h_elf->root.u.def.value < (isec->output_offset
14058 + isec->size))
14059 {
14060 h_elf->root.u.def.value -= isec->output_offset;
14061 h_elf->root.u.def.section = isec;
14062 sec = isec;
14063 break;
14064 }
14065 }
14066 }
5bd4f169 14067 }
039b3fef 14068 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 14069
dbaa2011 14070 if (sec != NULL && discarded_section (sec))
c316a17c
AM
14071 {
14072 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14073 input_bfd, input_section,
14074 contents + rel->r_offset);
14075 wrel->r_offset = rel->r_offset;
14076 wrel->r_info = 0;
14077 wrel->r_addend = 0;
14078
14079 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 14080 symbols defined in discarded sections. Not done for
c316a17c
AM
14081 non-debug to preserve relocs in .eh_frame which the
14082 eh_frame editing code expects to be present. */
14083 if (bfd_link_relocatable (info)
14084 && (input_section->flags & SEC_DEBUGGING))
14085 wrel--;
14086
14087 continue;
14088 }
ab96bf03 14089
0e1862bb 14090 if (bfd_link_relocatable (info))
c316a17c 14091 goto copy_reloc;
ab96bf03 14092
f40da81b
AM
14093 if (h != NULL && &h->elf == htab->elf.hgot)
14094 {
6f20ed8a 14095 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
14096 sec = bfd_abs_section_ptr;
14097 unresolved_reloc = FALSE;
14098 }
14099
951fd09b
AM
14100 /* TLS optimizations. Replace instruction sequences and relocs
14101 based on information we collected in tls_optimize. We edit
14102 RELOCS so that --emit-relocs will output something sensible
14103 for the final instruction stream. */
14104 tls_mask = 0;
14105 tls_gd = 0;
0d4792f7 14106 toc_symndx = 0;
727fc41e
AM
14107 if (h != NULL)
14108 tls_mask = h->tls_mask;
14109 else if (local_got_ents != NULL)
411e1bfb 14110 {
e054468f
AM
14111 struct plt_entry **local_plt = (struct plt_entry **)
14112 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 14113 unsigned char *lgot_masks = (unsigned char *)
e054468f 14114 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
14115 tls_mask = lgot_masks[r_symndx];
14116 }
37da22e5 14117 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
14118 && (r_type == R_PPC64_TLS
14119 || r_type == R_PPC64_TLSGD
14120 || r_type == R_PPC64_TLSLD))
14121 {
14122 /* Check for toc tls entries. */
f961d9dd 14123 unsigned char *toc_tls;
0d4792f7 14124
727fc41e
AM
14125 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14126 &local_syms, rel, input_bfd))
14127 return FALSE;
0d4792f7 14128
727fc41e
AM
14129 if (toc_tls)
14130 tls_mask = *toc_tls;
0d4792f7
AM
14131 }
14132
14133 /* Check that tls relocs are used with tls syms, and non-tls
14134 relocs are used with non-tls syms. */
cf35638d 14135 if (r_symndx != STN_UNDEF
0d4792f7
AM
14136 && r_type != R_PPC64_NONE
14137 && (h == NULL
039b3fef
AM
14138 || h->elf.root.type == bfd_link_hash_defined
14139 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
14140 && (IS_PPC64_TLS_RELOC (r_type)
14141 != (sym_type == STT_TLS
14142 || (sym_type == STT_SECTION
14143 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 14144 {
37da22e5 14145 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
14146 && (r_type == R_PPC64_TLS
14147 || r_type == R_PPC64_TLSGD
14148 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
14149 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14150 ;
14151 else
25f53a85 14152 info->callbacks->einfo
1d483afe 14153 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 14154 /* xgettext:c-format */
c1c8c1ef 14155 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 14156 /* xgettext:c-format */
c1c8c1ef 14157 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 14158 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
14159 ppc64_elf_howto_table[r_type]->name,
14160 sym_name);
411e1bfb
AM
14161 }
14162
14163 /* Ensure reloc mapping code below stays sane. */
14164 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14165 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14166 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14167 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14168 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14169 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14170 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14171 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14172 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14173 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14174 abort ();
0d4792f7 14175
411e1bfb
AM
14176 switch (r_type)
14177 {
14178 default:
411e1bfb
AM
14179 break;
14180
ba761f19 14181 case R_PPC64_LO_DS_OPT:
95f0d0d2 14182 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
14183 if ((insn & (0x3f << 26)) != 58u << 26)
14184 abort ();
14185 insn += (14u << 26) - (58u << 26);
95f0d0d2 14186 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
14187 r_type = R_PPC64_TOC16_LO;
14188 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14189 break;
14190
411e1bfb
AM
14191 case R_PPC64_TOC16:
14192 case R_PPC64_TOC16_LO:
14193 case R_PPC64_TOC16_DS:
14194 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
14195 {
14196 /* Check for toc tls entries. */
f961d9dd 14197 unsigned char *toc_tls;
951fd09b 14198 int retval;
411e1bfb 14199
3a71aa26
AM
14200 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14201 &local_syms, rel, input_bfd);
951fd09b 14202 if (retval == 0)
411e1bfb
AM
14203 return FALSE;
14204
14205 if (toc_tls)
14206 {
951fd09b 14207 tls_mask = *toc_tls;
411e1bfb
AM
14208 if (r_type == R_PPC64_TOC16_DS
14209 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 14210 {
37da22e5 14211 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
14212 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14213 goto toctprel;
14214 }
411e1bfb 14215 else
951fd09b
AM
14216 {
14217 /* If we found a GD reloc pair, then we might be
14218 doing a GD->IE transition. */
14219 if (retval == 2)
14220 {
14221 tls_gd = TLS_TPRELGD;
37da22e5
AM
14222 if ((tls_mask & TLS_TLS) != 0
14223 && (tls_mask & TLS_GD) == 0)
102890f0 14224 goto tls_ldgd_opt;
951fd09b
AM
14225 }
14226 else if (retval == 3)
14227 {
37da22e5
AM
14228 if ((tls_mask & TLS_TLS) != 0
14229 && (tls_mask & TLS_LD) == 0)
102890f0 14230 goto tls_ldgd_opt;
951fd09b
AM
14231 }
14232 }
411e1bfb
AM
14233 }
14234 }
14235 break;
14236
9d6ded02
AM
14237 case R_PPC64_GOT_TPREL16_HI:
14238 case R_PPC64_GOT_TPREL16_HA:
37da22e5 14239 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
14240 && (tls_mask & TLS_TPREL) == 0)
14241 {
14242 rel->r_offset -= d_offset;
95f0d0d2 14243 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
14244 r_type = R_PPC64_NONE;
14245 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14246 }
14247 break;
14248
411e1bfb
AM
14249 case R_PPC64_GOT_TPREL16_DS:
14250 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 14251 if ((tls_mask & TLS_TLS) != 0
951fd09b 14252 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14253 {
81407a69 14254 toctprel:
95f0d0d2 14255 insn = bfd_get_32 (input_bfd,
c316a17c 14256 contents + rel->r_offset - d_offset);
411e1bfb
AM
14257 insn &= 31 << 21;
14258 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 14259 bfd_put_32 (input_bfd, insn,
c316a17c 14260 contents + rel->r_offset - d_offset);
411e1bfb 14261 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
14262 if (toc_symndx != 0)
14263 {
14264 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14265 rel->r_addend = toc_addend;
0d4792f7
AM
14266 /* We changed the symbol. Start over in order to
14267 get h, sym, sec etc. right. */
c316a17c 14268 goto again;
0d4792f7
AM
14269 }
14270 else
14271 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14272 }
14273 break;
14274
14275 case R_PPC64_TLS:
37da22e5 14276 if ((tls_mask & TLS_TLS) != 0
951fd09b 14277 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14278 {
95f0d0d2 14279 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
14280 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14281 if (insn == 0)
411e1bfb 14282 abort ();
95f0d0d2 14283 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 14284 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
14285 PPC64_TPREL16_LO which is at low-order half-word. */
14286 rel->r_offset += d_offset;
0d4792f7
AM
14287 r_type = R_PPC64_TPREL16_LO;
14288 if (toc_symndx != 0)
14289 {
14290 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14291 rel->r_addend = toc_addend;
0d4792f7
AM
14292 /* We changed the symbol. Start over in order to
14293 get h, sym, sec etc. right. */
c316a17c 14294 goto again;
0d4792f7
AM
14295 }
14296 else
14297 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14298 }
14299 break;
14300
411e1bfb
AM
14301 case R_PPC64_GOT_TLSGD16_HI:
14302 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14303 tls_gd = TLS_TPRELGD;
37da22e5 14304 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
14305 goto tls_gdld_hi;
14306 break;
14307
411e1bfb
AM
14308 case R_PPC64_GOT_TLSLD16_HI:
14309 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 14310 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 14311 {
951fd09b
AM
14312 tls_gdld_hi:
14313 if ((tls_mask & tls_gd) != 0)
14314 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14315 + R_PPC64_GOT_TPREL16_DS);
14316 else
411e1bfb 14317 {
4fe5ca5b 14318 rel->r_offset -= d_offset;
95f0d0d2 14319 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 14320 r_type = R_PPC64_NONE;
411e1bfb 14321 }
951fd09b 14322 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14323 }
14324 break;
14325
951fd09b
AM
14326 case R_PPC64_GOT_TLSGD16:
14327 case R_PPC64_GOT_TLSGD16_LO:
14328 tls_gd = TLS_TPRELGD;
37da22e5 14329 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 14330 goto tls_ldgd_opt;
951fd09b 14331 break;
411e1bfb 14332
951fd09b
AM
14333 case R_PPC64_GOT_TLSLD16:
14334 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 14335 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 14336 {
b9f04fe0 14337 unsigned int insn1, insn2;
102890f0
AM
14338 bfd_vma offset;
14339
14340 tls_ldgd_opt:
727fc41e
AM
14341 offset = (bfd_vma) -1;
14342 /* If not using the newer R_PPC64_TLSGD/LD to mark
14343 __tls_get_addr calls, we must trust that the call
14344 stays with its arg setup insns, ie. that the next
14345 reloc is the __tls_get_addr call associated with
14346 the current reloc. Edit both insns. */
14347 if (input_section->has_tls_get_addr_call
14348 && rel + 1 < relend
14349 && branch_reloc_hash_match (input_bfd, rel + 1,
14350 htab->tls_get_addr,
14351 htab->tls_get_addr_fd))
14352 offset = rel[1].r_offset;
b86ac8e3
AM
14353 /* We read the low GOT_TLS (or TOC16) insn because we
14354 need to keep the destination reg. It may be
14355 something other than the usual r3, and moved to r3
14356 before the call by intervening code. */
95f0d0d2 14357 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 14358 contents + rel->r_offset - d_offset);
102890f0 14359 if ((tls_mask & tls_gd) != 0)
411e1bfb 14360 {
102890f0 14361 /* IE */
b86ac8e3 14362 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
14363 insn1 |= 58 << 26; /* ld */
14364 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 14365 if (offset != (bfd_vma) -1)
f58d5a2d 14366 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
14367 if ((tls_mask & TLS_EXPLICIT) == 0)
14368 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14369 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 14370 else
102890f0
AM
14371 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14372 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14373 }
14374 else
14375 {
14376 /* LE */
b86ac8e3
AM
14377 insn1 &= 0x1f << 21;
14378 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
14379 insn2 = 0x38630000; /* addi 3,3,0 */
14380 if (tls_gd == 0)
951fd09b 14381 {
102890f0 14382 /* Was an LD reloc. */
1d483afe
AM
14383 if (toc_symndx)
14384 sec = local_sections[toc_symndx];
14385 for (r_symndx = 0;
14386 r_symndx < symtab_hdr->sh_info;
14387 r_symndx++)
14388 if (local_sections[r_symndx] == sec)
14389 break;
14390 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14391 r_symndx = STN_UNDEF;
102890f0 14392 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14393 if (r_symndx != STN_UNDEF)
1d483afe
AM
14394 rel->r_addend -= (local_syms[r_symndx].st_value
14395 + sec->output_offset
14396 + sec->output_section->vma);
951fd09b 14397 }
102890f0 14398 else if (toc_symndx != 0)
3a71aa26
AM
14399 {
14400 r_symndx = toc_symndx;
14401 rel->r_addend = toc_addend;
14402 }
102890f0
AM
14403 r_type = R_PPC64_TPREL16_HA;
14404 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14405 if (offset != (bfd_vma) -1)
14406 {
14407 rel[1].r_info = ELF64_R_INFO (r_symndx,
14408 R_PPC64_TPREL16_LO);
14409 rel[1].r_offset = offset + d_offset;
14410 rel[1].r_addend = rel->r_addend;
14411 }
102890f0 14412 }
95f0d0d2 14413 bfd_put_32 (input_bfd, insn1,
3a71aa26 14414 contents + rel->r_offset - d_offset);
727fc41e 14415 if (offset != (bfd_vma) -1)
b9f04fe0 14416 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
14417 if ((tls_mask & tls_gd) == 0
14418 && (tls_gd == 0 || toc_symndx != 0))
14419 {
14420 /* We changed the symbol. Start over in order
14421 to get h, sym, sec etc. right. */
c316a17c 14422 goto again;
727fc41e
AM
14423 }
14424 }
14425 break;
14426
14427 case R_PPC64_TLSGD:
37da22e5 14428 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 14429 && rel + 1 < relend)
727fc41e 14430 {
b9f04fe0 14431 unsigned int insn2;
727fc41e
AM
14432 bfd_vma offset = rel->r_offset;
14433
23cedd1d
AM
14434 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14435 {
14436 bfd_put_32 (output_bfd, NOP, contents + offset);
14437 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14438 break;
14439 }
14440
14441 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14442 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14443
727fc41e
AM
14444 if ((tls_mask & TLS_TPRELGD) != 0)
14445 {
14446 /* IE */
14447 r_type = R_PPC64_NONE;
14448 insn2 = 0x7c636a14; /* add 3,3,13 */
14449 }
14450 else
14451 {
14452 /* LE */
14453 if (toc_symndx != 0)
14454 {
14455 r_symndx = toc_symndx;
14456 rel->r_addend = toc_addend;
14457 }
14458 r_type = R_PPC64_TPREL16_LO;
14459 rel->r_offset = offset + d_offset;
14460 insn2 = 0x38630000; /* addi 3,3,0 */
14461 }
14462 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14463 /* Zap the reloc on the _tls_get_addr call too. */
14464 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14465 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14466 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 14467 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 14468 goto again;
411e1bfb 14469 }
411e1bfb
AM
14470 break;
14471
727fc41e 14472 case R_PPC64_TLSLD:
37da22e5 14473 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 14474 && rel + 1 < relend)
727fc41e 14475 {
b9f04fe0 14476 unsigned int insn2;
727fc41e
AM
14477 bfd_vma offset = rel->r_offset;
14478
23cedd1d
AM
14479 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14480 {
14481 bfd_put_32 (output_bfd, NOP, contents + offset);
14482 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14483 break;
14484 }
14485
14486 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14487 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14488
727fc41e
AM
14489 if (toc_symndx)
14490 sec = local_sections[toc_symndx];
14491 for (r_symndx = 0;
14492 r_symndx < symtab_hdr->sh_info;
14493 r_symndx++)
14494 if (local_sections[r_symndx] == sec)
14495 break;
14496 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14497 r_symndx = STN_UNDEF;
727fc41e 14498 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14499 if (r_symndx != STN_UNDEF)
727fc41e
AM
14500 rel->r_addend -= (local_syms[r_symndx].st_value
14501 + sec->output_offset
14502 + sec->output_section->vma);
14503
14504 r_type = R_PPC64_TPREL16_LO;
14505 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14506 rel->r_offset = offset + d_offset;
14507 /* Zap the reloc on the _tls_get_addr call too. */
14508 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14509 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 14510 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 14511 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 14512 goto again;
727fc41e
AM
14513 }
14514 break;
14515
411e1bfb 14516 case R_PPC64_DTPMOD64:
951fd09b
AM
14517 if (rel + 1 < relend
14518 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14519 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 14520 {
951fd09b
AM
14521 if ((tls_mask & TLS_GD) == 0)
14522 {
14523 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14524 if ((tls_mask & TLS_TPRELGD) != 0)
14525 r_type = R_PPC64_TPREL64;
14526 else
14527 {
4ce794b7 14528 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
14529 r_type = R_PPC64_NONE;
14530 }
14531 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14532 }
14533 }
14534 else
14535 {
14536 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14537 {
4ce794b7 14538 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14539 r_type = R_PPC64_NONE;
951fd09b 14540 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14541 }
411e1bfb
AM
14542 }
14543 break;
14544
14545 case R_PPC64_TPREL64:
951fd09b 14546 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14547 {
14548 r_type = R_PPC64_NONE;
14549 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14550 }
14551 break;
52a82034 14552
006589cf
AM
14553 case R_PPC64_ENTRY:
14554 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14555 if (!bfd_link_pic (info)
14556 && !info->traditional_format
14557 && relocation + 0x80008000 <= 0xffffffff)
14558 {
14559 unsigned int insn1, insn2;
14560
14561 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14562 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14563 if ((insn1 & ~0xfffc) == LD_R2_0R12
14564 && insn2 == ADD_R2_R2_R12)
14565 {
95f0d0d2 14566 bfd_put_32 (input_bfd,
006589cf
AM
14567 LIS_R2 + PPC_HA (relocation),
14568 contents + rel->r_offset);
95f0d0d2 14569 bfd_put_32 (input_bfd,
006589cf
AM
14570 ADDI_R2_R2 + PPC_LO (relocation),
14571 contents + rel->r_offset + 4);
14572 }
14573 }
14574 else
14575 {
14576 relocation -= (rel->r_offset
14577 + input_section->output_offset
14578 + input_section->output_section->vma);
14579 if (relocation + 0x80008000 <= 0xffffffff)
14580 {
14581 unsigned int insn1, insn2;
14582
14583 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14584 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14585 if ((insn1 & ~0xfffc) == LD_R2_0R12
14586 && insn2 == ADD_R2_R2_R12)
14587 {
95f0d0d2 14588 bfd_put_32 (input_bfd,
006589cf
AM
14589 ADDIS_R2_R12 + PPC_HA (relocation),
14590 contents + rel->r_offset);
95f0d0d2 14591 bfd_put_32 (input_bfd,
006589cf
AM
14592 ADDI_R2_R2 + PPC_LO (relocation),
14593 contents + rel->r_offset + 4);
14594 }
14595 }
14596 }
14597 break;
14598
52a82034
AM
14599 case R_PPC64_REL16_HA:
14600 /* If we are generating a non-PIC executable, edit
14601 . 0: addis 2,12,.TOC.-0b@ha
14602 . addi 2,2,.TOC.-0b@l
14603 used by ELFv2 global entry points to set up r2, to
14604 . lis 2,.TOC.@ha
14605 . addi 2,2,.TOC.@l
14606 if .TOC. is in range. */
0e1862bb 14607 if (!bfd_link_pic (info)
810d4e75 14608 && !info->traditional_format
006589cf 14609 && !htab->opd_abi
4f038ee5 14610 && rel->r_addend == d_offset
52a82034
AM
14611 && h != NULL && &h->elf == htab->elf.hgot
14612 && rel + 1 < relend
14613 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14614 && rel[1].r_offset == rel->r_offset + 4
14615 && rel[1].r_addend == rel->r_addend + 4
14616 && relocation + 0x80008000 <= 0xffffffff)
14617 {
14618 unsigned int insn1, insn2;
14619 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14620 insn1 = bfd_get_32 (input_bfd, contents + offset);
14621 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14622 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14623 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14624 {
14625 r_type = R_PPC64_ADDR16_HA;
14626 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14627 rel->r_addend -= d_offset;
14628 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14629 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14630 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14631 }
14632 }
14633 break;
411e1bfb
AM
14634 }
14635
14636 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14637 insn = 0;
b25116a9
AM
14638 max_br_offset = 1 << 25;
14639 addend = rel->r_addend;
bc30df16 14640 reloc_dest = DEST_NORMAL;
65f38f15 14641 switch (r_type)
5bd4f169
AM
14642 {
14643 default:
65f38f15 14644 break;
5bd4f169 14645
3b421ab3
AM
14646 case R_PPC64_TOCSAVE:
14647 if (relocation + addend == (rel->r_offset
14648 + input_section->output_offset
14649 + input_section->output_section->vma)
14650 && tocsave_find (htab, NO_INSERT,
14651 &local_syms, rel, input_bfd))
14652 {
14653 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14654 if (insn == NOP
14655 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14656 bfd_put_32 (input_bfd,
14657 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14658 contents + rel->r_offset);
14659 }
14660 break;
14661
65f38f15
AM
14662 /* Branch taken prediction relocations. */
14663 case R_PPC64_ADDR14_BRTAKEN:
14664 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14665 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14666 /* Fall through. */
65f38f15 14667
86c76c7b 14668 /* Branch not taken prediction relocations. */
65f38f15
AM
14669 case R_PPC64_ADDR14_BRNTAKEN:
14670 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14671 insn |= bfd_get_32 (input_bfd,
411e1bfb 14672 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14673 /* Fall through. */
86c76c7b 14674
b25116a9
AM
14675 case R_PPC64_REL14:
14676 max_br_offset = 1 << 15;
1a0670f3 14677 /* Fall through. */
5bd4f169 14678
65f38f15 14679 case R_PPC64_REL24:
23cedd1d 14680 case R_PPC64_PLTCALL:
ad8e1ba5
AM
14681 /* Calls to functions with a different TOC, such as calls to
14682 shared objects, need to alter the TOC pointer. This is
14683 done using a linkage stub. A REL24 branching to these
14684 linkage stubs needs to be followed by a nop, as the nop
14685 will be replaced with an instruction to restore the TOC
14686 base pointer. */
8387904d 14687 fdh = h;
b31867b6
AM
14688 if (h != NULL
14689 && h->oh != NULL
14690 && h->oh->is_func_descriptor)
14691 fdh = ppc_follow_link (h->oh);
31c76678
DK
14692 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14693 htab);
23cedd1d
AM
14694 if (r_type == R_PPC64_PLTCALL
14695 && stub_entry != NULL
14696 && (stub_entry->stub_type == ppc_stub_plt_call
14697 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14698 stub_entry = NULL;
14699
6abec6bc 14700 if (stub_entry != NULL
ad8e1ba5 14701 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 14702 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
14703 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14704 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 14705 {
b25116a9 14706 bfd_boolean can_plt_call = FALSE;
721956f4 14707
6e1816be
AM
14708 if (stub_entry->stub_type == ppc_stub_plt_call
14709 && !htab->opd_abi
14710 && htab->params->plt_localentry0 != 0
14711 && is_elfv2_localentry0 (&h->elf))
14712 {
14713 /* The function doesn't use or change r2. */
14714 can_plt_call = TRUE;
14715 }
14716
f378ab09 14717 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
14718 branch and link followed by a nop. The nop is
14719 replaced by an insn to restore r2. */
6e1816be 14720 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14721 {
ba8ca3e7
AM
14722 unsigned long br;
14723
14724 br = bfd_get_32 (input_bfd,
14725 contents + rel->r_offset);
14726 if ((br & 1) != 0)
41bd81ab 14727 {
ba8ca3e7
AM
14728 unsigned long nop;
14729
14730 nop = bfd_get_32 (input_bfd,
14731 contents + rel->r_offset + 4);
23cedd1d
AM
14732 if (nop == LD_R2_0R1 + STK_TOC (htab))
14733 can_plt_call = TRUE;
14734 else if (nop == NOP
14735 || nop == CROR_151515
14736 || nop == CROR_313131)
a7f2871e 14737 {
ba8ca3e7
AM
14738 if (h != NULL
14739 && (h == htab->tls_get_addr_fd
14740 || h == htab->tls_get_addr)
7c9cf415 14741 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14742 {
14743 /* Special stub used, leave nop alone. */
14744 }
14745 else
a078d95a
AM
14746 bfd_put_32 (input_bfd,
14747 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14748 contents + rel->r_offset + 4);
14749 can_plt_call = TRUE;
a7f2871e 14750 }
41bd81ab 14751 }
5bd4f169 14752 }
721956f4 14753
ba8ca3e7 14754 if (!can_plt_call && h != NULL)
721956f4 14755 {
ba8ca3e7
AM
14756 const char *name = h->elf.root.root.string;
14757
14758 if (*name == '.')
14759 ++name;
14760
14761 if (strncmp (name, "__libc_start_main", 17) == 0
14762 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14763 {
ba8ca3e7
AM
14764 /* Allow crt1 branch to go via a toc adjusting
14765 stub. Other calls that never return could do
14766 the same, if we could detect such. */
b25116a9 14767 can_plt_call = TRUE;
6ab189d5 14768 }
ba8ca3e7
AM
14769 }
14770
14771 if (!can_plt_call)
14772 {
14773 /* g++ as of 20130507 emits self-calls without a
14774 following nop. This is arguably wrong since we
14775 have conflicting information. On the one hand a
14776 global symbol and on the other a local call
14777 sequence, but don't error for this special case.
14778 It isn't possible to cheaply verify we have
14779 exactly such a call. Allow all calls to the same
14780 section. */
14781 asection *code_sec = sec;
14782
14783 if (get_opd_info (sec) != NULL)
ad8e1ba5 14784 {
ba8ca3e7
AM
14785 bfd_vma off = (relocation + addend
14786 - sec->output_section->vma
14787 - sec->output_offset);
bc30df16 14788
ba8ca3e7 14789 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14790 }
ba8ca3e7
AM
14791 if (code_sec == input_section)
14792 can_plt_call = TRUE;
14793 }
14794
14795 if (!can_plt_call)
14796 {
4805fc55
AM
14797 if (stub_entry->stub_type == ppc_stub_plt_call
14798 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14799 info->callbacks->einfo
695344c0 14800 /* xgettext:c-format */
c1c8c1ef 14801 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
14802 "recompile with -fPIC\n"),
14803 input_bfd, input_section, rel->r_offset, sym_name);
14804 else
14805 info->callbacks->einfo
695344c0 14806 /* xgettext:c-format */
c1c8c1ef 14807 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
14808 "(-mcmodel=small toc adjust stub)\n"),
14809 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14810
14811 bfd_set_error (bfd_error_bad_value);
14812 ret = FALSE;
721956f4
AM
14813 }
14814
b25116a9 14815 if (can_plt_call
794e51c0
AM
14816 && (stub_entry->stub_type == ppc_stub_plt_call
14817 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14818 unresolved_reloc = FALSE;
14819 }
14820
6abec6bc
AM
14821 if ((stub_entry == NULL
14822 || stub_entry->stub_type == ppc_stub_long_branch
14823 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14824 && get_opd_info (sec) != NULL)
14825 {
14826 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14827 bfd_vma off = (relocation + addend
14828 - sec->output_section->vma
14829 - sec->output_offset);
aef36ac1 14830 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14831 if (dest != (bfd_vma) -1)
14832 {
14833 relocation = dest;
14834 addend = 0;
bc30df16 14835 reloc_dest = DEST_OPD;
8387904d
AM
14836 }
14837 }
14838
b25116a9
AM
14839 /* If the branch is out of reach we ought to have a long
14840 branch stub. */
14841 from = (rel->r_offset
14842 + input_section->output_offset
14843 + input_section->output_section->vma);
14844
6911b7dc
AM
14845 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14846 ? fdh->elf.other
14847 : sym->st_other);
14848
6abec6bc
AM
14849 if (stub_entry != NULL
14850 && (stub_entry->stub_type == ppc_stub_long_branch
14851 || stub_entry->stub_type == ppc_stub_plt_branch)
14852 && (r_type == R_PPC64_ADDR14_BRTAKEN
14853 || r_type == R_PPC64_ADDR14_BRNTAKEN
14854 || (relocation + addend - from + max_br_offset
14855 < 2 * max_br_offset)))
14856 /* Don't use the stub if this branch is in range. */
14857 stub_entry = NULL;
b25116a9
AM
14858
14859 if (stub_entry != NULL)
14860 {
14861 /* Munge up the value and addend so that we call the stub
14862 rather than the procedure directly. */
a4b6fadd
AM
14863 asection *stub_sec = stub_entry->group->stub_sec;
14864
14865 if (stub_entry->stub_type == ppc_stub_save_res)
14866 relocation += (stub_sec->output_offset
14867 + stub_sec->output_section->vma
14868 + stub_sec->size - htab->sfpr->size
14869 - htab->sfpr->output_offset
14870 - htab->sfpr->output_section->vma);
14871 else
14872 relocation = (stub_entry->stub_offset
14873 + stub_sec->output_offset
14874 + stub_sec->output_section->vma);
b25116a9 14875 addend = 0;
bc30df16 14876 reloc_dest = DEST_STUB;
3b421ab3 14877
954b63d4 14878 if ((stub_entry->stub_type == ppc_stub_plt_call
794e51c0
AM
14879 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14880 && (ALWAYS_EMIT_R2SAVE
14881 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14882 && rel + 1 < relend
14883 && rel[1].r_offset == rel->r_offset + 4
14884 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14885 relocation += 4;
b25116a9
AM
14886 }
14887
14888 if (insn != 0)
14889 {
794e51c0 14890 if (is_isa_v2)
721956f4 14891 {
b25116a9
AM
14892 /* Set 'a' bit. This is 0b00010 in BO field for branch
14893 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14894 for branch on CTR insns (BO == 1a00t or 1a01t). */
14895 if ((insn & (0x14 << 21)) == (0x04 << 21))
14896 insn |= 0x02 << 21;
14897 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14898 insn |= 0x08 << 21;
14899 else
14900 break;
14901 }
14902 else
14903 {
14904 /* Invert 'y' bit if not the default. */
4cc603a5 14905 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14906 insn ^= 0x01 << 21;
721956f4 14907 }
b25116a9 14908
95f0d0d2 14909 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14910 }
e86ce104 14911
06da1e8e
AM
14912 /* NOP out calls to undefined weak functions.
14913 We can thus call a weak function without first
14914 checking whether the function is defined. */
b25116a9 14915 else if (h != NULL
039b3fef 14916 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14917 && h->elf.dynindx == -1
b25116a9
AM
14918 && r_type == R_PPC64_REL24
14919 && relocation == 0
4cc603a5 14920 && addend == 0)
e86ce104 14921 {
95f0d0d2 14922 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14923 goto copy_reloc;
e86ce104 14924 }
65f38f15
AM
14925 break;
14926 }
5bd4f169 14927
65f38f15 14928 /* Set `addend'. */
411e1bfb 14929 tls_type = 0;
23cedd1d 14930 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
14931 switch (r_type)
14932 {
14933 default:
cf97bcb0
AM
14934 /* xgettext:c-format */
14935 _bfd_error_handler (_("%pB: %s unsupported"),
14936 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 14937
65f38f15 14938 bfd_set_error (bfd_error_bad_value);
b34976b6 14939 ret = FALSE;
c316a17c 14940 goto copy_reloc;
5bd4f169 14941
65f38f15 14942 case R_PPC64_NONE:
411e1bfb 14943 case R_PPC64_TLS:
727fc41e
AM
14944 case R_PPC64_TLSGD:
14945 case R_PPC64_TLSLD:
3b421ab3 14946 case R_PPC64_TOCSAVE:
04c9666a
AM
14947 case R_PPC64_GNU_VTINHERIT:
14948 case R_PPC64_GNU_VTENTRY:
006589cf 14949 case R_PPC64_ENTRY:
c316a17c 14950 goto copy_reloc;
5bd4f169
AM
14951
14952 /* GOT16 relocations. Like an ADDR16 using the symbol's
14953 address in the GOT as relocation value instead of the
411e1bfb 14954 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14955 symbol and put the symbol value there. */
411e1bfb
AM
14956 case R_PPC64_GOT_TLSGD16:
14957 case R_PPC64_GOT_TLSGD16_LO:
14958 case R_PPC64_GOT_TLSGD16_HI:
14959 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14960 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14961 goto dogot;
14962
14963 case R_PPC64_GOT_TLSLD16:
14964 case R_PPC64_GOT_TLSLD16_LO:
14965 case R_PPC64_GOT_TLSLD16_HI:
14966 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14967 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14968 goto dogot;
14969
14970 case R_PPC64_GOT_TPREL16_DS:
14971 case R_PPC64_GOT_TPREL16_LO_DS:
14972 case R_PPC64_GOT_TPREL16_HI:
14973 case R_PPC64_GOT_TPREL16_HA:
14974 tls_type = TLS_TLS | TLS_TPREL;
14975 goto dogot;
14976
14977 case R_PPC64_GOT_DTPREL16_DS:
14978 case R_PPC64_GOT_DTPREL16_LO_DS:
14979 case R_PPC64_GOT_DTPREL16_HI:
14980 case R_PPC64_GOT_DTPREL16_HA:
14981 tls_type = TLS_TLS | TLS_DTPREL;
14982 goto dogot;
14983
65f38f15
AM
14984 case R_PPC64_GOT16:
14985 case R_PPC64_GOT16_LO:
14986 case R_PPC64_GOT16_HI:
14987 case R_PPC64_GOT16_HA:
14988 case R_PPC64_GOT16_DS:
14989 case R_PPC64_GOT16_LO_DS:
411e1bfb 14990 dogot:
5bd4f169
AM
14991 {
14992 /* Relocation is to the entry for this symbol in the global
14993 offset table. */
e717da7e 14994 asection *got;
d881513a 14995 bfd_vma *offp;
5bd4f169 14996 bfd_vma off;
d881513a 14997 unsigned long indx = 0;
927be08e 14998 struct got_entry *ent;
65f38f15 14999
d881513a
AM
15000 if (tls_type == (TLS_TLS | TLS_LD)
15001 && (h == NULL
f5385ebf 15002 || !h->elf.def_dynamic))
927be08e 15003 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 15004 else
5bd4f169 15005 {
d881513a
AM
15006 if (h != NULL)
15007 {
f0158f44
AM
15008 if (!htab->elf.dynamic_sections_created
15009 || h->elf.dynindx == -1
15010 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 15011 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
15012 /* This is actually a static link, or it is a
15013 -Bsymbolic link and the symbol is defined
15014 locally, or the symbol was forced to be local
15015 because of a version file. */
15016 ;
15017 else
15018 {
039b3fef 15019 indx = h->elf.dynindx;
d881513a
AM
15020 unresolved_reloc = FALSE;
15021 }
039b3fef 15022 ent = h->elf.got.glist;
d881513a 15023 }
411e1bfb 15024 else
5bd4f169 15025 {
d881513a
AM
15026 if (local_got_ents == NULL)
15027 abort ();
15028 ent = local_got_ents[r_symndx];
5bd4f169 15029 }
d881513a
AM
15030
15031 for (; ent != NULL; ent = ent->next)
31c76678 15032 if (ent->addend == orig_rel.r_addend
e717da7e 15033 && ent->owner == input_bfd
d881513a
AM
15034 && ent->tls_type == tls_type)
15035 break;
5bd4f169 15036 }
411e1bfb 15037
927be08e
AM
15038 if (ent == NULL)
15039 abort ();
15040 if (ent->is_indirect)
15041 ent = ent->got.ent;
15042 offp = &ent->got.offset;
15043 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
15044 if (got == NULL)
15045 abort ();
15046
411e1bfb
AM
15047 /* The offset must always be a multiple of 8. We use the
15048 least significant bit to record whether we have already
15049 processed this entry. */
d881513a 15050 off = *offp;
411e1bfb
AM
15051 if ((off & 1) != 0)
15052 off &= ~1;
5bd4f169
AM
15053 else
15054 {
411e1bfb
AM
15055 /* Generate relocs for the dynamic linker, except in
15056 the case of TLSLD where we'll use one entry per
15057 module. */
25f23106
AM
15058 asection *relgot;
15059 bfd_boolean ifunc;
e717da7e 15060
d881513a 15061 *offp = off | 1;
25f23106
AM
15062 relgot = NULL;
15063 ifunc = (h != NULL
15064 ? h->elf.type == STT_GNU_IFUNC
15065 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 15066 if (ifunc)
82e66161
AM
15067 {
15068 relgot = htab->elf.irelplt;
15069 if (indx == 0)
15070 htab->local_ifunc_resolver = 1;
15071 else if (is_static_defined (&h->elf))
15072 htab->maybe_local_ifunc_resolver = 1;
15073 }
f0158f44
AM
15074 else if (indx != 0
15075 || (bfd_link_pic (info)
15076 && (h == NULL
21d68fcd 15077 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 15078 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
15079 && !h->elf.def_dynamic))
15080 && !(tls_type == (TLS_TLS | TLS_TPREL)
15081 && bfd_link_executable (info)
15082 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 15083 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 15084 if (relgot != NULL)
5bd4f169 15085 {
e717da7e
AM
15086 outrel.r_offset = (got->output_section->vma
15087 + got->output_offset
411e1bfb 15088 + off);
4cc603a5 15089 outrel.r_addend = addend;
d881513a 15090 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 15091 {
411e1bfb 15092 outrel.r_addend = 0;
e515b051 15093 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
15094 if (tls_type == (TLS_TLS | TLS_GD))
15095 {
e717da7e
AM
15096 loc = relgot->contents;
15097 loc += (relgot->reloc_count++
d881513a
AM
15098 * sizeof (Elf64_External_Rela));
15099 bfd_elf64_swap_reloca_out (output_bfd,
15100 &outrel, loc);
e515b051 15101 outrel.r_offset += 8;
4cc603a5 15102 outrel.r_addend = addend;
d881513a
AM
15103 outrel.r_info
15104 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 15105 }
411e1bfb 15106 }
951fd09b 15107 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 15108 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 15109 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 15110 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
15111 else if (indx != 0)
15112 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15113 else
81407a69 15114 {
25f23106
AM
15115 if (ifunc)
15116 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15117 else
15118 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
15119
15120 /* Write the .got section contents for the sake
15121 of prelink. */
e717da7e 15122 loc = got->contents + off;
23fbd6fa
JJ
15123 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15124 loc);
81407a69 15125 }
81407a69
AM
15126
15127 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
15128 {
15129 outrel.r_addend += relocation;
15130 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
15131 {
15132 if (htab->elf.tls_sec == NULL)
15133 outrel.r_addend = 0;
15134 else
15135 outrel.r_addend -= htab->elf.tls_sec->vma;
15136 }
e515b051 15137 }
e717da7e
AM
15138 loc = relgot->contents;
15139 loc += (relgot->reloc_count++
411e1bfb
AM
15140 * sizeof (Elf64_External_Rela));
15141 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15142 }
15143
ad8e1ba5 15144 /* Init the .got section contents here if we're not
81407a69 15145 emitting a reloc. */
d881513a 15146 else
411e1bfb 15147 {
4cc603a5 15148 relocation += addend;
f0158f44 15149 if (tls_type != 0)
411e1bfb 15150 {
989f9879
AM
15151 if (htab->elf.tls_sec == NULL)
15152 relocation = 0;
15153 else
15154 {
f0158f44
AM
15155 if (tls_type & TLS_LD)
15156 relocation = 0;
15157 else
15158 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 15159 if (tls_type & TLS_TPREL)
989f9879
AM
15160 relocation += DTP_OFFSET - TP_OFFSET;
15161 }
5bd4f169 15162
f0158f44 15163 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
15164 {
15165 bfd_put_64 (output_bfd, relocation,
e717da7e 15166 got->contents + off + 8);
676ee2b5 15167 relocation = 1;
7b609f53 15168 }
411e1bfb
AM
15169 }
15170 bfd_put_64 (output_bfd, relocation,
e717da7e 15171 got->contents + off);
5bd4f169
AM
15172 }
15173 }
15174
65f38f15
AM
15175 if (off >= (bfd_vma) -2)
15176 abort ();
15177
bf102f86 15178 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 15179 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 15180 }
65f38f15
AM
15181 break;
15182
15183 case R_PPC64_PLT16_HA:
15184 case R_PPC64_PLT16_HI:
15185 case R_PPC64_PLT16_LO:
08be3224 15186 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
15187 case R_PPC64_PLT32:
15188 case R_PPC64_PLT64:
23cedd1d
AM
15189 case R_PPC64_PLTSEQ:
15190 case R_PPC64_PLTCALL:
65f38f15
AM
15191 /* Relocation is to the entry for this symbol in the
15192 procedure linkage table. */
23cedd1d 15193 unresolved_reloc = TRUE;
cbf95972
AM
15194 {
15195 struct plt_entry **plt_list = NULL;
15196 if (h != NULL)
15197 plt_list = &h->elf.plt.plist;
15198 else if (local_got_ents != NULL)
15199 {
15200 struct plt_entry **local_plt = (struct plt_entry **)
15201 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 15202 plt_list = local_plt + r_symndx;
cbf95972
AM
15203 }
15204 if (plt_list)
15205 {
15206 struct plt_entry *ent;
65f38f15 15207
cbf95972
AM
15208 for (ent = *plt_list; ent != NULL; ent = ent->next)
15209 if (ent->plt.offset != (bfd_vma) -1
15210 && ent->addend == orig_rel.r_addend)
15211 {
15212 asection *plt;
08be3224 15213 bfd_vma got;
cbf95972
AM
15214
15215 plt = htab->elf.splt;
15216 if (!htab->elf.dynamic_sections_created
15217 || h == NULL
15218 || h->elf.dynindx == -1)
2d7ad24e
AM
15219 {
15220 if (h != NULL
15221 ? h->elf.type == STT_GNU_IFUNC
15222 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15223 plt = htab->elf.iplt;
15224 else
15225 plt = htab->pltlocal;
15226 }
15227 relocation = (plt->output_section->vma
15228 + plt->output_offset
15229 + ent->plt.offset);
08be3224
AM
15230 if (r_type == R_PPC64_PLT16_HA
15231 || r_type ==R_PPC64_PLT16_HI
15232 || r_type ==R_PPC64_PLT16_LO
15233 || r_type ==R_PPC64_PLT16_LO_DS)
15234 {
15235 got = (elf_gp (output_bfd)
15236 + htab->sec_info[input_section->id].toc_off);
15237 relocation -= got;
15238 }
cbf95972
AM
15239 addend = 0;
15240 unresolved_reloc = FALSE;
15241 break;
15242 }
15243 }
15244 }
65f38f15 15245 break;
5bd4f169 15246
0b13192e
AM
15247 case R_PPC64_TOC:
15248 /* Relocation value is TOC base. */
15249 relocation = TOCstart;
cf35638d 15250 if (r_symndx == STN_UNDEF)
6f20ed8a 15251 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
15252 else if (unresolved_reloc)
15253 ;
6f20ed8a
AM
15254 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15255 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
15256 else
15257 unresolved_reloc = TRUE;
ab96bf03 15258 goto dodyn;
0b13192e 15259
5bd4f169
AM
15260 /* TOC16 relocs. We want the offset relative to the TOC base,
15261 which is the address of the start of the TOC plus 0x8000.
15262 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15263 in this order. */
65f38f15
AM
15264 case R_PPC64_TOC16:
15265 case R_PPC64_TOC16_LO:
15266 case R_PPC64_TOC16_HI:
15267 case R_PPC64_TOC16_DS:
15268 case R_PPC64_TOC16_LO_DS:
15269 case R_PPC64_TOC16_HA:
6f20ed8a 15270 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
15271 break;
15272
15273 /* Relocate against the beginning of the section. */
65f38f15
AM
15274 case R_PPC64_SECTOFF:
15275 case R_PPC64_SECTOFF_LO:
15276 case R_PPC64_SECTOFF_HI:
15277 case R_PPC64_SECTOFF_DS:
15278 case R_PPC64_SECTOFF_LO_DS:
15279 case R_PPC64_SECTOFF_HA:
4ce794b7 15280 if (sec != NULL)
65f38f15 15281 addend -= sec->output_section->vma;
5bd4f169
AM
15282 break;
15283
25f23106
AM
15284 case R_PPC64_REL16:
15285 case R_PPC64_REL16_LO:
15286 case R_PPC64_REL16_HI:
15287 case R_PPC64_REL16_HA:
a680de9a 15288 case R_PPC64_REL16DX_HA:
25f23106
AM
15289 break;
15290
721956f4
AM
15291 case R_PPC64_REL14:
15292 case R_PPC64_REL14_BRNTAKEN:
15293 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
15294 case R_PPC64_REL24:
15295 break;
15296
411e1bfb
AM
15297 case R_PPC64_TPREL16:
15298 case R_PPC64_TPREL16_LO:
15299 case R_PPC64_TPREL16_HI:
15300 case R_PPC64_TPREL16_HA:
15301 case R_PPC64_TPREL16_DS:
15302 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
15303 case R_PPC64_TPREL16_HIGH:
15304 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
15305 case R_PPC64_TPREL16_HIGHER:
15306 case R_PPC64_TPREL16_HIGHERA:
15307 case R_PPC64_TPREL16_HIGHEST:
15308 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
15309 if (h != NULL
15310 && h->elf.root.type == bfd_link_hash_undefweak
15311 && h->elf.dynindx == -1)
15312 {
15313 /* Make this relocation against an undefined weak symbol
15314 resolve to zero. This is really just a tweak, since
15315 code using weak externs ought to check that they are
15316 defined before using them. */
15317 bfd_byte *p = contents + rel->r_offset - d_offset;
15318
95f0d0d2 15319 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
15320 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15321 if (insn != 0)
95f0d0d2 15322 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
15323 break;
15324 }
989f9879
AM
15325 if (htab->elf.tls_sec != NULL)
15326 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
15327 /* The TPREL16 relocs shouldn't really be used in shared
15328 libs or with non-local symbols as that will result in
15329 DT_TEXTREL being set, but support them anyway. */
15330 goto dodyn;
411e1bfb
AM
15331
15332 case R_PPC64_DTPREL16:
15333 case R_PPC64_DTPREL16_LO:
15334 case R_PPC64_DTPREL16_HI:
15335 case R_PPC64_DTPREL16_HA:
15336 case R_PPC64_DTPREL16_DS:
15337 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
15338 case R_PPC64_DTPREL16_HIGH:
15339 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
15340 case R_PPC64_DTPREL16_HIGHER:
15341 case R_PPC64_DTPREL16_HIGHERA:
15342 case R_PPC64_DTPREL16_HIGHEST:
15343 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
15344 if (htab->elf.tls_sec != NULL)
15345 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
15346 break;
15347
45965137
AM
15348 case R_PPC64_ADDR64_LOCAL:
15349 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15350 ? h->elf.other
15351 : sym->st_other);
15352 break;
15353
e515b051
AM
15354 case R_PPC64_DTPMOD64:
15355 relocation = 1;
15356 addend = 0;
15357 goto dodyn;
15358
411e1bfb 15359 case R_PPC64_TPREL64:
989f9879
AM
15360 if (htab->elf.tls_sec != NULL)
15361 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
15362 goto dodyn;
15363
15364 case R_PPC64_DTPREL64:
989f9879
AM
15365 if (htab->elf.tls_sec != NULL)
15366 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 15367 /* Fall through. */
411e1bfb 15368
65f38f15
AM
15369 /* Relocations that may need to be propagated if this is a
15370 dynamic object. */
04c9666a 15371 case R_PPC64_REL30:
65f38f15
AM
15372 case R_PPC64_REL32:
15373 case R_PPC64_REL64:
15374 case R_PPC64_ADDR14:
15375 case R_PPC64_ADDR14_BRNTAKEN:
15376 case R_PPC64_ADDR14_BRTAKEN:
15377 case R_PPC64_ADDR16:
15378 case R_PPC64_ADDR16_DS:
15379 case R_PPC64_ADDR16_HA:
15380 case R_PPC64_ADDR16_HI:
f9c6b907
AM
15381 case R_PPC64_ADDR16_HIGH:
15382 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15383 case R_PPC64_ADDR16_HIGHER:
15384 case R_PPC64_ADDR16_HIGHERA:
15385 case R_PPC64_ADDR16_HIGHEST:
15386 case R_PPC64_ADDR16_HIGHESTA:
15387 case R_PPC64_ADDR16_LO:
15388 case R_PPC64_ADDR16_LO_DS:
15389 case R_PPC64_ADDR24:
65f38f15
AM
15390 case R_PPC64_ADDR32:
15391 case R_PPC64_ADDR64:
15392 case R_PPC64_UADDR16:
15393 case R_PPC64_UADDR32:
15394 case R_PPC64_UADDR64:
411e1bfb 15395 dodyn:
5d1634d7 15396 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
15397 break;
15398
41bd81ab
AM
15399 if (NO_OPD_RELOCS && is_opd)
15400 break;
15401
8a9e8e72 15402 if (bfd_link_pic (info)
b1b07054
AM
15403 ? ((h == NULL
15404 || h->dyn_relocs != NULL)
15405 && ((h != NULL && pc_dynrelocs (h))
15406 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
15407 : (h != NULL
15408 ? h->dyn_relocs != NULL
d311bc8b 15409 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 15410 {
b34976b6 15411 bfd_boolean skip, relocate;
65f38f15 15412 asection *sreloc;
1cf1f670 15413 bfd_vma out_off;
82e66161 15414 long indx = 0;
65f38f15
AM
15415
15416 /* When generating a dynamic object, these relocations
15417 are copied into the output file to be resolved at run
15418 time. */
15419
b34976b6
AM
15420 skip = FALSE;
15421 relocate = FALSE;
65f38f15 15422
1cf1f670
AM
15423 out_off = _bfd_elf_section_offset (output_bfd, info,
15424 input_section, rel->r_offset);
15425 if (out_off == (bfd_vma) -1)
b34976b6 15426 skip = TRUE;
1cf1f670 15427 else if (out_off == (bfd_vma) -2)
b34976b6 15428 skip = TRUE, relocate = TRUE;
1cf1f670
AM
15429 out_off += (input_section->output_section->vma
15430 + input_section->output_offset);
15431 outrel.r_offset = out_off;
411e1bfb 15432 outrel.r_addend = rel->r_addend;
65f38f15 15433
1cf1f670
AM
15434 /* Optimize unaligned reloc use. */
15435 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15436 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15437 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15438 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15439 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15440 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15441 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15442 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15443 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15444
65f38f15 15445 if (skip)
0bb2d96a 15446 memset (&outrel, 0, sizeof outrel);
afe397ea 15447 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
15448 && !is_opd
15449 && r_type != R_PPC64_TOC)
14acf4dc 15450 {
82e66161
AM
15451 indx = h->elf.dynindx;
15452 BFD_ASSERT (indx != -1);
15453 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 15454 }
65f38f15
AM
15455 else
15456 {
41bd81ab
AM
15457 /* This symbol is local, or marked to become local,
15458 or this is an opd section reloc which must point
15459 at a local function. */
65f38f15 15460 outrel.r_addend += relocation;
e86ce104 15461 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 15462 {
3fad3c7c 15463 if (is_opd && h != NULL)
afbe61cf
AM
15464 {
15465 /* Lie about opd entries. This case occurs
15466 when building shared libraries and we
15467 reference a function in another shared
3fad3c7c
AM
15468 lib. The same thing happens for a weak
15469 definition in an application that's
15470 overridden by a strong definition in a
15471 shared lib. (I believe this is a generic
15472 bug in binutils handling of weak syms.)
15473 In these cases we won't use the opd
1e2f5b6e 15474 entry in this lib. */
b34976b6 15475 unresolved_reloc = FALSE;
afbe61cf 15476 }
25f23106
AM
15477 if (!is_opd
15478 && r_type == R_PPC64_ADDR64
15479 && (h != NULL
15480 ? h->elf.type == STT_GNU_IFUNC
15481 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15482 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15483 else
15484 {
15485 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 15486
25f23106
AM
15487 /* We need to relocate .opd contents for ld.so.
15488 Prelink also wants simple and consistent rules
15489 for relocs. This make all RELATIVE relocs have
15490 *r_offset equal to r_addend. */
15491 relocate = TRUE;
15492 }
65f38f15
AM
15493 }
15494 else
15495 {
25f23106
AM
15496 if (h != NULL
15497 ? h->elf.type == STT_GNU_IFUNC
15498 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15499 {
25f53a85 15500 info->callbacks->einfo
695344c0 15501 /* xgettext:c-format */
174d0a74 15502 (_("%H: %s for indirect "
c1c8c1ef 15503 "function `%pT' unsupported\n"),
25f53a85 15504 input_bfd, input_section, rel->r_offset,
25f23106
AM
15505 ppc64_elf_howto_table[r_type]->name,
15506 sym_name);
15507 ret = FALSE;
15508 }
cf35638d 15509 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
15510 ;
15511 else if (sec == NULL || sec->owner == NULL)
15512 {
15513 bfd_set_error (bfd_error_bad_value);
b34976b6 15514 return FALSE;
65f38f15
AM
15515 }
15516 else
15517 {
15518 asection *osec;
15519
15520 osec = sec->output_section;
15521 indx = elf_section_data (osec)->dynindx;
15522
74541ad4
AM
15523 if (indx == 0)
15524 {
15525 if ((osec->flags & SEC_READONLY) == 0
15526 && htab->elf.data_index_section != NULL)
15527 osec = htab->elf.data_index_section;
15528 else
15529 osec = htab->elf.text_index_section;
15530 indx = elf_section_data (osec)->dynindx;
15531 }
15532 BFD_ASSERT (indx != 0);
15533
65f38f15
AM
15534 /* We are turning this relocation into one
15535 against a section symbol, so subtract out
15536 the output section's address but not the
15537 offset of the input section in the output
15538 section. */
15539 outrel.r_addend -= osec->vma;
15540 }
15541
15542 outrel.r_info = ELF64_R_INFO (indx, r_type);
15543 }
15544 }
15545
15546 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
15547 if (h != NULL
15548 ? h->elf.type == STT_GNU_IFUNC
15549 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
15550 {
15551 sreloc = htab->elf.irelplt;
15552 if (indx == 0)
15553 htab->local_ifunc_resolver = 1;
15554 else if (is_static_defined (&h->elf))
15555 htab->maybe_local_ifunc_resolver = 1;
15556 }
65f38f15
AM
15557 if (sreloc == NULL)
15558 abort ();
15559
dfbb6ac9
AM
15560 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15561 >= sreloc->size)
15562 abort ();
947216bf
AM
15563 loc = sreloc->contents;
15564 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
15565 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15566
15567 /* If this reloc is against an external symbol, it will
15568 be computed at runtime, so there's no need to do
81407a69
AM
15569 anything now. However, for the sake of prelink ensure
15570 that the section contents are a known value. */
65f38f15 15571 if (! relocate)
81407a69
AM
15572 {
15573 unresolved_reloc = FALSE;
15574 /* The value chosen here is quite arbitrary as ld.so
15575 ignores section contents except for the special
15576 case of .opd where the contents might be accessed
15577 before relocation. Choose zero, as that won't
15578 cause reloc overflow. */
15579 relocation = 0;
15580 addend = 0;
15581 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15582 to improve backward compatibility with older
15583 versions of ld. */
15584 if (r_type == R_PPC64_ADDR64)
15585 addend = outrel.r_addend;
15586 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 15587 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 15588 addend = outrel.r_offset;
81407a69 15589 }
65f38f15 15590 }
5bd4f169
AM
15591 break;
15592
65f38f15
AM
15593 case R_PPC64_COPY:
15594 case R_PPC64_GLOB_DAT:
15595 case R_PPC64_JMP_SLOT:
25f23106 15596 case R_PPC64_JMP_IREL:
65f38f15
AM
15597 case R_PPC64_RELATIVE:
15598 /* We shouldn't ever see these dynamic relocs in relocatable
15599 files. */
ae9a127f 15600 /* Fall through. */
65f38f15
AM
15601
15602 case R_PPC64_PLTGOT16:
15603 case R_PPC64_PLTGOT16_DS:
15604 case R_PPC64_PLTGOT16_HA:
15605 case R_PPC64_PLTGOT16_HI:
15606 case R_PPC64_PLTGOT16_LO:
15607 case R_PPC64_PLTGOT16_LO_DS:
15608 case R_PPC64_PLTREL32:
15609 case R_PPC64_PLTREL64:
15610 /* These ones haven't been implemented yet. */
15611
25f53a85 15612 info->callbacks->einfo
695344c0 15613 /* xgettext:c-format */
c1c8c1ef 15614 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 15615 input_bfd,
4ce794b7 15616 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
15617
15618 bfd_set_error (bfd_error_invalid_operation);
b34976b6 15619 ret = FALSE;
c316a17c 15620 goto copy_reloc;
65f38f15 15621 }
5bd4f169 15622
67f0cbdb
AM
15623 /* Multi-instruction sequences that access the TOC can be
15624 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 15625 to nop; addi rb,r2,x; */
67f0cbdb
AM
15626 switch (r_type)
15627 {
15628 default:
15629 break;
15630
15631 case R_PPC64_GOT_TLSLD16_HI:
15632 case R_PPC64_GOT_TLSGD16_HI:
15633 case R_PPC64_GOT_TPREL16_HI:
15634 case R_PPC64_GOT_DTPREL16_HI:
15635 case R_PPC64_GOT16_HI:
15636 case R_PPC64_TOC16_HI:
15637 /* These relocs would only be useful if building up an
15638 offset to later add to r2, perhaps in an indexed
15639 addressing mode instruction. Don't try to optimize.
15640 Unfortunately, the possibility of someone building up an
15641 offset like this or even with the HA relocs, means that
15642 we need to check the high insn when optimizing the low
15643 insn. */
15644 break;
15645
23cedd1d
AM
15646 case R_PPC64_PLTCALL:
15647 if (unresolved_reloc)
15648 {
15649 /* No plt entry. Make this into a direct call. */
15650 bfd_byte *p = contents + rel->r_offset;
15651 insn = bfd_get_32 (input_bfd, p);
15652 insn &= 1;
15653 bfd_put_32 (input_bfd, B_DOT | insn, p);
15654 bfd_put_32 (input_bfd, NOP, p + 4);
15655 unresolved_reloc = save_unresolved_reloc;
15656 r_type = R_PPC64_REL24;
15657 }
15658 break;
15659
15660 case R_PPC64_PLTSEQ:
15661 if (unresolved_reloc)
15662 {
15663 unresolved_reloc = FALSE;
15664 goto nop_it;
15665 }
15666 break;
15667
15668 case R_PPC64_PLT16_HA:
15669 if (unresolved_reloc)
15670 {
15671 unresolved_reloc = FALSE;
15672 goto nop_it;
15673 }
15674 /* Fall through. */
67f0cbdb
AM
15675 case R_PPC64_GOT_TLSLD16_HA:
15676 case R_PPC64_GOT_TLSGD16_HA:
15677 case R_PPC64_GOT_TPREL16_HA:
15678 case R_PPC64_GOT_DTPREL16_HA:
15679 case R_PPC64_GOT16_HA:
15680 case R_PPC64_TOC16_HA:
98528052 15681 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15682 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 15683 {
23cedd1d
AM
15684 bfd_byte *p;
15685 nop_it:
15686 p = contents + (rel->r_offset & ~3);
98528052 15687 bfd_put_32 (input_bfd, NOP, p);
d830549d 15688 goto copy_reloc;
98528052 15689 }
67f0cbdb
AM
15690 break;
15691
23cedd1d
AM
15692 case R_PPC64_PLT16_LO:
15693 case R_PPC64_PLT16_LO_DS:
15694 if (unresolved_reloc)
15695 {
15696 unresolved_reloc = FALSE;
15697 goto nop_it;
15698 }
15699 /* Fall through. */
67f0cbdb
AM
15700 case R_PPC64_GOT_TLSLD16_LO:
15701 case R_PPC64_GOT_TLSGD16_LO:
15702 case R_PPC64_GOT_TPREL16_LO_DS:
15703 case R_PPC64_GOT_DTPREL16_LO_DS:
15704 case R_PPC64_GOT16_LO:
15705 case R_PPC64_GOT16_LO_DS:
15706 case R_PPC64_TOC16_LO:
15707 case R_PPC64_TOC16_LO_DS:
98528052 15708 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15709 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15710 {
15711 bfd_byte *p = contents + (rel->r_offset & ~3);
15712 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15713 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15714 {
15715 /* Transform addic to addi when we change reg. */
15716 insn &= ~((0x3f << 26) | (0x1f << 16));
15717 insn |= (14u << 26) | (2 << 16);
15718 }
15719 else
67f0cbdb 15720 {
98528052
AM
15721 insn &= ~(0x1f << 16);
15722 insn |= 2 << 16;
67f0cbdb 15723 }
560c8763 15724 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15725 }
15726 break;
9a23f96e
AM
15727
15728 case R_PPC64_TPREL16_HA:
15729 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15730 {
15731 bfd_byte *p = contents + (rel->r_offset & ~3);
15732 insn = bfd_get_32 (input_bfd, p);
15733 if ((insn & ((0x3f << 26) | 0x1f << 16))
15734 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15735 /* xgettext:c-format */
15736 info->callbacks->minfo
15737 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
15738 input_bfd, input_section, rel->r_offset,
15739 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 15740 else
d830549d
AM
15741 {
15742 bfd_put_32 (input_bfd, NOP, p);
15743 goto copy_reloc;
15744 }
9a23f96e
AM
15745 }
15746 break;
15747
15748 case R_PPC64_TPREL16_LO:
15749 case R_PPC64_TPREL16_LO_DS:
15750 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15751 {
15752 bfd_byte *p = contents + (rel->r_offset & ~3);
15753 insn = bfd_get_32 (input_bfd, p);
15754 insn &= ~(0x1f << 16);
15755 insn |= 13 << 16;
15756 bfd_put_32 (input_bfd, insn, p);
15757 }
15758 break;
67f0cbdb
AM
15759 }
15760
65f38f15
AM
15761 /* Do any further special processing. */
15762 switch (r_type)
15763 {
15764 default:
15765 break;
15766
25f23106 15767 case R_PPC64_REL16_HA:
a680de9a 15768 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15769 case R_PPC64_ADDR16_HA:
15770 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15771 case R_PPC64_ADDR16_HIGHERA:
15772 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15773 case R_PPC64_TOC16_HA:
15774 case R_PPC64_SECTOFF_HA:
411e1bfb 15775 case R_PPC64_TPREL16_HA:
f9c6b907 15776 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15777 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15778 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15779 case R_PPC64_DTPREL16_HA:
15780 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15781 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15782 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15783 /* It's just possible that this symbol is a weak symbol
15784 that's not actually defined anywhere. In that case,
15785 'sec' would be NULL, and we should leave the symbol
15786 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15787 if (sec == NULL)
15788 break;
1a0670f3 15789 /* Fall through. */
5c5f6e17
AM
15790
15791 case R_PPC64_GOT16_HA:
15792 case R_PPC64_PLTGOT16_HA:
15793 case R_PPC64_PLT16_HA:
15794 case R_PPC64_GOT_TLSGD16_HA:
15795 case R_PPC64_GOT_TLSLD16_HA:
15796 case R_PPC64_GOT_TPREL16_HA:
15797 case R_PPC64_GOT_DTPREL16_HA:
15798 /* Add 0x10000 if sign bit in 0:15 is set.
15799 Bits 0:15 are not used. */
15800 addend += 0x8000;
65f38f15
AM
15801 break;
15802
15803 case R_PPC64_ADDR16_DS:
15804 case R_PPC64_ADDR16_LO_DS:
15805 case R_PPC64_GOT16_DS:
15806 case R_PPC64_GOT16_LO_DS:
15807 case R_PPC64_PLT16_LO_DS:
15808 case R_PPC64_SECTOFF_DS:
15809 case R_PPC64_SECTOFF_LO_DS:
15810 case R_PPC64_TOC16_DS:
15811 case R_PPC64_TOC16_LO_DS:
15812 case R_PPC64_PLTGOT16_DS:
15813 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15814 case R_PPC64_GOT_TPREL16_DS:
15815 case R_PPC64_GOT_TPREL16_LO_DS:
15816 case R_PPC64_GOT_DTPREL16_DS:
15817 case R_PPC64_GOT_DTPREL16_LO_DS:
15818 case R_PPC64_TPREL16_DS:
15819 case R_PPC64_TPREL16_LO_DS:
15820 case R_PPC64_DTPREL16_DS:
15821 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15822 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15823 mask = 3;
a680de9a
PB
15824 /* If this reloc is against an lq, lxv, or stxv insn, then
15825 the value must be a multiple of 16. This is somewhat of
15826 a hack, but the "correct" way to do this by defining _DQ
15827 forms of all the _DS relocs bloats all reloc switches in
15828 this file. It doesn't make much sense to use these
15829 relocs in data, so testing the insn should be safe. */
15830 if ((insn & (0x3f << 26)) == (56u << 26)
15831 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15832 mask = 15;
a680de9a
PB
15833 relocation += addend;
15834 addend = insn & (mask ^ 3);
15835 if ((relocation & mask) != 0)
65f38f15 15836 {
a680de9a 15837 relocation ^= relocation & mask;
25f53a85 15838 info->callbacks->einfo
695344c0 15839 /* xgettext:c-format */
174d0a74 15840 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 15841 input_bfd, input_section, rel->r_offset,
d830549d 15842 ppc64_elf_howto_table[r_type]->name,
adadcc0c 15843 mask + 1);
65f38f15 15844 bfd_set_error (bfd_error_bad_value);
b34976b6 15845 ret = FALSE;
c316a17c 15846 goto copy_reloc;
65f38f15
AM
15847 }
15848 break;
5bd4f169
AM
15849 }
15850
239e1f3a
AM
15851 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15852 because such sections are not SEC_ALLOC and thus ld.so will
15853 not process them. */
d830549d 15854 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 15855 if (unresolved_reloc
239e1f3a 15856 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15857 && h->elf.def_dynamic)
15858 && _bfd_elf_section_offset (output_bfd, info, input_section,
15859 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15860 {
25f53a85 15861 info->callbacks->einfo
695344c0 15862 /* xgettext:c-format */
c1c8c1ef 15863 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 15864 input_bfd, input_section, rel->r_offset,
b80eed39 15865 howto->name,
039b3fef 15866 h->elf.root.root.string);
b34976b6 15867 ret = FALSE;
9c07fe7c 15868 }
5bd4f169 15869
b80eed39
AM
15870 /* 16-bit fields in insns mostly have signed values, but a
15871 few insns have 16-bit unsigned values. Really, we should
15872 have different reloc types. */
15873 if (howto->complain_on_overflow != complain_overflow_dont
15874 && howto->dst_mask == 0xffff
15875 && (input_section->flags & SEC_CODE) != 0)
15876 {
15877 enum complain_overflow complain = complain_overflow_signed;
15878
15879 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15880 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15881 complain = complain_overflow_bitfield;
15882 else if (howto->rightshift == 0
15883 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15884 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15885 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15886 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15887 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15888 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15889 complain = complain_overflow_unsigned;
15890 if (howto->complain_on_overflow != complain)
15891 {
15892 alt_howto = *howto;
15893 alt_howto.complain_on_overflow = complain;
15894 howto = &alt_howto;
15895 }
15896 }
15897
a680de9a
PB
15898 if (r_type == R_PPC64_REL16DX_HA)
15899 {
15900 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15901 if (rel->r_offset + 4 > input_section->size)
15902 r = bfd_reloc_outofrange;
15903 else
15904 {
15905 relocation += addend;
15906 relocation -= (rel->r_offset
15907 + input_section->output_offset
15908 + input_section->output_section->vma);
3de43e7b 15909 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
15910 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15911 insn &= ~0x1fffc1;
3de43e7b 15912 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
15913 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15914 r = bfd_reloc_ok;
3de43e7b 15915 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
15916 r = bfd_reloc_overflow;
15917 }
15918 }
15919 else
15920 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15921 rel->r_offset, relocation, addend);
5bd4f169 15922
ef60b7ff 15923 if (r != bfd_reloc_ok)
5bd4f169 15924 {
bc30df16 15925 char *more_info = NULL;
b80eed39 15926 const char *reloc_name = howto->name;
bc30df16
AM
15927
15928 if (reloc_dest != DEST_NORMAL)
15929 {
15930 more_info = bfd_malloc (strlen (reloc_name) + 8);
15931 if (more_info != NULL)
15932 {
15933 strcpy (more_info, reloc_name);
15934 strcat (more_info, (reloc_dest == DEST_OPD
15935 ? " (OPD)" : " (stub)"));
15936 reloc_name = more_info;
15937 }
15938 }
15939
cd27b276 15940 if (r == bfd_reloc_overflow)
5bd4f169 15941 {
8131c122
AM
15942 /* On code like "if (foo) foo();" don't report overflow
15943 on a branch to zero when foo is undefined. */
15944 if (!warned
15945 && (reloc_dest == DEST_STUB
15946 || !(h != NULL
15947 && (h->elf.root.type == bfd_link_hash_undefweak
15948 || h->elf.root.type == bfd_link_hash_undefined)
15949 && is_branch_reloc (r_type))))
1a72702b
AM
15950 info->callbacks->reloc_overflow (info, &h->elf.root,
15951 sym_name, reloc_name,
15952 orig_rel.r_addend,
15953 input_bfd, input_section,
15954 rel->r_offset);
ef60b7ff
AM
15955 }
15956 else
15957 {
25f53a85 15958 info->callbacks->einfo
695344c0 15959 /* xgettext:c-format */
c1c8c1ef 15960 (_("%H: %s against `%pT': error %d\n"),
25f53a85 15961 input_bfd, input_section, rel->r_offset,
bc30df16 15962 reloc_name, sym_name, (int) r);
b34976b6 15963 ret = FALSE;
ef60b7ff 15964 }
bc30df16
AM
15965 if (more_info != NULL)
15966 free (more_info);
5bd4f169 15967 }
c316a17c
AM
15968 copy_reloc:
15969 if (wrel != rel)
15970 *wrel = *rel;
15971 }
15972
15973 if (wrel != rel)
15974 {
15975 Elf_Internal_Shdr *rel_hdr;
15976 size_t deleted = rel - wrel;
15977
15978 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15979 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15980 if (rel_hdr->sh_size == 0)
15981 {
15982 /* It is too late to remove an empty reloc section. Leave
15983 one NONE reloc.
15984 ??? What is wrong with an empty section??? */
15985 rel_hdr->sh_size = rel_hdr->sh_entsize;
15986 deleted -= 1;
15987 }
15988 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15989 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15990 input_section->reloc_count -= deleted;
5bd4f169
AM
15991 }
15992
645ea6a9
AM
15993 /* If we're emitting relocations, then shortly after this function
15994 returns, reloc offsets and addends for this section will be
15995 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15996 file rather than the input. Save a copy of the relocs for
15997 opd_entry_value. */
0e1862bb 15998 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15999 {
16000 bfd_size_type amt;
16001 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16002 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
16003 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16004 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
16005 if (rel == NULL)
16006 return FALSE;
16007 memcpy (rel, relocs, amt);
16008 }
5bd4f169
AM
16009 return ret;
16010}
16011
754021d0
AM
16012/* Adjust the value of any local symbols in opd sections. */
16013
6e0b88f1 16014static int
754021d0
AM
16015ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16016 const char *name ATTRIBUTE_UNUSED,
16017 Elf_Internal_Sym *elfsym,
16018 asection *input_sec,
16019 struct elf_link_hash_entry *h)
16020{
74f0fb50
AM
16021 struct _opd_sec_data *opd;
16022 long adjust;
754021d0
AM
16023 bfd_vma value;
16024
4025353c 16025 if (h != NULL)
6e0b88f1 16026 return 1;
4025353c 16027
74f0fb50
AM
16028 opd = get_opd_info (input_sec);
16029 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 16030 return 1;
754021d0
AM
16031
16032 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 16033 if (!bfd_link_relocatable (info))
754021d0
AM
16034 value -= input_sec->output_section->vma;
16035
51aecdc5 16036 adjust = opd->adjust[OPD_NDX (value)];
4025353c 16037 if (adjust == -1)
6e0b88f1
AM
16038 return 2;
16039
16040 elfsym->st_value += adjust;
16041 return 1;
754021d0
AM
16042}
16043
5bd4f169
AM
16044/* Finish up dynamic symbol handling. We set the contents of various
16045 dynamic sections here. */
16046
b34976b6 16047static bfd_boolean
4ce794b7
AM
16048ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16049 struct bfd_link_info *info,
16050 struct elf_link_hash_entry *h,
4aef7643 16051 Elf_Internal_Sym *sym)
5bd4f169 16052{
65f38f15 16053 struct ppc_link_hash_table *htab;
8387904d 16054 struct plt_entry *ent;
5bd4f169 16055
65f38f15 16056 htab = ppc_hash_table (info);
4dfe6ac6
NC
16057 if (htab == NULL)
16058 return FALSE;
5bd4f169 16059
49c09209
AM
16060 if (!htab->opd_abi && !h->def_regular)
16061 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16062 if (ent->plt.offset != (bfd_vma) -1)
16063 {
16064 /* Mark the symbol as undefined, rather than as
16065 defined in glink. Leave the value if there were
16066 any relocations where pointer equality matters
16067 (this is a clue for the dynamic linker, to make
16068 function pointer comparisons work between an
16069 application and shared library), otherwise set it
16070 to zero. */
16071 sym->st_shndx = SHN_UNDEF;
16072 if (!h->pointer_equality_needed)
16073 sym->st_value = 0;
16074 else if (!h->ref_regular_nonweak)
16075 {
16076 /* This breaks function pointer comparisons, but
16077 that is better than breaking tests for a NULL
16078 function pointer. */
16079 sym->st_value = 0;
16080 }
16081 break;
16082 }
5bd4f169 16083
f5385ebf 16084 if (h->needs_copy)
5bd4f169 16085 {
65f38f15 16086 /* This symbol needs a copy reloc. Set it up. */
49c09209 16087 Elf_Internal_Rela rela;
5474d94f 16088 asection *srel;
49c09209 16089 bfd_byte *loc;
5bd4f169 16090
65f38f15
AM
16091 if (h->dynindx == -1
16092 || (h->root.type != bfd_link_hash_defined
16093 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
16094 || htab->elf.srelbss == NULL
16095 || htab->elf.sreldynrelro == NULL)
65f38f15 16096 abort ();
5bd4f169
AM
16097
16098 rela.r_offset = (h->root.u.def.value
16099 + h->root.u.def.section->output_section->vma
16100 + h->root.u.def.section->output_offset);
16101 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16102 rela.r_addend = 0;
afbf7e8e 16103 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
16104 srel = htab->elf.sreldynrelro;
16105 else
16106 srel = htab->elf.srelbss;
16107 loc = srel->contents;
16108 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 16109 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
16110 }
16111
b34976b6 16112 return TRUE;
5bd4f169
AM
16113}
16114
65f38f15
AM
16115/* Used to decide how to sort relocs in an optimal manner for the
16116 dynamic linker, before writing them out. */
16117
16118static enum elf_reloc_type_class
7e612e98
AM
16119ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16120 const asection *rel_sec,
16121 const Elf_Internal_Rela *rela)
65f38f15 16122{
04c9666a 16123 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
16124 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16125
33e44f2e 16126 if (rel_sec == htab->elf.irelplt)
7e612e98 16127 return reloc_class_ifunc;
a33d1f77 16128
4ce794b7 16129 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 16130 switch (r_type)
65f38f15
AM
16131 {
16132 case R_PPC64_RELATIVE:
16133 return reloc_class_relative;
16134 case R_PPC64_JMP_SLOT:
16135 return reloc_class_plt;
16136 case R_PPC64_COPY:
16137 return reloc_class_copy;
16138 default:
16139 return reloc_class_normal;
16140 }
16141}
16142
5bd4f169
AM
16143/* Finish up the dynamic sections. */
16144
b34976b6 16145static bfd_boolean
4ce794b7
AM
16146ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16147 struct bfd_link_info *info)
5bd4f169 16148{
65f38f15
AM
16149 struct ppc_link_hash_table *htab;
16150 bfd *dynobj;
5bd4f169 16151 asection *sdyn;
5bd4f169 16152
65f38f15 16153 htab = ppc_hash_table (info);
4dfe6ac6
NC
16154 if (htab == NULL)
16155 return FALSE;
16156
65f38f15 16157 dynobj = htab->elf.dynobj;
3d4d4302 16158 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 16159
65f38f15 16160 if (htab->elf.dynamic_sections_created)
5bd4f169 16161 {
5bd4f169
AM
16162 Elf64_External_Dyn *dyncon, *dynconend;
16163
33e44f2e 16164 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 16165 abort ();
5bd4f169
AM
16166
16167 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 16168 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
16169 for (; dyncon < dynconend; dyncon++)
16170 {
16171 Elf_Internal_Dyn dyn;
19397422 16172 asection *s;
5bd4f169
AM
16173
16174 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16175
16176 switch (dyn.d_tag)
16177 {
65f38f15
AM
16178 default:
16179 continue;
5bd4f169 16180
5d1634d7 16181 case DT_PPC64_GLINK:
4ce794b7 16182 s = htab->glink;
6348e046 16183 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
16184 /* We stupidly defined DT_PPC64_GLINK to be the start
16185 of glink rather than the first entry point, which is
16186 what ld.so needs, and now have a bigger stub to
16187 support automatic multiple TOCs. */
9e390558 16188 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
16189 break;
16190
19397422
AM
16191 case DT_PPC64_OPD:
16192 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16193 if (s == NULL)
16194 continue;
16195 dyn.d_un.d_ptr = s->vma;
19397422
AM
16196 break;
16197
e8910a83
AM
16198 case DT_PPC64_OPT:
16199 if (htab->do_multi_toc && htab->multi_toc_needed)
16200 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
16201 if (htab->has_plt_localentry0)
16202 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
16203 break;
16204
19397422
AM
16205 case DT_PPC64_OPDSZ:
16206 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16207 if (s == NULL)
16208 continue;
eea6121a 16209 dyn.d_un.d_val = s->size;
19397422
AM
16210 break;
16211
65f38f15 16212 case DT_PLTGOT:
33e44f2e 16213 s = htab->elf.splt;
6348e046 16214 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
16215 break;
16216
16217 case DT_JMPREL:
33e44f2e 16218 s = htab->elf.srelplt;
6348e046 16219 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 16220 break;
5bd4f169 16221
65f38f15 16222 case DT_PLTRELSZ:
33e44f2e 16223 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 16224 break;
82e66161
AM
16225
16226 case DT_TEXTREL:
16227 if (htab->local_ifunc_resolver)
16228 info->callbacks->einfo
16229 (_("%X%P: text relocations and GNU indirect "
16230 "functions will result in a segfault at runtime\n"));
16231 else if (htab->maybe_local_ifunc_resolver)
16232 info->callbacks->einfo
16233 (_("%P: warning: text relocations and GNU indirect "
16234 "functions may result in a segfault at runtime\n"));
16235 continue;
5bd4f169 16236 }
5bd4f169 16237
65f38f15 16238 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 16239 }
5bd4f169
AM
16240 }
16241
6528b6eb
AM
16242 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16243 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16244 {
16245 /* Fill in the first entry in the global offset table.
16246 We use it to hold the link-time TOCbase. */
16247 bfd_put_64 (output_bfd,
60ee0d4a 16248 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 16249 htab->elf.sgot->contents);
5d1634d7
AM
16250
16251 /* Set .got entry size. */
33e44f2e 16252 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
16253 }
16254
6528b6eb
AM
16255 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16256 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16257 {
16258 /* Set .plt entry size. */
33e44f2e 16259 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 16260 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
16261 }
16262
84f5d08e
AM
16263 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16264 brlt ourselves if emitrelocations. */
16265 if (htab->brlt != NULL
16266 && htab->brlt->reloc_count != 0
16267 && !_bfd_elf_link_output_relocs (output_bfd,
16268 htab->brlt,
d4730f92 16269 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
16270 elf_section_data (htab->brlt)->relocs,
16271 NULL))
16272 return FALSE;
16273
176a0d42
AM
16274 if (htab->glink != NULL
16275 && htab->glink->reloc_count != 0
16276 && !_bfd_elf_link_output_relocs (output_bfd,
16277 htab->glink,
d4730f92 16278 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
16279 elf_section_data (htab->glink)->relocs,
16280 NULL))
16281 return FALSE;
16282
58d180e8 16283 if (htab->glink_eh_frame != NULL
da44f4e5
AM
16284 && htab->glink_eh_frame->size != 0)
16285 {
16286 bfd_vma val;
16287 bfd_byte *p;
d4aaa2a0 16288 struct map_stub *group;
2e0ce1c8 16289 size_t align = 4;
da44f4e5 16290
2e0ce1c8
AM
16291 p = htab->glink_eh_frame->contents;
16292 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
d4aaa2a0
AM
16293
16294 for (group = htab->group; group != NULL; group = group->next)
16295 if (group->stub_sec != NULL)
da44f4e5 16296 {
da44f4e5 16297 /* Offset to stub section. */
d4aaa2a0
AM
16298 val = (group->stub_sec->output_section->vma
16299 + group->stub_sec->output_offset);
da44f4e5
AM
16300 val -= (htab->glink_eh_frame->output_section->vma
16301 + htab->glink_eh_frame->output_offset
d4aaa2a0 16302 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
16303 if (val + 0x80000000 > 0xffffffff)
16304 {
cf97bcb0
AM
16305 _bfd_error_handler
16306 (_("%s offset too large for .eh_frame sdata4 encoding"),
d4aaa2a0 16307 group->stub_sec->name);
da44f4e5
AM
16308 return FALSE;
16309 }
d4aaa2a0
AM
16310 bfd_put_32 (dynobj, val, p + 8);
16311 p += stub_eh_frame_size (group, align);
da44f4e5
AM
16312 }
16313 if (htab->glink != NULL && htab->glink->size != 0)
16314 {
da44f4e5
AM
16315 /* Offset to .glink. */
16316 val = (htab->glink->output_section->vma
16317 + htab->glink->output_offset
16318 + 8);
16319 val -= (htab->glink_eh_frame->output_section->vma
16320 + htab->glink_eh_frame->output_offset
d4aaa2a0 16321 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
16322 if (val + 0x80000000 > 0xffffffff)
16323 {
cf97bcb0
AM
16324 _bfd_error_handler
16325 (_("%s offset too large for .eh_frame sdata4 encoding"),
da44f4e5
AM
16326 htab->glink->name);
16327 return FALSE;
16328 }
d4aaa2a0
AM
16329 bfd_put_32 (dynobj, val, p + 8);
16330 p += (24 + align - 1) & -align;
da44f4e5
AM
16331 }
16332
16333 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16334 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16335 htab->glink_eh_frame,
16336 htab->glink_eh_frame->contents))
16337 return FALSE;
16338 }
58d180e8 16339
e717da7e 16340 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
16341 since we didn't add them to DYNOBJ. We know dynobj is the first
16342 bfd. */
c72f2fb2 16343 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
16344 {
16345 asection *s;
7b53ace3 16346
0c8d6e5c 16347 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
16348 continue;
16349
e717da7e
AM
16350 s = ppc64_elf_tdata (dynobj)->got;
16351 if (s != NULL
eea6121a 16352 && s->size != 0
e717da7e
AM
16353 && s->output_section != bfd_abs_section_ptr
16354 && !bfd_set_section_contents (output_bfd, s->output_section,
16355 s->contents, s->output_offset,
eea6121a 16356 s->size))
e717da7e
AM
16357 return FALSE;
16358 s = ppc64_elf_tdata (dynobj)->relgot;
16359 if (s != NULL
eea6121a 16360 && s->size != 0
e717da7e
AM
16361 && s->output_section != bfd_abs_section_ptr
16362 && !bfd_set_section_contents (output_bfd, s->output_section,
16363 s->contents, s->output_offset,
eea6121a 16364 s->size))
e717da7e
AM
16365 return FALSE;
16366 }
f6c52c13 16367
b34976b6 16368 return TRUE;
5bd4f169
AM
16369}
16370
5bd4f169 16371#include "elf64-target.h"
7b8e7dad
AM
16372
16373/* FreeBSD support */
16374
16375#undef TARGET_LITTLE_SYM
16376#undef TARGET_LITTLE_NAME
16377
16378#undef TARGET_BIG_SYM
6d00b590 16379#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
16380#undef TARGET_BIG_NAME
16381#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16382
16383#undef ELF_OSABI
16384#define ELF_OSABI ELFOSABI_FREEBSD
16385
16386#undef elf64_bed
16387#define elf64_bed elf64_powerpc_fbsd_bed
16388
16389#include "elf64-target.h"
This page took 2.743885 seconds and 4 git commands to generate.