Remove tp_t typedef
[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
AM
3043 strncpy (data + 40, va_arg (ap, const char *), 16);
3044 strncpy (data + 56, va_arg (ap, const char *), 80);
3045 va_end (ap);
3046 return elfcore_write_note (abfd, buf, bufsiz,
3047 "CORE", note_type, data, sizeof (data));
3048 }
3049
3050 case NT_PRSTATUS:
3051 {
3052 char data[504];
3053 va_list ap;
3054 long pid;
3055 int cursig;
3056 const void *greg;
3057
3058 va_start (ap, note_type);
3059 memset (data, 0, 112);
3060 pid = va_arg (ap, long);
3061 bfd_put_32 (abfd, pid, data + 32);
3062 cursig = va_arg (ap, int);
3063 bfd_put_16 (abfd, cursig, data + 12);
3064 greg = va_arg (ap, const void *);
3065 memcpy (data + 112, greg, 384);
3066 memset (data + 496, 0, 8);
3067 va_end (ap);
3068 return elfcore_write_note (abfd, buf, bufsiz,
3069 "CORE", note_type, data, sizeof (data));
3070 }
3071 }
3072}
3073
5d35169e
AM
3074/* Add extra PPC sections. */
3075
b35d266b 3076static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 3077{
07d6d2b8
AM
3078 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3079 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3080 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
3081 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3082 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3083 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 3084 { NULL, 0, 0, 0, 0 }
5d35169e
AM
3085};
3086
7c8fe5c4
AM
3087enum _ppc64_sec_type {
3088 sec_normal = 0,
3089 sec_opd = 1,
3090 sec_toc = 2
3091};
3092
f0abc2a1
AM
3093struct _ppc64_elf_section_data
3094{
3095 struct bfd_elf_section_data elf;
411e1bfb 3096
f0abc2a1
AM
3097 union
3098 {
51aecdc5
AM
3099 /* An array with one entry for each opd function descriptor,
3100 and some spares since opd entries may be either 16 or 24 bytes. */
3101#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
3102 struct _opd_sec_data
3103 {
3104 /* Points to the function code section for local opd entries. */
3105 asection **func_sec;
3106
3107 /* After editing .opd, adjust references to opd local syms. */
3108 long *adjust;
3109 } opd;
7c8fe5c4 3110
3a71aa26
AM
3111 /* An array for toc sections, indexed by offset/8. */
3112 struct _toc_sec_data
3113 {
3114 /* Specifies the relocation symbol index used at a given toc offset. */
3115 unsigned *symndx;
3116
3117 /* And the relocation addend. */
3118 bfd_vma *add;
3119 } toc;
7c8fe5c4
AM
3120 } u;
3121
3122 enum _ppc64_sec_type sec_type:2;
411e1bfb 3123
7c8fe5c4
AM
3124 /* Flag set when small branches are detected. Used to
3125 select suitable defaults for the stub group size. */
3126 unsigned int has_14bit_branch:1;
3e04d765
AM
3127
3128 /* Flag set when PLTCALL relocs are detected. */
3129 unsigned int has_pltcall:1;
f0abc2a1
AM
3130};
3131
3132#define ppc64_elf_section_data(sec) \
411e1bfb 3133 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
3134
3135static bfd_boolean
4ce794b7 3136ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 3137{
f592407e
AM
3138 if (!sec->used_by_bfd)
3139 {
3140 struct _ppc64_elf_section_data *sdata;
3141 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 3142
f592407e
AM
3143 sdata = bfd_zalloc (abfd, amt);
3144 if (sdata == NULL)
3145 return FALSE;
3146 sec->used_by_bfd = sdata;
3147 }
f0abc2a1
AM
3148
3149 return _bfd_elf_new_section_hook (abfd, sec);
3150}
4025353c 3151
74f0fb50 3152static struct _opd_sec_data *
4025353c
AM
3153get_opd_info (asection * sec)
3154{
3155 if (sec != NULL
3156 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3157 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3158 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3159 return NULL;
3160}
90e3cdf2
JJ
3161\f
3162/* Parameters for the qsort hook. */
90e3cdf2 3163static bfd_boolean synthetic_relocatable;
cd285db5 3164static asection *synthetic_opd;
90e3cdf2 3165
699733f6 3166/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3167
3168static int
3169compare_symbols (const void *ap, const void *bp)
3170{
3171 const asymbol *a = * (const asymbol **) ap;
3172 const asymbol *b = * (const asymbol **) bp;
3173
699733f6
AM
3174 /* Section symbols first. */
3175 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3176 return -1;
699733f6 3177 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3178 return 1;
3179
699733f6 3180 /* then .opd symbols. */
cd285db5
AM
3181 if (synthetic_opd != NULL)
3182 {
3183 if (strcmp (a->section->name, ".opd") == 0
3184 && strcmp (b->section->name, ".opd") != 0)
3185 return -1;
3186 if (strcmp (a->section->name, ".opd") != 0
3187 && strcmp (b->section->name, ".opd") == 0)
3188 return 1;
3189 }
90e3cdf2 3190
699733f6 3191 /* then other code symbols. */
90e3cdf2
JJ
3192 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3193 == (SEC_CODE | SEC_ALLOC)
3194 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3195 != (SEC_CODE | SEC_ALLOC))
3196 return -1;
3197
3198 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3199 != (SEC_CODE | SEC_ALLOC)
3200 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3201 == (SEC_CODE | SEC_ALLOC))
3202 return 1;
3203
3204 if (synthetic_relocatable)
3205 {
3206 if (a->section->id < b->section->id)
3207 return -1;
3208
3209 if (a->section->id > b->section->id)
3210 return 1;
3211 }
3212
3213 if (a->value + a->section->vma < b->value + b->section->vma)
3214 return -1;
3215
3216 if (a->value + a->section->vma > b->value + b->section->vma)
3217 return 1;
3218
4d35a0aa
AM
3219 /* For syms with the same value, prefer strong dynamic global function
3220 syms over other syms. */
3221 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3222 return -1;
3223
3224 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3225 return 1;
3226
3227 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3228 return -1;
3229
3230 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3231 return 1;
3232
3233 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3234 return -1;
3235
3236 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3237 return 1;
3238
3239 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3240 return -1;
3241
3242 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3243 return 1;
3244
aaed6f5b 3245 return a > b;
90e3cdf2
JJ
3246}
3247
699733f6 3248/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3249
699733f6 3250static asymbol *
7292b3ac 3251sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3252{
699733f6 3253 long mid;
90e3cdf2 3254
7292b3ac 3255 if (id == (unsigned) -1)
699733f6
AM
3256 {
3257 while (lo < hi)
3258 {
3259 mid = (lo + hi) >> 1;
3260 if (syms[mid]->value + syms[mid]->section->vma < value)
3261 lo = mid + 1;
3262 else if (syms[mid]->value + syms[mid]->section->vma > value)
3263 hi = mid;
3264 else
3265 return syms[mid];
3266 }
3267 }
3268 else
3269 {
3270 while (lo < hi)
3271 {
3272 mid = (lo + hi) >> 1;
3273 if (syms[mid]->section->id < id)
3274 lo = mid + 1;
3275 else if (syms[mid]->section->id > id)
3276 hi = mid;
3277 else if (syms[mid]->value < value)
3278 lo = mid + 1;
3279 else if (syms[mid]->value > value)
3280 hi = mid;
3281 else
3282 return syms[mid];
3283 }
3284 }
3285 return NULL;
90e3cdf2
JJ
3286}
3287
468392fb
AM
3288static bfd_boolean
3289section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3290{
3291 bfd_vma vma = *(bfd_vma *) ptr;
3292 return ((section->flags & SEC_ALLOC) != 0
3293 && section->vma <= vma
3294 && vma < section->vma + section->size);
3295}
3296
699733f6 3297/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3298 entry syms. Also generate @plt symbols for the glink branch table.
3299 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3300
3301static long
a7535cf3
AM
3302ppc64_elf_get_synthetic_symtab (bfd *abfd,
3303 long static_count, asymbol **static_syms,
3304 long dyn_count, asymbol **dyn_syms,
c9727e01 3305 asymbol **ret)
90e3cdf2
JJ
3306{
3307 asymbol *s;
0ccf57bd 3308 size_t i, j, count;
90e3cdf2 3309 char *names;
0ccf57bd 3310 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3311 asection *opd = NULL;
90e3cdf2 3312 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3313 asymbol **syms;
ee67d69a 3314 int abi = abiversion (abfd);
90e3cdf2
JJ
3315
3316 *ret = NULL;
3317
ee67d69a
AM
3318 if (abi < 2)
3319 {
3320 opd = bfd_get_section_by_name (abfd, ".opd");
3321 if (opd == NULL && abi == 1)
3322 return 0;
3323 }
90e3cdf2 3324
a5259595
AM
3325 syms = NULL;
3326 codesecsym = 0;
3327 codesecsymend = 0;
3328 secsymend = 0;
3329 opdsymend = 0;
3330 symcount = 0;
3331 if (opd != NULL)
c9727e01 3332 {
a5259595
AM
3333 symcount = static_count;
3334 if (!relocatable)
3335 symcount += dyn_count;
3336 if (symcount == 0)
3337 return 0;
c9727e01 3338
a5259595
AM
3339 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3340 if (syms == NULL)
3341 return -1;
90e3cdf2 3342
a5259595
AM
3343 if (!relocatable && static_count != 0 && dyn_count != 0)
3344 {
3345 /* Use both symbol tables. */
3346 memcpy (syms, static_syms, static_count * sizeof (*syms));
3347 memcpy (syms + static_count, dyn_syms,
3348 (dyn_count + 1) * sizeof (*syms));
3349 }
3350 else if (!relocatable && static_count == 0)
3351 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3352 else
3353 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 3354
0ccf57bd
AM
3355 /* Trim uninteresting symbols. Interesting symbols are section,
3356 function, and notype symbols. */
3357 for (i = 0, j = 0; i < symcount; ++i)
3358 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3359 | BSF_RELC | BSF_SRELC)) == 0)
3360 syms[j++] = syms[i];
3361 symcount = j;
3362
a5259595
AM
3363 synthetic_relocatable = relocatable;
3364 synthetic_opd = opd;
3365 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3366
a5259595
AM
3367 if (!relocatable && symcount > 1)
3368 {
bfa5bd2a
PA
3369 /* Trim duplicate syms, since we may have merged the normal
3370 and dynamic symbols. Actually, we only care about syms
3371 that have different values, so trim any with the same
3372 value. Don't consider ifunc and ifunc resolver symbols
3373 duplicates however, because GDB wants to know whether a
3374 text symbol is an ifunc resolver. */
a5259595 3375 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
3376 {
3377 const asymbol *s0 = syms[i - 1];
3378 const asymbol *s1 = syms[i];
3379
3380 if ((s0->value + s0->section->vma
3381 != s1->value + s1->section->vma)
3382 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
3383 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
3384 syms[j++] = syms[i];
3385 }
a5259595
AM
3386 symcount = j;
3387 }
699733f6 3388
a5259595
AM
3389 i = 0;
3390 /* Note that here and in compare_symbols we can't compare opd and
3391 sym->section directly. With separate debug info files, the
3392 symbols will be extracted from the debug file while abfd passed
3393 to this function is the real binary. */
0ccf57bd 3394 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
3395 ++i;
3396 codesecsym = i;
3397
3398 for (; i < symcount; ++i)
3399 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3400 | SEC_THREAD_LOCAL))
3401 != (SEC_CODE | SEC_ALLOC))
3402 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3403 break;
3404 codesecsymend = i;
3405
3406 for (; i < symcount; ++i)
3407 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3408 break;
3409 secsymend = i;
3410
3411 for (; i < symcount; ++i)
3412 if (strcmp (syms[i]->section->name, ".opd") != 0)
3413 break;
3414 opdsymend = i;
3415
3416 for (; i < symcount; ++i)
3417 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3418 != (SEC_CODE | SEC_ALLOC))
3419 break;
3420 symcount = i;
3421 }
c9727e01 3422 count = 0;
90e3cdf2 3423
699733f6 3424 if (relocatable)
90e3cdf2 3425 {
699733f6
AM
3426 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3427 arelent *r;
3428 size_t size;
0ccf57bd 3429 size_t relcount;
90e3cdf2 3430
468392fb
AM
3431 if (opdsymend == secsymend)
3432 goto done;
3433
699733f6 3434 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3435 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3436 if (relcount == 0)
c9727e01 3437 goto done;
90e3cdf2 3438
7356fed5
AM
3439 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3440 {
3441 count = -1;
3442 goto done;
3443 }
3444
699733f6 3445 size = 0;
595da8c5 3446 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3447 {
3448 asymbol *sym;
90e3cdf2 3449
595da8c5 3450 while (r < opd->relocation + relcount
699733f6
AM
3451 && r->address < syms[i]->value + opd->vma)
3452 ++r;
90e3cdf2 3453
595da8c5 3454 if (r == opd->relocation + relcount)
699733f6 3455 break;
90e3cdf2 3456
699733f6
AM
3457 if (r->address != syms[i]->value + opd->vma)
3458 continue;
90e3cdf2 3459
699733f6
AM
3460 if (r->howto->type != R_PPC64_ADDR64)
3461 continue;
90e3cdf2 3462
699733f6
AM
3463 sym = *r->sym_ptr_ptr;
3464 if (!sym_exists_at (syms, opdsymend, symcount,
3465 sym->section->id, sym->value + r->addend))
3466 {
3467 ++count;
3468 size += sizeof (asymbol);
3469 size += strlen (syms[i]->name) + 2;
3470 }
3471 }
90e3cdf2 3472
c4b0b099
AM
3473 if (size == 0)
3474 goto done;
699733f6
AM
3475 s = *ret = bfd_malloc (size);
3476 if (s == NULL)
3477 {
7356fed5 3478 count = -1;
c9727e01 3479 goto done;
699733f6 3480 }
90e3cdf2 3481
699733f6 3482 names = (char *) (s + count);
90e3cdf2 3483
595da8c5 3484 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3485 {
699733f6 3486 asymbol *sym;
90e3cdf2 3487
595da8c5 3488 while (r < opd->relocation + relcount
699733f6
AM
3489 && r->address < syms[i]->value + opd->vma)
3490 ++r;
90e3cdf2 3491
595da8c5 3492 if (r == opd->relocation + relcount)
699733f6
AM
3493 break;
3494
3495 if (r->address != syms[i]->value + opd->vma)
3496 continue;
3497
3498 if (r->howto->type != R_PPC64_ADDR64)
3499 continue;
90e3cdf2 3500
699733f6
AM
3501 sym = *r->sym_ptr_ptr;
3502 if (!sym_exists_at (syms, opdsymend, symcount,
3503 sym->section->id, sym->value + r->addend))
3504 {
3505 size_t len;
3506
3507 *s = *syms[i];
6ba2a415 3508 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3509 s->section = sym->section;
3510 s->value = sym->value + r->addend;
3511 s->name = names;
3512 *names++ = '.';
3513 len = strlen (syms[i]->name);
3514 memcpy (names, syms[i]->name, len + 1);
3515 names += len + 1;
6f610d07
UW
3516 /* Have udata.p point back to the original symbol this
3517 synthetic symbol was derived from. */
3518 s->udata.p = syms[i];
699733f6
AM
3519 s++;
3520 }
3521 }
3522 }
3523 else
90e3cdf2 3524 {
468392fb 3525 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3526 bfd_byte *contents = NULL;
699733f6 3527 size_t size;
0ccf57bd 3528 size_t plt_count = 0;
468392fb
AM
3529 bfd_vma glink_vma = 0, resolv_vma = 0;
3530 asection *dynamic, *glink = NULL, *relplt = NULL;
3531 arelent *p;
90e3cdf2 3532
ee67d69a 3533 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3534 {
c4b0b099
AM
3535 free_contents_and_exit_err:
3536 count = -1;
ee67d69a 3537 free_contents_and_exit:
699733f6 3538 if (contents)
ee67d69a 3539 free (contents);
c9727e01 3540 goto done;
699733f6 3541 }
90e3cdf2 3542
699733f6
AM
3543 size = 0;
3544 for (i = secsymend; i < opdsymend; ++i)
3545 {
3546 bfd_vma ent;
90e3cdf2 3547
5ef11c02
AM
3548 /* Ignore bogus symbols. */
3549 if (syms[i]->value > opd->size - 8)
3550 continue;
3551
699733f6
AM
3552 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3553 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3554 {
3555 ++count;
3556 size += sizeof (asymbol);
3557 size += strlen (syms[i]->name) + 2;
3558 }
3559 }
90e3cdf2 3560
468392fb 3561 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3562 if (dyn_count != 0
3563 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3564 {
3565 bfd_byte *dynbuf, *extdyn, *extdynend;
3566 size_t extdynsize;
3567 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3568
3569 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3570 goto free_contents_and_exit_err;
468392fb
AM
3571
3572 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3573 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3574
3575 extdyn = dynbuf;
3576 extdynend = extdyn + dynamic->size;
3577 for (; extdyn < extdynend; extdyn += extdynsize)
3578 {
3579 Elf_Internal_Dyn dyn;
3580 (*swap_dyn_in) (abfd, extdyn, &dyn);
3581
3582 if (dyn.d_tag == DT_NULL)
3583 break;
3584
3585 if (dyn.d_tag == DT_PPC64_GLINK)
3586 {
9e390558
AM
3587 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3588 See comment in ppc64_elf_finish_dynamic_sections. */
3589 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
3590 /* The .glink section usually does not survive the final
3591 link; search for the section (usually .text) where the
3592 glink stubs now reside. */
3593 glink = bfd_sections_find_if (abfd, section_covers_vma,
3594 &glink_vma);
3595 break;
3596 }
3597 }
3598
3599 free (dynbuf);
3600 }
3601
3602 if (glink != NULL)
3603 {
3604 /* Determine __glink trampoline by reading the relative branch
3605 from the first glink stub. */
3606 bfd_byte buf[4];
b9e5796b
AM
3607 unsigned int off = 0;
3608
3609 while (bfd_get_section_contents (abfd, glink, buf,
3610 glink_vma + off - glink->vma, 4))
468392fb
AM
3611 {
3612 unsigned int insn = bfd_get_32 (abfd, buf);
3613 insn ^= B_DOT;
3614 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3615 {
3616 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3617 break;
3618 }
3619 off += 4;
3620 if (off > 4)
3621 break;
468392fb
AM
3622 }
3623
3624 if (resolv_vma)
3625 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3626
066ee829
AM
3627 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3628 if (relplt != NULL)
3629 {
3630 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3631 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3632 goto free_contents_and_exit_err;
68ffbac6 3633
066ee829
AM
3634 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3635 size += plt_count * sizeof (asymbol);
468392fb 3636
066ee829
AM
3637 p = relplt->relocation;
3638 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3639 {
3640 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3641 if (p->addend != 0)
3642 size += sizeof ("+0x") - 1 + 16;
3643 }
066ee829 3644 }
468392fb
AM
3645 }
3646
c4b0b099
AM
3647 if (size == 0)
3648 goto free_contents_and_exit;
699733f6
AM
3649 s = *ret = bfd_malloc (size);
3650 if (s == NULL)
c4b0b099 3651 goto free_contents_and_exit_err;
90e3cdf2 3652
468392fb 3653 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3654
699733f6 3655 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3656 {
699733f6 3657 bfd_vma ent;
90e3cdf2 3658
5ef11c02
AM
3659 if (syms[i]->value > opd->size - 8)
3660 continue;
3661
699733f6
AM
3662 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3663 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3664 {
0ccf57bd 3665 size_t lo, hi;
699733f6 3666 size_t len;
c9727e01 3667 asection *sec = abfd->sections;
90e3cdf2 3668
699733f6
AM
3669 *s = *syms[i];
3670 lo = codesecsym;
3671 hi = codesecsymend;
3672 while (lo < hi)
3673 {
0ccf57bd 3674 size_t mid = (lo + hi) >> 1;
699733f6
AM
3675 if (syms[mid]->section->vma < ent)
3676 lo = mid + 1;
3677 else if (syms[mid]->section->vma > ent)
3678 hi = mid;
3679 else
c9727e01
AM
3680 {
3681 sec = syms[mid]->section;
3682 break;
3683 }
699733f6
AM
3684 }
3685
c9727e01 3686 if (lo >= hi && lo > codesecsym)
699733f6 3687 sec = syms[lo - 1]->section;
699733f6
AM
3688
3689 for (; sec != NULL; sec = sec->next)
3690 {
3691 if (sec->vma > ent)
3692 break;
63524580
JK
3693 /* SEC_LOAD may not be set if SEC is from a separate debug
3694 info file. */
3695 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3696 break;
3697 if ((sec->flags & SEC_CODE) != 0)
3698 s->section = sec;
3699 }
6ba2a415 3700 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3701 s->value = ent - s->section->vma;
3702 s->name = names;
3703 *names++ = '.';
3704 len = strlen (syms[i]->name);
3705 memcpy (names, syms[i]->name, len + 1);
3706 names += len + 1;
6f610d07
UW
3707 /* Have udata.p point back to the original symbol this
3708 synthetic symbol was derived from. */
3709 s->udata.p = syms[i];
699733f6 3710 s++;
90e3cdf2 3711 }
90e3cdf2 3712 }
699733f6 3713 free (contents);
468392fb
AM
3714
3715 if (glink != NULL && relplt != NULL)
3716 {
3717 if (resolv_vma)
3718 {
3719 /* Add a symbol for the main glink trampoline. */
86a4952b 3720 memset (s, 0, sizeof *s);
468392fb 3721 s->the_bfd = abfd;
6ba2a415 3722 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3723 s->section = glink;
3724 s->value = resolv_vma - glink->vma;
3725 s->name = names;
3726 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3727 names += sizeof ("__glink_PLTresolve");
3728 s++;
3729 count++;
3730 }
3731
3732 /* FIXME: It would be very much nicer to put sym@plt on the
3733 stub rather than on the glink branch table entry. The
3734 objdump disassembler would then use a sensible symbol
3735 name on plt calls. The difficulty in doing so is
3736 a) finding the stubs, and,
3737 b) matching stubs against plt entries, and,
3738 c) there can be multiple stubs for a given plt entry.
3739
3740 Solving (a) could be done by code scanning, but older
3741 ppc64 binaries used different stubs to current code.
3742 (b) is the tricky one since you need to known the toc
3743 pointer for at least one function that uses a pic stub to
3744 be able to calculate the plt address referenced.
3745 (c) means gdb would need to set multiple breakpoints (or
3746 find the glink branch itself) when setting breakpoints
3747 for pending shared library loads. */
3748 p = relplt->relocation;
3749 for (i = 0; i < plt_count; i++, p++)
3750 {
3751 size_t len;
3752
3753 *s = **p->sym_ptr_ptr;
3754 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3755 we are defining a symbol, ensure one of them is set. */
3756 if ((s->flags & BSF_LOCAL) == 0)
3757 s->flags |= BSF_GLOBAL;
6ba2a415 3758 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3759 s->section = glink;
3760 s->value = glink_vma - glink->vma;
3761 s->name = names;
3762 s->udata.p = NULL;
3763 len = strlen ((*p->sym_ptr_ptr)->name);
3764 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3765 names += len;
e054468f
AM
3766 if (p->addend != 0)
3767 {
3768 memcpy (names, "+0x", sizeof ("+0x") - 1);
3769 names += sizeof ("+0x") - 1;
3770 bfd_sprintf_vma (abfd, names, p->addend);
3771 names += strlen (names);
3772 }
468392fb
AM
3773 memcpy (names, "@plt", sizeof ("@plt"));
3774 names += sizeof ("@plt");
3775 s++;
b9e5796b
AM
3776 if (abi < 2)
3777 {
3778 glink_vma += 8;
3779 if (i >= 0x8000)
3780 glink_vma += 4;
3781 }
3782 else
468392fb
AM
3783 glink_vma += 4;
3784 }
3785 count += plt_count;
3786 }
90e3cdf2
JJ
3787 }
3788
c9727e01 3789 done:
a7535cf3 3790 free (syms);
90e3cdf2
JJ
3791 return count;
3792}
5bd4f169 3793\f
65f38f15
AM
3794/* The following functions are specific to the ELF linker, while
3795 functions above are used generally. Those named ppc64_elf_* are
3796 called by the main ELF linker code. They appear in this file more
3797 or less in the order in which they are called. eg.
3798 ppc64_elf_check_relocs is called early in the link process,
3799 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3800 called.
3801
3802 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3803 functions have both a function code symbol and a function descriptor
3804 symbol. A call to foo in a relocatable object file looks like:
3805
3806 . .text
3807 . x:
3808 . bl .foo
3809 . nop
3810
3811 The function definition in another object file might be:
3812
3813 . .section .opd
3814 . foo: .quad .foo
3815 . .quad .TOC.@tocbase
3816 . .quad 0
3817 .
3818 . .text
3819 . .foo: blr
3820
3821 When the linker resolves the call during a static link, the branch
3822 unsurprisingly just goes to .foo and the .opd information is unused.
3823 If the function definition is in a shared library, things are a little
3824 different: The call goes via a plt call stub, the opd information gets
3825 copied to the plt, and the linker patches the nop.
3826
3827 . x:
3828 . bl .foo_stub
3829 . ld 2,40(1)
3830 .
3831 .
3832 . .foo_stub:
71a39c98
AM
3833 . std 2,40(1) # in practice, the call stub
3834 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3835 . addi 11,11,Lfoo@toc@l # this is the general idea
3836 . ld 12,0(11)
3837 . ld 2,8(11)
3838 . mtctr 12
3839 . ld 11,16(11)
e86ce104
AM
3840 . bctr
3841 .
3842 . .section .plt
3843 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3844
3845 The "reloc ()" notation is supposed to indicate that the linker emits
3846 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3847 copying.
3848
3849 What are the difficulties here? Well, firstly, the relocations
3850 examined by the linker in check_relocs are against the function code
3851 sym .foo, while the dynamic relocation in the plt is emitted against
3852 the function descriptor symbol, foo. Somewhere along the line, we need
3853 to carefully copy dynamic link information from one symbol to the other.
3854 Secondly, the generic part of the elf linker will make .foo a dynamic
3855 symbol as is normal for most other backends. We need foo dynamic
3856 instead, at least for an application final link. However, when
3857 creating a shared library containing foo, we need to have both symbols
3858 dynamic so that references to .foo are satisfied during the early
3859 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3860 definition from some other object, eg. a static library.
3861
3862 Update: As of August 2004, we support a new convention. Function
3863 calls may use the function descriptor symbol, ie. "bl foo". This
3864 behaves exactly as "bl .foo". */
65f38f15 3865
7c8bbca5
AM
3866/* Of those relocs that might be copied as dynamic relocs, this
3867 function selects those that must be copied when linking a shared
3868 library or PIE, even when the symbol is local. */
65f38f15 3869
1d483afe
AM
3870static int
3871must_be_dyn_reloc (struct bfd_link_info *info,
3872 enum elf_ppc64_reloc_type r_type)
3873{
3874 switch (r_type)
3875 {
3876 default:
7c8bbca5
AM
3877 /* Only relative relocs can be resolved when the object load
3878 address isn't fixed. DTPREL64 is excluded because the
3879 dynamic linker needs to differentiate global dynamic from
3880 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
3881 return 1;
3882
3883 case R_PPC64_REL32:
3884 case R_PPC64_REL64:
3885 case R_PPC64_REL30:
3886 return 0;
3887
3888 case R_PPC64_TPREL16:
3889 case R_PPC64_TPREL16_LO:
3890 case R_PPC64_TPREL16_HI:
3891 case R_PPC64_TPREL16_HA:
3892 case R_PPC64_TPREL16_DS:
3893 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3894 case R_PPC64_TPREL16_HIGH:
3895 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3896 case R_PPC64_TPREL16_HIGHER:
3897 case R_PPC64_TPREL16_HIGHERA:
3898 case R_PPC64_TPREL16_HIGHEST:
3899 case R_PPC64_TPREL16_HIGHESTA:
3900 case R_PPC64_TPREL64:
7c8bbca5
AM
3901 /* These relocations are relative but in a shared library the
3902 linker doesn't know the thread pointer base. */
3903 return bfd_link_dll (info);
1d483afe
AM
3904 }
3905}
65f38f15 3906
f4656909
AM
3907/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3908 copying dynamic variables from a shared lib into an app's dynbss
3909 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3910 shared lib. With code that gcc generates, it's vital that this be
3911 enabled; In the PowerPC64 ABI, the address of a function is actually
3912 the address of a function descriptor, which resides in the .opd
3913 section. gcc uses the descriptor directly rather than going via the
3914 GOT as some other ABI's do, which means that initialized function
3915 pointers must reference the descriptor. Thus, a function pointer
3916 initialized to the address of a function in a shared library will
3917 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3918 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3919 presents a problem as a plt entry for that function is also
3920 initialized from the function descriptor symbol and the copy reloc
3921 may not be initialized first. */
a23b6845 3922#define ELIMINATE_COPY_RELOCS 1
f4656909 3923
721956f4 3924/* Section name for stubs is the associated section name plus this
29942be8
NC
3925 string. */
3926#define STUB_SUFFIX ".stub"
721956f4
AM
3927
3928/* Linker stubs.
3929 ppc_stub_long_branch:
3930 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3931 destination, but a 24 bit branch in a stub section will reach.
3932 . b dest
3933
3934 ppc_stub_plt_branch:
3935 Similar to the above, but a 24 bit branch in the stub section won't
3936 reach its destination.
71a39c98
AM
3937 . addis %r11,%r2,xxx@toc@ha
3938 . ld %r12,xxx@toc@l(%r11)
3939 . mtctr %r12
721956f4
AM
3940 . bctr
3941
3942 ppc_stub_plt_call:
2c66dc6c
AM
3943 Used to call a function in a shared library. If it so happens that
3944 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3945 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3946 . std %r2,40(%r1)
71a39c98
AM
3947 . addis %r11,%r2,xxx@toc@ha
3948 . ld %r12,xxx+0@toc@l(%r11)
3949 . mtctr %r12
3950 . ld %r2,xxx+8@toc@l(%r11)
3951 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3952 . bctr
ad8e1ba5
AM
3953
3954 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3955 code to adjust the value and save r2 to support multiple toc sections.
3956 A ppc_stub_long_branch with an r2 offset looks like:
3957 . std %r2,40(%r1)
3958 . addis %r2,%r2,off@ha
3959 . addi %r2,%r2,off@l
3960 . b dest
3961
3962 A ppc_stub_plt_branch with an r2 offset looks like:
3963 . std %r2,40(%r1)
71a39c98
AM
3964 . addis %r11,%r2,xxx@toc@ha
3965 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3966 . addis %r2,%r2,off@ha
3967 . addi %r2,%r2,off@l
71a39c98 3968 . mtctr %r12
ad8e1ba5 3969 . bctr
ac2df442
AM
3970
3971 In cases where the "addis" instruction would add zero, the "addis" is
3972 omitted and following instructions modified slightly in some cases.
721956f4
AM
3973*/
3974
3975enum ppc_stub_type {
3976 ppc_stub_none,
3977 ppc_stub_long_branch,
ad8e1ba5 3978 ppc_stub_long_branch_r2off,
721956f4 3979 ppc_stub_plt_branch,
ad8e1ba5 3980 ppc_stub_plt_branch_r2off,
794e51c0 3981 ppc_stub_plt_call,
7341d5e2 3982 ppc_stub_plt_call_r2save,
a4b6fadd
AM
3983 ppc_stub_global_entry,
3984 ppc_stub_save_res
721956f4
AM
3985};
3986
6f20ed8a
AM
3987/* Information on stub grouping. */
3988struct map_stub
3989{
3990 /* The stub section. */
3991 asection *stub_sec;
3992 /* This is the section to which stubs in the group will be attached. */
3993 asection *link_sec;
a4b6fadd
AM
3994 /* Next group. */
3995 struct map_stub *next;
3996 /* Whether to emit a copy of register save/restore functions in this
3997 group. */
3998 int needs_save_res;
d4aaa2a0
AM
3999 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
4000 or -1u if no such stub with bctrl exists. */
4001 unsigned int tls_get_addr_opt_bctrl;
6f20ed8a
AM
4002};
4003
721956f4
AM
4004struct ppc_stub_hash_entry {
4005
4006 /* Base hash table entry structure. */
4007 struct bfd_hash_entry root;
4008
ad8e1ba5
AM
4009 enum ppc_stub_type stub_type;
4010
6f20ed8a
AM
4011 /* Group information. */
4012 struct map_stub *group;
721956f4
AM
4013
4014 /* Offset within stub_sec of the beginning of this stub. */
4015 bfd_vma stub_offset;
4016
4017 /* Given the symbol's value and its section we can determine its final
4018 value when building the stubs (so the stub knows where to jump. */
4019 bfd_vma target_value;
4020 asection *target_section;
4021
721956f4
AM
4022 /* The symbol table entry, if any, that this was derived from. */
4023 struct ppc_link_hash_entry *h;
e054468f 4024 struct plt_entry *plt_ent;
721956f4 4025
2d7ad24e
AM
4026 /* Symbol type. */
4027 unsigned char symtype;
4028
6911b7dc
AM
4029 /* Symbol st_other. */
4030 unsigned char other;
721956f4
AM
4031};
4032
4033struct ppc_branch_hash_entry {
4034
4035 /* Base hash table entry structure. */
4036 struct bfd_hash_entry root;
4037
c456f082 4038 /* Offset within branch lookup table. */
721956f4
AM
4039 unsigned int offset;
4040
4041 /* Generation marker. */
4042 unsigned int iter;
4043};
65f38f15 4044
19e08130
AM
4045/* Used to track dynamic relocations for local symbols. */
4046struct ppc_dyn_relocs
4047{
4048 struct ppc_dyn_relocs *next;
4049
4050 /* The input section of the reloc. */
4051 asection *sec;
4052
4053 /* Total number of relocs copied for the input section. */
4054 unsigned int count : 31;
4055
4056 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4057 unsigned int ifunc : 1;
4058};
4059
65f38f15
AM
4060struct ppc_link_hash_entry
4061{
4062 struct elf_link_hash_entry elf;
4063
b3fac117
AM
4064 union {
4065 /* A pointer to the most recently used stub hash entry against this
4066 symbol. */
4067 struct ppc_stub_hash_entry *stub_cache;
4068
4069 /* A pointer to the next symbol starting with a '.' */
4070 struct ppc_link_hash_entry *next_dot_sym;
4071 } u;
721956f4 4072
65f38f15 4073 /* Track dynamic relocs copied for this symbol. */
6061a67d 4074 struct elf_dyn_relocs *dyn_relocs;
e86ce104 4075
721956f4 4076 /* Link between function code and descriptor symbols. */
34814b9f 4077 struct ppc_link_hash_entry *oh;
721956f4 4078
e86ce104
AM
4079 /* Flag function code and descriptor symbols. */
4080 unsigned int is_func:1;
4081 unsigned int is_func_descriptor:1;
908b32fc 4082 unsigned int fake:1;
411e1bfb 4083
c5614fa4
AM
4084 /* Whether global opd/toc sym has been adjusted or not.
4085 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4086 should be set for all globals defined in any opd/toc section. */
754021d0
AM
4087 unsigned int adjust_done:1;
4088
a4b6fadd
AM
4089 /* Set if this is an out-of-line register save/restore function,
4090 with non-standard calling convention. */
4091 unsigned int save_res:1;
4092
8b5f1ed8
AM
4093 /* Set if a duplicate symbol with non-zero localentry is detected,
4094 even when the duplicate symbol does not provide a definition. */
4095 unsigned int non_zero_localentry:1;
4096
411e1bfb 4097 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
4098 Bits are or'd into the mask as the corresponding relocs are
4099 encountered during check_relocs, with TLS_TLS being set when any
4100 of the other TLS bits are set. tls_optimize clears bits when
4101 optimizing to indicate the corresponding GOT entry type is not
4102 needed. If set, TLS_TLS is never cleared. tls_optimize may also
4103 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
4104 separate flag rather than setting TPREL just for convenience in
4105 distinguishing the two cases.
4106 These flags are also kept for local symbols. */
4107#define TLS_TLS 1 /* Any TLS reloc. */
4108#define TLS_GD 2 /* GD reloc. */
4109#define TLS_LD 4 /* LD reloc. */
4110#define TLS_TPREL 8 /* TPREL reloc, => IE. */
4111#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
4112#define TLS_MARK 32 /* __tls_get_addr call marked. */
e7b938ca 4113#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
37da22e5 4114#define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
f961d9dd 4115 unsigned char tls_mask;
37da22e5
AM
4116
4117 /* The above field is also used to mark function symbols. In which
4118 case TLS_TLS will be 0. */
4119#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 4120#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 4121#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
4122};
4123
4124/* ppc64 ELF linker hash table. */
4125
4126struct ppc_link_hash_table
4127{
4128 struct elf_link_hash_table elf;
4129
721956f4
AM
4130 /* The stub hash table. */
4131 struct bfd_hash_table stub_hash_table;
4132
4133 /* Another hash table for plt_branch stubs. */
4134 struct bfd_hash_table branch_hash_table;
4135
3b421ab3
AM
4136 /* Hash table for function prologue tocsave. */
4137 htab_t tocsave_htab;
4138
e7d1c40c
AM
4139 /* Various options and other info passed from the linker. */
4140 struct ppc64_elf_params *params;
721956f4 4141
6f20ed8a
AM
4142 /* The size of sec_info below. */
4143 unsigned int sec_info_arr_size;
4144
4145 /* Per-section array of extra section info. Done this way rather
4146 than as part of ppc64_elf_section_data so we have the info for
4147 non-ppc64 sections. */
4148 struct
4149 {
4150 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 4151 bfd_vma toc_off;
6f20ed8a
AM
4152
4153 union
4154 {
4155 /* The section group that this section belongs to. */
4156 struct map_stub *group;
4157 /* A temp section list pointer. */
4158 asection *list;
4159 } u;
4160 } *sec_info;
721956f4 4161
a4b6fadd
AM
4162 /* Linked list of groups. */
4163 struct map_stub *group;
4164
ad8e1ba5
AM
4165 /* Temp used when calculating TOC pointers. */
4166 bfd_vma toc_curr;
bf102f86
AM
4167 bfd *toc_bfd;
4168 asection *toc_first_sec;
ad8e1ba5 4169
b3fac117
AM
4170 /* Used when adding symbols. */
4171 struct ppc_link_hash_entry *dot_syms;
4172
33e44f2e 4173 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 4174 asection *glink;
9e390558 4175 asection *global_entry;
82bd7b59 4176 asection *sfpr;
2d7ad24e
AM
4177 asection *pltlocal;
4178 asection *relpltlocal;
4ce794b7
AM
4179 asection *brlt;
4180 asection *relbrlt;
58d180e8 4181 asection *glink_eh_frame;
ec338859 4182
8387904d
AM
4183 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4184 struct ppc_link_hash_entry *tls_get_addr;
4185 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4186
927be08e
AM
4187 /* The size of reliplt used by got entry relocs. */
4188 bfd_size_type got_reli_size;
4189
9b5ecbd0 4190 /* Statistics. */
7341d5e2 4191 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4192
ee75fd95
AM
4193 /* Number of stubs against global syms. */
4194 unsigned long stub_globals;
4195
ee67d69a
AM
4196 /* Set if we're linking code with function descriptors. */
4197 unsigned int opd_abi:1;
4198
4c52953f 4199 /* Support for multiple toc sections. */
33c0ec9d 4200 unsigned int do_multi_toc:1;
4c52953f 4201 unsigned int multi_toc_needed:1;
927be08e 4202 unsigned int second_toc_pass:1;
67f0cbdb 4203 unsigned int do_toc_opt:1;
4c52953f 4204
9a23f96e
AM
4205 /* Set if tls optimization is enabled. */
4206 unsigned int do_tls_opt:1;
4207
3e04d765
AM
4208 /* Set if inline plt calls should be converted to direct calls. */
4209 unsigned int can_convert_all_inline_plt:1;
4210
5d1634d7 4211 /* Set on error. */
99877b66 4212 unsigned int stub_error:1;
721956f4 4213
8c5b4e52
AM
4214 /* Whether func_desc_adjust needs to be run over symbols. */
4215 unsigned int need_func_desc_adj:1;
721956f4 4216
82e66161
AM
4217 /* Whether there exist local gnu indirect function resolvers,
4218 referenced by dynamic relocations. */
4219 unsigned int local_ifunc_resolver:1;
4220 unsigned int maybe_local_ifunc_resolver:1;
4221
f378ab09
AM
4222 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4223 unsigned int has_plt_localentry0:1;
4224
721956f4
AM
4225 /* Incremented every time we size stubs. */
4226 unsigned int stub_iteration;
5d1634d7 4227
87d72d41
AM
4228 /* Small local sym cache. */
4229 struct sym_cache sym_cache;
65f38f15
AM
4230};
4231
4c52953f
AM
4232/* Rename some of the generic section flags to better document how they
4233 are used here. */
b0dddeec
AM
4234
4235/* Nonzero if this section has TLS related relocations. */
4236#define has_tls_reloc sec_flg0
4237
23cedd1d 4238/* Nonzero if this section has an old-style call to __tls_get_addr. */
b0dddeec
AM
4239#define has_tls_get_addr_call sec_flg1
4240
4241/* Nonzero if this section has any toc or got relocs. */
4242#define has_toc_reloc sec_flg2
4243
4244/* Nonzero if this section has a call to another section that uses
4245 the toc or got. */
d77c8a4b 4246#define makes_toc_func_call sec_flg3
b0dddeec
AM
4247
4248/* Recursion protection when determining above flag. */
d77c8a4b 4249#define call_check_in_progress sec_flg4
70cc837d 4250#define call_check_done sec_flg5
4c52953f 4251
65f38f15
AM
4252/* Get the ppc64 ELF linker hash table from a link_info structure. */
4253
4254#define ppc_hash_table(p) \
4dfe6ac6
NC
4255 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4256 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4257
721956f4
AM
4258#define ppc_stub_hash_lookup(table, string, create, copy) \
4259 ((struct ppc_stub_hash_entry *) \
4260 bfd_hash_lookup ((table), (string), (create), (copy)))
4261
4262#define ppc_branch_hash_lookup(table, string, create, copy) \
4263 ((struct ppc_branch_hash_entry *) \
4264 bfd_hash_lookup ((table), (string), (create), (copy)))
4265
4266/* Create an entry in the stub hash table. */
4267
4268static struct bfd_hash_entry *
4ce794b7
AM
4269stub_hash_newfunc (struct bfd_hash_entry *entry,
4270 struct bfd_hash_table *table,
4271 const char *string)
721956f4
AM
4272{
4273 /* Allocate the structure if it has not already been allocated by a
4274 subclass. */
4275 if (entry == NULL)
4276 {
4277 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4278 if (entry == NULL)
4279 return entry;
4280 }
4281
4282 /* Call the allocation method of the superclass. */
4283 entry = bfd_hash_newfunc (entry, table, string);
4284 if (entry != NULL)
4285 {
4286 struct ppc_stub_hash_entry *eh;
4287
4288 /* Initialize the local fields. */
4289 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4290 eh->stub_type = ppc_stub_none;
6f20ed8a 4291 eh->group = NULL;
721956f4
AM
4292 eh->stub_offset = 0;
4293 eh->target_value = 0;
4294 eh->target_section = NULL;
721956f4 4295 eh->h = NULL;
6911b7dc 4296 eh->plt_ent = NULL;
6911b7dc 4297 eh->other = 0;
721956f4
AM
4298 }
4299
4300 return entry;
4301}
4302
4303/* Create an entry in the branch hash table. */
4304
4305static struct bfd_hash_entry *
4ce794b7
AM
4306branch_hash_newfunc (struct bfd_hash_entry *entry,
4307 struct bfd_hash_table *table,
4308 const char *string)
721956f4
AM
4309{
4310 /* Allocate the structure if it has not already been allocated by a
4311 subclass. */
4312 if (entry == NULL)
4313 {
4314 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4315 if (entry == NULL)
4316 return entry;
4317 }
4318
4319 /* Call the allocation method of the superclass. */
4320 entry = bfd_hash_newfunc (entry, table, string);
4321 if (entry != NULL)
4322 {
4323 struct ppc_branch_hash_entry *eh;
4324
4325 /* Initialize the local fields. */
4326 eh = (struct ppc_branch_hash_entry *) entry;
4327 eh->offset = 0;
4328 eh->iter = 0;
4329 }
4330
4331 return entry;
4332}
4333
65f38f15
AM
4334/* Create an entry in a ppc64 ELF linker hash table. */
4335
4336static struct bfd_hash_entry *
4ce794b7
AM
4337link_hash_newfunc (struct bfd_hash_entry *entry,
4338 struct bfd_hash_table *table,
4339 const char *string)
65f38f15
AM
4340{
4341 /* Allocate the structure if it has not already been allocated by a
4342 subclass. */
4343 if (entry == NULL)
4344 {
4345 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4346 if (entry == NULL)
4347 return entry;
4348 }
4349
4350 /* Call the allocation method of the superclass. */
4351 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4352 if (entry != NULL)
4353 {
4354 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4355
b3fac117 4356 memset (&eh->u.stub_cache, 0,
908b32fc 4357 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4358 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4359
4360 /* When making function calls, old ABI code references function entry
4361 points (dot symbols), while new ABI code references the function
4362 descriptor symbol. We need to make any combination of reference and
4363 definition work together, without breaking archive linking.
4364
4365 For a defined function "foo" and an undefined call to "bar":
4366 An old object defines "foo" and ".foo", references ".bar" (possibly
4367 "bar" too).
4368 A new object defines "foo" and references "bar".
4369
4370 A new object thus has no problem with its undefined symbols being
4371 satisfied by definitions in an old object. On the other hand, the
4372 old object won't have ".bar" satisfied by a new object.
4373
4374 Keep a list of newly added dot-symbols. */
4375
4376 if (string[0] == '.')
4377 {
4378 struct ppc_link_hash_table *htab;
4379
4380 htab = (struct ppc_link_hash_table *) table;
4381 eh->u.next_dot_sym = htab->dot_syms;
4382 htab->dot_syms = eh;
4383 }
65f38f15
AM
4384 }
4385
4386 return entry;
4387}
4388
3b421ab3
AM
4389struct tocsave_entry {
4390 asection *sec;
4391 bfd_vma offset;
4392};
4393
4394static hashval_t
4395tocsave_htab_hash (const void *p)
4396{
4397 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 4398 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
4399}
4400
4401static int
4402tocsave_htab_eq (const void *p1, const void *p2)
4403{
4404 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4405 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4406 return e1->sec == e2->sec && e1->offset == e2->offset;
4407}
4408
68faa637
AM
4409/* Destroy a ppc64 ELF linker hash table. */
4410
4411static void
d495ab0d 4412ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4413{
d495ab0d 4414 struct ppc_link_hash_table *htab;
68faa637 4415
d495ab0d 4416 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4417 if (htab->tocsave_htab)
4418 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4419 bfd_hash_table_free (&htab->branch_hash_table);
4420 bfd_hash_table_free (&htab->stub_hash_table);
4421 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4422}
4423
65f38f15
AM
4424/* Create a ppc64 ELF linker hash table. */
4425
4426static struct bfd_link_hash_table *
4ce794b7 4427ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4428{
4429 struct ppc_link_hash_table *htab;
4430 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4431
4ce794b7 4432 htab = bfd_zmalloc (amt);
65f38f15
AM
4433 if (htab == NULL)
4434 return NULL;
4435
66eb6687 4436 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4437 sizeof (struct ppc_link_hash_entry),
4438 PPC64_ELF_DATA))
65f38f15 4439 {
e2d34d7d 4440 free (htab);
65f38f15
AM
4441 return NULL;
4442 }
4443
721956f4 4444 /* Init the stub hash table too. */
66eb6687
AM
4445 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4446 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4447 {
d495ab0d 4448 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4449 return NULL;
4450 }
721956f4
AM
4451
4452 /* And the branch hash table. */
66eb6687
AM
4453 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4454 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4455 {
4456 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4457 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4458 return NULL;
4459 }
721956f4 4460
3b421ab3
AM
4461 htab->tocsave_htab = htab_try_create (1024,
4462 tocsave_htab_hash,
4463 tocsave_htab_eq,
4464 NULL);
4465 if (htab->tocsave_htab == NULL)
2915c55b 4466 {
d495ab0d 4467 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4468 return NULL;
4469 }
d495ab0d 4470 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4471
3254fd24
AM
4472 /* Initializing two fields of the union is just cosmetic. We really
4473 only care about glist, but when compiled on a 32-bit host the
4474 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4475 debugger inspection of these fields look nicer. */
a6aa5195
AM
4476 htab->elf.init_got_refcount.refcount = 0;
4477 htab->elf.init_got_refcount.glist = NULL;
4478 htab->elf.init_plt_refcount.refcount = 0;
4479 htab->elf.init_plt_refcount.glist = NULL;
4480 htab->elf.init_got_offset.offset = 0;
4481 htab->elf.init_got_offset.glist = NULL;
4482 htab->elf.init_plt_offset.offset = 0;
4483 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4484
65f38f15
AM
4485 return &htab->elf.root;
4486}
4487
bfeb4a28
AM
4488/* Create sections for linker generated code. */
4489
4490static bfd_boolean
4491create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4492{
4493 struct ppc_link_hash_table *htab;
4494 flagword flags;
4495
4496 htab = ppc_hash_table (info);
4497
bfeb4a28
AM
4498 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4499 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4500 if (htab->params->save_restore_funcs)
4501 {
4502 /* Create .sfpr for code to save and restore fp regs. */
4503 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4504 flags);
4505 if (htab->sfpr == NULL
4506 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4507 return FALSE;
4508 }
4509
4510 if (bfd_link_relocatable (info))
4511 return TRUE;
bfeb4a28
AM
4512
4513 /* Create .glink for lazy dynamic linking support. */
4514 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4515 flags);
4516 if (htab->glink == NULL
4517 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4518 return FALSE;
4519
9e390558
AM
4520 /* The part of .glink used by global entry stubs, separate so that
4521 it can be aligned appropriately without affecting htab->glink. */
4522 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4523 flags);
4524 if (htab->global_entry == NULL
4525 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4526 return FALSE;
4527
bfeb4a28
AM
4528 if (!info->no_ld_generated_unwind_info)
4529 {
4530 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4531 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4532 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4533 ".eh_frame",
4534 flags);
4535 if (htab->glink_eh_frame == NULL
4536 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4537 return FALSE;
4538 }
4539
4540 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4541 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4542 if (htab->elf.iplt == NULL
4543 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4544 return FALSE;
4545
4546 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4547 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4548 htab->elf.irelplt
4549 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4550 if (htab->elf.irelplt == NULL
4551 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4552 return FALSE;
4553
4554 /* Create branch lookup table for plt_branch stubs. */
4555 flags = (SEC_ALLOC | SEC_LOAD
4556 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4557 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4558 flags);
4559 if (htab->brlt == NULL
4560 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4561 return FALSE;
4562
2d7ad24e
AM
4563 /* Local plt entries, put in .branch_lt but a separate section for
4564 convenience. */
4565 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4566 flags);
4567 if (htab->pltlocal == NULL
4568 || ! bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
4569 return FALSE;
4570
0e1862bb 4571 if (!bfd_link_pic (info))
bfeb4a28
AM
4572 return TRUE;
4573
4574 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4575 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
4576 htab->relbrlt
4577 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28
AM
4578 if (htab->relbrlt == NULL
4579 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4580 return FALSE;
4581
2d7ad24e
AM
4582 htab->relpltlocal
4583 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4584 if (htab->relpltlocal == NULL
4585 || ! bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
4586 return FALSE;
4587
bfeb4a28
AM
4588 return TRUE;
4589}
4590
e717da7e
AM
4591/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4592
bfeb4a28 4593bfd_boolean
e7d1c40c
AM
4594ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4595 struct ppc64_elf_params *params)
e717da7e
AM
4596{
4597 struct ppc_link_hash_table *htab;
4598
e7d1c40c 4599 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4600
4601/* Always hook our dynamic sections into the first bfd, which is the
4602 linker created stub bfd. This ensures that the GOT header is at
4603 the start of the output TOC section. */
4604 htab = ppc_hash_table (info);
e7d1c40c
AM
4605 htab->elf.dynobj = params->stub_bfd;
4606 htab->params = params;
bfeb4a28 4607
bfeb4a28 4608 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4609}
4610
721956f4
AM
4611/* Build a name for an entry in the stub hash table. */
4612
4613static char *
4ce794b7
AM
4614ppc_stub_name (const asection *input_section,
4615 const asection *sym_sec,
4616 const struct ppc_link_hash_entry *h,
4617 const Elf_Internal_Rela *rel)
721956f4
AM
4618{
4619 char *stub_name;
bcaa2f82 4620 ssize_t len;
721956f4
AM
4621
4622 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4623 offsets from a sym as a branch target? In fact, we could
4624 probably assume the addend is always zero. */
4625 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4626
4627 if (h)
4628 {
4629 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4630 stub_name = bfd_malloc (len);
46de2a7c
AM
4631 if (stub_name == NULL)
4632 return stub_name;
4633
bcaa2f82
AM
4634 len = sprintf (stub_name, "%08x.%s+%x",
4635 input_section->id & 0xffffffff,
4636 h->elf.root.root.string,
4637 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4638 }
4639 else
4640 {
ad8e1ba5 4641 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4642 stub_name = bfd_malloc (len);
46de2a7c
AM
4643 if (stub_name == NULL)
4644 return stub_name;
4645
bcaa2f82
AM
4646 len = sprintf (stub_name, "%08x.%x:%x+%x",
4647 input_section->id & 0xffffffff,
4648 sym_sec->id & 0xffffffff,
4649 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4650 (int) rel->r_addend & 0xffffffff);
721956f4 4651 }
bcaa2f82 4652 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4653 stub_name[len - 2] = 0;
721956f4
AM
4654 return stub_name;
4655}
4656
4657/* Look up an entry in the stub hash. Stub entries are cached because
4658 creating the stub name takes a bit of time. */
4659
4660static struct ppc_stub_hash_entry *
4ce794b7
AM
4661ppc_get_stub_entry (const asection *input_section,
4662 const asection *sym_sec,
039b3fef 4663 struct ppc_link_hash_entry *h,
4ce794b7
AM
4664 const Elf_Internal_Rela *rel,
4665 struct ppc_link_hash_table *htab)
721956f4
AM
4666{
4667 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4668 struct map_stub *group;
721956f4
AM
4669
4670 /* If this input section is part of a group of sections sharing one
4671 stub section, then use the id of the first section in the group.
4672 Stub names need to include a section id, as there may well be
4673 more than one stub used to reach say, printf, and we need to
4674 distinguish between them. */
6f20ed8a 4675 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4676 if (group == NULL)
4677 return NULL;
721956f4 4678
b3fac117
AM
4679 if (h != NULL && h->u.stub_cache != NULL
4680 && h->u.stub_cache->h == h
6f20ed8a 4681 && h->u.stub_cache->group == group)
721956f4 4682 {
b3fac117 4683 stub_entry = h->u.stub_cache;
721956f4
AM
4684 }
4685 else
4686 {
4687 char *stub_name;
4688
6f20ed8a 4689 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4690 if (stub_name == NULL)
4691 return NULL;
4692
4693 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4694 stub_name, FALSE, FALSE);
721956f4 4695 if (h != NULL)
b3fac117 4696 h->u.stub_cache = stub_entry;
721956f4
AM
4697
4698 free (stub_name);
4699 }
4700
4701 return stub_entry;
4702}
4703
4704/* Add a new stub entry to the stub hash. Not all fields of the new
4705 stub entry are initialised. */
4706
4707static struct ppc_stub_hash_entry *
4ce794b7
AM
4708ppc_add_stub (const char *stub_name,
4709 asection *section,
25f53a85 4710 struct bfd_link_info *info)
721956f4 4711{
25f53a85 4712 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4713 struct map_stub *group;
721956f4
AM
4714 asection *link_sec;
4715 asection *stub_sec;
4716 struct ppc_stub_hash_entry *stub_entry;
4717
6f20ed8a
AM
4718 group = htab->sec_info[section->id].u.group;
4719 link_sec = group->link_sec;
4720 stub_sec = group->stub_sec;
721956f4
AM
4721 if (stub_sec == NULL)
4722 {
6f20ed8a
AM
4723 size_t namelen;
4724 bfd_size_type len;
4725 char *s_name;
721956f4 4726
6f20ed8a
AM
4727 namelen = strlen (link_sec->name);
4728 len = namelen + sizeof (STUB_SUFFIX);
4729 s_name = bfd_alloc (htab->params->stub_bfd, len);
4730 if (s_name == NULL)
4731 return NULL;
721956f4 4732
6f20ed8a
AM
4733 memcpy (s_name, link_sec->name, namelen);
4734 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4735 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4736 if (stub_sec == NULL)
4737 return NULL;
4738 group->stub_sec = stub_sec;
721956f4
AM
4739 }
4740
4741 /* Enter this entry into the linker stub hash table. */
4742 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4743 TRUE, FALSE);
721956f4
AM
4744 if (stub_entry == NULL)
4745 {
695344c0 4746 /* xgettext:c-format */
cf97bcb0
AM
4747 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4748 section->owner, stub_name);
721956f4
AM
4749 return NULL;
4750 }
4751
6f20ed8a 4752 stub_entry->group = group;
721956f4 4753 stub_entry->stub_offset = 0;
721956f4
AM
4754 return stub_entry;
4755}
4756
e717da7e
AM
4757/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4758 not already done. */
65f38f15 4759
b34976b6 4760static bfd_boolean
e717da7e 4761create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4762{
e717da7e
AM
4763 asection *got, *relgot;
4764 flagword flags;
4765 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4766
0c8d6e5c 4767 if (!is_ppc64_elf (abfd))
0ffa91dd 4768 return FALSE;
4dfe6ac6
NC
4769 if (htab == NULL)
4770 return FALSE;
0ffa91dd 4771
33e44f2e
AM
4772 if (!htab->elf.sgot
4773 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4774 return FALSE;
e717da7e
AM
4775
4776 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4777 | SEC_LINKER_CREATED);
4778
c456f082 4779 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4780 if (!got
e717da7e
AM
4781 || !bfd_set_section_alignment (abfd, got, 3))
4782 return FALSE;
65f38f15 4783
c456f082
AM
4784 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4785 flags | SEC_READONLY);
e717da7e 4786 if (!relgot
e717da7e 4787 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4788 return FALSE;
e717da7e
AM
4789
4790 ppc64_elf_tdata (abfd)->got = got;
4791 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4792 return TRUE;
65f38f15 4793}
5bd4f169 4794
b31867b6
AM
4795/* Follow indirect and warning symbol links. */
4796
4797static inline struct bfd_link_hash_entry *
4798follow_link (struct bfd_link_hash_entry *h)
4799{
4800 while (h->type == bfd_link_hash_indirect
4801 || h->type == bfd_link_hash_warning)
4802 h = h->u.i.link;
4803 return h;
4804}
4805
4806static inline struct elf_link_hash_entry *
4807elf_follow_link (struct elf_link_hash_entry *h)
4808{
4809 return (struct elf_link_hash_entry *) follow_link (&h->root);
4810}
4811
4812static inline struct ppc_link_hash_entry *
4813ppc_follow_link (struct ppc_link_hash_entry *h)
4814{
4815 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4816}
4817
40d16e0b
AM
4818/* Merge PLT info on FROM with that on TO. */
4819
4820static void
4821move_plt_plist (struct ppc_link_hash_entry *from,
4822 struct ppc_link_hash_entry *to)
4823{
4824 if (from->elf.plt.plist != NULL)
4825 {
4826 if (to->elf.plt.plist != NULL)
4827 {
4828 struct plt_entry **entp;
4829 struct plt_entry *ent;
4830
4831 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4832 {
4833 struct plt_entry *dent;
4834
4835 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4836 if (dent->addend == ent->addend)
4837 {
4838 dent->plt.refcount += ent->plt.refcount;
4839 *entp = ent->next;
4840 break;
4841 }
4842 if (dent == NULL)
4843 entp = &ent->next;
4844 }
4845 *entp = to->elf.plt.plist;
4846 }
4847
4848 to->elf.plt.plist = from->elf.plt.plist;
4849 from->elf.plt.plist = NULL;
4850 }
4851}
4852
65f38f15
AM
4853/* Copy the extra info we tack onto an elf_link_hash_entry. */
4854
4855static void
fcfa13d2
AM
4856ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4857 struct elf_link_hash_entry *dir,
4858 struct elf_link_hash_entry *ind)
65f38f15
AM
4859{
4860 struct ppc_link_hash_entry *edir, *eind;
4861
4862 edir = (struct ppc_link_hash_entry *) dir;
4863 eind = (struct ppc_link_hash_entry *) ind;
4864
c79d6685
AM
4865 edir->is_func |= eind->is_func;
4866 edir->is_func_descriptor |= eind->is_func_descriptor;
4867 edir->tls_mask |= eind->tls_mask;
4868 if (eind->oh != NULL)
4869 edir->oh = ppc_follow_link (eind->oh);
4870
474436e6 4871 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4872 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4873 edir->elf.ref_regular |= eind->elf.ref_regular;
4874 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 4875 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
4876 edir->elf.needs_plt |= eind->elf.needs_plt;
4877 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4878
d311bc8b
AM
4879 /* If we were called to copy over info for a weak sym, don't copy
4880 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4881 in order to simplify readonly_dynrelocs and save a field in the
4882 symbol hash entry, but that means dyn_relocs can't be used in any
4883 tests about a specific symbol, or affect other symbol flags which
ab2477e1 4884 are then tested. */
d311bc8b 4885 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 4886 return;
d311bc8b 4887
411e1bfb 4888 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4889 if (eind->dyn_relocs != NULL)
65f38f15 4890 {
bbd7ec4a
AM
4891 if (edir->dyn_relocs != NULL)
4892 {
6061a67d
AM
4893 struct elf_dyn_relocs **pp;
4894 struct elf_dyn_relocs *p;
bbd7ec4a 4895
fcfa13d2 4896 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4897 list. Merge any entries against the same section. */
4898 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4899 {
6061a67d 4900 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4901
4902 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4903 if (q->sec == p->sec)
4904 {
4905 q->pc_count += p->pc_count;
4906 q->count += p->count;
4907 *pp = p->next;
4908 break;
4909 }
4910 if (q == NULL)
4911 pp = &p->next;
4912 }
4913 *pp = edir->dyn_relocs;
4914 }
4915
65f38f15
AM
4916 edir->dyn_relocs = eind->dyn_relocs;
4917 eind->dyn_relocs = NULL;
4918 }
65f38f15 4919
81848ca0
AM
4920 /* Copy over got entries that we may have already seen to the
4921 symbol which just became indirect. */
411e1bfb
AM
4922 if (eind->elf.got.glist != NULL)
4923 {
4924 if (edir->elf.got.glist != NULL)
4925 {
4926 struct got_entry **entp;
4927 struct got_entry *ent;
4928
4929 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4930 {
4931 struct got_entry *dent;
4932
4933 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4934 if (dent->addend == ent->addend
e717da7e 4935 && dent->owner == ent->owner
411e1bfb
AM
4936 && dent->tls_type == ent->tls_type)
4937 {
4938 dent->got.refcount += ent->got.refcount;
4939 *entp = ent->next;
4940 break;
4941 }
4942 if (dent == NULL)
4943 entp = &ent->next;
4944 }
4945 *entp = edir->elf.got.glist;
4946 }
4947
4948 edir->elf.got.glist = eind->elf.got.glist;
4949 eind->elf.got.glist = NULL;
4950 }
4951
4952 /* And plt entries. */
40d16e0b 4953 move_plt_plist (eind, edir);
411e1bfb 4954
fcfa13d2 4955 if (eind->elf.dynindx != -1)
411e1bfb 4956 {
fcfa13d2
AM
4957 if (edir->elf.dynindx != -1)
4958 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4959 edir->elf.dynstr_index);
411e1bfb
AM
4960 edir->elf.dynindx = eind->elf.dynindx;
4961 edir->elf.dynstr_index = eind->elf.dynstr_index;
4962 eind->elf.dynindx = -1;
4963 eind->elf.dynstr_index = 0;
4964 }
411e1bfb
AM
4965}
4966
8387904d
AM
4967/* Find the function descriptor hash entry from the given function code
4968 hash entry FH. Link the entries via their OH fields. */
4969
4970static struct ppc_link_hash_entry *
b31867b6 4971lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4972{
4973 struct ppc_link_hash_entry *fdh = fh->oh;
4974
4975 if (fdh == NULL)
4976 {
4977 const char *fd_name = fh->elf.root.root.string + 1;
4978
4979 fdh = (struct ppc_link_hash_entry *)
4980 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4981 if (fdh == NULL)
4982 return fdh;
4983
4984 fdh->is_func_descriptor = 1;
4985 fdh->oh = fh;
4986 fh->is_func = 1;
4987 fh->oh = fdh;
8387904d
AM
4988 }
4989
8c5b4e52
AM
4990 fdh = ppc_follow_link (fdh);
4991 fdh->is_func_descriptor = 1;
4992 fdh->oh = fh;
4993 return fdh;
8387904d
AM
4994}
4995
8c5b4e52 4996/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4997
4998static struct ppc_link_hash_entry *
4999make_fdh (struct bfd_link_info *info,
908b32fc 5000 struct ppc_link_hash_entry *fh)
bb700d78 5001{
8c5b4e52
AM
5002 bfd *abfd = fh->elf.root.u.undef.abfd;
5003 struct bfd_link_hash_entry *bh = NULL;
bb700d78 5004 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
5005 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
5006 ? BSF_WEAK
5007 : BSF_GLOBAL);
5008
5009 if (!_bfd_generic_link_add_one_symbol (info, abfd,
5010 fh->elf.root.root.string + 1,
5011 flags, bfd_und_section_ptr, 0,
5012 NULL, FALSE, FALSE, &bh))
bb700d78
AM
5013 return NULL;
5014
5015 fdh = (struct ppc_link_hash_entry *) bh;
5016 fdh->elf.non_elf = 0;
908b32fc
AM
5017 fdh->fake = 1;
5018 fdh->is_func_descriptor = 1;
5019 fdh->oh = fh;
5020 fh->is_func = 1;
5021 fh->oh = fdh;
bb700d78
AM
5022 return fdh;
5023}
5024
8387904d
AM
5025/* Fix function descriptor symbols defined in .opd sections to be
5026 function type. */
555cd476
AM
5027
5028static bfd_boolean
c16153ae 5029ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 5030 struct bfd_link_info *info,
555cd476 5031 Elf_Internal_Sym *isym,
6911b7dc 5032 const char **name,
555cd476
AM
5033 flagword *flags ATTRIBUTE_UNUSED,
5034 asection **sec,
b53dfeb2 5035 bfd_vma *value)
555cd476 5036{
b53dfeb2 5037 if (*sec != NULL
f1885d1e 5038 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
5039 {
5040 asection *code_sec;
5041
5042 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5043 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5044 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5045
5046 /* If the symbol is a function defined in .opd, and the function
5047 code is in a discarded group, let it appear to be undefined. */
0e1862bb 5048 if (!bfd_link_relocatable (info)
b53dfeb2
AM
5049 && (*sec)->reloc_count != 0
5050 && opd_entry_value (*sec, *value, &code_sec, NULL,
5051 FALSE) != (bfd_vma) -1
5052 && discarded_section (code_sec))
5053 {
5054 *sec = bfd_und_section_ptr;
5055 isym->st_shndx = SHN_UNDEF;
5056 }
5057 }
dbd1e97e
AM
5058 else if (*sec != NULL
5059 && strcmp ((*sec)->name, ".toc") == 0
5060 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5061 {
5062 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5063 if (htab != NULL)
5064 htab->params->object_in_toc = 1;
5065 }
433817dd 5066
6911b7dc
AM
5067 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5068 {
5069 if (abiversion (ibfd) == 0)
5070 set_abiversion (ibfd, 2);
5071 else if (abiversion (ibfd) == 1)
5072 {
cf97bcb0
AM
5073 _bfd_error_handler (_("symbol '%s' has invalid st_other"
5074 " for ABI version 1"), *name);
6911b7dc
AM
5075 bfd_set_error (bfd_error_bad_value);
5076 return FALSE;
5077 }
5078 }
5079
555cd476
AM
5080 return TRUE;
5081}
5082
6911b7dc
AM
5083/* Merge non-visibility st_other attributes: local entry point. */
5084
5085static void
5086ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5087 const Elf_Internal_Sym *isym,
5088 bfd_boolean definition,
5089 bfd_boolean dynamic)
5090{
f378ab09 5091 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
5092 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5093 | ELF_ST_VISIBILITY (h->other));
5094}
5095
8c5b4e52
AM
5096/* Hook called on merging a symbol. We use this to clear "fake" since
5097 we now have a real symbol. */
5098
5099static bfd_boolean
5100ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 5101 const Elf_Internal_Sym *isym,
8c5b4e52
AM
5102 asection **psec ATTRIBUTE_UNUSED,
5103 bfd_boolean newdef ATTRIBUTE_UNUSED,
5104 bfd_boolean olddef ATTRIBUTE_UNUSED,
5105 bfd *oldbfd ATTRIBUTE_UNUSED,
5106 const asection *oldsec ATTRIBUTE_UNUSED)
5107{
5108 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
5109 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5110 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
5111 return TRUE;
5112}
5113
8387904d 5114/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5115 inclusion of a new ABI object archive that defines "bar".
5116 NAME is a symbol defined in an archive. Return a symbol in the hash
5117 table that might be satisfied by the archive symbols. */
8387904d
AM
5118
5119static struct elf_link_hash_entry *
5120ppc64_elf_archive_symbol_lookup (bfd *abfd,
5121 struct bfd_link_info *info,
5122 const char *name)
5123{
5124 struct elf_link_hash_entry *h;
5125 char *dot_name;
5126 size_t len;
5127
5128 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5129 if (h != NULL
5130 /* Don't return this sym if it is a fake function descriptor
5131 created by add_symbol_adjust. */
8c5b4e52 5132 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
5133 return h;
5134
5135 if (name[0] == '.')
5136 return h;
5137
5138 len = strlen (name);
5139 dot_name = bfd_alloc (abfd, len + 2);
5140 if (dot_name == NULL)
e99955cd 5141 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5142 dot_name[0] = '.';
5143 memcpy (dot_name + 1, name, len + 1);
5144 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5145 bfd_release (abfd, dot_name);
5146 return h;
5147}
5148
5149/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5150 new ABI object defines "bar". Well, at least, undefined dot symbols
5151 are made weak. This stops later archive searches from including an
5152 object if we already have a function descriptor definition. It also
35b0ce59
AM
5153 prevents the linker complaining about undefined symbols.
5154 We also check and correct mismatched symbol visibility here. The
5155 most restrictive visibility of the function descriptor and the
5156 function entry symbol is used. */
8387904d
AM
5157
5158static bfd_boolean
b3fac117 5159add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5160{
8387904d
AM
5161 struct ppc_link_hash_table *htab;
5162 struct ppc_link_hash_entry *fdh;
5163
b3fac117
AM
5164 if (eh->elf.root.type == bfd_link_hash_warning)
5165 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5166
8400d40d
AM
5167 if (eh->elf.root.type == bfd_link_hash_indirect)
5168 return TRUE;
5169
b3fac117
AM
5170 if (eh->elf.root.root.string[0] != '.')
5171 abort ();
8387904d 5172
b3fac117 5173 htab = ppc_hash_table (info);
4dfe6ac6
NC
5174 if (htab == NULL)
5175 return FALSE;
5176
b31867b6 5177 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
5178 if (fdh == NULL
5179 && !bfd_link_relocatable (info)
5180 && (eh->elf.root.type == bfd_link_hash_undefined
5181 || eh->elf.root.type == bfd_link_hash_undefweak)
5182 && eh->elf.ref_regular)
5183 {
5184 /* Make an undefined function descriptor sym, in order to
5185 pull in an --as-needed shared lib. Archives are handled
5186 elsewhere. */
5187 fdh = make_fdh (info, eh);
5188 if (fdh == NULL)
5189 return FALSE;
bb700d78 5190 }
8c5b4e52
AM
5191
5192 if (fdh != NULL)
8387904d 5193 {
35b0ce59
AM
5194 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5195 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
5196
5197 /* Make both descriptor and entry symbol have the most
5198 constraining visibility of either symbol. */
35b0ce59
AM
5199 if (entry_vis < descr_vis)
5200 fdh->elf.other += entry_vis - descr_vis;
5201 else if (entry_vis > descr_vis)
5202 eh->elf.other += descr_vis - entry_vis;
5203
8c5b4e52
AM
5204 /* Propagate reference flags from entry symbol to function
5205 descriptor symbol. */
bc4e12de 5206 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 5207 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
5208 fdh->elf.ref_regular |= eh->elf.ref_regular;
5209 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5210
5211 if (!fdh->elf.forced_local
5212 && fdh->elf.dynindx == -1
5213 && fdh->elf.versioned != versioned_hidden
5214 && (bfd_link_dll (info)
5215 || fdh->elf.def_dynamic
5216 || fdh->elf.ref_dynamic)
5217 && (eh->elf.ref_regular
5218 || eh->elf.def_regular))
5219 {
5220 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5221 return FALSE;
35b0ce59 5222 }
8387904d 5223 }
99877b66 5224
8387904d
AM
5225 return TRUE;
5226}
5227
f6c7c3e8
AM
5228/* Set up opd section info and abiversion for IBFD, and process list
5229 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5230
8387904d 5231static bfd_boolean
f6c7c3e8 5232ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5233{
99877b66 5234 struct ppc_link_hash_table *htab;
b3fac117 5235 struct ppc_link_hash_entry **p, *eh;
459609d6 5236 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5237
459609d6 5238 if (opd != NULL && opd->size != 0)
b3fac117 5239 {
b9399fcf
AM
5240 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5241 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5242
459609d6
AM
5243 if (abiversion (ibfd) == 0)
5244 set_abiversion (ibfd, 1);
8a2058b5 5245 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5246 {
695344c0 5247 /* xgettext:c-format */
cf97bcb0
AM
5248 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5249 ibfd, abiversion (ibfd));
459609d6
AM
5250 bfd_set_error (bfd_error_bad_value);
5251 return FALSE;
f6c7c3e8 5252 }
b9399fcf 5253 }
f6c7c3e8 5254
b9399fcf
AM
5255 if (is_ppc64_elf (info->output_bfd))
5256 {
5257 /* For input files without an explicit abiversion in e_flags
5258 we should have flagged any with symbol st_other bits set
5259 as ELFv1 and above flagged those with .opd as ELFv2.
5260 Set the output abiversion if not yet set, and for any input
5261 still ambiguous, take its abiversion from the output.
5262 Differences in ABI are reported later. */
5263 if (abiversion (info->output_bfd) == 0)
5264 set_abiversion (info->output_bfd, abiversion (ibfd));
5265 else if (abiversion (ibfd) == 0)
5266 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
5267 }
5268
459609d6
AM
5269 htab = ppc_hash_table (info);
5270 if (htab == NULL)
b9399fcf 5271 return TRUE;
459609d6 5272
b9399fcf
AM
5273 if (opd != NULL && opd->size != 0
5274 && (ibfd->flags & DYNAMIC) == 0
5275 && (opd->flags & SEC_RELOC) != 0
5276 && opd->reloc_count != 0
5277 && !bfd_is_abs_section (opd->output_section)
5278 && info->gc_sections)
5279 {
5280 /* Garbage collection needs some extra help with .opd sections.
5281 We don't want to necessarily keep everything referenced by
5282 relocs in .opd, as that would keep all functions. Instead,
5283 if we reference an .opd symbol (a function descriptor), we
5284 want to keep the function code symbol's section. This is
5285 easy for global symbols, but for local syms we need to keep
5286 information about the associated function section. */
5287 bfd_size_type amt;
5288 asection **opd_sym_map;
5289 Elf_Internal_Shdr *symtab_hdr;
5290 Elf_Internal_Rela *relocs, *rel_end, *rel;
5291
5292 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5293 opd_sym_map = bfd_zalloc (ibfd, amt);
5294 if (opd_sym_map == NULL)
5295 return FALSE;
5296 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5297 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5298 info->keep_memory);
5299 if (relocs == NULL)
5300 return FALSE;
5301 symtab_hdr = &elf_symtab_hdr (ibfd);
5302 rel_end = relocs + opd->reloc_count - 1;
5303 for (rel = relocs; rel < rel_end; rel++)
5304 {
5305 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5306 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5307
5308 if (r_type == R_PPC64_ADDR64
5309 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5310 && r_symndx < symtab_hdr->sh_info)
5311 {
5312 Elf_Internal_Sym *isym;
5313 asection *s;
5314
5315 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5316 if (isym == NULL)
5317 {
5318 if (elf_section_data (opd)->relocs != relocs)
5319 free (relocs);
5320 return FALSE;
5321 }
5322
5323 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5324 if (s != NULL && s != opd)
5325 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5326 }
5327 }
5328 if (elf_section_data (opd)->relocs != relocs)
5329 free (relocs);
5330 }
459609d6
AM
5331
5332 p = &htab->dot_syms;
5333 while ((eh = *p) != NULL)
5334 {
5335 *p = NULL;
5336 if (&eh->elf == htab->elf.hgot)
5337 ;
5338 else if (htab->elf.hgot == NULL
5339 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5340 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
5341 else if (abiversion (ibfd) <= 1)
5342 {
5343 htab->need_func_desc_adj = 1;
5344 if (!add_symbol_adjust (eh, info))
5345 return FALSE;
5346 }
459609d6
AM
5347 p = &eh->u.next_dot_sym;
5348 }
b3fac117 5349 return TRUE;
8387904d
AM
5350}
5351
97fed1c9
JJ
5352/* Undo hash table changes when an --as-needed input file is determined
5353 not to be needed. */
5354
5355static bfd_boolean
e5034e59
AM
5356ppc64_elf_notice_as_needed (bfd *ibfd,
5357 struct bfd_link_info *info,
5358 enum notice_asneeded_action act)
97fed1c9 5359{
e5034e59
AM
5360 if (act == notice_not_needed)
5361 {
5362 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5363
e5034e59
AM
5364 if (htab == NULL)
5365 return FALSE;
4dfe6ac6 5366
e5034e59
AM
5367 htab->dot_syms = NULL;
5368 }
5369 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5370}
5371
aa374f67
AM
5372/* If --just-symbols against a final linked binary, then assume we need
5373 toc adjusting stubs when calling functions defined there. */
5374
5375static void
5376ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5377{
5378 if ((sec->flags & SEC_CODE) != 0
5379 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5380 && is_ppc64_elf (sec->owner))
5381 {
2c3f079f
AM
5382 if (abiversion (sec->owner) >= 2
5383 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5384 sec->has_toc_reloc = 1;
5385 }
5386 _bfd_elf_link_just_syms (sec, info);
5387}
5388
e054468f 5389static struct plt_entry **
4ce794b7
AM
5390update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5391 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5392{
5393 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5394 struct plt_entry **local_plt;
f961d9dd 5395 unsigned char *local_got_tls_masks;
411e1bfb
AM
5396
5397 if (local_got_ents == NULL)
5398 {
5399 bfd_size_type size = symtab_hdr->sh_info;
5400
e054468f
AM
5401 size *= (sizeof (*local_got_ents)
5402 + sizeof (*local_plt)
5403 + sizeof (*local_got_tls_masks));
4ce794b7 5404 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5405 if (local_got_ents == NULL)
e054468f 5406 return NULL;
411e1bfb
AM
5407 elf_local_got_ents (abfd) = local_got_ents;
5408 }
5409
37da22e5 5410 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5411 {
5412 struct got_entry *ent;
5413
5414 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5415 if (ent->addend == r_addend
5416 && ent->owner == abfd
5417 && ent->tls_type == tls_type)
411e1bfb
AM
5418 break;
5419 if (ent == NULL)
5420 {
5421 bfd_size_type amt = sizeof (*ent);
4ce794b7 5422 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5423 if (ent == NULL)
5424 return FALSE;
5425 ent->next = local_got_ents[r_symndx];
5426 ent->addend = r_addend;
e717da7e 5427 ent->owner = abfd;
411e1bfb 5428 ent->tls_type = tls_type;
927be08e 5429 ent->is_indirect = FALSE;
411e1bfb
AM
5430 ent->got.refcount = 0;
5431 local_got_ents[r_symndx] = ent;
5432 }
5433 ent->got.refcount += 1;
5434 }
5435
e054468f 5436 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5437 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 5438 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
5439
5440 return local_plt + r_symndx;
65f38f15
AM
5441}
5442
411e1bfb 5443static bfd_boolean
e054468f 5444update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5445{
411e1bfb 5446 struct plt_entry *ent;
1e2f5b6e 5447
e054468f 5448 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5449 if (ent->addend == addend)
5450 break;
5451 if (ent == NULL)
1e2f5b6e 5452 {
411e1bfb 5453 bfd_size_type amt = sizeof (*ent);
4ce794b7 5454 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5455 if (ent == NULL)
5456 return FALSE;
e054468f 5457 ent->next = *plist;
411e1bfb
AM
5458 ent->addend = addend;
5459 ent->plt.refcount = 0;
e054468f 5460 *plist = ent;
1e2f5b6e 5461 }
411e1bfb 5462 ent->plt.refcount += 1;
b34976b6 5463 return TRUE;
1e2f5b6e
AM
5464}
5465
e054468f
AM
5466static bfd_boolean
5467is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5468{
5469 return (r_type == R_PPC64_REL24
5470 || r_type == R_PPC64_REL14
5471 || r_type == R_PPC64_REL14_BRTAKEN
5472 || r_type == R_PPC64_REL14_BRNTAKEN
5473 || r_type == R_PPC64_ADDR24
5474 || r_type == R_PPC64_ADDR14
5475 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d
AM
5476 || r_type == R_PPC64_ADDR14_BRNTAKEN
5477 || r_type == R_PPC64_PLTCALL);
5478}
5479
5480/* Relocs on inline plt call sequence insns prior to the call. */
5481
5482static bfd_boolean
5483is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
5484{
5485 return (r_type == R_PPC64_PLT16_HA
5486 || r_type == R_PPC64_PLT16_HI
5487 || r_type == R_PPC64_PLT16_LO
5488 || r_type == R_PPC64_PLT16_LO_DS
5489 || r_type == R_PPC64_PLTSEQ);
e054468f
AM
5490}
5491
5bd4f169 5492/* Look through the relocs for a section during the first phase, and
65f38f15 5493 calculate needed space in the global offset table, procedure
5d1634d7 5494 linkage table, and dynamic reloc sections. */
5bd4f169 5495
b34976b6 5496static bfd_boolean
4ce794b7
AM
5497ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5498 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5499{
65f38f15 5500 struct ppc_link_hash_table *htab;
5bd4f169 5501 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5502 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5503 const Elf_Internal_Rela *rel;
5504 const Elf_Internal_Rela *rel_end;
5bd4f169 5505 asection *sreloc;
3a71aa26 5506 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 5507 bfd_boolean is_opd;
5bd4f169 5508
0e1862bb 5509 if (bfd_link_relocatable (info))
b34976b6 5510 return TRUE;
5bd4f169 5511
680a3378
AM
5512 /* Don't do anything special with non-loaded, non-alloced sections.
5513 In particular, any relocs in such sections should not affect GOT
5514 and PLT reference counting (ie. we don't allow them to create GOT
5515 or PLT entries), there's no possibility or desire to optimize TLS
5516 relocs, and there's not much point in propagating relocs to shared
5517 libs that the dynamic linker won't relocate. */
5518 if ((sec->flags & SEC_ALLOC) == 0)
5519 return TRUE;
5520
0c8d6e5c 5521 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5522
65f38f15 5523 htab = ppc_hash_table (info);
4dfe6ac6
NC
5524 if (htab == NULL)
5525 return FALSE;
5526
3a71aa26
AM
5527 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5528 FALSE, FALSE, TRUE);
5529 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5530 FALSE, FALSE, TRUE);
0ffa91dd 5531 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5532 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5533 sreloc = NULL;
b9399fcf 5534 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
5535 rel_end = relocs + sec->reloc_count;
5536 for (rel = relocs; rel < rel_end; rel++)
5537 {
5538 unsigned long r_symndx;
5539 struct elf_link_hash_entry *h;
04c9666a 5540 enum elf_ppc64_reloc_type r_type;
727fc41e 5541 int tls_type;
7c8fe5c4 5542 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5543 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5544
5545 r_symndx = ELF64_R_SYM (rel->r_info);
5546 if (r_symndx < symtab_hdr->sh_info)
5547 h = NULL;
5548 else
973a3492
L
5549 {
5550 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5551 h = elf_follow_link (h);
1c865ab2
AM
5552
5553 if (h == htab->elf.hgot)
5554 sec->has_toc_reloc = 1;
973a3492 5555 }
5bd4f169 5556
727fc41e 5557 tls_type = 0;
e054468f 5558 ifunc = NULL;
25f23106
AM
5559 if (h != NULL)
5560 {
5561 if (h->type == STT_GNU_IFUNC)
5562 {
5563 h->needs_plt = 1;
5564 ifunc = &h->plt.plist;
5565 }
5566 }
5567 else
5568 {
5569 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5570 abfd, r_symndx);
5571 if (isym == NULL)
5572 return FALSE;
5573
5574 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5575 {
5576 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
37da22e5
AM
5577 rel->r_addend,
5578 NON_GOT | PLT_IFUNC);
25f23106
AM
5579 if (ifunc == NULL)
5580 return FALSE;
5581 }
5582 }
727fc41e 5583
cbf95972 5584 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5585 switch (r_type)
5bd4f169 5586 {
727fc41e
AM
5587 case R_PPC64_TLSGD:
5588 case R_PPC64_TLSLD:
5589 /* These special tls relocs tie a call to __tls_get_addr with
5590 its parameter symbol. */
37da22e5
AM
5591 if (h != NULL)
5592 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5593 else
5594 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5595 rel->r_addend,
5596 NON_GOT | TLS_TLS | TLS_MARK))
5597 return FALSE;
5598 sec->has_tls_reloc = 1;
727fc41e
AM
5599 break;
5600
411e1bfb
AM
5601 case R_PPC64_GOT_TLSLD16:
5602 case R_PPC64_GOT_TLSLD16_LO:
5603 case R_PPC64_GOT_TLSLD16_HI:
5604 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5605 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5606 goto dogottls;
5607
5608 case R_PPC64_GOT_TLSGD16:
5609 case R_PPC64_GOT_TLSGD16_LO:
5610 case R_PPC64_GOT_TLSGD16_HI:
5611 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5612 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5613 goto dogottls;
5614
5615 case R_PPC64_GOT_TPREL16_DS:
5616 case R_PPC64_GOT_TPREL16_LO_DS:
5617 case R_PPC64_GOT_TPREL16_HI:
5618 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 5619 if (bfd_link_dll (info))
411e1bfb
AM
5620 info->flags |= DF_STATIC_TLS;
5621 tls_type = TLS_TLS | TLS_TPREL;
5622 goto dogottls;
5623
5624 case R_PPC64_GOT_DTPREL16_DS:
5625 case R_PPC64_GOT_DTPREL16_LO_DS:
5626 case R_PPC64_GOT_DTPREL16_HI:
5627 case R_PPC64_GOT_DTPREL16_HA:
5628 tls_type = TLS_TLS | TLS_DTPREL;
5629 dogottls:
5630 sec->has_tls_reloc = 1;
1a0670f3 5631 /* Fall through */
411e1bfb 5632
5bd4f169 5633 case R_PPC64_GOT16:
5bd4f169 5634 case R_PPC64_GOT16_DS:
65f38f15
AM
5635 case R_PPC64_GOT16_HA:
5636 case R_PPC64_GOT16_HI:
5637 case R_PPC64_GOT16_LO:
5bd4f169 5638 case R_PPC64_GOT16_LO_DS:
65f38f15 5639 /* This symbol requires a global offset table entry. */
4c52953f 5640 sec->has_toc_reloc = 1;
33c0ec9d
AM
5641 if (r_type == R_PPC64_GOT_TLSLD16
5642 || r_type == R_PPC64_GOT_TLSGD16
5643 || r_type == R_PPC64_GOT_TPREL16_DS
5644 || r_type == R_PPC64_GOT_DTPREL16_DS
5645 || r_type == R_PPC64_GOT16
5646 || r_type == R_PPC64_GOT16_DS)
5647 {
5648 htab->do_multi_toc = 1;
d77c8a4b 5649 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5650 }
5651
e717da7e
AM
5652 if (ppc64_elf_tdata (abfd)->got == NULL
5653 && !create_got_section (abfd, info))
b34976b6 5654 return FALSE;
5bd4f169
AM
5655
5656 if (h != NULL)
5657 {
411e1bfb
AM
5658 struct ppc_link_hash_entry *eh;
5659 struct got_entry *ent;
65f38f15 5660
411e1bfb
AM
5661 eh = (struct ppc_link_hash_entry *) h;
5662 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5663 if (ent->addend == rel->r_addend
e717da7e 5664 && ent->owner == abfd
411e1bfb
AM
5665 && ent->tls_type == tls_type)
5666 break;
5667 if (ent == NULL)
5bd4f169 5668 {
411e1bfb 5669 bfd_size_type amt = sizeof (*ent);
4ce794b7 5670 ent = bfd_alloc (abfd, amt);
411e1bfb 5671 if (ent == NULL)
b34976b6 5672 return FALSE;
411e1bfb
AM
5673 ent->next = eh->elf.got.glist;
5674 ent->addend = rel->r_addend;
e717da7e 5675 ent->owner = abfd;
411e1bfb 5676 ent->tls_type = tls_type;
927be08e 5677 ent->is_indirect = FALSE;
411e1bfb
AM
5678 ent->got.refcount = 0;
5679 eh->elf.got.glist = ent;
5bd4f169 5680 }
411e1bfb 5681 ent->got.refcount += 1;
e7b938ca 5682 eh->tls_mask |= tls_type;
5bd4f169 5683 }
411e1bfb
AM
5684 else
5685 /* This is a global offset table entry for a local symbol. */
5686 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5687 rel->r_addend, tls_type))
5688 return FALSE;
a345bc8d
AM
5689
5690 /* We may also need a plt entry if the symbol turns out to be
5691 an ifunc. */
0e1862bb 5692 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5693 {
5694 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5695 return FALSE;
5696 }
5bd4f169
AM
5697 break;
5698
5bd4f169 5699 case R_PPC64_PLT16_HA:
65f38f15
AM
5700 case R_PPC64_PLT16_HI:
5701 case R_PPC64_PLT16_LO:
08be3224 5702 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
5703 case R_PPC64_PLT32:
5704 case R_PPC64_PLT64:
cbf95972
AM
5705 /* This symbol requires a procedure linkage table entry. */
5706 plt_list = ifunc;
5707 if (h != NULL)
e054468f 5708 {
e054468f
AM
5709 h->needs_plt = 1;
5710 if (h->root.root.string[0] == '.'
5711 && h->root.root.string[1] != '\0')
5712 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2d7ad24e 5713 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
cbf95972
AM
5714 plt_list = &h->plt.plist;
5715 }
5716 if (plt_list == NULL)
2d7ad24e
AM
5717 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5718 rel->r_addend,
5719 NON_GOT | PLT_KEEP);
cbf95972
AM
5720 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5721 return FALSE;
5bd4f169
AM
5722 break;
5723
5724 /* The following relocations don't need to propagate the
5725 relocation if linking a shared object since they are
5726 section relative. */
5727 case R_PPC64_SECTOFF:
5728 case R_PPC64_SECTOFF_LO:
5729 case R_PPC64_SECTOFF_HI:
5730 case R_PPC64_SECTOFF_HA:
5731 case R_PPC64_SECTOFF_DS:
5732 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5733 case R_PPC64_DTPREL16:
5734 case R_PPC64_DTPREL16_LO:
5735 case R_PPC64_DTPREL16_HI:
5736 case R_PPC64_DTPREL16_HA:
5737 case R_PPC64_DTPREL16_DS:
5738 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5739 case R_PPC64_DTPREL16_HIGH:
5740 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5741 case R_PPC64_DTPREL16_HIGHER:
5742 case R_PPC64_DTPREL16_HIGHERA:
5743 case R_PPC64_DTPREL16_HIGHEST:
5744 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5745 break;
5746
ad8e1ba5 5747 /* Nor do these. */
25f23106
AM
5748 case R_PPC64_REL16:
5749 case R_PPC64_REL16_LO:
5750 case R_PPC64_REL16_HI:
5751 case R_PPC64_REL16_HA:
a680de9a 5752 case R_PPC64_REL16DX_HA:
25f23106
AM
5753 break;
5754
45965137
AM
5755 /* Not supported as a dynamic relocation. */
5756 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5757 if (bfd_link_pic (info))
45965137
AM
5758 {
5759 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5760 ppc_howto_init ();
695344c0 5761 /* xgettext:c-format */
174d0a74 5762 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 5763 "in shared libraries and PIEs\n"),
45965137
AM
5764 abfd, sec, rel->r_offset,
5765 ppc64_elf_howto_table[r_type]->name);
5766 bfd_set_error (bfd_error_bad_value);
5767 return FALSE;
5768 }
5769 break;
5770
ad8e1ba5 5771 case R_PPC64_TOC16:
33c0ec9d
AM
5772 case R_PPC64_TOC16_DS:
5773 htab->do_multi_toc = 1;
d77c8a4b 5774 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5775 /* Fall through. */
ad8e1ba5
AM
5776 case R_PPC64_TOC16_LO:
5777 case R_PPC64_TOC16_HI:
5778 case R_PPC64_TOC16_HA:
ad8e1ba5 5779 case R_PPC64_TOC16_LO_DS:
4c52953f 5780 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5781 break;
5782
006589cf
AM
5783 /* Marker reloc. */
5784 case R_PPC64_ENTRY:
5785 break;
5786
5bd4f169
AM
5787 /* This relocation describes the C++ object vtable hierarchy.
5788 Reconstruct it for later use during GC. */
5789 case R_PPC64_GNU_VTINHERIT:
c152c796 5790 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5791 return FALSE;
5bd4f169
AM
5792 break;
5793
5794 /* This relocation describes which C++ vtable entries are actually
5795 used. Record for later use during GC. */
5796 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5797 BFD_ASSERT (h != NULL);
5798 if (h != NULL
5799 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5800 return FALSE;
5bd4f169
AM
5801 break;
5802
721956f4
AM
5803 case R_PPC64_REL14:
5804 case R_PPC64_REL14_BRTAKEN:
5805 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5806 {
5807 asection *dest = NULL;
5808
5809 /* Heuristic: If jumping outside our section, chances are
5810 we are going to need a stub. */
5811 if (h != NULL)
5812 {
5813 /* If the sym is weak it may be overridden later, so
5814 don't assume we know where a weak sym lives. */
5815 if (h->root.type == bfd_link_hash_defined)
5816 dest = h->root.u.def.section;
5817 }
5818 else
87d72d41
AM
5819 {
5820 Elf_Internal_Sym *isym;
5821
5822 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5823 abfd, r_symndx);
5824 if (isym == NULL)
5825 return FALSE;
5826
5827 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5828 }
5829
220c76dd 5830 if (dest != sec)
7c8fe5c4 5831 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5832 }
3e04d765
AM
5833 goto rel24;
5834
5835 case R_PPC64_PLTCALL:
5836 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
5837 /* Fall through. */
5838
5d1634d7 5839 case R_PPC64_REL24:
3e04d765 5840 rel24:
cbf95972
AM
5841 plt_list = ifunc;
5842 if (h != NULL)
5d1634d7 5843 {
e054468f
AM
5844 h->needs_plt = 1;
5845 if (h->root.root.string[0] == '.'
5846 && h->root.root.string[1] != '\0')
5847 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5848
3a71aa26 5849 if (h == tga || h == dottga)
cbf95972
AM
5850 {
5851 sec->has_tls_reloc = 1;
5852 if (rel != relocs
5853 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5854 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5855 /* We have a new-style __tls_get_addr call with
5856 a marker reloc. */
5857 ;
5858 else
5859 /* Mark this section as having an old-style call. */
5860 sec->has_tls_get_addr_call = 1;
5861 }
5862 plt_list = &h->plt.plist;
411e1bfb 5863 }
cbf95972
AM
5864
5865 /* We may need a .plt entry if the function this reloc
5866 refers to is in a shared lib. */
5867 if (plt_list
5868 && !update_plt_info (abfd, plt_list, rel->r_addend))
5869 return FALSE;
411e1bfb
AM
5870 break;
5871
cbf95972
AM
5872 case R_PPC64_ADDR14:
5873 case R_PPC64_ADDR14_BRNTAKEN:
5874 case R_PPC64_ADDR14_BRTAKEN:
5875 case R_PPC64_ADDR24:
5876 goto dodyn;
5877
411e1bfb
AM
5878 case R_PPC64_TPREL64:
5879 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5880 if (bfd_link_dll (info))
411e1bfb
AM
5881 info->flags |= DF_STATIC_TLS;
5882 goto dotlstoc;
5883
5884 case R_PPC64_DTPMOD64:
5885 if (rel + 1 < rel_end
5886 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5887 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5888 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5889 else
951fd09b 5890 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5891 goto dotlstoc;
5892
5893 case R_PPC64_DTPREL64:
5894 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5895 if (rel != relocs
5896 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5897 && rel[-1].r_offset == rel->r_offset - 8)
5898 /* This is the second reloc of a dtpmod, dtprel pair.
5899 Don't mark with TLS_DTPREL. */
5900 goto dodyn;
5901
5902 dotlstoc:
5903 sec->has_tls_reloc = 1;
5904 if (h != NULL)
5905 {
5906 struct ppc_link_hash_entry *eh;
5907 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5908 eh->tls_mask |= tls_type;
411e1bfb
AM
5909 }
5910 else
5911 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5912 rel->r_addend, tls_type))
5913 return FALSE;
5914
7c8fe5c4
AM
5915 ppc64_sec = ppc64_elf_section_data (sec);
5916 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5917 {
3a71aa26
AM
5918 bfd_size_type amt;
5919
e7b938ca 5920 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5921 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5922 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5923 if (ppc64_sec->u.toc.symndx == NULL)
5924 return FALSE;
5925 amt = sec->size * sizeof (bfd_vma) / 8;
5926 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5927 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5928 return FALSE;
7c8fe5c4
AM
5929 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5930 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5931 }
5932 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5933 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5934 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5935
5936 /* Mark the second slot of a GD or LD entry.
5937 -1 to indicate GD and -2 to indicate LD. */
5938 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5939 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5940 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5941 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5942 goto dodyn;
5943
5944 case R_PPC64_TPREL16:
5945 case R_PPC64_TPREL16_LO:
5946 case R_PPC64_TPREL16_HI:
5947 case R_PPC64_TPREL16_HA:
5948 case R_PPC64_TPREL16_DS:
5949 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5950 case R_PPC64_TPREL16_HIGH:
5951 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5952 case R_PPC64_TPREL16_HIGHER:
5953 case R_PPC64_TPREL16_HIGHERA:
5954 case R_PPC64_TPREL16_HIGHEST:
5955 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
5956 if (bfd_link_dll (info))
5957 info->flags |= DF_STATIC_TLS;
5958 goto dodyn;
5d1634d7 5959
e86ce104 5960 case R_PPC64_ADDR64:
b9399fcf 5961 if (is_opd
1e2f5b6e 5962 && rel + 1 < rel_end
4ce794b7 5963 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5964 {
8387904d 5965 if (h != NULL)
8c5b4e52 5966 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 5967 }
e86ce104
AM
5968 /* Fall through. */
5969
65f38f15
AM
5970 case R_PPC64_ADDR16:
5971 case R_PPC64_ADDR16_DS:
5972 case R_PPC64_ADDR16_HA:
5973 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5974 case R_PPC64_ADDR16_HIGH:
5975 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5976 case R_PPC64_ADDR16_HIGHER:
5977 case R_PPC64_ADDR16_HIGHERA:
5978 case R_PPC64_ADDR16_HIGHEST:
5979 case R_PPC64_ADDR16_HIGHESTA:
5980 case R_PPC64_ADDR16_LO:
5981 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5982 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5983 && rel->r_addend == 0)
5984 {
5985 /* We may need a .plt entry if this reloc refers to a
5986 function in a shared lib. */
5987 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5988 return FALSE;
5989 h->pointer_equality_needed = 1;
5990 }
5991 /* Fall through. */
5992
5993 case R_PPC64_REL30:
5994 case R_PPC64_REL32:
5995 case R_PPC64_REL64:
65f38f15 5996 case R_PPC64_ADDR32:
65f38f15
AM
5997 case R_PPC64_UADDR16:
5998 case R_PPC64_UADDR32:
5999 case R_PPC64_UADDR64:
5bd4f169 6000 case R_PPC64_TOC:
0e1862bb 6001 if (h != NULL && !bfd_link_pic (info))
81848ca0 6002 /* We may need a copy reloc. */
f5385ebf 6003 h->non_got_ref = 1;
81848ca0 6004
41bd81ab 6005 /* Don't propagate .opd relocs. */
b9399fcf 6006 if (NO_OPD_RELOCS && is_opd)
e86ce104 6007 break;
e86ce104 6008
65f38f15
AM
6009 /* If we are creating a shared library, and this is a reloc
6010 against a global symbol, or a non PC relative reloc
6011 against a local symbol, then we need to copy the reloc
6012 into the shared library. However, if we are linking with
6013 -Bsymbolic, we do not need to copy a reloc against a
6014 global symbol which is defined in an object we are
6015 including in the link (i.e., DEF_REGULAR is set). At
6016 this point we have not seen all the input files, so it is
6017 possible that DEF_REGULAR is not set now but will be set
6018 later (it is never cleared). In case of a weak definition,
6019 DEF_REGULAR may be cleared later by a strong definition in
6020 a shared library. We account for that possibility below by
f4656909 6021 storing information in the dyn_relocs field of the hash
65f38f15
AM
6022 table entry. A similar situation occurs when creating
6023 shared libraries and symbol visibility changes render the
6024 symbol local.
6025
6026 If on the other hand, we are creating an executable, we
6027 may need to keep relocations for symbols satisfied by a
6028 dynamic library if we manage to avoid copy relocs for the
6029 symbol. */
411e1bfb 6030 dodyn:
0e1862bb 6031 if ((bfd_link_pic (info)
1d483afe 6032 && (must_be_dyn_reloc (info, r_type)
65f38f15 6033 || (h != NULL
198f1157 6034 && (!SYMBOLIC_BIND (info, h)
65f38f15 6035 || h->root.type == bfd_link_hash_defweak
f5385ebf 6036 || !h->def_regular))))
f4656909 6037 || (ELIMINATE_COPY_RELOCS
0e1862bb 6038 && !bfd_link_pic (info)
65f38f15
AM
6039 && h != NULL
6040 && (h->root.type == bfd_link_hash_defweak
25f23106 6041 || !h->def_regular))
0e1862bb 6042 || (!bfd_link_pic (info)
25f23106 6043 && ifunc != NULL))
5bd4f169 6044 {
65f38f15
AM
6045 /* We must copy these reloc types into the output file.
6046 Create a reloc section in dynobj and make room for
6047 this reloc. */
5bd4f169
AM
6048 if (sreloc == NULL)
6049 {
83bac4b0
NC
6050 sreloc = _bfd_elf_make_dynamic_reloc_section
6051 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 6052
5bd4f169 6053 if (sreloc == NULL)
83bac4b0 6054 return FALSE;
5bd4f169
AM
6055 }
6056
65f38f15
AM
6057 /* If this is a global symbol, we count the number of
6058 relocations we need for this symbol. */
6059 if (h != NULL)
6060 {
19e08130
AM
6061 struct elf_dyn_relocs *p;
6062 struct elf_dyn_relocs **head;
6063
ec338859 6064 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
6065 p = *head;
6066 if (p == NULL || p->sec != sec)
6067 {
6068 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6069 if (p == NULL)
6070 return FALSE;
6071 p->next = *head;
6072 *head = p;
6073 p->sec = sec;
6074 p->count = 0;
6075 p->pc_count = 0;
6076 }
6077 p->count += 1;
6078 if (!must_be_dyn_reloc (info, r_type))
6079 p->pc_count += 1;
65f38f15
AM
6080 }
6081 else
6082 {
ec338859
AM
6083 /* Track dynamic relocs needed for local syms too.
6084 We really need local syms available to do this
6085 easily. Oh well. */
19e08130
AM
6086 struct ppc_dyn_relocs *p;
6087 struct ppc_dyn_relocs **head;
6088 bfd_boolean is_ifunc;
ec338859 6089 asection *s;
6edfbbad 6090 void *vpp;
87d72d41 6091 Elf_Internal_Sym *isym;
6edfbbad 6092
87d72d41
AM
6093 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6094 abfd, r_symndx);
6095 if (isym == NULL)
b34976b6 6096 return FALSE;
ec338859 6097
87d72d41
AM
6098 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6099 if (s == NULL)
6100 s = sec;
6101
6edfbbad 6102 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
6103 head = (struct ppc_dyn_relocs **) vpp;
6104 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6105 p = *head;
6106 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6107 p = p->next;
6108 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6109 {
6110 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6111 if (p == NULL)
6112 return FALSE;
6113 p->next = *head;
6114 *head = p;
6115 p->sec = sec;
6116 p->ifunc = is_ifunc;
6117 p->count = 0;
6118 }
6119 p->count += 1;
ec338859 6120 }
65f38f15 6121 }
5bd4f169 6122 break;
65f38f15
AM
6123
6124 default:
96e0dda4 6125 break;
5bd4f169
AM
6126 }
6127 }
6128
b34976b6 6129 return TRUE;
5bd4f169
AM
6130}
6131
ee67d69a
AM
6132/* Merge backend specific data from an object file to the output
6133 object file when linking. */
6134
6135static bfd_boolean
50e03d47 6136ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 6137{
50e03d47 6138 bfd *obfd = info->output_bfd;
ee67d69a
AM
6139 unsigned long iflags, oflags;
6140
6141 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6142 return TRUE;
6143
6144 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6145 return TRUE;
6146
50e03d47 6147 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6148 return FALSE;
6149
6150 iflags = elf_elfheader (ibfd)->e_flags;
6151 oflags = elf_elfheader (obfd)->e_flags;
6152
f6c7c3e8 6153 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6154 {
4eca0228 6155 _bfd_error_handler
695344c0 6156 /* xgettext:c-format */
871b3ab2 6157 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
6158 bfd_set_error (bfd_error_bad_value);
6159 return FALSE;
6160 }
f6c7c3e8 6161 else if (iflags != oflags && iflags != 0)
ee67d69a 6162 {
4eca0228 6163 _bfd_error_handler
695344c0 6164 /* xgettext:c-format */
871b3ab2 6165 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
6166 ibfd, iflags, oflags);
6167 bfd_set_error (bfd_error_bad_value);
6168 return FALSE;
6169 }
6170
50e03d47 6171 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
005d79fd 6172
ee67d69a 6173 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 6174 _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6175
6176 return TRUE;
6177}
6178
6179static bfd_boolean
6180ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6181{
6182 /* Print normal ELF private data. */
6183 _bfd_elf_print_private_bfd_data (abfd, ptr);
6184
6185 if (elf_elfheader (abfd)->e_flags != 0)
6186 {
6187 FILE *file = ptr;
6188
ee67d69a
AM
6189 fprintf (file, _("private flags = 0x%lx:"),
6190 elf_elfheader (abfd)->e_flags);
6191
6192 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6193 fprintf (file, _(" [abiv%ld]"),
6194 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6195 fputc ('\n', file);
6196 }
6197
6198 return TRUE;
6199}
6200
8387904d 6201/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6202 of the code entry point, and its section, which must be in the same
6203 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6204
6205static bfd_vma
6206opd_entry_value (asection *opd_sec,
6207 bfd_vma offset,
6208 asection **code_sec,
aef36ac1
AM
6209 bfd_vma *code_off,
6210 bfd_boolean in_code_sec)
8387904d
AM
6211{
6212 bfd *opd_bfd = opd_sec->owner;
8860955f 6213 Elf_Internal_Rela *relocs;
8387904d 6214 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6215 bfd_vma val;
8387904d 6216
9f296da3
AM
6217 /* No relocs implies we are linking a --just-symbols object, or looking
6218 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6219 if (opd_sec->reloc_count == 0)
6220 {
729eabd5 6221 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6222
729eabd5
AM
6223 if (contents == NULL)
6224 {
6225 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6226 return (bfd_vma) -1;
6227 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6228 }
ee1e4ede 6229
dbb3fbbb 6230 /* PR 17512: file: 64b9dfbb. */
451dfd38 6231 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6232 return (bfd_vma) -1;
6233
729eabd5 6234 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6235 if (code_sec != NULL)
6236 {
6237 asection *sec, *likely = NULL;
ee1e4ede 6238
aef36ac1 6239 if (in_code_sec)
4b85d634 6240 {
aef36ac1
AM
6241 sec = *code_sec;
6242 if (sec->vma <= val
6243 && val < sec->vma + sec->size)
6244 likely = sec;
6245 else
6246 val = -1;
6247 }
6248 else
6249 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6250 if (sec->vma <= val
6251 && (sec->flags & SEC_LOAD) != 0
6252 && (sec->flags & SEC_ALLOC) != 0)
6253 likely = sec;
6254 if (likely != NULL)
6255 {
6256 *code_sec = likely;
6257 if (code_off != NULL)
6258 *code_off = val - likely->vma;
4b85d634
AM
6259 }
6260 }
aef36ac1 6261 return val;
4b85d634
AM
6262 }
6263
0c8d6e5c 6264 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6265
729eabd5 6266 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6267 if (relocs == NULL)
6268 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6269 /* PR 17512: file: df8e1fd6. */
6270 if (relocs == NULL)
6271 return (bfd_vma) -1;
645ea6a9 6272
8387904d 6273 /* Go find the opd reloc at the sym address. */
8860955f 6274 lo = relocs;
8387904d 6275 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6276 val = (bfd_vma) -1;
8387904d
AM
6277 while (lo < hi)
6278 {
6279 look = lo + (hi - lo) / 2;
6280 if (look->r_offset < offset)
6281 lo = look + 1;
6282 else if (look->r_offset > offset)
6283 hi = look;
6284 else
6285 {
0ffa91dd
NC
6286 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6287
8387904d
AM
6288 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6289 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6290 {
6291 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6292 asection *sec = NULL;
8387904d 6293
b53dfeb2
AM
6294 if (symndx >= symtab_hdr->sh_info
6295 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6296 {
6297 struct elf_link_hash_entry **sym_hashes;
6298 struct elf_link_hash_entry *rh;
6299
6300 sym_hashes = elf_sym_hashes (opd_bfd);
6301 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6302 if (rh != NULL)
6303 {
6304 rh = elf_follow_link (rh);
bb854a36
AM
6305 if (rh->root.type != bfd_link_hash_defined
6306 && rh->root.type != bfd_link_hash_defweak)
6307 break;
6308 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6309 {
bb854a36
AM
6310 val = rh->root.u.def.value;
6311 sec = rh->root.u.def.section;
b53dfeb2
AM
6312 }
6313 }
6314 }
6315
6316 if (sec == NULL)
6317 {
6318 Elf_Internal_Sym *sym;
6319
6320 if (symndx < symtab_hdr->sh_info)
6321 {
6322 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6323 if (sym == NULL)
6324 {
6325 size_t symcnt = symtab_hdr->sh_info;
6326 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6327 symcnt, 0,
6328 NULL, NULL, NULL);
6329 if (sym == NULL)
6330 break;
6331 symtab_hdr->contents = (bfd_byte *) sym;
6332 }
6333 sym += symndx;
128205bb
AM
6334 }
6335 else
6336 {
b53dfeb2
AM
6337 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6338 1, symndx,
6339 NULL, NULL, NULL);
128205bb
AM
6340 if (sym == NULL)
6341 break;
128205bb 6342 }
b53dfeb2
AM
6343 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6344 if (sec == NULL)
6345 break;
6346 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6347 val = sym->st_value;
8387904d 6348 }
b53dfeb2 6349
8387904d
AM
6350 val += look->r_addend;
6351 if (code_off != NULL)
6352 *code_off = val;
6353 if (code_sec != NULL)
aef36ac1
AM
6354 {
6355 if (in_code_sec && *code_sec != sec)
6356 return -1;
6357 else
6358 *code_sec = sec;
6359 }
b53dfeb2 6360 if (sec->output_section != NULL)
8387904d 6361 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6362 }
6363 break;
6364 }
6365 }
645ea6a9 6366
645ea6a9 6367 return val;
8387904d
AM
6368}
6369
aef36ac1
AM
6370/* If the ELF symbol SYM might be a function in SEC, return the
6371 function size and set *CODE_OFF to the function's entry point,
6372 otherwise return zero. */
9f296da3 6373
aef36ac1
AM
6374static bfd_size_type
6375ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6376 bfd_vma *code_off)
9f296da3 6377{
aef36ac1
AM
6378 bfd_size_type size;
6379
6380 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6381 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6382 return 0;
6383
6384 size = 0;
6385 if (!(sym->flags & BSF_SYNTHETIC))
6386 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6387
6388 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6389 {
b07bca4e
AM
6390 struct _opd_sec_data *opd = get_opd_info (sym->section);
6391 bfd_vma symval = sym->value;
6392
6393 if (opd != NULL
6394 && opd->adjust != NULL
6395 && elf_section_data (sym->section)->relocs != NULL)
6396 {
6397 /* opd_entry_value will use cached relocs that have been
6398 adjusted, but with raw symbols. That means both local
6399 and global symbols need adjusting. */
6400 long adjust = opd->adjust[OPD_NDX (symval)];
6401 if (adjust == -1)
6402 return 0;
6403 symval += adjust;
6404 }
6405
6406 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6407 &sec, code_off, TRUE) == (bfd_vma) -1)
6408 return 0;
6409 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6410 symbol. This size has nothing to do with the code size of the
6411 function, which is what we're supposed to return, but the
6412 code size isn't available without looking up the dot-sym.
6413 However, doing that would be a waste of time particularly
6414 since elf_find_function will look at the dot-sym anyway.
6415 Now, elf_find_function will keep the largest size of any
6416 function sym found at the code address of interest, so return
6417 1 here to avoid it incorrectly caching a larger function size
6418 for a small function. This does mean we return the wrong
6419 size for a new-ABI function of size 24, but all that does is
6420 disable caching for such functions. */
6421 if (size == 24)
6422 size = 1;
9f296da3 6423 }
aef36ac1
AM
6424 else
6425 {
6426 if (sym->section != sec)
6427 return 0;
6428 *code_off = sym->value;
6429 }
6430 if (size == 0)
6431 size = 1;
6432 return size;
9f296da3
AM
6433}
6434
f378ab09
AM
6435/* Return true if symbol is a strong function defined in an ELFv2
6436 object with st_other localentry bits of zero, ie. its local entry
6437 point coincides with its global entry point. */
6438
6439static bfd_boolean
6440is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6441{
6442 return (h != NULL
6443 && h->type == STT_FUNC
6444 && h->root.type == bfd_link_hash_defined
6445 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 6446 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
6447 && is_ppc64_elf (h->root.u.def.section->owner)
6448 && abiversion (h->root.u.def.section->owner) >= 2);
6449}
6450
854b41e7
AM
6451/* Return true if symbol is defined in a regular object file. */
6452
6453static bfd_boolean
6454is_static_defined (struct elf_link_hash_entry *h)
6455{
6456 return ((h->root.type == bfd_link_hash_defined
6457 || h->root.type == bfd_link_hash_defweak)
6458 && h->root.u.def.section != NULL
6459 && h->root.u.def.section->output_section != NULL);
6460}
6461
b31867b6
AM
6462/* If FDH is a function descriptor symbol, return the associated code
6463 entry symbol if it is defined. Return NULL otherwise. */
6464
6465static struct ppc_link_hash_entry *
6466defined_code_entry (struct ppc_link_hash_entry *fdh)
6467{
6468 if (fdh->is_func_descriptor)
6469 {
6470 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6471 if (fh->elf.root.type == bfd_link_hash_defined
6472 || fh->elf.root.type == bfd_link_hash_defweak)
6473 return fh;
6474 }
6475 return NULL;
6476}
6477
6478/* If FH is a function code entry symbol, return the associated
6479 function descriptor symbol if it is defined. Return NULL otherwise. */
6480
6481static struct ppc_link_hash_entry *
6482defined_func_desc (struct ppc_link_hash_entry *fh)
6483{
6484 if (fh->oh != NULL
6485 && fh->oh->is_func_descriptor)
6486 {
6487 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6488 if (fdh->elf.root.type == bfd_link_hash_defined
6489 || fdh->elf.root.type == bfd_link_hash_defweak)
6490 return fdh;
6491 }
6492 return NULL;
6493}
6494
8c5b4e52
AM
6495static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6496
6497/* Garbage collect sections, after first dealing with dot-symbols. */
6498
6499static bfd_boolean
6500ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6501{
6502 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6503
6504 if (htab != NULL && htab->need_func_desc_adj)
6505 {
6506 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6507 htab->need_func_desc_adj = 0;
6508 }
6509 return bfd_elf_gc_sections (abfd, info);
6510}
6511
74f0fb50
AM
6512/* Mark all our entry sym sections, both opd and code section. */
6513
6514static void
6515ppc64_elf_gc_keep (struct bfd_link_info *info)
6516{
6517 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6518 struct bfd_sym_chain *sym;
6519
4dfe6ac6
NC
6520 if (htab == NULL)
6521 return;
6522
74f0fb50
AM
6523 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6524 {
b31867b6 6525 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6526 asection *sec;
6527
6528 eh = (struct ppc_link_hash_entry *)
b31867b6 6529 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6530 if (eh == NULL)
6531 continue;
6532 if (eh->elf.root.type != bfd_link_hash_defined
6533 && eh->elf.root.type != bfd_link_hash_defweak)
6534 continue;
6535
b31867b6
AM
6536 fh = defined_code_entry (eh);
6537 if (fh != NULL)
74f0fb50 6538 {
b31867b6 6539 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6540 sec->flags |= SEC_KEEP;
6541 }
6542 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6543 && opd_entry_value (eh->elf.root.u.def.section,
6544 eh->elf.root.u.def.value,
aef36ac1 6545 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6546 sec->flags |= SEC_KEEP;
6547
6548 sec = eh->elf.root.u.def.section;
6549 sec->flags |= SEC_KEEP;
6550 }
6551}
6552
64d03ab5
AM
6553/* Mark sections containing dynamically referenced symbols. When
6554 building shared libraries, we must assume that any visible symbol is
6555 referenced. */
6556
6557static bfd_boolean
6558ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6559{
6560 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6561 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6562 struct ppc_link_hash_entry *fdh;
b407645f 6563 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6564
64d03ab5 6565 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6566 fdh = defined_func_desc (eh);
6567 if (fdh != NULL)
6568 eh = fdh;
64d03ab5
AM
6569
6570 if ((eh->elf.root.type == bfd_link_hash_defined
6571 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 6572 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 6573 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6574 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6575 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6576 && (!bfd_link_executable (info)
e278ae05 6577 || info->gc_keep_exported
b407645f
AM
6578 || info->export_dynamic
6579 || (eh->elf.dynamic
6580 && d != NULL
6581 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
e278ae05 6582 && (eh->elf.versioned >= versioned
4c58e0d8
AM
6583 || !bfd_hide_sym_by_version (info->version_info,
6584 eh->elf.root.root.string)))))
64d03ab5
AM
6585 {
6586 asection *code_sec;
b31867b6 6587 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6588
6589 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6590
6591 /* Function descriptor syms cause the associated
6592 function code sym section to be marked. */
b31867b6
AM
6593 fh = defined_code_entry (eh);
6594 if (fh != NULL)
6595 {
6596 code_sec = fh->elf.root.u.def.section;
6597 code_sec->flags |= SEC_KEEP;
6598 }
64d03ab5
AM
6599 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6600 && opd_entry_value (eh->elf.root.u.def.section,
6601 eh->elf.root.u.def.value,
aef36ac1 6602 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6603 code_sec->flags |= SEC_KEEP;
6604 }
6605
6606 return TRUE;
6607}
6608
5bd4f169
AM
6609/* Return the section that should be marked against GC for a given
6610 relocation. */
6611
6612static asection *
4ce794b7 6613ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6614 struct bfd_link_info *info,
4ce794b7
AM
6615 Elf_Internal_Rela *rel,
6616 struct elf_link_hash_entry *h,
6617 Elf_Internal_Sym *sym)
5bd4f169 6618{
ccfa59ea
AM
6619 asection *rsec;
6620
ccfa59ea
AM
6621 /* Syms return NULL if we're marking .opd, so we avoid marking all
6622 function sections, as all functions are referenced in .opd. */
6623 rsec = NULL;
6624 if (get_opd_info (sec) != NULL)
6625 return rsec;
1e2f5b6e 6626
5bd4f169
AM
6627 if (h != NULL)
6628 {
04c9666a 6629 enum elf_ppc64_reloc_type r_type;
b31867b6 6630 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6631
4ce794b7 6632 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6633 switch (r_type)
5bd4f169
AM
6634 {
6635 case R_PPC64_GNU_VTINHERIT:
6636 case R_PPC64_GNU_VTENTRY:
6637 break;
6638
6639 default:
6640 switch (h->root.type)
6641 {
6642 case bfd_link_hash_defined:
6643 case bfd_link_hash_defweak:
ccfa59ea 6644 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6645 fdh = defined_func_desc (eh);
6646 if (fdh != NULL)
8c5b4e52
AM
6647 {
6648 /* -mcall-aixdesc code references the dot-symbol on
6649 a call reloc. Mark the function descriptor too
6650 against garbage collection. */
6651 fdh->elf.mark = 1;
60d67dc8
AM
6652 if (fdh->elf.is_weakalias)
6653 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6654 eh = fdh;
6655 }
1e2f5b6e
AM
6656
6657 /* Function descriptor syms cause the associated
6658 function code sym section to be marked. */
b31867b6
AM
6659 fh = defined_code_entry (eh);
6660 if (fh != NULL)
ccfa59ea
AM
6661 {
6662 /* They also mark their opd section. */
74f0fb50 6663 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6664
b31867b6 6665 rsec = fh->elf.root.u.def.section;
ccfa59ea 6666 }
8387904d
AM
6667 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6668 && opd_entry_value (eh->elf.root.u.def.section,
6669 eh->elf.root.u.def.value,
aef36ac1 6670 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6671 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6672 else
1e2f5b6e
AM
6673 rsec = h->root.u.def.section;
6674 break;
5bd4f169
AM
6675
6676 case bfd_link_hash_common:
1e2f5b6e
AM
6677 rsec = h->root.u.c.p->section;
6678 break;
5bd4f169
AM
6679
6680 default:
fb34365b 6681 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6682 }
6683 }
6684 }
6685 else
6686 {
74f0fb50 6687 struct _opd_sec_data *opd;
1e2f5b6e
AM
6688
6689 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6690 opd = get_opd_info (rsec);
6691 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6692 {
74f0fb50 6693 rsec->gc_mark = 1;
ccfa59ea 6694
51aecdc5 6695 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6696 }
5bd4f169
AM
6697 }
6698
1e2f5b6e 6699 return rsec;
5bd4f169
AM
6700}
6701
deb0e272
AM
6702/* The maximum size of .sfpr. */
6703#define SFPR_MAX (218*4)
6704
6705struct sfpr_def_parms
6706{
699733f6
AM
6707 const char name[12];
6708 unsigned char lo, hi;
deb0e272
AM
6709 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6710 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6711};
6712
a4b6fadd
AM
6713/* Auto-generate _save*, _rest* functions in .sfpr.
6714 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6715 instead. */
deb0e272 6716
4dfe6ac6 6717static bfd_boolean
a4b6fadd
AM
6718sfpr_define (struct bfd_link_info *info,
6719 const struct sfpr_def_parms *parm,
6720 asection *stub_sec)
deb0e272
AM
6721{
6722 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6723 unsigned int i;
6724 size_t len = strlen (parm->name);
6725 bfd_boolean writing = FALSE;
699733f6 6726 char sym[16];
deb0e272 6727
4dfe6ac6
NC
6728 if (htab == NULL)
6729 return FALSE;
6730
deb0e272
AM
6731 memcpy (sym, parm->name, len);
6732 sym[len + 2] = 0;
6733
6734 for (i = parm->lo; i <= parm->hi; i++)
6735 {
a4b6fadd 6736 struct ppc_link_hash_entry *h;
deb0e272
AM
6737
6738 sym[len + 0] = i / 10 + '0';
6739 sym[len + 1] = i % 10 + '0';
a4b6fadd 6740 h = (struct ppc_link_hash_entry *)
b32547cd 6741 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6742 if (stub_sec != NULL)
deb0e272 6743 {
a4b6fadd
AM
6744 if (h != NULL
6745 && h->elf.root.type == bfd_link_hash_defined
6746 && h->elf.root.u.def.section == htab->sfpr)
6747 {
6748 struct elf_link_hash_entry *s;
6749 char buf[32];
6750 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6751 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6752 if (s == NULL)
6753 return FALSE;
6754 if (s->root.type == bfd_link_hash_new
6755 || (s->root.type = bfd_link_hash_defined
6756 && s->root.u.def.section == stub_sec))
6757 {
6758 s->root.type = bfd_link_hash_defined;
6759 s->root.u.def.section = stub_sec;
7dda8d3c 6760 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
6761 + h->elf.root.u.def.value);
6762 s->ref_regular = 1;
6763 s->def_regular = 1;
6764 s->ref_regular_nonweak = 1;
6765 s->forced_local = 1;
6766 s->non_elf = 0;
6767 s->root.linker_def = 1;
6768 }
6769 }
6770 continue;
6771 }
6772 if (h != NULL)
6773 {
6774 h->save_res = 1;
6775 if (!h->elf.def_regular)
deb0e272 6776 {
a4b6fadd
AM
6777 h->elf.root.type = bfd_link_hash_defined;
6778 h->elf.root.u.def.section = htab->sfpr;
6779 h->elf.root.u.def.value = htab->sfpr->size;
6780 h->elf.type = STT_FUNC;
6781 h->elf.def_regular = 1;
b32547cd 6782 h->elf.non_elf = 0;
a4b6fadd
AM
6783 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6784 writing = TRUE;
deb0e272 6785 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6786 {
6787 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6788 if (htab->sfpr->contents == NULL)
6789 return FALSE;
6790 }
deb0e272
AM
6791 }
6792 }
6793 if (writing)
6794 {
6795 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6796 if (i != parm->hi)
6797 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6798 else
6799 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6800 htab->sfpr->size = p - htab->sfpr->contents;
6801 }
6802 }
6803
6804 return TRUE;
6805}
6806
6807static bfd_byte *
6808savegpr0 (bfd *abfd, bfd_byte *p, int r)
6809{
6810 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6811 return p + 4;
6812}
6813
6814static bfd_byte *
6815savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6816{
6817 p = savegpr0 (abfd, p, r);
a078d95a 6818 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6819 p = p + 4;
6820 bfd_put_32 (abfd, BLR, p);
6821 return p + 4;
6822}
6823
6824static bfd_byte *
6825restgpr0 (bfd *abfd, bfd_byte *p, int r)
6826{
6827 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6828 return p + 4;
6829}
6830
6831static bfd_byte *
6832restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6833{
a078d95a 6834 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6835 p = p + 4;
6836 p = restgpr0 (abfd, p, r);
6837 bfd_put_32 (abfd, MTLR_R0, p);
6838 p = p + 4;
6839 if (r == 29)
6840 {
6841 p = restgpr0 (abfd, p, 30);
6842 p = restgpr0 (abfd, p, 31);
6843 }
6844 bfd_put_32 (abfd, BLR, p);
6845 return p + 4;
6846}
6847
6848static bfd_byte *
6849savegpr1 (bfd *abfd, bfd_byte *p, int r)
6850{
6851 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6852 return p + 4;
6853}
6854
6855static bfd_byte *
6856savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6857{
6858 p = savegpr1 (abfd, p, r);
6859 bfd_put_32 (abfd, BLR, p);
6860 return p + 4;
6861}
6862
6863static bfd_byte *
6864restgpr1 (bfd *abfd, bfd_byte *p, int r)
6865{
6866 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6867 return p + 4;
6868}
6869
6870static bfd_byte *
6871restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6872{
6873 p = restgpr1 (abfd, p, r);
6874 bfd_put_32 (abfd, BLR, p);
6875 return p + 4;
6876}
6877
6878static bfd_byte *
6879savefpr (bfd *abfd, bfd_byte *p, int r)
6880{
6881 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6882 return p + 4;
6883}
6884
6885static bfd_byte *
6886savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6887{
6888 p = savefpr (abfd, p, r);
a078d95a 6889 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6890 p = p + 4;
6891 bfd_put_32 (abfd, BLR, p);
6892 return p + 4;
6893}
6894
6895static bfd_byte *
6896restfpr (bfd *abfd, bfd_byte *p, int r)
6897{
6898 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6899 return p + 4;
6900}
6901
6902static bfd_byte *
6903restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6904{
a078d95a 6905 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6906 p = p + 4;
6907 p = restfpr (abfd, p, r);
6908 bfd_put_32 (abfd, MTLR_R0, p);
6909 p = p + 4;
6910 if (r == 29)
6911 {
6912 p = restfpr (abfd, p, 30);
6913 p = restfpr (abfd, p, 31);
6914 }
6915 bfd_put_32 (abfd, BLR, p);
6916 return p + 4;
6917}
6918
6919static bfd_byte *
6920savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6921{
6922 p = savefpr (abfd, p, r);
6923 bfd_put_32 (abfd, BLR, p);
6924 return p + 4;
6925}
6926
6927static bfd_byte *
6928restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6929{
6930 p = restfpr (abfd, p, r);
6931 bfd_put_32 (abfd, BLR, p);
6932 return p + 4;
6933}
6934
6935static bfd_byte *
6936savevr (bfd *abfd, bfd_byte *p, int r)
6937{
6938 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6939 p = p + 4;
6940 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6941 return p + 4;
6942}
6943
6944static bfd_byte *
6945savevr_tail (bfd *abfd, bfd_byte *p, int r)
6946{
6947 p = savevr (abfd, p, r);
6948 bfd_put_32 (abfd, BLR, p);
6949 return p + 4;
6950}
6951
6952static bfd_byte *
6953restvr (bfd *abfd, bfd_byte *p, int r)
6954{
6955 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6956 p = p + 4;
6957 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6958 return p + 4;
6959}
6960
6961static bfd_byte *
6962restvr_tail (bfd *abfd, bfd_byte *p, int r)
6963{
6964 p = restvr (abfd, p, r);
6965 bfd_put_32 (abfd, BLR, p);
6966 return p + 4;
6967}
6968
e86ce104
AM
6969/* Called via elf_link_hash_traverse to transfer dynamic linking
6970 information on function code symbol entries to their corresponding
6971 function descriptor symbol entries. */
deb0e272 6972
b34976b6 6973static bfd_boolean
4ce794b7 6974func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6975{
e86ce104 6976 struct bfd_link_info *info;
65f38f15 6977 struct ppc_link_hash_table *htab;
50bc7936
AM
6978 struct ppc_link_hash_entry *fh;
6979 struct ppc_link_hash_entry *fdh;
6980 bfd_boolean force_local;
5bd4f169 6981
50bc7936
AM
6982 fh = (struct ppc_link_hash_entry *) h;
6983 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6984 return TRUE;
e86ce104 6985
8c5b4e52
AM
6986 if (!fh->is_func)
6987 return TRUE;
6988
6989 if (fh->elf.root.root.string[0] != '.'
6990 || fh->elf.root.root.string[1] == '\0')
6991 return TRUE;
6992
4ce794b7 6993 info = inf;
65f38f15 6994 htab = ppc_hash_table (info);
4dfe6ac6
NC
6995 if (htab == NULL)
6996 return FALSE;
5bd4f169 6997
8c5b4e52
AM
6998 /* Find the corresponding function descriptor symbol. */
6999 fdh = lookup_fdh (fh, htab);
7000
c09bdfe5
AM
7001 /* Resolve undefined references to dot-symbols as the value
7002 in the function descriptor, if we have one in a regular object.
7003 This is to satisfy cases like ".quad .foo". Calls to functions
7004 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
7005 if ((fh->elf.root.type == bfd_link_hash_undefined
7006 || fh->elf.root.type == bfd_link_hash_undefweak)
7007 && (fdh->elf.root.type == bfd_link_hash_defined
7008 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
7009 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7010 && opd_entry_value (fdh->elf.root.u.def.section,
7011 fdh->elf.root.u.def.value,
c09bdfe5 7012 &fh->elf.root.u.def.section,
aef36ac1 7013 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 7014 {
b31867b6 7015 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 7016 fh->elf.forced_local = 1;
b31867b6
AM
7017 fh->elf.def_regular = fdh->elf.def_regular;
7018 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
7019 }
7020
8c5b4e52
AM
7021 if (!fh->elf.dynamic)
7022 {
7023 struct plt_entry *ent;
5bd4f169 7024
8c5b4e52
AM
7025 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7026 if (ent->plt.refcount > 0)
7027 break;
7028 if (ent == NULL)
7029 return TRUE;
7030 }
5bd4f169 7031
8c5b4e52 7032 /* Create a descriptor as undefined if necessary. */
50bc7936 7033 if (fdh == NULL
0e1862bb 7034 && !bfd_link_executable (info)
50bc7936
AM
7035 && (fh->elf.root.type == bfd_link_hash_undefined
7036 || fh->elf.root.type == bfd_link_hash_undefweak))
7037 {
908b32fc 7038 fdh = make_fdh (info, fh);
bb700d78
AM
7039 if (fdh == NULL)
7040 return FALSE;
50bc7936 7041 }
648cca2c 7042
8c5b4e52 7043 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
7044 if (fdh != NULL
7045 && fdh->fake
8c5b4e52
AM
7046 && (fh->elf.root.type == bfd_link_hash_defined
7047 || fh->elf.root.type == bfd_link_hash_defweak))
7048 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 7049
8c5b4e52
AM
7050 /* Transfer dynamic linking information to the function descriptor. */
7051 if (fdh != NULL)
7052 {
f5385ebf
AM
7053 fdh->elf.ref_regular |= fh->elf.ref_regular;
7054 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7055 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7056 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
7057 fdh->elf.dynamic |= fh->elf.dynamic;
7058 fdh->elf.needs_plt |= (fh->elf.needs_plt
7059 || fh->elf.type == STT_FUNC
7060 || fh->elf.type == STT_GNU_IFUNC);
7061 move_plt_plist (fh, fdh);
7062
7063 if (!fdh->elf.forced_local
7064 && fh->elf.dynindx != -1)
7065 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7066 return FALSE;
e86ce104
AM
7067 }
7068
50bc7936
AM
7069 /* Now that the info is on the function descriptor, clear the
7070 function code sym info. Any function code syms for which we
7071 don't have a definition in a regular file, we force local.
7072 This prevents a shared library from exporting syms that have
7073 been imported from another library. Function code syms that
7074 are really in the library we must leave global to prevent the
7075 linker dragging in a definition from a static library. */
93f3fa99
AM
7076 force_local = (!fh->elf.def_regular
7077 || fdh == NULL
7078 || !fdh->elf.def_regular
7079 || fdh->elf.forced_local);
50bc7936
AM
7080 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7081
b34976b6 7082 return TRUE;
e86ce104 7083}
40b8271b 7084
a4b6fadd
AM
7085static const struct sfpr_def_parms save_res_funcs[] =
7086 {
7087 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7088 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7089 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7090 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7091 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7092 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7093 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7094 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7095 { "._savef", 14, 31, savefpr, savefpr1_tail },
7096 { "._restf", 14, 31, restfpr, restfpr1_tail },
7097 { "_savevr_", 20, 31, savevr, savevr_tail },
7098 { "_restvr_", 20, 31, restvr, restvr_tail }
7099 };
7100
e86ce104 7101/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
7102 this hook to a) provide some gcc support functions, and b) transfer
7103 dynamic linking information gathered so far on function code symbol
7104 entries, to their corresponding function descriptor symbol entries. */
deb0e272 7105
b34976b6 7106static bfd_boolean
4ce794b7
AM
7107ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7108 struct bfd_link_info *info)
e86ce104
AM
7109{
7110 struct ppc_link_hash_table *htab;
7111
7112 htab = ppc_hash_table (info);
4dfe6ac6
NC
7113 if (htab == NULL)
7114 return FALSE;
7115
b32547cd
AM
7116 /* Provide any missing _save* and _rest* functions. */
7117 if (htab->sfpr != NULL)
7118 {
7119 unsigned int i;
7120
7121 htab->sfpr->size = 0;
7122 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7123 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7124 return FALSE;
7125 if (htab->sfpr->size == 0)
7126 htab->sfpr->flags |= SEC_EXCLUDE;
7127 }
7128
7129 if (bfd_link_relocatable (info))
7130 return TRUE;
7131
7132 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7133 {
7134 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7135 /* Make .TOC. defined so as to prevent it being made dynamic.
7136 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7137 if (!htab->elf.hgot->def_regular
7138 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7139 {
7140 htab->elf.hgot->root.type = bfd_link_hash_defined;
7141 htab->elf.hgot->root.u.def.value = 0;
7142 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7143 htab->elf.hgot->def_regular = 1;
7144 htab->elf.hgot->root.linker_def = 1;
7145 }
dba6fa9b 7146 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7147 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7148 | STV_HIDDEN);
7149 }
c66bb0ee 7150
8c5b4e52
AM
7151 if (htab->need_func_desc_adj)
7152 {
7153 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7154 htab->need_func_desc_adj = 0;
7155 }
805fc799 7156
b34976b6 7157 return TRUE;
e86ce104
AM
7158}
7159
98bbb1b8 7160/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 7161
98bbb1b8 7162static asection *
a345bc8d
AM
7163readonly_dynrelocs (struct elf_link_hash_entry *h)
7164{
7165 struct ppc_link_hash_entry *eh;
7166 struct elf_dyn_relocs *p;
7167
7168 eh = (struct ppc_link_hash_entry *) h;
7169 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7170 {
7171 asection *s = p->sec->output_section;
7172
7173 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 7174 return p->sec;
a345bc8d 7175 }
98bbb1b8 7176 return NULL;
a345bc8d
AM
7177}
7178
d311bc8b 7179/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
7180 aliases, that apply to read-only sections. Cannot be used after
7181 size_dynamic_sections. */
d311bc8b
AM
7182
7183static bfd_boolean
7184alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7185{
7186 struct ppc_link_hash_entry *eh;
7187
7188 eh = (struct ppc_link_hash_entry *) h;
7189 do
7190 {
7191 if (readonly_dynrelocs (&eh->elf))
7192 return TRUE;
ab2477e1 7193 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
d311bc8b
AM
7194 } while (eh != NULL && &eh->elf != h);
7195
7196 return FALSE;
7197}
8a2058b5 7198
8a9e8e72
AM
7199/* Return whether EH has pc-relative dynamic relocs. */
7200
7201static bfd_boolean
7202pc_dynrelocs (struct ppc_link_hash_entry *eh)
7203{
7204 struct elf_dyn_relocs *p;
7205
7206 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7207 if (p->pc_count != 0)
7208 return TRUE;
7209 return FALSE;
7210}
7211
8a2058b5
AM
7212/* Return true if a global entry stub will be created for H. Valid
7213 for ELFv2 before plt entries have been allocated. */
7214
7215static bfd_boolean
7216global_entry_stub (struct elf_link_hash_entry *h)
7217{
7218 struct plt_entry *pent;
7219
7220 if (!h->pointer_equality_needed
7221 || h->def_regular)
7222 return FALSE;
7223
7224 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7225 if (pent->plt.refcount > 0
7226 && pent->addend == 0)
7227 return TRUE;
7228
7229 return FALSE;
7230}
7231
e86ce104
AM
7232/* Adjust a symbol defined by a dynamic object and referenced by a
7233 regular object. The current definition is in some section of the
7234 dynamic object, but we're not including those sections. We have to
7235 change the definition to something the rest of the link can
7236 understand. */
7237
b34976b6 7238static bfd_boolean
4ce794b7
AM
7239ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7240 struct elf_link_hash_entry *h)
e86ce104
AM
7241{
7242 struct ppc_link_hash_table *htab;
5474d94f 7243 asection *s, *srel;
e86ce104
AM
7244
7245 htab = ppc_hash_table (info);
4dfe6ac6
NC
7246 if (htab == NULL)
7247 return FALSE;
e86ce104
AM
7248
7249 /* Deal with function syms. */
7250 if (h->type == STT_FUNC
e054468f 7251 || h->type == STT_GNU_IFUNC
f5385ebf 7252 || h->needs_plt)
e86ce104 7253 {
529fe20e
AM
7254 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7255 || SYMBOL_CALLS_LOCAL (info, h)
7256 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7257 /* Discard dyn_relocs when non-pic if we've decided that a
7258 function symbol is local and not an ifunc. We keep dynamic
7259 relocs for ifuncs when local rather than always emitting a
7260 plt call stub for them and defining the symbol on the call
7261 stub. We can't do that for ELFv1 anyway (a function symbol
7262 is defined on a descriptor, not code) and it can be faster at
7263 run-time due to not needing to bounce through a stub. The
7264 dyn_relocs for ifuncs will be applied even in a static
7265 executable. */
7266 if (!bfd_link_pic (info)
7267 && h->type != STT_GNU_IFUNC
7268 && local)
7269 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7270
e86ce104
AM
7271 /* Clear procedure linkage table information for any symbol that
7272 won't need a .plt entry. */
411e1bfb
AM
7273 struct plt_entry *ent;
7274 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7275 if (ent->plt.refcount > 0)
7276 break;
8387904d 7277 if (ent == NULL
2d7ad24e
AM
7278 || (h->type != STT_GNU_IFUNC
7279 && local
3e04d765
AM
7280 && (htab->can_convert_all_inline_plt
7281 || (((struct ppc_link_hash_entry *) h)->tls_mask
7282 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 7283 {
411e1bfb 7284 h->plt.plist = NULL;
f5385ebf 7285 h->needs_plt = 0;
d1eca1e4 7286 h->pointer_equality_needed = 0;
40b8271b 7287 }
8a2058b5 7288 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7289 {
d1eca1e4
AM
7290 /* Taking a function's address in a read/write section
7291 doesn't require us to define the function symbol in the
7292 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7293 be used instead. The reason we prefer a few more dynamic
7294 relocs is that calling via a global entry stub costs a
7295 few more instructions, and pointer_equality_needed causes
7296 extra work in ld.so when resolving these symbols. */
529fe20e 7297 if (global_entry_stub (h))
d1eca1e4 7298 {
ab2477e1 7299 if (!readonly_dynrelocs (h))
529fe20e
AM
7300 {
7301 h->pointer_equality_needed = 0;
04383fd1
AM
7302 /* If we haven't seen a branch reloc and the symbol
7303 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
7304 if (!h->needs_plt)
7305 h->plt.plist = NULL;
7306 }
7307 else if (!bfd_link_pic (info))
7308 /* We are going to be defining the function symbol on the
7309 plt stub, so no dyn_relocs needed when non-pic. */
7310 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
7311 }
7312
3988aed5
AM
7313 /* ELFv2 function symbols can't have copy relocs. */
7314 return TRUE;
7315 }
7316 else if (!h->needs_plt
ab2477e1 7317 && !readonly_dynrelocs (h))
3988aed5 7318 {
04383fd1
AM
7319 /* If we haven't seen a branch reloc and the symbol isn't an
7320 ifunc then we don't need a plt entry. */
3988aed5
AM
7321 h->plt.plist = NULL;
7322 h->pointer_equality_needed = 0;
a345bc8d
AM
7323 return TRUE;
7324 }
5bd4f169 7325 }
bbd7ec4a 7326 else
411e1bfb 7327 h->plt.plist = NULL;
5bd4f169
AM
7328
7329 /* If this is a weak symbol, and there is a real definition, the
7330 processor independent code will have arranged for us to see the
7331 real definition first, and we can just use the same value. */
60d67dc8 7332 if (h->is_weakalias)
5bd4f169 7333 {
60d67dc8
AM
7334 struct elf_link_hash_entry *def = weakdef (h);
7335 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7336 h->root.u.def.section = def->root.u.def.section;
7337 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
7338 if (def->root.u.def.section == htab->elf.sdynbss
7339 || def->root.u.def.section == htab->elf.sdynrelro)
7340 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 7341 return TRUE;
5bd4f169
AM
7342 }
7343
5bd4f169
AM
7344 /* If we are creating a shared library, we must presume that the
7345 only references to the symbol are via the global offset table.
7346 For such cases we need not do anything here; the relocations will
7347 be handled correctly by relocate_section. */
0e1862bb 7348 if (bfd_link_pic (info))
b34976b6 7349 return TRUE;
5bd4f169 7350
65f38f15
AM
7351 /* If there are no references to this symbol that do not use the
7352 GOT, we don't need to generate a copy reloc. */
f5385ebf 7353 if (!h->non_got_ref)
b34976b6 7354 return TRUE;
65f38f15 7355
b186458a 7356 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7357 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7358
d93d1c80
AM
7359 /* If -z nocopyreloc was given, don't generate them either. */
7360 || info->nocopyreloc
a127494f 7361
dce2246a 7362 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 7363 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7364 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7365
d93d1c80
AM
7366 /* Protected variables do not work with .dynbss. The copy in
7367 .dynbss won't be used by the shared library with the protected
7368 definition for the variable. Text relocations are preferable
7369 to an incorrect program. */
7370 || h->protected_def)
529fe20e 7371 return TRUE;
a127494f 7372
5d35169e 7373 if (h->plt.plist != NULL)
97b639ba
AM
7374 {
7375 /* We should never get here, but unfortunately there are versions
7376 of gcc out there that improperly (for this ABI) put initialized
7377 function pointers, vtable refs and suchlike in read-only
7378 sections. Allow them to proceed, but warn that this might
7379 break at runtime. */
25f53a85 7380 info->callbacks->einfo
c1c8c1ef 7381 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 7382 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7383 h->root.root.string);
7384 }
5d35169e
AM
7385
7386 /* This is a reference to a symbol defined by a dynamic object which
7387 is not a function. */
7388
5bd4f169
AM
7389 /* We must allocate the symbol in our .dynbss section, which will
7390 become part of the .bss section of the executable. There will be
7391 an entry for this symbol in the .dynsym section. The dynamic
7392 object will contain position independent code, so all references
7393 from the dynamic object to this symbol will go through the global
7394 offset table. The dynamic linker will use the .dynsym entry to
7395 determine the address it must put in the global offset table, so
7396 both the dynamic object and the regular object will refer to the
7397 same memory location for the variable. */
5474d94f
AM
7398 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7399 {
7400 s = htab->elf.sdynrelro;
7401 srel = htab->elf.sreldynrelro;
7402 }
7403 else
7404 {
7405 s = htab->elf.sdynbss;
7406 srel = htab->elf.srelbss;
7407 }
1d7e9d18 7408 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7409 {
4a7e5234
AM
7410 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7411 linker to copy the initial value out of the dynamic object
7412 and into the runtime process image. */
5474d94f 7413 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 7414 h->needs_copy = 1;
5bd4f169
AM
7415 }
7416
529fe20e
AM
7417 /* We no longer want dyn_relocs. */
7418 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 7419 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7420}
7421
e86ce104
AM
7422/* If given a function descriptor symbol, hide both the function code
7423 sym and the descriptor. */
7424static void
4ce794b7
AM
7425ppc64_elf_hide_symbol (struct bfd_link_info *info,
7426 struct elf_link_hash_entry *h,
7427 bfd_boolean force_local)
e86ce104 7428{
34814b9f 7429 struct ppc_link_hash_entry *eh;
e86ce104
AM
7430 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7431
34814b9f
AM
7432 eh = (struct ppc_link_hash_entry *) h;
7433 if (eh->is_func_descriptor)
e86ce104 7434 {
34814b9f 7435 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7436
721956f4 7437 if (fh == NULL)
d1329ca3
AM
7438 {
7439 const char *p, *q;
b8ac2841 7440 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
7441 char save;
7442
7443 /* We aren't supposed to use alloca in BFD because on
7444 systems which do not have alloca the version in libiberty
7445 calls xmalloc, which might cause the program to crash
7446 when it runs out of memory. This function doesn't have a
7447 return status, so there's no way to gracefully return an
7448 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7449 accessed; It's either a string in an ELF string table,
7450 or allocated in an objalloc structure. */
d1329ca3 7451
34814b9f 7452 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7453 save = *p;
7454 *(char *) p = '.';
34814b9f 7455 fh = (struct ppc_link_hash_entry *)
b8ac2841 7456 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7457 *(char *) p = save;
7458
7459 /* Unfortunately, if it so happens that the string we were
7460 looking for was allocated immediately before this string,
7461 then we overwrote the string terminator. That's the only
7462 reason the lookup should fail. */
7463 if (fh == NULL)
7464 {
34814b9f
AM
7465 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7466 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7467 --q, --p;
34814b9f
AM
7468 if (q < eh->elf.root.root.string && *p == '.')
7469 fh = (struct ppc_link_hash_entry *)
b8ac2841 7470 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7471 }
7472 if (fh != NULL)
7473 {
34814b9f
AM
7474 eh->oh = fh;
7475 fh->oh = eh;
d1329ca3
AM
7476 }
7477 }
e86ce104 7478 if (fh != NULL)
34814b9f 7479 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7480 }
7481}
7482
411e1bfb 7483static bfd_boolean
8843416a
AM
7484get_sym_h (struct elf_link_hash_entry **hp,
7485 Elf_Internal_Sym **symp,
7486 asection **symsecp,
f961d9dd 7487 unsigned char **tls_maskp,
8843416a
AM
7488 Elf_Internal_Sym **locsymsp,
7489 unsigned long r_symndx,
7490 bfd *ibfd)
411e1bfb 7491{
0ffa91dd 7492 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7493
7494 if (r_symndx >= symtab_hdr->sh_info)
7495 {
7496 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7497 struct elf_link_hash_entry *h;
7498
7499 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7500 h = elf_follow_link (h);
411e1bfb
AM
7501
7502 if (hp != NULL)
7503 *hp = h;
7504
7505 if (symp != NULL)
7506 *symp = NULL;
7507
7508 if (symsecp != NULL)
7509 {
7510 asection *symsec = NULL;
7511 if (h->root.type == bfd_link_hash_defined
7512 || h->root.type == bfd_link_hash_defweak)
7513 symsec = h->root.u.def.section;
7514 *symsecp = symsec;
7515 }
7516
e7b938ca 7517 if (tls_maskp != NULL)
411e1bfb
AM
7518 {
7519 struct ppc_link_hash_entry *eh;
7520
7521 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7522 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7523 }
7524 }
7525 else
7526 {
7527 Elf_Internal_Sym *sym;
7528 Elf_Internal_Sym *locsyms = *locsymsp;
7529
7530 if (locsyms == NULL)
7531 {
7532 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7533 if (locsyms == NULL)
7534 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7535 symtab_hdr->sh_info,
7536 0, NULL, NULL, NULL);
7537 if (locsyms == NULL)
7538 return FALSE;
7539 *locsymsp = locsyms;
7540 }
7541 sym = locsyms + r_symndx;
7542
7543 if (hp != NULL)
7544 *hp = NULL;
7545
7546 if (symp != NULL)
7547 *symp = sym;
7548
7549 if (symsecp != NULL)
cb33740c 7550 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7551
e7b938ca 7552 if (tls_maskp != NULL)
411e1bfb
AM
7553 {
7554 struct got_entry **lgot_ents;
f961d9dd 7555 unsigned char *tls_mask;
411e1bfb 7556
e7b938ca 7557 tls_mask = NULL;
411e1bfb
AM
7558 lgot_ents = elf_local_got_ents (ibfd);
7559 if (lgot_ents != NULL)
7560 {
e054468f
AM
7561 struct plt_entry **local_plt = (struct plt_entry **)
7562 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7563 unsigned char *lgot_masks = (unsigned char *)
e054468f 7564 (local_plt + symtab_hdr->sh_info);
e7b938ca 7565 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7566 }
e7b938ca 7567 *tls_maskp = tls_mask;
411e1bfb
AM
7568 }
7569 }
7570 return TRUE;
7571}
7572
e7b938ca 7573/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7574 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7575 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7576
7577static int
f961d9dd 7578get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7579 unsigned long *toc_symndx,
7580 bfd_vma *toc_addend,
0d4792f7 7581 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7582 const Elf_Internal_Rela *rel,
7583 bfd *ibfd)
411e1bfb
AM
7584{
7585 unsigned long r_symndx;
0d4792f7 7586 int next_r;
411e1bfb
AM
7587 struct elf_link_hash_entry *h;
7588 Elf_Internal_Sym *sym;
7589 asection *sec;
7590 bfd_vma off;
7591
7592 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7593 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7594 return 0;
411e1bfb 7595
37da22e5
AM
7596 if ((*tls_maskp != NULL
7597 && (**tls_maskp & TLS_TLS) != 0
7598 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 7599 || sec == NULL
6bee8834 7600 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7601 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7602 return 1;
411e1bfb
AM
7603
7604 /* Look inside a TOC section too. */
7605 if (h != NULL)
7606 {
7607 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7608 off = h->root.u.def.value;
7609 }
7610 else
7611 off = sym->st_value;
7612 off += rel->r_addend;
7613 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7614 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7615 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7616 if (toc_symndx != NULL)
7617 *toc_symndx = r_symndx;
3a71aa26
AM
7618 if (toc_addend != NULL)
7619 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7620 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7621 return 0;
854b41e7 7622 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7623 && (next_r == -1 || next_r == -2))
7624 return 1 - next_r;
951fd09b 7625 return 1;
411e1bfb
AM
7626}
7627
3b421ab3
AM
7628/* Find (or create) an entry in the tocsave hash table. */
7629
7630static struct tocsave_entry *
7631tocsave_find (struct ppc_link_hash_table *htab,
7632 enum insert_option insert,
7633 Elf_Internal_Sym **local_syms,
7634 const Elf_Internal_Rela *irela,
7635 bfd *ibfd)
7636{
7637 unsigned long r_indx;
7638 struct elf_link_hash_entry *h;
7639 Elf_Internal_Sym *sym;
7640 struct tocsave_entry ent, *p;
7641 hashval_t hash;
7642 struct tocsave_entry **slot;
7643
7644 r_indx = ELF64_R_SYM (irela->r_info);
7645 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7646 return NULL;
7647 if (ent.sec == NULL || ent.sec->output_section == NULL)
7648 {
4eca0228 7649 _bfd_error_handler
871b3ab2 7650 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7651 return NULL;
7652 }
7653
7654 if (h != NULL)
7655 ent.offset = h->root.u.def.value;
7656 else
7657 ent.offset = sym->st_value;
7658 ent.offset += irela->r_addend;
7659
7660 hash = tocsave_htab_hash (&ent);
7661 slot = ((struct tocsave_entry **)
7662 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7663 if (slot == NULL)
7664 return NULL;
7665
7666 if (*slot == NULL)
7667 {
7668 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7669 if (p == NULL)
7670 return NULL;
7671 *p = ent;
7672 *slot = p;
7673 }
7674 return *slot;
7675}
7676
754021d0 7677/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7678 code for the old ABI, these will already have been done. */
754021d0
AM
7679
7680static bfd_boolean
7681adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7682{
7683 struct ppc_link_hash_entry *eh;
7684 asection *sym_sec;
74f0fb50 7685 struct _opd_sec_data *opd;
754021d0
AM
7686
7687 if (h->root.type == bfd_link_hash_indirect)
7688 return TRUE;
7689
754021d0
AM
7690 if (h->root.type != bfd_link_hash_defined
7691 && h->root.type != bfd_link_hash_defweak)
7692 return TRUE;
7693
7694 eh = (struct ppc_link_hash_entry *) h;
7695 if (eh->adjust_done)
7696 return TRUE;
7697
7698 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7699 opd = get_opd_info (sym_sec);
7700 if (opd != NULL && opd->adjust != NULL)
754021d0 7701 {
51aecdc5 7702 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7703 if (adjust == -1)
7704 {
7705 /* This entry has been deleted. */
b3fac117 7706 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7707 if (dsec == NULL)
7708 {
7709 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7710 if (discarded_section (dsec))
81688140 7711 {
b3fac117 7712 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7713 break;
7714 }
7715 }
4025353c 7716 eh->elf.root.u.def.value = 0;
81688140 7717 eh->elf.root.u.def.section = dsec;
4025353c
AM
7718 }
7719 else
7720 eh->elf.root.u.def.value += adjust;
754021d0
AM
7721 eh->adjust_done = 1;
7722 }
7723 return TRUE;
7724}
7725
8c1d1bb8 7726/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7727 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7728 have already been determined. */
7729
7730static bfd_boolean
7731dec_dynrel_count (bfd_vma r_info,
7732 asection *sec,
7733 struct bfd_link_info *info,
7734 Elf_Internal_Sym **local_syms,
7735 struct elf_link_hash_entry *h,
19e08130 7736 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7737{
7738 enum elf_ppc64_reloc_type r_type;
19e08130 7739 asection *sym_sec = NULL;
8c1d1bb8
AM
7740
7741 /* Can this reloc be dynamic? This switch, and later tests here
7742 should be kept in sync with the code in check_relocs. */
7743 r_type = ELF64_R_TYPE (r_info);
7744 switch (r_type)
7745 {
7746 default:
7747 return TRUE;
7748
7749 case R_PPC64_TPREL16:
7750 case R_PPC64_TPREL16_LO:
7751 case R_PPC64_TPREL16_HI:
7752 case R_PPC64_TPREL16_HA:
7753 case R_PPC64_TPREL16_DS:
7754 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7755 case R_PPC64_TPREL16_HIGH:
7756 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7757 case R_PPC64_TPREL16_HIGHER:
7758 case R_PPC64_TPREL16_HIGHERA:
7759 case R_PPC64_TPREL16_HIGHEST:
7760 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
7761 case R_PPC64_TPREL64:
7762 case R_PPC64_DTPMOD64:
7763 case R_PPC64_DTPREL64:
7764 case R_PPC64_ADDR64:
7765 case R_PPC64_REL30:
7766 case R_PPC64_REL32:
7767 case R_PPC64_REL64:
7768 case R_PPC64_ADDR14:
7769 case R_PPC64_ADDR14_BRNTAKEN:
7770 case R_PPC64_ADDR14_BRTAKEN:
7771 case R_PPC64_ADDR16:
7772 case R_PPC64_ADDR16_DS:
7773 case R_PPC64_ADDR16_HA:
7774 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7775 case R_PPC64_ADDR16_HIGH:
7776 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7777 case R_PPC64_ADDR16_HIGHER:
7778 case R_PPC64_ADDR16_HIGHERA:
7779 case R_PPC64_ADDR16_HIGHEST:
7780 case R_PPC64_ADDR16_HIGHESTA:
7781 case R_PPC64_ADDR16_LO:
7782 case R_PPC64_ADDR16_LO_DS:
7783 case R_PPC64_ADDR24:
7784 case R_PPC64_ADDR32:
7785 case R_PPC64_UADDR16:
7786 case R_PPC64_UADDR32:
7787 case R_PPC64_UADDR64:
7788 case R_PPC64_TOC:
7789 break;
7790 }
7791
7792 if (local_syms != NULL)
7793 {
7794 unsigned long r_symndx;
8c1d1bb8
AM
7795 bfd *ibfd = sec->owner;
7796
7797 r_symndx = ELF64_R_SYM (r_info);
7798 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7799 return FALSE;
7800 }
7801
0e1862bb 7802 if ((bfd_link_pic (info)
1d483afe 7803 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7804 || (h != NULL
198f1157 7805 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7806 || h->root.type == bfd_link_hash_defweak
7807 || !h->def_regular))))
7808 || (ELIMINATE_COPY_RELOCS
0e1862bb 7809 && !bfd_link_pic (info)
8c1d1bb8
AM
7810 && h != NULL
7811 && (h->root.type == bfd_link_hash_defweak
7812 || !h->def_regular)))
7813 ;
7814 else
7815 return TRUE;
7816
7817 if (h != NULL)
6edfbbad 7818 {
19e08130
AM
7819 struct elf_dyn_relocs *p;
7820 struct elf_dyn_relocs **pp;
7821 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7822
7823 /* elf_gc_sweep may have already removed all dyn relocs associated
7824 with local syms for a given section. Also, symbol flags are
7825 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7826 report a dynreloc miscount. */
7827 if (*pp == NULL && info->gc_sections)
7828 return TRUE;
7829
7830 while ((p = *pp) != NULL)
60124e18 7831 {
19e08130
AM
7832 if (p->sec == sec)
7833 {
7834 if (!must_be_dyn_reloc (info, r_type))
7835 p->pc_count -= 1;
7836 p->count -= 1;
7837 if (p->count == 0)
7838 *pp = p->next;
7839 return TRUE;
7840 }
7841 pp = &p->next;
60124e18 7842 }
6edfbbad 7843 }
19e08130
AM
7844 else
7845 {
7846 struct ppc_dyn_relocs *p;
7847 struct ppc_dyn_relocs **pp;
7848 void *vpp;
7849 bfd_boolean is_ifunc;
8c1d1bb8 7850
19e08130
AM
7851 if (local_syms == NULL)
7852 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7853 if (sym_sec == NULL)
7854 sym_sec = sec;
c57da1a7 7855
19e08130
AM
7856 vpp = &elf_section_data (sym_sec)->local_dynrel;
7857 pp = (struct ppc_dyn_relocs **) vpp;
7858
7859 if (*pp == NULL && info->gc_sections)
7860 return TRUE;
7861
7862 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7863 while ((p = *pp) != NULL)
8c1d1bb8 7864 {
19e08130
AM
7865 if (p->sec == sec && p->ifunc == is_ifunc)
7866 {
7867 p->count -= 1;
7868 if (p->count == 0)
7869 *pp = p->next;
7870 return TRUE;
7871 }
7872 pp = &p->next;
8c1d1bb8 7873 }
8c1d1bb8
AM
7874 }
7875
695344c0 7876 /* xgettext:c-format */
cf97bcb0
AM
7877 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7878 sec->owner, sec);
8c1d1bb8
AM
7879 bfd_set_error (bfd_error_bad_value);
7880 return FALSE;
7881}
7882
754021d0
AM
7883/* Remove unused Official Procedure Descriptor entries. Currently we
7884 only remove those associated with functions in discarded link-once
7885 sections, or weakly defined functions that have been overridden. It
7886 would be possible to remove many more entries for statically linked
7887 applications. */
7888
b34976b6 7889bfd_boolean
e7d1c40c 7890ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7891{
7892 bfd *ibfd;
754021d0 7893 bfd_boolean some_edited = FALSE;
3f764659 7894 asection *need_pad = NULL;
e7d1c40c
AM
7895 struct ppc_link_hash_table *htab;
7896
7897 htab = ppc_hash_table (info);
7898 if (htab == NULL)
7899 return FALSE;
1e2f5b6e 7900
c72f2fb2 7901 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7902 {
7903 asection *sec;
7904 Elf_Internal_Rela *relstart, *rel, *relend;
7905 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7906 Elf_Internal_Sym *local_syms;
74f0fb50 7907 struct _opd_sec_data *opd;
51aecdc5 7908 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7909 bfd_size_type cnt_16b = 0;
1e2f5b6e 7910
854b41e7
AM
7911 if (!is_ppc64_elf (ibfd))
7912 continue;
7913
1e2f5b6e 7914 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7915 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7916 continue;
7917
dbaa2011 7918 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7919 continue;
7920
1e2f5b6e
AM
7921 if (sec->output_section == bfd_abs_section_ptr)
7922 continue;
7923
7924 /* Look through the section relocs. */
7925 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7926 continue;
7927
6cdc0ccc 7928 local_syms = NULL;
0ffa91dd 7929 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7930
7931 /* Read the relocations. */
4ce794b7 7932 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7933 info->keep_memory);
1e2f5b6e 7934 if (relstart == NULL)
b34976b6 7935 return FALSE;
1e2f5b6e
AM
7936
7937 /* First run through the relocs to check they are sane, and to
7938 determine whether we need to edit this opd section. */
b34976b6 7939 need_edit = FALSE;
51aecdc5 7940 broken = FALSE;
3f764659 7941 need_pad = sec;
1e2f5b6e 7942 relend = relstart + sec->reloc_count;
50bc7936 7943 for (rel = relstart; rel < relend; )
1e2f5b6e 7944 {
04c9666a 7945 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7946 unsigned long r_symndx;
7947 asection *sym_sec;
7948 struct elf_link_hash_entry *h;
7949 Elf_Internal_Sym *sym;
51aecdc5 7950 bfd_vma offset;
1e2f5b6e 7951
51aecdc5 7952 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7953 only interested in the reloc pointing to a function entry
7954 point. */
51aecdc5
AM
7955 offset = rel->r_offset;
7956 if (rel + 1 == relend
7957 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7958 {
7959 /* If someone messes with .opd alignment then after a
7960 "ld -r" we might have padding in the middle of .opd.
7961 Also, there's nothing to prevent someone putting
7962 something silly in .opd with the assembler. No .opd
b34976b6 7963 optimization for them! */
3f764659 7964 broken_opd:
4eca0228 7965 _bfd_error_handler
871b3ab2 7966 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7967 broken = TRUE;
1e2f5b6e
AM
7968 break;
7969 }
7970
50bc7936
AM
7971 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7972 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7973 {
4eca0228 7974 _bfd_error_handler
695344c0 7975 /* xgettext:c-format */
871b3ab2 7976 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7977 ibfd, r_type);
51aecdc5 7978 broken = TRUE;
50bc7936
AM
7979 break;
7980 }
7981
1e2f5b6e 7982 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7983 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7984 r_symndx, ibfd))
50bc7936 7985 goto error_ret;
1e2f5b6e
AM
7986
7987 if (sym_sec == NULL || sym_sec->owner == NULL)
7988 {
411e1bfb
AM
7989 const char *sym_name;
7990 if (h != NULL)
7991 sym_name = h->root.root.string;
7992 else
26c61ae5
L
7993 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7994 sym_sec);
411e1bfb 7995
4eca0228 7996 _bfd_error_handler
695344c0 7997 /* xgettext:c-format */
871b3ab2 7998 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7999 ibfd, sym_name);
51aecdc5 8000 broken = TRUE;
1e2f5b6e
AM
8001 break;
8002 }
8003
51020317
AM
8004 /* opd entries are always for functions defined in the
8005 current input bfd. If the symbol isn't defined in the
8006 input bfd, then we won't be using the function in this
8007 bfd; It must be defined in a linkonce section in another
8008 bfd, or is weak. It's also possible that we are
8009 discarding the function due to a linker script /DISCARD/,
8010 which we test for via the output_section. */
8011 if (sym_sec->owner != ibfd
8012 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 8013 need_edit = TRUE;
1e2f5b6e 8014
50bc7936 8015 rel += 2;
51aecdc5
AM
8016 if (rel + 1 == relend
8017 || (rel + 2 < relend
8018 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8019 ++rel;
8020
8021 if (rel == relend)
3f764659
JJ
8022 {
8023 if (sec->size == offset + 24)
8024 {
8025 need_pad = NULL;
8026 break;
8027 }
51aecdc5 8028 if (sec->size == offset + 16)
3f764659
JJ
8029 {
8030 cnt_16b++;
8031 break;
8032 }
8033 goto broken_opd;
8034 }
3f764659
JJ
8035 else if (rel + 1 < relend
8036 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8037 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8038 {
51aecdc5
AM
8039 if (rel[0].r_offset == offset + 16)
8040 cnt_16b++;
8041 else if (rel[0].r_offset != offset + 24)
8042 goto broken_opd;
3f764659
JJ
8043 }
8044 else
8045 goto broken_opd;
1e2f5b6e
AM
8046 }
8047
e7d1c40c 8048 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 8049
51aecdc5 8050 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
8051 {
8052 Elf_Internal_Rela *write_rel;
d4730f92 8053 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 8054 bfd_byte *rptr, *wptr;
983bddc8 8055 bfd_byte *new_contents;
74f0fb50
AM
8056 bfd_size_type amt;
8057
983bddc8 8058 new_contents = NULL;
51aecdc5 8059 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 8060 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 8061 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
8062 if (opd->adjust == NULL)
8063 return FALSE;
1e2f5b6e
AM
8064
8065 /* This seems a waste of time as input .opd sections are all
8066 zeros as generated by gcc, but I suppose there's no reason
8067 this will always be so. We might start putting something in
8068 the third word of .opd entries. */
8069 if ((sec->flags & SEC_IN_MEMORY) == 0)
8070 {
eea6121a
AM
8071 bfd_byte *loc;
8072 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 8073 {
eea6121a
AM
8074 if (loc != NULL)
8075 free (loc);
50bc7936 8076 error_ret:
6cdc0ccc
AM
8077 if (local_syms != NULL
8078 && symtab_hdr->contents != (unsigned char *) local_syms)
8079 free (local_syms);
6cdc0ccc
AM
8080 if (elf_section_data (sec)->relocs != relstart)
8081 free (relstart);
b34976b6 8082 return FALSE;
6cdc0ccc 8083 }
1e2f5b6e
AM
8084 sec->contents = loc;
8085 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8086 }
8087
8088 elf_section_data (sec)->relocs = relstart;
8089
3f764659 8090 new_contents = sec->contents;
3f764659
JJ
8091 if (add_aux_fields)
8092 {
8093 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8094 if (new_contents == NULL)
8095 return FALSE;
51aecdc5 8096 need_pad = NULL;
3f764659 8097 }
b4f4e59f
AM
8098 wptr = new_contents;
8099 rptr = sec->contents;
1e2f5b6e 8100 write_rel = relstart;
51aecdc5 8101 for (rel = relstart; rel < relend; )
1e2f5b6e 8102 {
50bc7936
AM
8103 unsigned long r_symndx;
8104 asection *sym_sec;
8105 struct elf_link_hash_entry *h;
51aecdc5 8106 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 8107 Elf_Internal_Sym *sym;
51aecdc5
AM
8108 long opd_ent_size;
8109 Elf_Internal_Rela *next_rel;
8110 bfd_boolean skip;
50bc7936
AM
8111
8112 r_symndx = ELF64_R_SYM (rel->r_info);
8113 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 8114 r_symndx, ibfd))
50bc7936
AM
8115 goto error_ret;
8116
51aecdc5
AM
8117 next_rel = rel + 2;
8118 if (next_rel + 1 == relend
8119 || (next_rel + 2 < relend
8120 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8121 ++next_rel;
8122
8123 /* See if the .opd entry is full 24 byte or
8124 16 byte (with fd_aux entry overlapped with next
8125 fd_func). */
8126 opd_ent_size = 24;
8127 if (next_rel == relend)
1e2f5b6e 8128 {
51aecdc5 8129 if (sec->size == rel->r_offset + 16)
3f764659 8130 opd_ent_size = 16;
51aecdc5
AM
8131 }
8132 else if (next_rel->r_offset == rel->r_offset + 16)
8133 opd_ent_size = 16;
3f764659 8134
51aecdc5
AM
8135 if (h != NULL
8136 && h->root.root.string[0] == '.')
8137 {
8c5b4e52
AM
8138 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8139 if (fdh != NULL)
8140 {
8141 fdh = ppc_follow_link (fdh);
8142 if (fdh->elf.root.type != bfd_link_hash_defined
8143 && fdh->elf.root.type != bfd_link_hash_defweak)
8144 fdh = NULL;
8145 }
51aecdc5 8146 }
1e2f5b6e 8147
51aecdc5
AM
8148 skip = (sym_sec->owner != ibfd
8149 || sym_sec->output_section == bfd_abs_section_ptr);
8150 if (skip)
8151 {
8152 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8153 {
51aecdc5
AM
8154 /* Arrange for the function descriptor sym
8155 to be dropped. */
8156 fdh->elf.root.u.def.value = 0;
8157 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8158 }
51aecdc5 8159 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8160
0e1862bb 8161 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8162 rel = next_rel;
8163 else
8164 while (1)
8165 {
8166 if (!dec_dynrel_count (rel->r_info, sec, info,
8167 NULL, h, sym))
8168 goto error_ret;
754021d0 8169
51aecdc5
AM
8170 if (++rel == next_rel)
8171 break;
1e2f5b6e 8172
51aecdc5
AM
8173 r_symndx = ELF64_R_SYM (rel->r_info);
8174 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8175 r_symndx, ibfd))
8176 goto error_ret;
8177 }
50bc7936
AM
8178 }
8179 else
1e2f5b6e 8180 {
51aecdc5
AM
8181 /* We'll be keeping this opd entry. */
8182 long adjust;
8183
8184 if (fdh != NULL)
8185 {
8186 /* Redefine the function descriptor symbol to
8187 this location in the opd section. It is
8188 necessary to update the value here rather
8189 than using an array of adjustments as we do
8190 for local symbols, because various places
8191 in the generic ELF code use the value
8192 stored in u.def.value. */
8193 fdh->elf.root.u.def.value = wptr - new_contents;
8194 fdh->adjust_done = 1;
8195 }
8196
8197 /* Local syms are a bit tricky. We could
8198 tweak them as they can be cached, but
8199 we'd need to look through the local syms
8200 for the function descriptor sym which we
8201 don't have at the moment. So keep an
8202 array of adjustments. */
8203 adjust = (wptr - new_contents) - (rptr - sec->contents);
8204 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8205
8206 if (wptr != rptr)
8207 memcpy (wptr, rptr, opd_ent_size);
8208 wptr += opd_ent_size;
8209 if (add_aux_fields && opd_ent_size == 16)
8210 {
8211 memset (wptr, '\0', 8);
8212 wptr += 8;
8213 }
8214
50bc7936 8215 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8216 new opd entries. */
8217 for ( ; rel != next_rel; ++rel)
8218 {
8219 rel->r_offset += adjust;
8220 if (write_rel != rel)
8221 memcpy (write_rel, rel, sizeof (*rel));
8222 ++write_rel;
8223 }
1e2f5b6e 8224 }
51aecdc5
AM
8225
8226 rptr += opd_ent_size;
1e2f5b6e
AM
8227 }
8228
3f764659 8229 sec->size = wptr - new_contents;
1e2f5b6e 8230 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8231 if (add_aux_fields)
8232 {
8233 free (sec->contents);
8234 sec->contents = new_contents;
8235 }
8236
05bf9422 8237 /* Fudge the header size too, as this is used later in
cdcf6e38 8238 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8239 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8240 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8241 some_edited = TRUE;
1e2f5b6e 8242 }
6cdc0ccc 8243 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8244 free (relstart);
6cdc0ccc 8245
411e1bfb
AM
8246 if (local_syms != NULL
8247 && symtab_hdr->contents != (unsigned char *) local_syms)
8248 {
8249 if (!info->keep_memory)
8250 free (local_syms);
8251 else
8252 symtab_hdr->contents = (unsigned char *) local_syms;
8253 }
8254 }
8255
754021d0
AM
8256 if (some_edited)
8257 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8258
3f764659
JJ
8259 /* If we are doing a final link and the last .opd entry is just 16 byte
8260 long, add a 8 byte padding after it. */
0e1862bb 8261 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8262 {
8263 bfd_byte *p;
8264
8265 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8266 {
8267 BFD_ASSERT (need_pad->size > 0);
8268
8269 p = bfd_malloc (need_pad->size + 8);
8270 if (p == NULL)
8271 return FALSE;
699733f6 8272
3f764659
JJ
8273 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8274 p, 0, need_pad->size))
8275 return FALSE;
8276
8277 need_pad->contents = p;
8278 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8279 }
8280 else
8281 {
8282 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8283 if (p == NULL)
8284 return FALSE;
8285
8286 need_pad->contents = p;
8287 }
8288
8289 memset (need_pad->contents + need_pad->size, 0, 8);
8290 need_pad->size += 8;
8291 }
8292
411e1bfb
AM
8293 return TRUE;
8294}
8295
3e04d765
AM
8296/* Analyze inline PLT call relocations to see whether calls to locally
8297 defined functions can be converted to direct calls. */
8298
8299bfd_boolean
8300ppc64_elf_inline_plt (struct bfd_link_info *info)
8301{
8302 struct ppc_link_hash_table *htab;
8303 bfd *ibfd;
8304 asection *sec;
8305 bfd_vma low_vma, high_vma, limit;
8306
8307 htab = ppc_hash_table (info);
8308 if (htab == NULL)
8309 return FALSE;
8310
8311 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
8312 reduced somewhat to cater for possible stubs that might be added
8313 between the call and its destination. */
8314 if (htab->params->group_size < 0)
8315 {
8316 limit = -htab->params->group_size;
8317 if (limit == 1)
8318 limit = 0x1e00000;
8319 }
8320 else
8321 {
8322 limit = htab->params->group_size;
8323 if (limit == 1)
8324 limit = 0x1c00000;
8325 }
8326
8327 low_vma = -1;
8328 high_vma = 0;
8329 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
8330 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
8331 {
8332 if (low_vma > sec->vma)
8333 low_vma = sec->vma;
8334 if (high_vma < sec->vma + sec->size)
8335 high_vma = sec->vma + sec->size;
8336 }
8337
8338 /* If a "bl" can reach anywhere in local code sections, then we can
8339 convert all inline PLT sequences to direct calls when the symbol
8340 is local. */
8341 if (high_vma - low_vma < limit)
8342 {
8343 htab->can_convert_all_inline_plt = 1;
8344 return TRUE;
8345 }
8346
8347 /* Otherwise, go looking through relocs for cases where a direct
8348 call won't reach. Mark the symbol on any such reloc to disable
8349 the optimization and keep the PLT entry as it seems likely that
8350 this will be better than creating trampolines. Note that this
8351 will disable the optimization for all inline PLT calls to a
8352 particular symbol, not just those that won't reach. The
8353 difficulty in doing a more precise optimization is that the
8354 linker needs to make a decision depending on whether a
8355 particular R_PPC64_PLTCALL insn can be turned into a direct
8356 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
8357 the sequence, and there is nothing that ties those relocs
8358 together except their symbol. */
8359
8360 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8361 {
8362 Elf_Internal_Shdr *symtab_hdr;
8363 Elf_Internal_Sym *local_syms;
8364
8365 if (!is_ppc64_elf (ibfd))
8366 continue;
8367
8368 local_syms = NULL;
8369 symtab_hdr = &elf_symtab_hdr (ibfd);
8370
8371 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8372 if (ppc64_elf_section_data (sec)->has_pltcall
8373 && !bfd_is_abs_section (sec->output_section))
8374 {
8375 Elf_Internal_Rela *relstart, *rel, *relend;
8376
8377 /* Read the relocations. */
8378 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8379 info->keep_memory);
8380 if (relstart == NULL)
8381 return FALSE;
8382
8383 relend = relstart + sec->reloc_count;
8384 for (rel = relstart; rel < relend; )
8385 {
8386 enum elf_ppc64_reloc_type r_type;
8387 unsigned long r_symndx;
8388 asection *sym_sec;
8389 struct elf_link_hash_entry *h;
8390 Elf_Internal_Sym *sym;
8391 unsigned char *tls_maskp;
8392
8393 r_type = ELF64_R_TYPE (rel->r_info);
8394 if (r_type != R_PPC64_PLTCALL)
8395 continue;
8396
8397 r_symndx = ELF64_R_SYM (rel->r_info);
8398 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
8399 r_symndx, ibfd))
8400 {
8401 if (elf_section_data (sec)->relocs != relstart)
8402 free (relstart);
8403 if (local_syms != NULL
8404 && symtab_hdr->contents != (unsigned char *) local_syms)
8405 free (local_syms);
8406 return FALSE;
8407 }
8408
8409 if (sym_sec != NULL && sym_sec->output_section != NULL)
8410 {
8411 bfd_vma from, to;
8412 if (h != NULL)
8413 to = h->root.u.def.value;
8414 else
8415 to = sym->st_value;
8416 to += (rel->r_addend
8417 + sym_sec->output_offset
8418 + sym_sec->output_section->vma);
8419 from = (rel->r_offset
8420 + sec->output_offset
8421 + sec->output_section->vma);
8422 if (to - from + limit < 2 * limit)
8423 *tls_maskp &= ~PLT_KEEP;
8424 }
8425 }
8426 if (elf_section_data (sec)->relocs != relstart)
8427 free (relstart);
8428 }
8429
8430 if (local_syms != NULL
8431 && symtab_hdr->contents != (unsigned char *) local_syms)
8432 {
8433 if (!info->keep_memory)
8434 free (local_syms);
8435 else
8436 symtab_hdr->contents = (unsigned char *) local_syms;
8437 }
8438 }
8439
8440 return TRUE;
8441}
8442
e1918d23 8443/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8444
e1918d23 8445asection *
e7d1c40c 8446ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8447{
411e1bfb
AM
8448 struct ppc_link_hash_table *htab;
8449
411e1bfb 8450 htab = ppc_hash_table (info);
4dfe6ac6
NC
8451 if (htab == NULL)
8452 return NULL;
8453
ee67d69a
AM
8454 if (abiversion (info->output_bfd) == 1)
8455 htab->opd_abi = 1;
8456
e7d1c40c 8457 if (htab->params->no_multi_toc)
33c0ec9d
AM
8458 htab->do_multi_toc = 0;
8459 else if (!htab->do_multi_toc)
e7d1c40c 8460 htab->params->no_multi_toc = 1;
33c0ec9d 8461
8b5f1ed8
AM
8462 /* Default to --no-plt-localentry, as this option can cause problems
8463 with symbol interposition. For example, glibc libpthread.so and
8464 libc.so duplicate many pthread symbols, with a fallback
8465 implementation in libc.so. In some cases the fallback does more
8466 work than the pthread implementation. __pthread_condattr_destroy
8467 is one such symbol: the libpthread.so implementation is
8468 localentry:0 while the libc.so implementation is localentry:8.
8469 An app that "cleverly" uses dlopen to only load necessary
8470 libraries at runtime may omit loading libpthread.so when not
8471 running multi-threaded, which then results in the libc.so
8472 fallback symbols being used and ld.so complaining. Now there
8473 are workarounds in ld (see non_zero_localentry) to detect the
8474 pthread situation, but that may not be the only case where
8475 --plt-localentry can cause trouble. */
f378ab09 8476 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 8477 htab->params->plt_localentry0 = 0;
d44c746a
AM
8478 if (htab->params->plt_localentry0
8479 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8480 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
8481 _bfd_error_handler
8482 (_("warning: --plt-localentry is especially dangerous without "
8483 "ld.so support to detect ABI violations"));
f378ab09 8484
3a71aa26
AM
8485 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8486 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8487 FALSE, FALSE, TRUE));
a7f2871e
AM
8488 /* Move dynamic linking info to the function descriptor sym. */
8489 if (htab->tls_get_addr != NULL)
8490 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8491 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8492 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8493 FALSE, FALSE, TRUE));
7c9cf415 8494 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8495 {
8496 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8497
8498 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8499 FALSE, FALSE, TRUE);
8500 if (opt != NULL)
8501 func_desc_adjust (opt, info);
8502 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8503 FALSE, FALSE, TRUE);
8504 if (opt_fd != NULL
8505 && (opt_fd->root.type == bfd_link_hash_defined
8506 || opt_fd->root.type == bfd_link_hash_defweak))
8507 {
8508 /* If glibc supports an optimized __tls_get_addr call stub,
8509 signalled by the presence of __tls_get_addr_opt, and we'll
8510 be calling __tls_get_addr via a plt call stub, then
8511 make __tls_get_addr point to __tls_get_addr_opt. */
8512 tga_fd = &htab->tls_get_addr_fd->elf;
8513 if (htab->elf.dynamic_sections_created
8514 && tga_fd != NULL
8515 && (tga_fd->type == STT_FUNC
8516 || tga_fd->needs_plt)
8517 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 8518 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
8519 {
8520 struct plt_entry *ent;
8521
8522 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8523 if (ent->plt.refcount > 0)
8524 break;
8525 if (ent != NULL)
8526 {
8527 tga_fd->root.type = bfd_link_hash_indirect;
8528 tga_fd->root.u.i.link = &opt_fd->root;
8529 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 8530 opt_fd->mark = 1;
a7f2871e
AM
8531 if (opt_fd->dynindx != -1)
8532 {
8533 /* Use __tls_get_addr_opt in dynamic relocations. */
8534 opt_fd->dynindx = -1;
8535 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8536 opt_fd->dynstr_index);
8537 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8538 return NULL;
a7f2871e
AM
8539 }
8540 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8541 tga = &htab->tls_get_addr->elf;
8542 if (opt != NULL && tga != NULL)
8543 {
8544 tga->root.type = bfd_link_hash_indirect;
8545 tga->root.u.i.link = &opt->root;
8546 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 8547 opt->mark = 1;
a7f2871e
AM
8548 _bfd_elf_link_hash_hide_symbol (info, opt,
8549 tga->forced_local);
8550 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8551 }
8552 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8553 htab->tls_get_addr_fd->is_func_descriptor = 1;
8554 if (htab->tls_get_addr != NULL)
8555 {
8556 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8557 htab->tls_get_addr->is_func = 1;
8558 }
8559 }
8560 }
8561 }
7c9cf415
AM
8562 else if (htab->params->tls_get_addr_opt < 0)
8563 htab->params->tls_get_addr_opt = 0;
a7f2871e 8564 }
33c0ec9d 8565 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8566}
8387904d 8567
3a71aa26
AM
8568/* Return TRUE iff REL is a branch reloc with a global symbol matching
8569 HASH1 or HASH2. */
8387904d 8570
3a71aa26
AM
8571static bfd_boolean
8572branch_reloc_hash_match (const bfd *ibfd,
8573 const Elf_Internal_Rela *rel,
8574 const struct ppc_link_hash_entry *hash1,
8575 const struct ppc_link_hash_entry *hash2)
8576{
8577 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8578 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8579 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8580
e054468f 8581 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8582 {
3a71aa26
AM
8583 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8584 struct elf_link_hash_entry *h;
8387904d 8585
3a71aa26 8586 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8587 h = elf_follow_link (h);
3a71aa26
AM
8588 if (h == &hash1->elf || h == &hash2->elf)
8589 return TRUE;
a48ebf4d 8590 }
3a71aa26 8591 return FALSE;
951fd09b 8592}
411e1bfb 8593
951fd09b
AM
8594/* Run through all the TLS relocs looking for optimization
8595 opportunities. The linker has been hacked (see ppc64elf.em) to do
8596 a preliminary section layout so that we know the TLS segment
8597 offsets. We can't optimize earlier because some optimizations need
8598 to know the tp offset, and we need to optimize before allocating
8599 dynamic relocations. */
8600
8601bfd_boolean
33c0ec9d 8602ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8603{
8604 bfd *ibfd;
8605 asection *sec;
8606 struct ppc_link_hash_table *htab;
663a1470 8607 unsigned char *toc_ref;
102890f0 8608 int pass;
951fd09b 8609
3cbc1e5e 8610 if (!bfd_link_executable (info))
411e1bfb
AM
8611 return TRUE;
8612
951fd09b 8613 htab = ppc_hash_table (info);
4dfe6ac6
NC
8614 if (htab == NULL)
8615 return FALSE;
8616
663a1470
AM
8617 /* Make two passes over the relocs. On the first pass, mark toc
8618 entries involved with tls relocs, and check that tls relocs
8619 involved in setting up a tls_get_addr call are indeed followed by
8620 such a call. If they are not, we can't do any tls optimization.
8621 On the second pass twiddle tls_mask flags to notify
8622 relocate_section that optimization can be done, and adjust got
8623 and plt refcounts. */
8624 toc_ref = NULL;
8625 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8626 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8627 {
8628 Elf_Internal_Sym *locsyms = NULL;
8629 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8630
102890f0
AM
8631 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8632 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8633 {
8634 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8635 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8636
102890f0
AM
8637 /* Read the relocations. */
8638 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8639 info->keep_memory);
8640 if (relstart == NULL)
2915c55b
JK
8641 {
8642 free (toc_ref);
8643 return FALSE;
8644 }
411e1bfb 8645
102890f0
AM
8646 relend = relstart + sec->reloc_count;
8647 for (rel = relstart; rel < relend; rel++)
8648 {
8649 enum elf_ppc64_reloc_type r_type;
8650 unsigned long r_symndx;
8651 struct elf_link_hash_entry *h;
8652 Elf_Internal_Sym *sym;
8653 asection *sym_sec;
f961d9dd
AM
8654 unsigned char *tls_mask;
8655 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8656 bfd_vma value;
8657 bfd_boolean ok_tprel, is_local;
8658 long toc_ref_index = 0;
8659 int expecting_tls_get_addr = 0;
663a1470 8660 bfd_boolean ret = FALSE;
411e1bfb 8661
102890f0
AM
8662 r_symndx = ELF64_R_SYM (rel->r_info);
8663 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8664 r_symndx, ibfd))
8665 {
8666 err_free_rel:
8667 if (elf_section_data (sec)->relocs != relstart)
8668 free (relstart);
8669 if (toc_ref != NULL)
8670 free (toc_ref);
8671 if (locsyms != NULL
0ffa91dd 8672 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8673 != (unsigned char *) locsyms))
8674 free (locsyms);
663a1470 8675 return ret;
102890f0 8676 }
411e1bfb 8677
102890f0
AM
8678 if (h != NULL)
8679 {
766bc656
AM
8680 if (h->root.type == bfd_link_hash_defined
8681 || h->root.type == bfd_link_hash_defweak)
8682 value = h->root.u.def.value;
8683 else if (h->root.type == bfd_link_hash_undefweak)
8684 value = 0;
8685 else
663a1470
AM
8686 {
8687 found_tls_get_addr_arg = 0;
8688 continue;
8689 }
102890f0
AM
8690 }
8691 else
8692 /* Symbols referenced by TLS relocs must be of type
8693 STT_TLS. So no need for .opd local sym adjust. */
8694 value = sym->st_value;
8695
8696 ok_tprel = FALSE;
8697 is_local = FALSE;
8698 if (h == NULL
8699 || !h->def_dynamic)
8700 {
8701 is_local = TRUE;
766bc656
AM
8702 if (h != NULL
8703 && h->root.type == bfd_link_hash_undefweak)
8704 ok_tprel = TRUE;
c27b8c2a
AM
8705 else if (sym_sec != NULL
8706 && sym_sec->output_section != NULL)
766bc656
AM
8707 {
8708 value += sym_sec->output_offset;
8709 value += sym_sec->output_section->vma;
8710 value -= htab->elf.tls_sec->vma;
8711 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8712 < (bfd_vma) 1 << 32);
8713 }
102890f0 8714 }
951fd09b 8715
102890f0 8716 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8717 /* If this section has old-style __tls_get_addr calls
8718 without marker relocs, then check that each
8719 __tls_get_addr call reloc is preceded by a reloc
8720 that conceivably belongs to the __tls_get_addr arg
8721 setup insn. If we don't find matching arg setup
8722 relocs, don't do any tls optimization. */
8723 if (pass == 0
8724 && sec->has_tls_get_addr_call
8725 && h != NULL
8726 && (h == &htab->tls_get_addr->elf
8727 || h == &htab->tls_get_addr_fd->elf)
8728 && !found_tls_get_addr_arg
8729 && is_branch_reloc (r_type))
8730 {
25f53a85 8731 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8732 "TLS optimization disabled\n"),
8733 ibfd, sec, rel->r_offset);
8734 ret = TRUE;
8735 goto err_free_rel;
8736 }
8737
8738 found_tls_get_addr_arg = 0;
102890f0
AM
8739 switch (r_type)
8740 {
8741 case R_PPC64_GOT_TLSLD16:
8742 case R_PPC64_GOT_TLSLD16_LO:
8743 expecting_tls_get_addr = 1;
663a1470 8744 found_tls_get_addr_arg = 1;
1a0670f3 8745 /* Fall through. */
102890f0
AM
8746
8747 case R_PPC64_GOT_TLSLD16_HI:
8748 case R_PPC64_GOT_TLSLD16_HA:
8749 /* These relocs should never be against a symbol
8750 defined in a shared lib. Leave them alone if
8751 that turns out to be the case. */
8752 if (!is_local)
8753 continue;
411e1bfb 8754
102890f0 8755 /* LD -> LE */
411e1bfb 8756 tls_set = 0;
102890f0
AM
8757 tls_clear = TLS_LD;
8758 tls_type = TLS_TLS | TLS_LD;
8759 break;
411e1bfb 8760
102890f0
AM
8761 case R_PPC64_GOT_TLSGD16:
8762 case R_PPC64_GOT_TLSGD16_LO:
8763 expecting_tls_get_addr = 1;
663a1470 8764 found_tls_get_addr_arg = 1;
1a0670f3 8765 /* Fall through. */
102890f0
AM
8766
8767 case R_PPC64_GOT_TLSGD16_HI:
8768 case R_PPC64_GOT_TLSGD16_HA:
8769 if (ok_tprel)
8770 /* GD -> LE */
411e1bfb 8771 tls_set = 0;
102890f0
AM
8772 else
8773 /* GD -> IE */
8774 tls_set = TLS_TLS | TLS_TPRELGD;
8775 tls_clear = TLS_GD;
8776 tls_type = TLS_TLS | TLS_GD;
8777 break;
8778
8779 case R_PPC64_GOT_TPREL16_DS:
8780 case R_PPC64_GOT_TPREL16_LO_DS:
8781 case R_PPC64_GOT_TPREL16_HI:
8782 case R_PPC64_GOT_TPREL16_HA:
8783 if (ok_tprel)
8784 {
8785 /* IE -> LE */
8786 tls_set = 0;
8787 tls_clear = TLS_TPREL;
8788 tls_type = TLS_TLS | TLS_TPREL;
8789 break;
8790 }
411e1bfb
AM
8791 continue;
8792
727fc41e
AM
8793 case R_PPC64_TLSGD:
8794 case R_PPC64_TLSLD:
23cedd1d
AM
8795 if (rel + 1 < relend
8796 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8797 {
8798 if (pass != 0
8799 && ELF64_R_TYPE (rel[1].r_info) != R_PPC64_PLTSEQ)
8800 {
8801 r_symndx = ELF64_R_SYM (rel[1].r_info);
8802 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8803 r_symndx, ibfd))
8804 goto err_free_rel;
8805 if (h != NULL)
8806 {
8807 struct plt_entry *ent = NULL;
8808
8809 for (ent = h->plt.plist;
8810 ent != NULL;
8811 ent = ent->next)
8812 if (ent->addend == rel[1].r_addend)
8813 break;
8814
8815 if (ent != NULL
8816 && ent->plt.refcount > 0)
8817 ent->plt.refcount -= 1;
8818 }
8819 }
8820 continue;
8821 }
663a1470 8822 found_tls_get_addr_arg = 1;
1a0670f3 8823 /* Fall through. */
663a1470
AM
8824
8825 case R_PPC64_TLS:
8826 case R_PPC64_TOC16:
8827 case R_PPC64_TOC16_LO:
102890f0
AM
8828 if (sym_sec == NULL || sym_sec != toc)
8829 continue;
8830
8831 /* Mark this toc entry as referenced by a TLS
8832 code sequence. We can do that now in the
8833 case of R_PPC64_TLS, and after checking for
8834 tls_get_addr for the TOC16 relocs. */
8835 if (toc_ref == NULL)
663a1470
AM
8836 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8837 if (toc_ref == NULL)
8838 goto err_free_rel;
8839
102890f0
AM
8840 if (h != NULL)
8841 value = h->root.u.def.value;
8842 else
8843 value = sym->st_value;
8844 value += rel->r_addend;
73242275
AM
8845 if (value % 8 != 0)
8846 continue;
8847 BFD_ASSERT (value < toc->size
8848 && toc->output_offset % 8 == 0);
663a1470 8849 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8850 if (r_type == R_PPC64_TLS
8851 || r_type == R_PPC64_TLSGD
8852 || r_type == R_PPC64_TLSLD)
102890f0
AM
8853 {
8854 toc_ref[toc_ref_index] = 1;
8855 continue;
8856 }
8857
8858 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8859 continue;
8860
8861 tls_set = 0;
8862 tls_clear = 0;
8863 expecting_tls_get_addr = 2;
8864 break;
8865
8866 case R_PPC64_TPREL64:
8867 if (pass == 0
8868 || sec != toc
8869 || toc_ref == NULL
663a1470 8870 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8871 continue;
8872 if (ok_tprel)
8873 {
8874 /* IE -> LE */
8875 tls_set = TLS_EXPLICIT;
8876 tls_clear = TLS_TPREL;
8877 break;
8878 }
8879 continue;
8880
8881 case R_PPC64_DTPMOD64:
8882 if (pass == 0
8883 || sec != toc
8884 || toc_ref == NULL
663a1470 8885 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8886 continue;
8887 if (rel + 1 < relend
8888 && (rel[1].r_info
8889 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8890 && rel[1].r_offset == rel->r_offset + 8)
8891 {
8892 if (ok_tprel)
8893 /* GD -> LE */
8894 tls_set = TLS_EXPLICIT | TLS_GD;
8895 else
8896 /* GD -> IE */
8897 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8898 tls_clear = TLS_GD;
8899 }
8900 else
8901 {
8902 if (!is_local)
8903 continue;
8904
8905 /* LD -> LE */
8906 tls_set = TLS_EXPLICIT;
8907 tls_clear = TLS_LD;
8908 }
8909 break;
8910
8911 default:
8912 continue;
8913 }
8914
8915 if (pass == 0)
8916 {
727fc41e
AM
8917 if (!expecting_tls_get_addr
8918 || !sec->has_tls_get_addr_call)
102890f0
AM
8919 continue;
8920
3a71aa26
AM
8921 if (rel + 1 < relend
8922 && branch_reloc_hash_match (ibfd, rel + 1,
8923 htab->tls_get_addr,
8924 htab->tls_get_addr_fd))
102890f0 8925 {
3a71aa26 8926 if (expecting_tls_get_addr == 2)
102890f0 8927 {
3a71aa26 8928 /* Check for toc tls entries. */
f961d9dd 8929 unsigned char *toc_tls;
3a71aa26
AM
8930 int retval;
8931
8932 retval = get_tls_mask (&toc_tls, NULL, NULL,
8933 &locsyms,
8934 rel, ibfd);
8935 if (retval == 0)
8936 goto err_free_rel;
663a1470
AM
8937 if (toc_tls != NULL)
8938 {
37da22e5
AM
8939 if ((*toc_tls & TLS_TLS) != 0
8940 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8941 found_tls_get_addr_arg = 1;
8942 if (retval > 1)
8943 toc_ref[toc_ref_index] = 1;
8944 }
102890f0 8945 }
3a71aa26 8946 continue;
102890f0
AM
8947 }
8948
102890f0
AM
8949 /* Uh oh, we didn't find the expected call. We
8950 could just mark this symbol to exclude it
8951 from tls optimization but it's safer to skip
663a1470 8952 the entire optimization. */
695344c0 8953 /* xgettext:c-format */
25f53a85 8954 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8955 "TLS optimization disabled\n"),
8956 ibfd, sec, rel->r_offset);
8957 ret = TRUE;
8958 goto err_free_rel;
102890f0
AM
8959 }
8960
37da22e5
AM
8961 /* If we don't have old-style __tls_get_addr calls
8962 without TLSGD/TLSLD marker relocs, and we haven't
8963 found a new-style __tls_get_addr call with a
8964 marker for this symbol, then we either have a
8965 broken object file or an -mlongcall style
8966 indirect call to __tls_get_addr without a marker.
8967 Disable optimization in this case. */
8968 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8969 && (tls_set & TLS_EXPLICIT) == 0
8970 && !sec->has_tls_get_addr_call
8971 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8972 != (TLS_TLS | TLS_MARK)))
8973 continue;
8974
23cedd1d 8975 if (expecting_tls_get_addr)
102890f0 8976 {
23cedd1d
AM
8977 struct plt_entry *ent = NULL;
8978
8979 if (htab->tls_get_addr != NULL)
8980 for (ent = htab->tls_get_addr->elf.plt.plist;
8981 ent != NULL;
8982 ent = ent->next)
8983 if (ent->addend == 0)
102890f0 8984 break;
411e1bfb 8985
23cedd1d
AM
8986 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8987 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8988 ent != NULL;
8989 ent = ent->next)
8990 if (ent->addend == 0)
102890f0 8991 break;
23cedd1d
AM
8992
8993 if (ent != NULL
8994 && ent->plt.refcount > 0)
8995 ent->plt.refcount -= 1;
102890f0 8996 }
411e1bfb 8997
102890f0 8998 if (tls_clear == 0)
30038c59
AM
8999 continue;
9000
102890f0
AM
9001 if ((tls_set & TLS_EXPLICIT) == 0)
9002 {
9003 struct got_entry *ent;
411e1bfb 9004
102890f0
AM
9005 /* Adjust got entry for this reloc. */
9006 if (h != NULL)
9007 ent = h->got.glist;
9008 else
9009 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 9010
102890f0
AM
9011 for (; ent != NULL; ent = ent->next)
9012 if (ent->addend == rel->r_addend
9013 && ent->owner == ibfd
9014 && ent->tls_type == tls_type)
9015 break;
9016 if (ent == NULL)
9017 abort ();
411e1bfb 9018
102890f0
AM
9019 if (tls_set == 0)
9020 {
9021 /* We managed to get rid of a got entry. */
9022 if (ent->got.refcount > 0)
9023 ent->got.refcount -= 1;
9024 }
9025 }
9026 else
9027 {
9028 /* If we got rid of a DTPMOD/DTPREL reloc pair then
9029 we'll lose one or two dyn relocs. */
9030 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 9031 NULL, h, sym))
102890f0 9032 return FALSE;
411e1bfb 9033
102890f0
AM
9034 if (tls_set == (TLS_EXPLICIT | TLS_GD))
9035 {
9036 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 9037 NULL, h, sym))
102890f0
AM
9038 return FALSE;
9039 }
9040 }
411e1bfb 9041
102890f0
AM
9042 *tls_mask |= tls_set;
9043 *tls_mask &= ~tls_clear;
9044 }
8c1d1bb8 9045
102890f0
AM
9046 if (elf_section_data (sec)->relocs != relstart)
9047 free (relstart);
9048 }
411e1bfb 9049
663a1470
AM
9050 if (locsyms != NULL
9051 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
9052 {
9053 if (!info->keep_memory)
9054 free (locsyms);
9055 else
9056 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
9057 }
9058 }
411e1bfb 9059
663a1470
AM
9060 if (toc_ref != NULL)
9061 free (toc_ref);
9a23f96e 9062 htab->do_tls_opt = 1;
b34976b6 9063 return TRUE;
1e2f5b6e 9064}
b34976b6 9065
c5614fa4
AM
9066/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
9067 the values of any global symbols in a toc section that has been
9068 edited. Globals in toc sections should be a rarity, so this function
9069 sets a flag if any are found in toc sections other than the one just
de194d85 9070 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
9071
9072struct adjust_toc_info
9073{
9074 asection *toc;
9075 unsigned long *skip;
9076 bfd_boolean global_toc_syms;
9077};
9078
ba761f19
AM
9079enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
9080
c5614fa4
AM
9081static bfd_boolean
9082adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
9083{
9084 struct ppc_link_hash_entry *eh;
9085 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 9086 unsigned long i;
c5614fa4 9087
c5614fa4
AM
9088 if (h->root.type != bfd_link_hash_defined
9089 && h->root.type != bfd_link_hash_defweak)
9090 return TRUE;
9091
9092 eh = (struct ppc_link_hash_entry *) h;
9093 if (eh->adjust_done)
9094 return TRUE;
9095
9096 if (eh->elf.root.u.def.section == toc_inf->toc)
9097 {
854b41e7
AM
9098 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
9099 i = toc_inf->toc->rawsize >> 3;
c5614fa4 9100 else
854b41e7
AM
9101 i = eh->elf.root.u.def.value >> 3;
9102
ba761f19 9103 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 9104 {
4eca0228 9105 _bfd_error_handler
854b41e7
AM
9106 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
9107 do
9108 ++i;
ba761f19 9109 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 9110 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 9111 }
854b41e7
AM
9112
9113 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
9114 eh->adjust_done = 1;
9115 }
9116 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
9117 toc_inf->global_toc_syms = TRUE;
9118
9119 return TRUE;
9120}
9121
39eeab25
AM
9122/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9123 on a _LO variety toc/got reloc. */
560c8763
AM
9124
9125static bfd_boolean
39eeab25 9126ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 9127{
39eeab25
AM
9128 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9129 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
9130 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9131 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9132 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9133 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9134 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9135 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9136 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9137 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9138 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9139 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9140 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9141 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9142 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
9143 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9144 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9145 /* Exclude lfqu by testing reloc. If relocs are ever
9146 defined for the reduced D field in psq_lu then those
9147 will need testing too. */
9148 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9149 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9150 && (insn & 1) == 0)
9151 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9152 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9153 /* Exclude stfqu. psq_stu as above for psq_lu. */
9154 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9155 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9156 && (insn & 1) == 0));
560c8763
AM
9157}
9158
c5614fa4
AM
9159/* Examine all relocs referencing .toc sections in order to remove
9160 unused .toc entries. */
9161
9162bfd_boolean
33c0ec9d 9163ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
9164{
9165 bfd *ibfd;
9166 struct adjust_toc_info toc_inf;
67f0cbdb 9167 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 9168
67f0cbdb 9169 htab->do_toc_opt = 1;
c5614fa4 9170 toc_inf.global_toc_syms = TRUE;
c72f2fb2 9171 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
9172 {
9173 asection *toc, *sec;
9174 Elf_Internal_Shdr *symtab_hdr;
9175 Elf_Internal_Sym *local_syms;
425b145b 9176 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
9177 unsigned long *skip, *drop;
9178 unsigned char *used;
9179 unsigned char *keep, last, some_unused;
9180
854b41e7
AM
9181 if (!is_ppc64_elf (ibfd))
9182 continue;
9183
c5614fa4
AM
9184 toc = bfd_get_section_by_name (ibfd, ".toc");
9185 if (toc == NULL
92b7a70f 9186 || toc->size == 0
dbaa2011
AM
9187 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9188 || discarded_section (toc))
c5614fa4
AM
9189 continue;
9190
425b145b 9191 toc_relocs = NULL;
c5614fa4 9192 local_syms = NULL;
0ffa91dd 9193 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
9194
9195 /* Look at sections dropped from the final link. */
9196 skip = NULL;
9197 relstart = NULL;
9198 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9199 {
9200 if (sec->reloc_count == 0
dbaa2011 9201 || !discarded_section (sec)
c5614fa4
AM
9202 || get_opd_info (sec)
9203 || (sec->flags & SEC_ALLOC) == 0
9204 || (sec->flags & SEC_DEBUGGING) != 0)
9205 continue;
9206
9207 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9208 if (relstart == NULL)
9209 goto error_ret;
9210
9211 /* Run through the relocs to see which toc entries might be
9212 unused. */
9213 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9214 {
9215 enum elf_ppc64_reloc_type r_type;
9216 unsigned long r_symndx;
9217 asection *sym_sec;
9218 struct elf_link_hash_entry *h;
9219 Elf_Internal_Sym *sym;
9220 bfd_vma val;
9221
9222 r_type = ELF64_R_TYPE (rel->r_info);
9223 switch (r_type)
9224 {
9225 default:
9226 continue;
9227
9228 case R_PPC64_TOC16:
9229 case R_PPC64_TOC16_LO:
9230 case R_PPC64_TOC16_HI:
9231 case R_PPC64_TOC16_HA:
9232 case R_PPC64_TOC16_DS:
9233 case R_PPC64_TOC16_LO_DS:
9234 break;
9235 }
9236
9237 r_symndx = ELF64_R_SYM (rel->r_info);
9238 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9239 r_symndx, ibfd))
9240 goto error_ret;
9241
9242 if (sym_sec != toc)
9243 continue;
9244
9245 if (h != NULL)
9246 val = h->root.u.def.value;
9247 else
9248 val = sym->st_value;
9249 val += rel->r_addend;
9250
9251 if (val >= toc->size)
9252 continue;
9253
9254 /* Anything in the toc ought to be aligned to 8 bytes.
9255 If not, don't mark as unused. */
9256 if (val & 7)
9257 continue;
9258
9259 if (skip == NULL)
9260 {
854b41e7 9261 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9262 if (skip == NULL)
9263 goto error_ret;
9264 }
9265
ba761f19 9266 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9267 }
9268
9269 if (elf_section_data (sec)->relocs != relstart)
9270 free (relstart);
9271 }
9272
ba761f19
AM
9273 /* For largetoc loads of address constants, we can convert
9274 . addis rx,2,addr@got@ha
9275 . ld ry,addr@got@l(rx)
9276 to
9277 . addis rx,2,addr@toc@ha
9278 . addi ry,rx,addr@toc@l
9279 when addr is within 2G of the toc pointer. This then means
9280 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9281
ba761f19
AM
9282 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9283 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9284 && toc->reloc_count != 0)
9285 {
9286 /* Read toc relocs. */
425b145b
AM
9287 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9288 info->keep_memory);
9289 if (toc_relocs == NULL)
ba761f19
AM
9290 goto error_ret;
9291
425b145b 9292 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9293 {
9294 enum elf_ppc64_reloc_type r_type;
9295 unsigned long r_symndx;
9296 asection *sym_sec;
9297 struct elf_link_hash_entry *h;
9298 Elf_Internal_Sym *sym;
9299 bfd_vma val, addr;
9300
9301 r_type = ELF64_R_TYPE (rel->r_info);
9302 if (r_type != R_PPC64_ADDR64)
9303 continue;
9304
9305 r_symndx = ELF64_R_SYM (rel->r_info);
9306 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9307 r_symndx, ibfd))
9308 goto error_ret;
9309
425b145b 9310 if (sym_sec == NULL
c27b8c2a 9311 || sym_sec->output_section == NULL
dbaa2011 9312 || discarded_section (sym_sec))
425b145b
AM
9313 continue;
9314
afe397ea 9315 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9316 continue;
9317
9318 if (h != NULL)
bddc25c9
AM
9319 {
9320 if (h->type == STT_GNU_IFUNC)
9321 continue;
9322 val = h->root.u.def.value;
9323 }
ba761f19 9324 else
bddc25c9
AM
9325 {
9326 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9327 continue;
9328 val = sym->st_value;
9329 }
ba761f19
AM
9330 val += rel->r_addend;
9331 val += sym_sec->output_section->vma + sym_sec->output_offset;
9332
9333 /* We don't yet know the exact toc pointer value, but we
9334 know it will be somewhere in the toc section. Don't
9335 optimize if the difference from any possible toc
9336 pointer is outside [ff..f80008000, 7fff7fff]. */
9337 addr = toc->output_section->vma + TOC_BASE_OFF;
9338 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9339 continue;
9340
9341 addr = toc->output_section->vma + toc->output_section->rawsize;
9342 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9343 continue;
9344
9345 if (skip == NULL)
9346 {
9347 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9348 if (skip == NULL)
9349 goto error_ret;
9350 }
9351
9352 skip[rel->r_offset >> 3]
425b145b 9353 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9354 }
ba761f19
AM
9355 }
9356
c5614fa4
AM
9357 if (skip == NULL)
9358 continue;
9359
9360 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9361 if (used == NULL)
9362 {
9363 error_ret:
9364 if (local_syms != NULL
9365 && symtab_hdr->contents != (unsigned char *) local_syms)
9366 free (local_syms);
9367 if (sec != NULL
9368 && relstart != NULL
9369 && elf_section_data (sec)->relocs != relstart)
9370 free (relstart);
425b145b
AM
9371 if (toc_relocs != NULL
9372 && elf_section_data (toc)->relocs != toc_relocs)
9373 free (toc_relocs);
c5614fa4
AM
9374 if (skip != NULL)
9375 free (skip);
9376 return FALSE;
9377 }
9378
30038c59
AM
9379 /* Now check all kept sections that might reference the toc.
9380 Check the toc itself last. */
9381 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9382 : ibfd->sections);
c5614fa4 9383 sec != NULL;
c5614fa4 9384 sec = (sec == toc ? NULL
c5614fa4 9385 : sec->next == NULL ? toc
30038c59 9386 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9387 : sec->next))
9388 {
9389 int repeat;
9390
9391 if (sec->reloc_count == 0
dbaa2011 9392 || discarded_section (sec)
c5614fa4
AM
9393 || get_opd_info (sec)
9394 || (sec->flags & SEC_ALLOC) == 0
9395 || (sec->flags & SEC_DEBUGGING) != 0)
9396 continue;
9397
854b41e7
AM
9398 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9399 info->keep_memory);
c5614fa4 9400 if (relstart == NULL)
2915c55b
JK
9401 {
9402 free (used);
9403 goto error_ret;
9404 }
c5614fa4
AM
9405
9406 /* Mark toc entries referenced as used. */
c5614fa4 9407 do
d4f1ee75
AM
9408 {
9409 repeat = 0;
9410 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9411 {
9412 enum elf_ppc64_reloc_type r_type;
9413 unsigned long r_symndx;
9414 asection *sym_sec;
9415 struct elf_link_hash_entry *h;
9416 Elf_Internal_Sym *sym;
9417 bfd_vma val;
9418 enum {no_check, check_lo, check_ha} insn_check;
98528052 9419
d4f1ee75
AM
9420 r_type = ELF64_R_TYPE (rel->r_info);
9421 switch (r_type)
9422 {
9423 default:
9424 insn_check = no_check;
9425 break;
98528052 9426
d4f1ee75
AM
9427 case R_PPC64_GOT_TLSLD16_HA:
9428 case R_PPC64_GOT_TLSGD16_HA:
9429 case R_PPC64_GOT_TPREL16_HA:
9430 case R_PPC64_GOT_DTPREL16_HA:
9431 case R_PPC64_GOT16_HA:
9432 case R_PPC64_TOC16_HA:
9433 insn_check = check_ha;
9434 break;
98528052 9435
d4f1ee75
AM
9436 case R_PPC64_GOT_TLSLD16_LO:
9437 case R_PPC64_GOT_TLSGD16_LO:
9438 case R_PPC64_GOT_TPREL16_LO_DS:
9439 case R_PPC64_GOT_DTPREL16_LO_DS:
9440 case R_PPC64_GOT16_LO:
9441 case R_PPC64_GOT16_LO_DS:
9442 case R_PPC64_TOC16_LO:
9443 case R_PPC64_TOC16_LO_DS:
9444 insn_check = check_lo;
9445 break;
9446 }
560c8763 9447
d4f1ee75
AM
9448 if (insn_check != no_check)
9449 {
9450 bfd_vma off = rel->r_offset & ~3;
9451 unsigned char buf[4];
9452 unsigned int insn;
c5614fa4 9453
d4f1ee75
AM
9454 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9455 {
9456 free (used);
9457 goto error_ret;
9458 }
9459 insn = bfd_get_32 (ibfd, buf);
9460 if (insn_check == check_lo
39eeab25 9461 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
9462 : ((insn & ((0x3f << 26) | 0x1f << 16))
9463 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9464 {
9465 char str[12];
9466
9467 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9468 sprintf (str, "%#08x", insn);
9469 info->callbacks->einfo
695344c0 9470 /* xgettext:c-format */
174d0a74 9471 (_("%H: toc optimization is not supported for"
cf97bcb0 9472 " %s instruction\n"),
d4f1ee75
AM
9473 ibfd, sec, rel->r_offset & ~3, str);
9474 }
9475 }
c5614fa4 9476
d4f1ee75
AM
9477 switch (r_type)
9478 {
9479 case R_PPC64_TOC16:
9480 case R_PPC64_TOC16_LO:
9481 case R_PPC64_TOC16_HI:
9482 case R_PPC64_TOC16_HA:
9483 case R_PPC64_TOC16_DS:
9484 case R_PPC64_TOC16_LO_DS:
9485 /* In case we're taking addresses of toc entries. */
9486 case R_PPC64_ADDR64:
9487 break;
c5614fa4 9488
d4f1ee75
AM
9489 default:
9490 continue;
9491 }
c5614fa4 9492
d4f1ee75
AM
9493 r_symndx = ELF64_R_SYM (rel->r_info);
9494 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9495 r_symndx, ibfd))
9496 {
9497 free (used);
9498 goto error_ret;
9499 }
c5614fa4 9500
d4f1ee75
AM
9501 if (sym_sec != toc)
9502 continue;
c5614fa4 9503
d4f1ee75
AM
9504 if (h != NULL)
9505 val = h->root.u.def.value;
9506 else
9507 val = sym->st_value;
9508 val += rel->r_addend;
ba761f19 9509
d4f1ee75
AM
9510 if (val >= toc->size)
9511 continue;
ba761f19 9512
d4f1ee75
AM
9513 if ((skip[val >> 3] & can_optimize) != 0)
9514 {
9515 bfd_vma off;
9516 unsigned char opc;
9517
9518 switch (r_type)
9519 {
9520 case R_PPC64_TOC16_HA:
ba761f19 9521 break;
ba761f19 9522
d4f1ee75
AM
9523 case R_PPC64_TOC16_LO_DS:
9524 off = rel->r_offset;
9525 off += (bfd_big_endian (ibfd) ? -2 : 3);
9526 if (!bfd_get_section_contents (ibfd, sec, &opc,
9527 off, 1))
9528 {
9529 free (used);
9530 goto error_ret;
9531 }
9532 if ((opc & (0x3f << 2)) == (58u << 2))
9533 break;
1a0670f3 9534 /* Fall through. */
ba761f19 9535
d4f1ee75
AM
9536 default:
9537 /* Wrong sort of reloc, or not a ld. We may
9538 as well clear ref_from_discarded too. */
9539 skip[val >> 3] = 0;
9540 }
9541 }
9542
9543 if (sec != toc)
9544 used[val >> 3] = 1;
9545 /* For the toc section, we only mark as used if this
9546 entry itself isn't unused. */
9547 else if ((used[rel->r_offset >> 3]
9548 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9549 && !used[val >> 3])
9550 {
9551 /* Do all the relocs again, to catch reference
9552 chains. */
9553 repeat = 1;
9554 used[val >> 3] = 1;
9555 }
9556 }
9557 }
c5614fa4 9558 while (repeat);
854b41e7
AM
9559
9560 if (elf_section_data (sec)->relocs != relstart)
9561 free (relstart);
c5614fa4
AM
9562 }
9563
9564 /* Merge the used and skip arrays. Assume that TOC
9565 doublewords not appearing as either used or unused belong
de194d85 9566 to an entry more than one doubleword in size. */
c5614fa4
AM
9567 for (drop = skip, keep = used, last = 0, some_unused = 0;
9568 drop < skip + (toc->size + 7) / 8;
9569 ++drop, ++keep)
9570 {
9571 if (*keep)
9572 {
ba761f19
AM
9573 *drop &= ~ref_from_discarded;
9574 if ((*drop & can_optimize) != 0)
9575 some_unused = 1;
c5614fa4
AM
9576 last = 0;
9577 }
b140b010 9578 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9579 {
9580 some_unused = 1;
ba761f19 9581 last = ref_from_discarded;
c5614fa4
AM
9582 }
9583 else
9584 *drop = last;
9585 }
9586
9587 free (used);
9588
9589 if (some_unused)
9590 {
9591 bfd_byte *contents, *src;
9592 unsigned long off;
d62b3684 9593 Elf_Internal_Sym *sym;
ba761f19 9594 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9595
9596 /* Shuffle the toc contents, and at the same time convert the
9597 skip array from booleans into offsets. */
9598 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9599 goto error_ret;
9600
9601 elf_section_data (toc)->this_hdr.contents = contents;
9602
9603 for (src = contents, off = 0, drop = skip;
9604 src < contents + toc->size;
9605 src += 8, ++drop)
9606 {
ba761f19
AM
9607 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9608 off += 8;
c5614fa4
AM
9609 else if (off != 0)
9610 {
9611 *drop = off;
9612 memcpy (src - off, src, 8);
9613 }
9614 }
854b41e7 9615 *drop = off;
c5614fa4
AM
9616 toc->rawsize = toc->size;
9617 toc->size = src - contents - off;
9618
ba761f19
AM
9619 /* Adjust addends for relocs against the toc section sym,
9620 and optimize any accesses we can. */
c5614fa4
AM
9621 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9622 {
9623 if (sec->reloc_count == 0
dbaa2011 9624 || discarded_section (sec))
c5614fa4
AM
9625 continue;
9626
9627 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9628 info->keep_memory);
c5614fa4
AM
9629 if (relstart == NULL)
9630 goto error_ret;
9631
9632 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9633 {
9634 enum elf_ppc64_reloc_type r_type;
9635 unsigned long r_symndx;
9636 asection *sym_sec;
9637 struct elf_link_hash_entry *h;
854b41e7 9638 bfd_vma val;
c5614fa4
AM
9639
9640 r_type = ELF64_R_TYPE (rel->r_info);
9641 switch (r_type)
9642 {
9643 default:
9644 continue;
9645
9646 case R_PPC64_TOC16:
9647 case R_PPC64_TOC16_LO:
9648 case R_PPC64_TOC16_HI:
9649 case R_PPC64_TOC16_HA:
9650 case R_PPC64_TOC16_DS:
9651 case R_PPC64_TOC16_LO_DS:
9652 case R_PPC64_ADDR64:
9653 break;
9654 }
9655
9656 r_symndx = ELF64_R_SYM (rel->r_info);
9657 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9658 r_symndx, ibfd))
9659 goto error_ret;
9660
ba761f19 9661 if (sym_sec != toc)
c5614fa4
AM
9662 continue;
9663
ba761f19
AM
9664 if (h != NULL)
9665 val = h->root.u.def.value;
9666 else
9667 {
9668 val = sym->st_value;
9669 if (val != 0)
9670 local_toc_syms = TRUE;
9671 }
9672
9673 val += rel->r_addend;
854b41e7
AM
9674
9675 if (val > toc->rawsize)
9676 val = toc->rawsize;
ba761f19
AM
9677 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9678 continue;
9679 else if ((skip[val >> 3] & can_optimize) != 0)
9680 {
9681 Elf_Internal_Rela *tocrel
425b145b 9682 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9683 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9684
9685 switch (r_type)
9686 {
9687 case R_PPC64_TOC16_HA:
9688 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9689 break;
9690
9691 case R_PPC64_TOC16_LO_DS:
9692 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9693 break;
9694
9695 default:
28942f62
AM
9696 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9697 ppc_howto_init ();
b140b010 9698 info->callbacks->einfo
695344c0 9699 /* xgettext:c-format */
174d0a74 9700 (_("%H: %s references "
b140b010
AM
9701 "optimized away TOC entry\n"),
9702 ibfd, sec, rel->r_offset,
9703 ppc64_elf_howto_table[r_type]->name);
9704 bfd_set_error (bfd_error_bad_value);
9705 goto error_ret;
ba761f19
AM
9706 }
9707 rel->r_addend = tocrel->r_addend;
9708 elf_section_data (sec)->relocs = relstart;
9709 continue;
9710 }
9711
9712 if (h != NULL || sym->st_value != 0)
9713 continue;
854b41e7
AM
9714
9715 rel->r_addend -= skip[val >> 3];
9716 elf_section_data (sec)->relocs = relstart;
c5614fa4 9717 }
854b41e7
AM
9718
9719 if (elf_section_data (sec)->relocs != relstart)
9720 free (relstart);
c5614fa4
AM
9721 }
9722
9723 /* We shouldn't have local or global symbols defined in the TOC,
9724 but handle them anyway. */
df22d223
AM
9725 if (local_syms != NULL)
9726 for (sym = local_syms;
9727 sym < local_syms + symtab_hdr->sh_info;
9728 ++sym)
9729 if (sym->st_value != 0
9730 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9731 {
9732 unsigned long i;
854b41e7 9733
df22d223
AM
9734 if (sym->st_value > toc->rawsize)
9735 i = toc->rawsize >> 3;
9736 else
9737 i = sym->st_value >> 3;
854b41e7 9738
df22d223
AM
9739 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9740 {
9741 if (local_toc_syms)
4eca0228 9742 _bfd_error_handler
df22d223
AM
9743 (_("%s defined on removed toc entry"),
9744 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9745 do
9746 ++i;
9747 while ((skip[i] & (ref_from_discarded | can_optimize)));
9748 sym->st_value = (bfd_vma) i << 3;
9749 }
d62b3684 9750
df22d223
AM
9751 sym->st_value -= skip[i];
9752 symtab_hdr->contents = (unsigned char *) local_syms;
9753 }
c5614fa4 9754
854b41e7 9755 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9756 if (toc_inf.global_toc_syms)
9757 {
9758 toc_inf.toc = toc;
9759 toc_inf.skip = skip;
9760 toc_inf.global_toc_syms = FALSE;
9761 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9762 &toc_inf);
9763 }
854b41e7
AM
9764
9765 if (toc->reloc_count != 0)
9766 {
d4730f92 9767 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9768 Elf_Internal_Rela *wrel;
9769 bfd_size_type sz;
9770
854b41e7 9771 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9772 if (toc_relocs == NULL)
9773 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9774 info->keep_memory);
9775 if (toc_relocs == NULL)
9776 goto error_ret;
9777
425b145b
AM
9778 wrel = toc_relocs;
9779 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9780 if ((skip[rel->r_offset >> 3]
9781 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9782 {
9783 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9784 wrel->r_info = rel->r_info;
9785 wrel->r_addend = rel->r_addend;
9786 ++wrel;
9787 }
9788 else if (!dec_dynrel_count (rel->r_info, toc, info,
9789 &local_syms, NULL, NULL))
9790 goto error_ret;
9791
425b145b
AM
9792 elf_section_data (toc)->relocs = toc_relocs;
9793 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9794 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9795 sz = rel_hdr->sh_entsize;
9796 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9797 }
c5614fa4 9798 }
28be611c
AM
9799 else if (toc_relocs != NULL
9800 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9801 free (toc_relocs);
c5614fa4
AM
9802
9803 if (local_syms != NULL
9804 && symtab_hdr->contents != (unsigned char *) local_syms)
9805 {
9806 if (!info->keep_memory)
9807 free (local_syms);
9808 else
9809 symtab_hdr->contents = (unsigned char *) local_syms;
9810 }
9811 free (skip);
9812 }
9813
9814 return TRUE;
9815}
9816
1bbe0902
AM
9817/* Return true iff input section I references the TOC using
9818 instructions limited to +/-32k offsets. */
9819
9820bfd_boolean
9821ppc64_elf_has_small_toc_reloc (asection *i)
9822{
9823 return (is_ppc64_elf (i->owner)
9824 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9825}
9826
927be08e
AM
9827/* Allocate space for one GOT entry. */
9828
9829static void
9830allocate_got (struct elf_link_hash_entry *h,
9831 struct bfd_link_info *info,
9832 struct got_entry *gent)
9833{
9834 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9835 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9836 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9837 ? 16 : 8);
9838 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9839 ? 2 : 1) * sizeof (Elf64_External_Rela);
9840 asection *got = ppc64_elf_tdata (gent->owner)->got;
9841
9842 gent->got.offset = got->size;
9843 got->size += entsize;
9844
19e08130 9845 if (h->type == STT_GNU_IFUNC)
927be08e 9846 {
33e44f2e 9847 htab->elf.irelplt->size += rentsize;
19e08130 9848 htab->got_reli_size += rentsize;
927be08e 9849 }
f15d0b54
AM
9850 else if (((bfd_link_pic (info)
9851 && !((gent->tls_type & TLS_TPREL) != 0
9852 && bfd_link_executable (info)
9853 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9854 || (htab->elf.dynamic_sections_created
9855 && h->dynindx != -1
9856 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9857 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9858 {
19e08130 9859 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9860 relgot->size += rentsize;
927be08e
AM
9861 }
9862}
9863
7865406b
AM
9864/* This function merges got entries in the same toc group. */
9865
9866static void
9867merge_got_entries (struct got_entry **pent)
9868{
9869 struct got_entry *ent, *ent2;
9870
9871 for (ent = *pent; ent != NULL; ent = ent->next)
9872 if (!ent->is_indirect)
9873 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9874 if (!ent2->is_indirect
9875 && ent2->addend == ent->addend
9876 && ent2->tls_type == ent->tls_type
9877 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9878 {
9879 ent2->is_indirect = TRUE;
9880 ent2->got.ent = ent;
9881 }
9882}
9883
46434633 9884/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9885
9886static bfd_boolean
46434633
AM
9887ensure_undef_dynamic (struct bfd_link_info *info,
9888 struct elf_link_hash_entry *h)
f0158f44
AM
9889{
9890 struct elf_link_hash_table *htab = elf_hash_table (info);
9891
9892 if (htab->dynamic_sections_created
46434633
AM
9893 && ((info->dynamic_undefined_weak != 0
9894 && h->root.type == bfd_link_hash_undefweak)
9895 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9896 && h->dynindx == -1
9897 && !h->forced_local
9898 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9899 return bfd_elf_link_record_dynamic_symbol (info, h);
9900 return TRUE;
9901}
9902
65f38f15
AM
9903/* Allocate space in .plt, .got and associated reloc sections for
9904 dynamic relocs. */
5bd4f169 9905
b34976b6 9906static bfd_boolean
4ce794b7 9907allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9908{
65f38f15
AM
9909 struct bfd_link_info *info;
9910 struct ppc_link_hash_table *htab;
5bd4f169 9911 asection *s;
65f38f15 9912 struct ppc_link_hash_entry *eh;
0b8bcf0d 9913 struct got_entry **pgent, *gent;
5bd4f169 9914
e92d460e 9915 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9916 return TRUE;
5bd4f169 9917
65f38f15
AM
9918 info = (struct bfd_link_info *) inf;
9919 htab = ppc_hash_table (info);
4dfe6ac6
NC
9920 if (htab == NULL)
9921 return FALSE;
5bd4f169 9922
951fd09b
AM
9923 eh = (struct ppc_link_hash_entry *) h;
9924 /* Run through the TLS GD got entries first if we're changing them
9925 to TPREL. */
37da22e5 9926 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
951fd09b
AM
9927 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9928 if (gent->got.refcount > 0
9929 && (gent->tls_type & TLS_GD) != 0)
9930 {
9931 /* This was a GD entry that has been converted to TPREL. If
9932 there happens to be a TPREL entry we can use that one. */
9933 struct got_entry *ent;
9934 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9935 if (ent->got.refcount > 0
9936 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9937 && ent->addend == gent->addend
9938 && ent->owner == gent->owner)
951fd09b
AM
9939 {
9940 gent->got.refcount = 0;
9941 break;
9942 }
9943
9944 /* If not, then we'll be using our own TPREL entry. */
9945 if (gent->got.refcount != 0)
9946 gent->tls_type = TLS_TLS | TLS_TPREL;
9947 }
9948
7865406b
AM
9949 /* Remove any list entry that won't generate a word in the GOT before
9950 we call merge_got_entries. Otherwise we risk merging to empty
9951 entries. */
0b8bcf0d
AM
9952 pgent = &h->got.glist;
9953 while ((gent = *pgent) != NULL)
411e1bfb 9954 if (gent->got.refcount > 0)
7865406b
AM
9955 {
9956 if ((gent->tls_type & TLS_LD) != 0
9957 && !h->def_dynamic)
9958 {
9959 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9960 *pgent = gent->next;
9961 }
9962 else
9963 pgent = &gent->next;
9964 }
9965 else
9966 *pgent = gent->next;
9967
9968 if (!htab->do_multi_toc)
9969 merge_got_entries (&h->got.glist);
9970
9971 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9972 if (!gent->is_indirect)
411e1bfb 9973 {
46434633
AM
9974 /* Make sure this symbol is output as a dynamic symbol. */
9975 if (!ensure_undef_dynamic (info, h))
f0158f44 9976 return FALSE;
65f38f15 9977
0c8d6e5c 9978 if (!is_ppc64_elf (gent->owner))
927be08e 9979 abort ();
0ffa91dd 9980
927be08e 9981 allocate_got (h, info, gent);
411e1bfb 9982 }
65f38f15 9983
954b63d4
AM
9984 /* If no dynamic sections we can't have dynamic relocs, except for
9985 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9986 if (!htab->elf.dynamic_sections_created
9987 && h->type != STT_GNU_IFUNC)
9988 eh->dyn_relocs = NULL;
9989
529fe20e
AM
9990 /* Discard relocs on undefined symbols that must be local. */
9991 else if (h->root.type == bfd_link_hash_undefined
9992 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9993 eh->dyn_relocs = NULL;
9994
954b63d4
AM
9995 /* Also discard relocs on undefined weak syms with non-default
9996 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9997 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
9998 eh->dyn_relocs = NULL;
9999
8a2058b5 10000 if (eh->dyn_relocs != NULL)
65f38f15 10001 {
8a2058b5
AM
10002 struct elf_dyn_relocs *p, **pp;
10003
57e7d118
AM
10004 /* In the shared -Bsymbolic case, discard space allocated for
10005 dynamic pc-relative relocs against symbols which turn out to
10006 be defined in regular objects. For the normal shared case,
10007 discard space for relocs that have become local due to symbol
10008 visibility changes. */
10009
10010 if (bfd_link_pic (info))
65f38f15 10011 {
57e7d118
AM
10012 /* Relocs that use pc_count are those that appear on a call
10013 insn, or certain REL relocs (see must_be_dyn_reloc) that
10014 can be generated via assembly. We want calls to
10015 protected symbols to resolve directly to the function
10016 rather than going via the plt. If people want function
10017 pointer comparisons to work as expected then they should
10018 avoid writing weird assembly. */
10019 if (SYMBOL_CALLS_LOCAL (info, h))
10020 {
57e7d118
AM
10021 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
10022 {
10023 p->count -= p->pc_count;
10024 p->pc_count = 0;
10025 if (p->count == 0)
10026 *pp = p->next;
10027 else
10028 pp = &p->next;
10029 }
10030 }
65f38f15 10031
954b63d4 10032 if (eh->dyn_relocs != NULL)
5bd4f169 10033 {
46434633
AM
10034 /* Make sure this symbol is output as a dynamic symbol. */
10035 if (!ensure_undef_dynamic (info, h))
f0158f44 10036 return FALSE;
5bd4f169 10037 }
65f38f15 10038 }
529fe20e 10039 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 10040 {
8a2058b5 10041 /* For the non-pic case, discard space for relocs against
57e7d118
AM
10042 symbols which turn out to need copy relocs or are not
10043 dynamic. */
529fe20e
AM
10044 if (h->dynamic_adjusted
10045 && !h->def_regular
10046 && !ELF_COMMON_DEF_P (h))
f0158f44 10047 {
46434633
AM
10048 /* Make sure this symbol is output as a dynamic symbol. */
10049 if (!ensure_undef_dynamic (info, h))
f0158f44 10050 return FALSE;
dfbb6ac9 10051
f0158f44
AM
10052 if (h->dynindx == -1)
10053 eh->dyn_relocs = NULL;
10054 }
10055 else
8a2058b5 10056 eh->dyn_relocs = NULL;
57e7d118
AM
10057 }
10058
10059 /* Finally, allocate space. */
10060 for (p = eh->dyn_relocs; p != NULL; p = p->next)
10061 {
10062 asection *sreloc = elf_section_data (p->sec)->sreloc;
10063 if (eh->elf.type == STT_GNU_IFUNC)
10064 sreloc = htab->elf.irelplt;
10065 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 10066 }
65f38f15 10067 }
57e7d118 10068
2d7ad24e
AM
10069 /* We might need a PLT entry when the symbol
10070 a) is dynamic, or
10071 b) is an ifunc, or
10072 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10073 d) has plt16 relocs and we are linking statically. */
10074 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10075 || h->type == STT_GNU_IFUNC
10076 || (h->needs_plt && h->dynamic_adjusted)
10077 || (h->needs_plt
10078 && h->def_regular
10079 && !htab->elf.dynamic_sections_created
3e04d765 10080 && !htab->can_convert_all_inline_plt
2d7ad24e
AM
10081 && (((struct ppc_link_hash_entry *) h)->tls_mask
10082 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 10083 {
57e7d118
AM
10084 struct plt_entry *pent;
10085 bfd_boolean doneone = FALSE;
10086 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10087 if (pent->plt.refcount > 0)
10088 {
10089 if (!htab->elf.dynamic_sections_created
10090 || h->dynindx == -1)
10091 {
2d7ad24e
AM
10092 if (h->type == STT_GNU_IFUNC)
10093 {
10094 s = htab->elf.iplt;
10095 pent->plt.offset = s->size;
10096 s->size += PLT_ENTRY_SIZE (htab);
10097 s = htab->elf.irelplt;
10098 }
10099 else
10100 {
10101 s = htab->pltlocal;
10102 pent->plt.offset = s->size;
10103 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10104 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
10105 }
57e7d118
AM
10106 }
10107 else
10108 {
10109 /* If this is the first .plt entry, make room for the special
10110 first entry. */
10111 s = htab->elf.splt;
10112 if (s->size == 0)
10113 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 10114
57e7d118 10115 pent->plt.offset = s->size;
65f38f15 10116
57e7d118
AM
10117 /* Make room for this entry. */
10118 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 10119
57e7d118
AM
10120 /* Make room for the .glink code. */
10121 s = htab->glink;
10122 if (s->size == 0)
9e390558 10123 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
10124 if (htab->opd_abi)
10125 {
10126 /* We need bigger stubs past index 32767. */
9e390558 10127 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
10128 s->size += 4;
10129 s->size += 2*4;
10130 }
10131 else
10132 s->size += 4;
65f38f15 10133
57e7d118
AM
10134 /* We also need to make an entry in the .rela.plt section. */
10135 s = htab->elf.srelplt;
10136 }
2d7ad24e
AM
10137 if (s != NULL)
10138 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
10139 doneone = TRUE;
10140 }
10141 else
10142 pent->plt.offset = (bfd_vma) -1;
10143 if (!doneone)
10144 {
10145 h->plt.plist = NULL;
10146 h->needs_plt = 0;
10147 }
65f38f15 10148 }
57e7d118 10149 else
65f38f15 10150 {
57e7d118
AM
10151 h->plt.plist = NULL;
10152 h->needs_plt = 0;
65f38f15
AM
10153 }
10154
b34976b6 10155 return TRUE;
65f38f15
AM
10156}
10157
9e390558
AM
10158#define PPC_LO(v) ((v) & 0xffff)
10159#define PPC_HI(v) (((v) >> 16) & 0xffff)
10160#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10161
a345bc8d
AM
10162/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10163 to set up space for global entry stubs. These are put in glink,
10164 after the branch table. */
65f38f15 10165
b34976b6 10166static bfd_boolean
a345bc8d 10167size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 10168{
a345bc8d
AM
10169 struct bfd_link_info *info;
10170 struct ppc_link_hash_table *htab;
10171 struct plt_entry *pent;
9e390558 10172 asection *s, *plt;
65f38f15 10173
a345bc8d
AM
10174 if (h->root.type == bfd_link_hash_indirect)
10175 return TRUE;
65f38f15 10176
a345bc8d
AM
10177 if (!h->pointer_equality_needed)
10178 return TRUE;
65f38f15 10179
a345bc8d
AM
10180 if (h->def_regular)
10181 return TRUE;
65f38f15 10182
a345bc8d
AM
10183 info = inf;
10184 htab = ppc_hash_table (info);
10185 if (htab == NULL)
10186 return FALSE;
10187
9e390558
AM
10188 s = htab->global_entry;
10189 plt = htab->elf.splt;
a345bc8d
AM
10190 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10191 if (pent->plt.offset != (bfd_vma) -1
10192 && pent->addend == 0)
10193 {
afe397ea
AM
10194 /* For ELFv2, if this symbol is not defined in a regular file
10195 and we are not generating a shared library or pie, then we
10196 need to define the symbol in the executable on a call stub.
10197 This is to avoid text relocations. */
9e390558
AM
10198 bfd_vma off, stub_align, stub_off, stub_size;
10199 unsigned int align_power;
10200
10201 stub_size = 16;
10202 stub_off = s->size;
10203 if (htab->params->plt_stub_align >= 0)
10204 align_power = htab->params->plt_stub_align;
10205 else
10206 align_power = -htab->params->plt_stub_align;
10207 /* Setting section alignment is delayed until we know it is
10208 non-empty. Otherwise the .text output section will be
10209 aligned at least to plt_stub_align even when no global
10210 entry stubs are needed. */
10211 if (s->alignment_power < align_power)
10212 s->alignment_power = align_power;
10213 stub_align = (bfd_vma) 1 << align_power;
10214 if (htab->params->plt_stub_align >= 0
10215 || ((((stub_off + stub_size - 1) & -stub_align)
10216 - (stub_off & -stub_align))
10217 > ((stub_size - 1) & -stub_align)))
10218 stub_off = (stub_off + stub_align - 1) & -stub_align;
10219 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10220 off -= stub_off + s->output_offset + s->output_section->vma;
10221 /* Note that for --plt-stub-align negative we have a possible
10222 dependency between stub offset and size. Break that
10223 dependency by assuming the max stub size when calculating
10224 the stub offset. */
10225 if (PPC_HA (off) == 0)
10226 stub_size -= 4;
8a2058b5 10227 h->root.type = bfd_link_hash_defined;
afe397ea 10228 h->root.u.def.section = s;
9e390558
AM
10229 h->root.u.def.value = stub_off;
10230 s->size = stub_off + stub_size;
a345bc8d
AM
10231 break;
10232 }
10233 return TRUE;
10234}
10235
10236/* Set DF_TEXTREL if we find any dynamic relocs that apply to
10237 read-only sections. */
10238
10239static bfd_boolean
98bbb1b8 10240maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 10241{
98bbb1b8
AM
10242 asection *sec;
10243
a345bc8d
AM
10244 if (h->root.type == bfd_link_hash_indirect)
10245 return TRUE;
10246
98bbb1b8
AM
10247 sec = readonly_dynrelocs (h);
10248 if (sec != NULL)
a345bc8d 10249 {
98bbb1b8
AM
10250 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10251
10252 info->flags |= DF_TEXTREL;
10253 info->callbacks->minfo
c1c8c1ef 10254 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
63c1f59d 10255 sec->owner, h->root.root.string, sec);
a345bc8d
AM
10256
10257 /* Not an error, just cut short the traversal. */
10258 return FALSE;
65f38f15 10259 }
b34976b6 10260 return TRUE;
65f38f15
AM
10261}
10262
10263/* Set the sizes of the dynamic sections. */
10264
b34976b6 10265static bfd_boolean
ee67d69a 10266ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 10267 struct bfd_link_info *info)
65f38f15
AM
10268{
10269 struct ppc_link_hash_table *htab;
10270 bfd *dynobj;
10271 asection *s;
b34976b6 10272 bfd_boolean relocs;
65f38f15 10273 bfd *ibfd;
7865406b 10274 struct got_entry *first_tlsld;
65f38f15
AM
10275
10276 htab = ppc_hash_table (info);
4dfe6ac6
NC
10277 if (htab == NULL)
10278 return FALSE;
10279
65f38f15
AM
10280 dynobj = htab->elf.dynobj;
10281 if (dynobj == NULL)
10282 abort ();
10283
10284 if (htab->elf.dynamic_sections_created)
10285 {
10286 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10287 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10288 {
3d4d4302 10289 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10290 if (s == NULL)
10291 abort ();
eea6121a 10292 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10293 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10294 }
10295 }
10296
10297 /* Set up .got offsets for local syms, and space for local dynamic
10298 relocs. */
c72f2fb2 10299 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10300 {
411e1bfb
AM
10301 struct got_entry **lgot_ents;
10302 struct got_entry **end_lgot_ents;
e054468f
AM
10303 struct plt_entry **local_plt;
10304 struct plt_entry **end_local_plt;
f961d9dd 10305 unsigned char *lgot_masks;
65f38f15
AM
10306 bfd_size_type locsymcount;
10307 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10308
0c8d6e5c 10309 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10310 continue;
10311
10312 for (s = ibfd->sections; s != NULL; s = s->next)
10313 {
19e08130 10314 struct ppc_dyn_relocs *p;
65f38f15 10315
6edfbbad 10316 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10317 {
ec338859
AM
10318 if (!bfd_is_abs_section (p->sec)
10319 && bfd_is_abs_section (p->sec->output_section))
10320 {
10321 /* Input section has been discarded, either because
10322 it is a copy of a linkonce section or due to
10323 linker script /DISCARD/, so we'll be discarding
10324 the relocs too. */
10325 }
248866a8 10326 else if (p->count != 0)
ec338859 10327 {
19e08130
AM
10328 asection *srel = elf_section_data (p->sec)->sreloc;
10329 if (p->ifunc)
33e44f2e 10330 srel = htab->elf.irelplt;
eea6121a 10331 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10332 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10333 info->flags |= DF_TEXTREL;
ec338859 10334 }
65f38f15
AM
10335 }
10336 }
10337
411e1bfb
AM
10338 lgot_ents = elf_local_got_ents (ibfd);
10339 if (!lgot_ents)
65f38f15
AM
10340 continue;
10341
0ffa91dd 10342 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10343 locsymcount = symtab_hdr->sh_info;
411e1bfb 10344 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10345 local_plt = (struct plt_entry **) end_lgot_ents;
10346 end_local_plt = local_plt + locsymcount;
f961d9dd 10347 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10348 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10349 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10350 {
0b8bcf0d 10351 struct got_entry **pent, *ent;
411e1bfb 10352
0b8bcf0d
AM
10353 pent = lgot_ents;
10354 while ((ent = *pent) != NULL)
411e1bfb
AM
10355 if (ent->got.refcount > 0)
10356 {
e7b938ca 10357 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10358 {
927be08e 10359 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10360 *pent = ent->next;
411e1bfb
AM
10361 }
10362 else
10363 {
19e08130
AM
10364 unsigned int ent_size = 8;
10365 unsigned int rel_size = sizeof (Elf64_External_Rela);
10366
eea6121a 10367 ent->got.offset = s->size;
e7b938ca 10368 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10369 {
19e08130
AM
10370 ent_size *= 2;
10371 rel_size *= 2;
10372 }
10373 s->size += ent_size;
37da22e5 10374 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10375 {
33e44f2e 10376 htab->elf.irelplt->size += rel_size;
19e08130
AM
10377 htab->got_reli_size += rel_size;
10378 }
f15d0b54
AM
10379 else if (bfd_link_pic (info)
10380 && !((ent->tls_type & TLS_TPREL) != 0
10381 && bfd_link_executable (info)))
19e08130
AM
10382 {
10383 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10384 srel->size += rel_size;
927be08e 10385 }
0b8bcf0d 10386 pent = &ent->next;
411e1bfb
AM
10387 }
10388 }
10389 else
0b8bcf0d 10390 *pent = ent->next;
65f38f15 10391 }
e054468f 10392
2d7ad24e
AM
10393 /* Allocate space for plt calls to local syms. */
10394 lgot_masks = (unsigned char *) end_local_plt;
10395 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10396 {
10397 struct plt_entry *ent;
10398
10399 for (ent = *local_plt; ent != NULL; ent = ent->next)
10400 if (ent->plt.refcount > 0)
10401 {
2d7ad24e
AM
10402 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10403 {
10404 s = htab->elf.iplt;
10405 ent->plt.offset = s->size;
10406 s->size += PLT_ENTRY_SIZE (htab);
10407 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10408 }
3e04d765
AM
10409 else if (htab->can_convert_all_inline_plt
10410 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10411 ent->plt.offset = (bfd_vma) -1;
10412 else
10413 {
10414 s = htab->pltlocal;
10415 ent->plt.offset = s->size;
10416 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10417 if (bfd_link_pic (info))
10418 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10419 }
e054468f
AM
10420 }
10421 else
10422 ent->plt.offset = (bfd_vma) -1;
10423 }
65f38f15
AM
10424 }
10425
10426 /* Allocate global sym .plt and .got entries, and space for global
10427 sym dynamic relocs. */
4ce794b7 10428 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10429
0e1862bb 10430 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10431 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10432
7865406b 10433 first_tlsld = NULL;
c72f2fb2 10434 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10435 {
7865406b
AM
10436 struct got_entry *ent;
10437
0c8d6e5c 10438 if (!is_ppc64_elf (ibfd))
102890f0
AM
10439 continue;
10440
7865406b
AM
10441 ent = ppc64_tlsld_got (ibfd);
10442 if (ent->got.refcount > 0)
102890f0 10443 {
7865406b 10444 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10445 {
7865406b
AM
10446 ent->is_indirect = TRUE;
10447 ent->got.ent = first_tlsld;
10448 }
10449 else
10450 {
10451 if (first_tlsld == NULL)
10452 first_tlsld = ent;
10453 s = ppc64_elf_tdata (ibfd)->got;
10454 ent->got.offset = s->size;
10455 ent->owner = ibfd;
10456 s->size += 16;
0e1862bb 10457 if (bfd_link_pic (info))
7865406b
AM
10458 {
10459 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10460 srel->size += sizeof (Elf64_External_Rela);
10461 }
102890f0
AM
10462 }
10463 }
10464 else
7865406b 10465 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10466 }
10467
65f38f15
AM
10468 /* We now have determined the sizes of the various dynamic sections.
10469 Allocate memory for them. */
b34976b6 10470 relocs = FALSE;
65f38f15
AM
10471 for (s = dynobj->sections; s != NULL; s = s->next)
10472 {
10473 if ((s->flags & SEC_LINKER_CREATED) == 0)
10474 continue;
10475
4ce794b7 10476 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10477 /* These haven't been allocated yet; don't strip. */
10478 continue;
33e44f2e
AM
10479 else if (s == htab->elf.sgot
10480 || s == htab->elf.splt
10481 || s == htab->elf.iplt
2d7ad24e 10482 || s == htab->pltlocal
c456f082 10483 || s == htab->glink
9e390558 10484 || s == htab->global_entry
5474d94f
AM
10485 || s == htab->elf.sdynbss
10486 || s == htab->elf.sdynrelro)
65f38f15
AM
10487 {
10488 /* Strip this section if we don't need it; see the
10489 comment below. */
5bd4f169 10490 }
58d180e8
AM
10491 else if (s == htab->glink_eh_frame)
10492 {
10493 if (!bfd_is_abs_section (s->output_section))
10494 /* Not sized yet. */
10495 continue;
10496 }
70cc837d 10497 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10498 {
c456f082 10499 if (s->size != 0)
5bd4f169 10500 {
33e44f2e 10501 if (s != htab->elf.srelplt)
b34976b6 10502 relocs = TRUE;
5bd4f169
AM
10503
10504 /* We use the reloc_count field as a counter if we need
10505 to copy relocs into the output file. */
10506 s->reloc_count = 0;
10507 }
10508 }
65f38f15 10509 else
5bd4f169
AM
10510 {
10511 /* It's not one of our sections, so don't allocate space. */
10512 continue;
10513 }
10514
eea6121a 10515 if (s->size == 0)
5bd4f169 10516 {
c456f082
AM
10517 /* If we don't need this section, strip it from the
10518 output file. This is mostly to handle .rela.bss and
10519 .rela.plt. We must create both sections in
10520 create_dynamic_sections, because they must be created
10521 before the linker maps input sections to output
10522 sections. The linker does that before
10523 adjust_dynamic_symbol is called, and it is that
10524 function which decides whether anything needs to go
10525 into these sections. */
8423293d 10526 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10527 continue;
10528 }
10529
06bcf541
AM
10530 if (bfd_is_abs_section (s->output_section))
10531 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10532 s->name);
10533
c456f082 10534 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10535 continue;
10536
65f38f15
AM
10537 /* Allocate memory for the section contents. We use bfd_zalloc
10538 here in case unused entries are not reclaimed before the
10539 section's contents are written out. This should not happen,
411e1bfb
AM
10540 but this way if it does we get a R_PPC64_NONE reloc in .rela
10541 sections instead of garbage.
10542 We also rely on the section contents being zero when writing
5474d94f 10543 the GOT and .dynrelro. */
eea6121a 10544 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10545 if (s->contents == NULL)
b34976b6 10546 return FALSE;
5bd4f169
AM
10547 }
10548
c72f2fb2 10549 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10550 {
0c8d6e5c 10551 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10552 continue;
10553
e717da7e 10554 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10555 if (s != NULL && s != htab->elf.sgot)
e717da7e 10556 {
eea6121a 10557 if (s->size == 0)
8423293d 10558 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10559 else
10560 {
eea6121a 10561 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10562 if (s->contents == NULL)
10563 return FALSE;
10564 }
10565 }
10566 s = ppc64_elf_tdata (ibfd)->relgot;
10567 if (s != NULL)
10568 {
eea6121a 10569 if (s->size == 0)
8423293d 10570 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10571 else
10572 {
eea6121a 10573 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10574 if (s->contents == NULL)
10575 return FALSE;
10576 relocs = TRUE;
10577 s->reloc_count = 0;
10578 }
10579 }
10580 }
10581
e86ce104 10582 if (htab->elf.dynamic_sections_created)
5bd4f169 10583 {
e8910a83
AM
10584 bfd_boolean tls_opt;
10585
5bd4f169
AM
10586 /* Add some entries to the .dynamic section. We fill in the
10587 values later, in ppc64_elf_finish_dynamic_sections, but we
10588 must add the entries now so that we get the correct size for
10589 the .dynamic section. The DT_DEBUG entry is filled in by the
10590 dynamic linker and used by the debugger. */
dc810e39 10591#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10592 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10593
0e1862bb 10594 if (bfd_link_executable (info))
5bd4f169 10595 {
dc810e39 10596 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10597 return FALSE;
5bd4f169
AM
10598 }
10599
33e44f2e 10600 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10601 {
dc810e39
AM
10602 if (!add_dynamic_entry (DT_PLTGOT, 0)
10603 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10604 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10605 || !add_dynamic_entry (DT_JMPREL, 0)
10606 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10607 return FALSE;
5bd4f169
AM
10608 }
10609
ee67d69a 10610 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10611 {
10612 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10613 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10614 return FALSE;
19397422
AM
10615 }
10616
7c9cf415 10617 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10618 && htab->tls_get_addr_fd != NULL
10619 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10620 if (tls_opt || !htab->opd_abi)
10621 {
10622 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10623 return FALSE;
10624 }
a7f2871e 10625
5bd4f169
AM
10626 if (relocs)
10627 {
dc810e39
AM
10628 if (!add_dynamic_entry (DT_RELA, 0)
10629 || !add_dynamic_entry (DT_RELASZ, 0)
10630 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10631 return FALSE;
5bd4f169 10632
65f38f15
AM
10633 /* If any dynamic relocs apply to a read-only section,
10634 then we need a DT_TEXTREL entry. */
248866a8 10635 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10636 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10637
65f38f15 10638 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10639 {
65f38f15 10640 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10641 return FALSE;
5bd4f169 10642 }
5bd4f169 10643 }
5bd4f169 10644 }
65f38f15 10645#undef add_dynamic_entry
5bd4f169 10646
b34976b6 10647 return TRUE;
5bd4f169
AM
10648}
10649
a345bc8d
AM
10650/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10651
10652static bfd_boolean
10653ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10654{
10655 if (h->plt.plist != NULL
10656 && !h->def_regular
10657 && !h->pointer_equality_needed)
10658 return FALSE;
10659
10660 return _bfd_elf_hash_symbol (h);
10661}
10662
721956f4 10663/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10664
4ce794b7
AM
10665static inline enum ppc_stub_type
10666ppc_type_of_stub (asection *input_sec,
10667 const Elf_Internal_Rela *rel,
10668 struct ppc_link_hash_entry **hash,
e054468f 10669 struct plt_entry **plt_ent,
6911b7dc
AM
10670 bfd_vma destination,
10671 unsigned long local_off)
5bd4f169 10672{
721956f4
AM
10673 struct ppc_link_hash_entry *h = *hash;
10674 bfd_vma location;
10675 bfd_vma branch_offset;
10676 bfd_vma max_branch_offset;
4ce794b7 10677 enum elf_ppc64_reloc_type r_type;
5bd4f169 10678
721956f4
AM
10679 if (h != NULL)
10680 {
e054468f 10681 struct plt_entry *ent;
7fe2b9a6 10682 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10683 if (h->oh != NULL
10684 && h->oh->is_func_descriptor)
7b8f6675
AM
10685 {
10686 fdh = ppc_follow_link (h->oh);
10687 *hash = fdh;
10688 }
8387904d 10689
e054468f
AM
10690 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10691 if (ent->addend == rel->r_addend
10692 && ent->plt.offset != (bfd_vma) -1)
10693 {
e054468f
AM
10694 *plt_ent = ent;
10695 return ppc_stub_plt_call;
10696 }
5bd4f169 10697
7fe2b9a6
AM
10698 /* Here, we know we don't have a plt entry. If we don't have a
10699 either a defined function descriptor or a defined entry symbol
10700 in a regular object file, then it is pointless trying to make
10701 any other type of stub. */
854b41e7
AM
10702 if (!is_static_defined (&fdh->elf)
10703 && !is_static_defined (&h->elf))
721956f4 10704 return ppc_stub_none;
5d1634d7 10705 }
e054468f
AM
10706 else if (elf_local_got_ents (input_sec->owner) != NULL)
10707 {
10708 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10709 struct plt_entry **local_plt = (struct plt_entry **)
10710 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10711 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10712
10713 if (local_plt[r_symndx] != NULL)
10714 {
10715 struct plt_entry *ent;
10716
10717 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10718 if (ent->addend == rel->r_addend
10719 && ent->plt.offset != (bfd_vma) -1)
10720 {
10721 *plt_ent = ent;
10722 return ppc_stub_plt_call;
10723 }
10724 }
10725 }
5d1634d7 10726
721956f4
AM
10727 /* Determine where the call point is. */
10728 location = (input_sec->output_offset
10729 + input_sec->output_section->vma
10730 + rel->r_offset);
5d1634d7 10731
721956f4
AM
10732 branch_offset = destination - location;
10733 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10734
721956f4
AM
10735 /* Determine if a long branch stub is needed. */
10736 max_branch_offset = 1 << 25;
23cedd1d
AM
10737 if (r_type == R_PPC64_REL14
10738 || r_type == R_PPC64_REL14_BRTAKEN
10739 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10740 max_branch_offset = 1 << 15;
5d1634d7 10741
6911b7dc 10742 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10743 /* We need a stub. Figure out whether a long_branch or plt_branch
10744 is needed later. */
10745 return ppc_stub_long_branch;
5d1634d7 10746
721956f4 10747 return ppc_stub_none;
5d1634d7
AM
10748}
10749
794e51c0
AM
10750/* With power7 weakly ordered memory model, it is possible for ld.so
10751 to update a plt entry in one thread and have another thread see a
10752 stale zero toc entry. To avoid this we need some sort of acquire
10753 barrier in the call stub. One solution is to make the load of the
10754 toc word seem to appear to depend on the load of the function entry
10755 word. Another solution is to test for r2 being zero, and branch to
10756 the appropriate glink entry if so.
10757
10758 . fake dep barrier compare
71a39c98
AM
10759 . ld 12,xxx(2) ld 12,xxx(2)
10760 . mtctr 12 mtctr 12
10761 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10762 . add 2,2,11 cmpldi 2,0
10763 . ld 2,xxx+8(2) bnectr+
10764 . bctr b <glink_entry>
10765
10766 The solution involving the compare turns out to be faster, so
10767 that's what we use unless the branch won't reach. */
10768
10769#define ALWAYS_USE_FAKE_DEP 0
10770#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10771
794e51c0
AM
10772static inline unsigned int
10773plt_stub_size (struct ppc_link_hash_table *htab,
10774 struct ppc_stub_hash_entry *stub_entry,
10775 bfd_vma off)
10776{
b9e5796b
AM
10777 unsigned size = 12;
10778
10779 if (ALWAYS_EMIT_R2SAVE
10780 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10781 size += 4;
10782 if (PPC_HA (off) != 0)
794e51c0 10783 size += 4;
b9e5796b
AM
10784 if (htab->opd_abi)
10785 {
10786 size += 4;
e7d1c40c 10787 if (htab->params->plt_static_chain)
b9e5796b 10788 size += 4;
bd4d2eaa
AM
10789 if (htab->params->plt_thread_safe
10790 && htab->elf.dynamic_sections_created
10791 && stub_entry->h != NULL
10792 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10793 size += 8;
e7d1c40c 10794 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10795 size += 4;
10796 }
794e51c0
AM
10797 if (stub_entry->h != NULL
10798 && (stub_entry->h == htab->tls_get_addr_fd
10799 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10800 && htab->params->tls_get_addr_opt)
f378ab09
AM
10801 {
10802 size += 7 * 4;
10803 if (ALWAYS_EMIT_R2SAVE
10804 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 10805 size += 6 * 4;
f378ab09 10806 }
794e51c0
AM
10807 return size;
10808}
10809
2420fff6
AM
10810/* Depending on the sign of plt_stub_align:
10811 If positive, return the padding to align to a 2**plt_stub_align
10812 boundary.
10813 If negative, if this stub would cross fewer 2**plt_stub_align
10814 boundaries if we align, then return the padding needed to do so. */
10815
794e51c0
AM
10816static inline unsigned int
10817plt_stub_pad (struct ppc_link_hash_table *htab,
10818 struct ppc_stub_hash_entry *stub_entry,
10819 bfd_vma plt_off)
10820{
2420fff6 10821 int stub_align;
794e51c0 10822 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10823 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10824
2420fff6
AM
10825 if (htab->params->plt_stub_align >= 0)
10826 {
10827 stub_align = 1 << htab->params->plt_stub_align;
10828 if ((stub_off & (stub_align - 1)) != 0)
10829 return stub_align - (stub_off & (stub_align - 1));
10830 return 0;
10831 }
10832
10833 stub_align = 1 << -htab->params->plt_stub_align;
794e51c0 10834 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10835 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10836 return stub_align - (stub_off & (stub_align - 1));
10837 return 0;
10838}
10839
10840/* Build a .plt call stub. */
10841
10842static inline bfd_byte *
10843build_plt_stub (struct ppc_link_hash_table *htab,
10844 struct ppc_stub_hash_entry *stub_entry,
10845 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10846{
e7d1c40c 10847 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10848 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10849 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10850 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10851 && htab->elf.dynamic_sections_created
10852 && stub_entry->h != NULL
10853 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10854 bfd_boolean use_fake_dep = plt_thread_safe;
10855 bfd_vma cmp_branch_off = 0;
10856
10857 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10858 && plt_load_toc
794e51c0 10859 && plt_thread_safe
bd4d2eaa
AM
10860 && !((stub_entry->h == htab->tls_get_addr_fd
10861 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10862 && htab->params->tls_get_addr_opt))
794e51c0
AM
10863 {
10864 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10865 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10866 / PLT_ENTRY_SIZE (htab));
9e390558 10867 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10868 bfd_vma to, from;
10869
68d62958
AM
10870 if (pltindex > 32768)
10871 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10872 to = (glinkoff
10873 + htab->glink->output_offset
10874 + htab->glink->output_section->vma);
6f20ed8a 10875 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10876 + 4 * (ALWAYS_EMIT_R2SAVE
10877 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10878 + 4 * (PPC_HA (offset) != 0)
10879 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10880 != PPC_HA (offset))
10881 + 4 * (plt_static_chain != 0)
10882 + 20
6f20ed8a
AM
10883 + stub_entry->group->stub_sec->output_offset
10884 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10885 cmp_branch_off = to - from;
10886 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10887 }
10888
ac2df442
AM
10889 if (PPC_HA (offset) != 0)
10890 {
176a0d42
AM
10891 if (r != NULL)
10892 {
794e51c0
AM
10893 if (ALWAYS_EMIT_R2SAVE
10894 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10895 r[0].r_offset += 4;
176a0d42 10896 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10897 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10898 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10899 r[1].r_addend = r[0].r_addend;
b9e5796b 10900 if (plt_load_toc)
176a0d42 10901 {
b9e5796b 10902 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10903 {
b9e5796b
AM
10904 r[2].r_offset = r[1].r_offset + 4;
10905 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10906 r[2].r_addend = r[0].r_addend;
10907 }
10908 else
10909 {
10910 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10911 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10912 r[2].r_addend = r[0].r_addend + 8;
10913 if (plt_static_chain)
10914 {
10915 r[3].r_offset = r[2].r_offset + 4;
10916 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10917 r[3].r_addend = r[0].r_addend + 16;
10918 }
c7131b65 10919 }
176a0d42
AM
10920 }
10921 }
794e51c0
AM
10922 if (ALWAYS_EMIT_R2SAVE
10923 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10924 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10925 if (plt_load_toc)
10926 {
10927 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10928 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10929 }
10930 else
10931 {
10932 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10933 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10934 }
b9e5796b
AM
10935 if (plt_load_toc
10936 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10937 {
71a39c98 10938 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10939 offset = 0;
10940 }
71a39c98 10941 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10942 if (plt_load_toc)
794e51c0 10943 {
b9e5796b
AM
10944 if (use_fake_dep)
10945 {
10946 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10947 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10948 }
10949 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10950 if (plt_static_chain)
10951 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10952 }
ac2df442
AM
10953 }
10954 else
10955 {
176a0d42
AM
10956 if (r != NULL)
10957 {
794e51c0
AM
10958 if (ALWAYS_EMIT_R2SAVE
10959 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10960 r[0].r_offset += 4;
176a0d42 10961 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10962 if (plt_load_toc)
176a0d42 10963 {
b9e5796b 10964 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10965 {
b9e5796b
AM
10966 r[1].r_offset = r[0].r_offset + 4;
10967 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10968 r[1].r_addend = r[0].r_addend;
10969 }
10970 else
10971 {
10972 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10973 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10974 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10975 if (plt_static_chain)
10976 {
10977 r[2].r_offset = r[1].r_offset + 4;
10978 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10979 r[2].r_addend = r[0].r_addend + 8;
10980 }
c7131b65 10981 }
176a0d42
AM
10982 }
10983 }
794e51c0
AM
10984 if (ALWAYS_EMIT_R2SAVE
10985 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10986 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10987 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10988 if (plt_load_toc
10989 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10990 {
10991 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10992 offset = 0;
10993 }
71a39c98 10994 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10995 if (plt_load_toc)
794e51c0 10996 {
b9e5796b
AM
10997 if (use_fake_dep)
10998 {
10999 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11000 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11001 }
11002 if (plt_static_chain)
11003 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11004 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11005 }
ac2df442 11006 }
b9e5796b 11007 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11008 {
11009 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11010 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11011 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11012 }
11013 else
407aa07c 11014 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11015 return p;
11016}
11017
a7f2871e
AM
11018/* Build a special .plt call stub for __tls_get_addr. */
11019
11020#define LD_R11_0R3 0xe9630000
11021#define LD_R12_0R3 0xe9830000
11022#define MR_R0_R3 0x7c601b78
11023#define CMPDI_R11_0 0x2c2b0000
11024#define ADD_R3_R12_R13 0x7c6c6a14
11025#define BEQLR 0x4d820020
11026#define MR_R3_R0 0x7c030378
a7f2871e
AM
11027#define STD_R11_0R1 0xf9610000
11028#define BCTRL 0x4e800421
11029#define LD_R11_0R1 0xe9610000
a7f2871e
AM
11030#define MTLR_R11 0x7d6803a6
11031
11032static inline bfd_byte *
794e51c0
AM
11033build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11034 struct ppc_stub_hash_entry *stub_entry,
11035 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 11036{
e7d1c40c 11037 bfd *obfd = htab->params->stub_bfd;
794e51c0 11038
a7f2871e
AM
11039 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
11040 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11041 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11042 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
11043 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11044 bfd_put_32 (obfd, BEQLR, p), p += 4;
11045 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
11046 if (r != NULL)
11047 r[0].r_offset += 7 * 4;
11048 if (!ALWAYS_EMIT_R2SAVE
11049 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
11050 return build_plt_stub (htab, stub_entry, p, offset, r);
11051
a7f2871e 11052 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 11053 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
11054
11055 if (r != NULL)
f378ab09 11056 r[0].r_offset += 2 * 4;
794e51c0 11057 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 11058 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 11059
a078d95a 11060 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 11061 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
11062 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
11063 bfd_put_32 (obfd, BLR, p), p += 4;
11064
11065 return p;
11066}
11067
176a0d42
AM
11068static Elf_Internal_Rela *
11069get_relocs (asection *sec, int count)
11070{
11071 Elf_Internal_Rela *relocs;
11072 struct bfd_elf_section_data *elfsec_data;
11073
11074 elfsec_data = elf_section_data (sec);
11075 relocs = elfsec_data->relocs;
11076 if (relocs == NULL)
11077 {
11078 bfd_size_type relsize;
11079 relsize = sec->reloc_count * sizeof (*relocs);
11080 relocs = bfd_alloc (sec->owner, relsize);
11081 if (relocs == NULL)
11082 return NULL;
11083 elfsec_data->relocs = relocs;
d4730f92
BS
11084 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11085 sizeof (Elf_Internal_Shdr));
11086 if (elfsec_data->rela.hdr == NULL)
11087 return NULL;
11088 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11089 * sizeof (Elf64_External_Rela));
11090 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11091 sec->reloc_count = 0;
11092 }
11093 relocs += sec->reloc_count;
11094 sec->reloc_count += count;
11095 return relocs;
11096}
11097
aa374f67 11098static bfd_vma
25f53a85 11099get_r2off (struct bfd_link_info *info,
aa374f67
AM
11100 struct ppc_stub_hash_entry *stub_entry)
11101{
25f53a85 11102 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11103 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11104
11105 if (r2off == 0)
11106 {
11107 /* Support linking -R objects. Get the toc pointer from the
11108 opd entry. */
11109 char buf[8];
b9e5796b
AM
11110 if (!htab->opd_abi)
11111 return r2off;
aa374f67
AM
11112 asection *opd = stub_entry->h->elf.root.u.def.section;
11113 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11114
11115 if (strcmp (opd->name, ".opd") != 0
11116 || opd->reloc_count != 0)
11117 {
c1c8c1ef 11118 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
25f53a85 11119 stub_entry->h->elf.root.root.string);
aa374f67 11120 bfd_set_error (bfd_error_bad_value);
a7c49797 11121 return (bfd_vma) -1;
aa374f67
AM
11122 }
11123 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11124 return (bfd_vma) -1;
aa374f67 11125 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11126 r2off -= elf_gp (info->output_bfd);
aa374f67 11127 }
6f20ed8a 11128 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11129 return r2off;
11130}
11131
b34976b6 11132static bfd_boolean
4ce794b7 11133ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11134{
721956f4
AM
11135 struct ppc_stub_hash_entry *stub_entry;
11136 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11137 struct bfd_link_info *info;
11138 struct ppc_link_hash_table *htab;
721956f4
AM
11139 bfd_byte *loc;
11140 bfd_byte *p;
ee75fd95 11141 bfd_vma dest, off;
176a0d42 11142 Elf_Internal_Rela *r;
e054468f 11143 asection *plt;
5d1634d7 11144
721956f4
AM
11145 /* Massage our args to the form they really have. */
11146 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11147 info = in_arg;
5d1634d7 11148
5d1634d7 11149 htab = ppc_hash_table (info);
4dfe6ac6
NC
11150 if (htab == NULL)
11151 return FALSE;
5d1634d7 11152
721956f4 11153 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
11154 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11155 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11156
4ce794b7 11157 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11158 switch (stub_entry->stub_type)
5d1634d7 11159 {
721956f4 11160 case ppc_stub_long_branch:
ad8e1ba5 11161 case ppc_stub_long_branch_r2off:
721956f4 11162 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
11163 dest = (stub_entry->target_value
11164 + stub_entry->target_section->output_offset
11165 + stub_entry->target_section->output_section->vma);
11166 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11167 off = dest;
5d1634d7 11168
721956f4
AM
11169 /* And this is where we are coming from. */
11170 off -= (stub_entry->stub_offset
6f20ed8a
AM
11171 + stub_entry->group->stub_sec->output_offset
11172 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 11173
9e390558 11174 p = loc;
ac2df442 11175 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11176 {
25f53a85 11177 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11178
a7c49797 11179 if (r2off == (bfd_vma) -1)
aa374f67
AM
11180 {
11181 htab->stub_error = TRUE;
11182 return FALSE;
11183 }
9e390558
AM
11184 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11185 p += 4;
ac2df442
AM
11186 if (PPC_HA (r2off) != 0)
11187 {
e7d1c40c 11188 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11189 ADDIS_R2_R2 | PPC_HA (r2off), p);
11190 p += 4;
a7c49797
AM
11191 }
11192 if (PPC_LO (r2off) != 0)
11193 {
11194 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11195 ADDI_R2_R2 | PPC_LO (r2off), p);
11196 p += 4;
ac2df442 11197 }
9e390558 11198 off -= p - loc;
ad8e1ba5 11199 }
9e390558
AM
11200 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11201 p += 4;
ad8e1ba5 11202
5c3dead3
AM
11203 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11204 {
cf97bcb0
AM
11205 _bfd_error_handler
11206 (_("long branch stub `%s' offset overflow"),
bc30df16 11207 stub_entry->root.string);
5c3dead3
AM
11208 htab->stub_error = TRUE;
11209 return FALSE;
11210 }
ee75fd95
AM
11211
11212 if (info->emitrelocations)
11213 {
6f20ed8a 11214 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11215 if (r == NULL)
11216 return FALSE;
9e390558 11217 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95
AM
11218 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11219 r->r_addend = dest;
11220 if (stub_entry->h != NULL)
11221 {
11222 struct elf_link_hash_entry **hashes;
11223 unsigned long symndx;
11224 struct ppc_link_hash_entry *h;
11225
e7d1c40c 11226 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
11227 if (hashes == NULL)
11228 {
11229 bfd_size_type hsize;
11230
11231 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 11232 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
11233 if (hashes == NULL)
11234 return FALSE;
e7d1c40c 11235 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
11236 htab->stub_globals = 1;
11237 }
11238 symndx = htab->stub_globals++;
11239 h = stub_entry->h;
11240 hashes[symndx] = &h->elf;
11241 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11242 if (h->oh != NULL && h->oh->is_func)
b31867b6 11243 h = ppc_follow_link (h->oh);
ee75fd95
AM
11244 if (h->elf.root.u.def.section != stub_entry->target_section)
11245 /* H is an opd symbol. The addend must be zero. */
11246 r->r_addend = 0;
11247 else
11248 {
11249 off = (h->elf.root.u.def.value
11250 + h->elf.root.u.def.section->output_offset
11251 + h->elf.root.u.def.section->output_section->vma);
11252 r->r_addend -= off;
11253 }
11254 }
11255 }
721956f4 11256 break;
e86ce104 11257
721956f4 11258 case ppc_stub_plt_branch:
ad8e1ba5 11259 case ppc_stub_plt_branch_r2off:
721956f4
AM
11260 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11261 stub_entry->root.string + 9,
b34976b6 11262 FALSE, FALSE);
721956f4
AM
11263 if (br_entry == NULL)
11264 {
cf97bcb0
AM
11265 _bfd_error_handler (_("can't find branch stub `%s'"),
11266 stub_entry->root.string);
b34976b6
AM
11267 htab->stub_error = TRUE;
11268 return FALSE;
721956f4
AM
11269 }
11270
176a0d42
AM
11271 dest = (stub_entry->target_value
11272 + stub_entry->target_section->output_offset
11273 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
11274 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11275 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11276
176a0d42 11277 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 11278 htab->brlt->contents + br_entry->offset);
721956f4 11279
f94498ff 11280 if (br_entry->iter == htab->stub_iteration)
721956f4 11281 {
f94498ff 11282 br_entry->iter = 0;
84f5d08e 11283
f94498ff 11284 if (htab->relbrlt != NULL)
84f5d08e 11285 {
f94498ff
AM
11286 /* Create a reloc for the branch lookup table entry. */
11287 Elf_Internal_Rela rela;
11288 bfd_byte *rl;
11289
11290 rela.r_offset = (br_entry->offset
11291 + htab->brlt->output_offset
11292 + htab->brlt->output_section->vma);
11293 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 11294 rela.r_addend = dest;
f94498ff
AM
11295
11296 rl = htab->relbrlt->contents;
11297 rl += (htab->relbrlt->reloc_count++
11298 * sizeof (Elf64_External_Rela));
11299 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11300 }
11301 else if (info->emitrelocations)
11302 {
176a0d42
AM
11303 r = get_relocs (htab->brlt, 1);
11304 if (r == NULL)
11305 return FALSE;
11306 /* brlt, being SEC_LINKER_CREATED does not go through the
11307 normal reloc processing. Symbols and offsets are not
11308 translated from input file to output file form, so
11309 set up the offset per the output file. */
f94498ff
AM
11310 r->r_offset = (br_entry->offset
11311 + htab->brlt->output_offset
11312 + htab->brlt->output_section->vma);
11313 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 11314 r->r_addend = dest;
84f5d08e 11315 }
84f5d08e 11316 }
721956f4 11317
176a0d42
AM
11318 dest = (br_entry->offset
11319 + htab->brlt->output_offset
11320 + htab->brlt->output_section->vma);
11321
11322 off = (dest
06bcf541 11323 - elf_gp (info->output_bfd)
6f20ed8a 11324 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11325
ad8e1ba5 11326 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11327 {
25f53a85 11328 info->callbacks->einfo
c1c8c1ef 11329 (_("%P: linkage table error against `%pT'\n"),
721956f4 11330 stub_entry->root.string);
5d1634d7 11331 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11332 htab->stub_error = TRUE;
11333 return FALSE;
5d1634d7 11334 }
41bd81ab 11335
176a0d42
AM
11336 if (info->emitrelocations)
11337 {
6f20ed8a 11338 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11339 if (r == NULL)
11340 return FALSE;
6f20ed8a 11341 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11342 if (bfd_big_endian (info->output_bfd))
11343 r[0].r_offset += 2;
00f412ee 11344 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11345 r[0].r_offset += 4;
11346 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11347 r[0].r_addend = dest;
11348 if (PPC_HA (off) != 0)
11349 {
11350 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11351 r[1].r_offset = r[0].r_offset + 4;
11352 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11353 r[1].r_addend = r[0].r_addend;
11354 }
11355 }
11356
9e390558 11357 p = loc;
00f412ee 11358 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11359 {
176a0d42 11360 if (PPC_HA (off) != 0)
ac2df442 11361 {
e7d1c40c 11362 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11363 ADDIS_R12_R2 | PPC_HA (off), p);
11364 p += 4;
e7d1c40c 11365 bfd_put_32 (htab->params->stub_bfd,
9e390558 11366 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11367 }
11368 else
9e390558
AM
11369 bfd_put_32 (htab->params->stub_bfd,
11370 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11371 }
11372 else
11373 {
25f53a85 11374 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11375
a7c49797 11376 if (r2off == (bfd_vma) -1)
aa374f67
AM
11377 {
11378 htab->stub_error = TRUE;
11379 return FALSE;
11380 }
ad8e1ba5 11381
9e390558
AM
11382 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11383 p += 4;
176a0d42 11384 if (PPC_HA (off) != 0)
ac2df442 11385 {
e7d1c40c 11386 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11387 ADDIS_R12_R2 | PPC_HA (off), p);
11388 p += 4;
e7d1c40c 11389 bfd_put_32 (htab->params->stub_bfd,
9e390558 11390 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11391 }
11392 else
9e390558 11393 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11394
11395 if (PPC_HA (r2off) != 0)
11396 {
9e390558 11397 p += 4;
e7d1c40c 11398 bfd_put_32 (htab->params->stub_bfd,
9e390558 11399 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11400 }
11401 if (PPC_LO (r2off) != 0)
11402 {
9e390558 11403 p += 4;
e7d1c40c 11404 bfd_put_32 (htab->params->stub_bfd,
9e390558 11405 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11406 }
ad8e1ba5 11407 }
9e390558
AM
11408 p += 4;
11409 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11410 p += 4;
407aa07c
AM
11411 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11412 p += 4;
721956f4 11413 break;
5d1634d7 11414
721956f4 11415 case ppc_stub_plt_call:
794e51c0 11416 case ppc_stub_plt_call_r2save:
e054468f 11417 if (stub_entry->h != NULL
b31867b6
AM
11418 && stub_entry->h->is_func_descriptor
11419 && stub_entry->h->oh != NULL)
c862ae31 11420 {
b31867b6
AM
11421 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11422
11423 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11424 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11425 if (fh->elf.root.type == bfd_link_hash_undefined
11426 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11427 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11428 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11429 }
11430
721956f4 11431 /* Now build the stub. */
e054468f 11432 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 11433 if (dest >= (bfd_vma) -2)
721956f4
AM
11434 abort ();
11435
33e44f2e 11436 plt = htab->elf.splt;
25f23106
AM
11437 if (!htab->elf.dynamic_sections_created
11438 || stub_entry->h == NULL
11439 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11440 {
11441 if (stub_entry->symtype == STT_GNU_IFUNC)
11442 plt = htab->elf.iplt;
11443 else
11444 plt = htab->pltlocal;
11445 }
e054468f
AM
11446
11447 dest += plt->output_offset + plt->output_section->vma;
11448
176a0d42 11449 off = (dest
06bcf541 11450 - elf_gp (info->output_bfd)
6f20ed8a 11451 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11452
ad8e1ba5 11453 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11454 {
25f53a85 11455 info->callbacks->einfo
695344c0 11456 /* xgettext:c-format */
c1c8c1ef 11457 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11458 stub_entry->h != NULL
11459 ? stub_entry->h->elf.root.root.string
11460 : "<local sym>");
721956f4 11461 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11462 htab->stub_error = TRUE;
11463 return FALSE;
721956f4
AM
11464 }
11465
e7d1c40c 11466 if (htab->params->plt_stub_align != 0)
794e51c0
AM
11467 {
11468 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11469
6f20ed8a
AM
11470 stub_entry->group->stub_sec->size += pad;
11471 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
11472 loc += pad;
11473 }
11474
176a0d42
AM
11475 r = NULL;
11476 if (info->emitrelocations)
11477 {
6f20ed8a 11478 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11479 ((PPC_HA (off) != 0)
11480 + (htab->opd_abi
e7d1c40c 11481 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11482 && PPC_HA (off + 16) == PPC_HA (off))
11483 : 1)));
176a0d42
AM
11484 if (r == NULL)
11485 return FALSE;
6f20ed8a 11486 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11487 if (bfd_big_endian (info->output_bfd))
11488 r[0].r_offset += 2;
176a0d42
AM
11489 r[0].r_addend = dest;
11490 }
a7f2871e
AM
11491 if (stub_entry->h != NULL
11492 && (stub_entry->h == htab->tls_get_addr_fd
11493 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11494 && htab->params->tls_get_addr_opt)
794e51c0 11495 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11496 else
794e51c0 11497 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11498 break;
11499
a4b6fadd
AM
11500 case ppc_stub_save_res:
11501 return TRUE;
11502
721956f4
AM
11503 default:
11504 BFD_FAIL ();
b34976b6 11505 return FALSE;
721956f4
AM
11506 }
11507
9e390558 11508 stub_entry->group->stub_sec->size += p - loc;
97b639ba 11509
e7d1c40c 11510 if (htab->params->emit_stub_syms)
97b639ba
AM
11511 {
11512 struct elf_link_hash_entry *h;
ee75fd95
AM
11513 size_t len1, len2;
11514 char *name;
11515 const char *const stub_str[] = { "long_branch",
11516 "long_branch_r2off",
11517 "plt_branch",
11518 "plt_branch_r2off",
794e51c0 11519 "plt_call",
ee75fd95
AM
11520 "plt_call" };
11521
11522 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11523 len2 = strlen (stub_entry->root.string);
11524 name = bfd_malloc (len1 + len2 + 2);
11525 if (name == NULL)
11526 return FALSE;
11527 memcpy (name, stub_entry->root.string, 9);
11528 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11529 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11530 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11531 if (h == NULL)
11532 return FALSE;
11533 if (h->root.type == bfd_link_hash_new)
11534 {
11535 h->root.type = bfd_link_hash_defined;
6f20ed8a 11536 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11537 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11538 h->ref_regular = 1;
11539 h->def_regular = 1;
11540 h->ref_regular_nonweak = 1;
11541 h->forced_local = 1;
11542 h->non_elf = 0;
2ec55de3 11543 h->root.linker_def = 1;
97b639ba
AM
11544 }
11545 }
11546
b34976b6 11547 return TRUE;
721956f4
AM
11548}
11549
11550/* As above, but don't actually build the stub. Just bump offset so
11551 we know stub section sizes, and select plt_branch stubs where
11552 long_branch stubs won't do. */
11553
b34976b6 11554static bfd_boolean
4ce794b7 11555ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11556{
11557 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11558 struct bfd_link_info *info;
721956f4
AM
11559 struct ppc_link_hash_table *htab;
11560 bfd_vma off;
11561 int size;
11562
11563 /* Massage our args to the form they really have. */
11564 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11565 info = in_arg;
11566
11567 htab = ppc_hash_table (info);
4dfe6ac6
NC
11568 if (htab == NULL)
11569 return FALSE;
721956f4 11570
a4b6fadd
AM
11571 if (stub_entry->h != NULL
11572 && stub_entry->h->save_res
11573 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11574 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11575 {
11576 /* Don't make stubs to out-of-line register save/restore
11577 functions. Instead, emit copies of the functions. */
11578 stub_entry->group->needs_save_res = 1;
11579 stub_entry->stub_type = ppc_stub_save_res;
11580 return TRUE;
11581 }
11582
794e51c0
AM
11583 if (stub_entry->stub_type == ppc_stub_plt_call
11584 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11585 {
e054468f
AM
11586 asection *plt;
11587 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11588 if (off >= (bfd_vma) -2)
411e1bfb 11589 abort ();
33e44f2e 11590 plt = htab->elf.splt;
25f23106
AM
11591 if (!htab->elf.dynamic_sections_created
11592 || stub_entry->h == NULL
11593 || stub_entry->h->elf.dynindx == -1)
2d7ad24e
AM
11594 {
11595 if (stub_entry->symtype == STT_GNU_IFUNC)
11596 plt = htab->elf.iplt;
11597 else
11598 plt = htab->pltlocal;
11599 }
e054468f
AM
11600 off += (plt->output_offset
11601 + plt->output_section->vma
06bcf541 11602 - elf_gp (info->output_bfd)
6f20ed8a 11603 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11604
794e51c0 11605 size = plt_stub_size (htab, stub_entry, off);
d4aaa2a0
AM
11606 if (stub_entry->h != NULL
11607 && (stub_entry->h == htab->tls_get_addr_fd
11608 || stub_entry->h == htab->tls_get_addr)
11609 && htab->params->tls_get_addr_opt
11610 && (ALWAYS_EMIT_R2SAVE
11611 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11612 stub_entry->group->tls_get_addr_opt_bctrl
11613 = stub_entry->group->stub_sec->size + size - 5 * 4;
11614
e7d1c40c 11615 if (htab->params->plt_stub_align)
794e51c0 11616 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11617 if (info->emitrelocations)
11618 {
6f20ed8a 11619 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11620 += ((PPC_HA (off) != 0)
11621 + (htab->opd_abi
e7d1c40c 11622 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11623 && PPC_HA (off + 16) == PPC_HA (off))
11624 : 1));
6f20ed8a 11625 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11626 }
721956f4
AM
11627 }
11628 else
11629 {
ad8e1ba5
AM
11630 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11631 variants. */
ac2df442 11632 bfd_vma r2off = 0;
6911b7dc 11633 bfd_vma local_off = 0;
ac2df442 11634
721956f4
AM
11635 off = (stub_entry->target_value
11636 + stub_entry->target_section->output_offset
11637 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11638 off -= (stub_entry->group->stub_sec->size
11639 + stub_entry->group->stub_sec->output_offset
11640 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11641
ad8e1ba5
AM
11642 /* Reset the stub type from the plt variant in case we now
11643 can reach with a shorter stub. */
11644 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11645 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11646
11647 size = 4;
11648 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11649 {
25f53a85 11650 r2off = get_r2off (info, stub_entry);
a7c49797 11651 if (r2off == (bfd_vma) -1)
aa374f67
AM
11652 {
11653 htab->stub_error = TRUE;
11654 return FALSE;
11655 }
a7c49797 11656 size = 8;
ac2df442 11657 if (PPC_HA (r2off) != 0)
a7c49797
AM
11658 size += 4;
11659 if (PPC_LO (r2off) != 0)
11660 size += 4;
ac2df442 11661 off -= size - 4;
ad8e1ba5
AM
11662 }
11663
6911b7dc
AM
11664 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11665
6a3858a6 11666 /* If the branch offset is too big, use a ppc_stub_plt_branch.
b9e5796b
AM
11667 Do the same for -R objects without function descriptors. */
11668 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11669 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11670 && r2off == 0
11671 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11672 {
11673 struct ppc_branch_hash_entry *br_entry;
11674
11675 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11676 stub_entry->root.string + 9,
b34976b6 11677 TRUE, FALSE);
721956f4
AM
11678 if (br_entry == NULL)
11679 {
cf97bcb0
AM
11680 _bfd_error_handler (_("can't build branch stub `%s'"),
11681 stub_entry->root.string);
b34976b6
AM
11682 htab->stub_error = TRUE;
11683 return FALSE;
721956f4
AM
11684 }
11685
11686 if (br_entry->iter != htab->stub_iteration)
11687 {
11688 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11689 br_entry->offset = htab->brlt->size;
11690 htab->brlt->size += 8;
63bc6f6c 11691
ee75fd95 11692 if (htab->relbrlt != NULL)
eea6121a 11693 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11694 else if (info->emitrelocations)
11695 {
11696 htab->brlt->reloc_count += 1;
11697 htab->brlt->flags |= SEC_RELOC;
11698 }
721956f4 11699 }
ad8e1ba5
AM
11700
11701 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11702 off = (br_entry->offset
11703 + htab->brlt->output_offset
11704 + htab->brlt->output_section->vma
06bcf541 11705 - elf_gp (info->output_bfd)
6f20ed8a 11706 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11707
176a0d42
AM
11708 if (info->emitrelocations)
11709 {
6f20ed8a
AM
11710 stub_entry->group->stub_sec->reloc_count
11711 += 1 + (PPC_HA (off) != 0);
11712 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11713 }
11714
00f412ee 11715 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11716 {
11717 size = 12;
176a0d42 11718 if (PPC_HA (off) != 0)
ac2df442
AM
11719 size = 16;
11720 }
11721 else
11722 {
00f412ee 11723 size = 16;
176a0d42 11724 if (PPC_HA (off) != 0)
ac2df442
AM
11725 size += 4;
11726
11727 if (PPC_HA (r2off) != 0)
11728 size += 4;
00f412ee
AM
11729 if (PPC_LO (r2off) != 0)
11730 size += 4;
ac2df442 11731 }
721956f4 11732 }
84f5d08e
AM
11733 else if (info->emitrelocations)
11734 {
6f20ed8a
AM
11735 stub_entry->group->stub_sec->reloc_count += 1;
11736 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11737 }
721956f4
AM
11738 }
11739
6f20ed8a 11740 stub_entry->group->stub_sec->size += size;
b34976b6 11741 return TRUE;
721956f4
AM
11742}
11743
11744/* Set up various things so that we can make a list of input sections
11745 for each output section included in the link. Returns -1 on error,
cedb70c5 11746 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11747
11748int
e7d1c40c 11749ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11750{
6f20ed8a 11751 unsigned int id;
721956f4
AM
11752 bfd_size_type amt;
11753 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11754
4dfe6ac6
NC
11755 if (htab == NULL)
11756 return -1;
4c52953f 11757
7cf7fcc8 11758 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
11759 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11760 htab->sec_info = bfd_zmalloc (amt);
11761 if (htab->sec_info == NULL)
721956f4
AM
11762 return -1;
11763
3d6f9012
AM
11764 /* Set toc_off for com, und, abs and ind sections. */
11765 for (id = 0; id < 3; id++)
6f20ed8a 11766 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11767
721956f4
AM
11768 return 1;
11769}
11770
927be08e
AM
11771/* Set up for first pass at multitoc partitioning. */
11772
11773void
11774ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11775{
11776 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11777
1c865ab2 11778 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11779 htab->toc_bfd = NULL;
11780 htab->toc_first_sec = NULL;
11781}
11782
e717da7e
AM
11783/* The linker repeatedly calls this function for each TOC input section
11784 and linker generated GOT section. Group input bfds such that the toc
927be08e 11785 within a group is less than 64k in size. */
ad8e1ba5 11786
927be08e 11787bfd_boolean
4ce794b7 11788ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11789{
11790 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11791 bfd_vma addr, off, limit;
ad8e1ba5 11792
4dfe6ac6
NC
11793 if (htab == NULL)
11794 return FALSE;
11795
927be08e 11796 if (!htab->second_toc_pass)
4c52953f 11797 {
927be08e 11798 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11799 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11800
11801 if (new_bfd)
bf102f86
AM
11802 {
11803 htab->toc_bfd = isec->owner;
11804 htab->toc_first_sec = isec;
11805 }
927be08e 11806
bf102f86
AM
11807 addr = isec->output_offset + isec->output_section->vma;
11808 off = addr - htab->toc_curr;
d77c8a4b
AM
11809 limit = 0x80008000;
11810 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11811 limit = 0x10000;
11812 if (off + isec->size > limit)
bf102f86
AM
11813 {
11814 addr = (htab->toc_first_sec->output_offset
11815 + htab->toc_first_sec->output_section->vma);
11816 htab->toc_curr = addr;
a27e685f 11817 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11818 }
99877b66 11819
927be08e
AM
11820 /* toc_curr is the base address of this toc group. Set elf_gp
11821 for the input section to be the offset relative to the
11822 output toc base plus 0x8000. Making the input elf_gp an
11823 offset allows us to move the toc as a whole without
11824 recalculating input elf_gp. */
06bcf541 11825 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11826 off += TOC_BASE_OFF;
11827
11828 /* Die if someone uses a linker script that doesn't keep input
11829 file .toc and .got together. */
a4fd3de5
AM
11830 if (new_bfd
11831 && elf_gp (isec->owner) != 0
927be08e
AM
11832 && elf_gp (isec->owner) != off)
11833 return FALSE;
11834
11835 elf_gp (isec->owner) = off;
11836 return TRUE;
4c52953f 11837 }
927be08e
AM
11838
11839 /* During the second pass toc_first_sec points to the start of
11840 a toc group, and toc_curr is used to track the old elf_gp.
11841 We use toc_bfd to ensure we only look at each bfd once. */
11842 if (htab->toc_bfd == isec->owner)
11843 return TRUE;
11844 htab->toc_bfd = isec->owner;
11845
11846 if (htab->toc_first_sec == NULL
11847 || htab->toc_curr != elf_gp (isec->owner))
11848 {
11849 htab->toc_curr = elf_gp (isec->owner);
11850 htab->toc_first_sec = isec;
11851 }
11852 addr = (htab->toc_first_sec->output_offset
11853 + htab->toc_first_sec->output_section->vma);
06bcf541 11854 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
11855 elf_gp (isec->owner) = off;
11856
11857 return TRUE;
ad8e1ba5
AM
11858}
11859
927be08e
AM
11860/* Called via elf_link_hash_traverse to merge GOT entries for global
11861 symbol H. */
11862
11863static bfd_boolean
11864merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11865{
11866 if (h->root.type == bfd_link_hash_indirect)
11867 return TRUE;
11868
927be08e
AM
11869 merge_got_entries (&h->got.glist);
11870
11871 return TRUE;
11872}
11873
11874/* Called via elf_link_hash_traverse to allocate GOT entries for global
11875 symbol H. */
11876
11877static bfd_boolean
11878reallocate_got (struct elf_link_hash_entry *h, void *inf)
11879{
11880 struct got_entry *gent;
11881
11882 if (h->root.type == bfd_link_hash_indirect)
11883 return TRUE;
11884
927be08e
AM
11885 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11886 if (!gent->is_indirect)
11887 allocate_got (h, (struct bfd_link_info *) inf, gent);
11888 return TRUE;
11889}
11890
11891/* Called on the first multitoc pass after the last call to
11892 ppc64_elf_next_toc_section. This function removes duplicate GOT
11893 entries. */
11894
11895bfd_boolean
11896ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11897{
11898 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11899 struct bfd *ibfd, *ibfd2;
11900 bfd_boolean done_something;
11901
11902 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11903
7865406b
AM
11904 if (!htab->do_multi_toc)
11905 return FALSE;
11906
d0fae19d 11907 /* Merge global sym got entries within a toc group. */
927be08e
AM
11908 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11909
11910 /* And tlsld_got. */
c72f2fb2 11911 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11912 {
11913 struct got_entry *ent, *ent2;
11914
11915 if (!is_ppc64_elf (ibfd))
11916 continue;
11917
11918 ent = ppc64_tlsld_got (ibfd);
11919 if (!ent->is_indirect
11920 && ent->got.offset != (bfd_vma) -1)
11921 {
c72f2fb2 11922 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11923 {
11924 if (!is_ppc64_elf (ibfd2))
11925 continue;
11926
11927 ent2 = ppc64_tlsld_got (ibfd2);
11928 if (!ent2->is_indirect
11929 && ent2->got.offset != (bfd_vma) -1
11930 && elf_gp (ibfd2) == elf_gp (ibfd))
11931 {
11932 ent2->is_indirect = TRUE;
11933 ent2->got.ent = ent;
11934 }
11935 }
11936 }
11937 }
11938
11939 /* Zap sizes of got sections. */
33e44f2e
AM
11940 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11941 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11942 htab->got_reli_size = 0;
11943
c72f2fb2 11944 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11945 {
11946 asection *got, *relgot;
11947
11948 if (!is_ppc64_elf (ibfd))
11949 continue;
11950
11951 got = ppc64_elf_tdata (ibfd)->got;
11952 if (got != NULL)
11953 {
11954 got->rawsize = got->size;
11955 got->size = 0;
11956 relgot = ppc64_elf_tdata (ibfd)->relgot;
11957 relgot->rawsize = relgot->size;
11958 relgot->size = 0;
11959 }
11960 }
11961
11962 /* Now reallocate the got, local syms first. We don't need to
11963 allocate section contents again since we never increase size. */
c72f2fb2 11964 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11965 {
11966 struct got_entry **lgot_ents;
11967 struct got_entry **end_lgot_ents;
11968 struct plt_entry **local_plt;
11969 struct plt_entry **end_local_plt;
f961d9dd 11970 unsigned char *lgot_masks;
927be08e
AM
11971 bfd_size_type locsymcount;
11972 Elf_Internal_Shdr *symtab_hdr;
19e08130 11973 asection *s;
927be08e
AM
11974
11975 if (!is_ppc64_elf (ibfd))
11976 continue;
11977
11978 lgot_ents = elf_local_got_ents (ibfd);
11979 if (!lgot_ents)
11980 continue;
11981
11982 symtab_hdr = &elf_symtab_hdr (ibfd);
11983 locsymcount = symtab_hdr->sh_info;
11984 end_lgot_ents = lgot_ents + locsymcount;
11985 local_plt = (struct plt_entry **) end_lgot_ents;
11986 end_local_plt = local_plt + locsymcount;
f961d9dd 11987 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11988 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11989 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11990 {
11991 struct got_entry *ent;
11992
11993 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11994 {
19e08130
AM
11995 unsigned int ent_size = 8;
11996 unsigned int rel_size = sizeof (Elf64_External_Rela);
11997
d0fae19d
AM
11998 ent->got.offset = s->size;
11999 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12000 {
19e08130
AM
12001 ent_size *= 2;
12002 rel_size *= 2;
12003 }
12004 s->size += ent_size;
37da22e5 12005 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12006 {
33e44f2e 12007 htab->elf.irelplt->size += rel_size;
19e08130
AM
12008 htab->got_reli_size += rel_size;
12009 }
6a3858a6
AM
12010 else if (bfd_link_pic (info)
12011 && !((ent->tls_type & TLS_TPREL) != 0
12012 && bfd_link_executable (info)))
19e08130
AM
12013 {
12014 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12015 srel->size += rel_size;
d0fae19d
AM
12016 }
12017 }
927be08e
AM
12018 }
12019 }
12020
12021 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12022
c72f2fb2 12023 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12024 {
12025 struct got_entry *ent;
12026
12027 if (!is_ppc64_elf (ibfd))
12028 continue;
12029
12030 ent = ppc64_tlsld_got (ibfd);
12031 if (!ent->is_indirect
12032 && ent->got.offset != (bfd_vma) -1)
12033 {
12034 asection *s = ppc64_elf_tdata (ibfd)->got;
12035 ent->got.offset = s->size;
12036 s->size += 16;
0e1862bb 12037 if (bfd_link_pic (info))
927be08e
AM
12038 {
12039 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12040 srel->size += sizeof (Elf64_External_Rela);
12041 }
12042 }
12043 }
12044
33e44f2e 12045 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12046 if (!done_something)
c72f2fb2 12047 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12048 {
12049 asection *got;
12050
12051 if (!is_ppc64_elf (ibfd))
12052 continue;
12053
12054 got = ppc64_elf_tdata (ibfd)->got;
12055 if (got != NULL)
12056 {
12057 done_something = got->rawsize != got->size;
12058 if (done_something)
12059 break;
12060 }
12061 }
12062
12063 if (done_something)
e7d1c40c 12064 (*htab->params->layout_sections_again) ();
927be08e
AM
12065
12066 /* Set up for second pass over toc sections to recalculate elf_gp
12067 on input sections. */
12068 htab->toc_bfd = NULL;
12069 htab->toc_first_sec = NULL;
12070 htab->second_toc_pass = TRUE;
12071 return done_something;
12072}
12073
12074/* Called after second pass of multitoc partitioning. */
12075
12076void
12077ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12078{
12079 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12080
12081 /* After the second pass, toc_curr tracks the TOC offset used
12082 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12083 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12084}
12085
9b5ecbd0
AM
12086/* No toc references were found in ISEC. If the code in ISEC makes no
12087 calls, then there's no need to use toc adjusting stubs when branching
12088 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12089 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12090 needed, and 2 if a cyclical call-graph was found but no other reason
12091 for a stub was detected. If called from the top level, a return of
12092 2 means the same as a return of 0. */
9b5ecbd0
AM
12093
12094static int
4ce794b7 12095toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12096{
9b5ecbd0 12097 int ret;
70cc837d
AM
12098
12099 /* Mark this section as checked. */
12100 isec->call_check_done = 1;
9b5ecbd0 12101
772119ce
AM
12102 /* We know none of our code bearing sections will need toc stubs. */
12103 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12104 return 0;
12105
eea6121a 12106 if (isec->size == 0)
082c50f8
AM
12107 return 0;
12108
4c52953f
AM
12109 if (isec->output_section == NULL)
12110 return 0;
12111
4c52953f 12112 ret = 0;
70cc837d 12113 if (isec->reloc_count != 0)
9b5ecbd0 12114 {
70cc837d
AM
12115 Elf_Internal_Rela *relstart, *rel;
12116 Elf_Internal_Sym *local_syms;
12117 struct ppc_link_hash_table *htab;
2917689a 12118
70cc837d
AM
12119 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12120 info->keep_memory);
12121 if (relstart == NULL)
12122 return -1;
90aecf7a 12123
70cc837d
AM
12124 /* Look for branches to outside of this section. */
12125 local_syms = NULL;
12126 htab = ppc_hash_table (info);
12127 if (htab == NULL)
12128 return -1;
4c52953f 12129
70cc837d 12130 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12131 {
70cc837d
AM
12132 enum elf_ppc64_reloc_type r_type;
12133 unsigned long r_symndx;
12134 struct elf_link_hash_entry *h;
12135 struct ppc_link_hash_entry *eh;
12136 Elf_Internal_Sym *sym;
12137 asection *sym_sec;
12138 struct _opd_sec_data *opd;
12139 bfd_vma sym_value;
12140 bfd_vma dest;
12141
12142 r_type = ELF64_R_TYPE (rel->r_info);
12143 if (r_type != R_PPC64_REL24
12144 && r_type != R_PPC64_REL14
12145 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d
AM
12146 && r_type != R_PPC64_REL14_BRNTAKEN
12147 && r_type != R_PPC64_PLTCALL)
70cc837d 12148 continue;
4c52953f 12149
70cc837d
AM
12150 r_symndx = ELF64_R_SYM (rel->r_info);
12151 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12152 isec->owner))
4c52953f 12153 {
70cc837d
AM
12154 ret = -1;
12155 break;
12156 }
4c52953f 12157
70cc837d
AM
12158 /* Calls to dynamic lib functions go through a plt call stub
12159 that uses r2. */
12160 eh = (struct ppc_link_hash_entry *) h;
12161 if (eh != NULL
12162 && (eh->elf.plt.plist != NULL
12163 || (eh->oh != NULL
12164 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12165 {
12166 ret = 1;
12167 break;
4c52953f
AM
12168 }
12169
70cc837d
AM
12170 if (sym_sec == NULL)
12171 /* Ignore other undefined symbols. */
4c52953f 12172 continue;
4c52953f 12173
70cc837d
AM
12174 /* Assume branches to other sections not included in the
12175 link need stubs too, to cover -R and absolute syms. */
12176 if (sym_sec->output_section == NULL)
12177 {
12178 ret = 1;
12179 break;
12180 }
4c52953f 12181
70cc837d
AM
12182 if (h == NULL)
12183 sym_value = sym->st_value;
12184 else
12185 {
12186 if (h->root.type != bfd_link_hash_defined
12187 && h->root.type != bfd_link_hash_defweak)
12188 abort ();
12189 sym_value = h->root.u.def.value;
12190 }
12191 sym_value += rel->r_addend;
4c52953f 12192
70cc837d
AM
12193 /* If this branch reloc uses an opd sym, find the code section. */
12194 opd = get_opd_info (sym_sec);
12195 if (opd != NULL)
12196 {
12197 if (h == NULL && opd->adjust != NULL)
12198 {
12199 long adjust;
4c52953f 12200
92a9c616 12201 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12202 if (adjust == -1)
12203 /* Assume deleted functions won't ever be called. */
12204 continue;
12205 sym_value += adjust;
12206 }
4c52953f 12207
aef36ac1
AM
12208 dest = opd_entry_value (sym_sec, sym_value,
12209 &sym_sec, NULL, FALSE);
70cc837d
AM
12210 if (dest == (bfd_vma) -1)
12211 continue;
12212 }
12213 else
12214 dest = (sym_value
12215 + sym_sec->output_offset
12216 + sym_sec->output_section->vma);
4c52953f 12217
70cc837d
AM
12218 /* Ignore branch to self. */
12219 if (sym_sec == isec)
12220 continue;
4c52953f 12221
70cc837d
AM
12222 /* If the called function uses the toc, we need a stub. */
12223 if (sym_sec->has_toc_reloc
12224 || sym_sec->makes_toc_func_call)
4c52953f 12225 {
70cc837d 12226 ret = 1;
4c52953f
AM
12227 break;
12228 }
70cc837d
AM
12229
12230 /* Assume any branch that needs a long branch stub might in fact
12231 need a plt_branch stub. A plt_branch stub uses r2. */
12232 else if (dest - (isec->output_offset
12233 + isec->output_section->vma
6911b7dc
AM
12234 + rel->r_offset) + (1 << 25)
12235 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12236 ? h->other
12237 : sym->st_other))
4c52953f 12238 {
70cc837d
AM
12239 ret = 1;
12240 break;
12241 }
12242
12243 /* If calling back to a section in the process of being
12244 tested, we can't say for sure that no toc adjusting stubs
12245 are needed, so don't return zero. */
12246 else if (sym_sec->call_check_in_progress)
12247 ret = 2;
12248
12249 /* Branches to another section that itself doesn't have any TOC
12250 references are OK. Recursively call ourselves to check. */
12251 else if (!sym_sec->call_check_done)
12252 {
12253 int recur;
12254
12255 /* Mark current section as indeterminate, so that other
12256 sections that call back to current won't be marked as
12257 known. */
12258 isec->call_check_in_progress = 1;
12259 recur = toc_adjusting_stub_needed (info, sym_sec);
12260 isec->call_check_in_progress = 0;
12261
4c52953f
AM
12262 if (recur != 0)
12263 {
70cc837d
AM
12264 ret = recur;
12265 if (recur != 2)
12266 break;
4c52953f
AM
12267 }
12268 }
4c52953f 12269 }
70cc837d
AM
12270
12271 if (local_syms != NULL
12272 && (elf_symtab_hdr (isec->owner).contents
12273 != (unsigned char *) local_syms))
12274 free (local_syms);
12275 if (elf_section_data (isec)->relocs != relstart)
12276 free (relstart);
9b5ecbd0
AM
12277 }
12278
70cc837d
AM
12279 if ((ret & 1) == 0
12280 && isec->map_head.s != NULL
12281 && (strcmp (isec->output_section->name, ".init") == 0
12282 || strcmp (isec->output_section->name, ".fini") == 0))
12283 {
12284 if (isec->map_head.s->has_toc_reloc
12285 || isec->map_head.s->makes_toc_func_call)
12286 ret = 1;
12287 else if (!isec->map_head.s->call_check_done)
12288 {
12289 int recur;
12290 isec->call_check_in_progress = 1;
12291 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12292 isec->call_check_in_progress = 0;
12293 if (recur != 0)
12294 ret = recur;
12295 }
12296 }
12297
12298 if (ret == 1)
12299 isec->makes_toc_func_call = 1;
4c52953f 12300
9b5ecbd0
AM
12301 return ret;
12302}
12303
721956f4
AM
12304/* The linker repeatedly calls this function for each input section,
12305 in the order that input sections are linked into output sections.
12306 Build lists of input sections to determine groupings between which
12307 we may insert linker stubs. */
12308
9b5ecbd0 12309bfd_boolean
4ce794b7 12310ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12311{
12312 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12313
4dfe6ac6
NC
12314 if (htab == NULL)
12315 return FALSE;
12316
734b6cf9 12317 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12318 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12319 {
3d6f9012
AM
12320 /* This happens to make the list in reverse order,
12321 which is what we want. */
6f20ed8a
AM
12322 htab->sec_info[isec->id].u.list
12323 = htab->sec_info[isec->output_section->id].u.list;
12324 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12325 }
ad8e1ba5 12326
4c52953f 12327 if (htab->multi_toc_needed)
9b5ecbd0 12328 {
8b974ba3
AM
12329 /* Analyse sections that aren't already flagged as needing a
12330 valid toc pointer. Exclude .fixup for the linux kernel.
12331 .fixup contains branches, but only back to the function that
12332 hit an exception. */
12333 if (!(isec->has_toc_reloc
12334 || (isec->flags & SEC_CODE) == 0
12335 || strcmp (isec->name, ".fixup") == 0
12336 || isec->call_check_done))
12337 {
12338 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12339 return FALSE;
8b974ba3
AM
12340 }
12341 /* Make all sections use the TOC assigned for this object file.
12342 This will be wrong for pasted sections; We fix that in
12343 check_pasted_section(). */
12344 if (elf_gp (isec->owner) != 0)
12345 htab->toc_curr = elf_gp (isec->owner);
12346 }
12347
6f20ed8a 12348 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12349 return TRUE;
721956f4
AM
12350}
12351
70cc837d
AM
12352/* Check that all .init and .fini sections use the same toc, if they
12353 have toc relocs. */
12354
12355static bfd_boolean
12356check_pasted_section (struct bfd_link_info *info, const char *name)
12357{
12358 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12359
12360 if (o != NULL)
12361 {
12362 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12363 bfd_vma toc_off = 0;
12364 asection *i;
12365
12366 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12367 if (i->has_toc_reloc)
12368 {
12369 if (toc_off == 0)
6f20ed8a
AM
12370 toc_off = htab->sec_info[i->id].toc_off;
12371 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12372 return FALSE;
12373 }
6683a28d
AM
12374
12375 if (toc_off == 0)
12376 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12377 if (i->makes_toc_func_call)
12378 {
6f20ed8a 12379 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12380 break;
12381 }
12382
70cc837d
AM
12383 /* Make sure the whole pasted function uses the same toc offset. */
12384 if (toc_off != 0)
12385 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12386 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12387 }
12388 return TRUE;
12389}
12390
12391bfd_boolean
12392ppc64_elf_check_init_fini (struct bfd_link_info *info)
12393{
12394 return (check_pasted_section (info, ".init")
12395 & check_pasted_section (info, ".fini"));
12396}
12397
721956f4
AM
12398/* See whether we can group stub sections together. Grouping stub
12399 sections may result in fewer stubs. More importantly, we need to
12400 put all .init* and .fini* stubs at the beginning of the .init or
12401 .fini output sections respectively, because glibc splits the
12402 _init and _fini functions into multiple parts. Putting a stub in
12403 the middle of a function is not a good idea. */
12404
6f20ed8a
AM
12405static bfd_boolean
12406group_sections (struct bfd_link_info *info,
4ce794b7
AM
12407 bfd_size_type stub_group_size,
12408 bfd_boolean stubs_always_before_branch)
721956f4 12409{
6f20ed8a
AM
12410 struct ppc_link_hash_table *htab;
12411 asection *osec;
7c8fe5c4
AM
12412 bfd_boolean suppress_size_errors;
12413
6f20ed8a
AM
12414 htab = ppc_hash_table (info);
12415 if (htab == NULL)
12416 return FALSE;
12417
7c8fe5c4 12418 suppress_size_errors = FALSE;
7c8fe5c4
AM
12419 if (stub_group_size == 1)
12420 {
12421 /* Default values. */
12422 if (stubs_always_before_branch)
09f92717 12423 stub_group_size = 0x1e00000;
7c8fe5c4 12424 else
09f92717 12425 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12426 suppress_size_errors = TRUE;
12427 }
12428
6f20ed8a 12429 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12430 {
6f20ed8a
AM
12431 asection *tail;
12432
12433 if (osec->id >= htab->sec_info_arr_size)
12434 continue;
12435
12436 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12437 while (tail != NULL)
721956f4 12438 {
734b6cf9
AM
12439 asection *curr;
12440 asection *prev;
12441 bfd_size_type total;
12442 bfd_boolean big_sec;
12443 bfd_vma curr_toc;
6f20ed8a 12444 struct map_stub *group;
09f92717 12445 bfd_size_type group_size;
734b6cf9
AM
12446
12447 curr = tail;
eea6121a 12448 total = tail->size;
09f92717
AM
12449 group_size = (ppc64_elf_section_data (tail) != NULL
12450 && ppc64_elf_section_data (tail)->has_14bit_branch
12451 ? stub_group_size >> 10 : stub_group_size);
12452
12453 big_sec = total > group_size;
7c8fe5c4 12454 if (big_sec && !suppress_size_errors)
695344c0 12455 /* xgettext:c-format */
871b3ab2 12456 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12457 tail->owner, tail);
6f20ed8a 12458 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12459
6f20ed8a 12460 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12461 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12462 < (ppc64_elf_section_data (prev) != NULL
12463 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12464 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12465 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12466 curr = prev;
12467
12468 /* OK, the size from the start of CURR to the end is less
09f92717 12469 than group_size and thus can be handled by one stub
734b6cf9 12470 section. (or the tail section is itself larger than
09f92717
AM
12471 group_size, in which case we may be toast.) We should
12472 really be keeping track of the total size of stubs added
12473 here, as stubs contribute to the final output section
12474 size. That's a little tricky, and this way will only
12475 break if stubs added make the total size more than 2^25,
12476 ie. for the default stub_group_size, if stubs total more
12477 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12478 group = bfd_alloc (curr->owner, sizeof (*group));
12479 if (group == NULL)
12480 return FALSE;
12481 group->link_sec = curr;
12482 group->stub_sec = NULL;
a4b6fadd 12483 group->needs_save_res = 0;
d4aaa2a0 12484 group->tls_get_addr_opt_bctrl = -1u;
a4b6fadd
AM
12485 group->next = htab->group;
12486 htab->group = group;
734b6cf9 12487 do
721956f4 12488 {
6f20ed8a 12489 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12490 /* Set up this stub group. */
6f20ed8a 12491 htab->sec_info[tail->id].u.group = group;
721956f4 12492 }
734b6cf9
AM
12493 while (tail != curr && (tail = prev) != NULL);
12494
09f92717 12495 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12496 bytes before the stub section can be handled by it too.
12497 Don't do this if we have a really large section after the
12498 stubs, as adding more stubs increases the chance that
12499 branches may not reach into the stub section. */
12500 if (!stubs_always_before_branch && !big_sec)
12501 {
12502 total = 0;
12503 while (prev != NULL
12504 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12505 < (ppc64_elf_section_data (prev) != NULL
12506 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12507 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12508 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12509 {
12510 tail = prev;
6f20ed8a
AM
12511 prev = htab->sec_info[tail->id].u.list;
12512 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12513 }
12514 }
12515 tail = prev;
721956f4
AM
12516 }
12517 }
6f20ed8a 12518 return TRUE;
721956f4
AM
12519}
12520
58d180e8
AM
12521static const unsigned char glink_eh_frame_cie[] =
12522{
12523 0, 0, 0, 16, /* length. */
12524 0, 0, 0, 0, /* id. */
12525 1, /* CIE version. */
12526 'z', 'R', 0, /* Augmentation string. */
12527 4, /* Code alignment. */
12528 0x78, /* Data alignment. */
12529 65, /* RA reg. */
12530 1, /* Augmentation size. */
12531 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12532 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12533};
12534
d4aaa2a0
AM
12535static size_t
12536stub_eh_frame_size (struct map_stub *group, size_t align)
12537{
12538 size_t this_size = 17;
12539 if (group->tls_get_addr_opt_bctrl != -1u)
12540 {
12541 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12542 if (to_bctrl < 64)
12543 this_size += 1;
12544 else if (to_bctrl < 256)
12545 this_size += 2;
12546 else if (to_bctrl < 65536)
12547 this_size += 3;
12548 else
12549 this_size += 5;
12550 this_size += 6;
12551 }
12552 this_size = (this_size + align - 1) & -align;
12553 return this_size;
12554}
12555
d969d15f
AM
12556/* Stripping output sections is normally done before dynamic section
12557 symbols have been allocated. This function is called later, and
12558 handles cases like htab->brlt which is mapped to its own output
12559 section. */
12560
12561static void
12562maybe_strip_output (struct bfd_link_info *info, asection *isec)
12563{
12564 if (isec->size == 0
12565 && isec->output_section->size == 0
53d8967a 12566 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12567 && !bfd_section_removed_from_list (info->output_bfd,
12568 isec->output_section)
12569 && elf_section_data (isec->output_section)->dynindx == 0)
12570 {
12571 isec->output_section->flags |= SEC_EXCLUDE;
12572 bfd_section_list_remove (info->output_bfd, isec->output_section);
12573 info->output_bfd->section_count--;
12574 }
12575}
12576
721956f4
AM
12577/* Determine and set the size of the stub section for a final link.
12578
12579 The basic idea here is to examine all the relocations looking for
12580 PC-relative calls to a target that is unreachable with a "bl"
12581 instruction. */
12582
b34976b6 12583bfd_boolean
e7d1c40c 12584ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12585{
12586 bfd_size_type stub_group_size;
b34976b6 12587 bfd_boolean stubs_always_before_branch;
721956f4
AM
12588 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12589
4dfe6ac6
NC
12590 if (htab == NULL)
12591 return FALSE;
12592
0e1862bb 12593 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12594 htab->params->plt_thread_safe = 1;
b9e5796b 12595 if (!htab->opd_abi)
e7d1c40c
AM
12596 htab->params->plt_thread_safe = 0;
12597 else if (htab->params->plt_thread_safe == -1)
794e51c0 12598 {
e2458743 12599 static const char *const thread_starter[] =
794e51c0
AM
12600 {
12601 "pthread_create",
12602 /* libstdc++ */
12603 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12604 /* librt */
12605 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12606 "mq_notify", "create_timer",
12607 /* libanl */
12608 "getaddrinfo_a",
12609 /* libgomp */
2300b5a1 12610 "GOMP_parallel",
794e51c0 12611 "GOMP_parallel_start",
2300b5a1 12612 "GOMP_parallel_loop_static",
794e51c0 12613 "GOMP_parallel_loop_static_start",
2300b5a1 12614 "GOMP_parallel_loop_dynamic",
794e51c0 12615 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12616 "GOMP_parallel_loop_guided",
794e51c0 12617 "GOMP_parallel_loop_guided_start",
2300b5a1 12618 "GOMP_parallel_loop_runtime",
794e51c0 12619 "GOMP_parallel_loop_runtime_start",
2300b5a1 12620 "GOMP_parallel_sections",
68ffbac6 12621 "GOMP_parallel_sections_start",
f9dffbf0
AM
12622 /* libgo */
12623 "__go_go",
794e51c0
AM
12624 };
12625 unsigned i;
12626
a4b6fadd 12627 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12628 {
12629 struct elf_link_hash_entry *h;
12630 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12631 FALSE, FALSE, TRUE);
e7d1c40c
AM
12632 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12633 if (htab->params->plt_thread_safe)
794e51c0
AM
12634 break;
12635 }
12636 }
e7d1c40c
AM
12637 stubs_always_before_branch = htab->params->group_size < 0;
12638 if (htab->params->group_size < 0)
12639 stub_group_size = -htab->params->group_size;
721956f4 12640 else
e7d1c40c 12641 stub_group_size = htab->params->group_size;
721956f4 12642
6f20ed8a
AM
12643 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12644 return FALSE;
721956f4 12645
c9301e31
AM
12646#define STUB_SHRINK_ITER 20
12647 /* Loop until no stubs added. After iteration 20 of this loop we may
12648 exit on a stub section shrinking. This is to break out of a
12649 pathological case where adding stubs on one iteration decreases
12650 section gaps (perhaps due to alignment), which then requires
12651 fewer or smaller stubs on the next iteration. */
12652
721956f4
AM
12653 while (1)
12654 {
12655 bfd *input_bfd;
12656 unsigned int bfd_indx;
a4b6fadd 12657 struct map_stub *group;
721956f4
AM
12658
12659 htab->stub_iteration += 1;
721956f4
AM
12660
12661 for (input_bfd = info->input_bfds, bfd_indx = 0;
12662 input_bfd != NULL;
c72f2fb2 12663 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12664 {
12665 Elf_Internal_Shdr *symtab_hdr;
12666 asection *section;
6cdc0ccc 12667 Elf_Internal_Sym *local_syms = NULL;
721956f4 12668
0c8d6e5c 12669 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12670 continue;
12671
721956f4 12672 /* We'll need the symbol table in a second. */
0ffa91dd 12673 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12674 if (symtab_hdr->sh_info == 0)
12675 continue;
12676
721956f4
AM
12677 /* Walk over each section attached to the input bfd. */
12678 for (section = input_bfd->sections;
12679 section != NULL;
12680 section = section->next)
12681 {
721956f4 12682 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12683
12684 /* If there aren't any relocs, then there's nothing more
12685 to do. */
12686 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12687 || (section->flags & SEC_ALLOC) == 0
12688 || (section->flags & SEC_LOAD) == 0
12689 || (section->flags & SEC_CODE) == 0
721956f4
AM
12690 || section->reloc_count == 0)
12691 continue;
12692
12693 /* If this section is a link-once section that will be
12694 discarded, then don't create any stubs. */
12695 if (section->output_section == NULL
927be08e 12696 || section->output_section->owner != info->output_bfd)
721956f4
AM
12697 continue;
12698
1e2f5b6e
AM
12699 /* Get the relocs. */
12700 internal_relocs
4ce794b7 12701 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12702 info->keep_memory);
721956f4 12703 if (internal_relocs == NULL)
1e2f5b6e 12704 goto error_ret_free_local;
721956f4
AM
12705
12706 /* Now examine each relocation. */
12707 irela = internal_relocs;
12708 irelaend = irela + section->reloc_count;
12709 for (; irela < irelaend; irela++)
12710 {
4ce794b7
AM
12711 enum elf_ppc64_reloc_type r_type;
12712 unsigned int r_indx;
721956f4
AM
12713 enum ppc_stub_type stub_type;
12714 struct ppc_stub_hash_entry *stub_entry;
8387904d 12715 asection *sym_sec, *code_sec;
e054468f 12716 bfd_vma sym_value, code_value;
721956f4 12717 bfd_vma destination;
6911b7dc 12718 unsigned long local_off;
8843416a 12719 bfd_boolean ok_dest;
721956f4 12720 struct ppc_link_hash_entry *hash;
8387904d 12721 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12722 struct elf_link_hash_entry *h;
12723 Elf_Internal_Sym *sym;
721956f4
AM
12724 char *stub_name;
12725 const asection *id_sec;
74f0fb50 12726 struct _opd_sec_data *opd;
e054468f 12727 struct plt_entry *plt_ent;
721956f4
AM
12728
12729 r_type = ELF64_R_TYPE (irela->r_info);
12730 r_indx = ELF64_R_SYM (irela->r_info);
12731
4ce794b7 12732 if (r_type >= R_PPC64_max)
721956f4
AM
12733 {
12734 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12735 goto error_ret_free_internal;
721956f4
AM
12736 }
12737
12738 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12739 if (r_type != R_PPC64_REL24
12740 && r_type != R_PPC64_REL14
12741 && r_type != R_PPC64_REL14_BRTAKEN
12742 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12743 continue;
12744
12745 /* Now determine the call target, its name, value,
12746 section. */
411e1bfb
AM
12747 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12748 r_indx, input_bfd))
12749 goto error_ret_free_internal;
12750 hash = (struct ppc_link_hash_entry *) h;
12751
8843416a 12752 ok_dest = FALSE;
8387904d 12753 fdh = NULL;
7fe2b9a6 12754 sym_value = 0;
411e1bfb 12755 if (hash == NULL)
721956f4 12756 {
411e1bfb 12757 sym_value = sym->st_value;
c27b8c2a
AM
12758 if (sym_sec != NULL
12759 && sym_sec->output_section != NULL)
12760 ok_dest = TRUE;
721956f4 12761 }
7fe2b9a6
AM
12762 else if (hash->elf.root.type == bfd_link_hash_defined
12763 || hash->elf.root.type == bfd_link_hash_defweak)
12764 {
12765 sym_value = hash->elf.root.u.def.value;
12766 if (sym_sec->output_section != NULL)
12767 ok_dest = TRUE;
12768 }
12769 else if (hash->elf.root.type == bfd_link_hash_undefweak
12770 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12771 {
99877b66 12772 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12773 use the func descriptor sym instead if it is
12774 defined. */
ceb1f1ef 12775 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12776 && hash->oh != NULL)
8387904d 12777 {
8c5b4e52 12778 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12779 if (fdh->elf.root.type == bfd_link_hash_defined
12780 || fdh->elf.root.type == bfd_link_hash_defweak)
12781 {
12782 sym_sec = fdh->elf.root.u.def.section;
12783 sym_value = fdh->elf.root.u.def.value;
12784 if (sym_sec->output_section != NULL)
12785 ok_dest = TRUE;
12786 }
99877b66
AM
12787 else
12788 fdh = NULL;
8387904d 12789 }
7fe2b9a6
AM
12790 }
12791 else
12792 {
12793 bfd_set_error (bfd_error_bad_value);
12794 goto error_ret_free_internal;
721956f4
AM
12795 }
12796
8843416a 12797 destination = 0;
6911b7dc 12798 local_off = 0;
8843416a
AM
12799 if (ok_dest)
12800 {
12801 sym_value += irela->r_addend;
12802 destination = (sym_value
12803 + sym_sec->output_offset
12804 + sym_sec->output_section->vma);
6911b7dc
AM
12805 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12806 ? hash->elf.other
12807 : sym->st_other);
8843416a
AM
12808 }
12809
8387904d 12810 code_sec = sym_sec;
e054468f 12811 code_value = sym_value;
74f0fb50
AM
12812 opd = get_opd_info (sym_sec);
12813 if (opd != NULL)
8387904d
AM
12814 {
12815 bfd_vma dest;
12816
74f0fb50 12817 if (hash == NULL && opd->adjust != NULL)
8387904d 12818 {
51aecdc5 12819 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12820 if (adjust == -1)
12821 continue;
e054468f 12822 code_value += adjust;
8387904d
AM
12823 sym_value += adjust;
12824 }
12825 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12826 &code_sec, &code_value, FALSE);
8387904d
AM
12827 if (dest != (bfd_vma) -1)
12828 {
12829 destination = dest;
12830 if (fdh != NULL)
12831 {
12832 /* Fixup old ABI sym to point at code
12833 entry. */
99877b66 12834 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12835 hash->elf.root.u.def.section = code_sec;
e054468f 12836 hash->elf.root.u.def.value = code_value;
8387904d
AM
12837 }
12838 }
12839 }
12840
721956f4 12841 /* Determine what (if any) linker stub is needed. */
e054468f 12842 plt_ent = NULL;
721956f4 12843 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12844 &plt_ent, destination,
12845 local_off);
ad8e1ba5
AM
12846
12847 if (stub_type != ppc_stub_plt_call)
12848 {
12849 /* Check whether we need a TOC adjusting stub.
12850 Since the linker pastes together pieces from
12851 different object files when creating the
12852 _init and _fini functions, it may be that a
12853 call to what looks like a local sym is in
12854 fact a call needing a TOC adjustment. */
8387904d
AM
12855 if (code_sec != NULL
12856 && code_sec->output_section != NULL
6f20ed8a
AM
12857 && (htab->sec_info[code_sec->id].toc_off
12858 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12859 && (code_sec->has_toc_reloc
12860 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12861 stub_type = ppc_stub_long_branch_r2off;
12862 }
12863
721956f4
AM
12864 if (stub_type == ppc_stub_none)
12865 continue;
12866
411e1bfb
AM
12867 /* __tls_get_addr calls might be eliminated. */
12868 if (stub_type != ppc_stub_plt_call
12869 && hash != NULL
8387904d
AM
12870 && (hash == htab->tls_get_addr
12871 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12872 && section->has_tls_reloc
12873 && irela != internal_relocs)
12874 {
12875 /* Get tls info. */
f961d9dd 12876 unsigned char *tls_mask;
411e1bfb 12877
3a71aa26 12878 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12879 irela - 1, input_bfd))
12880 goto error_ret_free_internal;
37da22e5 12881 if ((*tls_mask & TLS_TLS) != 0)
411e1bfb
AM
12882 continue;
12883 }
12884
f378ab09 12885 if (stub_type == ppc_stub_plt_call)
794e51c0 12886 {
6e1816be
AM
12887 if (!htab->opd_abi
12888 && htab->params->plt_localentry0 != 0
12889 && is_elfv2_localentry0 (&hash->elf))
12890 htab->has_plt_localentry0 = 1;
12891 else if (irela + 1 < irelaend
12892 && irela[1].r_offset == irela->r_offset + 4
12893 && (ELF64_R_TYPE (irela[1].r_info)
12894 == R_PPC64_TOCSAVE))
f378ab09
AM
12895 {
12896 if (!tocsave_find (htab, INSERT,
12897 &local_syms, irela + 1, input_bfd))
12898 goto error_ret_free_internal;
12899 }
f378ab09
AM
12900 else
12901 stub_type = ppc_stub_plt_call_r2save;
794e51c0 12902 }
3b421ab3 12903
721956f4 12904 /* Support for grouping stub sections. */
6f20ed8a 12905 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12906
12907 /* Get the name of this stub. */
12908 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12909 if (!stub_name)
12910 goto error_ret_free_internal;
12911
12912 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12913 stub_name, FALSE, FALSE);
721956f4
AM
12914 if (stub_entry != NULL)
12915 {
12916 /* The proper stub has already been created. */
12917 free (stub_name);
794e51c0
AM
12918 if (stub_type == ppc_stub_plt_call_r2save)
12919 stub_entry->stub_type = stub_type;
721956f4
AM
12920 continue;
12921 }
12922
25f53a85 12923 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12924 if (stub_entry == NULL)
12925 {
12926 free (stub_name);
6cdc0ccc
AM
12927 error_ret_free_internal:
12928 if (elf_section_data (section)->relocs == NULL)
12929 free (internal_relocs);
12930 error_ret_free_local:
12931 if (local_syms != NULL
12932 && (symtab_hdr->contents
12933 != (unsigned char *) local_syms))
12934 free (local_syms);
b34976b6 12935 return FALSE;
721956f4
AM
12936 }
12937
ad8e1ba5 12938 stub_entry->stub_type = stub_type;
794e51c0
AM
12939 if (stub_type != ppc_stub_plt_call
12940 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12941 {
12942 stub_entry->target_value = code_value;
12943 stub_entry->target_section = code_sec;
12944 }
12945 else
12946 {
12947 stub_entry->target_value = sym_value;
12948 stub_entry->target_section = sym_sec;
12949 }
721956f4 12950 stub_entry->h = hash;
e054468f 12951 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
12952 stub_entry->symtype
12953 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 12954 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12955
12956 if (stub_entry->h != NULL)
12957 htab->stub_globals += 1;
721956f4
AM
12958 }
12959
12960 /* We're done with the internal relocs, free them. */
6cdc0ccc 12961 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12962 free (internal_relocs);
721956f4 12963 }
6cdc0ccc
AM
12964
12965 if (local_syms != NULL
12966 && symtab_hdr->contents != (unsigned char *) local_syms)
12967 {
12968 if (!info->keep_memory)
12969 free (local_syms);
12970 else
12971 symtab_hdr->contents = (unsigned char *) local_syms;
12972 }
721956f4
AM
12973 }
12974
5c3dead3 12975 /* We may have added some stubs. Find out the new size of the
721956f4 12976 stub sections. */
d4aaa2a0
AM
12977 for (group = htab->group; group != NULL; group = group->next)
12978 if (group->stub_sec != NULL)
ee75fd95 12979 {
d4aaa2a0
AM
12980 asection *stub_sec = group->stub_sec;
12981
ea3d7d1c
AM
12982 if (htab->stub_iteration <= STUB_SHRINK_ITER
12983 || stub_sec->rawsize < stub_sec->size)
12984 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12985 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12986 stub_sec->size = 0;
12987 stub_sec->reloc_count = 0;
84f5d08e 12988 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12989 }
eea6121a 12990
ba21f564
AM
12991 if (htab->stub_iteration <= STUB_SHRINK_ITER
12992 || htab->brlt->rawsize < htab->brlt->size)
12993 htab->brlt->rawsize = htab->brlt->size;
eea6121a 12994 htab->brlt->size = 0;
84f5d08e
AM
12995 htab->brlt->reloc_count = 0;
12996 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12997 if (htab->relbrlt != NULL)
eea6121a 12998 htab->relbrlt->size = 0;
721956f4 12999
63bc6f6c 13000 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13001
a4b6fadd
AM
13002 for (group = htab->group; group != NULL; group = group->next)
13003 if (group->needs_save_res)
13004 group->stub_sec->size += htab->sfpr->size;
13005
176a0d42
AM
13006 if (info->emitrelocations
13007 && htab->glink != NULL && htab->glink->size != 0)
13008 {
13009 htab->glink->reloc_count = 1;
13010 htab->glink->flags |= SEC_RELOC;
13011 }
13012
58d180e8
AM
13013 if (htab->glink_eh_frame != NULL
13014 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13015 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13016 {
2e0ce1c8 13017 size_t size = 0, align = 4;
58d180e8 13018
d4aaa2a0
AM
13019 for (group = htab->group; group != NULL; group = group->next)
13020 if (group->stub_sec != NULL)
13021 size += stub_eh_frame_size (group, align);
58d180e8 13022 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13023 size += (24 + align - 1) & -align;
58d180e8 13024 if (size != 0)
2e0ce1c8
AM
13025 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13026 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13027 size = (size + align - 1) & -align;
58d180e8
AM
13028 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13029 htab->glink_eh_frame->size = size;
13030 }
13031
e7d1c40c 13032 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13033 for (group = htab->group; group != NULL; group = group->next)
13034 if (group->stub_sec != NULL)
691d2e9a
AM
13035 {
13036 int align = abs (htab->params->plt_stub_align);
13037 group->stub_sec->size
13038 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13039 }
d4aaa2a0
AM
13040
13041 for (group = htab->group; group != NULL; group = group->next)
13042 if (group->stub_sec != NULL
13043 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13044 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13045 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13046 break;
13047
d4aaa2a0 13048 if (group == NULL
ba21f564
AM
13049 && (htab->brlt->rawsize == htab->brlt->size
13050 || (htab->stub_iteration > STUB_SHRINK_ITER
13051 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
13052 && (htab->glink_eh_frame == NULL
13053 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
13054 break;
13055
721956f4 13056 /* Ask the linker to do its stuff. */
e7d1c40c 13057 (*htab->params->layout_sections_again) ();
721956f4
AM
13058 }
13059
da44f4e5
AM
13060 if (htab->glink_eh_frame != NULL
13061 && htab->glink_eh_frame->size != 0)
13062 {
13063 bfd_vma val;
13064 bfd_byte *p, *last_fde;
13065 size_t last_fde_len, size, align, pad;
d4aaa2a0 13066 struct map_stub *group;
da44f4e5
AM
13067
13068 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13069 if (p == NULL)
13070 return FALSE;
13071 htab->glink_eh_frame->contents = p;
13072 last_fde = p;
2e0ce1c8 13073 align = 4;
da44f4e5
AM
13074
13075 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13076 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13077 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13078 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13079 p += last_fde_len + 4;
da44f4e5 13080
d4aaa2a0
AM
13081 for (group = htab->group; group != NULL; group = group->next)
13082 if (group->stub_sec != NULL)
da44f4e5
AM
13083 {
13084 last_fde = p;
d4aaa2a0 13085 last_fde_len = stub_eh_frame_size (group, align) - 4;
da44f4e5 13086 /* FDE length. */
2e0ce1c8 13087 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13088 p += 4;
13089 /* CIE pointer. */
13090 val = p - htab->glink_eh_frame->contents;
13091 bfd_put_32 (htab->elf.dynobj, val, p);
13092 p += 4;
13093 /* Offset to stub section, written later. */
13094 p += 4;
13095 /* stub section size. */
d4aaa2a0 13096 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13097 p += 4;
13098 /* Augmentation. */
13099 p += 1;
d4aaa2a0
AM
13100 if (group->tls_get_addr_opt_bctrl != -1u)
13101 {
13102 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
13103
13104 /* This FDE needs more than just the default.
13105 Describe __tls_get_addr_opt stub LR. */
13106 if (to_bctrl < 64)
13107 *p++ = DW_CFA_advance_loc + to_bctrl;
13108 else if (to_bctrl < 256)
13109 {
13110 *p++ = DW_CFA_advance_loc1;
13111 *p++ = to_bctrl;
13112 }
13113 else if (to_bctrl < 65536)
13114 {
13115 *p++ = DW_CFA_advance_loc2;
13116 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
13117 p += 2;
13118 }
13119 else
13120 {
13121 *p++ = DW_CFA_advance_loc4;
13122 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
13123 p += 4;
13124 }
13125 *p++ = DW_CFA_offset_extended_sf;
13126 *p++ = 65;
13127 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
13128 *p++ = DW_CFA_advance_loc + 4;
13129 *p++ = DW_CFA_restore_extended;
13130 *p++ = 65;
13131 }
da44f4e5 13132 /* Pad. */
d4aaa2a0 13133 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13134 }
13135 if (htab->glink != NULL && htab->glink->size != 0)
13136 {
13137 last_fde = p;
2e0ce1c8 13138 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13139 /* FDE length. */
2e0ce1c8 13140 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13141 p += 4;
13142 /* CIE pointer. */
13143 val = p - htab->glink_eh_frame->contents;
13144 bfd_put_32 (htab->elf.dynobj, val, p);
13145 p += 4;
13146 /* Offset to .glink, written later. */
13147 p += 4;
13148 /* .glink size. */
13149 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13150 p += 4;
13151 /* Augmentation. */
13152 p += 1;
13153
13154 *p++ = DW_CFA_advance_loc + 1;
13155 *p++ = DW_CFA_register;
13156 *p++ = 65;
9f08fa5c 13157 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 13158 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
13159 *p++ = DW_CFA_restore_extended;
13160 *p++ = 65;
2e0ce1c8 13161 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13162 }
13163 /* Subsume any padding into the last FDE if user .eh_frame
13164 sections are aligned more than glink_eh_frame. Otherwise any
13165 zero padding will be seen as a terminator. */
2e0ce1c8 13166 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13167 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13168 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13169 htab->glink_eh_frame->size = size + pad;
13170 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13171 }
13172
d969d15f
AM
13173 maybe_strip_output (info, htab->brlt);
13174 if (htab->glink_eh_frame != NULL)
13175 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13176
b34976b6 13177 return TRUE;
721956f4
AM
13178}
13179
13180/* Called after we have determined section placement. If sections
805fc799 13181 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13182
805fc799 13183bfd_vma
1c865ab2 13184ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13185{
805fc799 13186 asection *s;
a27e685f 13187 bfd_vma TOCstart, adjust;
721956f4 13188
43417696
AM
13189 if (info != NULL)
13190 {
13191 struct elf_link_hash_entry *h;
13192 struct elf_link_hash_table *htab = elf_hash_table (info);
13193
13194 if (is_elf_hash_table (htab)
13195 && htab->hgot != NULL)
13196 h = htab->hgot;
13197 else
13198 {
13199 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13200 if (is_elf_hash_table (htab))
13201 htab->hgot = h;
13202 }
13203 if (h != NULL
13204 && h->root.type == bfd_link_hash_defined
13205 && !h->root.linker_def
13206 && (!is_elf_hash_table (htab)
13207 || h->def_regular))
13208 {
13209 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13210 + h->root.u.def.section->output_offset
13211 + h->root.u.def.section->output_section->vma);
13212 _bfd_set_gp_value (obfd, TOCstart);
13213 return TOCstart;
13214 }
13215 }
13216
805fc799
AM
13217 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13218 order. The TOC starts where the first of these sections starts. */
13219 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13220 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13221 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13222 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13223 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13224 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13225 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13226 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13227 {
13228 /* This may happen for
13229 o references to TOC base (SYM@toc / TOC[tc0]) without a
13230 .toc directive
13231 o bad linker script
13232 o --gc-sections and empty TOC sections
13233
13234 FIXME: Warn user? */
13235
13236 /* Look for a likely section. We probably won't even be
13237 using TOCstart. */
13238 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13239 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13240 | SEC_EXCLUDE))
805fc799
AM
13241 == (SEC_ALLOC | SEC_SMALL_DATA))
13242 break;
721956f4 13243 if (s == NULL)
805fc799 13244 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13245 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
13246 == (SEC_ALLOC | SEC_SMALL_DATA))
13247 break;
721956f4 13248 if (s == NULL)
805fc799 13249 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13250 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13251 == SEC_ALLOC)
805fc799 13252 break;
721956f4 13253 if (s == NULL)
805fc799 13254 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 13255 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
13256 break;
13257 }
721956f4 13258
805fc799
AM
13259 TOCstart = 0;
13260 if (s != NULL)
13261 TOCstart = s->output_section->vma + s->output_offset;
721956f4 13262
a27e685f
AM
13263 /* Force alignment. */
13264 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13265 TOCstart -= adjust;
1c865ab2
AM
13266 _bfd_set_gp_value (obfd, TOCstart);
13267
810d4e75 13268 if (info != NULL && s != NULL)
1c865ab2
AM
13269 {
13270 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13271
810d4e75
AM
13272 if (htab != NULL)
13273 {
13274 if (htab->elf.hgot != NULL)
13275 {
a27e685f 13276 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
13277 htab->elf.hgot->root.u.def.section = s;
13278 }
13279 }
13280 else
1c865ab2 13281 {
810d4e75
AM
13282 struct bfd_link_hash_entry *bh = NULL;
13283 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
13284 s, TOC_BASE_OFF - adjust,
13285 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
13286 }
13287 }
805fc799 13288 return TOCstart;
721956f4
AM
13289}
13290
a345bc8d 13291/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 13292 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
13293
13294static bfd_boolean
49c09209 13295build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
13296{
13297 struct bfd_link_info *info;
13298 struct ppc_link_hash_table *htab;
49c09209 13299 struct plt_entry *ent;
a345bc8d
AM
13300 asection *s;
13301
13302 if (h->root.type == bfd_link_hash_indirect)
13303 return TRUE;
13304
49c09209
AM
13305 info = inf;
13306 htab = ppc_hash_table (info);
13307 if (htab == NULL)
13308 return FALSE;
13309
13310 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13311 if (ent->plt.offset != (bfd_vma) -1)
13312 {
13313 /* This symbol has an entry in the procedure linkage
13314 table. Set it up. */
13315 Elf_Internal_Rela rela;
2d7ad24e 13316 asection *plt, *relplt;
49c09209
AM
13317 bfd_byte *loc;
13318
13319 if (!htab->elf.dynamic_sections_created
13320 || h->dynindx == -1)
13321 {
13322 if (!(h->def_regular
13323 && (h->root.type == bfd_link_hash_defined
13324 || h->root.type == bfd_link_hash_defweak)))
13325 continue;
2d7ad24e
AM
13326 if (h->type == STT_GNU_IFUNC)
13327 {
13328 plt = htab->elf.iplt;
13329 relplt = htab->elf.irelplt;
13330 htab->local_ifunc_resolver = 1;
13331 if (htab->opd_abi)
13332 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13333 else
13334 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13335 }
49c09209 13336 else
2d7ad24e
AM
13337 {
13338 plt = htab->pltlocal;
13339 if (bfd_link_pic (info))
13340 {
13341 relplt = htab->relpltlocal;
13342 if (htab->opd_abi)
13343 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13344 else
13345 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13346 }
13347 else
13348 relplt = NULL;
13349 }
49c09209
AM
13350 rela.r_addend = (h->root.u.def.value
13351 + h->root.u.def.section->output_offset
13352 + h->root.u.def.section->output_section->vma
13353 + ent->addend);
2d7ad24e
AM
13354
13355 if (relplt == NULL)
13356 {
13357 loc = plt->contents + ent->plt.offset;
13358 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13359 if (htab->opd_abi)
13360 {
13361 bfd_vma toc = elf_gp (info->output_bfd);
13362 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13363 bfd_put_64 (info->output_bfd, toc, loc + 8);
13364 }
13365 }
13366 else
13367 {
13368 rela.r_offset = (plt->output_section->vma
13369 + plt->output_offset
13370 + ent->plt.offset);
13371 loc = relplt->contents + (relplt->reloc_count++
13372 * sizeof (Elf64_External_Rela));
13373 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13374 }
49c09209
AM
13375 }
13376 else
13377 {
13378 rela.r_offset = (htab->elf.splt->output_section->vma
13379 + htab->elf.splt->output_offset
13380 + ent->plt.offset);
13381 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13382 rela.r_addend = ent->addend;
13383 loc = (htab->elf.srelplt->contents
13384 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13385 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13386 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13387 htab->maybe_local_ifunc_resolver = 1;
2d7ad24e 13388 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 13389 }
49c09209
AM
13390 }
13391
a345bc8d
AM
13392 if (!h->pointer_equality_needed)
13393 return TRUE;
13394
13395 if (h->def_regular)
13396 return TRUE;
13397
9e390558 13398 s = htab->global_entry;
49c09209
AM
13399 if (s == NULL || s->size == 0)
13400 return TRUE;
13401
13402 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13403 if (ent->plt.offset != (bfd_vma) -1
13404 && ent->addend == 0)
a345bc8d
AM
13405 {
13406 bfd_byte *p;
13407 asection *plt;
13408 bfd_vma off;
13409
a345bc8d 13410 p = s->contents + h->root.u.def.value;
33e44f2e 13411 plt = htab->elf.splt;
a345bc8d
AM
13412 if (!htab->elf.dynamic_sections_created
13413 || h->dynindx == -1)
2d7ad24e
AM
13414 {
13415 if (h->type == STT_GNU_IFUNC)
13416 plt = htab->elf.iplt;
13417 else
13418 plt = htab->pltlocal;
13419 }
49c09209 13420 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
13421 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13422
13423 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13424 {
13425 info->callbacks->einfo
c1c8c1ef 13426 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13427 h->root.root.string);
13428 bfd_set_error (bfd_error_bad_value);
13429 htab->stub_error = TRUE;
13430 }
13431
7341d5e2
AM
13432 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13433 if (htab->params->emit_stub_syms)
13434 {
13435 size_t len = strlen (h->root.root.string);
13436 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13437
13438 if (name == NULL)
13439 return FALSE;
13440
13441 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13442 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13443 if (h == NULL)
13444 return FALSE;
13445 if (h->root.type == bfd_link_hash_new)
13446 {
13447 h->root.type = bfd_link_hash_defined;
13448 h->root.u.def.section = s;
13449 h->root.u.def.value = p - s->contents;
13450 h->ref_regular = 1;
13451 h->def_regular = 1;
13452 h->ref_regular_nonweak = 1;
13453 h->forced_local = 1;
13454 h->non_elf = 0;
2ec55de3 13455 h->root.linker_def = 1;
7341d5e2
AM
13456 }
13457 }
13458
a345bc8d
AM
13459 if (PPC_HA (off) != 0)
13460 {
13461 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13462 p += 4;
13463 }
13464 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13465 p += 4;
13466 bfd_put_32 (s->owner, MTCTR_R12, p);
13467 p += 4;
407aa07c 13468 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13469 break;
13470 }
13471 return TRUE;
13472}
13473
49c09209
AM
13474/* Write PLT relocs for locals. */
13475
13476static bfd_boolean
13477write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13478{
13479 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13480 bfd *ibfd;
13481
13482 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13483 {
13484 struct got_entry **lgot_ents, **end_lgot_ents;
13485 struct plt_entry **local_plt, **lplt, **end_local_plt;
13486 Elf_Internal_Shdr *symtab_hdr;
13487 bfd_size_type locsymcount;
13488 Elf_Internal_Sym *local_syms = NULL;
13489 struct plt_entry *ent;
13490
13491 if (!is_ppc64_elf (ibfd))
13492 continue;
13493
13494 lgot_ents = elf_local_got_ents (ibfd);
13495 if (!lgot_ents)
13496 continue;
13497
13498 symtab_hdr = &elf_symtab_hdr (ibfd);
13499 locsymcount = symtab_hdr->sh_info;
13500 end_lgot_ents = lgot_ents + locsymcount;
13501 local_plt = (struct plt_entry **) end_lgot_ents;
13502 end_local_plt = local_plt + locsymcount;
13503 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13504 for (ent = *lplt; ent != NULL; ent = ent->next)
13505 if (ent->plt.offset != (bfd_vma) -1)
13506 {
13507 Elf_Internal_Sym *sym;
13508 asection *sym_sec;
13509 asection *plt, *relplt;
13510 bfd_byte *loc;
13511 bfd_vma val;
49c09209
AM
13512
13513 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13514 lplt - local_plt, ibfd))
13515 {
13516 if (local_syms != NULL
13517 && symtab_hdr->contents != (unsigned char *) local_syms)
13518 free (local_syms);
13519 return FALSE;
13520 }
13521
13522 val = sym->st_value + ent->addend;
13523 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13524 if (sym_sec != NULL && sym_sec->output_section != NULL)
13525 val += sym_sec->output_offset + sym_sec->output_section->vma;
13526
2d7ad24e
AM
13527 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13528 {
13529 htab->local_ifunc_resolver = 1;
13530 plt = htab->elf.iplt;
13531 relplt = htab->elf.irelplt;
13532 }
13533 else
13534 {
13535 plt = htab->pltlocal;
13536 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13537 }
49c09209 13538
2d7ad24e
AM
13539 if (relplt == NULL)
13540 {
13541 loc = plt->contents + ent->plt.offset;
13542 bfd_put_64 (info->output_bfd, val, loc);
13543 if (htab->opd_abi)
13544 {
13545 bfd_vma toc = elf_gp (ibfd);
13546 bfd_put_64 (info->output_bfd, toc, loc + 8);
13547 }
13548 }
49c09209 13549 else
2d7ad24e
AM
13550 {
13551 Elf_Internal_Rela rela;
13552 rela.r_offset = (ent->plt.offset
13553 + plt->output_offset
13554 + plt->output_section->vma);
13555 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13556 {
13557 if (htab->opd_abi)
13558 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13559 else
13560 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13561 }
13562 else
13563 {
13564 if (htab->opd_abi)
13565 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13566 else
13567 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13568 }
13569 rela.r_addend = val;
13570 loc = relplt->contents + (relplt->reloc_count++
13571 * sizeof (Elf64_External_Rela));
13572 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13573 }
49c09209
AM
13574 }
13575
13576 if (local_syms != NULL
13577 && symtab_hdr->contents != (unsigned char *) local_syms)
13578 {
13579 if (!info->keep_memory)
13580 free (local_syms);
13581 else
13582 symtab_hdr->contents = (unsigned char *) local_syms;
13583 }
13584 }
13585 return TRUE;
13586}
13587
721956f4
AM
13588/* Build all the stubs associated with the current output file.
13589 The stubs are kept in a hash table attached to the main linker
13590 hash table. This function is called via gldelf64ppc_finish. */
13591
b34976b6 13592bfd_boolean
e7d1c40c 13593ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13594 char **stats)
5d1634d7
AM
13595{
13596 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13597 struct map_stub *group;
721956f4 13598 asection *stub_sec;
5d1634d7 13599 bfd_byte *p;
e717da7e 13600 int stub_sec_count = 0;
5d1634d7 13601
4dfe6ac6
NC
13602 if (htab == NULL)
13603 return FALSE;
13604
eea6121a 13605 /* Allocate memory to hold the linker stubs. */
d4aaa2a0
AM
13606 for (group = htab->group; group != NULL; group = group->next)
13607 if ((stub_sec = group->stub_sec) != NULL
eea6121a 13608 && stub_sec->size != 0)
e717da7e 13609 {
e7d1c40c 13610 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
13611 if (stub_sec->contents == NULL)
13612 return FALSE;
eea6121a 13613 stub_sec->size = 0;
e717da7e 13614 }
5d1634d7 13615
23eb7e01 13616 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13617 {
9f951329 13618 unsigned int indx;
ad8e1ba5 13619 bfd_vma plt0;
9f951329 13620
721956f4 13621 /* Build the .glink plt call stub. */
e7d1c40c 13622 if (htab->params->emit_stub_syms)
97b639ba
AM
13623 {
13624 struct elf_link_hash_entry *h;
468392fb
AM
13625 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13626 TRUE, FALSE, FALSE);
97b639ba
AM
13627 if (h == NULL)
13628 return FALSE;
13629 if (h->root.type == bfd_link_hash_new)
13630 {
13631 h->root.type = bfd_link_hash_defined;
13632 h->root.u.def.section = htab->glink;
ee4bf8d2 13633 h->root.u.def.value = 8;
f5385ebf
AM
13634 h->ref_regular = 1;
13635 h->def_regular = 1;
13636 h->ref_regular_nonweak = 1;
13637 h->forced_local = 1;
13638 h->non_elf = 0;
2ec55de3 13639 h->root.linker_def = 1;
97b639ba
AM
13640 }
13641 }
33e44f2e
AM
13642 plt0 = (htab->elf.splt->output_section->vma
13643 + htab->elf.splt->output_offset
13644 - 16);
176a0d42
AM
13645 if (info->emitrelocations)
13646 {
13647 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13648 if (r == NULL)
13649 return FALSE;
13650 r->r_offset = (htab->glink->output_offset
13651 + htab->glink->output_section->vma);
13652 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13653 r->r_addend = plt0;
13654 }
4ce794b7 13655 p = htab->glink->contents;
176a0d42 13656 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13657 bfd_put_64 (htab->glink->owner, plt0, p);
13658 p += 8;
b9e5796b
AM
13659 if (htab->opd_abi)
13660 {
13661 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13662 p += 4;
13663 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13664 p += 4;
13665 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13666 p += 4;
13667 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13668 p += 4;
13669 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13670 p += 4;
13671 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13672 p += 4;
13673 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13674 p += 4;
13675 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13676 p += 4;
13677 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13678 p += 4;
13679 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13680 p += 4;
13681 }
13682 else
13683 {
13684 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13685 p += 4;
13686 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13687 p += 4;
13688 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13689 p += 4;
f378ab09
AM
13690 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13691 p += 4;
b9e5796b
AM
13692 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13693 p += 4;
13694 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13695 p += 4;
13696 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13697 p += 4;
13698 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13699 p += 4;
13700 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13701 p += 4;
13702 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13703 p += 4;
13704 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13705 p += 4;
13706 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13707 p += 4;
13708 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13709 p += 4;
13710 }
407aa07c
AM
13711 bfd_put_32 (htab->glink->owner, BCTR, p);
13712 p += 4;
c75bc4f7 13713 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13714
9f951329
AM
13715 /* Build the .glink lazy link call stubs. */
13716 indx = 0;
9e390558 13717 while (p < htab->glink->contents + htab->glink->size)
9f951329 13718 {
b9e5796b 13719 if (htab->opd_abi)
9f951329 13720 {
b9e5796b
AM
13721 if (indx < 0x8000)
13722 {
13723 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13724 p += 4;
13725 }
13726 else
13727 {
13728 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13729 p += 4;
13730 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13731 p);
13732 p += 4;
13733 }
9f951329 13734 }
4ce794b7 13735 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13736 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13737 indx++;
9f951329
AM
13738 p += 4;
13739 }
5d1634d7 13740 }
5d1634d7 13741
49c09209
AM
13742 /* Build .glink global entry stubs, and PLT relocs for globals. */
13743 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13744
13745 if (!write_plt_relocs_for_local_syms (info))
13746 return FALSE;
9e390558 13747
7341d5e2 13748 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13749 {
4ce794b7 13750 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13751 htab->brlt->size);
4ce794b7 13752 if (htab->brlt->contents == NULL)
b34976b6 13753 return FALSE;
721956f4 13754 }
ee75fd95 13755 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13756 {
13757 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13758 htab->relbrlt->size);
63bc6f6c
AM
13759 if (htab->relbrlt->contents == NULL)
13760 return FALSE;
13761 }
5d1634d7 13762
721956f4
AM
13763 /* Build the stubs as directed by the stub hash table. */
13764 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13765
a4b6fadd
AM
13766 for (group = htab->group; group != NULL; group = group->next)
13767 if (group->needs_save_res)
7dda8d3c 13768 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 13769
aa8a7074
AM
13770 if (htab->relbrlt != NULL)
13771 htab->relbrlt->reloc_count = 0;
13772
e7d1c40c 13773 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13774 for (group = htab->group; group != NULL; group = group->next)
13775 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13776 {
13777 int align = abs (htab->params->plt_stub_align);
13778 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13779 }
794e51c0 13780
7dda8d3c
AM
13781 for (group = htab->group; group != NULL; group = group->next)
13782 if (group->needs_save_res)
13783 {
13784 stub_sec = group->stub_sec;
13785 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13786 htab->sfpr->contents, htab->sfpr->size);
13787 if (htab->params->emit_stub_syms)
13788 {
13789 unsigned int i;
13790
13791 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13792 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13793 return FALSE;
13794 }
13795 }
13796
d4aaa2a0
AM
13797 for (group = htab->group; group != NULL; group = group->next)
13798 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
13799 {
13800 stub_sec_count += 1;
c9301e31
AM
13801 if (stub_sec->rawsize != stub_sec->size
13802 && (htab->stub_iteration <= STUB_SHRINK_ITER
13803 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13804 break;
13805 }
5d1634d7 13806
25516cc5 13807 if (group != NULL)
5d1634d7 13808 {
b34976b6 13809 htab->stub_error = TRUE;
cf97bcb0 13810 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 13811 }
721956f4 13812
d2a300cf
AM
13813 if (htab->stub_error)
13814 return FALSE;
13815
13816 if (stats != NULL)
13817 {
db84fff3 13818 size_t len;
d2a300cf
AM
13819 *stats = bfd_malloc (500);
13820 if (*stats == NULL)
13821 return FALSE;
13822
db84fff3
AM
13823 len = sprintf (*stats,
13824 ngettext ("linker stubs in %u group\n",
13825 "linker stubs in %u groups\n",
13826 stub_sec_count),
13827 stub_sec_count);
13828 sprintf (*stats + len, _(" branch %lu\n"
13829 " toc adjust %lu\n"
13830 " long branch %lu\n"
13831 " long toc adj %lu\n"
13832 " plt call %lu\n"
13833 " plt call toc %lu\n"
13834 " global entry %lu"),
4ce794b7
AM
13835 htab->stub_count[ppc_stub_long_branch - 1],
13836 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13837 htab->stub_count[ppc_stub_plt_branch - 1],
13838 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13839 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13840 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13841 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13842 }
13843 return TRUE;
5bd4f169
AM
13844}
13845
60124e18
AM
13846/* What to do when ld finds relocations against symbols defined in
13847 discarded sections. */
13848
13849static unsigned int
13850ppc64_elf_action_discarded (asection *sec)
13851{
13852 if (strcmp (".opd", sec->name) == 0)
13853 return 0;
13854
13855 if (strcmp (".toc", sec->name) == 0)
13856 return 0;
13857
bce50a28
JJ
13858 if (strcmp (".toc1", sec->name) == 0)
13859 return 0;
13860
60124e18
AM
13861 return _bfd_elf_default_action_discarded (sec);
13862}
13863
5bd4f169
AM
13864/* The RELOCATE_SECTION function is called by the ELF backend linker
13865 to handle the relocations for a section.
13866
13867 The relocs are always passed as Rela structures; if the section
13868 actually uses Rel structures, the r_addend field will always be
13869 zero.
13870
13871 This function is responsible for adjust the section contents as
13872 necessary, and (if using Rela relocs and generating a
1049f94e 13873 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13874 necessary.
13875
13876 This function does not have to worry about setting the reloc
13877 address or the reloc symbol index.
13878
13879 LOCAL_SYMS is a pointer to the swapped in local symbols.
13880
13881 LOCAL_SECTIONS is an array giving the section in the input file
13882 corresponding to the st_shndx field of each local symbol.
13883
13884 The global hash table entry for the global symbols can be found
13885 via elf_sym_hashes (input_bfd).
13886
1049f94e 13887 When generating relocatable output, this function must handle
5bd4f169
AM
13888 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13889 going to be the section symbol corresponding to the output
13890 section, which means that the addend must be adjusted
13891 accordingly. */
13892
b34976b6 13893static bfd_boolean
4ce794b7
AM
13894ppc64_elf_relocate_section (bfd *output_bfd,
13895 struct bfd_link_info *info,
13896 bfd *input_bfd,
13897 asection *input_section,
13898 bfd_byte *contents,
13899 Elf_Internal_Rela *relocs,
13900 Elf_Internal_Sym *local_syms,
13901 asection **local_sections)
5bd4f169 13902{
65f38f15 13903 struct ppc_link_hash_table *htab;
5bd4f169
AM
13904 Elf_Internal_Shdr *symtab_hdr;
13905 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13906 Elf_Internal_Rela *rel;
c316a17c 13907 Elf_Internal_Rela *wrel;
5bd4f169 13908 Elf_Internal_Rela *relend;
411e1bfb
AM
13909 Elf_Internal_Rela outrel;
13910 bfd_byte *loc;
411e1bfb 13911 struct got_entry **local_got_ents;
5bd4f169 13912 bfd_vma TOCstart;
b34976b6
AM
13913 bfd_boolean ret = TRUE;
13914 bfd_boolean is_opd;
794e51c0
AM
13915 /* Assume 'at' branch hints. */
13916 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13917 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13918
65f38f15 13919 /* Initialize howto table if needed. */
5bd4f169 13920 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13921 ppc_howto_init ();
13922
65f38f15 13923 htab = ppc_hash_table (info);
4dfe6ac6
NC
13924 if (htab == NULL)
13925 return FALSE;
ee75fd95
AM
13926
13927 /* Don't relocate stub sections. */
e7d1c40c 13928 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13929 return TRUE;
13930
0c8d6e5c 13931 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13932
411e1bfb 13933 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13934 TOCstart = elf_gp (output_bfd);
0ffa91dd 13935 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13936 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13937 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13938
c316a17c 13939 rel = wrel = relocs;
5bd4f169 13940 relend = relocs + input_section->reloc_count;
c316a17c 13941 for (; rel < relend; wrel++, rel++)
5bd4f169 13942 {
04c9666a 13943 enum elf_ppc64_reloc_type r_type;
31c76678 13944 bfd_vma addend;
5bd4f169
AM
13945 bfd_reloc_status_type r;
13946 Elf_Internal_Sym *sym;
13947 asection *sec;
039b3fef
AM
13948 struct elf_link_hash_entry *h_elf;
13949 struct ppc_link_hash_entry *h;
13950 struct ppc_link_hash_entry *fdh;
5bd4f169 13951 const char *sym_name;
0d4792f7 13952 unsigned long r_symndx, toc_symndx;
3a71aa26 13953 bfd_vma toc_addend;
f961d9dd
AM
13954 unsigned char tls_mask, tls_gd, tls_type;
13955 unsigned char sym_type;
5bd4f169 13956 bfd_vma relocation;
23cedd1d 13957 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 13958 bfd_boolean warned;
bc30df16 13959 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13960 unsigned int insn;
e11840f9 13961 unsigned int mask;
721956f4
AM
13962 struct ppc_stub_hash_entry *stub_entry;
13963 bfd_vma max_br_offset;
13964 bfd_vma from;
c316a17c 13965 Elf_Internal_Rela orig_rel;
b80eed39
AM
13966 reloc_howto_type *howto;
13967 struct reloc_howto_struct alt_howto;
5bd4f169 13968
c316a17c
AM
13969 again:
13970 orig_rel = *rel;
13971
4ce794b7 13972 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13973 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13974
13975 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13976 symbol of the previous ADDR64 reloc. The symbol gives us the
13977 proper TOC base to use. */
13978 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13979 && wrel != relocs
13980 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13981 && is_opd)
c316a17c 13982 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13983
4ce794b7
AM
13984 sym = NULL;
13985 sec = NULL;
039b3fef 13986 h_elf = NULL;
4ce794b7 13987 sym_name = NULL;
b34976b6
AM
13988 unresolved_reloc = FALSE;
13989 warned = FALSE;
65f38f15 13990
0b13192e 13991 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13992 {
13993 /* It's a local symbol. */
74f0fb50 13994 struct _opd_sec_data *opd;
4025353c 13995
5bd4f169
AM
13996 sym = local_syms + r_symndx;
13997 sec = local_sections[r_symndx];
26c61ae5 13998 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13999 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 14000 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
14001 opd = get_opd_info (sec);
14002 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 14003 {
51aecdc5
AM
14004 long adjust = opd->adjust[OPD_NDX (sym->st_value
14005 + rel->r_addend)];
4025353c
AM
14006 if (adjust == -1)
14007 relocation = 0;
14008 else
4cc603a5
AM
14009 {
14010 /* If this is a relocation against the opd section sym
14011 and we have edited .opd, adjust the reloc addend so
14012 that ld -r and ld --emit-relocs output is correct.
14013 If it is a reloc against some other .opd symbol,
14014 then the symbol value will be adjusted later. */
14015 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14016 rel->r_addend += adjust;
14017 else
14018 relocation += adjust;
14019 }
1e2f5b6e 14020 }
5bd4f169
AM
14021 }
14022 else
14023 {
62d887d4
L
14024 bfd_boolean ignored;
14025
b2a8e766
AM
14026 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14027 r_symndx, symtab_hdr, sym_hashes,
039b3fef 14028 h_elf, sec, relocation,
62d887d4 14029 unresolved_reloc, warned, ignored);
039b3fef
AM
14030 sym_name = h_elf->root.root.string;
14031 sym_type = h_elf->type;
b69fdb4e
AM
14032 if (sec != NULL
14033 && sec->owner == output_bfd
14034 && strcmp (sec->name, ".opd") == 0)
14035 {
14036 /* This is a symbol defined in a linker script. All
14037 such are defined in output sections, even those
14038 defined by simple assignment from a symbol defined in
14039 an input section. Transfer the symbol to an
14040 appropriate input .opd section, so that a branch to
14041 this symbol will be mapped to the location specified
14042 by the opd entry. */
14043 struct bfd_link_order *lo;
14044 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14045 if (lo->type == bfd_indirect_link_order)
14046 {
14047 asection *isec = lo->u.indirect.section;
14048 if (h_elf->root.u.def.value >= isec->output_offset
14049 && h_elf->root.u.def.value < (isec->output_offset
14050 + isec->size))
14051 {
14052 h_elf->root.u.def.value -= isec->output_offset;
14053 h_elf->root.u.def.section = isec;
14054 sec = isec;
14055 break;
14056 }
14057 }
14058 }
5bd4f169 14059 }
039b3fef 14060 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 14061
dbaa2011 14062 if (sec != NULL && discarded_section (sec))
c316a17c
AM
14063 {
14064 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14065 input_bfd, input_section,
14066 contents + rel->r_offset);
14067 wrel->r_offset = rel->r_offset;
14068 wrel->r_info = 0;
14069 wrel->r_addend = 0;
14070
14071 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 14072 symbols defined in discarded sections. Not done for
c316a17c
AM
14073 non-debug to preserve relocs in .eh_frame which the
14074 eh_frame editing code expects to be present. */
14075 if (bfd_link_relocatable (info)
14076 && (input_section->flags & SEC_DEBUGGING))
14077 wrel--;
14078
14079 continue;
14080 }
ab96bf03 14081
0e1862bb 14082 if (bfd_link_relocatable (info))
c316a17c 14083 goto copy_reloc;
ab96bf03 14084
f40da81b
AM
14085 if (h != NULL && &h->elf == htab->elf.hgot)
14086 {
6f20ed8a 14087 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
14088 sec = bfd_abs_section_ptr;
14089 unresolved_reloc = FALSE;
14090 }
14091
951fd09b
AM
14092 /* TLS optimizations. Replace instruction sequences and relocs
14093 based on information we collected in tls_optimize. We edit
14094 RELOCS so that --emit-relocs will output something sensible
14095 for the final instruction stream. */
14096 tls_mask = 0;
14097 tls_gd = 0;
0d4792f7 14098 toc_symndx = 0;
727fc41e
AM
14099 if (h != NULL)
14100 tls_mask = h->tls_mask;
14101 else if (local_got_ents != NULL)
411e1bfb 14102 {
e054468f
AM
14103 struct plt_entry **local_plt = (struct plt_entry **)
14104 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 14105 unsigned char *lgot_masks = (unsigned char *)
e054468f 14106 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
14107 tls_mask = lgot_masks[r_symndx];
14108 }
37da22e5 14109 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
14110 && (r_type == R_PPC64_TLS
14111 || r_type == R_PPC64_TLSGD
14112 || r_type == R_PPC64_TLSLD))
14113 {
14114 /* Check for toc tls entries. */
f961d9dd 14115 unsigned char *toc_tls;
0d4792f7 14116
727fc41e
AM
14117 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14118 &local_syms, rel, input_bfd))
14119 return FALSE;
0d4792f7 14120
727fc41e
AM
14121 if (toc_tls)
14122 tls_mask = *toc_tls;
0d4792f7
AM
14123 }
14124
14125 /* Check that tls relocs are used with tls syms, and non-tls
14126 relocs are used with non-tls syms. */
cf35638d 14127 if (r_symndx != STN_UNDEF
0d4792f7
AM
14128 && r_type != R_PPC64_NONE
14129 && (h == NULL
039b3fef
AM
14130 || h->elf.root.type == bfd_link_hash_defined
14131 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
14132 && (IS_PPC64_TLS_RELOC (r_type)
14133 != (sym_type == STT_TLS
14134 || (sym_type == STT_SECTION
14135 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 14136 {
37da22e5 14137 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
14138 && (r_type == R_PPC64_TLS
14139 || r_type == R_PPC64_TLSGD
14140 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
14141 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14142 ;
14143 else
25f53a85 14144 info->callbacks->einfo
1d483afe 14145 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 14146 /* xgettext:c-format */
c1c8c1ef 14147 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 14148 /* xgettext:c-format */
c1c8c1ef 14149 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 14150 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
14151 ppc64_elf_howto_table[r_type]->name,
14152 sym_name);
411e1bfb
AM
14153 }
14154
14155 /* Ensure reloc mapping code below stays sane. */
14156 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14157 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14158 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14159 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14160 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14161 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14162 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14163 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14164 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14165 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14166 abort ();
0d4792f7 14167
411e1bfb
AM
14168 switch (r_type)
14169 {
14170 default:
411e1bfb
AM
14171 break;
14172
ba761f19 14173 case R_PPC64_LO_DS_OPT:
95f0d0d2 14174 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
14175 if ((insn & (0x3f << 26)) != 58u << 26)
14176 abort ();
14177 insn += (14u << 26) - (58u << 26);
95f0d0d2 14178 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
14179 r_type = R_PPC64_TOC16_LO;
14180 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14181 break;
14182
411e1bfb
AM
14183 case R_PPC64_TOC16:
14184 case R_PPC64_TOC16_LO:
14185 case R_PPC64_TOC16_DS:
14186 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
14187 {
14188 /* Check for toc tls entries. */
f961d9dd 14189 unsigned char *toc_tls;
951fd09b 14190 int retval;
411e1bfb 14191
3a71aa26
AM
14192 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14193 &local_syms, rel, input_bfd);
951fd09b 14194 if (retval == 0)
411e1bfb
AM
14195 return FALSE;
14196
14197 if (toc_tls)
14198 {
951fd09b 14199 tls_mask = *toc_tls;
411e1bfb
AM
14200 if (r_type == R_PPC64_TOC16_DS
14201 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 14202 {
37da22e5 14203 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
14204 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14205 goto toctprel;
14206 }
411e1bfb 14207 else
951fd09b
AM
14208 {
14209 /* If we found a GD reloc pair, then we might be
14210 doing a GD->IE transition. */
14211 if (retval == 2)
14212 {
14213 tls_gd = TLS_TPRELGD;
37da22e5
AM
14214 if ((tls_mask & TLS_TLS) != 0
14215 && (tls_mask & TLS_GD) == 0)
102890f0 14216 goto tls_ldgd_opt;
951fd09b
AM
14217 }
14218 else if (retval == 3)
14219 {
37da22e5
AM
14220 if ((tls_mask & TLS_TLS) != 0
14221 && (tls_mask & TLS_LD) == 0)
102890f0 14222 goto tls_ldgd_opt;
951fd09b
AM
14223 }
14224 }
411e1bfb
AM
14225 }
14226 }
14227 break;
14228
9d6ded02
AM
14229 case R_PPC64_GOT_TPREL16_HI:
14230 case R_PPC64_GOT_TPREL16_HA:
37da22e5 14231 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
14232 && (tls_mask & TLS_TPREL) == 0)
14233 {
14234 rel->r_offset -= d_offset;
95f0d0d2 14235 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
14236 r_type = R_PPC64_NONE;
14237 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14238 }
14239 break;
14240
411e1bfb
AM
14241 case R_PPC64_GOT_TPREL16_DS:
14242 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 14243 if ((tls_mask & TLS_TLS) != 0
951fd09b 14244 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14245 {
81407a69 14246 toctprel:
95f0d0d2 14247 insn = bfd_get_32 (input_bfd,
c316a17c 14248 contents + rel->r_offset - d_offset);
411e1bfb
AM
14249 insn &= 31 << 21;
14250 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 14251 bfd_put_32 (input_bfd, insn,
c316a17c 14252 contents + rel->r_offset - d_offset);
411e1bfb 14253 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
14254 if (toc_symndx != 0)
14255 {
14256 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14257 rel->r_addend = toc_addend;
0d4792f7
AM
14258 /* We changed the symbol. Start over in order to
14259 get h, sym, sec etc. right. */
c316a17c 14260 goto again;
0d4792f7
AM
14261 }
14262 else
14263 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14264 }
14265 break;
14266
14267 case R_PPC64_TLS:
37da22e5 14268 if ((tls_mask & TLS_TLS) != 0
951fd09b 14269 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 14270 {
95f0d0d2 14271 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
14272 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14273 if (insn == 0)
411e1bfb 14274 abort ();
95f0d0d2 14275 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 14276 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
14277 PPC64_TPREL16_LO which is at low-order half-word. */
14278 rel->r_offset += d_offset;
0d4792f7
AM
14279 r_type = R_PPC64_TPREL16_LO;
14280 if (toc_symndx != 0)
14281 {
14282 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 14283 rel->r_addend = toc_addend;
0d4792f7
AM
14284 /* We changed the symbol. Start over in order to
14285 get h, sym, sec etc. right. */
c316a17c 14286 goto again;
0d4792f7
AM
14287 }
14288 else
14289 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14290 }
14291 break;
14292
411e1bfb
AM
14293 case R_PPC64_GOT_TLSGD16_HI:
14294 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14295 tls_gd = TLS_TPRELGD;
37da22e5 14296 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
14297 goto tls_gdld_hi;
14298 break;
14299
411e1bfb
AM
14300 case R_PPC64_GOT_TLSLD16_HI:
14301 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 14302 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 14303 {
951fd09b
AM
14304 tls_gdld_hi:
14305 if ((tls_mask & tls_gd) != 0)
14306 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14307 + R_PPC64_GOT_TPREL16_DS);
14308 else
411e1bfb 14309 {
4fe5ca5b 14310 rel->r_offset -= d_offset;
95f0d0d2 14311 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 14312 r_type = R_PPC64_NONE;
411e1bfb 14313 }
951fd09b 14314 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
14315 }
14316 break;
14317
951fd09b
AM
14318 case R_PPC64_GOT_TLSGD16:
14319 case R_PPC64_GOT_TLSGD16_LO:
14320 tls_gd = TLS_TPRELGD;
37da22e5 14321 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 14322 goto tls_ldgd_opt;
951fd09b 14323 break;
411e1bfb 14324
951fd09b
AM
14325 case R_PPC64_GOT_TLSLD16:
14326 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 14327 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 14328 {
b9f04fe0 14329 unsigned int insn1, insn2;
102890f0
AM
14330 bfd_vma offset;
14331
14332 tls_ldgd_opt:
727fc41e
AM
14333 offset = (bfd_vma) -1;
14334 /* If not using the newer R_PPC64_TLSGD/LD to mark
14335 __tls_get_addr calls, we must trust that the call
14336 stays with its arg setup insns, ie. that the next
14337 reloc is the __tls_get_addr call associated with
14338 the current reloc. Edit both insns. */
14339 if (input_section->has_tls_get_addr_call
14340 && rel + 1 < relend
14341 && branch_reloc_hash_match (input_bfd, rel + 1,
14342 htab->tls_get_addr,
14343 htab->tls_get_addr_fd))
14344 offset = rel[1].r_offset;
b86ac8e3
AM
14345 /* We read the low GOT_TLS (or TOC16) insn because we
14346 need to keep the destination reg. It may be
14347 something other than the usual r3, and moved to r3
14348 before the call by intervening code. */
95f0d0d2 14349 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 14350 contents + rel->r_offset - d_offset);
102890f0 14351 if ((tls_mask & tls_gd) != 0)
411e1bfb 14352 {
102890f0 14353 /* IE */
b86ac8e3 14354 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
14355 insn1 |= 58 << 26; /* ld */
14356 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 14357 if (offset != (bfd_vma) -1)
f58d5a2d 14358 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
14359 if ((tls_mask & TLS_EXPLICIT) == 0)
14360 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14361 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 14362 else
102890f0
AM
14363 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14364 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14365 }
14366 else
14367 {
14368 /* LE */
b86ac8e3
AM
14369 insn1 &= 0x1f << 21;
14370 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
14371 insn2 = 0x38630000; /* addi 3,3,0 */
14372 if (tls_gd == 0)
951fd09b 14373 {
102890f0 14374 /* Was an LD reloc. */
1d483afe
AM
14375 if (toc_symndx)
14376 sec = local_sections[toc_symndx];
14377 for (r_symndx = 0;
14378 r_symndx < symtab_hdr->sh_info;
14379 r_symndx++)
14380 if (local_sections[r_symndx] == sec)
14381 break;
14382 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14383 r_symndx = STN_UNDEF;
102890f0 14384 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14385 if (r_symndx != STN_UNDEF)
1d483afe
AM
14386 rel->r_addend -= (local_syms[r_symndx].st_value
14387 + sec->output_offset
14388 + sec->output_section->vma);
951fd09b 14389 }
102890f0 14390 else if (toc_symndx != 0)
3a71aa26
AM
14391 {
14392 r_symndx = toc_symndx;
14393 rel->r_addend = toc_addend;
14394 }
102890f0
AM
14395 r_type = R_PPC64_TPREL16_HA;
14396 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
14397 if (offset != (bfd_vma) -1)
14398 {
14399 rel[1].r_info = ELF64_R_INFO (r_symndx,
14400 R_PPC64_TPREL16_LO);
14401 rel[1].r_offset = offset + d_offset;
14402 rel[1].r_addend = rel->r_addend;
14403 }
102890f0 14404 }
95f0d0d2 14405 bfd_put_32 (input_bfd, insn1,
3a71aa26 14406 contents + rel->r_offset - d_offset);
727fc41e 14407 if (offset != (bfd_vma) -1)
b9f04fe0 14408 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
14409 if ((tls_mask & tls_gd) == 0
14410 && (tls_gd == 0 || toc_symndx != 0))
14411 {
14412 /* We changed the symbol. Start over in order
14413 to get h, sym, sec etc. right. */
c316a17c 14414 goto again;
727fc41e
AM
14415 }
14416 }
14417 break;
14418
14419 case R_PPC64_TLSGD:
37da22e5 14420 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 14421 && rel + 1 < relend)
727fc41e 14422 {
b9f04fe0 14423 unsigned int insn2;
727fc41e
AM
14424 bfd_vma offset = rel->r_offset;
14425
23cedd1d
AM
14426 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14427 {
14428 bfd_put_32 (output_bfd, NOP, contents + offset);
14429 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14430 break;
14431 }
14432
14433 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14434 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14435
727fc41e
AM
14436 if ((tls_mask & TLS_TPRELGD) != 0)
14437 {
14438 /* IE */
14439 r_type = R_PPC64_NONE;
14440 insn2 = 0x7c636a14; /* add 3,3,13 */
14441 }
14442 else
14443 {
14444 /* LE */
14445 if (toc_symndx != 0)
14446 {
14447 r_symndx = toc_symndx;
14448 rel->r_addend = toc_addend;
14449 }
14450 r_type = R_PPC64_TPREL16_LO;
14451 rel->r_offset = offset + d_offset;
14452 insn2 = 0x38630000; /* addi 3,3,0 */
14453 }
14454 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14455 /* Zap the reloc on the _tls_get_addr call too. */
14456 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14457 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 14458 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 14459 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 14460 goto again;
411e1bfb 14461 }
411e1bfb
AM
14462 break;
14463
727fc41e 14464 case R_PPC64_TLSLD:
37da22e5 14465 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 14466 && rel + 1 < relend)
727fc41e 14467 {
b9f04fe0 14468 unsigned int insn2;
727fc41e
AM
14469 bfd_vma offset = rel->r_offset;
14470
23cedd1d
AM
14471 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14472 {
14473 bfd_put_32 (output_bfd, NOP, contents + offset);
14474 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14475 break;
14476 }
14477
14478 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14479 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14480
727fc41e
AM
14481 if (toc_symndx)
14482 sec = local_sections[toc_symndx];
14483 for (r_symndx = 0;
14484 r_symndx < symtab_hdr->sh_info;
14485 r_symndx++)
14486 if (local_sections[r_symndx] == sec)
14487 break;
14488 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 14489 r_symndx = STN_UNDEF;
727fc41e 14490 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 14491 if (r_symndx != STN_UNDEF)
727fc41e
AM
14492 rel->r_addend -= (local_syms[r_symndx].st_value
14493 + sec->output_offset
14494 + sec->output_section->vma);
14495
14496 r_type = R_PPC64_TPREL16_LO;
14497 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14498 rel->r_offset = offset + d_offset;
14499 /* Zap the reloc on the _tls_get_addr call too. */
14500 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 14501 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 14502 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 14503 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 14504 goto again;
727fc41e
AM
14505 }
14506 break;
14507
411e1bfb 14508 case R_PPC64_DTPMOD64:
951fd09b
AM
14509 if (rel + 1 < relend
14510 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14511 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 14512 {
951fd09b
AM
14513 if ((tls_mask & TLS_GD) == 0)
14514 {
14515 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14516 if ((tls_mask & TLS_TPRELGD) != 0)
14517 r_type = R_PPC64_TPREL64;
14518 else
14519 {
4ce794b7 14520 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
14521 r_type = R_PPC64_NONE;
14522 }
14523 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14524 }
14525 }
14526 else
14527 {
14528 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14529 {
4ce794b7 14530 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14531 r_type = R_PPC64_NONE;
951fd09b 14532 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14533 }
411e1bfb
AM
14534 }
14535 break;
14536
14537 case R_PPC64_TPREL64:
951fd09b 14538 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14539 {
14540 r_type = R_PPC64_NONE;
14541 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14542 }
14543 break;
52a82034 14544
006589cf
AM
14545 case R_PPC64_ENTRY:
14546 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14547 if (!bfd_link_pic (info)
14548 && !info->traditional_format
14549 && relocation + 0x80008000 <= 0xffffffff)
14550 {
14551 unsigned int insn1, insn2;
14552
14553 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14554 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14555 if ((insn1 & ~0xfffc) == LD_R2_0R12
14556 && insn2 == ADD_R2_R2_R12)
14557 {
95f0d0d2 14558 bfd_put_32 (input_bfd,
006589cf
AM
14559 LIS_R2 + PPC_HA (relocation),
14560 contents + rel->r_offset);
95f0d0d2 14561 bfd_put_32 (input_bfd,
006589cf
AM
14562 ADDI_R2_R2 + PPC_LO (relocation),
14563 contents + rel->r_offset + 4);
14564 }
14565 }
14566 else
14567 {
14568 relocation -= (rel->r_offset
14569 + input_section->output_offset
14570 + input_section->output_section->vma);
14571 if (relocation + 0x80008000 <= 0xffffffff)
14572 {
14573 unsigned int insn1, insn2;
14574
14575 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14576 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14577 if ((insn1 & ~0xfffc) == LD_R2_0R12
14578 && insn2 == ADD_R2_R2_R12)
14579 {
95f0d0d2 14580 bfd_put_32 (input_bfd,
006589cf
AM
14581 ADDIS_R2_R12 + PPC_HA (relocation),
14582 contents + rel->r_offset);
95f0d0d2 14583 bfd_put_32 (input_bfd,
006589cf
AM
14584 ADDI_R2_R2 + PPC_LO (relocation),
14585 contents + rel->r_offset + 4);
14586 }
14587 }
14588 }
14589 break;
14590
52a82034
AM
14591 case R_PPC64_REL16_HA:
14592 /* If we are generating a non-PIC executable, edit
14593 . 0: addis 2,12,.TOC.-0b@ha
14594 . addi 2,2,.TOC.-0b@l
14595 used by ELFv2 global entry points to set up r2, to
14596 . lis 2,.TOC.@ha
14597 . addi 2,2,.TOC.@l
14598 if .TOC. is in range. */
0e1862bb 14599 if (!bfd_link_pic (info)
810d4e75 14600 && !info->traditional_format
006589cf 14601 && !htab->opd_abi
4f038ee5 14602 && rel->r_addend == d_offset
52a82034
AM
14603 && h != NULL && &h->elf == htab->elf.hgot
14604 && rel + 1 < relend
14605 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14606 && rel[1].r_offset == rel->r_offset + 4
14607 && rel[1].r_addend == rel->r_addend + 4
14608 && relocation + 0x80008000 <= 0xffffffff)
14609 {
14610 unsigned int insn1, insn2;
14611 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14612 insn1 = bfd_get_32 (input_bfd, contents + offset);
14613 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14614 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14615 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14616 {
14617 r_type = R_PPC64_ADDR16_HA;
14618 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14619 rel->r_addend -= d_offset;
14620 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14621 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14622 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14623 }
14624 }
14625 break;
411e1bfb
AM
14626 }
14627
14628 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14629 insn = 0;
b25116a9
AM
14630 max_br_offset = 1 << 25;
14631 addend = rel->r_addend;
bc30df16 14632 reloc_dest = DEST_NORMAL;
65f38f15 14633 switch (r_type)
5bd4f169
AM
14634 {
14635 default:
65f38f15 14636 break;
5bd4f169 14637
3b421ab3
AM
14638 case R_PPC64_TOCSAVE:
14639 if (relocation + addend == (rel->r_offset
14640 + input_section->output_offset
14641 + input_section->output_section->vma)
14642 && tocsave_find (htab, NO_INSERT,
14643 &local_syms, rel, input_bfd))
14644 {
14645 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14646 if (insn == NOP
14647 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14648 bfd_put_32 (input_bfd,
14649 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14650 contents + rel->r_offset);
14651 }
14652 break;
14653
65f38f15
AM
14654 /* Branch taken prediction relocations. */
14655 case R_PPC64_ADDR14_BRTAKEN:
14656 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14657 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14658 /* Fall through. */
65f38f15 14659
86c76c7b 14660 /* Branch not taken prediction relocations. */
65f38f15
AM
14661 case R_PPC64_ADDR14_BRNTAKEN:
14662 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14663 insn |= bfd_get_32 (input_bfd,
411e1bfb 14664 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14665 /* Fall through. */
86c76c7b 14666
b25116a9
AM
14667 case R_PPC64_REL14:
14668 max_br_offset = 1 << 15;
1a0670f3 14669 /* Fall through. */
5bd4f169 14670
65f38f15 14671 case R_PPC64_REL24:
23cedd1d 14672 case R_PPC64_PLTCALL:
ad8e1ba5
AM
14673 /* Calls to functions with a different TOC, such as calls to
14674 shared objects, need to alter the TOC pointer. This is
14675 done using a linkage stub. A REL24 branching to these
14676 linkage stubs needs to be followed by a nop, as the nop
14677 will be replaced with an instruction to restore the TOC
14678 base pointer. */
8387904d 14679 fdh = h;
b31867b6
AM
14680 if (h != NULL
14681 && h->oh != NULL
14682 && h->oh->is_func_descriptor)
14683 fdh = ppc_follow_link (h->oh);
31c76678
DK
14684 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14685 htab);
23cedd1d
AM
14686 if (r_type == R_PPC64_PLTCALL
14687 && stub_entry != NULL
14688 && (stub_entry->stub_type == ppc_stub_plt_call
14689 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14690 stub_entry = NULL;
14691
6abec6bc 14692 if (stub_entry != NULL
ad8e1ba5 14693 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 14694 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
14695 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14696 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 14697 {
b25116a9 14698 bfd_boolean can_plt_call = FALSE;
721956f4 14699
6e1816be
AM
14700 if (stub_entry->stub_type == ppc_stub_plt_call
14701 && !htab->opd_abi
14702 && htab->params->plt_localentry0 != 0
14703 && is_elfv2_localentry0 (&h->elf))
14704 {
14705 /* The function doesn't use or change r2. */
14706 can_plt_call = TRUE;
14707 }
14708
f378ab09 14709 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
14710 branch and link followed by a nop. The nop is
14711 replaced by an insn to restore r2. */
6e1816be 14712 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14713 {
ba8ca3e7
AM
14714 unsigned long br;
14715
14716 br = bfd_get_32 (input_bfd,
14717 contents + rel->r_offset);
14718 if ((br & 1) != 0)
41bd81ab 14719 {
ba8ca3e7
AM
14720 unsigned long nop;
14721
14722 nop = bfd_get_32 (input_bfd,
14723 contents + rel->r_offset + 4);
23cedd1d
AM
14724 if (nop == LD_R2_0R1 + STK_TOC (htab))
14725 can_plt_call = TRUE;
14726 else if (nop == NOP
14727 || nop == CROR_151515
14728 || nop == CROR_313131)
a7f2871e 14729 {
ba8ca3e7
AM
14730 if (h != NULL
14731 && (h == htab->tls_get_addr_fd
14732 || h == htab->tls_get_addr)
7c9cf415 14733 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14734 {
14735 /* Special stub used, leave nop alone. */
14736 }
14737 else
a078d95a
AM
14738 bfd_put_32 (input_bfd,
14739 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14740 contents + rel->r_offset + 4);
14741 can_plt_call = TRUE;
a7f2871e 14742 }
41bd81ab 14743 }
5bd4f169 14744 }
721956f4 14745
ba8ca3e7 14746 if (!can_plt_call && h != NULL)
721956f4 14747 {
ba8ca3e7
AM
14748 const char *name = h->elf.root.root.string;
14749
14750 if (*name == '.')
14751 ++name;
14752
14753 if (strncmp (name, "__libc_start_main", 17) == 0
14754 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14755 {
ba8ca3e7
AM
14756 /* Allow crt1 branch to go via a toc adjusting
14757 stub. Other calls that never return could do
14758 the same, if we could detect such. */
b25116a9 14759 can_plt_call = TRUE;
6ab189d5 14760 }
ba8ca3e7
AM
14761 }
14762
14763 if (!can_plt_call)
14764 {
14765 /* g++ as of 20130507 emits self-calls without a
14766 following nop. This is arguably wrong since we
14767 have conflicting information. On the one hand a
14768 global symbol and on the other a local call
14769 sequence, but don't error for this special case.
14770 It isn't possible to cheaply verify we have
14771 exactly such a call. Allow all calls to the same
14772 section. */
14773 asection *code_sec = sec;
14774
14775 if (get_opd_info (sec) != NULL)
ad8e1ba5 14776 {
ba8ca3e7
AM
14777 bfd_vma off = (relocation + addend
14778 - sec->output_section->vma
14779 - sec->output_offset);
bc30df16 14780
ba8ca3e7 14781 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14782 }
ba8ca3e7
AM
14783 if (code_sec == input_section)
14784 can_plt_call = TRUE;
14785 }
14786
14787 if (!can_plt_call)
14788 {
4805fc55
AM
14789 if (stub_entry->stub_type == ppc_stub_plt_call
14790 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14791 info->callbacks->einfo
695344c0 14792 /* xgettext:c-format */
c1c8c1ef 14793 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
14794 "recompile with -fPIC\n"),
14795 input_bfd, input_section, rel->r_offset, sym_name);
14796 else
14797 info->callbacks->einfo
695344c0 14798 /* xgettext:c-format */
c1c8c1ef 14799 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
14800 "(-mcmodel=small toc adjust stub)\n"),
14801 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14802
14803 bfd_set_error (bfd_error_bad_value);
14804 ret = FALSE;
721956f4
AM
14805 }
14806
b25116a9 14807 if (can_plt_call
794e51c0
AM
14808 && (stub_entry->stub_type == ppc_stub_plt_call
14809 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14810 unresolved_reloc = FALSE;
14811 }
14812
6abec6bc
AM
14813 if ((stub_entry == NULL
14814 || stub_entry->stub_type == ppc_stub_long_branch
14815 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14816 && get_opd_info (sec) != NULL)
14817 {
14818 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14819 bfd_vma off = (relocation + addend
14820 - sec->output_section->vma
14821 - sec->output_offset);
aef36ac1 14822 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14823 if (dest != (bfd_vma) -1)
14824 {
14825 relocation = dest;
14826 addend = 0;
bc30df16 14827 reloc_dest = DEST_OPD;
8387904d
AM
14828 }
14829 }
14830
b25116a9
AM
14831 /* If the branch is out of reach we ought to have a long
14832 branch stub. */
14833 from = (rel->r_offset
14834 + input_section->output_offset
14835 + input_section->output_section->vma);
14836
6911b7dc
AM
14837 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14838 ? fdh->elf.other
14839 : sym->st_other);
14840
6abec6bc
AM
14841 if (stub_entry != NULL
14842 && (stub_entry->stub_type == ppc_stub_long_branch
14843 || stub_entry->stub_type == ppc_stub_plt_branch)
14844 && (r_type == R_PPC64_ADDR14_BRTAKEN
14845 || r_type == R_PPC64_ADDR14_BRNTAKEN
14846 || (relocation + addend - from + max_br_offset
14847 < 2 * max_br_offset)))
14848 /* Don't use the stub if this branch is in range. */
14849 stub_entry = NULL;
b25116a9
AM
14850
14851 if (stub_entry != NULL)
14852 {
14853 /* Munge up the value and addend so that we call the stub
14854 rather than the procedure directly. */
a4b6fadd
AM
14855 asection *stub_sec = stub_entry->group->stub_sec;
14856
14857 if (stub_entry->stub_type == ppc_stub_save_res)
14858 relocation += (stub_sec->output_offset
14859 + stub_sec->output_section->vma
14860 + stub_sec->size - htab->sfpr->size
14861 - htab->sfpr->output_offset
14862 - htab->sfpr->output_section->vma);
14863 else
14864 relocation = (stub_entry->stub_offset
14865 + stub_sec->output_offset
14866 + stub_sec->output_section->vma);
b25116a9 14867 addend = 0;
bc30df16 14868 reloc_dest = DEST_STUB;
3b421ab3 14869
954b63d4 14870 if ((stub_entry->stub_type == ppc_stub_plt_call
794e51c0
AM
14871 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14872 && (ALWAYS_EMIT_R2SAVE
14873 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14874 && rel + 1 < relend
14875 && rel[1].r_offset == rel->r_offset + 4
14876 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14877 relocation += 4;
b25116a9
AM
14878 }
14879
14880 if (insn != 0)
14881 {
794e51c0 14882 if (is_isa_v2)
721956f4 14883 {
b25116a9
AM
14884 /* Set 'a' bit. This is 0b00010 in BO field for branch
14885 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14886 for branch on CTR insns (BO == 1a00t or 1a01t). */
14887 if ((insn & (0x14 << 21)) == (0x04 << 21))
14888 insn |= 0x02 << 21;
14889 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14890 insn |= 0x08 << 21;
14891 else
14892 break;
14893 }
14894 else
14895 {
14896 /* Invert 'y' bit if not the default. */
4cc603a5 14897 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14898 insn ^= 0x01 << 21;
721956f4 14899 }
b25116a9 14900
95f0d0d2 14901 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14902 }
e86ce104 14903
06da1e8e
AM
14904 /* NOP out calls to undefined weak functions.
14905 We can thus call a weak function without first
14906 checking whether the function is defined. */
b25116a9 14907 else if (h != NULL
039b3fef 14908 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14909 && h->elf.dynindx == -1
b25116a9
AM
14910 && r_type == R_PPC64_REL24
14911 && relocation == 0
4cc603a5 14912 && addend == 0)
e86ce104 14913 {
95f0d0d2 14914 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14915 goto copy_reloc;
e86ce104 14916 }
65f38f15
AM
14917 break;
14918 }
5bd4f169 14919
65f38f15 14920 /* Set `addend'. */
411e1bfb 14921 tls_type = 0;
23cedd1d 14922 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
14923 switch (r_type)
14924 {
14925 default:
cf97bcb0
AM
14926 /* xgettext:c-format */
14927 _bfd_error_handler (_("%pB: %s unsupported"),
14928 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 14929
65f38f15 14930 bfd_set_error (bfd_error_bad_value);
b34976b6 14931 ret = FALSE;
c316a17c 14932 goto copy_reloc;
5bd4f169 14933
65f38f15 14934 case R_PPC64_NONE:
411e1bfb 14935 case R_PPC64_TLS:
727fc41e
AM
14936 case R_PPC64_TLSGD:
14937 case R_PPC64_TLSLD:
3b421ab3 14938 case R_PPC64_TOCSAVE:
04c9666a
AM
14939 case R_PPC64_GNU_VTINHERIT:
14940 case R_PPC64_GNU_VTENTRY:
006589cf 14941 case R_PPC64_ENTRY:
c316a17c 14942 goto copy_reloc;
5bd4f169
AM
14943
14944 /* GOT16 relocations. Like an ADDR16 using the symbol's
14945 address in the GOT as relocation value instead of the
411e1bfb 14946 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14947 symbol and put the symbol value there. */
411e1bfb
AM
14948 case R_PPC64_GOT_TLSGD16:
14949 case R_PPC64_GOT_TLSGD16_LO:
14950 case R_PPC64_GOT_TLSGD16_HI:
14951 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14952 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14953 goto dogot;
14954
14955 case R_PPC64_GOT_TLSLD16:
14956 case R_PPC64_GOT_TLSLD16_LO:
14957 case R_PPC64_GOT_TLSLD16_HI:
14958 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14959 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14960 goto dogot;
14961
14962 case R_PPC64_GOT_TPREL16_DS:
14963 case R_PPC64_GOT_TPREL16_LO_DS:
14964 case R_PPC64_GOT_TPREL16_HI:
14965 case R_PPC64_GOT_TPREL16_HA:
14966 tls_type = TLS_TLS | TLS_TPREL;
14967 goto dogot;
14968
14969 case R_PPC64_GOT_DTPREL16_DS:
14970 case R_PPC64_GOT_DTPREL16_LO_DS:
14971 case R_PPC64_GOT_DTPREL16_HI:
14972 case R_PPC64_GOT_DTPREL16_HA:
14973 tls_type = TLS_TLS | TLS_DTPREL;
14974 goto dogot;
14975
65f38f15
AM
14976 case R_PPC64_GOT16:
14977 case R_PPC64_GOT16_LO:
14978 case R_PPC64_GOT16_HI:
14979 case R_PPC64_GOT16_HA:
14980 case R_PPC64_GOT16_DS:
14981 case R_PPC64_GOT16_LO_DS:
411e1bfb 14982 dogot:
5bd4f169
AM
14983 {
14984 /* Relocation is to the entry for this symbol in the global
14985 offset table. */
e717da7e 14986 asection *got;
d881513a 14987 bfd_vma *offp;
5bd4f169 14988 bfd_vma off;
d881513a 14989 unsigned long indx = 0;
927be08e 14990 struct got_entry *ent;
65f38f15 14991
d881513a
AM
14992 if (tls_type == (TLS_TLS | TLS_LD)
14993 && (h == NULL
f5385ebf 14994 || !h->elf.def_dynamic))
927be08e 14995 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14996 else
5bd4f169 14997 {
d881513a
AM
14998 if (h != NULL)
14999 {
f0158f44
AM
15000 if (!htab->elf.dynamic_sections_created
15001 || h->elf.dynindx == -1
15002 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 15003 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
15004 /* This is actually a static link, or it is a
15005 -Bsymbolic link and the symbol is defined
15006 locally, or the symbol was forced to be local
15007 because of a version file. */
15008 ;
15009 else
15010 {
039b3fef 15011 indx = h->elf.dynindx;
d881513a
AM
15012 unresolved_reloc = FALSE;
15013 }
039b3fef 15014 ent = h->elf.got.glist;
d881513a 15015 }
411e1bfb 15016 else
5bd4f169 15017 {
d881513a
AM
15018 if (local_got_ents == NULL)
15019 abort ();
15020 ent = local_got_ents[r_symndx];
5bd4f169 15021 }
d881513a
AM
15022
15023 for (; ent != NULL; ent = ent->next)
31c76678 15024 if (ent->addend == orig_rel.r_addend
e717da7e 15025 && ent->owner == input_bfd
d881513a
AM
15026 && ent->tls_type == tls_type)
15027 break;
5bd4f169 15028 }
411e1bfb 15029
927be08e
AM
15030 if (ent == NULL)
15031 abort ();
15032 if (ent->is_indirect)
15033 ent = ent->got.ent;
15034 offp = &ent->got.offset;
15035 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
15036 if (got == NULL)
15037 abort ();
15038
411e1bfb
AM
15039 /* The offset must always be a multiple of 8. We use the
15040 least significant bit to record whether we have already
15041 processed this entry. */
d881513a 15042 off = *offp;
411e1bfb
AM
15043 if ((off & 1) != 0)
15044 off &= ~1;
5bd4f169
AM
15045 else
15046 {
411e1bfb
AM
15047 /* Generate relocs for the dynamic linker, except in
15048 the case of TLSLD where we'll use one entry per
15049 module. */
25f23106
AM
15050 asection *relgot;
15051 bfd_boolean ifunc;
e717da7e 15052
d881513a 15053 *offp = off | 1;
25f23106
AM
15054 relgot = NULL;
15055 ifunc = (h != NULL
15056 ? h->elf.type == STT_GNU_IFUNC
15057 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 15058 if (ifunc)
82e66161
AM
15059 {
15060 relgot = htab->elf.irelplt;
15061 if (indx == 0)
15062 htab->local_ifunc_resolver = 1;
15063 else if (is_static_defined (&h->elf))
15064 htab->maybe_local_ifunc_resolver = 1;
15065 }
f0158f44
AM
15066 else if (indx != 0
15067 || (bfd_link_pic (info)
15068 && (h == NULL
21d68fcd 15069 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 15070 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
15071 && !h->elf.def_dynamic))
15072 && !(tls_type == (TLS_TLS | TLS_TPREL)
15073 && bfd_link_executable (info)
15074 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 15075 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 15076 if (relgot != NULL)
5bd4f169 15077 {
e717da7e
AM
15078 outrel.r_offset = (got->output_section->vma
15079 + got->output_offset
411e1bfb 15080 + off);
4cc603a5 15081 outrel.r_addend = addend;
d881513a 15082 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 15083 {
411e1bfb 15084 outrel.r_addend = 0;
e515b051 15085 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
15086 if (tls_type == (TLS_TLS | TLS_GD))
15087 {
e717da7e
AM
15088 loc = relgot->contents;
15089 loc += (relgot->reloc_count++
d881513a
AM
15090 * sizeof (Elf64_External_Rela));
15091 bfd_elf64_swap_reloca_out (output_bfd,
15092 &outrel, loc);
e515b051 15093 outrel.r_offset += 8;
4cc603a5 15094 outrel.r_addend = addend;
d881513a
AM
15095 outrel.r_info
15096 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 15097 }
411e1bfb 15098 }
951fd09b 15099 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 15100 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 15101 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 15102 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
15103 else if (indx != 0)
15104 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15105 else
81407a69 15106 {
25f23106
AM
15107 if (ifunc)
15108 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15109 else
15110 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
15111
15112 /* Write the .got section contents for the sake
15113 of prelink. */
e717da7e 15114 loc = got->contents + off;
23fbd6fa
JJ
15115 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15116 loc);
81407a69 15117 }
81407a69
AM
15118
15119 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
15120 {
15121 outrel.r_addend += relocation;
15122 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
15123 {
15124 if (htab->elf.tls_sec == NULL)
15125 outrel.r_addend = 0;
15126 else
15127 outrel.r_addend -= htab->elf.tls_sec->vma;
15128 }
e515b051 15129 }
e717da7e
AM
15130 loc = relgot->contents;
15131 loc += (relgot->reloc_count++
411e1bfb
AM
15132 * sizeof (Elf64_External_Rela));
15133 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15134 }
15135
ad8e1ba5 15136 /* Init the .got section contents here if we're not
81407a69 15137 emitting a reloc. */
d881513a 15138 else
411e1bfb 15139 {
4cc603a5 15140 relocation += addend;
f0158f44 15141 if (tls_type != 0)
411e1bfb 15142 {
989f9879
AM
15143 if (htab->elf.tls_sec == NULL)
15144 relocation = 0;
15145 else
15146 {
f0158f44
AM
15147 if (tls_type & TLS_LD)
15148 relocation = 0;
15149 else
15150 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 15151 if (tls_type & TLS_TPREL)
989f9879
AM
15152 relocation += DTP_OFFSET - TP_OFFSET;
15153 }
5bd4f169 15154
f0158f44 15155 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
15156 {
15157 bfd_put_64 (output_bfd, relocation,
e717da7e 15158 got->contents + off + 8);
676ee2b5 15159 relocation = 1;
7b609f53 15160 }
411e1bfb
AM
15161 }
15162 bfd_put_64 (output_bfd, relocation,
e717da7e 15163 got->contents + off);
5bd4f169
AM
15164 }
15165 }
15166
65f38f15
AM
15167 if (off >= (bfd_vma) -2)
15168 abort ();
15169
bf102f86 15170 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 15171 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 15172 }
65f38f15
AM
15173 break;
15174
15175 case R_PPC64_PLT16_HA:
15176 case R_PPC64_PLT16_HI:
15177 case R_PPC64_PLT16_LO:
08be3224 15178 case R_PPC64_PLT16_LO_DS:
65f38f15
AM
15179 case R_PPC64_PLT32:
15180 case R_PPC64_PLT64:
23cedd1d
AM
15181 case R_PPC64_PLTSEQ:
15182 case R_PPC64_PLTCALL:
65f38f15
AM
15183 /* Relocation is to the entry for this symbol in the
15184 procedure linkage table. */
23cedd1d 15185 unresolved_reloc = TRUE;
cbf95972
AM
15186 {
15187 struct plt_entry **plt_list = NULL;
15188 if (h != NULL)
15189 plt_list = &h->elf.plt.plist;
15190 else if (local_got_ents != NULL)
15191 {
15192 struct plt_entry **local_plt = (struct plt_entry **)
15193 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 15194 plt_list = local_plt + r_symndx;
cbf95972
AM
15195 }
15196 if (plt_list)
15197 {
15198 struct plt_entry *ent;
65f38f15 15199
cbf95972
AM
15200 for (ent = *plt_list; ent != NULL; ent = ent->next)
15201 if (ent->plt.offset != (bfd_vma) -1
15202 && ent->addend == orig_rel.r_addend)
15203 {
15204 asection *plt;
08be3224 15205 bfd_vma got;
cbf95972
AM
15206
15207 plt = htab->elf.splt;
15208 if (!htab->elf.dynamic_sections_created
15209 || h == NULL
15210 || h->elf.dynindx == -1)
2d7ad24e
AM
15211 {
15212 if (h != NULL
15213 ? h->elf.type == STT_GNU_IFUNC
15214 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15215 plt = htab->elf.iplt;
15216 else
15217 plt = htab->pltlocal;
15218 }
15219 relocation = (plt->output_section->vma
15220 + plt->output_offset
15221 + ent->plt.offset);
08be3224
AM
15222 if (r_type == R_PPC64_PLT16_HA
15223 || r_type ==R_PPC64_PLT16_HI
15224 || r_type ==R_PPC64_PLT16_LO
15225 || r_type ==R_PPC64_PLT16_LO_DS)
15226 {
15227 got = (elf_gp (output_bfd)
15228 + htab->sec_info[input_section->id].toc_off);
15229 relocation -= got;
15230 }
cbf95972
AM
15231 addend = 0;
15232 unresolved_reloc = FALSE;
15233 break;
15234 }
15235 }
15236 }
65f38f15 15237 break;
5bd4f169 15238
0b13192e
AM
15239 case R_PPC64_TOC:
15240 /* Relocation value is TOC base. */
15241 relocation = TOCstart;
cf35638d 15242 if (r_symndx == STN_UNDEF)
6f20ed8a 15243 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
15244 else if (unresolved_reloc)
15245 ;
6f20ed8a
AM
15246 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15247 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
15248 else
15249 unresolved_reloc = TRUE;
ab96bf03 15250 goto dodyn;
0b13192e 15251
5bd4f169
AM
15252 /* TOC16 relocs. We want the offset relative to the TOC base,
15253 which is the address of the start of the TOC plus 0x8000.
15254 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15255 in this order. */
65f38f15
AM
15256 case R_PPC64_TOC16:
15257 case R_PPC64_TOC16_LO:
15258 case R_PPC64_TOC16_HI:
15259 case R_PPC64_TOC16_DS:
15260 case R_PPC64_TOC16_LO_DS:
15261 case R_PPC64_TOC16_HA:
6f20ed8a 15262 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
15263 break;
15264
15265 /* Relocate against the beginning of the section. */
65f38f15
AM
15266 case R_PPC64_SECTOFF:
15267 case R_PPC64_SECTOFF_LO:
15268 case R_PPC64_SECTOFF_HI:
15269 case R_PPC64_SECTOFF_DS:
15270 case R_PPC64_SECTOFF_LO_DS:
15271 case R_PPC64_SECTOFF_HA:
4ce794b7 15272 if (sec != NULL)
65f38f15 15273 addend -= sec->output_section->vma;
5bd4f169
AM
15274 break;
15275
25f23106
AM
15276 case R_PPC64_REL16:
15277 case R_PPC64_REL16_LO:
15278 case R_PPC64_REL16_HI:
15279 case R_PPC64_REL16_HA:
a680de9a 15280 case R_PPC64_REL16DX_HA:
25f23106
AM
15281 break;
15282
721956f4
AM
15283 case R_PPC64_REL14:
15284 case R_PPC64_REL14_BRNTAKEN:
15285 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
15286 case R_PPC64_REL24:
15287 break;
15288
411e1bfb
AM
15289 case R_PPC64_TPREL16:
15290 case R_PPC64_TPREL16_LO:
15291 case R_PPC64_TPREL16_HI:
15292 case R_PPC64_TPREL16_HA:
15293 case R_PPC64_TPREL16_DS:
15294 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
15295 case R_PPC64_TPREL16_HIGH:
15296 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
15297 case R_PPC64_TPREL16_HIGHER:
15298 case R_PPC64_TPREL16_HIGHERA:
15299 case R_PPC64_TPREL16_HIGHEST:
15300 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
15301 if (h != NULL
15302 && h->elf.root.type == bfd_link_hash_undefweak
15303 && h->elf.dynindx == -1)
15304 {
15305 /* Make this relocation against an undefined weak symbol
15306 resolve to zero. This is really just a tweak, since
15307 code using weak externs ought to check that they are
15308 defined before using them. */
15309 bfd_byte *p = contents + rel->r_offset - d_offset;
15310
95f0d0d2 15311 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
15312 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15313 if (insn != 0)
95f0d0d2 15314 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
15315 break;
15316 }
989f9879
AM
15317 if (htab->elf.tls_sec != NULL)
15318 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
15319 /* The TPREL16 relocs shouldn't really be used in shared
15320 libs or with non-local symbols as that will result in
15321 DT_TEXTREL being set, but support them anyway. */
15322 goto dodyn;
411e1bfb
AM
15323
15324 case R_PPC64_DTPREL16:
15325 case R_PPC64_DTPREL16_LO:
15326 case R_PPC64_DTPREL16_HI:
15327 case R_PPC64_DTPREL16_HA:
15328 case R_PPC64_DTPREL16_DS:
15329 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
15330 case R_PPC64_DTPREL16_HIGH:
15331 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
15332 case R_PPC64_DTPREL16_HIGHER:
15333 case R_PPC64_DTPREL16_HIGHERA:
15334 case R_PPC64_DTPREL16_HIGHEST:
15335 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
15336 if (htab->elf.tls_sec != NULL)
15337 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
15338 break;
15339
45965137
AM
15340 case R_PPC64_ADDR64_LOCAL:
15341 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15342 ? h->elf.other
15343 : sym->st_other);
15344 break;
15345
e515b051
AM
15346 case R_PPC64_DTPMOD64:
15347 relocation = 1;
15348 addend = 0;
15349 goto dodyn;
15350
411e1bfb 15351 case R_PPC64_TPREL64:
989f9879
AM
15352 if (htab->elf.tls_sec != NULL)
15353 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
15354 goto dodyn;
15355
15356 case R_PPC64_DTPREL64:
989f9879
AM
15357 if (htab->elf.tls_sec != NULL)
15358 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 15359 /* Fall through. */
411e1bfb 15360
65f38f15
AM
15361 /* Relocations that may need to be propagated if this is a
15362 dynamic object. */
04c9666a 15363 case R_PPC64_REL30:
65f38f15
AM
15364 case R_PPC64_REL32:
15365 case R_PPC64_REL64:
15366 case R_PPC64_ADDR14:
15367 case R_PPC64_ADDR14_BRNTAKEN:
15368 case R_PPC64_ADDR14_BRTAKEN:
15369 case R_PPC64_ADDR16:
15370 case R_PPC64_ADDR16_DS:
15371 case R_PPC64_ADDR16_HA:
15372 case R_PPC64_ADDR16_HI:
f9c6b907
AM
15373 case R_PPC64_ADDR16_HIGH:
15374 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15375 case R_PPC64_ADDR16_HIGHER:
15376 case R_PPC64_ADDR16_HIGHERA:
15377 case R_PPC64_ADDR16_HIGHEST:
15378 case R_PPC64_ADDR16_HIGHESTA:
15379 case R_PPC64_ADDR16_LO:
15380 case R_PPC64_ADDR16_LO_DS:
15381 case R_PPC64_ADDR24:
65f38f15
AM
15382 case R_PPC64_ADDR32:
15383 case R_PPC64_ADDR64:
15384 case R_PPC64_UADDR16:
15385 case R_PPC64_UADDR32:
15386 case R_PPC64_UADDR64:
411e1bfb 15387 dodyn:
5d1634d7 15388 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
15389 break;
15390
41bd81ab
AM
15391 if (NO_OPD_RELOCS && is_opd)
15392 break;
15393
8a9e8e72 15394 if (bfd_link_pic (info)
b1b07054
AM
15395 ? ((h == NULL
15396 || h->dyn_relocs != NULL)
15397 && ((h != NULL && pc_dynrelocs (h))
15398 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
15399 : (h != NULL
15400 ? h->dyn_relocs != NULL
d311bc8b 15401 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 15402 {
b34976b6 15403 bfd_boolean skip, relocate;
65f38f15 15404 asection *sreloc;
1cf1f670 15405 bfd_vma out_off;
82e66161 15406 long indx = 0;
65f38f15
AM
15407
15408 /* When generating a dynamic object, these relocations
15409 are copied into the output file to be resolved at run
15410 time. */
15411
b34976b6
AM
15412 skip = FALSE;
15413 relocate = FALSE;
65f38f15 15414
1cf1f670
AM
15415 out_off = _bfd_elf_section_offset (output_bfd, info,
15416 input_section, rel->r_offset);
15417 if (out_off == (bfd_vma) -1)
b34976b6 15418 skip = TRUE;
1cf1f670 15419 else if (out_off == (bfd_vma) -2)
b34976b6 15420 skip = TRUE, relocate = TRUE;
1cf1f670
AM
15421 out_off += (input_section->output_section->vma
15422 + input_section->output_offset);
15423 outrel.r_offset = out_off;
411e1bfb 15424 outrel.r_addend = rel->r_addend;
65f38f15 15425
1cf1f670
AM
15426 /* Optimize unaligned reloc use. */
15427 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15428 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15429 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15430 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15431 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15432 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15433 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15434 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15435 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15436
65f38f15 15437 if (skip)
0bb2d96a 15438 memset (&outrel, 0, sizeof outrel);
afe397ea 15439 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
15440 && !is_opd
15441 && r_type != R_PPC64_TOC)
14acf4dc 15442 {
82e66161
AM
15443 indx = h->elf.dynindx;
15444 BFD_ASSERT (indx != -1);
15445 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 15446 }
65f38f15
AM
15447 else
15448 {
41bd81ab
AM
15449 /* This symbol is local, or marked to become local,
15450 or this is an opd section reloc which must point
15451 at a local function. */
65f38f15 15452 outrel.r_addend += relocation;
e86ce104 15453 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 15454 {
3fad3c7c 15455 if (is_opd && h != NULL)
afbe61cf
AM
15456 {
15457 /* Lie about opd entries. This case occurs
15458 when building shared libraries and we
15459 reference a function in another shared
3fad3c7c
AM
15460 lib. The same thing happens for a weak
15461 definition in an application that's
15462 overridden by a strong definition in a
15463 shared lib. (I believe this is a generic
15464 bug in binutils handling of weak syms.)
15465 In these cases we won't use the opd
1e2f5b6e 15466 entry in this lib. */
b34976b6 15467 unresolved_reloc = FALSE;
afbe61cf 15468 }
25f23106
AM
15469 if (!is_opd
15470 && r_type == R_PPC64_ADDR64
15471 && (h != NULL
15472 ? h->elf.type == STT_GNU_IFUNC
15473 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15474 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15475 else
15476 {
15477 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 15478
25f23106
AM
15479 /* We need to relocate .opd contents for ld.so.
15480 Prelink also wants simple and consistent rules
15481 for relocs. This make all RELATIVE relocs have
15482 *r_offset equal to r_addend. */
15483 relocate = TRUE;
15484 }
65f38f15
AM
15485 }
15486 else
15487 {
25f23106
AM
15488 if (h != NULL
15489 ? h->elf.type == STT_GNU_IFUNC
15490 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15491 {
25f53a85 15492 info->callbacks->einfo
695344c0 15493 /* xgettext:c-format */
174d0a74 15494 (_("%H: %s for indirect "
c1c8c1ef 15495 "function `%pT' unsupported\n"),
25f53a85 15496 input_bfd, input_section, rel->r_offset,
25f23106
AM
15497 ppc64_elf_howto_table[r_type]->name,
15498 sym_name);
15499 ret = FALSE;
15500 }
cf35638d 15501 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
15502 ;
15503 else if (sec == NULL || sec->owner == NULL)
15504 {
15505 bfd_set_error (bfd_error_bad_value);
b34976b6 15506 return FALSE;
65f38f15
AM
15507 }
15508 else
15509 {
15510 asection *osec;
15511
15512 osec = sec->output_section;
15513 indx = elf_section_data (osec)->dynindx;
15514
74541ad4
AM
15515 if (indx == 0)
15516 {
15517 if ((osec->flags & SEC_READONLY) == 0
15518 && htab->elf.data_index_section != NULL)
15519 osec = htab->elf.data_index_section;
15520 else
15521 osec = htab->elf.text_index_section;
15522 indx = elf_section_data (osec)->dynindx;
15523 }
15524 BFD_ASSERT (indx != 0);
15525
65f38f15
AM
15526 /* We are turning this relocation into one
15527 against a section symbol, so subtract out
15528 the output section's address but not the
15529 offset of the input section in the output
15530 section. */
15531 outrel.r_addend -= osec->vma;
15532 }
15533
15534 outrel.r_info = ELF64_R_INFO (indx, r_type);
15535 }
15536 }
15537
15538 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
15539 if (h != NULL
15540 ? h->elf.type == STT_GNU_IFUNC
15541 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
15542 {
15543 sreloc = htab->elf.irelplt;
15544 if (indx == 0)
15545 htab->local_ifunc_resolver = 1;
15546 else if (is_static_defined (&h->elf))
15547 htab->maybe_local_ifunc_resolver = 1;
15548 }
65f38f15
AM
15549 if (sreloc == NULL)
15550 abort ();
15551
dfbb6ac9
AM
15552 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15553 >= sreloc->size)
15554 abort ();
947216bf
AM
15555 loc = sreloc->contents;
15556 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
15557 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15558
15559 /* If this reloc is against an external symbol, it will
15560 be computed at runtime, so there's no need to do
81407a69
AM
15561 anything now. However, for the sake of prelink ensure
15562 that the section contents are a known value. */
65f38f15 15563 if (! relocate)
81407a69
AM
15564 {
15565 unresolved_reloc = FALSE;
15566 /* The value chosen here is quite arbitrary as ld.so
15567 ignores section contents except for the special
15568 case of .opd where the contents might be accessed
15569 before relocation. Choose zero, as that won't
15570 cause reloc overflow. */
15571 relocation = 0;
15572 addend = 0;
15573 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15574 to improve backward compatibility with older
15575 versions of ld. */
15576 if (r_type == R_PPC64_ADDR64)
15577 addend = outrel.r_addend;
15578 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 15579 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 15580 addend = outrel.r_offset;
81407a69 15581 }
65f38f15 15582 }
5bd4f169
AM
15583 break;
15584
65f38f15
AM
15585 case R_PPC64_COPY:
15586 case R_PPC64_GLOB_DAT:
15587 case R_PPC64_JMP_SLOT:
25f23106 15588 case R_PPC64_JMP_IREL:
65f38f15
AM
15589 case R_PPC64_RELATIVE:
15590 /* We shouldn't ever see these dynamic relocs in relocatable
15591 files. */
ae9a127f 15592 /* Fall through. */
65f38f15
AM
15593
15594 case R_PPC64_PLTGOT16:
15595 case R_PPC64_PLTGOT16_DS:
15596 case R_PPC64_PLTGOT16_HA:
15597 case R_PPC64_PLTGOT16_HI:
15598 case R_PPC64_PLTGOT16_LO:
15599 case R_PPC64_PLTGOT16_LO_DS:
15600 case R_PPC64_PLTREL32:
15601 case R_PPC64_PLTREL64:
15602 /* These ones haven't been implemented yet. */
15603
25f53a85 15604 info->callbacks->einfo
695344c0 15605 /* xgettext:c-format */
c1c8c1ef 15606 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 15607 input_bfd,
4ce794b7 15608 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
15609
15610 bfd_set_error (bfd_error_invalid_operation);
b34976b6 15611 ret = FALSE;
c316a17c 15612 goto copy_reloc;
65f38f15 15613 }
5bd4f169 15614
67f0cbdb
AM
15615 /* Multi-instruction sequences that access the TOC can be
15616 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 15617 to nop; addi rb,r2,x; */
67f0cbdb
AM
15618 switch (r_type)
15619 {
15620 default:
15621 break;
15622
15623 case R_PPC64_GOT_TLSLD16_HI:
15624 case R_PPC64_GOT_TLSGD16_HI:
15625 case R_PPC64_GOT_TPREL16_HI:
15626 case R_PPC64_GOT_DTPREL16_HI:
15627 case R_PPC64_GOT16_HI:
15628 case R_PPC64_TOC16_HI:
15629 /* These relocs would only be useful if building up an
15630 offset to later add to r2, perhaps in an indexed
15631 addressing mode instruction. Don't try to optimize.
15632 Unfortunately, the possibility of someone building up an
15633 offset like this or even with the HA relocs, means that
15634 we need to check the high insn when optimizing the low
15635 insn. */
15636 break;
15637
23cedd1d
AM
15638 case R_PPC64_PLTCALL:
15639 if (unresolved_reloc)
15640 {
15641 /* No plt entry. Make this into a direct call. */
15642 bfd_byte *p = contents + rel->r_offset;
15643 insn = bfd_get_32 (input_bfd, p);
15644 insn &= 1;
15645 bfd_put_32 (input_bfd, B_DOT | insn, p);
15646 bfd_put_32 (input_bfd, NOP, p + 4);
15647 unresolved_reloc = save_unresolved_reloc;
15648 r_type = R_PPC64_REL24;
15649 }
15650 break;
15651
15652 case R_PPC64_PLTSEQ:
15653 if (unresolved_reloc)
15654 {
15655 unresolved_reloc = FALSE;
15656 goto nop_it;
15657 }
15658 break;
15659
15660 case R_PPC64_PLT16_HA:
15661 if (unresolved_reloc)
15662 {
15663 unresolved_reloc = FALSE;
15664 goto nop_it;
15665 }
15666 /* Fall through. */
67f0cbdb
AM
15667 case R_PPC64_GOT_TLSLD16_HA:
15668 case R_PPC64_GOT_TLSGD16_HA:
15669 case R_PPC64_GOT_TPREL16_HA:
15670 case R_PPC64_GOT_DTPREL16_HA:
15671 case R_PPC64_GOT16_HA:
15672 case R_PPC64_TOC16_HA:
98528052 15673 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15674 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 15675 {
23cedd1d
AM
15676 bfd_byte *p;
15677 nop_it:
15678 p = contents + (rel->r_offset & ~3);
98528052 15679 bfd_put_32 (input_bfd, NOP, p);
d830549d 15680 goto copy_reloc;
98528052 15681 }
67f0cbdb
AM
15682 break;
15683
23cedd1d
AM
15684 case R_PPC64_PLT16_LO:
15685 case R_PPC64_PLT16_LO_DS:
15686 if (unresolved_reloc)
15687 {
15688 unresolved_reloc = FALSE;
15689 goto nop_it;
15690 }
15691 /* Fall through. */
67f0cbdb
AM
15692 case R_PPC64_GOT_TLSLD16_LO:
15693 case R_PPC64_GOT_TLSGD16_LO:
15694 case R_PPC64_GOT_TPREL16_LO_DS:
15695 case R_PPC64_GOT_DTPREL16_LO_DS:
15696 case R_PPC64_GOT16_LO:
15697 case R_PPC64_GOT16_LO_DS:
15698 case R_PPC64_TOC16_LO:
15699 case R_PPC64_TOC16_LO_DS:
98528052 15700 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15701 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15702 {
15703 bfd_byte *p = contents + (rel->r_offset & ~3);
15704 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15705 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15706 {
15707 /* Transform addic to addi when we change reg. */
15708 insn &= ~((0x3f << 26) | (0x1f << 16));
15709 insn |= (14u << 26) | (2 << 16);
15710 }
15711 else
67f0cbdb 15712 {
98528052
AM
15713 insn &= ~(0x1f << 16);
15714 insn |= 2 << 16;
67f0cbdb 15715 }
560c8763 15716 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15717 }
15718 break;
9a23f96e
AM
15719
15720 case R_PPC64_TPREL16_HA:
15721 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15722 {
15723 bfd_byte *p = contents + (rel->r_offset & ~3);
15724 insn = bfd_get_32 (input_bfd, p);
15725 if ((insn & ((0x3f << 26) | 0x1f << 16))
15726 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15727 /* xgettext:c-format */
15728 info->callbacks->minfo
15729 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
15730 input_bfd, input_section, rel->r_offset,
15731 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 15732 else
d830549d
AM
15733 {
15734 bfd_put_32 (input_bfd, NOP, p);
15735 goto copy_reloc;
15736 }
9a23f96e
AM
15737 }
15738 break;
15739
15740 case R_PPC64_TPREL16_LO:
15741 case R_PPC64_TPREL16_LO_DS:
15742 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15743 {
15744 bfd_byte *p = contents + (rel->r_offset & ~3);
15745 insn = bfd_get_32 (input_bfd, p);
15746 insn &= ~(0x1f << 16);
15747 insn |= 13 << 16;
15748 bfd_put_32 (input_bfd, insn, p);
15749 }
15750 break;
67f0cbdb
AM
15751 }
15752
65f38f15
AM
15753 /* Do any further special processing. */
15754 switch (r_type)
15755 {
15756 default:
15757 break;
15758
25f23106 15759 case R_PPC64_REL16_HA:
a680de9a 15760 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15761 case R_PPC64_ADDR16_HA:
15762 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15763 case R_PPC64_ADDR16_HIGHERA:
15764 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15765 case R_PPC64_TOC16_HA:
15766 case R_PPC64_SECTOFF_HA:
411e1bfb 15767 case R_PPC64_TPREL16_HA:
f9c6b907 15768 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15769 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15770 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15771 case R_PPC64_DTPREL16_HA:
15772 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15773 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15774 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15775 /* It's just possible that this symbol is a weak symbol
15776 that's not actually defined anywhere. In that case,
15777 'sec' would be NULL, and we should leave the symbol
15778 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15779 if (sec == NULL)
15780 break;
1a0670f3 15781 /* Fall through. */
5c5f6e17
AM
15782
15783 case R_PPC64_GOT16_HA:
15784 case R_PPC64_PLTGOT16_HA:
15785 case R_PPC64_PLT16_HA:
15786 case R_PPC64_GOT_TLSGD16_HA:
15787 case R_PPC64_GOT_TLSLD16_HA:
15788 case R_PPC64_GOT_TPREL16_HA:
15789 case R_PPC64_GOT_DTPREL16_HA:
15790 /* Add 0x10000 if sign bit in 0:15 is set.
15791 Bits 0:15 are not used. */
15792 addend += 0x8000;
65f38f15
AM
15793 break;
15794
15795 case R_PPC64_ADDR16_DS:
15796 case R_PPC64_ADDR16_LO_DS:
15797 case R_PPC64_GOT16_DS:
15798 case R_PPC64_GOT16_LO_DS:
15799 case R_PPC64_PLT16_LO_DS:
15800 case R_PPC64_SECTOFF_DS:
15801 case R_PPC64_SECTOFF_LO_DS:
15802 case R_PPC64_TOC16_DS:
15803 case R_PPC64_TOC16_LO_DS:
15804 case R_PPC64_PLTGOT16_DS:
15805 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15806 case R_PPC64_GOT_TPREL16_DS:
15807 case R_PPC64_GOT_TPREL16_LO_DS:
15808 case R_PPC64_GOT_DTPREL16_DS:
15809 case R_PPC64_GOT_DTPREL16_LO_DS:
15810 case R_PPC64_TPREL16_DS:
15811 case R_PPC64_TPREL16_LO_DS:
15812 case R_PPC64_DTPREL16_DS:
15813 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15814 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15815 mask = 3;
a680de9a
PB
15816 /* If this reloc is against an lq, lxv, or stxv insn, then
15817 the value must be a multiple of 16. This is somewhat of
15818 a hack, but the "correct" way to do this by defining _DQ
15819 forms of all the _DS relocs bloats all reloc switches in
15820 this file. It doesn't make much sense to use these
15821 relocs in data, so testing the insn should be safe. */
15822 if ((insn & (0x3f << 26)) == (56u << 26)
15823 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15824 mask = 15;
a680de9a
PB
15825 relocation += addend;
15826 addend = insn & (mask ^ 3);
15827 if ((relocation & mask) != 0)
65f38f15 15828 {
a680de9a 15829 relocation ^= relocation & mask;
25f53a85 15830 info->callbacks->einfo
695344c0 15831 /* xgettext:c-format */
174d0a74 15832 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 15833 input_bfd, input_section, rel->r_offset,
d830549d 15834 ppc64_elf_howto_table[r_type]->name,
adadcc0c 15835 mask + 1);
65f38f15 15836 bfd_set_error (bfd_error_bad_value);
b34976b6 15837 ret = FALSE;
c316a17c 15838 goto copy_reloc;
65f38f15
AM
15839 }
15840 break;
5bd4f169
AM
15841 }
15842
239e1f3a
AM
15843 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15844 because such sections are not SEC_ALLOC and thus ld.so will
15845 not process them. */
d830549d 15846 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 15847 if (unresolved_reloc
239e1f3a 15848 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15849 && h->elf.def_dynamic)
15850 && _bfd_elf_section_offset (output_bfd, info, input_section,
15851 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15852 {
25f53a85 15853 info->callbacks->einfo
695344c0 15854 /* xgettext:c-format */
c1c8c1ef 15855 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 15856 input_bfd, input_section, rel->r_offset,
b80eed39 15857 howto->name,
039b3fef 15858 h->elf.root.root.string);
b34976b6 15859 ret = FALSE;
9c07fe7c 15860 }
5bd4f169 15861
b80eed39
AM
15862 /* 16-bit fields in insns mostly have signed values, but a
15863 few insns have 16-bit unsigned values. Really, we should
15864 have different reloc types. */
15865 if (howto->complain_on_overflow != complain_overflow_dont
15866 && howto->dst_mask == 0xffff
15867 && (input_section->flags & SEC_CODE) != 0)
15868 {
15869 enum complain_overflow complain = complain_overflow_signed;
15870
15871 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15872 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15873 complain = complain_overflow_bitfield;
15874 else if (howto->rightshift == 0
15875 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15876 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15877 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15878 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15879 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15880 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15881 complain = complain_overflow_unsigned;
15882 if (howto->complain_on_overflow != complain)
15883 {
15884 alt_howto = *howto;
15885 alt_howto.complain_on_overflow = complain;
15886 howto = &alt_howto;
15887 }
15888 }
15889
a680de9a
PB
15890 if (r_type == R_PPC64_REL16DX_HA)
15891 {
15892 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15893 if (rel->r_offset + 4 > input_section->size)
15894 r = bfd_reloc_outofrange;
15895 else
15896 {
15897 relocation += addend;
15898 relocation -= (rel->r_offset
15899 + input_section->output_offset
15900 + input_section->output_section->vma);
3de43e7b 15901 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
15902 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15903 insn &= ~0x1fffc1;
3de43e7b 15904 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
15905 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15906 r = bfd_reloc_ok;
3de43e7b 15907 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
15908 r = bfd_reloc_overflow;
15909 }
15910 }
15911 else
15912 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15913 rel->r_offset, relocation, addend);
5bd4f169 15914
ef60b7ff 15915 if (r != bfd_reloc_ok)
5bd4f169 15916 {
bc30df16 15917 char *more_info = NULL;
b80eed39 15918 const char *reloc_name = howto->name;
bc30df16
AM
15919
15920 if (reloc_dest != DEST_NORMAL)
15921 {
15922 more_info = bfd_malloc (strlen (reloc_name) + 8);
15923 if (more_info != NULL)
15924 {
15925 strcpy (more_info, reloc_name);
15926 strcat (more_info, (reloc_dest == DEST_OPD
15927 ? " (OPD)" : " (stub)"));
15928 reloc_name = more_info;
15929 }
15930 }
15931
cd27b276 15932 if (r == bfd_reloc_overflow)
5bd4f169 15933 {
8131c122
AM
15934 /* On code like "if (foo) foo();" don't report overflow
15935 on a branch to zero when foo is undefined. */
15936 if (!warned
15937 && (reloc_dest == DEST_STUB
15938 || !(h != NULL
15939 && (h->elf.root.type == bfd_link_hash_undefweak
15940 || h->elf.root.type == bfd_link_hash_undefined)
15941 && is_branch_reloc (r_type))))
1a72702b
AM
15942 info->callbacks->reloc_overflow (info, &h->elf.root,
15943 sym_name, reloc_name,
15944 orig_rel.r_addend,
15945 input_bfd, input_section,
15946 rel->r_offset);
ef60b7ff
AM
15947 }
15948 else
15949 {
25f53a85 15950 info->callbacks->einfo
695344c0 15951 /* xgettext:c-format */
c1c8c1ef 15952 (_("%H: %s against `%pT': error %d\n"),
25f53a85 15953 input_bfd, input_section, rel->r_offset,
bc30df16 15954 reloc_name, sym_name, (int) r);
b34976b6 15955 ret = FALSE;
ef60b7ff 15956 }
bc30df16
AM
15957 if (more_info != NULL)
15958 free (more_info);
5bd4f169 15959 }
c316a17c
AM
15960 copy_reloc:
15961 if (wrel != rel)
15962 *wrel = *rel;
15963 }
15964
15965 if (wrel != rel)
15966 {
15967 Elf_Internal_Shdr *rel_hdr;
15968 size_t deleted = rel - wrel;
15969
15970 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15971 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15972 if (rel_hdr->sh_size == 0)
15973 {
15974 /* It is too late to remove an empty reloc section. Leave
15975 one NONE reloc.
15976 ??? What is wrong with an empty section??? */
15977 rel_hdr->sh_size = rel_hdr->sh_entsize;
15978 deleted -= 1;
15979 }
15980 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15981 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15982 input_section->reloc_count -= deleted;
5bd4f169
AM
15983 }
15984
645ea6a9
AM
15985 /* If we're emitting relocations, then shortly after this function
15986 returns, reloc offsets and addends for this section will be
15987 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15988 file rather than the input. Save a copy of the relocs for
15989 opd_entry_value. */
0e1862bb 15990 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15991 {
15992 bfd_size_type amt;
15993 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15994 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15995 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15996 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15997 if (rel == NULL)
15998 return FALSE;
15999 memcpy (rel, relocs, amt);
16000 }
5bd4f169
AM
16001 return ret;
16002}
16003
754021d0
AM
16004/* Adjust the value of any local symbols in opd sections. */
16005
6e0b88f1 16006static int
754021d0
AM
16007ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16008 const char *name ATTRIBUTE_UNUSED,
16009 Elf_Internal_Sym *elfsym,
16010 asection *input_sec,
16011 struct elf_link_hash_entry *h)
16012{
74f0fb50
AM
16013 struct _opd_sec_data *opd;
16014 long adjust;
754021d0
AM
16015 bfd_vma value;
16016
4025353c 16017 if (h != NULL)
6e0b88f1 16018 return 1;
4025353c 16019
74f0fb50
AM
16020 opd = get_opd_info (input_sec);
16021 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 16022 return 1;
754021d0
AM
16023
16024 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 16025 if (!bfd_link_relocatable (info))
754021d0
AM
16026 value -= input_sec->output_section->vma;
16027
51aecdc5 16028 adjust = opd->adjust[OPD_NDX (value)];
4025353c 16029 if (adjust == -1)
6e0b88f1
AM
16030 return 2;
16031
16032 elfsym->st_value += adjust;
16033 return 1;
754021d0
AM
16034}
16035
5bd4f169
AM
16036/* Finish up dynamic symbol handling. We set the contents of various
16037 dynamic sections here. */
16038
b34976b6 16039static bfd_boolean
4ce794b7
AM
16040ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16041 struct bfd_link_info *info,
16042 struct elf_link_hash_entry *h,
4aef7643 16043 Elf_Internal_Sym *sym)
5bd4f169 16044{
65f38f15 16045 struct ppc_link_hash_table *htab;
8387904d 16046 struct plt_entry *ent;
5bd4f169 16047
65f38f15 16048 htab = ppc_hash_table (info);
4dfe6ac6
NC
16049 if (htab == NULL)
16050 return FALSE;
5bd4f169 16051
49c09209
AM
16052 if (!htab->opd_abi && !h->def_regular)
16053 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16054 if (ent->plt.offset != (bfd_vma) -1)
16055 {
16056 /* Mark the symbol as undefined, rather than as
16057 defined in glink. Leave the value if there were
16058 any relocations where pointer equality matters
16059 (this is a clue for the dynamic linker, to make
16060 function pointer comparisons work between an
16061 application and shared library), otherwise set it
16062 to zero. */
16063 sym->st_shndx = SHN_UNDEF;
16064 if (!h->pointer_equality_needed)
16065 sym->st_value = 0;
16066 else if (!h->ref_regular_nonweak)
16067 {
16068 /* This breaks function pointer comparisons, but
16069 that is better than breaking tests for a NULL
16070 function pointer. */
16071 sym->st_value = 0;
16072 }
16073 break;
16074 }
5bd4f169 16075
f5385ebf 16076 if (h->needs_copy)
5bd4f169 16077 {
65f38f15 16078 /* This symbol needs a copy reloc. Set it up. */
49c09209 16079 Elf_Internal_Rela rela;
5474d94f 16080 asection *srel;
49c09209 16081 bfd_byte *loc;
5bd4f169 16082
65f38f15
AM
16083 if (h->dynindx == -1
16084 || (h->root.type != bfd_link_hash_defined
16085 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
16086 || htab->elf.srelbss == NULL
16087 || htab->elf.sreldynrelro == NULL)
65f38f15 16088 abort ();
5bd4f169
AM
16089
16090 rela.r_offset = (h->root.u.def.value
16091 + h->root.u.def.section->output_section->vma
16092 + h->root.u.def.section->output_offset);
16093 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16094 rela.r_addend = 0;
afbf7e8e 16095 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
16096 srel = htab->elf.sreldynrelro;
16097 else
16098 srel = htab->elf.srelbss;
16099 loc = srel->contents;
16100 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 16101 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
16102 }
16103
b34976b6 16104 return TRUE;
5bd4f169
AM
16105}
16106
65f38f15
AM
16107/* Used to decide how to sort relocs in an optimal manner for the
16108 dynamic linker, before writing them out. */
16109
16110static enum elf_reloc_type_class
7e612e98
AM
16111ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16112 const asection *rel_sec,
16113 const Elf_Internal_Rela *rela)
65f38f15 16114{
04c9666a 16115 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
16116 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16117
33e44f2e 16118 if (rel_sec == htab->elf.irelplt)
7e612e98 16119 return reloc_class_ifunc;
a33d1f77 16120
4ce794b7 16121 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 16122 switch (r_type)
65f38f15
AM
16123 {
16124 case R_PPC64_RELATIVE:
16125 return reloc_class_relative;
16126 case R_PPC64_JMP_SLOT:
16127 return reloc_class_plt;
16128 case R_PPC64_COPY:
16129 return reloc_class_copy;
16130 default:
16131 return reloc_class_normal;
16132 }
16133}
16134
5bd4f169
AM
16135/* Finish up the dynamic sections. */
16136
b34976b6 16137static bfd_boolean
4ce794b7
AM
16138ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16139 struct bfd_link_info *info)
5bd4f169 16140{
65f38f15
AM
16141 struct ppc_link_hash_table *htab;
16142 bfd *dynobj;
5bd4f169 16143 asection *sdyn;
5bd4f169 16144
65f38f15 16145 htab = ppc_hash_table (info);
4dfe6ac6
NC
16146 if (htab == NULL)
16147 return FALSE;
16148
65f38f15 16149 dynobj = htab->elf.dynobj;
3d4d4302 16150 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 16151
65f38f15 16152 if (htab->elf.dynamic_sections_created)
5bd4f169 16153 {
5bd4f169
AM
16154 Elf64_External_Dyn *dyncon, *dynconend;
16155
33e44f2e 16156 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 16157 abort ();
5bd4f169
AM
16158
16159 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 16160 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
16161 for (; dyncon < dynconend; dyncon++)
16162 {
16163 Elf_Internal_Dyn dyn;
19397422 16164 asection *s;
5bd4f169
AM
16165
16166 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16167
16168 switch (dyn.d_tag)
16169 {
65f38f15
AM
16170 default:
16171 continue;
5bd4f169 16172
5d1634d7 16173 case DT_PPC64_GLINK:
4ce794b7 16174 s = htab->glink;
6348e046 16175 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
16176 /* We stupidly defined DT_PPC64_GLINK to be the start
16177 of glink rather than the first entry point, which is
16178 what ld.so needs, and now have a bigger stub to
16179 support automatic multiple TOCs. */
9e390558 16180 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
16181 break;
16182
19397422
AM
16183 case DT_PPC64_OPD:
16184 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16185 if (s == NULL)
16186 continue;
16187 dyn.d_un.d_ptr = s->vma;
19397422
AM
16188 break;
16189
e8910a83
AM
16190 case DT_PPC64_OPT:
16191 if (htab->do_multi_toc && htab->multi_toc_needed)
16192 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
16193 if (htab->has_plt_localentry0)
16194 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
16195 break;
16196
19397422
AM
16197 case DT_PPC64_OPDSZ:
16198 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
16199 if (s == NULL)
16200 continue;
eea6121a 16201 dyn.d_un.d_val = s->size;
19397422
AM
16202 break;
16203
65f38f15 16204 case DT_PLTGOT:
33e44f2e 16205 s = htab->elf.splt;
6348e046 16206 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
16207 break;
16208
16209 case DT_JMPREL:
33e44f2e 16210 s = htab->elf.srelplt;
6348e046 16211 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 16212 break;
5bd4f169 16213
65f38f15 16214 case DT_PLTRELSZ:
33e44f2e 16215 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 16216 break;
82e66161
AM
16217
16218 case DT_TEXTREL:
16219 if (htab->local_ifunc_resolver)
16220 info->callbacks->einfo
16221 (_("%X%P: text relocations and GNU indirect "
16222 "functions will result in a segfault at runtime\n"));
16223 else if (htab->maybe_local_ifunc_resolver)
16224 info->callbacks->einfo
16225 (_("%P: warning: text relocations and GNU indirect "
16226 "functions may result in a segfault at runtime\n"));
16227 continue;
5bd4f169 16228 }
5bd4f169 16229
65f38f15 16230 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 16231 }
5bd4f169
AM
16232 }
16233
6528b6eb
AM
16234 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16235 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16236 {
16237 /* Fill in the first entry in the global offset table.
16238 We use it to hold the link-time TOCbase. */
16239 bfd_put_64 (output_bfd,
60ee0d4a 16240 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 16241 htab->elf.sgot->contents);
5d1634d7
AM
16242
16243 /* Set .got entry size. */
33e44f2e 16244 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
16245 }
16246
6528b6eb
AM
16247 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16248 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
16249 {
16250 /* Set .plt entry size. */
33e44f2e 16251 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 16252 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
16253 }
16254
84f5d08e
AM
16255 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16256 brlt ourselves if emitrelocations. */
16257 if (htab->brlt != NULL
16258 && htab->brlt->reloc_count != 0
16259 && !_bfd_elf_link_output_relocs (output_bfd,
16260 htab->brlt,
d4730f92 16261 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
16262 elf_section_data (htab->brlt)->relocs,
16263 NULL))
16264 return FALSE;
16265
176a0d42
AM
16266 if (htab->glink != NULL
16267 && htab->glink->reloc_count != 0
16268 && !_bfd_elf_link_output_relocs (output_bfd,
16269 htab->glink,
d4730f92 16270 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
16271 elf_section_data (htab->glink)->relocs,
16272 NULL))
16273 return FALSE;
16274
58d180e8 16275 if (htab->glink_eh_frame != NULL
da44f4e5
AM
16276 && htab->glink_eh_frame->size != 0)
16277 {
16278 bfd_vma val;
16279 bfd_byte *p;
d4aaa2a0 16280 struct map_stub *group;
2e0ce1c8 16281 size_t align = 4;
da44f4e5 16282
2e0ce1c8
AM
16283 p = htab->glink_eh_frame->contents;
16284 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
d4aaa2a0
AM
16285
16286 for (group = htab->group; group != NULL; group = group->next)
16287 if (group->stub_sec != NULL)
da44f4e5 16288 {
da44f4e5 16289 /* Offset to stub section. */
d4aaa2a0
AM
16290 val = (group->stub_sec->output_section->vma
16291 + group->stub_sec->output_offset);
da44f4e5
AM
16292 val -= (htab->glink_eh_frame->output_section->vma
16293 + htab->glink_eh_frame->output_offset
d4aaa2a0 16294 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
16295 if (val + 0x80000000 > 0xffffffff)
16296 {
cf97bcb0
AM
16297 _bfd_error_handler
16298 (_("%s offset too large for .eh_frame sdata4 encoding"),
d4aaa2a0 16299 group->stub_sec->name);
da44f4e5
AM
16300 return FALSE;
16301 }
d4aaa2a0
AM
16302 bfd_put_32 (dynobj, val, p + 8);
16303 p += stub_eh_frame_size (group, align);
da44f4e5
AM
16304 }
16305 if (htab->glink != NULL && htab->glink->size != 0)
16306 {
da44f4e5
AM
16307 /* Offset to .glink. */
16308 val = (htab->glink->output_section->vma
16309 + htab->glink->output_offset
16310 + 8);
16311 val -= (htab->glink_eh_frame->output_section->vma
16312 + htab->glink_eh_frame->output_offset
d4aaa2a0 16313 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
16314 if (val + 0x80000000 > 0xffffffff)
16315 {
cf97bcb0
AM
16316 _bfd_error_handler
16317 (_("%s offset too large for .eh_frame sdata4 encoding"),
da44f4e5
AM
16318 htab->glink->name);
16319 return FALSE;
16320 }
d4aaa2a0
AM
16321 bfd_put_32 (dynobj, val, p + 8);
16322 p += (24 + align - 1) & -align;
da44f4e5
AM
16323 }
16324
16325 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16326 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16327 htab->glink_eh_frame,
16328 htab->glink_eh_frame->contents))
16329 return FALSE;
16330 }
58d180e8 16331
e717da7e 16332 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
16333 since we didn't add them to DYNOBJ. We know dynobj is the first
16334 bfd. */
c72f2fb2 16335 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
16336 {
16337 asection *s;
7b53ace3 16338
0c8d6e5c 16339 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
16340 continue;
16341
e717da7e
AM
16342 s = ppc64_elf_tdata (dynobj)->got;
16343 if (s != NULL
eea6121a 16344 && s->size != 0
e717da7e
AM
16345 && s->output_section != bfd_abs_section_ptr
16346 && !bfd_set_section_contents (output_bfd, s->output_section,
16347 s->contents, s->output_offset,
eea6121a 16348 s->size))
e717da7e
AM
16349 return FALSE;
16350 s = ppc64_elf_tdata (dynobj)->relgot;
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 }
f6c52c13 16359
b34976b6 16360 return TRUE;
5bd4f169
AM
16361}
16362
5bd4f169 16363#include "elf64-target.h"
7b8e7dad
AM
16364
16365/* FreeBSD support */
16366
16367#undef TARGET_LITTLE_SYM
16368#undef TARGET_LITTLE_NAME
16369
16370#undef TARGET_BIG_SYM
6d00b590 16371#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
16372#undef TARGET_BIG_NAME
16373#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16374
16375#undef ELF_OSABI
16376#define ELF_OSABI ELFOSABI_FREEBSD
16377
16378#undef elf64_bed
16379#define elf64_bed elf64_powerpc_fbsd_bed
16380
16381#include "elf64-target.h"
This page took 3.17059 seconds and 4 git commands to generate.