]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
GC: Also check the local debug definition section
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
219d1afa 2 Copyright (C) 1999-2018 Free Software Foundation, Inc.
5bd4f169
AM
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 5 Largely rewritten by Alan Modra.
5bd4f169 6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 8
ae9a127f
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
5bd4f169 13
ae9a127f
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
5bd4f169 18
4ce794b7
AM
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 22
cd123cb7 23
4ce794b7
AM
24/* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 27
3db64b00 28#include "sysdep.h"
183e98be 29#include <stdarg.h>
5bd4f169 30#include "bfd.h"
5bd4f169
AM
31#include "bfdlink.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
04c9666a 34#include "elf/ppc64.h"
5d1634d7 35#include "elf64-ppc.h"
58d180e8 36#include "dwarf2.h"
5bd4f169 37
805fc799 38static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 54static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 56static bfd_vma opd_entry_value
aef36ac1 57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 58
6d00b590 59#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 60#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 61#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
62#define TARGET_BIG_NAME "elf64-powerpc"
63#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 64#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
65#define ELF_MACHINE_CODE EM_PPC64
66#define ELF_MAXPAGESIZE 0x10000
04c6a44c 67#define ELF_COMMONPAGESIZE 0x10000
ad8e1ba5
AM
68#define elf_info_to_howto ppc64_elf_info_to_howto
69
70#define elf_backend_want_got_sym 0
71#define elf_backend_want_plt_sym 0
72#define elf_backend_plt_alignment 3
73#define elf_backend_plt_not_loaded 1
ad8e1ba5 74#define elf_backend_got_header_size 8
5474d94f 75#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
76#define elf_backend_can_gc_sections 1
77#define elf_backend_can_refcount 1
78#define elf_backend_rela_normal 1
64f52338 79#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 80#define elf_backend_default_execstack 0
ad8e1ba5 81
e717da7e 82#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 83#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 84#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
85#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
86#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
87#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
88#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 89#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 90#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 91#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
92
93#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
94#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
95#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 96#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 97#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 98#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 99#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 100#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 101#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 102#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 103#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 104#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 105#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 106#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 107#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
108#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
109#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 110#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
111#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
112#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 113#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 114#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 115#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
116#define elf_backend_relocate_section ppc64_elf_relocate_section
117#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
118#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
119#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 120#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 121#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 122#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 123#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 124#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 125
5bd4f169
AM
126/* The name of the dynamic interpreter. This is put in the .interp
127 section. */
128#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
129
130/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 131#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
5bd4f169
AM
132
133/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 134#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 135
a078d95a
AM
136/* Offsets to some stack save slots. */
137#define STK_LR 16
138#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 139/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
140 CR save slot. Used only by optimised __tls_get_addr call stub,
141 relying on __tls_get_addr_opt not saving CR.. */
142#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
143
5bd4f169 144/* TOC base pointers offset from start of TOC. */
411e1bfb 145#define TOC_BASE_OFF 0x8000
a27e685f
AM
146/* TOC base alignment. */
147#define TOC_BASE_ALIGN 256
411e1bfb
AM
148
149/* Offset of tp and dtp pointers from start of TLS block. */
150#define TP_OFFSET 0x7000
151#define DTP_OFFSET 0x8000
5bd4f169 152
ad8e1ba5
AM
153/* .plt call stub instructions. The normal stub is like this, but
154 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 155 insert an addi to adjust r11. */
a078d95a 156#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
157#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
158#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
159#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
160#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
161#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
162#define BCTR 0x4e800420 /* bctr */
163
07d6d2b8
AM
164#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
165#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
166#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
167
168#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
169#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
170#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
171#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
172#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
173#define BNECTR 0x4ca20420 /* bnectr+ */
174#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 175
71a39c98 176#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 177#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 178#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 179
07d6d2b8
AM
180#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
181#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
182#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 183
07d6d2b8 184#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
006589cf
AM
185#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
186#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
a345bc8d 187#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
07d6d2b8 188#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
a345bc8d 189
9e390558
AM
190/* __glink_PLTresolve stub instructions. We enter with the index in R0. */
191#define GLINK_PLTRESOLVE_SIZE(htab) \
407aa07c 192 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
ee4bf8d2
AM
193 /* 0: */
194 /* .quad plt0-1f */
195 /* __glink: */
196#define MFLR_R12 0x7d8802a6 /* mflr %12 */
197#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
198 /* 1: */
199#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 200 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 201#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
202#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
203 /* ld %12,0(%11) */
204 /* ld %2,8(%11) */
205 /* mtctr %12 */
206 /* ld %11,16(%11) */
ee4bf8d2 207 /* bctr */
b9e5796b
AM
208#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
209#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
210#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
211#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
212#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
213
214/* Pad with this. */
215#define NOP 0x60000000
216
721956f4
AM
217/* Some other nops. */
218#define CROR_151515 0x4def7b82
219#define CROR_313131 0x4ffffb82
220
cedb70c5 221/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
222#define LI_R0_0 0x38000000 /* li %r0,0 */
223#define B_DOT 0x48000000 /* b . */
224
225/* After that, we need two instructions to load the index, followed by
226 a branch. */
227#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 228#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 229
deb0e272
AM
230/* Instructions used by the save and restore reg functions. */
231#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
232#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
233#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
234#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
235#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
236#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
237#define LI_R12_0 0x39800000 /* li %r12,0 */
238#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
239#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
240#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
241#define BLR 0x4e800020 /* blr */
242
41bd81ab
AM
243/* Since .opd is an array of descriptors and each entry will end up
244 with identical R_PPC64_RELATIVE relocs, there is really no need to
245 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 246 relocate .opd without reloc entries. */
41bd81ab
AM
247#ifndef NO_OPD_RELOCS
248#define NO_OPD_RELOCS 0
249#endif
810d4e75 250
a4b6fadd
AM
251#ifndef ARRAY_SIZE
252#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
253#endif
254
810d4e75
AM
255static inline int
256abiversion (bfd *abfd)
257{
258 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
259}
260
261static inline void
262set_abiversion (bfd *abfd, int ver)
263{
264 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
265 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
266}
5bd4f169 267\f
f5e87a1d 268#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 269
5bd4f169 270/* Relocation HOWTO's. */
04c9666a 271static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 272
f3185997
NC
273static reloc_howto_type ppc64_elf_howto_raw[] =
274{
5bd4f169
AM
275 /* This reloc does nothing. */
276 HOWTO (R_PPC64_NONE, /* type */
277 0, /* rightshift */
6346d5ca
AM
278 3, /* size (0 = byte, 1 = short, 2 = long) */
279 0, /* bitsize */
b34976b6 280 FALSE, /* pc_relative */
5bd4f169 281 0, /* bitpos */
f5e87a1d 282 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
283 bfd_elf_generic_reloc, /* special_function */
284 "R_PPC64_NONE", /* name */
b34976b6 285 FALSE, /* partial_inplace */
d006db6c 286 0, /* src_mask */
5bd4f169 287 0, /* dst_mask */
b34976b6 288 FALSE), /* pcrel_offset */
5bd4f169
AM
289
290 /* A standard 32 bit relocation. */
291 HOWTO (R_PPC64_ADDR32, /* type */
292 0, /* rightshift */
293 2, /* size (0 = byte, 1 = short, 2 = long) */
294 32, /* bitsize */
b34976b6 295 FALSE, /* pc_relative */
5bd4f169
AM
296 0, /* bitpos */
297 complain_overflow_bitfield, /* complain_on_overflow */
298 bfd_elf_generic_reloc, /* special_function */
299 "R_PPC64_ADDR32", /* name */
b34976b6 300 FALSE, /* partial_inplace */
5bd4f169
AM
301 0, /* src_mask */
302 0xffffffff, /* dst_mask */
b34976b6 303 FALSE), /* pcrel_offset */
5bd4f169
AM
304
305 /* An absolute 26 bit branch; the lower two bits must be zero.
306 FIXME: we don't check that, we just clear them. */
307 HOWTO (R_PPC64_ADDR24, /* type */
308 0, /* rightshift */
309 2, /* size (0 = byte, 1 = short, 2 = long) */
310 26, /* bitsize */
b34976b6 311 FALSE, /* pc_relative */
5bd4f169
AM
312 0, /* bitpos */
313 complain_overflow_bitfield, /* complain_on_overflow */
314 bfd_elf_generic_reloc, /* special_function */
315 "R_PPC64_ADDR24", /* name */
b34976b6 316 FALSE, /* partial_inplace */
d006db6c 317 0, /* src_mask */
f5e87a1d 318 0x03fffffc, /* dst_mask */
b34976b6 319 FALSE), /* pcrel_offset */
5bd4f169
AM
320
321 /* A standard 16 bit relocation. */
322 HOWTO (R_PPC64_ADDR16, /* type */
323 0, /* rightshift */
324 1, /* size (0 = byte, 1 = short, 2 = long) */
325 16, /* bitsize */
b34976b6 326 FALSE, /* pc_relative */
5bd4f169
AM
327 0, /* bitpos */
328 complain_overflow_bitfield, /* complain_on_overflow */
329 bfd_elf_generic_reloc, /* special_function */
330 "R_PPC64_ADDR16", /* name */
b34976b6 331 FALSE, /* partial_inplace */
5bd4f169
AM
332 0, /* src_mask */
333 0xffff, /* dst_mask */
b34976b6 334 FALSE), /* pcrel_offset */
5bd4f169
AM
335
336 /* A 16 bit relocation without overflow. */
337 HOWTO (R_PPC64_ADDR16_LO, /* type */
338 0, /* rightshift */
339 1, /* size (0 = byte, 1 = short, 2 = long) */
340 16, /* bitsize */
b34976b6 341 FALSE, /* pc_relative */
5bd4f169
AM
342 0, /* bitpos */
343 complain_overflow_dont,/* complain_on_overflow */
344 bfd_elf_generic_reloc, /* special_function */
345 "R_PPC64_ADDR16_LO", /* name */
b34976b6 346 FALSE, /* partial_inplace */
5bd4f169
AM
347 0, /* src_mask */
348 0xffff, /* dst_mask */
b34976b6 349 FALSE), /* pcrel_offset */
5bd4f169
AM
350
351 /* Bits 16-31 of an address. */
352 HOWTO (R_PPC64_ADDR16_HI, /* type */
353 16, /* rightshift */
354 1, /* size (0 = byte, 1 = short, 2 = long) */
355 16, /* bitsize */
b34976b6 356 FALSE, /* pc_relative */
5bd4f169 357 0, /* bitpos */
f9c6b907 358 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
359 bfd_elf_generic_reloc, /* special_function */
360 "R_PPC64_ADDR16_HI", /* name */
b34976b6 361 FALSE, /* partial_inplace */
5bd4f169
AM
362 0, /* src_mask */
363 0xffff, /* dst_mask */
b34976b6 364 FALSE), /* pcrel_offset */
5bd4f169
AM
365
366 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
367 bits, treated as a signed number, is negative. */
368 HOWTO (R_PPC64_ADDR16_HA, /* type */
369 16, /* rightshift */
370 1, /* size (0 = byte, 1 = short, 2 = long) */
371 16, /* bitsize */
b34976b6 372 FALSE, /* pc_relative */
5bd4f169 373 0, /* bitpos */
f9c6b907 374 complain_overflow_signed, /* complain_on_overflow */
805fc799 375 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 376 "R_PPC64_ADDR16_HA", /* name */
b34976b6 377 FALSE, /* partial_inplace */
5bd4f169
AM
378 0, /* src_mask */
379 0xffff, /* dst_mask */
b34976b6 380 FALSE), /* pcrel_offset */
5bd4f169
AM
381
382 /* An absolute 16 bit branch; the lower two bits must be zero.
383 FIXME: we don't check that, we just clear them. */
384 HOWTO (R_PPC64_ADDR14, /* type */
385 0, /* rightshift */
386 2, /* size (0 = byte, 1 = short, 2 = long) */
387 16, /* bitsize */
b34976b6 388 FALSE, /* pc_relative */
5bd4f169 389 0, /* bitpos */
b80eed39 390 complain_overflow_signed, /* complain_on_overflow */
2441e016 391 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 392 "R_PPC64_ADDR14", /* name */
b34976b6 393 FALSE, /* partial_inplace */
d006db6c 394 0, /* src_mask */
f5e87a1d 395 0x0000fffc, /* dst_mask */
b34976b6 396 FALSE), /* pcrel_offset */
5bd4f169
AM
397
398 /* An absolute 16 bit branch, for which bit 10 should be set to
399 indicate that the branch is expected to be taken. The lower two
400 bits must be zero. */
401 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
402 0, /* rightshift */
403 2, /* size (0 = byte, 1 = short, 2 = long) */
404 16, /* bitsize */
b34976b6 405 FALSE, /* pc_relative */
5bd4f169 406 0, /* bitpos */
b80eed39 407 complain_overflow_signed, /* complain_on_overflow */
805fc799 408 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 409 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 410 FALSE, /* partial_inplace */
d006db6c 411 0, /* src_mask */
f5e87a1d 412 0x0000fffc, /* dst_mask */
b34976b6 413 FALSE), /* pcrel_offset */
5bd4f169
AM
414
415 /* An absolute 16 bit branch, for which bit 10 should be set to
416 indicate that the branch is not expected to be taken. The lower
417 two bits must be zero. */
418 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
419 0, /* rightshift */
420 2, /* size (0 = byte, 1 = short, 2 = long) */
421 16, /* bitsize */
b34976b6 422 FALSE, /* pc_relative */
5bd4f169 423 0, /* bitpos */
b80eed39 424 complain_overflow_signed, /* complain_on_overflow */
805fc799 425 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 426 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 427 FALSE, /* partial_inplace */
d006db6c 428 0, /* src_mask */
f5e87a1d 429 0x0000fffc, /* dst_mask */
b34976b6 430 FALSE), /* pcrel_offset */
5bd4f169
AM
431
432 /* A relative 26 bit branch; the lower two bits must be zero. */
433 HOWTO (R_PPC64_REL24, /* type */
434 0, /* rightshift */
435 2, /* size (0 = byte, 1 = short, 2 = long) */
436 26, /* bitsize */
b34976b6 437 TRUE, /* pc_relative */
5bd4f169
AM
438 0, /* bitpos */
439 complain_overflow_signed, /* complain_on_overflow */
2441e016 440 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 441 "R_PPC64_REL24", /* name */
b34976b6 442 FALSE, /* partial_inplace */
d006db6c 443 0, /* src_mask */
f5e87a1d 444 0x03fffffc, /* dst_mask */
b34976b6 445 TRUE), /* pcrel_offset */
5bd4f169
AM
446
447 /* A relative 16 bit branch; the lower two bits must be zero. */
448 HOWTO (R_PPC64_REL14, /* type */
449 0, /* rightshift */
450 2, /* size (0 = byte, 1 = short, 2 = long) */
451 16, /* bitsize */
b34976b6 452 TRUE, /* pc_relative */
5bd4f169
AM
453 0, /* bitpos */
454 complain_overflow_signed, /* complain_on_overflow */
2441e016 455 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 456 "R_PPC64_REL14", /* name */
b34976b6 457 FALSE, /* partial_inplace */
d006db6c 458 0, /* src_mask */
f5e87a1d 459 0x0000fffc, /* dst_mask */
b34976b6 460 TRUE), /* pcrel_offset */
5bd4f169
AM
461
462 /* A relative 16 bit branch. Bit 10 should be set to indicate that
463 the branch is expected to be taken. The lower two bits must be
464 zero. */
465 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
466 0, /* rightshift */
467 2, /* size (0 = byte, 1 = short, 2 = long) */
468 16, /* bitsize */
b34976b6 469 TRUE, /* pc_relative */
5bd4f169
AM
470 0, /* bitpos */
471 complain_overflow_signed, /* complain_on_overflow */
805fc799 472 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 473 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 474 FALSE, /* partial_inplace */
d006db6c 475 0, /* src_mask */
f5e87a1d 476 0x0000fffc, /* dst_mask */
b34976b6 477 TRUE), /* pcrel_offset */
5bd4f169
AM
478
479 /* A relative 16 bit branch. Bit 10 should be set to indicate that
480 the branch is not expected to be taken. The lower two bits must
481 be zero. */
482 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
483 0, /* rightshift */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
485 16, /* bitsize */
b34976b6 486 TRUE, /* pc_relative */
5bd4f169
AM
487 0, /* bitpos */
488 complain_overflow_signed, /* complain_on_overflow */
805fc799 489 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 490 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 491 FALSE, /* partial_inplace */
d006db6c 492 0, /* src_mask */
f5e87a1d 493 0x0000fffc, /* dst_mask */
b34976b6 494 TRUE), /* pcrel_offset */
5bd4f169
AM
495
496 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
497 symbol. */
498 HOWTO (R_PPC64_GOT16, /* type */
499 0, /* rightshift */
500 1, /* size (0 = byte, 1 = short, 2 = long) */
501 16, /* bitsize */
b34976b6 502 FALSE, /* pc_relative */
5bd4f169
AM
503 0, /* bitpos */
504 complain_overflow_signed, /* complain_on_overflow */
805fc799 505 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 506 "R_PPC64_GOT16", /* name */
b34976b6 507 FALSE, /* partial_inplace */
5bd4f169
AM
508 0, /* src_mask */
509 0xffff, /* dst_mask */
b34976b6 510 FALSE), /* pcrel_offset */
5bd4f169
AM
511
512 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
513 the symbol. */
514 HOWTO (R_PPC64_GOT16_LO, /* type */
515 0, /* rightshift */
516 1, /* size (0 = byte, 1 = short, 2 = long) */
517 16, /* bitsize */
b34976b6 518 FALSE, /* pc_relative */
5bd4f169
AM
519 0, /* bitpos */
520 complain_overflow_dont, /* complain_on_overflow */
805fc799 521 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 522 "R_PPC64_GOT16_LO", /* name */
b34976b6 523 FALSE, /* partial_inplace */
5bd4f169
AM
524 0, /* src_mask */
525 0xffff, /* dst_mask */
b34976b6 526 FALSE), /* pcrel_offset */
5bd4f169
AM
527
528 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
529 the symbol. */
530 HOWTO (R_PPC64_GOT16_HI, /* type */
531 16, /* rightshift */
532 1, /* size (0 = byte, 1 = short, 2 = long) */
533 16, /* bitsize */
b34976b6 534 FALSE, /* pc_relative */
5bd4f169 535 0, /* bitpos */
f9c6b907 536 complain_overflow_signed,/* complain_on_overflow */
805fc799 537 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 538 "R_PPC64_GOT16_HI", /* name */
b34976b6 539 FALSE, /* partial_inplace */
5bd4f169
AM
540 0, /* src_mask */
541 0xffff, /* dst_mask */
b34976b6 542 FALSE), /* pcrel_offset */
5bd4f169
AM
543
544 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
545 the symbol. */
546 HOWTO (R_PPC64_GOT16_HA, /* type */
547 16, /* rightshift */
548 1, /* size (0 = byte, 1 = short, 2 = long) */
549 16, /* bitsize */
b34976b6 550 FALSE, /* pc_relative */
5bd4f169 551 0, /* bitpos */
f9c6b907 552 complain_overflow_signed,/* complain_on_overflow */
805fc799 553 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 554 "R_PPC64_GOT16_HA", /* name */
b34976b6 555 FALSE, /* partial_inplace */
5bd4f169
AM
556 0, /* src_mask */
557 0xffff, /* dst_mask */
b34976b6 558 FALSE), /* pcrel_offset */
5bd4f169
AM
559
560 /* This is used only by the dynamic linker. The symbol should exist
561 both in the object being run and in some shared library. The
562 dynamic linker copies the data addressed by the symbol from the
563 shared library into the object, because the object being
564 run has to have the data at some particular address. */
565 HOWTO (R_PPC64_COPY, /* type */
566 0, /* rightshift */
f5e87a1d
AM
567 0, /* this one is variable size */
568 0, /* bitsize */
b34976b6 569 FALSE, /* pc_relative */
5bd4f169 570 0, /* bitpos */
f5e87a1d
AM
571 complain_overflow_dont, /* complain_on_overflow */
572 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 573 "R_PPC64_COPY", /* name */
b34976b6 574 FALSE, /* partial_inplace */
5bd4f169
AM
575 0, /* src_mask */
576 0, /* dst_mask */
b34976b6 577 FALSE), /* pcrel_offset */
5bd4f169
AM
578
579 /* Like R_PPC64_ADDR64, but used when setting global offset table
580 entries. */
581 HOWTO (R_PPC64_GLOB_DAT, /* type */
582 0, /* rightshift */
583 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
584 64, /* bitsize */
b34976b6 585 FALSE, /* pc_relative */
5bd4f169
AM
586 0, /* bitpos */
587 complain_overflow_dont, /* complain_on_overflow */
805fc799 588 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 589 "R_PPC64_GLOB_DAT", /* name */
b34976b6 590 FALSE, /* partial_inplace */
5bd4f169 591 0, /* src_mask */
f5e87a1d 592 ONES (64), /* dst_mask */
b34976b6 593 FALSE), /* pcrel_offset */
5bd4f169
AM
594
595 /* Created by the link editor. Marks a procedure linkage table
596 entry for a symbol. */
597 HOWTO (R_PPC64_JMP_SLOT, /* type */
598 0, /* rightshift */
599 0, /* size (0 = byte, 1 = short, 2 = long) */
600 0, /* bitsize */
b34976b6 601 FALSE, /* pc_relative */
5bd4f169
AM
602 0, /* bitpos */
603 complain_overflow_dont, /* complain_on_overflow */
805fc799 604 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 605 "R_PPC64_JMP_SLOT", /* name */
b34976b6 606 FALSE, /* partial_inplace */
5bd4f169
AM
607 0, /* src_mask */
608 0, /* dst_mask */
b34976b6 609 FALSE), /* pcrel_offset */
5bd4f169
AM
610
611 /* Used only by the dynamic linker. When the object is run, this
612 doubleword64 is set to the load address of the object, plus the
613 addend. */
614 HOWTO (R_PPC64_RELATIVE, /* type */
615 0, /* rightshift */
616 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
617 64, /* bitsize */
b34976b6 618 FALSE, /* pc_relative */
5bd4f169
AM
619 0, /* bitpos */
620 complain_overflow_dont, /* complain_on_overflow */
621 bfd_elf_generic_reloc, /* special_function */
622 "R_PPC64_RELATIVE", /* name */
b34976b6 623 FALSE, /* partial_inplace */
5bd4f169 624 0, /* src_mask */
f5e87a1d 625 ONES (64), /* dst_mask */
b34976b6 626 FALSE), /* pcrel_offset */
5bd4f169
AM
627
628 /* Like R_PPC64_ADDR32, but may be unaligned. */
629 HOWTO (R_PPC64_UADDR32, /* type */
630 0, /* rightshift */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
632 32, /* bitsize */
b34976b6 633 FALSE, /* pc_relative */
5bd4f169
AM
634 0, /* bitpos */
635 complain_overflow_bitfield, /* complain_on_overflow */
636 bfd_elf_generic_reloc, /* special_function */
637 "R_PPC64_UADDR32", /* name */
b34976b6 638 FALSE, /* partial_inplace */
5bd4f169
AM
639 0, /* src_mask */
640 0xffffffff, /* dst_mask */
b34976b6 641 FALSE), /* pcrel_offset */
5bd4f169
AM
642
643 /* Like R_PPC64_ADDR16, but may be unaligned. */
644 HOWTO (R_PPC64_UADDR16, /* type */
645 0, /* rightshift */
646 1, /* size (0 = byte, 1 = short, 2 = long) */
647 16, /* bitsize */
b34976b6 648 FALSE, /* pc_relative */
5bd4f169
AM
649 0, /* bitpos */
650 complain_overflow_bitfield, /* complain_on_overflow */
651 bfd_elf_generic_reloc, /* special_function */
652 "R_PPC64_UADDR16", /* name */
b34976b6 653 FALSE, /* partial_inplace */
5bd4f169
AM
654 0, /* src_mask */
655 0xffff, /* dst_mask */
b34976b6 656 FALSE), /* pcrel_offset */
5bd4f169
AM
657
658 /* 32-bit PC relative. */
659 HOWTO (R_PPC64_REL32, /* type */
660 0, /* rightshift */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
662 32, /* bitsize */
b34976b6 663 TRUE, /* pc_relative */
5bd4f169 664 0, /* bitpos */
5bd4f169
AM
665 complain_overflow_signed, /* complain_on_overflow */
666 bfd_elf_generic_reloc, /* special_function */
667 "R_PPC64_REL32", /* name */
b34976b6 668 FALSE, /* partial_inplace */
5bd4f169
AM
669 0, /* src_mask */
670 0xffffffff, /* dst_mask */
b34976b6 671 TRUE), /* pcrel_offset */
5bd4f169 672
10ed1bba 673 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
674 HOWTO (R_PPC64_PLT32, /* type */
675 0, /* rightshift */
676 2, /* size (0 = byte, 1 = short, 2 = long) */
677 32, /* bitsize */
b34976b6 678 FALSE, /* pc_relative */
5bd4f169
AM
679 0, /* bitpos */
680 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 681 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 682 "R_PPC64_PLT32", /* name */
b34976b6 683 FALSE, /* partial_inplace */
5bd4f169 684 0, /* src_mask */
f5e87a1d 685 0xffffffff, /* dst_mask */
b34976b6 686 FALSE), /* pcrel_offset */
5bd4f169
AM
687
688 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
689 FIXME: R_PPC64_PLTREL32 not supported. */
690 HOWTO (R_PPC64_PLTREL32, /* type */
691 0, /* rightshift */
692 2, /* size (0 = byte, 1 = short, 2 = long) */
693 32, /* bitsize */
b34976b6 694 TRUE, /* pc_relative */
5bd4f169
AM
695 0, /* bitpos */
696 complain_overflow_signed, /* complain_on_overflow */
3ce51288 697 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 698 "R_PPC64_PLTREL32", /* name */
b34976b6 699 FALSE, /* partial_inplace */
5bd4f169 700 0, /* src_mask */
f5e87a1d 701 0xffffffff, /* dst_mask */
b34976b6 702 TRUE), /* pcrel_offset */
5bd4f169
AM
703
704 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
705 the symbol. */
706 HOWTO (R_PPC64_PLT16_LO, /* type */
707 0, /* rightshift */
708 1, /* size (0 = byte, 1 = short, 2 = long) */
709 16, /* bitsize */
b34976b6 710 FALSE, /* pc_relative */
5bd4f169
AM
711 0, /* bitpos */
712 complain_overflow_dont, /* complain_on_overflow */
805fc799 713 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 714 "R_PPC64_PLT16_LO", /* name */
b34976b6 715 FALSE, /* partial_inplace */
5bd4f169
AM
716 0, /* src_mask */
717 0xffff, /* dst_mask */
b34976b6 718 FALSE), /* pcrel_offset */
5bd4f169
AM
719
720 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
721 the symbol. */
722 HOWTO (R_PPC64_PLT16_HI, /* type */
723 16, /* rightshift */
724 1, /* size (0 = byte, 1 = short, 2 = long) */
725 16, /* bitsize */
b34976b6 726 FALSE, /* pc_relative */
5bd4f169 727 0, /* bitpos */
f9c6b907 728 complain_overflow_signed, /* complain_on_overflow */
805fc799 729 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 730 "R_PPC64_PLT16_HI", /* name */
b34976b6 731 FALSE, /* partial_inplace */
5bd4f169
AM
732 0, /* src_mask */
733 0xffff, /* dst_mask */
b34976b6 734 FALSE), /* pcrel_offset */
5bd4f169
AM
735
736 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
737 the symbol. */
738 HOWTO (R_PPC64_PLT16_HA, /* type */
739 16, /* rightshift */
740 1, /* size (0 = byte, 1 = short, 2 = long) */
741 16, /* bitsize */
b34976b6 742 FALSE, /* pc_relative */
5bd4f169 743 0, /* bitpos */
f9c6b907 744 complain_overflow_signed, /* complain_on_overflow */
805fc799 745 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 746 "R_PPC64_PLT16_HA", /* name */
b34976b6 747 FALSE, /* partial_inplace */
5bd4f169
AM
748 0, /* src_mask */
749 0xffff, /* dst_mask */
b34976b6 750 FALSE), /* pcrel_offset */
5bd4f169 751
c061c2d8 752 /* 16-bit section relative relocation. */
5bd4f169
AM
753 HOWTO (R_PPC64_SECTOFF, /* type */
754 0, /* rightshift */
c061c2d8
AM
755 1, /* size (0 = byte, 1 = short, 2 = long) */
756 16, /* bitsize */
b34976b6 757 FALSE, /* pc_relative */
5bd4f169 758 0, /* bitpos */
b80eed39 759 complain_overflow_signed, /* complain_on_overflow */
805fc799 760 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 761 "R_PPC64_SECTOFF", /* name */
b34976b6 762 FALSE, /* partial_inplace */
5bd4f169 763 0, /* src_mask */
c061c2d8 764 0xffff, /* dst_mask */
b34976b6 765 FALSE), /* pcrel_offset */
5bd4f169 766
c061c2d8 767 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
768 HOWTO (R_PPC64_SECTOFF_LO, /* type */
769 0, /* rightshift */
770 1, /* size (0 = byte, 1 = short, 2 = long) */
771 16, /* bitsize */
b34976b6 772 FALSE, /* pc_relative */
5bd4f169
AM
773 0, /* bitpos */
774 complain_overflow_dont, /* complain_on_overflow */
805fc799 775 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 776 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 777 FALSE, /* partial_inplace */
5bd4f169
AM
778 0, /* src_mask */
779 0xffff, /* dst_mask */
b34976b6 780 FALSE), /* pcrel_offset */
5bd4f169
AM
781
782 /* 16-bit upper half section relative relocation. */
783 HOWTO (R_PPC64_SECTOFF_HI, /* type */
784 16, /* rightshift */
785 1, /* size (0 = byte, 1 = short, 2 = long) */
786 16, /* bitsize */
b34976b6 787 FALSE, /* pc_relative */
5bd4f169 788 0, /* bitpos */
f9c6b907 789 complain_overflow_signed, /* complain_on_overflow */
805fc799 790 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 791 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 792 FALSE, /* partial_inplace */
5bd4f169
AM
793 0, /* src_mask */
794 0xffff, /* dst_mask */
b34976b6 795 FALSE), /* pcrel_offset */
5bd4f169
AM
796
797 /* 16-bit upper half adjusted section relative relocation. */
798 HOWTO (R_PPC64_SECTOFF_HA, /* type */
799 16, /* rightshift */
800 1, /* size (0 = byte, 1 = short, 2 = long) */
801 16, /* bitsize */
b34976b6 802 FALSE, /* pc_relative */
5bd4f169 803 0, /* bitpos */
f9c6b907 804 complain_overflow_signed, /* complain_on_overflow */
805fc799 805 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 806 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 807 FALSE, /* partial_inplace */
5bd4f169
AM
808 0, /* src_mask */
809 0xffff, /* dst_mask */
b34976b6 810 FALSE), /* pcrel_offset */
5bd4f169 811
04c9666a
AM
812 /* Like R_PPC64_REL24 without touching the two least significant bits. */
813 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
814 2, /* rightshift */
815 2, /* size (0 = byte, 1 = short, 2 = long) */
816 30, /* bitsize */
b34976b6 817 TRUE, /* pc_relative */
5bd4f169
AM
818 0, /* bitpos */
819 complain_overflow_dont, /* complain_on_overflow */
820 bfd_elf_generic_reloc, /* special_function */
04c9666a 821 "R_PPC64_REL30", /* name */
b34976b6 822 FALSE, /* partial_inplace */
d006db6c 823 0, /* src_mask */
5bd4f169 824 0xfffffffc, /* dst_mask */
b34976b6 825 TRUE), /* pcrel_offset */
5bd4f169
AM
826
827 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
828
829 /* A standard 64-bit relocation. */
830 HOWTO (R_PPC64_ADDR64, /* type */
831 0, /* rightshift */
832 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
833 64, /* bitsize */
b34976b6 834 FALSE, /* pc_relative */
5bd4f169
AM
835 0, /* bitpos */
836 complain_overflow_dont, /* complain_on_overflow */
837 bfd_elf_generic_reloc, /* special_function */
838 "R_PPC64_ADDR64", /* name */
b34976b6 839 FALSE, /* partial_inplace */
5bd4f169 840 0, /* src_mask */
f5e87a1d 841 ONES (64), /* dst_mask */
b34976b6 842 FALSE), /* pcrel_offset */
5bd4f169
AM
843
844 /* The bits 32-47 of an address. */
845 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
846 32, /* rightshift */
847 1, /* size (0 = byte, 1 = short, 2 = long) */
848 16, /* bitsize */
b34976b6 849 FALSE, /* pc_relative */
5bd4f169
AM
850 0, /* bitpos */
851 complain_overflow_dont, /* complain_on_overflow */
852 bfd_elf_generic_reloc, /* special_function */
853 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 854 FALSE, /* partial_inplace */
5bd4f169
AM
855 0, /* src_mask */
856 0xffff, /* dst_mask */
b34976b6 857 FALSE), /* pcrel_offset */
5bd4f169
AM
858
859 /* The bits 32-47 of an address, plus 1 if the contents of the low
860 16 bits, treated as a signed number, is negative. */
861 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
862 32, /* rightshift */
863 1, /* size (0 = byte, 1 = short, 2 = long) */
864 16, /* bitsize */
b34976b6 865 FALSE, /* pc_relative */
5bd4f169
AM
866 0, /* bitpos */
867 complain_overflow_dont, /* complain_on_overflow */
805fc799 868 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 869 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 870 FALSE, /* partial_inplace */
5bd4f169
AM
871 0, /* src_mask */
872 0xffff, /* dst_mask */
b34976b6 873 FALSE), /* pcrel_offset */
5bd4f169
AM
874
875 /* The bits 48-63 of an address. */
876 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
877 48, /* rightshift */
878 1, /* size (0 = byte, 1 = short, 2 = long) */
879 16, /* bitsize */
b34976b6 880 FALSE, /* pc_relative */
5bd4f169
AM
881 0, /* bitpos */
882 complain_overflow_dont, /* complain_on_overflow */
883 bfd_elf_generic_reloc, /* special_function */
884 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 885 FALSE, /* partial_inplace */
5bd4f169
AM
886 0, /* src_mask */
887 0xffff, /* dst_mask */
b34976b6 888 FALSE), /* pcrel_offset */
5bd4f169
AM
889
890 /* The bits 48-63 of an address, plus 1 if the contents of the low
891 16 bits, treated as a signed number, is negative. */
892 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
893 48, /* rightshift */
894 1, /* size (0 = byte, 1 = short, 2 = long) */
895 16, /* bitsize */
b34976b6 896 FALSE, /* pc_relative */
5bd4f169
AM
897 0, /* bitpos */
898 complain_overflow_dont, /* complain_on_overflow */
805fc799 899 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 900 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 901 FALSE, /* partial_inplace */
5bd4f169
AM
902 0, /* src_mask */
903 0xffff, /* dst_mask */
b34976b6 904 FALSE), /* pcrel_offset */
5bd4f169
AM
905
906 /* Like ADDR64, but may be unaligned. */
907 HOWTO (R_PPC64_UADDR64, /* type */
908 0, /* rightshift */
909 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
910 64, /* bitsize */
b34976b6 911 FALSE, /* pc_relative */
5bd4f169
AM
912 0, /* bitpos */
913 complain_overflow_dont, /* complain_on_overflow */
914 bfd_elf_generic_reloc, /* special_function */
915 "R_PPC64_UADDR64", /* name */
b34976b6 916 FALSE, /* partial_inplace */
5bd4f169 917 0, /* src_mask */
f5e87a1d 918 ONES (64), /* dst_mask */
b34976b6 919 FALSE), /* pcrel_offset */
5bd4f169
AM
920
921 /* 64-bit relative relocation. */
922 HOWTO (R_PPC64_REL64, /* type */
923 0, /* rightshift */
924 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
925 64, /* bitsize */
b34976b6 926 TRUE, /* pc_relative */
5bd4f169
AM
927 0, /* bitpos */
928 complain_overflow_dont, /* complain_on_overflow */
929 bfd_elf_generic_reloc, /* special_function */
930 "R_PPC64_REL64", /* name */
b34976b6 931 FALSE, /* partial_inplace */
5bd4f169 932 0, /* src_mask */
f5e87a1d 933 ONES (64), /* dst_mask */
b34976b6 934 TRUE), /* pcrel_offset */
5bd4f169 935
cedb70c5 936 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
937 HOWTO (R_PPC64_PLT64, /* type */
938 0, /* rightshift */
939 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
940 64, /* bitsize */
b34976b6 941 FALSE, /* pc_relative */
5bd4f169
AM
942 0, /* bitpos */
943 complain_overflow_dont, /* complain_on_overflow */
805fc799 944 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 945 "R_PPC64_PLT64", /* name */
b34976b6 946 FALSE, /* partial_inplace */
5bd4f169 947 0, /* src_mask */
f5e87a1d 948 ONES (64), /* dst_mask */
b34976b6 949 FALSE), /* pcrel_offset */
5bd4f169
AM
950
951 /* 64-bit PC relative relocation to the symbol's procedure linkage
952 table. */
953 /* FIXME: R_PPC64_PLTREL64 not supported. */
954 HOWTO (R_PPC64_PLTREL64, /* type */
955 0, /* rightshift */
956 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
957 64, /* bitsize */
b34976b6 958 TRUE, /* pc_relative */
5bd4f169
AM
959 0, /* bitpos */
960 complain_overflow_dont, /* complain_on_overflow */
805fc799 961 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 962 "R_PPC64_PLTREL64", /* name */
b34976b6 963 FALSE, /* partial_inplace */
5bd4f169 964 0, /* src_mask */
f5e87a1d 965 ONES (64), /* dst_mask */
b34976b6 966 TRUE), /* pcrel_offset */
5bd4f169
AM
967
968 /* 16 bit TOC-relative relocation. */
969
970 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
971 HOWTO (R_PPC64_TOC16, /* type */
972 0, /* rightshift */
973 1, /* size (0 = byte, 1 = short, 2 = long) */
974 16, /* bitsize */
b34976b6 975 FALSE, /* pc_relative */
5bd4f169
AM
976 0, /* bitpos */
977 complain_overflow_signed, /* complain_on_overflow */
805fc799 978 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 979 "R_PPC64_TOC16", /* name */
b34976b6 980 FALSE, /* partial_inplace */
5bd4f169
AM
981 0, /* src_mask */
982 0xffff, /* dst_mask */
b34976b6 983 FALSE), /* pcrel_offset */
5bd4f169
AM
984
985 /* 16 bit TOC-relative relocation without overflow. */
986
987 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
988 HOWTO (R_PPC64_TOC16_LO, /* type */
989 0, /* rightshift */
990 1, /* size (0 = byte, 1 = short, 2 = long) */
991 16, /* bitsize */
b34976b6 992 FALSE, /* pc_relative */
5bd4f169
AM
993 0, /* bitpos */
994 complain_overflow_dont, /* complain_on_overflow */
805fc799 995 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 996 "R_PPC64_TOC16_LO", /* name */
b34976b6 997 FALSE, /* partial_inplace */
5bd4f169
AM
998 0, /* src_mask */
999 0xffff, /* dst_mask */
b34976b6 1000 FALSE), /* pcrel_offset */
5bd4f169
AM
1001
1002 /* 16 bit TOC-relative relocation, high 16 bits. */
1003
1004 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1005 HOWTO (R_PPC64_TOC16_HI, /* type */
1006 16, /* rightshift */
1007 1, /* size (0 = byte, 1 = short, 2 = long) */
1008 16, /* bitsize */
b34976b6 1009 FALSE, /* pc_relative */
5bd4f169 1010 0, /* bitpos */
f9c6b907 1011 complain_overflow_signed, /* complain_on_overflow */
805fc799 1012 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 1013 "R_PPC64_TOC16_HI", /* name */
b34976b6 1014 FALSE, /* partial_inplace */
5bd4f169
AM
1015 0, /* src_mask */
1016 0xffff, /* dst_mask */
b34976b6 1017 FALSE), /* pcrel_offset */
5bd4f169
AM
1018
1019 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1020 contents of the low 16 bits, treated as a signed number, is
1021 negative. */
1022
1023 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1024 HOWTO (R_PPC64_TOC16_HA, /* type */
1025 16, /* rightshift */
1026 1, /* size (0 = byte, 1 = short, 2 = long) */
1027 16, /* bitsize */
b34976b6 1028 FALSE, /* pc_relative */
5bd4f169 1029 0, /* bitpos */
f9c6b907 1030 complain_overflow_signed, /* complain_on_overflow */
805fc799 1031 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 1032 "R_PPC64_TOC16_HA", /* name */
b34976b6 1033 FALSE, /* partial_inplace */
5bd4f169
AM
1034 0, /* src_mask */
1035 0xffff, /* dst_mask */
b34976b6 1036 FALSE), /* pcrel_offset */
5bd4f169
AM
1037
1038 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1039
1040 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1041 HOWTO (R_PPC64_TOC, /* type */
1042 0, /* rightshift */
1043 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1044 64, /* bitsize */
b34976b6 1045 FALSE, /* pc_relative */
5bd4f169 1046 0, /* bitpos */
b80eed39 1047 complain_overflow_dont, /* complain_on_overflow */
805fc799 1048 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1049 "R_PPC64_TOC", /* name */
b34976b6 1050 FALSE, /* partial_inplace */
5bd4f169 1051 0, /* src_mask */
f5e87a1d 1052 ONES (64), /* dst_mask */
b34976b6 1053 FALSE), /* pcrel_offset */
5bd4f169
AM
1054
1055 /* Like R_PPC64_GOT16, but also informs the link editor that the
1056 value to relocate may (!) refer to a PLT entry which the link
1057 editor (a) may replace with the symbol value. If the link editor
1058 is unable to fully resolve the symbol, it may (b) create a PLT
1059 entry and store the address to the new PLT entry in the GOT.
1060 This permits lazy resolution of function symbols at run time.
1061 The link editor may also skip all of this and just (c) emit a
1062 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1063 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1064 HOWTO (R_PPC64_PLTGOT16, /* type */
1065 0, /* rightshift */
1066 1, /* size (0 = byte, 1 = short, 2 = long) */
1067 16, /* bitsize */
b34976b6 1068 FALSE, /* pc_relative */
5bd4f169
AM
1069 0, /* bitpos */
1070 complain_overflow_signed, /* complain_on_overflow */
805fc799 1071 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1072 "R_PPC64_PLTGOT16", /* name */
1073 FALSE, /* partial_inplace */
1074 0, /* src_mask */
1075 0xffff, /* dst_mask */
1076 FALSE), /* pcrel_offset */
1077
1078 /* Like R_PPC64_PLTGOT16, but without overflow. */
1079 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1080 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1081 0, /* rightshift */
1082 1, /* size (0 = byte, 1 = short, 2 = long) */
1083 16, /* bitsize */
1084 FALSE, /* pc_relative */
1085 0, /* bitpos */
1086 complain_overflow_dont, /* complain_on_overflow */
1087 ppc64_elf_unhandled_reloc, /* special_function */
1088 "R_PPC64_PLTGOT16_LO", /* name */
1089 FALSE, /* partial_inplace */
1090 0, /* src_mask */
1091 0xffff, /* dst_mask */
1092 FALSE), /* pcrel_offset */
1093
1094 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1095 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1096 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1097 16, /* rightshift */
1098 1, /* size (0 = byte, 1 = short, 2 = long) */
1099 16, /* bitsize */
1100 FALSE, /* pc_relative */
1101 0, /* bitpos */
f9c6b907 1102 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1103 ppc64_elf_unhandled_reloc, /* special_function */
1104 "R_PPC64_PLTGOT16_HI", /* name */
1105 FALSE, /* partial_inplace */
1106 0, /* src_mask */
1107 0xffff, /* dst_mask */
1108 FALSE), /* pcrel_offset */
1109
1110 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1111 1 if the contents of the low 16 bits, treated as a signed number,
1112 is negative. */
1113 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1114 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1115 16, /* rightshift */
1116 1, /* size (0 = byte, 1 = short, 2 = long) */
1117 16, /* bitsize */
1118 FALSE, /* pc_relative */
1119 0, /* bitpos */
f9c6b907 1120 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1121 ppc64_elf_unhandled_reloc, /* special_function */
1122 "R_PPC64_PLTGOT16_HA", /* name */
1123 FALSE, /* partial_inplace */
1124 0, /* src_mask */
1125 0xffff, /* dst_mask */
1126 FALSE), /* pcrel_offset */
1127
1128 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1129 HOWTO (R_PPC64_ADDR16_DS, /* type */
1130 0, /* rightshift */
1131 1, /* size (0 = byte, 1 = short, 2 = long) */
1132 16, /* bitsize */
1133 FALSE, /* pc_relative */
1134 0, /* bitpos */
b80eed39 1135 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1136 bfd_elf_generic_reloc, /* special_function */
1137 "R_PPC64_ADDR16_DS", /* name */
1138 FALSE, /* partial_inplace */
1139 0, /* src_mask */
1140 0xfffc, /* dst_mask */
1141 FALSE), /* pcrel_offset */
1142
1143 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1144 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1145 0, /* rightshift */
1146 1, /* size (0 = byte, 1 = short, 2 = long) */
1147 16, /* bitsize */
1148 FALSE, /* pc_relative */
1149 0, /* bitpos */
1150 complain_overflow_dont,/* complain_on_overflow */
1151 bfd_elf_generic_reloc, /* special_function */
1152 "R_PPC64_ADDR16_LO_DS",/* name */
1153 FALSE, /* partial_inplace */
1154 0, /* src_mask */
1155 0xfffc, /* dst_mask */
1156 FALSE), /* pcrel_offset */
1157
1158 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1159 HOWTO (R_PPC64_GOT16_DS, /* type */
1160 0, /* rightshift */
1161 1, /* size (0 = byte, 1 = short, 2 = long) */
1162 16, /* bitsize */
1163 FALSE, /* pc_relative */
1164 0, /* bitpos */
1165 complain_overflow_signed, /* complain_on_overflow */
1166 ppc64_elf_unhandled_reloc, /* special_function */
1167 "R_PPC64_GOT16_DS", /* name */
1168 FALSE, /* partial_inplace */
1169 0, /* src_mask */
1170 0xfffc, /* dst_mask */
1171 FALSE), /* pcrel_offset */
1172
1173 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1174 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1175 0, /* rightshift */
1176 1, /* size (0 = byte, 1 = short, 2 = long) */
1177 16, /* bitsize */
1178 FALSE, /* pc_relative */
1179 0, /* bitpos */
1180 complain_overflow_dont, /* complain_on_overflow */
1181 ppc64_elf_unhandled_reloc, /* special_function */
1182 "R_PPC64_GOT16_LO_DS", /* name */
1183 FALSE, /* partial_inplace */
1184 0, /* src_mask */
1185 0xfffc, /* dst_mask */
1186 FALSE), /* pcrel_offset */
1187
1188 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1189 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1190 0, /* rightshift */
1191 1, /* size (0 = byte, 1 = short, 2 = long) */
1192 16, /* bitsize */
1193 FALSE, /* pc_relative */
1194 0, /* bitpos */
1195 complain_overflow_dont, /* complain_on_overflow */
1196 ppc64_elf_unhandled_reloc, /* special_function */
1197 "R_PPC64_PLT16_LO_DS", /* name */
1198 FALSE, /* partial_inplace */
1199 0, /* src_mask */
1200 0xfffc, /* dst_mask */
1201 FALSE), /* pcrel_offset */
1202
1203 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1204 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1205 0, /* rightshift */
1206 1, /* size (0 = byte, 1 = short, 2 = long) */
1207 16, /* bitsize */
1208 FALSE, /* pc_relative */
1209 0, /* bitpos */
b80eed39 1210 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1211 ppc64_elf_sectoff_reloc, /* special_function */
1212 "R_PPC64_SECTOFF_DS", /* name */
1213 FALSE, /* partial_inplace */
1214 0, /* src_mask */
1215 0xfffc, /* dst_mask */
1216 FALSE), /* pcrel_offset */
1217
1218 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1219 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1220 0, /* rightshift */
1221 1, /* size (0 = byte, 1 = short, 2 = long) */
1222 16, /* bitsize */
1223 FALSE, /* pc_relative */
1224 0, /* bitpos */
1225 complain_overflow_dont, /* complain_on_overflow */
1226 ppc64_elf_sectoff_reloc, /* special_function */
1227 "R_PPC64_SECTOFF_LO_DS",/* name */
1228 FALSE, /* partial_inplace */
1229 0, /* src_mask */
1230 0xfffc, /* dst_mask */
1231 FALSE), /* pcrel_offset */
1232
1233 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1234 HOWTO (R_PPC64_TOC16_DS, /* type */
1235 0, /* rightshift */
1236 1, /* size (0 = byte, 1 = short, 2 = long) */
1237 16, /* bitsize */
1238 FALSE, /* pc_relative */
1239 0, /* bitpos */
1240 complain_overflow_signed, /* complain_on_overflow */
1241 ppc64_elf_toc_reloc, /* special_function */
1242 "R_PPC64_TOC16_DS", /* name */
1243 FALSE, /* partial_inplace */
1244 0, /* src_mask */
1245 0xfffc, /* dst_mask */
1246 FALSE), /* pcrel_offset */
1247
1248 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1249 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1250 0, /* rightshift */
1251 1, /* size (0 = byte, 1 = short, 2 = long) */
1252 16, /* bitsize */
1253 FALSE, /* pc_relative */
1254 0, /* bitpos */
1255 complain_overflow_dont, /* complain_on_overflow */
1256 ppc64_elf_toc_reloc, /* special_function */
1257 "R_PPC64_TOC16_LO_DS", /* name */
1258 FALSE, /* partial_inplace */
1259 0, /* src_mask */
1260 0xfffc, /* dst_mask */
1261 FALSE), /* pcrel_offset */
1262
1263 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1264 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1265 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1266 0, /* rightshift */
1267 1, /* size (0 = byte, 1 = short, 2 = long) */
1268 16, /* bitsize */
1269 FALSE, /* pc_relative */
1270 0, /* bitpos */
1271 complain_overflow_signed, /* complain_on_overflow */
1272 ppc64_elf_unhandled_reloc, /* special_function */
1273 "R_PPC64_PLTGOT16_DS", /* name */
1274 FALSE, /* partial_inplace */
1275 0, /* src_mask */
1276 0xfffc, /* dst_mask */
1277 FALSE), /* pcrel_offset */
1278
1279 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1280 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1281 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1282 0, /* rightshift */
1283 1, /* size (0 = byte, 1 = short, 2 = long) */
1284 16, /* bitsize */
1285 FALSE, /* pc_relative */
1286 0, /* bitpos */
1287 complain_overflow_dont, /* complain_on_overflow */
1288 ppc64_elf_unhandled_reloc, /* special_function */
1289 "R_PPC64_PLTGOT16_LO_DS",/* name */
1290 FALSE, /* partial_inplace */
1291 0, /* src_mask */
1292 0xfffc, /* dst_mask */
1293 FALSE), /* pcrel_offset */
1294
727fc41e 1295 /* Marker relocs for TLS. */
411e1bfb
AM
1296 HOWTO (R_PPC64_TLS,
1297 0, /* rightshift */
1298 2, /* size (0 = byte, 1 = short, 2 = long) */
1299 32, /* bitsize */
1300 FALSE, /* pc_relative */
1301 0, /* bitpos */
1302 complain_overflow_dont, /* complain_on_overflow */
1303 bfd_elf_generic_reloc, /* special_function */
1304 "R_PPC64_TLS", /* name */
1305 FALSE, /* partial_inplace */
1306 0, /* src_mask */
1307 0, /* dst_mask */
1308 FALSE), /* pcrel_offset */
1309
727fc41e
AM
1310 HOWTO (R_PPC64_TLSGD,
1311 0, /* rightshift */
1312 2, /* size (0 = byte, 1 = short, 2 = long) */
1313 32, /* bitsize */
1314 FALSE, /* pc_relative */
1315 0, /* bitpos */
1316 complain_overflow_dont, /* complain_on_overflow */
1317 bfd_elf_generic_reloc, /* special_function */
1318 "R_PPC64_TLSGD", /* name */
1319 FALSE, /* partial_inplace */
1320 0, /* src_mask */
1321 0, /* dst_mask */
1322 FALSE), /* pcrel_offset */
1323
1324 HOWTO (R_PPC64_TLSLD,
1325 0, /* rightshift */
1326 2, /* size (0 = byte, 1 = short, 2 = long) */
1327 32, /* bitsize */
1328 FALSE, /* pc_relative */
1329 0, /* bitpos */
1330 complain_overflow_dont, /* complain_on_overflow */
1331 bfd_elf_generic_reloc, /* special_function */
1332 "R_PPC64_TLSLD", /* name */
1333 FALSE, /* partial_inplace */
1334 0, /* src_mask */
1335 0, /* dst_mask */
1336 FALSE), /* pcrel_offset */
1337
3b421ab3
AM
1338 HOWTO (R_PPC64_TOCSAVE,
1339 0, /* rightshift */
1340 2, /* size (0 = byte, 1 = short, 2 = long) */
1341 32, /* bitsize */
1342 FALSE, /* pc_relative */
1343 0, /* bitpos */
1344 complain_overflow_dont, /* complain_on_overflow */
1345 bfd_elf_generic_reloc, /* special_function */
1346 "R_PPC64_TOCSAVE", /* name */
1347 FALSE, /* partial_inplace */
1348 0, /* src_mask */
1349 0, /* dst_mask */
1350 FALSE), /* pcrel_offset */
1351
411e1bfb
AM
1352 /* Computes the load module index of the load module that contains the
1353 definition of its TLS sym. */
1354 HOWTO (R_PPC64_DTPMOD64,
1355 0, /* rightshift */
1356 4, /* size (0 = byte, 1 = short, 2 = long) */
1357 64, /* bitsize */
1358 FALSE, /* pc_relative */
1359 0, /* bitpos */
1360 complain_overflow_dont, /* complain_on_overflow */
1361 ppc64_elf_unhandled_reloc, /* special_function */
1362 "R_PPC64_DTPMOD64", /* name */
1363 FALSE, /* partial_inplace */
1364 0, /* src_mask */
1365 ONES (64), /* dst_mask */
1366 FALSE), /* pcrel_offset */
1367
1368 /* Computes a dtv-relative displacement, the difference between the value
1369 of sym+add and the base address of the thread-local storage block that
1370 contains the definition of sym, minus 0x8000. */
1371 HOWTO (R_PPC64_DTPREL64,
1372 0, /* rightshift */
1373 4, /* size (0 = byte, 1 = short, 2 = long) */
1374 64, /* bitsize */
1375 FALSE, /* pc_relative */
1376 0, /* bitpos */
1377 complain_overflow_dont, /* complain_on_overflow */
1378 ppc64_elf_unhandled_reloc, /* special_function */
1379 "R_PPC64_DTPREL64", /* name */
1380 FALSE, /* partial_inplace */
1381 0, /* src_mask */
1382 ONES (64), /* dst_mask */
1383 FALSE), /* pcrel_offset */
1384
1385 /* A 16 bit dtprel reloc. */
1386 HOWTO (R_PPC64_DTPREL16,
1387 0, /* rightshift */
1388 1, /* size (0 = byte, 1 = short, 2 = long) */
1389 16, /* bitsize */
1390 FALSE, /* pc_relative */
1391 0, /* bitpos */
1392 complain_overflow_signed, /* complain_on_overflow */
1393 ppc64_elf_unhandled_reloc, /* special_function */
1394 "R_PPC64_DTPREL16", /* name */
1395 FALSE, /* partial_inplace */
1396 0, /* src_mask */
1397 0xffff, /* dst_mask */
1398 FALSE), /* pcrel_offset */
1399
1400 /* Like DTPREL16, but no overflow. */
1401 HOWTO (R_PPC64_DTPREL16_LO,
1402 0, /* rightshift */
1403 1, /* size (0 = byte, 1 = short, 2 = long) */
1404 16, /* bitsize */
1405 FALSE, /* pc_relative */
1406 0, /* bitpos */
1407 complain_overflow_dont, /* complain_on_overflow */
1408 ppc64_elf_unhandled_reloc, /* special_function */
1409 "R_PPC64_DTPREL16_LO", /* name */
1410 FALSE, /* partial_inplace */
1411 0, /* src_mask */
1412 0xffff, /* dst_mask */
1413 FALSE), /* pcrel_offset */
1414
1415 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1416 HOWTO (R_PPC64_DTPREL16_HI,
1417 16, /* rightshift */
1418 1, /* size (0 = byte, 1 = short, 2 = long) */
1419 16, /* bitsize */
1420 FALSE, /* pc_relative */
1421 0, /* bitpos */
f9c6b907 1422 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1423 ppc64_elf_unhandled_reloc, /* special_function */
1424 "R_PPC64_DTPREL16_HI", /* name */
1425 FALSE, /* partial_inplace */
1426 0, /* src_mask */
1427 0xffff, /* dst_mask */
1428 FALSE), /* pcrel_offset */
1429
1430 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1431 HOWTO (R_PPC64_DTPREL16_HA,
1432 16, /* rightshift */
1433 1, /* size (0 = byte, 1 = short, 2 = long) */
1434 16, /* bitsize */
1435 FALSE, /* pc_relative */
1436 0, /* bitpos */
f9c6b907 1437 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1438 ppc64_elf_unhandled_reloc, /* special_function */
1439 "R_PPC64_DTPREL16_HA", /* name */
1440 FALSE, /* partial_inplace */
1441 0, /* src_mask */
1442 0xffff, /* dst_mask */
1443 FALSE), /* pcrel_offset */
1444
1445 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1446 HOWTO (R_PPC64_DTPREL16_HIGHER,
1447 32, /* rightshift */
1448 1, /* size (0 = byte, 1 = short, 2 = long) */
1449 16, /* bitsize */
1450 FALSE, /* pc_relative */
1451 0, /* bitpos */
1452 complain_overflow_dont, /* complain_on_overflow */
1453 ppc64_elf_unhandled_reloc, /* special_function */
1454 "R_PPC64_DTPREL16_HIGHER", /* name */
1455 FALSE, /* partial_inplace */
1456 0, /* src_mask */
1457 0xffff, /* dst_mask */
1458 FALSE), /* pcrel_offset */
1459
1460 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1461 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1462 32, /* rightshift */
1463 1, /* size (0 = byte, 1 = short, 2 = long) */
1464 16, /* bitsize */
1465 FALSE, /* pc_relative */
1466 0, /* bitpos */
1467 complain_overflow_dont, /* complain_on_overflow */
1468 ppc64_elf_unhandled_reloc, /* special_function */
1469 "R_PPC64_DTPREL16_HIGHERA", /* name */
1470 FALSE, /* partial_inplace */
1471 0, /* src_mask */
1472 0xffff, /* dst_mask */
1473 FALSE), /* pcrel_offset */
1474
1475 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1476 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1477 48, /* rightshift */
1478 1, /* size (0 = byte, 1 = short, 2 = long) */
1479 16, /* bitsize */
1480 FALSE, /* pc_relative */
1481 0, /* bitpos */
1482 complain_overflow_dont, /* complain_on_overflow */
1483 ppc64_elf_unhandled_reloc, /* special_function */
1484 "R_PPC64_DTPREL16_HIGHEST", /* name */
1485 FALSE, /* partial_inplace */
1486 0, /* src_mask */
1487 0xffff, /* dst_mask */
1488 FALSE), /* pcrel_offset */
1489
1490 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1491 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1492 48, /* rightshift */
1493 1, /* size (0 = byte, 1 = short, 2 = long) */
1494 16, /* bitsize */
1495 FALSE, /* pc_relative */
1496 0, /* bitpos */
1497 complain_overflow_dont, /* complain_on_overflow */
1498 ppc64_elf_unhandled_reloc, /* special_function */
1499 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1500 FALSE, /* partial_inplace */
1501 0, /* src_mask */
1502 0xffff, /* dst_mask */
1503 FALSE), /* pcrel_offset */
1504
1505 /* Like DTPREL16, but for insns with a DS field. */
1506 HOWTO (R_PPC64_DTPREL16_DS,
1507 0, /* rightshift */
1508 1, /* size (0 = byte, 1 = short, 2 = long) */
1509 16, /* bitsize */
1510 FALSE, /* pc_relative */
1511 0, /* bitpos */
1512 complain_overflow_signed, /* complain_on_overflow */
1513 ppc64_elf_unhandled_reloc, /* special_function */
1514 "R_PPC64_DTPREL16_DS", /* name */
1515 FALSE, /* partial_inplace */
1516 0, /* src_mask */
1517 0xfffc, /* dst_mask */
1518 FALSE), /* pcrel_offset */
1519
1520 /* Like DTPREL16_DS, but no overflow. */
1521 HOWTO (R_PPC64_DTPREL16_LO_DS,
1522 0, /* rightshift */
1523 1, /* size (0 = byte, 1 = short, 2 = long) */
1524 16, /* bitsize */
1525 FALSE, /* pc_relative */
1526 0, /* bitpos */
1527 complain_overflow_dont, /* complain_on_overflow */
1528 ppc64_elf_unhandled_reloc, /* special_function */
1529 "R_PPC64_DTPREL16_LO_DS", /* name */
1530 FALSE, /* partial_inplace */
1531 0, /* src_mask */
1532 0xfffc, /* dst_mask */
1533 FALSE), /* pcrel_offset */
1534
1535 /* Computes a tp-relative displacement, the difference between the value of
1536 sym+add and the value of the thread pointer (r13). */
1537 HOWTO (R_PPC64_TPREL64,
1538 0, /* rightshift */
1539 4, /* size (0 = byte, 1 = short, 2 = long) */
1540 64, /* bitsize */
1541 FALSE, /* pc_relative */
1542 0, /* bitpos */
1543 complain_overflow_dont, /* complain_on_overflow */
1544 ppc64_elf_unhandled_reloc, /* special_function */
1545 "R_PPC64_TPREL64", /* name */
1546 FALSE, /* partial_inplace */
1547 0, /* src_mask */
1548 ONES (64), /* dst_mask */
1549 FALSE), /* pcrel_offset */
1550
1551 /* A 16 bit tprel reloc. */
1552 HOWTO (R_PPC64_TPREL16,
1553 0, /* rightshift */
1554 1, /* size (0 = byte, 1 = short, 2 = long) */
1555 16, /* bitsize */
1556 FALSE, /* pc_relative */
1557 0, /* bitpos */
1558 complain_overflow_signed, /* complain_on_overflow */
1559 ppc64_elf_unhandled_reloc, /* special_function */
1560 "R_PPC64_TPREL16", /* name */
1561 FALSE, /* partial_inplace */
1562 0, /* src_mask */
1563 0xffff, /* dst_mask */
1564 FALSE), /* pcrel_offset */
1565
1566 /* Like TPREL16, but no overflow. */
1567 HOWTO (R_PPC64_TPREL16_LO,
1568 0, /* rightshift */
1569 1, /* size (0 = byte, 1 = short, 2 = long) */
1570 16, /* bitsize */
1571 FALSE, /* pc_relative */
1572 0, /* bitpos */
1573 complain_overflow_dont, /* complain_on_overflow */
1574 ppc64_elf_unhandled_reloc, /* special_function */
1575 "R_PPC64_TPREL16_LO", /* name */
1576 FALSE, /* partial_inplace */
1577 0, /* src_mask */
1578 0xffff, /* dst_mask */
1579 FALSE), /* pcrel_offset */
1580
1581 /* Like TPREL16_LO, but next higher group of 16 bits. */
1582 HOWTO (R_PPC64_TPREL16_HI,
1583 16, /* rightshift */
1584 1, /* size (0 = byte, 1 = short, 2 = long) */
1585 16, /* bitsize */
1586 FALSE, /* pc_relative */
1587 0, /* bitpos */
f9c6b907 1588 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1589 ppc64_elf_unhandled_reloc, /* special_function */
1590 "R_PPC64_TPREL16_HI", /* name */
1591 FALSE, /* partial_inplace */
1592 0, /* src_mask */
1593 0xffff, /* dst_mask */
1594 FALSE), /* pcrel_offset */
1595
1596 /* Like TPREL16_HI, but adjust for low 16 bits. */
1597 HOWTO (R_PPC64_TPREL16_HA,
1598 16, /* rightshift */
1599 1, /* size (0 = byte, 1 = short, 2 = long) */
1600 16, /* bitsize */
1601 FALSE, /* pc_relative */
1602 0, /* bitpos */
f9c6b907 1603 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1604 ppc64_elf_unhandled_reloc, /* special_function */
1605 "R_PPC64_TPREL16_HA", /* name */
1606 FALSE, /* partial_inplace */
1607 0, /* src_mask */
1608 0xffff, /* dst_mask */
1609 FALSE), /* pcrel_offset */
1610
1611 /* Like TPREL16_HI, but next higher group of 16 bits. */
1612 HOWTO (R_PPC64_TPREL16_HIGHER,
1613 32, /* rightshift */
1614 1, /* size (0 = byte, 1 = short, 2 = long) */
1615 16, /* bitsize */
1616 FALSE, /* pc_relative */
1617 0, /* bitpos */
1618 complain_overflow_dont, /* complain_on_overflow */
1619 ppc64_elf_unhandled_reloc, /* special_function */
1620 "R_PPC64_TPREL16_HIGHER", /* name */
1621 FALSE, /* partial_inplace */
1622 0, /* src_mask */
1623 0xffff, /* dst_mask */
1624 FALSE), /* pcrel_offset */
1625
1626 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1627 HOWTO (R_PPC64_TPREL16_HIGHERA,
1628 32, /* rightshift */
1629 1, /* size (0 = byte, 1 = short, 2 = long) */
1630 16, /* bitsize */
1631 FALSE, /* pc_relative */
1632 0, /* bitpos */
1633 complain_overflow_dont, /* complain_on_overflow */
1634 ppc64_elf_unhandled_reloc, /* special_function */
1635 "R_PPC64_TPREL16_HIGHERA", /* name */
1636 FALSE, /* partial_inplace */
1637 0, /* src_mask */
1638 0xffff, /* dst_mask */
1639 FALSE), /* pcrel_offset */
1640
1641 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1642 HOWTO (R_PPC64_TPREL16_HIGHEST,
1643 48, /* rightshift */
1644 1, /* size (0 = byte, 1 = short, 2 = long) */
1645 16, /* bitsize */
1646 FALSE, /* pc_relative */
1647 0, /* bitpos */
1648 complain_overflow_dont, /* complain_on_overflow */
1649 ppc64_elf_unhandled_reloc, /* special_function */
1650 "R_PPC64_TPREL16_HIGHEST", /* name */
1651 FALSE, /* partial_inplace */
1652 0, /* src_mask */
1653 0xffff, /* dst_mask */
1654 FALSE), /* pcrel_offset */
1655
1656 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1657 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1658 48, /* rightshift */
1659 1, /* size (0 = byte, 1 = short, 2 = long) */
1660 16, /* bitsize */
1661 FALSE, /* pc_relative */
1662 0, /* bitpos */
1663 complain_overflow_dont, /* complain_on_overflow */
1664 ppc64_elf_unhandled_reloc, /* special_function */
1665 "R_PPC64_TPREL16_HIGHESTA", /* name */
1666 FALSE, /* partial_inplace */
1667 0, /* src_mask */
1668 0xffff, /* dst_mask */
1669 FALSE), /* pcrel_offset */
1670
1671 /* Like TPREL16, but for insns with a DS field. */
1672 HOWTO (R_PPC64_TPREL16_DS,
1673 0, /* rightshift */
1674 1, /* size (0 = byte, 1 = short, 2 = long) */
1675 16, /* bitsize */
1676 FALSE, /* pc_relative */
1677 0, /* bitpos */
1678 complain_overflow_signed, /* complain_on_overflow */
1679 ppc64_elf_unhandled_reloc, /* special_function */
1680 "R_PPC64_TPREL16_DS", /* name */
1681 FALSE, /* partial_inplace */
1682 0, /* src_mask */
1683 0xfffc, /* dst_mask */
1684 FALSE), /* pcrel_offset */
1685
1686 /* Like TPREL16_DS, but no overflow. */
1687 HOWTO (R_PPC64_TPREL16_LO_DS,
1688 0, /* rightshift */
1689 1, /* size (0 = byte, 1 = short, 2 = long) */
1690 16, /* bitsize */
1691 FALSE, /* pc_relative */
1692 0, /* bitpos */
1693 complain_overflow_dont, /* complain_on_overflow */
1694 ppc64_elf_unhandled_reloc, /* special_function */
1695 "R_PPC64_TPREL16_LO_DS", /* name */
1696 FALSE, /* partial_inplace */
1697 0, /* src_mask */
1698 0xfffc, /* dst_mask */
1699 FALSE), /* pcrel_offset */
1700
1701 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1702 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1703 to the first entry relative to the TOC base (r2). */
1704 HOWTO (R_PPC64_GOT_TLSGD16,
1705 0, /* rightshift */
1706 1, /* size (0 = byte, 1 = short, 2 = long) */
1707 16, /* bitsize */
1708 FALSE, /* pc_relative */
1709 0, /* bitpos */
1710 complain_overflow_signed, /* complain_on_overflow */
1711 ppc64_elf_unhandled_reloc, /* special_function */
1712 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1713 FALSE, /* partial_inplace */
5bd4f169
AM
1714 0, /* src_mask */
1715 0xffff, /* dst_mask */
b34976b6 1716 FALSE), /* pcrel_offset */
5bd4f169 1717
411e1bfb
AM
1718 /* Like GOT_TLSGD16, but no overflow. */
1719 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1720 0, /* rightshift */
1721 1, /* size (0 = byte, 1 = short, 2 = long) */
1722 16, /* bitsize */
b34976b6 1723 FALSE, /* pc_relative */
5bd4f169
AM
1724 0, /* bitpos */
1725 complain_overflow_dont, /* complain_on_overflow */
805fc799 1726 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1727 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1728 FALSE, /* partial_inplace */
5bd4f169
AM
1729 0, /* src_mask */
1730 0xffff, /* dst_mask */
b34976b6 1731 FALSE), /* pcrel_offset */
5bd4f169 1732
411e1bfb
AM
1733 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1734 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1735 16, /* rightshift */
1736 1, /* size (0 = byte, 1 = short, 2 = long) */
1737 16, /* bitsize */
b34976b6 1738 FALSE, /* pc_relative */
5bd4f169 1739 0, /* bitpos */
f9c6b907 1740 complain_overflow_signed, /* complain_on_overflow */
805fc799 1741 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1742 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1743 FALSE, /* partial_inplace */
5bd4f169
AM
1744 0, /* src_mask */
1745 0xffff, /* dst_mask */
b34976b6 1746 FALSE), /* pcrel_offset */
5bd4f169 1747
411e1bfb
AM
1748 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1749 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1750 16, /* rightshift */
1751 1, /* size (0 = byte, 1 = short, 2 = long) */
1752 16, /* bitsize */
b34976b6 1753 FALSE, /* pc_relative */
5bd4f169 1754 0, /* bitpos */
f9c6b907 1755 complain_overflow_signed, /* complain_on_overflow */
805fc799 1756 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1757 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1758 FALSE, /* partial_inplace */
5bd4f169
AM
1759 0, /* src_mask */
1760 0xffff, /* dst_mask */
b34976b6 1761 FALSE), /* pcrel_offset */
5bd4f169 1762
411e1bfb
AM
1763 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1764 with values (sym+add)@dtpmod and zero, and computes the offset to the
1765 first entry relative to the TOC base (r2). */
1766 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1767 0, /* rightshift */
1768 1, /* size (0 = byte, 1 = short, 2 = long) */
1769 16, /* bitsize */
b34976b6 1770 FALSE, /* pc_relative */
5bd4f169 1771 0, /* bitpos */
411e1bfb
AM
1772 complain_overflow_signed, /* complain_on_overflow */
1773 ppc64_elf_unhandled_reloc, /* special_function */
1774 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1775 FALSE, /* partial_inplace */
d006db6c 1776 0, /* src_mask */
411e1bfb 1777 0xffff, /* dst_mask */
b34976b6 1778 FALSE), /* pcrel_offset */
5bd4f169 1779
411e1bfb
AM
1780 /* Like GOT_TLSLD16, but no overflow. */
1781 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1782 0, /* rightshift */
1783 1, /* size (0 = byte, 1 = short, 2 = long) */
1784 16, /* bitsize */
b34976b6 1785 FALSE, /* pc_relative */
5bd4f169 1786 0, /* bitpos */
411e1bfb
AM
1787 complain_overflow_dont, /* complain_on_overflow */
1788 ppc64_elf_unhandled_reloc, /* special_function */
1789 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1790 FALSE, /* partial_inplace */
d006db6c 1791 0, /* src_mask */
411e1bfb 1792 0xffff, /* dst_mask */
b34976b6 1793 FALSE), /* pcrel_offset */
5bd4f169 1794
411e1bfb
AM
1795 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1796 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1797 16, /* rightshift */
5bd4f169
AM
1798 1, /* size (0 = byte, 1 = short, 2 = long) */
1799 16, /* bitsize */
b34976b6 1800 FALSE, /* pc_relative */
5bd4f169 1801 0, /* bitpos */
f9c6b907 1802 complain_overflow_signed, /* complain_on_overflow */
805fc799 1803 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1804 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1805 FALSE, /* partial_inplace */
d006db6c 1806 0, /* src_mask */
411e1bfb 1807 0xffff, /* dst_mask */
b34976b6 1808 FALSE), /* pcrel_offset */
5bd4f169 1809
411e1bfb
AM
1810 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1811 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1812 16, /* rightshift */
5bd4f169
AM
1813 1, /* size (0 = byte, 1 = short, 2 = long) */
1814 16, /* bitsize */
b34976b6 1815 FALSE, /* pc_relative */
5bd4f169 1816 0, /* bitpos */
f9c6b907 1817 complain_overflow_signed, /* complain_on_overflow */
805fc799 1818 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1819 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1820 FALSE, /* partial_inplace */
d006db6c 1821 0, /* src_mask */
411e1bfb 1822 0xffff, /* dst_mask */
b34976b6 1823 FALSE), /* pcrel_offset */
5bd4f169 1824
411e1bfb
AM
1825 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1826 the offset to the entry relative to the TOC base (r2). */
1827 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1828 0, /* rightshift */
1829 1, /* size (0 = byte, 1 = short, 2 = long) */
1830 16, /* bitsize */
b34976b6 1831 FALSE, /* pc_relative */
5bd4f169 1832 0, /* bitpos */
411e1bfb 1833 complain_overflow_signed, /* complain_on_overflow */
805fc799 1834 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1835 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1836 FALSE, /* partial_inplace */
d006db6c 1837 0, /* src_mask */
5bd4f169 1838 0xfffc, /* dst_mask */
b34976b6 1839 FALSE), /* pcrel_offset */
5bd4f169 1840
411e1bfb
AM
1841 /* Like GOT_DTPREL16_DS, but no overflow. */
1842 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1843 0, /* rightshift */
c061c2d8
AM
1844 1, /* size (0 = byte, 1 = short, 2 = long) */
1845 16, /* bitsize */
b34976b6 1846 FALSE, /* pc_relative */
5bd4f169 1847 0, /* bitpos */
411e1bfb
AM
1848 complain_overflow_dont, /* complain_on_overflow */
1849 ppc64_elf_unhandled_reloc, /* special_function */
1850 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1851 FALSE, /* partial_inplace */
d006db6c 1852 0, /* src_mask */
c061c2d8 1853 0xfffc, /* dst_mask */
b34976b6 1854 FALSE), /* pcrel_offset */
5bd4f169 1855
411e1bfb
AM
1856 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1857 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1858 16, /* rightshift */
5bd4f169
AM
1859 1, /* size (0 = byte, 1 = short, 2 = long) */
1860 16, /* bitsize */
b34976b6 1861 FALSE, /* pc_relative */
5bd4f169 1862 0, /* bitpos */
f9c6b907 1863 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1864 ppc64_elf_unhandled_reloc, /* special_function */
1865 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1866 FALSE, /* partial_inplace */
d006db6c 1867 0, /* src_mask */
411e1bfb 1868 0xffff, /* dst_mask */
b34976b6 1869 FALSE), /* pcrel_offset */
5bd4f169 1870
411e1bfb
AM
1871 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1872 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1873 16, /* rightshift */
1874 1, /* size (0 = byte, 1 = short, 2 = long) */
1875 16, /* bitsize */
1876 FALSE, /* pc_relative */
1877 0, /* bitpos */
f9c6b907 1878 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1879 ppc64_elf_unhandled_reloc, /* special_function */
1880 "R_PPC64_GOT_DTPREL16_HA", /* name */
1881 FALSE, /* partial_inplace */
1882 0, /* src_mask */
1883 0xffff, /* dst_mask */
1884 FALSE), /* pcrel_offset */
1885
1886 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1887 offset to the entry relative to the TOC base (r2). */
1888 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1889 0, /* rightshift */
1890 1, /* size (0 = byte, 1 = short, 2 = long) */
1891 16, /* bitsize */
b34976b6 1892 FALSE, /* pc_relative */
5bd4f169
AM
1893 0, /* bitpos */
1894 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1895 ppc64_elf_unhandled_reloc, /* special_function */
1896 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1897 FALSE, /* partial_inplace */
d006db6c 1898 0, /* src_mask */
ad8e1ba5 1899 0xfffc, /* dst_mask */
b34976b6 1900 FALSE), /* pcrel_offset */
5bd4f169 1901
411e1bfb
AM
1902 /* Like GOT_TPREL16_DS, but no overflow. */
1903 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1904 0, /* rightshift */
1905 1, /* size (0 = byte, 1 = short, 2 = long) */
1906 16, /* bitsize */
b34976b6 1907 FALSE, /* pc_relative */
5bd4f169
AM
1908 0, /* bitpos */
1909 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1910 ppc64_elf_unhandled_reloc, /* special_function */
1911 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1912 FALSE, /* partial_inplace */
d006db6c 1913 0, /* src_mask */
ad8e1ba5 1914 0xfffc, /* dst_mask */
b34976b6 1915 FALSE), /* pcrel_offset */
5bd4f169 1916
411e1bfb
AM
1917 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1918 HOWTO (R_PPC64_GOT_TPREL16_HI,
1919 16, /* rightshift */
5bd4f169
AM
1920 1, /* size (0 = byte, 1 = short, 2 = long) */
1921 16, /* bitsize */
b34976b6 1922 FALSE, /* pc_relative */
5bd4f169 1923 0, /* bitpos */
f9c6b907 1924 complain_overflow_signed, /* complain_on_overflow */
805fc799 1925 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1926 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1927 FALSE, /* partial_inplace */
d006db6c 1928 0, /* src_mask */
411e1bfb 1929 0xffff, /* dst_mask */
b34976b6 1930 FALSE), /* pcrel_offset */
5bd4f169 1931
411e1bfb
AM
1932 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1933 HOWTO (R_PPC64_GOT_TPREL16_HA,
1934 16, /* rightshift */
5bd4f169
AM
1935 1, /* size (0 = byte, 1 = short, 2 = long) */
1936 16, /* bitsize */
b34976b6 1937 FALSE, /* pc_relative */
5bd4f169 1938 0, /* bitpos */
f9c6b907 1939 complain_overflow_signed, /* complain_on_overflow */
805fc799 1940 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1941 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1942 FALSE, /* partial_inplace */
d006db6c 1943 0, /* src_mask */
411e1bfb 1944 0xffff, /* dst_mask */
b34976b6 1945 FALSE), /* pcrel_offset */
5bd4f169 1946
25f23106
AM
1947 HOWTO (R_PPC64_JMP_IREL, /* type */
1948 0, /* rightshift */
1949 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1950 0, /* bitsize */
1951 FALSE, /* pc_relative */
1952 0, /* bitpos */
1953 complain_overflow_dont, /* complain_on_overflow */
1954 ppc64_elf_unhandled_reloc, /* special_function */
1955 "R_PPC64_JMP_IREL", /* name */
1956 FALSE, /* partial_inplace */
1957 0, /* src_mask */
1958 0, /* dst_mask */
1959 FALSE), /* pcrel_offset */
1960
e054468f
AM
1961 HOWTO (R_PPC64_IRELATIVE, /* type */
1962 0, /* rightshift */
1963 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1964 64, /* bitsize */
1965 FALSE, /* pc_relative */
1966 0, /* bitpos */
1967 complain_overflow_dont, /* complain_on_overflow */
1968 bfd_elf_generic_reloc, /* special_function */
1969 "R_PPC64_IRELATIVE", /* name */
1970 FALSE, /* partial_inplace */
1971 0, /* src_mask */
1972 ONES (64), /* dst_mask */
1973 FALSE), /* pcrel_offset */
1974
25f23106
AM
1975 /* A 16 bit relative relocation. */
1976 HOWTO (R_PPC64_REL16, /* type */
1977 0, /* rightshift */
1978 1, /* size (0 = byte, 1 = short, 2 = long) */
1979 16, /* bitsize */
1980 TRUE, /* pc_relative */
1981 0, /* bitpos */
b80eed39 1982 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1983 bfd_elf_generic_reloc, /* special_function */
1984 "R_PPC64_REL16", /* name */
1985 FALSE, /* partial_inplace */
1986 0, /* src_mask */
1987 0xffff, /* dst_mask */
1988 TRUE), /* pcrel_offset */
1989
1990 /* A 16 bit relative relocation without overflow. */
1991 HOWTO (R_PPC64_REL16_LO, /* type */
1992 0, /* rightshift */
1993 1, /* size (0 = byte, 1 = short, 2 = long) */
1994 16, /* bitsize */
1995 TRUE, /* pc_relative */
1996 0, /* bitpos */
1997 complain_overflow_dont,/* complain_on_overflow */
1998 bfd_elf_generic_reloc, /* special_function */
1999 "R_PPC64_REL16_LO", /* name */
2000 FALSE, /* partial_inplace */
2001 0, /* src_mask */
2002 0xffff, /* dst_mask */
2003 TRUE), /* pcrel_offset */
2004
2005 /* The high order 16 bits of a relative address. */
2006 HOWTO (R_PPC64_REL16_HI, /* type */
2007 16, /* rightshift */
2008 1, /* size (0 = byte, 1 = short, 2 = long) */
2009 16, /* bitsize */
2010 TRUE, /* pc_relative */
2011 0, /* bitpos */
f9c6b907 2012 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2013 bfd_elf_generic_reloc, /* special_function */
2014 "R_PPC64_REL16_HI", /* name */
2015 FALSE, /* partial_inplace */
2016 0, /* src_mask */
2017 0xffff, /* dst_mask */
2018 TRUE), /* pcrel_offset */
2019
2020 /* The high order 16 bits of a relative address, plus 1 if the contents of
2021 the low 16 bits, treated as a signed number, is negative. */
2022 HOWTO (R_PPC64_REL16_HA, /* type */
2023 16, /* rightshift */
2024 1, /* size (0 = byte, 1 = short, 2 = long) */
2025 16, /* bitsize */
2026 TRUE, /* pc_relative */
2027 0, /* bitpos */
f9c6b907 2028 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2029 ppc64_elf_ha_reloc, /* special_function */
2030 "R_PPC64_REL16_HA", /* name */
2031 FALSE, /* partial_inplace */
2032 0, /* src_mask */
2033 0xffff, /* dst_mask */
2034 TRUE), /* pcrel_offset */
2035
a680de9a
PB
2036 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2037 HOWTO (R_PPC64_REL16DX_HA, /* type */
2038 16, /* rightshift */
2039 2, /* size (0 = byte, 1 = short, 2 = long) */
2040 16, /* bitsize */
2041 TRUE, /* pc_relative */
2042 0, /* bitpos */
2043 complain_overflow_signed, /* complain_on_overflow */
2044 ppc64_elf_ha_reloc, /* special_function */
2045 "R_PPC64_REL16DX_HA", /* name */
2046 FALSE, /* partial_inplace */
2047 0, /* src_mask */
2048 0x1fffc1, /* dst_mask */
2049 TRUE), /* pcrel_offset */
2050
7ba71655
AM
2051 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2052 HOWTO (R_PPC64_16DX_HA, /* type */
2053 16, /* rightshift */
2054 2, /* size (0 = byte, 1 = short, 2 = long) */
2055 16, /* bitsize */
2056 FALSE, /* pc_relative */
2057 0, /* bitpos */
2058 complain_overflow_signed, /* complain_on_overflow */
2059 ppc64_elf_ha_reloc, /* special_function */
2060 "R_PPC64_16DX_HA", /* name */
2061 FALSE, /* partial_inplace */
2062 0, /* src_mask */
2063 0x1fffc1, /* dst_mask */
2064 FALSE), /* pcrel_offset */
2065
f9c6b907
AM
2066 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2067 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2068 16, /* rightshift */
2069 1, /* size (0 = byte, 1 = short, 2 = long) */
2070 16, /* bitsize */
2071 FALSE, /* pc_relative */
2072 0, /* bitpos */
2073 complain_overflow_dont, /* complain_on_overflow */
2074 bfd_elf_generic_reloc, /* special_function */
2075 "R_PPC64_ADDR16_HIGH", /* name */
2076 FALSE, /* partial_inplace */
2077 0, /* src_mask */
2078 0xffff, /* dst_mask */
2079 FALSE), /* pcrel_offset */
2080
2081 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2082 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2083 16, /* rightshift */
2084 1, /* size (0 = byte, 1 = short, 2 = long) */
2085 16, /* bitsize */
2086 FALSE, /* pc_relative */
2087 0, /* bitpos */
2088 complain_overflow_dont, /* complain_on_overflow */
2089 ppc64_elf_ha_reloc, /* special_function */
2090 "R_PPC64_ADDR16_HIGHA", /* name */
2091 FALSE, /* partial_inplace */
2092 0, /* src_mask */
2093 0xffff, /* dst_mask */
2094 FALSE), /* pcrel_offset */
2095
2096 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2097 HOWTO (R_PPC64_DTPREL16_HIGH,
2098 16, /* rightshift */
2099 1, /* size (0 = byte, 1 = short, 2 = long) */
2100 16, /* bitsize */
2101 FALSE, /* pc_relative */
2102 0, /* bitpos */
2103 complain_overflow_dont, /* complain_on_overflow */
2104 ppc64_elf_unhandled_reloc, /* special_function */
2105 "R_PPC64_DTPREL16_HIGH", /* name */
2106 FALSE, /* partial_inplace */
2107 0, /* src_mask */
2108 0xffff, /* dst_mask */
2109 FALSE), /* pcrel_offset */
2110
2111 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2112 HOWTO (R_PPC64_DTPREL16_HIGHA,
2113 16, /* rightshift */
2114 1, /* size (0 = byte, 1 = short, 2 = long) */
2115 16, /* bitsize */
2116 FALSE, /* pc_relative */
2117 0, /* bitpos */
2118 complain_overflow_dont, /* complain_on_overflow */
2119 ppc64_elf_unhandled_reloc, /* special_function */
2120 "R_PPC64_DTPREL16_HIGHA", /* name */
2121 FALSE, /* partial_inplace */
2122 0, /* src_mask */
2123 0xffff, /* dst_mask */
2124 FALSE), /* pcrel_offset */
2125
2126 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2127 HOWTO (R_PPC64_TPREL16_HIGH,
2128 16, /* rightshift */
2129 1, /* size (0 = byte, 1 = short, 2 = long) */
2130 16, /* bitsize */
2131 FALSE, /* pc_relative */
2132 0, /* bitpos */
2133 complain_overflow_dont, /* complain_on_overflow */
2134 ppc64_elf_unhandled_reloc, /* special_function */
2135 "R_PPC64_TPREL16_HIGH", /* name */
2136 FALSE, /* partial_inplace */
2137 0, /* src_mask */
2138 0xffff, /* dst_mask */
2139 FALSE), /* pcrel_offset */
2140
2141 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2142 HOWTO (R_PPC64_TPREL16_HIGHA,
2143 16, /* rightshift */
2144 1, /* size (0 = byte, 1 = short, 2 = long) */
2145 16, /* bitsize */
2146 FALSE, /* pc_relative */
2147 0, /* bitpos */
2148 complain_overflow_dont, /* complain_on_overflow */
2149 ppc64_elf_unhandled_reloc, /* special_function */
2150 "R_PPC64_TPREL16_HIGHA", /* name */
2151 FALSE, /* partial_inplace */
2152 0, /* src_mask */
2153 0xffff, /* dst_mask */
2154 FALSE), /* pcrel_offset */
2155
006589cf
AM
2156 /* Marker reloc on ELFv2 large-model function entry. */
2157 HOWTO (R_PPC64_ENTRY,
2158 0, /* rightshift */
2159 2, /* size (0 = byte, 1 = short, 2 = long) */
2160 32, /* bitsize */
2161 FALSE, /* pc_relative */
2162 0, /* bitpos */
2163 complain_overflow_dont, /* complain_on_overflow */
2164 bfd_elf_generic_reloc, /* special_function */
2165 "R_PPC64_ENTRY", /* name */
2166 FALSE, /* partial_inplace */
2167 0, /* src_mask */
2168 0, /* dst_mask */
2169 FALSE), /* pcrel_offset */
2170
45965137
AM
2171 /* Like ADDR64, but use local entry point of function. */
2172 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2173 0, /* rightshift */
2174 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2175 64, /* bitsize */
2176 FALSE, /* pc_relative */
2177 0, /* bitpos */
2178 complain_overflow_dont, /* complain_on_overflow */
2179 bfd_elf_generic_reloc, /* special_function */
2180 "R_PPC64_ADDR64_LOCAL", /* name */
2181 FALSE, /* partial_inplace */
2182 0, /* src_mask */
2183 ONES (64), /* dst_mask */
2184 FALSE), /* pcrel_offset */
2185
5bd4f169
AM
2186 /* GNU extension to record C++ vtable hierarchy. */
2187 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2188 0, /* rightshift */
2189 0, /* size (0 = byte, 1 = short, 2 = long) */
2190 0, /* bitsize */
b34976b6 2191 FALSE, /* pc_relative */
5bd4f169
AM
2192 0, /* bitpos */
2193 complain_overflow_dont, /* complain_on_overflow */
2194 NULL, /* special_function */
2195 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2196 FALSE, /* partial_inplace */
5bd4f169
AM
2197 0, /* src_mask */
2198 0, /* dst_mask */
b34976b6 2199 FALSE), /* pcrel_offset */
5bd4f169
AM
2200
2201 /* GNU extension to record C++ vtable member usage. */
2202 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2203 0, /* rightshift */
2204 0, /* size (0 = byte, 1 = short, 2 = long) */
2205 0, /* bitsize */
b34976b6 2206 FALSE, /* pc_relative */
5bd4f169
AM
2207 0, /* bitpos */
2208 complain_overflow_dont, /* complain_on_overflow */
2209 NULL, /* special_function */
2210 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2211 FALSE, /* partial_inplace */
5bd4f169
AM
2212 0, /* src_mask */
2213 0, /* dst_mask */
b34976b6 2214 FALSE), /* pcrel_offset */
5bd4f169
AM
2215};
2216
2217\f
2218/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2219 be done. */
2220
2221static void
4ce794b7 2222ppc_howto_init (void)
5bd4f169
AM
2223{
2224 unsigned int i, type;
2225
a4b6fadd 2226 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
2227 {
2228 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 2229 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
2230 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2231 }
2232}
2233
2234static reloc_howto_type *
f3185997 2235ppc64_elf_reloc_type_lookup (bfd *abfd,
4ce794b7 2236 bfd_reloc_code_real_type code)
5bd4f169 2237{
411e1bfb 2238 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2239
2240 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2241 /* Initialize howto table if needed. */
2242 ppc_howto_init ();
2243
4ce794b7 2244 switch (code)
5bd4f169
AM
2245 {
2246 default:
f3185997 2247 /* xgettext:c-format */
e8f5af78 2248 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
f3185997 2249 bfd_set_error (bfd_error_bad_value);
4ce794b7 2250 return NULL;
5bd4f169 2251
411e1bfb
AM
2252 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2253 break;
2254 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2255 break;
2256 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2257 break;
2258 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2259 break;
2260 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2261 break;
2262 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2263 break;
f9c6b907
AM
2264 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2265 break;
411e1bfb 2266 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2267 break;
f9c6b907
AM
2268 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2269 break;
411e1bfb 2270 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2271 break;
411e1bfb 2272 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2273 break;
411e1bfb 2274 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2275 break;
411e1bfb 2276 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2277 break;
411e1bfb 2278 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2279 break;
411e1bfb 2280 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2281 break;
411e1bfb 2282 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2283 break;
411e1bfb 2284 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2285 break;
411e1bfb 2286 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2287 break;
411e1bfb 2288 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2289 break;
411e1bfb 2290 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2291 break;
411e1bfb 2292 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2293 break;
411e1bfb 2294 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2295 break;
411e1bfb 2296 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2297 break;
411e1bfb 2298 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2299 break;
411e1bfb 2300 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2301 break;
411e1bfb 2302 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2303 break;
411e1bfb 2304 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2305 break;
411e1bfb 2306 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2307 break;
411e1bfb 2308 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2309 break;
411e1bfb 2310 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2311 break;
411e1bfb 2312 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2313 break;
411e1bfb 2314 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2315 break;
411e1bfb 2316 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2317 break;
411e1bfb 2318 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2319 break;
411e1bfb 2320 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2321 break;
411e1bfb 2322 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2323 break;
411e1bfb 2324 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2325 break;
411e1bfb 2326 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2327 break;
411e1bfb 2328 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2329 break;
411e1bfb 2330 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2331 break;
411e1bfb 2332 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2333 break;
411e1bfb 2334 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2335 break;
411e1bfb 2336 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2337 break;
411e1bfb 2338 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2339 break;
411e1bfb 2340 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2341 break;
411e1bfb 2342 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2343 break;
411e1bfb 2344 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2345 break;
411e1bfb 2346 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2347 break;
411e1bfb 2348 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2349 break;
411e1bfb 2350 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2351 break;
411e1bfb 2352 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2353 break;
411e1bfb 2354 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2355 break;
411e1bfb 2356 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2357 break;
411e1bfb 2358 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2359 break;
411e1bfb 2360 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2361 break;
411e1bfb 2362 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2363 break;
411e1bfb 2364 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2365 break;
411e1bfb 2366 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2367 break;
411e1bfb 2368 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2369 break;
411e1bfb 2370 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2371 break;
411e1bfb 2372 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2373 break;
411e1bfb 2374 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2375 break;
727fc41e
AM
2376 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2377 break;
2378 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2379 break;
411e1bfb 2380 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2381 break;
411e1bfb 2382 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2383 break;
411e1bfb 2384 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2385 break;
411e1bfb 2386 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2387 break;
f9c6b907
AM
2388 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2389 break;
411e1bfb 2390 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2391 break;
f9c6b907
AM
2392 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2393 break;
411e1bfb 2394 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2395 break;
411e1bfb
AM
2396 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2397 break;
2398 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2399 break;
2400 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2401 break;
f9c6b907
AM
2402 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2403 break;
411e1bfb
AM
2404 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2405 break;
f9c6b907
AM
2406 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2407 break;
411e1bfb
AM
2408 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2409 break;
2410 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2411 break;
2412 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2413 break;
2414 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2415 break;
2416 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2417 break;
2418 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2419 break;
2420 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2421 break;
2422 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2423 break;
2424 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2425 break;
2426 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2427 break;
2428 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2429 break;
2430 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2431 break;
2432 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2433 break;
2434 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2435 break;
2436 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2437 break;
2438 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2439 break;
2440 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2441 break;
2442 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2443 break;
2444 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2445 break;
2446 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2447 break;
2448 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2449 break;
2450 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2451 break;
2452 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2453 break;
2454 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2455 break;
2456 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2457 break;
2458 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2459 break;
2460 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2461 break;
2462 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2463 break;
2464 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2465 break;
25f23106
AM
2466 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2467 break;
2468 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2469 break;
2470 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2471 break;
2472 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2473 break;
7ba71655
AM
2474 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2475 break;
a680de9a
PB
2476 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2477 break;
006589cf
AM
2478 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2479 break;
45965137
AM
2480 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2481 break;
411e1bfb
AM
2482 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2483 break;
2484 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2485 break;
2486 }
2487
4ce794b7 2488 return ppc64_elf_howto_table[r];
5bd4f169
AM
2489};
2490
157090f7
AM
2491static reloc_howto_type *
2492ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2493 const char *r_name)
2494{
2495 unsigned int i;
2496
a4b6fadd 2497 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
2498 if (ppc64_elf_howto_raw[i].name != NULL
2499 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2500 return &ppc64_elf_howto_raw[i];
2501
f3185997 2502
157090f7
AM
2503 return NULL;
2504}
2505
5bd4f169
AM
2506/* Set the howto pointer for a PowerPC ELF reloc. */
2507
f3185997 2508static bfd_boolean
4aef7643 2509ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 2510 Elf_Internal_Rela *dst)
5bd4f169 2511{
65f38f15
AM
2512 unsigned int type;
2513
ef60b7ff 2514 /* Initialize howto table if needed. */
5bd4f169 2515 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2516 ppc_howto_init ();
2517
65f38f15 2518 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 2519 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 2520 {
695344c0 2521 /* xgettext:c-format */
0aa13fee 2522 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 2523 abfd, type);
f3185997
NC
2524 bfd_set_error (bfd_error_bad_value);
2525 return FALSE;
d0fb9a8d 2526 }
65f38f15 2527 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
2528 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
2529 {
2530 /* xgettext:c-format */
2531 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2532 abfd, type);
2533 bfd_set_error (bfd_error_bad_value);
2534 return FALSE;
2535 }
2536
2537 return TRUE;
5bd4f169
AM
2538}
2539
04c9666a 2540/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2541
2542static bfd_reloc_status_type
4ce794b7
AM
2543ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2544 void *data, asection *input_section,
2545 bfd *output_bfd, char **error_message)
5bd4f169 2546{
a680de9a
PB
2547 enum elf_ppc64_reloc_type r_type;
2548 long insn;
2549 bfd_size_type octets;
3de43e7b 2550 bfd_vma value;
a680de9a 2551
805fc799
AM
2552 /* If this is a relocatable link (output_bfd test tells us), just
2553 call the generic function. Any adjustment will be done at final
2554 link time. */
2555 if (output_bfd != NULL)
cedb70c5 2556 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2557 input_section, output_bfd, error_message);
2558
2559 /* Adjust the addend for sign extension of the low 16 bits.
2560 We won't actually be using the low 16 bits, so trashing them
2561 doesn't matter. */
2562 reloc_entry->addend += 0x8000;
a680de9a
PB
2563 r_type = reloc_entry->howto->type;
2564 if (r_type != R_PPC64_REL16DX_HA)
2565 return bfd_reloc_continue;
2566
2567 value = 0;
2568 if (!bfd_is_com_section (symbol->section))
2569 value = symbol->value;
2570 value += (reloc_entry->addend
2571 + symbol->section->output_offset
2572 + symbol->section->output_section->vma);
2573 value -= (reloc_entry->address
2574 + input_section->output_offset
2575 + input_section->output_section->vma);
3de43e7b 2576 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
2577
2578 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2579 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2580 insn &= ~0x1fffc1;
3de43e7b 2581 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 2582 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 2583 if (value + 0x8000 > 0xffff)
a680de9a
PB
2584 return bfd_reloc_overflow;
2585 return bfd_reloc_ok;
805fc799 2586}
5bd4f169 2587
2441e016
AM
2588static bfd_reloc_status_type
2589ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2590 void *data, asection *input_section,
2591 bfd *output_bfd, char **error_message)
2592{
2593 if (output_bfd != NULL)
2594 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2595 input_section, output_bfd, error_message);
2596
699733f6
AM
2597 if (strcmp (symbol->section->name, ".opd") == 0
2598 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2599 {
2600 bfd_vma dest = opd_entry_value (symbol->section,
2601 symbol->value + reloc_entry->addend,
aef36ac1 2602 NULL, NULL, FALSE);
2441e016
AM
2603 if (dest != (bfd_vma) -1)
2604 reloc_entry->addend = dest - (symbol->value
2605 + symbol->section->output_section->vma
2606 + symbol->section->output_offset);
2607 }
810d4e75
AM
2608 else
2609 {
2610 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2611
2612 if (symbol->section->owner != abfd
9f284bf9 2613 && symbol->section->owner != NULL
810d4e75
AM
2614 && abiversion (symbol->section->owner) >= 2)
2615 {
2616 unsigned int i;
2617
2618 for (i = 0; i < symbol->section->owner->symcount; ++i)
2619 {
2620 asymbol *symdef = symbol->section->owner->outsymbols[i];
2621
2622 if (strcmp (symdef->name, symbol->name) == 0)
2623 {
2624 elfsym = (elf_symbol_type *) symdef;
2625 break;
2626 }
2627 }
2628 }
2629 reloc_entry->addend
2630 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2631 }
2441e016
AM
2632 return bfd_reloc_continue;
2633}
2634
805fc799 2635static bfd_reloc_status_type
4ce794b7
AM
2636ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2637 void *data, asection *input_section,
2638 bfd *output_bfd, char **error_message)
805fc799
AM
2639{
2640 long insn;
04c9666a 2641 enum elf_ppc64_reloc_type r_type;
805fc799 2642 bfd_size_type octets;
794e51c0
AM
2643 /* Assume 'at' branch hints. */
2644 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2645
2646 /* If this is a relocatable link (output_bfd test tells us), just
2647 call the generic function. Any adjustment will be done at final
2648 link time. */
5bd4f169 2649 if (output_bfd != NULL)
cedb70c5 2650 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2651 input_section, output_bfd, error_message);
2652
2653 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2654 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2655 insn &= ~(0x01 << 21);
4ce794b7 2656 r_type = reloc_entry->howto->type;
805fc799
AM
2657 if (r_type == R_PPC64_ADDR14_BRTAKEN
2658 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2659 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2660
794e51c0 2661 if (is_isa_v2)
5bd4f169 2662 {
805fc799
AM
2663 /* Set 'a' bit. This is 0b00010 in BO field for branch
2664 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2665 for branch on CTR insns (BO == 1a00t or 1a01t). */
2666 if ((insn & (0x14 << 21)) == (0x04 << 21))
2667 insn |= 0x02 << 21;
2668 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2669 insn |= 0x08 << 21;
2670 else
2441e016 2671 goto out;
5bd4f169 2672 }
805fc799
AM
2673 else
2674 {
2675 bfd_vma target = 0;
2676 bfd_vma from;
5bd4f169 2677
805fc799
AM
2678 if (!bfd_is_com_section (symbol->section))
2679 target = symbol->value;
2680 target += symbol->section->output_section->vma;
2681 target += symbol->section->output_offset;
2682 target += reloc_entry->addend;
5bd4f169 2683
805fc799
AM
2684 from = (reloc_entry->address
2685 + input_section->output_offset
2686 + input_section->output_section->vma);
5bd4f169 2687
805fc799
AM
2688 /* Invert 'y' bit if not the default. */
2689 if ((bfd_signed_vma) (target - from) < 0)
2690 insn ^= 0x01 << 21;
2691 }
4ce794b7 2692 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2693 out:
2694 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2695 input_section, output_bfd, error_message);
805fc799 2696}
5bd4f169 2697
805fc799 2698static bfd_reloc_status_type
4ce794b7
AM
2699ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2700 void *data, asection *input_section,
2701 bfd *output_bfd, char **error_message)
805fc799
AM
2702{
2703 /* If this is a relocatable link (output_bfd test tells us), just
2704 call the generic function. Any adjustment will be done at final
2705 link time. */
2706 if (output_bfd != NULL)
cedb70c5 2707 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2708 input_section, output_bfd, error_message);
5bd4f169 2709
805fc799
AM
2710 /* Subtract the symbol section base address. */
2711 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2712 return bfd_reloc_continue;
2713}
2714
805fc799 2715static bfd_reloc_status_type
4ce794b7
AM
2716ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2717 void *data, asection *input_section,
2718 bfd *output_bfd, char **error_message)
805fc799
AM
2719{
2720 /* If this is a relocatable link (output_bfd test tells us), just
2721 call the generic function. Any adjustment will be done at final
2722 link time. */
2723 if (output_bfd != NULL)
cedb70c5 2724 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2725 input_section, output_bfd, error_message);
2726
2727 /* Subtract the symbol section base address. */
2728 reloc_entry->addend -= symbol->section->output_section->vma;
2729
2730 /* Adjust the addend for sign extension of the low 16 bits. */
2731 reloc_entry->addend += 0x8000;
2732 return bfd_reloc_continue;
2733}
2734
2735static bfd_reloc_status_type
4ce794b7
AM
2736ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2737 void *data, asection *input_section,
2738 bfd *output_bfd, char **error_message)
805fc799
AM
2739{
2740 bfd_vma TOCstart;
2741
2742 /* If this is a relocatable link (output_bfd test tells us), just
2743 call the generic function. Any adjustment will be done at final
2744 link time. */
2745 if (output_bfd != NULL)
cedb70c5 2746 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2747 input_section, output_bfd, error_message);
2748
2749 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2750 if (TOCstart == 0)
1c865ab2 2751 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2752
2753 /* Subtract the TOC base address. */
2754 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2755 return bfd_reloc_continue;
2756}
2757
2758static bfd_reloc_status_type
4ce794b7
AM
2759ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2760 void *data, asection *input_section,
2761 bfd *output_bfd, char **error_message)
805fc799
AM
2762{
2763 bfd_vma TOCstart;
2764
2765 /* If this is a relocatable link (output_bfd test tells us), just
2766 call the generic function. Any adjustment will be done at final
2767 link time. */
2768 if (output_bfd != NULL)
cedb70c5 2769 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2770 input_section, output_bfd, error_message);
2771
2772 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2773 if (TOCstart == 0)
1c865ab2 2774 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2775
2776 /* Subtract the TOC base address. */
2777 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2778
2779 /* Adjust the addend for sign extension of the low 16 bits. */
2780 reloc_entry->addend += 0x8000;
2781 return bfd_reloc_continue;
2782}
2783
2784static bfd_reloc_status_type
4ce794b7
AM
2785ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2786 void *data, asection *input_section,
2787 bfd *output_bfd, char **error_message)
805fc799
AM
2788{
2789 bfd_vma TOCstart;
2790 bfd_size_type octets;
2791
2792 /* If this is a relocatable link (output_bfd test tells us), just
2793 call the generic function. Any adjustment will be done at final
2794 link time. */
2795 if (output_bfd != NULL)
cedb70c5 2796 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2797 input_section, output_bfd, error_message);
2798
2799 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2800 if (TOCstart == 0)
1c865ab2 2801 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2802
2803 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2804 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2805 return bfd_reloc_ok;
2806}
2807
2808static bfd_reloc_status_type
4ce794b7
AM
2809ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2810 void *data, asection *input_section,
2811 bfd *output_bfd, char **error_message)
805fc799
AM
2812{
2813 /* If this is a relocatable link (output_bfd test tells us), just
2814 call the generic function. Any adjustment will be done at final
2815 link time. */
2816 if (output_bfd != NULL)
cedb70c5 2817 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2818 input_section, output_bfd, error_message);
2819
2820 if (error_message != NULL)
2821 {
2822 static char buf[60];
2823 sprintf (buf, "generic linker can't handle %s",
2824 reloc_entry->howto->name);
2825 *error_message = buf;
2826 }
2827 return bfd_reloc_dangerous;
2828}
2829
927be08e
AM
2830/* Track GOT entries needed for a given symbol. We might need more
2831 than one got entry per symbol. */
2832struct got_entry
2833{
2834 struct got_entry *next;
2835
2836 /* The symbol addend that we'll be placing in the GOT. */
2837 bfd_vma addend;
2838
2839 /* Unlike other ELF targets, we use separate GOT entries for the same
2840 symbol referenced from different input files. This is to support
2841 automatic multiple TOC/GOT sections, where the TOC base can vary
2842 from one input file to another. After partitioning into TOC groups
2843 we merge entries within the group.
2844
2845 Point to the BFD owning this GOT entry. */
2846 bfd *owner;
2847
2848 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2849 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2850 unsigned char tls_type;
927be08e
AM
2851
2852 /* Non-zero if got.ent points to real entry. */
f961d9dd 2853 unsigned char is_indirect;
927be08e
AM
2854
2855 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2856 union
2857 {
2858 bfd_signed_vma refcount;
2859 bfd_vma offset;
2860 struct got_entry *ent;
2861 } got;
2862};
2863
2864/* The same for PLT. */
2865struct plt_entry
2866{
2867 struct plt_entry *next;
2868
2869 bfd_vma addend;
2870
2871 union
2872 {
2873 bfd_signed_vma refcount;
2874 bfd_vma offset;
2875 } plt;
2876};
2877
e717da7e
AM
2878struct ppc64_elf_obj_tdata
2879{
2880 struct elf_obj_tdata elf;
2881
2882 /* Shortcuts to dynamic linker sections. */
2883 asection *got;
2884 asection *relgot;
2885
b3fac117
AM
2886 /* Used during garbage collection. We attach global symbols defined
2887 on removed .opd entries to this section so that the sym is removed. */
2888 asection *deleted_section;
81688140 2889
927be08e 2890 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2891 sections means we potentially need one of these for each input bfd. */
927be08e 2892 struct got_entry tlsld_got;
8860955f 2893
729eabd5
AM
2894 union {
2895 /* A copy of relocs before they are modified for --emit-relocs. */
2896 Elf_Internal_Rela *relocs;
2897
2898 /* Section contents. */
2899 bfd_byte *contents;
2900 } opd;
d77c8a4b
AM
2901
2902 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2903 the reloc to be in the range -32768 to 32767. */
98528052
AM
2904 unsigned int has_small_toc_reloc : 1;
2905
560c8763
AM
2906 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2907 instruction not one we handle. */
2908 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2909};
2910
2911#define ppc64_elf_tdata(bfd) \
2912 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2913
2914#define ppc64_tlsld_got(bfd) \
2915 (&ppc64_elf_tdata (bfd)->tlsld_got)
2916
0c8d6e5c
AM
2917#define is_ppc64_elf(bfd) \
2918 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2919 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2920
e717da7e
AM
2921/* Override the generic function because we store some extras. */
2922
2923static bfd_boolean
2924ppc64_elf_mkobject (bfd *abfd)
2925{
0ffa91dd 2926 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2927 PPC64_ELF_DATA);
e717da7e
AM
2928}
2929
feee612b 2930/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 2931 default is 32 bit. Also select arch based on apuinfo. */
feee612b 2932
b34976b6 2933static bfd_boolean
4ce794b7 2934ppc64_elf_object_p (bfd *abfd)
feee612b 2935{
14b57c7c
AM
2936 if (!abfd->arch_info->the_default)
2937 return TRUE;
2938
2939 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
2940 {
2941 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2942
2943 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2944 {
2945 /* Relies on arch after 32 bit default being 64 bit default. */
2946 abfd->arch_info = abfd->arch_info->next;
2947 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2948 }
2949 }
14b57c7c 2950 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
2951}
2952
d37c89e5
AM
2953/* Support for core dump NOTE sections. */
2954
2955static bfd_boolean
2956ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2957{
eea6121a 2958 size_t offset, size;
d37c89e5
AM
2959
2960 if (note->descsz != 504)
2961 return FALSE;
2962
2963 /* pr_cursig */
228e534f 2964 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2965
2966 /* pr_pid */
228e534f 2967 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2968
2969 /* pr_reg */
2970 offset = 112;
eea6121a 2971 size = 384;
d37c89e5
AM
2972
2973 /* Make a ".reg/999" section. */
2974 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2975 size, note->descpos + offset);
d37c89e5
AM
2976}
2977
2978static bfd_boolean
2979ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2980{
2981 if (note->descsz != 136)
2982 return FALSE;
2983
228e534f 2984 elf_tdata (abfd)->core->pid
bc989cdc 2985 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2986 elf_tdata (abfd)->core->program
d37c89e5 2987 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2988 elf_tdata (abfd)->core->command
d37c89e5
AM
2989 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2990
2991 return TRUE;
2992}
2993
183e98be
AM
2994static char *
2995ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2996 ...)
2997{
2998 switch (note_type)
2999 {
3000 default:
3001 return NULL;
3002
3003 case NT_PRPSINFO:
3004 {
3005 char data[136];
3006 va_list ap;
3007
3008 va_start (ap, note_type);
75cd47ed 3009 memset (data, 0, sizeof (data));
183e98be
AM
3010 strncpy (data + 40, va_arg (ap, const char *), 16);
3011 strncpy (data + 56, va_arg (ap, const char *), 80);
3012 va_end (ap);
3013 return elfcore_write_note (abfd, buf, bufsiz,
3014 "CORE", note_type, data, sizeof (data));
3015 }
3016
3017 case NT_PRSTATUS:
3018 {
3019 char data[504];
3020 va_list ap;
3021 long pid;
3022 int cursig;
3023 const void *greg;
3024
3025 va_start (ap, note_type);
3026 memset (data, 0, 112);
3027 pid = va_arg (ap, long);
3028 bfd_put_32 (abfd, pid, data + 32);
3029 cursig = va_arg (ap, int);
3030 bfd_put_16 (abfd, cursig, data + 12);
3031 greg = va_arg (ap, const void *);
3032 memcpy (data + 112, greg, 384);
3033 memset (data + 496, 0, 8);
3034 va_end (ap);
3035 return elfcore_write_note (abfd, buf, bufsiz,
3036 "CORE", note_type, data, sizeof (data));
3037 }
3038 }
3039}
3040
5d35169e
AM
3041/* Add extra PPC sections. */
3042
b35d266b 3043static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 3044{
07d6d2b8
AM
3045 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3046 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3047 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
3048 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3049 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 3050 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 3051 { NULL, 0, 0, 0, 0 }
5d35169e
AM
3052};
3053
7c8fe5c4
AM
3054enum _ppc64_sec_type {
3055 sec_normal = 0,
3056 sec_opd = 1,
3057 sec_toc = 2
3058};
3059
f0abc2a1
AM
3060struct _ppc64_elf_section_data
3061{
3062 struct bfd_elf_section_data elf;
411e1bfb 3063
f0abc2a1
AM
3064 union
3065 {
51aecdc5
AM
3066 /* An array with one entry for each opd function descriptor,
3067 and some spares since opd entries may be either 16 or 24 bytes. */
3068#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
3069 struct _opd_sec_data
3070 {
3071 /* Points to the function code section for local opd entries. */
3072 asection **func_sec;
3073
3074 /* After editing .opd, adjust references to opd local syms. */
3075 long *adjust;
3076 } opd;
7c8fe5c4 3077
3a71aa26
AM
3078 /* An array for toc sections, indexed by offset/8. */
3079 struct _toc_sec_data
3080 {
3081 /* Specifies the relocation symbol index used at a given toc offset. */
3082 unsigned *symndx;
3083
3084 /* And the relocation addend. */
3085 bfd_vma *add;
3086 } toc;
7c8fe5c4
AM
3087 } u;
3088
3089 enum _ppc64_sec_type sec_type:2;
411e1bfb 3090
7c8fe5c4
AM
3091 /* Flag set when small branches are detected. Used to
3092 select suitable defaults for the stub group size. */
3093 unsigned int has_14bit_branch:1;
f0abc2a1
AM
3094};
3095
3096#define ppc64_elf_section_data(sec) \
411e1bfb 3097 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
3098
3099static bfd_boolean
4ce794b7 3100ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 3101{
f592407e
AM
3102 if (!sec->used_by_bfd)
3103 {
3104 struct _ppc64_elf_section_data *sdata;
3105 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 3106
f592407e
AM
3107 sdata = bfd_zalloc (abfd, amt);
3108 if (sdata == NULL)
3109 return FALSE;
3110 sec->used_by_bfd = sdata;
3111 }
f0abc2a1
AM
3112
3113 return _bfd_elf_new_section_hook (abfd, sec);
3114}
4025353c 3115
74f0fb50 3116static struct _opd_sec_data *
4025353c
AM
3117get_opd_info (asection * sec)
3118{
3119 if (sec != NULL
3120 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3121 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3122 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3123 return NULL;
3124}
90e3cdf2
JJ
3125\f
3126/* Parameters for the qsort hook. */
90e3cdf2 3127static bfd_boolean synthetic_relocatable;
cd285db5 3128static asection *synthetic_opd;
90e3cdf2 3129
699733f6 3130/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3131
3132static int
3133compare_symbols (const void *ap, const void *bp)
3134{
3135 const asymbol *a = * (const asymbol **) ap;
3136 const asymbol *b = * (const asymbol **) bp;
3137
699733f6
AM
3138 /* Section symbols first. */
3139 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3140 return -1;
699733f6 3141 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3142 return 1;
3143
699733f6 3144 /* then .opd symbols. */
cd285db5
AM
3145 if (synthetic_opd != NULL)
3146 {
3147 if (strcmp (a->section->name, ".opd") == 0
3148 && strcmp (b->section->name, ".opd") != 0)
3149 return -1;
3150 if (strcmp (a->section->name, ".opd") != 0
3151 && strcmp (b->section->name, ".opd") == 0)
3152 return 1;
3153 }
90e3cdf2 3154
699733f6 3155 /* then other code symbols. */
90e3cdf2
JJ
3156 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3157 == (SEC_CODE | SEC_ALLOC)
3158 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3159 != (SEC_CODE | SEC_ALLOC))
3160 return -1;
3161
3162 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3163 != (SEC_CODE | SEC_ALLOC)
3164 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3165 == (SEC_CODE | SEC_ALLOC))
3166 return 1;
3167
3168 if (synthetic_relocatable)
3169 {
3170 if (a->section->id < b->section->id)
3171 return -1;
3172
3173 if (a->section->id > b->section->id)
3174 return 1;
3175 }
3176
3177 if (a->value + a->section->vma < b->value + b->section->vma)
3178 return -1;
3179
3180 if (a->value + a->section->vma > b->value + b->section->vma)
3181 return 1;
3182
4d35a0aa
AM
3183 /* For syms with the same value, prefer strong dynamic global function
3184 syms over other syms. */
3185 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3186 return -1;
3187
3188 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3189 return 1;
3190
3191 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3192 return -1;
3193
3194 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3195 return 1;
3196
3197 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3198 return -1;
3199
3200 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3201 return 1;
3202
3203 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3204 return -1;
3205
3206 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3207 return 1;
3208
aaed6f5b 3209 return a > b;
90e3cdf2
JJ
3210}
3211
699733f6 3212/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3213
699733f6 3214static asymbol *
7292b3ac 3215sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3216{
699733f6 3217 long mid;
90e3cdf2 3218
7292b3ac 3219 if (id == (unsigned) -1)
699733f6
AM
3220 {
3221 while (lo < hi)
3222 {
3223 mid = (lo + hi) >> 1;
3224 if (syms[mid]->value + syms[mid]->section->vma < value)
3225 lo = mid + 1;
3226 else if (syms[mid]->value + syms[mid]->section->vma > value)
3227 hi = mid;
3228 else
3229 return syms[mid];
3230 }
3231 }
3232 else
3233 {
3234 while (lo < hi)
3235 {
3236 mid = (lo + hi) >> 1;
3237 if (syms[mid]->section->id < id)
3238 lo = mid + 1;
3239 else if (syms[mid]->section->id > id)
3240 hi = mid;
3241 else if (syms[mid]->value < value)
3242 lo = mid + 1;
3243 else if (syms[mid]->value > value)
3244 hi = mid;
3245 else
3246 return syms[mid];
3247 }
3248 }
3249 return NULL;
90e3cdf2
JJ
3250}
3251
468392fb
AM
3252static bfd_boolean
3253section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3254{
3255 bfd_vma vma = *(bfd_vma *) ptr;
3256 return ((section->flags & SEC_ALLOC) != 0
3257 && section->vma <= vma
3258 && vma < section->vma + section->size);
3259}
3260
699733f6 3261/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3262 entry syms. Also generate @plt symbols for the glink branch table.
3263 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3264
3265static long
a7535cf3
AM
3266ppc64_elf_get_synthetic_symtab (bfd *abfd,
3267 long static_count, asymbol **static_syms,
3268 long dyn_count, asymbol **dyn_syms,
c9727e01 3269 asymbol **ret)
90e3cdf2
JJ
3270{
3271 asymbol *s;
699733f6
AM
3272 long i;
3273 long count;
90e3cdf2 3274 char *names;
a7535cf3 3275 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3276 asection *opd = NULL;
90e3cdf2 3277 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3278 asymbol **syms;
ee67d69a 3279 int abi = abiversion (abfd);
90e3cdf2
JJ
3280
3281 *ret = NULL;
3282
ee67d69a
AM
3283 if (abi < 2)
3284 {
3285 opd = bfd_get_section_by_name (abfd, ".opd");
3286 if (opd == NULL && abi == 1)
3287 return 0;
3288 }
90e3cdf2 3289
a5259595
AM
3290 syms = NULL;
3291 codesecsym = 0;
3292 codesecsymend = 0;
3293 secsymend = 0;
3294 opdsymend = 0;
3295 symcount = 0;
3296 if (opd != NULL)
c9727e01 3297 {
a5259595
AM
3298 symcount = static_count;
3299 if (!relocatable)
3300 symcount += dyn_count;
3301 if (symcount == 0)
3302 return 0;
c9727e01 3303
a5259595
AM
3304 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3305 if (syms == NULL)
3306 return -1;
90e3cdf2 3307
a5259595
AM
3308 if (!relocatable && static_count != 0 && dyn_count != 0)
3309 {
3310 /* Use both symbol tables. */
3311 memcpy (syms, static_syms, static_count * sizeof (*syms));
3312 memcpy (syms + static_count, dyn_syms,
3313 (dyn_count + 1) * sizeof (*syms));
3314 }
3315 else if (!relocatable && static_count == 0)
3316 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3317 else
3318 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 3319
a5259595
AM
3320 synthetic_relocatable = relocatable;
3321 synthetic_opd = opd;
3322 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3323
a5259595
AM
3324 if (!relocatable && symcount > 1)
3325 {
3326 long j;
3327 /* Trim duplicate syms, since we may have merged the normal and
3328 dynamic symbols. Actually, we only care about syms that have
3329 different values, so trim any with the same value. */
3330 for (i = 1, j = 1; i < symcount; ++i)
3331 if (syms[i - 1]->value + syms[i - 1]->section->vma
3332 != syms[i]->value + syms[i]->section->vma)
3333 syms[j++] = syms[i];
3334 symcount = j;
3335 }
699733f6 3336
a5259595
AM
3337 i = 0;
3338 /* Note that here and in compare_symbols we can't compare opd and
3339 sym->section directly. With separate debug info files, the
3340 symbols will be extracted from the debug file while abfd passed
3341 to this function is the real binary. */
3342 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
3343 ++i;
3344 codesecsym = i;
3345
3346 for (; i < symcount; ++i)
3347 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3348 | SEC_THREAD_LOCAL))
3349 != (SEC_CODE | SEC_ALLOC))
3350 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3351 break;
3352 codesecsymend = i;
3353
3354 for (; i < symcount; ++i)
3355 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3356 break;
3357 secsymend = i;
3358
3359 for (; i < symcount; ++i)
3360 if (strcmp (syms[i]->section->name, ".opd") != 0)
3361 break;
3362 opdsymend = i;
3363
3364 for (; i < symcount; ++i)
3365 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3366 != (SEC_CODE | SEC_ALLOC))
3367 break;
3368 symcount = i;
3369 }
c9727e01 3370 count = 0;
90e3cdf2 3371
699733f6 3372 if (relocatable)
90e3cdf2 3373 {
699733f6
AM
3374 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3375 arelent *r;
3376 size_t size;
3377 long relcount;
90e3cdf2 3378
468392fb
AM
3379 if (opdsymend == secsymend)
3380 goto done;
3381
699733f6 3382 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3383 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3384 if (relcount == 0)
c9727e01 3385 goto done;
90e3cdf2 3386
7356fed5
AM
3387 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3388 {
3389 count = -1;
3390 goto done;
3391 }
3392
699733f6 3393 size = 0;
595da8c5 3394 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3395 {
3396 asymbol *sym;
90e3cdf2 3397
595da8c5 3398 while (r < opd->relocation + relcount
699733f6
AM
3399 && r->address < syms[i]->value + opd->vma)
3400 ++r;
90e3cdf2 3401
595da8c5 3402 if (r == opd->relocation + relcount)
699733f6 3403 break;
90e3cdf2 3404
699733f6
AM
3405 if (r->address != syms[i]->value + opd->vma)
3406 continue;
90e3cdf2 3407
699733f6
AM
3408 if (r->howto->type != R_PPC64_ADDR64)
3409 continue;
90e3cdf2 3410
699733f6
AM
3411 sym = *r->sym_ptr_ptr;
3412 if (!sym_exists_at (syms, opdsymend, symcount,
3413 sym->section->id, sym->value + r->addend))
3414 {
3415 ++count;
3416 size += sizeof (asymbol);
3417 size += strlen (syms[i]->name) + 2;
3418 }
3419 }
90e3cdf2 3420
c4b0b099
AM
3421 if (size == 0)
3422 goto done;
699733f6
AM
3423 s = *ret = bfd_malloc (size);
3424 if (s == NULL)
3425 {
7356fed5 3426 count = -1;
c9727e01 3427 goto done;
699733f6 3428 }
90e3cdf2 3429
699733f6 3430 names = (char *) (s + count);
90e3cdf2 3431
595da8c5 3432 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3433 {
699733f6 3434 asymbol *sym;
90e3cdf2 3435
595da8c5 3436 while (r < opd->relocation + relcount
699733f6
AM
3437 && r->address < syms[i]->value + opd->vma)
3438 ++r;
90e3cdf2 3439
595da8c5 3440 if (r == opd->relocation + relcount)
699733f6
AM
3441 break;
3442
3443 if (r->address != syms[i]->value + opd->vma)
3444 continue;
3445
3446 if (r->howto->type != R_PPC64_ADDR64)
3447 continue;
90e3cdf2 3448
699733f6
AM
3449 sym = *r->sym_ptr_ptr;
3450 if (!sym_exists_at (syms, opdsymend, symcount,
3451 sym->section->id, sym->value + r->addend))
3452 {
3453 size_t len;
3454
3455 *s = *syms[i];
6ba2a415 3456 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3457 s->section = sym->section;
3458 s->value = sym->value + r->addend;
3459 s->name = names;
3460 *names++ = '.';
3461 len = strlen (syms[i]->name);
3462 memcpy (names, syms[i]->name, len + 1);
3463 names += len + 1;
6f610d07
UW
3464 /* Have udata.p point back to the original symbol this
3465 synthetic symbol was derived from. */
3466 s->udata.p = syms[i];
699733f6
AM
3467 s++;
3468 }
3469 }
3470 }
3471 else
90e3cdf2 3472 {
468392fb 3473 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3474 bfd_byte *contents = NULL;
699733f6 3475 size_t size;
468392fb
AM
3476 long plt_count = 0;
3477 bfd_vma glink_vma = 0, resolv_vma = 0;
3478 asection *dynamic, *glink = NULL, *relplt = NULL;
3479 arelent *p;
90e3cdf2 3480
ee67d69a 3481 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3482 {
c4b0b099
AM
3483 free_contents_and_exit_err:
3484 count = -1;
ee67d69a 3485 free_contents_and_exit:
699733f6 3486 if (contents)
ee67d69a 3487 free (contents);
c9727e01 3488 goto done;
699733f6 3489 }
90e3cdf2 3490
699733f6
AM
3491 size = 0;
3492 for (i = secsymend; i < opdsymend; ++i)
3493 {
3494 bfd_vma ent;
90e3cdf2 3495
5ef11c02
AM
3496 /* Ignore bogus symbols. */
3497 if (syms[i]->value > opd->size - 8)
3498 continue;
3499
699733f6
AM
3500 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3501 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3502 {
3503 ++count;
3504 size += sizeof (asymbol);
3505 size += strlen (syms[i]->name) + 2;
3506 }
3507 }
90e3cdf2 3508
468392fb 3509 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3510 if (dyn_count != 0
3511 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3512 {
3513 bfd_byte *dynbuf, *extdyn, *extdynend;
3514 size_t extdynsize;
3515 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3516
3517 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3518 goto free_contents_and_exit_err;
468392fb
AM
3519
3520 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3521 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3522
3523 extdyn = dynbuf;
3524 extdynend = extdyn + dynamic->size;
3525 for (; extdyn < extdynend; extdyn += extdynsize)
3526 {
3527 Elf_Internal_Dyn dyn;
3528 (*swap_dyn_in) (abfd, extdyn, &dyn);
3529
3530 if (dyn.d_tag == DT_NULL)
3531 break;
3532
3533 if (dyn.d_tag == DT_PPC64_GLINK)
3534 {
9e390558
AM
3535 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3536 See comment in ppc64_elf_finish_dynamic_sections. */
3537 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
3538 /* The .glink section usually does not survive the final
3539 link; search for the section (usually .text) where the
3540 glink stubs now reside. */
3541 glink = bfd_sections_find_if (abfd, section_covers_vma,
3542 &glink_vma);
3543 break;
3544 }
3545 }
3546
3547 free (dynbuf);
3548 }
3549
3550 if (glink != NULL)
3551 {
3552 /* Determine __glink trampoline by reading the relative branch
3553 from the first glink stub. */
3554 bfd_byte buf[4];
b9e5796b
AM
3555 unsigned int off = 0;
3556
3557 while (bfd_get_section_contents (abfd, glink, buf,
3558 glink_vma + off - glink->vma, 4))
468392fb
AM
3559 {
3560 unsigned int insn = bfd_get_32 (abfd, buf);
3561 insn ^= B_DOT;
3562 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3563 {
3564 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3565 break;
3566 }
3567 off += 4;
3568 if (off > 4)
3569 break;
468392fb
AM
3570 }
3571
3572 if (resolv_vma)
3573 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3574
066ee829
AM
3575 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3576 if (relplt != NULL)
3577 {
3578 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3579 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3580 goto free_contents_and_exit_err;
68ffbac6 3581
066ee829
AM
3582 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3583 size += plt_count * sizeof (asymbol);
468392fb 3584
066ee829
AM
3585 p = relplt->relocation;
3586 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3587 {
3588 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3589 if (p->addend != 0)
3590 size += sizeof ("+0x") - 1 + 16;
3591 }
066ee829 3592 }
468392fb
AM
3593 }
3594
c4b0b099
AM
3595 if (size == 0)
3596 goto free_contents_and_exit;
699733f6
AM
3597 s = *ret = bfd_malloc (size);
3598 if (s == NULL)
c4b0b099 3599 goto free_contents_and_exit_err;
90e3cdf2 3600
468392fb 3601 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3602
699733f6 3603 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3604 {
699733f6 3605 bfd_vma ent;
90e3cdf2 3606
5ef11c02
AM
3607 if (syms[i]->value > opd->size - 8)
3608 continue;
3609
699733f6
AM
3610 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3611 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3612 {
c9727e01 3613 long lo, hi;
699733f6 3614 size_t len;
c9727e01 3615 asection *sec = abfd->sections;
90e3cdf2 3616
699733f6
AM
3617 *s = *syms[i];
3618 lo = codesecsym;
3619 hi = codesecsymend;
3620 while (lo < hi)
3621 {
c9727e01 3622 long mid = (lo + hi) >> 1;
699733f6
AM
3623 if (syms[mid]->section->vma < ent)
3624 lo = mid + 1;
3625 else if (syms[mid]->section->vma > ent)
3626 hi = mid;
3627 else
c9727e01
AM
3628 {
3629 sec = syms[mid]->section;
3630 break;
3631 }
699733f6
AM
3632 }
3633
c9727e01 3634 if (lo >= hi && lo > codesecsym)
699733f6 3635 sec = syms[lo - 1]->section;
699733f6
AM
3636
3637 for (; sec != NULL; sec = sec->next)
3638 {
3639 if (sec->vma > ent)
3640 break;
63524580
JK
3641 /* SEC_LOAD may not be set if SEC is from a separate debug
3642 info file. */
3643 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3644 break;
3645 if ((sec->flags & SEC_CODE) != 0)
3646 s->section = sec;
3647 }
6ba2a415 3648 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3649 s->value = ent - s->section->vma;
3650 s->name = names;
3651 *names++ = '.';
3652 len = strlen (syms[i]->name);
3653 memcpy (names, syms[i]->name, len + 1);
3654 names += len + 1;
6f610d07
UW
3655 /* Have udata.p point back to the original symbol this
3656 synthetic symbol was derived from. */
3657 s->udata.p = syms[i];
699733f6 3658 s++;
90e3cdf2 3659 }
90e3cdf2 3660 }
699733f6 3661 free (contents);
468392fb
AM
3662
3663 if (glink != NULL && relplt != NULL)
3664 {
3665 if (resolv_vma)
3666 {
3667 /* Add a symbol for the main glink trampoline. */
86a4952b 3668 memset (s, 0, sizeof *s);
468392fb 3669 s->the_bfd = abfd;
6ba2a415 3670 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3671 s->section = glink;
3672 s->value = resolv_vma - glink->vma;
3673 s->name = names;
3674 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3675 names += sizeof ("__glink_PLTresolve");
3676 s++;
3677 count++;
3678 }
3679
3680 /* FIXME: It would be very much nicer to put sym@plt on the
3681 stub rather than on the glink branch table entry. The
3682 objdump disassembler would then use a sensible symbol
3683 name on plt calls. The difficulty in doing so is
3684 a) finding the stubs, and,
3685 b) matching stubs against plt entries, and,
3686 c) there can be multiple stubs for a given plt entry.
3687
3688 Solving (a) could be done by code scanning, but older
3689 ppc64 binaries used different stubs to current code.
3690 (b) is the tricky one since you need to known the toc
3691 pointer for at least one function that uses a pic stub to
3692 be able to calculate the plt address referenced.
3693 (c) means gdb would need to set multiple breakpoints (or
3694 find the glink branch itself) when setting breakpoints
3695 for pending shared library loads. */
3696 p = relplt->relocation;
3697 for (i = 0; i < plt_count; i++, p++)
3698 {
3699 size_t len;
3700
3701 *s = **p->sym_ptr_ptr;
3702 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3703 we are defining a symbol, ensure one of them is set. */
3704 if ((s->flags & BSF_LOCAL) == 0)
3705 s->flags |= BSF_GLOBAL;
6ba2a415 3706 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3707 s->section = glink;
3708 s->value = glink_vma - glink->vma;
3709 s->name = names;
3710 s->udata.p = NULL;
3711 len = strlen ((*p->sym_ptr_ptr)->name);
3712 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3713 names += len;
e054468f
AM
3714 if (p->addend != 0)
3715 {
3716 memcpy (names, "+0x", sizeof ("+0x") - 1);
3717 names += sizeof ("+0x") - 1;
3718 bfd_sprintf_vma (abfd, names, p->addend);
3719 names += strlen (names);
3720 }
468392fb
AM
3721 memcpy (names, "@plt", sizeof ("@plt"));
3722 names += sizeof ("@plt");
3723 s++;
b9e5796b
AM
3724 if (abi < 2)
3725 {
3726 glink_vma += 8;
3727 if (i >= 0x8000)
3728 glink_vma += 4;
3729 }
3730 else
468392fb
AM
3731 glink_vma += 4;
3732 }
3733 count += plt_count;
3734 }
90e3cdf2
JJ
3735 }
3736
c9727e01 3737 done:
a7535cf3 3738 free (syms);
90e3cdf2
JJ
3739 return count;
3740}
5bd4f169 3741\f
65f38f15
AM
3742/* The following functions are specific to the ELF linker, while
3743 functions above are used generally. Those named ppc64_elf_* are
3744 called by the main ELF linker code. They appear in this file more
3745 or less in the order in which they are called. eg.
3746 ppc64_elf_check_relocs is called early in the link process,
3747 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3748 called.
3749
3750 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3751 functions have both a function code symbol and a function descriptor
3752 symbol. A call to foo in a relocatable object file looks like:
3753
3754 . .text
3755 . x:
3756 . bl .foo
3757 . nop
3758
3759 The function definition in another object file might be:
3760
3761 . .section .opd
3762 . foo: .quad .foo
3763 . .quad .TOC.@tocbase
3764 . .quad 0
3765 .
3766 . .text
3767 . .foo: blr
3768
3769 When the linker resolves the call during a static link, the branch
3770 unsurprisingly just goes to .foo and the .opd information is unused.
3771 If the function definition is in a shared library, things are a little
3772 different: The call goes via a plt call stub, the opd information gets
3773 copied to the plt, and the linker patches the nop.
3774
3775 . x:
3776 . bl .foo_stub
3777 . ld 2,40(1)
3778 .
3779 .
3780 . .foo_stub:
71a39c98
AM
3781 . std 2,40(1) # in practice, the call stub
3782 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3783 . addi 11,11,Lfoo@toc@l # this is the general idea
3784 . ld 12,0(11)
3785 . ld 2,8(11)
3786 . mtctr 12
3787 . ld 11,16(11)
e86ce104
AM
3788 . bctr
3789 .
3790 . .section .plt
3791 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3792
3793 The "reloc ()" notation is supposed to indicate that the linker emits
3794 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3795 copying.
3796
3797 What are the difficulties here? Well, firstly, the relocations
3798 examined by the linker in check_relocs are against the function code
3799 sym .foo, while the dynamic relocation in the plt is emitted against
3800 the function descriptor symbol, foo. Somewhere along the line, we need
3801 to carefully copy dynamic link information from one symbol to the other.
3802 Secondly, the generic part of the elf linker will make .foo a dynamic
3803 symbol as is normal for most other backends. We need foo dynamic
3804 instead, at least for an application final link. However, when
3805 creating a shared library containing foo, we need to have both symbols
3806 dynamic so that references to .foo are satisfied during the early
3807 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3808 definition from some other object, eg. a static library.
3809
3810 Update: As of August 2004, we support a new convention. Function
3811 calls may use the function descriptor symbol, ie. "bl foo". This
3812 behaves exactly as "bl .foo". */
65f38f15 3813
7c8bbca5
AM
3814/* Of those relocs that might be copied as dynamic relocs, this
3815 function selects those that must be copied when linking a shared
3816 library or PIE, even when the symbol is local. */
65f38f15 3817
1d483afe
AM
3818static int
3819must_be_dyn_reloc (struct bfd_link_info *info,
3820 enum elf_ppc64_reloc_type r_type)
3821{
3822 switch (r_type)
3823 {
3824 default:
7c8bbca5
AM
3825 /* Only relative relocs can be resolved when the object load
3826 address isn't fixed. DTPREL64 is excluded because the
3827 dynamic linker needs to differentiate global dynamic from
3828 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
3829 return 1;
3830
3831 case R_PPC64_REL32:
3832 case R_PPC64_REL64:
3833 case R_PPC64_REL30:
3834 return 0;
3835
3836 case R_PPC64_TPREL16:
3837 case R_PPC64_TPREL16_LO:
3838 case R_PPC64_TPREL16_HI:
3839 case R_PPC64_TPREL16_HA:
3840 case R_PPC64_TPREL16_DS:
3841 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3842 case R_PPC64_TPREL16_HIGH:
3843 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3844 case R_PPC64_TPREL16_HIGHER:
3845 case R_PPC64_TPREL16_HIGHERA:
3846 case R_PPC64_TPREL16_HIGHEST:
3847 case R_PPC64_TPREL16_HIGHESTA:
3848 case R_PPC64_TPREL64:
7c8bbca5
AM
3849 /* These relocations are relative but in a shared library the
3850 linker doesn't know the thread pointer base. */
3851 return bfd_link_dll (info);
1d483afe
AM
3852 }
3853}
65f38f15 3854
f4656909
AM
3855/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3856 copying dynamic variables from a shared lib into an app's dynbss
3857 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3858 shared lib. With code that gcc generates, it's vital that this be
3859 enabled; In the PowerPC64 ABI, the address of a function is actually
3860 the address of a function descriptor, which resides in the .opd
3861 section. gcc uses the descriptor directly rather than going via the
3862 GOT as some other ABI's do, which means that initialized function
3863 pointers must reference the descriptor. Thus, a function pointer
3864 initialized to the address of a function in a shared library will
3865 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3866 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3867 presents a problem as a plt entry for that function is also
3868 initialized from the function descriptor symbol and the copy reloc
3869 may not be initialized first. */
a23b6845 3870#define ELIMINATE_COPY_RELOCS 1
f4656909 3871
721956f4 3872/* Section name for stubs is the associated section name plus this
29942be8
NC
3873 string. */
3874#define STUB_SUFFIX ".stub"
721956f4
AM
3875
3876/* Linker stubs.
3877 ppc_stub_long_branch:
3878 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3879 destination, but a 24 bit branch in a stub section will reach.
3880 . b dest
3881
3882 ppc_stub_plt_branch:
3883 Similar to the above, but a 24 bit branch in the stub section won't
3884 reach its destination.
71a39c98
AM
3885 . addis %r11,%r2,xxx@toc@ha
3886 . ld %r12,xxx@toc@l(%r11)
3887 . mtctr %r12
721956f4
AM
3888 . bctr
3889
3890 ppc_stub_plt_call:
2c66dc6c
AM
3891 Used to call a function in a shared library. If it so happens that
3892 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3893 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3894 . std %r2,40(%r1)
71a39c98
AM
3895 . addis %r11,%r2,xxx@toc@ha
3896 . ld %r12,xxx+0@toc@l(%r11)
3897 . mtctr %r12
3898 . ld %r2,xxx+8@toc@l(%r11)
3899 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3900 . bctr
ad8e1ba5
AM
3901
3902 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3903 code to adjust the value and save r2 to support multiple toc sections.
3904 A ppc_stub_long_branch with an r2 offset looks like:
3905 . std %r2,40(%r1)
3906 . addis %r2,%r2,off@ha
3907 . addi %r2,%r2,off@l
3908 . b dest
3909
3910 A ppc_stub_plt_branch with an r2 offset looks like:
3911 . std %r2,40(%r1)
71a39c98
AM
3912 . addis %r11,%r2,xxx@toc@ha
3913 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3914 . addis %r2,%r2,off@ha
3915 . addi %r2,%r2,off@l
71a39c98 3916 . mtctr %r12
ad8e1ba5 3917 . bctr
ac2df442
AM
3918
3919 In cases where the "addis" instruction would add zero, the "addis" is
3920 omitted and following instructions modified slightly in some cases.
721956f4
AM
3921*/
3922
3923enum ppc_stub_type {
3924 ppc_stub_none,
3925 ppc_stub_long_branch,
ad8e1ba5 3926 ppc_stub_long_branch_r2off,
721956f4 3927 ppc_stub_plt_branch,
ad8e1ba5 3928 ppc_stub_plt_branch_r2off,
794e51c0 3929 ppc_stub_plt_call,
7341d5e2 3930 ppc_stub_plt_call_r2save,
a4b6fadd
AM
3931 ppc_stub_global_entry,
3932 ppc_stub_save_res
721956f4
AM
3933};
3934
6f20ed8a
AM
3935/* Information on stub grouping. */
3936struct map_stub
3937{
3938 /* The stub section. */
3939 asection *stub_sec;
3940 /* This is the section to which stubs in the group will be attached. */
3941 asection *link_sec;
a4b6fadd
AM
3942 /* Next group. */
3943 struct map_stub *next;
3944 /* Whether to emit a copy of register save/restore functions in this
3945 group. */
3946 int needs_save_res;
d4aaa2a0
AM
3947 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
3948 or -1u if no such stub with bctrl exists. */
3949 unsigned int tls_get_addr_opt_bctrl;
6f20ed8a
AM
3950};
3951
721956f4
AM
3952struct ppc_stub_hash_entry {
3953
3954 /* Base hash table entry structure. */
3955 struct bfd_hash_entry root;
3956
ad8e1ba5
AM
3957 enum ppc_stub_type stub_type;
3958
6f20ed8a
AM
3959 /* Group information. */
3960 struct map_stub *group;
721956f4
AM
3961
3962 /* Offset within stub_sec of the beginning of this stub. */
3963 bfd_vma stub_offset;
3964
3965 /* Given the symbol's value and its section we can determine its final
3966 value when building the stubs (so the stub knows where to jump. */
3967 bfd_vma target_value;
3968 asection *target_section;
3969
721956f4
AM
3970 /* The symbol table entry, if any, that this was derived from. */
3971 struct ppc_link_hash_entry *h;
e054468f 3972 struct plt_entry *plt_ent;
721956f4 3973
6911b7dc
AM
3974 /* Symbol st_other. */
3975 unsigned char other;
721956f4
AM
3976};
3977
3978struct ppc_branch_hash_entry {
3979
3980 /* Base hash table entry structure. */
3981 struct bfd_hash_entry root;
3982
c456f082 3983 /* Offset within branch lookup table. */
721956f4
AM
3984 unsigned int offset;
3985
3986 /* Generation marker. */
3987 unsigned int iter;
3988};
65f38f15 3989
19e08130
AM
3990/* Used to track dynamic relocations for local symbols. */
3991struct ppc_dyn_relocs
3992{
3993 struct ppc_dyn_relocs *next;
3994
3995 /* The input section of the reloc. */
3996 asection *sec;
3997
3998 /* Total number of relocs copied for the input section. */
3999 unsigned int count : 31;
4000
4001 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4002 unsigned int ifunc : 1;
4003};
4004
65f38f15
AM
4005struct ppc_link_hash_entry
4006{
4007 struct elf_link_hash_entry elf;
4008
b3fac117
AM
4009 union {
4010 /* A pointer to the most recently used stub hash entry against this
4011 symbol. */
4012 struct ppc_stub_hash_entry *stub_cache;
4013
4014 /* A pointer to the next symbol starting with a '.' */
4015 struct ppc_link_hash_entry *next_dot_sym;
4016 } u;
721956f4 4017
65f38f15 4018 /* Track dynamic relocs copied for this symbol. */
6061a67d 4019 struct elf_dyn_relocs *dyn_relocs;
e86ce104 4020
721956f4 4021 /* Link between function code and descriptor symbols. */
34814b9f 4022 struct ppc_link_hash_entry *oh;
721956f4 4023
e86ce104
AM
4024 /* Flag function code and descriptor symbols. */
4025 unsigned int is_func:1;
4026 unsigned int is_func_descriptor:1;
908b32fc 4027 unsigned int fake:1;
411e1bfb 4028
c5614fa4
AM
4029 /* Whether global opd/toc sym has been adjusted or not.
4030 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4031 should be set for all globals defined in any opd/toc section. */
754021d0
AM
4032 unsigned int adjust_done:1;
4033
a4b6fadd
AM
4034 /* Set if this is an out-of-line register save/restore function,
4035 with non-standard calling convention. */
4036 unsigned int save_res:1;
4037
8b5f1ed8
AM
4038 /* Set if a duplicate symbol with non-zero localentry is detected,
4039 even when the duplicate symbol does not provide a definition. */
4040 unsigned int non_zero_localentry:1;
4041
411e1bfb 4042 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
4043 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
4044 corresponding relocs are encountered during check_relocs.
4045 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
4046 indicate the corresponding GOT entry type is not needed.
4047 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
4048 a TPREL one. We use a separate flag rather than setting TPREL
4049 just for convenience in distinguishing the two cases. */
4050#define TLS_GD 1 /* GD reloc. */
4051#define TLS_LD 2 /* LD reloc. */
4052#define TLS_TPREL 4 /* TPREL reloc, => IE. */
4053#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4054#define TLS_TLS 16 /* Any TLS reloc. */
4055#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4056#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 4057#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 4058 unsigned char tls_mask;
65f38f15
AM
4059};
4060
4061/* ppc64 ELF linker hash table. */
4062
4063struct ppc_link_hash_table
4064{
4065 struct elf_link_hash_table elf;
4066
721956f4
AM
4067 /* The stub hash table. */
4068 struct bfd_hash_table stub_hash_table;
4069
4070 /* Another hash table for plt_branch stubs. */
4071 struct bfd_hash_table branch_hash_table;
4072
3b421ab3
AM
4073 /* Hash table for function prologue tocsave. */
4074 htab_t tocsave_htab;
4075
e7d1c40c
AM
4076 /* Various options and other info passed from the linker. */
4077 struct ppc64_elf_params *params;
721956f4 4078
6f20ed8a
AM
4079 /* The size of sec_info below. */
4080 unsigned int sec_info_arr_size;
4081
4082 /* Per-section array of extra section info. Done this way rather
4083 than as part of ppc64_elf_section_data so we have the info for
4084 non-ppc64 sections. */
4085 struct
4086 {
4087 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 4088 bfd_vma toc_off;
6f20ed8a
AM
4089
4090 union
4091 {
4092 /* The section group that this section belongs to. */
4093 struct map_stub *group;
4094 /* A temp section list pointer. */
4095 asection *list;
4096 } u;
4097 } *sec_info;
721956f4 4098
a4b6fadd
AM
4099 /* Linked list of groups. */
4100 struct map_stub *group;
4101
ad8e1ba5
AM
4102 /* Temp used when calculating TOC pointers. */
4103 bfd_vma toc_curr;
bf102f86
AM
4104 bfd *toc_bfd;
4105 asection *toc_first_sec;
ad8e1ba5 4106
b3fac117
AM
4107 /* Used when adding symbols. */
4108 struct ppc_link_hash_entry *dot_syms;
4109
33e44f2e 4110 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 4111 asection *glink;
9e390558 4112 asection *global_entry;
82bd7b59 4113 asection *sfpr;
4ce794b7
AM
4114 asection *brlt;
4115 asection *relbrlt;
58d180e8 4116 asection *glink_eh_frame;
ec338859 4117
8387904d
AM
4118 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4119 struct ppc_link_hash_entry *tls_get_addr;
4120 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4121
927be08e
AM
4122 /* The size of reliplt used by got entry relocs. */
4123 bfd_size_type got_reli_size;
4124
9b5ecbd0 4125 /* Statistics. */
7341d5e2 4126 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4127
ee75fd95
AM
4128 /* Number of stubs against global syms. */
4129 unsigned long stub_globals;
4130
ee67d69a
AM
4131 /* Set if we're linking code with function descriptors. */
4132 unsigned int opd_abi:1;
4133
4c52953f 4134 /* Support for multiple toc sections. */
33c0ec9d 4135 unsigned int do_multi_toc:1;
4c52953f 4136 unsigned int multi_toc_needed:1;
927be08e 4137 unsigned int second_toc_pass:1;
67f0cbdb 4138 unsigned int do_toc_opt:1;
4c52953f 4139
9a23f96e
AM
4140 /* Set if tls optimization is enabled. */
4141 unsigned int do_tls_opt:1;
4142
5d1634d7 4143 /* Set on error. */
99877b66 4144 unsigned int stub_error:1;
721956f4 4145
8c5b4e52
AM
4146 /* Whether func_desc_adjust needs to be run over symbols. */
4147 unsigned int need_func_desc_adj:1;
721956f4 4148
82e66161
AM
4149 /* Whether there exist local gnu indirect function resolvers,
4150 referenced by dynamic relocations. */
4151 unsigned int local_ifunc_resolver:1;
4152 unsigned int maybe_local_ifunc_resolver:1;
4153
f378ab09
AM
4154 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4155 unsigned int has_plt_localentry0:1;
4156
721956f4
AM
4157 /* Incremented every time we size stubs. */
4158 unsigned int stub_iteration;
5d1634d7 4159
87d72d41
AM
4160 /* Small local sym cache. */
4161 struct sym_cache sym_cache;
65f38f15
AM
4162};
4163
4c52953f
AM
4164/* Rename some of the generic section flags to better document how they
4165 are used here. */
b0dddeec
AM
4166
4167/* Nonzero if this section has TLS related relocations. */
4168#define has_tls_reloc sec_flg0
4169
4170/* Nonzero if this section has a call to __tls_get_addr. */
4171#define has_tls_get_addr_call sec_flg1
4172
4173/* Nonzero if this section has any toc or got relocs. */
4174#define has_toc_reloc sec_flg2
4175
4176/* Nonzero if this section has a call to another section that uses
4177 the toc or got. */
d77c8a4b 4178#define makes_toc_func_call sec_flg3
b0dddeec
AM
4179
4180/* Recursion protection when determining above flag. */
d77c8a4b 4181#define call_check_in_progress sec_flg4
70cc837d 4182#define call_check_done sec_flg5
4c52953f 4183
65f38f15
AM
4184/* Get the ppc64 ELF linker hash table from a link_info structure. */
4185
4186#define ppc_hash_table(p) \
4dfe6ac6
NC
4187 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4188 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4189
721956f4
AM
4190#define ppc_stub_hash_lookup(table, string, create, copy) \
4191 ((struct ppc_stub_hash_entry *) \
4192 bfd_hash_lookup ((table), (string), (create), (copy)))
4193
4194#define ppc_branch_hash_lookup(table, string, create, copy) \
4195 ((struct ppc_branch_hash_entry *) \
4196 bfd_hash_lookup ((table), (string), (create), (copy)))
4197
4198/* Create an entry in the stub hash table. */
4199
4200static struct bfd_hash_entry *
4ce794b7
AM
4201stub_hash_newfunc (struct bfd_hash_entry *entry,
4202 struct bfd_hash_table *table,
4203 const char *string)
721956f4
AM
4204{
4205 /* Allocate the structure if it has not already been allocated by a
4206 subclass. */
4207 if (entry == NULL)
4208 {
4209 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4210 if (entry == NULL)
4211 return entry;
4212 }
4213
4214 /* Call the allocation method of the superclass. */
4215 entry = bfd_hash_newfunc (entry, table, string);
4216 if (entry != NULL)
4217 {
4218 struct ppc_stub_hash_entry *eh;
4219
4220 /* Initialize the local fields. */
4221 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4222 eh->stub_type = ppc_stub_none;
6f20ed8a 4223 eh->group = NULL;
721956f4
AM
4224 eh->stub_offset = 0;
4225 eh->target_value = 0;
4226 eh->target_section = NULL;
721956f4 4227 eh->h = NULL;
6911b7dc 4228 eh->plt_ent = NULL;
6911b7dc 4229 eh->other = 0;
721956f4
AM
4230 }
4231
4232 return entry;
4233}
4234
4235/* Create an entry in the branch hash table. */
4236
4237static struct bfd_hash_entry *
4ce794b7
AM
4238branch_hash_newfunc (struct bfd_hash_entry *entry,
4239 struct bfd_hash_table *table,
4240 const char *string)
721956f4
AM
4241{
4242 /* Allocate the structure if it has not already been allocated by a
4243 subclass. */
4244 if (entry == NULL)
4245 {
4246 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4247 if (entry == NULL)
4248 return entry;
4249 }
4250
4251 /* Call the allocation method of the superclass. */
4252 entry = bfd_hash_newfunc (entry, table, string);
4253 if (entry != NULL)
4254 {
4255 struct ppc_branch_hash_entry *eh;
4256
4257 /* Initialize the local fields. */
4258 eh = (struct ppc_branch_hash_entry *) entry;
4259 eh->offset = 0;
4260 eh->iter = 0;
4261 }
4262
4263 return entry;
4264}
4265
65f38f15
AM
4266/* Create an entry in a ppc64 ELF linker hash table. */
4267
4268static struct bfd_hash_entry *
4ce794b7
AM
4269link_hash_newfunc (struct bfd_hash_entry *entry,
4270 struct bfd_hash_table *table,
4271 const char *string)
65f38f15
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_link_hash_entry));
4278 if (entry == NULL)
4279 return entry;
4280 }
4281
4282 /* Call the allocation method of the superclass. */
4283 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4284 if (entry != NULL)
4285 {
4286 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4287
b3fac117 4288 memset (&eh->u.stub_cache, 0,
908b32fc 4289 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4290 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4291
4292 /* When making function calls, old ABI code references function entry
4293 points (dot symbols), while new ABI code references the function
4294 descriptor symbol. We need to make any combination of reference and
4295 definition work together, without breaking archive linking.
4296
4297 For a defined function "foo" and an undefined call to "bar":
4298 An old object defines "foo" and ".foo", references ".bar" (possibly
4299 "bar" too).
4300 A new object defines "foo" and references "bar".
4301
4302 A new object thus has no problem with its undefined symbols being
4303 satisfied by definitions in an old object. On the other hand, the
4304 old object won't have ".bar" satisfied by a new object.
4305
4306 Keep a list of newly added dot-symbols. */
4307
4308 if (string[0] == '.')
4309 {
4310 struct ppc_link_hash_table *htab;
4311
4312 htab = (struct ppc_link_hash_table *) table;
4313 eh->u.next_dot_sym = htab->dot_syms;
4314 htab->dot_syms = eh;
4315 }
65f38f15
AM
4316 }
4317
4318 return entry;
4319}
4320
3b421ab3
AM
4321struct tocsave_entry {
4322 asection *sec;
4323 bfd_vma offset;
4324};
4325
4326static hashval_t
4327tocsave_htab_hash (const void *p)
4328{
4329 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 4330 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
4331}
4332
4333static int
4334tocsave_htab_eq (const void *p1, const void *p2)
4335{
4336 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4337 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4338 return e1->sec == e2->sec && e1->offset == e2->offset;
4339}
4340
68faa637
AM
4341/* Destroy a ppc64 ELF linker hash table. */
4342
4343static void
d495ab0d 4344ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4345{
d495ab0d 4346 struct ppc_link_hash_table *htab;
68faa637 4347
d495ab0d 4348 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4349 if (htab->tocsave_htab)
4350 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4351 bfd_hash_table_free (&htab->branch_hash_table);
4352 bfd_hash_table_free (&htab->stub_hash_table);
4353 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4354}
4355
65f38f15
AM
4356/* Create a ppc64 ELF linker hash table. */
4357
4358static struct bfd_link_hash_table *
4ce794b7 4359ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4360{
4361 struct ppc_link_hash_table *htab;
4362 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4363
4ce794b7 4364 htab = bfd_zmalloc (amt);
65f38f15
AM
4365 if (htab == NULL)
4366 return NULL;
4367
66eb6687 4368 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4369 sizeof (struct ppc_link_hash_entry),
4370 PPC64_ELF_DATA))
65f38f15 4371 {
e2d34d7d 4372 free (htab);
65f38f15
AM
4373 return NULL;
4374 }
4375
721956f4 4376 /* Init the stub hash table too. */
66eb6687
AM
4377 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4378 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4379 {
d495ab0d 4380 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4381 return NULL;
4382 }
721956f4
AM
4383
4384 /* And the branch hash table. */
66eb6687
AM
4385 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4386 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4387 {
4388 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4389 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4390 return NULL;
4391 }
721956f4 4392
3b421ab3
AM
4393 htab->tocsave_htab = htab_try_create (1024,
4394 tocsave_htab_hash,
4395 tocsave_htab_eq,
4396 NULL);
4397 if (htab->tocsave_htab == NULL)
2915c55b 4398 {
d495ab0d 4399 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4400 return NULL;
4401 }
d495ab0d 4402 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4403
3254fd24
AM
4404 /* Initializing two fields of the union is just cosmetic. We really
4405 only care about glist, but when compiled on a 32-bit host the
4406 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4407 debugger inspection of these fields look nicer. */
a6aa5195
AM
4408 htab->elf.init_got_refcount.refcount = 0;
4409 htab->elf.init_got_refcount.glist = NULL;
4410 htab->elf.init_plt_refcount.refcount = 0;
4411 htab->elf.init_plt_refcount.glist = NULL;
4412 htab->elf.init_got_offset.offset = 0;
4413 htab->elf.init_got_offset.glist = NULL;
4414 htab->elf.init_plt_offset.offset = 0;
4415 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4416
65f38f15
AM
4417 return &htab->elf.root;
4418}
4419
bfeb4a28
AM
4420/* Create sections for linker generated code. */
4421
4422static bfd_boolean
4423create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4424{
4425 struct ppc_link_hash_table *htab;
4426 flagword flags;
4427
4428 htab = ppc_hash_table (info);
4429
bfeb4a28
AM
4430 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4431 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4432 if (htab->params->save_restore_funcs)
4433 {
4434 /* Create .sfpr for code to save and restore fp regs. */
4435 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4436 flags);
4437 if (htab->sfpr == NULL
4438 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4439 return FALSE;
4440 }
4441
4442 if (bfd_link_relocatable (info))
4443 return TRUE;
bfeb4a28
AM
4444
4445 /* Create .glink for lazy dynamic linking support. */
4446 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4447 flags);
4448 if (htab->glink == NULL
4449 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4450 return FALSE;
4451
9e390558
AM
4452 /* The part of .glink used by global entry stubs, separate so that
4453 it can be aligned appropriately without affecting htab->glink. */
4454 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4455 flags);
4456 if (htab->global_entry == NULL
4457 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4458 return FALSE;
4459
bfeb4a28
AM
4460 if (!info->no_ld_generated_unwind_info)
4461 {
4462 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4463 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4464 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4465 ".eh_frame",
4466 flags);
4467 if (htab->glink_eh_frame == NULL
4468 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4469 return FALSE;
4470 }
4471
4472 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4473 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4474 if (htab->elf.iplt == NULL
4475 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4476 return FALSE;
4477
4478 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4479 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4480 htab->elf.irelplt
4481 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4482 if (htab->elf.irelplt == NULL
4483 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4484 return FALSE;
4485
4486 /* Create branch lookup table for plt_branch stubs. */
4487 flags = (SEC_ALLOC | SEC_LOAD
4488 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4489 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4490 flags);
4491 if (htab->brlt == NULL
4492 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4493 return FALSE;
4494
0e1862bb 4495 if (!bfd_link_pic (info))
bfeb4a28
AM
4496 return TRUE;
4497
4498 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4499 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4500 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4501 ".rela.branch_lt",
4502 flags);
4503 if (htab->relbrlt == NULL
4504 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4505 return FALSE;
4506
4507 return TRUE;
4508}
4509
e717da7e
AM
4510/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4511
bfeb4a28 4512bfd_boolean
e7d1c40c
AM
4513ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4514 struct ppc64_elf_params *params)
e717da7e
AM
4515{
4516 struct ppc_link_hash_table *htab;
4517
e7d1c40c 4518 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4519
4520/* Always hook our dynamic sections into the first bfd, which is the
4521 linker created stub bfd. This ensures that the GOT header is at
4522 the start of the output TOC section. */
4523 htab = ppc_hash_table (info);
e7d1c40c
AM
4524 htab->elf.dynobj = params->stub_bfd;
4525 htab->params = params;
bfeb4a28 4526
bfeb4a28 4527 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4528}
4529
721956f4
AM
4530/* Build a name for an entry in the stub hash table. */
4531
4532static char *
4ce794b7
AM
4533ppc_stub_name (const asection *input_section,
4534 const asection *sym_sec,
4535 const struct ppc_link_hash_entry *h,
4536 const Elf_Internal_Rela *rel)
721956f4
AM
4537{
4538 char *stub_name;
bcaa2f82 4539 ssize_t len;
721956f4
AM
4540
4541 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4542 offsets from a sym as a branch target? In fact, we could
4543 probably assume the addend is always zero. */
4544 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4545
4546 if (h)
4547 {
4548 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4549 stub_name = bfd_malloc (len);
46de2a7c
AM
4550 if (stub_name == NULL)
4551 return stub_name;
4552
bcaa2f82
AM
4553 len = sprintf (stub_name, "%08x.%s+%x",
4554 input_section->id & 0xffffffff,
4555 h->elf.root.root.string,
4556 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4557 }
4558 else
4559 {
ad8e1ba5 4560 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4561 stub_name = bfd_malloc (len);
46de2a7c
AM
4562 if (stub_name == NULL)
4563 return stub_name;
4564
bcaa2f82
AM
4565 len = sprintf (stub_name, "%08x.%x:%x+%x",
4566 input_section->id & 0xffffffff,
4567 sym_sec->id & 0xffffffff,
4568 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4569 (int) rel->r_addend & 0xffffffff);
721956f4 4570 }
bcaa2f82 4571 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4572 stub_name[len - 2] = 0;
721956f4
AM
4573 return stub_name;
4574}
4575
4576/* Look up an entry in the stub hash. Stub entries are cached because
4577 creating the stub name takes a bit of time. */
4578
4579static struct ppc_stub_hash_entry *
4ce794b7
AM
4580ppc_get_stub_entry (const asection *input_section,
4581 const asection *sym_sec,
039b3fef 4582 struct ppc_link_hash_entry *h,
4ce794b7
AM
4583 const Elf_Internal_Rela *rel,
4584 struct ppc_link_hash_table *htab)
721956f4
AM
4585{
4586 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4587 struct map_stub *group;
721956f4
AM
4588
4589 /* If this input section is part of a group of sections sharing one
4590 stub section, then use the id of the first section in the group.
4591 Stub names need to include a section id, as there may well be
4592 more than one stub used to reach say, printf, and we need to
4593 distinguish between them. */
6f20ed8a 4594 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4595 if (group == NULL)
4596 return NULL;
721956f4 4597
b3fac117
AM
4598 if (h != NULL && h->u.stub_cache != NULL
4599 && h->u.stub_cache->h == h
6f20ed8a 4600 && h->u.stub_cache->group == group)
721956f4 4601 {
b3fac117 4602 stub_entry = h->u.stub_cache;
721956f4
AM
4603 }
4604 else
4605 {
4606 char *stub_name;
4607
6f20ed8a 4608 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4609 if (stub_name == NULL)
4610 return NULL;
4611
4612 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4613 stub_name, FALSE, FALSE);
721956f4 4614 if (h != NULL)
b3fac117 4615 h->u.stub_cache = stub_entry;
721956f4
AM
4616
4617 free (stub_name);
4618 }
4619
4620 return stub_entry;
4621}
4622
4623/* Add a new stub entry to the stub hash. Not all fields of the new
4624 stub entry are initialised. */
4625
4626static struct ppc_stub_hash_entry *
4ce794b7
AM
4627ppc_add_stub (const char *stub_name,
4628 asection *section,
25f53a85 4629 struct bfd_link_info *info)
721956f4 4630{
25f53a85 4631 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4632 struct map_stub *group;
721956f4
AM
4633 asection *link_sec;
4634 asection *stub_sec;
4635 struct ppc_stub_hash_entry *stub_entry;
4636
6f20ed8a
AM
4637 group = htab->sec_info[section->id].u.group;
4638 link_sec = group->link_sec;
4639 stub_sec = group->stub_sec;
721956f4
AM
4640 if (stub_sec == NULL)
4641 {
6f20ed8a
AM
4642 size_t namelen;
4643 bfd_size_type len;
4644 char *s_name;
721956f4 4645
6f20ed8a
AM
4646 namelen = strlen (link_sec->name);
4647 len = namelen + sizeof (STUB_SUFFIX);
4648 s_name = bfd_alloc (htab->params->stub_bfd, len);
4649 if (s_name == NULL)
4650 return NULL;
721956f4 4651
6f20ed8a
AM
4652 memcpy (s_name, link_sec->name, namelen);
4653 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4654 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4655 if (stub_sec == NULL)
4656 return NULL;
4657 group->stub_sec = stub_sec;
721956f4
AM
4658 }
4659
4660 /* Enter this entry into the linker stub hash table. */
4661 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4662 TRUE, FALSE);
721956f4
AM
4663 if (stub_entry == NULL)
4664 {
695344c0 4665 /* xgettext:c-format */
cf97bcb0
AM
4666 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4667 section->owner, stub_name);
721956f4
AM
4668 return NULL;
4669 }
4670
6f20ed8a 4671 stub_entry->group = group;
721956f4 4672 stub_entry->stub_offset = 0;
721956f4
AM
4673 return stub_entry;
4674}
4675
e717da7e
AM
4676/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4677 not already done. */
65f38f15 4678
b34976b6 4679static bfd_boolean
e717da7e 4680create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4681{
e717da7e
AM
4682 asection *got, *relgot;
4683 flagword flags;
4684 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4685
0c8d6e5c 4686 if (!is_ppc64_elf (abfd))
0ffa91dd 4687 return FALSE;
4dfe6ac6
NC
4688 if (htab == NULL)
4689 return FALSE;
0ffa91dd 4690
33e44f2e
AM
4691 if (!htab->elf.sgot
4692 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4693 return FALSE;
e717da7e
AM
4694
4695 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4696 | SEC_LINKER_CREATED);
4697
c456f082 4698 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4699 if (!got
e717da7e
AM
4700 || !bfd_set_section_alignment (abfd, got, 3))
4701 return FALSE;
65f38f15 4702
c456f082
AM
4703 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4704 flags | SEC_READONLY);
e717da7e 4705 if (!relgot
e717da7e 4706 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4707 return FALSE;
e717da7e
AM
4708
4709 ppc64_elf_tdata (abfd)->got = got;
4710 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4711 return TRUE;
65f38f15 4712}
5bd4f169 4713
b31867b6
AM
4714/* Follow indirect and warning symbol links. */
4715
4716static inline struct bfd_link_hash_entry *
4717follow_link (struct bfd_link_hash_entry *h)
4718{
4719 while (h->type == bfd_link_hash_indirect
4720 || h->type == bfd_link_hash_warning)
4721 h = h->u.i.link;
4722 return h;
4723}
4724
4725static inline struct elf_link_hash_entry *
4726elf_follow_link (struct elf_link_hash_entry *h)
4727{
4728 return (struct elf_link_hash_entry *) follow_link (&h->root);
4729}
4730
4731static inline struct ppc_link_hash_entry *
4732ppc_follow_link (struct ppc_link_hash_entry *h)
4733{
4734 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4735}
4736
40d16e0b
AM
4737/* Merge PLT info on FROM with that on TO. */
4738
4739static void
4740move_plt_plist (struct ppc_link_hash_entry *from,
4741 struct ppc_link_hash_entry *to)
4742{
4743 if (from->elf.plt.plist != NULL)
4744 {
4745 if (to->elf.plt.plist != NULL)
4746 {
4747 struct plt_entry **entp;
4748 struct plt_entry *ent;
4749
4750 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4751 {
4752 struct plt_entry *dent;
4753
4754 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4755 if (dent->addend == ent->addend)
4756 {
4757 dent->plt.refcount += ent->plt.refcount;
4758 *entp = ent->next;
4759 break;
4760 }
4761 if (dent == NULL)
4762 entp = &ent->next;
4763 }
4764 *entp = to->elf.plt.plist;
4765 }
4766
4767 to->elf.plt.plist = from->elf.plt.plist;
4768 from->elf.plt.plist = NULL;
4769 }
4770}
4771
65f38f15
AM
4772/* Copy the extra info we tack onto an elf_link_hash_entry. */
4773
4774static void
fcfa13d2
AM
4775ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4776 struct elf_link_hash_entry *dir,
4777 struct elf_link_hash_entry *ind)
65f38f15
AM
4778{
4779 struct ppc_link_hash_entry *edir, *eind;
4780
4781 edir = (struct ppc_link_hash_entry *) dir;
4782 eind = (struct ppc_link_hash_entry *) ind;
4783
c79d6685
AM
4784 edir->is_func |= eind->is_func;
4785 edir->is_func_descriptor |= eind->is_func_descriptor;
4786 edir->tls_mask |= eind->tls_mask;
4787 if (eind->oh != NULL)
4788 edir->oh = ppc_follow_link (eind->oh);
4789
474436e6 4790 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4791 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4792 edir->elf.ref_regular |= eind->elf.ref_regular;
4793 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 4794 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
4795 edir->elf.needs_plt |= eind->elf.needs_plt;
4796 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4797
d311bc8b
AM
4798 /* If we were called to copy over info for a weak sym, don't copy
4799 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4800 in order to simplify readonly_dynrelocs and save a field in the
4801 symbol hash entry, but that means dyn_relocs can't be used in any
4802 tests about a specific symbol, or affect other symbol flags which
ab2477e1 4803 are then tested. */
d311bc8b 4804 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 4805 return;
d311bc8b 4806
411e1bfb 4807 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4808 if (eind->dyn_relocs != NULL)
65f38f15 4809 {
bbd7ec4a
AM
4810 if (edir->dyn_relocs != NULL)
4811 {
6061a67d
AM
4812 struct elf_dyn_relocs **pp;
4813 struct elf_dyn_relocs *p;
bbd7ec4a 4814
fcfa13d2 4815 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4816 list. Merge any entries against the same section. */
4817 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4818 {
6061a67d 4819 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4820
4821 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4822 if (q->sec == p->sec)
4823 {
4824 q->pc_count += p->pc_count;
4825 q->count += p->count;
4826 *pp = p->next;
4827 break;
4828 }
4829 if (q == NULL)
4830 pp = &p->next;
4831 }
4832 *pp = edir->dyn_relocs;
4833 }
4834
65f38f15
AM
4835 edir->dyn_relocs = eind->dyn_relocs;
4836 eind->dyn_relocs = NULL;
4837 }
65f38f15 4838
81848ca0
AM
4839 /* Copy over got entries that we may have already seen to the
4840 symbol which just became indirect. */
411e1bfb
AM
4841 if (eind->elf.got.glist != NULL)
4842 {
4843 if (edir->elf.got.glist != NULL)
4844 {
4845 struct got_entry **entp;
4846 struct got_entry *ent;
4847
4848 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4849 {
4850 struct got_entry *dent;
4851
4852 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4853 if (dent->addend == ent->addend
e717da7e 4854 && dent->owner == ent->owner
411e1bfb
AM
4855 && dent->tls_type == ent->tls_type)
4856 {
4857 dent->got.refcount += ent->got.refcount;
4858 *entp = ent->next;
4859 break;
4860 }
4861 if (dent == NULL)
4862 entp = &ent->next;
4863 }
4864 *entp = edir->elf.got.glist;
4865 }
4866
4867 edir->elf.got.glist = eind->elf.got.glist;
4868 eind->elf.got.glist = NULL;
4869 }
4870
4871 /* And plt entries. */
40d16e0b 4872 move_plt_plist (eind, edir);
411e1bfb 4873
fcfa13d2 4874 if (eind->elf.dynindx != -1)
411e1bfb 4875 {
fcfa13d2
AM
4876 if (edir->elf.dynindx != -1)
4877 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4878 edir->elf.dynstr_index);
411e1bfb
AM
4879 edir->elf.dynindx = eind->elf.dynindx;
4880 edir->elf.dynstr_index = eind->elf.dynstr_index;
4881 eind->elf.dynindx = -1;
4882 eind->elf.dynstr_index = 0;
4883 }
411e1bfb
AM
4884}
4885
8387904d
AM
4886/* Find the function descriptor hash entry from the given function code
4887 hash entry FH. Link the entries via their OH fields. */
4888
4889static struct ppc_link_hash_entry *
b31867b6 4890lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4891{
4892 struct ppc_link_hash_entry *fdh = fh->oh;
4893
4894 if (fdh == NULL)
4895 {
4896 const char *fd_name = fh->elf.root.root.string + 1;
4897
4898 fdh = (struct ppc_link_hash_entry *)
4899 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4900 if (fdh == NULL)
4901 return fdh;
4902
4903 fdh->is_func_descriptor = 1;
4904 fdh->oh = fh;
4905 fh->is_func = 1;
4906 fh->oh = fdh;
8387904d
AM
4907 }
4908
8c5b4e52
AM
4909 fdh = ppc_follow_link (fdh);
4910 fdh->is_func_descriptor = 1;
4911 fdh->oh = fh;
4912 return fdh;
8387904d
AM
4913}
4914
8c5b4e52 4915/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4916
4917static struct ppc_link_hash_entry *
4918make_fdh (struct bfd_link_info *info,
908b32fc 4919 struct ppc_link_hash_entry *fh)
bb700d78 4920{
8c5b4e52
AM
4921 bfd *abfd = fh->elf.root.u.undef.abfd;
4922 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4923 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4924 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4925 ? BSF_WEAK
4926 : BSF_GLOBAL);
4927
4928 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4929 fh->elf.root.root.string + 1,
4930 flags, bfd_und_section_ptr, 0,
4931 NULL, FALSE, FALSE, &bh))
bb700d78
AM
4932 return NULL;
4933
4934 fdh = (struct ppc_link_hash_entry *) bh;
4935 fdh->elf.non_elf = 0;
908b32fc
AM
4936 fdh->fake = 1;
4937 fdh->is_func_descriptor = 1;
4938 fdh->oh = fh;
4939 fh->is_func = 1;
4940 fh->oh = fdh;
bb700d78
AM
4941 return fdh;
4942}
4943
8387904d
AM
4944/* Fix function descriptor symbols defined in .opd sections to be
4945 function type. */
555cd476
AM
4946
4947static bfd_boolean
c16153ae 4948ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4949 struct bfd_link_info *info,
555cd476 4950 Elf_Internal_Sym *isym,
6911b7dc 4951 const char **name,
555cd476
AM
4952 flagword *flags ATTRIBUTE_UNUSED,
4953 asection **sec,
b53dfeb2 4954 bfd_vma *value)
555cd476 4955{
a43942db 4956 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
f1885d1e
AM
4957 && (ibfd->flags & DYNAMIC) == 0
4958 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
a43942db 4959 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
f64b2e8d 4960
b53dfeb2 4961 if (*sec != NULL
f1885d1e 4962 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4963 {
4964 asection *code_sec;
4965
4966 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4967 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4968 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4969
4970 /* If the symbol is a function defined in .opd, and the function
4971 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4972 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4973 && (*sec)->reloc_count != 0
4974 && opd_entry_value (*sec, *value, &code_sec, NULL,
4975 FALSE) != (bfd_vma) -1
4976 && discarded_section (code_sec))
4977 {
4978 *sec = bfd_und_section_ptr;
4979 isym->st_shndx = SHN_UNDEF;
4980 }
4981 }
dbd1e97e
AM
4982 else if (*sec != NULL
4983 && strcmp ((*sec)->name, ".toc") == 0
4984 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4985 {
4986 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4987 if (htab != NULL)
4988 htab->params->object_in_toc = 1;
4989 }
433817dd 4990
6911b7dc
AM
4991 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4992 {
4993 if (abiversion (ibfd) == 0)
4994 set_abiversion (ibfd, 2);
4995 else if (abiversion (ibfd) == 1)
4996 {
cf97bcb0
AM
4997 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4998 " for ABI version 1"), *name);
6911b7dc
AM
4999 bfd_set_error (bfd_error_bad_value);
5000 return FALSE;
5001 }
5002 }
5003
555cd476
AM
5004 return TRUE;
5005}
5006
6911b7dc
AM
5007/* Merge non-visibility st_other attributes: local entry point. */
5008
5009static void
5010ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5011 const Elf_Internal_Sym *isym,
5012 bfd_boolean definition,
5013 bfd_boolean dynamic)
5014{
f378ab09 5015 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
5016 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5017 | ELF_ST_VISIBILITY (h->other));
5018}
5019
8c5b4e52
AM
5020/* Hook called on merging a symbol. We use this to clear "fake" since
5021 we now have a real symbol. */
5022
5023static bfd_boolean
5024ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 5025 const Elf_Internal_Sym *isym,
8c5b4e52
AM
5026 asection **psec ATTRIBUTE_UNUSED,
5027 bfd_boolean newdef ATTRIBUTE_UNUSED,
5028 bfd_boolean olddef ATTRIBUTE_UNUSED,
5029 bfd *oldbfd ATTRIBUTE_UNUSED,
5030 const asection *oldsec ATTRIBUTE_UNUSED)
5031{
5032 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
5033 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5034 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
5035 return TRUE;
5036}
5037
8387904d 5038/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5039 inclusion of a new ABI object archive that defines "bar".
5040 NAME is a symbol defined in an archive. Return a symbol in the hash
5041 table that might be satisfied by the archive symbols. */
8387904d
AM
5042
5043static struct elf_link_hash_entry *
5044ppc64_elf_archive_symbol_lookup (bfd *abfd,
5045 struct bfd_link_info *info,
5046 const char *name)
5047{
5048 struct elf_link_hash_entry *h;
5049 char *dot_name;
5050 size_t len;
5051
5052 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5053 if (h != NULL
5054 /* Don't return this sym if it is a fake function descriptor
5055 created by add_symbol_adjust. */
8c5b4e52 5056 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
5057 return h;
5058
5059 if (name[0] == '.')
5060 return h;
5061
5062 len = strlen (name);
5063 dot_name = bfd_alloc (abfd, len + 2);
5064 if (dot_name == NULL)
e99955cd 5065 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5066 dot_name[0] = '.';
5067 memcpy (dot_name + 1, name, len + 1);
5068 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5069 bfd_release (abfd, dot_name);
5070 return h;
5071}
5072
5073/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5074 new ABI object defines "bar". Well, at least, undefined dot symbols
5075 are made weak. This stops later archive searches from including an
5076 object if we already have a function descriptor definition. It also
35b0ce59
AM
5077 prevents the linker complaining about undefined symbols.
5078 We also check and correct mismatched symbol visibility here. The
5079 most restrictive visibility of the function descriptor and the
5080 function entry symbol is used. */
8387904d
AM
5081
5082static bfd_boolean
b3fac117 5083add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5084{
8387904d
AM
5085 struct ppc_link_hash_table *htab;
5086 struct ppc_link_hash_entry *fdh;
5087
b3fac117
AM
5088 if (eh->elf.root.type == bfd_link_hash_warning)
5089 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5090
8400d40d
AM
5091 if (eh->elf.root.type == bfd_link_hash_indirect)
5092 return TRUE;
5093
b3fac117
AM
5094 if (eh->elf.root.root.string[0] != '.')
5095 abort ();
8387904d 5096
b3fac117 5097 htab = ppc_hash_table (info);
4dfe6ac6
NC
5098 if (htab == NULL)
5099 return FALSE;
5100
b31867b6 5101 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
5102 if (fdh == NULL
5103 && !bfd_link_relocatable (info)
5104 && (eh->elf.root.type == bfd_link_hash_undefined
5105 || eh->elf.root.type == bfd_link_hash_undefweak)
5106 && eh->elf.ref_regular)
5107 {
5108 /* Make an undefined function descriptor sym, in order to
5109 pull in an --as-needed shared lib. Archives are handled
5110 elsewhere. */
5111 fdh = make_fdh (info, eh);
5112 if (fdh == NULL)
5113 return FALSE;
bb700d78 5114 }
8c5b4e52
AM
5115
5116 if (fdh != NULL)
8387904d 5117 {
35b0ce59
AM
5118 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5119 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
5120
5121 /* Make both descriptor and entry symbol have the most
5122 constraining visibility of either symbol. */
35b0ce59
AM
5123 if (entry_vis < descr_vis)
5124 fdh->elf.other += entry_vis - descr_vis;
5125 else if (entry_vis > descr_vis)
5126 eh->elf.other += descr_vis - entry_vis;
5127
8c5b4e52
AM
5128 /* Propagate reference flags from entry symbol to function
5129 descriptor symbol. */
bc4e12de 5130 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 5131 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
5132 fdh->elf.ref_regular |= eh->elf.ref_regular;
5133 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5134
5135 if (!fdh->elf.forced_local
5136 && fdh->elf.dynindx == -1
5137 && fdh->elf.versioned != versioned_hidden
5138 && (bfd_link_dll (info)
5139 || fdh->elf.def_dynamic
5140 || fdh->elf.ref_dynamic)
5141 && (eh->elf.ref_regular
5142 || eh->elf.def_regular))
5143 {
5144 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5145 return FALSE;
35b0ce59 5146 }
8387904d 5147 }
99877b66 5148
8387904d
AM
5149 return TRUE;
5150}
5151
f6c7c3e8
AM
5152/* Set up opd section info and abiversion for IBFD, and process list
5153 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5154
8387904d 5155static bfd_boolean
f6c7c3e8 5156ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5157{
99877b66 5158 struct ppc_link_hash_table *htab;
b3fac117 5159 struct ppc_link_hash_entry **p, *eh;
459609d6 5160 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5161
459609d6 5162 if (opd != NULL && opd->size != 0)
b3fac117 5163 {
b9399fcf
AM
5164 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5165 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5166
459609d6
AM
5167 if (abiversion (ibfd) == 0)
5168 set_abiversion (ibfd, 1);
8a2058b5 5169 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5170 {
695344c0 5171 /* xgettext:c-format */
cf97bcb0
AM
5172 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5173 ibfd, abiversion (ibfd));
459609d6
AM
5174 bfd_set_error (bfd_error_bad_value);
5175 return FALSE;
f6c7c3e8 5176 }
b9399fcf 5177 }
f6c7c3e8 5178
b9399fcf
AM
5179 if (is_ppc64_elf (info->output_bfd))
5180 {
5181 /* For input files without an explicit abiversion in e_flags
5182 we should have flagged any with symbol st_other bits set
5183 as ELFv1 and above flagged those with .opd as ELFv2.
5184 Set the output abiversion if not yet set, and for any input
5185 still ambiguous, take its abiversion from the output.
5186 Differences in ABI are reported later. */
5187 if (abiversion (info->output_bfd) == 0)
5188 set_abiversion (info->output_bfd, abiversion (ibfd));
5189 else if (abiversion (ibfd) == 0)
5190 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
5191 }
5192
459609d6
AM
5193 htab = ppc_hash_table (info);
5194 if (htab == NULL)
b9399fcf 5195 return TRUE;
459609d6 5196
b9399fcf
AM
5197 if (opd != NULL && opd->size != 0
5198 && (ibfd->flags & DYNAMIC) == 0
5199 && (opd->flags & SEC_RELOC) != 0
5200 && opd->reloc_count != 0
5201 && !bfd_is_abs_section (opd->output_section)
5202 && info->gc_sections)
5203 {
5204 /* Garbage collection needs some extra help with .opd sections.
5205 We don't want to necessarily keep everything referenced by
5206 relocs in .opd, as that would keep all functions. Instead,
5207 if we reference an .opd symbol (a function descriptor), we
5208 want to keep the function code symbol's section. This is
5209 easy for global symbols, but for local syms we need to keep
5210 information about the associated function section. */
5211 bfd_size_type amt;
5212 asection **opd_sym_map;
5213 Elf_Internal_Shdr *symtab_hdr;
5214 Elf_Internal_Rela *relocs, *rel_end, *rel;
5215
5216 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5217 opd_sym_map = bfd_zalloc (ibfd, amt);
5218 if (opd_sym_map == NULL)
5219 return FALSE;
5220 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5221 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5222 info->keep_memory);
5223 if (relocs == NULL)
5224 return FALSE;
5225 symtab_hdr = &elf_symtab_hdr (ibfd);
5226 rel_end = relocs + opd->reloc_count - 1;
5227 for (rel = relocs; rel < rel_end; rel++)
5228 {
5229 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5230 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5231
5232 if (r_type == R_PPC64_ADDR64
5233 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5234 && r_symndx < symtab_hdr->sh_info)
5235 {
5236 Elf_Internal_Sym *isym;
5237 asection *s;
5238
5239 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5240 if (isym == NULL)
5241 {
5242 if (elf_section_data (opd)->relocs != relocs)
5243 free (relocs);
5244 return FALSE;
5245 }
5246
5247 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5248 if (s != NULL && s != opd)
5249 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5250 }
5251 }
5252 if (elf_section_data (opd)->relocs != relocs)
5253 free (relocs);
5254 }
459609d6
AM
5255
5256 p = &htab->dot_syms;
5257 while ((eh = *p) != NULL)
5258 {
5259 *p = NULL;
5260 if (&eh->elf == htab->elf.hgot)
5261 ;
5262 else if (htab->elf.hgot == NULL
5263 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5264 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
5265 else if (abiversion (ibfd) <= 1)
5266 {
5267 htab->need_func_desc_adj = 1;
5268 if (!add_symbol_adjust (eh, info))
5269 return FALSE;
5270 }
459609d6
AM
5271 p = &eh->u.next_dot_sym;
5272 }
b3fac117 5273 return TRUE;
8387904d
AM
5274}
5275
97fed1c9
JJ
5276/* Undo hash table changes when an --as-needed input file is determined
5277 not to be needed. */
5278
5279static bfd_boolean
e5034e59
AM
5280ppc64_elf_notice_as_needed (bfd *ibfd,
5281 struct bfd_link_info *info,
5282 enum notice_asneeded_action act)
97fed1c9 5283{
e5034e59
AM
5284 if (act == notice_not_needed)
5285 {
5286 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5287
e5034e59
AM
5288 if (htab == NULL)
5289 return FALSE;
4dfe6ac6 5290
e5034e59
AM
5291 htab->dot_syms = NULL;
5292 }
5293 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5294}
5295
aa374f67
AM
5296/* If --just-symbols against a final linked binary, then assume we need
5297 toc adjusting stubs when calling functions defined there. */
5298
5299static void
5300ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5301{
5302 if ((sec->flags & SEC_CODE) != 0
5303 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5304 && is_ppc64_elf (sec->owner))
5305 {
2c3f079f
AM
5306 if (abiversion (sec->owner) >= 2
5307 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5308 sec->has_toc_reloc = 1;
5309 }
5310 _bfd_elf_link_just_syms (sec, info);
5311}
5312
e054468f 5313static struct plt_entry **
4ce794b7
AM
5314update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5315 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5316{
5317 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5318 struct plt_entry **local_plt;
f961d9dd 5319 unsigned char *local_got_tls_masks;
411e1bfb
AM
5320
5321 if (local_got_ents == NULL)
5322 {
5323 bfd_size_type size = symtab_hdr->sh_info;
5324
e054468f
AM
5325 size *= (sizeof (*local_got_ents)
5326 + sizeof (*local_plt)
5327 + sizeof (*local_got_tls_masks));
4ce794b7 5328 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5329 if (local_got_ents == NULL)
e054468f 5330 return NULL;
411e1bfb
AM
5331 elf_local_got_ents (abfd) = local_got_ents;
5332 }
5333
e054468f 5334 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5335 {
5336 struct got_entry *ent;
5337
5338 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5339 if (ent->addend == r_addend
5340 && ent->owner == abfd
5341 && ent->tls_type == tls_type)
411e1bfb
AM
5342 break;
5343 if (ent == NULL)
5344 {
5345 bfd_size_type amt = sizeof (*ent);
4ce794b7 5346 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5347 if (ent == NULL)
5348 return FALSE;
5349 ent->next = local_got_ents[r_symndx];
5350 ent->addend = r_addend;
e717da7e 5351 ent->owner = abfd;
411e1bfb 5352 ent->tls_type = tls_type;
927be08e 5353 ent->is_indirect = FALSE;
411e1bfb
AM
5354 ent->got.refcount = 0;
5355 local_got_ents[r_symndx] = ent;
5356 }
5357 ent->got.refcount += 1;
5358 }
5359
e054468f 5360 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5361 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5362 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5363
5364 return local_plt + r_symndx;
65f38f15
AM
5365}
5366
411e1bfb 5367static bfd_boolean
e054468f 5368update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5369{
411e1bfb 5370 struct plt_entry *ent;
1e2f5b6e 5371
e054468f 5372 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5373 if (ent->addend == addend)
5374 break;
5375 if (ent == NULL)
1e2f5b6e 5376 {
411e1bfb 5377 bfd_size_type amt = sizeof (*ent);
4ce794b7 5378 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5379 if (ent == NULL)
5380 return FALSE;
e054468f 5381 ent->next = *plist;
411e1bfb
AM
5382 ent->addend = addend;
5383 ent->plt.refcount = 0;
e054468f 5384 *plist = ent;
1e2f5b6e 5385 }
411e1bfb 5386 ent->plt.refcount += 1;
b34976b6 5387 return TRUE;
1e2f5b6e
AM
5388}
5389
e054468f
AM
5390static bfd_boolean
5391is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5392{
5393 return (r_type == R_PPC64_REL24
5394 || r_type == R_PPC64_REL14
5395 || r_type == R_PPC64_REL14_BRTAKEN
5396 || r_type == R_PPC64_REL14_BRNTAKEN
5397 || r_type == R_PPC64_ADDR24
5398 || r_type == R_PPC64_ADDR14
5399 || r_type == R_PPC64_ADDR14_BRTAKEN
5400 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5401}
5402
5bd4f169 5403/* Look through the relocs for a section during the first phase, and
65f38f15 5404 calculate needed space in the global offset table, procedure
5d1634d7 5405 linkage table, and dynamic reloc sections. */
5bd4f169 5406
b34976b6 5407static bfd_boolean
4ce794b7
AM
5408ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5409 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5410{
65f38f15 5411 struct ppc_link_hash_table *htab;
5bd4f169 5412 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5413 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5414 const Elf_Internal_Rela *rel;
5415 const Elf_Internal_Rela *rel_end;
5bd4f169 5416 asection *sreloc;
3a71aa26 5417 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 5418 bfd_boolean is_opd;
5bd4f169 5419
0e1862bb 5420 if (bfd_link_relocatable (info))
b34976b6 5421 return TRUE;
5bd4f169 5422
680a3378
AM
5423 /* Don't do anything special with non-loaded, non-alloced sections.
5424 In particular, any relocs in such sections should not affect GOT
5425 and PLT reference counting (ie. we don't allow them to create GOT
5426 or PLT entries), there's no possibility or desire to optimize TLS
5427 relocs, and there's not much point in propagating relocs to shared
5428 libs that the dynamic linker won't relocate. */
5429 if ((sec->flags & SEC_ALLOC) == 0)
5430 return TRUE;
5431
0c8d6e5c 5432 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5433
65f38f15 5434 htab = ppc_hash_table (info);
4dfe6ac6
NC
5435 if (htab == NULL)
5436 return FALSE;
5437
3a71aa26
AM
5438 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5439 FALSE, FALSE, TRUE);
5440 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5441 FALSE, FALSE, TRUE);
0ffa91dd 5442 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5443 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5444 sreloc = NULL;
b9399fcf 5445 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
5446 rel_end = relocs + sec->reloc_count;
5447 for (rel = relocs; rel < rel_end; rel++)
5448 {
5449 unsigned long r_symndx;
5450 struct elf_link_hash_entry *h;
04c9666a 5451 enum elf_ppc64_reloc_type r_type;
727fc41e 5452 int tls_type;
7c8fe5c4 5453 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5454 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5455
5456 r_symndx = ELF64_R_SYM (rel->r_info);
5457 if (r_symndx < symtab_hdr->sh_info)
5458 h = NULL;
5459 else
973a3492
L
5460 {
5461 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5462 h = elf_follow_link (h);
1c865ab2
AM
5463
5464 if (h == htab->elf.hgot)
5465 sec->has_toc_reloc = 1;
973a3492 5466 }
5bd4f169 5467
727fc41e 5468 tls_type = 0;
e054468f 5469 ifunc = NULL;
25f23106
AM
5470 if (h != NULL)
5471 {
5472 if (h->type == STT_GNU_IFUNC)
5473 {
5474 h->needs_plt = 1;
5475 ifunc = &h->plt.plist;
5476 }
5477 }
5478 else
5479 {
5480 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5481 abfd, r_symndx);
5482 if (isym == NULL)
5483 return FALSE;
5484
5485 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5486 {
5487 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5488 rel->r_addend, PLT_IFUNC);
5489 if (ifunc == NULL)
5490 return FALSE;
5491 }
5492 }
727fc41e 5493
cbf95972 5494 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5495 switch (r_type)
5bd4f169 5496 {
727fc41e
AM
5497 case R_PPC64_TLSGD:
5498 case R_PPC64_TLSLD:
5499 /* These special tls relocs tie a call to __tls_get_addr with
5500 its parameter symbol. */
5501 break;
5502
411e1bfb
AM
5503 case R_PPC64_GOT_TLSLD16:
5504 case R_PPC64_GOT_TLSLD16_LO:
5505 case R_PPC64_GOT_TLSLD16_HI:
5506 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5507 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5508 goto dogottls;
5509
5510 case R_PPC64_GOT_TLSGD16:
5511 case R_PPC64_GOT_TLSGD16_LO:
5512 case R_PPC64_GOT_TLSGD16_HI:
5513 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5514 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5515 goto dogottls;
5516
5517 case R_PPC64_GOT_TPREL16_DS:
5518 case R_PPC64_GOT_TPREL16_LO_DS:
5519 case R_PPC64_GOT_TPREL16_HI:
5520 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 5521 if (bfd_link_dll (info))
411e1bfb
AM
5522 info->flags |= DF_STATIC_TLS;
5523 tls_type = TLS_TLS | TLS_TPREL;
5524 goto dogottls;
5525
5526 case R_PPC64_GOT_DTPREL16_DS:
5527 case R_PPC64_GOT_DTPREL16_LO_DS:
5528 case R_PPC64_GOT_DTPREL16_HI:
5529 case R_PPC64_GOT_DTPREL16_HA:
5530 tls_type = TLS_TLS | TLS_DTPREL;
5531 dogottls:
5532 sec->has_tls_reloc = 1;
1a0670f3 5533 /* Fall through */
411e1bfb 5534
5bd4f169 5535 case R_PPC64_GOT16:
5bd4f169 5536 case R_PPC64_GOT16_DS:
65f38f15
AM
5537 case R_PPC64_GOT16_HA:
5538 case R_PPC64_GOT16_HI:
5539 case R_PPC64_GOT16_LO:
5bd4f169 5540 case R_PPC64_GOT16_LO_DS:
65f38f15 5541 /* This symbol requires a global offset table entry. */
4c52953f 5542 sec->has_toc_reloc = 1;
33c0ec9d
AM
5543 if (r_type == R_PPC64_GOT_TLSLD16
5544 || r_type == R_PPC64_GOT_TLSGD16
5545 || r_type == R_PPC64_GOT_TPREL16_DS
5546 || r_type == R_PPC64_GOT_DTPREL16_DS
5547 || r_type == R_PPC64_GOT16
5548 || r_type == R_PPC64_GOT16_DS)
5549 {
5550 htab->do_multi_toc = 1;
d77c8a4b 5551 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5552 }
5553
e717da7e
AM
5554 if (ppc64_elf_tdata (abfd)->got == NULL
5555 && !create_got_section (abfd, info))
b34976b6 5556 return FALSE;
5bd4f169
AM
5557
5558 if (h != NULL)
5559 {
411e1bfb
AM
5560 struct ppc_link_hash_entry *eh;
5561 struct got_entry *ent;
65f38f15 5562
411e1bfb
AM
5563 eh = (struct ppc_link_hash_entry *) h;
5564 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5565 if (ent->addend == rel->r_addend
e717da7e 5566 && ent->owner == abfd
411e1bfb
AM
5567 && ent->tls_type == tls_type)
5568 break;
5569 if (ent == NULL)
5bd4f169 5570 {
411e1bfb 5571 bfd_size_type amt = sizeof (*ent);
4ce794b7 5572 ent = bfd_alloc (abfd, amt);
411e1bfb 5573 if (ent == NULL)
b34976b6 5574 return FALSE;
411e1bfb
AM
5575 ent->next = eh->elf.got.glist;
5576 ent->addend = rel->r_addend;
e717da7e 5577 ent->owner = abfd;
411e1bfb 5578 ent->tls_type = tls_type;
927be08e 5579 ent->is_indirect = FALSE;
411e1bfb
AM
5580 ent->got.refcount = 0;
5581 eh->elf.got.glist = ent;
5bd4f169 5582 }
411e1bfb 5583 ent->got.refcount += 1;
e7b938ca 5584 eh->tls_mask |= tls_type;
5bd4f169 5585 }
411e1bfb
AM
5586 else
5587 /* This is a global offset table entry for a local symbol. */
5588 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5589 rel->r_addend, tls_type))
5590 return FALSE;
a345bc8d
AM
5591
5592 /* We may also need a plt entry if the symbol turns out to be
5593 an ifunc. */
0e1862bb 5594 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5595 {
5596 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5597 return FALSE;
5598 }
5bd4f169
AM
5599 break;
5600
5bd4f169 5601 case R_PPC64_PLT16_HA:
65f38f15
AM
5602 case R_PPC64_PLT16_HI:
5603 case R_PPC64_PLT16_LO:
5604 case R_PPC64_PLT32:
5605 case R_PPC64_PLT64:
cbf95972
AM
5606 /* This symbol requires a procedure linkage table entry. */
5607 plt_list = ifunc;
5608 if (h != NULL)
e054468f 5609 {
e054468f
AM
5610 h->needs_plt = 1;
5611 if (h->root.root.string[0] == '.'
5612 && h->root.root.string[1] != '\0')
5613 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972
AM
5614 plt_list = &h->plt.plist;
5615 }
5616 if (plt_list == NULL)
5617 {
5618 /* It does not make sense to have a procedure linkage
5619 table entry for a non-ifunc local symbol. */
5620 info->callbacks->einfo
695344c0 5621 /* xgettext:c-format */
174d0a74 5622 (_("%H: %s reloc against local symbol\n"),
cbf95972
AM
5623 abfd, sec, rel->r_offset,
5624 ppc64_elf_howto_table[r_type]->name);
5625 bfd_set_error (bfd_error_bad_value);
5626 return FALSE;
e054468f 5627 }
cbf95972
AM
5628 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5629 return FALSE;
5bd4f169
AM
5630 break;
5631
5632 /* The following relocations don't need to propagate the
5633 relocation if linking a shared object since they are
5634 section relative. */
5635 case R_PPC64_SECTOFF:
5636 case R_PPC64_SECTOFF_LO:
5637 case R_PPC64_SECTOFF_HI:
5638 case R_PPC64_SECTOFF_HA:
5639 case R_PPC64_SECTOFF_DS:
5640 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5641 case R_PPC64_DTPREL16:
5642 case R_PPC64_DTPREL16_LO:
5643 case R_PPC64_DTPREL16_HI:
5644 case R_PPC64_DTPREL16_HA:
5645 case R_PPC64_DTPREL16_DS:
5646 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5647 case R_PPC64_DTPREL16_HIGH:
5648 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5649 case R_PPC64_DTPREL16_HIGHER:
5650 case R_PPC64_DTPREL16_HIGHERA:
5651 case R_PPC64_DTPREL16_HIGHEST:
5652 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5653 break;
5654
ad8e1ba5 5655 /* Nor do these. */
25f23106
AM
5656 case R_PPC64_REL16:
5657 case R_PPC64_REL16_LO:
5658 case R_PPC64_REL16_HI:
5659 case R_PPC64_REL16_HA:
a680de9a 5660 case R_PPC64_REL16DX_HA:
25f23106
AM
5661 break;
5662
45965137
AM
5663 /* Not supported as a dynamic relocation. */
5664 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5665 if (bfd_link_pic (info))
45965137
AM
5666 {
5667 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5668 ppc_howto_init ();
695344c0 5669 /* xgettext:c-format */
174d0a74 5670 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 5671 "in shared libraries and PIEs\n"),
45965137
AM
5672 abfd, sec, rel->r_offset,
5673 ppc64_elf_howto_table[r_type]->name);
5674 bfd_set_error (bfd_error_bad_value);
5675 return FALSE;
5676 }
5677 break;
5678
ad8e1ba5 5679 case R_PPC64_TOC16:
33c0ec9d
AM
5680 case R_PPC64_TOC16_DS:
5681 htab->do_multi_toc = 1;
d77c8a4b 5682 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5683 /* Fall through. */
ad8e1ba5
AM
5684 case R_PPC64_TOC16_LO:
5685 case R_PPC64_TOC16_HI:
5686 case R_PPC64_TOC16_HA:
ad8e1ba5 5687 case R_PPC64_TOC16_LO_DS:
4c52953f 5688 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5689 break;
5690
006589cf
AM
5691 /* Marker reloc. */
5692 case R_PPC64_ENTRY:
5693 break;
5694
5bd4f169
AM
5695 /* This relocation describes the C++ object vtable hierarchy.
5696 Reconstruct it for later use during GC. */
5697 case R_PPC64_GNU_VTINHERIT:
c152c796 5698 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5699 return FALSE;
5bd4f169
AM
5700 break;
5701
5702 /* This relocation describes which C++ vtable entries are actually
5703 used. Record for later use during GC. */
5704 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5705 BFD_ASSERT (h != NULL);
5706 if (h != NULL
5707 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5708 return FALSE;
5bd4f169
AM
5709 break;
5710
721956f4
AM
5711 case R_PPC64_REL14:
5712 case R_PPC64_REL14_BRTAKEN:
5713 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5714 {
5715 asection *dest = NULL;
5716
5717 /* Heuristic: If jumping outside our section, chances are
5718 we are going to need a stub. */
5719 if (h != NULL)
5720 {
5721 /* If the sym is weak it may be overridden later, so
5722 don't assume we know where a weak sym lives. */
5723 if (h->root.type == bfd_link_hash_defined)
5724 dest = h->root.u.def.section;
5725 }
5726 else
87d72d41
AM
5727 {
5728 Elf_Internal_Sym *isym;
5729
5730 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5731 abfd, r_symndx);
5732 if (isym == NULL)
5733 return FALSE;
5734
5735 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5736 }
5737
220c76dd 5738 if (dest != sec)
7c8fe5c4 5739 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5740 }
721956f4
AM
5741 /* Fall through. */
5742
5d1634d7 5743 case R_PPC64_REL24:
cbf95972
AM
5744 plt_list = ifunc;
5745 if (h != NULL)
5d1634d7 5746 {
e054468f
AM
5747 h->needs_plt = 1;
5748 if (h->root.root.string[0] == '.'
5749 && h->root.root.string[1] != '\0')
5750 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5751
3a71aa26 5752 if (h == tga || h == dottga)
cbf95972
AM
5753 {
5754 sec->has_tls_reloc = 1;
5755 if (rel != relocs
5756 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5757 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5758 /* We have a new-style __tls_get_addr call with
5759 a marker reloc. */
5760 ;
5761 else
5762 /* Mark this section as having an old-style call. */
5763 sec->has_tls_get_addr_call = 1;
5764 }
5765 plt_list = &h->plt.plist;
411e1bfb 5766 }
cbf95972
AM
5767
5768 /* We may need a .plt entry if the function this reloc
5769 refers to is in a shared lib. */
5770 if (plt_list
5771 && !update_plt_info (abfd, plt_list, rel->r_addend))
5772 return FALSE;
411e1bfb
AM
5773 break;
5774
cbf95972
AM
5775 case R_PPC64_ADDR14:
5776 case R_PPC64_ADDR14_BRNTAKEN:
5777 case R_PPC64_ADDR14_BRTAKEN:
5778 case R_PPC64_ADDR24:
5779 goto dodyn;
5780
411e1bfb
AM
5781 case R_PPC64_TPREL64:
5782 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5783 if (bfd_link_dll (info))
411e1bfb
AM
5784 info->flags |= DF_STATIC_TLS;
5785 goto dotlstoc;
5786
5787 case R_PPC64_DTPMOD64:
5788 if (rel + 1 < rel_end
5789 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5790 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5791 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5792 else
951fd09b 5793 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5794 goto dotlstoc;
5795
5796 case R_PPC64_DTPREL64:
5797 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5798 if (rel != relocs
5799 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5800 && rel[-1].r_offset == rel->r_offset - 8)
5801 /* This is the second reloc of a dtpmod, dtprel pair.
5802 Don't mark with TLS_DTPREL. */
5803 goto dodyn;
5804
5805 dotlstoc:
5806 sec->has_tls_reloc = 1;
5807 if (h != NULL)
5808 {
5809 struct ppc_link_hash_entry *eh;
5810 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5811 eh->tls_mask |= tls_type;
411e1bfb
AM
5812 }
5813 else
5814 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5815 rel->r_addend, tls_type))
5816 return FALSE;
5817
7c8fe5c4
AM
5818 ppc64_sec = ppc64_elf_section_data (sec);
5819 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5820 {
3a71aa26
AM
5821 bfd_size_type amt;
5822
e7b938ca 5823 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5824 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5825 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5826 if (ppc64_sec->u.toc.symndx == NULL)
5827 return FALSE;
5828 amt = sec->size * sizeof (bfd_vma) / 8;
5829 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5830 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5831 return FALSE;
7c8fe5c4
AM
5832 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5833 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5834 }
5835 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5836 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5837 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5838
5839 /* Mark the second slot of a GD or LD entry.
5840 -1 to indicate GD and -2 to indicate LD. */
5841 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5842 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5843 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5844 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5845 goto dodyn;
5846
5847 case R_PPC64_TPREL16:
5848 case R_PPC64_TPREL16_LO:
5849 case R_PPC64_TPREL16_HI:
5850 case R_PPC64_TPREL16_HA:
5851 case R_PPC64_TPREL16_DS:
5852 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5853 case R_PPC64_TPREL16_HIGH:
5854 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5855 case R_PPC64_TPREL16_HIGHER:
5856 case R_PPC64_TPREL16_HIGHERA:
5857 case R_PPC64_TPREL16_HIGHEST:
5858 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
5859 if (bfd_link_dll (info))
5860 info->flags |= DF_STATIC_TLS;
5861 goto dodyn;
5d1634d7 5862
e86ce104 5863 case R_PPC64_ADDR64:
b9399fcf 5864 if (is_opd
1e2f5b6e 5865 && rel + 1 < rel_end
4ce794b7 5866 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5867 {
8387904d 5868 if (h != NULL)
8c5b4e52 5869 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 5870 }
e86ce104
AM
5871 /* Fall through. */
5872
65f38f15
AM
5873 case R_PPC64_ADDR16:
5874 case R_PPC64_ADDR16_DS:
5875 case R_PPC64_ADDR16_HA:
5876 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5877 case R_PPC64_ADDR16_HIGH:
5878 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5879 case R_PPC64_ADDR16_HIGHER:
5880 case R_PPC64_ADDR16_HIGHERA:
5881 case R_PPC64_ADDR16_HIGHEST:
5882 case R_PPC64_ADDR16_HIGHESTA:
5883 case R_PPC64_ADDR16_LO:
5884 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5885 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5886 && rel->r_addend == 0)
5887 {
5888 /* We may need a .plt entry if this reloc refers to a
5889 function in a shared lib. */
5890 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5891 return FALSE;
5892 h->pointer_equality_needed = 1;
5893 }
5894 /* Fall through. */
5895
5896 case R_PPC64_REL30:
5897 case R_PPC64_REL32:
5898 case R_PPC64_REL64:
65f38f15 5899 case R_PPC64_ADDR32:
65f38f15
AM
5900 case R_PPC64_UADDR16:
5901 case R_PPC64_UADDR32:
5902 case R_PPC64_UADDR64:
5bd4f169 5903 case R_PPC64_TOC:
0e1862bb 5904 if (h != NULL && !bfd_link_pic (info))
81848ca0 5905 /* We may need a copy reloc. */
f5385ebf 5906 h->non_got_ref = 1;
81848ca0 5907
41bd81ab 5908 /* Don't propagate .opd relocs. */
b9399fcf 5909 if (NO_OPD_RELOCS && is_opd)
e86ce104 5910 break;
e86ce104 5911
65f38f15
AM
5912 /* If we are creating a shared library, and this is a reloc
5913 against a global symbol, or a non PC relative reloc
5914 against a local symbol, then we need to copy the reloc
5915 into the shared library. However, if we are linking with
5916 -Bsymbolic, we do not need to copy a reloc against a
5917 global symbol which is defined in an object we are
5918 including in the link (i.e., DEF_REGULAR is set). At
5919 this point we have not seen all the input files, so it is
5920 possible that DEF_REGULAR is not set now but will be set
5921 later (it is never cleared). In case of a weak definition,
5922 DEF_REGULAR may be cleared later by a strong definition in
5923 a shared library. We account for that possibility below by
f4656909 5924 storing information in the dyn_relocs field of the hash
65f38f15
AM
5925 table entry. A similar situation occurs when creating
5926 shared libraries and symbol visibility changes render the
5927 symbol local.
5928
5929 If on the other hand, we are creating an executable, we
5930 may need to keep relocations for symbols satisfied by a
5931 dynamic library if we manage to avoid copy relocs for the
5932 symbol. */
411e1bfb 5933 dodyn:
0e1862bb 5934 if ((bfd_link_pic (info)
1d483afe 5935 && (must_be_dyn_reloc (info, r_type)
65f38f15 5936 || (h != NULL
198f1157 5937 && (!SYMBOLIC_BIND (info, h)
65f38f15 5938 || h->root.type == bfd_link_hash_defweak
f5385ebf 5939 || !h->def_regular))))
f4656909 5940 || (ELIMINATE_COPY_RELOCS
0e1862bb 5941 && !bfd_link_pic (info)
65f38f15
AM
5942 && h != NULL
5943 && (h->root.type == bfd_link_hash_defweak
25f23106 5944 || !h->def_regular))
0e1862bb 5945 || (!bfd_link_pic (info)
25f23106 5946 && ifunc != NULL))
5bd4f169 5947 {
65f38f15
AM
5948 /* We must copy these reloc types into the output file.
5949 Create a reloc section in dynobj and make room for
5950 this reloc. */
5bd4f169
AM
5951 if (sreloc == NULL)
5952 {
83bac4b0
NC
5953 sreloc = _bfd_elf_make_dynamic_reloc_section
5954 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5955
5bd4f169 5956 if (sreloc == NULL)
83bac4b0 5957 return FALSE;
5bd4f169
AM
5958 }
5959
65f38f15
AM
5960 /* If this is a global symbol, we count the number of
5961 relocations we need for this symbol. */
5962 if (h != NULL)
5963 {
19e08130
AM
5964 struct elf_dyn_relocs *p;
5965 struct elf_dyn_relocs **head;
5966
ec338859 5967 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5968 p = *head;
5969 if (p == NULL || p->sec != sec)
5970 {
5971 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5972 if (p == NULL)
5973 return FALSE;
5974 p->next = *head;
5975 *head = p;
5976 p->sec = sec;
5977 p->count = 0;
5978 p->pc_count = 0;
5979 }
5980 p->count += 1;
5981 if (!must_be_dyn_reloc (info, r_type))
5982 p->pc_count += 1;
65f38f15
AM
5983 }
5984 else
5985 {
ec338859
AM
5986 /* Track dynamic relocs needed for local syms too.
5987 We really need local syms available to do this
5988 easily. Oh well. */
19e08130
AM
5989 struct ppc_dyn_relocs *p;
5990 struct ppc_dyn_relocs **head;
5991 bfd_boolean is_ifunc;
ec338859 5992 asection *s;
6edfbbad 5993 void *vpp;
87d72d41 5994 Elf_Internal_Sym *isym;
6edfbbad 5995
87d72d41
AM
5996 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5997 abfd, r_symndx);
5998 if (isym == NULL)
b34976b6 5999 return FALSE;
ec338859 6000
87d72d41
AM
6001 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6002 if (s == NULL)
6003 s = sec;
6004
6edfbbad 6005 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
6006 head = (struct ppc_dyn_relocs **) vpp;
6007 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6008 p = *head;
6009 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6010 p = p->next;
6011 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6012 {
6013 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6014 if (p == NULL)
6015 return FALSE;
6016 p->next = *head;
6017 *head = p;
6018 p->sec = sec;
6019 p->ifunc = is_ifunc;
6020 p->count = 0;
6021 }
6022 p->count += 1;
ec338859 6023 }
65f38f15 6024 }
5bd4f169 6025 break;
65f38f15
AM
6026
6027 default:
96e0dda4 6028 break;
5bd4f169
AM
6029 }
6030 }
6031
b34976b6 6032 return TRUE;
5bd4f169
AM
6033}
6034
ee67d69a
AM
6035/* Merge backend specific data from an object file to the output
6036 object file when linking. */
6037
6038static bfd_boolean
50e03d47 6039ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 6040{
50e03d47 6041 bfd *obfd = info->output_bfd;
ee67d69a
AM
6042 unsigned long iflags, oflags;
6043
6044 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6045 return TRUE;
6046
6047 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6048 return TRUE;
6049
50e03d47 6050 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6051 return FALSE;
6052
6053 iflags = elf_elfheader (ibfd)->e_flags;
6054 oflags = elf_elfheader (obfd)->e_flags;
6055
f6c7c3e8 6056 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6057 {
4eca0228 6058 _bfd_error_handler
695344c0 6059 /* xgettext:c-format */
871b3ab2 6060 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
6061 bfd_set_error (bfd_error_bad_value);
6062 return FALSE;
6063 }
f6c7c3e8 6064 else if (iflags != oflags && iflags != 0)
ee67d69a 6065 {
4eca0228 6066 _bfd_error_handler
695344c0 6067 /* xgettext:c-format */
871b3ab2 6068 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
6069 ibfd, iflags, oflags);
6070 bfd_set_error (bfd_error_bad_value);
6071 return FALSE;
6072 }
6073
50e03d47 6074 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
005d79fd 6075
ee67d69a 6076 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 6077 _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6078
6079 return TRUE;
6080}
6081
6082static bfd_boolean
6083ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6084{
6085 /* Print normal ELF private data. */
6086 _bfd_elf_print_private_bfd_data (abfd, ptr);
6087
6088 if (elf_elfheader (abfd)->e_flags != 0)
6089 {
6090 FILE *file = ptr;
6091
ee67d69a
AM
6092 fprintf (file, _("private flags = 0x%lx:"),
6093 elf_elfheader (abfd)->e_flags);
6094
6095 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6096 fprintf (file, _(" [abiv%ld]"),
6097 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6098 fputc ('\n', file);
6099 }
6100
6101 return TRUE;
6102}
6103
8387904d 6104/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6105 of the code entry point, and its section, which must be in the same
6106 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6107
6108static bfd_vma
6109opd_entry_value (asection *opd_sec,
6110 bfd_vma offset,
6111 asection **code_sec,
aef36ac1
AM
6112 bfd_vma *code_off,
6113 bfd_boolean in_code_sec)
8387904d
AM
6114{
6115 bfd *opd_bfd = opd_sec->owner;
8860955f 6116 Elf_Internal_Rela *relocs;
8387904d 6117 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6118 bfd_vma val;
8387904d 6119
9f296da3
AM
6120 /* No relocs implies we are linking a --just-symbols object, or looking
6121 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6122 if (opd_sec->reloc_count == 0)
6123 {
729eabd5 6124 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6125
729eabd5
AM
6126 if (contents == NULL)
6127 {
6128 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6129 return (bfd_vma) -1;
6130 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6131 }
ee1e4ede 6132
dbb3fbbb 6133 /* PR 17512: file: 64b9dfbb. */
451dfd38 6134 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6135 return (bfd_vma) -1;
6136
729eabd5 6137 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6138 if (code_sec != NULL)
6139 {
6140 asection *sec, *likely = NULL;
ee1e4ede 6141
aef36ac1 6142 if (in_code_sec)
4b85d634 6143 {
aef36ac1
AM
6144 sec = *code_sec;
6145 if (sec->vma <= val
6146 && val < sec->vma + sec->size)
6147 likely = sec;
6148 else
6149 val = -1;
6150 }
6151 else
6152 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6153 if (sec->vma <= val
6154 && (sec->flags & SEC_LOAD) != 0
6155 && (sec->flags & SEC_ALLOC) != 0)
6156 likely = sec;
6157 if (likely != NULL)
6158 {
6159 *code_sec = likely;
6160 if (code_off != NULL)
6161 *code_off = val - likely->vma;
4b85d634
AM
6162 }
6163 }
aef36ac1 6164 return val;
4b85d634
AM
6165 }
6166
0c8d6e5c 6167 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6168
729eabd5 6169 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6170 if (relocs == NULL)
6171 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6172 /* PR 17512: file: df8e1fd6. */
6173 if (relocs == NULL)
6174 return (bfd_vma) -1;
645ea6a9 6175
8387904d 6176 /* Go find the opd reloc at the sym address. */
8860955f 6177 lo = relocs;
8387904d 6178 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6179 val = (bfd_vma) -1;
8387904d
AM
6180 while (lo < hi)
6181 {
6182 look = lo + (hi - lo) / 2;
6183 if (look->r_offset < offset)
6184 lo = look + 1;
6185 else if (look->r_offset > offset)
6186 hi = look;
6187 else
6188 {
0ffa91dd
NC
6189 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6190
8387904d
AM
6191 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6192 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6193 {
6194 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6195 asection *sec = NULL;
8387904d 6196
b53dfeb2
AM
6197 if (symndx >= symtab_hdr->sh_info
6198 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6199 {
6200 struct elf_link_hash_entry **sym_hashes;
6201 struct elf_link_hash_entry *rh;
6202
6203 sym_hashes = elf_sym_hashes (opd_bfd);
6204 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6205 if (rh != NULL)
6206 {
6207 rh = elf_follow_link (rh);
bb854a36
AM
6208 if (rh->root.type != bfd_link_hash_defined
6209 && rh->root.type != bfd_link_hash_defweak)
6210 break;
6211 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6212 {
bb854a36
AM
6213 val = rh->root.u.def.value;
6214 sec = rh->root.u.def.section;
b53dfeb2
AM
6215 }
6216 }
6217 }
6218
6219 if (sec == NULL)
6220 {
6221 Elf_Internal_Sym *sym;
6222
6223 if (symndx < symtab_hdr->sh_info)
6224 {
6225 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6226 if (sym == NULL)
6227 {
6228 size_t symcnt = symtab_hdr->sh_info;
6229 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6230 symcnt, 0,
6231 NULL, NULL, NULL);
6232 if (sym == NULL)
6233 break;
6234 symtab_hdr->contents = (bfd_byte *) sym;
6235 }
6236 sym += symndx;
128205bb
AM
6237 }
6238 else
6239 {
b53dfeb2
AM
6240 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6241 1, symndx,
6242 NULL, NULL, NULL);
128205bb
AM
6243 if (sym == NULL)
6244 break;
128205bb 6245 }
b53dfeb2
AM
6246 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6247 if (sec == NULL)
6248 break;
6249 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6250 val = sym->st_value;
8387904d 6251 }
b53dfeb2 6252
8387904d
AM
6253 val += look->r_addend;
6254 if (code_off != NULL)
6255 *code_off = val;
6256 if (code_sec != NULL)
aef36ac1
AM
6257 {
6258 if (in_code_sec && *code_sec != sec)
6259 return -1;
6260 else
6261 *code_sec = sec;
6262 }
b53dfeb2 6263 if (sec->output_section != NULL)
8387904d 6264 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6265 }
6266 break;
6267 }
6268 }
645ea6a9 6269
645ea6a9 6270 return val;
8387904d
AM
6271}
6272
aef36ac1
AM
6273/* If the ELF symbol SYM might be a function in SEC, return the
6274 function size and set *CODE_OFF to the function's entry point,
6275 otherwise return zero. */
9f296da3 6276
aef36ac1
AM
6277static bfd_size_type
6278ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6279 bfd_vma *code_off)
9f296da3 6280{
aef36ac1
AM
6281 bfd_size_type size;
6282
6283 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6284 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6285 return 0;
6286
6287 size = 0;
6288 if (!(sym->flags & BSF_SYNTHETIC))
6289 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6290
6291 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6292 {
b07bca4e
AM
6293 struct _opd_sec_data *opd = get_opd_info (sym->section);
6294 bfd_vma symval = sym->value;
6295
6296 if (opd != NULL
6297 && opd->adjust != NULL
6298 && elf_section_data (sym->section)->relocs != NULL)
6299 {
6300 /* opd_entry_value will use cached relocs that have been
6301 adjusted, but with raw symbols. That means both local
6302 and global symbols need adjusting. */
6303 long adjust = opd->adjust[OPD_NDX (symval)];
6304 if (adjust == -1)
6305 return 0;
6306 symval += adjust;
6307 }
6308
6309 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6310 &sec, code_off, TRUE) == (bfd_vma) -1)
6311 return 0;
6312 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6313 symbol. This size has nothing to do with the code size of the
6314 function, which is what we're supposed to return, but the
6315 code size isn't available without looking up the dot-sym.
6316 However, doing that would be a waste of time particularly
6317 since elf_find_function will look at the dot-sym anyway.
6318 Now, elf_find_function will keep the largest size of any
6319 function sym found at the code address of interest, so return
6320 1 here to avoid it incorrectly caching a larger function size
6321 for a small function. This does mean we return the wrong
6322 size for a new-ABI function of size 24, but all that does is
6323 disable caching for such functions. */
6324 if (size == 24)
6325 size = 1;
9f296da3 6326 }
aef36ac1
AM
6327 else
6328 {
6329 if (sym->section != sec)
6330 return 0;
6331 *code_off = sym->value;
6332 }
6333 if (size == 0)
6334 size = 1;
6335 return size;
9f296da3
AM
6336}
6337
f378ab09
AM
6338/* Return true if symbol is a strong function defined in an ELFv2
6339 object with st_other localentry bits of zero, ie. its local entry
6340 point coincides with its global entry point. */
6341
6342static bfd_boolean
6343is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6344{
6345 return (h != NULL
6346 && h->type == STT_FUNC
6347 && h->root.type == bfd_link_hash_defined
6348 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 6349 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
6350 && is_ppc64_elf (h->root.u.def.section->owner)
6351 && abiversion (h->root.u.def.section->owner) >= 2);
6352}
6353
854b41e7
AM
6354/* Return true if symbol is defined in a regular object file. */
6355
6356static bfd_boolean
6357is_static_defined (struct elf_link_hash_entry *h)
6358{
6359 return ((h->root.type == bfd_link_hash_defined
6360 || h->root.type == bfd_link_hash_defweak)
6361 && h->root.u.def.section != NULL
6362 && h->root.u.def.section->output_section != NULL);
6363}
6364
b31867b6
AM
6365/* If FDH is a function descriptor symbol, return the associated code
6366 entry symbol if it is defined. Return NULL otherwise. */
6367
6368static struct ppc_link_hash_entry *
6369defined_code_entry (struct ppc_link_hash_entry *fdh)
6370{
6371 if (fdh->is_func_descriptor)
6372 {
6373 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6374 if (fh->elf.root.type == bfd_link_hash_defined
6375 || fh->elf.root.type == bfd_link_hash_defweak)
6376 return fh;
6377 }
6378 return NULL;
6379}
6380
6381/* If FH is a function code entry symbol, return the associated
6382 function descriptor symbol if it is defined. Return NULL otherwise. */
6383
6384static struct ppc_link_hash_entry *
6385defined_func_desc (struct ppc_link_hash_entry *fh)
6386{
6387 if (fh->oh != NULL
6388 && fh->oh->is_func_descriptor)
6389 {
6390 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6391 if (fdh->elf.root.type == bfd_link_hash_defined
6392 || fdh->elf.root.type == bfd_link_hash_defweak)
6393 return fdh;
6394 }
6395 return NULL;
6396}
6397
8c5b4e52
AM
6398static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6399
6400/* Garbage collect sections, after first dealing with dot-symbols. */
6401
6402static bfd_boolean
6403ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6404{
6405 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6406
6407 if (htab != NULL && htab->need_func_desc_adj)
6408 {
6409 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6410 htab->need_func_desc_adj = 0;
6411 }
6412 return bfd_elf_gc_sections (abfd, info);
6413}
6414
74f0fb50
AM
6415/* Mark all our entry sym sections, both opd and code section. */
6416
6417static void
6418ppc64_elf_gc_keep (struct bfd_link_info *info)
6419{
6420 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6421 struct bfd_sym_chain *sym;
6422
4dfe6ac6
NC
6423 if (htab == NULL)
6424 return;
6425
74f0fb50
AM
6426 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6427 {
b31867b6 6428 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6429 asection *sec;
6430
6431 eh = (struct ppc_link_hash_entry *)
b31867b6 6432 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6433 if (eh == NULL)
6434 continue;
6435 if (eh->elf.root.type != bfd_link_hash_defined
6436 && eh->elf.root.type != bfd_link_hash_defweak)
6437 continue;
6438
b31867b6
AM
6439 fh = defined_code_entry (eh);
6440 if (fh != NULL)
74f0fb50 6441 {
b31867b6 6442 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6443 sec->flags |= SEC_KEEP;
6444 }
6445 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6446 && opd_entry_value (eh->elf.root.u.def.section,
6447 eh->elf.root.u.def.value,
aef36ac1 6448 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6449 sec->flags |= SEC_KEEP;
6450
6451 sec = eh->elf.root.u.def.section;
6452 sec->flags |= SEC_KEEP;
6453 }
6454}
6455
64d03ab5
AM
6456/* Mark sections containing dynamically referenced symbols. When
6457 building shared libraries, we must assume that any visible symbol is
6458 referenced. */
6459
6460static bfd_boolean
6461ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6462{
6463 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6464 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6465 struct ppc_link_hash_entry *fdh;
b407645f 6466 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6467
64d03ab5 6468 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6469 fdh = defined_func_desc (eh);
6470 if (fdh != NULL)
6471 eh = fdh;
64d03ab5
AM
6472
6473 if ((eh->elf.root.type == bfd_link_hash_defined
6474 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 6475 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 6476 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6477 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6478 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6479 && (!bfd_link_executable (info)
e278ae05 6480 || info->gc_keep_exported
b407645f
AM
6481 || info->export_dynamic
6482 || (eh->elf.dynamic
6483 && d != NULL
6484 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
e278ae05 6485 && (eh->elf.versioned >= versioned
4c58e0d8
AM
6486 || !bfd_hide_sym_by_version (info->version_info,
6487 eh->elf.root.root.string)))))
64d03ab5
AM
6488 {
6489 asection *code_sec;
b31867b6 6490 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6491
6492 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6493
6494 /* Function descriptor syms cause the associated
6495 function code sym section to be marked. */
b31867b6
AM
6496 fh = defined_code_entry (eh);
6497 if (fh != NULL)
6498 {
6499 code_sec = fh->elf.root.u.def.section;
6500 code_sec->flags |= SEC_KEEP;
6501 }
64d03ab5
AM
6502 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6503 && opd_entry_value (eh->elf.root.u.def.section,
6504 eh->elf.root.u.def.value,
aef36ac1 6505 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6506 code_sec->flags |= SEC_KEEP;
6507 }
6508
6509 return TRUE;
6510}
6511
5bd4f169
AM
6512/* Return the section that should be marked against GC for a given
6513 relocation. */
6514
6515static asection *
4ce794b7 6516ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6517 struct bfd_link_info *info,
4ce794b7
AM
6518 Elf_Internal_Rela *rel,
6519 struct elf_link_hash_entry *h,
6520 Elf_Internal_Sym *sym)
5bd4f169 6521{
ccfa59ea
AM
6522 asection *rsec;
6523
ccfa59ea
AM
6524 /* Syms return NULL if we're marking .opd, so we avoid marking all
6525 function sections, as all functions are referenced in .opd. */
6526 rsec = NULL;
6527 if (get_opd_info (sec) != NULL)
6528 return rsec;
1e2f5b6e 6529
5bd4f169
AM
6530 if (h != NULL)
6531 {
04c9666a 6532 enum elf_ppc64_reloc_type r_type;
b31867b6 6533 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6534
4ce794b7 6535 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6536 switch (r_type)
5bd4f169
AM
6537 {
6538 case R_PPC64_GNU_VTINHERIT:
6539 case R_PPC64_GNU_VTENTRY:
6540 break;
6541
6542 default:
6543 switch (h->root.type)
6544 {
6545 case bfd_link_hash_defined:
6546 case bfd_link_hash_defweak:
ccfa59ea 6547 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6548 fdh = defined_func_desc (eh);
6549 if (fdh != NULL)
8c5b4e52
AM
6550 {
6551 /* -mcall-aixdesc code references the dot-symbol on
6552 a call reloc. Mark the function descriptor too
6553 against garbage collection. */
6554 fdh->elf.mark = 1;
60d67dc8
AM
6555 if (fdh->elf.is_weakalias)
6556 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6557 eh = fdh;
6558 }
1e2f5b6e
AM
6559
6560 /* Function descriptor syms cause the associated
6561 function code sym section to be marked. */
b31867b6
AM
6562 fh = defined_code_entry (eh);
6563 if (fh != NULL)
ccfa59ea
AM
6564 {
6565 /* They also mark their opd section. */
74f0fb50 6566 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6567
b31867b6 6568 rsec = fh->elf.root.u.def.section;
ccfa59ea 6569 }
8387904d
AM
6570 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6571 && opd_entry_value (eh->elf.root.u.def.section,
6572 eh->elf.root.u.def.value,
aef36ac1 6573 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6574 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6575 else
1e2f5b6e
AM
6576 rsec = h->root.u.def.section;
6577 break;
5bd4f169
AM
6578
6579 case bfd_link_hash_common:
1e2f5b6e
AM
6580 rsec = h->root.u.c.p->section;
6581 break;
5bd4f169
AM
6582
6583 default:
fb34365b 6584 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6585 }
6586 }
6587 }
6588 else
6589 {
74f0fb50 6590 struct _opd_sec_data *opd;
1e2f5b6e
AM
6591
6592 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6593 opd = get_opd_info (rsec);
6594 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6595 {
74f0fb50 6596 rsec->gc_mark = 1;
ccfa59ea 6597
51aecdc5 6598 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6599 }
5bd4f169
AM
6600 }
6601
1e2f5b6e 6602 return rsec;
5bd4f169
AM
6603}
6604
deb0e272
AM
6605/* The maximum size of .sfpr. */
6606#define SFPR_MAX (218*4)
6607
6608struct sfpr_def_parms
6609{
699733f6
AM
6610 const char name[12];
6611 unsigned char lo, hi;
deb0e272
AM
6612 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6613 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6614};
6615
a4b6fadd
AM
6616/* Auto-generate _save*, _rest* functions in .sfpr.
6617 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6618 instead. */
deb0e272 6619
4dfe6ac6 6620static bfd_boolean
a4b6fadd
AM
6621sfpr_define (struct bfd_link_info *info,
6622 const struct sfpr_def_parms *parm,
6623 asection *stub_sec)
deb0e272
AM
6624{
6625 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6626 unsigned int i;
6627 size_t len = strlen (parm->name);
6628 bfd_boolean writing = FALSE;
699733f6 6629 char sym[16];
deb0e272 6630
4dfe6ac6
NC
6631 if (htab == NULL)
6632 return FALSE;
6633
deb0e272
AM
6634 memcpy (sym, parm->name, len);
6635 sym[len + 2] = 0;
6636
6637 for (i = parm->lo; i <= parm->hi; i++)
6638 {
a4b6fadd 6639 struct ppc_link_hash_entry *h;
deb0e272
AM
6640
6641 sym[len + 0] = i / 10 + '0';
6642 sym[len + 1] = i % 10 + '0';
a4b6fadd 6643 h = (struct ppc_link_hash_entry *)
b32547cd 6644 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6645 if (stub_sec != NULL)
deb0e272 6646 {
a4b6fadd
AM
6647 if (h != NULL
6648 && h->elf.root.type == bfd_link_hash_defined
6649 && h->elf.root.u.def.section == htab->sfpr)
6650 {
6651 struct elf_link_hash_entry *s;
6652 char buf[32];
6653 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6654 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6655 if (s == NULL)
6656 return FALSE;
6657 if (s->root.type == bfd_link_hash_new
6658 || (s->root.type = bfd_link_hash_defined
6659 && s->root.u.def.section == stub_sec))
6660 {
6661 s->root.type = bfd_link_hash_defined;
6662 s->root.u.def.section = stub_sec;
6663 s->root.u.def.value = (stub_sec->size
6664 + h->elf.root.u.def.value);
6665 s->ref_regular = 1;
6666 s->def_regular = 1;
6667 s->ref_regular_nonweak = 1;
6668 s->forced_local = 1;
6669 s->non_elf = 0;
6670 s->root.linker_def = 1;
6671 }
6672 }
6673 continue;
6674 }
6675 if (h != NULL)
6676 {
6677 h->save_res = 1;
6678 if (!h->elf.def_regular)
deb0e272 6679 {
a4b6fadd
AM
6680 h->elf.root.type = bfd_link_hash_defined;
6681 h->elf.root.u.def.section = htab->sfpr;
6682 h->elf.root.u.def.value = htab->sfpr->size;
6683 h->elf.type = STT_FUNC;
6684 h->elf.def_regular = 1;
b32547cd 6685 h->elf.non_elf = 0;
a4b6fadd
AM
6686 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6687 writing = TRUE;
deb0e272 6688 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6689 {
6690 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6691 if (htab->sfpr->contents == NULL)
6692 return FALSE;
6693 }
deb0e272
AM
6694 }
6695 }
6696 if (writing)
6697 {
6698 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6699 if (i != parm->hi)
6700 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6701 else
6702 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6703 htab->sfpr->size = p - htab->sfpr->contents;
6704 }
6705 }
6706
6707 return TRUE;
6708}
6709
6710static bfd_byte *
6711savegpr0 (bfd *abfd, bfd_byte *p, int r)
6712{
6713 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6714 return p + 4;
6715}
6716
6717static bfd_byte *
6718savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6719{
6720 p = savegpr0 (abfd, p, r);
a078d95a 6721 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6722 p = p + 4;
6723 bfd_put_32 (abfd, BLR, p);
6724 return p + 4;
6725}
6726
6727static bfd_byte *
6728restgpr0 (bfd *abfd, bfd_byte *p, int r)
6729{
6730 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6731 return p + 4;
6732}
6733
6734static bfd_byte *
6735restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6736{
a078d95a 6737 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6738 p = p + 4;
6739 p = restgpr0 (abfd, p, r);
6740 bfd_put_32 (abfd, MTLR_R0, p);
6741 p = p + 4;
6742 if (r == 29)
6743 {
6744 p = restgpr0 (abfd, p, 30);
6745 p = restgpr0 (abfd, p, 31);
6746 }
6747 bfd_put_32 (abfd, BLR, p);
6748 return p + 4;
6749}
6750
6751static bfd_byte *
6752savegpr1 (bfd *abfd, bfd_byte *p, int r)
6753{
6754 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6755 return p + 4;
6756}
6757
6758static bfd_byte *
6759savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6760{
6761 p = savegpr1 (abfd, p, r);
6762 bfd_put_32 (abfd, BLR, p);
6763 return p + 4;
6764}
6765
6766static bfd_byte *
6767restgpr1 (bfd *abfd, bfd_byte *p, int r)
6768{
6769 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6770 return p + 4;
6771}
6772
6773static bfd_byte *
6774restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6775{
6776 p = restgpr1 (abfd, p, r);
6777 bfd_put_32 (abfd, BLR, p);
6778 return p + 4;
6779}
6780
6781static bfd_byte *
6782savefpr (bfd *abfd, bfd_byte *p, int r)
6783{
6784 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6785 return p + 4;
6786}
6787
6788static bfd_byte *
6789savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6790{
6791 p = savefpr (abfd, p, r);
a078d95a 6792 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6793 p = p + 4;
6794 bfd_put_32 (abfd, BLR, p);
6795 return p + 4;
6796}
6797
6798static bfd_byte *
6799restfpr (bfd *abfd, bfd_byte *p, int r)
6800{
6801 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6802 return p + 4;
6803}
6804
6805static bfd_byte *
6806restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6807{
a078d95a 6808 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6809 p = p + 4;
6810 p = restfpr (abfd, p, r);
6811 bfd_put_32 (abfd, MTLR_R0, p);
6812 p = p + 4;
6813 if (r == 29)
6814 {
6815 p = restfpr (abfd, p, 30);
6816 p = restfpr (abfd, p, 31);
6817 }
6818 bfd_put_32 (abfd, BLR, p);
6819 return p + 4;
6820}
6821
6822static bfd_byte *
6823savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6824{
6825 p = savefpr (abfd, p, r);
6826 bfd_put_32 (abfd, BLR, p);
6827 return p + 4;
6828}
6829
6830static bfd_byte *
6831restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6832{
6833 p = restfpr (abfd, p, r);
6834 bfd_put_32 (abfd, BLR, p);
6835 return p + 4;
6836}
6837
6838static bfd_byte *
6839savevr (bfd *abfd, bfd_byte *p, int r)
6840{
6841 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6842 p = p + 4;
6843 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6844 return p + 4;
6845}
6846
6847static bfd_byte *
6848savevr_tail (bfd *abfd, bfd_byte *p, int r)
6849{
6850 p = savevr (abfd, p, r);
6851 bfd_put_32 (abfd, BLR, p);
6852 return p + 4;
6853}
6854
6855static bfd_byte *
6856restvr (bfd *abfd, bfd_byte *p, int r)
6857{
6858 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6859 p = p + 4;
6860 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6861 return p + 4;
6862}
6863
6864static bfd_byte *
6865restvr_tail (bfd *abfd, bfd_byte *p, int r)
6866{
6867 p = restvr (abfd, p, r);
6868 bfd_put_32 (abfd, BLR, p);
6869 return p + 4;
6870}
6871
e86ce104
AM
6872/* Called via elf_link_hash_traverse to transfer dynamic linking
6873 information on function code symbol entries to their corresponding
6874 function descriptor symbol entries. */
deb0e272 6875
b34976b6 6876static bfd_boolean
4ce794b7 6877func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6878{
e86ce104 6879 struct bfd_link_info *info;
65f38f15 6880 struct ppc_link_hash_table *htab;
50bc7936
AM
6881 struct ppc_link_hash_entry *fh;
6882 struct ppc_link_hash_entry *fdh;
6883 bfd_boolean force_local;
5bd4f169 6884
50bc7936
AM
6885 fh = (struct ppc_link_hash_entry *) h;
6886 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6887 return TRUE;
e86ce104 6888
8c5b4e52
AM
6889 if (!fh->is_func)
6890 return TRUE;
6891
6892 if (fh->elf.root.root.string[0] != '.'
6893 || fh->elf.root.root.string[1] == '\0')
6894 return TRUE;
6895
4ce794b7 6896 info = inf;
65f38f15 6897 htab = ppc_hash_table (info);
4dfe6ac6
NC
6898 if (htab == NULL)
6899 return FALSE;
5bd4f169 6900
8c5b4e52
AM
6901 /* Find the corresponding function descriptor symbol. */
6902 fdh = lookup_fdh (fh, htab);
6903
c09bdfe5
AM
6904 /* Resolve undefined references to dot-symbols as the value
6905 in the function descriptor, if we have one in a regular object.
6906 This is to satisfy cases like ".quad .foo". Calls to functions
6907 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6908 if ((fh->elf.root.type == bfd_link_hash_undefined
6909 || fh->elf.root.type == bfd_link_hash_undefweak)
6910 && (fdh->elf.root.type == bfd_link_hash_defined
6911 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6912 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6913 && opd_entry_value (fdh->elf.root.u.def.section,
6914 fdh->elf.root.u.def.value,
c09bdfe5 6915 &fh->elf.root.u.def.section,
aef36ac1 6916 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6917 {
b31867b6 6918 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6919 fh->elf.forced_local = 1;
b31867b6
AM
6920 fh->elf.def_regular = fdh->elf.def_regular;
6921 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6922 }
6923
8c5b4e52
AM
6924 if (!fh->elf.dynamic)
6925 {
6926 struct plt_entry *ent;
5bd4f169 6927
8c5b4e52
AM
6928 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6929 if (ent->plt.refcount > 0)
6930 break;
6931 if (ent == NULL)
6932 return TRUE;
6933 }
5bd4f169 6934
8c5b4e52 6935 /* Create a descriptor as undefined if necessary. */
50bc7936 6936 if (fdh == NULL
0e1862bb 6937 && !bfd_link_executable (info)
50bc7936
AM
6938 && (fh->elf.root.type == bfd_link_hash_undefined
6939 || fh->elf.root.type == bfd_link_hash_undefweak))
6940 {
908b32fc 6941 fdh = make_fdh (info, fh);
bb700d78
AM
6942 if (fdh == NULL)
6943 return FALSE;
50bc7936 6944 }
648cca2c 6945
8c5b4e52 6946 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6947 if (fdh != NULL
6948 && fdh->fake
8c5b4e52
AM
6949 && (fh->elf.root.type == bfd_link_hash_defined
6950 || fh->elf.root.type == bfd_link_hash_defweak))
6951 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6952
8c5b4e52
AM
6953 /* Transfer dynamic linking information to the function descriptor. */
6954 if (fdh != NULL)
6955 {
f5385ebf
AM
6956 fdh->elf.ref_regular |= fh->elf.ref_regular;
6957 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6958 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6959 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6960 fdh->elf.dynamic |= fh->elf.dynamic;
6961 fdh->elf.needs_plt |= (fh->elf.needs_plt
6962 || fh->elf.type == STT_FUNC
6963 || fh->elf.type == STT_GNU_IFUNC);
6964 move_plt_plist (fh, fdh);
6965
6966 if (!fdh->elf.forced_local
6967 && fh->elf.dynindx != -1)
6968 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6969 return FALSE;
e86ce104
AM
6970 }
6971
50bc7936
AM
6972 /* Now that the info is on the function descriptor, clear the
6973 function code sym info. Any function code syms for which we
6974 don't have a definition in a regular file, we force local.
6975 This prevents a shared library from exporting syms that have
6976 been imported from another library. Function code syms that
6977 are really in the library we must leave global to prevent the
6978 linker dragging in a definition from a static library. */
93f3fa99
AM
6979 force_local = (!fh->elf.def_regular
6980 || fdh == NULL
6981 || !fdh->elf.def_regular
6982 || fdh->elf.forced_local);
50bc7936
AM
6983 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6984
b34976b6 6985 return TRUE;
e86ce104 6986}
40b8271b 6987
a4b6fadd
AM
6988static const struct sfpr_def_parms save_res_funcs[] =
6989 {
6990 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6991 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6992 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6993 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6994 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6995 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6996 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6997 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6998 { "._savef", 14, 31, savefpr, savefpr1_tail },
6999 { "._restf", 14, 31, restfpr, restfpr1_tail },
7000 { "_savevr_", 20, 31, savevr, savevr_tail },
7001 { "_restvr_", 20, 31, restvr, restvr_tail }
7002 };
7003
e86ce104 7004/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
7005 this hook to a) provide some gcc support functions, and b) transfer
7006 dynamic linking information gathered so far on function code symbol
7007 entries, to their corresponding function descriptor symbol entries. */
deb0e272 7008
b34976b6 7009static bfd_boolean
4ce794b7
AM
7010ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7011 struct bfd_link_info *info)
e86ce104
AM
7012{
7013 struct ppc_link_hash_table *htab;
7014
7015 htab = ppc_hash_table (info);
4dfe6ac6
NC
7016 if (htab == NULL)
7017 return FALSE;
7018
b32547cd
AM
7019 /* Provide any missing _save* and _rest* functions. */
7020 if (htab->sfpr != NULL)
7021 {
7022 unsigned int i;
7023
7024 htab->sfpr->size = 0;
7025 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7026 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7027 return FALSE;
7028 if (htab->sfpr->size == 0)
7029 htab->sfpr->flags |= SEC_EXCLUDE;
7030 }
7031
7032 if (bfd_link_relocatable (info))
7033 return TRUE;
7034
7035 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7036 {
7037 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7038 /* Make .TOC. defined so as to prevent it being made dynamic.
7039 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7040 if (!htab->elf.hgot->def_regular
7041 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7042 {
7043 htab->elf.hgot->root.type = bfd_link_hash_defined;
7044 htab->elf.hgot->root.u.def.value = 0;
7045 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7046 htab->elf.hgot->def_regular = 1;
7047 htab->elf.hgot->root.linker_def = 1;
7048 }
dba6fa9b 7049 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7050 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7051 | STV_HIDDEN);
7052 }
c66bb0ee 7053
8c5b4e52
AM
7054 if (htab->need_func_desc_adj)
7055 {
7056 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7057 htab->need_func_desc_adj = 0;
7058 }
805fc799 7059
b34976b6 7060 return TRUE;
e86ce104
AM
7061}
7062
98bbb1b8 7063/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 7064
98bbb1b8 7065static asection *
a345bc8d
AM
7066readonly_dynrelocs (struct elf_link_hash_entry *h)
7067{
7068 struct ppc_link_hash_entry *eh;
7069 struct elf_dyn_relocs *p;
7070
7071 eh = (struct ppc_link_hash_entry *) h;
7072 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7073 {
7074 asection *s = p->sec->output_section;
7075
7076 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 7077 return p->sec;
a345bc8d 7078 }
98bbb1b8 7079 return NULL;
a345bc8d
AM
7080}
7081
d311bc8b 7082/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
7083 aliases, that apply to read-only sections. Cannot be used after
7084 size_dynamic_sections. */
d311bc8b
AM
7085
7086static bfd_boolean
7087alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7088{
7089 struct ppc_link_hash_entry *eh;
7090
7091 eh = (struct ppc_link_hash_entry *) h;
7092 do
7093 {
7094 if (readonly_dynrelocs (&eh->elf))
7095 return TRUE;
ab2477e1 7096 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
d311bc8b
AM
7097 } while (eh != NULL && &eh->elf != h);
7098
7099 return FALSE;
7100}
8a2058b5 7101
8a9e8e72
AM
7102/* Return whether EH has pc-relative dynamic relocs. */
7103
7104static bfd_boolean
7105pc_dynrelocs (struct ppc_link_hash_entry *eh)
7106{
7107 struct elf_dyn_relocs *p;
7108
7109 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7110 if (p->pc_count != 0)
7111 return TRUE;
7112 return FALSE;
7113}
7114
8a2058b5
AM
7115/* Return true if a global entry stub will be created for H. Valid
7116 for ELFv2 before plt entries have been allocated. */
7117
7118static bfd_boolean
7119global_entry_stub (struct elf_link_hash_entry *h)
7120{
7121 struct plt_entry *pent;
7122
7123 if (!h->pointer_equality_needed
7124 || h->def_regular)
7125 return FALSE;
7126
7127 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7128 if (pent->plt.refcount > 0
7129 && pent->addend == 0)
7130 return TRUE;
7131
7132 return FALSE;
7133}
7134
e86ce104
AM
7135/* Adjust a symbol defined by a dynamic object and referenced by a
7136 regular object. The current definition is in some section of the
7137 dynamic object, but we're not including those sections. We have to
7138 change the definition to something the rest of the link can
7139 understand. */
7140
b34976b6 7141static bfd_boolean
4ce794b7
AM
7142ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7143 struct elf_link_hash_entry *h)
e86ce104
AM
7144{
7145 struct ppc_link_hash_table *htab;
5474d94f 7146 asection *s, *srel;
e86ce104
AM
7147
7148 htab = ppc_hash_table (info);
4dfe6ac6
NC
7149 if (htab == NULL)
7150 return FALSE;
e86ce104
AM
7151
7152 /* Deal with function syms. */
7153 if (h->type == STT_FUNC
e054468f 7154 || h->type == STT_GNU_IFUNC
f5385ebf 7155 || h->needs_plt)
e86ce104 7156 {
529fe20e
AM
7157 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7158 || SYMBOL_CALLS_LOCAL (info, h)
7159 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7160 /* Discard dyn_relocs when non-pic if we've decided that a
7161 function symbol is local and not an ifunc. We keep dynamic
7162 relocs for ifuncs when local rather than always emitting a
7163 plt call stub for them and defining the symbol on the call
7164 stub. We can't do that for ELFv1 anyway (a function symbol
7165 is defined on a descriptor, not code) and it can be faster at
7166 run-time due to not needing to bounce through a stub. The
7167 dyn_relocs for ifuncs will be applied even in a static
7168 executable. */
7169 if (!bfd_link_pic (info)
7170 && h->type != STT_GNU_IFUNC
7171 && local)
7172 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7173
e86ce104
AM
7174 /* Clear procedure linkage table information for any symbol that
7175 won't need a .plt entry. */
411e1bfb
AM
7176 struct plt_entry *ent;
7177 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7178 if (ent->plt.refcount > 0)
7179 break;
8387904d 7180 if (ent == NULL
529fe20e 7181 || (h->type != STT_GNU_IFUNC && local))
40b8271b 7182 {
411e1bfb 7183 h->plt.plist = NULL;
f5385ebf 7184 h->needs_plt = 0;
d1eca1e4 7185 h->pointer_equality_needed = 0;
40b8271b 7186 }
8a2058b5 7187 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7188 {
d1eca1e4
AM
7189 /* Taking a function's address in a read/write section
7190 doesn't require us to define the function symbol in the
7191 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7192 be used instead. The reason we prefer a few more dynamic
7193 relocs is that calling via a global entry stub costs a
7194 few more instructions, and pointer_equality_needed causes
7195 extra work in ld.so when resolving these symbols. */
529fe20e 7196 if (global_entry_stub (h))
d1eca1e4 7197 {
ab2477e1 7198 if (!readonly_dynrelocs (h))
529fe20e
AM
7199 {
7200 h->pointer_equality_needed = 0;
7201 /* If we haven't seen a branch reloc then we don't need
7202 a plt entry. */
7203 if (!h->needs_plt)
7204 h->plt.plist = NULL;
7205 }
7206 else if (!bfd_link_pic (info))
7207 /* We are going to be defining the function symbol on the
7208 plt stub, so no dyn_relocs needed when non-pic. */
7209 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
7210 }
7211
3988aed5
AM
7212 /* ELFv2 function symbols can't have copy relocs. */
7213 return TRUE;
7214 }
7215 else if (!h->needs_plt
ab2477e1 7216 && !readonly_dynrelocs (h))
3988aed5
AM
7217 {
7218 /* If we haven't seen a branch reloc then we don't need a
7219 plt entry. */
7220 h->plt.plist = NULL;
7221 h->pointer_equality_needed = 0;
a345bc8d
AM
7222 return TRUE;
7223 }
5bd4f169 7224 }
bbd7ec4a 7225 else
411e1bfb 7226 h->plt.plist = NULL;
5bd4f169
AM
7227
7228 /* If this is a weak symbol, and there is a real definition, the
7229 processor independent code will have arranged for us to see the
7230 real definition first, and we can just use the same value. */
60d67dc8 7231 if (h->is_weakalias)
5bd4f169 7232 {
60d67dc8
AM
7233 struct elf_link_hash_entry *def = weakdef (h);
7234 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7235 h->root.u.def.section = def->root.u.def.section;
7236 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
7237 if (def->root.u.def.section == htab->elf.sdynbss
7238 || def->root.u.def.section == htab->elf.sdynrelro)
7239 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 7240 return TRUE;
5bd4f169
AM
7241 }
7242
5bd4f169
AM
7243 /* If we are creating a shared library, we must presume that the
7244 only references to the symbol are via the global offset table.
7245 For such cases we need not do anything here; the relocations will
7246 be handled correctly by relocate_section. */
0e1862bb 7247 if (bfd_link_pic (info))
b34976b6 7248 return TRUE;
5bd4f169 7249
65f38f15
AM
7250 /* If there are no references to this symbol that do not use the
7251 GOT, we don't need to generate a copy reloc. */
f5385ebf 7252 if (!h->non_got_ref)
b34976b6 7253 return TRUE;
65f38f15 7254
b186458a 7255 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7256 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7257
d93d1c80
AM
7258 /* If -z nocopyreloc was given, don't generate them either. */
7259 || info->nocopyreloc
a127494f 7260
dce2246a 7261 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 7262 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7263 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7264
d93d1c80
AM
7265 /* Protected variables do not work with .dynbss. The copy in
7266 .dynbss won't be used by the shared library with the protected
7267 definition for the variable. Text relocations are preferable
7268 to an incorrect program. */
7269 || h->protected_def)
529fe20e 7270 return TRUE;
a127494f 7271
5d35169e 7272 if (h->plt.plist != NULL)
97b639ba
AM
7273 {
7274 /* We should never get here, but unfortunately there are versions
7275 of gcc out there that improperly (for this ABI) put initialized
7276 function pointers, vtable refs and suchlike in read-only
7277 sections. Allow them to proceed, but warn that this might
7278 break at runtime. */
25f53a85 7279 info->callbacks->einfo
c1c8c1ef 7280 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 7281 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7282 h->root.root.string);
7283 }
5d35169e
AM
7284
7285 /* This is a reference to a symbol defined by a dynamic object which
7286 is not a function. */
7287
5bd4f169
AM
7288 /* We must allocate the symbol in our .dynbss section, which will
7289 become part of the .bss section of the executable. There will be
7290 an entry for this symbol in the .dynsym section. The dynamic
7291 object will contain position independent code, so all references
7292 from the dynamic object to this symbol will go through the global
7293 offset table. The dynamic linker will use the .dynsym entry to
7294 determine the address it must put in the global offset table, so
7295 both the dynamic object and the regular object will refer to the
7296 same memory location for the variable. */
5474d94f
AM
7297 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7298 {
7299 s = htab->elf.sdynrelro;
7300 srel = htab->elf.sreldynrelro;
7301 }
7302 else
7303 {
7304 s = htab->elf.sdynbss;
7305 srel = htab->elf.srelbss;
7306 }
1d7e9d18 7307 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7308 {
4a7e5234
AM
7309 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7310 linker to copy the initial value out of the dynamic object
7311 and into the runtime process image. */
5474d94f 7312 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 7313 h->needs_copy = 1;
5bd4f169
AM
7314 }
7315
529fe20e
AM
7316 /* We no longer want dyn_relocs. */
7317 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 7318 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7319}
7320
e86ce104
AM
7321/* If given a function descriptor symbol, hide both the function code
7322 sym and the descriptor. */
7323static void
4ce794b7
AM
7324ppc64_elf_hide_symbol (struct bfd_link_info *info,
7325 struct elf_link_hash_entry *h,
7326 bfd_boolean force_local)
e86ce104 7327{
34814b9f 7328 struct ppc_link_hash_entry *eh;
e86ce104
AM
7329 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7330
34814b9f
AM
7331 eh = (struct ppc_link_hash_entry *) h;
7332 if (eh->is_func_descriptor)
e86ce104 7333 {
34814b9f 7334 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7335
721956f4 7336 if (fh == NULL)
d1329ca3
AM
7337 {
7338 const char *p, *q;
b8ac2841 7339 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
7340 char save;
7341
7342 /* We aren't supposed to use alloca in BFD because on
7343 systems which do not have alloca the version in libiberty
7344 calls xmalloc, which might cause the program to crash
7345 when it runs out of memory. This function doesn't have a
7346 return status, so there's no way to gracefully return an
7347 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7348 accessed; It's either a string in an ELF string table,
7349 or allocated in an objalloc structure. */
d1329ca3 7350
34814b9f 7351 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7352 save = *p;
7353 *(char *) p = '.';
34814b9f 7354 fh = (struct ppc_link_hash_entry *)
b8ac2841 7355 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7356 *(char *) p = save;
7357
7358 /* Unfortunately, if it so happens that the string we were
7359 looking for was allocated immediately before this string,
7360 then we overwrote the string terminator. That's the only
7361 reason the lookup should fail. */
7362 if (fh == NULL)
7363 {
34814b9f
AM
7364 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7365 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7366 --q, --p;
34814b9f
AM
7367 if (q < eh->elf.root.root.string && *p == '.')
7368 fh = (struct ppc_link_hash_entry *)
b8ac2841 7369 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7370 }
7371 if (fh != NULL)
7372 {
34814b9f
AM
7373 eh->oh = fh;
7374 fh->oh = eh;
d1329ca3
AM
7375 }
7376 }
e86ce104 7377 if (fh != NULL)
34814b9f 7378 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7379 }
7380}
7381
411e1bfb 7382static bfd_boolean
8843416a
AM
7383get_sym_h (struct elf_link_hash_entry **hp,
7384 Elf_Internal_Sym **symp,
7385 asection **symsecp,
f961d9dd 7386 unsigned char **tls_maskp,
8843416a
AM
7387 Elf_Internal_Sym **locsymsp,
7388 unsigned long r_symndx,
7389 bfd *ibfd)
411e1bfb 7390{
0ffa91dd 7391 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7392
7393 if (r_symndx >= symtab_hdr->sh_info)
7394 {
7395 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7396 struct elf_link_hash_entry *h;
7397
7398 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7399 h = elf_follow_link (h);
411e1bfb
AM
7400
7401 if (hp != NULL)
7402 *hp = h;
7403
7404 if (symp != NULL)
7405 *symp = NULL;
7406
7407 if (symsecp != NULL)
7408 {
7409 asection *symsec = NULL;
7410 if (h->root.type == bfd_link_hash_defined
7411 || h->root.type == bfd_link_hash_defweak)
7412 symsec = h->root.u.def.section;
7413 *symsecp = symsec;
7414 }
7415
e7b938ca 7416 if (tls_maskp != NULL)
411e1bfb
AM
7417 {
7418 struct ppc_link_hash_entry *eh;
7419
7420 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7421 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7422 }
7423 }
7424 else
7425 {
7426 Elf_Internal_Sym *sym;
7427 Elf_Internal_Sym *locsyms = *locsymsp;
7428
7429 if (locsyms == NULL)
7430 {
7431 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7432 if (locsyms == NULL)
7433 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7434 symtab_hdr->sh_info,
7435 0, NULL, NULL, NULL);
7436 if (locsyms == NULL)
7437 return FALSE;
7438 *locsymsp = locsyms;
7439 }
7440 sym = locsyms + r_symndx;
7441
7442 if (hp != NULL)
7443 *hp = NULL;
7444
7445 if (symp != NULL)
7446 *symp = sym;
7447
7448 if (symsecp != NULL)
cb33740c 7449 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7450
e7b938ca 7451 if (tls_maskp != NULL)
411e1bfb
AM
7452 {
7453 struct got_entry **lgot_ents;
f961d9dd 7454 unsigned char *tls_mask;
411e1bfb 7455
e7b938ca 7456 tls_mask = NULL;
411e1bfb
AM
7457 lgot_ents = elf_local_got_ents (ibfd);
7458 if (lgot_ents != NULL)
7459 {
e054468f
AM
7460 struct plt_entry **local_plt = (struct plt_entry **)
7461 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7462 unsigned char *lgot_masks = (unsigned char *)
e054468f 7463 (local_plt + symtab_hdr->sh_info);
e7b938ca 7464 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7465 }
e7b938ca 7466 *tls_maskp = tls_mask;
411e1bfb
AM
7467 }
7468 }
7469 return TRUE;
7470}
7471
e7b938ca 7472/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7473 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7474 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7475
7476static int
f961d9dd 7477get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7478 unsigned long *toc_symndx,
7479 bfd_vma *toc_addend,
0d4792f7 7480 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7481 const Elf_Internal_Rela *rel,
7482 bfd *ibfd)
411e1bfb
AM
7483{
7484 unsigned long r_symndx;
0d4792f7 7485 int next_r;
411e1bfb
AM
7486 struct elf_link_hash_entry *h;
7487 Elf_Internal_Sym *sym;
7488 asection *sec;
7489 bfd_vma off;
7490
7491 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7492 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7493 return 0;
411e1bfb 7494
e7b938ca 7495 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7496 || sec == NULL
6bee8834 7497 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7498 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7499 return 1;
411e1bfb
AM
7500
7501 /* Look inside a TOC section too. */
7502 if (h != NULL)
7503 {
7504 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7505 off = h->root.u.def.value;
7506 }
7507 else
7508 off = sym->st_value;
7509 off += rel->r_addend;
7510 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7511 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7512 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7513 if (toc_symndx != NULL)
7514 *toc_symndx = r_symndx;
3a71aa26
AM
7515 if (toc_addend != NULL)
7516 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7517 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7518 return 0;
854b41e7 7519 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7520 && (next_r == -1 || next_r == -2))
7521 return 1 - next_r;
951fd09b 7522 return 1;
411e1bfb
AM
7523}
7524
3b421ab3
AM
7525/* Find (or create) an entry in the tocsave hash table. */
7526
7527static struct tocsave_entry *
7528tocsave_find (struct ppc_link_hash_table *htab,
7529 enum insert_option insert,
7530 Elf_Internal_Sym **local_syms,
7531 const Elf_Internal_Rela *irela,
7532 bfd *ibfd)
7533{
7534 unsigned long r_indx;
7535 struct elf_link_hash_entry *h;
7536 Elf_Internal_Sym *sym;
7537 struct tocsave_entry ent, *p;
7538 hashval_t hash;
7539 struct tocsave_entry **slot;
7540
7541 r_indx = ELF64_R_SYM (irela->r_info);
7542 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7543 return NULL;
7544 if (ent.sec == NULL || ent.sec->output_section == NULL)
7545 {
4eca0228 7546 _bfd_error_handler
871b3ab2 7547 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7548 return NULL;
7549 }
7550
7551 if (h != NULL)
7552 ent.offset = h->root.u.def.value;
7553 else
7554 ent.offset = sym->st_value;
7555 ent.offset += irela->r_addend;
7556
7557 hash = tocsave_htab_hash (&ent);
7558 slot = ((struct tocsave_entry **)
7559 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7560 if (slot == NULL)
7561 return NULL;
7562
7563 if (*slot == NULL)
7564 {
7565 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7566 if (p == NULL)
7567 return NULL;
7568 *p = ent;
7569 *slot = p;
7570 }
7571 return *slot;
7572}
7573
754021d0 7574/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7575 code for the old ABI, these will already have been done. */
754021d0
AM
7576
7577static bfd_boolean
7578adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7579{
7580 struct ppc_link_hash_entry *eh;
7581 asection *sym_sec;
74f0fb50 7582 struct _opd_sec_data *opd;
754021d0
AM
7583
7584 if (h->root.type == bfd_link_hash_indirect)
7585 return TRUE;
7586
754021d0
AM
7587 if (h->root.type != bfd_link_hash_defined
7588 && h->root.type != bfd_link_hash_defweak)
7589 return TRUE;
7590
7591 eh = (struct ppc_link_hash_entry *) h;
7592 if (eh->adjust_done)
7593 return TRUE;
7594
7595 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7596 opd = get_opd_info (sym_sec);
7597 if (opd != NULL && opd->adjust != NULL)
754021d0 7598 {
51aecdc5 7599 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7600 if (adjust == -1)
7601 {
7602 /* This entry has been deleted. */
b3fac117 7603 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7604 if (dsec == NULL)
7605 {
7606 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7607 if (discarded_section (dsec))
81688140 7608 {
b3fac117 7609 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7610 break;
7611 }
7612 }
4025353c 7613 eh->elf.root.u.def.value = 0;
81688140 7614 eh->elf.root.u.def.section = dsec;
4025353c
AM
7615 }
7616 else
7617 eh->elf.root.u.def.value += adjust;
754021d0
AM
7618 eh->adjust_done = 1;
7619 }
7620 return TRUE;
7621}
7622
8c1d1bb8 7623/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7624 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7625 have already been determined. */
7626
7627static bfd_boolean
7628dec_dynrel_count (bfd_vma r_info,
7629 asection *sec,
7630 struct bfd_link_info *info,
7631 Elf_Internal_Sym **local_syms,
7632 struct elf_link_hash_entry *h,
19e08130 7633 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7634{
7635 enum elf_ppc64_reloc_type r_type;
19e08130 7636 asection *sym_sec = NULL;
8c1d1bb8
AM
7637
7638 /* Can this reloc be dynamic? This switch, and later tests here
7639 should be kept in sync with the code in check_relocs. */
7640 r_type = ELF64_R_TYPE (r_info);
7641 switch (r_type)
7642 {
7643 default:
7644 return TRUE;
7645
7646 case R_PPC64_TPREL16:
7647 case R_PPC64_TPREL16_LO:
7648 case R_PPC64_TPREL16_HI:
7649 case R_PPC64_TPREL16_HA:
7650 case R_PPC64_TPREL16_DS:
7651 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7652 case R_PPC64_TPREL16_HIGH:
7653 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7654 case R_PPC64_TPREL16_HIGHER:
7655 case R_PPC64_TPREL16_HIGHERA:
7656 case R_PPC64_TPREL16_HIGHEST:
7657 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
7658 case R_PPC64_TPREL64:
7659 case R_PPC64_DTPMOD64:
7660 case R_PPC64_DTPREL64:
7661 case R_PPC64_ADDR64:
7662 case R_PPC64_REL30:
7663 case R_PPC64_REL32:
7664 case R_PPC64_REL64:
7665 case R_PPC64_ADDR14:
7666 case R_PPC64_ADDR14_BRNTAKEN:
7667 case R_PPC64_ADDR14_BRTAKEN:
7668 case R_PPC64_ADDR16:
7669 case R_PPC64_ADDR16_DS:
7670 case R_PPC64_ADDR16_HA:
7671 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7672 case R_PPC64_ADDR16_HIGH:
7673 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7674 case R_PPC64_ADDR16_HIGHER:
7675 case R_PPC64_ADDR16_HIGHERA:
7676 case R_PPC64_ADDR16_HIGHEST:
7677 case R_PPC64_ADDR16_HIGHESTA:
7678 case R_PPC64_ADDR16_LO:
7679 case R_PPC64_ADDR16_LO_DS:
7680 case R_PPC64_ADDR24:
7681 case R_PPC64_ADDR32:
7682 case R_PPC64_UADDR16:
7683 case R_PPC64_UADDR32:
7684 case R_PPC64_UADDR64:
7685 case R_PPC64_TOC:
7686 break;
7687 }
7688
7689 if (local_syms != NULL)
7690 {
7691 unsigned long r_symndx;
8c1d1bb8
AM
7692 bfd *ibfd = sec->owner;
7693
7694 r_symndx = ELF64_R_SYM (r_info);
7695 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7696 return FALSE;
7697 }
7698
0e1862bb 7699 if ((bfd_link_pic (info)
1d483afe 7700 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7701 || (h != NULL
198f1157 7702 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7703 || h->root.type == bfd_link_hash_defweak
7704 || !h->def_regular))))
7705 || (ELIMINATE_COPY_RELOCS
0e1862bb 7706 && !bfd_link_pic (info)
8c1d1bb8
AM
7707 && h != NULL
7708 && (h->root.type == bfd_link_hash_defweak
7709 || !h->def_regular)))
7710 ;
7711 else
7712 return TRUE;
7713
7714 if (h != NULL)
6edfbbad 7715 {
19e08130
AM
7716 struct elf_dyn_relocs *p;
7717 struct elf_dyn_relocs **pp;
7718 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7719
7720 /* elf_gc_sweep may have already removed all dyn relocs associated
7721 with local syms for a given section. Also, symbol flags are
7722 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7723 report a dynreloc miscount. */
7724 if (*pp == NULL && info->gc_sections)
7725 return TRUE;
7726
7727 while ((p = *pp) != NULL)
60124e18 7728 {
19e08130
AM
7729 if (p->sec == sec)
7730 {
7731 if (!must_be_dyn_reloc (info, r_type))
7732 p->pc_count -= 1;
7733 p->count -= 1;
7734 if (p->count == 0)
7735 *pp = p->next;
7736 return TRUE;
7737 }
7738 pp = &p->next;
60124e18 7739 }
6edfbbad 7740 }
19e08130
AM
7741 else
7742 {
7743 struct ppc_dyn_relocs *p;
7744 struct ppc_dyn_relocs **pp;
7745 void *vpp;
7746 bfd_boolean is_ifunc;
8c1d1bb8 7747
19e08130
AM
7748 if (local_syms == NULL)
7749 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7750 if (sym_sec == NULL)
7751 sym_sec = sec;
c57da1a7 7752
19e08130
AM
7753 vpp = &elf_section_data (sym_sec)->local_dynrel;
7754 pp = (struct ppc_dyn_relocs **) vpp;
7755
7756 if (*pp == NULL && info->gc_sections)
7757 return TRUE;
7758
7759 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7760 while ((p = *pp) != NULL)
8c1d1bb8 7761 {
19e08130
AM
7762 if (p->sec == sec && p->ifunc == is_ifunc)
7763 {
7764 p->count -= 1;
7765 if (p->count == 0)
7766 *pp = p->next;
7767 return TRUE;
7768 }
7769 pp = &p->next;
8c1d1bb8 7770 }
8c1d1bb8
AM
7771 }
7772
695344c0 7773 /* xgettext:c-format */
cf97bcb0
AM
7774 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7775 sec->owner, sec);
8c1d1bb8
AM
7776 bfd_set_error (bfd_error_bad_value);
7777 return FALSE;
7778}
7779
754021d0
AM
7780/* Remove unused Official Procedure Descriptor entries. Currently we
7781 only remove those associated with functions in discarded link-once
7782 sections, or weakly defined functions that have been overridden. It
7783 would be possible to remove many more entries for statically linked
7784 applications. */
7785
b34976b6 7786bfd_boolean
e7d1c40c 7787ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7788{
7789 bfd *ibfd;
754021d0 7790 bfd_boolean some_edited = FALSE;
3f764659 7791 asection *need_pad = NULL;
e7d1c40c
AM
7792 struct ppc_link_hash_table *htab;
7793
7794 htab = ppc_hash_table (info);
7795 if (htab == NULL)
7796 return FALSE;
1e2f5b6e 7797
c72f2fb2 7798 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7799 {
7800 asection *sec;
7801 Elf_Internal_Rela *relstart, *rel, *relend;
7802 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7803 Elf_Internal_Sym *local_syms;
74f0fb50 7804 struct _opd_sec_data *opd;
51aecdc5 7805 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7806 bfd_size_type cnt_16b = 0;
1e2f5b6e 7807
854b41e7
AM
7808 if (!is_ppc64_elf (ibfd))
7809 continue;
7810
1e2f5b6e 7811 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7812 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7813 continue;
7814
dbaa2011 7815 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7816 continue;
7817
1e2f5b6e
AM
7818 if (sec->output_section == bfd_abs_section_ptr)
7819 continue;
7820
7821 /* Look through the section relocs. */
7822 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7823 continue;
7824
6cdc0ccc 7825 local_syms = NULL;
0ffa91dd 7826 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7827
7828 /* Read the relocations. */
4ce794b7 7829 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7830 info->keep_memory);
1e2f5b6e 7831 if (relstart == NULL)
b34976b6 7832 return FALSE;
1e2f5b6e
AM
7833
7834 /* First run through the relocs to check they are sane, and to
7835 determine whether we need to edit this opd section. */
b34976b6 7836 need_edit = FALSE;
51aecdc5 7837 broken = FALSE;
3f764659 7838 need_pad = sec;
1e2f5b6e 7839 relend = relstart + sec->reloc_count;
50bc7936 7840 for (rel = relstart; rel < relend; )
1e2f5b6e 7841 {
04c9666a 7842 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7843 unsigned long r_symndx;
7844 asection *sym_sec;
7845 struct elf_link_hash_entry *h;
7846 Elf_Internal_Sym *sym;
51aecdc5 7847 bfd_vma offset;
1e2f5b6e 7848
51aecdc5 7849 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7850 only interested in the reloc pointing to a function entry
7851 point. */
51aecdc5
AM
7852 offset = rel->r_offset;
7853 if (rel + 1 == relend
7854 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7855 {
7856 /* If someone messes with .opd alignment then after a
7857 "ld -r" we might have padding in the middle of .opd.
7858 Also, there's nothing to prevent someone putting
7859 something silly in .opd with the assembler. No .opd
b34976b6 7860 optimization for them! */
3f764659 7861 broken_opd:
4eca0228 7862 _bfd_error_handler
871b3ab2 7863 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7864 broken = TRUE;
1e2f5b6e
AM
7865 break;
7866 }
7867
50bc7936
AM
7868 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7869 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7870 {
4eca0228 7871 _bfd_error_handler
695344c0 7872 /* xgettext:c-format */
871b3ab2 7873 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7874 ibfd, r_type);
51aecdc5 7875 broken = TRUE;
50bc7936
AM
7876 break;
7877 }
7878
1e2f5b6e 7879 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7880 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7881 r_symndx, ibfd))
50bc7936 7882 goto error_ret;
1e2f5b6e
AM
7883
7884 if (sym_sec == NULL || sym_sec->owner == NULL)
7885 {
411e1bfb
AM
7886 const char *sym_name;
7887 if (h != NULL)
7888 sym_name = h->root.root.string;
7889 else
26c61ae5
L
7890 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7891 sym_sec);
411e1bfb 7892
4eca0228 7893 _bfd_error_handler
695344c0 7894 /* xgettext:c-format */
871b3ab2 7895 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7896 ibfd, sym_name);
51aecdc5 7897 broken = TRUE;
1e2f5b6e
AM
7898 break;
7899 }
7900
51020317
AM
7901 /* opd entries are always for functions defined in the
7902 current input bfd. If the symbol isn't defined in the
7903 input bfd, then we won't be using the function in this
7904 bfd; It must be defined in a linkonce section in another
7905 bfd, or is weak. It's also possible that we are
7906 discarding the function due to a linker script /DISCARD/,
7907 which we test for via the output_section. */
7908 if (sym_sec->owner != ibfd
7909 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7910 need_edit = TRUE;
1e2f5b6e 7911
50bc7936 7912 rel += 2;
51aecdc5
AM
7913 if (rel + 1 == relend
7914 || (rel + 2 < relend
7915 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7916 ++rel;
7917
7918 if (rel == relend)
3f764659
JJ
7919 {
7920 if (sec->size == offset + 24)
7921 {
7922 need_pad = NULL;
7923 break;
7924 }
51aecdc5 7925 if (sec->size == offset + 16)
3f764659
JJ
7926 {
7927 cnt_16b++;
7928 break;
7929 }
7930 goto broken_opd;
7931 }
3f764659
JJ
7932 else if (rel + 1 < relend
7933 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7934 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7935 {
51aecdc5
AM
7936 if (rel[0].r_offset == offset + 16)
7937 cnt_16b++;
7938 else if (rel[0].r_offset != offset + 24)
7939 goto broken_opd;
3f764659
JJ
7940 }
7941 else
7942 goto broken_opd;
1e2f5b6e
AM
7943 }
7944
e7d1c40c 7945 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7946
51aecdc5 7947 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7948 {
7949 Elf_Internal_Rela *write_rel;
d4730f92 7950 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7951 bfd_byte *rptr, *wptr;
983bddc8 7952 bfd_byte *new_contents;
74f0fb50
AM
7953 bfd_size_type amt;
7954
983bddc8 7955 new_contents = NULL;
51aecdc5 7956 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7957 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7958 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7959 if (opd->adjust == NULL)
7960 return FALSE;
1e2f5b6e
AM
7961
7962 /* This seems a waste of time as input .opd sections are all
7963 zeros as generated by gcc, but I suppose there's no reason
7964 this will always be so. We might start putting something in
7965 the third word of .opd entries. */
7966 if ((sec->flags & SEC_IN_MEMORY) == 0)
7967 {
eea6121a
AM
7968 bfd_byte *loc;
7969 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7970 {
eea6121a
AM
7971 if (loc != NULL)
7972 free (loc);
50bc7936 7973 error_ret:
6cdc0ccc
AM
7974 if (local_syms != NULL
7975 && symtab_hdr->contents != (unsigned char *) local_syms)
7976 free (local_syms);
6cdc0ccc
AM
7977 if (elf_section_data (sec)->relocs != relstart)
7978 free (relstart);
b34976b6 7979 return FALSE;
6cdc0ccc 7980 }
1e2f5b6e
AM
7981 sec->contents = loc;
7982 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7983 }
7984
7985 elf_section_data (sec)->relocs = relstart;
7986
3f764659 7987 new_contents = sec->contents;
3f764659
JJ
7988 if (add_aux_fields)
7989 {
7990 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7991 if (new_contents == NULL)
7992 return FALSE;
51aecdc5 7993 need_pad = NULL;
3f764659 7994 }
b4f4e59f
AM
7995 wptr = new_contents;
7996 rptr = sec->contents;
1e2f5b6e 7997 write_rel = relstart;
51aecdc5 7998 for (rel = relstart; rel < relend; )
1e2f5b6e 7999 {
50bc7936
AM
8000 unsigned long r_symndx;
8001 asection *sym_sec;
8002 struct elf_link_hash_entry *h;
51aecdc5 8003 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 8004 Elf_Internal_Sym *sym;
51aecdc5
AM
8005 long opd_ent_size;
8006 Elf_Internal_Rela *next_rel;
8007 bfd_boolean skip;
50bc7936
AM
8008
8009 r_symndx = ELF64_R_SYM (rel->r_info);
8010 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 8011 r_symndx, ibfd))
50bc7936
AM
8012 goto error_ret;
8013
51aecdc5
AM
8014 next_rel = rel + 2;
8015 if (next_rel + 1 == relend
8016 || (next_rel + 2 < relend
8017 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8018 ++next_rel;
8019
8020 /* See if the .opd entry is full 24 byte or
8021 16 byte (with fd_aux entry overlapped with next
8022 fd_func). */
8023 opd_ent_size = 24;
8024 if (next_rel == relend)
1e2f5b6e 8025 {
51aecdc5 8026 if (sec->size == rel->r_offset + 16)
3f764659 8027 opd_ent_size = 16;
51aecdc5
AM
8028 }
8029 else if (next_rel->r_offset == rel->r_offset + 16)
8030 opd_ent_size = 16;
3f764659 8031
51aecdc5
AM
8032 if (h != NULL
8033 && h->root.root.string[0] == '.')
8034 {
8c5b4e52
AM
8035 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8036 if (fdh != NULL)
8037 {
8038 fdh = ppc_follow_link (fdh);
8039 if (fdh->elf.root.type != bfd_link_hash_defined
8040 && fdh->elf.root.type != bfd_link_hash_defweak)
8041 fdh = NULL;
8042 }
51aecdc5 8043 }
1e2f5b6e 8044
51aecdc5
AM
8045 skip = (sym_sec->owner != ibfd
8046 || sym_sec->output_section == bfd_abs_section_ptr);
8047 if (skip)
8048 {
8049 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8050 {
51aecdc5
AM
8051 /* Arrange for the function descriptor sym
8052 to be dropped. */
8053 fdh->elf.root.u.def.value = 0;
8054 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8055 }
51aecdc5 8056 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8057
0e1862bb 8058 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8059 rel = next_rel;
8060 else
8061 while (1)
8062 {
8063 if (!dec_dynrel_count (rel->r_info, sec, info,
8064 NULL, h, sym))
8065 goto error_ret;
754021d0 8066
51aecdc5
AM
8067 if (++rel == next_rel)
8068 break;
1e2f5b6e 8069
51aecdc5
AM
8070 r_symndx = ELF64_R_SYM (rel->r_info);
8071 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8072 r_symndx, ibfd))
8073 goto error_ret;
8074 }
50bc7936
AM
8075 }
8076 else
1e2f5b6e 8077 {
51aecdc5
AM
8078 /* We'll be keeping this opd entry. */
8079 long adjust;
8080
8081 if (fdh != NULL)
8082 {
8083 /* Redefine the function descriptor symbol to
8084 this location in the opd section. It is
8085 necessary to update the value here rather
8086 than using an array of adjustments as we do
8087 for local symbols, because various places
8088 in the generic ELF code use the value
8089 stored in u.def.value. */
8090 fdh->elf.root.u.def.value = wptr - new_contents;
8091 fdh->adjust_done = 1;
8092 }
8093
8094 /* Local syms are a bit tricky. We could
8095 tweak them as they can be cached, but
8096 we'd need to look through the local syms
8097 for the function descriptor sym which we
8098 don't have at the moment. So keep an
8099 array of adjustments. */
8100 adjust = (wptr - new_contents) - (rptr - sec->contents);
8101 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8102
8103 if (wptr != rptr)
8104 memcpy (wptr, rptr, opd_ent_size);
8105 wptr += opd_ent_size;
8106 if (add_aux_fields && opd_ent_size == 16)
8107 {
8108 memset (wptr, '\0', 8);
8109 wptr += 8;
8110 }
8111
50bc7936 8112 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8113 new opd entries. */
8114 for ( ; rel != next_rel; ++rel)
8115 {
8116 rel->r_offset += adjust;
8117 if (write_rel != rel)
8118 memcpy (write_rel, rel, sizeof (*rel));
8119 ++write_rel;
8120 }
1e2f5b6e 8121 }
51aecdc5
AM
8122
8123 rptr += opd_ent_size;
1e2f5b6e
AM
8124 }
8125
3f764659 8126 sec->size = wptr - new_contents;
1e2f5b6e 8127 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8128 if (add_aux_fields)
8129 {
8130 free (sec->contents);
8131 sec->contents = new_contents;
8132 }
8133
05bf9422 8134 /* Fudge the header size too, as this is used later in
cdcf6e38 8135 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8136 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8137 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8138 some_edited = TRUE;
1e2f5b6e 8139 }
6cdc0ccc 8140 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8141 free (relstart);
6cdc0ccc 8142
411e1bfb
AM
8143 if (local_syms != NULL
8144 && symtab_hdr->contents != (unsigned char *) local_syms)
8145 {
8146 if (!info->keep_memory)
8147 free (local_syms);
8148 else
8149 symtab_hdr->contents = (unsigned char *) local_syms;
8150 }
8151 }
8152
754021d0
AM
8153 if (some_edited)
8154 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8155
3f764659
JJ
8156 /* If we are doing a final link and the last .opd entry is just 16 byte
8157 long, add a 8 byte padding after it. */
0e1862bb 8158 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8159 {
8160 bfd_byte *p;
8161
8162 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8163 {
8164 BFD_ASSERT (need_pad->size > 0);
8165
8166 p = bfd_malloc (need_pad->size + 8);
8167 if (p == NULL)
8168 return FALSE;
699733f6 8169
3f764659
JJ
8170 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8171 p, 0, need_pad->size))
8172 return FALSE;
8173
8174 need_pad->contents = p;
8175 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8176 }
8177 else
8178 {
8179 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8180 if (p == NULL)
8181 return FALSE;
8182
8183 need_pad->contents = p;
8184 }
8185
8186 memset (need_pad->contents + need_pad->size, 0, 8);
8187 need_pad->size += 8;
8188 }
8189
411e1bfb
AM
8190 return TRUE;
8191}
8192
e1918d23 8193/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8194
e1918d23 8195asection *
e7d1c40c 8196ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8197{
411e1bfb
AM
8198 struct ppc_link_hash_table *htab;
8199
411e1bfb 8200 htab = ppc_hash_table (info);
4dfe6ac6
NC
8201 if (htab == NULL)
8202 return NULL;
8203
ee67d69a
AM
8204 if (abiversion (info->output_bfd) == 1)
8205 htab->opd_abi = 1;
8206
e7d1c40c 8207 if (htab->params->no_multi_toc)
33c0ec9d
AM
8208 htab->do_multi_toc = 0;
8209 else if (!htab->do_multi_toc)
e7d1c40c 8210 htab->params->no_multi_toc = 1;
33c0ec9d 8211
8b5f1ed8
AM
8212 /* Default to --no-plt-localentry, as this option can cause problems
8213 with symbol interposition. For example, glibc libpthread.so and
8214 libc.so duplicate many pthread symbols, with a fallback
8215 implementation in libc.so. In some cases the fallback does more
8216 work than the pthread implementation. __pthread_condattr_destroy
8217 is one such symbol: the libpthread.so implementation is
8218 localentry:0 while the libc.so implementation is localentry:8.
8219 An app that "cleverly" uses dlopen to only load necessary
8220 libraries at runtime may omit loading libpthread.so when not
8221 running multi-threaded, which then results in the libc.so
8222 fallback symbols being used and ld.so complaining. Now there
8223 are workarounds in ld (see non_zero_localentry) to detect the
8224 pthread situation, but that may not be the only case where
8225 --plt-localentry can cause trouble. */
f378ab09 8226 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 8227 htab->params->plt_localentry0 = 0;
d44c746a
AM
8228 if (htab->params->plt_localentry0
8229 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8230 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
8231 _bfd_error_handler
8232 (_("warning: --plt-localentry is especially dangerous without "
8233 "ld.so support to detect ABI violations"));
f378ab09 8234
3a71aa26
AM
8235 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8236 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8237 FALSE, FALSE, TRUE));
a7f2871e
AM
8238 /* Move dynamic linking info to the function descriptor sym. */
8239 if (htab->tls_get_addr != NULL)
8240 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8241 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8242 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8243 FALSE, FALSE, TRUE));
7c9cf415 8244 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8245 {
8246 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8247
8248 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8249 FALSE, FALSE, TRUE);
8250 if (opt != NULL)
8251 func_desc_adjust (opt, info);
8252 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8253 FALSE, FALSE, TRUE);
8254 if (opt_fd != NULL
8255 && (opt_fd->root.type == bfd_link_hash_defined
8256 || opt_fd->root.type == bfd_link_hash_defweak))
8257 {
8258 /* If glibc supports an optimized __tls_get_addr call stub,
8259 signalled by the presence of __tls_get_addr_opt, and we'll
8260 be calling __tls_get_addr via a plt call stub, then
8261 make __tls_get_addr point to __tls_get_addr_opt. */
8262 tga_fd = &htab->tls_get_addr_fd->elf;
8263 if (htab->elf.dynamic_sections_created
8264 && tga_fd != NULL
8265 && (tga_fd->type == STT_FUNC
8266 || tga_fd->needs_plt)
8267 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 8268 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
8269 {
8270 struct plt_entry *ent;
8271
8272 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8273 if (ent->plt.refcount > 0)
8274 break;
8275 if (ent != NULL)
8276 {
8277 tga_fd->root.type = bfd_link_hash_indirect;
8278 tga_fd->root.u.i.link = &opt_fd->root;
8279 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 8280 opt_fd->mark = 1;
a7f2871e
AM
8281 if (opt_fd->dynindx != -1)
8282 {
8283 /* Use __tls_get_addr_opt in dynamic relocations. */
8284 opt_fd->dynindx = -1;
8285 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8286 opt_fd->dynstr_index);
8287 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8288 return NULL;
a7f2871e
AM
8289 }
8290 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8291 tga = &htab->tls_get_addr->elf;
8292 if (opt != NULL && tga != NULL)
8293 {
8294 tga->root.type = bfd_link_hash_indirect;
8295 tga->root.u.i.link = &opt->root;
8296 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 8297 opt->mark = 1;
a7f2871e
AM
8298 _bfd_elf_link_hash_hide_symbol (info, opt,
8299 tga->forced_local);
8300 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8301 }
8302 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8303 htab->tls_get_addr_fd->is_func_descriptor = 1;
8304 if (htab->tls_get_addr != NULL)
8305 {
8306 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8307 htab->tls_get_addr->is_func = 1;
8308 }
8309 }
8310 }
8311 }
7c9cf415
AM
8312 else if (htab->params->tls_get_addr_opt < 0)
8313 htab->params->tls_get_addr_opt = 0;
a7f2871e 8314 }
33c0ec9d 8315 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8316}
8387904d 8317
3a71aa26
AM
8318/* Return TRUE iff REL is a branch reloc with a global symbol matching
8319 HASH1 or HASH2. */
8387904d 8320
3a71aa26
AM
8321static bfd_boolean
8322branch_reloc_hash_match (const bfd *ibfd,
8323 const Elf_Internal_Rela *rel,
8324 const struct ppc_link_hash_entry *hash1,
8325 const struct ppc_link_hash_entry *hash2)
8326{
8327 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8328 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8329 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8330
e054468f 8331 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8332 {
3a71aa26
AM
8333 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8334 struct elf_link_hash_entry *h;
8387904d 8335
3a71aa26 8336 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8337 h = elf_follow_link (h);
3a71aa26
AM
8338 if (h == &hash1->elf || h == &hash2->elf)
8339 return TRUE;
a48ebf4d 8340 }
3a71aa26 8341 return FALSE;
951fd09b 8342}
411e1bfb 8343
951fd09b
AM
8344/* Run through all the TLS relocs looking for optimization
8345 opportunities. The linker has been hacked (see ppc64elf.em) to do
8346 a preliminary section layout so that we know the TLS segment
8347 offsets. We can't optimize earlier because some optimizations need
8348 to know the tp offset, and we need to optimize before allocating
8349 dynamic relocations. */
8350
8351bfd_boolean
33c0ec9d 8352ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8353{
8354 bfd *ibfd;
8355 asection *sec;
8356 struct ppc_link_hash_table *htab;
663a1470 8357 unsigned char *toc_ref;
102890f0 8358 int pass;
951fd09b 8359
3cbc1e5e 8360 if (!bfd_link_executable (info))
411e1bfb
AM
8361 return TRUE;
8362
951fd09b 8363 htab = ppc_hash_table (info);
4dfe6ac6
NC
8364 if (htab == NULL)
8365 return FALSE;
8366
663a1470
AM
8367 /* Make two passes over the relocs. On the first pass, mark toc
8368 entries involved with tls relocs, and check that tls relocs
8369 involved in setting up a tls_get_addr call are indeed followed by
8370 such a call. If they are not, we can't do any tls optimization.
8371 On the second pass twiddle tls_mask flags to notify
8372 relocate_section that optimization can be done, and adjust got
8373 and plt refcounts. */
8374 toc_ref = NULL;
8375 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8376 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8377 {
8378 Elf_Internal_Sym *locsyms = NULL;
8379 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8380
102890f0
AM
8381 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8382 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8383 {
8384 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8385 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8386
102890f0
AM
8387 /* Read the relocations. */
8388 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8389 info->keep_memory);
8390 if (relstart == NULL)
2915c55b
JK
8391 {
8392 free (toc_ref);
8393 return FALSE;
8394 }
411e1bfb 8395
102890f0
AM
8396 relend = relstart + sec->reloc_count;
8397 for (rel = relstart; rel < relend; rel++)
8398 {
8399 enum elf_ppc64_reloc_type r_type;
8400 unsigned long r_symndx;
8401 struct elf_link_hash_entry *h;
8402 Elf_Internal_Sym *sym;
8403 asection *sym_sec;
f961d9dd
AM
8404 unsigned char *tls_mask;
8405 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8406 bfd_vma value;
8407 bfd_boolean ok_tprel, is_local;
8408 long toc_ref_index = 0;
8409 int expecting_tls_get_addr = 0;
663a1470 8410 bfd_boolean ret = FALSE;
411e1bfb 8411
102890f0
AM
8412 r_symndx = ELF64_R_SYM (rel->r_info);
8413 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8414 r_symndx, ibfd))
8415 {
8416 err_free_rel:
8417 if (elf_section_data (sec)->relocs != relstart)
8418 free (relstart);
8419 if (toc_ref != NULL)
8420 free (toc_ref);
8421 if (locsyms != NULL
0ffa91dd 8422 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8423 != (unsigned char *) locsyms))
8424 free (locsyms);
663a1470 8425 return ret;
102890f0 8426 }
411e1bfb 8427
102890f0
AM
8428 if (h != NULL)
8429 {
766bc656
AM
8430 if (h->root.type == bfd_link_hash_defined
8431 || h->root.type == bfd_link_hash_defweak)
8432 value = h->root.u.def.value;
8433 else if (h->root.type == bfd_link_hash_undefweak)
8434 value = 0;
8435 else
663a1470
AM
8436 {
8437 found_tls_get_addr_arg = 0;
8438 continue;
8439 }
102890f0
AM
8440 }
8441 else
8442 /* Symbols referenced by TLS relocs must be of type
8443 STT_TLS. So no need for .opd local sym adjust. */
8444 value = sym->st_value;
8445
8446 ok_tprel = FALSE;
8447 is_local = FALSE;
8448 if (h == NULL
8449 || !h->def_dynamic)
8450 {
8451 is_local = TRUE;
766bc656
AM
8452 if (h != NULL
8453 && h->root.type == bfd_link_hash_undefweak)
8454 ok_tprel = TRUE;
c27b8c2a
AM
8455 else if (sym_sec != NULL
8456 && sym_sec->output_section != NULL)
766bc656
AM
8457 {
8458 value += sym_sec->output_offset;
8459 value += sym_sec->output_section->vma;
8460 value -= htab->elf.tls_sec->vma;
8461 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8462 < (bfd_vma) 1 << 32);
8463 }
102890f0 8464 }
951fd09b 8465
102890f0 8466 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8467 /* If this section has old-style __tls_get_addr calls
8468 without marker relocs, then check that each
8469 __tls_get_addr call reloc is preceded by a reloc
8470 that conceivably belongs to the __tls_get_addr arg
8471 setup insn. If we don't find matching arg setup
8472 relocs, don't do any tls optimization. */
8473 if (pass == 0
8474 && sec->has_tls_get_addr_call
8475 && h != NULL
8476 && (h == &htab->tls_get_addr->elf
8477 || h == &htab->tls_get_addr_fd->elf)
8478 && !found_tls_get_addr_arg
8479 && is_branch_reloc (r_type))
8480 {
25f53a85 8481 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8482 "TLS optimization disabled\n"),
8483 ibfd, sec, rel->r_offset);
8484 ret = TRUE;
8485 goto err_free_rel;
8486 }
8487
8488 found_tls_get_addr_arg = 0;
102890f0
AM
8489 switch (r_type)
8490 {
8491 case R_PPC64_GOT_TLSLD16:
8492 case R_PPC64_GOT_TLSLD16_LO:
8493 expecting_tls_get_addr = 1;
663a1470 8494 found_tls_get_addr_arg = 1;
1a0670f3 8495 /* Fall through. */
102890f0
AM
8496
8497 case R_PPC64_GOT_TLSLD16_HI:
8498 case R_PPC64_GOT_TLSLD16_HA:
8499 /* These relocs should never be against a symbol
8500 defined in a shared lib. Leave them alone if
8501 that turns out to be the case. */
8502 if (!is_local)
8503 continue;
411e1bfb 8504
102890f0 8505 /* LD -> LE */
411e1bfb 8506 tls_set = 0;
102890f0
AM
8507 tls_clear = TLS_LD;
8508 tls_type = TLS_TLS | TLS_LD;
8509 break;
411e1bfb 8510
102890f0
AM
8511 case R_PPC64_GOT_TLSGD16:
8512 case R_PPC64_GOT_TLSGD16_LO:
8513 expecting_tls_get_addr = 1;
663a1470 8514 found_tls_get_addr_arg = 1;
1a0670f3 8515 /* Fall through. */
102890f0
AM
8516
8517 case R_PPC64_GOT_TLSGD16_HI:
8518 case R_PPC64_GOT_TLSGD16_HA:
8519 if (ok_tprel)
8520 /* GD -> LE */
411e1bfb 8521 tls_set = 0;
102890f0
AM
8522 else
8523 /* GD -> IE */
8524 tls_set = TLS_TLS | TLS_TPRELGD;
8525 tls_clear = TLS_GD;
8526 tls_type = TLS_TLS | TLS_GD;
8527 break;
8528
8529 case R_PPC64_GOT_TPREL16_DS:
8530 case R_PPC64_GOT_TPREL16_LO_DS:
8531 case R_PPC64_GOT_TPREL16_HI:
8532 case R_PPC64_GOT_TPREL16_HA:
8533 if (ok_tprel)
8534 {
8535 /* IE -> LE */
8536 tls_set = 0;
8537 tls_clear = TLS_TPREL;
8538 tls_type = TLS_TLS | TLS_TPREL;
8539 break;
8540 }
411e1bfb
AM
8541 continue;
8542
727fc41e
AM
8543 case R_PPC64_TLSGD:
8544 case R_PPC64_TLSLD:
663a1470 8545 found_tls_get_addr_arg = 1;
1a0670f3 8546 /* Fall through. */
663a1470
AM
8547
8548 case R_PPC64_TLS:
8549 case R_PPC64_TOC16:
8550 case R_PPC64_TOC16_LO:
102890f0
AM
8551 if (sym_sec == NULL || sym_sec != toc)
8552 continue;
8553
8554 /* Mark this toc entry as referenced by a TLS
8555 code sequence. We can do that now in the
8556 case of R_PPC64_TLS, and after checking for
8557 tls_get_addr for the TOC16 relocs. */
8558 if (toc_ref == NULL)
663a1470
AM
8559 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8560 if (toc_ref == NULL)
8561 goto err_free_rel;
8562
102890f0
AM
8563 if (h != NULL)
8564 value = h->root.u.def.value;
8565 else
8566 value = sym->st_value;
8567 value += rel->r_addend;
73242275
AM
8568 if (value % 8 != 0)
8569 continue;
8570 BFD_ASSERT (value < toc->size
8571 && toc->output_offset % 8 == 0);
663a1470 8572 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8573 if (r_type == R_PPC64_TLS
8574 || r_type == R_PPC64_TLSGD
8575 || r_type == R_PPC64_TLSLD)
102890f0
AM
8576 {
8577 toc_ref[toc_ref_index] = 1;
8578 continue;
8579 }
8580
8581 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8582 continue;
8583
8584 tls_set = 0;
8585 tls_clear = 0;
8586 expecting_tls_get_addr = 2;
8587 break;
8588
8589 case R_PPC64_TPREL64:
8590 if (pass == 0
8591 || sec != toc
8592 || toc_ref == NULL
663a1470 8593 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8594 continue;
8595 if (ok_tprel)
8596 {
8597 /* IE -> LE */
8598 tls_set = TLS_EXPLICIT;
8599 tls_clear = TLS_TPREL;
8600 break;
8601 }
8602 continue;
8603
8604 case R_PPC64_DTPMOD64:
8605 if (pass == 0
8606 || sec != toc
8607 || toc_ref == NULL
663a1470 8608 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8609 continue;
8610 if (rel + 1 < relend
8611 && (rel[1].r_info
8612 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8613 && rel[1].r_offset == rel->r_offset + 8)
8614 {
8615 if (ok_tprel)
8616 /* GD -> LE */
8617 tls_set = TLS_EXPLICIT | TLS_GD;
8618 else
8619 /* GD -> IE */
8620 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8621 tls_clear = TLS_GD;
8622 }
8623 else
8624 {
8625 if (!is_local)
8626 continue;
8627
8628 /* LD -> LE */
8629 tls_set = TLS_EXPLICIT;
8630 tls_clear = TLS_LD;
8631 }
8632 break;
8633
8634 default:
8635 continue;
8636 }
8637
8638 if (pass == 0)
8639 {
727fc41e
AM
8640 if (!expecting_tls_get_addr
8641 || !sec->has_tls_get_addr_call)
102890f0
AM
8642 continue;
8643
3a71aa26
AM
8644 if (rel + 1 < relend
8645 && branch_reloc_hash_match (ibfd, rel + 1,
8646 htab->tls_get_addr,
8647 htab->tls_get_addr_fd))
102890f0 8648 {
3a71aa26 8649 if (expecting_tls_get_addr == 2)
102890f0 8650 {
3a71aa26 8651 /* Check for toc tls entries. */
f961d9dd 8652 unsigned char *toc_tls;
3a71aa26
AM
8653 int retval;
8654
8655 retval = get_tls_mask (&toc_tls, NULL, NULL,
8656 &locsyms,
8657 rel, ibfd);
8658 if (retval == 0)
8659 goto err_free_rel;
663a1470
AM
8660 if (toc_tls != NULL)
8661 {
8662 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8663 found_tls_get_addr_arg = 1;
8664 if (retval > 1)
8665 toc_ref[toc_ref_index] = 1;
8666 }
102890f0 8667 }
3a71aa26 8668 continue;
102890f0
AM
8669 }
8670
8671 if (expecting_tls_get_addr != 1)
8672 continue;
8673
8674 /* Uh oh, we didn't find the expected call. We
8675 could just mark this symbol to exclude it
8676 from tls optimization but it's safer to skip
663a1470 8677 the entire optimization. */
695344c0 8678 /* xgettext:c-format */
25f53a85 8679 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8680 "TLS optimization disabled\n"),
8681 ibfd, sec, rel->r_offset);
8682 ret = TRUE;
8683 goto err_free_rel;
102890f0
AM
8684 }
8685
85f7a9cb 8686 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8687 {
8688 struct plt_entry *ent;
8689 for (ent = htab->tls_get_addr->elf.plt.plist;
8690 ent != NULL;
8691 ent = ent->next)
8692 if (ent->addend == 0)
411e1bfb 8693 {
102890f0 8694 if (ent->plt.refcount > 0)
30038c59 8695 {
102890f0
AM
8696 ent->plt.refcount -= 1;
8697 expecting_tls_get_addr = 0;
30038c59 8698 }
102890f0 8699 break;
411e1bfb 8700 }
102890f0 8701 }
411e1bfb 8702
85f7a9cb 8703 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8704 {
8705 struct plt_entry *ent;
8706 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8707 ent != NULL;
8708 ent = ent->next)
8709 if (ent->addend == 0)
411e1bfb 8710 {
102890f0
AM
8711 if (ent->plt.refcount > 0)
8712 ent->plt.refcount -= 1;
8713 break;
411e1bfb 8714 }
102890f0 8715 }
411e1bfb 8716
102890f0 8717 if (tls_clear == 0)
30038c59
AM
8718 continue;
8719
102890f0
AM
8720 if ((tls_set & TLS_EXPLICIT) == 0)
8721 {
8722 struct got_entry *ent;
411e1bfb 8723
102890f0
AM
8724 /* Adjust got entry for this reloc. */
8725 if (h != NULL)
8726 ent = h->got.glist;
8727 else
8728 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8729
102890f0
AM
8730 for (; ent != NULL; ent = ent->next)
8731 if (ent->addend == rel->r_addend
8732 && ent->owner == ibfd
8733 && ent->tls_type == tls_type)
8734 break;
8735 if (ent == NULL)
8736 abort ();
411e1bfb 8737
102890f0
AM
8738 if (tls_set == 0)
8739 {
8740 /* We managed to get rid of a got entry. */
8741 if (ent->got.refcount > 0)
8742 ent->got.refcount -= 1;
8743 }
8744 }
8745 else
8746 {
8747 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8748 we'll lose one or two dyn relocs. */
8749 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8750 NULL, h, sym))
102890f0 8751 return FALSE;
411e1bfb 8752
102890f0
AM
8753 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8754 {
8755 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8756 NULL, h, sym))
102890f0
AM
8757 return FALSE;
8758 }
8759 }
411e1bfb 8760
102890f0
AM
8761 *tls_mask |= tls_set;
8762 *tls_mask &= ~tls_clear;
8763 }
8c1d1bb8 8764
102890f0
AM
8765 if (elf_section_data (sec)->relocs != relstart)
8766 free (relstart);
8767 }
411e1bfb 8768
663a1470
AM
8769 if (locsyms != NULL
8770 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8771 {
8772 if (!info->keep_memory)
8773 free (locsyms);
8774 else
8775 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8776 }
8777 }
411e1bfb 8778
663a1470
AM
8779 if (toc_ref != NULL)
8780 free (toc_ref);
9a23f96e 8781 htab->do_tls_opt = 1;
b34976b6 8782 return TRUE;
1e2f5b6e 8783}
b34976b6 8784
c5614fa4
AM
8785/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8786 the values of any global symbols in a toc section that has been
8787 edited. Globals in toc sections should be a rarity, so this function
8788 sets a flag if any are found in toc sections other than the one just
de194d85 8789 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8790
8791struct adjust_toc_info
8792{
8793 asection *toc;
8794 unsigned long *skip;
8795 bfd_boolean global_toc_syms;
8796};
8797
ba761f19
AM
8798enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8799
c5614fa4
AM
8800static bfd_boolean
8801adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8802{
8803 struct ppc_link_hash_entry *eh;
8804 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8805 unsigned long i;
c5614fa4 8806
c5614fa4
AM
8807 if (h->root.type != bfd_link_hash_defined
8808 && h->root.type != bfd_link_hash_defweak)
8809 return TRUE;
8810
8811 eh = (struct ppc_link_hash_entry *) h;
8812 if (eh->adjust_done)
8813 return TRUE;
8814
8815 if (eh->elf.root.u.def.section == toc_inf->toc)
8816 {
854b41e7
AM
8817 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8818 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8819 else
854b41e7
AM
8820 i = eh->elf.root.u.def.value >> 3;
8821
ba761f19 8822 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8823 {
4eca0228 8824 _bfd_error_handler
854b41e7
AM
8825 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8826 do
8827 ++i;
ba761f19 8828 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8829 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8830 }
854b41e7
AM
8831
8832 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8833 eh->adjust_done = 1;
8834 }
8835 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8836 toc_inf->global_toc_syms = TRUE;
8837
8838 return TRUE;
8839}
8840
39eeab25
AM
8841/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8842 on a _LO variety toc/got reloc. */
560c8763
AM
8843
8844static bfd_boolean
39eeab25 8845ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8846{
39eeab25
AM
8847 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8848 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8849 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8850 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8851 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8852 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8853 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8854 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8855 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8856 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8857 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8858 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8859 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8860 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8861 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8862 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8863 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8864 /* Exclude lfqu by testing reloc. If relocs are ever
8865 defined for the reduced D field in psq_lu then those
8866 will need testing too. */
8867 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8868 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8869 && (insn & 1) == 0)
8870 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8871 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8872 /* Exclude stfqu. psq_stu as above for psq_lu. */
8873 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8874 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8875 && (insn & 1) == 0));
560c8763
AM
8876}
8877
c5614fa4
AM
8878/* Examine all relocs referencing .toc sections in order to remove
8879 unused .toc entries. */
8880
8881bfd_boolean
33c0ec9d 8882ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8883{
8884 bfd *ibfd;
8885 struct adjust_toc_info toc_inf;
67f0cbdb 8886 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8887
67f0cbdb 8888 htab->do_toc_opt = 1;
c5614fa4 8889 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8890 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8891 {
8892 asection *toc, *sec;
8893 Elf_Internal_Shdr *symtab_hdr;
8894 Elf_Internal_Sym *local_syms;
425b145b 8895 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8896 unsigned long *skip, *drop;
8897 unsigned char *used;
8898 unsigned char *keep, last, some_unused;
8899
854b41e7
AM
8900 if (!is_ppc64_elf (ibfd))
8901 continue;
8902
c5614fa4
AM
8903 toc = bfd_get_section_by_name (ibfd, ".toc");
8904 if (toc == NULL
92b7a70f 8905 || toc->size == 0
dbaa2011
AM
8906 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8907 || discarded_section (toc))
c5614fa4
AM
8908 continue;
8909
425b145b 8910 toc_relocs = NULL;
c5614fa4 8911 local_syms = NULL;
0ffa91dd 8912 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8913
8914 /* Look at sections dropped from the final link. */
8915 skip = NULL;
8916 relstart = NULL;
8917 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8918 {
8919 if (sec->reloc_count == 0
dbaa2011 8920 || !discarded_section (sec)
c5614fa4
AM
8921 || get_opd_info (sec)
8922 || (sec->flags & SEC_ALLOC) == 0
8923 || (sec->flags & SEC_DEBUGGING) != 0)
8924 continue;
8925
8926 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8927 if (relstart == NULL)
8928 goto error_ret;
8929
8930 /* Run through the relocs to see which toc entries might be
8931 unused. */
8932 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8933 {
8934 enum elf_ppc64_reloc_type r_type;
8935 unsigned long r_symndx;
8936 asection *sym_sec;
8937 struct elf_link_hash_entry *h;
8938 Elf_Internal_Sym *sym;
8939 bfd_vma val;
8940
8941 r_type = ELF64_R_TYPE (rel->r_info);
8942 switch (r_type)
8943 {
8944 default:
8945 continue;
8946
8947 case R_PPC64_TOC16:
8948 case R_PPC64_TOC16_LO:
8949 case R_PPC64_TOC16_HI:
8950 case R_PPC64_TOC16_HA:
8951 case R_PPC64_TOC16_DS:
8952 case R_PPC64_TOC16_LO_DS:
8953 break;
8954 }
8955
8956 r_symndx = ELF64_R_SYM (rel->r_info);
8957 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8958 r_symndx, ibfd))
8959 goto error_ret;
8960
8961 if (sym_sec != toc)
8962 continue;
8963
8964 if (h != NULL)
8965 val = h->root.u.def.value;
8966 else
8967 val = sym->st_value;
8968 val += rel->r_addend;
8969
8970 if (val >= toc->size)
8971 continue;
8972
8973 /* Anything in the toc ought to be aligned to 8 bytes.
8974 If not, don't mark as unused. */
8975 if (val & 7)
8976 continue;
8977
8978 if (skip == NULL)
8979 {
854b41e7 8980 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8981 if (skip == NULL)
8982 goto error_ret;
8983 }
8984
ba761f19 8985 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8986 }
8987
8988 if (elf_section_data (sec)->relocs != relstart)
8989 free (relstart);
8990 }
8991
ba761f19
AM
8992 /* For largetoc loads of address constants, we can convert
8993 . addis rx,2,addr@got@ha
8994 . ld ry,addr@got@l(rx)
8995 to
8996 . addis rx,2,addr@toc@ha
8997 . addi ry,rx,addr@toc@l
8998 when addr is within 2G of the toc pointer. This then means
8999 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9000
ba761f19
AM
9001 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9002 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9003 && toc->reloc_count != 0)
9004 {
9005 /* Read toc relocs. */
425b145b
AM
9006 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9007 info->keep_memory);
9008 if (toc_relocs == NULL)
ba761f19
AM
9009 goto error_ret;
9010
425b145b 9011 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9012 {
9013 enum elf_ppc64_reloc_type r_type;
9014 unsigned long r_symndx;
9015 asection *sym_sec;
9016 struct elf_link_hash_entry *h;
9017 Elf_Internal_Sym *sym;
9018 bfd_vma val, addr;
9019
9020 r_type = ELF64_R_TYPE (rel->r_info);
9021 if (r_type != R_PPC64_ADDR64)
9022 continue;
9023
9024 r_symndx = ELF64_R_SYM (rel->r_info);
9025 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9026 r_symndx, ibfd))
9027 goto error_ret;
9028
425b145b 9029 if (sym_sec == NULL
c27b8c2a 9030 || sym_sec->output_section == NULL
dbaa2011 9031 || discarded_section (sym_sec))
425b145b
AM
9032 continue;
9033
afe397ea 9034 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9035 continue;
9036
9037 if (h != NULL)
bddc25c9
AM
9038 {
9039 if (h->type == STT_GNU_IFUNC)
9040 continue;
9041 val = h->root.u.def.value;
9042 }
ba761f19 9043 else
bddc25c9
AM
9044 {
9045 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9046 continue;
9047 val = sym->st_value;
9048 }
ba761f19
AM
9049 val += rel->r_addend;
9050 val += sym_sec->output_section->vma + sym_sec->output_offset;
9051
9052 /* We don't yet know the exact toc pointer value, but we
9053 know it will be somewhere in the toc section. Don't
9054 optimize if the difference from any possible toc
9055 pointer is outside [ff..f80008000, 7fff7fff]. */
9056 addr = toc->output_section->vma + TOC_BASE_OFF;
9057 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9058 continue;
9059
9060 addr = toc->output_section->vma + toc->output_section->rawsize;
9061 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9062 continue;
9063
9064 if (skip == NULL)
9065 {
9066 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9067 if (skip == NULL)
9068 goto error_ret;
9069 }
9070
9071 skip[rel->r_offset >> 3]
425b145b 9072 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9073 }
ba761f19
AM
9074 }
9075
c5614fa4
AM
9076 if (skip == NULL)
9077 continue;
9078
9079 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9080 if (used == NULL)
9081 {
9082 error_ret:
9083 if (local_syms != NULL
9084 && symtab_hdr->contents != (unsigned char *) local_syms)
9085 free (local_syms);
9086 if (sec != NULL
9087 && relstart != NULL
9088 && elf_section_data (sec)->relocs != relstart)
9089 free (relstart);
425b145b
AM
9090 if (toc_relocs != NULL
9091 && elf_section_data (toc)->relocs != toc_relocs)
9092 free (toc_relocs);
c5614fa4
AM
9093 if (skip != NULL)
9094 free (skip);
9095 return FALSE;
9096 }
9097
30038c59
AM
9098 /* Now check all kept sections that might reference the toc.
9099 Check the toc itself last. */
9100 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9101 : ibfd->sections);
c5614fa4 9102 sec != NULL;
c5614fa4 9103 sec = (sec == toc ? NULL
c5614fa4 9104 : sec->next == NULL ? toc
30038c59 9105 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9106 : sec->next))
9107 {
9108 int repeat;
9109
9110 if (sec->reloc_count == 0
dbaa2011 9111 || discarded_section (sec)
c5614fa4
AM
9112 || get_opd_info (sec)
9113 || (sec->flags & SEC_ALLOC) == 0
9114 || (sec->flags & SEC_DEBUGGING) != 0)
9115 continue;
9116
854b41e7
AM
9117 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9118 info->keep_memory);
c5614fa4 9119 if (relstart == NULL)
2915c55b
JK
9120 {
9121 free (used);
9122 goto error_ret;
9123 }
c5614fa4
AM
9124
9125 /* Mark toc entries referenced as used. */
c5614fa4 9126 do
d4f1ee75
AM
9127 {
9128 repeat = 0;
9129 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9130 {
9131 enum elf_ppc64_reloc_type r_type;
9132 unsigned long r_symndx;
9133 asection *sym_sec;
9134 struct elf_link_hash_entry *h;
9135 Elf_Internal_Sym *sym;
9136 bfd_vma val;
9137 enum {no_check, check_lo, check_ha} insn_check;
98528052 9138
d4f1ee75
AM
9139 r_type = ELF64_R_TYPE (rel->r_info);
9140 switch (r_type)
9141 {
9142 default:
9143 insn_check = no_check;
9144 break;
98528052 9145
d4f1ee75
AM
9146 case R_PPC64_GOT_TLSLD16_HA:
9147 case R_PPC64_GOT_TLSGD16_HA:
9148 case R_PPC64_GOT_TPREL16_HA:
9149 case R_PPC64_GOT_DTPREL16_HA:
9150 case R_PPC64_GOT16_HA:
9151 case R_PPC64_TOC16_HA:
9152 insn_check = check_ha;
9153 break;
98528052 9154
d4f1ee75
AM
9155 case R_PPC64_GOT_TLSLD16_LO:
9156 case R_PPC64_GOT_TLSGD16_LO:
9157 case R_PPC64_GOT_TPREL16_LO_DS:
9158 case R_PPC64_GOT_DTPREL16_LO_DS:
9159 case R_PPC64_GOT16_LO:
9160 case R_PPC64_GOT16_LO_DS:
9161 case R_PPC64_TOC16_LO:
9162 case R_PPC64_TOC16_LO_DS:
9163 insn_check = check_lo;
9164 break;
9165 }
560c8763 9166
d4f1ee75
AM
9167 if (insn_check != no_check)
9168 {
9169 bfd_vma off = rel->r_offset & ~3;
9170 unsigned char buf[4];
9171 unsigned int insn;
c5614fa4 9172
d4f1ee75
AM
9173 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9174 {
9175 free (used);
9176 goto error_ret;
9177 }
9178 insn = bfd_get_32 (ibfd, buf);
9179 if (insn_check == check_lo
39eeab25 9180 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
9181 : ((insn & ((0x3f << 26) | 0x1f << 16))
9182 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9183 {
9184 char str[12];
9185
9186 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9187 sprintf (str, "%#08x", insn);
9188 info->callbacks->einfo
695344c0 9189 /* xgettext:c-format */
174d0a74 9190 (_("%H: toc optimization is not supported for"
cf97bcb0 9191 " %s instruction\n"),
d4f1ee75
AM
9192 ibfd, sec, rel->r_offset & ~3, str);
9193 }
9194 }
c5614fa4 9195
d4f1ee75
AM
9196 switch (r_type)
9197 {
9198 case R_PPC64_TOC16:
9199 case R_PPC64_TOC16_LO:
9200 case R_PPC64_TOC16_HI:
9201 case R_PPC64_TOC16_HA:
9202 case R_PPC64_TOC16_DS:
9203 case R_PPC64_TOC16_LO_DS:
9204 /* In case we're taking addresses of toc entries. */
9205 case R_PPC64_ADDR64:
9206 break;
c5614fa4 9207
d4f1ee75
AM
9208 default:
9209 continue;
9210 }
c5614fa4 9211
d4f1ee75
AM
9212 r_symndx = ELF64_R_SYM (rel->r_info);
9213 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9214 r_symndx, ibfd))
9215 {
9216 free (used);
9217 goto error_ret;
9218 }
c5614fa4 9219
d4f1ee75
AM
9220 if (sym_sec != toc)
9221 continue;
c5614fa4 9222
d4f1ee75
AM
9223 if (h != NULL)
9224 val = h->root.u.def.value;
9225 else
9226 val = sym->st_value;
9227 val += rel->r_addend;
ba761f19 9228
d4f1ee75
AM
9229 if (val >= toc->size)
9230 continue;
ba761f19 9231
d4f1ee75
AM
9232 if ((skip[val >> 3] & can_optimize) != 0)
9233 {
9234 bfd_vma off;
9235 unsigned char opc;
9236
9237 switch (r_type)
9238 {
9239 case R_PPC64_TOC16_HA:
ba761f19 9240 break;
ba761f19 9241
d4f1ee75
AM
9242 case R_PPC64_TOC16_LO_DS:
9243 off = rel->r_offset;
9244 off += (bfd_big_endian (ibfd) ? -2 : 3);
9245 if (!bfd_get_section_contents (ibfd, sec, &opc,
9246 off, 1))
9247 {
9248 free (used);
9249 goto error_ret;
9250 }
9251 if ((opc & (0x3f << 2)) == (58u << 2))
9252 break;
1a0670f3 9253 /* Fall through. */
ba761f19 9254
d4f1ee75
AM
9255 default:
9256 /* Wrong sort of reloc, or not a ld. We may
9257 as well clear ref_from_discarded too. */
9258 skip[val >> 3] = 0;
9259 }
9260 }
9261
9262 if (sec != toc)
9263 used[val >> 3] = 1;
9264 /* For the toc section, we only mark as used if this
9265 entry itself isn't unused. */
9266 else if ((used[rel->r_offset >> 3]
9267 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9268 && !used[val >> 3])
9269 {
9270 /* Do all the relocs again, to catch reference
9271 chains. */
9272 repeat = 1;
9273 used[val >> 3] = 1;
9274 }
9275 }
9276 }
c5614fa4 9277 while (repeat);
854b41e7
AM
9278
9279 if (elf_section_data (sec)->relocs != relstart)
9280 free (relstart);
c5614fa4
AM
9281 }
9282
9283 /* Merge the used and skip arrays. Assume that TOC
9284 doublewords not appearing as either used or unused belong
de194d85 9285 to an entry more than one doubleword in size. */
c5614fa4
AM
9286 for (drop = skip, keep = used, last = 0, some_unused = 0;
9287 drop < skip + (toc->size + 7) / 8;
9288 ++drop, ++keep)
9289 {
9290 if (*keep)
9291 {
ba761f19
AM
9292 *drop &= ~ref_from_discarded;
9293 if ((*drop & can_optimize) != 0)
9294 some_unused = 1;
c5614fa4
AM
9295 last = 0;
9296 }
b140b010 9297 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9298 {
9299 some_unused = 1;
ba761f19 9300 last = ref_from_discarded;
c5614fa4
AM
9301 }
9302 else
9303 *drop = last;
9304 }
9305
9306 free (used);
9307
9308 if (some_unused)
9309 {
9310 bfd_byte *contents, *src;
9311 unsigned long off;
d62b3684 9312 Elf_Internal_Sym *sym;
ba761f19 9313 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9314
9315 /* Shuffle the toc contents, and at the same time convert the
9316 skip array from booleans into offsets. */
9317 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9318 goto error_ret;
9319
9320 elf_section_data (toc)->this_hdr.contents = contents;
9321
9322 for (src = contents, off = 0, drop = skip;
9323 src < contents + toc->size;
9324 src += 8, ++drop)
9325 {
ba761f19
AM
9326 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9327 off += 8;
c5614fa4
AM
9328 else if (off != 0)
9329 {
9330 *drop = off;
9331 memcpy (src - off, src, 8);
9332 }
9333 }
854b41e7 9334 *drop = off;
c5614fa4
AM
9335 toc->rawsize = toc->size;
9336 toc->size = src - contents - off;
9337
ba761f19
AM
9338 /* Adjust addends for relocs against the toc section sym,
9339 and optimize any accesses we can. */
c5614fa4
AM
9340 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9341 {
9342 if (sec->reloc_count == 0
dbaa2011 9343 || discarded_section (sec))
c5614fa4
AM
9344 continue;
9345
9346 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9347 info->keep_memory);
c5614fa4
AM
9348 if (relstart == NULL)
9349 goto error_ret;
9350
9351 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9352 {
9353 enum elf_ppc64_reloc_type r_type;
9354 unsigned long r_symndx;
9355 asection *sym_sec;
9356 struct elf_link_hash_entry *h;
854b41e7 9357 bfd_vma val;
c5614fa4
AM
9358
9359 r_type = ELF64_R_TYPE (rel->r_info);
9360 switch (r_type)
9361 {
9362 default:
9363 continue;
9364
9365 case R_PPC64_TOC16:
9366 case R_PPC64_TOC16_LO:
9367 case R_PPC64_TOC16_HI:
9368 case R_PPC64_TOC16_HA:
9369 case R_PPC64_TOC16_DS:
9370 case R_PPC64_TOC16_LO_DS:
9371 case R_PPC64_ADDR64:
9372 break;
9373 }
9374
9375 r_symndx = ELF64_R_SYM (rel->r_info);
9376 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9377 r_symndx, ibfd))
9378 goto error_ret;
9379
ba761f19 9380 if (sym_sec != toc)
c5614fa4
AM
9381 continue;
9382
ba761f19
AM
9383 if (h != NULL)
9384 val = h->root.u.def.value;
9385 else
9386 {
9387 val = sym->st_value;
9388 if (val != 0)
9389 local_toc_syms = TRUE;
9390 }
9391
9392 val += rel->r_addend;
854b41e7
AM
9393
9394 if (val > toc->rawsize)
9395 val = toc->rawsize;
ba761f19
AM
9396 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9397 continue;
9398 else if ((skip[val >> 3] & can_optimize) != 0)
9399 {
9400 Elf_Internal_Rela *tocrel
425b145b 9401 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9402 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9403
9404 switch (r_type)
9405 {
9406 case R_PPC64_TOC16_HA:
9407 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9408 break;
9409
9410 case R_PPC64_TOC16_LO_DS:
9411 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9412 break;
9413
9414 default:
28942f62
AM
9415 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9416 ppc_howto_init ();
b140b010 9417 info->callbacks->einfo
695344c0 9418 /* xgettext:c-format */
174d0a74 9419 (_("%H: %s references "
b140b010
AM
9420 "optimized away TOC entry\n"),
9421 ibfd, sec, rel->r_offset,
9422 ppc64_elf_howto_table[r_type]->name);
9423 bfd_set_error (bfd_error_bad_value);
9424 goto error_ret;
ba761f19
AM
9425 }
9426 rel->r_addend = tocrel->r_addend;
9427 elf_section_data (sec)->relocs = relstart;
9428 continue;
9429 }
9430
9431 if (h != NULL || sym->st_value != 0)
9432 continue;
854b41e7
AM
9433
9434 rel->r_addend -= skip[val >> 3];
9435 elf_section_data (sec)->relocs = relstart;
c5614fa4 9436 }
854b41e7
AM
9437
9438 if (elf_section_data (sec)->relocs != relstart)
9439 free (relstart);
c5614fa4
AM
9440 }
9441
9442 /* We shouldn't have local or global symbols defined in the TOC,
9443 but handle them anyway. */
df22d223
AM
9444 if (local_syms != NULL)
9445 for (sym = local_syms;
9446 sym < local_syms + symtab_hdr->sh_info;
9447 ++sym)
9448 if (sym->st_value != 0
9449 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9450 {
9451 unsigned long i;
854b41e7 9452
df22d223
AM
9453 if (sym->st_value > toc->rawsize)
9454 i = toc->rawsize >> 3;
9455 else
9456 i = sym->st_value >> 3;
854b41e7 9457
df22d223
AM
9458 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9459 {
9460 if (local_toc_syms)
4eca0228 9461 _bfd_error_handler
df22d223
AM
9462 (_("%s defined on removed toc entry"),
9463 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9464 do
9465 ++i;
9466 while ((skip[i] & (ref_from_discarded | can_optimize)));
9467 sym->st_value = (bfd_vma) i << 3;
9468 }
d62b3684 9469
df22d223
AM
9470 sym->st_value -= skip[i];
9471 symtab_hdr->contents = (unsigned char *) local_syms;
9472 }
c5614fa4 9473
854b41e7 9474 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9475 if (toc_inf.global_toc_syms)
9476 {
9477 toc_inf.toc = toc;
9478 toc_inf.skip = skip;
9479 toc_inf.global_toc_syms = FALSE;
9480 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9481 &toc_inf);
9482 }
854b41e7
AM
9483
9484 if (toc->reloc_count != 0)
9485 {
d4730f92 9486 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9487 Elf_Internal_Rela *wrel;
9488 bfd_size_type sz;
9489
854b41e7 9490 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9491 if (toc_relocs == NULL)
9492 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9493 info->keep_memory);
9494 if (toc_relocs == NULL)
9495 goto error_ret;
9496
425b145b
AM
9497 wrel = toc_relocs;
9498 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9499 if ((skip[rel->r_offset >> 3]
9500 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9501 {
9502 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9503 wrel->r_info = rel->r_info;
9504 wrel->r_addend = rel->r_addend;
9505 ++wrel;
9506 }
9507 else if (!dec_dynrel_count (rel->r_info, toc, info,
9508 &local_syms, NULL, NULL))
9509 goto error_ret;
9510
425b145b
AM
9511 elf_section_data (toc)->relocs = toc_relocs;
9512 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9513 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9514 sz = rel_hdr->sh_entsize;
9515 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9516 }
c5614fa4 9517 }
28be611c
AM
9518 else if (toc_relocs != NULL
9519 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9520 free (toc_relocs);
c5614fa4
AM
9521
9522 if (local_syms != NULL
9523 && symtab_hdr->contents != (unsigned char *) local_syms)
9524 {
9525 if (!info->keep_memory)
9526 free (local_syms);
9527 else
9528 symtab_hdr->contents = (unsigned char *) local_syms;
9529 }
9530 free (skip);
9531 }
9532
9533 return TRUE;
9534}
9535
1bbe0902
AM
9536/* Return true iff input section I references the TOC using
9537 instructions limited to +/-32k offsets. */
9538
9539bfd_boolean
9540ppc64_elf_has_small_toc_reloc (asection *i)
9541{
9542 return (is_ppc64_elf (i->owner)
9543 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9544}
9545
927be08e
AM
9546/* Allocate space for one GOT entry. */
9547
9548static void
9549allocate_got (struct elf_link_hash_entry *h,
9550 struct bfd_link_info *info,
9551 struct got_entry *gent)
9552{
9553 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9554 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9555 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9556 ? 16 : 8);
9557 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9558 ? 2 : 1) * sizeof (Elf64_External_Rela);
9559 asection *got = ppc64_elf_tdata (gent->owner)->got;
9560
9561 gent->got.offset = got->size;
9562 got->size += entsize;
9563
19e08130 9564 if (h->type == STT_GNU_IFUNC)
927be08e 9565 {
33e44f2e 9566 htab->elf.irelplt->size += rentsize;
19e08130 9567 htab->got_reli_size += rentsize;
927be08e 9568 }
f15d0b54
AM
9569 else if (((bfd_link_pic (info)
9570 && !((gent->tls_type & TLS_TPREL) != 0
9571 && bfd_link_executable (info)
9572 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9573 || (htab->elf.dynamic_sections_created
9574 && h->dynindx != -1
9575 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9576 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9577 {
19e08130 9578 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9579 relgot->size += rentsize;
927be08e
AM
9580 }
9581}
9582
7865406b
AM
9583/* This function merges got entries in the same toc group. */
9584
9585static void
9586merge_got_entries (struct got_entry **pent)
9587{
9588 struct got_entry *ent, *ent2;
9589
9590 for (ent = *pent; ent != NULL; ent = ent->next)
9591 if (!ent->is_indirect)
9592 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9593 if (!ent2->is_indirect
9594 && ent2->addend == ent->addend
9595 && ent2->tls_type == ent->tls_type
9596 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9597 {
9598 ent2->is_indirect = TRUE;
9599 ent2->got.ent = ent;
9600 }
9601}
9602
46434633 9603/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9604
9605static bfd_boolean
46434633
AM
9606ensure_undef_dynamic (struct bfd_link_info *info,
9607 struct elf_link_hash_entry *h)
f0158f44
AM
9608{
9609 struct elf_link_hash_table *htab = elf_hash_table (info);
9610
9611 if (htab->dynamic_sections_created
46434633
AM
9612 && ((info->dynamic_undefined_weak != 0
9613 && h->root.type == bfd_link_hash_undefweak)
9614 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9615 && h->dynindx == -1
9616 && !h->forced_local
9617 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9618 return bfd_elf_link_record_dynamic_symbol (info, h);
9619 return TRUE;
9620}
9621
65f38f15
AM
9622/* Allocate space in .plt, .got and associated reloc sections for
9623 dynamic relocs. */
5bd4f169 9624
b34976b6 9625static bfd_boolean
4ce794b7 9626allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9627{
65f38f15
AM
9628 struct bfd_link_info *info;
9629 struct ppc_link_hash_table *htab;
5bd4f169 9630 asection *s;
65f38f15 9631 struct ppc_link_hash_entry *eh;
0b8bcf0d 9632 struct got_entry **pgent, *gent;
5bd4f169 9633
e92d460e 9634 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9635 return TRUE;
5bd4f169 9636
65f38f15
AM
9637 info = (struct bfd_link_info *) inf;
9638 htab = ppc_hash_table (info);
4dfe6ac6
NC
9639 if (htab == NULL)
9640 return FALSE;
5bd4f169 9641
951fd09b
AM
9642 eh = (struct ppc_link_hash_entry *) h;
9643 /* Run through the TLS GD got entries first if we're changing them
9644 to TPREL. */
e7b938ca 9645 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9646 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9647 if (gent->got.refcount > 0
9648 && (gent->tls_type & TLS_GD) != 0)
9649 {
9650 /* This was a GD entry that has been converted to TPREL. If
9651 there happens to be a TPREL entry we can use that one. */
9652 struct got_entry *ent;
9653 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9654 if (ent->got.refcount > 0
9655 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9656 && ent->addend == gent->addend
9657 && ent->owner == gent->owner)
951fd09b
AM
9658 {
9659 gent->got.refcount = 0;
9660 break;
9661 }
9662
9663 /* If not, then we'll be using our own TPREL entry. */
9664 if (gent->got.refcount != 0)
9665 gent->tls_type = TLS_TLS | TLS_TPREL;
9666 }
9667
7865406b
AM
9668 /* Remove any list entry that won't generate a word in the GOT before
9669 we call merge_got_entries. Otherwise we risk merging to empty
9670 entries. */
0b8bcf0d
AM
9671 pgent = &h->got.glist;
9672 while ((gent = *pgent) != NULL)
411e1bfb 9673 if (gent->got.refcount > 0)
7865406b
AM
9674 {
9675 if ((gent->tls_type & TLS_LD) != 0
9676 && !h->def_dynamic)
9677 {
9678 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9679 *pgent = gent->next;
9680 }
9681 else
9682 pgent = &gent->next;
9683 }
9684 else
9685 *pgent = gent->next;
9686
9687 if (!htab->do_multi_toc)
9688 merge_got_entries (&h->got.glist);
9689
9690 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9691 if (!gent->is_indirect)
411e1bfb 9692 {
46434633
AM
9693 /* Make sure this symbol is output as a dynamic symbol. */
9694 if (!ensure_undef_dynamic (info, h))
f0158f44 9695 return FALSE;
65f38f15 9696
0c8d6e5c 9697 if (!is_ppc64_elf (gent->owner))
927be08e 9698 abort ();
0ffa91dd 9699
927be08e 9700 allocate_got (h, info, gent);
411e1bfb 9701 }
65f38f15 9702
954b63d4
AM
9703 /* If no dynamic sections we can't have dynamic relocs, except for
9704 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9705 if (!htab->elf.dynamic_sections_created
9706 && h->type != STT_GNU_IFUNC)
9707 eh->dyn_relocs = NULL;
9708
529fe20e
AM
9709 /* Discard relocs on undefined symbols that must be local. */
9710 else if (h->root.type == bfd_link_hash_undefined
9711 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9712 eh->dyn_relocs = NULL;
9713
954b63d4
AM
9714 /* Also discard relocs on undefined weak syms with non-default
9715 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9716 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
9717 eh->dyn_relocs = NULL;
9718
8a2058b5 9719 if (eh->dyn_relocs != NULL)
65f38f15 9720 {
8a2058b5
AM
9721 struct elf_dyn_relocs *p, **pp;
9722
57e7d118
AM
9723 /* In the shared -Bsymbolic case, discard space allocated for
9724 dynamic pc-relative relocs against symbols which turn out to
9725 be defined in regular objects. For the normal shared case,
9726 discard space for relocs that have become local due to symbol
9727 visibility changes. */
9728
9729 if (bfd_link_pic (info))
65f38f15 9730 {
57e7d118
AM
9731 /* Relocs that use pc_count are those that appear on a call
9732 insn, or certain REL relocs (see must_be_dyn_reloc) that
9733 can be generated via assembly. We want calls to
9734 protected symbols to resolve directly to the function
9735 rather than going via the plt. If people want function
9736 pointer comparisons to work as expected then they should
9737 avoid writing weird assembly. */
9738 if (SYMBOL_CALLS_LOCAL (info, h))
9739 {
57e7d118
AM
9740 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9741 {
9742 p->count -= p->pc_count;
9743 p->pc_count = 0;
9744 if (p->count == 0)
9745 *pp = p->next;
9746 else
9747 pp = &p->next;
9748 }
9749 }
65f38f15 9750
954b63d4 9751 if (eh->dyn_relocs != NULL)
5bd4f169 9752 {
46434633
AM
9753 /* Make sure this symbol is output as a dynamic symbol. */
9754 if (!ensure_undef_dynamic (info, h))
f0158f44 9755 return FALSE;
5bd4f169 9756 }
65f38f15 9757 }
529fe20e 9758 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 9759 {
8a2058b5 9760 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9761 symbols which turn out to need copy relocs or are not
9762 dynamic. */
529fe20e
AM
9763 if (h->dynamic_adjusted
9764 && !h->def_regular
9765 && !ELF_COMMON_DEF_P (h))
f0158f44 9766 {
46434633
AM
9767 /* Make sure this symbol is output as a dynamic symbol. */
9768 if (!ensure_undef_dynamic (info, h))
f0158f44 9769 return FALSE;
dfbb6ac9 9770
f0158f44
AM
9771 if (h->dynindx == -1)
9772 eh->dyn_relocs = NULL;
9773 }
9774 else
8a2058b5 9775 eh->dyn_relocs = NULL;
57e7d118
AM
9776 }
9777
9778 /* Finally, allocate space. */
9779 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9780 {
9781 asection *sreloc = elf_section_data (p->sec)->sreloc;
9782 if (eh->elf.type == STT_GNU_IFUNC)
9783 sreloc = htab->elf.irelplt;
9784 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9785 }
65f38f15 9786 }
57e7d118
AM
9787
9788 if ((htab->elf.dynamic_sections_created
f0158f44 9789 && h->dynindx != -1)
57e7d118 9790 || h->type == STT_GNU_IFUNC)
65f38f15 9791 {
57e7d118
AM
9792 struct plt_entry *pent;
9793 bfd_boolean doneone = FALSE;
9794 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9795 if (pent->plt.refcount > 0)
9796 {
9797 if (!htab->elf.dynamic_sections_created
9798 || h->dynindx == -1)
9799 {
9800 s = htab->elf.iplt;
9801 pent->plt.offset = s->size;
9802 s->size += PLT_ENTRY_SIZE (htab);
9803 s = htab->elf.irelplt;
9804 }
9805 else
9806 {
9807 /* If this is the first .plt entry, make room for the special
9808 first entry. */
9809 s = htab->elf.splt;
9810 if (s->size == 0)
9811 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9812
57e7d118 9813 pent->plt.offset = s->size;
65f38f15 9814
57e7d118
AM
9815 /* Make room for this entry. */
9816 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9817
57e7d118
AM
9818 /* Make room for the .glink code. */
9819 s = htab->glink;
9820 if (s->size == 0)
9e390558 9821 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9822 if (htab->opd_abi)
9823 {
9824 /* We need bigger stubs past index 32767. */
9e390558 9825 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9826 s->size += 4;
9827 s->size += 2*4;
9828 }
9829 else
9830 s->size += 4;
65f38f15 9831
57e7d118
AM
9832 /* We also need to make an entry in the .rela.plt section. */
9833 s = htab->elf.srelplt;
9834 }
9835 s->size += sizeof (Elf64_External_Rela);
9836 doneone = TRUE;
9837 }
9838 else
9839 pent->plt.offset = (bfd_vma) -1;
9840 if (!doneone)
9841 {
9842 h->plt.plist = NULL;
9843 h->needs_plt = 0;
9844 }
65f38f15 9845 }
57e7d118 9846 else
65f38f15 9847 {
57e7d118
AM
9848 h->plt.plist = NULL;
9849 h->needs_plt = 0;
65f38f15
AM
9850 }
9851
b34976b6 9852 return TRUE;
65f38f15
AM
9853}
9854
9e390558
AM
9855#define PPC_LO(v) ((v) & 0xffff)
9856#define PPC_HI(v) (((v) >> 16) & 0xffff)
9857#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9858
a345bc8d
AM
9859/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9860 to set up space for global entry stubs. These are put in glink,
9861 after the branch table. */
65f38f15 9862
b34976b6 9863static bfd_boolean
a345bc8d 9864size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9865{
a345bc8d
AM
9866 struct bfd_link_info *info;
9867 struct ppc_link_hash_table *htab;
9868 struct plt_entry *pent;
9e390558 9869 asection *s, *plt;
65f38f15 9870
a345bc8d
AM
9871 if (h->root.type == bfd_link_hash_indirect)
9872 return TRUE;
65f38f15 9873
a345bc8d
AM
9874 if (!h->pointer_equality_needed)
9875 return TRUE;
65f38f15 9876
a345bc8d
AM
9877 if (h->def_regular)
9878 return TRUE;
65f38f15 9879
a345bc8d
AM
9880 info = inf;
9881 htab = ppc_hash_table (info);
9882 if (htab == NULL)
9883 return FALSE;
9884
9e390558
AM
9885 s = htab->global_entry;
9886 plt = htab->elf.splt;
a345bc8d
AM
9887 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9888 if (pent->plt.offset != (bfd_vma) -1
9889 && pent->addend == 0)
9890 {
afe397ea
AM
9891 /* For ELFv2, if this symbol is not defined in a regular file
9892 and we are not generating a shared library or pie, then we
9893 need to define the symbol in the executable on a call stub.
9894 This is to avoid text relocations. */
9e390558
AM
9895 bfd_vma off, stub_align, stub_off, stub_size;
9896 unsigned int align_power;
9897
9898 stub_size = 16;
9899 stub_off = s->size;
9900 if (htab->params->plt_stub_align >= 0)
9901 align_power = htab->params->plt_stub_align;
9902 else
9903 align_power = -htab->params->plt_stub_align;
9904 /* Setting section alignment is delayed until we know it is
9905 non-empty. Otherwise the .text output section will be
9906 aligned at least to plt_stub_align even when no global
9907 entry stubs are needed. */
9908 if (s->alignment_power < align_power)
9909 s->alignment_power = align_power;
9910 stub_align = (bfd_vma) 1 << align_power;
9911 if (htab->params->plt_stub_align >= 0
9912 || ((((stub_off + stub_size - 1) & -stub_align)
9913 - (stub_off & -stub_align))
9914 > ((stub_size - 1) & -stub_align)))
9915 stub_off = (stub_off + stub_align - 1) & -stub_align;
9916 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9917 off -= stub_off + s->output_offset + s->output_section->vma;
9918 /* Note that for --plt-stub-align negative we have a possible
9919 dependency between stub offset and size. Break that
9920 dependency by assuming the max stub size when calculating
9921 the stub offset. */
9922 if (PPC_HA (off) == 0)
9923 stub_size -= 4;
8a2058b5 9924 h->root.type = bfd_link_hash_defined;
afe397ea 9925 h->root.u.def.section = s;
9e390558
AM
9926 h->root.u.def.value = stub_off;
9927 s->size = stub_off + stub_size;
a345bc8d
AM
9928 break;
9929 }
9930 return TRUE;
9931}
9932
9933/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9934 read-only sections. */
9935
9936static bfd_boolean
98bbb1b8 9937maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9938{
98bbb1b8
AM
9939 asection *sec;
9940
a345bc8d
AM
9941 if (h->root.type == bfd_link_hash_indirect)
9942 return TRUE;
9943
98bbb1b8
AM
9944 sec = readonly_dynrelocs (h);
9945 if (sec != NULL)
a345bc8d 9946 {
98bbb1b8
AM
9947 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9948
9949 info->flags |= DF_TEXTREL;
9950 info->callbacks->minfo
c1c8c1ef 9951 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
63c1f59d 9952 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9953
9954 /* Not an error, just cut short the traversal. */
9955 return FALSE;
65f38f15 9956 }
b34976b6 9957 return TRUE;
65f38f15
AM
9958}
9959
9960/* Set the sizes of the dynamic sections. */
9961
b34976b6 9962static bfd_boolean
ee67d69a 9963ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9964 struct bfd_link_info *info)
65f38f15
AM
9965{
9966 struct ppc_link_hash_table *htab;
9967 bfd *dynobj;
9968 asection *s;
b34976b6 9969 bfd_boolean relocs;
65f38f15 9970 bfd *ibfd;
7865406b 9971 struct got_entry *first_tlsld;
65f38f15
AM
9972
9973 htab = ppc_hash_table (info);
4dfe6ac6
NC
9974 if (htab == NULL)
9975 return FALSE;
9976
65f38f15
AM
9977 dynobj = htab->elf.dynobj;
9978 if (dynobj == NULL)
9979 abort ();
9980
9981 if (htab->elf.dynamic_sections_created)
9982 {
9983 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9984 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9985 {
3d4d4302 9986 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9987 if (s == NULL)
9988 abort ();
eea6121a 9989 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9990 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9991 }
9992 }
9993
9994 /* Set up .got offsets for local syms, and space for local dynamic
9995 relocs. */
c72f2fb2 9996 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9997 {
411e1bfb
AM
9998 struct got_entry **lgot_ents;
9999 struct got_entry **end_lgot_ents;
e054468f
AM
10000 struct plt_entry **local_plt;
10001 struct plt_entry **end_local_plt;
f961d9dd 10002 unsigned char *lgot_masks;
65f38f15
AM
10003 bfd_size_type locsymcount;
10004 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10005
0c8d6e5c 10006 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10007 continue;
10008
10009 for (s = ibfd->sections; s != NULL; s = s->next)
10010 {
19e08130 10011 struct ppc_dyn_relocs *p;
65f38f15 10012
6edfbbad 10013 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10014 {
ec338859
AM
10015 if (!bfd_is_abs_section (p->sec)
10016 && bfd_is_abs_section (p->sec->output_section))
10017 {
10018 /* Input section has been discarded, either because
10019 it is a copy of a linkonce section or due to
10020 linker script /DISCARD/, so we'll be discarding
10021 the relocs too. */
10022 }
248866a8 10023 else if (p->count != 0)
ec338859 10024 {
19e08130
AM
10025 asection *srel = elf_section_data (p->sec)->sreloc;
10026 if (p->ifunc)
33e44f2e 10027 srel = htab->elf.irelplt;
eea6121a 10028 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10029 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10030 info->flags |= DF_TEXTREL;
ec338859 10031 }
65f38f15
AM
10032 }
10033 }
10034
411e1bfb
AM
10035 lgot_ents = elf_local_got_ents (ibfd);
10036 if (!lgot_ents)
65f38f15
AM
10037 continue;
10038
0ffa91dd 10039 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10040 locsymcount = symtab_hdr->sh_info;
411e1bfb 10041 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10042 local_plt = (struct plt_entry **) end_lgot_ents;
10043 end_local_plt = local_plt + locsymcount;
f961d9dd 10044 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10045 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10046 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10047 {
0b8bcf0d 10048 struct got_entry **pent, *ent;
411e1bfb 10049
0b8bcf0d
AM
10050 pent = lgot_ents;
10051 while ((ent = *pent) != NULL)
411e1bfb
AM
10052 if (ent->got.refcount > 0)
10053 {
e7b938ca 10054 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10055 {
927be08e 10056 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10057 *pent = ent->next;
411e1bfb
AM
10058 }
10059 else
10060 {
19e08130
AM
10061 unsigned int ent_size = 8;
10062 unsigned int rel_size = sizeof (Elf64_External_Rela);
10063
eea6121a 10064 ent->got.offset = s->size;
e7b938ca 10065 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10066 {
19e08130
AM
10067 ent_size *= 2;
10068 rel_size *= 2;
10069 }
10070 s->size += ent_size;
10071 if ((*lgot_masks & PLT_IFUNC) != 0)
10072 {
33e44f2e 10073 htab->elf.irelplt->size += rel_size;
19e08130
AM
10074 htab->got_reli_size += rel_size;
10075 }
f15d0b54
AM
10076 else if (bfd_link_pic (info)
10077 && !((ent->tls_type & TLS_TPREL) != 0
10078 && bfd_link_executable (info)))
19e08130
AM
10079 {
10080 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10081 srel->size += rel_size;
927be08e 10082 }
0b8bcf0d 10083 pent = &ent->next;
411e1bfb
AM
10084 }
10085 }
10086 else
0b8bcf0d 10087 *pent = ent->next;
65f38f15 10088 }
e054468f
AM
10089
10090 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10091 for (; local_plt < end_local_plt; ++local_plt)
10092 {
10093 struct plt_entry *ent;
10094
10095 for (ent = *local_plt; ent != NULL; ent = ent->next)
10096 if (ent->plt.refcount > 0)
10097 {
33e44f2e 10098 s = htab->elf.iplt;
e054468f 10099 ent->plt.offset = s->size;
b9e5796b 10100 s->size += PLT_ENTRY_SIZE (htab);
e054468f 10101
33e44f2e 10102 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
10103 }
10104 else
10105 ent->plt.offset = (bfd_vma) -1;
10106 }
65f38f15
AM
10107 }
10108
10109 /* Allocate global sym .plt and .got entries, and space for global
10110 sym dynamic relocs. */
4ce794b7 10111 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10112
0e1862bb 10113 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10114 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10115
7865406b 10116 first_tlsld = NULL;
c72f2fb2 10117 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10118 {
7865406b
AM
10119 struct got_entry *ent;
10120
0c8d6e5c 10121 if (!is_ppc64_elf (ibfd))
102890f0
AM
10122 continue;
10123
7865406b
AM
10124 ent = ppc64_tlsld_got (ibfd);
10125 if (ent->got.refcount > 0)
102890f0 10126 {
7865406b 10127 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10128 {
7865406b
AM
10129 ent->is_indirect = TRUE;
10130 ent->got.ent = first_tlsld;
10131 }
10132 else
10133 {
10134 if (first_tlsld == NULL)
10135 first_tlsld = ent;
10136 s = ppc64_elf_tdata (ibfd)->got;
10137 ent->got.offset = s->size;
10138 ent->owner = ibfd;
10139 s->size += 16;
0e1862bb 10140 if (bfd_link_pic (info))
7865406b
AM
10141 {
10142 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10143 srel->size += sizeof (Elf64_External_Rela);
10144 }
102890f0
AM
10145 }
10146 }
10147 else
7865406b 10148 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10149 }
10150
65f38f15
AM
10151 /* We now have determined the sizes of the various dynamic sections.
10152 Allocate memory for them. */
b34976b6 10153 relocs = FALSE;
65f38f15
AM
10154 for (s = dynobj->sections; s != NULL; s = s->next)
10155 {
10156 if ((s->flags & SEC_LINKER_CREATED) == 0)
10157 continue;
10158
4ce794b7 10159 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10160 /* These haven't been allocated yet; don't strip. */
10161 continue;
33e44f2e
AM
10162 else if (s == htab->elf.sgot
10163 || s == htab->elf.splt
10164 || s == htab->elf.iplt
c456f082 10165 || s == htab->glink
9e390558 10166 || s == htab->global_entry
5474d94f
AM
10167 || s == htab->elf.sdynbss
10168 || s == htab->elf.sdynrelro)
65f38f15
AM
10169 {
10170 /* Strip this section if we don't need it; see the
10171 comment below. */
5bd4f169 10172 }
58d180e8
AM
10173 else if (s == htab->glink_eh_frame)
10174 {
10175 if (!bfd_is_abs_section (s->output_section))
10176 /* Not sized yet. */
10177 continue;
10178 }
70cc837d 10179 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10180 {
c456f082 10181 if (s->size != 0)
5bd4f169 10182 {
33e44f2e 10183 if (s != htab->elf.srelplt)
b34976b6 10184 relocs = TRUE;
5bd4f169
AM
10185
10186 /* We use the reloc_count field as a counter if we need
10187 to copy relocs into the output file. */
10188 s->reloc_count = 0;
10189 }
10190 }
65f38f15 10191 else
5bd4f169
AM
10192 {
10193 /* It's not one of our sections, so don't allocate space. */
10194 continue;
10195 }
10196
eea6121a 10197 if (s->size == 0)
5bd4f169 10198 {
c456f082
AM
10199 /* If we don't need this section, strip it from the
10200 output file. This is mostly to handle .rela.bss and
10201 .rela.plt. We must create both sections in
10202 create_dynamic_sections, because they must be created
10203 before the linker maps input sections to output
10204 sections. The linker does that before
10205 adjust_dynamic_symbol is called, and it is that
10206 function which decides whether anything needs to go
10207 into these sections. */
8423293d 10208 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10209 continue;
10210 }
10211
06bcf541
AM
10212 if (bfd_is_abs_section (s->output_section))
10213 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10214 s->name);
10215
c456f082 10216 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10217 continue;
10218
65f38f15
AM
10219 /* Allocate memory for the section contents. We use bfd_zalloc
10220 here in case unused entries are not reclaimed before the
10221 section's contents are written out. This should not happen,
411e1bfb
AM
10222 but this way if it does we get a R_PPC64_NONE reloc in .rela
10223 sections instead of garbage.
10224 We also rely on the section contents being zero when writing
5474d94f 10225 the GOT and .dynrelro. */
eea6121a 10226 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10227 if (s->contents == NULL)
b34976b6 10228 return FALSE;
5bd4f169
AM
10229 }
10230
c72f2fb2 10231 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10232 {
0c8d6e5c 10233 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10234 continue;
10235
e717da7e 10236 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10237 if (s != NULL && s != htab->elf.sgot)
e717da7e 10238 {
eea6121a 10239 if (s->size == 0)
8423293d 10240 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10241 else
10242 {
eea6121a 10243 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10244 if (s->contents == NULL)
10245 return FALSE;
10246 }
10247 }
10248 s = ppc64_elf_tdata (ibfd)->relgot;
10249 if (s != NULL)
10250 {
eea6121a 10251 if (s->size == 0)
8423293d 10252 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10253 else
10254 {
eea6121a 10255 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10256 if (s->contents == NULL)
10257 return FALSE;
10258 relocs = TRUE;
10259 s->reloc_count = 0;
10260 }
10261 }
10262 }
10263
e86ce104 10264 if (htab->elf.dynamic_sections_created)
5bd4f169 10265 {
e8910a83
AM
10266 bfd_boolean tls_opt;
10267
5bd4f169
AM
10268 /* Add some entries to the .dynamic section. We fill in the
10269 values later, in ppc64_elf_finish_dynamic_sections, but we
10270 must add the entries now so that we get the correct size for
10271 the .dynamic section. The DT_DEBUG entry is filled in by the
10272 dynamic linker and used by the debugger. */
dc810e39 10273#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10274 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10275
0e1862bb 10276 if (bfd_link_executable (info))
5bd4f169 10277 {
dc810e39 10278 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10279 return FALSE;
5bd4f169
AM
10280 }
10281
33e44f2e 10282 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10283 {
dc810e39
AM
10284 if (!add_dynamic_entry (DT_PLTGOT, 0)
10285 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10286 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10287 || !add_dynamic_entry (DT_JMPREL, 0)
10288 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10289 return FALSE;
5bd4f169
AM
10290 }
10291
ee67d69a 10292 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10293 {
10294 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10295 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10296 return FALSE;
19397422
AM
10297 }
10298
7c9cf415 10299 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10300 && htab->tls_get_addr_fd != NULL
10301 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10302 if (tls_opt || !htab->opd_abi)
10303 {
10304 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10305 return FALSE;
10306 }
a7f2871e 10307
5bd4f169
AM
10308 if (relocs)
10309 {
dc810e39
AM
10310 if (!add_dynamic_entry (DT_RELA, 0)
10311 || !add_dynamic_entry (DT_RELASZ, 0)
10312 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10313 return FALSE;
5bd4f169 10314
65f38f15
AM
10315 /* If any dynamic relocs apply to a read-only section,
10316 then we need a DT_TEXTREL entry. */
248866a8 10317 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10318 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10319
65f38f15 10320 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10321 {
65f38f15 10322 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10323 return FALSE;
5bd4f169 10324 }
5bd4f169 10325 }
5bd4f169 10326 }
65f38f15 10327#undef add_dynamic_entry
5bd4f169 10328
b34976b6 10329 return TRUE;
5bd4f169
AM
10330}
10331
a345bc8d
AM
10332/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10333
10334static bfd_boolean
10335ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10336{
10337 if (h->plt.plist != NULL
10338 && !h->def_regular
10339 && !h->pointer_equality_needed)
10340 return FALSE;
10341
10342 return _bfd_elf_hash_symbol (h);
10343}
10344
721956f4 10345/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10346
4ce794b7
AM
10347static inline enum ppc_stub_type
10348ppc_type_of_stub (asection *input_sec,
10349 const Elf_Internal_Rela *rel,
10350 struct ppc_link_hash_entry **hash,
e054468f 10351 struct plt_entry **plt_ent,
6911b7dc
AM
10352 bfd_vma destination,
10353 unsigned long local_off)
5bd4f169 10354{
721956f4
AM
10355 struct ppc_link_hash_entry *h = *hash;
10356 bfd_vma location;
10357 bfd_vma branch_offset;
10358 bfd_vma max_branch_offset;
4ce794b7 10359 enum elf_ppc64_reloc_type r_type;
5bd4f169 10360
721956f4
AM
10361 if (h != NULL)
10362 {
e054468f 10363 struct plt_entry *ent;
7fe2b9a6 10364 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10365 if (h->oh != NULL
10366 && h->oh->is_func_descriptor)
7b8f6675
AM
10367 {
10368 fdh = ppc_follow_link (h->oh);
10369 *hash = fdh;
10370 }
8387904d 10371
e054468f
AM
10372 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10373 if (ent->addend == rel->r_addend
10374 && ent->plt.offset != (bfd_vma) -1)
10375 {
e054468f
AM
10376 *plt_ent = ent;
10377 return ppc_stub_plt_call;
10378 }
5bd4f169 10379
7fe2b9a6
AM
10380 /* Here, we know we don't have a plt entry. If we don't have a
10381 either a defined function descriptor or a defined entry symbol
10382 in a regular object file, then it is pointless trying to make
10383 any other type of stub. */
854b41e7
AM
10384 if (!is_static_defined (&fdh->elf)
10385 && !is_static_defined (&h->elf))
721956f4 10386 return ppc_stub_none;
5d1634d7 10387 }
e054468f
AM
10388 else if (elf_local_got_ents (input_sec->owner) != NULL)
10389 {
10390 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10391 struct plt_entry **local_plt = (struct plt_entry **)
10392 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10393 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10394
10395 if (local_plt[r_symndx] != NULL)
10396 {
10397 struct plt_entry *ent;
10398
10399 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10400 if (ent->addend == rel->r_addend
10401 && ent->plt.offset != (bfd_vma) -1)
10402 {
10403 *plt_ent = ent;
10404 return ppc_stub_plt_call;
10405 }
10406 }
10407 }
5d1634d7 10408
721956f4
AM
10409 /* Determine where the call point is. */
10410 location = (input_sec->output_offset
10411 + input_sec->output_section->vma
10412 + rel->r_offset);
5d1634d7 10413
721956f4
AM
10414 branch_offset = destination - location;
10415 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10416
721956f4
AM
10417 /* Determine if a long branch stub is needed. */
10418 max_branch_offset = 1 << 25;
4ce794b7 10419 if (r_type != R_PPC64_REL24)
721956f4 10420 max_branch_offset = 1 << 15;
5d1634d7 10421
6911b7dc 10422 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10423 /* We need a stub. Figure out whether a long_branch or plt_branch
10424 is needed later. */
10425 return ppc_stub_long_branch;
5d1634d7 10426
721956f4 10427 return ppc_stub_none;
5d1634d7
AM
10428}
10429
794e51c0
AM
10430/* With power7 weakly ordered memory model, it is possible for ld.so
10431 to update a plt entry in one thread and have another thread see a
10432 stale zero toc entry. To avoid this we need some sort of acquire
10433 barrier in the call stub. One solution is to make the load of the
10434 toc word seem to appear to depend on the load of the function entry
10435 word. Another solution is to test for r2 being zero, and branch to
10436 the appropriate glink entry if so.
10437
10438 . fake dep barrier compare
71a39c98
AM
10439 . ld 12,xxx(2) ld 12,xxx(2)
10440 . mtctr 12 mtctr 12
10441 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10442 . add 2,2,11 cmpldi 2,0
10443 . ld 2,xxx+8(2) bnectr+
10444 . bctr b <glink_entry>
10445
10446 The solution involving the compare turns out to be faster, so
10447 that's what we use unless the branch won't reach. */
10448
10449#define ALWAYS_USE_FAKE_DEP 0
10450#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10451
794e51c0
AM
10452static inline unsigned int
10453plt_stub_size (struct ppc_link_hash_table *htab,
10454 struct ppc_stub_hash_entry *stub_entry,
10455 bfd_vma off)
10456{
b9e5796b
AM
10457 unsigned size = 12;
10458
10459 if (ALWAYS_EMIT_R2SAVE
10460 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10461 size += 4;
10462 if (PPC_HA (off) != 0)
794e51c0 10463 size += 4;
b9e5796b
AM
10464 if (htab->opd_abi)
10465 {
10466 size += 4;
e7d1c40c 10467 if (htab->params->plt_static_chain)
b9e5796b 10468 size += 4;
bd4d2eaa
AM
10469 if (htab->params->plt_thread_safe
10470 && htab->elf.dynamic_sections_created
10471 && stub_entry->h != NULL
10472 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10473 size += 8;
e7d1c40c 10474 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10475 size += 4;
10476 }
794e51c0
AM
10477 if (stub_entry->h != NULL
10478 && (stub_entry->h == htab->tls_get_addr_fd
10479 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10480 && htab->params->tls_get_addr_opt)
f378ab09
AM
10481 {
10482 size += 7 * 4;
10483 if (ALWAYS_EMIT_R2SAVE
10484 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 10485 size += 6 * 4;
f378ab09 10486 }
794e51c0
AM
10487 return size;
10488}
10489
2420fff6
AM
10490/* Depending on the sign of plt_stub_align:
10491 If positive, return the padding to align to a 2**plt_stub_align
10492 boundary.
10493 If negative, if this stub would cross fewer 2**plt_stub_align
10494 boundaries if we align, then return the padding needed to do so. */
10495
794e51c0
AM
10496static inline unsigned int
10497plt_stub_pad (struct ppc_link_hash_table *htab,
10498 struct ppc_stub_hash_entry *stub_entry,
10499 bfd_vma plt_off)
10500{
2420fff6 10501 int stub_align;
794e51c0 10502 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10503 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10504
2420fff6
AM
10505 if (htab->params->plt_stub_align >= 0)
10506 {
10507 stub_align = 1 << htab->params->plt_stub_align;
10508 if ((stub_off & (stub_align - 1)) != 0)
10509 return stub_align - (stub_off & (stub_align - 1));
10510 return 0;
10511 }
10512
10513 stub_align = 1 << -htab->params->plt_stub_align;
794e51c0 10514 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10515 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10516 return stub_align - (stub_off & (stub_align - 1));
10517 return 0;
10518}
10519
10520/* Build a .plt call stub. */
10521
10522static inline bfd_byte *
10523build_plt_stub (struct ppc_link_hash_table *htab,
10524 struct ppc_stub_hash_entry *stub_entry,
10525 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10526{
e7d1c40c 10527 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10528 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10529 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10530 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10531 && htab->elf.dynamic_sections_created
10532 && stub_entry->h != NULL
10533 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10534 bfd_boolean use_fake_dep = plt_thread_safe;
10535 bfd_vma cmp_branch_off = 0;
10536
10537 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10538 && plt_load_toc
794e51c0 10539 && plt_thread_safe
bd4d2eaa
AM
10540 && !((stub_entry->h == htab->tls_get_addr_fd
10541 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10542 && htab->params->tls_get_addr_opt))
794e51c0
AM
10543 {
10544 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10545 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10546 / PLT_ENTRY_SIZE (htab));
9e390558 10547 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10548 bfd_vma to, from;
10549
68d62958
AM
10550 if (pltindex > 32768)
10551 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10552 to = (glinkoff
10553 + htab->glink->output_offset
10554 + htab->glink->output_section->vma);
6f20ed8a 10555 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10556 + 4 * (ALWAYS_EMIT_R2SAVE
10557 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10558 + 4 * (PPC_HA (offset) != 0)
10559 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10560 != PPC_HA (offset))
10561 + 4 * (plt_static_chain != 0)
10562 + 20
6f20ed8a
AM
10563 + stub_entry->group->stub_sec->output_offset
10564 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10565 cmp_branch_off = to - from;
10566 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10567 }
10568
ac2df442
AM
10569 if (PPC_HA (offset) != 0)
10570 {
176a0d42
AM
10571 if (r != NULL)
10572 {
794e51c0
AM
10573 if (ALWAYS_EMIT_R2SAVE
10574 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10575 r[0].r_offset += 4;
176a0d42 10576 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10577 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10578 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10579 r[1].r_addend = r[0].r_addend;
b9e5796b 10580 if (plt_load_toc)
176a0d42 10581 {
b9e5796b 10582 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10583 {
b9e5796b
AM
10584 r[2].r_offset = r[1].r_offset + 4;
10585 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10586 r[2].r_addend = r[0].r_addend;
10587 }
10588 else
10589 {
10590 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10591 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10592 r[2].r_addend = r[0].r_addend + 8;
10593 if (plt_static_chain)
10594 {
10595 r[3].r_offset = r[2].r_offset + 4;
10596 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10597 r[3].r_addend = r[0].r_addend + 16;
10598 }
c7131b65 10599 }
176a0d42
AM
10600 }
10601 }
794e51c0
AM
10602 if (ALWAYS_EMIT_R2SAVE
10603 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10604 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10605 if (plt_load_toc)
10606 {
10607 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10608 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10609 }
10610 else
10611 {
10612 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10613 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10614 }
b9e5796b
AM
10615 if (plt_load_toc
10616 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10617 {
71a39c98 10618 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10619 offset = 0;
10620 }
71a39c98 10621 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10622 if (plt_load_toc)
794e51c0 10623 {
b9e5796b
AM
10624 if (use_fake_dep)
10625 {
10626 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10627 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10628 }
10629 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10630 if (plt_static_chain)
10631 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10632 }
ac2df442
AM
10633 }
10634 else
10635 {
176a0d42
AM
10636 if (r != NULL)
10637 {
794e51c0
AM
10638 if (ALWAYS_EMIT_R2SAVE
10639 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10640 r[0].r_offset += 4;
176a0d42 10641 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10642 if (plt_load_toc)
176a0d42 10643 {
b9e5796b 10644 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10645 {
b9e5796b
AM
10646 r[1].r_offset = r[0].r_offset + 4;
10647 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10648 r[1].r_addend = r[0].r_addend;
10649 }
10650 else
10651 {
10652 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10653 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10654 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10655 if (plt_static_chain)
10656 {
10657 r[2].r_offset = r[1].r_offset + 4;
10658 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10659 r[2].r_addend = r[0].r_addend + 8;
10660 }
c7131b65 10661 }
176a0d42
AM
10662 }
10663 }
794e51c0
AM
10664 if (ALWAYS_EMIT_R2SAVE
10665 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10666 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10667 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10668 if (plt_load_toc
10669 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10670 {
10671 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10672 offset = 0;
10673 }
71a39c98 10674 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10675 if (plt_load_toc)
794e51c0 10676 {
b9e5796b
AM
10677 if (use_fake_dep)
10678 {
10679 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10680 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10681 }
10682 if (plt_static_chain)
10683 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10684 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10685 }
ac2df442 10686 }
b9e5796b 10687 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10688 {
10689 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10690 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10691 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10692 }
10693 else
407aa07c 10694 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10695 return p;
10696}
10697
a7f2871e
AM
10698/* Build a special .plt call stub for __tls_get_addr. */
10699
10700#define LD_R11_0R3 0xe9630000
10701#define LD_R12_0R3 0xe9830000
10702#define MR_R0_R3 0x7c601b78
10703#define CMPDI_R11_0 0x2c2b0000
10704#define ADD_R3_R12_R13 0x7c6c6a14
10705#define BEQLR 0x4d820020
10706#define MR_R3_R0 0x7c030378
a7f2871e
AM
10707#define STD_R11_0R1 0xf9610000
10708#define BCTRL 0x4e800421
10709#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10710#define MTLR_R11 0x7d6803a6
10711
10712static inline bfd_byte *
794e51c0
AM
10713build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10714 struct ppc_stub_hash_entry *stub_entry,
10715 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10716{
e7d1c40c 10717 bfd *obfd = htab->params->stub_bfd;
794e51c0 10718
a7f2871e
AM
10719 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10720 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10721 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10722 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10723 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10724 bfd_put_32 (obfd, BEQLR, p), p += 4;
10725 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10726 if (r != NULL)
10727 r[0].r_offset += 7 * 4;
10728 if (!ALWAYS_EMIT_R2SAVE
10729 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
10730 return build_plt_stub (htab, stub_entry, p, offset, r);
10731
a7f2871e 10732 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10733 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10734
10735 if (r != NULL)
f378ab09 10736 r[0].r_offset += 2 * 4;
794e51c0 10737 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 10738 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10739
a078d95a 10740 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10741 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10742 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10743 bfd_put_32 (obfd, BLR, p), p += 4;
10744
10745 return p;
10746}
10747
176a0d42
AM
10748static Elf_Internal_Rela *
10749get_relocs (asection *sec, int count)
10750{
10751 Elf_Internal_Rela *relocs;
10752 struct bfd_elf_section_data *elfsec_data;
10753
10754 elfsec_data = elf_section_data (sec);
10755 relocs = elfsec_data->relocs;
10756 if (relocs == NULL)
10757 {
10758 bfd_size_type relsize;
10759 relsize = sec->reloc_count * sizeof (*relocs);
10760 relocs = bfd_alloc (sec->owner, relsize);
10761 if (relocs == NULL)
10762 return NULL;
10763 elfsec_data->relocs = relocs;
d4730f92
BS
10764 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10765 sizeof (Elf_Internal_Shdr));
10766 if (elfsec_data->rela.hdr == NULL)
10767 return NULL;
10768 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10769 * sizeof (Elf64_External_Rela));
10770 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10771 sec->reloc_count = 0;
10772 }
10773 relocs += sec->reloc_count;
10774 sec->reloc_count += count;
10775 return relocs;
10776}
10777
aa374f67 10778static bfd_vma
25f53a85 10779get_r2off (struct bfd_link_info *info,
aa374f67
AM
10780 struct ppc_stub_hash_entry *stub_entry)
10781{
25f53a85 10782 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10783 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10784
10785 if (r2off == 0)
10786 {
10787 /* Support linking -R objects. Get the toc pointer from the
10788 opd entry. */
10789 char buf[8];
b9e5796b
AM
10790 if (!htab->opd_abi)
10791 return r2off;
aa374f67
AM
10792 asection *opd = stub_entry->h->elf.root.u.def.section;
10793 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10794
10795 if (strcmp (opd->name, ".opd") != 0
10796 || opd->reloc_count != 0)
10797 {
c1c8c1ef 10798 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
25f53a85 10799 stub_entry->h->elf.root.root.string);
aa374f67 10800 bfd_set_error (bfd_error_bad_value);
a7c49797 10801 return (bfd_vma) -1;
aa374f67
AM
10802 }
10803 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10804 return (bfd_vma) -1;
aa374f67 10805 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10806 r2off -= elf_gp (info->output_bfd);
aa374f67 10807 }
6f20ed8a 10808 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10809 return r2off;
10810}
10811
b34976b6 10812static bfd_boolean
4ce794b7 10813ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10814{
721956f4
AM
10815 struct ppc_stub_hash_entry *stub_entry;
10816 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10817 struct bfd_link_info *info;
10818 struct ppc_link_hash_table *htab;
721956f4
AM
10819 bfd_byte *loc;
10820 bfd_byte *p;
ee75fd95 10821 bfd_vma dest, off;
176a0d42 10822 Elf_Internal_Rela *r;
e054468f 10823 asection *plt;
5d1634d7 10824
721956f4
AM
10825 /* Massage our args to the form they really have. */
10826 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10827 info = in_arg;
5d1634d7 10828
5d1634d7 10829 htab = ppc_hash_table (info);
4dfe6ac6
NC
10830 if (htab == NULL)
10831 return FALSE;
5d1634d7 10832
721956f4 10833 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
10834 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10835 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10836
4ce794b7 10837 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10838 switch (stub_entry->stub_type)
5d1634d7 10839 {
721956f4 10840 case ppc_stub_long_branch:
ad8e1ba5 10841 case ppc_stub_long_branch_r2off:
721956f4 10842 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10843 dest = (stub_entry->target_value
10844 + stub_entry->target_section->output_offset
10845 + stub_entry->target_section->output_section->vma);
10846 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10847 off = dest;
5d1634d7 10848
721956f4
AM
10849 /* And this is where we are coming from. */
10850 off -= (stub_entry->stub_offset
6f20ed8a
AM
10851 + stub_entry->group->stub_sec->output_offset
10852 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 10853
9e390558 10854 p = loc;
ac2df442 10855 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10856 {
25f53a85 10857 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10858
a7c49797 10859 if (r2off == (bfd_vma) -1)
aa374f67
AM
10860 {
10861 htab->stub_error = TRUE;
10862 return FALSE;
10863 }
9e390558
AM
10864 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10865 p += 4;
ac2df442
AM
10866 if (PPC_HA (r2off) != 0)
10867 {
e7d1c40c 10868 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10869 ADDIS_R2_R2 | PPC_HA (r2off), p);
10870 p += 4;
a7c49797
AM
10871 }
10872 if (PPC_LO (r2off) != 0)
10873 {
10874 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10875 ADDI_R2_R2 | PPC_LO (r2off), p);
10876 p += 4;
ac2df442 10877 }
9e390558 10878 off -= p - loc;
ad8e1ba5 10879 }
9e390558
AM
10880 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10881 p += 4;
ad8e1ba5 10882
5c3dead3
AM
10883 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10884 {
cf97bcb0
AM
10885 _bfd_error_handler
10886 (_("long branch stub `%s' offset overflow"),
bc30df16 10887 stub_entry->root.string);
5c3dead3
AM
10888 htab->stub_error = TRUE;
10889 return FALSE;
10890 }
ee75fd95
AM
10891
10892 if (info->emitrelocations)
10893 {
6f20ed8a 10894 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10895 if (r == NULL)
10896 return FALSE;
9e390558 10897 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95
AM
10898 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10899 r->r_addend = dest;
10900 if (stub_entry->h != NULL)
10901 {
10902 struct elf_link_hash_entry **hashes;
10903 unsigned long symndx;
10904 struct ppc_link_hash_entry *h;
10905
e7d1c40c 10906 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10907 if (hashes == NULL)
10908 {
10909 bfd_size_type hsize;
10910
10911 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10912 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10913 if (hashes == NULL)
10914 return FALSE;
e7d1c40c 10915 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10916 htab->stub_globals = 1;
10917 }
10918 symndx = htab->stub_globals++;
10919 h = stub_entry->h;
10920 hashes[symndx] = &h->elf;
10921 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10922 if (h->oh != NULL && h->oh->is_func)
b31867b6 10923 h = ppc_follow_link (h->oh);
ee75fd95
AM
10924 if (h->elf.root.u.def.section != stub_entry->target_section)
10925 /* H is an opd symbol. The addend must be zero. */
10926 r->r_addend = 0;
10927 else
10928 {
10929 off = (h->elf.root.u.def.value
10930 + h->elf.root.u.def.section->output_offset
10931 + h->elf.root.u.def.section->output_section->vma);
10932 r->r_addend -= off;
10933 }
10934 }
10935 }
721956f4 10936 break;
e86ce104 10937
721956f4 10938 case ppc_stub_plt_branch:
ad8e1ba5 10939 case ppc_stub_plt_branch_r2off:
721956f4
AM
10940 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10941 stub_entry->root.string + 9,
b34976b6 10942 FALSE, FALSE);
721956f4
AM
10943 if (br_entry == NULL)
10944 {
cf97bcb0
AM
10945 _bfd_error_handler (_("can't find branch stub `%s'"),
10946 stub_entry->root.string);
b34976b6
AM
10947 htab->stub_error = TRUE;
10948 return FALSE;
721956f4
AM
10949 }
10950
176a0d42
AM
10951 dest = (stub_entry->target_value
10952 + stub_entry->target_section->output_offset
10953 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10954 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10955 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10956
176a0d42 10957 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10958 htab->brlt->contents + br_entry->offset);
721956f4 10959
f94498ff 10960 if (br_entry->iter == htab->stub_iteration)
721956f4 10961 {
f94498ff 10962 br_entry->iter = 0;
84f5d08e 10963
f94498ff 10964 if (htab->relbrlt != NULL)
84f5d08e 10965 {
f94498ff
AM
10966 /* Create a reloc for the branch lookup table entry. */
10967 Elf_Internal_Rela rela;
10968 bfd_byte *rl;
10969
10970 rela.r_offset = (br_entry->offset
10971 + htab->brlt->output_offset
10972 + htab->brlt->output_section->vma);
10973 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10974 rela.r_addend = dest;
f94498ff
AM
10975
10976 rl = htab->relbrlt->contents;
10977 rl += (htab->relbrlt->reloc_count++
10978 * sizeof (Elf64_External_Rela));
10979 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10980 }
10981 else if (info->emitrelocations)
10982 {
176a0d42
AM
10983 r = get_relocs (htab->brlt, 1);
10984 if (r == NULL)
10985 return FALSE;
10986 /* brlt, being SEC_LINKER_CREATED does not go through the
10987 normal reloc processing. Symbols and offsets are not
10988 translated from input file to output file form, so
10989 set up the offset per the output file. */
f94498ff
AM
10990 r->r_offset = (br_entry->offset
10991 + htab->brlt->output_offset
10992 + htab->brlt->output_section->vma);
10993 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10994 r->r_addend = dest;
84f5d08e 10995 }
84f5d08e 10996 }
721956f4 10997
176a0d42
AM
10998 dest = (br_entry->offset
10999 + htab->brlt->output_offset
11000 + htab->brlt->output_section->vma);
11001
11002 off = (dest
06bcf541 11003 - elf_gp (info->output_bfd)
6f20ed8a 11004 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11005
ad8e1ba5 11006 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11007 {
25f53a85 11008 info->callbacks->einfo
c1c8c1ef 11009 (_("%P: linkage table error against `%pT'\n"),
721956f4 11010 stub_entry->root.string);
5d1634d7 11011 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11012 htab->stub_error = TRUE;
11013 return FALSE;
5d1634d7 11014 }
41bd81ab 11015
176a0d42
AM
11016 if (info->emitrelocations)
11017 {
6f20ed8a 11018 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11019 if (r == NULL)
11020 return FALSE;
6f20ed8a 11021 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11022 if (bfd_big_endian (info->output_bfd))
11023 r[0].r_offset += 2;
00f412ee 11024 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11025 r[0].r_offset += 4;
11026 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11027 r[0].r_addend = dest;
11028 if (PPC_HA (off) != 0)
11029 {
11030 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11031 r[1].r_offset = r[0].r_offset + 4;
11032 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11033 r[1].r_addend = r[0].r_addend;
11034 }
11035 }
11036
9e390558 11037 p = loc;
00f412ee 11038 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11039 {
176a0d42 11040 if (PPC_HA (off) != 0)
ac2df442 11041 {
e7d1c40c 11042 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11043 ADDIS_R12_R2 | PPC_HA (off), p);
11044 p += 4;
e7d1c40c 11045 bfd_put_32 (htab->params->stub_bfd,
9e390558 11046 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11047 }
11048 else
9e390558
AM
11049 bfd_put_32 (htab->params->stub_bfd,
11050 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11051 }
11052 else
11053 {
25f53a85 11054 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11055
a7c49797 11056 if (r2off == (bfd_vma) -1)
aa374f67
AM
11057 {
11058 htab->stub_error = TRUE;
11059 return FALSE;
11060 }
ad8e1ba5 11061
9e390558
AM
11062 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11063 p += 4;
176a0d42 11064 if (PPC_HA (off) != 0)
ac2df442 11065 {
e7d1c40c 11066 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11067 ADDIS_R12_R2 | PPC_HA (off), p);
11068 p += 4;
e7d1c40c 11069 bfd_put_32 (htab->params->stub_bfd,
9e390558 11070 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11071 }
11072 else
9e390558 11073 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11074
11075 if (PPC_HA (r2off) != 0)
11076 {
9e390558 11077 p += 4;
e7d1c40c 11078 bfd_put_32 (htab->params->stub_bfd,
9e390558 11079 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11080 }
11081 if (PPC_LO (r2off) != 0)
11082 {
9e390558 11083 p += 4;
e7d1c40c 11084 bfd_put_32 (htab->params->stub_bfd,
9e390558 11085 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11086 }
ad8e1ba5 11087 }
9e390558
AM
11088 p += 4;
11089 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11090 p += 4;
407aa07c
AM
11091 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11092 p += 4;
721956f4 11093 break;
5d1634d7 11094
721956f4 11095 case ppc_stub_plt_call:
794e51c0 11096 case ppc_stub_plt_call_r2save:
e054468f 11097 if (stub_entry->h != NULL
b31867b6
AM
11098 && stub_entry->h->is_func_descriptor
11099 && stub_entry->h->oh != NULL)
c862ae31 11100 {
b31867b6
AM
11101 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11102
11103 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11104 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11105 if (fh->elf.root.type == bfd_link_hash_undefined
11106 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11107 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11108 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11109 }
11110
721956f4 11111 /* Now build the stub. */
e054468f 11112 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 11113 if (dest >= (bfd_vma) -2)
721956f4
AM
11114 abort ();
11115
33e44f2e 11116 plt = htab->elf.splt;
25f23106
AM
11117 if (!htab->elf.dynamic_sections_created
11118 || stub_entry->h == NULL
11119 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11120 plt = htab->elf.iplt;
e054468f
AM
11121
11122 dest += plt->output_offset + plt->output_section->vma;
11123
11124 if (stub_entry->h == NULL
11125 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11126 {
11127 Elf_Internal_Rela rela;
11128 bfd_byte *rl;
11129
11130 rela.r_offset = dest;
ee67d69a
AM
11131 if (htab->opd_abi)
11132 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11133 else
11134 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
11135 rela.r_addend = (stub_entry->target_value
11136 + stub_entry->target_section->output_offset
11137 + stub_entry->target_section->output_section->vma);
11138
33e44f2e
AM
11139 rl = (htab->elf.irelplt->contents
11140 + (htab->elf.irelplt->reloc_count++
25f23106
AM
11141 * sizeof (Elf64_External_Rela)));
11142 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f 11143 stub_entry->plt_ent->plt.offset |= 1;
82e66161 11144 htab->local_ifunc_resolver = 1;
e054468f 11145 }
176a0d42
AM
11146
11147 off = (dest
06bcf541 11148 - elf_gp (info->output_bfd)
6f20ed8a 11149 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11150
ad8e1ba5 11151 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11152 {
25f53a85 11153 info->callbacks->einfo
695344c0 11154 /* xgettext:c-format */
c1c8c1ef 11155 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11156 stub_entry->h != NULL
11157 ? stub_entry->h->elf.root.root.string
11158 : "<local sym>");
721956f4 11159 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11160 htab->stub_error = TRUE;
11161 return FALSE;
721956f4
AM
11162 }
11163
e7d1c40c 11164 if (htab->params->plt_stub_align != 0)
794e51c0
AM
11165 {
11166 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11167
6f20ed8a
AM
11168 stub_entry->group->stub_sec->size += pad;
11169 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
11170 loc += pad;
11171 }
11172
176a0d42
AM
11173 r = NULL;
11174 if (info->emitrelocations)
11175 {
6f20ed8a 11176 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11177 ((PPC_HA (off) != 0)
11178 + (htab->opd_abi
e7d1c40c 11179 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11180 && PPC_HA (off + 16) == PPC_HA (off))
11181 : 1)));
176a0d42
AM
11182 if (r == NULL)
11183 return FALSE;
6f20ed8a 11184 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11185 if (bfd_big_endian (info->output_bfd))
11186 r[0].r_offset += 2;
176a0d42
AM
11187 r[0].r_addend = dest;
11188 }
a7f2871e
AM
11189 if (stub_entry->h != NULL
11190 && (stub_entry->h == htab->tls_get_addr_fd
11191 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11192 && htab->params->tls_get_addr_opt)
794e51c0 11193 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11194 else
794e51c0 11195 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11196 break;
11197
a4b6fadd
AM
11198 case ppc_stub_save_res:
11199 return TRUE;
11200
721956f4
AM
11201 default:
11202 BFD_FAIL ();
b34976b6 11203 return FALSE;
721956f4
AM
11204 }
11205
9e390558 11206 stub_entry->group->stub_sec->size += p - loc;
97b639ba 11207
e7d1c40c 11208 if (htab->params->emit_stub_syms)
97b639ba
AM
11209 {
11210 struct elf_link_hash_entry *h;
ee75fd95
AM
11211 size_t len1, len2;
11212 char *name;
11213 const char *const stub_str[] = { "long_branch",
11214 "long_branch_r2off",
11215 "plt_branch",
11216 "plt_branch_r2off",
794e51c0 11217 "plt_call",
ee75fd95
AM
11218 "plt_call" };
11219
11220 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11221 len2 = strlen (stub_entry->root.string);
11222 name = bfd_malloc (len1 + len2 + 2);
11223 if (name == NULL)
11224 return FALSE;
11225 memcpy (name, stub_entry->root.string, 9);
11226 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11227 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11228 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11229 if (h == NULL)
11230 return FALSE;
11231 if (h->root.type == bfd_link_hash_new)
11232 {
11233 h->root.type = bfd_link_hash_defined;
6f20ed8a 11234 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11235 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11236 h->ref_regular = 1;
11237 h->def_regular = 1;
11238 h->ref_regular_nonweak = 1;
11239 h->forced_local = 1;
11240 h->non_elf = 0;
2ec55de3 11241 h->root.linker_def = 1;
97b639ba
AM
11242 }
11243 }
11244
b34976b6 11245 return TRUE;
721956f4
AM
11246}
11247
11248/* As above, but don't actually build the stub. Just bump offset so
11249 we know stub section sizes, and select plt_branch stubs where
11250 long_branch stubs won't do. */
11251
b34976b6 11252static bfd_boolean
4ce794b7 11253ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11254{
11255 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11256 struct bfd_link_info *info;
721956f4
AM
11257 struct ppc_link_hash_table *htab;
11258 bfd_vma off;
11259 int size;
11260
11261 /* Massage our args to the form they really have. */
11262 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11263 info = in_arg;
11264
11265 htab = ppc_hash_table (info);
4dfe6ac6
NC
11266 if (htab == NULL)
11267 return FALSE;
721956f4 11268
a4b6fadd
AM
11269 if (stub_entry->h != NULL
11270 && stub_entry->h->save_res
11271 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11272 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11273 {
11274 /* Don't make stubs to out-of-line register save/restore
11275 functions. Instead, emit copies of the functions. */
11276 stub_entry->group->needs_save_res = 1;
11277 stub_entry->stub_type = ppc_stub_save_res;
11278 return TRUE;
11279 }
11280
794e51c0
AM
11281 if (stub_entry->stub_type == ppc_stub_plt_call
11282 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11283 {
e054468f
AM
11284 asection *plt;
11285 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11286 if (off >= (bfd_vma) -2)
411e1bfb 11287 abort ();
33e44f2e 11288 plt = htab->elf.splt;
25f23106
AM
11289 if (!htab->elf.dynamic_sections_created
11290 || stub_entry->h == NULL
11291 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11292 plt = htab->elf.iplt;
e054468f
AM
11293 off += (plt->output_offset
11294 + plt->output_section->vma
06bcf541 11295 - elf_gp (info->output_bfd)
6f20ed8a 11296 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11297
794e51c0 11298 size = plt_stub_size (htab, stub_entry, off);
d4aaa2a0
AM
11299 if (stub_entry->h != NULL
11300 && (stub_entry->h == htab->tls_get_addr_fd
11301 || stub_entry->h == htab->tls_get_addr)
11302 && htab->params->tls_get_addr_opt
11303 && (ALWAYS_EMIT_R2SAVE
11304 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11305 stub_entry->group->tls_get_addr_opt_bctrl
11306 = stub_entry->group->stub_sec->size + size - 5 * 4;
11307
e7d1c40c 11308 if (htab->params->plt_stub_align)
794e51c0 11309 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11310 if (info->emitrelocations)
11311 {
6f20ed8a 11312 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11313 += ((PPC_HA (off) != 0)
11314 + (htab->opd_abi
e7d1c40c 11315 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11316 && PPC_HA (off + 16) == PPC_HA (off))
11317 : 1));
6f20ed8a 11318 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11319 }
721956f4
AM
11320 }
11321 else
11322 {
ad8e1ba5
AM
11323 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11324 variants. */
ac2df442 11325 bfd_vma r2off = 0;
6911b7dc 11326 bfd_vma local_off = 0;
ac2df442 11327
721956f4
AM
11328 off = (stub_entry->target_value
11329 + stub_entry->target_section->output_offset
11330 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11331 off -= (stub_entry->group->stub_sec->size
11332 + stub_entry->group->stub_sec->output_offset
11333 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11334
ad8e1ba5
AM
11335 /* Reset the stub type from the plt variant in case we now
11336 can reach with a shorter stub. */
11337 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11338 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11339
11340 size = 4;
11341 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11342 {
25f53a85 11343 r2off = get_r2off (info, stub_entry);
a7c49797 11344 if (r2off == (bfd_vma) -1)
aa374f67
AM
11345 {
11346 htab->stub_error = TRUE;
11347 return FALSE;
11348 }
a7c49797 11349 size = 8;
ac2df442 11350 if (PPC_HA (r2off) != 0)
a7c49797
AM
11351 size += 4;
11352 if (PPC_LO (r2off) != 0)
11353 size += 4;
ac2df442 11354 off -= size - 4;
ad8e1ba5
AM
11355 }
11356
6911b7dc
AM
11357 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11358
b9e5796b
AM
11359 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11360 Do the same for -R objects without function descriptors. */
11361 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11362 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11363 && r2off == 0
11364 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11365 {
11366 struct ppc_branch_hash_entry *br_entry;
11367
11368 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11369 stub_entry->root.string + 9,
b34976b6 11370 TRUE, FALSE);
721956f4
AM
11371 if (br_entry == NULL)
11372 {
cf97bcb0
AM
11373 _bfd_error_handler (_("can't build branch stub `%s'"),
11374 stub_entry->root.string);
b34976b6
AM
11375 htab->stub_error = TRUE;
11376 return FALSE;
721956f4
AM
11377 }
11378
11379 if (br_entry->iter != htab->stub_iteration)
11380 {
11381 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11382 br_entry->offset = htab->brlt->size;
11383 htab->brlt->size += 8;
63bc6f6c 11384
ee75fd95 11385 if (htab->relbrlt != NULL)
eea6121a 11386 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11387 else if (info->emitrelocations)
11388 {
11389 htab->brlt->reloc_count += 1;
11390 htab->brlt->flags |= SEC_RELOC;
11391 }
721956f4 11392 }
ad8e1ba5
AM
11393
11394 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11395 off = (br_entry->offset
11396 + htab->brlt->output_offset
11397 + htab->brlt->output_section->vma
06bcf541 11398 - elf_gp (info->output_bfd)
6f20ed8a 11399 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11400
176a0d42
AM
11401 if (info->emitrelocations)
11402 {
6f20ed8a
AM
11403 stub_entry->group->stub_sec->reloc_count
11404 += 1 + (PPC_HA (off) != 0);
11405 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11406 }
11407
00f412ee 11408 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11409 {
11410 size = 12;
176a0d42 11411 if (PPC_HA (off) != 0)
ac2df442
AM
11412 size = 16;
11413 }
11414 else
11415 {
00f412ee 11416 size = 16;
176a0d42 11417 if (PPC_HA (off) != 0)
ac2df442
AM
11418 size += 4;
11419
11420 if (PPC_HA (r2off) != 0)
11421 size += 4;
00f412ee
AM
11422 if (PPC_LO (r2off) != 0)
11423 size += 4;
ac2df442 11424 }
721956f4 11425 }
84f5d08e
AM
11426 else if (info->emitrelocations)
11427 {
6f20ed8a
AM
11428 stub_entry->group->stub_sec->reloc_count += 1;
11429 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11430 }
721956f4
AM
11431 }
11432
6f20ed8a 11433 stub_entry->group->stub_sec->size += size;
b34976b6 11434 return TRUE;
721956f4
AM
11435}
11436
11437/* Set up various things so that we can make a list of input sections
11438 for each output section included in the link. Returns -1 on error,
cedb70c5 11439 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11440
11441int
e7d1c40c 11442ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11443{
6f20ed8a 11444 unsigned int id;
721956f4
AM
11445 bfd_size_type amt;
11446 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11447
4dfe6ac6
NC
11448 if (htab == NULL)
11449 return -1;
4c52953f 11450
6f20ed8a
AM
11451 htab->sec_info_arr_size = bfd_get_next_section_id ();
11452 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11453 htab->sec_info = bfd_zmalloc (amt);
11454 if (htab->sec_info == NULL)
721956f4
AM
11455 return -1;
11456
3d6f9012
AM
11457 /* Set toc_off for com, und, abs and ind sections. */
11458 for (id = 0; id < 3; id++)
6f20ed8a 11459 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11460
721956f4
AM
11461 return 1;
11462}
11463
927be08e
AM
11464/* Set up for first pass at multitoc partitioning. */
11465
11466void
11467ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11468{
11469 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11470
1c865ab2 11471 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11472 htab->toc_bfd = NULL;
11473 htab->toc_first_sec = NULL;
11474}
11475
e717da7e
AM
11476/* The linker repeatedly calls this function for each TOC input section
11477 and linker generated GOT section. Group input bfds such that the toc
927be08e 11478 within a group is less than 64k in size. */
ad8e1ba5 11479
927be08e 11480bfd_boolean
4ce794b7 11481ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11482{
11483 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11484 bfd_vma addr, off, limit;
ad8e1ba5 11485
4dfe6ac6
NC
11486 if (htab == NULL)
11487 return FALSE;
11488
927be08e 11489 if (!htab->second_toc_pass)
4c52953f 11490 {
927be08e 11491 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11492 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11493
11494 if (new_bfd)
bf102f86
AM
11495 {
11496 htab->toc_bfd = isec->owner;
11497 htab->toc_first_sec = isec;
11498 }
927be08e 11499
bf102f86
AM
11500 addr = isec->output_offset + isec->output_section->vma;
11501 off = addr - htab->toc_curr;
d77c8a4b
AM
11502 limit = 0x80008000;
11503 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11504 limit = 0x10000;
11505 if (off + isec->size > limit)
bf102f86
AM
11506 {
11507 addr = (htab->toc_first_sec->output_offset
11508 + htab->toc_first_sec->output_section->vma);
11509 htab->toc_curr = addr;
a27e685f 11510 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11511 }
99877b66 11512
927be08e
AM
11513 /* toc_curr is the base address of this toc group. Set elf_gp
11514 for the input section to be the offset relative to the
11515 output toc base plus 0x8000. Making the input elf_gp an
11516 offset allows us to move the toc as a whole without
11517 recalculating input elf_gp. */
06bcf541 11518 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11519 off += TOC_BASE_OFF;
11520
11521 /* Die if someone uses a linker script that doesn't keep input
11522 file .toc and .got together. */
a4fd3de5
AM
11523 if (new_bfd
11524 && elf_gp (isec->owner) != 0
927be08e
AM
11525 && elf_gp (isec->owner) != off)
11526 return FALSE;
11527
11528 elf_gp (isec->owner) = off;
11529 return TRUE;
4c52953f 11530 }
927be08e
AM
11531
11532 /* During the second pass toc_first_sec points to the start of
11533 a toc group, and toc_curr is used to track the old elf_gp.
11534 We use toc_bfd to ensure we only look at each bfd once. */
11535 if (htab->toc_bfd == isec->owner)
11536 return TRUE;
11537 htab->toc_bfd = isec->owner;
11538
11539 if (htab->toc_first_sec == NULL
11540 || htab->toc_curr != elf_gp (isec->owner))
11541 {
11542 htab->toc_curr = elf_gp (isec->owner);
11543 htab->toc_first_sec = isec;
11544 }
11545 addr = (htab->toc_first_sec->output_offset
11546 + htab->toc_first_sec->output_section->vma);
06bcf541 11547 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
11548 elf_gp (isec->owner) = off;
11549
11550 return TRUE;
ad8e1ba5
AM
11551}
11552
927be08e
AM
11553/* Called via elf_link_hash_traverse to merge GOT entries for global
11554 symbol H. */
11555
11556static bfd_boolean
11557merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11558{
11559 if (h->root.type == bfd_link_hash_indirect)
11560 return TRUE;
11561
927be08e
AM
11562 merge_got_entries (&h->got.glist);
11563
11564 return TRUE;
11565}
11566
11567/* Called via elf_link_hash_traverse to allocate GOT entries for global
11568 symbol H. */
11569
11570static bfd_boolean
11571reallocate_got (struct elf_link_hash_entry *h, void *inf)
11572{
11573 struct got_entry *gent;
11574
11575 if (h->root.type == bfd_link_hash_indirect)
11576 return TRUE;
11577
927be08e
AM
11578 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11579 if (!gent->is_indirect)
11580 allocate_got (h, (struct bfd_link_info *) inf, gent);
11581 return TRUE;
11582}
11583
11584/* Called on the first multitoc pass after the last call to
11585 ppc64_elf_next_toc_section. This function removes duplicate GOT
11586 entries. */
11587
11588bfd_boolean
11589ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11590{
11591 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11592 struct bfd *ibfd, *ibfd2;
11593 bfd_boolean done_something;
11594
11595 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11596
7865406b
AM
11597 if (!htab->do_multi_toc)
11598 return FALSE;
11599
d0fae19d 11600 /* Merge global sym got entries within a toc group. */
927be08e
AM
11601 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11602
11603 /* And tlsld_got. */
c72f2fb2 11604 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11605 {
11606 struct got_entry *ent, *ent2;
11607
11608 if (!is_ppc64_elf (ibfd))
11609 continue;
11610
11611 ent = ppc64_tlsld_got (ibfd);
11612 if (!ent->is_indirect
11613 && ent->got.offset != (bfd_vma) -1)
11614 {
c72f2fb2 11615 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11616 {
11617 if (!is_ppc64_elf (ibfd2))
11618 continue;
11619
11620 ent2 = ppc64_tlsld_got (ibfd2);
11621 if (!ent2->is_indirect
11622 && ent2->got.offset != (bfd_vma) -1
11623 && elf_gp (ibfd2) == elf_gp (ibfd))
11624 {
11625 ent2->is_indirect = TRUE;
11626 ent2->got.ent = ent;
11627 }
11628 }
11629 }
11630 }
11631
11632 /* Zap sizes of got sections. */
33e44f2e
AM
11633 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11634 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11635 htab->got_reli_size = 0;
11636
c72f2fb2 11637 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11638 {
11639 asection *got, *relgot;
11640
11641 if (!is_ppc64_elf (ibfd))
11642 continue;
11643
11644 got = ppc64_elf_tdata (ibfd)->got;
11645 if (got != NULL)
11646 {
11647 got->rawsize = got->size;
11648 got->size = 0;
11649 relgot = ppc64_elf_tdata (ibfd)->relgot;
11650 relgot->rawsize = relgot->size;
11651 relgot->size = 0;
11652 }
11653 }
11654
11655 /* Now reallocate the got, local syms first. We don't need to
11656 allocate section contents again since we never increase size. */
c72f2fb2 11657 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11658 {
11659 struct got_entry **lgot_ents;
11660 struct got_entry **end_lgot_ents;
11661 struct plt_entry **local_plt;
11662 struct plt_entry **end_local_plt;
f961d9dd 11663 unsigned char *lgot_masks;
927be08e
AM
11664 bfd_size_type locsymcount;
11665 Elf_Internal_Shdr *symtab_hdr;
19e08130 11666 asection *s;
927be08e
AM
11667
11668 if (!is_ppc64_elf (ibfd))
11669 continue;
11670
11671 lgot_ents = elf_local_got_ents (ibfd);
11672 if (!lgot_ents)
11673 continue;
11674
11675 symtab_hdr = &elf_symtab_hdr (ibfd);
11676 locsymcount = symtab_hdr->sh_info;
11677 end_lgot_ents = lgot_ents + locsymcount;
11678 local_plt = (struct plt_entry **) end_lgot_ents;
11679 end_local_plt = local_plt + locsymcount;
f961d9dd 11680 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11681 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11682 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11683 {
11684 struct got_entry *ent;
11685
11686 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11687 {
19e08130
AM
11688 unsigned int ent_size = 8;
11689 unsigned int rel_size = sizeof (Elf64_External_Rela);
11690
d0fae19d
AM
11691 ent->got.offset = s->size;
11692 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11693 {
19e08130
AM
11694 ent_size *= 2;
11695 rel_size *= 2;
11696 }
11697 s->size += ent_size;
11698 if ((*lgot_masks & PLT_IFUNC) != 0)
11699 {
33e44f2e 11700 htab->elf.irelplt->size += rel_size;
19e08130
AM
11701 htab->got_reli_size += rel_size;
11702 }
0e1862bb 11703 else if (bfd_link_pic (info))
19e08130
AM
11704 {
11705 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11706 srel->size += rel_size;
d0fae19d
AM
11707 }
11708 }
927be08e
AM
11709 }
11710 }
11711
11712 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11713
c72f2fb2 11714 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11715 {
11716 struct got_entry *ent;
11717
11718 if (!is_ppc64_elf (ibfd))
11719 continue;
11720
11721 ent = ppc64_tlsld_got (ibfd);
11722 if (!ent->is_indirect
11723 && ent->got.offset != (bfd_vma) -1)
11724 {
11725 asection *s = ppc64_elf_tdata (ibfd)->got;
11726 ent->got.offset = s->size;
11727 s->size += 16;
0e1862bb 11728 if (bfd_link_pic (info))
927be08e
AM
11729 {
11730 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11731 srel->size += sizeof (Elf64_External_Rela);
11732 }
11733 }
11734 }
11735
33e44f2e 11736 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11737 if (!done_something)
c72f2fb2 11738 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11739 {
11740 asection *got;
11741
11742 if (!is_ppc64_elf (ibfd))
11743 continue;
11744
11745 got = ppc64_elf_tdata (ibfd)->got;
11746 if (got != NULL)
11747 {
11748 done_something = got->rawsize != got->size;
11749 if (done_something)
11750 break;
11751 }
11752 }
11753
11754 if (done_something)
e7d1c40c 11755 (*htab->params->layout_sections_again) ();
927be08e
AM
11756
11757 /* Set up for second pass over toc sections to recalculate elf_gp
11758 on input sections. */
11759 htab->toc_bfd = NULL;
11760 htab->toc_first_sec = NULL;
11761 htab->second_toc_pass = TRUE;
11762 return done_something;
11763}
11764
11765/* Called after second pass of multitoc partitioning. */
11766
11767void
11768ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11769{
11770 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11771
11772 /* After the second pass, toc_curr tracks the TOC offset used
11773 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11774 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11775}
11776
9b5ecbd0
AM
11777/* No toc references were found in ISEC. If the code in ISEC makes no
11778 calls, then there's no need to use toc adjusting stubs when branching
11779 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11780 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11781 needed, and 2 if a cyclical call-graph was found but no other reason
11782 for a stub was detected. If called from the top level, a return of
11783 2 means the same as a return of 0. */
9b5ecbd0
AM
11784
11785static int
4ce794b7 11786toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11787{
9b5ecbd0 11788 int ret;
70cc837d
AM
11789
11790 /* Mark this section as checked. */
11791 isec->call_check_done = 1;
9b5ecbd0 11792
772119ce
AM
11793 /* We know none of our code bearing sections will need toc stubs. */
11794 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11795 return 0;
11796
eea6121a 11797 if (isec->size == 0)
082c50f8
AM
11798 return 0;
11799
4c52953f
AM
11800 if (isec->output_section == NULL)
11801 return 0;
11802
4c52953f 11803 ret = 0;
70cc837d 11804 if (isec->reloc_count != 0)
9b5ecbd0 11805 {
70cc837d
AM
11806 Elf_Internal_Rela *relstart, *rel;
11807 Elf_Internal_Sym *local_syms;
11808 struct ppc_link_hash_table *htab;
2917689a 11809
70cc837d
AM
11810 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11811 info->keep_memory);
11812 if (relstart == NULL)
11813 return -1;
90aecf7a 11814
70cc837d
AM
11815 /* Look for branches to outside of this section. */
11816 local_syms = NULL;
11817 htab = ppc_hash_table (info);
11818 if (htab == NULL)
11819 return -1;
4c52953f 11820
70cc837d 11821 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11822 {
70cc837d
AM
11823 enum elf_ppc64_reloc_type r_type;
11824 unsigned long r_symndx;
11825 struct elf_link_hash_entry *h;
11826 struct ppc_link_hash_entry *eh;
11827 Elf_Internal_Sym *sym;
11828 asection *sym_sec;
11829 struct _opd_sec_data *opd;
11830 bfd_vma sym_value;
11831 bfd_vma dest;
11832
11833 r_type = ELF64_R_TYPE (rel->r_info);
11834 if (r_type != R_PPC64_REL24
11835 && r_type != R_PPC64_REL14
11836 && r_type != R_PPC64_REL14_BRTAKEN
11837 && r_type != R_PPC64_REL14_BRNTAKEN)
11838 continue;
4c52953f 11839
70cc837d
AM
11840 r_symndx = ELF64_R_SYM (rel->r_info);
11841 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11842 isec->owner))
4c52953f 11843 {
70cc837d
AM
11844 ret = -1;
11845 break;
11846 }
4c52953f 11847
70cc837d
AM
11848 /* Calls to dynamic lib functions go through a plt call stub
11849 that uses r2. */
11850 eh = (struct ppc_link_hash_entry *) h;
11851 if (eh != NULL
11852 && (eh->elf.plt.plist != NULL
11853 || (eh->oh != NULL
11854 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11855 {
11856 ret = 1;
11857 break;
4c52953f
AM
11858 }
11859
70cc837d
AM
11860 if (sym_sec == NULL)
11861 /* Ignore other undefined symbols. */
4c52953f 11862 continue;
4c52953f 11863
70cc837d
AM
11864 /* Assume branches to other sections not included in the
11865 link need stubs too, to cover -R and absolute syms. */
11866 if (sym_sec->output_section == NULL)
11867 {
11868 ret = 1;
11869 break;
11870 }
4c52953f 11871
70cc837d
AM
11872 if (h == NULL)
11873 sym_value = sym->st_value;
11874 else
11875 {
11876 if (h->root.type != bfd_link_hash_defined
11877 && h->root.type != bfd_link_hash_defweak)
11878 abort ();
11879 sym_value = h->root.u.def.value;
11880 }
11881 sym_value += rel->r_addend;
4c52953f 11882
70cc837d
AM
11883 /* If this branch reloc uses an opd sym, find the code section. */
11884 opd = get_opd_info (sym_sec);
11885 if (opd != NULL)
11886 {
11887 if (h == NULL && opd->adjust != NULL)
11888 {
11889 long adjust;
4c52953f 11890
92a9c616 11891 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11892 if (adjust == -1)
11893 /* Assume deleted functions won't ever be called. */
11894 continue;
11895 sym_value += adjust;
11896 }
4c52953f 11897
aef36ac1
AM
11898 dest = opd_entry_value (sym_sec, sym_value,
11899 &sym_sec, NULL, FALSE);
70cc837d
AM
11900 if (dest == (bfd_vma) -1)
11901 continue;
11902 }
11903 else
11904 dest = (sym_value
11905 + sym_sec->output_offset
11906 + sym_sec->output_section->vma);
4c52953f 11907
70cc837d
AM
11908 /* Ignore branch to self. */
11909 if (sym_sec == isec)
11910 continue;
4c52953f 11911
70cc837d
AM
11912 /* If the called function uses the toc, we need a stub. */
11913 if (sym_sec->has_toc_reloc
11914 || sym_sec->makes_toc_func_call)
4c52953f 11915 {
70cc837d 11916 ret = 1;
4c52953f
AM
11917 break;
11918 }
70cc837d
AM
11919
11920 /* Assume any branch that needs a long branch stub might in fact
11921 need a plt_branch stub. A plt_branch stub uses r2. */
11922 else if (dest - (isec->output_offset
11923 + isec->output_section->vma
6911b7dc
AM
11924 + rel->r_offset) + (1 << 25)
11925 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11926 ? h->other
11927 : sym->st_other))
4c52953f 11928 {
70cc837d
AM
11929 ret = 1;
11930 break;
11931 }
11932
11933 /* If calling back to a section in the process of being
11934 tested, we can't say for sure that no toc adjusting stubs
11935 are needed, so don't return zero. */
11936 else if (sym_sec->call_check_in_progress)
11937 ret = 2;
11938
11939 /* Branches to another section that itself doesn't have any TOC
11940 references are OK. Recursively call ourselves to check. */
11941 else if (!sym_sec->call_check_done)
11942 {
11943 int recur;
11944
11945 /* Mark current section as indeterminate, so that other
11946 sections that call back to current won't be marked as
11947 known. */
11948 isec->call_check_in_progress = 1;
11949 recur = toc_adjusting_stub_needed (info, sym_sec);
11950 isec->call_check_in_progress = 0;
11951
4c52953f
AM
11952 if (recur != 0)
11953 {
70cc837d
AM
11954 ret = recur;
11955 if (recur != 2)
11956 break;
4c52953f
AM
11957 }
11958 }
4c52953f 11959 }
70cc837d
AM
11960
11961 if (local_syms != NULL
11962 && (elf_symtab_hdr (isec->owner).contents
11963 != (unsigned char *) local_syms))
11964 free (local_syms);
11965 if (elf_section_data (isec)->relocs != relstart)
11966 free (relstart);
9b5ecbd0
AM
11967 }
11968
70cc837d
AM
11969 if ((ret & 1) == 0
11970 && isec->map_head.s != NULL
11971 && (strcmp (isec->output_section->name, ".init") == 0
11972 || strcmp (isec->output_section->name, ".fini") == 0))
11973 {
11974 if (isec->map_head.s->has_toc_reloc
11975 || isec->map_head.s->makes_toc_func_call)
11976 ret = 1;
11977 else if (!isec->map_head.s->call_check_done)
11978 {
11979 int recur;
11980 isec->call_check_in_progress = 1;
11981 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11982 isec->call_check_in_progress = 0;
11983 if (recur != 0)
11984 ret = recur;
11985 }
11986 }
11987
11988 if (ret == 1)
11989 isec->makes_toc_func_call = 1;
4c52953f 11990
9b5ecbd0
AM
11991 return ret;
11992}
11993
721956f4
AM
11994/* The linker repeatedly calls this function for each input section,
11995 in the order that input sections are linked into output sections.
11996 Build lists of input sections to determine groupings between which
11997 we may insert linker stubs. */
11998
9b5ecbd0 11999bfd_boolean
4ce794b7 12000ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12001{
12002 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12003
4dfe6ac6
NC
12004 if (htab == NULL)
12005 return FALSE;
12006
734b6cf9 12007 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12008 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12009 {
3d6f9012
AM
12010 /* This happens to make the list in reverse order,
12011 which is what we want. */
6f20ed8a
AM
12012 htab->sec_info[isec->id].u.list
12013 = htab->sec_info[isec->output_section->id].u.list;
12014 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12015 }
ad8e1ba5 12016
4c52953f 12017 if (htab->multi_toc_needed)
9b5ecbd0 12018 {
8b974ba3
AM
12019 /* Analyse sections that aren't already flagged as needing a
12020 valid toc pointer. Exclude .fixup for the linux kernel.
12021 .fixup contains branches, but only back to the function that
12022 hit an exception. */
12023 if (!(isec->has_toc_reloc
12024 || (isec->flags & SEC_CODE) == 0
12025 || strcmp (isec->name, ".fixup") == 0
12026 || isec->call_check_done))
12027 {
12028 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12029 return FALSE;
8b974ba3
AM
12030 }
12031 /* Make all sections use the TOC assigned for this object file.
12032 This will be wrong for pasted sections; We fix that in
12033 check_pasted_section(). */
12034 if (elf_gp (isec->owner) != 0)
12035 htab->toc_curr = elf_gp (isec->owner);
12036 }
12037
6f20ed8a 12038 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12039 return TRUE;
721956f4
AM
12040}
12041
70cc837d
AM
12042/* Check that all .init and .fini sections use the same toc, if they
12043 have toc relocs. */
12044
12045static bfd_boolean
12046check_pasted_section (struct bfd_link_info *info, const char *name)
12047{
12048 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12049
12050 if (o != NULL)
12051 {
12052 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12053 bfd_vma toc_off = 0;
12054 asection *i;
12055
12056 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12057 if (i->has_toc_reloc)
12058 {
12059 if (toc_off == 0)
6f20ed8a
AM
12060 toc_off = htab->sec_info[i->id].toc_off;
12061 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12062 return FALSE;
12063 }
6683a28d
AM
12064
12065 if (toc_off == 0)
12066 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12067 if (i->makes_toc_func_call)
12068 {
6f20ed8a 12069 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12070 break;
12071 }
12072
70cc837d
AM
12073 /* Make sure the whole pasted function uses the same toc offset. */
12074 if (toc_off != 0)
12075 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12076 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12077 }
12078 return TRUE;
12079}
12080
12081bfd_boolean
12082ppc64_elf_check_init_fini (struct bfd_link_info *info)
12083{
12084 return (check_pasted_section (info, ".init")
12085 & check_pasted_section (info, ".fini"));
12086}
12087
721956f4
AM
12088/* See whether we can group stub sections together. Grouping stub
12089 sections may result in fewer stubs. More importantly, we need to
12090 put all .init* and .fini* stubs at the beginning of the .init or
12091 .fini output sections respectively, because glibc splits the
12092 _init and _fini functions into multiple parts. Putting a stub in
12093 the middle of a function is not a good idea. */
12094
6f20ed8a
AM
12095static bfd_boolean
12096group_sections (struct bfd_link_info *info,
4ce794b7
AM
12097 bfd_size_type stub_group_size,
12098 bfd_boolean stubs_always_before_branch)
721956f4 12099{
6f20ed8a
AM
12100 struct ppc_link_hash_table *htab;
12101 asection *osec;
7c8fe5c4
AM
12102 bfd_boolean suppress_size_errors;
12103
6f20ed8a
AM
12104 htab = ppc_hash_table (info);
12105 if (htab == NULL)
12106 return FALSE;
12107
7c8fe5c4 12108 suppress_size_errors = FALSE;
7c8fe5c4
AM
12109 if (stub_group_size == 1)
12110 {
12111 /* Default values. */
12112 if (stubs_always_before_branch)
09f92717 12113 stub_group_size = 0x1e00000;
7c8fe5c4 12114 else
09f92717 12115 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12116 suppress_size_errors = TRUE;
12117 }
12118
6f20ed8a 12119 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12120 {
6f20ed8a
AM
12121 asection *tail;
12122
12123 if (osec->id >= htab->sec_info_arr_size)
12124 continue;
12125
12126 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12127 while (tail != NULL)
721956f4 12128 {
734b6cf9
AM
12129 asection *curr;
12130 asection *prev;
12131 bfd_size_type total;
12132 bfd_boolean big_sec;
12133 bfd_vma curr_toc;
6f20ed8a 12134 struct map_stub *group;
09f92717 12135 bfd_size_type group_size;
734b6cf9
AM
12136
12137 curr = tail;
eea6121a 12138 total = tail->size;
09f92717
AM
12139 group_size = (ppc64_elf_section_data (tail) != NULL
12140 && ppc64_elf_section_data (tail)->has_14bit_branch
12141 ? stub_group_size >> 10 : stub_group_size);
12142
12143 big_sec = total > group_size;
7c8fe5c4 12144 if (big_sec && !suppress_size_errors)
695344c0 12145 /* xgettext:c-format */
871b3ab2 12146 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12147 tail->owner, tail);
6f20ed8a 12148 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12149
6f20ed8a 12150 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12151 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12152 < (ppc64_elf_section_data (prev) != NULL
12153 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12154 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12155 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12156 curr = prev;
12157
12158 /* OK, the size from the start of CURR to the end is less
09f92717 12159 than group_size and thus can be handled by one stub
734b6cf9 12160 section. (or the tail section is itself larger than
09f92717
AM
12161 group_size, in which case we may be toast.) We should
12162 really be keeping track of the total size of stubs added
12163 here, as stubs contribute to the final output section
12164 size. That's a little tricky, and this way will only
12165 break if stubs added make the total size more than 2^25,
12166 ie. for the default stub_group_size, if stubs total more
12167 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12168 group = bfd_alloc (curr->owner, sizeof (*group));
12169 if (group == NULL)
12170 return FALSE;
12171 group->link_sec = curr;
12172 group->stub_sec = NULL;
a4b6fadd 12173 group->needs_save_res = 0;
d4aaa2a0 12174 group->tls_get_addr_opt_bctrl = -1u;
a4b6fadd
AM
12175 group->next = htab->group;
12176 htab->group = group;
734b6cf9 12177 do
721956f4 12178 {
6f20ed8a 12179 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12180 /* Set up this stub group. */
6f20ed8a 12181 htab->sec_info[tail->id].u.group = group;
721956f4 12182 }
734b6cf9
AM
12183 while (tail != curr && (tail = prev) != NULL);
12184
09f92717 12185 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12186 bytes before the stub section can be handled by it too.
12187 Don't do this if we have a really large section after the
12188 stubs, as adding more stubs increases the chance that
12189 branches may not reach into the stub section. */
12190 if (!stubs_always_before_branch && !big_sec)
12191 {
12192 total = 0;
12193 while (prev != NULL
12194 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12195 < (ppc64_elf_section_data (prev) != NULL
12196 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12197 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12198 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12199 {
12200 tail = prev;
6f20ed8a
AM
12201 prev = htab->sec_info[tail->id].u.list;
12202 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12203 }
12204 }
12205 tail = prev;
721956f4
AM
12206 }
12207 }
6f20ed8a 12208 return TRUE;
721956f4
AM
12209}
12210
58d180e8
AM
12211static const unsigned char glink_eh_frame_cie[] =
12212{
12213 0, 0, 0, 16, /* length. */
12214 0, 0, 0, 0, /* id. */
12215 1, /* CIE version. */
12216 'z', 'R', 0, /* Augmentation string. */
12217 4, /* Code alignment. */
12218 0x78, /* Data alignment. */
12219 65, /* RA reg. */
12220 1, /* Augmentation size. */
12221 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12222 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12223};
12224
d4aaa2a0
AM
12225static size_t
12226stub_eh_frame_size (struct map_stub *group, size_t align)
12227{
12228 size_t this_size = 17;
12229 if (group->tls_get_addr_opt_bctrl != -1u)
12230 {
12231 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12232 if (to_bctrl < 64)
12233 this_size += 1;
12234 else if (to_bctrl < 256)
12235 this_size += 2;
12236 else if (to_bctrl < 65536)
12237 this_size += 3;
12238 else
12239 this_size += 5;
12240 this_size += 6;
12241 }
12242 this_size = (this_size + align - 1) & -align;
12243 return this_size;
12244}
12245
d969d15f
AM
12246/* Stripping output sections is normally done before dynamic section
12247 symbols have been allocated. This function is called later, and
12248 handles cases like htab->brlt which is mapped to its own output
12249 section. */
12250
12251static void
12252maybe_strip_output (struct bfd_link_info *info, asection *isec)
12253{
12254 if (isec->size == 0
12255 && isec->output_section->size == 0
53d8967a 12256 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12257 && !bfd_section_removed_from_list (info->output_bfd,
12258 isec->output_section)
12259 && elf_section_data (isec->output_section)->dynindx == 0)
12260 {
12261 isec->output_section->flags |= SEC_EXCLUDE;
12262 bfd_section_list_remove (info->output_bfd, isec->output_section);
12263 info->output_bfd->section_count--;
12264 }
12265}
12266
721956f4
AM
12267/* Determine and set the size of the stub section for a final link.
12268
12269 The basic idea here is to examine all the relocations looking for
12270 PC-relative calls to a target that is unreachable with a "bl"
12271 instruction. */
12272
b34976b6 12273bfd_boolean
e7d1c40c 12274ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12275{
12276 bfd_size_type stub_group_size;
b34976b6 12277 bfd_boolean stubs_always_before_branch;
721956f4
AM
12278 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12279
4dfe6ac6
NC
12280 if (htab == NULL)
12281 return FALSE;
12282
0e1862bb 12283 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12284 htab->params->plt_thread_safe = 1;
b9e5796b 12285 if (!htab->opd_abi)
e7d1c40c
AM
12286 htab->params->plt_thread_safe = 0;
12287 else if (htab->params->plt_thread_safe == -1)
794e51c0 12288 {
e2458743 12289 static const char *const thread_starter[] =
794e51c0
AM
12290 {
12291 "pthread_create",
12292 /* libstdc++ */
12293 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12294 /* librt */
12295 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12296 "mq_notify", "create_timer",
12297 /* libanl */
12298 "getaddrinfo_a",
12299 /* libgomp */
2300b5a1 12300 "GOMP_parallel",
794e51c0 12301 "GOMP_parallel_start",
2300b5a1 12302 "GOMP_parallel_loop_static",
794e51c0 12303 "GOMP_parallel_loop_static_start",
2300b5a1 12304 "GOMP_parallel_loop_dynamic",
794e51c0 12305 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12306 "GOMP_parallel_loop_guided",
794e51c0 12307 "GOMP_parallel_loop_guided_start",
2300b5a1 12308 "GOMP_parallel_loop_runtime",
794e51c0 12309 "GOMP_parallel_loop_runtime_start",
2300b5a1 12310 "GOMP_parallel_sections",
68ffbac6 12311 "GOMP_parallel_sections_start",
f9dffbf0
AM
12312 /* libgo */
12313 "__go_go",
794e51c0
AM
12314 };
12315 unsigned i;
12316
a4b6fadd 12317 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12318 {
12319 struct elf_link_hash_entry *h;
12320 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12321 FALSE, FALSE, TRUE);
e7d1c40c
AM
12322 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12323 if (htab->params->plt_thread_safe)
794e51c0
AM
12324 break;
12325 }
12326 }
e7d1c40c
AM
12327 stubs_always_before_branch = htab->params->group_size < 0;
12328 if (htab->params->group_size < 0)
12329 stub_group_size = -htab->params->group_size;
721956f4 12330 else
e7d1c40c 12331 stub_group_size = htab->params->group_size;
721956f4 12332
6f20ed8a
AM
12333 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12334 return FALSE;
721956f4 12335
c9301e31
AM
12336#define STUB_SHRINK_ITER 20
12337 /* Loop until no stubs added. After iteration 20 of this loop we may
12338 exit on a stub section shrinking. This is to break out of a
12339 pathological case where adding stubs on one iteration decreases
12340 section gaps (perhaps due to alignment), which then requires
12341 fewer or smaller stubs on the next iteration. */
12342
721956f4
AM
12343 while (1)
12344 {
12345 bfd *input_bfd;
12346 unsigned int bfd_indx;
a4b6fadd 12347 struct map_stub *group;
721956f4
AM
12348
12349 htab->stub_iteration += 1;
721956f4
AM
12350
12351 for (input_bfd = info->input_bfds, bfd_indx = 0;
12352 input_bfd != NULL;
c72f2fb2 12353 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12354 {
12355 Elf_Internal_Shdr *symtab_hdr;
12356 asection *section;
6cdc0ccc 12357 Elf_Internal_Sym *local_syms = NULL;
721956f4 12358
0c8d6e5c 12359 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12360 continue;
12361
721956f4 12362 /* We'll need the symbol table in a second. */
0ffa91dd 12363 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12364 if (symtab_hdr->sh_info == 0)
12365 continue;
12366
721956f4
AM
12367 /* Walk over each section attached to the input bfd. */
12368 for (section = input_bfd->sections;
12369 section != NULL;
12370 section = section->next)
12371 {
721956f4 12372 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12373
12374 /* If there aren't any relocs, then there's nothing more
12375 to do. */
12376 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12377 || (section->flags & SEC_ALLOC) == 0
12378 || (section->flags & SEC_LOAD) == 0
12379 || (section->flags & SEC_CODE) == 0
721956f4
AM
12380 || section->reloc_count == 0)
12381 continue;
12382
12383 /* If this section is a link-once section that will be
12384 discarded, then don't create any stubs. */
12385 if (section->output_section == NULL
927be08e 12386 || section->output_section->owner != info->output_bfd)
721956f4
AM
12387 continue;
12388
1e2f5b6e
AM
12389 /* Get the relocs. */
12390 internal_relocs
4ce794b7 12391 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12392 info->keep_memory);
721956f4 12393 if (internal_relocs == NULL)
1e2f5b6e 12394 goto error_ret_free_local;
721956f4
AM
12395
12396 /* Now examine each relocation. */
12397 irela = internal_relocs;
12398 irelaend = irela + section->reloc_count;
12399 for (; irela < irelaend; irela++)
12400 {
4ce794b7
AM
12401 enum elf_ppc64_reloc_type r_type;
12402 unsigned int r_indx;
721956f4
AM
12403 enum ppc_stub_type stub_type;
12404 struct ppc_stub_hash_entry *stub_entry;
8387904d 12405 asection *sym_sec, *code_sec;
e054468f 12406 bfd_vma sym_value, code_value;
721956f4 12407 bfd_vma destination;
6911b7dc 12408 unsigned long local_off;
8843416a 12409 bfd_boolean ok_dest;
721956f4 12410 struct ppc_link_hash_entry *hash;
8387904d 12411 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12412 struct elf_link_hash_entry *h;
12413 Elf_Internal_Sym *sym;
721956f4
AM
12414 char *stub_name;
12415 const asection *id_sec;
74f0fb50 12416 struct _opd_sec_data *opd;
e054468f 12417 struct plt_entry *plt_ent;
721956f4
AM
12418
12419 r_type = ELF64_R_TYPE (irela->r_info);
12420 r_indx = ELF64_R_SYM (irela->r_info);
12421
4ce794b7 12422 if (r_type >= R_PPC64_max)
721956f4
AM
12423 {
12424 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12425 goto error_ret_free_internal;
721956f4
AM
12426 }
12427
12428 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12429 if (r_type != R_PPC64_REL24
12430 && r_type != R_PPC64_REL14
12431 && r_type != R_PPC64_REL14_BRTAKEN
12432 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12433 continue;
12434
12435 /* Now determine the call target, its name, value,
12436 section. */
411e1bfb
AM
12437 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12438 r_indx, input_bfd))
12439 goto error_ret_free_internal;
12440 hash = (struct ppc_link_hash_entry *) h;
12441
8843416a 12442 ok_dest = FALSE;
8387904d 12443 fdh = NULL;
7fe2b9a6 12444 sym_value = 0;
411e1bfb 12445 if (hash == NULL)
721956f4 12446 {
411e1bfb 12447 sym_value = sym->st_value;
c27b8c2a
AM
12448 if (sym_sec != NULL
12449 && sym_sec->output_section != NULL)
12450 ok_dest = TRUE;
721956f4 12451 }
7fe2b9a6
AM
12452 else if (hash->elf.root.type == bfd_link_hash_defined
12453 || hash->elf.root.type == bfd_link_hash_defweak)
12454 {
12455 sym_value = hash->elf.root.u.def.value;
12456 if (sym_sec->output_section != NULL)
12457 ok_dest = TRUE;
12458 }
12459 else if (hash->elf.root.type == bfd_link_hash_undefweak
12460 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12461 {
99877b66 12462 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12463 use the func descriptor sym instead if it is
12464 defined. */
ceb1f1ef 12465 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12466 && hash->oh != NULL)
8387904d 12467 {
8c5b4e52 12468 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12469 if (fdh->elf.root.type == bfd_link_hash_defined
12470 || fdh->elf.root.type == bfd_link_hash_defweak)
12471 {
12472 sym_sec = fdh->elf.root.u.def.section;
12473 sym_value = fdh->elf.root.u.def.value;
12474 if (sym_sec->output_section != NULL)
12475 ok_dest = TRUE;
12476 }
99877b66
AM
12477 else
12478 fdh = NULL;
8387904d 12479 }
7fe2b9a6
AM
12480 }
12481 else
12482 {
12483 bfd_set_error (bfd_error_bad_value);
12484 goto error_ret_free_internal;
721956f4
AM
12485 }
12486
8843416a 12487 destination = 0;
6911b7dc 12488 local_off = 0;
8843416a
AM
12489 if (ok_dest)
12490 {
12491 sym_value += irela->r_addend;
12492 destination = (sym_value
12493 + sym_sec->output_offset
12494 + sym_sec->output_section->vma);
6911b7dc
AM
12495 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12496 ? hash->elf.other
12497 : sym->st_other);
8843416a
AM
12498 }
12499
8387904d 12500 code_sec = sym_sec;
e054468f 12501 code_value = sym_value;
74f0fb50
AM
12502 opd = get_opd_info (sym_sec);
12503 if (opd != NULL)
8387904d
AM
12504 {
12505 bfd_vma dest;
12506
74f0fb50 12507 if (hash == NULL && opd->adjust != NULL)
8387904d 12508 {
51aecdc5 12509 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12510 if (adjust == -1)
12511 continue;
e054468f 12512 code_value += adjust;
8387904d
AM
12513 sym_value += adjust;
12514 }
12515 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12516 &code_sec, &code_value, FALSE);
8387904d
AM
12517 if (dest != (bfd_vma) -1)
12518 {
12519 destination = dest;
12520 if (fdh != NULL)
12521 {
12522 /* Fixup old ABI sym to point at code
12523 entry. */
99877b66 12524 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12525 hash->elf.root.u.def.section = code_sec;
e054468f 12526 hash->elf.root.u.def.value = code_value;
8387904d
AM
12527 }
12528 }
12529 }
12530
721956f4 12531 /* Determine what (if any) linker stub is needed. */
e054468f 12532 plt_ent = NULL;
721956f4 12533 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12534 &plt_ent, destination,
12535 local_off);
ad8e1ba5
AM
12536
12537 if (stub_type != ppc_stub_plt_call)
12538 {
12539 /* Check whether we need a TOC adjusting stub.
12540 Since the linker pastes together pieces from
12541 different object files when creating the
12542 _init and _fini functions, it may be that a
12543 call to what looks like a local sym is in
12544 fact a call needing a TOC adjustment. */
8387904d
AM
12545 if (code_sec != NULL
12546 && code_sec->output_section != NULL
6f20ed8a
AM
12547 && (htab->sec_info[code_sec->id].toc_off
12548 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12549 && (code_sec->has_toc_reloc
12550 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12551 stub_type = ppc_stub_long_branch_r2off;
12552 }
12553
721956f4
AM
12554 if (stub_type == ppc_stub_none)
12555 continue;
12556
411e1bfb
AM
12557 /* __tls_get_addr calls might be eliminated. */
12558 if (stub_type != ppc_stub_plt_call
12559 && hash != NULL
8387904d
AM
12560 && (hash == htab->tls_get_addr
12561 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12562 && section->has_tls_reloc
12563 && irela != internal_relocs)
12564 {
12565 /* Get tls info. */
f961d9dd 12566 unsigned char *tls_mask;
411e1bfb 12567
3a71aa26 12568 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12569 irela - 1, input_bfd))
12570 goto error_ret_free_internal;
e7b938ca 12571 if (*tls_mask != 0)
411e1bfb
AM
12572 continue;
12573 }
12574
f378ab09 12575 if (stub_type == ppc_stub_plt_call)
794e51c0 12576 {
6e1816be
AM
12577 if (!htab->opd_abi
12578 && htab->params->plt_localentry0 != 0
12579 && is_elfv2_localentry0 (&hash->elf))
12580 htab->has_plt_localentry0 = 1;
12581 else if (irela + 1 < irelaend
12582 && irela[1].r_offset == irela->r_offset + 4
12583 && (ELF64_R_TYPE (irela[1].r_info)
12584 == R_PPC64_TOCSAVE))
f378ab09
AM
12585 {
12586 if (!tocsave_find (htab, INSERT,
12587 &local_syms, irela + 1, input_bfd))
12588 goto error_ret_free_internal;
12589 }
f378ab09
AM
12590 else
12591 stub_type = ppc_stub_plt_call_r2save;
794e51c0 12592 }
3b421ab3 12593
721956f4 12594 /* Support for grouping stub sections. */
6f20ed8a 12595 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12596
12597 /* Get the name of this stub. */
12598 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12599 if (!stub_name)
12600 goto error_ret_free_internal;
12601
12602 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12603 stub_name, FALSE, FALSE);
721956f4
AM
12604 if (stub_entry != NULL)
12605 {
12606 /* The proper stub has already been created. */
12607 free (stub_name);
794e51c0
AM
12608 if (stub_type == ppc_stub_plt_call_r2save)
12609 stub_entry->stub_type = stub_type;
721956f4
AM
12610 continue;
12611 }
12612
25f53a85 12613 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12614 if (stub_entry == NULL)
12615 {
12616 free (stub_name);
6cdc0ccc
AM
12617 error_ret_free_internal:
12618 if (elf_section_data (section)->relocs == NULL)
12619 free (internal_relocs);
12620 error_ret_free_local:
12621 if (local_syms != NULL
12622 && (symtab_hdr->contents
12623 != (unsigned char *) local_syms))
12624 free (local_syms);
b34976b6 12625 return FALSE;
721956f4
AM
12626 }
12627
ad8e1ba5 12628 stub_entry->stub_type = stub_type;
794e51c0
AM
12629 if (stub_type != ppc_stub_plt_call
12630 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12631 {
12632 stub_entry->target_value = code_value;
12633 stub_entry->target_section = code_sec;
12634 }
12635 else
12636 {
12637 stub_entry->target_value = sym_value;
12638 stub_entry->target_section = sym_sec;
12639 }
721956f4 12640 stub_entry->h = hash;
e054468f 12641 stub_entry->plt_ent = plt_ent;
6911b7dc 12642 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12643
12644 if (stub_entry->h != NULL)
12645 htab->stub_globals += 1;
721956f4
AM
12646 }
12647
12648 /* We're done with the internal relocs, free them. */
6cdc0ccc 12649 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12650 free (internal_relocs);
721956f4 12651 }
6cdc0ccc
AM
12652
12653 if (local_syms != NULL
12654 && symtab_hdr->contents != (unsigned char *) local_syms)
12655 {
12656 if (!info->keep_memory)
12657 free (local_syms);
12658 else
12659 symtab_hdr->contents = (unsigned char *) local_syms;
12660 }
721956f4
AM
12661 }
12662
5c3dead3 12663 /* We may have added some stubs. Find out the new size of the
721956f4 12664 stub sections. */
d4aaa2a0
AM
12665 for (group = htab->group; group != NULL; group = group->next)
12666 if (group->stub_sec != NULL)
ee75fd95 12667 {
d4aaa2a0
AM
12668 asection *stub_sec = group->stub_sec;
12669
ea3d7d1c
AM
12670 if (htab->stub_iteration <= STUB_SHRINK_ITER
12671 || stub_sec->rawsize < stub_sec->size)
12672 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12673 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12674 stub_sec->size = 0;
12675 stub_sec->reloc_count = 0;
84f5d08e 12676 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12677 }
eea6121a 12678
ba21f564
AM
12679 if (htab->stub_iteration <= STUB_SHRINK_ITER
12680 || htab->brlt->rawsize < htab->brlt->size)
12681 htab->brlt->rawsize = htab->brlt->size;
eea6121a 12682 htab->brlt->size = 0;
84f5d08e
AM
12683 htab->brlt->reloc_count = 0;
12684 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12685 if (htab->relbrlt != NULL)
eea6121a 12686 htab->relbrlt->size = 0;
721956f4 12687
63bc6f6c 12688 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12689
a4b6fadd
AM
12690 for (group = htab->group; group != NULL; group = group->next)
12691 if (group->needs_save_res)
12692 group->stub_sec->size += htab->sfpr->size;
12693
176a0d42
AM
12694 if (info->emitrelocations
12695 && htab->glink != NULL && htab->glink->size != 0)
12696 {
12697 htab->glink->reloc_count = 1;
12698 htab->glink->flags |= SEC_RELOC;
12699 }
12700
58d180e8
AM
12701 if (htab->glink_eh_frame != NULL
12702 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 12703 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 12704 {
2e0ce1c8 12705 size_t size = 0, align = 4;
58d180e8 12706
d4aaa2a0
AM
12707 for (group = htab->group; group != NULL; group = group->next)
12708 if (group->stub_sec != NULL)
12709 size += stub_eh_frame_size (group, align);
58d180e8 12710 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 12711 size += (24 + align - 1) & -align;
58d180e8 12712 if (size != 0)
2e0ce1c8
AM
12713 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12714 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12715 size = (size + align - 1) & -align;
58d180e8
AM
12716 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12717 htab->glink_eh_frame->size = size;
12718 }
12719
e7d1c40c 12720 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
12721 for (group = htab->group; group != NULL; group = group->next)
12722 if (group->stub_sec != NULL)
691d2e9a
AM
12723 {
12724 int align = abs (htab->params->plt_stub_align);
12725 group->stub_sec->size
12726 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12727 }
d4aaa2a0
AM
12728
12729 for (group = htab->group; group != NULL; group = group->next)
12730 if (group->stub_sec != NULL
12731 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 12732 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 12733 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
12734 break;
12735
d4aaa2a0 12736 if (group == NULL
ba21f564
AM
12737 && (htab->brlt->rawsize == htab->brlt->size
12738 || (htab->stub_iteration > STUB_SHRINK_ITER
12739 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
12740 && (htab->glink_eh_frame == NULL
12741 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12742 break;
12743
721956f4 12744 /* Ask the linker to do its stuff. */
e7d1c40c 12745 (*htab->params->layout_sections_again) ();
721956f4
AM
12746 }
12747
da44f4e5
AM
12748 if (htab->glink_eh_frame != NULL
12749 && htab->glink_eh_frame->size != 0)
12750 {
12751 bfd_vma val;
12752 bfd_byte *p, *last_fde;
12753 size_t last_fde_len, size, align, pad;
d4aaa2a0 12754 struct map_stub *group;
da44f4e5
AM
12755
12756 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12757 if (p == NULL)
12758 return FALSE;
12759 htab->glink_eh_frame->contents = p;
12760 last_fde = p;
2e0ce1c8 12761 align = 4;
da44f4e5
AM
12762
12763 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12764 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 12765 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 12766 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 12767 p += last_fde_len + 4;
da44f4e5 12768
d4aaa2a0
AM
12769 for (group = htab->group; group != NULL; group = group->next)
12770 if (group->stub_sec != NULL)
da44f4e5
AM
12771 {
12772 last_fde = p;
d4aaa2a0 12773 last_fde_len = stub_eh_frame_size (group, align) - 4;
da44f4e5 12774 /* FDE length. */
2e0ce1c8 12775 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12776 p += 4;
12777 /* CIE pointer. */
12778 val = p - htab->glink_eh_frame->contents;
12779 bfd_put_32 (htab->elf.dynobj, val, p);
12780 p += 4;
12781 /* Offset to stub section, written later. */
12782 p += 4;
12783 /* stub section size. */
d4aaa2a0 12784 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
12785 p += 4;
12786 /* Augmentation. */
12787 p += 1;
d4aaa2a0
AM
12788 if (group->tls_get_addr_opt_bctrl != -1u)
12789 {
12790 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12791
12792 /* This FDE needs more than just the default.
12793 Describe __tls_get_addr_opt stub LR. */
12794 if (to_bctrl < 64)
12795 *p++ = DW_CFA_advance_loc + to_bctrl;
12796 else if (to_bctrl < 256)
12797 {
12798 *p++ = DW_CFA_advance_loc1;
12799 *p++ = to_bctrl;
12800 }
12801 else if (to_bctrl < 65536)
12802 {
12803 *p++ = DW_CFA_advance_loc2;
12804 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
12805 p += 2;
12806 }
12807 else
12808 {
12809 *p++ = DW_CFA_advance_loc4;
12810 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
12811 p += 4;
12812 }
12813 *p++ = DW_CFA_offset_extended_sf;
12814 *p++ = 65;
12815 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
12816 *p++ = DW_CFA_advance_loc + 4;
12817 *p++ = DW_CFA_restore_extended;
12818 *p++ = 65;
12819 }
da44f4e5 12820 /* Pad. */
d4aaa2a0 12821 p = last_fde + last_fde_len + 4;
da44f4e5
AM
12822 }
12823 if (htab->glink != NULL && htab->glink->size != 0)
12824 {
12825 last_fde = p;
2e0ce1c8 12826 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 12827 /* FDE length. */
2e0ce1c8 12828 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12829 p += 4;
12830 /* CIE pointer. */
12831 val = p - htab->glink_eh_frame->contents;
12832 bfd_put_32 (htab->elf.dynobj, val, p);
12833 p += 4;
12834 /* Offset to .glink, written later. */
12835 p += 4;
12836 /* .glink size. */
12837 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12838 p += 4;
12839 /* Augmentation. */
12840 p += 1;
12841
12842 *p++ = DW_CFA_advance_loc + 1;
12843 *p++ = DW_CFA_register;
12844 *p++ = 65;
9f08fa5c 12845 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 12846 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
12847 *p++ = DW_CFA_restore_extended;
12848 *p++ = 65;
2e0ce1c8 12849 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
12850 }
12851 /* Subsume any padding into the last FDE if user .eh_frame
12852 sections are aligned more than glink_eh_frame. Otherwise any
12853 zero padding will be seen as a terminator. */
2e0ce1c8 12854 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 12855 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 12856 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
12857 htab->glink_eh_frame->size = size + pad;
12858 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12859 }
12860
d969d15f
AM
12861 maybe_strip_output (info, htab->brlt);
12862 if (htab->glink_eh_frame != NULL)
12863 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12864
b34976b6 12865 return TRUE;
721956f4
AM
12866}
12867
12868/* Called after we have determined section placement. If sections
805fc799 12869 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12870
805fc799 12871bfd_vma
1c865ab2 12872ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12873{
805fc799 12874 asection *s;
a27e685f 12875 bfd_vma TOCstart, adjust;
721956f4 12876
43417696
AM
12877 if (info != NULL)
12878 {
12879 struct elf_link_hash_entry *h;
12880 struct elf_link_hash_table *htab = elf_hash_table (info);
12881
12882 if (is_elf_hash_table (htab)
12883 && htab->hgot != NULL)
12884 h = htab->hgot;
12885 else
12886 {
12887 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12888 if (is_elf_hash_table (htab))
12889 htab->hgot = h;
12890 }
12891 if (h != NULL
12892 && h->root.type == bfd_link_hash_defined
12893 && !h->root.linker_def
12894 && (!is_elf_hash_table (htab)
12895 || h->def_regular))
12896 {
12897 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12898 + h->root.u.def.section->output_offset
12899 + h->root.u.def.section->output_section->vma);
12900 _bfd_set_gp_value (obfd, TOCstart);
12901 return TOCstart;
12902 }
12903 }
12904
805fc799
AM
12905 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12906 order. The TOC starts where the first of these sections starts. */
12907 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12908 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12909 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12910 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12911 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12912 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12913 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12914 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12915 {
12916 /* This may happen for
12917 o references to TOC base (SYM@toc / TOC[tc0]) without a
12918 .toc directive
12919 o bad linker script
12920 o --gc-sections and empty TOC sections
12921
12922 FIXME: Warn user? */
12923
12924 /* Look for a likely section. We probably won't even be
12925 using TOCstart. */
12926 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12927 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12928 | SEC_EXCLUDE))
805fc799
AM
12929 == (SEC_ALLOC | SEC_SMALL_DATA))
12930 break;
721956f4 12931 if (s == NULL)
805fc799 12932 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12933 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12934 == (SEC_ALLOC | SEC_SMALL_DATA))
12935 break;
721956f4 12936 if (s == NULL)
805fc799 12937 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12938 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12939 == SEC_ALLOC)
805fc799 12940 break;
721956f4 12941 if (s == NULL)
805fc799 12942 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12943 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12944 break;
12945 }
721956f4 12946
805fc799
AM
12947 TOCstart = 0;
12948 if (s != NULL)
12949 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12950
a27e685f
AM
12951 /* Force alignment. */
12952 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12953 TOCstart -= adjust;
1c865ab2
AM
12954 _bfd_set_gp_value (obfd, TOCstart);
12955
810d4e75 12956 if (info != NULL && s != NULL)
1c865ab2
AM
12957 {
12958 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12959
810d4e75
AM
12960 if (htab != NULL)
12961 {
12962 if (htab->elf.hgot != NULL)
12963 {
a27e685f 12964 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12965 htab->elf.hgot->root.u.def.section = s;
12966 }
12967 }
12968 else
1c865ab2 12969 {
810d4e75
AM
12970 struct bfd_link_hash_entry *bh = NULL;
12971 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12972 s, TOC_BASE_OFF - adjust,
12973 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12974 }
12975 }
805fc799 12976 return TOCstart;
721956f4
AM
12977}
12978
a345bc8d
AM
12979/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12980 write out any global entry stubs. */
12981
12982static bfd_boolean
12983build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12984{
12985 struct bfd_link_info *info;
12986 struct ppc_link_hash_table *htab;
12987 struct plt_entry *pent;
12988 asection *s;
12989
12990 if (h->root.type == bfd_link_hash_indirect)
12991 return TRUE;
12992
12993 if (!h->pointer_equality_needed)
12994 return TRUE;
12995
12996 if (h->def_regular)
12997 return TRUE;
12998
12999 info = inf;
13000 htab = ppc_hash_table (info);
13001 if (htab == NULL)
13002 return FALSE;
13003
9e390558 13004 s = htab->global_entry;
a345bc8d
AM
13005 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13006 if (pent->plt.offset != (bfd_vma) -1
13007 && pent->addend == 0)
13008 {
13009 bfd_byte *p;
13010 asection *plt;
13011 bfd_vma off;
13012
a345bc8d 13013 p = s->contents + h->root.u.def.value;
33e44f2e 13014 plt = htab->elf.splt;
a345bc8d
AM
13015 if (!htab->elf.dynamic_sections_created
13016 || h->dynindx == -1)
33e44f2e 13017 plt = htab->elf.iplt;
a345bc8d
AM
13018 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
13019 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13020
13021 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13022 {
13023 info->callbacks->einfo
c1c8c1ef 13024 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13025 h->root.root.string);
13026 bfd_set_error (bfd_error_bad_value);
13027 htab->stub_error = TRUE;
13028 }
13029
7341d5e2
AM
13030 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13031 if (htab->params->emit_stub_syms)
13032 {
13033 size_t len = strlen (h->root.root.string);
13034 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13035
13036 if (name == NULL)
13037 return FALSE;
13038
13039 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13040 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13041 if (h == NULL)
13042 return FALSE;
13043 if (h->root.type == bfd_link_hash_new)
13044 {
13045 h->root.type = bfd_link_hash_defined;
13046 h->root.u.def.section = s;
13047 h->root.u.def.value = p - s->contents;
13048 h->ref_regular = 1;
13049 h->def_regular = 1;
13050 h->ref_regular_nonweak = 1;
13051 h->forced_local = 1;
13052 h->non_elf = 0;
2ec55de3 13053 h->root.linker_def = 1;
7341d5e2
AM
13054 }
13055 }
13056
a345bc8d
AM
13057 if (PPC_HA (off) != 0)
13058 {
13059 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13060 p += 4;
13061 }
13062 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13063 p += 4;
13064 bfd_put_32 (s->owner, MTCTR_R12, p);
13065 p += 4;
407aa07c 13066 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13067 break;
13068 }
13069 return TRUE;
13070}
13071
721956f4
AM
13072/* Build all the stubs associated with the current output file.
13073 The stubs are kept in a hash table attached to the main linker
13074 hash table. This function is called via gldelf64ppc_finish. */
13075
b34976b6 13076bfd_boolean
e7d1c40c 13077ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13078 char **stats)
5d1634d7
AM
13079{
13080 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13081 struct map_stub *group;
721956f4 13082 asection *stub_sec;
5d1634d7 13083 bfd_byte *p;
e717da7e 13084 int stub_sec_count = 0;
5d1634d7 13085
4dfe6ac6
NC
13086 if (htab == NULL)
13087 return FALSE;
13088
eea6121a 13089 /* Allocate memory to hold the linker stubs. */
d4aaa2a0
AM
13090 for (group = htab->group; group != NULL; group = group->next)
13091 if ((stub_sec = group->stub_sec) != NULL
eea6121a 13092 && stub_sec->size != 0)
e717da7e 13093 {
e7d1c40c 13094 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
13095 if (stub_sec->contents == NULL)
13096 return FALSE;
eea6121a 13097 stub_sec->size = 0;
e717da7e 13098 }
5d1634d7 13099
23eb7e01 13100 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13101 {
9f951329 13102 unsigned int indx;
ad8e1ba5 13103 bfd_vma plt0;
9f951329 13104
721956f4 13105 /* Build the .glink plt call stub. */
e7d1c40c 13106 if (htab->params->emit_stub_syms)
97b639ba
AM
13107 {
13108 struct elf_link_hash_entry *h;
468392fb
AM
13109 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13110 TRUE, FALSE, FALSE);
97b639ba
AM
13111 if (h == NULL)
13112 return FALSE;
13113 if (h->root.type == bfd_link_hash_new)
13114 {
13115 h->root.type = bfd_link_hash_defined;
13116 h->root.u.def.section = htab->glink;
ee4bf8d2 13117 h->root.u.def.value = 8;
f5385ebf
AM
13118 h->ref_regular = 1;
13119 h->def_regular = 1;
13120 h->ref_regular_nonweak = 1;
13121 h->forced_local = 1;
13122 h->non_elf = 0;
2ec55de3 13123 h->root.linker_def = 1;
97b639ba
AM
13124 }
13125 }
33e44f2e
AM
13126 plt0 = (htab->elf.splt->output_section->vma
13127 + htab->elf.splt->output_offset
13128 - 16);
176a0d42
AM
13129 if (info->emitrelocations)
13130 {
13131 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13132 if (r == NULL)
13133 return FALSE;
13134 r->r_offset = (htab->glink->output_offset
13135 + htab->glink->output_section->vma);
13136 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13137 r->r_addend = plt0;
13138 }
4ce794b7 13139 p = htab->glink->contents;
176a0d42 13140 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13141 bfd_put_64 (htab->glink->owner, plt0, p);
13142 p += 8;
b9e5796b
AM
13143 if (htab->opd_abi)
13144 {
13145 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13146 p += 4;
13147 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13148 p += 4;
13149 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13150 p += 4;
13151 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13152 p += 4;
13153 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13154 p += 4;
13155 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13156 p += 4;
13157 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13158 p += 4;
13159 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13160 p += 4;
13161 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13162 p += 4;
13163 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13164 p += 4;
13165 }
13166 else
13167 {
13168 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13169 p += 4;
13170 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13171 p += 4;
13172 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13173 p += 4;
f378ab09
AM
13174 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13175 p += 4;
b9e5796b
AM
13176 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13177 p += 4;
13178 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13179 p += 4;
13180 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13181 p += 4;
13182 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13183 p += 4;
13184 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13185 p += 4;
13186 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13187 p += 4;
13188 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13189 p += 4;
13190 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13191 p += 4;
13192 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13193 p += 4;
13194 }
407aa07c
AM
13195 bfd_put_32 (htab->glink->owner, BCTR, p);
13196 p += 4;
c75bc4f7 13197 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13198
9f951329
AM
13199 /* Build the .glink lazy link call stubs. */
13200 indx = 0;
9e390558 13201 while (p < htab->glink->contents + htab->glink->size)
9f951329 13202 {
b9e5796b 13203 if (htab->opd_abi)
9f951329 13204 {
b9e5796b
AM
13205 if (indx < 0x8000)
13206 {
13207 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13208 p += 4;
13209 }
13210 else
13211 {
13212 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13213 p += 4;
13214 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13215 p);
13216 p += 4;
13217 }
9f951329 13218 }
4ce794b7 13219 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13220 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13221 indx++;
9f951329
AM
13222 p += 4;
13223 }
5d1634d7 13224 }
5d1634d7 13225
9e390558
AM
13226 /* Build .glink global entry stubs. */
13227 if (htab->global_entry != NULL && htab->global_entry->size != 0)
13228 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13229
7341d5e2 13230 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13231 {
4ce794b7 13232 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13233 htab->brlt->size);
4ce794b7 13234 if (htab->brlt->contents == NULL)
b34976b6 13235 return FALSE;
721956f4 13236 }
ee75fd95 13237 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13238 {
13239 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13240 htab->relbrlt->size);
63bc6f6c
AM
13241 if (htab->relbrlt->contents == NULL)
13242 return FALSE;
13243 }
5d1634d7 13244
721956f4
AM
13245 /* Build the stubs as directed by the stub hash table. */
13246 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13247
a4b6fadd
AM
13248 for (group = htab->group; group != NULL; group = group->next)
13249 if (group->needs_save_res)
13250 {
13251 stub_sec = group->stub_sec;
13252 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13253 htab->sfpr->size);
13254 if (htab->params->emit_stub_syms)
13255 {
13256 unsigned int i;
13257
13258 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13259 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13260 return FALSE;
13261 }
13262 stub_sec->size += htab->sfpr->size;
13263 }
13264
aa8a7074
AM
13265 if (htab->relbrlt != NULL)
13266 htab->relbrlt->reloc_count = 0;
13267
e7d1c40c 13268 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13269 for (group = htab->group; group != NULL; group = group->next)
13270 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13271 {
13272 int align = abs (htab->params->plt_stub_align);
13273 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13274 }
794e51c0 13275
d4aaa2a0
AM
13276 for (group = htab->group; group != NULL; group = group->next)
13277 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
13278 {
13279 stub_sec_count += 1;
c9301e31
AM
13280 if (stub_sec->rawsize != stub_sec->size
13281 && (htab->stub_iteration <= STUB_SHRINK_ITER
13282 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13283 break;
13284 }
5d1634d7 13285
25516cc5 13286 if (group != NULL)
5d1634d7 13287 {
b34976b6 13288 htab->stub_error = TRUE;
cf97bcb0 13289 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 13290 }
721956f4 13291
d2a300cf
AM
13292 if (htab->stub_error)
13293 return FALSE;
13294
13295 if (stats != NULL)
13296 {
db84fff3 13297 size_t len;
d2a300cf
AM
13298 *stats = bfd_malloc (500);
13299 if (*stats == NULL)
13300 return FALSE;
13301
db84fff3
AM
13302 len = sprintf (*stats,
13303 ngettext ("linker stubs in %u group\n",
13304 "linker stubs in %u groups\n",
13305 stub_sec_count),
13306 stub_sec_count);
13307 sprintf (*stats + len, _(" branch %lu\n"
13308 " toc adjust %lu\n"
13309 " long branch %lu\n"
13310 " long toc adj %lu\n"
13311 " plt call %lu\n"
13312 " plt call toc %lu\n"
13313 " global entry %lu"),
4ce794b7
AM
13314 htab->stub_count[ppc_stub_long_branch - 1],
13315 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13316 htab->stub_count[ppc_stub_plt_branch - 1],
13317 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13318 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13319 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13320 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13321 }
13322 return TRUE;
5bd4f169
AM
13323}
13324
60124e18
AM
13325/* What to do when ld finds relocations against symbols defined in
13326 discarded sections. */
13327
13328static unsigned int
13329ppc64_elf_action_discarded (asection *sec)
13330{
13331 if (strcmp (".opd", sec->name) == 0)
13332 return 0;
13333
13334 if (strcmp (".toc", sec->name) == 0)
13335 return 0;
13336
bce50a28
JJ
13337 if (strcmp (".toc1", sec->name) == 0)
13338 return 0;
13339
60124e18
AM
13340 return _bfd_elf_default_action_discarded (sec);
13341}
13342
5bd4f169
AM
13343/* The RELOCATE_SECTION function is called by the ELF backend linker
13344 to handle the relocations for a section.
13345
13346 The relocs are always passed as Rela structures; if the section
13347 actually uses Rel structures, the r_addend field will always be
13348 zero.
13349
13350 This function is responsible for adjust the section contents as
13351 necessary, and (if using Rela relocs and generating a
1049f94e 13352 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13353 necessary.
13354
13355 This function does not have to worry about setting the reloc
13356 address or the reloc symbol index.
13357
13358 LOCAL_SYMS is a pointer to the swapped in local symbols.
13359
13360 LOCAL_SECTIONS is an array giving the section in the input file
13361 corresponding to the st_shndx field of each local symbol.
13362
13363 The global hash table entry for the global symbols can be found
13364 via elf_sym_hashes (input_bfd).
13365
1049f94e 13366 When generating relocatable output, this function must handle
5bd4f169
AM
13367 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13368 going to be the section symbol corresponding to the output
13369 section, which means that the addend must be adjusted
13370 accordingly. */
13371
b34976b6 13372static bfd_boolean
4ce794b7
AM
13373ppc64_elf_relocate_section (bfd *output_bfd,
13374 struct bfd_link_info *info,
13375 bfd *input_bfd,
13376 asection *input_section,
13377 bfd_byte *contents,
13378 Elf_Internal_Rela *relocs,
13379 Elf_Internal_Sym *local_syms,
13380 asection **local_sections)
5bd4f169 13381{
65f38f15 13382 struct ppc_link_hash_table *htab;
5bd4f169
AM
13383 Elf_Internal_Shdr *symtab_hdr;
13384 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13385 Elf_Internal_Rela *rel;
c316a17c 13386 Elf_Internal_Rela *wrel;
5bd4f169 13387 Elf_Internal_Rela *relend;
411e1bfb
AM
13388 Elf_Internal_Rela outrel;
13389 bfd_byte *loc;
411e1bfb 13390 struct got_entry **local_got_ents;
5bd4f169 13391 bfd_vma TOCstart;
b34976b6
AM
13392 bfd_boolean ret = TRUE;
13393 bfd_boolean is_opd;
794e51c0
AM
13394 /* Assume 'at' branch hints. */
13395 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13396 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13397
65f38f15 13398 /* Initialize howto table if needed. */
5bd4f169 13399 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13400 ppc_howto_init ();
13401
65f38f15 13402 htab = ppc_hash_table (info);
4dfe6ac6
NC
13403 if (htab == NULL)
13404 return FALSE;
ee75fd95
AM
13405
13406 /* Don't relocate stub sections. */
e7d1c40c 13407 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13408 return TRUE;
13409
0c8d6e5c 13410 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13411
411e1bfb 13412 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13413 TOCstart = elf_gp (output_bfd);
0ffa91dd 13414 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13415 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13416 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13417
c316a17c 13418 rel = wrel = relocs;
5bd4f169 13419 relend = relocs + input_section->reloc_count;
c316a17c 13420 for (; rel < relend; wrel++, rel++)
5bd4f169 13421 {
04c9666a 13422 enum elf_ppc64_reloc_type r_type;
31c76678 13423 bfd_vma addend;
5bd4f169
AM
13424 bfd_reloc_status_type r;
13425 Elf_Internal_Sym *sym;
13426 asection *sec;
039b3fef
AM
13427 struct elf_link_hash_entry *h_elf;
13428 struct ppc_link_hash_entry *h;
13429 struct ppc_link_hash_entry *fdh;
5bd4f169 13430 const char *sym_name;
0d4792f7 13431 unsigned long r_symndx, toc_symndx;
3a71aa26 13432 bfd_vma toc_addend;
f961d9dd
AM
13433 unsigned char tls_mask, tls_gd, tls_type;
13434 unsigned char sym_type;
5bd4f169 13435 bfd_vma relocation;
b34976b6
AM
13436 bfd_boolean unresolved_reloc;
13437 bfd_boolean warned;
bc30df16 13438 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13439 unsigned int insn;
e11840f9 13440 unsigned int mask;
721956f4
AM
13441 struct ppc_stub_hash_entry *stub_entry;
13442 bfd_vma max_br_offset;
13443 bfd_vma from;
c316a17c 13444 Elf_Internal_Rela orig_rel;
b80eed39
AM
13445 reloc_howto_type *howto;
13446 struct reloc_howto_struct alt_howto;
5bd4f169 13447
c316a17c
AM
13448 again:
13449 orig_rel = *rel;
13450
4ce794b7 13451 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13452 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13453
13454 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13455 symbol of the previous ADDR64 reloc. The symbol gives us the
13456 proper TOC base to use. */
13457 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13458 && wrel != relocs
13459 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13460 && is_opd)
c316a17c 13461 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13462
4ce794b7
AM
13463 sym = NULL;
13464 sec = NULL;
039b3fef 13465 h_elf = NULL;
4ce794b7 13466 sym_name = NULL;
b34976b6
AM
13467 unresolved_reloc = FALSE;
13468 warned = FALSE;
65f38f15 13469
0b13192e 13470 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13471 {
13472 /* It's a local symbol. */
74f0fb50 13473 struct _opd_sec_data *opd;
4025353c 13474
5bd4f169
AM
13475 sym = local_syms + r_symndx;
13476 sec = local_sections[r_symndx];
26c61ae5 13477 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13478 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13479 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13480 opd = get_opd_info (sec);
13481 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13482 {
51aecdc5
AM
13483 long adjust = opd->adjust[OPD_NDX (sym->st_value
13484 + rel->r_addend)];
4025353c
AM
13485 if (adjust == -1)
13486 relocation = 0;
13487 else
4cc603a5
AM
13488 {
13489 /* If this is a relocation against the opd section sym
13490 and we have edited .opd, adjust the reloc addend so
13491 that ld -r and ld --emit-relocs output is correct.
13492 If it is a reloc against some other .opd symbol,
13493 then the symbol value will be adjusted later. */
13494 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13495 rel->r_addend += adjust;
13496 else
13497 relocation += adjust;
13498 }
1e2f5b6e 13499 }
5bd4f169
AM
13500 }
13501 else
13502 {
62d887d4
L
13503 bfd_boolean ignored;
13504
b2a8e766
AM
13505 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13506 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13507 h_elf, sec, relocation,
62d887d4 13508 unresolved_reloc, warned, ignored);
039b3fef
AM
13509 sym_name = h_elf->root.root.string;
13510 sym_type = h_elf->type;
b69fdb4e
AM
13511 if (sec != NULL
13512 && sec->owner == output_bfd
13513 && strcmp (sec->name, ".opd") == 0)
13514 {
13515 /* This is a symbol defined in a linker script. All
13516 such are defined in output sections, even those
13517 defined by simple assignment from a symbol defined in
13518 an input section. Transfer the symbol to an
13519 appropriate input .opd section, so that a branch to
13520 this symbol will be mapped to the location specified
13521 by the opd entry. */
13522 struct bfd_link_order *lo;
13523 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13524 if (lo->type == bfd_indirect_link_order)
13525 {
13526 asection *isec = lo->u.indirect.section;
13527 if (h_elf->root.u.def.value >= isec->output_offset
13528 && h_elf->root.u.def.value < (isec->output_offset
13529 + isec->size))
13530 {
13531 h_elf->root.u.def.value -= isec->output_offset;
13532 h_elf->root.u.def.section = isec;
13533 sec = isec;
13534 break;
13535 }
13536 }
13537 }
5bd4f169 13538 }
039b3fef 13539 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13540
dbaa2011 13541 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13542 {
13543 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13544 input_bfd, input_section,
13545 contents + rel->r_offset);
13546 wrel->r_offset = rel->r_offset;
13547 wrel->r_info = 0;
13548 wrel->r_addend = 0;
13549
13550 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 13551 symbols defined in discarded sections. Not done for
c316a17c
AM
13552 non-debug to preserve relocs in .eh_frame which the
13553 eh_frame editing code expects to be present. */
13554 if (bfd_link_relocatable (info)
13555 && (input_section->flags & SEC_DEBUGGING))
13556 wrel--;
13557
13558 continue;
13559 }
ab96bf03 13560
0e1862bb 13561 if (bfd_link_relocatable (info))
c316a17c 13562 goto copy_reloc;
ab96bf03 13563
f40da81b
AM
13564 if (h != NULL && &h->elf == htab->elf.hgot)
13565 {
6f20ed8a 13566 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13567 sec = bfd_abs_section_ptr;
13568 unresolved_reloc = FALSE;
13569 }
13570
951fd09b
AM
13571 /* TLS optimizations. Replace instruction sequences and relocs
13572 based on information we collected in tls_optimize. We edit
13573 RELOCS so that --emit-relocs will output something sensible
13574 for the final instruction stream. */
13575 tls_mask = 0;
13576 tls_gd = 0;
0d4792f7 13577 toc_symndx = 0;
727fc41e
AM
13578 if (h != NULL)
13579 tls_mask = h->tls_mask;
13580 else if (local_got_ents != NULL)
411e1bfb 13581 {
e054468f
AM
13582 struct plt_entry **local_plt = (struct plt_entry **)
13583 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13584 unsigned char *lgot_masks = (unsigned char *)
e054468f 13585 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13586 tls_mask = lgot_masks[r_symndx];
13587 }
13588 if (tls_mask == 0
13589 && (r_type == R_PPC64_TLS
13590 || r_type == R_PPC64_TLSGD
13591 || r_type == R_PPC64_TLSLD))
13592 {
13593 /* Check for toc tls entries. */
f961d9dd 13594 unsigned char *toc_tls;
0d4792f7 13595
727fc41e
AM
13596 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13597 &local_syms, rel, input_bfd))
13598 return FALSE;
0d4792f7 13599
727fc41e
AM
13600 if (toc_tls)
13601 tls_mask = *toc_tls;
0d4792f7
AM
13602 }
13603
13604 /* Check that tls relocs are used with tls syms, and non-tls
13605 relocs are used with non-tls syms. */
cf35638d 13606 if (r_symndx != STN_UNDEF
0d4792f7
AM
13607 && r_type != R_PPC64_NONE
13608 && (h == NULL
039b3fef
AM
13609 || h->elf.root.type == bfd_link_hash_defined
13610 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13611 && (IS_PPC64_TLS_RELOC (r_type)
13612 != (sym_type == STT_TLS
13613 || (sym_type == STT_SECTION
13614 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13615 {
727fc41e
AM
13616 if (tls_mask != 0
13617 && (r_type == R_PPC64_TLS
13618 || r_type == R_PPC64_TLSGD
13619 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13620 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13621 ;
13622 else
25f53a85 13623 info->callbacks->einfo
1d483afe 13624 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 13625 /* xgettext:c-format */
c1c8c1ef 13626 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 13627 /* xgettext:c-format */
c1c8c1ef 13628 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 13629 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13630 ppc64_elf_howto_table[r_type]->name,
13631 sym_name);
411e1bfb
AM
13632 }
13633
13634 /* Ensure reloc mapping code below stays sane. */
13635 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13636 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13637 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13638 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13639 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13640 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13641 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13642 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13643 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13644 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13645 abort ();
0d4792f7 13646
411e1bfb
AM
13647 switch (r_type)
13648 {
13649 default:
411e1bfb
AM
13650 break;
13651
ba761f19 13652 case R_PPC64_LO_DS_OPT:
95f0d0d2 13653 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
13654 if ((insn & (0x3f << 26)) != 58u << 26)
13655 abort ();
13656 insn += (14u << 26) - (58u << 26);
95f0d0d2 13657 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
13658 r_type = R_PPC64_TOC16_LO;
13659 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13660 break;
13661
411e1bfb
AM
13662 case R_PPC64_TOC16:
13663 case R_PPC64_TOC16_LO:
13664 case R_PPC64_TOC16_DS:
13665 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13666 {
13667 /* Check for toc tls entries. */
f961d9dd 13668 unsigned char *toc_tls;
951fd09b 13669 int retval;
411e1bfb 13670
3a71aa26
AM
13671 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13672 &local_syms, rel, input_bfd);
951fd09b 13673 if (retval == 0)
411e1bfb
AM
13674 return FALSE;
13675
13676 if (toc_tls)
13677 {
951fd09b 13678 tls_mask = *toc_tls;
411e1bfb
AM
13679 if (r_type == R_PPC64_TOC16_DS
13680 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
13681 {
13682 if (tls_mask != 0
13683 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13684 goto toctprel;
13685 }
411e1bfb 13686 else
951fd09b
AM
13687 {
13688 /* If we found a GD reloc pair, then we might be
13689 doing a GD->IE transition. */
13690 if (retval == 2)
13691 {
13692 tls_gd = TLS_TPRELGD;
13693 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13694 goto tls_ldgd_opt;
951fd09b
AM
13695 }
13696 else if (retval == 3)
13697 {
13698 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 13699 goto tls_ldgd_opt;
951fd09b
AM
13700 }
13701 }
411e1bfb
AM
13702 }
13703 }
13704 break;
13705
9d6ded02
AM
13706 case R_PPC64_GOT_TPREL16_HI:
13707 case R_PPC64_GOT_TPREL16_HA:
13708 if (tls_mask != 0
13709 && (tls_mask & TLS_TPREL) == 0)
13710 {
13711 rel->r_offset -= d_offset;
95f0d0d2 13712 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
13713 r_type = R_PPC64_NONE;
13714 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13715 }
13716 break;
13717
411e1bfb
AM
13718 case R_PPC64_GOT_TPREL16_DS:
13719 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13720 if (tls_mask != 0
13721 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13722 {
81407a69 13723 toctprel:
95f0d0d2 13724 insn = bfd_get_32 (input_bfd,
c316a17c 13725 contents + rel->r_offset - d_offset);
411e1bfb
AM
13726 insn &= 31 << 21;
13727 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 13728 bfd_put_32 (input_bfd, insn,
c316a17c 13729 contents + rel->r_offset - d_offset);
411e1bfb 13730 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13731 if (toc_symndx != 0)
13732 {
13733 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13734 rel->r_addend = toc_addend;
0d4792f7
AM
13735 /* We changed the symbol. Start over in order to
13736 get h, sym, sec etc. right. */
c316a17c 13737 goto again;
0d4792f7
AM
13738 }
13739 else
13740 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13741 }
13742 break;
13743
13744 case R_PPC64_TLS:
951fd09b
AM
13745 if (tls_mask != 0
13746 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13747 {
95f0d0d2 13748 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
13749 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13750 if (insn == 0)
411e1bfb 13751 abort ();
95f0d0d2 13752 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 13753 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13754 PPC64_TPREL16_LO which is at low-order half-word. */
13755 rel->r_offset += d_offset;
0d4792f7
AM
13756 r_type = R_PPC64_TPREL16_LO;
13757 if (toc_symndx != 0)
13758 {
13759 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13760 rel->r_addend = toc_addend;
0d4792f7
AM
13761 /* We changed the symbol. Start over in order to
13762 get h, sym, sec etc. right. */
c316a17c 13763 goto again;
0d4792f7
AM
13764 }
13765 else
13766 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13767 }
13768 break;
13769
411e1bfb
AM
13770 case R_PPC64_GOT_TLSGD16_HI:
13771 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13772 tls_gd = TLS_TPRELGD;
13773 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13774 goto tls_gdld_hi;
13775 break;
13776
411e1bfb
AM
13777 case R_PPC64_GOT_TLSLD16_HI:
13778 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13779 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13780 {
951fd09b
AM
13781 tls_gdld_hi:
13782 if ((tls_mask & tls_gd) != 0)
13783 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13784 + R_PPC64_GOT_TPREL16_DS);
13785 else
411e1bfb 13786 {
4fe5ca5b 13787 rel->r_offset -= d_offset;
95f0d0d2 13788 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 13789 r_type = R_PPC64_NONE;
411e1bfb 13790 }
951fd09b 13791 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13792 }
13793 break;
13794
951fd09b
AM
13795 case R_PPC64_GOT_TLSGD16:
13796 case R_PPC64_GOT_TLSGD16_LO:
13797 tls_gd = TLS_TPRELGD;
13798 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13799 goto tls_ldgd_opt;
951fd09b 13800 break;
411e1bfb 13801
951fd09b
AM
13802 case R_PPC64_GOT_TLSLD16:
13803 case R_PPC64_GOT_TLSLD16_LO:
13804 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13805 {
b9f04fe0 13806 unsigned int insn1, insn2;
102890f0
AM
13807 bfd_vma offset;
13808
13809 tls_ldgd_opt:
727fc41e
AM
13810 offset = (bfd_vma) -1;
13811 /* If not using the newer R_PPC64_TLSGD/LD to mark
13812 __tls_get_addr calls, we must trust that the call
13813 stays with its arg setup insns, ie. that the next
13814 reloc is the __tls_get_addr call associated with
13815 the current reloc. Edit both insns. */
13816 if (input_section->has_tls_get_addr_call
13817 && rel + 1 < relend
13818 && branch_reloc_hash_match (input_bfd, rel + 1,
13819 htab->tls_get_addr,
13820 htab->tls_get_addr_fd))
13821 offset = rel[1].r_offset;
b86ac8e3
AM
13822 /* We read the low GOT_TLS (or TOC16) insn because we
13823 need to keep the destination reg. It may be
13824 something other than the usual r3, and moved to r3
13825 before the call by intervening code. */
95f0d0d2 13826 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 13827 contents + rel->r_offset - d_offset);
102890f0 13828 if ((tls_mask & tls_gd) != 0)
411e1bfb 13829 {
102890f0 13830 /* IE */
b86ac8e3 13831 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13832 insn1 |= 58 << 26; /* ld */
13833 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13834 if (offset != (bfd_vma) -1)
f58d5a2d 13835 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13836 if ((tls_mask & TLS_EXPLICIT) == 0)
13837 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13838 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13839 else
102890f0
AM
13840 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13841 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13842 }
13843 else
13844 {
13845 /* LE */
b86ac8e3
AM
13846 insn1 &= 0x1f << 21;
13847 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13848 insn2 = 0x38630000; /* addi 3,3,0 */
13849 if (tls_gd == 0)
951fd09b 13850 {
102890f0 13851 /* Was an LD reloc. */
1d483afe
AM
13852 if (toc_symndx)
13853 sec = local_sections[toc_symndx];
13854 for (r_symndx = 0;
13855 r_symndx < symtab_hdr->sh_info;
13856 r_symndx++)
13857 if (local_sections[r_symndx] == sec)
13858 break;
13859 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13860 r_symndx = STN_UNDEF;
102890f0 13861 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13862 if (r_symndx != STN_UNDEF)
1d483afe
AM
13863 rel->r_addend -= (local_syms[r_symndx].st_value
13864 + sec->output_offset
13865 + sec->output_section->vma);
951fd09b 13866 }
102890f0 13867 else if (toc_symndx != 0)
3a71aa26
AM
13868 {
13869 r_symndx = toc_symndx;
13870 rel->r_addend = toc_addend;
13871 }
102890f0
AM
13872 r_type = R_PPC64_TPREL16_HA;
13873 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13874 if (offset != (bfd_vma) -1)
13875 {
13876 rel[1].r_info = ELF64_R_INFO (r_symndx,
13877 R_PPC64_TPREL16_LO);
13878 rel[1].r_offset = offset + d_offset;
13879 rel[1].r_addend = rel->r_addend;
13880 }
102890f0 13881 }
95f0d0d2 13882 bfd_put_32 (input_bfd, insn1,
3a71aa26 13883 contents + rel->r_offset - d_offset);
727fc41e 13884 if (offset != (bfd_vma) -1)
b9f04fe0 13885 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
13886 if ((tls_mask & tls_gd) == 0
13887 && (tls_gd == 0 || toc_symndx != 0))
13888 {
13889 /* We changed the symbol. Start over in order
13890 to get h, sym, sec etc. right. */
c316a17c 13891 goto again;
727fc41e
AM
13892 }
13893 }
13894 break;
13895
13896 case R_PPC64_TLSGD:
13897 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13898 {
b9f04fe0 13899 unsigned int insn2;
727fc41e
AM
13900 bfd_vma offset = rel->r_offset;
13901
13902 if ((tls_mask & TLS_TPRELGD) != 0)
13903 {
13904 /* IE */
13905 r_type = R_PPC64_NONE;
13906 insn2 = 0x7c636a14; /* add 3,3,13 */
13907 }
13908 else
13909 {
13910 /* LE */
13911 if (toc_symndx != 0)
13912 {
13913 r_symndx = toc_symndx;
13914 rel->r_addend = toc_addend;
13915 }
13916 r_type = R_PPC64_TPREL16_LO;
13917 rel->r_offset = offset + d_offset;
13918 insn2 = 0x38630000; /* addi 3,3,0 */
13919 }
13920 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13921 /* Zap the reloc on the _tls_get_addr call too. */
13922 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13923 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 13924 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 13925 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13926 goto again;
411e1bfb 13927 }
411e1bfb
AM
13928 break;
13929
727fc41e
AM
13930 case R_PPC64_TLSLD:
13931 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13932 {
b9f04fe0 13933 unsigned int insn2;
727fc41e
AM
13934 bfd_vma offset = rel->r_offset;
13935
13936 if (toc_symndx)
13937 sec = local_sections[toc_symndx];
13938 for (r_symndx = 0;
13939 r_symndx < symtab_hdr->sh_info;
13940 r_symndx++)
13941 if (local_sections[r_symndx] == sec)
13942 break;
13943 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13944 r_symndx = STN_UNDEF;
727fc41e 13945 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13946 if (r_symndx != STN_UNDEF)
727fc41e
AM
13947 rel->r_addend -= (local_syms[r_symndx].st_value
13948 + sec->output_offset
13949 + sec->output_section->vma);
13950
13951 r_type = R_PPC64_TPREL16_LO;
13952 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13953 rel->r_offset = offset + d_offset;
13954 /* Zap the reloc on the _tls_get_addr call too. */
13955 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13956 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 13957 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 13958 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 13959 goto again;
727fc41e
AM
13960 }
13961 break;
13962
411e1bfb 13963 case R_PPC64_DTPMOD64:
951fd09b
AM
13964 if (rel + 1 < relend
13965 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13966 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13967 {
951fd09b
AM
13968 if ((tls_mask & TLS_GD) == 0)
13969 {
13970 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13971 if ((tls_mask & TLS_TPRELGD) != 0)
13972 r_type = R_PPC64_TPREL64;
13973 else
13974 {
4ce794b7 13975 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13976 r_type = R_PPC64_NONE;
13977 }
13978 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13979 }
13980 }
13981 else
13982 {
13983 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13984 {
4ce794b7 13985 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13986 r_type = R_PPC64_NONE;
951fd09b 13987 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13988 }
411e1bfb
AM
13989 }
13990 break;
13991
13992 case R_PPC64_TPREL64:
951fd09b 13993 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13994 {
13995 r_type = R_PPC64_NONE;
13996 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13997 }
13998 break;
52a82034 13999
006589cf
AM
14000 case R_PPC64_ENTRY:
14001 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14002 if (!bfd_link_pic (info)
14003 && !info->traditional_format
14004 && relocation + 0x80008000 <= 0xffffffff)
14005 {
14006 unsigned int insn1, insn2;
14007
14008 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14009 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14010 if ((insn1 & ~0xfffc) == LD_R2_0R12
14011 && insn2 == ADD_R2_R2_R12)
14012 {
95f0d0d2 14013 bfd_put_32 (input_bfd,
006589cf
AM
14014 LIS_R2 + PPC_HA (relocation),
14015 contents + rel->r_offset);
95f0d0d2 14016 bfd_put_32 (input_bfd,
006589cf
AM
14017 ADDI_R2_R2 + PPC_LO (relocation),
14018 contents + rel->r_offset + 4);
14019 }
14020 }
14021 else
14022 {
14023 relocation -= (rel->r_offset
14024 + input_section->output_offset
14025 + input_section->output_section->vma);
14026 if (relocation + 0x80008000 <= 0xffffffff)
14027 {
14028 unsigned int insn1, insn2;
14029
14030 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14031 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14032 if ((insn1 & ~0xfffc) == LD_R2_0R12
14033 && insn2 == ADD_R2_R2_R12)
14034 {
95f0d0d2 14035 bfd_put_32 (input_bfd,
006589cf
AM
14036 ADDIS_R2_R12 + PPC_HA (relocation),
14037 contents + rel->r_offset);
95f0d0d2 14038 bfd_put_32 (input_bfd,
006589cf
AM
14039 ADDI_R2_R2 + PPC_LO (relocation),
14040 contents + rel->r_offset + 4);
14041 }
14042 }
14043 }
14044 break;
14045
52a82034
AM
14046 case R_PPC64_REL16_HA:
14047 /* If we are generating a non-PIC executable, edit
14048 . 0: addis 2,12,.TOC.-0b@ha
14049 . addi 2,2,.TOC.-0b@l
14050 used by ELFv2 global entry points to set up r2, to
14051 . lis 2,.TOC.@ha
14052 . addi 2,2,.TOC.@l
14053 if .TOC. is in range. */
0e1862bb 14054 if (!bfd_link_pic (info)
810d4e75 14055 && !info->traditional_format
006589cf 14056 && !htab->opd_abi
4f038ee5 14057 && rel->r_addend == d_offset
52a82034
AM
14058 && h != NULL && &h->elf == htab->elf.hgot
14059 && rel + 1 < relend
14060 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14061 && rel[1].r_offset == rel->r_offset + 4
14062 && rel[1].r_addend == rel->r_addend + 4
14063 && relocation + 0x80008000 <= 0xffffffff)
14064 {
14065 unsigned int insn1, insn2;
14066 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14067 insn1 = bfd_get_32 (input_bfd, contents + offset);
14068 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14069 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14070 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14071 {
14072 r_type = R_PPC64_ADDR16_HA;
14073 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14074 rel->r_addend -= d_offset;
14075 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14076 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14077 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14078 }
14079 }
14080 break;
411e1bfb
AM
14081 }
14082
14083 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14084 insn = 0;
b25116a9
AM
14085 max_br_offset = 1 << 25;
14086 addend = rel->r_addend;
bc30df16 14087 reloc_dest = DEST_NORMAL;
65f38f15 14088 switch (r_type)
5bd4f169
AM
14089 {
14090 default:
65f38f15 14091 break;
5bd4f169 14092
3b421ab3
AM
14093 case R_PPC64_TOCSAVE:
14094 if (relocation + addend == (rel->r_offset
14095 + input_section->output_offset
14096 + input_section->output_section->vma)
14097 && tocsave_find (htab, NO_INSERT,
14098 &local_syms, rel, input_bfd))
14099 {
14100 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14101 if (insn == NOP
14102 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14103 bfd_put_32 (input_bfd,
14104 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14105 contents + rel->r_offset);
14106 }
14107 break;
14108
65f38f15
AM
14109 /* Branch taken prediction relocations. */
14110 case R_PPC64_ADDR14_BRTAKEN:
14111 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14112 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14113 /* Fall through. */
65f38f15 14114
86c76c7b 14115 /* Branch not taken prediction relocations. */
65f38f15
AM
14116 case R_PPC64_ADDR14_BRNTAKEN:
14117 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14118 insn |= bfd_get_32 (input_bfd,
411e1bfb 14119 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14120 /* Fall through. */
86c76c7b 14121
b25116a9
AM
14122 case R_PPC64_REL14:
14123 max_br_offset = 1 << 15;
1a0670f3 14124 /* Fall through. */
5bd4f169 14125
65f38f15 14126 case R_PPC64_REL24:
ad8e1ba5
AM
14127 /* Calls to functions with a different TOC, such as calls to
14128 shared objects, need to alter the TOC pointer. This is
14129 done using a linkage stub. A REL24 branching to these
14130 linkage stubs needs to be followed by a nop, as the nop
14131 will be replaced with an instruction to restore the TOC
14132 base pointer. */
8387904d 14133 fdh = h;
b31867b6
AM
14134 if (h != NULL
14135 && h->oh != NULL
14136 && h->oh->is_func_descriptor)
14137 fdh = ppc_follow_link (h->oh);
31c76678
DK
14138 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14139 htab);
6abec6bc 14140 if (stub_entry != NULL
ad8e1ba5 14141 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 14142 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
14143 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14144 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 14145 {
b25116a9 14146 bfd_boolean can_plt_call = FALSE;
721956f4 14147
6e1816be
AM
14148 if (stub_entry->stub_type == ppc_stub_plt_call
14149 && !htab->opd_abi
14150 && htab->params->plt_localentry0 != 0
14151 && is_elfv2_localentry0 (&h->elf))
14152 {
14153 /* The function doesn't use or change r2. */
14154 can_plt_call = TRUE;
14155 }
14156
f378ab09 14157 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
14158 branch and link followed by a nop. The nop is
14159 replaced by an insn to restore r2. */
6e1816be 14160 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14161 {
ba8ca3e7
AM
14162 unsigned long br;
14163
14164 br = bfd_get_32 (input_bfd,
14165 contents + rel->r_offset);
14166 if ((br & 1) != 0)
41bd81ab 14167 {
ba8ca3e7
AM
14168 unsigned long nop;
14169
14170 nop = bfd_get_32 (input_bfd,
14171 contents + rel->r_offset + 4);
14172 if (nop == NOP
14173 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 14174 {
ba8ca3e7
AM
14175 if (h != NULL
14176 && (h == htab->tls_get_addr_fd
14177 || h == htab->tls_get_addr)
7c9cf415 14178 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14179 {
14180 /* Special stub used, leave nop alone. */
14181 }
14182 else
a078d95a
AM
14183 bfd_put_32 (input_bfd,
14184 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14185 contents + rel->r_offset + 4);
14186 can_plt_call = TRUE;
a7f2871e 14187 }
41bd81ab 14188 }
5bd4f169 14189 }
721956f4 14190
ba8ca3e7 14191 if (!can_plt_call && h != NULL)
721956f4 14192 {
ba8ca3e7
AM
14193 const char *name = h->elf.root.root.string;
14194
14195 if (*name == '.')
14196 ++name;
14197
14198 if (strncmp (name, "__libc_start_main", 17) == 0
14199 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14200 {
ba8ca3e7
AM
14201 /* Allow crt1 branch to go via a toc adjusting
14202 stub. Other calls that never return could do
14203 the same, if we could detect such. */
b25116a9 14204 can_plt_call = TRUE;
6ab189d5 14205 }
ba8ca3e7
AM
14206 }
14207
14208 if (!can_plt_call)
14209 {
14210 /* g++ as of 20130507 emits self-calls without a
14211 following nop. This is arguably wrong since we
14212 have conflicting information. On the one hand a
14213 global symbol and on the other a local call
14214 sequence, but don't error for this special case.
14215 It isn't possible to cheaply verify we have
14216 exactly such a call. Allow all calls to the same
14217 section. */
14218 asection *code_sec = sec;
14219
14220 if (get_opd_info (sec) != NULL)
ad8e1ba5 14221 {
ba8ca3e7
AM
14222 bfd_vma off = (relocation + addend
14223 - sec->output_section->vma
14224 - sec->output_offset);
bc30df16 14225
ba8ca3e7 14226 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14227 }
ba8ca3e7
AM
14228 if (code_sec == input_section)
14229 can_plt_call = TRUE;
14230 }
14231
14232 if (!can_plt_call)
14233 {
4805fc55
AM
14234 if (stub_entry->stub_type == ppc_stub_plt_call
14235 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14236 info->callbacks->einfo
695344c0 14237 /* xgettext:c-format */
c1c8c1ef 14238 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
14239 "recompile with -fPIC\n"),
14240 input_bfd, input_section, rel->r_offset, sym_name);
14241 else
14242 info->callbacks->einfo
695344c0 14243 /* xgettext:c-format */
c1c8c1ef 14244 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
14245 "(-mcmodel=small toc adjust stub)\n"),
14246 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14247
14248 bfd_set_error (bfd_error_bad_value);
14249 ret = FALSE;
721956f4
AM
14250 }
14251
b25116a9 14252 if (can_plt_call
794e51c0
AM
14253 && (stub_entry->stub_type == ppc_stub_plt_call
14254 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14255 unresolved_reloc = FALSE;
14256 }
14257
6abec6bc
AM
14258 if ((stub_entry == NULL
14259 || stub_entry->stub_type == ppc_stub_long_branch
14260 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14261 && get_opd_info (sec) != NULL)
14262 {
14263 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14264 bfd_vma off = (relocation + addend
14265 - sec->output_section->vma
14266 - sec->output_offset);
aef36ac1 14267 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14268 if (dest != (bfd_vma) -1)
14269 {
14270 relocation = dest;
14271 addend = 0;
bc30df16 14272 reloc_dest = DEST_OPD;
8387904d
AM
14273 }
14274 }
14275
b25116a9
AM
14276 /* If the branch is out of reach we ought to have a long
14277 branch stub. */
14278 from = (rel->r_offset
14279 + input_section->output_offset
14280 + input_section->output_section->vma);
14281
6911b7dc
AM
14282 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14283 ? fdh->elf.other
14284 : sym->st_other);
14285
6abec6bc
AM
14286 if (stub_entry != NULL
14287 && (stub_entry->stub_type == ppc_stub_long_branch
14288 || stub_entry->stub_type == ppc_stub_plt_branch)
14289 && (r_type == R_PPC64_ADDR14_BRTAKEN
14290 || r_type == R_PPC64_ADDR14_BRNTAKEN
14291 || (relocation + addend - from + max_br_offset
14292 < 2 * max_br_offset)))
14293 /* Don't use the stub if this branch is in range. */
14294 stub_entry = NULL;
b25116a9
AM
14295
14296 if (stub_entry != NULL)
14297 {
14298 /* Munge up the value and addend so that we call the stub
14299 rather than the procedure directly. */
a4b6fadd
AM
14300 asection *stub_sec = stub_entry->group->stub_sec;
14301
14302 if (stub_entry->stub_type == ppc_stub_save_res)
14303 relocation += (stub_sec->output_offset
14304 + stub_sec->output_section->vma
14305 + stub_sec->size - htab->sfpr->size
14306 - htab->sfpr->output_offset
14307 - htab->sfpr->output_section->vma);
14308 else
14309 relocation = (stub_entry->stub_offset
14310 + stub_sec->output_offset
14311 + stub_sec->output_section->vma);
b25116a9 14312 addend = 0;
bc30df16 14313 reloc_dest = DEST_STUB;
3b421ab3 14314
954b63d4 14315 if ((stub_entry->stub_type == ppc_stub_plt_call
794e51c0
AM
14316 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14317 && (ALWAYS_EMIT_R2SAVE
14318 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14319 && rel + 1 < relend
14320 && rel[1].r_offset == rel->r_offset + 4
14321 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14322 relocation += 4;
b25116a9
AM
14323 }
14324
14325 if (insn != 0)
14326 {
794e51c0 14327 if (is_isa_v2)
721956f4 14328 {
b25116a9
AM
14329 /* Set 'a' bit. This is 0b00010 in BO field for branch
14330 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14331 for branch on CTR insns (BO == 1a00t or 1a01t). */
14332 if ((insn & (0x14 << 21)) == (0x04 << 21))
14333 insn |= 0x02 << 21;
14334 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14335 insn |= 0x08 << 21;
14336 else
14337 break;
14338 }
14339 else
14340 {
14341 /* Invert 'y' bit if not the default. */
4cc603a5 14342 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14343 insn ^= 0x01 << 21;
721956f4 14344 }
b25116a9 14345
95f0d0d2 14346 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14347 }
e86ce104 14348
06da1e8e
AM
14349 /* NOP out calls to undefined weak functions.
14350 We can thus call a weak function without first
14351 checking whether the function is defined. */
b25116a9 14352 else if (h != NULL
039b3fef 14353 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14354 && h->elf.dynindx == -1
b25116a9
AM
14355 && r_type == R_PPC64_REL24
14356 && relocation == 0
4cc603a5 14357 && addend == 0)
e86ce104 14358 {
95f0d0d2 14359 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14360 goto copy_reloc;
e86ce104 14361 }
65f38f15
AM
14362 break;
14363 }
5bd4f169 14364
65f38f15 14365 /* Set `addend'. */
411e1bfb 14366 tls_type = 0;
65f38f15
AM
14367 switch (r_type)
14368 {
14369 default:
cf97bcb0
AM
14370 /* xgettext:c-format */
14371 _bfd_error_handler (_("%pB: %s unsupported"),
14372 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 14373
65f38f15 14374 bfd_set_error (bfd_error_bad_value);
b34976b6 14375 ret = FALSE;
c316a17c 14376 goto copy_reloc;
5bd4f169 14377
65f38f15 14378 case R_PPC64_NONE:
411e1bfb 14379 case R_PPC64_TLS:
727fc41e
AM
14380 case R_PPC64_TLSGD:
14381 case R_PPC64_TLSLD:
3b421ab3 14382 case R_PPC64_TOCSAVE:
04c9666a
AM
14383 case R_PPC64_GNU_VTINHERIT:
14384 case R_PPC64_GNU_VTENTRY:
006589cf 14385 case R_PPC64_ENTRY:
c316a17c 14386 goto copy_reloc;
5bd4f169
AM
14387
14388 /* GOT16 relocations. Like an ADDR16 using the symbol's
14389 address in the GOT as relocation value instead of the
411e1bfb 14390 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14391 symbol and put the symbol value there. */
411e1bfb
AM
14392 case R_PPC64_GOT_TLSGD16:
14393 case R_PPC64_GOT_TLSGD16_LO:
14394 case R_PPC64_GOT_TLSGD16_HI:
14395 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14396 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14397 goto dogot;
14398
14399 case R_PPC64_GOT_TLSLD16:
14400 case R_PPC64_GOT_TLSLD16_LO:
14401 case R_PPC64_GOT_TLSLD16_HI:
14402 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14403 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14404 goto dogot;
14405
14406 case R_PPC64_GOT_TPREL16_DS:
14407 case R_PPC64_GOT_TPREL16_LO_DS:
14408 case R_PPC64_GOT_TPREL16_HI:
14409 case R_PPC64_GOT_TPREL16_HA:
14410 tls_type = TLS_TLS | TLS_TPREL;
14411 goto dogot;
14412
14413 case R_PPC64_GOT_DTPREL16_DS:
14414 case R_PPC64_GOT_DTPREL16_LO_DS:
14415 case R_PPC64_GOT_DTPREL16_HI:
14416 case R_PPC64_GOT_DTPREL16_HA:
14417 tls_type = TLS_TLS | TLS_DTPREL;
14418 goto dogot;
14419
65f38f15
AM
14420 case R_PPC64_GOT16:
14421 case R_PPC64_GOT16_LO:
14422 case R_PPC64_GOT16_HI:
14423 case R_PPC64_GOT16_HA:
14424 case R_PPC64_GOT16_DS:
14425 case R_PPC64_GOT16_LO_DS:
411e1bfb 14426 dogot:
5bd4f169
AM
14427 {
14428 /* Relocation is to the entry for this symbol in the global
14429 offset table. */
e717da7e 14430 asection *got;
d881513a 14431 bfd_vma *offp;
5bd4f169 14432 bfd_vma off;
d881513a 14433 unsigned long indx = 0;
927be08e 14434 struct got_entry *ent;
65f38f15 14435
d881513a
AM
14436 if (tls_type == (TLS_TLS | TLS_LD)
14437 && (h == NULL
f5385ebf 14438 || !h->elf.def_dynamic))
927be08e 14439 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14440 else
5bd4f169 14441 {
d881513a
AM
14442 if (h != NULL)
14443 {
f0158f44
AM
14444 if (!htab->elf.dynamic_sections_created
14445 || h->elf.dynindx == -1
14446 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 14447 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
14448 /* This is actually a static link, or it is a
14449 -Bsymbolic link and the symbol is defined
14450 locally, or the symbol was forced to be local
14451 because of a version file. */
14452 ;
14453 else
14454 {
039b3fef 14455 indx = h->elf.dynindx;
d881513a
AM
14456 unresolved_reloc = FALSE;
14457 }
039b3fef 14458 ent = h->elf.got.glist;
d881513a 14459 }
411e1bfb 14460 else
5bd4f169 14461 {
d881513a
AM
14462 if (local_got_ents == NULL)
14463 abort ();
14464 ent = local_got_ents[r_symndx];
5bd4f169 14465 }
d881513a
AM
14466
14467 for (; ent != NULL; ent = ent->next)
31c76678 14468 if (ent->addend == orig_rel.r_addend
e717da7e 14469 && ent->owner == input_bfd
d881513a
AM
14470 && ent->tls_type == tls_type)
14471 break;
5bd4f169 14472 }
411e1bfb 14473
927be08e
AM
14474 if (ent == NULL)
14475 abort ();
14476 if (ent->is_indirect)
14477 ent = ent->got.ent;
14478 offp = &ent->got.offset;
14479 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14480 if (got == NULL)
14481 abort ();
14482
411e1bfb
AM
14483 /* The offset must always be a multiple of 8. We use the
14484 least significant bit to record whether we have already
14485 processed this entry. */
d881513a 14486 off = *offp;
411e1bfb
AM
14487 if ((off & 1) != 0)
14488 off &= ~1;
5bd4f169
AM
14489 else
14490 {
411e1bfb
AM
14491 /* Generate relocs for the dynamic linker, except in
14492 the case of TLSLD where we'll use one entry per
14493 module. */
25f23106
AM
14494 asection *relgot;
14495 bfd_boolean ifunc;
e717da7e 14496
d881513a 14497 *offp = off | 1;
25f23106
AM
14498 relgot = NULL;
14499 ifunc = (h != NULL
14500 ? h->elf.type == STT_GNU_IFUNC
14501 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14502 if (ifunc)
82e66161
AM
14503 {
14504 relgot = htab->elf.irelplt;
14505 if (indx == 0)
14506 htab->local_ifunc_resolver = 1;
14507 else if (is_static_defined (&h->elf))
14508 htab->maybe_local_ifunc_resolver = 1;
14509 }
f0158f44
AM
14510 else if (indx != 0
14511 || (bfd_link_pic (info)
14512 && (h == NULL
21d68fcd 14513 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 14514 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
14515 && !h->elf.def_dynamic))
14516 && !(tls_type == (TLS_TLS | TLS_TPREL)
14517 && bfd_link_executable (info)
14518 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 14519 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14520 if (relgot != NULL)
5bd4f169 14521 {
e717da7e
AM
14522 outrel.r_offset = (got->output_section->vma
14523 + got->output_offset
411e1bfb 14524 + off);
4cc603a5 14525 outrel.r_addend = addend;
d881513a 14526 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14527 {
411e1bfb 14528 outrel.r_addend = 0;
e515b051 14529 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14530 if (tls_type == (TLS_TLS | TLS_GD))
14531 {
e717da7e
AM
14532 loc = relgot->contents;
14533 loc += (relgot->reloc_count++
d881513a
AM
14534 * sizeof (Elf64_External_Rela));
14535 bfd_elf64_swap_reloca_out (output_bfd,
14536 &outrel, loc);
e515b051 14537 outrel.r_offset += 8;
4cc603a5 14538 outrel.r_addend = addend;
d881513a
AM
14539 outrel.r_info
14540 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14541 }
411e1bfb 14542 }
951fd09b 14543 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14544 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14545 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14546 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14547 else if (indx != 0)
14548 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14549 else
81407a69 14550 {
25f23106
AM
14551 if (ifunc)
14552 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14553 else
14554 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14555
14556 /* Write the .got section contents for the sake
14557 of prelink. */
e717da7e 14558 loc = got->contents + off;
23fbd6fa
JJ
14559 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14560 loc);
81407a69 14561 }
81407a69
AM
14562
14563 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14564 {
14565 outrel.r_addend += relocation;
14566 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14567 {
14568 if (htab->elf.tls_sec == NULL)
14569 outrel.r_addend = 0;
14570 else
14571 outrel.r_addend -= htab->elf.tls_sec->vma;
14572 }
e515b051 14573 }
e717da7e
AM
14574 loc = relgot->contents;
14575 loc += (relgot->reloc_count++
411e1bfb
AM
14576 * sizeof (Elf64_External_Rela));
14577 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14578 }
14579
ad8e1ba5 14580 /* Init the .got section contents here if we're not
81407a69 14581 emitting a reloc. */
d881513a 14582 else
411e1bfb 14583 {
4cc603a5 14584 relocation += addend;
f0158f44 14585 if (tls_type != 0)
411e1bfb 14586 {
989f9879
AM
14587 if (htab->elf.tls_sec == NULL)
14588 relocation = 0;
14589 else
14590 {
f0158f44
AM
14591 if (tls_type & TLS_LD)
14592 relocation = 0;
14593 else
14594 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 14595 if (tls_type & TLS_TPREL)
989f9879
AM
14596 relocation += DTP_OFFSET - TP_OFFSET;
14597 }
5bd4f169 14598
f0158f44 14599 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
14600 {
14601 bfd_put_64 (output_bfd, relocation,
e717da7e 14602 got->contents + off + 8);
676ee2b5 14603 relocation = 1;
7b609f53 14604 }
411e1bfb
AM
14605 }
14606 bfd_put_64 (output_bfd, relocation,
e717da7e 14607 got->contents + off);
5bd4f169
AM
14608 }
14609 }
14610
65f38f15
AM
14611 if (off >= (bfd_vma) -2)
14612 abort ();
14613
bf102f86 14614 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14615 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14616 }
65f38f15
AM
14617 break;
14618
14619 case R_PPC64_PLT16_HA:
14620 case R_PPC64_PLT16_HI:
14621 case R_PPC64_PLT16_LO:
14622 case R_PPC64_PLT32:
14623 case R_PPC64_PLT64:
14624 /* Relocation is to the entry for this symbol in the
14625 procedure linkage table. */
cbf95972
AM
14626 {
14627 struct plt_entry **plt_list = NULL;
14628 if (h != NULL)
14629 plt_list = &h->elf.plt.plist;
14630 else if (local_got_ents != NULL)
14631 {
14632 struct plt_entry **local_plt = (struct plt_entry **)
14633 (local_got_ents + symtab_hdr->sh_info);
14634 unsigned char *local_got_tls_masks = (unsigned char *)
14635 (local_plt + symtab_hdr->sh_info);
14636 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14637 plt_list = local_plt + r_symndx;
14638 }
14639 if (plt_list)
14640 {
14641 struct plt_entry *ent;
65f38f15 14642
cbf95972
AM
14643 for (ent = *plt_list; ent != NULL; ent = ent->next)
14644 if (ent->plt.offset != (bfd_vma) -1
14645 && ent->addend == orig_rel.r_addend)
14646 {
14647 asection *plt;
14648
14649 plt = htab->elf.splt;
14650 if (!htab->elf.dynamic_sections_created
14651 || h == NULL
14652 || h->elf.dynindx == -1)
14653 plt = htab->elf.iplt;
14654 relocation = (plt->output_section->vma
14655 + plt->output_offset
14656 + ent->plt.offset);
14657 addend = 0;
14658 unresolved_reloc = FALSE;
14659 break;
14660 }
14661 }
14662 }
65f38f15 14663 break;
5bd4f169 14664
0b13192e
AM
14665 case R_PPC64_TOC:
14666 /* Relocation value is TOC base. */
14667 relocation = TOCstart;
cf35638d 14668 if (r_symndx == STN_UNDEF)
6f20ed8a 14669 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14670 else if (unresolved_reloc)
14671 ;
6f20ed8a
AM
14672 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14673 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14674 else
14675 unresolved_reloc = TRUE;
ab96bf03 14676 goto dodyn;
0b13192e 14677
5bd4f169
AM
14678 /* TOC16 relocs. We want the offset relative to the TOC base,
14679 which is the address of the start of the TOC plus 0x8000.
14680 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14681 in this order. */
65f38f15
AM
14682 case R_PPC64_TOC16:
14683 case R_PPC64_TOC16_LO:
14684 case R_PPC64_TOC16_HI:
14685 case R_PPC64_TOC16_DS:
14686 case R_PPC64_TOC16_LO_DS:
14687 case R_PPC64_TOC16_HA:
6f20ed8a 14688 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14689 break;
14690
14691 /* Relocate against the beginning of the section. */
65f38f15
AM
14692 case R_PPC64_SECTOFF:
14693 case R_PPC64_SECTOFF_LO:
14694 case R_PPC64_SECTOFF_HI:
14695 case R_PPC64_SECTOFF_DS:
14696 case R_PPC64_SECTOFF_LO_DS:
14697 case R_PPC64_SECTOFF_HA:
4ce794b7 14698 if (sec != NULL)
65f38f15 14699 addend -= sec->output_section->vma;
5bd4f169
AM
14700 break;
14701
25f23106
AM
14702 case R_PPC64_REL16:
14703 case R_PPC64_REL16_LO:
14704 case R_PPC64_REL16_HI:
14705 case R_PPC64_REL16_HA:
a680de9a 14706 case R_PPC64_REL16DX_HA:
25f23106
AM
14707 break;
14708
721956f4
AM
14709 case R_PPC64_REL14:
14710 case R_PPC64_REL14_BRNTAKEN:
14711 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
14712 case R_PPC64_REL24:
14713 break;
14714
411e1bfb
AM
14715 case R_PPC64_TPREL16:
14716 case R_PPC64_TPREL16_LO:
14717 case R_PPC64_TPREL16_HI:
14718 case R_PPC64_TPREL16_HA:
14719 case R_PPC64_TPREL16_DS:
14720 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14721 case R_PPC64_TPREL16_HIGH:
14722 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14723 case R_PPC64_TPREL16_HIGHER:
14724 case R_PPC64_TPREL16_HIGHERA:
14725 case R_PPC64_TPREL16_HIGHEST:
14726 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14727 if (h != NULL
14728 && h->elf.root.type == bfd_link_hash_undefweak
14729 && h->elf.dynindx == -1)
14730 {
14731 /* Make this relocation against an undefined weak symbol
14732 resolve to zero. This is really just a tweak, since
14733 code using weak externs ought to check that they are
14734 defined before using them. */
14735 bfd_byte *p = contents + rel->r_offset - d_offset;
14736
95f0d0d2 14737 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
14738 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14739 if (insn != 0)
95f0d0d2 14740 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
14741 break;
14742 }
989f9879
AM
14743 if (htab->elf.tls_sec != NULL)
14744 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
14745 /* The TPREL16 relocs shouldn't really be used in shared
14746 libs or with non-local symbols as that will result in
14747 DT_TEXTREL being set, but support them anyway. */
14748 goto dodyn;
411e1bfb
AM
14749
14750 case R_PPC64_DTPREL16:
14751 case R_PPC64_DTPREL16_LO:
14752 case R_PPC64_DTPREL16_HI:
14753 case R_PPC64_DTPREL16_HA:
14754 case R_PPC64_DTPREL16_DS:
14755 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14756 case R_PPC64_DTPREL16_HIGH:
14757 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14758 case R_PPC64_DTPREL16_HIGHER:
14759 case R_PPC64_DTPREL16_HIGHERA:
14760 case R_PPC64_DTPREL16_HIGHEST:
14761 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14762 if (htab->elf.tls_sec != NULL)
14763 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14764 break;
14765
45965137
AM
14766 case R_PPC64_ADDR64_LOCAL:
14767 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14768 ? h->elf.other
14769 : sym->st_other);
14770 break;
14771
e515b051
AM
14772 case R_PPC64_DTPMOD64:
14773 relocation = 1;
14774 addend = 0;
14775 goto dodyn;
14776
411e1bfb 14777 case R_PPC64_TPREL64:
989f9879
AM
14778 if (htab->elf.tls_sec != NULL)
14779 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14780 goto dodyn;
14781
14782 case R_PPC64_DTPREL64:
989f9879
AM
14783 if (htab->elf.tls_sec != NULL)
14784 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 14785 /* Fall through. */
411e1bfb 14786
65f38f15
AM
14787 /* Relocations that may need to be propagated if this is a
14788 dynamic object. */
04c9666a 14789 case R_PPC64_REL30:
65f38f15
AM
14790 case R_PPC64_REL32:
14791 case R_PPC64_REL64:
14792 case R_PPC64_ADDR14:
14793 case R_PPC64_ADDR14_BRNTAKEN:
14794 case R_PPC64_ADDR14_BRTAKEN:
14795 case R_PPC64_ADDR16:
14796 case R_PPC64_ADDR16_DS:
14797 case R_PPC64_ADDR16_HA:
14798 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14799 case R_PPC64_ADDR16_HIGH:
14800 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14801 case R_PPC64_ADDR16_HIGHER:
14802 case R_PPC64_ADDR16_HIGHERA:
14803 case R_PPC64_ADDR16_HIGHEST:
14804 case R_PPC64_ADDR16_HIGHESTA:
14805 case R_PPC64_ADDR16_LO:
14806 case R_PPC64_ADDR16_LO_DS:
14807 case R_PPC64_ADDR24:
65f38f15
AM
14808 case R_PPC64_ADDR32:
14809 case R_PPC64_ADDR64:
14810 case R_PPC64_UADDR16:
14811 case R_PPC64_UADDR32:
14812 case R_PPC64_UADDR64:
411e1bfb 14813 dodyn:
5d1634d7 14814 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14815 break;
14816
41bd81ab
AM
14817 if (NO_OPD_RELOCS && is_opd)
14818 break;
14819
8a9e8e72 14820 if (bfd_link_pic (info)
b1b07054
AM
14821 ? ((h == NULL
14822 || h->dyn_relocs != NULL)
14823 && ((h != NULL && pc_dynrelocs (h))
14824 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
14825 : (h != NULL
14826 ? h->dyn_relocs != NULL
d311bc8b 14827 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 14828 {
b34976b6 14829 bfd_boolean skip, relocate;
65f38f15 14830 asection *sreloc;
1cf1f670 14831 bfd_vma out_off;
82e66161 14832 long indx = 0;
65f38f15
AM
14833
14834 /* When generating a dynamic object, these relocations
14835 are copied into the output file to be resolved at run
14836 time. */
14837
b34976b6
AM
14838 skip = FALSE;
14839 relocate = FALSE;
65f38f15 14840
1cf1f670
AM
14841 out_off = _bfd_elf_section_offset (output_bfd, info,
14842 input_section, rel->r_offset);
14843 if (out_off == (bfd_vma) -1)
b34976b6 14844 skip = TRUE;
1cf1f670 14845 else if (out_off == (bfd_vma) -2)
b34976b6 14846 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14847 out_off += (input_section->output_section->vma
14848 + input_section->output_offset);
14849 outrel.r_offset = out_off;
411e1bfb 14850 outrel.r_addend = rel->r_addend;
65f38f15 14851
1cf1f670
AM
14852 /* Optimize unaligned reloc use. */
14853 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14854 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14855 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14856 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14857 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14858 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14859 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14860 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14861 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14862
65f38f15 14863 if (skip)
0bb2d96a 14864 memset (&outrel, 0, sizeof outrel);
afe397ea 14865 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14866 && !is_opd
14867 && r_type != R_PPC64_TOC)
14acf4dc 14868 {
82e66161
AM
14869 indx = h->elf.dynindx;
14870 BFD_ASSERT (indx != -1);
14871 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 14872 }
65f38f15
AM
14873 else
14874 {
41bd81ab
AM
14875 /* This symbol is local, or marked to become local,
14876 or this is an opd section reloc which must point
14877 at a local function. */
65f38f15 14878 outrel.r_addend += relocation;
e86ce104 14879 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14880 {
3fad3c7c 14881 if (is_opd && h != NULL)
afbe61cf
AM
14882 {
14883 /* Lie about opd entries. This case occurs
14884 when building shared libraries and we
14885 reference a function in another shared
3fad3c7c
AM
14886 lib. The same thing happens for a weak
14887 definition in an application that's
14888 overridden by a strong definition in a
14889 shared lib. (I believe this is a generic
14890 bug in binutils handling of weak syms.)
14891 In these cases we won't use the opd
1e2f5b6e 14892 entry in this lib. */
b34976b6 14893 unresolved_reloc = FALSE;
afbe61cf 14894 }
25f23106
AM
14895 if (!is_opd
14896 && r_type == R_PPC64_ADDR64
14897 && (h != NULL
14898 ? h->elf.type == STT_GNU_IFUNC
14899 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14900 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14901 else
14902 {
14903 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14904
25f23106
AM
14905 /* We need to relocate .opd contents for ld.so.
14906 Prelink also wants simple and consistent rules
14907 for relocs. This make all RELATIVE relocs have
14908 *r_offset equal to r_addend. */
14909 relocate = TRUE;
14910 }
65f38f15
AM
14911 }
14912 else
14913 {
25f23106
AM
14914 if (h != NULL
14915 ? h->elf.type == STT_GNU_IFUNC
14916 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14917 {
25f53a85 14918 info->callbacks->einfo
695344c0 14919 /* xgettext:c-format */
174d0a74 14920 (_("%H: %s for indirect "
c1c8c1ef 14921 "function `%pT' unsupported\n"),
25f53a85 14922 input_bfd, input_section, rel->r_offset,
25f23106
AM
14923 ppc64_elf_howto_table[r_type]->name,
14924 sym_name);
14925 ret = FALSE;
14926 }
cf35638d 14927 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14928 ;
14929 else if (sec == NULL || sec->owner == NULL)
14930 {
14931 bfd_set_error (bfd_error_bad_value);
b34976b6 14932 return FALSE;
65f38f15
AM
14933 }
14934 else
14935 {
14936 asection *osec;
14937
14938 osec = sec->output_section;
14939 indx = elf_section_data (osec)->dynindx;
14940
74541ad4
AM
14941 if (indx == 0)
14942 {
14943 if ((osec->flags & SEC_READONLY) == 0
14944 && htab->elf.data_index_section != NULL)
14945 osec = htab->elf.data_index_section;
14946 else
14947 osec = htab->elf.text_index_section;
14948 indx = elf_section_data (osec)->dynindx;
14949 }
14950 BFD_ASSERT (indx != 0);
14951
65f38f15
AM
14952 /* We are turning this relocation into one
14953 against a section symbol, so subtract out
14954 the output section's address but not the
14955 offset of the input section in the output
14956 section. */
14957 outrel.r_addend -= osec->vma;
14958 }
14959
14960 outrel.r_info = ELF64_R_INFO (indx, r_type);
14961 }
14962 }
14963
14964 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14965 if (h != NULL
14966 ? h->elf.type == STT_GNU_IFUNC
14967 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
14968 {
14969 sreloc = htab->elf.irelplt;
14970 if (indx == 0)
14971 htab->local_ifunc_resolver = 1;
14972 else if (is_static_defined (&h->elf))
14973 htab->maybe_local_ifunc_resolver = 1;
14974 }
65f38f15
AM
14975 if (sreloc == NULL)
14976 abort ();
14977
dfbb6ac9
AM
14978 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14979 >= sreloc->size)
14980 abort ();
947216bf
AM
14981 loc = sreloc->contents;
14982 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14983 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14984
14985 /* If this reloc is against an external symbol, it will
14986 be computed at runtime, so there's no need to do
81407a69
AM
14987 anything now. However, for the sake of prelink ensure
14988 that the section contents are a known value. */
65f38f15 14989 if (! relocate)
81407a69
AM
14990 {
14991 unresolved_reloc = FALSE;
14992 /* The value chosen here is quite arbitrary as ld.so
14993 ignores section contents except for the special
14994 case of .opd where the contents might be accessed
14995 before relocation. Choose zero, as that won't
14996 cause reloc overflow. */
14997 relocation = 0;
14998 addend = 0;
14999 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15000 to improve backward compatibility with older
15001 versions of ld. */
15002 if (r_type == R_PPC64_ADDR64)
15003 addend = outrel.r_addend;
15004 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 15005 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 15006 addend = outrel.r_offset;
81407a69 15007 }
65f38f15 15008 }
5bd4f169
AM
15009 break;
15010
65f38f15
AM
15011 case R_PPC64_COPY:
15012 case R_PPC64_GLOB_DAT:
15013 case R_PPC64_JMP_SLOT:
25f23106 15014 case R_PPC64_JMP_IREL:
65f38f15
AM
15015 case R_PPC64_RELATIVE:
15016 /* We shouldn't ever see these dynamic relocs in relocatable
15017 files. */
ae9a127f 15018 /* Fall through. */
65f38f15
AM
15019
15020 case R_PPC64_PLTGOT16:
15021 case R_PPC64_PLTGOT16_DS:
15022 case R_PPC64_PLTGOT16_HA:
15023 case R_PPC64_PLTGOT16_HI:
15024 case R_PPC64_PLTGOT16_LO:
15025 case R_PPC64_PLTGOT16_LO_DS:
15026 case R_PPC64_PLTREL32:
15027 case R_PPC64_PLTREL64:
15028 /* These ones haven't been implemented yet. */
15029
25f53a85 15030 info->callbacks->einfo
695344c0 15031 /* xgettext:c-format */
c1c8c1ef 15032 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 15033 input_bfd,
4ce794b7 15034 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
15035
15036 bfd_set_error (bfd_error_invalid_operation);
b34976b6 15037 ret = FALSE;
c316a17c 15038 goto copy_reloc;
65f38f15 15039 }
5bd4f169 15040
67f0cbdb
AM
15041 /* Multi-instruction sequences that access the TOC can be
15042 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 15043 to nop; addi rb,r2,x; */
67f0cbdb
AM
15044 switch (r_type)
15045 {
15046 default:
15047 break;
15048
15049 case R_PPC64_GOT_TLSLD16_HI:
15050 case R_PPC64_GOT_TLSGD16_HI:
15051 case R_PPC64_GOT_TPREL16_HI:
15052 case R_PPC64_GOT_DTPREL16_HI:
15053 case R_PPC64_GOT16_HI:
15054 case R_PPC64_TOC16_HI:
15055 /* These relocs would only be useful if building up an
15056 offset to later add to r2, perhaps in an indexed
15057 addressing mode instruction. Don't try to optimize.
15058 Unfortunately, the possibility of someone building up an
15059 offset like this or even with the HA relocs, means that
15060 we need to check the high insn when optimizing the low
15061 insn. */
15062 break;
15063
15064 case R_PPC64_GOT_TLSLD16_HA:
15065 case R_PPC64_GOT_TLSGD16_HA:
15066 case R_PPC64_GOT_TPREL16_HA:
15067 case R_PPC64_GOT_DTPREL16_HA:
15068 case R_PPC64_GOT16_HA:
15069 case R_PPC64_TOC16_HA:
98528052 15070 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15071 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
15072 {
15073 bfd_byte *p = contents + (rel->r_offset & ~3);
15074 bfd_put_32 (input_bfd, NOP, p);
d830549d 15075 goto copy_reloc;
98528052 15076 }
67f0cbdb
AM
15077 break;
15078
15079 case R_PPC64_GOT_TLSLD16_LO:
15080 case R_PPC64_GOT_TLSGD16_LO:
15081 case R_PPC64_GOT_TPREL16_LO_DS:
15082 case R_PPC64_GOT_DTPREL16_LO_DS:
15083 case R_PPC64_GOT16_LO:
15084 case R_PPC64_GOT16_LO_DS:
15085 case R_PPC64_TOC16_LO:
15086 case R_PPC64_TOC16_LO_DS:
98528052 15087 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15088 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15089 {
15090 bfd_byte *p = contents + (rel->r_offset & ~3);
15091 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15092 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15093 {
15094 /* Transform addic to addi when we change reg. */
15095 insn &= ~((0x3f << 26) | (0x1f << 16));
15096 insn |= (14u << 26) | (2 << 16);
15097 }
15098 else
67f0cbdb 15099 {
98528052
AM
15100 insn &= ~(0x1f << 16);
15101 insn |= 2 << 16;
67f0cbdb 15102 }
560c8763 15103 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15104 }
15105 break;
9a23f96e
AM
15106
15107 case R_PPC64_TPREL16_HA:
15108 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15109 {
15110 bfd_byte *p = contents + (rel->r_offset & ~3);
15111 insn = bfd_get_32 (input_bfd, p);
15112 if ((insn & ((0x3f << 26) | 0x1f << 16))
15113 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15114 /* xgettext:c-format */
15115 info->callbacks->minfo
15116 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
15117 input_bfd, input_section, rel->r_offset,
15118 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 15119 else
d830549d
AM
15120 {
15121 bfd_put_32 (input_bfd, NOP, p);
15122 goto copy_reloc;
15123 }
9a23f96e
AM
15124 }
15125 break;
15126
15127 case R_PPC64_TPREL16_LO:
15128 case R_PPC64_TPREL16_LO_DS:
15129 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15130 {
15131 bfd_byte *p = contents + (rel->r_offset & ~3);
15132 insn = bfd_get_32 (input_bfd, p);
15133 insn &= ~(0x1f << 16);
15134 insn |= 13 << 16;
15135 bfd_put_32 (input_bfd, insn, p);
15136 }
15137 break;
67f0cbdb
AM
15138 }
15139
65f38f15
AM
15140 /* Do any further special processing. */
15141 switch (r_type)
15142 {
15143 default:
15144 break;
15145
25f23106 15146 case R_PPC64_REL16_HA:
a680de9a 15147 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15148 case R_PPC64_ADDR16_HA:
15149 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15150 case R_PPC64_ADDR16_HIGHERA:
15151 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15152 case R_PPC64_TOC16_HA:
15153 case R_PPC64_SECTOFF_HA:
411e1bfb 15154 case R_PPC64_TPREL16_HA:
f9c6b907 15155 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15156 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15157 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15158 case R_PPC64_DTPREL16_HA:
15159 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15160 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15161 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15162 /* It's just possible that this symbol is a weak symbol
15163 that's not actually defined anywhere. In that case,
15164 'sec' would be NULL, and we should leave the symbol
15165 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15166 if (sec == NULL)
15167 break;
1a0670f3 15168 /* Fall through. */
5c5f6e17
AM
15169
15170 case R_PPC64_GOT16_HA:
15171 case R_PPC64_PLTGOT16_HA:
15172 case R_PPC64_PLT16_HA:
15173 case R_PPC64_GOT_TLSGD16_HA:
15174 case R_PPC64_GOT_TLSLD16_HA:
15175 case R_PPC64_GOT_TPREL16_HA:
15176 case R_PPC64_GOT_DTPREL16_HA:
15177 /* Add 0x10000 if sign bit in 0:15 is set.
15178 Bits 0:15 are not used. */
15179 addend += 0x8000;
65f38f15
AM
15180 break;
15181
15182 case R_PPC64_ADDR16_DS:
15183 case R_PPC64_ADDR16_LO_DS:
15184 case R_PPC64_GOT16_DS:
15185 case R_PPC64_GOT16_LO_DS:
15186 case R_PPC64_PLT16_LO_DS:
15187 case R_PPC64_SECTOFF_DS:
15188 case R_PPC64_SECTOFF_LO_DS:
15189 case R_PPC64_TOC16_DS:
15190 case R_PPC64_TOC16_LO_DS:
15191 case R_PPC64_PLTGOT16_DS:
15192 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15193 case R_PPC64_GOT_TPREL16_DS:
15194 case R_PPC64_GOT_TPREL16_LO_DS:
15195 case R_PPC64_GOT_DTPREL16_DS:
15196 case R_PPC64_GOT_DTPREL16_LO_DS:
15197 case R_PPC64_TPREL16_DS:
15198 case R_PPC64_TPREL16_LO_DS:
15199 case R_PPC64_DTPREL16_DS:
15200 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15201 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15202 mask = 3;
a680de9a
PB
15203 /* If this reloc is against an lq, lxv, or stxv insn, then
15204 the value must be a multiple of 16. This is somewhat of
15205 a hack, but the "correct" way to do this by defining _DQ
15206 forms of all the _DS relocs bloats all reloc switches in
15207 this file. It doesn't make much sense to use these
15208 relocs in data, so testing the insn should be safe. */
15209 if ((insn & (0x3f << 26)) == (56u << 26)
15210 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15211 mask = 15;
a680de9a
PB
15212 relocation += addend;
15213 addend = insn & (mask ^ 3);
15214 if ((relocation & mask) != 0)
65f38f15 15215 {
a680de9a 15216 relocation ^= relocation & mask;
25f53a85 15217 info->callbacks->einfo
695344c0 15218 /* xgettext:c-format */
174d0a74 15219 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 15220 input_bfd, input_section, rel->r_offset,
d830549d 15221 ppc64_elf_howto_table[r_type]->name,
adadcc0c 15222 mask + 1);
65f38f15 15223 bfd_set_error (bfd_error_bad_value);
b34976b6 15224 ret = FALSE;
c316a17c 15225 goto copy_reloc;
65f38f15
AM
15226 }
15227 break;
5bd4f169
AM
15228 }
15229
239e1f3a
AM
15230 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15231 because such sections are not SEC_ALLOC and thus ld.so will
15232 not process them. */
d830549d 15233 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 15234 if (unresolved_reloc
239e1f3a 15235 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15236 && h->elf.def_dynamic)
15237 && _bfd_elf_section_offset (output_bfd, info, input_section,
15238 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15239 {
25f53a85 15240 info->callbacks->einfo
695344c0 15241 /* xgettext:c-format */
c1c8c1ef 15242 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 15243 input_bfd, input_section, rel->r_offset,
b80eed39 15244 howto->name,
039b3fef 15245 h->elf.root.root.string);
b34976b6 15246 ret = FALSE;
9c07fe7c 15247 }
5bd4f169 15248
b80eed39
AM
15249 /* 16-bit fields in insns mostly have signed values, but a
15250 few insns have 16-bit unsigned values. Really, we should
15251 have different reloc types. */
15252 if (howto->complain_on_overflow != complain_overflow_dont
15253 && howto->dst_mask == 0xffff
15254 && (input_section->flags & SEC_CODE) != 0)
15255 {
15256 enum complain_overflow complain = complain_overflow_signed;
15257
15258 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15259 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15260 complain = complain_overflow_bitfield;
15261 else if (howto->rightshift == 0
15262 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15263 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15264 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15265 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15266 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15267 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15268 complain = complain_overflow_unsigned;
15269 if (howto->complain_on_overflow != complain)
15270 {
15271 alt_howto = *howto;
15272 alt_howto.complain_on_overflow = complain;
15273 howto = &alt_howto;
15274 }
15275 }
15276
a680de9a
PB
15277 if (r_type == R_PPC64_REL16DX_HA)
15278 {
15279 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15280 if (rel->r_offset + 4 > input_section->size)
15281 r = bfd_reloc_outofrange;
15282 else
15283 {
15284 relocation += addend;
15285 relocation -= (rel->r_offset
15286 + input_section->output_offset
15287 + input_section->output_section->vma);
3de43e7b 15288 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
15289 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15290 insn &= ~0x1fffc1;
3de43e7b 15291 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
15292 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15293 r = bfd_reloc_ok;
3de43e7b 15294 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
15295 r = bfd_reloc_overflow;
15296 }
15297 }
15298 else
15299 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15300 rel->r_offset, relocation, addend);
5bd4f169 15301
ef60b7ff 15302 if (r != bfd_reloc_ok)
5bd4f169 15303 {
bc30df16 15304 char *more_info = NULL;
b80eed39 15305 const char *reloc_name = howto->name;
bc30df16
AM
15306
15307 if (reloc_dest != DEST_NORMAL)
15308 {
15309 more_info = bfd_malloc (strlen (reloc_name) + 8);
15310 if (more_info != NULL)
15311 {
15312 strcpy (more_info, reloc_name);
15313 strcat (more_info, (reloc_dest == DEST_OPD
15314 ? " (OPD)" : " (stub)"));
15315 reloc_name = more_info;
15316 }
15317 }
15318
cd27b276 15319 if (r == bfd_reloc_overflow)
5bd4f169 15320 {
8131c122
AM
15321 /* On code like "if (foo) foo();" don't report overflow
15322 on a branch to zero when foo is undefined. */
15323 if (!warned
15324 && (reloc_dest == DEST_STUB
15325 || !(h != NULL
15326 && (h->elf.root.type == bfd_link_hash_undefweak
15327 || h->elf.root.type == bfd_link_hash_undefined)
15328 && is_branch_reloc (r_type))))
1a72702b
AM
15329 info->callbacks->reloc_overflow (info, &h->elf.root,
15330 sym_name, reloc_name,
15331 orig_rel.r_addend,
15332 input_bfd, input_section,
15333 rel->r_offset);
ef60b7ff
AM
15334 }
15335 else
15336 {
25f53a85 15337 info->callbacks->einfo
695344c0 15338 /* xgettext:c-format */
c1c8c1ef 15339 (_("%H: %s against `%pT': error %d\n"),
25f53a85 15340 input_bfd, input_section, rel->r_offset,
bc30df16 15341 reloc_name, sym_name, (int) r);
b34976b6 15342 ret = FALSE;
ef60b7ff 15343 }
bc30df16
AM
15344 if (more_info != NULL)
15345 free (more_info);
5bd4f169 15346 }
c316a17c
AM
15347 copy_reloc:
15348 if (wrel != rel)
15349 *wrel = *rel;
15350 }
15351
15352 if (wrel != rel)
15353 {
15354 Elf_Internal_Shdr *rel_hdr;
15355 size_t deleted = rel - wrel;
15356
15357 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15358 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15359 if (rel_hdr->sh_size == 0)
15360 {
15361 /* It is too late to remove an empty reloc section. Leave
15362 one NONE reloc.
15363 ??? What is wrong with an empty section??? */
15364 rel_hdr->sh_size = rel_hdr->sh_entsize;
15365 deleted -= 1;
15366 }
15367 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15368 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15369 input_section->reloc_count -= deleted;
5bd4f169
AM
15370 }
15371
645ea6a9
AM
15372 /* If we're emitting relocations, then shortly after this function
15373 returns, reloc offsets and addends for this section will be
15374 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15375 file rather than the input. Save a copy of the relocs for
15376 opd_entry_value. */
0e1862bb 15377 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15378 {
15379 bfd_size_type amt;
15380 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15381 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15382 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15383 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15384 if (rel == NULL)
15385 return FALSE;
15386 memcpy (rel, relocs, amt);
15387 }
5bd4f169
AM
15388 return ret;
15389}
15390
754021d0
AM
15391/* Adjust the value of any local symbols in opd sections. */
15392
6e0b88f1 15393static int
754021d0
AM
15394ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15395 const char *name ATTRIBUTE_UNUSED,
15396 Elf_Internal_Sym *elfsym,
15397 asection *input_sec,
15398 struct elf_link_hash_entry *h)
15399{
74f0fb50
AM
15400 struct _opd_sec_data *opd;
15401 long adjust;
754021d0
AM
15402 bfd_vma value;
15403
4025353c 15404 if (h != NULL)
6e0b88f1 15405 return 1;
4025353c 15406
74f0fb50
AM
15407 opd = get_opd_info (input_sec);
15408 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15409 return 1;
754021d0
AM
15410
15411 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15412 if (!bfd_link_relocatable (info))
754021d0
AM
15413 value -= input_sec->output_section->vma;
15414
51aecdc5 15415 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15416 if (adjust == -1)
6e0b88f1
AM
15417 return 2;
15418
15419 elfsym->st_value += adjust;
15420 return 1;
754021d0
AM
15421}
15422
5bd4f169
AM
15423/* Finish up dynamic symbol handling. We set the contents of various
15424 dynamic sections here. */
15425
b34976b6 15426static bfd_boolean
4ce794b7
AM
15427ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15428 struct bfd_link_info *info,
15429 struct elf_link_hash_entry *h,
4aef7643 15430 Elf_Internal_Sym *sym)
5bd4f169 15431{
65f38f15 15432 struct ppc_link_hash_table *htab;
8387904d
AM
15433 struct plt_entry *ent;
15434 Elf_Internal_Rela rela;
15435 bfd_byte *loc;
5bd4f169 15436
65f38f15 15437 htab = ppc_hash_table (info);
4dfe6ac6
NC
15438 if (htab == NULL)
15439 return FALSE;
5bd4f169 15440
8387904d
AM
15441 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15442 if (ent->plt.offset != (bfd_vma) -1)
15443 {
15444 /* This symbol has an entry in the procedure linkage
15445 table. Set it up. */
e054468f
AM
15446 if (!htab->elf.dynamic_sections_created
15447 || h->dynindx == -1)
15448 {
15449 BFD_ASSERT (h->type == STT_GNU_IFUNC
15450 && h->def_regular
15451 && (h->root.type == bfd_link_hash_defined
15452 || h->root.type == bfd_link_hash_defweak));
33e44f2e
AM
15453 rela.r_offset = (htab->elf.iplt->output_section->vma
15454 + htab->elf.iplt->output_offset
25f23106 15455 + ent->plt.offset);
ee67d69a
AM
15456 if (htab->opd_abi)
15457 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15458 else
15459 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
15460 rela.r_addend = (h->root.u.def.value
15461 + h->root.u.def.section->output_offset
15462 + h->root.u.def.section->output_section->vma
15463 + ent->addend);
33e44f2e
AM
15464 loc = (htab->elf.irelplt->contents
15465 + (htab->elf.irelplt->reloc_count++
25f23106 15466 * sizeof (Elf64_External_Rela)));
82e66161 15467 htab->local_ifunc_resolver = 1;
e054468f
AM
15468 }
15469 else
15470 {
33e44f2e
AM
15471 rela.r_offset = (htab->elf.splt->output_section->vma
15472 + htab->elf.splt->output_offset
25f23106 15473 + ent->plt.offset);
e054468f
AM
15474 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15475 rela.r_addend = ent->addend;
33e44f2e 15476 loc = (htab->elf.srelplt->contents
b9e5796b
AM
15477 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15478 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
82e66161
AM
15479 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
15480 htab->maybe_local_ifunc_resolver = 1;
e054468f 15481 }
8387904d 15482 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a345bc8d
AM
15483
15484 if (!htab->opd_abi)
15485 {
15486 if (!h->def_regular)
15487 {
15488 /* Mark the symbol as undefined, rather than as
15489 defined in glink. Leave the value if there were
15490 any relocations where pointer equality matters
15491 (this is a clue for the dynamic linker, to make
15492 function pointer comparisons work between an
15493 application and shared library), otherwise set it
15494 to zero. */
15495 sym->st_shndx = SHN_UNDEF;
15496 if (!h->pointer_equality_needed)
15497 sym->st_value = 0;
15498 else if (!h->ref_regular_nonweak)
15499 {
15500 /* This breaks function pointer comparisons, but
15501 that is better than breaking tests for a NULL
15502 function pointer. */
15503 sym->st_value = 0;
15504 }
15505 }
15506 }
8387904d 15507 }
5bd4f169 15508
f5385ebf 15509 if (h->needs_copy)
5bd4f169 15510 {
65f38f15 15511 /* This symbol needs a copy reloc. Set it up. */
5474d94f 15512 asection *srel;
5bd4f169 15513
65f38f15
AM
15514 if (h->dynindx == -1
15515 || (h->root.type != bfd_link_hash_defined
15516 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
15517 || htab->elf.srelbss == NULL
15518 || htab->elf.sreldynrelro == NULL)
65f38f15 15519 abort ();
5bd4f169
AM
15520
15521 rela.r_offset = (h->root.u.def.value
15522 + h->root.u.def.section->output_section->vma
15523 + h->root.u.def.section->output_offset);
15524 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15525 rela.r_addend = 0;
afbf7e8e 15526 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
15527 srel = htab->elf.sreldynrelro;
15528 else
15529 srel = htab->elf.srelbss;
15530 loc = srel->contents;
15531 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15532 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15533 }
15534
b34976b6 15535 return TRUE;
5bd4f169
AM
15536}
15537
65f38f15
AM
15538/* Used to decide how to sort relocs in an optimal manner for the
15539 dynamic linker, before writing them out. */
15540
15541static enum elf_reloc_type_class
7e612e98
AM
15542ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15543 const asection *rel_sec,
15544 const Elf_Internal_Rela *rela)
65f38f15 15545{
04c9666a 15546 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15547 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15548
33e44f2e 15549 if (rel_sec == htab->elf.irelplt)
7e612e98 15550 return reloc_class_ifunc;
a33d1f77 15551
4ce794b7 15552 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15553 switch (r_type)
65f38f15
AM
15554 {
15555 case R_PPC64_RELATIVE:
15556 return reloc_class_relative;
15557 case R_PPC64_JMP_SLOT:
15558 return reloc_class_plt;
15559 case R_PPC64_COPY:
15560 return reloc_class_copy;
15561 default:
15562 return reloc_class_normal;
15563 }
15564}
15565
5bd4f169
AM
15566/* Finish up the dynamic sections. */
15567
b34976b6 15568static bfd_boolean
4ce794b7
AM
15569ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15570 struct bfd_link_info *info)
5bd4f169 15571{
65f38f15
AM
15572 struct ppc_link_hash_table *htab;
15573 bfd *dynobj;
5bd4f169 15574 asection *sdyn;
5bd4f169 15575
65f38f15 15576 htab = ppc_hash_table (info);
4dfe6ac6
NC
15577 if (htab == NULL)
15578 return FALSE;
15579
65f38f15 15580 dynobj = htab->elf.dynobj;
3d4d4302 15581 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15582
65f38f15 15583 if (htab->elf.dynamic_sections_created)
5bd4f169 15584 {
5bd4f169
AM
15585 Elf64_External_Dyn *dyncon, *dynconend;
15586
33e44f2e 15587 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15588 abort ();
5bd4f169
AM
15589
15590 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15591 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15592 for (; dyncon < dynconend; dyncon++)
15593 {
15594 Elf_Internal_Dyn dyn;
19397422 15595 asection *s;
5bd4f169
AM
15596
15597 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15598
15599 switch (dyn.d_tag)
15600 {
65f38f15
AM
15601 default:
15602 continue;
5bd4f169 15603
5d1634d7 15604 case DT_PPC64_GLINK:
4ce794b7 15605 s = htab->glink;
6348e046 15606 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15607 /* We stupidly defined DT_PPC64_GLINK to be the start
15608 of glink rather than the first entry point, which is
15609 what ld.so needs, and now have a bigger stub to
15610 support automatic multiple TOCs. */
9e390558 15611 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
15612 break;
15613
19397422
AM
15614 case DT_PPC64_OPD:
15615 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15616 if (s == NULL)
15617 continue;
15618 dyn.d_un.d_ptr = s->vma;
19397422
AM
15619 break;
15620
e8910a83
AM
15621 case DT_PPC64_OPT:
15622 if (htab->do_multi_toc && htab->multi_toc_needed)
15623 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
15624 if (htab->has_plt_localentry0)
15625 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
15626 break;
15627
19397422
AM
15628 case DT_PPC64_OPDSZ:
15629 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15630 if (s == NULL)
15631 continue;
eea6121a 15632 dyn.d_un.d_val = s->size;
19397422
AM
15633 break;
15634
65f38f15 15635 case DT_PLTGOT:
33e44f2e 15636 s = htab->elf.splt;
6348e046 15637 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15638 break;
15639
15640 case DT_JMPREL:
33e44f2e 15641 s = htab->elf.srelplt;
6348e046 15642 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15643 break;
5bd4f169 15644
65f38f15 15645 case DT_PLTRELSZ:
33e44f2e 15646 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 15647 break;
82e66161
AM
15648
15649 case DT_TEXTREL:
15650 if (htab->local_ifunc_resolver)
15651 info->callbacks->einfo
15652 (_("%X%P: text relocations and GNU indirect "
15653 "functions will result in a segfault at runtime\n"));
15654 else if (htab->maybe_local_ifunc_resolver)
15655 info->callbacks->einfo
15656 (_("%P: warning: text relocations and GNU indirect "
15657 "functions may result in a segfault at runtime\n"));
15658 continue;
5bd4f169 15659 }
5bd4f169 15660
65f38f15 15661 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15662 }
5bd4f169
AM
15663 }
15664
6528b6eb
AM
15665 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15666 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15667 {
15668 /* Fill in the first entry in the global offset table.
15669 We use it to hold the link-time TOCbase. */
15670 bfd_put_64 (output_bfd,
60ee0d4a 15671 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15672 htab->elf.sgot->contents);
5d1634d7
AM
15673
15674 /* Set .got entry size. */
33e44f2e 15675 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
15676 }
15677
6528b6eb
AM
15678 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15679 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15680 {
15681 /* Set .plt entry size. */
33e44f2e 15682 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15683 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15684 }
15685
84f5d08e
AM
15686 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15687 brlt ourselves if emitrelocations. */
15688 if (htab->brlt != NULL
15689 && htab->brlt->reloc_count != 0
15690 && !_bfd_elf_link_output_relocs (output_bfd,
15691 htab->brlt,
d4730f92 15692 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15693 elf_section_data (htab->brlt)->relocs,
15694 NULL))
15695 return FALSE;
15696
176a0d42
AM
15697 if (htab->glink != NULL
15698 && htab->glink->reloc_count != 0
15699 && !_bfd_elf_link_output_relocs (output_bfd,
15700 htab->glink,
d4730f92 15701 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15702 elf_section_data (htab->glink)->relocs,
15703 NULL))
15704 return FALSE;
15705
58d180e8 15706 if (htab->glink_eh_frame != NULL
da44f4e5
AM
15707 && htab->glink_eh_frame->size != 0)
15708 {
15709 bfd_vma val;
15710 bfd_byte *p;
d4aaa2a0 15711 struct map_stub *group;
2e0ce1c8 15712 size_t align = 4;
da44f4e5 15713
2e0ce1c8
AM
15714 p = htab->glink_eh_frame->contents;
15715 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
d4aaa2a0
AM
15716
15717 for (group = htab->group; group != NULL; group = group->next)
15718 if (group->stub_sec != NULL)
da44f4e5 15719 {
da44f4e5 15720 /* Offset to stub section. */
d4aaa2a0
AM
15721 val = (group->stub_sec->output_section->vma
15722 + group->stub_sec->output_offset);
da44f4e5
AM
15723 val -= (htab->glink_eh_frame->output_section->vma
15724 + htab->glink_eh_frame->output_offset
d4aaa2a0 15725 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
15726 if (val + 0x80000000 > 0xffffffff)
15727 {
cf97bcb0
AM
15728 _bfd_error_handler
15729 (_("%s offset too large for .eh_frame sdata4 encoding"),
d4aaa2a0 15730 group->stub_sec->name);
da44f4e5
AM
15731 return FALSE;
15732 }
d4aaa2a0
AM
15733 bfd_put_32 (dynobj, val, p + 8);
15734 p += stub_eh_frame_size (group, align);
da44f4e5
AM
15735 }
15736 if (htab->glink != NULL && htab->glink->size != 0)
15737 {
da44f4e5
AM
15738 /* Offset to .glink. */
15739 val = (htab->glink->output_section->vma
15740 + htab->glink->output_offset
15741 + 8);
15742 val -= (htab->glink_eh_frame->output_section->vma
15743 + htab->glink_eh_frame->output_offset
d4aaa2a0 15744 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
15745 if (val + 0x80000000 > 0xffffffff)
15746 {
cf97bcb0
AM
15747 _bfd_error_handler
15748 (_("%s offset too large for .eh_frame sdata4 encoding"),
da44f4e5
AM
15749 htab->glink->name);
15750 return FALSE;
15751 }
d4aaa2a0
AM
15752 bfd_put_32 (dynobj, val, p + 8);
15753 p += (24 + align - 1) & -align;
da44f4e5
AM
15754 }
15755
15756 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15757 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15758 htab->glink_eh_frame,
15759 htab->glink_eh_frame->contents))
15760 return FALSE;
15761 }
58d180e8 15762
e717da7e 15763 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15764 since we didn't add them to DYNOBJ. We know dynobj is the first
15765 bfd. */
c72f2fb2 15766 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15767 {
15768 asection *s;
7b53ace3 15769
0c8d6e5c 15770 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15771 continue;
15772
e717da7e
AM
15773 s = ppc64_elf_tdata (dynobj)->got;
15774 if (s != NULL
eea6121a 15775 && s->size != 0
e717da7e
AM
15776 && s->output_section != bfd_abs_section_ptr
15777 && !bfd_set_section_contents (output_bfd, s->output_section,
15778 s->contents, s->output_offset,
eea6121a 15779 s->size))
e717da7e
AM
15780 return FALSE;
15781 s = ppc64_elf_tdata (dynobj)->relgot;
15782 if (s != NULL
eea6121a 15783 && s->size != 0
e717da7e
AM
15784 && s->output_section != bfd_abs_section_ptr
15785 && !bfd_set_section_contents (output_bfd, s->output_section,
15786 s->contents, s->output_offset,
eea6121a 15787 s->size))
e717da7e
AM
15788 return FALSE;
15789 }
f6c52c13 15790
b34976b6 15791 return TRUE;
5bd4f169
AM
15792}
15793
5bd4f169 15794#include "elf64-target.h"
7b8e7dad
AM
15795
15796/* FreeBSD support */
15797
15798#undef TARGET_LITTLE_SYM
15799#undef TARGET_LITTLE_NAME
15800
15801#undef TARGET_BIG_SYM
6d00b590 15802#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15803#undef TARGET_BIG_NAME
15804#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15805
15806#undef ELF_OSABI
15807#define ELF_OSABI ELFOSABI_FREEBSD
15808
15809#undef elf64_bed
15810#define elf64_bed elf64_powerpc_fbsd_bed
15811
15812#include "elf64-target.h"