Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
219d1afa 2 Copyright (C) 1999-2018 Free Software Foundation, Inc.
5bd4f169
AM
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 5 Largely rewritten by Alan Modra.
5bd4f169 6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 8
ae9a127f
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
5bd4f169 13
ae9a127f
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
5bd4f169 18
4ce794b7
AM
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 22
cd123cb7 23
4ce794b7
AM
24/* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 27
3db64b00 28#include "sysdep.h"
183e98be 29#include <stdarg.h>
5bd4f169 30#include "bfd.h"
5bd4f169
AM
31#include "bfdlink.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
04c9666a 34#include "elf/ppc64.h"
5d1634d7 35#include "elf64-ppc.h"
58d180e8 36#include "dwarf2.h"
5bd4f169 37
805fc799 38static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 54static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 56static bfd_vma opd_entry_value
aef36ac1 57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
5bd4f169 58
6d00b590 59#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 60#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 61#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
62#define TARGET_BIG_NAME "elf64-powerpc"
63#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 64#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
65#define ELF_MACHINE_CODE EM_PPC64
66#define ELF_MAXPAGESIZE 0x10000
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;
0ccf57bd 3272 size_t i, j, count;
90e3cdf2 3273 char *names;
0ccf57bd 3274 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3275 asection *opd = NULL;
90e3cdf2 3276 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3277 asymbol **syms;
ee67d69a 3278 int abi = abiversion (abfd);
90e3cdf2
JJ
3279
3280 *ret = NULL;
3281
ee67d69a
AM
3282 if (abi < 2)
3283 {
3284 opd = bfd_get_section_by_name (abfd, ".opd");
3285 if (opd == NULL && abi == 1)
3286 return 0;
3287 }
90e3cdf2 3288
a5259595
AM
3289 syms = NULL;
3290 codesecsym = 0;
3291 codesecsymend = 0;
3292 secsymend = 0;
3293 opdsymend = 0;
3294 symcount = 0;
3295 if (opd != NULL)
c9727e01 3296 {
a5259595
AM
3297 symcount = static_count;
3298 if (!relocatable)
3299 symcount += dyn_count;
3300 if (symcount == 0)
3301 return 0;
c9727e01 3302
a5259595
AM
3303 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3304 if (syms == NULL)
3305 return -1;
90e3cdf2 3306
a5259595
AM
3307 if (!relocatable && static_count != 0 && dyn_count != 0)
3308 {
3309 /* Use both symbol tables. */
3310 memcpy (syms, static_syms, static_count * sizeof (*syms));
3311 memcpy (syms + static_count, dyn_syms,
3312 (dyn_count + 1) * sizeof (*syms));
3313 }
3314 else if (!relocatable && static_count == 0)
3315 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3316 else
3317 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 3318
0ccf57bd
AM
3319 /* Trim uninteresting symbols. Interesting symbols are section,
3320 function, and notype symbols. */
3321 for (i = 0, j = 0; i < symcount; ++i)
3322 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3323 | BSF_RELC | BSF_SRELC)) == 0)
3324 syms[j++] = syms[i];
3325 symcount = j;
3326
a5259595
AM
3327 synthetic_relocatable = relocatable;
3328 synthetic_opd = opd;
3329 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3330
a5259595
AM
3331 if (!relocatable && symcount > 1)
3332 {
a5259595
AM
3333 /* Trim duplicate syms, since we may have merged the normal and
3334 dynamic symbols. Actually, we only care about syms that have
3335 different values, so trim any with the same value. */
3336 for (i = 1, j = 1; i < symcount; ++i)
3337 if (syms[i - 1]->value + syms[i - 1]->section->vma
3338 != syms[i]->value + syms[i]->section->vma)
3339 syms[j++] = syms[i];
3340 symcount = j;
3341 }
699733f6 3342
a5259595
AM
3343 i = 0;
3344 /* Note that here and in compare_symbols we can't compare opd and
3345 sym->section directly. With separate debug info files, the
3346 symbols will be extracted from the debug file while abfd passed
3347 to this function is the real binary. */
0ccf57bd 3348 if (strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
3349 ++i;
3350 codesecsym = i;
3351
3352 for (; i < symcount; ++i)
3353 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3354 | SEC_THREAD_LOCAL))
3355 != (SEC_CODE | SEC_ALLOC))
3356 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3357 break;
3358 codesecsymend = i;
3359
3360 for (; i < symcount; ++i)
3361 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3362 break;
3363 secsymend = i;
3364
3365 for (; i < symcount; ++i)
3366 if (strcmp (syms[i]->section->name, ".opd") != 0)
3367 break;
3368 opdsymend = i;
3369
3370 for (; i < symcount; ++i)
3371 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3372 != (SEC_CODE | SEC_ALLOC))
3373 break;
3374 symcount = i;
3375 }
c9727e01 3376 count = 0;
90e3cdf2 3377
699733f6 3378 if (relocatable)
90e3cdf2 3379 {
699733f6
AM
3380 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3381 arelent *r;
3382 size_t size;
0ccf57bd 3383 size_t relcount;
90e3cdf2 3384
468392fb
AM
3385 if (opdsymend == secsymend)
3386 goto done;
3387
699733f6 3388 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3389 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3390 if (relcount == 0)
c9727e01 3391 goto done;
90e3cdf2 3392
7356fed5
AM
3393 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3394 {
3395 count = -1;
3396 goto done;
3397 }
3398
699733f6 3399 size = 0;
595da8c5 3400 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3401 {
3402 asymbol *sym;
90e3cdf2 3403
595da8c5 3404 while (r < opd->relocation + relcount
699733f6
AM
3405 && r->address < syms[i]->value + opd->vma)
3406 ++r;
90e3cdf2 3407
595da8c5 3408 if (r == opd->relocation + relcount)
699733f6 3409 break;
90e3cdf2 3410
699733f6
AM
3411 if (r->address != syms[i]->value + opd->vma)
3412 continue;
90e3cdf2 3413
699733f6
AM
3414 if (r->howto->type != R_PPC64_ADDR64)
3415 continue;
90e3cdf2 3416
699733f6
AM
3417 sym = *r->sym_ptr_ptr;
3418 if (!sym_exists_at (syms, opdsymend, symcount,
3419 sym->section->id, sym->value + r->addend))
3420 {
3421 ++count;
3422 size += sizeof (asymbol);
3423 size += strlen (syms[i]->name) + 2;
3424 }
3425 }
90e3cdf2 3426
c4b0b099
AM
3427 if (size == 0)
3428 goto done;
699733f6
AM
3429 s = *ret = bfd_malloc (size);
3430 if (s == NULL)
3431 {
7356fed5 3432 count = -1;
c9727e01 3433 goto done;
699733f6 3434 }
90e3cdf2 3435
699733f6 3436 names = (char *) (s + count);
90e3cdf2 3437
595da8c5 3438 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3439 {
699733f6 3440 asymbol *sym;
90e3cdf2 3441
595da8c5 3442 while (r < opd->relocation + relcount
699733f6
AM
3443 && r->address < syms[i]->value + opd->vma)
3444 ++r;
90e3cdf2 3445
595da8c5 3446 if (r == opd->relocation + relcount)
699733f6
AM
3447 break;
3448
3449 if (r->address != syms[i]->value + opd->vma)
3450 continue;
3451
3452 if (r->howto->type != R_PPC64_ADDR64)
3453 continue;
90e3cdf2 3454
699733f6
AM
3455 sym = *r->sym_ptr_ptr;
3456 if (!sym_exists_at (syms, opdsymend, symcount,
3457 sym->section->id, sym->value + r->addend))
3458 {
3459 size_t len;
3460
3461 *s = *syms[i];
6ba2a415 3462 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3463 s->section = sym->section;
3464 s->value = sym->value + r->addend;
3465 s->name = names;
3466 *names++ = '.';
3467 len = strlen (syms[i]->name);
3468 memcpy (names, syms[i]->name, len + 1);
3469 names += len + 1;
6f610d07
UW
3470 /* Have udata.p point back to the original symbol this
3471 synthetic symbol was derived from. */
3472 s->udata.p = syms[i];
699733f6
AM
3473 s++;
3474 }
3475 }
3476 }
3477 else
90e3cdf2 3478 {
468392fb 3479 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3480 bfd_byte *contents = NULL;
699733f6 3481 size_t size;
0ccf57bd 3482 size_t plt_count = 0;
468392fb
AM
3483 bfd_vma glink_vma = 0, resolv_vma = 0;
3484 asection *dynamic, *glink = NULL, *relplt = NULL;
3485 arelent *p;
90e3cdf2 3486
ee67d69a 3487 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3488 {
c4b0b099
AM
3489 free_contents_and_exit_err:
3490 count = -1;
ee67d69a 3491 free_contents_and_exit:
699733f6 3492 if (contents)
ee67d69a 3493 free (contents);
c9727e01 3494 goto done;
699733f6 3495 }
90e3cdf2 3496
699733f6
AM
3497 size = 0;
3498 for (i = secsymend; i < opdsymend; ++i)
3499 {
3500 bfd_vma ent;
90e3cdf2 3501
5ef11c02
AM
3502 /* Ignore bogus symbols. */
3503 if (syms[i]->value > opd->size - 8)
3504 continue;
3505
699733f6
AM
3506 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3507 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3508 {
3509 ++count;
3510 size += sizeof (asymbol);
3511 size += strlen (syms[i]->name) + 2;
3512 }
3513 }
90e3cdf2 3514
468392fb 3515 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3516 if (dyn_count != 0
3517 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3518 {
3519 bfd_byte *dynbuf, *extdyn, *extdynend;
3520 size_t extdynsize;
3521 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3522
3523 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3524 goto free_contents_and_exit_err;
468392fb
AM
3525
3526 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3527 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3528
3529 extdyn = dynbuf;
3530 extdynend = extdyn + dynamic->size;
3531 for (; extdyn < extdynend; extdyn += extdynsize)
3532 {
3533 Elf_Internal_Dyn dyn;
3534 (*swap_dyn_in) (abfd, extdyn, &dyn);
3535
3536 if (dyn.d_tag == DT_NULL)
3537 break;
3538
3539 if (dyn.d_tag == DT_PPC64_GLINK)
3540 {
9e390558
AM
3541 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3542 See comment in ppc64_elf_finish_dynamic_sections. */
3543 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
3544 /* The .glink section usually does not survive the final
3545 link; search for the section (usually .text) where the
3546 glink stubs now reside. */
3547 glink = bfd_sections_find_if (abfd, section_covers_vma,
3548 &glink_vma);
3549 break;
3550 }
3551 }
3552
3553 free (dynbuf);
3554 }
3555
3556 if (glink != NULL)
3557 {
3558 /* Determine __glink trampoline by reading the relative branch
3559 from the first glink stub. */
3560 bfd_byte buf[4];
b9e5796b
AM
3561 unsigned int off = 0;
3562
3563 while (bfd_get_section_contents (abfd, glink, buf,
3564 glink_vma + off - glink->vma, 4))
468392fb
AM
3565 {
3566 unsigned int insn = bfd_get_32 (abfd, buf);
3567 insn ^= B_DOT;
3568 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3569 {
3570 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3571 break;
3572 }
3573 off += 4;
3574 if (off > 4)
3575 break;
468392fb
AM
3576 }
3577
3578 if (resolv_vma)
3579 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3580
066ee829
AM
3581 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3582 if (relplt != NULL)
3583 {
3584 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3585 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3586 goto free_contents_and_exit_err;
68ffbac6 3587
066ee829
AM
3588 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3589 size += plt_count * sizeof (asymbol);
468392fb 3590
066ee829
AM
3591 p = relplt->relocation;
3592 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3593 {
3594 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3595 if (p->addend != 0)
3596 size += sizeof ("+0x") - 1 + 16;
3597 }
066ee829 3598 }
468392fb
AM
3599 }
3600
c4b0b099
AM
3601 if (size == 0)
3602 goto free_contents_and_exit;
699733f6
AM
3603 s = *ret = bfd_malloc (size);
3604 if (s == NULL)
c4b0b099 3605 goto free_contents_and_exit_err;
90e3cdf2 3606
468392fb 3607 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3608
699733f6 3609 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3610 {
699733f6 3611 bfd_vma ent;
90e3cdf2 3612
5ef11c02
AM
3613 if (syms[i]->value > opd->size - 8)
3614 continue;
3615
699733f6
AM
3616 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3617 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3618 {
0ccf57bd 3619 size_t lo, hi;
699733f6 3620 size_t len;
c9727e01 3621 asection *sec = abfd->sections;
90e3cdf2 3622
699733f6
AM
3623 *s = *syms[i];
3624 lo = codesecsym;
3625 hi = codesecsymend;
3626 while (lo < hi)
3627 {
0ccf57bd 3628 size_t mid = (lo + hi) >> 1;
699733f6
AM
3629 if (syms[mid]->section->vma < ent)
3630 lo = mid + 1;
3631 else if (syms[mid]->section->vma > ent)
3632 hi = mid;
3633 else
c9727e01
AM
3634 {
3635 sec = syms[mid]->section;
3636 break;
3637 }
699733f6
AM
3638 }
3639
c9727e01 3640 if (lo >= hi && lo > codesecsym)
699733f6 3641 sec = syms[lo - 1]->section;
699733f6
AM
3642
3643 for (; sec != NULL; sec = sec->next)
3644 {
3645 if (sec->vma > ent)
3646 break;
63524580
JK
3647 /* SEC_LOAD may not be set if SEC is from a separate debug
3648 info file. */
3649 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3650 break;
3651 if ((sec->flags & SEC_CODE) != 0)
3652 s->section = sec;
3653 }
6ba2a415 3654 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3655 s->value = ent - s->section->vma;
3656 s->name = names;
3657 *names++ = '.';
3658 len = strlen (syms[i]->name);
3659 memcpy (names, syms[i]->name, len + 1);
3660 names += len + 1;
6f610d07
UW
3661 /* Have udata.p point back to the original symbol this
3662 synthetic symbol was derived from. */
3663 s->udata.p = syms[i];
699733f6 3664 s++;
90e3cdf2 3665 }
90e3cdf2 3666 }
699733f6 3667 free (contents);
468392fb
AM
3668
3669 if (glink != NULL && relplt != NULL)
3670 {
3671 if (resolv_vma)
3672 {
3673 /* Add a symbol for the main glink trampoline. */
86a4952b 3674 memset (s, 0, sizeof *s);
468392fb 3675 s->the_bfd = abfd;
6ba2a415 3676 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3677 s->section = glink;
3678 s->value = resolv_vma - glink->vma;
3679 s->name = names;
3680 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3681 names += sizeof ("__glink_PLTresolve");
3682 s++;
3683 count++;
3684 }
3685
3686 /* FIXME: It would be very much nicer to put sym@plt on the
3687 stub rather than on the glink branch table entry. The
3688 objdump disassembler would then use a sensible symbol
3689 name on plt calls. The difficulty in doing so is
3690 a) finding the stubs, and,
3691 b) matching stubs against plt entries, and,
3692 c) there can be multiple stubs for a given plt entry.
3693
3694 Solving (a) could be done by code scanning, but older
3695 ppc64 binaries used different stubs to current code.
3696 (b) is the tricky one since you need to known the toc
3697 pointer for at least one function that uses a pic stub to
3698 be able to calculate the plt address referenced.
3699 (c) means gdb would need to set multiple breakpoints (or
3700 find the glink branch itself) when setting breakpoints
3701 for pending shared library loads. */
3702 p = relplt->relocation;
3703 for (i = 0; i < plt_count; i++, p++)
3704 {
3705 size_t len;
3706
3707 *s = **p->sym_ptr_ptr;
3708 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3709 we are defining a symbol, ensure one of them is set. */
3710 if ((s->flags & BSF_LOCAL) == 0)
3711 s->flags |= BSF_GLOBAL;
6ba2a415 3712 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3713 s->section = glink;
3714 s->value = glink_vma - glink->vma;
3715 s->name = names;
3716 s->udata.p = NULL;
3717 len = strlen ((*p->sym_ptr_ptr)->name);
3718 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3719 names += len;
e054468f
AM
3720 if (p->addend != 0)
3721 {
3722 memcpy (names, "+0x", sizeof ("+0x") - 1);
3723 names += sizeof ("+0x") - 1;
3724 bfd_sprintf_vma (abfd, names, p->addend);
3725 names += strlen (names);
3726 }
468392fb
AM
3727 memcpy (names, "@plt", sizeof ("@plt"));
3728 names += sizeof ("@plt");
3729 s++;
b9e5796b
AM
3730 if (abi < 2)
3731 {
3732 glink_vma += 8;
3733 if (i >= 0x8000)
3734 glink_vma += 4;
3735 }
3736 else
468392fb
AM
3737 glink_vma += 4;
3738 }
3739 count += plt_count;
3740 }
90e3cdf2
JJ
3741 }
3742
c9727e01 3743 done:
a7535cf3 3744 free (syms);
90e3cdf2
JJ
3745 return count;
3746}
5bd4f169 3747\f
65f38f15
AM
3748/* The following functions are specific to the ELF linker, while
3749 functions above are used generally. Those named ppc64_elf_* are
3750 called by the main ELF linker code. They appear in this file more
3751 or less in the order in which they are called. eg.
3752 ppc64_elf_check_relocs is called early in the link process,
3753 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3754 called.
3755
3756 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3757 functions have both a function code symbol and a function descriptor
3758 symbol. A call to foo in a relocatable object file looks like:
3759
3760 . .text
3761 . x:
3762 . bl .foo
3763 . nop
3764
3765 The function definition in another object file might be:
3766
3767 . .section .opd
3768 . foo: .quad .foo
3769 . .quad .TOC.@tocbase
3770 . .quad 0
3771 .
3772 . .text
3773 . .foo: blr
3774
3775 When the linker resolves the call during a static link, the branch
3776 unsurprisingly just goes to .foo and the .opd information is unused.
3777 If the function definition is in a shared library, things are a little
3778 different: The call goes via a plt call stub, the opd information gets
3779 copied to the plt, and the linker patches the nop.
3780
3781 . x:
3782 . bl .foo_stub
3783 . ld 2,40(1)
3784 .
3785 .
3786 . .foo_stub:
71a39c98
AM
3787 . std 2,40(1) # in practice, the call stub
3788 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3789 . addi 11,11,Lfoo@toc@l # this is the general idea
3790 . ld 12,0(11)
3791 . ld 2,8(11)
3792 . mtctr 12
3793 . ld 11,16(11)
e86ce104
AM
3794 . bctr
3795 .
3796 . .section .plt
3797 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3798
3799 The "reloc ()" notation is supposed to indicate that the linker emits
3800 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3801 copying.
3802
3803 What are the difficulties here? Well, firstly, the relocations
3804 examined by the linker in check_relocs are against the function code
3805 sym .foo, while the dynamic relocation in the plt is emitted against
3806 the function descriptor symbol, foo. Somewhere along the line, we need
3807 to carefully copy dynamic link information from one symbol to the other.
3808 Secondly, the generic part of the elf linker will make .foo a dynamic
3809 symbol as is normal for most other backends. We need foo dynamic
3810 instead, at least for an application final link. However, when
3811 creating a shared library containing foo, we need to have both symbols
3812 dynamic so that references to .foo are satisfied during the early
3813 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3814 definition from some other object, eg. a static library.
3815
3816 Update: As of August 2004, we support a new convention. Function
3817 calls may use the function descriptor symbol, ie. "bl foo". This
3818 behaves exactly as "bl .foo". */
65f38f15 3819
7c8bbca5
AM
3820/* Of those relocs that might be copied as dynamic relocs, this
3821 function selects those that must be copied when linking a shared
3822 library or PIE, even when the symbol is local. */
65f38f15 3823
1d483afe
AM
3824static int
3825must_be_dyn_reloc (struct bfd_link_info *info,
3826 enum elf_ppc64_reloc_type r_type)
3827{
3828 switch (r_type)
3829 {
3830 default:
7c8bbca5
AM
3831 /* Only relative relocs can be resolved when the object load
3832 address isn't fixed. DTPREL64 is excluded because the
3833 dynamic linker needs to differentiate global dynamic from
3834 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
3835 return 1;
3836
3837 case R_PPC64_REL32:
3838 case R_PPC64_REL64:
3839 case R_PPC64_REL30:
3840 return 0;
3841
3842 case R_PPC64_TPREL16:
3843 case R_PPC64_TPREL16_LO:
3844 case R_PPC64_TPREL16_HI:
3845 case R_PPC64_TPREL16_HA:
3846 case R_PPC64_TPREL16_DS:
3847 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3848 case R_PPC64_TPREL16_HIGH:
3849 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3850 case R_PPC64_TPREL16_HIGHER:
3851 case R_PPC64_TPREL16_HIGHERA:
3852 case R_PPC64_TPREL16_HIGHEST:
3853 case R_PPC64_TPREL16_HIGHESTA:
3854 case R_PPC64_TPREL64:
7c8bbca5
AM
3855 /* These relocations are relative but in a shared library the
3856 linker doesn't know the thread pointer base. */
3857 return bfd_link_dll (info);
1d483afe
AM
3858 }
3859}
65f38f15 3860
f4656909
AM
3861/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3862 copying dynamic variables from a shared lib into an app's dynbss
3863 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3864 shared lib. With code that gcc generates, it's vital that this be
3865 enabled; In the PowerPC64 ABI, the address of a function is actually
3866 the address of a function descriptor, which resides in the .opd
3867 section. gcc uses the descriptor directly rather than going via the
3868 GOT as some other ABI's do, which means that initialized function
3869 pointers must reference the descriptor. Thus, a function pointer
3870 initialized to the address of a function in a shared library will
3871 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3872 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3873 presents a problem as a plt entry for that function is also
3874 initialized from the function descriptor symbol and the copy reloc
3875 may not be initialized first. */
a23b6845 3876#define ELIMINATE_COPY_RELOCS 1
f4656909 3877
721956f4 3878/* Section name for stubs is the associated section name plus this
29942be8
NC
3879 string. */
3880#define STUB_SUFFIX ".stub"
721956f4
AM
3881
3882/* Linker stubs.
3883 ppc_stub_long_branch:
3884 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3885 destination, but a 24 bit branch in a stub section will reach.
3886 . b dest
3887
3888 ppc_stub_plt_branch:
3889 Similar to the above, but a 24 bit branch in the stub section won't
3890 reach its destination.
71a39c98
AM
3891 . addis %r11,%r2,xxx@toc@ha
3892 . ld %r12,xxx@toc@l(%r11)
3893 . mtctr %r12
721956f4
AM
3894 . bctr
3895
3896 ppc_stub_plt_call:
2c66dc6c
AM
3897 Used to call a function in a shared library. If it so happens that
3898 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3899 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3900 . std %r2,40(%r1)
71a39c98
AM
3901 . addis %r11,%r2,xxx@toc@ha
3902 . ld %r12,xxx+0@toc@l(%r11)
3903 . mtctr %r12
3904 . ld %r2,xxx+8@toc@l(%r11)
3905 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3906 . bctr
ad8e1ba5
AM
3907
3908 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3909 code to adjust the value and save r2 to support multiple toc sections.
3910 A ppc_stub_long_branch with an r2 offset looks like:
3911 . std %r2,40(%r1)
3912 . addis %r2,%r2,off@ha
3913 . addi %r2,%r2,off@l
3914 . b dest
3915
3916 A ppc_stub_plt_branch with an r2 offset looks like:
3917 . std %r2,40(%r1)
71a39c98
AM
3918 . addis %r11,%r2,xxx@toc@ha
3919 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3920 . addis %r2,%r2,off@ha
3921 . addi %r2,%r2,off@l
71a39c98 3922 . mtctr %r12
ad8e1ba5 3923 . bctr
ac2df442
AM
3924
3925 In cases where the "addis" instruction would add zero, the "addis" is
3926 omitted and following instructions modified slightly in some cases.
721956f4
AM
3927*/
3928
3929enum ppc_stub_type {
3930 ppc_stub_none,
3931 ppc_stub_long_branch,
ad8e1ba5 3932 ppc_stub_long_branch_r2off,
721956f4 3933 ppc_stub_plt_branch,
ad8e1ba5 3934 ppc_stub_plt_branch_r2off,
794e51c0 3935 ppc_stub_plt_call,
7341d5e2 3936 ppc_stub_plt_call_r2save,
a4b6fadd
AM
3937 ppc_stub_global_entry,
3938 ppc_stub_save_res
721956f4
AM
3939};
3940
6f20ed8a
AM
3941/* Information on stub grouping. */
3942struct map_stub
3943{
3944 /* The stub section. */
3945 asection *stub_sec;
3946 /* This is the section to which stubs in the group will be attached. */
3947 asection *link_sec;
a4b6fadd
AM
3948 /* Next group. */
3949 struct map_stub *next;
3950 /* Whether to emit a copy of register save/restore functions in this
3951 group. */
3952 int needs_save_res;
d4aaa2a0
AM
3953 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
3954 or -1u if no such stub with bctrl exists. */
3955 unsigned int tls_get_addr_opt_bctrl;
6f20ed8a
AM
3956};
3957
721956f4
AM
3958struct ppc_stub_hash_entry {
3959
3960 /* Base hash table entry structure. */
3961 struct bfd_hash_entry root;
3962
ad8e1ba5
AM
3963 enum ppc_stub_type stub_type;
3964
6f20ed8a
AM
3965 /* Group information. */
3966 struct map_stub *group;
721956f4
AM
3967
3968 /* Offset within stub_sec of the beginning of this stub. */
3969 bfd_vma stub_offset;
3970
3971 /* Given the symbol's value and its section we can determine its final
3972 value when building the stubs (so the stub knows where to jump. */
3973 bfd_vma target_value;
3974 asection *target_section;
3975
721956f4
AM
3976 /* The symbol table entry, if any, that this was derived from. */
3977 struct ppc_link_hash_entry *h;
e054468f 3978 struct plt_entry *plt_ent;
721956f4 3979
6911b7dc
AM
3980 /* Symbol st_other. */
3981 unsigned char other;
721956f4
AM
3982};
3983
3984struct ppc_branch_hash_entry {
3985
3986 /* Base hash table entry structure. */
3987 struct bfd_hash_entry root;
3988
c456f082 3989 /* Offset within branch lookup table. */
721956f4
AM
3990 unsigned int offset;
3991
3992 /* Generation marker. */
3993 unsigned int iter;
3994};
65f38f15 3995
19e08130
AM
3996/* Used to track dynamic relocations for local symbols. */
3997struct ppc_dyn_relocs
3998{
3999 struct ppc_dyn_relocs *next;
4000
4001 /* The input section of the reloc. */
4002 asection *sec;
4003
4004 /* Total number of relocs copied for the input section. */
4005 unsigned int count : 31;
4006
4007 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4008 unsigned int ifunc : 1;
4009};
4010
65f38f15
AM
4011struct ppc_link_hash_entry
4012{
4013 struct elf_link_hash_entry elf;
4014
b3fac117
AM
4015 union {
4016 /* A pointer to the most recently used stub hash entry against this
4017 symbol. */
4018 struct ppc_stub_hash_entry *stub_cache;
4019
4020 /* A pointer to the next symbol starting with a '.' */
4021 struct ppc_link_hash_entry *next_dot_sym;
4022 } u;
721956f4 4023
65f38f15 4024 /* Track dynamic relocs copied for this symbol. */
6061a67d 4025 struct elf_dyn_relocs *dyn_relocs;
e86ce104 4026
721956f4 4027 /* Link between function code and descriptor symbols. */
34814b9f 4028 struct ppc_link_hash_entry *oh;
721956f4 4029
e86ce104
AM
4030 /* Flag function code and descriptor symbols. */
4031 unsigned int is_func:1;
4032 unsigned int is_func_descriptor:1;
908b32fc 4033 unsigned int fake:1;
411e1bfb 4034
c5614fa4
AM
4035 /* Whether global opd/toc sym has been adjusted or not.
4036 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4037 should be set for all globals defined in any opd/toc section. */
754021d0
AM
4038 unsigned int adjust_done:1;
4039
a4b6fadd
AM
4040 /* Set if this is an out-of-line register save/restore function,
4041 with non-standard calling convention. */
4042 unsigned int save_res:1;
4043
8b5f1ed8
AM
4044 /* Set if a duplicate symbol with non-zero localentry is detected,
4045 even when the duplicate symbol does not provide a definition. */
4046 unsigned int non_zero_localentry:1;
4047
411e1bfb 4048 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
4049 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
4050 corresponding relocs are encountered during check_relocs.
4051 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
4052 indicate the corresponding GOT entry type is not needed.
4053 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
4054 a TPREL one. We use a separate flag rather than setting TPREL
4055 just for convenience in distinguishing the two cases. */
4056#define TLS_GD 1 /* GD reloc. */
4057#define TLS_LD 2 /* LD reloc. */
4058#define TLS_TPREL 4 /* TPREL reloc, => IE. */
4059#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4060#define TLS_TLS 16 /* Any TLS reloc. */
4061#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4062#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 4063#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 4064 unsigned char tls_mask;
65f38f15
AM
4065};
4066
4067/* ppc64 ELF linker hash table. */
4068
4069struct ppc_link_hash_table
4070{
4071 struct elf_link_hash_table elf;
4072
721956f4
AM
4073 /* The stub hash table. */
4074 struct bfd_hash_table stub_hash_table;
4075
4076 /* Another hash table for plt_branch stubs. */
4077 struct bfd_hash_table branch_hash_table;
4078
3b421ab3
AM
4079 /* Hash table for function prologue tocsave. */
4080 htab_t tocsave_htab;
4081
e7d1c40c
AM
4082 /* Various options and other info passed from the linker. */
4083 struct ppc64_elf_params *params;
721956f4 4084
6f20ed8a
AM
4085 /* The size of sec_info below. */
4086 unsigned int sec_info_arr_size;
4087
4088 /* Per-section array of extra section info. Done this way rather
4089 than as part of ppc64_elf_section_data so we have the info for
4090 non-ppc64 sections. */
4091 struct
4092 {
4093 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 4094 bfd_vma toc_off;
6f20ed8a
AM
4095
4096 union
4097 {
4098 /* The section group that this section belongs to. */
4099 struct map_stub *group;
4100 /* A temp section list pointer. */
4101 asection *list;
4102 } u;
4103 } *sec_info;
721956f4 4104
a4b6fadd
AM
4105 /* Linked list of groups. */
4106 struct map_stub *group;
4107
ad8e1ba5
AM
4108 /* Temp used when calculating TOC pointers. */
4109 bfd_vma toc_curr;
bf102f86
AM
4110 bfd *toc_bfd;
4111 asection *toc_first_sec;
ad8e1ba5 4112
b3fac117
AM
4113 /* Used when adding symbols. */
4114 struct ppc_link_hash_entry *dot_syms;
4115
33e44f2e 4116 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 4117 asection *glink;
9e390558 4118 asection *global_entry;
82bd7b59 4119 asection *sfpr;
4ce794b7
AM
4120 asection *brlt;
4121 asection *relbrlt;
58d180e8 4122 asection *glink_eh_frame;
ec338859 4123
8387904d
AM
4124 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4125 struct ppc_link_hash_entry *tls_get_addr;
4126 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4127
927be08e
AM
4128 /* The size of reliplt used by got entry relocs. */
4129 bfd_size_type got_reli_size;
4130
9b5ecbd0 4131 /* Statistics. */
7341d5e2 4132 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4133
ee75fd95
AM
4134 /* Number of stubs against global syms. */
4135 unsigned long stub_globals;
4136
ee67d69a
AM
4137 /* Set if we're linking code with function descriptors. */
4138 unsigned int opd_abi:1;
4139
4c52953f 4140 /* Support for multiple toc sections. */
33c0ec9d 4141 unsigned int do_multi_toc:1;
4c52953f 4142 unsigned int multi_toc_needed:1;
927be08e 4143 unsigned int second_toc_pass:1;
67f0cbdb 4144 unsigned int do_toc_opt:1;
4c52953f 4145
9a23f96e
AM
4146 /* Set if tls optimization is enabled. */
4147 unsigned int do_tls_opt:1;
4148
5d1634d7 4149 /* Set on error. */
99877b66 4150 unsigned int stub_error:1;
721956f4 4151
8c5b4e52
AM
4152 /* Whether func_desc_adjust needs to be run over symbols. */
4153 unsigned int need_func_desc_adj:1;
721956f4 4154
82e66161
AM
4155 /* Whether there exist local gnu indirect function resolvers,
4156 referenced by dynamic relocations. */
4157 unsigned int local_ifunc_resolver:1;
4158 unsigned int maybe_local_ifunc_resolver:1;
4159
f378ab09
AM
4160 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4161 unsigned int has_plt_localentry0:1;
4162
721956f4
AM
4163 /* Incremented every time we size stubs. */
4164 unsigned int stub_iteration;
5d1634d7 4165
87d72d41
AM
4166 /* Small local sym cache. */
4167 struct sym_cache sym_cache;
65f38f15
AM
4168};
4169
4c52953f
AM
4170/* Rename some of the generic section flags to better document how they
4171 are used here. */
b0dddeec
AM
4172
4173/* Nonzero if this section has TLS related relocations. */
4174#define has_tls_reloc sec_flg0
4175
4176/* Nonzero if this section has a call to __tls_get_addr. */
4177#define has_tls_get_addr_call sec_flg1
4178
4179/* Nonzero if this section has any toc or got relocs. */
4180#define has_toc_reloc sec_flg2
4181
4182/* Nonzero if this section has a call to another section that uses
4183 the toc or got. */
d77c8a4b 4184#define makes_toc_func_call sec_flg3
b0dddeec
AM
4185
4186/* Recursion protection when determining above flag. */
d77c8a4b 4187#define call_check_in_progress sec_flg4
70cc837d 4188#define call_check_done sec_flg5
4c52953f 4189
65f38f15
AM
4190/* Get the ppc64 ELF linker hash table from a link_info structure. */
4191
4192#define ppc_hash_table(p) \
4dfe6ac6
NC
4193 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4194 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4195
721956f4
AM
4196#define ppc_stub_hash_lookup(table, string, create, copy) \
4197 ((struct ppc_stub_hash_entry *) \
4198 bfd_hash_lookup ((table), (string), (create), (copy)))
4199
4200#define ppc_branch_hash_lookup(table, string, create, copy) \
4201 ((struct ppc_branch_hash_entry *) \
4202 bfd_hash_lookup ((table), (string), (create), (copy)))
4203
4204/* Create an entry in the stub hash table. */
4205
4206static struct bfd_hash_entry *
4ce794b7
AM
4207stub_hash_newfunc (struct bfd_hash_entry *entry,
4208 struct bfd_hash_table *table,
4209 const char *string)
721956f4
AM
4210{
4211 /* Allocate the structure if it has not already been allocated by a
4212 subclass. */
4213 if (entry == NULL)
4214 {
4215 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4216 if (entry == NULL)
4217 return entry;
4218 }
4219
4220 /* Call the allocation method of the superclass. */
4221 entry = bfd_hash_newfunc (entry, table, string);
4222 if (entry != NULL)
4223 {
4224 struct ppc_stub_hash_entry *eh;
4225
4226 /* Initialize the local fields. */
4227 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4228 eh->stub_type = ppc_stub_none;
6f20ed8a 4229 eh->group = NULL;
721956f4
AM
4230 eh->stub_offset = 0;
4231 eh->target_value = 0;
4232 eh->target_section = NULL;
721956f4 4233 eh->h = NULL;
6911b7dc 4234 eh->plt_ent = NULL;
6911b7dc 4235 eh->other = 0;
721956f4
AM
4236 }
4237
4238 return entry;
4239}
4240
4241/* Create an entry in the branch hash table. */
4242
4243static struct bfd_hash_entry *
4ce794b7
AM
4244branch_hash_newfunc (struct bfd_hash_entry *entry,
4245 struct bfd_hash_table *table,
4246 const char *string)
721956f4
AM
4247{
4248 /* Allocate the structure if it has not already been allocated by a
4249 subclass. */
4250 if (entry == NULL)
4251 {
4252 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4253 if (entry == NULL)
4254 return entry;
4255 }
4256
4257 /* Call the allocation method of the superclass. */
4258 entry = bfd_hash_newfunc (entry, table, string);
4259 if (entry != NULL)
4260 {
4261 struct ppc_branch_hash_entry *eh;
4262
4263 /* Initialize the local fields. */
4264 eh = (struct ppc_branch_hash_entry *) entry;
4265 eh->offset = 0;
4266 eh->iter = 0;
4267 }
4268
4269 return entry;
4270}
4271
65f38f15
AM
4272/* Create an entry in a ppc64 ELF linker hash table. */
4273
4274static struct bfd_hash_entry *
4ce794b7
AM
4275link_hash_newfunc (struct bfd_hash_entry *entry,
4276 struct bfd_hash_table *table,
4277 const char *string)
65f38f15
AM
4278{
4279 /* Allocate the structure if it has not already been allocated by a
4280 subclass. */
4281 if (entry == NULL)
4282 {
4283 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4284 if (entry == NULL)
4285 return entry;
4286 }
4287
4288 /* Call the allocation method of the superclass. */
4289 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4290 if (entry != NULL)
4291 {
4292 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4293
b3fac117 4294 memset (&eh->u.stub_cache, 0,
908b32fc 4295 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4296 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4297
4298 /* When making function calls, old ABI code references function entry
4299 points (dot symbols), while new ABI code references the function
4300 descriptor symbol. We need to make any combination of reference and
4301 definition work together, without breaking archive linking.
4302
4303 For a defined function "foo" and an undefined call to "bar":
4304 An old object defines "foo" and ".foo", references ".bar" (possibly
4305 "bar" too).
4306 A new object defines "foo" and references "bar".
4307
4308 A new object thus has no problem with its undefined symbols being
4309 satisfied by definitions in an old object. On the other hand, the
4310 old object won't have ".bar" satisfied by a new object.
4311
4312 Keep a list of newly added dot-symbols. */
4313
4314 if (string[0] == '.')
4315 {
4316 struct ppc_link_hash_table *htab;
4317
4318 htab = (struct ppc_link_hash_table *) table;
4319 eh->u.next_dot_sym = htab->dot_syms;
4320 htab->dot_syms = eh;
4321 }
65f38f15
AM
4322 }
4323
4324 return entry;
4325}
4326
3b421ab3
AM
4327struct tocsave_entry {
4328 asection *sec;
4329 bfd_vma offset;
4330};
4331
4332static hashval_t
4333tocsave_htab_hash (const void *p)
4334{
4335 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 4336 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
4337}
4338
4339static int
4340tocsave_htab_eq (const void *p1, const void *p2)
4341{
4342 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4343 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4344 return e1->sec == e2->sec && e1->offset == e2->offset;
4345}
4346
68faa637
AM
4347/* Destroy a ppc64 ELF linker hash table. */
4348
4349static void
d495ab0d 4350ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4351{
d495ab0d 4352 struct ppc_link_hash_table *htab;
68faa637 4353
d495ab0d 4354 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4355 if (htab->tocsave_htab)
4356 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4357 bfd_hash_table_free (&htab->branch_hash_table);
4358 bfd_hash_table_free (&htab->stub_hash_table);
4359 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4360}
4361
65f38f15
AM
4362/* Create a ppc64 ELF linker hash table. */
4363
4364static struct bfd_link_hash_table *
4ce794b7 4365ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4366{
4367 struct ppc_link_hash_table *htab;
4368 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4369
4ce794b7 4370 htab = bfd_zmalloc (amt);
65f38f15
AM
4371 if (htab == NULL)
4372 return NULL;
4373
66eb6687 4374 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4375 sizeof (struct ppc_link_hash_entry),
4376 PPC64_ELF_DATA))
65f38f15 4377 {
e2d34d7d 4378 free (htab);
65f38f15
AM
4379 return NULL;
4380 }
4381
721956f4 4382 /* Init the stub hash table too. */
66eb6687
AM
4383 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4384 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4385 {
d495ab0d 4386 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4387 return NULL;
4388 }
721956f4
AM
4389
4390 /* And the branch hash table. */
66eb6687
AM
4391 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4392 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4393 {
4394 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4395 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4396 return NULL;
4397 }
721956f4 4398
3b421ab3
AM
4399 htab->tocsave_htab = htab_try_create (1024,
4400 tocsave_htab_hash,
4401 tocsave_htab_eq,
4402 NULL);
4403 if (htab->tocsave_htab == NULL)
2915c55b 4404 {
d495ab0d 4405 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4406 return NULL;
4407 }
d495ab0d 4408 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4409
3254fd24
AM
4410 /* Initializing two fields of the union is just cosmetic. We really
4411 only care about glist, but when compiled on a 32-bit host the
4412 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4413 debugger inspection of these fields look nicer. */
a6aa5195
AM
4414 htab->elf.init_got_refcount.refcount = 0;
4415 htab->elf.init_got_refcount.glist = NULL;
4416 htab->elf.init_plt_refcount.refcount = 0;
4417 htab->elf.init_plt_refcount.glist = NULL;
4418 htab->elf.init_got_offset.offset = 0;
4419 htab->elf.init_got_offset.glist = NULL;
4420 htab->elf.init_plt_offset.offset = 0;
4421 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4422
65f38f15
AM
4423 return &htab->elf.root;
4424}
4425
bfeb4a28
AM
4426/* Create sections for linker generated code. */
4427
4428static bfd_boolean
4429create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4430{
4431 struct ppc_link_hash_table *htab;
4432 flagword flags;
4433
4434 htab = ppc_hash_table (info);
4435
bfeb4a28
AM
4436 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4437 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4438 if (htab->params->save_restore_funcs)
4439 {
4440 /* Create .sfpr for code to save and restore fp regs. */
4441 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4442 flags);
4443 if (htab->sfpr == NULL
4444 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4445 return FALSE;
4446 }
4447
4448 if (bfd_link_relocatable (info))
4449 return TRUE;
bfeb4a28
AM
4450
4451 /* Create .glink for lazy dynamic linking support. */
4452 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4453 flags);
4454 if (htab->glink == NULL
4455 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4456 return FALSE;
4457
9e390558
AM
4458 /* The part of .glink used by global entry stubs, separate so that
4459 it can be aligned appropriately without affecting htab->glink. */
4460 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4461 flags);
4462 if (htab->global_entry == NULL
4463 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4464 return FALSE;
4465
bfeb4a28
AM
4466 if (!info->no_ld_generated_unwind_info)
4467 {
4468 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4469 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4470 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4471 ".eh_frame",
4472 flags);
4473 if (htab->glink_eh_frame == NULL
4474 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4475 return FALSE;
4476 }
4477
4478 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4479 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4480 if (htab->elf.iplt == NULL
4481 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4482 return FALSE;
4483
4484 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4485 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4486 htab->elf.irelplt
4487 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4488 if (htab->elf.irelplt == NULL
4489 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4490 return FALSE;
4491
4492 /* Create branch lookup table for plt_branch stubs. */
4493 flags = (SEC_ALLOC | SEC_LOAD
4494 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4495 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4496 flags);
4497 if (htab->brlt == NULL
4498 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4499 return FALSE;
4500
0e1862bb 4501 if (!bfd_link_pic (info))
bfeb4a28
AM
4502 return TRUE;
4503
4504 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4505 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4506 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4507 ".rela.branch_lt",
4508 flags);
4509 if (htab->relbrlt == NULL
4510 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4511 return FALSE;
4512
4513 return TRUE;
4514}
4515
e717da7e
AM
4516/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4517
bfeb4a28 4518bfd_boolean
e7d1c40c
AM
4519ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4520 struct ppc64_elf_params *params)
e717da7e
AM
4521{
4522 struct ppc_link_hash_table *htab;
4523
e7d1c40c 4524 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4525
4526/* Always hook our dynamic sections into the first bfd, which is the
4527 linker created stub bfd. This ensures that the GOT header is at
4528 the start of the output TOC section. */
4529 htab = ppc_hash_table (info);
e7d1c40c
AM
4530 htab->elf.dynobj = params->stub_bfd;
4531 htab->params = params;
bfeb4a28 4532
bfeb4a28 4533 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4534}
4535
721956f4
AM
4536/* Build a name for an entry in the stub hash table. */
4537
4538static char *
4ce794b7
AM
4539ppc_stub_name (const asection *input_section,
4540 const asection *sym_sec,
4541 const struct ppc_link_hash_entry *h,
4542 const Elf_Internal_Rela *rel)
721956f4
AM
4543{
4544 char *stub_name;
bcaa2f82 4545 ssize_t len;
721956f4
AM
4546
4547 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4548 offsets from a sym as a branch target? In fact, we could
4549 probably assume the addend is always zero. */
4550 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4551
4552 if (h)
4553 {
4554 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4555 stub_name = bfd_malloc (len);
46de2a7c
AM
4556 if (stub_name == NULL)
4557 return stub_name;
4558
bcaa2f82
AM
4559 len = sprintf (stub_name, "%08x.%s+%x",
4560 input_section->id & 0xffffffff,
4561 h->elf.root.root.string,
4562 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4563 }
4564 else
4565 {
ad8e1ba5 4566 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4567 stub_name = bfd_malloc (len);
46de2a7c
AM
4568 if (stub_name == NULL)
4569 return stub_name;
4570
bcaa2f82
AM
4571 len = sprintf (stub_name, "%08x.%x:%x+%x",
4572 input_section->id & 0xffffffff,
4573 sym_sec->id & 0xffffffff,
4574 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4575 (int) rel->r_addend & 0xffffffff);
721956f4 4576 }
bcaa2f82 4577 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4578 stub_name[len - 2] = 0;
721956f4
AM
4579 return stub_name;
4580}
4581
4582/* Look up an entry in the stub hash. Stub entries are cached because
4583 creating the stub name takes a bit of time. */
4584
4585static struct ppc_stub_hash_entry *
4ce794b7
AM
4586ppc_get_stub_entry (const asection *input_section,
4587 const asection *sym_sec,
039b3fef 4588 struct ppc_link_hash_entry *h,
4ce794b7
AM
4589 const Elf_Internal_Rela *rel,
4590 struct ppc_link_hash_table *htab)
721956f4
AM
4591{
4592 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4593 struct map_stub *group;
721956f4
AM
4594
4595 /* If this input section is part of a group of sections sharing one
4596 stub section, then use the id of the first section in the group.
4597 Stub names need to include a section id, as there may well be
4598 more than one stub used to reach say, printf, and we need to
4599 distinguish between them. */
6f20ed8a 4600 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4601 if (group == NULL)
4602 return NULL;
721956f4 4603
b3fac117
AM
4604 if (h != NULL && h->u.stub_cache != NULL
4605 && h->u.stub_cache->h == h
6f20ed8a 4606 && h->u.stub_cache->group == group)
721956f4 4607 {
b3fac117 4608 stub_entry = h->u.stub_cache;
721956f4
AM
4609 }
4610 else
4611 {
4612 char *stub_name;
4613
6f20ed8a 4614 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4615 if (stub_name == NULL)
4616 return NULL;
4617
4618 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4619 stub_name, FALSE, FALSE);
721956f4 4620 if (h != NULL)
b3fac117 4621 h->u.stub_cache = stub_entry;
721956f4
AM
4622
4623 free (stub_name);
4624 }
4625
4626 return stub_entry;
4627}
4628
4629/* Add a new stub entry to the stub hash. Not all fields of the new
4630 stub entry are initialised. */
4631
4632static struct ppc_stub_hash_entry *
4ce794b7
AM
4633ppc_add_stub (const char *stub_name,
4634 asection *section,
25f53a85 4635 struct bfd_link_info *info)
721956f4 4636{
25f53a85 4637 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4638 struct map_stub *group;
721956f4
AM
4639 asection *link_sec;
4640 asection *stub_sec;
4641 struct ppc_stub_hash_entry *stub_entry;
4642
6f20ed8a
AM
4643 group = htab->sec_info[section->id].u.group;
4644 link_sec = group->link_sec;
4645 stub_sec = group->stub_sec;
721956f4
AM
4646 if (stub_sec == NULL)
4647 {
6f20ed8a
AM
4648 size_t namelen;
4649 bfd_size_type len;
4650 char *s_name;
721956f4 4651
6f20ed8a
AM
4652 namelen = strlen (link_sec->name);
4653 len = namelen + sizeof (STUB_SUFFIX);
4654 s_name = bfd_alloc (htab->params->stub_bfd, len);
4655 if (s_name == NULL)
4656 return NULL;
721956f4 4657
6f20ed8a
AM
4658 memcpy (s_name, link_sec->name, namelen);
4659 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4660 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4661 if (stub_sec == NULL)
4662 return NULL;
4663 group->stub_sec = stub_sec;
721956f4
AM
4664 }
4665
4666 /* Enter this entry into the linker stub hash table. */
4667 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4668 TRUE, FALSE);
721956f4
AM
4669 if (stub_entry == NULL)
4670 {
695344c0 4671 /* xgettext:c-format */
cf97bcb0
AM
4672 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4673 section->owner, stub_name);
721956f4
AM
4674 return NULL;
4675 }
4676
6f20ed8a 4677 stub_entry->group = group;
721956f4 4678 stub_entry->stub_offset = 0;
721956f4
AM
4679 return stub_entry;
4680}
4681
e717da7e
AM
4682/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4683 not already done. */
65f38f15 4684
b34976b6 4685static bfd_boolean
e717da7e 4686create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4687{
e717da7e
AM
4688 asection *got, *relgot;
4689 flagword flags;
4690 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4691
0c8d6e5c 4692 if (!is_ppc64_elf (abfd))
0ffa91dd 4693 return FALSE;
4dfe6ac6
NC
4694 if (htab == NULL)
4695 return FALSE;
0ffa91dd 4696
33e44f2e
AM
4697 if (!htab->elf.sgot
4698 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4699 return FALSE;
e717da7e
AM
4700
4701 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4702 | SEC_LINKER_CREATED);
4703
c456f082 4704 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4705 if (!got
e717da7e
AM
4706 || !bfd_set_section_alignment (abfd, got, 3))
4707 return FALSE;
65f38f15 4708
c456f082
AM
4709 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4710 flags | SEC_READONLY);
e717da7e 4711 if (!relgot
e717da7e 4712 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4713 return FALSE;
e717da7e
AM
4714
4715 ppc64_elf_tdata (abfd)->got = got;
4716 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4717 return TRUE;
65f38f15 4718}
5bd4f169 4719
b31867b6
AM
4720/* Follow indirect and warning symbol links. */
4721
4722static inline struct bfd_link_hash_entry *
4723follow_link (struct bfd_link_hash_entry *h)
4724{
4725 while (h->type == bfd_link_hash_indirect
4726 || h->type == bfd_link_hash_warning)
4727 h = h->u.i.link;
4728 return h;
4729}
4730
4731static inline struct elf_link_hash_entry *
4732elf_follow_link (struct elf_link_hash_entry *h)
4733{
4734 return (struct elf_link_hash_entry *) follow_link (&h->root);
4735}
4736
4737static inline struct ppc_link_hash_entry *
4738ppc_follow_link (struct ppc_link_hash_entry *h)
4739{
4740 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4741}
4742
40d16e0b
AM
4743/* Merge PLT info on FROM with that on TO. */
4744
4745static void
4746move_plt_plist (struct ppc_link_hash_entry *from,
4747 struct ppc_link_hash_entry *to)
4748{
4749 if (from->elf.plt.plist != NULL)
4750 {
4751 if (to->elf.plt.plist != NULL)
4752 {
4753 struct plt_entry **entp;
4754 struct plt_entry *ent;
4755
4756 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4757 {
4758 struct plt_entry *dent;
4759
4760 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4761 if (dent->addend == ent->addend)
4762 {
4763 dent->plt.refcount += ent->plt.refcount;
4764 *entp = ent->next;
4765 break;
4766 }
4767 if (dent == NULL)
4768 entp = &ent->next;
4769 }
4770 *entp = to->elf.plt.plist;
4771 }
4772
4773 to->elf.plt.plist = from->elf.plt.plist;
4774 from->elf.plt.plist = NULL;
4775 }
4776}
4777
65f38f15
AM
4778/* Copy the extra info we tack onto an elf_link_hash_entry. */
4779
4780static void
fcfa13d2
AM
4781ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4782 struct elf_link_hash_entry *dir,
4783 struct elf_link_hash_entry *ind)
65f38f15
AM
4784{
4785 struct ppc_link_hash_entry *edir, *eind;
4786
4787 edir = (struct ppc_link_hash_entry *) dir;
4788 eind = (struct ppc_link_hash_entry *) ind;
4789
c79d6685
AM
4790 edir->is_func |= eind->is_func;
4791 edir->is_func_descriptor |= eind->is_func_descriptor;
4792 edir->tls_mask |= eind->tls_mask;
4793 if (eind->oh != NULL)
4794 edir->oh = ppc_follow_link (eind->oh);
4795
474436e6 4796 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4797 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4798 edir->elf.ref_regular |= eind->elf.ref_regular;
4799 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 4800 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
4801 edir->elf.needs_plt |= eind->elf.needs_plt;
4802 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4803
d311bc8b
AM
4804 /* If we were called to copy over info for a weak sym, don't copy
4805 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4806 in order to simplify readonly_dynrelocs and save a field in the
4807 symbol hash entry, but that means dyn_relocs can't be used in any
4808 tests about a specific symbol, or affect other symbol flags which
ab2477e1 4809 are then tested. */
d311bc8b 4810 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 4811 return;
d311bc8b 4812
411e1bfb 4813 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4814 if (eind->dyn_relocs != NULL)
65f38f15 4815 {
bbd7ec4a
AM
4816 if (edir->dyn_relocs != NULL)
4817 {
6061a67d
AM
4818 struct elf_dyn_relocs **pp;
4819 struct elf_dyn_relocs *p;
bbd7ec4a 4820
fcfa13d2 4821 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4822 list. Merge any entries against the same section. */
4823 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4824 {
6061a67d 4825 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4826
4827 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4828 if (q->sec == p->sec)
4829 {
4830 q->pc_count += p->pc_count;
4831 q->count += p->count;
4832 *pp = p->next;
4833 break;
4834 }
4835 if (q == NULL)
4836 pp = &p->next;
4837 }
4838 *pp = edir->dyn_relocs;
4839 }
4840
65f38f15
AM
4841 edir->dyn_relocs = eind->dyn_relocs;
4842 eind->dyn_relocs = NULL;
4843 }
65f38f15 4844
81848ca0
AM
4845 /* Copy over got entries that we may have already seen to the
4846 symbol which just became indirect. */
411e1bfb
AM
4847 if (eind->elf.got.glist != NULL)
4848 {
4849 if (edir->elf.got.glist != NULL)
4850 {
4851 struct got_entry **entp;
4852 struct got_entry *ent;
4853
4854 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4855 {
4856 struct got_entry *dent;
4857
4858 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4859 if (dent->addend == ent->addend
e717da7e 4860 && dent->owner == ent->owner
411e1bfb
AM
4861 && dent->tls_type == ent->tls_type)
4862 {
4863 dent->got.refcount += ent->got.refcount;
4864 *entp = ent->next;
4865 break;
4866 }
4867 if (dent == NULL)
4868 entp = &ent->next;
4869 }
4870 *entp = edir->elf.got.glist;
4871 }
4872
4873 edir->elf.got.glist = eind->elf.got.glist;
4874 eind->elf.got.glist = NULL;
4875 }
4876
4877 /* And plt entries. */
40d16e0b 4878 move_plt_plist (eind, edir);
411e1bfb 4879
fcfa13d2 4880 if (eind->elf.dynindx != -1)
411e1bfb 4881 {
fcfa13d2
AM
4882 if (edir->elf.dynindx != -1)
4883 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4884 edir->elf.dynstr_index);
411e1bfb
AM
4885 edir->elf.dynindx = eind->elf.dynindx;
4886 edir->elf.dynstr_index = eind->elf.dynstr_index;
4887 eind->elf.dynindx = -1;
4888 eind->elf.dynstr_index = 0;
4889 }
411e1bfb
AM
4890}
4891
8387904d
AM
4892/* Find the function descriptor hash entry from the given function code
4893 hash entry FH. Link the entries via their OH fields. */
4894
4895static struct ppc_link_hash_entry *
b31867b6 4896lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4897{
4898 struct ppc_link_hash_entry *fdh = fh->oh;
4899
4900 if (fdh == NULL)
4901 {
4902 const char *fd_name = fh->elf.root.root.string + 1;
4903
4904 fdh = (struct ppc_link_hash_entry *)
4905 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4906 if (fdh == NULL)
4907 return fdh;
4908
4909 fdh->is_func_descriptor = 1;
4910 fdh->oh = fh;
4911 fh->is_func = 1;
4912 fh->oh = fdh;
8387904d
AM
4913 }
4914
8c5b4e52
AM
4915 fdh = ppc_follow_link (fdh);
4916 fdh->is_func_descriptor = 1;
4917 fdh->oh = fh;
4918 return fdh;
8387904d
AM
4919}
4920
8c5b4e52 4921/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4922
4923static struct ppc_link_hash_entry *
4924make_fdh (struct bfd_link_info *info,
908b32fc 4925 struct ppc_link_hash_entry *fh)
bb700d78 4926{
8c5b4e52
AM
4927 bfd *abfd = fh->elf.root.u.undef.abfd;
4928 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4929 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4930 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4931 ? BSF_WEAK
4932 : BSF_GLOBAL);
4933
4934 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4935 fh->elf.root.root.string + 1,
4936 flags, bfd_und_section_ptr, 0,
4937 NULL, FALSE, FALSE, &bh))
bb700d78
AM
4938 return NULL;
4939
4940 fdh = (struct ppc_link_hash_entry *) bh;
4941 fdh->elf.non_elf = 0;
908b32fc
AM
4942 fdh->fake = 1;
4943 fdh->is_func_descriptor = 1;
4944 fdh->oh = fh;
4945 fh->is_func = 1;
4946 fh->oh = fdh;
bb700d78
AM
4947 return fdh;
4948}
4949
8387904d
AM
4950/* Fix function descriptor symbols defined in .opd sections to be
4951 function type. */
555cd476
AM
4952
4953static bfd_boolean
c16153ae 4954ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4955 struct bfd_link_info *info,
555cd476 4956 Elf_Internal_Sym *isym,
6911b7dc 4957 const char **name,
555cd476
AM
4958 flagword *flags ATTRIBUTE_UNUSED,
4959 asection **sec,
b53dfeb2 4960 bfd_vma *value)
555cd476 4961{
a43942db 4962 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
f1885d1e
AM
4963 && (ibfd->flags & DYNAMIC) == 0
4964 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
a43942db 4965 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
f64b2e8d 4966
b53dfeb2 4967 if (*sec != NULL
f1885d1e 4968 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4969 {
4970 asection *code_sec;
4971
4972 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4973 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4974 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4975
4976 /* If the symbol is a function defined in .opd, and the function
4977 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4978 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4979 && (*sec)->reloc_count != 0
4980 && opd_entry_value (*sec, *value, &code_sec, NULL,
4981 FALSE) != (bfd_vma) -1
4982 && discarded_section (code_sec))
4983 {
4984 *sec = bfd_und_section_ptr;
4985 isym->st_shndx = SHN_UNDEF;
4986 }
4987 }
dbd1e97e
AM
4988 else if (*sec != NULL
4989 && strcmp ((*sec)->name, ".toc") == 0
4990 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4991 {
4992 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4993 if (htab != NULL)
4994 htab->params->object_in_toc = 1;
4995 }
433817dd 4996
6911b7dc
AM
4997 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4998 {
4999 if (abiversion (ibfd) == 0)
5000 set_abiversion (ibfd, 2);
5001 else if (abiversion (ibfd) == 1)
5002 {
cf97bcb0
AM
5003 _bfd_error_handler (_("symbol '%s' has invalid st_other"
5004 " for ABI version 1"), *name);
6911b7dc
AM
5005 bfd_set_error (bfd_error_bad_value);
5006 return FALSE;
5007 }
5008 }
5009
555cd476
AM
5010 return TRUE;
5011}
5012
6911b7dc
AM
5013/* Merge non-visibility st_other attributes: local entry point. */
5014
5015static void
5016ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5017 const Elf_Internal_Sym *isym,
5018 bfd_boolean definition,
5019 bfd_boolean dynamic)
5020{
f378ab09 5021 if (definition && (!dynamic || !h->def_regular))
6911b7dc
AM
5022 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5023 | ELF_ST_VISIBILITY (h->other));
5024}
5025
8c5b4e52
AM
5026/* Hook called on merging a symbol. We use this to clear "fake" since
5027 we now have a real symbol. */
5028
5029static bfd_boolean
5030ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 5031 const Elf_Internal_Sym *isym,
8c5b4e52
AM
5032 asection **psec ATTRIBUTE_UNUSED,
5033 bfd_boolean newdef ATTRIBUTE_UNUSED,
5034 bfd_boolean olddef ATTRIBUTE_UNUSED,
5035 bfd *oldbfd ATTRIBUTE_UNUSED,
5036 const asection *oldsec ATTRIBUTE_UNUSED)
5037{
5038 ((struct ppc_link_hash_entry *) h)->fake = 0;
8b5f1ed8
AM
5039 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5040 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
8c5b4e52
AM
5041 return TRUE;
5042}
5043
8387904d 5044/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5045 inclusion of a new ABI object archive that defines "bar".
5046 NAME is a symbol defined in an archive. Return a symbol in the hash
5047 table that might be satisfied by the archive symbols. */
8387904d
AM
5048
5049static struct elf_link_hash_entry *
5050ppc64_elf_archive_symbol_lookup (bfd *abfd,
5051 struct bfd_link_info *info,
5052 const char *name)
5053{
5054 struct elf_link_hash_entry *h;
5055 char *dot_name;
5056 size_t len;
5057
5058 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5059 if (h != NULL
5060 /* Don't return this sym if it is a fake function descriptor
5061 created by add_symbol_adjust. */
8c5b4e52 5062 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
5063 return h;
5064
5065 if (name[0] == '.')
5066 return h;
5067
5068 len = strlen (name);
5069 dot_name = bfd_alloc (abfd, len + 2);
5070 if (dot_name == NULL)
e99955cd 5071 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5072 dot_name[0] = '.';
5073 memcpy (dot_name + 1, name, len + 1);
5074 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5075 bfd_release (abfd, dot_name);
5076 return h;
5077}
5078
5079/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5080 new ABI object defines "bar". Well, at least, undefined dot symbols
5081 are made weak. This stops later archive searches from including an
5082 object if we already have a function descriptor definition. It also
35b0ce59
AM
5083 prevents the linker complaining about undefined symbols.
5084 We also check and correct mismatched symbol visibility here. The
5085 most restrictive visibility of the function descriptor and the
5086 function entry symbol is used. */
8387904d
AM
5087
5088static bfd_boolean
b3fac117 5089add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5090{
8387904d
AM
5091 struct ppc_link_hash_table *htab;
5092 struct ppc_link_hash_entry *fdh;
5093
b3fac117
AM
5094 if (eh->elf.root.type == bfd_link_hash_warning)
5095 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5096
8400d40d
AM
5097 if (eh->elf.root.type == bfd_link_hash_indirect)
5098 return TRUE;
5099
b3fac117
AM
5100 if (eh->elf.root.root.string[0] != '.')
5101 abort ();
8387904d 5102
b3fac117 5103 htab = ppc_hash_table (info);
4dfe6ac6
NC
5104 if (htab == NULL)
5105 return FALSE;
5106
b31867b6 5107 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
5108 if (fdh == NULL
5109 && !bfd_link_relocatable (info)
5110 && (eh->elf.root.type == bfd_link_hash_undefined
5111 || eh->elf.root.type == bfd_link_hash_undefweak)
5112 && eh->elf.ref_regular)
5113 {
5114 /* Make an undefined function descriptor sym, in order to
5115 pull in an --as-needed shared lib. Archives are handled
5116 elsewhere. */
5117 fdh = make_fdh (info, eh);
5118 if (fdh == NULL)
5119 return FALSE;
bb700d78 5120 }
8c5b4e52
AM
5121
5122 if (fdh != NULL)
8387904d 5123 {
35b0ce59
AM
5124 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5125 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
5126
5127 /* Make both descriptor and entry symbol have the most
5128 constraining visibility of either symbol. */
35b0ce59
AM
5129 if (entry_vis < descr_vis)
5130 fdh->elf.other += entry_vis - descr_vis;
5131 else if (entry_vis > descr_vis)
5132 eh->elf.other += descr_vis - entry_vis;
5133
8c5b4e52
AM
5134 /* Propagate reference flags from entry symbol to function
5135 descriptor symbol. */
bc4e12de 5136 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 5137 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
5138 fdh->elf.ref_regular |= eh->elf.ref_regular;
5139 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5140
5141 if (!fdh->elf.forced_local
5142 && fdh->elf.dynindx == -1
5143 && fdh->elf.versioned != versioned_hidden
5144 && (bfd_link_dll (info)
5145 || fdh->elf.def_dynamic
5146 || fdh->elf.ref_dynamic)
5147 && (eh->elf.ref_regular
5148 || eh->elf.def_regular))
5149 {
5150 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5151 return FALSE;
35b0ce59 5152 }
8387904d 5153 }
99877b66 5154
8387904d
AM
5155 return TRUE;
5156}
5157
f6c7c3e8
AM
5158/* Set up opd section info and abiversion for IBFD, and process list
5159 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5160
8387904d 5161static bfd_boolean
f6c7c3e8 5162ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5163{
99877b66 5164 struct ppc_link_hash_table *htab;
b3fac117 5165 struct ppc_link_hash_entry **p, *eh;
459609d6 5166 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5167
459609d6 5168 if (opd != NULL && opd->size != 0)
b3fac117 5169 {
b9399fcf
AM
5170 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5171 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5172
459609d6
AM
5173 if (abiversion (ibfd) == 0)
5174 set_abiversion (ibfd, 1);
8a2058b5 5175 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5176 {
695344c0 5177 /* xgettext:c-format */
cf97bcb0
AM
5178 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5179 ibfd, abiversion (ibfd));
459609d6
AM
5180 bfd_set_error (bfd_error_bad_value);
5181 return FALSE;
f6c7c3e8 5182 }
b9399fcf 5183 }
f6c7c3e8 5184
b9399fcf
AM
5185 if (is_ppc64_elf (info->output_bfd))
5186 {
5187 /* For input files without an explicit abiversion in e_flags
5188 we should have flagged any with symbol st_other bits set
5189 as ELFv1 and above flagged those with .opd as ELFv2.
5190 Set the output abiversion if not yet set, and for any input
5191 still ambiguous, take its abiversion from the output.
5192 Differences in ABI are reported later. */
5193 if (abiversion (info->output_bfd) == 0)
5194 set_abiversion (info->output_bfd, abiversion (ibfd));
5195 else if (abiversion (ibfd) == 0)
5196 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
5197 }
5198
459609d6
AM
5199 htab = ppc_hash_table (info);
5200 if (htab == NULL)
b9399fcf 5201 return TRUE;
459609d6 5202
b9399fcf
AM
5203 if (opd != NULL && opd->size != 0
5204 && (ibfd->flags & DYNAMIC) == 0
5205 && (opd->flags & SEC_RELOC) != 0
5206 && opd->reloc_count != 0
5207 && !bfd_is_abs_section (opd->output_section)
5208 && info->gc_sections)
5209 {
5210 /* Garbage collection needs some extra help with .opd sections.
5211 We don't want to necessarily keep everything referenced by
5212 relocs in .opd, as that would keep all functions. Instead,
5213 if we reference an .opd symbol (a function descriptor), we
5214 want to keep the function code symbol's section. This is
5215 easy for global symbols, but for local syms we need to keep
5216 information about the associated function section. */
5217 bfd_size_type amt;
5218 asection **opd_sym_map;
5219 Elf_Internal_Shdr *symtab_hdr;
5220 Elf_Internal_Rela *relocs, *rel_end, *rel;
5221
5222 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5223 opd_sym_map = bfd_zalloc (ibfd, amt);
5224 if (opd_sym_map == NULL)
5225 return FALSE;
5226 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5227 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5228 info->keep_memory);
5229 if (relocs == NULL)
5230 return FALSE;
5231 symtab_hdr = &elf_symtab_hdr (ibfd);
5232 rel_end = relocs + opd->reloc_count - 1;
5233 for (rel = relocs; rel < rel_end; rel++)
5234 {
5235 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5236 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5237
5238 if (r_type == R_PPC64_ADDR64
5239 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5240 && r_symndx < symtab_hdr->sh_info)
5241 {
5242 Elf_Internal_Sym *isym;
5243 asection *s;
5244
5245 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5246 if (isym == NULL)
5247 {
5248 if (elf_section_data (opd)->relocs != relocs)
5249 free (relocs);
5250 return FALSE;
5251 }
5252
5253 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5254 if (s != NULL && s != opd)
5255 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5256 }
5257 }
5258 if (elf_section_data (opd)->relocs != relocs)
5259 free (relocs);
5260 }
459609d6
AM
5261
5262 p = &htab->dot_syms;
5263 while ((eh = *p) != NULL)
5264 {
5265 *p = NULL;
5266 if (&eh->elf == htab->elf.hgot)
5267 ;
5268 else if (htab->elf.hgot == NULL
5269 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5270 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
5271 else if (abiversion (ibfd) <= 1)
5272 {
5273 htab->need_func_desc_adj = 1;
5274 if (!add_symbol_adjust (eh, info))
5275 return FALSE;
5276 }
459609d6
AM
5277 p = &eh->u.next_dot_sym;
5278 }
b3fac117 5279 return TRUE;
8387904d
AM
5280}
5281
97fed1c9
JJ
5282/* Undo hash table changes when an --as-needed input file is determined
5283 not to be needed. */
5284
5285static bfd_boolean
e5034e59
AM
5286ppc64_elf_notice_as_needed (bfd *ibfd,
5287 struct bfd_link_info *info,
5288 enum notice_asneeded_action act)
97fed1c9 5289{
e5034e59
AM
5290 if (act == notice_not_needed)
5291 {
5292 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5293
e5034e59
AM
5294 if (htab == NULL)
5295 return FALSE;
4dfe6ac6 5296
e5034e59
AM
5297 htab->dot_syms = NULL;
5298 }
5299 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5300}
5301
aa374f67
AM
5302/* If --just-symbols against a final linked binary, then assume we need
5303 toc adjusting stubs when calling functions defined there. */
5304
5305static void
5306ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5307{
5308 if ((sec->flags & SEC_CODE) != 0
5309 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5310 && is_ppc64_elf (sec->owner))
5311 {
2c3f079f
AM
5312 if (abiversion (sec->owner) >= 2
5313 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5314 sec->has_toc_reloc = 1;
5315 }
5316 _bfd_elf_link_just_syms (sec, info);
5317}
5318
e054468f 5319static struct plt_entry **
4ce794b7
AM
5320update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5321 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5322{
5323 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5324 struct plt_entry **local_plt;
f961d9dd 5325 unsigned char *local_got_tls_masks;
411e1bfb
AM
5326
5327 if (local_got_ents == NULL)
5328 {
5329 bfd_size_type size = symtab_hdr->sh_info;
5330
e054468f
AM
5331 size *= (sizeof (*local_got_ents)
5332 + sizeof (*local_plt)
5333 + sizeof (*local_got_tls_masks));
4ce794b7 5334 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5335 if (local_got_ents == NULL)
e054468f 5336 return NULL;
411e1bfb
AM
5337 elf_local_got_ents (abfd) = local_got_ents;
5338 }
5339
e054468f 5340 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5341 {
5342 struct got_entry *ent;
5343
5344 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5345 if (ent->addend == r_addend
5346 && ent->owner == abfd
5347 && ent->tls_type == tls_type)
411e1bfb
AM
5348 break;
5349 if (ent == NULL)
5350 {
5351 bfd_size_type amt = sizeof (*ent);
4ce794b7 5352 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5353 if (ent == NULL)
5354 return FALSE;
5355 ent->next = local_got_ents[r_symndx];
5356 ent->addend = r_addend;
e717da7e 5357 ent->owner = abfd;
411e1bfb 5358 ent->tls_type = tls_type;
927be08e 5359 ent->is_indirect = FALSE;
411e1bfb
AM
5360 ent->got.refcount = 0;
5361 local_got_ents[r_symndx] = ent;
5362 }
5363 ent->got.refcount += 1;
5364 }
5365
e054468f 5366 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5367 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5368 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5369
5370 return local_plt + r_symndx;
65f38f15
AM
5371}
5372
411e1bfb 5373static bfd_boolean
e054468f 5374update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5375{
411e1bfb 5376 struct plt_entry *ent;
1e2f5b6e 5377
e054468f 5378 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5379 if (ent->addend == addend)
5380 break;
5381 if (ent == NULL)
1e2f5b6e 5382 {
411e1bfb 5383 bfd_size_type amt = sizeof (*ent);
4ce794b7 5384 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5385 if (ent == NULL)
5386 return FALSE;
e054468f 5387 ent->next = *plist;
411e1bfb
AM
5388 ent->addend = addend;
5389 ent->plt.refcount = 0;
e054468f 5390 *plist = ent;
1e2f5b6e 5391 }
411e1bfb 5392 ent->plt.refcount += 1;
b34976b6 5393 return TRUE;
1e2f5b6e
AM
5394}
5395
e054468f
AM
5396static bfd_boolean
5397is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5398{
5399 return (r_type == R_PPC64_REL24
5400 || r_type == R_PPC64_REL14
5401 || r_type == R_PPC64_REL14_BRTAKEN
5402 || r_type == R_PPC64_REL14_BRNTAKEN
5403 || r_type == R_PPC64_ADDR24
5404 || r_type == R_PPC64_ADDR14
5405 || r_type == R_PPC64_ADDR14_BRTAKEN
5406 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5407}
5408
5bd4f169 5409/* Look through the relocs for a section during the first phase, and
65f38f15 5410 calculate needed space in the global offset table, procedure
5d1634d7 5411 linkage table, and dynamic reloc sections. */
5bd4f169 5412
b34976b6 5413static bfd_boolean
4ce794b7
AM
5414ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5415 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5416{
65f38f15 5417 struct ppc_link_hash_table *htab;
5bd4f169 5418 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5419 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5420 const Elf_Internal_Rela *rel;
5421 const Elf_Internal_Rela *rel_end;
5bd4f169 5422 asection *sreloc;
3a71aa26 5423 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 5424 bfd_boolean is_opd;
5bd4f169 5425
0e1862bb 5426 if (bfd_link_relocatable (info))
b34976b6 5427 return TRUE;
5bd4f169 5428
680a3378
AM
5429 /* Don't do anything special with non-loaded, non-alloced sections.
5430 In particular, any relocs in such sections should not affect GOT
5431 and PLT reference counting (ie. we don't allow them to create GOT
5432 or PLT entries), there's no possibility or desire to optimize TLS
5433 relocs, and there's not much point in propagating relocs to shared
5434 libs that the dynamic linker won't relocate. */
5435 if ((sec->flags & SEC_ALLOC) == 0)
5436 return TRUE;
5437
0c8d6e5c 5438 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5439
65f38f15 5440 htab = ppc_hash_table (info);
4dfe6ac6
NC
5441 if (htab == NULL)
5442 return FALSE;
5443
3a71aa26
AM
5444 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5445 FALSE, FALSE, TRUE);
5446 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5447 FALSE, FALSE, TRUE);
0ffa91dd 5448 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5449 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5450 sreloc = NULL;
b9399fcf 5451 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
5452 rel_end = relocs + sec->reloc_count;
5453 for (rel = relocs; rel < rel_end; rel++)
5454 {
5455 unsigned long r_symndx;
5456 struct elf_link_hash_entry *h;
04c9666a 5457 enum elf_ppc64_reloc_type r_type;
727fc41e 5458 int tls_type;
7c8fe5c4 5459 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5460 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5461
5462 r_symndx = ELF64_R_SYM (rel->r_info);
5463 if (r_symndx < symtab_hdr->sh_info)
5464 h = NULL;
5465 else
973a3492
L
5466 {
5467 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5468 h = elf_follow_link (h);
1c865ab2
AM
5469
5470 if (h == htab->elf.hgot)
5471 sec->has_toc_reloc = 1;
973a3492 5472 }
5bd4f169 5473
727fc41e 5474 tls_type = 0;
e054468f 5475 ifunc = NULL;
25f23106
AM
5476 if (h != NULL)
5477 {
5478 if (h->type == STT_GNU_IFUNC)
5479 {
5480 h->needs_plt = 1;
5481 ifunc = &h->plt.plist;
5482 }
5483 }
5484 else
5485 {
5486 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5487 abfd, r_symndx);
5488 if (isym == NULL)
5489 return FALSE;
5490
5491 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5492 {
5493 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5494 rel->r_addend, PLT_IFUNC);
5495 if (ifunc == NULL)
5496 return FALSE;
5497 }
5498 }
727fc41e 5499
cbf95972 5500 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5501 switch (r_type)
5bd4f169 5502 {
727fc41e
AM
5503 case R_PPC64_TLSGD:
5504 case R_PPC64_TLSLD:
5505 /* These special tls relocs tie a call to __tls_get_addr with
5506 its parameter symbol. */
5507 break;
5508
411e1bfb
AM
5509 case R_PPC64_GOT_TLSLD16:
5510 case R_PPC64_GOT_TLSLD16_LO:
5511 case R_PPC64_GOT_TLSLD16_HI:
5512 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5513 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5514 goto dogottls;
5515
5516 case R_PPC64_GOT_TLSGD16:
5517 case R_PPC64_GOT_TLSGD16_LO:
5518 case R_PPC64_GOT_TLSGD16_HI:
5519 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5520 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5521 goto dogottls;
5522
5523 case R_PPC64_GOT_TPREL16_DS:
5524 case R_PPC64_GOT_TPREL16_LO_DS:
5525 case R_PPC64_GOT_TPREL16_HI:
5526 case R_PPC64_GOT_TPREL16_HA:
7c8bbca5 5527 if (bfd_link_dll (info))
411e1bfb
AM
5528 info->flags |= DF_STATIC_TLS;
5529 tls_type = TLS_TLS | TLS_TPREL;
5530 goto dogottls;
5531
5532 case R_PPC64_GOT_DTPREL16_DS:
5533 case R_PPC64_GOT_DTPREL16_LO_DS:
5534 case R_PPC64_GOT_DTPREL16_HI:
5535 case R_PPC64_GOT_DTPREL16_HA:
5536 tls_type = TLS_TLS | TLS_DTPREL;
5537 dogottls:
5538 sec->has_tls_reloc = 1;
1a0670f3 5539 /* Fall through */
411e1bfb 5540
5bd4f169 5541 case R_PPC64_GOT16:
5bd4f169 5542 case R_PPC64_GOT16_DS:
65f38f15
AM
5543 case R_PPC64_GOT16_HA:
5544 case R_PPC64_GOT16_HI:
5545 case R_PPC64_GOT16_LO:
5bd4f169 5546 case R_PPC64_GOT16_LO_DS:
65f38f15 5547 /* This symbol requires a global offset table entry. */
4c52953f 5548 sec->has_toc_reloc = 1;
33c0ec9d
AM
5549 if (r_type == R_PPC64_GOT_TLSLD16
5550 || r_type == R_PPC64_GOT_TLSGD16
5551 || r_type == R_PPC64_GOT_TPREL16_DS
5552 || r_type == R_PPC64_GOT_DTPREL16_DS
5553 || r_type == R_PPC64_GOT16
5554 || r_type == R_PPC64_GOT16_DS)
5555 {
5556 htab->do_multi_toc = 1;
d77c8a4b 5557 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5558 }
5559
e717da7e
AM
5560 if (ppc64_elf_tdata (abfd)->got == NULL
5561 && !create_got_section (abfd, info))
b34976b6 5562 return FALSE;
5bd4f169
AM
5563
5564 if (h != NULL)
5565 {
411e1bfb
AM
5566 struct ppc_link_hash_entry *eh;
5567 struct got_entry *ent;
65f38f15 5568
411e1bfb
AM
5569 eh = (struct ppc_link_hash_entry *) h;
5570 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5571 if (ent->addend == rel->r_addend
e717da7e 5572 && ent->owner == abfd
411e1bfb
AM
5573 && ent->tls_type == tls_type)
5574 break;
5575 if (ent == NULL)
5bd4f169 5576 {
411e1bfb 5577 bfd_size_type amt = sizeof (*ent);
4ce794b7 5578 ent = bfd_alloc (abfd, amt);
411e1bfb 5579 if (ent == NULL)
b34976b6 5580 return FALSE;
411e1bfb
AM
5581 ent->next = eh->elf.got.glist;
5582 ent->addend = rel->r_addend;
e717da7e 5583 ent->owner = abfd;
411e1bfb 5584 ent->tls_type = tls_type;
927be08e 5585 ent->is_indirect = FALSE;
411e1bfb
AM
5586 ent->got.refcount = 0;
5587 eh->elf.got.glist = ent;
5bd4f169 5588 }
411e1bfb 5589 ent->got.refcount += 1;
e7b938ca 5590 eh->tls_mask |= tls_type;
5bd4f169 5591 }
411e1bfb
AM
5592 else
5593 /* This is a global offset table entry for a local symbol. */
5594 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5595 rel->r_addend, tls_type))
5596 return FALSE;
a345bc8d
AM
5597
5598 /* We may also need a plt entry if the symbol turns out to be
5599 an ifunc. */
0e1862bb 5600 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5601 {
5602 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5603 return FALSE;
5604 }
5bd4f169
AM
5605 break;
5606
5bd4f169 5607 case R_PPC64_PLT16_HA:
65f38f15
AM
5608 case R_PPC64_PLT16_HI:
5609 case R_PPC64_PLT16_LO:
5610 case R_PPC64_PLT32:
5611 case R_PPC64_PLT64:
cbf95972
AM
5612 /* This symbol requires a procedure linkage table entry. */
5613 plt_list = ifunc;
5614 if (h != NULL)
e054468f 5615 {
e054468f
AM
5616 h->needs_plt = 1;
5617 if (h->root.root.string[0] == '.'
5618 && h->root.root.string[1] != '\0')
5619 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972
AM
5620 plt_list = &h->plt.plist;
5621 }
5622 if (plt_list == NULL)
5623 {
5624 /* It does not make sense to have a procedure linkage
5625 table entry for a non-ifunc local symbol. */
5626 info->callbacks->einfo
695344c0 5627 /* xgettext:c-format */
174d0a74 5628 (_("%H: %s reloc against local symbol\n"),
cbf95972
AM
5629 abfd, sec, rel->r_offset,
5630 ppc64_elf_howto_table[r_type]->name);
5631 bfd_set_error (bfd_error_bad_value);
5632 return FALSE;
e054468f 5633 }
cbf95972
AM
5634 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5635 return FALSE;
5bd4f169
AM
5636 break;
5637
5638 /* The following relocations don't need to propagate the
5639 relocation if linking a shared object since they are
5640 section relative. */
5641 case R_PPC64_SECTOFF:
5642 case R_PPC64_SECTOFF_LO:
5643 case R_PPC64_SECTOFF_HI:
5644 case R_PPC64_SECTOFF_HA:
5645 case R_PPC64_SECTOFF_DS:
5646 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5647 case R_PPC64_DTPREL16:
5648 case R_PPC64_DTPREL16_LO:
5649 case R_PPC64_DTPREL16_HI:
5650 case R_PPC64_DTPREL16_HA:
5651 case R_PPC64_DTPREL16_DS:
5652 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5653 case R_PPC64_DTPREL16_HIGH:
5654 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5655 case R_PPC64_DTPREL16_HIGHER:
5656 case R_PPC64_DTPREL16_HIGHERA:
5657 case R_PPC64_DTPREL16_HIGHEST:
5658 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5659 break;
5660
ad8e1ba5 5661 /* Nor do these. */
25f23106
AM
5662 case R_PPC64_REL16:
5663 case R_PPC64_REL16_LO:
5664 case R_PPC64_REL16_HI:
5665 case R_PPC64_REL16_HA:
a680de9a 5666 case R_PPC64_REL16DX_HA:
25f23106
AM
5667 break;
5668
45965137
AM
5669 /* Not supported as a dynamic relocation. */
5670 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5671 if (bfd_link_pic (info))
45965137
AM
5672 {
5673 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5674 ppc_howto_init ();
695344c0 5675 /* xgettext:c-format */
174d0a74 5676 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 5677 "in shared libraries and PIEs\n"),
45965137
AM
5678 abfd, sec, rel->r_offset,
5679 ppc64_elf_howto_table[r_type]->name);
5680 bfd_set_error (bfd_error_bad_value);
5681 return FALSE;
5682 }
5683 break;
5684
ad8e1ba5 5685 case R_PPC64_TOC16:
33c0ec9d
AM
5686 case R_PPC64_TOC16_DS:
5687 htab->do_multi_toc = 1;
d77c8a4b 5688 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5689 /* Fall through. */
ad8e1ba5
AM
5690 case R_PPC64_TOC16_LO:
5691 case R_PPC64_TOC16_HI:
5692 case R_PPC64_TOC16_HA:
ad8e1ba5 5693 case R_PPC64_TOC16_LO_DS:
4c52953f 5694 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5695 break;
5696
006589cf
AM
5697 /* Marker reloc. */
5698 case R_PPC64_ENTRY:
5699 break;
5700
5bd4f169
AM
5701 /* This relocation describes the C++ object vtable hierarchy.
5702 Reconstruct it for later use during GC. */
5703 case R_PPC64_GNU_VTINHERIT:
c152c796 5704 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5705 return FALSE;
5bd4f169
AM
5706 break;
5707
5708 /* This relocation describes which C++ vtable entries are actually
5709 used. Record for later use during GC. */
5710 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5711 BFD_ASSERT (h != NULL);
5712 if (h != NULL
5713 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5714 return FALSE;
5bd4f169
AM
5715 break;
5716
721956f4
AM
5717 case R_PPC64_REL14:
5718 case R_PPC64_REL14_BRTAKEN:
5719 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5720 {
5721 asection *dest = NULL;
5722
5723 /* Heuristic: If jumping outside our section, chances are
5724 we are going to need a stub. */
5725 if (h != NULL)
5726 {
5727 /* If the sym is weak it may be overridden later, so
5728 don't assume we know where a weak sym lives. */
5729 if (h->root.type == bfd_link_hash_defined)
5730 dest = h->root.u.def.section;
5731 }
5732 else
87d72d41
AM
5733 {
5734 Elf_Internal_Sym *isym;
5735
5736 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5737 abfd, r_symndx);
5738 if (isym == NULL)
5739 return FALSE;
5740
5741 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5742 }
5743
220c76dd 5744 if (dest != sec)
7c8fe5c4 5745 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5746 }
721956f4
AM
5747 /* Fall through. */
5748
5d1634d7 5749 case R_PPC64_REL24:
cbf95972
AM
5750 plt_list = ifunc;
5751 if (h != NULL)
5d1634d7 5752 {
e054468f
AM
5753 h->needs_plt = 1;
5754 if (h->root.root.string[0] == '.'
5755 && h->root.root.string[1] != '\0')
5756 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5757
3a71aa26 5758 if (h == tga || h == dottga)
cbf95972
AM
5759 {
5760 sec->has_tls_reloc = 1;
5761 if (rel != relocs
5762 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5763 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5764 /* We have a new-style __tls_get_addr call with
5765 a marker reloc. */
5766 ;
5767 else
5768 /* Mark this section as having an old-style call. */
5769 sec->has_tls_get_addr_call = 1;
5770 }
5771 plt_list = &h->plt.plist;
411e1bfb 5772 }
cbf95972
AM
5773
5774 /* We may need a .plt entry if the function this reloc
5775 refers to is in a shared lib. */
5776 if (plt_list
5777 && !update_plt_info (abfd, plt_list, rel->r_addend))
5778 return FALSE;
411e1bfb
AM
5779 break;
5780
cbf95972
AM
5781 case R_PPC64_ADDR14:
5782 case R_PPC64_ADDR14_BRNTAKEN:
5783 case R_PPC64_ADDR14_BRTAKEN:
5784 case R_PPC64_ADDR24:
5785 goto dodyn;
5786
411e1bfb
AM
5787 case R_PPC64_TPREL64:
5788 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5789 if (bfd_link_dll (info))
411e1bfb
AM
5790 info->flags |= DF_STATIC_TLS;
5791 goto dotlstoc;
5792
5793 case R_PPC64_DTPMOD64:
5794 if (rel + 1 < rel_end
5795 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5796 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5797 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5798 else
951fd09b 5799 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5800 goto dotlstoc;
5801
5802 case R_PPC64_DTPREL64:
5803 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5804 if (rel != relocs
5805 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5806 && rel[-1].r_offset == rel->r_offset - 8)
5807 /* This is the second reloc of a dtpmod, dtprel pair.
5808 Don't mark with TLS_DTPREL. */
5809 goto dodyn;
5810
5811 dotlstoc:
5812 sec->has_tls_reloc = 1;
5813 if (h != NULL)
5814 {
5815 struct ppc_link_hash_entry *eh;
5816 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5817 eh->tls_mask |= tls_type;
411e1bfb
AM
5818 }
5819 else
5820 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5821 rel->r_addend, tls_type))
5822 return FALSE;
5823
7c8fe5c4
AM
5824 ppc64_sec = ppc64_elf_section_data (sec);
5825 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5826 {
3a71aa26
AM
5827 bfd_size_type amt;
5828
e7b938ca 5829 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5830 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5831 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5832 if (ppc64_sec->u.toc.symndx == NULL)
5833 return FALSE;
5834 amt = sec->size * sizeof (bfd_vma) / 8;
5835 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5836 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5837 return FALSE;
7c8fe5c4
AM
5838 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5839 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5840 }
5841 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5842 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5843 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5844
5845 /* Mark the second slot of a GD or LD entry.
5846 -1 to indicate GD and -2 to indicate LD. */
5847 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5848 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5849 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5850 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5851 goto dodyn;
5852
5853 case R_PPC64_TPREL16:
5854 case R_PPC64_TPREL16_LO:
5855 case R_PPC64_TPREL16_HI:
5856 case R_PPC64_TPREL16_HA:
5857 case R_PPC64_TPREL16_DS:
5858 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5859 case R_PPC64_TPREL16_HIGH:
5860 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5861 case R_PPC64_TPREL16_HIGHER:
5862 case R_PPC64_TPREL16_HIGHERA:
5863 case R_PPC64_TPREL16_HIGHEST:
5864 case R_PPC64_TPREL16_HIGHESTA:
7c8bbca5
AM
5865 if (bfd_link_dll (info))
5866 info->flags |= DF_STATIC_TLS;
5867 goto dodyn;
5d1634d7 5868
e86ce104 5869 case R_PPC64_ADDR64:
b9399fcf 5870 if (is_opd
1e2f5b6e 5871 && rel + 1 < rel_end
4ce794b7 5872 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5873 {
8387904d 5874 if (h != NULL)
8c5b4e52 5875 ((struct ppc_link_hash_entry *) h)->is_func = 1;
1e2f5b6e 5876 }
e86ce104
AM
5877 /* Fall through. */
5878
65f38f15
AM
5879 case R_PPC64_ADDR16:
5880 case R_PPC64_ADDR16_DS:
5881 case R_PPC64_ADDR16_HA:
5882 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5883 case R_PPC64_ADDR16_HIGH:
5884 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5885 case R_PPC64_ADDR16_HIGHER:
5886 case R_PPC64_ADDR16_HIGHERA:
5887 case R_PPC64_ADDR16_HIGHEST:
5888 case R_PPC64_ADDR16_HIGHESTA:
5889 case R_PPC64_ADDR16_LO:
5890 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5891 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5892 && rel->r_addend == 0)
5893 {
5894 /* We may need a .plt entry if this reloc refers to a
5895 function in a shared lib. */
5896 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5897 return FALSE;
5898 h->pointer_equality_needed = 1;
5899 }
5900 /* Fall through. */
5901
5902 case R_PPC64_REL30:
5903 case R_PPC64_REL32:
5904 case R_PPC64_REL64:
65f38f15 5905 case R_PPC64_ADDR32:
65f38f15
AM
5906 case R_PPC64_UADDR16:
5907 case R_PPC64_UADDR32:
5908 case R_PPC64_UADDR64:
5bd4f169 5909 case R_PPC64_TOC:
0e1862bb 5910 if (h != NULL && !bfd_link_pic (info))
81848ca0 5911 /* We may need a copy reloc. */
f5385ebf 5912 h->non_got_ref = 1;
81848ca0 5913
41bd81ab 5914 /* Don't propagate .opd relocs. */
b9399fcf 5915 if (NO_OPD_RELOCS && is_opd)
e86ce104 5916 break;
e86ce104 5917
65f38f15
AM
5918 /* If we are creating a shared library, and this is a reloc
5919 against a global symbol, or a non PC relative reloc
5920 against a local symbol, then we need to copy the reloc
5921 into the shared library. However, if we are linking with
5922 -Bsymbolic, we do not need to copy a reloc against a
5923 global symbol which is defined in an object we are
5924 including in the link (i.e., DEF_REGULAR is set). At
5925 this point we have not seen all the input files, so it is
5926 possible that DEF_REGULAR is not set now but will be set
5927 later (it is never cleared). In case of a weak definition,
5928 DEF_REGULAR may be cleared later by a strong definition in
5929 a shared library. We account for that possibility below by
f4656909 5930 storing information in the dyn_relocs field of the hash
65f38f15
AM
5931 table entry. A similar situation occurs when creating
5932 shared libraries and symbol visibility changes render the
5933 symbol local.
5934
5935 If on the other hand, we are creating an executable, we
5936 may need to keep relocations for symbols satisfied by a
5937 dynamic library if we manage to avoid copy relocs for the
5938 symbol. */
411e1bfb 5939 dodyn:
0e1862bb 5940 if ((bfd_link_pic (info)
1d483afe 5941 && (must_be_dyn_reloc (info, r_type)
65f38f15 5942 || (h != NULL
198f1157 5943 && (!SYMBOLIC_BIND (info, h)
65f38f15 5944 || h->root.type == bfd_link_hash_defweak
f5385ebf 5945 || !h->def_regular))))
f4656909 5946 || (ELIMINATE_COPY_RELOCS
0e1862bb 5947 && !bfd_link_pic (info)
65f38f15
AM
5948 && h != NULL
5949 && (h->root.type == bfd_link_hash_defweak
25f23106 5950 || !h->def_regular))
0e1862bb 5951 || (!bfd_link_pic (info)
25f23106 5952 && ifunc != NULL))
5bd4f169 5953 {
65f38f15
AM
5954 /* We must copy these reloc types into the output file.
5955 Create a reloc section in dynobj and make room for
5956 this reloc. */
5bd4f169
AM
5957 if (sreloc == NULL)
5958 {
83bac4b0
NC
5959 sreloc = _bfd_elf_make_dynamic_reloc_section
5960 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5961
5bd4f169 5962 if (sreloc == NULL)
83bac4b0 5963 return FALSE;
5bd4f169
AM
5964 }
5965
65f38f15
AM
5966 /* If this is a global symbol, we count the number of
5967 relocations we need for this symbol. */
5968 if (h != NULL)
5969 {
19e08130
AM
5970 struct elf_dyn_relocs *p;
5971 struct elf_dyn_relocs **head;
5972
ec338859 5973 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5974 p = *head;
5975 if (p == NULL || p->sec != sec)
5976 {
5977 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5978 if (p == NULL)
5979 return FALSE;
5980 p->next = *head;
5981 *head = p;
5982 p->sec = sec;
5983 p->count = 0;
5984 p->pc_count = 0;
5985 }
5986 p->count += 1;
5987 if (!must_be_dyn_reloc (info, r_type))
5988 p->pc_count += 1;
65f38f15
AM
5989 }
5990 else
5991 {
ec338859
AM
5992 /* Track dynamic relocs needed for local syms too.
5993 We really need local syms available to do this
5994 easily. Oh well. */
19e08130
AM
5995 struct ppc_dyn_relocs *p;
5996 struct ppc_dyn_relocs **head;
5997 bfd_boolean is_ifunc;
ec338859 5998 asection *s;
6edfbbad 5999 void *vpp;
87d72d41 6000 Elf_Internal_Sym *isym;
6edfbbad 6001
87d72d41
AM
6002 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6003 abfd, r_symndx);
6004 if (isym == NULL)
b34976b6 6005 return FALSE;
ec338859 6006
87d72d41
AM
6007 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6008 if (s == NULL)
6009 s = sec;
6010
6edfbbad 6011 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
6012 head = (struct ppc_dyn_relocs **) vpp;
6013 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6014 p = *head;
6015 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6016 p = p->next;
6017 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6018 {
6019 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6020 if (p == NULL)
6021 return FALSE;
6022 p->next = *head;
6023 *head = p;
6024 p->sec = sec;
6025 p->ifunc = is_ifunc;
6026 p->count = 0;
6027 }
6028 p->count += 1;
ec338859 6029 }
65f38f15 6030 }
5bd4f169 6031 break;
65f38f15
AM
6032
6033 default:
96e0dda4 6034 break;
5bd4f169
AM
6035 }
6036 }
6037
b34976b6 6038 return TRUE;
5bd4f169
AM
6039}
6040
ee67d69a
AM
6041/* Merge backend specific data from an object file to the output
6042 object file when linking. */
6043
6044static bfd_boolean
50e03d47 6045ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 6046{
50e03d47 6047 bfd *obfd = info->output_bfd;
ee67d69a
AM
6048 unsigned long iflags, oflags;
6049
6050 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6051 return TRUE;
6052
6053 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6054 return TRUE;
6055
50e03d47 6056 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6057 return FALSE;
6058
6059 iflags = elf_elfheader (ibfd)->e_flags;
6060 oflags = elf_elfheader (obfd)->e_flags;
6061
f6c7c3e8 6062 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6063 {
4eca0228 6064 _bfd_error_handler
695344c0 6065 /* xgettext:c-format */
871b3ab2 6066 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
6067 bfd_set_error (bfd_error_bad_value);
6068 return FALSE;
6069 }
f6c7c3e8 6070 else if (iflags != oflags && iflags != 0)
ee67d69a 6071 {
4eca0228 6072 _bfd_error_handler
695344c0 6073 /* xgettext:c-format */
871b3ab2 6074 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
6075 ibfd, iflags, oflags);
6076 bfd_set_error (bfd_error_bad_value);
6077 return FALSE;
6078 }
6079
50e03d47 6080 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
005d79fd 6081
ee67d69a 6082 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 6083 _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6084
6085 return TRUE;
6086}
6087
6088static bfd_boolean
6089ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6090{
6091 /* Print normal ELF private data. */
6092 _bfd_elf_print_private_bfd_data (abfd, ptr);
6093
6094 if (elf_elfheader (abfd)->e_flags != 0)
6095 {
6096 FILE *file = ptr;
6097
ee67d69a
AM
6098 fprintf (file, _("private flags = 0x%lx:"),
6099 elf_elfheader (abfd)->e_flags);
6100
6101 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6102 fprintf (file, _(" [abiv%ld]"),
6103 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6104 fputc ('\n', file);
6105 }
6106
6107 return TRUE;
6108}
6109
8387904d 6110/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6111 of the code entry point, and its section, which must be in the same
6112 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6113
6114static bfd_vma
6115opd_entry_value (asection *opd_sec,
6116 bfd_vma offset,
6117 asection **code_sec,
aef36ac1
AM
6118 bfd_vma *code_off,
6119 bfd_boolean in_code_sec)
8387904d
AM
6120{
6121 bfd *opd_bfd = opd_sec->owner;
8860955f 6122 Elf_Internal_Rela *relocs;
8387904d 6123 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6124 bfd_vma val;
8387904d 6125
9f296da3
AM
6126 /* No relocs implies we are linking a --just-symbols object, or looking
6127 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6128 if (opd_sec->reloc_count == 0)
6129 {
729eabd5 6130 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6131
729eabd5
AM
6132 if (contents == NULL)
6133 {
6134 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6135 return (bfd_vma) -1;
6136 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6137 }
ee1e4ede 6138
dbb3fbbb 6139 /* PR 17512: file: 64b9dfbb. */
451dfd38 6140 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6141 return (bfd_vma) -1;
6142
729eabd5 6143 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6144 if (code_sec != NULL)
6145 {
6146 asection *sec, *likely = NULL;
ee1e4ede 6147
aef36ac1 6148 if (in_code_sec)
4b85d634 6149 {
aef36ac1
AM
6150 sec = *code_sec;
6151 if (sec->vma <= val
6152 && val < sec->vma + sec->size)
6153 likely = sec;
6154 else
6155 val = -1;
6156 }
6157 else
6158 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6159 if (sec->vma <= val
6160 && (sec->flags & SEC_LOAD) != 0
6161 && (sec->flags & SEC_ALLOC) != 0)
6162 likely = sec;
6163 if (likely != NULL)
6164 {
6165 *code_sec = likely;
6166 if (code_off != NULL)
6167 *code_off = val - likely->vma;
4b85d634
AM
6168 }
6169 }
aef36ac1 6170 return val;
4b85d634
AM
6171 }
6172
0c8d6e5c 6173 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6174
729eabd5 6175 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6176 if (relocs == NULL)
6177 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6178 /* PR 17512: file: df8e1fd6. */
6179 if (relocs == NULL)
6180 return (bfd_vma) -1;
645ea6a9 6181
8387904d 6182 /* Go find the opd reloc at the sym address. */
8860955f 6183 lo = relocs;
8387904d 6184 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6185 val = (bfd_vma) -1;
8387904d
AM
6186 while (lo < hi)
6187 {
6188 look = lo + (hi - lo) / 2;
6189 if (look->r_offset < offset)
6190 lo = look + 1;
6191 else if (look->r_offset > offset)
6192 hi = look;
6193 else
6194 {
0ffa91dd
NC
6195 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6196
8387904d
AM
6197 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6198 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6199 {
6200 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6201 asection *sec = NULL;
8387904d 6202
b53dfeb2
AM
6203 if (symndx >= symtab_hdr->sh_info
6204 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6205 {
6206 struct elf_link_hash_entry **sym_hashes;
6207 struct elf_link_hash_entry *rh;
6208
6209 sym_hashes = elf_sym_hashes (opd_bfd);
6210 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6211 if (rh != NULL)
6212 {
6213 rh = elf_follow_link (rh);
bb854a36
AM
6214 if (rh->root.type != bfd_link_hash_defined
6215 && rh->root.type != bfd_link_hash_defweak)
6216 break;
6217 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6218 {
bb854a36
AM
6219 val = rh->root.u.def.value;
6220 sec = rh->root.u.def.section;
b53dfeb2
AM
6221 }
6222 }
6223 }
6224
6225 if (sec == NULL)
6226 {
6227 Elf_Internal_Sym *sym;
6228
6229 if (symndx < symtab_hdr->sh_info)
6230 {
6231 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6232 if (sym == NULL)
6233 {
6234 size_t symcnt = symtab_hdr->sh_info;
6235 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6236 symcnt, 0,
6237 NULL, NULL, NULL);
6238 if (sym == NULL)
6239 break;
6240 symtab_hdr->contents = (bfd_byte *) sym;
6241 }
6242 sym += symndx;
128205bb
AM
6243 }
6244 else
6245 {
b53dfeb2
AM
6246 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6247 1, symndx,
6248 NULL, NULL, NULL);
128205bb
AM
6249 if (sym == NULL)
6250 break;
128205bb 6251 }
b53dfeb2
AM
6252 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6253 if (sec == NULL)
6254 break;
6255 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6256 val = sym->st_value;
8387904d 6257 }
b53dfeb2 6258
8387904d
AM
6259 val += look->r_addend;
6260 if (code_off != NULL)
6261 *code_off = val;
6262 if (code_sec != NULL)
aef36ac1
AM
6263 {
6264 if (in_code_sec && *code_sec != sec)
6265 return -1;
6266 else
6267 *code_sec = sec;
6268 }
b53dfeb2 6269 if (sec->output_section != NULL)
8387904d 6270 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6271 }
6272 break;
6273 }
6274 }
645ea6a9 6275
645ea6a9 6276 return val;
8387904d
AM
6277}
6278
aef36ac1
AM
6279/* If the ELF symbol SYM might be a function in SEC, return the
6280 function size and set *CODE_OFF to the function's entry point,
6281 otherwise return zero. */
9f296da3 6282
aef36ac1
AM
6283static bfd_size_type
6284ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6285 bfd_vma *code_off)
9f296da3 6286{
aef36ac1
AM
6287 bfd_size_type size;
6288
6289 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6290 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6291 return 0;
6292
6293 size = 0;
6294 if (!(sym->flags & BSF_SYNTHETIC))
6295 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6296
6297 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6298 {
b07bca4e
AM
6299 struct _opd_sec_data *opd = get_opd_info (sym->section);
6300 bfd_vma symval = sym->value;
6301
6302 if (opd != NULL
6303 && opd->adjust != NULL
6304 && elf_section_data (sym->section)->relocs != NULL)
6305 {
6306 /* opd_entry_value will use cached relocs that have been
6307 adjusted, but with raw symbols. That means both local
6308 and global symbols need adjusting. */
6309 long adjust = opd->adjust[OPD_NDX (symval)];
6310 if (adjust == -1)
6311 return 0;
6312 symval += adjust;
6313 }
6314
6315 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6316 &sec, code_off, TRUE) == (bfd_vma) -1)
6317 return 0;
6318 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6319 symbol. This size has nothing to do with the code size of the
6320 function, which is what we're supposed to return, but the
6321 code size isn't available without looking up the dot-sym.
6322 However, doing that would be a waste of time particularly
6323 since elf_find_function will look at the dot-sym anyway.
6324 Now, elf_find_function will keep the largest size of any
6325 function sym found at the code address of interest, so return
6326 1 here to avoid it incorrectly caching a larger function size
6327 for a small function. This does mean we return the wrong
6328 size for a new-ABI function of size 24, but all that does is
6329 disable caching for such functions. */
6330 if (size == 24)
6331 size = 1;
9f296da3 6332 }
aef36ac1
AM
6333 else
6334 {
6335 if (sym->section != sec)
6336 return 0;
6337 *code_off = sym->value;
6338 }
6339 if (size == 0)
6340 size = 1;
6341 return size;
9f296da3
AM
6342}
6343
f378ab09
AM
6344/* Return true if symbol is a strong function defined in an ELFv2
6345 object with st_other localentry bits of zero, ie. its local entry
6346 point coincides with its global entry point. */
6347
6348static bfd_boolean
6349is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6350{
6351 return (h != NULL
6352 && h->type == STT_FUNC
6353 && h->root.type == bfd_link_hash_defined
6354 && (STO_PPC64_LOCAL_MASK & h->other) == 0
8b5f1ed8 6355 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
f378ab09
AM
6356 && is_ppc64_elf (h->root.u.def.section->owner)
6357 && abiversion (h->root.u.def.section->owner) >= 2);
6358}
6359
854b41e7
AM
6360/* Return true if symbol is defined in a regular object file. */
6361
6362static bfd_boolean
6363is_static_defined (struct elf_link_hash_entry *h)
6364{
6365 return ((h->root.type == bfd_link_hash_defined
6366 || h->root.type == bfd_link_hash_defweak)
6367 && h->root.u.def.section != NULL
6368 && h->root.u.def.section->output_section != NULL);
6369}
6370
b31867b6
AM
6371/* If FDH is a function descriptor symbol, return the associated code
6372 entry symbol if it is defined. Return NULL otherwise. */
6373
6374static struct ppc_link_hash_entry *
6375defined_code_entry (struct ppc_link_hash_entry *fdh)
6376{
6377 if (fdh->is_func_descriptor)
6378 {
6379 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6380 if (fh->elf.root.type == bfd_link_hash_defined
6381 || fh->elf.root.type == bfd_link_hash_defweak)
6382 return fh;
6383 }
6384 return NULL;
6385}
6386
6387/* If FH is a function code entry symbol, return the associated
6388 function descriptor symbol if it is defined. Return NULL otherwise. */
6389
6390static struct ppc_link_hash_entry *
6391defined_func_desc (struct ppc_link_hash_entry *fh)
6392{
6393 if (fh->oh != NULL
6394 && fh->oh->is_func_descriptor)
6395 {
6396 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6397 if (fdh->elf.root.type == bfd_link_hash_defined
6398 || fdh->elf.root.type == bfd_link_hash_defweak)
6399 return fdh;
6400 }
6401 return NULL;
6402}
6403
8c5b4e52
AM
6404static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6405
6406/* Garbage collect sections, after first dealing with dot-symbols. */
6407
6408static bfd_boolean
6409ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6410{
6411 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6412
6413 if (htab != NULL && htab->need_func_desc_adj)
6414 {
6415 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6416 htab->need_func_desc_adj = 0;
6417 }
6418 return bfd_elf_gc_sections (abfd, info);
6419}
6420
74f0fb50
AM
6421/* Mark all our entry sym sections, both opd and code section. */
6422
6423static void
6424ppc64_elf_gc_keep (struct bfd_link_info *info)
6425{
6426 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6427 struct bfd_sym_chain *sym;
6428
4dfe6ac6
NC
6429 if (htab == NULL)
6430 return;
6431
74f0fb50
AM
6432 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6433 {
b31867b6 6434 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6435 asection *sec;
6436
6437 eh = (struct ppc_link_hash_entry *)
b31867b6 6438 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6439 if (eh == NULL)
6440 continue;
6441 if (eh->elf.root.type != bfd_link_hash_defined
6442 && eh->elf.root.type != bfd_link_hash_defweak)
6443 continue;
6444
b31867b6
AM
6445 fh = defined_code_entry (eh);
6446 if (fh != NULL)
74f0fb50 6447 {
b31867b6 6448 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6449 sec->flags |= SEC_KEEP;
6450 }
6451 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6452 && opd_entry_value (eh->elf.root.u.def.section,
6453 eh->elf.root.u.def.value,
aef36ac1 6454 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6455 sec->flags |= SEC_KEEP;
6456
6457 sec = eh->elf.root.u.def.section;
6458 sec->flags |= SEC_KEEP;
6459 }
6460}
6461
64d03ab5
AM
6462/* Mark sections containing dynamically referenced symbols. When
6463 building shared libraries, we must assume that any visible symbol is
6464 referenced. */
6465
6466static bfd_boolean
6467ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6468{
6469 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6470 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6471 struct ppc_link_hash_entry *fdh;
b407645f 6472 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6473
64d03ab5 6474 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6475 fdh = defined_func_desc (eh);
6476 if (fdh != NULL)
6477 eh = fdh;
64d03ab5
AM
6478
6479 if ((eh->elf.root.type == bfd_link_hash_defined
6480 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 6481 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 6482 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6483 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6484 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6485 && (!bfd_link_executable (info)
e278ae05 6486 || info->gc_keep_exported
b407645f
AM
6487 || info->export_dynamic
6488 || (eh->elf.dynamic
6489 && d != NULL
6490 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
e278ae05 6491 && (eh->elf.versioned >= versioned
4c58e0d8
AM
6492 || !bfd_hide_sym_by_version (info->version_info,
6493 eh->elf.root.root.string)))))
64d03ab5
AM
6494 {
6495 asection *code_sec;
b31867b6 6496 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6497
6498 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6499
6500 /* Function descriptor syms cause the associated
6501 function code sym section to be marked. */
b31867b6
AM
6502 fh = defined_code_entry (eh);
6503 if (fh != NULL)
6504 {
6505 code_sec = fh->elf.root.u.def.section;
6506 code_sec->flags |= SEC_KEEP;
6507 }
64d03ab5
AM
6508 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6509 && opd_entry_value (eh->elf.root.u.def.section,
6510 eh->elf.root.u.def.value,
aef36ac1 6511 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6512 code_sec->flags |= SEC_KEEP;
6513 }
6514
6515 return TRUE;
6516}
6517
5bd4f169
AM
6518/* Return the section that should be marked against GC for a given
6519 relocation. */
6520
6521static asection *
4ce794b7 6522ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6523 struct bfd_link_info *info,
4ce794b7
AM
6524 Elf_Internal_Rela *rel,
6525 struct elf_link_hash_entry *h,
6526 Elf_Internal_Sym *sym)
5bd4f169 6527{
ccfa59ea
AM
6528 asection *rsec;
6529
ccfa59ea
AM
6530 /* Syms return NULL if we're marking .opd, so we avoid marking all
6531 function sections, as all functions are referenced in .opd. */
6532 rsec = NULL;
6533 if (get_opd_info (sec) != NULL)
6534 return rsec;
1e2f5b6e 6535
5bd4f169
AM
6536 if (h != NULL)
6537 {
04c9666a 6538 enum elf_ppc64_reloc_type r_type;
b31867b6 6539 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6540
4ce794b7 6541 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6542 switch (r_type)
5bd4f169
AM
6543 {
6544 case R_PPC64_GNU_VTINHERIT:
6545 case R_PPC64_GNU_VTENTRY:
6546 break;
6547
6548 default:
6549 switch (h->root.type)
6550 {
6551 case bfd_link_hash_defined:
6552 case bfd_link_hash_defweak:
ccfa59ea 6553 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6554 fdh = defined_func_desc (eh);
6555 if (fdh != NULL)
8c5b4e52
AM
6556 {
6557 /* -mcall-aixdesc code references the dot-symbol on
6558 a call reloc. Mark the function descriptor too
6559 against garbage collection. */
6560 fdh->elf.mark = 1;
60d67dc8
AM
6561 if (fdh->elf.is_weakalias)
6562 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6563 eh = fdh;
6564 }
1e2f5b6e
AM
6565
6566 /* Function descriptor syms cause the associated
6567 function code sym section to be marked. */
b31867b6
AM
6568 fh = defined_code_entry (eh);
6569 if (fh != NULL)
ccfa59ea
AM
6570 {
6571 /* They also mark their opd section. */
74f0fb50 6572 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6573
b31867b6 6574 rsec = fh->elf.root.u.def.section;
ccfa59ea 6575 }
8387904d
AM
6576 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6577 && opd_entry_value (eh->elf.root.u.def.section,
6578 eh->elf.root.u.def.value,
aef36ac1 6579 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6580 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6581 else
1e2f5b6e
AM
6582 rsec = h->root.u.def.section;
6583 break;
5bd4f169
AM
6584
6585 case bfd_link_hash_common:
1e2f5b6e
AM
6586 rsec = h->root.u.c.p->section;
6587 break;
5bd4f169
AM
6588
6589 default:
fb34365b 6590 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6591 }
6592 }
6593 }
6594 else
6595 {
74f0fb50 6596 struct _opd_sec_data *opd;
1e2f5b6e
AM
6597
6598 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6599 opd = get_opd_info (rsec);
6600 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6601 {
74f0fb50 6602 rsec->gc_mark = 1;
ccfa59ea 6603
51aecdc5 6604 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6605 }
5bd4f169
AM
6606 }
6607
1e2f5b6e 6608 return rsec;
5bd4f169
AM
6609}
6610
deb0e272
AM
6611/* The maximum size of .sfpr. */
6612#define SFPR_MAX (218*4)
6613
6614struct sfpr_def_parms
6615{
699733f6
AM
6616 const char name[12];
6617 unsigned char lo, hi;
deb0e272
AM
6618 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6619 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6620};
6621
a4b6fadd
AM
6622/* Auto-generate _save*, _rest* functions in .sfpr.
6623 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6624 instead. */
deb0e272 6625
4dfe6ac6 6626static bfd_boolean
a4b6fadd
AM
6627sfpr_define (struct bfd_link_info *info,
6628 const struct sfpr_def_parms *parm,
6629 asection *stub_sec)
deb0e272
AM
6630{
6631 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6632 unsigned int i;
6633 size_t len = strlen (parm->name);
6634 bfd_boolean writing = FALSE;
699733f6 6635 char sym[16];
deb0e272 6636
4dfe6ac6
NC
6637 if (htab == NULL)
6638 return FALSE;
6639
deb0e272
AM
6640 memcpy (sym, parm->name, len);
6641 sym[len + 2] = 0;
6642
6643 for (i = parm->lo; i <= parm->hi; i++)
6644 {
a4b6fadd 6645 struct ppc_link_hash_entry *h;
deb0e272
AM
6646
6647 sym[len + 0] = i / 10 + '0';
6648 sym[len + 1] = i % 10 + '0';
a4b6fadd 6649 h = (struct ppc_link_hash_entry *)
b32547cd 6650 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6651 if (stub_sec != NULL)
deb0e272 6652 {
a4b6fadd
AM
6653 if (h != NULL
6654 && h->elf.root.type == bfd_link_hash_defined
6655 && h->elf.root.u.def.section == htab->sfpr)
6656 {
6657 struct elf_link_hash_entry *s;
6658 char buf[32];
6659 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6660 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6661 if (s == NULL)
6662 return FALSE;
6663 if (s->root.type == bfd_link_hash_new
6664 || (s->root.type = bfd_link_hash_defined
6665 && s->root.u.def.section == stub_sec))
6666 {
6667 s->root.type = bfd_link_hash_defined;
6668 s->root.u.def.section = stub_sec;
7dda8d3c 6669 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
6670 + h->elf.root.u.def.value);
6671 s->ref_regular = 1;
6672 s->def_regular = 1;
6673 s->ref_regular_nonweak = 1;
6674 s->forced_local = 1;
6675 s->non_elf = 0;
6676 s->root.linker_def = 1;
6677 }
6678 }
6679 continue;
6680 }
6681 if (h != NULL)
6682 {
6683 h->save_res = 1;
6684 if (!h->elf.def_regular)
deb0e272 6685 {
a4b6fadd
AM
6686 h->elf.root.type = bfd_link_hash_defined;
6687 h->elf.root.u.def.section = htab->sfpr;
6688 h->elf.root.u.def.value = htab->sfpr->size;
6689 h->elf.type = STT_FUNC;
6690 h->elf.def_regular = 1;
b32547cd 6691 h->elf.non_elf = 0;
a4b6fadd
AM
6692 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6693 writing = TRUE;
deb0e272 6694 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6695 {
6696 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6697 if (htab->sfpr->contents == NULL)
6698 return FALSE;
6699 }
deb0e272
AM
6700 }
6701 }
6702 if (writing)
6703 {
6704 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6705 if (i != parm->hi)
6706 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6707 else
6708 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6709 htab->sfpr->size = p - htab->sfpr->contents;
6710 }
6711 }
6712
6713 return TRUE;
6714}
6715
6716static bfd_byte *
6717savegpr0 (bfd *abfd, bfd_byte *p, int r)
6718{
6719 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6720 return p + 4;
6721}
6722
6723static bfd_byte *
6724savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6725{
6726 p = savegpr0 (abfd, p, r);
a078d95a 6727 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6728 p = p + 4;
6729 bfd_put_32 (abfd, BLR, p);
6730 return p + 4;
6731}
6732
6733static bfd_byte *
6734restgpr0 (bfd *abfd, bfd_byte *p, int r)
6735{
6736 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6737 return p + 4;
6738}
6739
6740static bfd_byte *
6741restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6742{
a078d95a 6743 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6744 p = p + 4;
6745 p = restgpr0 (abfd, p, r);
6746 bfd_put_32 (abfd, MTLR_R0, p);
6747 p = p + 4;
6748 if (r == 29)
6749 {
6750 p = restgpr0 (abfd, p, 30);
6751 p = restgpr0 (abfd, p, 31);
6752 }
6753 bfd_put_32 (abfd, BLR, p);
6754 return p + 4;
6755}
6756
6757static bfd_byte *
6758savegpr1 (bfd *abfd, bfd_byte *p, int r)
6759{
6760 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6761 return p + 4;
6762}
6763
6764static bfd_byte *
6765savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6766{
6767 p = savegpr1 (abfd, p, r);
6768 bfd_put_32 (abfd, BLR, p);
6769 return p + 4;
6770}
6771
6772static bfd_byte *
6773restgpr1 (bfd *abfd, bfd_byte *p, int r)
6774{
6775 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6776 return p + 4;
6777}
6778
6779static bfd_byte *
6780restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6781{
6782 p = restgpr1 (abfd, p, r);
6783 bfd_put_32 (abfd, BLR, p);
6784 return p + 4;
6785}
6786
6787static bfd_byte *
6788savefpr (bfd *abfd, bfd_byte *p, int r)
6789{
6790 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6791 return p + 4;
6792}
6793
6794static bfd_byte *
6795savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6796{
6797 p = savefpr (abfd, p, r);
a078d95a 6798 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6799 p = p + 4;
6800 bfd_put_32 (abfd, BLR, p);
6801 return p + 4;
6802}
6803
6804static bfd_byte *
6805restfpr (bfd *abfd, bfd_byte *p, int r)
6806{
6807 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6808 return p + 4;
6809}
6810
6811static bfd_byte *
6812restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6813{
a078d95a 6814 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6815 p = p + 4;
6816 p = restfpr (abfd, p, r);
6817 bfd_put_32 (abfd, MTLR_R0, p);
6818 p = p + 4;
6819 if (r == 29)
6820 {
6821 p = restfpr (abfd, p, 30);
6822 p = restfpr (abfd, p, 31);
6823 }
6824 bfd_put_32 (abfd, BLR, p);
6825 return p + 4;
6826}
6827
6828static bfd_byte *
6829savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6830{
6831 p = savefpr (abfd, p, r);
6832 bfd_put_32 (abfd, BLR, p);
6833 return p + 4;
6834}
6835
6836static bfd_byte *
6837restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6838{
6839 p = restfpr (abfd, p, r);
6840 bfd_put_32 (abfd, BLR, p);
6841 return p + 4;
6842}
6843
6844static bfd_byte *
6845savevr (bfd *abfd, bfd_byte *p, int r)
6846{
6847 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6848 p = p + 4;
6849 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6850 return p + 4;
6851}
6852
6853static bfd_byte *
6854savevr_tail (bfd *abfd, bfd_byte *p, int r)
6855{
6856 p = savevr (abfd, p, r);
6857 bfd_put_32 (abfd, BLR, p);
6858 return p + 4;
6859}
6860
6861static bfd_byte *
6862restvr (bfd *abfd, bfd_byte *p, int r)
6863{
6864 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6865 p = p + 4;
6866 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6867 return p + 4;
6868}
6869
6870static bfd_byte *
6871restvr_tail (bfd *abfd, bfd_byte *p, int r)
6872{
6873 p = restvr (abfd, p, r);
6874 bfd_put_32 (abfd, BLR, p);
6875 return p + 4;
6876}
6877
e86ce104
AM
6878/* Called via elf_link_hash_traverse to transfer dynamic linking
6879 information on function code symbol entries to their corresponding
6880 function descriptor symbol entries. */
deb0e272 6881
b34976b6 6882static bfd_boolean
4ce794b7 6883func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6884{
e86ce104 6885 struct bfd_link_info *info;
65f38f15 6886 struct ppc_link_hash_table *htab;
50bc7936
AM
6887 struct ppc_link_hash_entry *fh;
6888 struct ppc_link_hash_entry *fdh;
6889 bfd_boolean force_local;
5bd4f169 6890
50bc7936
AM
6891 fh = (struct ppc_link_hash_entry *) h;
6892 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6893 return TRUE;
e86ce104 6894
8c5b4e52
AM
6895 if (!fh->is_func)
6896 return TRUE;
6897
6898 if (fh->elf.root.root.string[0] != '.'
6899 || fh->elf.root.root.string[1] == '\0')
6900 return TRUE;
6901
4ce794b7 6902 info = inf;
65f38f15 6903 htab = ppc_hash_table (info);
4dfe6ac6
NC
6904 if (htab == NULL)
6905 return FALSE;
5bd4f169 6906
8c5b4e52
AM
6907 /* Find the corresponding function descriptor symbol. */
6908 fdh = lookup_fdh (fh, htab);
6909
c09bdfe5
AM
6910 /* Resolve undefined references to dot-symbols as the value
6911 in the function descriptor, if we have one in a regular object.
6912 This is to satisfy cases like ".quad .foo". Calls to functions
6913 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6914 if ((fh->elf.root.type == bfd_link_hash_undefined
6915 || fh->elf.root.type == bfd_link_hash_undefweak)
6916 && (fdh->elf.root.type == bfd_link_hash_defined
6917 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6918 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6919 && opd_entry_value (fdh->elf.root.u.def.section,
6920 fdh->elf.root.u.def.value,
c09bdfe5 6921 &fh->elf.root.u.def.section,
aef36ac1 6922 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6923 {
b31867b6 6924 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6925 fh->elf.forced_local = 1;
b31867b6
AM
6926 fh->elf.def_regular = fdh->elf.def_regular;
6927 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6928 }
6929
8c5b4e52
AM
6930 if (!fh->elf.dynamic)
6931 {
6932 struct plt_entry *ent;
5bd4f169 6933
8c5b4e52
AM
6934 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6935 if (ent->plt.refcount > 0)
6936 break;
6937 if (ent == NULL)
6938 return TRUE;
6939 }
5bd4f169 6940
8c5b4e52 6941 /* Create a descriptor as undefined if necessary. */
50bc7936 6942 if (fdh == NULL
0e1862bb 6943 && !bfd_link_executable (info)
50bc7936
AM
6944 && (fh->elf.root.type == bfd_link_hash_undefined
6945 || fh->elf.root.type == bfd_link_hash_undefweak))
6946 {
908b32fc 6947 fdh = make_fdh (info, fh);
bb700d78
AM
6948 if (fdh == NULL)
6949 return FALSE;
50bc7936 6950 }
648cca2c 6951
8c5b4e52 6952 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6953 if (fdh != NULL
6954 && fdh->fake
8c5b4e52
AM
6955 && (fh->elf.root.type == bfd_link_hash_defined
6956 || fh->elf.root.type == bfd_link_hash_defweak))
6957 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6958
8c5b4e52
AM
6959 /* Transfer dynamic linking information to the function descriptor. */
6960 if (fdh != NULL)
6961 {
f5385ebf
AM
6962 fdh->elf.ref_regular |= fh->elf.ref_regular;
6963 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6964 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6965 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6966 fdh->elf.dynamic |= fh->elf.dynamic;
6967 fdh->elf.needs_plt |= (fh->elf.needs_plt
6968 || fh->elf.type == STT_FUNC
6969 || fh->elf.type == STT_GNU_IFUNC);
6970 move_plt_plist (fh, fdh);
6971
6972 if (!fdh->elf.forced_local
6973 && fh->elf.dynindx != -1)
6974 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6975 return FALSE;
e86ce104
AM
6976 }
6977
50bc7936
AM
6978 /* Now that the info is on the function descriptor, clear the
6979 function code sym info. Any function code syms for which we
6980 don't have a definition in a regular file, we force local.
6981 This prevents a shared library from exporting syms that have
6982 been imported from another library. Function code syms that
6983 are really in the library we must leave global to prevent the
6984 linker dragging in a definition from a static library. */
93f3fa99
AM
6985 force_local = (!fh->elf.def_regular
6986 || fdh == NULL
6987 || !fdh->elf.def_regular
6988 || fdh->elf.forced_local);
50bc7936
AM
6989 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6990
b34976b6 6991 return TRUE;
e86ce104 6992}
40b8271b 6993
a4b6fadd
AM
6994static const struct sfpr_def_parms save_res_funcs[] =
6995 {
6996 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6997 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6998 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6999 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7000 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7001 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7002 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7003 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7004 { "._savef", 14, 31, savefpr, savefpr1_tail },
7005 { "._restf", 14, 31, restfpr, restfpr1_tail },
7006 { "_savevr_", 20, 31, savevr, savevr_tail },
7007 { "_restvr_", 20, 31, restvr, restvr_tail }
7008 };
7009
e86ce104 7010/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
7011 this hook to a) provide some gcc support functions, and b) transfer
7012 dynamic linking information gathered so far on function code symbol
7013 entries, to their corresponding function descriptor symbol entries. */
deb0e272 7014
b34976b6 7015static bfd_boolean
4ce794b7
AM
7016ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7017 struct bfd_link_info *info)
e86ce104
AM
7018{
7019 struct ppc_link_hash_table *htab;
7020
7021 htab = ppc_hash_table (info);
4dfe6ac6
NC
7022 if (htab == NULL)
7023 return FALSE;
7024
b32547cd
AM
7025 /* Provide any missing _save* and _rest* functions. */
7026 if (htab->sfpr != NULL)
7027 {
7028 unsigned int i;
7029
7030 htab->sfpr->size = 0;
7031 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7032 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7033 return FALSE;
7034 if (htab->sfpr->size == 0)
7035 htab->sfpr->flags |= SEC_EXCLUDE;
7036 }
7037
7038 if (bfd_link_relocatable (info))
7039 return TRUE;
7040
7041 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7042 {
7043 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7044 /* Make .TOC. defined so as to prevent it being made dynamic.
7045 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7046 if (!htab->elf.hgot->def_regular
7047 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7048 {
7049 htab->elf.hgot->root.type = bfd_link_hash_defined;
7050 htab->elf.hgot->root.u.def.value = 0;
7051 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7052 htab->elf.hgot->def_regular = 1;
7053 htab->elf.hgot->root.linker_def = 1;
7054 }
dba6fa9b 7055 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7056 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7057 | STV_HIDDEN);
7058 }
c66bb0ee 7059
8c5b4e52
AM
7060 if (htab->need_func_desc_adj)
7061 {
7062 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7063 htab->need_func_desc_adj = 0;
7064 }
805fc799 7065
b34976b6 7066 return TRUE;
e86ce104
AM
7067}
7068
98bbb1b8 7069/* Find dynamic relocs for H that apply to read-only sections. */
a345bc8d 7070
98bbb1b8 7071static asection *
a345bc8d
AM
7072readonly_dynrelocs (struct elf_link_hash_entry *h)
7073{
7074 struct ppc_link_hash_entry *eh;
7075 struct elf_dyn_relocs *p;
7076
7077 eh = (struct ppc_link_hash_entry *) h;
7078 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7079 {
7080 asection *s = p->sec->output_section;
7081
7082 if (s != NULL && (s->flags & SEC_READONLY) != 0)
98bbb1b8 7083 return p->sec;
a345bc8d 7084 }
98bbb1b8 7085 return NULL;
a345bc8d
AM
7086}
7087
d311bc8b 7088/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
7089 aliases, that apply to read-only sections. Cannot be used after
7090 size_dynamic_sections. */
d311bc8b
AM
7091
7092static bfd_boolean
7093alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7094{
7095 struct ppc_link_hash_entry *eh;
7096
7097 eh = (struct ppc_link_hash_entry *) h;
7098 do
7099 {
7100 if (readonly_dynrelocs (&eh->elf))
7101 return TRUE;
ab2477e1 7102 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
d311bc8b
AM
7103 } while (eh != NULL && &eh->elf != h);
7104
7105 return FALSE;
7106}
8a2058b5 7107
8a9e8e72
AM
7108/* Return whether EH has pc-relative dynamic relocs. */
7109
7110static bfd_boolean
7111pc_dynrelocs (struct ppc_link_hash_entry *eh)
7112{
7113 struct elf_dyn_relocs *p;
7114
7115 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7116 if (p->pc_count != 0)
7117 return TRUE;
7118 return FALSE;
7119}
7120
8a2058b5
AM
7121/* Return true if a global entry stub will be created for H. Valid
7122 for ELFv2 before plt entries have been allocated. */
7123
7124static bfd_boolean
7125global_entry_stub (struct elf_link_hash_entry *h)
7126{
7127 struct plt_entry *pent;
7128
7129 if (!h->pointer_equality_needed
7130 || h->def_regular)
7131 return FALSE;
7132
7133 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7134 if (pent->plt.refcount > 0
7135 && pent->addend == 0)
7136 return TRUE;
7137
7138 return FALSE;
7139}
7140
e86ce104
AM
7141/* Adjust a symbol defined by a dynamic object and referenced by a
7142 regular object. The current definition is in some section of the
7143 dynamic object, but we're not including those sections. We have to
7144 change the definition to something the rest of the link can
7145 understand. */
7146
b34976b6 7147static bfd_boolean
4ce794b7
AM
7148ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7149 struct elf_link_hash_entry *h)
e86ce104
AM
7150{
7151 struct ppc_link_hash_table *htab;
5474d94f 7152 asection *s, *srel;
e86ce104
AM
7153
7154 htab = ppc_hash_table (info);
4dfe6ac6
NC
7155 if (htab == NULL)
7156 return FALSE;
e86ce104
AM
7157
7158 /* Deal with function syms. */
7159 if (h->type == STT_FUNC
e054468f 7160 || h->type == STT_GNU_IFUNC
f5385ebf 7161 || h->needs_plt)
e86ce104 7162 {
529fe20e
AM
7163 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7164 || SYMBOL_CALLS_LOCAL (info, h)
7165 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7166 /* Discard dyn_relocs when non-pic if we've decided that a
7167 function symbol is local and not an ifunc. We keep dynamic
7168 relocs for ifuncs when local rather than always emitting a
7169 plt call stub for them and defining the symbol on the call
7170 stub. We can't do that for ELFv1 anyway (a function symbol
7171 is defined on a descriptor, not code) and it can be faster at
7172 run-time due to not needing to bounce through a stub. The
7173 dyn_relocs for ifuncs will be applied even in a static
7174 executable. */
7175 if (!bfd_link_pic (info)
7176 && h->type != STT_GNU_IFUNC
7177 && local)
7178 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7179
e86ce104
AM
7180 /* Clear procedure linkage table information for any symbol that
7181 won't need a .plt entry. */
411e1bfb
AM
7182 struct plt_entry *ent;
7183 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7184 if (ent->plt.refcount > 0)
7185 break;
8387904d 7186 if (ent == NULL
529fe20e 7187 || (h->type != STT_GNU_IFUNC && local))
40b8271b 7188 {
411e1bfb 7189 h->plt.plist = NULL;
f5385ebf 7190 h->needs_plt = 0;
d1eca1e4 7191 h->pointer_equality_needed = 0;
40b8271b 7192 }
8a2058b5 7193 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7194 {
d1eca1e4
AM
7195 /* Taking a function's address in a read/write section
7196 doesn't require us to define the function symbol in the
7197 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7198 be used instead. The reason we prefer a few more dynamic
7199 relocs is that calling via a global entry stub costs a
7200 few more instructions, and pointer_equality_needed causes
7201 extra work in ld.so when resolving these symbols. */
529fe20e 7202 if (global_entry_stub (h))
d1eca1e4 7203 {
ab2477e1 7204 if (!readonly_dynrelocs (h))
529fe20e
AM
7205 {
7206 h->pointer_equality_needed = 0;
7207 /* If we haven't seen a branch reloc then we don't need
7208 a plt entry. */
7209 if (!h->needs_plt)
7210 h->plt.plist = NULL;
7211 }
7212 else if (!bfd_link_pic (info))
7213 /* We are going to be defining the function symbol on the
7214 plt stub, so no dyn_relocs needed when non-pic. */
7215 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
d1eca1e4
AM
7216 }
7217
3988aed5
AM
7218 /* ELFv2 function symbols can't have copy relocs. */
7219 return TRUE;
7220 }
7221 else if (!h->needs_plt
ab2477e1 7222 && !readonly_dynrelocs (h))
3988aed5
AM
7223 {
7224 /* If we haven't seen a branch reloc then we don't need a
7225 plt entry. */
7226 h->plt.plist = NULL;
7227 h->pointer_equality_needed = 0;
a345bc8d
AM
7228 return TRUE;
7229 }
5bd4f169 7230 }
bbd7ec4a 7231 else
411e1bfb 7232 h->plt.plist = NULL;
5bd4f169
AM
7233
7234 /* If this is a weak symbol, and there is a real definition, the
7235 processor independent code will have arranged for us to see the
7236 real definition first, and we can just use the same value. */
60d67dc8 7237 if (h->is_weakalias)
5bd4f169 7238 {
60d67dc8
AM
7239 struct elf_link_hash_entry *def = weakdef (h);
7240 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7241 h->root.u.def.section = def->root.u.def.section;
7242 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
7243 if (def->root.u.def.section == htab->elf.sdynbss
7244 || def->root.u.def.section == htab->elf.sdynrelro)
7245 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
b34976b6 7246 return TRUE;
5bd4f169
AM
7247 }
7248
5bd4f169
AM
7249 /* If we are creating a shared library, we must presume that the
7250 only references to the symbol are via the global offset table.
7251 For such cases we need not do anything here; the relocations will
7252 be handled correctly by relocate_section. */
0e1862bb 7253 if (bfd_link_pic (info))
b34976b6 7254 return TRUE;
5bd4f169 7255
65f38f15
AM
7256 /* If there are no references to this symbol that do not use the
7257 GOT, we don't need to generate a copy reloc. */
f5385ebf 7258 if (!h->non_got_ref)
b34976b6 7259 return TRUE;
65f38f15 7260
b186458a 7261 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7262 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7263
d93d1c80
AM
7264 /* If -z nocopyreloc was given, don't generate them either. */
7265 || info->nocopyreloc
a127494f 7266
dce2246a 7267 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 7268 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7269 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7270
d93d1c80
AM
7271 /* Protected variables do not work with .dynbss. The copy in
7272 .dynbss won't be used by the shared library with the protected
7273 definition for the variable. Text relocations are preferable
7274 to an incorrect program. */
7275 || h->protected_def)
529fe20e 7276 return TRUE;
a127494f 7277
5d35169e 7278 if (h->plt.plist != NULL)
97b639ba
AM
7279 {
7280 /* We should never get here, but unfortunately there are versions
7281 of gcc out there that improperly (for this ABI) put initialized
7282 function pointers, vtable refs and suchlike in read-only
7283 sections. Allow them to proceed, but warn that this might
7284 break at runtime. */
25f53a85 7285 info->callbacks->einfo
c1c8c1ef 7286 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 7287 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7288 h->root.root.string);
7289 }
5d35169e
AM
7290
7291 /* This is a reference to a symbol defined by a dynamic object which
7292 is not a function. */
7293
5bd4f169
AM
7294 /* We must allocate the symbol in our .dynbss section, which will
7295 become part of the .bss section of the executable. There will be
7296 an entry for this symbol in the .dynsym section. The dynamic
7297 object will contain position independent code, so all references
7298 from the dynamic object to this symbol will go through the global
7299 offset table. The dynamic linker will use the .dynsym entry to
7300 determine the address it must put in the global offset table, so
7301 both the dynamic object and the regular object will refer to the
7302 same memory location for the variable. */
5474d94f
AM
7303 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7304 {
7305 s = htab->elf.sdynrelro;
7306 srel = htab->elf.sreldynrelro;
7307 }
7308 else
7309 {
7310 s = htab->elf.sdynbss;
7311 srel = htab->elf.srelbss;
7312 }
1d7e9d18 7313 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7314 {
4a7e5234
AM
7315 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7316 linker to copy the initial value out of the dynamic object
7317 and into the runtime process image. */
5474d94f 7318 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 7319 h->needs_copy = 1;
5bd4f169
AM
7320 }
7321
529fe20e
AM
7322 /* We no longer want dyn_relocs. */
7323 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6cabe1ea 7324 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7325}
7326
e86ce104
AM
7327/* If given a function descriptor symbol, hide both the function code
7328 sym and the descriptor. */
7329static void
4ce794b7
AM
7330ppc64_elf_hide_symbol (struct bfd_link_info *info,
7331 struct elf_link_hash_entry *h,
7332 bfd_boolean force_local)
e86ce104 7333{
34814b9f 7334 struct ppc_link_hash_entry *eh;
e86ce104
AM
7335 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7336
34814b9f
AM
7337 eh = (struct ppc_link_hash_entry *) h;
7338 if (eh->is_func_descriptor)
e86ce104 7339 {
34814b9f 7340 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7341
721956f4 7342 if (fh == NULL)
d1329ca3
AM
7343 {
7344 const char *p, *q;
b8ac2841 7345 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
7346 char save;
7347
7348 /* We aren't supposed to use alloca in BFD because on
7349 systems which do not have alloca the version in libiberty
7350 calls xmalloc, which might cause the program to crash
7351 when it runs out of memory. This function doesn't have a
7352 return status, so there's no way to gracefully return an
7353 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7354 accessed; It's either a string in an ELF string table,
7355 or allocated in an objalloc structure. */
d1329ca3 7356
34814b9f 7357 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7358 save = *p;
7359 *(char *) p = '.';
34814b9f 7360 fh = (struct ppc_link_hash_entry *)
b8ac2841 7361 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7362 *(char *) p = save;
7363
7364 /* Unfortunately, if it so happens that the string we were
7365 looking for was allocated immediately before this string,
7366 then we overwrote the string terminator. That's the only
7367 reason the lookup should fail. */
7368 if (fh == NULL)
7369 {
34814b9f
AM
7370 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7371 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7372 --q, --p;
34814b9f
AM
7373 if (q < eh->elf.root.root.string && *p == '.')
7374 fh = (struct ppc_link_hash_entry *)
b8ac2841 7375 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7376 }
7377 if (fh != NULL)
7378 {
34814b9f
AM
7379 eh->oh = fh;
7380 fh->oh = eh;
d1329ca3
AM
7381 }
7382 }
e86ce104 7383 if (fh != NULL)
34814b9f 7384 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7385 }
7386}
7387
411e1bfb 7388static bfd_boolean
8843416a
AM
7389get_sym_h (struct elf_link_hash_entry **hp,
7390 Elf_Internal_Sym **symp,
7391 asection **symsecp,
f961d9dd 7392 unsigned char **tls_maskp,
8843416a
AM
7393 Elf_Internal_Sym **locsymsp,
7394 unsigned long r_symndx,
7395 bfd *ibfd)
411e1bfb 7396{
0ffa91dd 7397 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7398
7399 if (r_symndx >= symtab_hdr->sh_info)
7400 {
7401 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7402 struct elf_link_hash_entry *h;
7403
7404 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7405 h = elf_follow_link (h);
411e1bfb
AM
7406
7407 if (hp != NULL)
7408 *hp = h;
7409
7410 if (symp != NULL)
7411 *symp = NULL;
7412
7413 if (symsecp != NULL)
7414 {
7415 asection *symsec = NULL;
7416 if (h->root.type == bfd_link_hash_defined
7417 || h->root.type == bfd_link_hash_defweak)
7418 symsec = h->root.u.def.section;
7419 *symsecp = symsec;
7420 }
7421
e7b938ca 7422 if (tls_maskp != NULL)
411e1bfb
AM
7423 {
7424 struct ppc_link_hash_entry *eh;
7425
7426 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7427 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7428 }
7429 }
7430 else
7431 {
7432 Elf_Internal_Sym *sym;
7433 Elf_Internal_Sym *locsyms = *locsymsp;
7434
7435 if (locsyms == NULL)
7436 {
7437 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7438 if (locsyms == NULL)
7439 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7440 symtab_hdr->sh_info,
7441 0, NULL, NULL, NULL);
7442 if (locsyms == NULL)
7443 return FALSE;
7444 *locsymsp = locsyms;
7445 }
7446 sym = locsyms + r_symndx;
7447
7448 if (hp != NULL)
7449 *hp = NULL;
7450
7451 if (symp != NULL)
7452 *symp = sym;
7453
7454 if (symsecp != NULL)
cb33740c 7455 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7456
e7b938ca 7457 if (tls_maskp != NULL)
411e1bfb
AM
7458 {
7459 struct got_entry **lgot_ents;
f961d9dd 7460 unsigned char *tls_mask;
411e1bfb 7461
e7b938ca 7462 tls_mask = NULL;
411e1bfb
AM
7463 lgot_ents = elf_local_got_ents (ibfd);
7464 if (lgot_ents != NULL)
7465 {
e054468f
AM
7466 struct plt_entry **local_plt = (struct plt_entry **)
7467 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7468 unsigned char *lgot_masks = (unsigned char *)
e054468f 7469 (local_plt + symtab_hdr->sh_info);
e7b938ca 7470 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7471 }
e7b938ca 7472 *tls_maskp = tls_mask;
411e1bfb
AM
7473 }
7474 }
7475 return TRUE;
7476}
7477
e7b938ca 7478/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7479 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7480 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7481
7482static int
f961d9dd 7483get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7484 unsigned long *toc_symndx,
7485 bfd_vma *toc_addend,
0d4792f7 7486 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7487 const Elf_Internal_Rela *rel,
7488 bfd *ibfd)
411e1bfb
AM
7489{
7490 unsigned long r_symndx;
0d4792f7 7491 int next_r;
411e1bfb
AM
7492 struct elf_link_hash_entry *h;
7493 Elf_Internal_Sym *sym;
7494 asection *sec;
7495 bfd_vma off;
7496
7497 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7498 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7499 return 0;
411e1bfb 7500
e7b938ca 7501 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7502 || sec == NULL
6bee8834 7503 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7504 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7505 return 1;
411e1bfb
AM
7506
7507 /* Look inside a TOC section too. */
7508 if (h != NULL)
7509 {
7510 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7511 off = h->root.u.def.value;
7512 }
7513 else
7514 off = sym->st_value;
7515 off += rel->r_addend;
7516 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7517 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7518 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7519 if (toc_symndx != NULL)
7520 *toc_symndx = r_symndx;
3a71aa26
AM
7521 if (toc_addend != NULL)
7522 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7523 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7524 return 0;
854b41e7 7525 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7526 && (next_r == -1 || next_r == -2))
7527 return 1 - next_r;
951fd09b 7528 return 1;
411e1bfb
AM
7529}
7530
3b421ab3
AM
7531/* Find (or create) an entry in the tocsave hash table. */
7532
7533static struct tocsave_entry *
7534tocsave_find (struct ppc_link_hash_table *htab,
7535 enum insert_option insert,
7536 Elf_Internal_Sym **local_syms,
7537 const Elf_Internal_Rela *irela,
7538 bfd *ibfd)
7539{
7540 unsigned long r_indx;
7541 struct elf_link_hash_entry *h;
7542 Elf_Internal_Sym *sym;
7543 struct tocsave_entry ent, *p;
7544 hashval_t hash;
7545 struct tocsave_entry **slot;
7546
7547 r_indx = ELF64_R_SYM (irela->r_info);
7548 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7549 return NULL;
7550 if (ent.sec == NULL || ent.sec->output_section == NULL)
7551 {
4eca0228 7552 _bfd_error_handler
871b3ab2 7553 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7554 return NULL;
7555 }
7556
7557 if (h != NULL)
7558 ent.offset = h->root.u.def.value;
7559 else
7560 ent.offset = sym->st_value;
7561 ent.offset += irela->r_addend;
7562
7563 hash = tocsave_htab_hash (&ent);
7564 slot = ((struct tocsave_entry **)
7565 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7566 if (slot == NULL)
7567 return NULL;
7568
7569 if (*slot == NULL)
7570 {
7571 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7572 if (p == NULL)
7573 return NULL;
7574 *p = ent;
7575 *slot = p;
7576 }
7577 return *slot;
7578}
7579
754021d0 7580/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7581 code for the old ABI, these will already have been done. */
754021d0
AM
7582
7583static bfd_boolean
7584adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7585{
7586 struct ppc_link_hash_entry *eh;
7587 asection *sym_sec;
74f0fb50 7588 struct _opd_sec_data *opd;
754021d0
AM
7589
7590 if (h->root.type == bfd_link_hash_indirect)
7591 return TRUE;
7592
754021d0
AM
7593 if (h->root.type != bfd_link_hash_defined
7594 && h->root.type != bfd_link_hash_defweak)
7595 return TRUE;
7596
7597 eh = (struct ppc_link_hash_entry *) h;
7598 if (eh->adjust_done)
7599 return TRUE;
7600
7601 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7602 opd = get_opd_info (sym_sec);
7603 if (opd != NULL && opd->adjust != NULL)
754021d0 7604 {
51aecdc5 7605 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7606 if (adjust == -1)
7607 {
7608 /* This entry has been deleted. */
b3fac117 7609 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7610 if (dsec == NULL)
7611 {
7612 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7613 if (discarded_section (dsec))
81688140 7614 {
b3fac117 7615 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7616 break;
7617 }
7618 }
4025353c 7619 eh->elf.root.u.def.value = 0;
81688140 7620 eh->elf.root.u.def.section = dsec;
4025353c
AM
7621 }
7622 else
7623 eh->elf.root.u.def.value += adjust;
754021d0
AM
7624 eh->adjust_done = 1;
7625 }
7626 return TRUE;
7627}
7628
8c1d1bb8 7629/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7630 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7631 have already been determined. */
7632
7633static bfd_boolean
7634dec_dynrel_count (bfd_vma r_info,
7635 asection *sec,
7636 struct bfd_link_info *info,
7637 Elf_Internal_Sym **local_syms,
7638 struct elf_link_hash_entry *h,
19e08130 7639 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7640{
7641 enum elf_ppc64_reloc_type r_type;
19e08130 7642 asection *sym_sec = NULL;
8c1d1bb8
AM
7643
7644 /* Can this reloc be dynamic? This switch, and later tests here
7645 should be kept in sync with the code in check_relocs. */
7646 r_type = ELF64_R_TYPE (r_info);
7647 switch (r_type)
7648 {
7649 default:
7650 return TRUE;
7651
7652 case R_PPC64_TPREL16:
7653 case R_PPC64_TPREL16_LO:
7654 case R_PPC64_TPREL16_HI:
7655 case R_PPC64_TPREL16_HA:
7656 case R_PPC64_TPREL16_DS:
7657 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7658 case R_PPC64_TPREL16_HIGH:
7659 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7660 case R_PPC64_TPREL16_HIGHER:
7661 case R_PPC64_TPREL16_HIGHERA:
7662 case R_PPC64_TPREL16_HIGHEST:
7663 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8
AM
7664 case R_PPC64_TPREL64:
7665 case R_PPC64_DTPMOD64:
7666 case R_PPC64_DTPREL64:
7667 case R_PPC64_ADDR64:
7668 case R_PPC64_REL30:
7669 case R_PPC64_REL32:
7670 case R_PPC64_REL64:
7671 case R_PPC64_ADDR14:
7672 case R_PPC64_ADDR14_BRNTAKEN:
7673 case R_PPC64_ADDR14_BRTAKEN:
7674 case R_PPC64_ADDR16:
7675 case R_PPC64_ADDR16_DS:
7676 case R_PPC64_ADDR16_HA:
7677 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7678 case R_PPC64_ADDR16_HIGH:
7679 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7680 case R_PPC64_ADDR16_HIGHER:
7681 case R_PPC64_ADDR16_HIGHERA:
7682 case R_PPC64_ADDR16_HIGHEST:
7683 case R_PPC64_ADDR16_HIGHESTA:
7684 case R_PPC64_ADDR16_LO:
7685 case R_PPC64_ADDR16_LO_DS:
7686 case R_PPC64_ADDR24:
7687 case R_PPC64_ADDR32:
7688 case R_PPC64_UADDR16:
7689 case R_PPC64_UADDR32:
7690 case R_PPC64_UADDR64:
7691 case R_PPC64_TOC:
7692 break;
7693 }
7694
7695 if (local_syms != NULL)
7696 {
7697 unsigned long r_symndx;
8c1d1bb8
AM
7698 bfd *ibfd = sec->owner;
7699
7700 r_symndx = ELF64_R_SYM (r_info);
7701 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7702 return FALSE;
7703 }
7704
0e1862bb 7705 if ((bfd_link_pic (info)
1d483afe 7706 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7707 || (h != NULL
198f1157 7708 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7709 || h->root.type == bfd_link_hash_defweak
7710 || !h->def_regular))))
7711 || (ELIMINATE_COPY_RELOCS
0e1862bb 7712 && !bfd_link_pic (info)
8c1d1bb8
AM
7713 && h != NULL
7714 && (h->root.type == bfd_link_hash_defweak
7715 || !h->def_regular)))
7716 ;
7717 else
7718 return TRUE;
7719
7720 if (h != NULL)
6edfbbad 7721 {
19e08130
AM
7722 struct elf_dyn_relocs *p;
7723 struct elf_dyn_relocs **pp;
7724 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7725
7726 /* elf_gc_sweep may have already removed all dyn relocs associated
7727 with local syms for a given section. Also, symbol flags are
7728 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7729 report a dynreloc miscount. */
7730 if (*pp == NULL && info->gc_sections)
7731 return TRUE;
7732
7733 while ((p = *pp) != NULL)
60124e18 7734 {
19e08130
AM
7735 if (p->sec == sec)
7736 {
7737 if (!must_be_dyn_reloc (info, r_type))
7738 p->pc_count -= 1;
7739 p->count -= 1;
7740 if (p->count == 0)
7741 *pp = p->next;
7742 return TRUE;
7743 }
7744 pp = &p->next;
60124e18 7745 }
6edfbbad 7746 }
19e08130
AM
7747 else
7748 {
7749 struct ppc_dyn_relocs *p;
7750 struct ppc_dyn_relocs **pp;
7751 void *vpp;
7752 bfd_boolean is_ifunc;
8c1d1bb8 7753
19e08130
AM
7754 if (local_syms == NULL)
7755 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7756 if (sym_sec == NULL)
7757 sym_sec = sec;
c57da1a7 7758
19e08130
AM
7759 vpp = &elf_section_data (sym_sec)->local_dynrel;
7760 pp = (struct ppc_dyn_relocs **) vpp;
7761
7762 if (*pp == NULL && info->gc_sections)
7763 return TRUE;
7764
7765 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7766 while ((p = *pp) != NULL)
8c1d1bb8 7767 {
19e08130
AM
7768 if (p->sec == sec && p->ifunc == is_ifunc)
7769 {
7770 p->count -= 1;
7771 if (p->count == 0)
7772 *pp = p->next;
7773 return TRUE;
7774 }
7775 pp = &p->next;
8c1d1bb8 7776 }
8c1d1bb8
AM
7777 }
7778
695344c0 7779 /* xgettext:c-format */
cf97bcb0
AM
7780 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7781 sec->owner, sec);
8c1d1bb8
AM
7782 bfd_set_error (bfd_error_bad_value);
7783 return FALSE;
7784}
7785
754021d0
AM
7786/* Remove unused Official Procedure Descriptor entries. Currently we
7787 only remove those associated with functions in discarded link-once
7788 sections, or weakly defined functions that have been overridden. It
7789 would be possible to remove many more entries for statically linked
7790 applications. */
7791
b34976b6 7792bfd_boolean
e7d1c40c 7793ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7794{
7795 bfd *ibfd;
754021d0 7796 bfd_boolean some_edited = FALSE;
3f764659 7797 asection *need_pad = NULL;
e7d1c40c
AM
7798 struct ppc_link_hash_table *htab;
7799
7800 htab = ppc_hash_table (info);
7801 if (htab == NULL)
7802 return FALSE;
1e2f5b6e 7803
c72f2fb2 7804 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7805 {
7806 asection *sec;
7807 Elf_Internal_Rela *relstart, *rel, *relend;
7808 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7809 Elf_Internal_Sym *local_syms;
74f0fb50 7810 struct _opd_sec_data *opd;
51aecdc5 7811 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7812 bfd_size_type cnt_16b = 0;
1e2f5b6e 7813
854b41e7
AM
7814 if (!is_ppc64_elf (ibfd))
7815 continue;
7816
1e2f5b6e 7817 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7818 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7819 continue;
7820
dbaa2011 7821 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7822 continue;
7823
1e2f5b6e
AM
7824 if (sec->output_section == bfd_abs_section_ptr)
7825 continue;
7826
7827 /* Look through the section relocs. */
7828 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7829 continue;
7830
6cdc0ccc 7831 local_syms = NULL;
0ffa91dd 7832 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7833
7834 /* Read the relocations. */
4ce794b7 7835 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7836 info->keep_memory);
1e2f5b6e 7837 if (relstart == NULL)
b34976b6 7838 return FALSE;
1e2f5b6e
AM
7839
7840 /* First run through the relocs to check they are sane, and to
7841 determine whether we need to edit this opd section. */
b34976b6 7842 need_edit = FALSE;
51aecdc5 7843 broken = FALSE;
3f764659 7844 need_pad = sec;
1e2f5b6e 7845 relend = relstart + sec->reloc_count;
50bc7936 7846 for (rel = relstart; rel < relend; )
1e2f5b6e 7847 {
04c9666a 7848 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7849 unsigned long r_symndx;
7850 asection *sym_sec;
7851 struct elf_link_hash_entry *h;
7852 Elf_Internal_Sym *sym;
51aecdc5 7853 bfd_vma offset;
1e2f5b6e 7854
51aecdc5 7855 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7856 only interested in the reloc pointing to a function entry
7857 point. */
51aecdc5
AM
7858 offset = rel->r_offset;
7859 if (rel + 1 == relend
7860 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7861 {
7862 /* If someone messes with .opd alignment then after a
7863 "ld -r" we might have padding in the middle of .opd.
7864 Also, there's nothing to prevent someone putting
7865 something silly in .opd with the assembler. No .opd
b34976b6 7866 optimization for them! */
3f764659 7867 broken_opd:
4eca0228 7868 _bfd_error_handler
871b3ab2 7869 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7870 broken = TRUE;
1e2f5b6e
AM
7871 break;
7872 }
7873
50bc7936
AM
7874 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7875 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7876 {
4eca0228 7877 _bfd_error_handler
695344c0 7878 /* xgettext:c-format */
871b3ab2 7879 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7880 ibfd, r_type);
51aecdc5 7881 broken = TRUE;
50bc7936
AM
7882 break;
7883 }
7884
1e2f5b6e 7885 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7886 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7887 r_symndx, ibfd))
50bc7936 7888 goto error_ret;
1e2f5b6e
AM
7889
7890 if (sym_sec == NULL || sym_sec->owner == NULL)
7891 {
411e1bfb
AM
7892 const char *sym_name;
7893 if (h != NULL)
7894 sym_name = h->root.root.string;
7895 else
26c61ae5
L
7896 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7897 sym_sec);
411e1bfb 7898
4eca0228 7899 _bfd_error_handler
695344c0 7900 /* xgettext:c-format */
871b3ab2 7901 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7902 ibfd, sym_name);
51aecdc5 7903 broken = TRUE;
1e2f5b6e
AM
7904 break;
7905 }
7906
51020317
AM
7907 /* opd entries are always for functions defined in the
7908 current input bfd. If the symbol isn't defined in the
7909 input bfd, then we won't be using the function in this
7910 bfd; It must be defined in a linkonce section in another
7911 bfd, or is weak. It's also possible that we are
7912 discarding the function due to a linker script /DISCARD/,
7913 which we test for via the output_section. */
7914 if (sym_sec->owner != ibfd
7915 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7916 need_edit = TRUE;
1e2f5b6e 7917
50bc7936 7918 rel += 2;
51aecdc5
AM
7919 if (rel + 1 == relend
7920 || (rel + 2 < relend
7921 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7922 ++rel;
7923
7924 if (rel == relend)
3f764659
JJ
7925 {
7926 if (sec->size == offset + 24)
7927 {
7928 need_pad = NULL;
7929 break;
7930 }
51aecdc5 7931 if (sec->size == offset + 16)
3f764659
JJ
7932 {
7933 cnt_16b++;
7934 break;
7935 }
7936 goto broken_opd;
7937 }
3f764659
JJ
7938 else if (rel + 1 < relend
7939 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7940 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7941 {
51aecdc5
AM
7942 if (rel[0].r_offset == offset + 16)
7943 cnt_16b++;
7944 else if (rel[0].r_offset != offset + 24)
7945 goto broken_opd;
3f764659
JJ
7946 }
7947 else
7948 goto broken_opd;
1e2f5b6e
AM
7949 }
7950
e7d1c40c 7951 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7952
51aecdc5 7953 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7954 {
7955 Elf_Internal_Rela *write_rel;
d4730f92 7956 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7957 bfd_byte *rptr, *wptr;
983bddc8 7958 bfd_byte *new_contents;
74f0fb50
AM
7959 bfd_size_type amt;
7960
983bddc8 7961 new_contents = NULL;
51aecdc5 7962 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7963 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7964 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7965 if (opd->adjust == NULL)
7966 return FALSE;
1e2f5b6e
AM
7967
7968 /* This seems a waste of time as input .opd sections are all
7969 zeros as generated by gcc, but I suppose there's no reason
7970 this will always be so. We might start putting something in
7971 the third word of .opd entries. */
7972 if ((sec->flags & SEC_IN_MEMORY) == 0)
7973 {
eea6121a
AM
7974 bfd_byte *loc;
7975 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7976 {
eea6121a
AM
7977 if (loc != NULL)
7978 free (loc);
50bc7936 7979 error_ret:
6cdc0ccc
AM
7980 if (local_syms != NULL
7981 && symtab_hdr->contents != (unsigned char *) local_syms)
7982 free (local_syms);
6cdc0ccc
AM
7983 if (elf_section_data (sec)->relocs != relstart)
7984 free (relstart);
b34976b6 7985 return FALSE;
6cdc0ccc 7986 }
1e2f5b6e
AM
7987 sec->contents = loc;
7988 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7989 }
7990
7991 elf_section_data (sec)->relocs = relstart;
7992
3f764659 7993 new_contents = sec->contents;
3f764659
JJ
7994 if (add_aux_fields)
7995 {
7996 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7997 if (new_contents == NULL)
7998 return FALSE;
51aecdc5 7999 need_pad = NULL;
3f764659 8000 }
b4f4e59f
AM
8001 wptr = new_contents;
8002 rptr = sec->contents;
1e2f5b6e 8003 write_rel = relstart;
51aecdc5 8004 for (rel = relstart; rel < relend; )
1e2f5b6e 8005 {
50bc7936
AM
8006 unsigned long r_symndx;
8007 asection *sym_sec;
8008 struct elf_link_hash_entry *h;
51aecdc5 8009 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 8010 Elf_Internal_Sym *sym;
51aecdc5
AM
8011 long opd_ent_size;
8012 Elf_Internal_Rela *next_rel;
8013 bfd_boolean skip;
50bc7936
AM
8014
8015 r_symndx = ELF64_R_SYM (rel->r_info);
8016 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 8017 r_symndx, ibfd))
50bc7936
AM
8018 goto error_ret;
8019
51aecdc5
AM
8020 next_rel = rel + 2;
8021 if (next_rel + 1 == relend
8022 || (next_rel + 2 < relend
8023 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8024 ++next_rel;
8025
8026 /* See if the .opd entry is full 24 byte or
8027 16 byte (with fd_aux entry overlapped with next
8028 fd_func). */
8029 opd_ent_size = 24;
8030 if (next_rel == relend)
1e2f5b6e 8031 {
51aecdc5 8032 if (sec->size == rel->r_offset + 16)
3f764659 8033 opd_ent_size = 16;
51aecdc5
AM
8034 }
8035 else if (next_rel->r_offset == rel->r_offset + 16)
8036 opd_ent_size = 16;
3f764659 8037
51aecdc5
AM
8038 if (h != NULL
8039 && h->root.root.string[0] == '.')
8040 {
8c5b4e52
AM
8041 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8042 if (fdh != NULL)
8043 {
8044 fdh = ppc_follow_link (fdh);
8045 if (fdh->elf.root.type != bfd_link_hash_defined
8046 && fdh->elf.root.type != bfd_link_hash_defweak)
8047 fdh = NULL;
8048 }
51aecdc5 8049 }
1e2f5b6e 8050
51aecdc5
AM
8051 skip = (sym_sec->owner != ibfd
8052 || sym_sec->output_section == bfd_abs_section_ptr);
8053 if (skip)
8054 {
8055 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8056 {
51aecdc5
AM
8057 /* Arrange for the function descriptor sym
8058 to be dropped. */
8059 fdh->elf.root.u.def.value = 0;
8060 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8061 }
51aecdc5 8062 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8063
0e1862bb 8064 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8065 rel = next_rel;
8066 else
8067 while (1)
8068 {
8069 if (!dec_dynrel_count (rel->r_info, sec, info,
8070 NULL, h, sym))
8071 goto error_ret;
754021d0 8072
51aecdc5
AM
8073 if (++rel == next_rel)
8074 break;
1e2f5b6e 8075
51aecdc5
AM
8076 r_symndx = ELF64_R_SYM (rel->r_info);
8077 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8078 r_symndx, ibfd))
8079 goto error_ret;
8080 }
50bc7936
AM
8081 }
8082 else
1e2f5b6e 8083 {
51aecdc5
AM
8084 /* We'll be keeping this opd entry. */
8085 long adjust;
8086
8087 if (fdh != NULL)
8088 {
8089 /* Redefine the function descriptor symbol to
8090 this location in the opd section. It is
8091 necessary to update the value here rather
8092 than using an array of adjustments as we do
8093 for local symbols, because various places
8094 in the generic ELF code use the value
8095 stored in u.def.value. */
8096 fdh->elf.root.u.def.value = wptr - new_contents;
8097 fdh->adjust_done = 1;
8098 }
8099
8100 /* Local syms are a bit tricky. We could
8101 tweak them as they can be cached, but
8102 we'd need to look through the local syms
8103 for the function descriptor sym which we
8104 don't have at the moment. So keep an
8105 array of adjustments. */
8106 adjust = (wptr - new_contents) - (rptr - sec->contents);
8107 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8108
8109 if (wptr != rptr)
8110 memcpy (wptr, rptr, opd_ent_size);
8111 wptr += opd_ent_size;
8112 if (add_aux_fields && opd_ent_size == 16)
8113 {
8114 memset (wptr, '\0', 8);
8115 wptr += 8;
8116 }
8117
50bc7936 8118 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8119 new opd entries. */
8120 for ( ; rel != next_rel; ++rel)
8121 {
8122 rel->r_offset += adjust;
8123 if (write_rel != rel)
8124 memcpy (write_rel, rel, sizeof (*rel));
8125 ++write_rel;
8126 }
1e2f5b6e 8127 }
51aecdc5
AM
8128
8129 rptr += opd_ent_size;
1e2f5b6e
AM
8130 }
8131
3f764659 8132 sec->size = wptr - new_contents;
1e2f5b6e 8133 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8134 if (add_aux_fields)
8135 {
8136 free (sec->contents);
8137 sec->contents = new_contents;
8138 }
8139
05bf9422 8140 /* Fudge the header size too, as this is used later in
cdcf6e38 8141 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8142 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8143 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8144 some_edited = TRUE;
1e2f5b6e 8145 }
6cdc0ccc 8146 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8147 free (relstart);
6cdc0ccc 8148
411e1bfb
AM
8149 if (local_syms != NULL
8150 && symtab_hdr->contents != (unsigned char *) local_syms)
8151 {
8152 if (!info->keep_memory)
8153 free (local_syms);
8154 else
8155 symtab_hdr->contents = (unsigned char *) local_syms;
8156 }
8157 }
8158
754021d0
AM
8159 if (some_edited)
8160 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8161
3f764659
JJ
8162 /* If we are doing a final link and the last .opd entry is just 16 byte
8163 long, add a 8 byte padding after it. */
0e1862bb 8164 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8165 {
8166 bfd_byte *p;
8167
8168 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8169 {
8170 BFD_ASSERT (need_pad->size > 0);
8171
8172 p = bfd_malloc (need_pad->size + 8);
8173 if (p == NULL)
8174 return FALSE;
699733f6 8175
3f764659
JJ
8176 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8177 p, 0, need_pad->size))
8178 return FALSE;
8179
8180 need_pad->contents = p;
8181 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8182 }
8183 else
8184 {
8185 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8186 if (p == NULL)
8187 return FALSE;
8188
8189 need_pad->contents = p;
8190 }
8191
8192 memset (need_pad->contents + need_pad->size, 0, 8);
8193 need_pad->size += 8;
8194 }
8195
411e1bfb
AM
8196 return TRUE;
8197}
8198
e1918d23 8199/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8200
e1918d23 8201asection *
e7d1c40c 8202ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8203{
411e1bfb
AM
8204 struct ppc_link_hash_table *htab;
8205
411e1bfb 8206 htab = ppc_hash_table (info);
4dfe6ac6
NC
8207 if (htab == NULL)
8208 return NULL;
8209
ee67d69a
AM
8210 if (abiversion (info->output_bfd) == 1)
8211 htab->opd_abi = 1;
8212
e7d1c40c 8213 if (htab->params->no_multi_toc)
33c0ec9d
AM
8214 htab->do_multi_toc = 0;
8215 else if (!htab->do_multi_toc)
e7d1c40c 8216 htab->params->no_multi_toc = 1;
33c0ec9d 8217
8b5f1ed8
AM
8218 /* Default to --no-plt-localentry, as this option can cause problems
8219 with symbol interposition. For example, glibc libpthread.so and
8220 libc.so duplicate many pthread symbols, with a fallback
8221 implementation in libc.so. In some cases the fallback does more
8222 work than the pthread implementation. __pthread_condattr_destroy
8223 is one such symbol: the libpthread.so implementation is
8224 localentry:0 while the libc.so implementation is localentry:8.
8225 An app that "cleverly" uses dlopen to only load necessary
8226 libraries at runtime may omit loading libpthread.so when not
8227 running multi-threaded, which then results in the libc.so
8228 fallback symbols being used and ld.so complaining. Now there
8229 are workarounds in ld (see non_zero_localentry) to detect the
8230 pthread situation, but that may not be the only case where
8231 --plt-localentry can cause trouble. */
f378ab09 8232 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 8233 htab->params->plt_localentry0 = 0;
d44c746a
AM
8234 if (htab->params->plt_localentry0
8235 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8236 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
8237 _bfd_error_handler
8238 (_("warning: --plt-localentry is especially dangerous without "
8239 "ld.so support to detect ABI violations"));
f378ab09 8240
3a71aa26
AM
8241 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8242 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8243 FALSE, FALSE, TRUE));
a7f2871e
AM
8244 /* Move dynamic linking info to the function descriptor sym. */
8245 if (htab->tls_get_addr != NULL)
8246 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8247 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8248 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8249 FALSE, FALSE, TRUE));
7c9cf415 8250 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8251 {
8252 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8253
8254 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8255 FALSE, FALSE, TRUE);
8256 if (opt != NULL)
8257 func_desc_adjust (opt, info);
8258 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8259 FALSE, FALSE, TRUE);
8260 if (opt_fd != NULL
8261 && (opt_fd->root.type == bfd_link_hash_defined
8262 || opt_fd->root.type == bfd_link_hash_defweak))
8263 {
8264 /* If glibc supports an optimized __tls_get_addr call stub,
8265 signalled by the presence of __tls_get_addr_opt, and we'll
8266 be calling __tls_get_addr via a plt call stub, then
8267 make __tls_get_addr point to __tls_get_addr_opt. */
8268 tga_fd = &htab->tls_get_addr_fd->elf;
8269 if (htab->elf.dynamic_sections_created
8270 && tga_fd != NULL
8271 && (tga_fd->type == STT_FUNC
8272 || tga_fd->needs_plt)
8273 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
21d68fcd 8274 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
a7f2871e
AM
8275 {
8276 struct plt_entry *ent;
8277
8278 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8279 if (ent->plt.refcount > 0)
8280 break;
8281 if (ent != NULL)
8282 {
8283 tga_fd->root.type = bfd_link_hash_indirect;
8284 tga_fd->root.u.i.link = &opt_fd->root;
8285 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 8286 opt_fd->mark = 1;
a7f2871e
AM
8287 if (opt_fd->dynindx != -1)
8288 {
8289 /* Use __tls_get_addr_opt in dynamic relocations. */
8290 opt_fd->dynindx = -1;
8291 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8292 opt_fd->dynstr_index);
8293 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8294 return NULL;
a7f2871e
AM
8295 }
8296 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8297 tga = &htab->tls_get_addr->elf;
8298 if (opt != NULL && tga != NULL)
8299 {
8300 tga->root.type = bfd_link_hash_indirect;
8301 tga->root.u.i.link = &opt->root;
8302 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 8303 opt->mark = 1;
a7f2871e
AM
8304 _bfd_elf_link_hash_hide_symbol (info, opt,
8305 tga->forced_local);
8306 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8307 }
8308 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8309 htab->tls_get_addr_fd->is_func_descriptor = 1;
8310 if (htab->tls_get_addr != NULL)
8311 {
8312 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8313 htab->tls_get_addr->is_func = 1;
8314 }
8315 }
8316 }
8317 }
7c9cf415
AM
8318 else if (htab->params->tls_get_addr_opt < 0)
8319 htab->params->tls_get_addr_opt = 0;
a7f2871e 8320 }
33c0ec9d 8321 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8322}
8387904d 8323
3a71aa26
AM
8324/* Return TRUE iff REL is a branch reloc with a global symbol matching
8325 HASH1 or HASH2. */
8387904d 8326
3a71aa26
AM
8327static bfd_boolean
8328branch_reloc_hash_match (const bfd *ibfd,
8329 const Elf_Internal_Rela *rel,
8330 const struct ppc_link_hash_entry *hash1,
8331 const struct ppc_link_hash_entry *hash2)
8332{
8333 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8334 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8335 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8336
e054468f 8337 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8338 {
3a71aa26
AM
8339 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8340 struct elf_link_hash_entry *h;
8387904d 8341
3a71aa26 8342 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8343 h = elf_follow_link (h);
3a71aa26
AM
8344 if (h == &hash1->elf || h == &hash2->elf)
8345 return TRUE;
a48ebf4d 8346 }
3a71aa26 8347 return FALSE;
951fd09b 8348}
411e1bfb 8349
951fd09b
AM
8350/* Run through all the TLS relocs looking for optimization
8351 opportunities. The linker has been hacked (see ppc64elf.em) to do
8352 a preliminary section layout so that we know the TLS segment
8353 offsets. We can't optimize earlier because some optimizations need
8354 to know the tp offset, and we need to optimize before allocating
8355 dynamic relocations. */
8356
8357bfd_boolean
33c0ec9d 8358ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8359{
8360 bfd *ibfd;
8361 asection *sec;
8362 struct ppc_link_hash_table *htab;
663a1470 8363 unsigned char *toc_ref;
102890f0 8364 int pass;
951fd09b 8365
3cbc1e5e 8366 if (!bfd_link_executable (info))
411e1bfb
AM
8367 return TRUE;
8368
951fd09b 8369 htab = ppc_hash_table (info);
4dfe6ac6
NC
8370 if (htab == NULL)
8371 return FALSE;
8372
663a1470
AM
8373 /* Make two passes over the relocs. On the first pass, mark toc
8374 entries involved with tls relocs, and check that tls relocs
8375 involved in setting up a tls_get_addr call are indeed followed by
8376 such a call. If they are not, we can't do any tls optimization.
8377 On the second pass twiddle tls_mask flags to notify
8378 relocate_section that optimization can be done, and adjust got
8379 and plt refcounts. */
8380 toc_ref = NULL;
8381 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8382 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8383 {
8384 Elf_Internal_Sym *locsyms = NULL;
8385 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8386
102890f0
AM
8387 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8388 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8389 {
8390 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8391 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8392
102890f0
AM
8393 /* Read the relocations. */
8394 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8395 info->keep_memory);
8396 if (relstart == NULL)
2915c55b
JK
8397 {
8398 free (toc_ref);
8399 return FALSE;
8400 }
411e1bfb 8401
102890f0
AM
8402 relend = relstart + sec->reloc_count;
8403 for (rel = relstart; rel < relend; rel++)
8404 {
8405 enum elf_ppc64_reloc_type r_type;
8406 unsigned long r_symndx;
8407 struct elf_link_hash_entry *h;
8408 Elf_Internal_Sym *sym;
8409 asection *sym_sec;
f961d9dd
AM
8410 unsigned char *tls_mask;
8411 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8412 bfd_vma value;
8413 bfd_boolean ok_tprel, is_local;
8414 long toc_ref_index = 0;
8415 int expecting_tls_get_addr = 0;
663a1470 8416 bfd_boolean ret = FALSE;
411e1bfb 8417
102890f0
AM
8418 r_symndx = ELF64_R_SYM (rel->r_info);
8419 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8420 r_symndx, ibfd))
8421 {
8422 err_free_rel:
8423 if (elf_section_data (sec)->relocs != relstart)
8424 free (relstart);
8425 if (toc_ref != NULL)
8426 free (toc_ref);
8427 if (locsyms != NULL
0ffa91dd 8428 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8429 != (unsigned char *) locsyms))
8430 free (locsyms);
663a1470 8431 return ret;
102890f0 8432 }
411e1bfb 8433
102890f0
AM
8434 if (h != NULL)
8435 {
766bc656
AM
8436 if (h->root.type == bfd_link_hash_defined
8437 || h->root.type == bfd_link_hash_defweak)
8438 value = h->root.u.def.value;
8439 else if (h->root.type == bfd_link_hash_undefweak)
8440 value = 0;
8441 else
663a1470
AM
8442 {
8443 found_tls_get_addr_arg = 0;
8444 continue;
8445 }
102890f0
AM
8446 }
8447 else
8448 /* Symbols referenced by TLS relocs must be of type
8449 STT_TLS. So no need for .opd local sym adjust. */
8450 value = sym->st_value;
8451
8452 ok_tprel = FALSE;
8453 is_local = FALSE;
8454 if (h == NULL
8455 || !h->def_dynamic)
8456 {
8457 is_local = TRUE;
766bc656
AM
8458 if (h != NULL
8459 && h->root.type == bfd_link_hash_undefweak)
8460 ok_tprel = TRUE;
c27b8c2a
AM
8461 else if (sym_sec != NULL
8462 && sym_sec->output_section != NULL)
766bc656
AM
8463 {
8464 value += sym_sec->output_offset;
8465 value += sym_sec->output_section->vma;
8466 value -= htab->elf.tls_sec->vma;
8467 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8468 < (bfd_vma) 1 << 32);
8469 }
102890f0 8470 }
951fd09b 8471
102890f0 8472 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8473 /* If this section has old-style __tls_get_addr calls
8474 without marker relocs, then check that each
8475 __tls_get_addr call reloc is preceded by a reloc
8476 that conceivably belongs to the __tls_get_addr arg
8477 setup insn. If we don't find matching arg setup
8478 relocs, don't do any tls optimization. */
8479 if (pass == 0
8480 && sec->has_tls_get_addr_call
8481 && h != NULL
8482 && (h == &htab->tls_get_addr->elf
8483 || h == &htab->tls_get_addr_fd->elf)
8484 && !found_tls_get_addr_arg
8485 && is_branch_reloc (r_type))
8486 {
25f53a85 8487 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8488 "TLS optimization disabled\n"),
8489 ibfd, sec, rel->r_offset);
8490 ret = TRUE;
8491 goto err_free_rel;
8492 }
8493
8494 found_tls_get_addr_arg = 0;
102890f0
AM
8495 switch (r_type)
8496 {
8497 case R_PPC64_GOT_TLSLD16:
8498 case R_PPC64_GOT_TLSLD16_LO:
8499 expecting_tls_get_addr = 1;
663a1470 8500 found_tls_get_addr_arg = 1;
1a0670f3 8501 /* Fall through. */
102890f0
AM
8502
8503 case R_PPC64_GOT_TLSLD16_HI:
8504 case R_PPC64_GOT_TLSLD16_HA:
8505 /* These relocs should never be against a symbol
8506 defined in a shared lib. Leave them alone if
8507 that turns out to be the case. */
8508 if (!is_local)
8509 continue;
411e1bfb 8510
102890f0 8511 /* LD -> LE */
411e1bfb 8512 tls_set = 0;
102890f0
AM
8513 tls_clear = TLS_LD;
8514 tls_type = TLS_TLS | TLS_LD;
8515 break;
411e1bfb 8516
102890f0
AM
8517 case R_PPC64_GOT_TLSGD16:
8518 case R_PPC64_GOT_TLSGD16_LO:
8519 expecting_tls_get_addr = 1;
663a1470 8520 found_tls_get_addr_arg = 1;
1a0670f3 8521 /* Fall through. */
102890f0
AM
8522
8523 case R_PPC64_GOT_TLSGD16_HI:
8524 case R_PPC64_GOT_TLSGD16_HA:
8525 if (ok_tprel)
8526 /* GD -> LE */
411e1bfb 8527 tls_set = 0;
102890f0
AM
8528 else
8529 /* GD -> IE */
8530 tls_set = TLS_TLS | TLS_TPRELGD;
8531 tls_clear = TLS_GD;
8532 tls_type = TLS_TLS | TLS_GD;
8533 break;
8534
8535 case R_PPC64_GOT_TPREL16_DS:
8536 case R_PPC64_GOT_TPREL16_LO_DS:
8537 case R_PPC64_GOT_TPREL16_HI:
8538 case R_PPC64_GOT_TPREL16_HA:
8539 if (ok_tprel)
8540 {
8541 /* IE -> LE */
8542 tls_set = 0;
8543 tls_clear = TLS_TPREL;
8544 tls_type = TLS_TLS | TLS_TPREL;
8545 break;
8546 }
411e1bfb
AM
8547 continue;
8548
727fc41e
AM
8549 case R_PPC64_TLSGD:
8550 case R_PPC64_TLSLD:
663a1470 8551 found_tls_get_addr_arg = 1;
1a0670f3 8552 /* Fall through. */
663a1470
AM
8553
8554 case R_PPC64_TLS:
8555 case R_PPC64_TOC16:
8556 case R_PPC64_TOC16_LO:
102890f0
AM
8557 if (sym_sec == NULL || sym_sec != toc)
8558 continue;
8559
8560 /* Mark this toc entry as referenced by a TLS
8561 code sequence. We can do that now in the
8562 case of R_PPC64_TLS, and after checking for
8563 tls_get_addr for the TOC16 relocs. */
8564 if (toc_ref == NULL)
663a1470
AM
8565 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8566 if (toc_ref == NULL)
8567 goto err_free_rel;
8568
102890f0
AM
8569 if (h != NULL)
8570 value = h->root.u.def.value;
8571 else
8572 value = sym->st_value;
8573 value += rel->r_addend;
73242275
AM
8574 if (value % 8 != 0)
8575 continue;
8576 BFD_ASSERT (value < toc->size
8577 && toc->output_offset % 8 == 0);
663a1470 8578 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8579 if (r_type == R_PPC64_TLS
8580 || r_type == R_PPC64_TLSGD
8581 || r_type == R_PPC64_TLSLD)
102890f0
AM
8582 {
8583 toc_ref[toc_ref_index] = 1;
8584 continue;
8585 }
8586
8587 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8588 continue;
8589
8590 tls_set = 0;
8591 tls_clear = 0;
8592 expecting_tls_get_addr = 2;
8593 break;
8594
8595 case R_PPC64_TPREL64:
8596 if (pass == 0
8597 || sec != toc
8598 || toc_ref == NULL
663a1470 8599 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8600 continue;
8601 if (ok_tprel)
8602 {
8603 /* IE -> LE */
8604 tls_set = TLS_EXPLICIT;
8605 tls_clear = TLS_TPREL;
8606 break;
8607 }
8608 continue;
8609
8610 case R_PPC64_DTPMOD64:
8611 if (pass == 0
8612 || sec != toc
8613 || toc_ref == NULL
663a1470 8614 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8615 continue;
8616 if (rel + 1 < relend
8617 && (rel[1].r_info
8618 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8619 && rel[1].r_offset == rel->r_offset + 8)
8620 {
8621 if (ok_tprel)
8622 /* GD -> LE */
8623 tls_set = TLS_EXPLICIT | TLS_GD;
8624 else
8625 /* GD -> IE */
8626 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8627 tls_clear = TLS_GD;
8628 }
8629 else
8630 {
8631 if (!is_local)
8632 continue;
8633
8634 /* LD -> LE */
8635 tls_set = TLS_EXPLICIT;
8636 tls_clear = TLS_LD;
8637 }
8638 break;
8639
8640 default:
8641 continue;
8642 }
8643
8644 if (pass == 0)
8645 {
727fc41e
AM
8646 if (!expecting_tls_get_addr
8647 || !sec->has_tls_get_addr_call)
102890f0
AM
8648 continue;
8649
3a71aa26
AM
8650 if (rel + 1 < relend
8651 && branch_reloc_hash_match (ibfd, rel + 1,
8652 htab->tls_get_addr,
8653 htab->tls_get_addr_fd))
102890f0 8654 {
3a71aa26 8655 if (expecting_tls_get_addr == 2)
102890f0 8656 {
3a71aa26 8657 /* Check for toc tls entries. */
f961d9dd 8658 unsigned char *toc_tls;
3a71aa26
AM
8659 int retval;
8660
8661 retval = get_tls_mask (&toc_tls, NULL, NULL,
8662 &locsyms,
8663 rel, ibfd);
8664 if (retval == 0)
8665 goto err_free_rel;
663a1470
AM
8666 if (toc_tls != NULL)
8667 {
8668 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8669 found_tls_get_addr_arg = 1;
8670 if (retval > 1)
8671 toc_ref[toc_ref_index] = 1;
8672 }
102890f0 8673 }
3a71aa26 8674 continue;
102890f0
AM
8675 }
8676
8677 if (expecting_tls_get_addr != 1)
8678 continue;
8679
8680 /* Uh oh, we didn't find the expected call. We
8681 could just mark this symbol to exclude it
8682 from tls optimization but it's safer to skip
663a1470 8683 the entire optimization. */
695344c0 8684 /* xgettext:c-format */
25f53a85 8685 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8686 "TLS optimization disabled\n"),
8687 ibfd, sec, rel->r_offset);
8688 ret = TRUE;
8689 goto err_free_rel;
102890f0
AM
8690 }
8691
85f7a9cb 8692 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8693 {
8694 struct plt_entry *ent;
8695 for (ent = htab->tls_get_addr->elf.plt.plist;
8696 ent != NULL;
8697 ent = ent->next)
8698 if (ent->addend == 0)
411e1bfb 8699 {
102890f0 8700 if (ent->plt.refcount > 0)
30038c59 8701 {
102890f0
AM
8702 ent->plt.refcount -= 1;
8703 expecting_tls_get_addr = 0;
30038c59 8704 }
102890f0 8705 break;
411e1bfb 8706 }
102890f0 8707 }
411e1bfb 8708
85f7a9cb 8709 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8710 {
8711 struct plt_entry *ent;
8712 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8713 ent != NULL;
8714 ent = ent->next)
8715 if (ent->addend == 0)
411e1bfb 8716 {
102890f0
AM
8717 if (ent->plt.refcount > 0)
8718 ent->plt.refcount -= 1;
8719 break;
411e1bfb 8720 }
102890f0 8721 }
411e1bfb 8722
102890f0 8723 if (tls_clear == 0)
30038c59
AM
8724 continue;
8725
102890f0
AM
8726 if ((tls_set & TLS_EXPLICIT) == 0)
8727 {
8728 struct got_entry *ent;
411e1bfb 8729
102890f0
AM
8730 /* Adjust got entry for this reloc. */
8731 if (h != NULL)
8732 ent = h->got.glist;
8733 else
8734 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8735
102890f0
AM
8736 for (; ent != NULL; ent = ent->next)
8737 if (ent->addend == rel->r_addend
8738 && ent->owner == ibfd
8739 && ent->tls_type == tls_type)
8740 break;
8741 if (ent == NULL)
8742 abort ();
411e1bfb 8743
102890f0
AM
8744 if (tls_set == 0)
8745 {
8746 /* We managed to get rid of a got entry. */
8747 if (ent->got.refcount > 0)
8748 ent->got.refcount -= 1;
8749 }
8750 }
8751 else
8752 {
8753 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8754 we'll lose one or two dyn relocs. */
8755 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8756 NULL, h, sym))
102890f0 8757 return FALSE;
411e1bfb 8758
102890f0
AM
8759 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8760 {
8761 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8762 NULL, h, sym))
102890f0
AM
8763 return FALSE;
8764 }
8765 }
411e1bfb 8766
102890f0
AM
8767 *tls_mask |= tls_set;
8768 *tls_mask &= ~tls_clear;
8769 }
8c1d1bb8 8770
102890f0
AM
8771 if (elf_section_data (sec)->relocs != relstart)
8772 free (relstart);
8773 }
411e1bfb 8774
663a1470
AM
8775 if (locsyms != NULL
8776 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8777 {
8778 if (!info->keep_memory)
8779 free (locsyms);
8780 else
8781 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8782 }
8783 }
411e1bfb 8784
663a1470
AM
8785 if (toc_ref != NULL)
8786 free (toc_ref);
9a23f96e 8787 htab->do_tls_opt = 1;
b34976b6 8788 return TRUE;
1e2f5b6e 8789}
b34976b6 8790
c5614fa4
AM
8791/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8792 the values of any global symbols in a toc section that has been
8793 edited. Globals in toc sections should be a rarity, so this function
8794 sets a flag if any are found in toc sections other than the one just
de194d85 8795 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8796
8797struct adjust_toc_info
8798{
8799 asection *toc;
8800 unsigned long *skip;
8801 bfd_boolean global_toc_syms;
8802};
8803
ba761f19
AM
8804enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8805
c5614fa4
AM
8806static bfd_boolean
8807adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8808{
8809 struct ppc_link_hash_entry *eh;
8810 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8811 unsigned long i;
c5614fa4 8812
c5614fa4
AM
8813 if (h->root.type != bfd_link_hash_defined
8814 && h->root.type != bfd_link_hash_defweak)
8815 return TRUE;
8816
8817 eh = (struct ppc_link_hash_entry *) h;
8818 if (eh->adjust_done)
8819 return TRUE;
8820
8821 if (eh->elf.root.u.def.section == toc_inf->toc)
8822 {
854b41e7
AM
8823 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8824 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8825 else
854b41e7
AM
8826 i = eh->elf.root.u.def.value >> 3;
8827
ba761f19 8828 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8829 {
4eca0228 8830 _bfd_error_handler
854b41e7
AM
8831 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8832 do
8833 ++i;
ba761f19 8834 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8835 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8836 }
854b41e7
AM
8837
8838 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8839 eh->adjust_done = 1;
8840 }
8841 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8842 toc_inf->global_toc_syms = TRUE;
8843
8844 return TRUE;
8845}
8846
39eeab25
AM
8847/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8848 on a _LO variety toc/got reloc. */
560c8763
AM
8849
8850static bfd_boolean
39eeab25 8851ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8852{
39eeab25
AM
8853 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8854 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8855 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8856 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8857 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8858 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8859 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8860 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8861 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8862 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8863 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8864 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8865 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8866 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8867 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8868 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8869 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8870 /* Exclude lfqu by testing reloc. If relocs are ever
8871 defined for the reduced D field in psq_lu then those
8872 will need testing too. */
8873 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8874 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8875 && (insn & 1) == 0)
8876 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8877 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8878 /* Exclude stfqu. psq_stu as above for psq_lu. */
8879 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8880 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8881 && (insn & 1) == 0));
560c8763
AM
8882}
8883
c5614fa4
AM
8884/* Examine all relocs referencing .toc sections in order to remove
8885 unused .toc entries. */
8886
8887bfd_boolean
33c0ec9d 8888ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8889{
8890 bfd *ibfd;
8891 struct adjust_toc_info toc_inf;
67f0cbdb 8892 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8893
67f0cbdb 8894 htab->do_toc_opt = 1;
c5614fa4 8895 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8896 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8897 {
8898 asection *toc, *sec;
8899 Elf_Internal_Shdr *symtab_hdr;
8900 Elf_Internal_Sym *local_syms;
425b145b 8901 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8902 unsigned long *skip, *drop;
8903 unsigned char *used;
8904 unsigned char *keep, last, some_unused;
8905
854b41e7
AM
8906 if (!is_ppc64_elf (ibfd))
8907 continue;
8908
c5614fa4
AM
8909 toc = bfd_get_section_by_name (ibfd, ".toc");
8910 if (toc == NULL
92b7a70f 8911 || toc->size == 0
dbaa2011
AM
8912 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8913 || discarded_section (toc))
c5614fa4
AM
8914 continue;
8915
425b145b 8916 toc_relocs = NULL;
c5614fa4 8917 local_syms = NULL;
0ffa91dd 8918 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8919
8920 /* Look at sections dropped from the final link. */
8921 skip = NULL;
8922 relstart = NULL;
8923 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8924 {
8925 if (sec->reloc_count == 0
dbaa2011 8926 || !discarded_section (sec)
c5614fa4
AM
8927 || get_opd_info (sec)
8928 || (sec->flags & SEC_ALLOC) == 0
8929 || (sec->flags & SEC_DEBUGGING) != 0)
8930 continue;
8931
8932 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8933 if (relstart == NULL)
8934 goto error_ret;
8935
8936 /* Run through the relocs to see which toc entries might be
8937 unused. */
8938 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8939 {
8940 enum elf_ppc64_reloc_type r_type;
8941 unsigned long r_symndx;
8942 asection *sym_sec;
8943 struct elf_link_hash_entry *h;
8944 Elf_Internal_Sym *sym;
8945 bfd_vma val;
8946
8947 r_type = ELF64_R_TYPE (rel->r_info);
8948 switch (r_type)
8949 {
8950 default:
8951 continue;
8952
8953 case R_PPC64_TOC16:
8954 case R_PPC64_TOC16_LO:
8955 case R_PPC64_TOC16_HI:
8956 case R_PPC64_TOC16_HA:
8957 case R_PPC64_TOC16_DS:
8958 case R_PPC64_TOC16_LO_DS:
8959 break;
8960 }
8961
8962 r_symndx = ELF64_R_SYM (rel->r_info);
8963 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8964 r_symndx, ibfd))
8965 goto error_ret;
8966
8967 if (sym_sec != toc)
8968 continue;
8969
8970 if (h != NULL)
8971 val = h->root.u.def.value;
8972 else
8973 val = sym->st_value;
8974 val += rel->r_addend;
8975
8976 if (val >= toc->size)
8977 continue;
8978
8979 /* Anything in the toc ought to be aligned to 8 bytes.
8980 If not, don't mark as unused. */
8981 if (val & 7)
8982 continue;
8983
8984 if (skip == NULL)
8985 {
854b41e7 8986 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8987 if (skip == NULL)
8988 goto error_ret;
8989 }
8990
ba761f19 8991 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8992 }
8993
8994 if (elf_section_data (sec)->relocs != relstart)
8995 free (relstart);
8996 }
8997
ba761f19
AM
8998 /* For largetoc loads of address constants, we can convert
8999 . addis rx,2,addr@got@ha
9000 . ld ry,addr@got@l(rx)
9001 to
9002 . addis rx,2,addr@toc@ha
9003 . addi ry,rx,addr@toc@l
9004 when addr is within 2G of the toc pointer. This then means
9005 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9006
ba761f19
AM
9007 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9008 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9009 && toc->reloc_count != 0)
9010 {
9011 /* Read toc relocs. */
425b145b
AM
9012 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9013 info->keep_memory);
9014 if (toc_relocs == NULL)
ba761f19
AM
9015 goto error_ret;
9016
425b145b 9017 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9018 {
9019 enum elf_ppc64_reloc_type r_type;
9020 unsigned long r_symndx;
9021 asection *sym_sec;
9022 struct elf_link_hash_entry *h;
9023 Elf_Internal_Sym *sym;
9024 bfd_vma val, addr;
9025
9026 r_type = ELF64_R_TYPE (rel->r_info);
9027 if (r_type != R_PPC64_ADDR64)
9028 continue;
9029
9030 r_symndx = ELF64_R_SYM (rel->r_info);
9031 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9032 r_symndx, ibfd))
9033 goto error_ret;
9034
425b145b 9035 if (sym_sec == NULL
c27b8c2a 9036 || sym_sec->output_section == NULL
dbaa2011 9037 || discarded_section (sym_sec))
425b145b
AM
9038 continue;
9039
afe397ea 9040 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9041 continue;
9042
9043 if (h != NULL)
bddc25c9
AM
9044 {
9045 if (h->type == STT_GNU_IFUNC)
9046 continue;
9047 val = h->root.u.def.value;
9048 }
ba761f19 9049 else
bddc25c9
AM
9050 {
9051 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9052 continue;
9053 val = sym->st_value;
9054 }
ba761f19
AM
9055 val += rel->r_addend;
9056 val += sym_sec->output_section->vma + sym_sec->output_offset;
9057
9058 /* We don't yet know the exact toc pointer value, but we
9059 know it will be somewhere in the toc section. Don't
9060 optimize if the difference from any possible toc
9061 pointer is outside [ff..f80008000, 7fff7fff]. */
9062 addr = toc->output_section->vma + TOC_BASE_OFF;
9063 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9064 continue;
9065
9066 addr = toc->output_section->vma + toc->output_section->rawsize;
9067 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9068 continue;
9069
9070 if (skip == NULL)
9071 {
9072 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9073 if (skip == NULL)
9074 goto error_ret;
9075 }
9076
9077 skip[rel->r_offset >> 3]
425b145b 9078 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9079 }
ba761f19
AM
9080 }
9081
c5614fa4
AM
9082 if (skip == NULL)
9083 continue;
9084
9085 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9086 if (used == NULL)
9087 {
9088 error_ret:
9089 if (local_syms != NULL
9090 && symtab_hdr->contents != (unsigned char *) local_syms)
9091 free (local_syms);
9092 if (sec != NULL
9093 && relstart != NULL
9094 && elf_section_data (sec)->relocs != relstart)
9095 free (relstart);
425b145b
AM
9096 if (toc_relocs != NULL
9097 && elf_section_data (toc)->relocs != toc_relocs)
9098 free (toc_relocs);
c5614fa4
AM
9099 if (skip != NULL)
9100 free (skip);
9101 return FALSE;
9102 }
9103
30038c59
AM
9104 /* Now check all kept sections that might reference the toc.
9105 Check the toc itself last. */
9106 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9107 : ibfd->sections);
c5614fa4 9108 sec != NULL;
c5614fa4 9109 sec = (sec == toc ? NULL
c5614fa4 9110 : sec->next == NULL ? toc
30038c59 9111 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9112 : sec->next))
9113 {
9114 int repeat;
9115
9116 if (sec->reloc_count == 0
dbaa2011 9117 || discarded_section (sec)
c5614fa4
AM
9118 || get_opd_info (sec)
9119 || (sec->flags & SEC_ALLOC) == 0
9120 || (sec->flags & SEC_DEBUGGING) != 0)
9121 continue;
9122
854b41e7
AM
9123 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9124 info->keep_memory);
c5614fa4 9125 if (relstart == NULL)
2915c55b
JK
9126 {
9127 free (used);
9128 goto error_ret;
9129 }
c5614fa4
AM
9130
9131 /* Mark toc entries referenced as used. */
c5614fa4 9132 do
d4f1ee75
AM
9133 {
9134 repeat = 0;
9135 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9136 {
9137 enum elf_ppc64_reloc_type r_type;
9138 unsigned long r_symndx;
9139 asection *sym_sec;
9140 struct elf_link_hash_entry *h;
9141 Elf_Internal_Sym *sym;
9142 bfd_vma val;
9143 enum {no_check, check_lo, check_ha} insn_check;
98528052 9144
d4f1ee75
AM
9145 r_type = ELF64_R_TYPE (rel->r_info);
9146 switch (r_type)
9147 {
9148 default:
9149 insn_check = no_check;
9150 break;
98528052 9151
d4f1ee75
AM
9152 case R_PPC64_GOT_TLSLD16_HA:
9153 case R_PPC64_GOT_TLSGD16_HA:
9154 case R_PPC64_GOT_TPREL16_HA:
9155 case R_PPC64_GOT_DTPREL16_HA:
9156 case R_PPC64_GOT16_HA:
9157 case R_PPC64_TOC16_HA:
9158 insn_check = check_ha;
9159 break;
98528052 9160
d4f1ee75
AM
9161 case R_PPC64_GOT_TLSLD16_LO:
9162 case R_PPC64_GOT_TLSGD16_LO:
9163 case R_PPC64_GOT_TPREL16_LO_DS:
9164 case R_PPC64_GOT_DTPREL16_LO_DS:
9165 case R_PPC64_GOT16_LO:
9166 case R_PPC64_GOT16_LO_DS:
9167 case R_PPC64_TOC16_LO:
9168 case R_PPC64_TOC16_LO_DS:
9169 insn_check = check_lo;
9170 break;
9171 }
560c8763 9172
d4f1ee75
AM
9173 if (insn_check != no_check)
9174 {
9175 bfd_vma off = rel->r_offset & ~3;
9176 unsigned char buf[4];
9177 unsigned int insn;
c5614fa4 9178
d4f1ee75
AM
9179 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9180 {
9181 free (used);
9182 goto error_ret;
9183 }
9184 insn = bfd_get_32 (ibfd, buf);
9185 if (insn_check == check_lo
39eeab25 9186 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
9187 : ((insn & ((0x3f << 26) | 0x1f << 16))
9188 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9189 {
9190 char str[12];
9191
9192 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9193 sprintf (str, "%#08x", insn);
9194 info->callbacks->einfo
695344c0 9195 /* xgettext:c-format */
174d0a74 9196 (_("%H: toc optimization is not supported for"
cf97bcb0 9197 " %s instruction\n"),
d4f1ee75
AM
9198 ibfd, sec, rel->r_offset & ~3, str);
9199 }
9200 }
c5614fa4 9201
d4f1ee75
AM
9202 switch (r_type)
9203 {
9204 case R_PPC64_TOC16:
9205 case R_PPC64_TOC16_LO:
9206 case R_PPC64_TOC16_HI:
9207 case R_PPC64_TOC16_HA:
9208 case R_PPC64_TOC16_DS:
9209 case R_PPC64_TOC16_LO_DS:
9210 /* In case we're taking addresses of toc entries. */
9211 case R_PPC64_ADDR64:
9212 break;
c5614fa4 9213
d4f1ee75
AM
9214 default:
9215 continue;
9216 }
c5614fa4 9217
d4f1ee75
AM
9218 r_symndx = ELF64_R_SYM (rel->r_info);
9219 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9220 r_symndx, ibfd))
9221 {
9222 free (used);
9223 goto error_ret;
9224 }
c5614fa4 9225
d4f1ee75
AM
9226 if (sym_sec != toc)
9227 continue;
c5614fa4 9228
d4f1ee75
AM
9229 if (h != NULL)
9230 val = h->root.u.def.value;
9231 else
9232 val = sym->st_value;
9233 val += rel->r_addend;
ba761f19 9234
d4f1ee75
AM
9235 if (val >= toc->size)
9236 continue;
ba761f19 9237
d4f1ee75
AM
9238 if ((skip[val >> 3] & can_optimize) != 0)
9239 {
9240 bfd_vma off;
9241 unsigned char opc;
9242
9243 switch (r_type)
9244 {
9245 case R_PPC64_TOC16_HA:
ba761f19 9246 break;
ba761f19 9247
d4f1ee75
AM
9248 case R_PPC64_TOC16_LO_DS:
9249 off = rel->r_offset;
9250 off += (bfd_big_endian (ibfd) ? -2 : 3);
9251 if (!bfd_get_section_contents (ibfd, sec, &opc,
9252 off, 1))
9253 {
9254 free (used);
9255 goto error_ret;
9256 }
9257 if ((opc & (0x3f << 2)) == (58u << 2))
9258 break;
1a0670f3 9259 /* Fall through. */
ba761f19 9260
d4f1ee75
AM
9261 default:
9262 /* Wrong sort of reloc, or not a ld. We may
9263 as well clear ref_from_discarded too. */
9264 skip[val >> 3] = 0;
9265 }
9266 }
9267
9268 if (sec != toc)
9269 used[val >> 3] = 1;
9270 /* For the toc section, we only mark as used if this
9271 entry itself isn't unused. */
9272 else if ((used[rel->r_offset >> 3]
9273 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9274 && !used[val >> 3])
9275 {
9276 /* Do all the relocs again, to catch reference
9277 chains. */
9278 repeat = 1;
9279 used[val >> 3] = 1;
9280 }
9281 }
9282 }
c5614fa4 9283 while (repeat);
854b41e7
AM
9284
9285 if (elf_section_data (sec)->relocs != relstart)
9286 free (relstart);
c5614fa4
AM
9287 }
9288
9289 /* Merge the used and skip arrays. Assume that TOC
9290 doublewords not appearing as either used or unused belong
de194d85 9291 to an entry more than one doubleword in size. */
c5614fa4
AM
9292 for (drop = skip, keep = used, last = 0, some_unused = 0;
9293 drop < skip + (toc->size + 7) / 8;
9294 ++drop, ++keep)
9295 {
9296 if (*keep)
9297 {
ba761f19
AM
9298 *drop &= ~ref_from_discarded;
9299 if ((*drop & can_optimize) != 0)
9300 some_unused = 1;
c5614fa4
AM
9301 last = 0;
9302 }
b140b010 9303 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9304 {
9305 some_unused = 1;
ba761f19 9306 last = ref_from_discarded;
c5614fa4
AM
9307 }
9308 else
9309 *drop = last;
9310 }
9311
9312 free (used);
9313
9314 if (some_unused)
9315 {
9316 bfd_byte *contents, *src;
9317 unsigned long off;
d62b3684 9318 Elf_Internal_Sym *sym;
ba761f19 9319 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9320
9321 /* Shuffle the toc contents, and at the same time convert the
9322 skip array from booleans into offsets. */
9323 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9324 goto error_ret;
9325
9326 elf_section_data (toc)->this_hdr.contents = contents;
9327
9328 for (src = contents, off = 0, drop = skip;
9329 src < contents + toc->size;
9330 src += 8, ++drop)
9331 {
ba761f19
AM
9332 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9333 off += 8;
c5614fa4
AM
9334 else if (off != 0)
9335 {
9336 *drop = off;
9337 memcpy (src - off, src, 8);
9338 }
9339 }
854b41e7 9340 *drop = off;
c5614fa4
AM
9341 toc->rawsize = toc->size;
9342 toc->size = src - contents - off;
9343
ba761f19
AM
9344 /* Adjust addends for relocs against the toc section sym,
9345 and optimize any accesses we can. */
c5614fa4
AM
9346 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9347 {
9348 if (sec->reloc_count == 0
dbaa2011 9349 || discarded_section (sec))
c5614fa4
AM
9350 continue;
9351
9352 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9353 info->keep_memory);
c5614fa4
AM
9354 if (relstart == NULL)
9355 goto error_ret;
9356
9357 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9358 {
9359 enum elf_ppc64_reloc_type r_type;
9360 unsigned long r_symndx;
9361 asection *sym_sec;
9362 struct elf_link_hash_entry *h;
854b41e7 9363 bfd_vma val;
c5614fa4
AM
9364
9365 r_type = ELF64_R_TYPE (rel->r_info);
9366 switch (r_type)
9367 {
9368 default:
9369 continue;
9370
9371 case R_PPC64_TOC16:
9372 case R_PPC64_TOC16_LO:
9373 case R_PPC64_TOC16_HI:
9374 case R_PPC64_TOC16_HA:
9375 case R_PPC64_TOC16_DS:
9376 case R_PPC64_TOC16_LO_DS:
9377 case R_PPC64_ADDR64:
9378 break;
9379 }
9380
9381 r_symndx = ELF64_R_SYM (rel->r_info);
9382 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9383 r_symndx, ibfd))
9384 goto error_ret;
9385
ba761f19 9386 if (sym_sec != toc)
c5614fa4
AM
9387 continue;
9388
ba761f19
AM
9389 if (h != NULL)
9390 val = h->root.u.def.value;
9391 else
9392 {
9393 val = sym->st_value;
9394 if (val != 0)
9395 local_toc_syms = TRUE;
9396 }
9397
9398 val += rel->r_addend;
854b41e7
AM
9399
9400 if (val > toc->rawsize)
9401 val = toc->rawsize;
ba761f19
AM
9402 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9403 continue;
9404 else if ((skip[val >> 3] & can_optimize) != 0)
9405 {
9406 Elf_Internal_Rela *tocrel
425b145b 9407 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9408 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9409
9410 switch (r_type)
9411 {
9412 case R_PPC64_TOC16_HA:
9413 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9414 break;
9415
9416 case R_PPC64_TOC16_LO_DS:
9417 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9418 break;
9419
9420 default:
28942f62
AM
9421 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9422 ppc_howto_init ();
b140b010 9423 info->callbacks->einfo
695344c0 9424 /* xgettext:c-format */
174d0a74 9425 (_("%H: %s references "
b140b010
AM
9426 "optimized away TOC entry\n"),
9427 ibfd, sec, rel->r_offset,
9428 ppc64_elf_howto_table[r_type]->name);
9429 bfd_set_error (bfd_error_bad_value);
9430 goto error_ret;
ba761f19
AM
9431 }
9432 rel->r_addend = tocrel->r_addend;
9433 elf_section_data (sec)->relocs = relstart;
9434 continue;
9435 }
9436
9437 if (h != NULL || sym->st_value != 0)
9438 continue;
854b41e7
AM
9439
9440 rel->r_addend -= skip[val >> 3];
9441 elf_section_data (sec)->relocs = relstart;
c5614fa4 9442 }
854b41e7
AM
9443
9444 if (elf_section_data (sec)->relocs != relstart)
9445 free (relstart);
c5614fa4
AM
9446 }
9447
9448 /* We shouldn't have local or global symbols defined in the TOC,
9449 but handle them anyway. */
df22d223
AM
9450 if (local_syms != NULL)
9451 for (sym = local_syms;
9452 sym < local_syms + symtab_hdr->sh_info;
9453 ++sym)
9454 if (sym->st_value != 0
9455 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9456 {
9457 unsigned long i;
854b41e7 9458
df22d223
AM
9459 if (sym->st_value > toc->rawsize)
9460 i = toc->rawsize >> 3;
9461 else
9462 i = sym->st_value >> 3;
854b41e7 9463
df22d223
AM
9464 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9465 {
9466 if (local_toc_syms)
4eca0228 9467 _bfd_error_handler
df22d223
AM
9468 (_("%s defined on removed toc entry"),
9469 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9470 do
9471 ++i;
9472 while ((skip[i] & (ref_from_discarded | can_optimize)));
9473 sym->st_value = (bfd_vma) i << 3;
9474 }
d62b3684 9475
df22d223
AM
9476 sym->st_value -= skip[i];
9477 symtab_hdr->contents = (unsigned char *) local_syms;
9478 }
c5614fa4 9479
854b41e7 9480 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9481 if (toc_inf.global_toc_syms)
9482 {
9483 toc_inf.toc = toc;
9484 toc_inf.skip = skip;
9485 toc_inf.global_toc_syms = FALSE;
9486 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9487 &toc_inf);
9488 }
854b41e7
AM
9489
9490 if (toc->reloc_count != 0)
9491 {
d4730f92 9492 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9493 Elf_Internal_Rela *wrel;
9494 bfd_size_type sz;
9495
854b41e7 9496 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9497 if (toc_relocs == NULL)
9498 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9499 info->keep_memory);
9500 if (toc_relocs == NULL)
9501 goto error_ret;
9502
425b145b
AM
9503 wrel = toc_relocs;
9504 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9505 if ((skip[rel->r_offset >> 3]
9506 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9507 {
9508 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9509 wrel->r_info = rel->r_info;
9510 wrel->r_addend = rel->r_addend;
9511 ++wrel;
9512 }
9513 else if (!dec_dynrel_count (rel->r_info, toc, info,
9514 &local_syms, NULL, NULL))
9515 goto error_ret;
9516
425b145b
AM
9517 elf_section_data (toc)->relocs = toc_relocs;
9518 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9519 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9520 sz = rel_hdr->sh_entsize;
9521 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9522 }
c5614fa4 9523 }
28be611c
AM
9524 else if (toc_relocs != NULL
9525 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9526 free (toc_relocs);
c5614fa4
AM
9527
9528 if (local_syms != NULL
9529 && symtab_hdr->contents != (unsigned char *) local_syms)
9530 {
9531 if (!info->keep_memory)
9532 free (local_syms);
9533 else
9534 symtab_hdr->contents = (unsigned char *) local_syms;
9535 }
9536 free (skip);
9537 }
9538
9539 return TRUE;
9540}
9541
1bbe0902
AM
9542/* Return true iff input section I references the TOC using
9543 instructions limited to +/-32k offsets. */
9544
9545bfd_boolean
9546ppc64_elf_has_small_toc_reloc (asection *i)
9547{
9548 return (is_ppc64_elf (i->owner)
9549 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9550}
9551
927be08e
AM
9552/* Allocate space for one GOT entry. */
9553
9554static void
9555allocate_got (struct elf_link_hash_entry *h,
9556 struct bfd_link_info *info,
9557 struct got_entry *gent)
9558{
9559 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9560 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9561 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9562 ? 16 : 8);
9563 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9564 ? 2 : 1) * sizeof (Elf64_External_Rela);
9565 asection *got = ppc64_elf_tdata (gent->owner)->got;
9566
9567 gent->got.offset = got->size;
9568 got->size += entsize;
9569
19e08130 9570 if (h->type == STT_GNU_IFUNC)
927be08e 9571 {
33e44f2e 9572 htab->elf.irelplt->size += rentsize;
19e08130 9573 htab->got_reli_size += rentsize;
927be08e 9574 }
f15d0b54
AM
9575 else if (((bfd_link_pic (info)
9576 && !((gent->tls_type & TLS_TPREL) != 0
9577 && bfd_link_executable (info)
9578 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9579 || (htab->elf.dynamic_sections_created
9580 && h->dynindx != -1
9581 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9582 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9583 {
19e08130 9584 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9585 relgot->size += rentsize;
927be08e
AM
9586 }
9587}
9588
7865406b
AM
9589/* This function merges got entries in the same toc group. */
9590
9591static void
9592merge_got_entries (struct got_entry **pent)
9593{
9594 struct got_entry *ent, *ent2;
9595
9596 for (ent = *pent; ent != NULL; ent = ent->next)
9597 if (!ent->is_indirect)
9598 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9599 if (!ent2->is_indirect
9600 && ent2->addend == ent->addend
9601 && ent2->tls_type == ent->tls_type
9602 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9603 {
9604 ent2->is_indirect = TRUE;
9605 ent2->got.ent = ent;
9606 }
9607}
9608
46434633 9609/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9610
9611static bfd_boolean
46434633
AM
9612ensure_undef_dynamic (struct bfd_link_info *info,
9613 struct elf_link_hash_entry *h)
f0158f44
AM
9614{
9615 struct elf_link_hash_table *htab = elf_hash_table (info);
9616
9617 if (htab->dynamic_sections_created
46434633
AM
9618 && ((info->dynamic_undefined_weak != 0
9619 && h->root.type == bfd_link_hash_undefweak)
9620 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9621 && h->dynindx == -1
9622 && !h->forced_local
9623 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9624 return bfd_elf_link_record_dynamic_symbol (info, h);
9625 return TRUE;
9626}
9627
65f38f15
AM
9628/* Allocate space in .plt, .got and associated reloc sections for
9629 dynamic relocs. */
5bd4f169 9630
b34976b6 9631static bfd_boolean
4ce794b7 9632allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9633{
65f38f15
AM
9634 struct bfd_link_info *info;
9635 struct ppc_link_hash_table *htab;
5bd4f169 9636 asection *s;
65f38f15 9637 struct ppc_link_hash_entry *eh;
0b8bcf0d 9638 struct got_entry **pgent, *gent;
5bd4f169 9639
e92d460e 9640 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9641 return TRUE;
5bd4f169 9642
65f38f15
AM
9643 info = (struct bfd_link_info *) inf;
9644 htab = ppc_hash_table (info);
4dfe6ac6
NC
9645 if (htab == NULL)
9646 return FALSE;
5bd4f169 9647
951fd09b
AM
9648 eh = (struct ppc_link_hash_entry *) h;
9649 /* Run through the TLS GD got entries first if we're changing them
9650 to TPREL. */
e7b938ca 9651 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9652 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9653 if (gent->got.refcount > 0
9654 && (gent->tls_type & TLS_GD) != 0)
9655 {
9656 /* This was a GD entry that has been converted to TPREL. If
9657 there happens to be a TPREL entry we can use that one. */
9658 struct got_entry *ent;
9659 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9660 if (ent->got.refcount > 0
9661 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9662 && ent->addend == gent->addend
9663 && ent->owner == gent->owner)
951fd09b
AM
9664 {
9665 gent->got.refcount = 0;
9666 break;
9667 }
9668
9669 /* If not, then we'll be using our own TPREL entry. */
9670 if (gent->got.refcount != 0)
9671 gent->tls_type = TLS_TLS | TLS_TPREL;
9672 }
9673
7865406b
AM
9674 /* Remove any list entry that won't generate a word in the GOT before
9675 we call merge_got_entries. Otherwise we risk merging to empty
9676 entries. */
0b8bcf0d
AM
9677 pgent = &h->got.glist;
9678 while ((gent = *pgent) != NULL)
411e1bfb 9679 if (gent->got.refcount > 0)
7865406b
AM
9680 {
9681 if ((gent->tls_type & TLS_LD) != 0
9682 && !h->def_dynamic)
9683 {
9684 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9685 *pgent = gent->next;
9686 }
9687 else
9688 pgent = &gent->next;
9689 }
9690 else
9691 *pgent = gent->next;
9692
9693 if (!htab->do_multi_toc)
9694 merge_got_entries (&h->got.glist);
9695
9696 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9697 if (!gent->is_indirect)
411e1bfb 9698 {
46434633
AM
9699 /* Make sure this symbol is output as a dynamic symbol. */
9700 if (!ensure_undef_dynamic (info, h))
f0158f44 9701 return FALSE;
65f38f15 9702
0c8d6e5c 9703 if (!is_ppc64_elf (gent->owner))
927be08e 9704 abort ();
0ffa91dd 9705
927be08e 9706 allocate_got (h, info, gent);
411e1bfb 9707 }
65f38f15 9708
954b63d4
AM
9709 /* If no dynamic sections we can't have dynamic relocs, except for
9710 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9711 if (!htab->elf.dynamic_sections_created
9712 && h->type != STT_GNU_IFUNC)
9713 eh->dyn_relocs = NULL;
9714
529fe20e
AM
9715 /* Discard relocs on undefined symbols that must be local. */
9716 else if (h->root.type == bfd_link_hash_undefined
9717 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9718 eh->dyn_relocs = NULL;
9719
954b63d4
AM
9720 /* Also discard relocs on undefined weak syms with non-default
9721 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9722 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
954b63d4
AM
9723 eh->dyn_relocs = NULL;
9724
8a2058b5 9725 if (eh->dyn_relocs != NULL)
65f38f15 9726 {
8a2058b5
AM
9727 struct elf_dyn_relocs *p, **pp;
9728
57e7d118
AM
9729 /* In the shared -Bsymbolic case, discard space allocated for
9730 dynamic pc-relative relocs against symbols which turn out to
9731 be defined in regular objects. For the normal shared case,
9732 discard space for relocs that have become local due to symbol
9733 visibility changes. */
9734
9735 if (bfd_link_pic (info))
65f38f15 9736 {
57e7d118
AM
9737 /* Relocs that use pc_count are those that appear on a call
9738 insn, or certain REL relocs (see must_be_dyn_reloc) that
9739 can be generated via assembly. We want calls to
9740 protected symbols to resolve directly to the function
9741 rather than going via the plt. If people want function
9742 pointer comparisons to work as expected then they should
9743 avoid writing weird assembly. */
9744 if (SYMBOL_CALLS_LOCAL (info, h))
9745 {
57e7d118
AM
9746 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9747 {
9748 p->count -= p->pc_count;
9749 p->pc_count = 0;
9750 if (p->count == 0)
9751 *pp = p->next;
9752 else
9753 pp = &p->next;
9754 }
9755 }
65f38f15 9756
954b63d4 9757 if (eh->dyn_relocs != NULL)
5bd4f169 9758 {
46434633
AM
9759 /* Make sure this symbol is output as a dynamic symbol. */
9760 if (!ensure_undef_dynamic (info, h))
f0158f44 9761 return FALSE;
5bd4f169 9762 }
65f38f15 9763 }
529fe20e 9764 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
57e7d118 9765 {
8a2058b5 9766 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9767 symbols which turn out to need copy relocs or are not
9768 dynamic. */
529fe20e
AM
9769 if (h->dynamic_adjusted
9770 && !h->def_regular
9771 && !ELF_COMMON_DEF_P (h))
f0158f44 9772 {
46434633
AM
9773 /* Make sure this symbol is output as a dynamic symbol. */
9774 if (!ensure_undef_dynamic (info, h))
f0158f44 9775 return FALSE;
dfbb6ac9 9776
f0158f44
AM
9777 if (h->dynindx == -1)
9778 eh->dyn_relocs = NULL;
9779 }
9780 else
8a2058b5 9781 eh->dyn_relocs = NULL;
57e7d118
AM
9782 }
9783
9784 /* Finally, allocate space. */
9785 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9786 {
9787 asection *sreloc = elf_section_data (p->sec)->sreloc;
9788 if (eh->elf.type == STT_GNU_IFUNC)
9789 sreloc = htab->elf.irelplt;
9790 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9791 }
65f38f15 9792 }
57e7d118
AM
9793
9794 if ((htab->elf.dynamic_sections_created
f0158f44 9795 && h->dynindx != -1)
57e7d118 9796 || h->type == STT_GNU_IFUNC)
65f38f15 9797 {
57e7d118
AM
9798 struct plt_entry *pent;
9799 bfd_boolean doneone = FALSE;
9800 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9801 if (pent->plt.refcount > 0)
9802 {
9803 if (!htab->elf.dynamic_sections_created
9804 || h->dynindx == -1)
9805 {
9806 s = htab->elf.iplt;
9807 pent->plt.offset = s->size;
9808 s->size += PLT_ENTRY_SIZE (htab);
9809 s = htab->elf.irelplt;
9810 }
9811 else
9812 {
9813 /* If this is the first .plt entry, make room for the special
9814 first entry. */
9815 s = htab->elf.splt;
9816 if (s->size == 0)
9817 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9818
57e7d118 9819 pent->plt.offset = s->size;
65f38f15 9820
57e7d118
AM
9821 /* Make room for this entry. */
9822 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9823
57e7d118
AM
9824 /* Make room for the .glink code. */
9825 s = htab->glink;
9826 if (s->size == 0)
9e390558 9827 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9828 if (htab->opd_abi)
9829 {
9830 /* We need bigger stubs past index 32767. */
9e390558 9831 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9832 s->size += 4;
9833 s->size += 2*4;
9834 }
9835 else
9836 s->size += 4;
65f38f15 9837
57e7d118
AM
9838 /* We also need to make an entry in the .rela.plt section. */
9839 s = htab->elf.srelplt;
9840 }
9841 s->size += sizeof (Elf64_External_Rela);
9842 doneone = TRUE;
9843 }
9844 else
9845 pent->plt.offset = (bfd_vma) -1;
9846 if (!doneone)
9847 {
9848 h->plt.plist = NULL;
9849 h->needs_plt = 0;
9850 }
65f38f15 9851 }
57e7d118 9852 else
65f38f15 9853 {
57e7d118
AM
9854 h->plt.plist = NULL;
9855 h->needs_plt = 0;
65f38f15
AM
9856 }
9857
b34976b6 9858 return TRUE;
65f38f15
AM
9859}
9860
9e390558
AM
9861#define PPC_LO(v) ((v) & 0xffff)
9862#define PPC_HI(v) (((v) >> 16) & 0xffff)
9863#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9864
a345bc8d
AM
9865/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9866 to set up space for global entry stubs. These are put in glink,
9867 after the branch table. */
65f38f15 9868
b34976b6 9869static bfd_boolean
a345bc8d 9870size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9871{
a345bc8d
AM
9872 struct bfd_link_info *info;
9873 struct ppc_link_hash_table *htab;
9874 struct plt_entry *pent;
9e390558 9875 asection *s, *plt;
65f38f15 9876
a345bc8d
AM
9877 if (h->root.type == bfd_link_hash_indirect)
9878 return TRUE;
65f38f15 9879
a345bc8d
AM
9880 if (!h->pointer_equality_needed)
9881 return TRUE;
65f38f15 9882
a345bc8d
AM
9883 if (h->def_regular)
9884 return TRUE;
65f38f15 9885
a345bc8d
AM
9886 info = inf;
9887 htab = ppc_hash_table (info);
9888 if (htab == NULL)
9889 return FALSE;
9890
9e390558
AM
9891 s = htab->global_entry;
9892 plt = htab->elf.splt;
a345bc8d
AM
9893 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9894 if (pent->plt.offset != (bfd_vma) -1
9895 && pent->addend == 0)
9896 {
afe397ea
AM
9897 /* For ELFv2, if this symbol is not defined in a regular file
9898 and we are not generating a shared library or pie, then we
9899 need to define the symbol in the executable on a call stub.
9900 This is to avoid text relocations. */
9e390558
AM
9901 bfd_vma off, stub_align, stub_off, stub_size;
9902 unsigned int align_power;
9903
9904 stub_size = 16;
9905 stub_off = s->size;
9906 if (htab->params->plt_stub_align >= 0)
9907 align_power = htab->params->plt_stub_align;
9908 else
9909 align_power = -htab->params->plt_stub_align;
9910 /* Setting section alignment is delayed until we know it is
9911 non-empty. Otherwise the .text output section will be
9912 aligned at least to plt_stub_align even when no global
9913 entry stubs are needed. */
9914 if (s->alignment_power < align_power)
9915 s->alignment_power = align_power;
9916 stub_align = (bfd_vma) 1 << align_power;
9917 if (htab->params->plt_stub_align >= 0
9918 || ((((stub_off + stub_size - 1) & -stub_align)
9919 - (stub_off & -stub_align))
9920 > ((stub_size - 1) & -stub_align)))
9921 stub_off = (stub_off + stub_align - 1) & -stub_align;
9922 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9923 off -= stub_off + s->output_offset + s->output_section->vma;
9924 /* Note that for --plt-stub-align negative we have a possible
9925 dependency between stub offset and size. Break that
9926 dependency by assuming the max stub size when calculating
9927 the stub offset. */
9928 if (PPC_HA (off) == 0)
9929 stub_size -= 4;
8a2058b5 9930 h->root.type = bfd_link_hash_defined;
afe397ea 9931 h->root.u.def.section = s;
9e390558
AM
9932 h->root.u.def.value = stub_off;
9933 s->size = stub_off + stub_size;
a345bc8d
AM
9934 break;
9935 }
9936 return TRUE;
9937}
9938
9939/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9940 read-only sections. */
9941
9942static bfd_boolean
98bbb1b8 9943maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
a345bc8d 9944{
98bbb1b8
AM
9945 asection *sec;
9946
a345bc8d
AM
9947 if (h->root.type == bfd_link_hash_indirect)
9948 return TRUE;
9949
98bbb1b8
AM
9950 sec = readonly_dynrelocs (h);
9951 if (sec != NULL)
a345bc8d 9952 {
98bbb1b8
AM
9953 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9954
9955 info->flags |= DF_TEXTREL;
9956 info->callbacks->minfo
c1c8c1ef 9957 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
63c1f59d 9958 sec->owner, h->root.root.string, sec);
a345bc8d
AM
9959
9960 /* Not an error, just cut short the traversal. */
9961 return FALSE;
65f38f15 9962 }
b34976b6 9963 return TRUE;
65f38f15
AM
9964}
9965
9966/* Set the sizes of the dynamic sections. */
9967
b34976b6 9968static bfd_boolean
ee67d69a 9969ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9970 struct bfd_link_info *info)
65f38f15
AM
9971{
9972 struct ppc_link_hash_table *htab;
9973 bfd *dynobj;
9974 asection *s;
b34976b6 9975 bfd_boolean relocs;
65f38f15 9976 bfd *ibfd;
7865406b 9977 struct got_entry *first_tlsld;
65f38f15
AM
9978
9979 htab = ppc_hash_table (info);
4dfe6ac6
NC
9980 if (htab == NULL)
9981 return FALSE;
9982
65f38f15
AM
9983 dynobj = htab->elf.dynobj;
9984 if (dynobj == NULL)
9985 abort ();
9986
9987 if (htab->elf.dynamic_sections_created)
9988 {
9989 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9990 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9991 {
3d4d4302 9992 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9993 if (s == NULL)
9994 abort ();
eea6121a 9995 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9996 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9997 }
9998 }
9999
10000 /* Set up .got offsets for local syms, and space for local dynamic
10001 relocs. */
c72f2fb2 10002 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10003 {
411e1bfb
AM
10004 struct got_entry **lgot_ents;
10005 struct got_entry **end_lgot_ents;
e054468f
AM
10006 struct plt_entry **local_plt;
10007 struct plt_entry **end_local_plt;
f961d9dd 10008 unsigned char *lgot_masks;
65f38f15
AM
10009 bfd_size_type locsymcount;
10010 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10011
0c8d6e5c 10012 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10013 continue;
10014
10015 for (s = ibfd->sections; s != NULL; s = s->next)
10016 {
19e08130 10017 struct ppc_dyn_relocs *p;
65f38f15 10018
6edfbbad 10019 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10020 {
ec338859
AM
10021 if (!bfd_is_abs_section (p->sec)
10022 && bfd_is_abs_section (p->sec->output_section))
10023 {
10024 /* Input section has been discarded, either because
10025 it is a copy of a linkonce section or due to
10026 linker script /DISCARD/, so we'll be discarding
10027 the relocs too. */
10028 }
248866a8 10029 else if (p->count != 0)
ec338859 10030 {
19e08130
AM
10031 asection *srel = elf_section_data (p->sec)->sreloc;
10032 if (p->ifunc)
33e44f2e 10033 srel = htab->elf.irelplt;
eea6121a 10034 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10035 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10036 info->flags |= DF_TEXTREL;
ec338859 10037 }
65f38f15
AM
10038 }
10039 }
10040
411e1bfb
AM
10041 lgot_ents = elf_local_got_ents (ibfd);
10042 if (!lgot_ents)
65f38f15
AM
10043 continue;
10044
0ffa91dd 10045 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10046 locsymcount = symtab_hdr->sh_info;
411e1bfb 10047 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10048 local_plt = (struct plt_entry **) end_lgot_ents;
10049 end_local_plt = local_plt + locsymcount;
f961d9dd 10050 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10051 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10052 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10053 {
0b8bcf0d 10054 struct got_entry **pent, *ent;
411e1bfb 10055
0b8bcf0d
AM
10056 pent = lgot_ents;
10057 while ((ent = *pent) != NULL)
411e1bfb
AM
10058 if (ent->got.refcount > 0)
10059 {
e7b938ca 10060 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10061 {
927be08e 10062 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10063 *pent = ent->next;
411e1bfb
AM
10064 }
10065 else
10066 {
19e08130
AM
10067 unsigned int ent_size = 8;
10068 unsigned int rel_size = sizeof (Elf64_External_Rela);
10069
eea6121a 10070 ent->got.offset = s->size;
e7b938ca 10071 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10072 {
19e08130
AM
10073 ent_size *= 2;
10074 rel_size *= 2;
10075 }
10076 s->size += ent_size;
10077 if ((*lgot_masks & PLT_IFUNC) != 0)
10078 {
33e44f2e 10079 htab->elf.irelplt->size += rel_size;
19e08130
AM
10080 htab->got_reli_size += rel_size;
10081 }
f15d0b54
AM
10082 else if (bfd_link_pic (info)
10083 && !((ent->tls_type & TLS_TPREL) != 0
10084 && bfd_link_executable (info)))
19e08130
AM
10085 {
10086 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10087 srel->size += rel_size;
927be08e 10088 }
0b8bcf0d 10089 pent = &ent->next;
411e1bfb
AM
10090 }
10091 }
10092 else
0b8bcf0d 10093 *pent = ent->next;
65f38f15 10094 }
e054468f
AM
10095
10096 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10097 for (; local_plt < end_local_plt; ++local_plt)
10098 {
10099 struct plt_entry *ent;
10100
10101 for (ent = *local_plt; ent != NULL; ent = ent->next)
10102 if (ent->plt.refcount > 0)
10103 {
33e44f2e 10104 s = htab->elf.iplt;
e054468f 10105 ent->plt.offset = s->size;
b9e5796b 10106 s->size += PLT_ENTRY_SIZE (htab);
e054468f 10107
33e44f2e 10108 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
10109 }
10110 else
10111 ent->plt.offset = (bfd_vma) -1;
10112 }
65f38f15
AM
10113 }
10114
10115 /* Allocate global sym .plt and .got entries, and space for global
10116 sym dynamic relocs. */
4ce794b7 10117 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10118
0e1862bb 10119 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10120 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10121
7865406b 10122 first_tlsld = NULL;
c72f2fb2 10123 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10124 {
7865406b
AM
10125 struct got_entry *ent;
10126
0c8d6e5c 10127 if (!is_ppc64_elf (ibfd))
102890f0
AM
10128 continue;
10129
7865406b
AM
10130 ent = ppc64_tlsld_got (ibfd);
10131 if (ent->got.refcount > 0)
102890f0 10132 {
7865406b 10133 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10134 {
7865406b
AM
10135 ent->is_indirect = TRUE;
10136 ent->got.ent = first_tlsld;
10137 }
10138 else
10139 {
10140 if (first_tlsld == NULL)
10141 first_tlsld = ent;
10142 s = ppc64_elf_tdata (ibfd)->got;
10143 ent->got.offset = s->size;
10144 ent->owner = ibfd;
10145 s->size += 16;
0e1862bb 10146 if (bfd_link_pic (info))
7865406b
AM
10147 {
10148 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10149 srel->size += sizeof (Elf64_External_Rela);
10150 }
102890f0
AM
10151 }
10152 }
10153 else
7865406b 10154 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10155 }
10156
65f38f15
AM
10157 /* We now have determined the sizes of the various dynamic sections.
10158 Allocate memory for them. */
b34976b6 10159 relocs = FALSE;
65f38f15
AM
10160 for (s = dynobj->sections; s != NULL; s = s->next)
10161 {
10162 if ((s->flags & SEC_LINKER_CREATED) == 0)
10163 continue;
10164
4ce794b7 10165 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10166 /* These haven't been allocated yet; don't strip. */
10167 continue;
33e44f2e
AM
10168 else if (s == htab->elf.sgot
10169 || s == htab->elf.splt
10170 || s == htab->elf.iplt
c456f082 10171 || s == htab->glink
9e390558 10172 || s == htab->global_entry
5474d94f
AM
10173 || s == htab->elf.sdynbss
10174 || s == htab->elf.sdynrelro)
65f38f15
AM
10175 {
10176 /* Strip this section if we don't need it; see the
10177 comment below. */
5bd4f169 10178 }
58d180e8
AM
10179 else if (s == htab->glink_eh_frame)
10180 {
10181 if (!bfd_is_abs_section (s->output_section))
10182 /* Not sized yet. */
10183 continue;
10184 }
70cc837d 10185 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10186 {
c456f082 10187 if (s->size != 0)
5bd4f169 10188 {
33e44f2e 10189 if (s != htab->elf.srelplt)
b34976b6 10190 relocs = TRUE;
5bd4f169
AM
10191
10192 /* We use the reloc_count field as a counter if we need
10193 to copy relocs into the output file. */
10194 s->reloc_count = 0;
10195 }
10196 }
65f38f15 10197 else
5bd4f169
AM
10198 {
10199 /* It's not one of our sections, so don't allocate space. */
10200 continue;
10201 }
10202
eea6121a 10203 if (s->size == 0)
5bd4f169 10204 {
c456f082
AM
10205 /* If we don't need this section, strip it from the
10206 output file. This is mostly to handle .rela.bss and
10207 .rela.plt. We must create both sections in
10208 create_dynamic_sections, because they must be created
10209 before the linker maps input sections to output
10210 sections. The linker does that before
10211 adjust_dynamic_symbol is called, and it is that
10212 function which decides whether anything needs to go
10213 into these sections. */
8423293d 10214 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10215 continue;
10216 }
10217
06bcf541
AM
10218 if (bfd_is_abs_section (s->output_section))
10219 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10220 s->name);
10221
c456f082 10222 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10223 continue;
10224
65f38f15
AM
10225 /* Allocate memory for the section contents. We use bfd_zalloc
10226 here in case unused entries are not reclaimed before the
10227 section's contents are written out. This should not happen,
411e1bfb
AM
10228 but this way if it does we get a R_PPC64_NONE reloc in .rela
10229 sections instead of garbage.
10230 We also rely on the section contents being zero when writing
5474d94f 10231 the GOT and .dynrelro. */
eea6121a 10232 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10233 if (s->contents == NULL)
b34976b6 10234 return FALSE;
5bd4f169
AM
10235 }
10236
c72f2fb2 10237 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10238 {
0c8d6e5c 10239 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10240 continue;
10241
e717da7e 10242 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10243 if (s != NULL && s != htab->elf.sgot)
e717da7e 10244 {
eea6121a 10245 if (s->size == 0)
8423293d 10246 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10247 else
10248 {
eea6121a 10249 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10250 if (s->contents == NULL)
10251 return FALSE;
10252 }
10253 }
10254 s = ppc64_elf_tdata (ibfd)->relgot;
10255 if (s != NULL)
10256 {
eea6121a 10257 if (s->size == 0)
8423293d 10258 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10259 else
10260 {
eea6121a 10261 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10262 if (s->contents == NULL)
10263 return FALSE;
10264 relocs = TRUE;
10265 s->reloc_count = 0;
10266 }
10267 }
10268 }
10269
e86ce104 10270 if (htab->elf.dynamic_sections_created)
5bd4f169 10271 {
e8910a83
AM
10272 bfd_boolean tls_opt;
10273
5bd4f169
AM
10274 /* Add some entries to the .dynamic section. We fill in the
10275 values later, in ppc64_elf_finish_dynamic_sections, but we
10276 must add the entries now so that we get the correct size for
10277 the .dynamic section. The DT_DEBUG entry is filled in by the
10278 dynamic linker and used by the debugger. */
dc810e39 10279#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10280 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10281
0e1862bb 10282 if (bfd_link_executable (info))
5bd4f169 10283 {
dc810e39 10284 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10285 return FALSE;
5bd4f169
AM
10286 }
10287
33e44f2e 10288 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10289 {
dc810e39
AM
10290 if (!add_dynamic_entry (DT_PLTGOT, 0)
10291 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10292 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10293 || !add_dynamic_entry (DT_JMPREL, 0)
10294 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10295 return FALSE;
5bd4f169
AM
10296 }
10297
ee67d69a 10298 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10299 {
10300 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10301 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10302 return FALSE;
19397422
AM
10303 }
10304
7c9cf415 10305 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10306 && htab->tls_get_addr_fd != NULL
10307 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10308 if (tls_opt || !htab->opd_abi)
10309 {
10310 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10311 return FALSE;
10312 }
a7f2871e 10313
5bd4f169
AM
10314 if (relocs)
10315 {
dc810e39
AM
10316 if (!add_dynamic_entry (DT_RELA, 0)
10317 || !add_dynamic_entry (DT_RELASZ, 0)
10318 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10319 return FALSE;
5bd4f169 10320
65f38f15
AM
10321 /* If any dynamic relocs apply to a read-only section,
10322 then we need a DT_TEXTREL entry. */
248866a8 10323 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10324 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10325
65f38f15 10326 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10327 {
65f38f15 10328 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10329 return FALSE;
5bd4f169 10330 }
5bd4f169 10331 }
5bd4f169 10332 }
65f38f15 10333#undef add_dynamic_entry
5bd4f169 10334
b34976b6 10335 return TRUE;
5bd4f169
AM
10336}
10337
a345bc8d
AM
10338/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10339
10340static bfd_boolean
10341ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10342{
10343 if (h->plt.plist != NULL
10344 && !h->def_regular
10345 && !h->pointer_equality_needed)
10346 return FALSE;
10347
10348 return _bfd_elf_hash_symbol (h);
10349}
10350
721956f4 10351/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10352
4ce794b7
AM
10353static inline enum ppc_stub_type
10354ppc_type_of_stub (asection *input_sec,
10355 const Elf_Internal_Rela *rel,
10356 struct ppc_link_hash_entry **hash,
e054468f 10357 struct plt_entry **plt_ent,
6911b7dc
AM
10358 bfd_vma destination,
10359 unsigned long local_off)
5bd4f169 10360{
721956f4
AM
10361 struct ppc_link_hash_entry *h = *hash;
10362 bfd_vma location;
10363 bfd_vma branch_offset;
10364 bfd_vma max_branch_offset;
4ce794b7 10365 enum elf_ppc64_reloc_type r_type;
5bd4f169 10366
721956f4
AM
10367 if (h != NULL)
10368 {
e054468f 10369 struct plt_entry *ent;
7fe2b9a6 10370 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10371 if (h->oh != NULL
10372 && h->oh->is_func_descriptor)
7b8f6675
AM
10373 {
10374 fdh = ppc_follow_link (h->oh);
10375 *hash = fdh;
10376 }
8387904d 10377
e054468f
AM
10378 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10379 if (ent->addend == rel->r_addend
10380 && ent->plt.offset != (bfd_vma) -1)
10381 {
e054468f
AM
10382 *plt_ent = ent;
10383 return ppc_stub_plt_call;
10384 }
5bd4f169 10385
7fe2b9a6
AM
10386 /* Here, we know we don't have a plt entry. If we don't have a
10387 either a defined function descriptor or a defined entry symbol
10388 in a regular object file, then it is pointless trying to make
10389 any other type of stub. */
854b41e7
AM
10390 if (!is_static_defined (&fdh->elf)
10391 && !is_static_defined (&h->elf))
721956f4 10392 return ppc_stub_none;
5d1634d7 10393 }
e054468f
AM
10394 else if (elf_local_got_ents (input_sec->owner) != NULL)
10395 {
10396 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10397 struct plt_entry **local_plt = (struct plt_entry **)
10398 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10399 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10400
10401 if (local_plt[r_symndx] != NULL)
10402 {
10403 struct plt_entry *ent;
10404
10405 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10406 if (ent->addend == rel->r_addend
10407 && ent->plt.offset != (bfd_vma) -1)
10408 {
10409 *plt_ent = ent;
10410 return ppc_stub_plt_call;
10411 }
10412 }
10413 }
5d1634d7 10414
721956f4
AM
10415 /* Determine where the call point is. */
10416 location = (input_sec->output_offset
10417 + input_sec->output_section->vma
10418 + rel->r_offset);
5d1634d7 10419
721956f4
AM
10420 branch_offset = destination - location;
10421 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10422
721956f4
AM
10423 /* Determine if a long branch stub is needed. */
10424 max_branch_offset = 1 << 25;
4ce794b7 10425 if (r_type != R_PPC64_REL24)
721956f4 10426 max_branch_offset = 1 << 15;
5d1634d7 10427
6911b7dc 10428 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10429 /* We need a stub. Figure out whether a long_branch or plt_branch
10430 is needed later. */
10431 return ppc_stub_long_branch;
5d1634d7 10432
721956f4 10433 return ppc_stub_none;
5d1634d7
AM
10434}
10435
794e51c0
AM
10436/* With power7 weakly ordered memory model, it is possible for ld.so
10437 to update a plt entry in one thread and have another thread see a
10438 stale zero toc entry. To avoid this we need some sort of acquire
10439 barrier in the call stub. One solution is to make the load of the
10440 toc word seem to appear to depend on the load of the function entry
10441 word. Another solution is to test for r2 being zero, and branch to
10442 the appropriate glink entry if so.
10443
10444 . fake dep barrier compare
71a39c98
AM
10445 . ld 12,xxx(2) ld 12,xxx(2)
10446 . mtctr 12 mtctr 12
10447 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10448 . add 2,2,11 cmpldi 2,0
10449 . ld 2,xxx+8(2) bnectr+
10450 . bctr b <glink_entry>
10451
10452 The solution involving the compare turns out to be faster, so
10453 that's what we use unless the branch won't reach. */
10454
10455#define ALWAYS_USE_FAKE_DEP 0
10456#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10457
794e51c0
AM
10458static inline unsigned int
10459plt_stub_size (struct ppc_link_hash_table *htab,
10460 struct ppc_stub_hash_entry *stub_entry,
10461 bfd_vma off)
10462{
b9e5796b
AM
10463 unsigned size = 12;
10464
10465 if (ALWAYS_EMIT_R2SAVE
10466 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10467 size += 4;
10468 if (PPC_HA (off) != 0)
794e51c0 10469 size += 4;
b9e5796b
AM
10470 if (htab->opd_abi)
10471 {
10472 size += 4;
e7d1c40c 10473 if (htab->params->plt_static_chain)
b9e5796b 10474 size += 4;
bd4d2eaa
AM
10475 if (htab->params->plt_thread_safe
10476 && htab->elf.dynamic_sections_created
10477 && stub_entry->h != NULL
10478 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10479 size += 8;
e7d1c40c 10480 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10481 size += 4;
10482 }
794e51c0
AM
10483 if (stub_entry->h != NULL
10484 && (stub_entry->h == htab->tls_get_addr_fd
10485 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10486 && htab->params->tls_get_addr_opt)
f378ab09
AM
10487 {
10488 size += 7 * 4;
10489 if (ALWAYS_EMIT_R2SAVE
10490 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
407aa07c 10491 size += 6 * 4;
f378ab09 10492 }
794e51c0
AM
10493 return size;
10494}
10495
2420fff6
AM
10496/* Depending on the sign of plt_stub_align:
10497 If positive, return the padding to align to a 2**plt_stub_align
10498 boundary.
10499 If negative, if this stub would cross fewer 2**plt_stub_align
10500 boundaries if we align, then return the padding needed to do so. */
10501
794e51c0
AM
10502static inline unsigned int
10503plt_stub_pad (struct ppc_link_hash_table *htab,
10504 struct ppc_stub_hash_entry *stub_entry,
10505 bfd_vma plt_off)
10506{
2420fff6 10507 int stub_align;
794e51c0 10508 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10509 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10510
2420fff6
AM
10511 if (htab->params->plt_stub_align >= 0)
10512 {
10513 stub_align = 1 << htab->params->plt_stub_align;
10514 if ((stub_off & (stub_align - 1)) != 0)
10515 return stub_align - (stub_off & (stub_align - 1));
10516 return 0;
10517 }
10518
10519 stub_align = 1 << -htab->params->plt_stub_align;
794e51c0 10520 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10521 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10522 return stub_align - (stub_off & (stub_align - 1));
10523 return 0;
10524}
10525
10526/* Build a .plt call stub. */
10527
10528static inline bfd_byte *
10529build_plt_stub (struct ppc_link_hash_table *htab,
10530 struct ppc_stub_hash_entry *stub_entry,
10531 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10532{
e7d1c40c 10533 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10534 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10535 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10536 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10537 && htab->elf.dynamic_sections_created
10538 && stub_entry->h != NULL
10539 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10540 bfd_boolean use_fake_dep = plt_thread_safe;
10541 bfd_vma cmp_branch_off = 0;
10542
10543 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10544 && plt_load_toc
794e51c0 10545 && plt_thread_safe
bd4d2eaa
AM
10546 && !((stub_entry->h == htab->tls_get_addr_fd
10547 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10548 && htab->params->tls_get_addr_opt))
794e51c0
AM
10549 {
10550 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10551 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10552 / PLT_ENTRY_SIZE (htab));
9e390558 10553 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
10554 bfd_vma to, from;
10555
68d62958
AM
10556 if (pltindex > 32768)
10557 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10558 to = (glinkoff
10559 + htab->glink->output_offset
10560 + htab->glink->output_section->vma);
6f20ed8a 10561 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10562 + 4 * (ALWAYS_EMIT_R2SAVE
10563 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10564 + 4 * (PPC_HA (offset) != 0)
10565 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10566 != PPC_HA (offset))
10567 + 4 * (plt_static_chain != 0)
10568 + 20
6f20ed8a
AM
10569 + stub_entry->group->stub_sec->output_offset
10570 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10571 cmp_branch_off = to - from;
10572 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10573 }
10574
ac2df442
AM
10575 if (PPC_HA (offset) != 0)
10576 {
176a0d42
AM
10577 if (r != NULL)
10578 {
794e51c0
AM
10579 if (ALWAYS_EMIT_R2SAVE
10580 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10581 r[0].r_offset += 4;
176a0d42 10582 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10583 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10584 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10585 r[1].r_addend = r[0].r_addend;
b9e5796b 10586 if (plt_load_toc)
176a0d42 10587 {
b9e5796b 10588 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10589 {
b9e5796b
AM
10590 r[2].r_offset = r[1].r_offset + 4;
10591 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10592 r[2].r_addend = r[0].r_addend;
10593 }
10594 else
10595 {
10596 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10597 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10598 r[2].r_addend = r[0].r_addend + 8;
10599 if (plt_static_chain)
10600 {
10601 r[3].r_offset = r[2].r_offset + 4;
10602 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10603 r[3].r_addend = r[0].r_addend + 16;
10604 }
c7131b65 10605 }
176a0d42
AM
10606 }
10607 }
794e51c0
AM
10608 if (ALWAYS_EMIT_R2SAVE
10609 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10610 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10611 if (plt_load_toc)
10612 {
10613 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10614 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10615 }
10616 else
10617 {
10618 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10619 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10620 }
b9e5796b
AM
10621 if (plt_load_toc
10622 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10623 {
71a39c98 10624 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10625 offset = 0;
10626 }
71a39c98 10627 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10628 if (plt_load_toc)
794e51c0 10629 {
b9e5796b
AM
10630 if (use_fake_dep)
10631 {
10632 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10633 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10634 }
10635 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10636 if (plt_static_chain)
10637 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10638 }
ac2df442
AM
10639 }
10640 else
10641 {
176a0d42
AM
10642 if (r != NULL)
10643 {
794e51c0
AM
10644 if (ALWAYS_EMIT_R2SAVE
10645 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10646 r[0].r_offset += 4;
176a0d42 10647 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10648 if (plt_load_toc)
176a0d42 10649 {
b9e5796b 10650 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10651 {
b9e5796b
AM
10652 r[1].r_offset = r[0].r_offset + 4;
10653 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10654 r[1].r_addend = r[0].r_addend;
10655 }
10656 else
10657 {
10658 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10659 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10660 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10661 if (plt_static_chain)
10662 {
10663 r[2].r_offset = r[1].r_offset + 4;
10664 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10665 r[2].r_addend = r[0].r_addend + 8;
10666 }
c7131b65 10667 }
176a0d42
AM
10668 }
10669 }
794e51c0
AM
10670 if (ALWAYS_EMIT_R2SAVE
10671 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10672 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10673 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10674 if (plt_load_toc
10675 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10676 {
10677 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10678 offset = 0;
10679 }
71a39c98 10680 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10681 if (plt_load_toc)
794e51c0 10682 {
b9e5796b
AM
10683 if (use_fake_dep)
10684 {
10685 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10686 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10687 }
10688 if (plt_static_chain)
10689 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10690 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10691 }
ac2df442 10692 }
b9e5796b 10693 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10694 {
10695 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10696 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10697 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10698 }
10699 else
407aa07c 10700 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10701 return p;
10702}
10703
a7f2871e
AM
10704/* Build a special .plt call stub for __tls_get_addr. */
10705
10706#define LD_R11_0R3 0xe9630000
10707#define LD_R12_0R3 0xe9830000
10708#define MR_R0_R3 0x7c601b78
10709#define CMPDI_R11_0 0x2c2b0000
10710#define ADD_R3_R12_R13 0x7c6c6a14
10711#define BEQLR 0x4d820020
10712#define MR_R3_R0 0x7c030378
a7f2871e
AM
10713#define STD_R11_0R1 0xf9610000
10714#define BCTRL 0x4e800421
10715#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10716#define MTLR_R11 0x7d6803a6
10717
10718static inline bfd_byte *
794e51c0
AM
10719build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10720 struct ppc_stub_hash_entry *stub_entry,
10721 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10722{
e7d1c40c 10723 bfd *obfd = htab->params->stub_bfd;
794e51c0 10724
a7f2871e
AM
10725 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10726 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10727 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10728 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10729 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10730 bfd_put_32 (obfd, BEQLR, p), p += 4;
10731 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
f378ab09
AM
10732 if (r != NULL)
10733 r[0].r_offset += 7 * 4;
10734 if (!ALWAYS_EMIT_R2SAVE
10735 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
10736 return build_plt_stub (htab, stub_entry, p, offset, r);
10737
a7f2871e 10738 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10739 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10740
10741 if (r != NULL)
f378ab09 10742 r[0].r_offset += 2 * 4;
794e51c0 10743 p = build_plt_stub (htab, stub_entry, p, offset, r);
407aa07c 10744 bfd_put_32 (obfd, BCTRL, p - 4);
a7f2871e 10745
a078d95a 10746 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10747 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10748 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10749 bfd_put_32 (obfd, BLR, p), p += 4;
10750
10751 return p;
10752}
10753
176a0d42
AM
10754static Elf_Internal_Rela *
10755get_relocs (asection *sec, int count)
10756{
10757 Elf_Internal_Rela *relocs;
10758 struct bfd_elf_section_data *elfsec_data;
10759
10760 elfsec_data = elf_section_data (sec);
10761 relocs = elfsec_data->relocs;
10762 if (relocs == NULL)
10763 {
10764 bfd_size_type relsize;
10765 relsize = sec->reloc_count * sizeof (*relocs);
10766 relocs = bfd_alloc (sec->owner, relsize);
10767 if (relocs == NULL)
10768 return NULL;
10769 elfsec_data->relocs = relocs;
d4730f92
BS
10770 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10771 sizeof (Elf_Internal_Shdr));
10772 if (elfsec_data->rela.hdr == NULL)
10773 return NULL;
10774 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10775 * sizeof (Elf64_External_Rela));
10776 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10777 sec->reloc_count = 0;
10778 }
10779 relocs += sec->reloc_count;
10780 sec->reloc_count += count;
10781 return relocs;
10782}
10783
aa374f67 10784static bfd_vma
25f53a85 10785get_r2off (struct bfd_link_info *info,
aa374f67
AM
10786 struct ppc_stub_hash_entry *stub_entry)
10787{
25f53a85 10788 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10789 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10790
10791 if (r2off == 0)
10792 {
10793 /* Support linking -R objects. Get the toc pointer from the
10794 opd entry. */
10795 char buf[8];
b9e5796b
AM
10796 if (!htab->opd_abi)
10797 return r2off;
aa374f67
AM
10798 asection *opd = stub_entry->h->elf.root.u.def.section;
10799 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10800
10801 if (strcmp (opd->name, ".opd") != 0
10802 || opd->reloc_count != 0)
10803 {
c1c8c1ef 10804 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
25f53a85 10805 stub_entry->h->elf.root.root.string);
aa374f67 10806 bfd_set_error (bfd_error_bad_value);
a7c49797 10807 return (bfd_vma) -1;
aa374f67
AM
10808 }
10809 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10810 return (bfd_vma) -1;
aa374f67 10811 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10812 r2off -= elf_gp (info->output_bfd);
aa374f67 10813 }
6f20ed8a 10814 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10815 return r2off;
10816}
10817
b34976b6 10818static bfd_boolean
4ce794b7 10819ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10820{
721956f4
AM
10821 struct ppc_stub_hash_entry *stub_entry;
10822 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10823 struct bfd_link_info *info;
10824 struct ppc_link_hash_table *htab;
721956f4
AM
10825 bfd_byte *loc;
10826 bfd_byte *p;
ee75fd95 10827 bfd_vma dest, off;
176a0d42 10828 Elf_Internal_Rela *r;
e054468f 10829 asection *plt;
5d1634d7 10830
721956f4
AM
10831 /* Massage our args to the form they really have. */
10832 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10833 info = in_arg;
5d1634d7 10834
5d1634d7 10835 htab = ppc_hash_table (info);
4dfe6ac6
NC
10836 if (htab == NULL)
10837 return FALSE;
5d1634d7 10838
721956f4 10839 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
10840 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10841 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10842
4ce794b7 10843 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10844 switch (stub_entry->stub_type)
5d1634d7 10845 {
721956f4 10846 case ppc_stub_long_branch:
ad8e1ba5 10847 case ppc_stub_long_branch_r2off:
721956f4 10848 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10849 dest = (stub_entry->target_value
10850 + stub_entry->target_section->output_offset
10851 + stub_entry->target_section->output_section->vma);
10852 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10853 off = dest;
5d1634d7 10854
721956f4
AM
10855 /* And this is where we are coming from. */
10856 off -= (stub_entry->stub_offset
6f20ed8a
AM
10857 + stub_entry->group->stub_sec->output_offset
10858 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 10859
9e390558 10860 p = loc;
ac2df442 10861 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10862 {
25f53a85 10863 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10864
a7c49797 10865 if (r2off == (bfd_vma) -1)
aa374f67
AM
10866 {
10867 htab->stub_error = TRUE;
10868 return FALSE;
10869 }
9e390558
AM
10870 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10871 p += 4;
ac2df442
AM
10872 if (PPC_HA (r2off) != 0)
10873 {
e7d1c40c 10874 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10875 ADDIS_R2_R2 | PPC_HA (r2off), p);
10876 p += 4;
a7c49797
AM
10877 }
10878 if (PPC_LO (r2off) != 0)
10879 {
10880 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
10881 ADDI_R2_R2 | PPC_LO (r2off), p);
10882 p += 4;
ac2df442 10883 }
9e390558 10884 off -= p - loc;
ad8e1ba5 10885 }
9e390558
AM
10886 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10887 p += 4;
ad8e1ba5 10888
5c3dead3
AM
10889 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10890 {
cf97bcb0
AM
10891 _bfd_error_handler
10892 (_("long branch stub `%s' offset overflow"),
bc30df16 10893 stub_entry->root.string);
5c3dead3
AM
10894 htab->stub_error = TRUE;
10895 return FALSE;
10896 }
ee75fd95
AM
10897
10898 if (info->emitrelocations)
10899 {
6f20ed8a 10900 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10901 if (r == NULL)
10902 return FALSE;
9e390558 10903 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95
AM
10904 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10905 r->r_addend = dest;
10906 if (stub_entry->h != NULL)
10907 {
10908 struct elf_link_hash_entry **hashes;
10909 unsigned long symndx;
10910 struct ppc_link_hash_entry *h;
10911
e7d1c40c 10912 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10913 if (hashes == NULL)
10914 {
10915 bfd_size_type hsize;
10916
10917 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10918 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10919 if (hashes == NULL)
10920 return FALSE;
e7d1c40c 10921 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10922 htab->stub_globals = 1;
10923 }
10924 symndx = htab->stub_globals++;
10925 h = stub_entry->h;
10926 hashes[symndx] = &h->elf;
10927 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10928 if (h->oh != NULL && h->oh->is_func)
b31867b6 10929 h = ppc_follow_link (h->oh);
ee75fd95
AM
10930 if (h->elf.root.u.def.section != stub_entry->target_section)
10931 /* H is an opd symbol. The addend must be zero. */
10932 r->r_addend = 0;
10933 else
10934 {
10935 off = (h->elf.root.u.def.value
10936 + h->elf.root.u.def.section->output_offset
10937 + h->elf.root.u.def.section->output_section->vma);
10938 r->r_addend -= off;
10939 }
10940 }
10941 }
721956f4 10942 break;
e86ce104 10943
721956f4 10944 case ppc_stub_plt_branch:
ad8e1ba5 10945 case ppc_stub_plt_branch_r2off:
721956f4
AM
10946 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10947 stub_entry->root.string + 9,
b34976b6 10948 FALSE, FALSE);
721956f4
AM
10949 if (br_entry == NULL)
10950 {
cf97bcb0
AM
10951 _bfd_error_handler (_("can't find branch stub `%s'"),
10952 stub_entry->root.string);
b34976b6
AM
10953 htab->stub_error = TRUE;
10954 return FALSE;
721956f4
AM
10955 }
10956
176a0d42
AM
10957 dest = (stub_entry->target_value
10958 + stub_entry->target_section->output_offset
10959 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10960 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10961 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10962
176a0d42 10963 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10964 htab->brlt->contents + br_entry->offset);
721956f4 10965
f94498ff 10966 if (br_entry->iter == htab->stub_iteration)
721956f4 10967 {
f94498ff 10968 br_entry->iter = 0;
84f5d08e 10969
f94498ff 10970 if (htab->relbrlt != NULL)
84f5d08e 10971 {
f94498ff
AM
10972 /* Create a reloc for the branch lookup table entry. */
10973 Elf_Internal_Rela rela;
10974 bfd_byte *rl;
10975
10976 rela.r_offset = (br_entry->offset
10977 + htab->brlt->output_offset
10978 + htab->brlt->output_section->vma);
10979 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10980 rela.r_addend = dest;
f94498ff
AM
10981
10982 rl = htab->relbrlt->contents;
10983 rl += (htab->relbrlt->reloc_count++
10984 * sizeof (Elf64_External_Rela));
10985 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10986 }
10987 else if (info->emitrelocations)
10988 {
176a0d42
AM
10989 r = get_relocs (htab->brlt, 1);
10990 if (r == NULL)
10991 return FALSE;
10992 /* brlt, being SEC_LINKER_CREATED does not go through the
10993 normal reloc processing. Symbols and offsets are not
10994 translated from input file to output file form, so
10995 set up the offset per the output file. */
f94498ff
AM
10996 r->r_offset = (br_entry->offset
10997 + htab->brlt->output_offset
10998 + htab->brlt->output_section->vma);
10999 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 11000 r->r_addend = dest;
84f5d08e 11001 }
84f5d08e 11002 }
721956f4 11003
176a0d42
AM
11004 dest = (br_entry->offset
11005 + htab->brlt->output_offset
11006 + htab->brlt->output_section->vma);
11007
11008 off = (dest
06bcf541 11009 - elf_gp (info->output_bfd)
6f20ed8a 11010 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11011
ad8e1ba5 11012 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11013 {
25f53a85 11014 info->callbacks->einfo
c1c8c1ef 11015 (_("%P: linkage table error against `%pT'\n"),
721956f4 11016 stub_entry->root.string);
5d1634d7 11017 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11018 htab->stub_error = TRUE;
11019 return FALSE;
5d1634d7 11020 }
41bd81ab 11021
176a0d42
AM
11022 if (info->emitrelocations)
11023 {
6f20ed8a 11024 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11025 if (r == NULL)
11026 return FALSE;
6f20ed8a 11027 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11028 if (bfd_big_endian (info->output_bfd))
11029 r[0].r_offset += 2;
00f412ee 11030 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11031 r[0].r_offset += 4;
11032 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11033 r[0].r_addend = dest;
11034 if (PPC_HA (off) != 0)
11035 {
11036 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11037 r[1].r_offset = r[0].r_offset + 4;
11038 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11039 r[1].r_addend = r[0].r_addend;
11040 }
11041 }
11042
9e390558 11043 p = loc;
00f412ee 11044 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11045 {
176a0d42 11046 if (PPC_HA (off) != 0)
ac2df442 11047 {
e7d1c40c 11048 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11049 ADDIS_R12_R2 | PPC_HA (off), p);
11050 p += 4;
e7d1c40c 11051 bfd_put_32 (htab->params->stub_bfd,
9e390558 11052 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11053 }
11054 else
9e390558
AM
11055 bfd_put_32 (htab->params->stub_bfd,
11056 LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11057 }
11058 else
11059 {
25f53a85 11060 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11061
a7c49797 11062 if (r2off == (bfd_vma) -1)
aa374f67
AM
11063 {
11064 htab->stub_error = TRUE;
11065 return FALSE;
11066 }
ad8e1ba5 11067
9e390558
AM
11068 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11069 p += 4;
176a0d42 11070 if (PPC_HA (off) != 0)
ac2df442 11071 {
e7d1c40c 11072 bfd_put_32 (htab->params->stub_bfd,
9e390558
AM
11073 ADDIS_R12_R2 | PPC_HA (off), p);
11074 p += 4;
e7d1c40c 11075 bfd_put_32 (htab->params->stub_bfd,
9e390558 11076 LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11077 }
11078 else
9e390558 11079 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11080
11081 if (PPC_HA (r2off) != 0)
11082 {
9e390558 11083 p += 4;
e7d1c40c 11084 bfd_put_32 (htab->params->stub_bfd,
9e390558 11085 ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11086 }
11087 if (PPC_LO (r2off) != 0)
11088 {
9e390558 11089 p += 4;
e7d1c40c 11090 bfd_put_32 (htab->params->stub_bfd,
9e390558 11091 ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11092 }
ad8e1ba5 11093 }
9e390558
AM
11094 p += 4;
11095 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11096 p += 4;
407aa07c
AM
11097 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11098 p += 4;
721956f4 11099 break;
5d1634d7 11100
721956f4 11101 case ppc_stub_plt_call:
794e51c0 11102 case ppc_stub_plt_call_r2save:
e054468f 11103 if (stub_entry->h != NULL
b31867b6
AM
11104 && stub_entry->h->is_func_descriptor
11105 && stub_entry->h->oh != NULL)
c862ae31 11106 {
b31867b6
AM
11107 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11108
11109 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11110 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11111 if (fh->elf.root.type == bfd_link_hash_undefined
11112 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11113 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11114 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11115 }
11116
721956f4 11117 /* Now build the stub. */
e054468f 11118 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 11119 if (dest >= (bfd_vma) -2)
721956f4
AM
11120 abort ();
11121
33e44f2e 11122 plt = htab->elf.splt;
25f23106
AM
11123 if (!htab->elf.dynamic_sections_created
11124 || stub_entry->h == NULL
11125 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11126 plt = htab->elf.iplt;
e054468f
AM
11127
11128 dest += plt->output_offset + plt->output_section->vma;
11129
11130 if (stub_entry->h == NULL
11131 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11132 {
11133 Elf_Internal_Rela rela;
11134 bfd_byte *rl;
11135
11136 rela.r_offset = dest;
ee67d69a
AM
11137 if (htab->opd_abi)
11138 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11139 else
11140 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
11141 rela.r_addend = (stub_entry->target_value
11142 + stub_entry->target_section->output_offset
11143 + stub_entry->target_section->output_section->vma);
11144
33e44f2e
AM
11145 rl = (htab->elf.irelplt->contents
11146 + (htab->elf.irelplt->reloc_count++
25f23106
AM
11147 * sizeof (Elf64_External_Rela)));
11148 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f 11149 stub_entry->plt_ent->plt.offset |= 1;
82e66161 11150 htab->local_ifunc_resolver = 1;
e054468f 11151 }
176a0d42
AM
11152
11153 off = (dest
06bcf541 11154 - elf_gp (info->output_bfd)
6f20ed8a 11155 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11156
ad8e1ba5 11157 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11158 {
25f53a85 11159 info->callbacks->einfo
695344c0 11160 /* xgettext:c-format */
c1c8c1ef 11161 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11162 stub_entry->h != NULL
11163 ? stub_entry->h->elf.root.root.string
11164 : "<local sym>");
721956f4 11165 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11166 htab->stub_error = TRUE;
11167 return FALSE;
721956f4
AM
11168 }
11169
e7d1c40c 11170 if (htab->params->plt_stub_align != 0)
794e51c0
AM
11171 {
11172 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11173
6f20ed8a
AM
11174 stub_entry->group->stub_sec->size += pad;
11175 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
11176 loc += pad;
11177 }
11178
176a0d42
AM
11179 r = NULL;
11180 if (info->emitrelocations)
11181 {
6f20ed8a 11182 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11183 ((PPC_HA (off) != 0)
11184 + (htab->opd_abi
e7d1c40c 11185 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11186 && PPC_HA (off + 16) == PPC_HA (off))
11187 : 1)));
176a0d42
AM
11188 if (r == NULL)
11189 return FALSE;
6f20ed8a 11190 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11191 if (bfd_big_endian (info->output_bfd))
11192 r[0].r_offset += 2;
176a0d42
AM
11193 r[0].r_addend = dest;
11194 }
a7f2871e
AM
11195 if (stub_entry->h != NULL
11196 && (stub_entry->h == htab->tls_get_addr_fd
11197 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11198 && htab->params->tls_get_addr_opt)
794e51c0 11199 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11200 else
794e51c0 11201 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11202 break;
11203
a4b6fadd
AM
11204 case ppc_stub_save_res:
11205 return TRUE;
11206
721956f4
AM
11207 default:
11208 BFD_FAIL ();
b34976b6 11209 return FALSE;
721956f4
AM
11210 }
11211
9e390558 11212 stub_entry->group->stub_sec->size += p - loc;
97b639ba 11213
e7d1c40c 11214 if (htab->params->emit_stub_syms)
97b639ba
AM
11215 {
11216 struct elf_link_hash_entry *h;
ee75fd95
AM
11217 size_t len1, len2;
11218 char *name;
11219 const char *const stub_str[] = { "long_branch",
11220 "long_branch_r2off",
11221 "plt_branch",
11222 "plt_branch_r2off",
794e51c0 11223 "plt_call",
ee75fd95
AM
11224 "plt_call" };
11225
11226 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11227 len2 = strlen (stub_entry->root.string);
11228 name = bfd_malloc (len1 + len2 + 2);
11229 if (name == NULL)
11230 return FALSE;
11231 memcpy (name, stub_entry->root.string, 9);
11232 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11233 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11234 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11235 if (h == NULL)
11236 return FALSE;
11237 if (h->root.type == bfd_link_hash_new)
11238 {
11239 h->root.type = bfd_link_hash_defined;
6f20ed8a 11240 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11241 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11242 h->ref_regular = 1;
11243 h->def_regular = 1;
11244 h->ref_regular_nonweak = 1;
11245 h->forced_local = 1;
11246 h->non_elf = 0;
2ec55de3 11247 h->root.linker_def = 1;
97b639ba
AM
11248 }
11249 }
11250
b34976b6 11251 return TRUE;
721956f4
AM
11252}
11253
11254/* As above, but don't actually build the stub. Just bump offset so
11255 we know stub section sizes, and select plt_branch stubs where
11256 long_branch stubs won't do. */
11257
b34976b6 11258static bfd_boolean
4ce794b7 11259ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11260{
11261 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11262 struct bfd_link_info *info;
721956f4
AM
11263 struct ppc_link_hash_table *htab;
11264 bfd_vma off;
11265 int size;
11266
11267 /* Massage our args to the form they really have. */
11268 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11269 info = in_arg;
11270
11271 htab = ppc_hash_table (info);
4dfe6ac6
NC
11272 if (htab == NULL)
11273 return FALSE;
721956f4 11274
a4b6fadd
AM
11275 if (stub_entry->h != NULL
11276 && stub_entry->h->save_res
11277 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11278 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11279 {
11280 /* Don't make stubs to out-of-line register save/restore
11281 functions. Instead, emit copies of the functions. */
11282 stub_entry->group->needs_save_res = 1;
11283 stub_entry->stub_type = ppc_stub_save_res;
11284 return TRUE;
11285 }
11286
794e51c0
AM
11287 if (stub_entry->stub_type == ppc_stub_plt_call
11288 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11289 {
e054468f
AM
11290 asection *plt;
11291 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11292 if (off >= (bfd_vma) -2)
411e1bfb 11293 abort ();
33e44f2e 11294 plt = htab->elf.splt;
25f23106
AM
11295 if (!htab->elf.dynamic_sections_created
11296 || stub_entry->h == NULL
11297 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11298 plt = htab->elf.iplt;
e054468f
AM
11299 off += (plt->output_offset
11300 + plt->output_section->vma
06bcf541 11301 - elf_gp (info->output_bfd)
6f20ed8a 11302 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11303
794e51c0 11304 size = plt_stub_size (htab, stub_entry, off);
d4aaa2a0
AM
11305 if (stub_entry->h != NULL
11306 && (stub_entry->h == htab->tls_get_addr_fd
11307 || stub_entry->h == htab->tls_get_addr)
11308 && htab->params->tls_get_addr_opt
11309 && (ALWAYS_EMIT_R2SAVE
11310 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11311 stub_entry->group->tls_get_addr_opt_bctrl
11312 = stub_entry->group->stub_sec->size + size - 5 * 4;
11313
e7d1c40c 11314 if (htab->params->plt_stub_align)
794e51c0 11315 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11316 if (info->emitrelocations)
11317 {
6f20ed8a 11318 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11319 += ((PPC_HA (off) != 0)
11320 + (htab->opd_abi
e7d1c40c 11321 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11322 && PPC_HA (off + 16) == PPC_HA (off))
11323 : 1));
6f20ed8a 11324 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11325 }
721956f4
AM
11326 }
11327 else
11328 {
ad8e1ba5
AM
11329 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11330 variants. */
ac2df442 11331 bfd_vma r2off = 0;
6911b7dc 11332 bfd_vma local_off = 0;
ac2df442 11333
721956f4
AM
11334 off = (stub_entry->target_value
11335 + stub_entry->target_section->output_offset
11336 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11337 off -= (stub_entry->group->stub_sec->size
11338 + stub_entry->group->stub_sec->output_offset
11339 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11340
ad8e1ba5
AM
11341 /* Reset the stub type from the plt variant in case we now
11342 can reach with a shorter stub. */
11343 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11344 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11345
11346 size = 4;
11347 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11348 {
25f53a85 11349 r2off = get_r2off (info, stub_entry);
a7c49797 11350 if (r2off == (bfd_vma) -1)
aa374f67
AM
11351 {
11352 htab->stub_error = TRUE;
11353 return FALSE;
11354 }
a7c49797 11355 size = 8;
ac2df442 11356 if (PPC_HA (r2off) != 0)
a7c49797
AM
11357 size += 4;
11358 if (PPC_LO (r2off) != 0)
11359 size += 4;
ac2df442 11360 off -= size - 4;
ad8e1ba5
AM
11361 }
11362
6911b7dc
AM
11363 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11364
6a3858a6 11365 /* If the branch offset is too big, use a ppc_stub_plt_branch.
b9e5796b
AM
11366 Do the same for -R objects without function descriptors. */
11367 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11368 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11369 && r2off == 0
11370 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11371 {
11372 struct ppc_branch_hash_entry *br_entry;
11373
11374 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11375 stub_entry->root.string + 9,
b34976b6 11376 TRUE, FALSE);
721956f4
AM
11377 if (br_entry == NULL)
11378 {
cf97bcb0
AM
11379 _bfd_error_handler (_("can't build branch stub `%s'"),
11380 stub_entry->root.string);
b34976b6
AM
11381 htab->stub_error = TRUE;
11382 return FALSE;
721956f4
AM
11383 }
11384
11385 if (br_entry->iter != htab->stub_iteration)
11386 {
11387 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11388 br_entry->offset = htab->brlt->size;
11389 htab->brlt->size += 8;
63bc6f6c 11390
ee75fd95 11391 if (htab->relbrlt != NULL)
eea6121a 11392 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11393 else if (info->emitrelocations)
11394 {
11395 htab->brlt->reloc_count += 1;
11396 htab->brlt->flags |= SEC_RELOC;
11397 }
721956f4 11398 }
ad8e1ba5
AM
11399
11400 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11401 off = (br_entry->offset
11402 + htab->brlt->output_offset
11403 + htab->brlt->output_section->vma
06bcf541 11404 - elf_gp (info->output_bfd)
6f20ed8a 11405 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11406
176a0d42
AM
11407 if (info->emitrelocations)
11408 {
6f20ed8a
AM
11409 stub_entry->group->stub_sec->reloc_count
11410 += 1 + (PPC_HA (off) != 0);
11411 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11412 }
11413
00f412ee 11414 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11415 {
11416 size = 12;
176a0d42 11417 if (PPC_HA (off) != 0)
ac2df442
AM
11418 size = 16;
11419 }
11420 else
11421 {
00f412ee 11422 size = 16;
176a0d42 11423 if (PPC_HA (off) != 0)
ac2df442
AM
11424 size += 4;
11425
11426 if (PPC_HA (r2off) != 0)
11427 size += 4;
00f412ee
AM
11428 if (PPC_LO (r2off) != 0)
11429 size += 4;
ac2df442 11430 }
721956f4 11431 }
84f5d08e
AM
11432 else if (info->emitrelocations)
11433 {
6f20ed8a
AM
11434 stub_entry->group->stub_sec->reloc_count += 1;
11435 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11436 }
721956f4
AM
11437 }
11438
6f20ed8a 11439 stub_entry->group->stub_sec->size += size;
b34976b6 11440 return TRUE;
721956f4
AM
11441}
11442
11443/* Set up various things so that we can make a list of input sections
11444 for each output section included in the link. Returns -1 on error,
cedb70c5 11445 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11446
11447int
e7d1c40c 11448ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11449{
6f20ed8a 11450 unsigned int id;
721956f4
AM
11451 bfd_size_type amt;
11452 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11453
4dfe6ac6
NC
11454 if (htab == NULL)
11455 return -1;
4c52953f 11456
6f20ed8a
AM
11457 htab->sec_info_arr_size = bfd_get_next_section_id ();
11458 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11459 htab->sec_info = bfd_zmalloc (amt);
11460 if (htab->sec_info == NULL)
721956f4
AM
11461 return -1;
11462
3d6f9012
AM
11463 /* Set toc_off for com, und, abs and ind sections. */
11464 for (id = 0; id < 3; id++)
6f20ed8a 11465 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11466
721956f4
AM
11467 return 1;
11468}
11469
927be08e
AM
11470/* Set up for first pass at multitoc partitioning. */
11471
11472void
11473ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11474{
11475 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11476
1c865ab2 11477 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11478 htab->toc_bfd = NULL;
11479 htab->toc_first_sec = NULL;
11480}
11481
e717da7e
AM
11482/* The linker repeatedly calls this function for each TOC input section
11483 and linker generated GOT section. Group input bfds such that the toc
927be08e 11484 within a group is less than 64k in size. */
ad8e1ba5 11485
927be08e 11486bfd_boolean
4ce794b7 11487ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11488{
11489 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11490 bfd_vma addr, off, limit;
ad8e1ba5 11491
4dfe6ac6
NC
11492 if (htab == NULL)
11493 return FALSE;
11494
927be08e 11495 if (!htab->second_toc_pass)
4c52953f 11496 {
927be08e 11497 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11498 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11499
11500 if (new_bfd)
bf102f86
AM
11501 {
11502 htab->toc_bfd = isec->owner;
11503 htab->toc_first_sec = isec;
11504 }
927be08e 11505
bf102f86
AM
11506 addr = isec->output_offset + isec->output_section->vma;
11507 off = addr - htab->toc_curr;
d77c8a4b
AM
11508 limit = 0x80008000;
11509 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11510 limit = 0x10000;
11511 if (off + isec->size > limit)
bf102f86
AM
11512 {
11513 addr = (htab->toc_first_sec->output_offset
11514 + htab->toc_first_sec->output_section->vma);
11515 htab->toc_curr = addr;
a27e685f 11516 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11517 }
99877b66 11518
927be08e
AM
11519 /* toc_curr is the base address of this toc group. Set elf_gp
11520 for the input section to be the offset relative to the
11521 output toc base plus 0x8000. Making the input elf_gp an
11522 offset allows us to move the toc as a whole without
11523 recalculating input elf_gp. */
06bcf541 11524 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
11525 off += TOC_BASE_OFF;
11526
11527 /* Die if someone uses a linker script that doesn't keep input
11528 file .toc and .got together. */
a4fd3de5
AM
11529 if (new_bfd
11530 && elf_gp (isec->owner) != 0
927be08e
AM
11531 && elf_gp (isec->owner) != off)
11532 return FALSE;
11533
11534 elf_gp (isec->owner) = off;
11535 return TRUE;
4c52953f 11536 }
927be08e
AM
11537
11538 /* During the second pass toc_first_sec points to the start of
11539 a toc group, and toc_curr is used to track the old elf_gp.
11540 We use toc_bfd to ensure we only look at each bfd once. */
11541 if (htab->toc_bfd == isec->owner)
11542 return TRUE;
11543 htab->toc_bfd = isec->owner;
11544
11545 if (htab->toc_first_sec == NULL
11546 || htab->toc_curr != elf_gp (isec->owner))
11547 {
11548 htab->toc_curr = elf_gp (isec->owner);
11549 htab->toc_first_sec = isec;
11550 }
11551 addr = (htab->toc_first_sec->output_offset
11552 + htab->toc_first_sec->output_section->vma);
06bcf541 11553 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
11554 elf_gp (isec->owner) = off;
11555
11556 return TRUE;
ad8e1ba5
AM
11557}
11558
927be08e
AM
11559/* Called via elf_link_hash_traverse to merge GOT entries for global
11560 symbol H. */
11561
11562static bfd_boolean
11563merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11564{
11565 if (h->root.type == bfd_link_hash_indirect)
11566 return TRUE;
11567
927be08e
AM
11568 merge_got_entries (&h->got.glist);
11569
11570 return TRUE;
11571}
11572
11573/* Called via elf_link_hash_traverse to allocate GOT entries for global
11574 symbol H. */
11575
11576static bfd_boolean
11577reallocate_got (struct elf_link_hash_entry *h, void *inf)
11578{
11579 struct got_entry *gent;
11580
11581 if (h->root.type == bfd_link_hash_indirect)
11582 return TRUE;
11583
927be08e
AM
11584 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11585 if (!gent->is_indirect)
11586 allocate_got (h, (struct bfd_link_info *) inf, gent);
11587 return TRUE;
11588}
11589
11590/* Called on the first multitoc pass after the last call to
11591 ppc64_elf_next_toc_section. This function removes duplicate GOT
11592 entries. */
11593
11594bfd_boolean
11595ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11596{
11597 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11598 struct bfd *ibfd, *ibfd2;
11599 bfd_boolean done_something;
11600
11601 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11602
7865406b
AM
11603 if (!htab->do_multi_toc)
11604 return FALSE;
11605
d0fae19d 11606 /* Merge global sym got entries within a toc group. */
927be08e
AM
11607 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11608
11609 /* And tlsld_got. */
c72f2fb2 11610 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11611 {
11612 struct got_entry *ent, *ent2;
11613
11614 if (!is_ppc64_elf (ibfd))
11615 continue;
11616
11617 ent = ppc64_tlsld_got (ibfd);
11618 if (!ent->is_indirect
11619 && ent->got.offset != (bfd_vma) -1)
11620 {
c72f2fb2 11621 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11622 {
11623 if (!is_ppc64_elf (ibfd2))
11624 continue;
11625
11626 ent2 = ppc64_tlsld_got (ibfd2);
11627 if (!ent2->is_indirect
11628 && ent2->got.offset != (bfd_vma) -1
11629 && elf_gp (ibfd2) == elf_gp (ibfd))
11630 {
11631 ent2->is_indirect = TRUE;
11632 ent2->got.ent = ent;
11633 }
11634 }
11635 }
11636 }
11637
11638 /* Zap sizes of got sections. */
33e44f2e
AM
11639 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11640 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11641 htab->got_reli_size = 0;
11642
c72f2fb2 11643 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11644 {
11645 asection *got, *relgot;
11646
11647 if (!is_ppc64_elf (ibfd))
11648 continue;
11649
11650 got = ppc64_elf_tdata (ibfd)->got;
11651 if (got != NULL)
11652 {
11653 got->rawsize = got->size;
11654 got->size = 0;
11655 relgot = ppc64_elf_tdata (ibfd)->relgot;
11656 relgot->rawsize = relgot->size;
11657 relgot->size = 0;
11658 }
11659 }
11660
11661 /* Now reallocate the got, local syms first. We don't need to
11662 allocate section contents again since we never increase size. */
c72f2fb2 11663 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11664 {
11665 struct got_entry **lgot_ents;
11666 struct got_entry **end_lgot_ents;
11667 struct plt_entry **local_plt;
11668 struct plt_entry **end_local_plt;
f961d9dd 11669 unsigned char *lgot_masks;
927be08e
AM
11670 bfd_size_type locsymcount;
11671 Elf_Internal_Shdr *symtab_hdr;
19e08130 11672 asection *s;
927be08e
AM
11673
11674 if (!is_ppc64_elf (ibfd))
11675 continue;
11676
11677 lgot_ents = elf_local_got_ents (ibfd);
11678 if (!lgot_ents)
11679 continue;
11680
11681 symtab_hdr = &elf_symtab_hdr (ibfd);
11682 locsymcount = symtab_hdr->sh_info;
11683 end_lgot_ents = lgot_ents + locsymcount;
11684 local_plt = (struct plt_entry **) end_lgot_ents;
11685 end_local_plt = local_plt + locsymcount;
f961d9dd 11686 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11687 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11688 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11689 {
11690 struct got_entry *ent;
11691
11692 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11693 {
19e08130
AM
11694 unsigned int ent_size = 8;
11695 unsigned int rel_size = sizeof (Elf64_External_Rela);
11696
d0fae19d
AM
11697 ent->got.offset = s->size;
11698 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11699 {
19e08130
AM
11700 ent_size *= 2;
11701 rel_size *= 2;
11702 }
11703 s->size += ent_size;
11704 if ((*lgot_masks & PLT_IFUNC) != 0)
11705 {
33e44f2e 11706 htab->elf.irelplt->size += rel_size;
19e08130
AM
11707 htab->got_reli_size += rel_size;
11708 }
6a3858a6
AM
11709 else if (bfd_link_pic (info)
11710 && !((ent->tls_type & TLS_TPREL) != 0
11711 && bfd_link_executable (info)))
19e08130
AM
11712 {
11713 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11714 srel->size += rel_size;
d0fae19d
AM
11715 }
11716 }
927be08e
AM
11717 }
11718 }
11719
11720 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11721
c72f2fb2 11722 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11723 {
11724 struct got_entry *ent;
11725
11726 if (!is_ppc64_elf (ibfd))
11727 continue;
11728
11729 ent = ppc64_tlsld_got (ibfd);
11730 if (!ent->is_indirect
11731 && ent->got.offset != (bfd_vma) -1)
11732 {
11733 asection *s = ppc64_elf_tdata (ibfd)->got;
11734 ent->got.offset = s->size;
11735 s->size += 16;
0e1862bb 11736 if (bfd_link_pic (info))
927be08e
AM
11737 {
11738 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11739 srel->size += sizeof (Elf64_External_Rela);
11740 }
11741 }
11742 }
11743
33e44f2e 11744 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11745 if (!done_something)
c72f2fb2 11746 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11747 {
11748 asection *got;
11749
11750 if (!is_ppc64_elf (ibfd))
11751 continue;
11752
11753 got = ppc64_elf_tdata (ibfd)->got;
11754 if (got != NULL)
11755 {
11756 done_something = got->rawsize != got->size;
11757 if (done_something)
11758 break;
11759 }
11760 }
11761
11762 if (done_something)
e7d1c40c 11763 (*htab->params->layout_sections_again) ();
927be08e
AM
11764
11765 /* Set up for second pass over toc sections to recalculate elf_gp
11766 on input sections. */
11767 htab->toc_bfd = NULL;
11768 htab->toc_first_sec = NULL;
11769 htab->second_toc_pass = TRUE;
11770 return done_something;
11771}
11772
11773/* Called after second pass of multitoc partitioning. */
11774
11775void
11776ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11777{
11778 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11779
11780 /* After the second pass, toc_curr tracks the TOC offset used
11781 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11782 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11783}
11784
9b5ecbd0
AM
11785/* No toc references were found in ISEC. If the code in ISEC makes no
11786 calls, then there's no need to use toc adjusting stubs when branching
11787 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11788 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11789 needed, and 2 if a cyclical call-graph was found but no other reason
11790 for a stub was detected. If called from the top level, a return of
11791 2 means the same as a return of 0. */
9b5ecbd0
AM
11792
11793static int
4ce794b7 11794toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11795{
9b5ecbd0 11796 int ret;
70cc837d
AM
11797
11798 /* Mark this section as checked. */
11799 isec->call_check_done = 1;
9b5ecbd0 11800
772119ce
AM
11801 /* We know none of our code bearing sections will need toc stubs. */
11802 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11803 return 0;
11804
eea6121a 11805 if (isec->size == 0)
082c50f8
AM
11806 return 0;
11807
4c52953f
AM
11808 if (isec->output_section == NULL)
11809 return 0;
11810
4c52953f 11811 ret = 0;
70cc837d 11812 if (isec->reloc_count != 0)
9b5ecbd0 11813 {
70cc837d
AM
11814 Elf_Internal_Rela *relstart, *rel;
11815 Elf_Internal_Sym *local_syms;
11816 struct ppc_link_hash_table *htab;
2917689a 11817
70cc837d
AM
11818 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11819 info->keep_memory);
11820 if (relstart == NULL)
11821 return -1;
90aecf7a 11822
70cc837d
AM
11823 /* Look for branches to outside of this section. */
11824 local_syms = NULL;
11825 htab = ppc_hash_table (info);
11826 if (htab == NULL)
11827 return -1;
4c52953f 11828
70cc837d 11829 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11830 {
70cc837d
AM
11831 enum elf_ppc64_reloc_type r_type;
11832 unsigned long r_symndx;
11833 struct elf_link_hash_entry *h;
11834 struct ppc_link_hash_entry *eh;
11835 Elf_Internal_Sym *sym;
11836 asection *sym_sec;
11837 struct _opd_sec_data *opd;
11838 bfd_vma sym_value;
11839 bfd_vma dest;
11840
11841 r_type = ELF64_R_TYPE (rel->r_info);
11842 if (r_type != R_PPC64_REL24
11843 && r_type != R_PPC64_REL14
11844 && r_type != R_PPC64_REL14_BRTAKEN
11845 && r_type != R_PPC64_REL14_BRNTAKEN)
11846 continue;
4c52953f 11847
70cc837d
AM
11848 r_symndx = ELF64_R_SYM (rel->r_info);
11849 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11850 isec->owner))
4c52953f 11851 {
70cc837d
AM
11852 ret = -1;
11853 break;
11854 }
4c52953f 11855
70cc837d
AM
11856 /* Calls to dynamic lib functions go through a plt call stub
11857 that uses r2. */
11858 eh = (struct ppc_link_hash_entry *) h;
11859 if (eh != NULL
11860 && (eh->elf.plt.plist != NULL
11861 || (eh->oh != NULL
11862 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11863 {
11864 ret = 1;
11865 break;
4c52953f
AM
11866 }
11867
70cc837d
AM
11868 if (sym_sec == NULL)
11869 /* Ignore other undefined symbols. */
4c52953f 11870 continue;
4c52953f 11871
70cc837d
AM
11872 /* Assume branches to other sections not included in the
11873 link need stubs too, to cover -R and absolute syms. */
11874 if (sym_sec->output_section == NULL)
11875 {
11876 ret = 1;
11877 break;
11878 }
4c52953f 11879
70cc837d
AM
11880 if (h == NULL)
11881 sym_value = sym->st_value;
11882 else
11883 {
11884 if (h->root.type != bfd_link_hash_defined
11885 && h->root.type != bfd_link_hash_defweak)
11886 abort ();
11887 sym_value = h->root.u.def.value;
11888 }
11889 sym_value += rel->r_addend;
4c52953f 11890
70cc837d
AM
11891 /* If this branch reloc uses an opd sym, find the code section. */
11892 opd = get_opd_info (sym_sec);
11893 if (opd != NULL)
11894 {
11895 if (h == NULL && opd->adjust != NULL)
11896 {
11897 long adjust;
4c52953f 11898
92a9c616 11899 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11900 if (adjust == -1)
11901 /* Assume deleted functions won't ever be called. */
11902 continue;
11903 sym_value += adjust;
11904 }
4c52953f 11905
aef36ac1
AM
11906 dest = opd_entry_value (sym_sec, sym_value,
11907 &sym_sec, NULL, FALSE);
70cc837d
AM
11908 if (dest == (bfd_vma) -1)
11909 continue;
11910 }
11911 else
11912 dest = (sym_value
11913 + sym_sec->output_offset
11914 + sym_sec->output_section->vma);
4c52953f 11915
70cc837d
AM
11916 /* Ignore branch to self. */
11917 if (sym_sec == isec)
11918 continue;
4c52953f 11919
70cc837d
AM
11920 /* If the called function uses the toc, we need a stub. */
11921 if (sym_sec->has_toc_reloc
11922 || sym_sec->makes_toc_func_call)
4c52953f 11923 {
70cc837d 11924 ret = 1;
4c52953f
AM
11925 break;
11926 }
70cc837d
AM
11927
11928 /* Assume any branch that needs a long branch stub might in fact
11929 need a plt_branch stub. A plt_branch stub uses r2. */
11930 else if (dest - (isec->output_offset
11931 + isec->output_section->vma
6911b7dc
AM
11932 + rel->r_offset) + (1 << 25)
11933 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11934 ? h->other
11935 : sym->st_other))
4c52953f 11936 {
70cc837d
AM
11937 ret = 1;
11938 break;
11939 }
11940
11941 /* If calling back to a section in the process of being
11942 tested, we can't say for sure that no toc adjusting stubs
11943 are needed, so don't return zero. */
11944 else if (sym_sec->call_check_in_progress)
11945 ret = 2;
11946
11947 /* Branches to another section that itself doesn't have any TOC
11948 references are OK. Recursively call ourselves to check. */
11949 else if (!sym_sec->call_check_done)
11950 {
11951 int recur;
11952
11953 /* Mark current section as indeterminate, so that other
11954 sections that call back to current won't be marked as
11955 known. */
11956 isec->call_check_in_progress = 1;
11957 recur = toc_adjusting_stub_needed (info, sym_sec);
11958 isec->call_check_in_progress = 0;
11959
4c52953f
AM
11960 if (recur != 0)
11961 {
70cc837d
AM
11962 ret = recur;
11963 if (recur != 2)
11964 break;
4c52953f
AM
11965 }
11966 }
4c52953f 11967 }
70cc837d
AM
11968
11969 if (local_syms != NULL
11970 && (elf_symtab_hdr (isec->owner).contents
11971 != (unsigned char *) local_syms))
11972 free (local_syms);
11973 if (elf_section_data (isec)->relocs != relstart)
11974 free (relstart);
9b5ecbd0
AM
11975 }
11976
70cc837d
AM
11977 if ((ret & 1) == 0
11978 && isec->map_head.s != NULL
11979 && (strcmp (isec->output_section->name, ".init") == 0
11980 || strcmp (isec->output_section->name, ".fini") == 0))
11981 {
11982 if (isec->map_head.s->has_toc_reloc
11983 || isec->map_head.s->makes_toc_func_call)
11984 ret = 1;
11985 else if (!isec->map_head.s->call_check_done)
11986 {
11987 int recur;
11988 isec->call_check_in_progress = 1;
11989 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11990 isec->call_check_in_progress = 0;
11991 if (recur != 0)
11992 ret = recur;
11993 }
11994 }
11995
11996 if (ret == 1)
11997 isec->makes_toc_func_call = 1;
4c52953f 11998
9b5ecbd0
AM
11999 return ret;
12000}
12001
721956f4
AM
12002/* The linker repeatedly calls this function for each input section,
12003 in the order that input sections are linked into output sections.
12004 Build lists of input sections to determine groupings between which
12005 we may insert linker stubs. */
12006
9b5ecbd0 12007bfd_boolean
4ce794b7 12008ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12009{
12010 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12011
4dfe6ac6
NC
12012 if (htab == NULL)
12013 return FALSE;
12014
734b6cf9 12015 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12016 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12017 {
3d6f9012
AM
12018 /* This happens to make the list in reverse order,
12019 which is what we want. */
6f20ed8a
AM
12020 htab->sec_info[isec->id].u.list
12021 = htab->sec_info[isec->output_section->id].u.list;
12022 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12023 }
ad8e1ba5 12024
4c52953f 12025 if (htab->multi_toc_needed)
9b5ecbd0 12026 {
8b974ba3
AM
12027 /* Analyse sections that aren't already flagged as needing a
12028 valid toc pointer. Exclude .fixup for the linux kernel.
12029 .fixup contains branches, but only back to the function that
12030 hit an exception. */
12031 if (!(isec->has_toc_reloc
12032 || (isec->flags & SEC_CODE) == 0
12033 || strcmp (isec->name, ".fixup") == 0
12034 || isec->call_check_done))
12035 {
12036 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12037 return FALSE;
8b974ba3
AM
12038 }
12039 /* Make all sections use the TOC assigned for this object file.
12040 This will be wrong for pasted sections; We fix that in
12041 check_pasted_section(). */
12042 if (elf_gp (isec->owner) != 0)
12043 htab->toc_curr = elf_gp (isec->owner);
12044 }
12045
6f20ed8a 12046 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12047 return TRUE;
721956f4
AM
12048}
12049
70cc837d
AM
12050/* Check that all .init and .fini sections use the same toc, if they
12051 have toc relocs. */
12052
12053static bfd_boolean
12054check_pasted_section (struct bfd_link_info *info, const char *name)
12055{
12056 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12057
12058 if (o != NULL)
12059 {
12060 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12061 bfd_vma toc_off = 0;
12062 asection *i;
12063
12064 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12065 if (i->has_toc_reloc)
12066 {
12067 if (toc_off == 0)
6f20ed8a
AM
12068 toc_off = htab->sec_info[i->id].toc_off;
12069 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12070 return FALSE;
12071 }
6683a28d
AM
12072
12073 if (toc_off == 0)
12074 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12075 if (i->makes_toc_func_call)
12076 {
6f20ed8a 12077 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12078 break;
12079 }
12080
70cc837d
AM
12081 /* Make sure the whole pasted function uses the same toc offset. */
12082 if (toc_off != 0)
12083 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12084 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12085 }
12086 return TRUE;
12087}
12088
12089bfd_boolean
12090ppc64_elf_check_init_fini (struct bfd_link_info *info)
12091{
12092 return (check_pasted_section (info, ".init")
12093 & check_pasted_section (info, ".fini"));
12094}
12095
721956f4
AM
12096/* See whether we can group stub sections together. Grouping stub
12097 sections may result in fewer stubs. More importantly, we need to
12098 put all .init* and .fini* stubs at the beginning of the .init or
12099 .fini output sections respectively, because glibc splits the
12100 _init and _fini functions into multiple parts. Putting a stub in
12101 the middle of a function is not a good idea. */
12102
6f20ed8a
AM
12103static bfd_boolean
12104group_sections (struct bfd_link_info *info,
4ce794b7
AM
12105 bfd_size_type stub_group_size,
12106 bfd_boolean stubs_always_before_branch)
721956f4 12107{
6f20ed8a
AM
12108 struct ppc_link_hash_table *htab;
12109 asection *osec;
7c8fe5c4
AM
12110 bfd_boolean suppress_size_errors;
12111
6f20ed8a
AM
12112 htab = ppc_hash_table (info);
12113 if (htab == NULL)
12114 return FALSE;
12115
7c8fe5c4 12116 suppress_size_errors = FALSE;
7c8fe5c4
AM
12117 if (stub_group_size == 1)
12118 {
12119 /* Default values. */
12120 if (stubs_always_before_branch)
09f92717 12121 stub_group_size = 0x1e00000;
7c8fe5c4 12122 else
09f92717 12123 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12124 suppress_size_errors = TRUE;
12125 }
12126
6f20ed8a 12127 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12128 {
6f20ed8a
AM
12129 asection *tail;
12130
12131 if (osec->id >= htab->sec_info_arr_size)
12132 continue;
12133
12134 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12135 while (tail != NULL)
721956f4 12136 {
734b6cf9
AM
12137 asection *curr;
12138 asection *prev;
12139 bfd_size_type total;
12140 bfd_boolean big_sec;
12141 bfd_vma curr_toc;
6f20ed8a 12142 struct map_stub *group;
09f92717 12143 bfd_size_type group_size;
734b6cf9
AM
12144
12145 curr = tail;
eea6121a 12146 total = tail->size;
09f92717
AM
12147 group_size = (ppc64_elf_section_data (tail) != NULL
12148 && ppc64_elf_section_data (tail)->has_14bit_branch
12149 ? stub_group_size >> 10 : stub_group_size);
12150
12151 big_sec = total > group_size;
7c8fe5c4 12152 if (big_sec && !suppress_size_errors)
695344c0 12153 /* xgettext:c-format */
871b3ab2 12154 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 12155 tail->owner, tail);
6f20ed8a 12156 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12157
6f20ed8a 12158 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12159 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12160 < (ppc64_elf_section_data (prev) != NULL
12161 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12162 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12163 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12164 curr = prev;
12165
12166 /* OK, the size from the start of CURR to the end is less
09f92717 12167 than group_size and thus can be handled by one stub
734b6cf9 12168 section. (or the tail section is itself larger than
09f92717
AM
12169 group_size, in which case we may be toast.) We should
12170 really be keeping track of the total size of stubs added
12171 here, as stubs contribute to the final output section
12172 size. That's a little tricky, and this way will only
12173 break if stubs added make the total size more than 2^25,
12174 ie. for the default stub_group_size, if stubs total more
12175 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12176 group = bfd_alloc (curr->owner, sizeof (*group));
12177 if (group == NULL)
12178 return FALSE;
12179 group->link_sec = curr;
12180 group->stub_sec = NULL;
a4b6fadd 12181 group->needs_save_res = 0;
d4aaa2a0 12182 group->tls_get_addr_opt_bctrl = -1u;
a4b6fadd
AM
12183 group->next = htab->group;
12184 htab->group = group;
734b6cf9 12185 do
721956f4 12186 {
6f20ed8a 12187 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12188 /* Set up this stub group. */
6f20ed8a 12189 htab->sec_info[tail->id].u.group = group;
721956f4 12190 }
734b6cf9
AM
12191 while (tail != curr && (tail = prev) != NULL);
12192
09f92717 12193 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12194 bytes before the stub section can be handled by it too.
12195 Don't do this if we have a really large section after the
12196 stubs, as adding more stubs increases the chance that
12197 branches may not reach into the stub section. */
12198 if (!stubs_always_before_branch && !big_sec)
12199 {
12200 total = 0;
12201 while (prev != NULL
12202 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12203 < (ppc64_elf_section_data (prev) != NULL
12204 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12205 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12206 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12207 {
12208 tail = prev;
6f20ed8a
AM
12209 prev = htab->sec_info[tail->id].u.list;
12210 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12211 }
12212 }
12213 tail = prev;
721956f4
AM
12214 }
12215 }
6f20ed8a 12216 return TRUE;
721956f4
AM
12217}
12218
58d180e8
AM
12219static const unsigned char glink_eh_frame_cie[] =
12220{
12221 0, 0, 0, 16, /* length. */
12222 0, 0, 0, 0, /* id. */
12223 1, /* CIE version. */
12224 'z', 'R', 0, /* Augmentation string. */
12225 4, /* Code alignment. */
12226 0x78, /* Data alignment. */
12227 65, /* RA reg. */
12228 1, /* Augmentation size. */
12229 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12230 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12231};
12232
d4aaa2a0
AM
12233static size_t
12234stub_eh_frame_size (struct map_stub *group, size_t align)
12235{
12236 size_t this_size = 17;
12237 if (group->tls_get_addr_opt_bctrl != -1u)
12238 {
12239 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12240 if (to_bctrl < 64)
12241 this_size += 1;
12242 else if (to_bctrl < 256)
12243 this_size += 2;
12244 else if (to_bctrl < 65536)
12245 this_size += 3;
12246 else
12247 this_size += 5;
12248 this_size += 6;
12249 }
12250 this_size = (this_size + align - 1) & -align;
12251 return this_size;
12252}
12253
d969d15f
AM
12254/* Stripping output sections is normally done before dynamic section
12255 symbols have been allocated. This function is called later, and
12256 handles cases like htab->brlt which is mapped to its own output
12257 section. */
12258
12259static void
12260maybe_strip_output (struct bfd_link_info *info, asection *isec)
12261{
12262 if (isec->size == 0
12263 && isec->output_section->size == 0
53d8967a 12264 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12265 && !bfd_section_removed_from_list (info->output_bfd,
12266 isec->output_section)
12267 && elf_section_data (isec->output_section)->dynindx == 0)
12268 {
12269 isec->output_section->flags |= SEC_EXCLUDE;
12270 bfd_section_list_remove (info->output_bfd, isec->output_section);
12271 info->output_bfd->section_count--;
12272 }
12273}
12274
721956f4
AM
12275/* Determine and set the size of the stub section for a final link.
12276
12277 The basic idea here is to examine all the relocations looking for
12278 PC-relative calls to a target that is unreachable with a "bl"
12279 instruction. */
12280
b34976b6 12281bfd_boolean
e7d1c40c 12282ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12283{
12284 bfd_size_type stub_group_size;
b34976b6 12285 bfd_boolean stubs_always_before_branch;
721956f4
AM
12286 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12287
4dfe6ac6
NC
12288 if (htab == NULL)
12289 return FALSE;
12290
0e1862bb 12291 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12292 htab->params->plt_thread_safe = 1;
b9e5796b 12293 if (!htab->opd_abi)
e7d1c40c
AM
12294 htab->params->plt_thread_safe = 0;
12295 else if (htab->params->plt_thread_safe == -1)
794e51c0 12296 {
e2458743 12297 static const char *const thread_starter[] =
794e51c0
AM
12298 {
12299 "pthread_create",
12300 /* libstdc++ */
12301 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12302 /* librt */
12303 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12304 "mq_notify", "create_timer",
12305 /* libanl */
12306 "getaddrinfo_a",
12307 /* libgomp */
2300b5a1 12308 "GOMP_parallel",
794e51c0 12309 "GOMP_parallel_start",
2300b5a1 12310 "GOMP_parallel_loop_static",
794e51c0 12311 "GOMP_parallel_loop_static_start",
2300b5a1 12312 "GOMP_parallel_loop_dynamic",
794e51c0 12313 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12314 "GOMP_parallel_loop_guided",
794e51c0 12315 "GOMP_parallel_loop_guided_start",
2300b5a1 12316 "GOMP_parallel_loop_runtime",
794e51c0 12317 "GOMP_parallel_loop_runtime_start",
2300b5a1 12318 "GOMP_parallel_sections",
68ffbac6 12319 "GOMP_parallel_sections_start",
f9dffbf0
AM
12320 /* libgo */
12321 "__go_go",
794e51c0
AM
12322 };
12323 unsigned i;
12324
a4b6fadd 12325 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12326 {
12327 struct elf_link_hash_entry *h;
12328 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12329 FALSE, FALSE, TRUE);
e7d1c40c
AM
12330 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12331 if (htab->params->plt_thread_safe)
794e51c0
AM
12332 break;
12333 }
12334 }
e7d1c40c
AM
12335 stubs_always_before_branch = htab->params->group_size < 0;
12336 if (htab->params->group_size < 0)
12337 stub_group_size = -htab->params->group_size;
721956f4 12338 else
e7d1c40c 12339 stub_group_size = htab->params->group_size;
721956f4 12340
6f20ed8a
AM
12341 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12342 return FALSE;
721956f4 12343
c9301e31
AM
12344#define STUB_SHRINK_ITER 20
12345 /* Loop until no stubs added. After iteration 20 of this loop we may
12346 exit on a stub section shrinking. This is to break out of a
12347 pathological case where adding stubs on one iteration decreases
12348 section gaps (perhaps due to alignment), which then requires
12349 fewer or smaller stubs on the next iteration. */
12350
721956f4
AM
12351 while (1)
12352 {
12353 bfd *input_bfd;
12354 unsigned int bfd_indx;
a4b6fadd 12355 struct map_stub *group;
721956f4
AM
12356
12357 htab->stub_iteration += 1;
721956f4
AM
12358
12359 for (input_bfd = info->input_bfds, bfd_indx = 0;
12360 input_bfd != NULL;
c72f2fb2 12361 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12362 {
12363 Elf_Internal_Shdr *symtab_hdr;
12364 asection *section;
6cdc0ccc 12365 Elf_Internal_Sym *local_syms = NULL;
721956f4 12366
0c8d6e5c 12367 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12368 continue;
12369
721956f4 12370 /* We'll need the symbol table in a second. */
0ffa91dd 12371 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12372 if (symtab_hdr->sh_info == 0)
12373 continue;
12374
721956f4
AM
12375 /* Walk over each section attached to the input bfd. */
12376 for (section = input_bfd->sections;
12377 section != NULL;
12378 section = section->next)
12379 {
721956f4 12380 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12381
12382 /* If there aren't any relocs, then there's nothing more
12383 to do. */
12384 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12385 || (section->flags & SEC_ALLOC) == 0
12386 || (section->flags & SEC_LOAD) == 0
12387 || (section->flags & SEC_CODE) == 0
721956f4
AM
12388 || section->reloc_count == 0)
12389 continue;
12390
12391 /* If this section is a link-once section that will be
12392 discarded, then don't create any stubs. */
12393 if (section->output_section == NULL
927be08e 12394 || section->output_section->owner != info->output_bfd)
721956f4
AM
12395 continue;
12396
1e2f5b6e
AM
12397 /* Get the relocs. */
12398 internal_relocs
4ce794b7 12399 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12400 info->keep_memory);
721956f4 12401 if (internal_relocs == NULL)
1e2f5b6e 12402 goto error_ret_free_local;
721956f4
AM
12403
12404 /* Now examine each relocation. */
12405 irela = internal_relocs;
12406 irelaend = irela + section->reloc_count;
12407 for (; irela < irelaend; irela++)
12408 {
4ce794b7
AM
12409 enum elf_ppc64_reloc_type r_type;
12410 unsigned int r_indx;
721956f4
AM
12411 enum ppc_stub_type stub_type;
12412 struct ppc_stub_hash_entry *stub_entry;
8387904d 12413 asection *sym_sec, *code_sec;
e054468f 12414 bfd_vma sym_value, code_value;
721956f4 12415 bfd_vma destination;
6911b7dc 12416 unsigned long local_off;
8843416a 12417 bfd_boolean ok_dest;
721956f4 12418 struct ppc_link_hash_entry *hash;
8387904d 12419 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12420 struct elf_link_hash_entry *h;
12421 Elf_Internal_Sym *sym;
721956f4
AM
12422 char *stub_name;
12423 const asection *id_sec;
74f0fb50 12424 struct _opd_sec_data *opd;
e054468f 12425 struct plt_entry *plt_ent;
721956f4
AM
12426
12427 r_type = ELF64_R_TYPE (irela->r_info);
12428 r_indx = ELF64_R_SYM (irela->r_info);
12429
4ce794b7 12430 if (r_type >= R_PPC64_max)
721956f4
AM
12431 {
12432 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12433 goto error_ret_free_internal;
721956f4
AM
12434 }
12435
12436 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12437 if (r_type != R_PPC64_REL24
12438 && r_type != R_PPC64_REL14
12439 && r_type != R_PPC64_REL14_BRTAKEN
12440 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12441 continue;
12442
12443 /* Now determine the call target, its name, value,
12444 section. */
411e1bfb
AM
12445 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12446 r_indx, input_bfd))
12447 goto error_ret_free_internal;
12448 hash = (struct ppc_link_hash_entry *) h;
12449
8843416a 12450 ok_dest = FALSE;
8387904d 12451 fdh = NULL;
7fe2b9a6 12452 sym_value = 0;
411e1bfb 12453 if (hash == NULL)
721956f4 12454 {
411e1bfb 12455 sym_value = sym->st_value;
c27b8c2a
AM
12456 if (sym_sec != NULL
12457 && sym_sec->output_section != NULL)
12458 ok_dest = TRUE;
721956f4 12459 }
7fe2b9a6
AM
12460 else if (hash->elf.root.type == bfd_link_hash_defined
12461 || hash->elf.root.type == bfd_link_hash_defweak)
12462 {
12463 sym_value = hash->elf.root.u.def.value;
12464 if (sym_sec->output_section != NULL)
12465 ok_dest = TRUE;
12466 }
12467 else if (hash->elf.root.type == bfd_link_hash_undefweak
12468 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12469 {
99877b66 12470 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12471 use the func descriptor sym instead if it is
12472 defined. */
ceb1f1ef 12473 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12474 && hash->oh != NULL)
8387904d 12475 {
8c5b4e52 12476 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12477 if (fdh->elf.root.type == bfd_link_hash_defined
12478 || fdh->elf.root.type == bfd_link_hash_defweak)
12479 {
12480 sym_sec = fdh->elf.root.u.def.section;
12481 sym_value = fdh->elf.root.u.def.value;
12482 if (sym_sec->output_section != NULL)
12483 ok_dest = TRUE;
12484 }
99877b66
AM
12485 else
12486 fdh = NULL;
8387904d 12487 }
7fe2b9a6
AM
12488 }
12489 else
12490 {
12491 bfd_set_error (bfd_error_bad_value);
12492 goto error_ret_free_internal;
721956f4
AM
12493 }
12494
8843416a 12495 destination = 0;
6911b7dc 12496 local_off = 0;
8843416a
AM
12497 if (ok_dest)
12498 {
12499 sym_value += irela->r_addend;
12500 destination = (sym_value
12501 + sym_sec->output_offset
12502 + sym_sec->output_section->vma);
6911b7dc
AM
12503 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12504 ? hash->elf.other
12505 : sym->st_other);
8843416a
AM
12506 }
12507
8387904d 12508 code_sec = sym_sec;
e054468f 12509 code_value = sym_value;
74f0fb50
AM
12510 opd = get_opd_info (sym_sec);
12511 if (opd != NULL)
8387904d
AM
12512 {
12513 bfd_vma dest;
12514
74f0fb50 12515 if (hash == NULL && opd->adjust != NULL)
8387904d 12516 {
51aecdc5 12517 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12518 if (adjust == -1)
12519 continue;
e054468f 12520 code_value += adjust;
8387904d
AM
12521 sym_value += adjust;
12522 }
12523 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12524 &code_sec, &code_value, FALSE);
8387904d
AM
12525 if (dest != (bfd_vma) -1)
12526 {
12527 destination = dest;
12528 if (fdh != NULL)
12529 {
12530 /* Fixup old ABI sym to point at code
12531 entry. */
99877b66 12532 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12533 hash->elf.root.u.def.section = code_sec;
e054468f 12534 hash->elf.root.u.def.value = code_value;
8387904d
AM
12535 }
12536 }
12537 }
12538
721956f4 12539 /* Determine what (if any) linker stub is needed. */
e054468f 12540 plt_ent = NULL;
721956f4 12541 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12542 &plt_ent, destination,
12543 local_off);
ad8e1ba5
AM
12544
12545 if (stub_type != ppc_stub_plt_call)
12546 {
12547 /* Check whether we need a TOC adjusting stub.
12548 Since the linker pastes together pieces from
12549 different object files when creating the
12550 _init and _fini functions, it may be that a
12551 call to what looks like a local sym is in
12552 fact a call needing a TOC adjustment. */
8387904d
AM
12553 if (code_sec != NULL
12554 && code_sec->output_section != NULL
6f20ed8a
AM
12555 && (htab->sec_info[code_sec->id].toc_off
12556 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12557 && (code_sec->has_toc_reloc
12558 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12559 stub_type = ppc_stub_long_branch_r2off;
12560 }
12561
721956f4
AM
12562 if (stub_type == ppc_stub_none)
12563 continue;
12564
411e1bfb
AM
12565 /* __tls_get_addr calls might be eliminated. */
12566 if (stub_type != ppc_stub_plt_call
12567 && hash != NULL
8387904d
AM
12568 && (hash == htab->tls_get_addr
12569 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12570 && section->has_tls_reloc
12571 && irela != internal_relocs)
12572 {
12573 /* Get tls info. */
f961d9dd 12574 unsigned char *tls_mask;
411e1bfb 12575
3a71aa26 12576 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12577 irela - 1, input_bfd))
12578 goto error_ret_free_internal;
e7b938ca 12579 if (*tls_mask != 0)
411e1bfb
AM
12580 continue;
12581 }
12582
f378ab09 12583 if (stub_type == ppc_stub_plt_call)
794e51c0 12584 {
6e1816be
AM
12585 if (!htab->opd_abi
12586 && htab->params->plt_localentry0 != 0
12587 && is_elfv2_localentry0 (&hash->elf))
12588 htab->has_plt_localentry0 = 1;
12589 else if (irela + 1 < irelaend
12590 && irela[1].r_offset == irela->r_offset + 4
12591 && (ELF64_R_TYPE (irela[1].r_info)
12592 == R_PPC64_TOCSAVE))
f378ab09
AM
12593 {
12594 if (!tocsave_find (htab, INSERT,
12595 &local_syms, irela + 1, input_bfd))
12596 goto error_ret_free_internal;
12597 }
f378ab09
AM
12598 else
12599 stub_type = ppc_stub_plt_call_r2save;
794e51c0 12600 }
3b421ab3 12601
721956f4 12602 /* Support for grouping stub sections. */
6f20ed8a 12603 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12604
12605 /* Get the name of this stub. */
12606 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12607 if (!stub_name)
12608 goto error_ret_free_internal;
12609
12610 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12611 stub_name, FALSE, FALSE);
721956f4
AM
12612 if (stub_entry != NULL)
12613 {
12614 /* The proper stub has already been created. */
12615 free (stub_name);
794e51c0
AM
12616 if (stub_type == ppc_stub_plt_call_r2save)
12617 stub_entry->stub_type = stub_type;
721956f4
AM
12618 continue;
12619 }
12620
25f53a85 12621 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12622 if (stub_entry == NULL)
12623 {
12624 free (stub_name);
6cdc0ccc
AM
12625 error_ret_free_internal:
12626 if (elf_section_data (section)->relocs == NULL)
12627 free (internal_relocs);
12628 error_ret_free_local:
12629 if (local_syms != NULL
12630 && (symtab_hdr->contents
12631 != (unsigned char *) local_syms))
12632 free (local_syms);
b34976b6 12633 return FALSE;
721956f4
AM
12634 }
12635
ad8e1ba5 12636 stub_entry->stub_type = stub_type;
794e51c0
AM
12637 if (stub_type != ppc_stub_plt_call
12638 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12639 {
12640 stub_entry->target_value = code_value;
12641 stub_entry->target_section = code_sec;
12642 }
12643 else
12644 {
12645 stub_entry->target_value = sym_value;
12646 stub_entry->target_section = sym_sec;
12647 }
721956f4 12648 stub_entry->h = hash;
e054468f 12649 stub_entry->plt_ent = plt_ent;
6911b7dc 12650 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12651
12652 if (stub_entry->h != NULL)
12653 htab->stub_globals += 1;
721956f4
AM
12654 }
12655
12656 /* We're done with the internal relocs, free them. */
6cdc0ccc 12657 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12658 free (internal_relocs);
721956f4 12659 }
6cdc0ccc
AM
12660
12661 if (local_syms != NULL
12662 && symtab_hdr->contents != (unsigned char *) local_syms)
12663 {
12664 if (!info->keep_memory)
12665 free (local_syms);
12666 else
12667 symtab_hdr->contents = (unsigned char *) local_syms;
12668 }
721956f4
AM
12669 }
12670
5c3dead3 12671 /* We may have added some stubs. Find out the new size of the
721956f4 12672 stub sections. */
d4aaa2a0
AM
12673 for (group = htab->group; group != NULL; group = group->next)
12674 if (group->stub_sec != NULL)
ee75fd95 12675 {
d4aaa2a0
AM
12676 asection *stub_sec = group->stub_sec;
12677
ea3d7d1c
AM
12678 if (htab->stub_iteration <= STUB_SHRINK_ITER
12679 || stub_sec->rawsize < stub_sec->size)
12680 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12681 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12682 stub_sec->size = 0;
12683 stub_sec->reloc_count = 0;
84f5d08e 12684 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12685 }
eea6121a 12686
ba21f564
AM
12687 if (htab->stub_iteration <= STUB_SHRINK_ITER
12688 || htab->brlt->rawsize < htab->brlt->size)
12689 htab->brlt->rawsize = htab->brlt->size;
eea6121a 12690 htab->brlt->size = 0;
84f5d08e
AM
12691 htab->brlt->reloc_count = 0;
12692 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12693 if (htab->relbrlt != NULL)
eea6121a 12694 htab->relbrlt->size = 0;
721956f4 12695
63bc6f6c 12696 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12697
a4b6fadd
AM
12698 for (group = htab->group; group != NULL; group = group->next)
12699 if (group->needs_save_res)
12700 group->stub_sec->size += htab->sfpr->size;
12701
176a0d42
AM
12702 if (info->emitrelocations
12703 && htab->glink != NULL && htab->glink->size != 0)
12704 {
12705 htab->glink->reloc_count = 1;
12706 htab->glink->flags |= SEC_RELOC;
12707 }
12708
58d180e8
AM
12709 if (htab->glink_eh_frame != NULL
12710 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 12711 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 12712 {
2e0ce1c8 12713 size_t size = 0, align = 4;
58d180e8 12714
d4aaa2a0
AM
12715 for (group = htab->group; group != NULL; group = group->next)
12716 if (group->stub_sec != NULL)
12717 size += stub_eh_frame_size (group, align);
58d180e8 12718 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 12719 size += (24 + align - 1) & -align;
58d180e8 12720 if (size != 0)
2e0ce1c8
AM
12721 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12722 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12723 size = (size + align - 1) & -align;
58d180e8
AM
12724 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12725 htab->glink_eh_frame->size = size;
12726 }
12727
e7d1c40c 12728 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
12729 for (group = htab->group; group != NULL; group = group->next)
12730 if (group->stub_sec != NULL)
691d2e9a
AM
12731 {
12732 int align = abs (htab->params->plt_stub_align);
12733 group->stub_sec->size
12734 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12735 }
d4aaa2a0
AM
12736
12737 for (group = htab->group; group != NULL; group = group->next)
12738 if (group->stub_sec != NULL
12739 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 12740 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 12741 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
12742 break;
12743
d4aaa2a0 12744 if (group == NULL
ba21f564
AM
12745 && (htab->brlt->rawsize == htab->brlt->size
12746 || (htab->stub_iteration > STUB_SHRINK_ITER
12747 && htab->brlt->rawsize > htab->brlt->size))
58d180e8
AM
12748 && (htab->glink_eh_frame == NULL
12749 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12750 break;
12751
721956f4 12752 /* Ask the linker to do its stuff. */
e7d1c40c 12753 (*htab->params->layout_sections_again) ();
721956f4
AM
12754 }
12755
da44f4e5
AM
12756 if (htab->glink_eh_frame != NULL
12757 && htab->glink_eh_frame->size != 0)
12758 {
12759 bfd_vma val;
12760 bfd_byte *p, *last_fde;
12761 size_t last_fde_len, size, align, pad;
d4aaa2a0 12762 struct map_stub *group;
da44f4e5
AM
12763
12764 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12765 if (p == NULL)
12766 return FALSE;
12767 htab->glink_eh_frame->contents = p;
12768 last_fde = p;
2e0ce1c8 12769 align = 4;
da44f4e5
AM
12770
12771 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12772 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 12773 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 12774 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 12775 p += last_fde_len + 4;
da44f4e5 12776
d4aaa2a0
AM
12777 for (group = htab->group; group != NULL; group = group->next)
12778 if (group->stub_sec != NULL)
da44f4e5
AM
12779 {
12780 last_fde = p;
d4aaa2a0 12781 last_fde_len = stub_eh_frame_size (group, align) - 4;
da44f4e5 12782 /* FDE length. */
2e0ce1c8 12783 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12784 p += 4;
12785 /* CIE pointer. */
12786 val = p - htab->glink_eh_frame->contents;
12787 bfd_put_32 (htab->elf.dynobj, val, p);
12788 p += 4;
12789 /* Offset to stub section, written later. */
12790 p += 4;
12791 /* stub section size. */
d4aaa2a0 12792 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
12793 p += 4;
12794 /* Augmentation. */
12795 p += 1;
d4aaa2a0
AM
12796 if (group->tls_get_addr_opt_bctrl != -1u)
12797 {
12798 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12799
12800 /* This FDE needs more than just the default.
12801 Describe __tls_get_addr_opt stub LR. */
12802 if (to_bctrl < 64)
12803 *p++ = DW_CFA_advance_loc + to_bctrl;
12804 else if (to_bctrl < 256)
12805 {
12806 *p++ = DW_CFA_advance_loc1;
12807 *p++ = to_bctrl;
12808 }
12809 else if (to_bctrl < 65536)
12810 {
12811 *p++ = DW_CFA_advance_loc2;
12812 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
12813 p += 2;
12814 }
12815 else
12816 {
12817 *p++ = DW_CFA_advance_loc4;
12818 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
12819 p += 4;
12820 }
12821 *p++ = DW_CFA_offset_extended_sf;
12822 *p++ = 65;
12823 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
12824 *p++ = DW_CFA_advance_loc + 4;
12825 *p++ = DW_CFA_restore_extended;
12826 *p++ = 65;
12827 }
da44f4e5 12828 /* Pad. */
d4aaa2a0 12829 p = last_fde + last_fde_len + 4;
da44f4e5
AM
12830 }
12831 if (htab->glink != NULL && htab->glink->size != 0)
12832 {
12833 last_fde = p;
2e0ce1c8 12834 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 12835 /* FDE length. */
2e0ce1c8 12836 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12837 p += 4;
12838 /* CIE pointer. */
12839 val = p - htab->glink_eh_frame->contents;
12840 bfd_put_32 (htab->elf.dynobj, val, p);
12841 p += 4;
12842 /* Offset to .glink, written later. */
12843 p += 4;
12844 /* .glink size. */
12845 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12846 p += 4;
12847 /* Augmentation. */
12848 p += 1;
12849
12850 *p++ = DW_CFA_advance_loc + 1;
12851 *p++ = DW_CFA_register;
12852 *p++ = 65;
9f08fa5c 12853 *p++ = htab->opd_abi ? 12 : 0;
15a3a14f 12854 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
da44f4e5
AM
12855 *p++ = DW_CFA_restore_extended;
12856 *p++ = 65;
2e0ce1c8 12857 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
12858 }
12859 /* Subsume any padding into the last FDE if user .eh_frame
12860 sections are aligned more than glink_eh_frame. Otherwise any
12861 zero padding will be seen as a terminator. */
2e0ce1c8 12862 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 12863 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 12864 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
12865 htab->glink_eh_frame->size = size + pad;
12866 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12867 }
12868
d969d15f
AM
12869 maybe_strip_output (info, htab->brlt);
12870 if (htab->glink_eh_frame != NULL)
12871 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12872
b34976b6 12873 return TRUE;
721956f4
AM
12874}
12875
12876/* Called after we have determined section placement. If sections
805fc799 12877 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12878
805fc799 12879bfd_vma
1c865ab2 12880ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12881{
805fc799 12882 asection *s;
a27e685f 12883 bfd_vma TOCstart, adjust;
721956f4 12884
43417696
AM
12885 if (info != NULL)
12886 {
12887 struct elf_link_hash_entry *h;
12888 struct elf_link_hash_table *htab = elf_hash_table (info);
12889
12890 if (is_elf_hash_table (htab)
12891 && htab->hgot != NULL)
12892 h = htab->hgot;
12893 else
12894 {
12895 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12896 if (is_elf_hash_table (htab))
12897 htab->hgot = h;
12898 }
12899 if (h != NULL
12900 && h->root.type == bfd_link_hash_defined
12901 && !h->root.linker_def
12902 && (!is_elf_hash_table (htab)
12903 || h->def_regular))
12904 {
12905 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12906 + h->root.u.def.section->output_offset
12907 + h->root.u.def.section->output_section->vma);
12908 _bfd_set_gp_value (obfd, TOCstart);
12909 return TOCstart;
12910 }
12911 }
12912
805fc799
AM
12913 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12914 order. The TOC starts where the first of these sections starts. */
12915 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12916 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12917 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12918 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12919 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12920 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12921 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12922 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12923 {
12924 /* This may happen for
12925 o references to TOC base (SYM@toc / TOC[tc0]) without a
12926 .toc directive
12927 o bad linker script
12928 o --gc-sections and empty TOC sections
12929
12930 FIXME: Warn user? */
12931
12932 /* Look for a likely section. We probably won't even be
12933 using TOCstart. */
12934 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12935 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12936 | SEC_EXCLUDE))
805fc799
AM
12937 == (SEC_ALLOC | SEC_SMALL_DATA))
12938 break;
721956f4 12939 if (s == NULL)
805fc799 12940 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12941 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12942 == (SEC_ALLOC | SEC_SMALL_DATA))
12943 break;
721956f4 12944 if (s == NULL)
805fc799 12945 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12946 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12947 == SEC_ALLOC)
805fc799 12948 break;
721956f4 12949 if (s == NULL)
805fc799 12950 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12951 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12952 break;
12953 }
721956f4 12954
805fc799
AM
12955 TOCstart = 0;
12956 if (s != NULL)
12957 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12958
a27e685f
AM
12959 /* Force alignment. */
12960 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12961 TOCstart -= adjust;
1c865ab2
AM
12962 _bfd_set_gp_value (obfd, TOCstart);
12963
810d4e75 12964 if (info != NULL && s != NULL)
1c865ab2
AM
12965 {
12966 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12967
810d4e75
AM
12968 if (htab != NULL)
12969 {
12970 if (htab->elf.hgot != NULL)
12971 {
a27e685f 12972 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12973 htab->elf.hgot->root.u.def.section = s;
12974 }
12975 }
12976 else
1c865ab2 12977 {
810d4e75
AM
12978 struct bfd_link_hash_entry *bh = NULL;
12979 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12980 s, TOC_BASE_OFF - adjust,
12981 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12982 }
12983 }
805fc799 12984 return TOCstart;
721956f4
AM
12985}
12986
a345bc8d
AM
12987/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12988 write out any global entry stubs. */
12989
12990static bfd_boolean
12991build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12992{
12993 struct bfd_link_info *info;
12994 struct ppc_link_hash_table *htab;
12995 struct plt_entry *pent;
12996 asection *s;
12997
12998 if (h->root.type == bfd_link_hash_indirect)
12999 return TRUE;
13000
13001 if (!h->pointer_equality_needed)
13002 return TRUE;
13003
13004 if (h->def_regular)
13005 return TRUE;
13006
13007 info = inf;
13008 htab = ppc_hash_table (info);
13009 if (htab == NULL)
13010 return FALSE;
13011
9e390558 13012 s = htab->global_entry;
a345bc8d
AM
13013 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13014 if (pent->plt.offset != (bfd_vma) -1
13015 && pent->addend == 0)
13016 {
13017 bfd_byte *p;
13018 asection *plt;
13019 bfd_vma off;
13020
a345bc8d 13021 p = s->contents + h->root.u.def.value;
33e44f2e 13022 plt = htab->elf.splt;
a345bc8d
AM
13023 if (!htab->elf.dynamic_sections_created
13024 || h->dynindx == -1)
33e44f2e 13025 plt = htab->elf.iplt;
a345bc8d
AM
13026 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
13027 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13028
13029 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13030 {
13031 info->callbacks->einfo
c1c8c1ef 13032 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
13033 h->root.root.string);
13034 bfd_set_error (bfd_error_bad_value);
13035 htab->stub_error = TRUE;
13036 }
13037
7341d5e2
AM
13038 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13039 if (htab->params->emit_stub_syms)
13040 {
13041 size_t len = strlen (h->root.root.string);
13042 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13043
13044 if (name == NULL)
13045 return FALSE;
13046
13047 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13048 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13049 if (h == NULL)
13050 return FALSE;
13051 if (h->root.type == bfd_link_hash_new)
13052 {
13053 h->root.type = bfd_link_hash_defined;
13054 h->root.u.def.section = s;
13055 h->root.u.def.value = p - s->contents;
13056 h->ref_regular = 1;
13057 h->def_regular = 1;
13058 h->ref_regular_nonweak = 1;
13059 h->forced_local = 1;
13060 h->non_elf = 0;
2ec55de3 13061 h->root.linker_def = 1;
7341d5e2
AM
13062 }
13063 }
13064
a345bc8d
AM
13065 if (PPC_HA (off) != 0)
13066 {
13067 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13068 p += 4;
13069 }
13070 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13071 p += 4;
13072 bfd_put_32 (s->owner, MTCTR_R12, p);
13073 p += 4;
407aa07c 13074 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
13075 break;
13076 }
13077 return TRUE;
13078}
13079
721956f4
AM
13080/* Build all the stubs associated with the current output file.
13081 The stubs are kept in a hash table attached to the main linker
13082 hash table. This function is called via gldelf64ppc_finish. */
13083
b34976b6 13084bfd_boolean
e7d1c40c 13085ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13086 char **stats)
5d1634d7
AM
13087{
13088 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13089 struct map_stub *group;
721956f4 13090 asection *stub_sec;
5d1634d7 13091 bfd_byte *p;
e717da7e 13092 int stub_sec_count = 0;
5d1634d7 13093
4dfe6ac6
NC
13094 if (htab == NULL)
13095 return FALSE;
13096
eea6121a 13097 /* Allocate memory to hold the linker stubs. */
d4aaa2a0
AM
13098 for (group = htab->group; group != NULL; group = group->next)
13099 if ((stub_sec = group->stub_sec) != NULL
eea6121a 13100 && stub_sec->size != 0)
e717da7e 13101 {
e7d1c40c 13102 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
13103 if (stub_sec->contents == NULL)
13104 return FALSE;
eea6121a 13105 stub_sec->size = 0;
e717da7e 13106 }
5d1634d7 13107
23eb7e01 13108 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13109 {
9f951329 13110 unsigned int indx;
ad8e1ba5 13111 bfd_vma plt0;
9f951329 13112
721956f4 13113 /* Build the .glink plt call stub. */
e7d1c40c 13114 if (htab->params->emit_stub_syms)
97b639ba
AM
13115 {
13116 struct elf_link_hash_entry *h;
468392fb
AM
13117 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13118 TRUE, FALSE, FALSE);
97b639ba
AM
13119 if (h == NULL)
13120 return FALSE;
13121 if (h->root.type == bfd_link_hash_new)
13122 {
13123 h->root.type = bfd_link_hash_defined;
13124 h->root.u.def.section = htab->glink;
ee4bf8d2 13125 h->root.u.def.value = 8;
f5385ebf
AM
13126 h->ref_regular = 1;
13127 h->def_regular = 1;
13128 h->ref_regular_nonweak = 1;
13129 h->forced_local = 1;
13130 h->non_elf = 0;
2ec55de3 13131 h->root.linker_def = 1;
97b639ba
AM
13132 }
13133 }
33e44f2e
AM
13134 plt0 = (htab->elf.splt->output_section->vma
13135 + htab->elf.splt->output_offset
13136 - 16);
176a0d42
AM
13137 if (info->emitrelocations)
13138 {
13139 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13140 if (r == NULL)
13141 return FALSE;
13142 r->r_offset = (htab->glink->output_offset
13143 + htab->glink->output_section->vma);
13144 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13145 r->r_addend = plt0;
13146 }
4ce794b7 13147 p = htab->glink->contents;
176a0d42 13148 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13149 bfd_put_64 (htab->glink->owner, plt0, p);
13150 p += 8;
b9e5796b
AM
13151 if (htab->opd_abi)
13152 {
13153 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13154 p += 4;
13155 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13156 p += 4;
13157 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13158 p += 4;
13159 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13160 p += 4;
13161 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13162 p += 4;
13163 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13164 p += 4;
13165 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13166 p += 4;
13167 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13168 p += 4;
13169 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13170 p += 4;
13171 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13172 p += 4;
13173 }
13174 else
13175 {
13176 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13177 p += 4;
13178 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13179 p += 4;
13180 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13181 p += 4;
f378ab09
AM
13182 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13183 p += 4;
b9e5796b
AM
13184 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13185 p += 4;
13186 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13187 p += 4;
13188 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13189 p += 4;
13190 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13191 p += 4;
13192 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13193 p += 4;
13194 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13195 p += 4;
13196 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13197 p += 4;
13198 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13199 p += 4;
13200 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13201 p += 4;
13202 }
407aa07c
AM
13203 bfd_put_32 (htab->glink->owner, BCTR, p);
13204 p += 4;
c75bc4f7 13205 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 13206
9f951329
AM
13207 /* Build the .glink lazy link call stubs. */
13208 indx = 0;
9e390558 13209 while (p < htab->glink->contents + htab->glink->size)
9f951329 13210 {
b9e5796b 13211 if (htab->opd_abi)
9f951329 13212 {
b9e5796b
AM
13213 if (indx < 0x8000)
13214 {
13215 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13216 p += 4;
13217 }
13218 else
13219 {
13220 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13221 p += 4;
13222 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13223 p);
13224 p += 4;
13225 }
9f951329 13226 }
4ce794b7 13227 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13228 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13229 indx++;
9f951329
AM
13230 p += 4;
13231 }
5d1634d7 13232 }
5d1634d7 13233
9e390558
AM
13234 /* Build .glink global entry stubs. */
13235 if (htab->global_entry != NULL && htab->global_entry->size != 0)
13236 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13237
7341d5e2 13238 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13239 {
4ce794b7 13240 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13241 htab->brlt->size);
4ce794b7 13242 if (htab->brlt->contents == NULL)
b34976b6 13243 return FALSE;
721956f4 13244 }
ee75fd95 13245 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13246 {
13247 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13248 htab->relbrlt->size);
63bc6f6c
AM
13249 if (htab->relbrlt->contents == NULL)
13250 return FALSE;
13251 }
5d1634d7 13252
721956f4
AM
13253 /* Build the stubs as directed by the stub hash table. */
13254 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13255
a4b6fadd
AM
13256 for (group = htab->group; group != NULL; group = group->next)
13257 if (group->needs_save_res)
7dda8d3c 13258 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 13259
aa8a7074
AM
13260 if (htab->relbrlt != NULL)
13261 htab->relbrlt->reloc_count = 0;
13262
e7d1c40c 13263 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13264 for (group = htab->group; group != NULL; group = group->next)
13265 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
13266 {
13267 int align = abs (htab->params->plt_stub_align);
13268 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13269 }
794e51c0 13270
7dda8d3c
AM
13271 for (group = htab->group; group != NULL; group = group->next)
13272 if (group->needs_save_res)
13273 {
13274 stub_sec = group->stub_sec;
13275 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13276 htab->sfpr->contents, htab->sfpr->size);
13277 if (htab->params->emit_stub_syms)
13278 {
13279 unsigned int i;
13280
13281 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13282 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13283 return FALSE;
13284 }
13285 }
13286
d4aaa2a0
AM
13287 for (group = htab->group; group != NULL; group = group->next)
13288 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
13289 {
13290 stub_sec_count += 1;
c9301e31
AM
13291 if (stub_sec->rawsize != stub_sec->size
13292 && (htab->stub_iteration <= STUB_SHRINK_ITER
13293 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13294 break;
13295 }
5d1634d7 13296
25516cc5 13297 if (group != NULL)
5d1634d7 13298 {
b34976b6 13299 htab->stub_error = TRUE;
cf97bcb0 13300 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 13301 }
721956f4 13302
d2a300cf
AM
13303 if (htab->stub_error)
13304 return FALSE;
13305
13306 if (stats != NULL)
13307 {
db84fff3 13308 size_t len;
d2a300cf
AM
13309 *stats = bfd_malloc (500);
13310 if (*stats == NULL)
13311 return FALSE;
13312
db84fff3
AM
13313 len = sprintf (*stats,
13314 ngettext ("linker stubs in %u group\n",
13315 "linker stubs in %u groups\n",
13316 stub_sec_count),
13317 stub_sec_count);
13318 sprintf (*stats + len, _(" branch %lu\n"
13319 " toc adjust %lu\n"
13320 " long branch %lu\n"
13321 " long toc adj %lu\n"
13322 " plt call %lu\n"
13323 " plt call toc %lu\n"
13324 " global entry %lu"),
4ce794b7
AM
13325 htab->stub_count[ppc_stub_long_branch - 1],
13326 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13327 htab->stub_count[ppc_stub_plt_branch - 1],
13328 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13329 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13330 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13331 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13332 }
13333 return TRUE;
5bd4f169
AM
13334}
13335
60124e18
AM
13336/* What to do when ld finds relocations against symbols defined in
13337 discarded sections. */
13338
13339static unsigned int
13340ppc64_elf_action_discarded (asection *sec)
13341{
13342 if (strcmp (".opd", sec->name) == 0)
13343 return 0;
13344
13345 if (strcmp (".toc", sec->name) == 0)
13346 return 0;
13347
bce50a28
JJ
13348 if (strcmp (".toc1", sec->name) == 0)
13349 return 0;
13350
60124e18
AM
13351 return _bfd_elf_default_action_discarded (sec);
13352}
13353
5bd4f169
AM
13354/* The RELOCATE_SECTION function is called by the ELF backend linker
13355 to handle the relocations for a section.
13356
13357 The relocs are always passed as Rela structures; if the section
13358 actually uses Rel structures, the r_addend field will always be
13359 zero.
13360
13361 This function is responsible for adjust the section contents as
13362 necessary, and (if using Rela relocs and generating a
1049f94e 13363 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13364 necessary.
13365
13366 This function does not have to worry about setting the reloc
13367 address or the reloc symbol index.
13368
13369 LOCAL_SYMS is a pointer to the swapped in local symbols.
13370
13371 LOCAL_SECTIONS is an array giving the section in the input file
13372 corresponding to the st_shndx field of each local symbol.
13373
13374 The global hash table entry for the global symbols can be found
13375 via elf_sym_hashes (input_bfd).
13376
1049f94e 13377 When generating relocatable output, this function must handle
5bd4f169
AM
13378 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13379 going to be the section symbol corresponding to the output
13380 section, which means that the addend must be adjusted
13381 accordingly. */
13382
b34976b6 13383static bfd_boolean
4ce794b7
AM
13384ppc64_elf_relocate_section (bfd *output_bfd,
13385 struct bfd_link_info *info,
13386 bfd *input_bfd,
13387 asection *input_section,
13388 bfd_byte *contents,
13389 Elf_Internal_Rela *relocs,
13390 Elf_Internal_Sym *local_syms,
13391 asection **local_sections)
5bd4f169 13392{
65f38f15 13393 struct ppc_link_hash_table *htab;
5bd4f169
AM
13394 Elf_Internal_Shdr *symtab_hdr;
13395 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13396 Elf_Internal_Rela *rel;
c316a17c 13397 Elf_Internal_Rela *wrel;
5bd4f169 13398 Elf_Internal_Rela *relend;
411e1bfb
AM
13399 Elf_Internal_Rela outrel;
13400 bfd_byte *loc;
411e1bfb 13401 struct got_entry **local_got_ents;
5bd4f169 13402 bfd_vma TOCstart;
b34976b6
AM
13403 bfd_boolean ret = TRUE;
13404 bfd_boolean is_opd;
794e51c0
AM
13405 /* Assume 'at' branch hints. */
13406 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13407 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13408
65f38f15 13409 /* Initialize howto table if needed. */
5bd4f169 13410 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13411 ppc_howto_init ();
13412
65f38f15 13413 htab = ppc_hash_table (info);
4dfe6ac6
NC
13414 if (htab == NULL)
13415 return FALSE;
ee75fd95
AM
13416
13417 /* Don't relocate stub sections. */
e7d1c40c 13418 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13419 return TRUE;
13420
0c8d6e5c 13421 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13422
411e1bfb 13423 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13424 TOCstart = elf_gp (output_bfd);
0ffa91dd 13425 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13426 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13427 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13428
c316a17c 13429 rel = wrel = relocs;
5bd4f169 13430 relend = relocs + input_section->reloc_count;
c316a17c 13431 for (; rel < relend; wrel++, rel++)
5bd4f169 13432 {
04c9666a 13433 enum elf_ppc64_reloc_type r_type;
31c76678 13434 bfd_vma addend;
5bd4f169
AM
13435 bfd_reloc_status_type r;
13436 Elf_Internal_Sym *sym;
13437 asection *sec;
039b3fef
AM
13438 struct elf_link_hash_entry *h_elf;
13439 struct ppc_link_hash_entry *h;
13440 struct ppc_link_hash_entry *fdh;
5bd4f169 13441 const char *sym_name;
0d4792f7 13442 unsigned long r_symndx, toc_symndx;
3a71aa26 13443 bfd_vma toc_addend;
f961d9dd
AM
13444 unsigned char tls_mask, tls_gd, tls_type;
13445 unsigned char sym_type;
5bd4f169 13446 bfd_vma relocation;
b34976b6
AM
13447 bfd_boolean unresolved_reloc;
13448 bfd_boolean warned;
bc30df16 13449 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13450 unsigned int insn;
e11840f9 13451 unsigned int mask;
721956f4
AM
13452 struct ppc_stub_hash_entry *stub_entry;
13453 bfd_vma max_br_offset;
13454 bfd_vma from;
c316a17c 13455 Elf_Internal_Rela orig_rel;
b80eed39
AM
13456 reloc_howto_type *howto;
13457 struct reloc_howto_struct alt_howto;
5bd4f169 13458
c316a17c
AM
13459 again:
13460 orig_rel = *rel;
13461
4ce794b7 13462 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13463 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13464
13465 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13466 symbol of the previous ADDR64 reloc. The symbol gives us the
13467 proper TOC base to use. */
13468 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13469 && wrel != relocs
13470 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13471 && is_opd)
c316a17c 13472 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13473
4ce794b7
AM
13474 sym = NULL;
13475 sec = NULL;
039b3fef 13476 h_elf = NULL;
4ce794b7 13477 sym_name = NULL;
b34976b6
AM
13478 unresolved_reloc = FALSE;
13479 warned = FALSE;
65f38f15 13480
0b13192e 13481 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13482 {
13483 /* It's a local symbol. */
74f0fb50 13484 struct _opd_sec_data *opd;
4025353c 13485
5bd4f169
AM
13486 sym = local_syms + r_symndx;
13487 sec = local_sections[r_symndx];
26c61ae5 13488 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13489 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13490 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13491 opd = get_opd_info (sec);
13492 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13493 {
51aecdc5
AM
13494 long adjust = opd->adjust[OPD_NDX (sym->st_value
13495 + rel->r_addend)];
4025353c
AM
13496 if (adjust == -1)
13497 relocation = 0;
13498 else
4cc603a5
AM
13499 {
13500 /* If this is a relocation against the opd section sym
13501 and we have edited .opd, adjust the reloc addend so
13502 that ld -r and ld --emit-relocs output is correct.
13503 If it is a reloc against some other .opd symbol,
13504 then the symbol value will be adjusted later. */
13505 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13506 rel->r_addend += adjust;
13507 else
13508 relocation += adjust;
13509 }
1e2f5b6e 13510 }
5bd4f169
AM
13511 }
13512 else
13513 {
62d887d4
L
13514 bfd_boolean ignored;
13515
b2a8e766
AM
13516 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13517 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13518 h_elf, sec, relocation,
62d887d4 13519 unresolved_reloc, warned, ignored);
039b3fef
AM
13520 sym_name = h_elf->root.root.string;
13521 sym_type = h_elf->type;
b69fdb4e
AM
13522 if (sec != NULL
13523 && sec->owner == output_bfd
13524 && strcmp (sec->name, ".opd") == 0)
13525 {
13526 /* This is a symbol defined in a linker script. All
13527 such are defined in output sections, even those
13528 defined by simple assignment from a symbol defined in
13529 an input section. Transfer the symbol to an
13530 appropriate input .opd section, so that a branch to
13531 this symbol will be mapped to the location specified
13532 by the opd entry. */
13533 struct bfd_link_order *lo;
13534 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13535 if (lo->type == bfd_indirect_link_order)
13536 {
13537 asection *isec = lo->u.indirect.section;
13538 if (h_elf->root.u.def.value >= isec->output_offset
13539 && h_elf->root.u.def.value < (isec->output_offset
13540 + isec->size))
13541 {
13542 h_elf->root.u.def.value -= isec->output_offset;
13543 h_elf->root.u.def.section = isec;
13544 sec = isec;
13545 break;
13546 }
13547 }
13548 }
5bd4f169 13549 }
039b3fef 13550 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13551
dbaa2011 13552 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13553 {
13554 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13555 input_bfd, input_section,
13556 contents + rel->r_offset);
13557 wrel->r_offset = rel->r_offset;
13558 wrel->r_info = 0;
13559 wrel->r_addend = 0;
13560
13561 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 13562 symbols defined in discarded sections. Not done for
c316a17c
AM
13563 non-debug to preserve relocs in .eh_frame which the
13564 eh_frame editing code expects to be present. */
13565 if (bfd_link_relocatable (info)
13566 && (input_section->flags & SEC_DEBUGGING))
13567 wrel--;
13568
13569 continue;
13570 }
ab96bf03 13571
0e1862bb 13572 if (bfd_link_relocatable (info))
c316a17c 13573 goto copy_reloc;
ab96bf03 13574
f40da81b
AM
13575 if (h != NULL && &h->elf == htab->elf.hgot)
13576 {
6f20ed8a 13577 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13578 sec = bfd_abs_section_ptr;
13579 unresolved_reloc = FALSE;
13580 }
13581
951fd09b
AM
13582 /* TLS optimizations. Replace instruction sequences and relocs
13583 based on information we collected in tls_optimize. We edit
13584 RELOCS so that --emit-relocs will output something sensible
13585 for the final instruction stream. */
13586 tls_mask = 0;
13587 tls_gd = 0;
0d4792f7 13588 toc_symndx = 0;
727fc41e
AM
13589 if (h != NULL)
13590 tls_mask = h->tls_mask;
13591 else if (local_got_ents != NULL)
411e1bfb 13592 {
e054468f
AM
13593 struct plt_entry **local_plt = (struct plt_entry **)
13594 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13595 unsigned char *lgot_masks = (unsigned char *)
e054468f 13596 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13597 tls_mask = lgot_masks[r_symndx];
13598 }
13599 if (tls_mask == 0
13600 && (r_type == R_PPC64_TLS
13601 || r_type == R_PPC64_TLSGD
13602 || r_type == R_PPC64_TLSLD))
13603 {
13604 /* Check for toc tls entries. */
f961d9dd 13605 unsigned char *toc_tls;
0d4792f7 13606
727fc41e
AM
13607 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13608 &local_syms, rel, input_bfd))
13609 return FALSE;
0d4792f7 13610
727fc41e
AM
13611 if (toc_tls)
13612 tls_mask = *toc_tls;
0d4792f7
AM
13613 }
13614
13615 /* Check that tls relocs are used with tls syms, and non-tls
13616 relocs are used with non-tls syms. */
cf35638d 13617 if (r_symndx != STN_UNDEF
0d4792f7
AM
13618 && r_type != R_PPC64_NONE
13619 && (h == NULL
039b3fef
AM
13620 || h->elf.root.type == bfd_link_hash_defined
13621 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13622 && (IS_PPC64_TLS_RELOC (r_type)
13623 != (sym_type == STT_TLS
13624 || (sym_type == STT_SECTION
13625 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13626 {
727fc41e
AM
13627 if (tls_mask != 0
13628 && (r_type == R_PPC64_TLS
13629 || r_type == R_PPC64_TLSGD
13630 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13631 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13632 ;
13633 else
25f53a85 13634 info->callbacks->einfo
1d483afe 13635 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 13636 /* xgettext:c-format */
c1c8c1ef 13637 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 13638 /* xgettext:c-format */
c1c8c1ef 13639 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 13640 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13641 ppc64_elf_howto_table[r_type]->name,
13642 sym_name);
411e1bfb
AM
13643 }
13644
13645 /* Ensure reloc mapping code below stays sane. */
13646 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13647 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13648 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13649 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13650 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13651 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13652 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13653 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13654 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13655 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13656 abort ();
0d4792f7 13657
411e1bfb
AM
13658 switch (r_type)
13659 {
13660 default:
411e1bfb
AM
13661 break;
13662
ba761f19 13663 case R_PPC64_LO_DS_OPT:
95f0d0d2 13664 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
13665 if ((insn & (0x3f << 26)) != 58u << 26)
13666 abort ();
13667 insn += (14u << 26) - (58u << 26);
95f0d0d2 13668 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
13669 r_type = R_PPC64_TOC16_LO;
13670 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13671 break;
13672
411e1bfb
AM
13673 case R_PPC64_TOC16:
13674 case R_PPC64_TOC16_LO:
13675 case R_PPC64_TOC16_DS:
13676 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13677 {
13678 /* Check for toc tls entries. */
f961d9dd 13679 unsigned char *toc_tls;
951fd09b 13680 int retval;
411e1bfb 13681
3a71aa26
AM
13682 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13683 &local_syms, rel, input_bfd);
951fd09b 13684 if (retval == 0)
411e1bfb
AM
13685 return FALSE;
13686
13687 if (toc_tls)
13688 {
951fd09b 13689 tls_mask = *toc_tls;
411e1bfb
AM
13690 if (r_type == R_PPC64_TOC16_DS
13691 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
13692 {
13693 if (tls_mask != 0
13694 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13695 goto toctprel;
13696 }
411e1bfb 13697 else
951fd09b
AM
13698 {
13699 /* If we found a GD reloc pair, then we might be
13700 doing a GD->IE transition. */
13701 if (retval == 2)
13702 {
13703 tls_gd = TLS_TPRELGD;
13704 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13705 goto tls_ldgd_opt;
951fd09b
AM
13706 }
13707 else if (retval == 3)
13708 {
13709 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 13710 goto tls_ldgd_opt;
951fd09b
AM
13711 }
13712 }
411e1bfb
AM
13713 }
13714 }
13715 break;
13716
9d6ded02
AM
13717 case R_PPC64_GOT_TPREL16_HI:
13718 case R_PPC64_GOT_TPREL16_HA:
13719 if (tls_mask != 0
13720 && (tls_mask & TLS_TPREL) == 0)
13721 {
13722 rel->r_offset -= d_offset;
95f0d0d2 13723 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
13724 r_type = R_PPC64_NONE;
13725 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13726 }
13727 break;
13728
411e1bfb
AM
13729 case R_PPC64_GOT_TPREL16_DS:
13730 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13731 if (tls_mask != 0
13732 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13733 {
81407a69 13734 toctprel:
95f0d0d2 13735 insn = bfd_get_32 (input_bfd,
c316a17c 13736 contents + rel->r_offset - d_offset);
411e1bfb
AM
13737 insn &= 31 << 21;
13738 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 13739 bfd_put_32 (input_bfd, insn,
c316a17c 13740 contents + rel->r_offset - d_offset);
411e1bfb 13741 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13742 if (toc_symndx != 0)
13743 {
13744 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13745 rel->r_addend = toc_addend;
0d4792f7
AM
13746 /* We changed the symbol. Start over in order to
13747 get h, sym, sec etc. right. */
c316a17c 13748 goto again;
0d4792f7
AM
13749 }
13750 else
13751 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13752 }
13753 break;
13754
13755 case R_PPC64_TLS:
951fd09b
AM
13756 if (tls_mask != 0
13757 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13758 {
95f0d0d2 13759 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
13760 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13761 if (insn == 0)
411e1bfb 13762 abort ();
95f0d0d2 13763 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 13764 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13765 PPC64_TPREL16_LO which is at low-order half-word. */
13766 rel->r_offset += d_offset;
0d4792f7
AM
13767 r_type = R_PPC64_TPREL16_LO;
13768 if (toc_symndx != 0)
13769 {
13770 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13771 rel->r_addend = toc_addend;
0d4792f7
AM
13772 /* We changed the symbol. Start over in order to
13773 get h, sym, sec etc. right. */
c316a17c 13774 goto again;
0d4792f7
AM
13775 }
13776 else
13777 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13778 }
13779 break;
13780
411e1bfb
AM
13781 case R_PPC64_GOT_TLSGD16_HI:
13782 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13783 tls_gd = TLS_TPRELGD;
13784 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13785 goto tls_gdld_hi;
13786 break;
13787
411e1bfb
AM
13788 case R_PPC64_GOT_TLSLD16_HI:
13789 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13790 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13791 {
951fd09b
AM
13792 tls_gdld_hi:
13793 if ((tls_mask & tls_gd) != 0)
13794 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13795 + R_PPC64_GOT_TPREL16_DS);
13796 else
411e1bfb 13797 {
4fe5ca5b 13798 rel->r_offset -= d_offset;
95f0d0d2 13799 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 13800 r_type = R_PPC64_NONE;
411e1bfb 13801 }
951fd09b 13802 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13803 }
13804 break;
13805
951fd09b
AM
13806 case R_PPC64_GOT_TLSGD16:
13807 case R_PPC64_GOT_TLSGD16_LO:
13808 tls_gd = TLS_TPRELGD;
13809 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13810 goto tls_ldgd_opt;
951fd09b 13811 break;
411e1bfb 13812
951fd09b
AM
13813 case R_PPC64_GOT_TLSLD16:
13814 case R_PPC64_GOT_TLSLD16_LO:
13815 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13816 {
b9f04fe0 13817 unsigned int insn1, insn2;
102890f0
AM
13818 bfd_vma offset;
13819
13820 tls_ldgd_opt:
727fc41e
AM
13821 offset = (bfd_vma) -1;
13822 /* If not using the newer R_PPC64_TLSGD/LD to mark
13823 __tls_get_addr calls, we must trust that the call
13824 stays with its arg setup insns, ie. that the next
13825 reloc is the __tls_get_addr call associated with
13826 the current reloc. Edit both insns. */
13827 if (input_section->has_tls_get_addr_call
13828 && rel + 1 < relend
13829 && branch_reloc_hash_match (input_bfd, rel + 1,
13830 htab->tls_get_addr,
13831 htab->tls_get_addr_fd))
13832 offset = rel[1].r_offset;
b86ac8e3
AM
13833 /* We read the low GOT_TLS (or TOC16) insn because we
13834 need to keep the destination reg. It may be
13835 something other than the usual r3, and moved to r3
13836 before the call by intervening code. */
95f0d0d2 13837 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 13838 contents + rel->r_offset - d_offset);
102890f0 13839 if ((tls_mask & tls_gd) != 0)
411e1bfb 13840 {
102890f0 13841 /* IE */
b86ac8e3 13842 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13843 insn1 |= 58 << 26; /* ld */
13844 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13845 if (offset != (bfd_vma) -1)
f58d5a2d 13846 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13847 if ((tls_mask & TLS_EXPLICIT) == 0)
13848 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13849 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13850 else
102890f0
AM
13851 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13852 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13853 }
13854 else
13855 {
13856 /* LE */
b86ac8e3
AM
13857 insn1 &= 0x1f << 21;
13858 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13859 insn2 = 0x38630000; /* addi 3,3,0 */
13860 if (tls_gd == 0)
951fd09b 13861 {
102890f0 13862 /* Was an LD reloc. */
1d483afe
AM
13863 if (toc_symndx)
13864 sec = local_sections[toc_symndx];
13865 for (r_symndx = 0;
13866 r_symndx < symtab_hdr->sh_info;
13867 r_symndx++)
13868 if (local_sections[r_symndx] == sec)
13869 break;
13870 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13871 r_symndx = STN_UNDEF;
102890f0 13872 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13873 if (r_symndx != STN_UNDEF)
1d483afe
AM
13874 rel->r_addend -= (local_syms[r_symndx].st_value
13875 + sec->output_offset
13876 + sec->output_section->vma);
951fd09b 13877 }
102890f0 13878 else if (toc_symndx != 0)
3a71aa26
AM
13879 {
13880 r_symndx = toc_symndx;
13881 rel->r_addend = toc_addend;
13882 }
102890f0
AM
13883 r_type = R_PPC64_TPREL16_HA;
13884 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13885 if (offset != (bfd_vma) -1)
13886 {
13887 rel[1].r_info = ELF64_R_INFO (r_symndx,
13888 R_PPC64_TPREL16_LO);
13889 rel[1].r_offset = offset + d_offset;
13890 rel[1].r_addend = rel->r_addend;
13891 }
102890f0 13892 }
95f0d0d2 13893 bfd_put_32 (input_bfd, insn1,
3a71aa26 13894 contents + rel->r_offset - d_offset);
727fc41e 13895 if (offset != (bfd_vma) -1)
b9f04fe0 13896 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
13897 if ((tls_mask & tls_gd) == 0
13898 && (tls_gd == 0 || toc_symndx != 0))
13899 {
13900 /* We changed the symbol. Start over in order
13901 to get h, sym, sec etc. right. */
c316a17c 13902 goto again;
727fc41e
AM
13903 }
13904 }
13905 break;
13906
13907 case R_PPC64_TLSGD:
675e2809
AM
13908 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0
13909 && rel + 1 < relend)
727fc41e 13910 {
b9f04fe0 13911 unsigned int insn2;
727fc41e
AM
13912 bfd_vma offset = rel->r_offset;
13913
13914 if ((tls_mask & TLS_TPRELGD) != 0)
13915 {
13916 /* IE */
13917 r_type = R_PPC64_NONE;
13918 insn2 = 0x7c636a14; /* add 3,3,13 */
13919 }
13920 else
13921 {
13922 /* LE */
13923 if (toc_symndx != 0)
13924 {
13925 r_symndx = toc_symndx;
13926 rel->r_addend = toc_addend;
13927 }
13928 r_type = R_PPC64_TPREL16_LO;
13929 rel->r_offset = offset + d_offset;
13930 insn2 = 0x38630000; /* addi 3,3,0 */
13931 }
13932 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13933 /* Zap the reloc on the _tls_get_addr call too. */
13934 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13935 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 13936 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 13937 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13938 goto again;
411e1bfb 13939 }
411e1bfb
AM
13940 break;
13941
727fc41e 13942 case R_PPC64_TLSLD:
675e2809
AM
13943 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0
13944 && rel + 1 < relend)
727fc41e 13945 {
b9f04fe0 13946 unsigned int insn2;
727fc41e
AM
13947 bfd_vma offset = rel->r_offset;
13948
13949 if (toc_symndx)
13950 sec = local_sections[toc_symndx];
13951 for (r_symndx = 0;
13952 r_symndx < symtab_hdr->sh_info;
13953 r_symndx++)
13954 if (local_sections[r_symndx] == sec)
13955 break;
13956 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13957 r_symndx = STN_UNDEF;
727fc41e 13958 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13959 if (r_symndx != STN_UNDEF)
727fc41e
AM
13960 rel->r_addend -= (local_syms[r_symndx].st_value
13961 + sec->output_offset
13962 + sec->output_section->vma);
13963
13964 r_type = R_PPC64_TPREL16_LO;
13965 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13966 rel->r_offset = offset + d_offset;
13967 /* Zap the reloc on the _tls_get_addr call too. */
13968 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13969 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 13970 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 13971 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 13972 goto again;
727fc41e
AM
13973 }
13974 break;
13975
411e1bfb 13976 case R_PPC64_DTPMOD64:
951fd09b
AM
13977 if (rel + 1 < relend
13978 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13979 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13980 {
951fd09b
AM
13981 if ((tls_mask & TLS_GD) == 0)
13982 {
13983 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13984 if ((tls_mask & TLS_TPRELGD) != 0)
13985 r_type = R_PPC64_TPREL64;
13986 else
13987 {
4ce794b7 13988 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13989 r_type = R_PPC64_NONE;
13990 }
13991 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13992 }
13993 }
13994 else
13995 {
13996 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13997 {
4ce794b7 13998 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13999 r_type = R_PPC64_NONE;
951fd09b 14000 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14001 }
411e1bfb
AM
14002 }
14003 break;
14004
14005 case R_PPC64_TPREL64:
951fd09b 14006 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14007 {
14008 r_type = R_PPC64_NONE;
14009 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14010 }
14011 break;
52a82034 14012
006589cf
AM
14013 case R_PPC64_ENTRY:
14014 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14015 if (!bfd_link_pic (info)
14016 && !info->traditional_format
14017 && relocation + 0x80008000 <= 0xffffffff)
14018 {
14019 unsigned int insn1, insn2;
14020
14021 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14022 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14023 if ((insn1 & ~0xfffc) == LD_R2_0R12
14024 && insn2 == ADD_R2_R2_R12)
14025 {
95f0d0d2 14026 bfd_put_32 (input_bfd,
006589cf
AM
14027 LIS_R2 + PPC_HA (relocation),
14028 contents + rel->r_offset);
95f0d0d2 14029 bfd_put_32 (input_bfd,
006589cf
AM
14030 ADDI_R2_R2 + PPC_LO (relocation),
14031 contents + rel->r_offset + 4);
14032 }
14033 }
14034 else
14035 {
14036 relocation -= (rel->r_offset
14037 + input_section->output_offset
14038 + input_section->output_section->vma);
14039 if (relocation + 0x80008000 <= 0xffffffff)
14040 {
14041 unsigned int insn1, insn2;
14042
14043 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14044 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14045 if ((insn1 & ~0xfffc) == LD_R2_0R12
14046 && insn2 == ADD_R2_R2_R12)
14047 {
95f0d0d2 14048 bfd_put_32 (input_bfd,
006589cf
AM
14049 ADDIS_R2_R12 + PPC_HA (relocation),
14050 contents + rel->r_offset);
95f0d0d2 14051 bfd_put_32 (input_bfd,
006589cf
AM
14052 ADDI_R2_R2 + PPC_LO (relocation),
14053 contents + rel->r_offset + 4);
14054 }
14055 }
14056 }
14057 break;
14058
52a82034
AM
14059 case R_PPC64_REL16_HA:
14060 /* If we are generating a non-PIC executable, edit
14061 . 0: addis 2,12,.TOC.-0b@ha
14062 . addi 2,2,.TOC.-0b@l
14063 used by ELFv2 global entry points to set up r2, to
14064 . lis 2,.TOC.@ha
14065 . addi 2,2,.TOC.@l
14066 if .TOC. is in range. */
0e1862bb 14067 if (!bfd_link_pic (info)
810d4e75 14068 && !info->traditional_format
006589cf 14069 && !htab->opd_abi
4f038ee5 14070 && rel->r_addend == d_offset
52a82034
AM
14071 && h != NULL && &h->elf == htab->elf.hgot
14072 && rel + 1 < relend
14073 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14074 && rel[1].r_offset == rel->r_offset + 4
14075 && rel[1].r_addend == rel->r_addend + 4
14076 && relocation + 0x80008000 <= 0xffffffff)
14077 {
14078 unsigned int insn1, insn2;
14079 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14080 insn1 = bfd_get_32 (input_bfd, contents + offset);
14081 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14082 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14083 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14084 {
14085 r_type = R_PPC64_ADDR16_HA;
14086 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14087 rel->r_addend -= d_offset;
14088 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14089 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14090 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14091 }
14092 }
14093 break;
411e1bfb
AM
14094 }
14095
14096 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14097 insn = 0;
b25116a9
AM
14098 max_br_offset = 1 << 25;
14099 addend = rel->r_addend;
bc30df16 14100 reloc_dest = DEST_NORMAL;
65f38f15 14101 switch (r_type)
5bd4f169
AM
14102 {
14103 default:
65f38f15 14104 break;
5bd4f169 14105
3b421ab3
AM
14106 case R_PPC64_TOCSAVE:
14107 if (relocation + addend == (rel->r_offset
14108 + input_section->output_offset
14109 + input_section->output_section->vma)
14110 && tocsave_find (htab, NO_INSERT,
14111 &local_syms, rel, input_bfd))
14112 {
14113 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14114 if (insn == NOP
14115 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14116 bfd_put_32 (input_bfd,
14117 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14118 contents + rel->r_offset);
14119 }
14120 break;
14121
65f38f15
AM
14122 /* Branch taken prediction relocations. */
14123 case R_PPC64_ADDR14_BRTAKEN:
14124 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14125 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14126 /* Fall through. */
65f38f15 14127
86c76c7b 14128 /* Branch not taken prediction relocations. */
65f38f15
AM
14129 case R_PPC64_ADDR14_BRNTAKEN:
14130 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14131 insn |= bfd_get_32 (input_bfd,
411e1bfb 14132 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14133 /* Fall through. */
86c76c7b 14134
b25116a9
AM
14135 case R_PPC64_REL14:
14136 max_br_offset = 1 << 15;
1a0670f3 14137 /* Fall through. */
5bd4f169 14138
65f38f15 14139 case R_PPC64_REL24:
ad8e1ba5
AM
14140 /* Calls to functions with a different TOC, such as calls to
14141 shared objects, need to alter the TOC pointer. This is
14142 done using a linkage stub. A REL24 branching to these
14143 linkage stubs needs to be followed by a nop, as the nop
14144 will be replaced with an instruction to restore the TOC
14145 base pointer. */
8387904d 14146 fdh = h;
b31867b6
AM
14147 if (h != NULL
14148 && h->oh != NULL
14149 && h->oh->is_func_descriptor)
14150 fdh = ppc_follow_link (h->oh);
31c76678
DK
14151 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14152 htab);
6abec6bc 14153 if (stub_entry != NULL
ad8e1ba5 14154 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 14155 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
14156 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14157 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 14158 {
b25116a9 14159 bfd_boolean can_plt_call = FALSE;
721956f4 14160
6e1816be
AM
14161 if (stub_entry->stub_type == ppc_stub_plt_call
14162 && !htab->opd_abi
14163 && htab->params->plt_localentry0 != 0
14164 && is_elfv2_localentry0 (&h->elf))
14165 {
14166 /* The function doesn't use or change r2. */
14167 can_plt_call = TRUE;
14168 }
14169
f378ab09 14170 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
14171 branch and link followed by a nop. The nop is
14172 replaced by an insn to restore r2. */
6e1816be 14173 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14174 {
ba8ca3e7
AM
14175 unsigned long br;
14176
14177 br = bfd_get_32 (input_bfd,
14178 contents + rel->r_offset);
14179 if ((br & 1) != 0)
41bd81ab 14180 {
ba8ca3e7
AM
14181 unsigned long nop;
14182
14183 nop = bfd_get_32 (input_bfd,
14184 contents + rel->r_offset + 4);
14185 if (nop == NOP
14186 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 14187 {
ba8ca3e7
AM
14188 if (h != NULL
14189 && (h == htab->tls_get_addr_fd
14190 || h == htab->tls_get_addr)
7c9cf415 14191 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14192 {
14193 /* Special stub used, leave nop alone. */
14194 }
14195 else
a078d95a
AM
14196 bfd_put_32 (input_bfd,
14197 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14198 contents + rel->r_offset + 4);
14199 can_plt_call = TRUE;
a7f2871e 14200 }
41bd81ab 14201 }
5bd4f169 14202 }
721956f4 14203
ba8ca3e7 14204 if (!can_plt_call && h != NULL)
721956f4 14205 {
ba8ca3e7
AM
14206 const char *name = h->elf.root.root.string;
14207
14208 if (*name == '.')
14209 ++name;
14210
14211 if (strncmp (name, "__libc_start_main", 17) == 0
14212 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14213 {
ba8ca3e7
AM
14214 /* Allow crt1 branch to go via a toc adjusting
14215 stub. Other calls that never return could do
14216 the same, if we could detect such. */
b25116a9 14217 can_plt_call = TRUE;
6ab189d5 14218 }
ba8ca3e7
AM
14219 }
14220
14221 if (!can_plt_call)
14222 {
14223 /* g++ as of 20130507 emits self-calls without a
14224 following nop. This is arguably wrong since we
14225 have conflicting information. On the one hand a
14226 global symbol and on the other a local call
14227 sequence, but don't error for this special case.
14228 It isn't possible to cheaply verify we have
14229 exactly such a call. Allow all calls to the same
14230 section. */
14231 asection *code_sec = sec;
14232
14233 if (get_opd_info (sec) != NULL)
ad8e1ba5 14234 {
ba8ca3e7
AM
14235 bfd_vma off = (relocation + addend
14236 - sec->output_section->vma
14237 - sec->output_offset);
bc30df16 14238
ba8ca3e7 14239 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14240 }
ba8ca3e7
AM
14241 if (code_sec == input_section)
14242 can_plt_call = TRUE;
14243 }
14244
14245 if (!can_plt_call)
14246 {
4805fc55
AM
14247 if (stub_entry->stub_type == ppc_stub_plt_call
14248 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14249 info->callbacks->einfo
695344c0 14250 /* xgettext:c-format */
c1c8c1ef 14251 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
14252 "recompile with -fPIC\n"),
14253 input_bfd, input_section, rel->r_offset, sym_name);
14254 else
14255 info->callbacks->einfo
695344c0 14256 /* xgettext:c-format */
c1c8c1ef 14257 (_("%H: call to `%pT' lacks nop, can't restore toc; "
4805fc55
AM
14258 "(-mcmodel=small toc adjust stub)\n"),
14259 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14260
14261 bfd_set_error (bfd_error_bad_value);
14262 ret = FALSE;
721956f4
AM
14263 }
14264
b25116a9 14265 if (can_plt_call
794e51c0
AM
14266 && (stub_entry->stub_type == ppc_stub_plt_call
14267 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14268 unresolved_reloc = FALSE;
14269 }
14270
6abec6bc
AM
14271 if ((stub_entry == NULL
14272 || stub_entry->stub_type == ppc_stub_long_branch
14273 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14274 && get_opd_info (sec) != NULL)
14275 {
14276 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14277 bfd_vma off = (relocation + addend
14278 - sec->output_section->vma
14279 - sec->output_offset);
aef36ac1 14280 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14281 if (dest != (bfd_vma) -1)
14282 {
14283 relocation = dest;
14284 addend = 0;
bc30df16 14285 reloc_dest = DEST_OPD;
8387904d
AM
14286 }
14287 }
14288
b25116a9
AM
14289 /* If the branch is out of reach we ought to have a long
14290 branch stub. */
14291 from = (rel->r_offset
14292 + input_section->output_offset
14293 + input_section->output_section->vma);
14294
6911b7dc
AM
14295 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14296 ? fdh->elf.other
14297 : sym->st_other);
14298
6abec6bc
AM
14299 if (stub_entry != NULL
14300 && (stub_entry->stub_type == ppc_stub_long_branch
14301 || stub_entry->stub_type == ppc_stub_plt_branch)
14302 && (r_type == R_PPC64_ADDR14_BRTAKEN
14303 || r_type == R_PPC64_ADDR14_BRNTAKEN
14304 || (relocation + addend - from + max_br_offset
14305 < 2 * max_br_offset)))
14306 /* Don't use the stub if this branch is in range. */
14307 stub_entry = NULL;
b25116a9
AM
14308
14309 if (stub_entry != NULL)
14310 {
14311 /* Munge up the value and addend so that we call the stub
14312 rather than the procedure directly. */
a4b6fadd
AM
14313 asection *stub_sec = stub_entry->group->stub_sec;
14314
14315 if (stub_entry->stub_type == ppc_stub_save_res)
14316 relocation += (stub_sec->output_offset
14317 + stub_sec->output_section->vma
14318 + stub_sec->size - htab->sfpr->size
14319 - htab->sfpr->output_offset
14320 - htab->sfpr->output_section->vma);
14321 else
14322 relocation = (stub_entry->stub_offset
14323 + stub_sec->output_offset
14324 + stub_sec->output_section->vma);
b25116a9 14325 addend = 0;
bc30df16 14326 reloc_dest = DEST_STUB;
3b421ab3 14327
954b63d4 14328 if ((stub_entry->stub_type == ppc_stub_plt_call
794e51c0
AM
14329 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14330 && (ALWAYS_EMIT_R2SAVE
14331 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14332 && rel + 1 < relend
14333 && rel[1].r_offset == rel->r_offset + 4
14334 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14335 relocation += 4;
b25116a9
AM
14336 }
14337
14338 if (insn != 0)
14339 {
794e51c0 14340 if (is_isa_v2)
721956f4 14341 {
b25116a9
AM
14342 /* Set 'a' bit. This is 0b00010 in BO field for branch
14343 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14344 for branch on CTR insns (BO == 1a00t or 1a01t). */
14345 if ((insn & (0x14 << 21)) == (0x04 << 21))
14346 insn |= 0x02 << 21;
14347 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14348 insn |= 0x08 << 21;
14349 else
14350 break;
14351 }
14352 else
14353 {
14354 /* Invert 'y' bit if not the default. */
4cc603a5 14355 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14356 insn ^= 0x01 << 21;
721956f4 14357 }
b25116a9 14358
95f0d0d2 14359 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14360 }
e86ce104 14361
06da1e8e
AM
14362 /* NOP out calls to undefined weak functions.
14363 We can thus call a weak function without first
14364 checking whether the function is defined. */
b25116a9 14365 else if (h != NULL
039b3fef 14366 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14367 && h->elf.dynindx == -1
b25116a9
AM
14368 && r_type == R_PPC64_REL24
14369 && relocation == 0
4cc603a5 14370 && addend == 0)
e86ce104 14371 {
95f0d0d2 14372 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14373 goto copy_reloc;
e86ce104 14374 }
65f38f15
AM
14375 break;
14376 }
5bd4f169 14377
65f38f15 14378 /* Set `addend'. */
411e1bfb 14379 tls_type = 0;
65f38f15
AM
14380 switch (r_type)
14381 {
14382 default:
cf97bcb0
AM
14383 /* xgettext:c-format */
14384 _bfd_error_handler (_("%pB: %s unsupported"),
14385 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 14386
65f38f15 14387 bfd_set_error (bfd_error_bad_value);
b34976b6 14388 ret = FALSE;
c316a17c 14389 goto copy_reloc;
5bd4f169 14390
65f38f15 14391 case R_PPC64_NONE:
411e1bfb 14392 case R_PPC64_TLS:
727fc41e
AM
14393 case R_PPC64_TLSGD:
14394 case R_PPC64_TLSLD:
3b421ab3 14395 case R_PPC64_TOCSAVE:
04c9666a
AM
14396 case R_PPC64_GNU_VTINHERIT:
14397 case R_PPC64_GNU_VTENTRY:
006589cf 14398 case R_PPC64_ENTRY:
c316a17c 14399 goto copy_reloc;
5bd4f169
AM
14400
14401 /* GOT16 relocations. Like an ADDR16 using the symbol's
14402 address in the GOT as relocation value instead of the
411e1bfb 14403 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14404 symbol and put the symbol value there. */
411e1bfb
AM
14405 case R_PPC64_GOT_TLSGD16:
14406 case R_PPC64_GOT_TLSGD16_LO:
14407 case R_PPC64_GOT_TLSGD16_HI:
14408 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14409 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14410 goto dogot;
14411
14412 case R_PPC64_GOT_TLSLD16:
14413 case R_PPC64_GOT_TLSLD16_LO:
14414 case R_PPC64_GOT_TLSLD16_HI:
14415 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14416 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14417 goto dogot;
14418
14419 case R_PPC64_GOT_TPREL16_DS:
14420 case R_PPC64_GOT_TPREL16_LO_DS:
14421 case R_PPC64_GOT_TPREL16_HI:
14422 case R_PPC64_GOT_TPREL16_HA:
14423 tls_type = TLS_TLS | TLS_TPREL;
14424 goto dogot;
14425
14426 case R_PPC64_GOT_DTPREL16_DS:
14427 case R_PPC64_GOT_DTPREL16_LO_DS:
14428 case R_PPC64_GOT_DTPREL16_HI:
14429 case R_PPC64_GOT_DTPREL16_HA:
14430 tls_type = TLS_TLS | TLS_DTPREL;
14431 goto dogot;
14432
65f38f15
AM
14433 case R_PPC64_GOT16:
14434 case R_PPC64_GOT16_LO:
14435 case R_PPC64_GOT16_HI:
14436 case R_PPC64_GOT16_HA:
14437 case R_PPC64_GOT16_DS:
14438 case R_PPC64_GOT16_LO_DS:
411e1bfb 14439 dogot:
5bd4f169
AM
14440 {
14441 /* Relocation is to the entry for this symbol in the global
14442 offset table. */
e717da7e 14443 asection *got;
d881513a 14444 bfd_vma *offp;
5bd4f169 14445 bfd_vma off;
d881513a 14446 unsigned long indx = 0;
927be08e 14447 struct got_entry *ent;
65f38f15 14448
d881513a
AM
14449 if (tls_type == (TLS_TLS | TLS_LD)
14450 && (h == NULL
f5385ebf 14451 || !h->elf.def_dynamic))
927be08e 14452 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14453 else
5bd4f169 14454 {
d881513a
AM
14455 if (h != NULL)
14456 {
f0158f44
AM
14457 if (!htab->elf.dynamic_sections_created
14458 || h->elf.dynindx == -1
14459 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 14460 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
14461 /* This is actually a static link, or it is a
14462 -Bsymbolic link and the symbol is defined
14463 locally, or the symbol was forced to be local
14464 because of a version file. */
14465 ;
14466 else
14467 {
039b3fef 14468 indx = h->elf.dynindx;
d881513a
AM
14469 unresolved_reloc = FALSE;
14470 }
039b3fef 14471 ent = h->elf.got.glist;
d881513a 14472 }
411e1bfb 14473 else
5bd4f169 14474 {
d881513a
AM
14475 if (local_got_ents == NULL)
14476 abort ();
14477 ent = local_got_ents[r_symndx];
5bd4f169 14478 }
d881513a
AM
14479
14480 for (; ent != NULL; ent = ent->next)
31c76678 14481 if (ent->addend == orig_rel.r_addend
e717da7e 14482 && ent->owner == input_bfd
d881513a
AM
14483 && ent->tls_type == tls_type)
14484 break;
5bd4f169 14485 }
411e1bfb 14486
927be08e
AM
14487 if (ent == NULL)
14488 abort ();
14489 if (ent->is_indirect)
14490 ent = ent->got.ent;
14491 offp = &ent->got.offset;
14492 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14493 if (got == NULL)
14494 abort ();
14495
411e1bfb
AM
14496 /* The offset must always be a multiple of 8. We use the
14497 least significant bit to record whether we have already
14498 processed this entry. */
d881513a 14499 off = *offp;
411e1bfb
AM
14500 if ((off & 1) != 0)
14501 off &= ~1;
5bd4f169
AM
14502 else
14503 {
411e1bfb
AM
14504 /* Generate relocs for the dynamic linker, except in
14505 the case of TLSLD where we'll use one entry per
14506 module. */
25f23106
AM
14507 asection *relgot;
14508 bfd_boolean ifunc;
e717da7e 14509
d881513a 14510 *offp = off | 1;
25f23106
AM
14511 relgot = NULL;
14512 ifunc = (h != NULL
14513 ? h->elf.type == STT_GNU_IFUNC
14514 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14515 if (ifunc)
82e66161
AM
14516 {
14517 relgot = htab->elf.irelplt;
14518 if (indx == 0)
14519 htab->local_ifunc_resolver = 1;
14520 else if (is_static_defined (&h->elf))
14521 htab->maybe_local_ifunc_resolver = 1;
14522 }
f0158f44
AM
14523 else if (indx != 0
14524 || (bfd_link_pic (info)
14525 && (h == NULL
21d68fcd 14526 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
f0158f44 14527 || (tls_type == (TLS_TLS | TLS_LD)
f15d0b54
AM
14528 && !h->elf.def_dynamic))
14529 && !(tls_type == (TLS_TLS | TLS_TPREL)
14530 && bfd_link_executable (info)
14531 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
19e08130 14532 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14533 if (relgot != NULL)
5bd4f169 14534 {
e717da7e
AM
14535 outrel.r_offset = (got->output_section->vma
14536 + got->output_offset
411e1bfb 14537 + off);
4cc603a5 14538 outrel.r_addend = addend;
d881513a 14539 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14540 {
411e1bfb 14541 outrel.r_addend = 0;
e515b051 14542 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14543 if (tls_type == (TLS_TLS | TLS_GD))
14544 {
e717da7e
AM
14545 loc = relgot->contents;
14546 loc += (relgot->reloc_count++
d881513a
AM
14547 * sizeof (Elf64_External_Rela));
14548 bfd_elf64_swap_reloca_out (output_bfd,
14549 &outrel, loc);
e515b051 14550 outrel.r_offset += 8;
4cc603a5 14551 outrel.r_addend = addend;
d881513a
AM
14552 outrel.r_info
14553 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14554 }
411e1bfb 14555 }
951fd09b 14556 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14557 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14558 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14559 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14560 else if (indx != 0)
14561 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14562 else
81407a69 14563 {
25f23106
AM
14564 if (ifunc)
14565 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14566 else
14567 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14568
14569 /* Write the .got section contents for the sake
14570 of prelink. */
e717da7e 14571 loc = got->contents + off;
23fbd6fa
JJ
14572 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14573 loc);
81407a69 14574 }
81407a69
AM
14575
14576 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14577 {
14578 outrel.r_addend += relocation;
14579 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14580 {
14581 if (htab->elf.tls_sec == NULL)
14582 outrel.r_addend = 0;
14583 else
14584 outrel.r_addend -= htab->elf.tls_sec->vma;
14585 }
e515b051 14586 }
e717da7e
AM
14587 loc = relgot->contents;
14588 loc += (relgot->reloc_count++
411e1bfb
AM
14589 * sizeof (Elf64_External_Rela));
14590 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14591 }
14592
ad8e1ba5 14593 /* Init the .got section contents here if we're not
81407a69 14594 emitting a reloc. */
d881513a 14595 else
411e1bfb 14596 {
4cc603a5 14597 relocation += addend;
f0158f44 14598 if (tls_type != 0)
411e1bfb 14599 {
989f9879
AM
14600 if (htab->elf.tls_sec == NULL)
14601 relocation = 0;
14602 else
14603 {
f0158f44
AM
14604 if (tls_type & TLS_LD)
14605 relocation = 0;
14606 else
14607 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 14608 if (tls_type & TLS_TPREL)
989f9879
AM
14609 relocation += DTP_OFFSET - TP_OFFSET;
14610 }
5bd4f169 14611
f0158f44 14612 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
14613 {
14614 bfd_put_64 (output_bfd, relocation,
e717da7e 14615 got->contents + off + 8);
676ee2b5 14616 relocation = 1;
7b609f53 14617 }
411e1bfb
AM
14618 }
14619 bfd_put_64 (output_bfd, relocation,
e717da7e 14620 got->contents + off);
5bd4f169
AM
14621 }
14622 }
14623
65f38f15
AM
14624 if (off >= (bfd_vma) -2)
14625 abort ();
14626
bf102f86 14627 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14628 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14629 }
65f38f15
AM
14630 break;
14631
14632 case R_PPC64_PLT16_HA:
14633 case R_PPC64_PLT16_HI:
14634 case R_PPC64_PLT16_LO:
14635 case R_PPC64_PLT32:
14636 case R_PPC64_PLT64:
14637 /* Relocation is to the entry for this symbol in the
14638 procedure linkage table. */
cbf95972
AM
14639 {
14640 struct plt_entry **plt_list = NULL;
14641 if (h != NULL)
14642 plt_list = &h->elf.plt.plist;
14643 else if (local_got_ents != NULL)
14644 {
14645 struct plt_entry **local_plt = (struct plt_entry **)
14646 (local_got_ents + symtab_hdr->sh_info);
14647 unsigned char *local_got_tls_masks = (unsigned char *)
14648 (local_plt + symtab_hdr->sh_info);
14649 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14650 plt_list = local_plt + r_symndx;
14651 }
14652 if (plt_list)
14653 {
14654 struct plt_entry *ent;
65f38f15 14655
cbf95972
AM
14656 for (ent = *plt_list; ent != NULL; ent = ent->next)
14657 if (ent->plt.offset != (bfd_vma) -1
14658 && ent->addend == orig_rel.r_addend)
14659 {
14660 asection *plt;
14661
14662 plt = htab->elf.splt;
14663 if (!htab->elf.dynamic_sections_created
14664 || h == NULL
14665 || h->elf.dynindx == -1)
14666 plt = htab->elf.iplt;
14667 relocation = (plt->output_section->vma
14668 + plt->output_offset
14669 + ent->plt.offset);
14670 addend = 0;
14671 unresolved_reloc = FALSE;
14672 break;
14673 }
14674 }
14675 }
65f38f15 14676 break;
5bd4f169 14677
0b13192e
AM
14678 case R_PPC64_TOC:
14679 /* Relocation value is TOC base. */
14680 relocation = TOCstart;
cf35638d 14681 if (r_symndx == STN_UNDEF)
6f20ed8a 14682 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14683 else if (unresolved_reloc)
14684 ;
6f20ed8a
AM
14685 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14686 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14687 else
14688 unresolved_reloc = TRUE;
ab96bf03 14689 goto dodyn;
0b13192e 14690
5bd4f169
AM
14691 /* TOC16 relocs. We want the offset relative to the TOC base,
14692 which is the address of the start of the TOC plus 0x8000.
14693 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14694 in this order. */
65f38f15
AM
14695 case R_PPC64_TOC16:
14696 case R_PPC64_TOC16_LO:
14697 case R_PPC64_TOC16_HI:
14698 case R_PPC64_TOC16_DS:
14699 case R_PPC64_TOC16_LO_DS:
14700 case R_PPC64_TOC16_HA:
6f20ed8a 14701 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14702 break;
14703
14704 /* Relocate against the beginning of the section. */
65f38f15
AM
14705 case R_PPC64_SECTOFF:
14706 case R_PPC64_SECTOFF_LO:
14707 case R_PPC64_SECTOFF_HI:
14708 case R_PPC64_SECTOFF_DS:
14709 case R_PPC64_SECTOFF_LO_DS:
14710 case R_PPC64_SECTOFF_HA:
4ce794b7 14711 if (sec != NULL)
65f38f15 14712 addend -= sec->output_section->vma;
5bd4f169
AM
14713 break;
14714
25f23106
AM
14715 case R_PPC64_REL16:
14716 case R_PPC64_REL16_LO:
14717 case R_PPC64_REL16_HI:
14718 case R_PPC64_REL16_HA:
a680de9a 14719 case R_PPC64_REL16DX_HA:
25f23106
AM
14720 break;
14721
721956f4
AM
14722 case R_PPC64_REL14:
14723 case R_PPC64_REL14_BRNTAKEN:
14724 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
14725 case R_PPC64_REL24:
14726 break;
14727
411e1bfb
AM
14728 case R_PPC64_TPREL16:
14729 case R_PPC64_TPREL16_LO:
14730 case R_PPC64_TPREL16_HI:
14731 case R_PPC64_TPREL16_HA:
14732 case R_PPC64_TPREL16_DS:
14733 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14734 case R_PPC64_TPREL16_HIGH:
14735 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14736 case R_PPC64_TPREL16_HIGHER:
14737 case R_PPC64_TPREL16_HIGHERA:
14738 case R_PPC64_TPREL16_HIGHEST:
14739 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14740 if (h != NULL
14741 && h->elf.root.type == bfd_link_hash_undefweak
14742 && h->elf.dynindx == -1)
14743 {
14744 /* Make this relocation against an undefined weak symbol
14745 resolve to zero. This is really just a tweak, since
14746 code using weak externs ought to check that they are
14747 defined before using them. */
14748 bfd_byte *p = contents + rel->r_offset - d_offset;
14749
95f0d0d2 14750 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
14751 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14752 if (insn != 0)
95f0d0d2 14753 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
14754 break;
14755 }
989f9879
AM
14756 if (htab->elf.tls_sec != NULL)
14757 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
14758 /* The TPREL16 relocs shouldn't really be used in shared
14759 libs or with non-local symbols as that will result in
14760 DT_TEXTREL being set, but support them anyway. */
14761 goto dodyn;
411e1bfb
AM
14762
14763 case R_PPC64_DTPREL16:
14764 case R_PPC64_DTPREL16_LO:
14765 case R_PPC64_DTPREL16_HI:
14766 case R_PPC64_DTPREL16_HA:
14767 case R_PPC64_DTPREL16_DS:
14768 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14769 case R_PPC64_DTPREL16_HIGH:
14770 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14771 case R_PPC64_DTPREL16_HIGHER:
14772 case R_PPC64_DTPREL16_HIGHERA:
14773 case R_PPC64_DTPREL16_HIGHEST:
14774 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14775 if (htab->elf.tls_sec != NULL)
14776 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14777 break;
14778
45965137
AM
14779 case R_PPC64_ADDR64_LOCAL:
14780 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14781 ? h->elf.other
14782 : sym->st_other);
14783 break;
14784
e515b051
AM
14785 case R_PPC64_DTPMOD64:
14786 relocation = 1;
14787 addend = 0;
14788 goto dodyn;
14789
411e1bfb 14790 case R_PPC64_TPREL64:
989f9879
AM
14791 if (htab->elf.tls_sec != NULL)
14792 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14793 goto dodyn;
14794
14795 case R_PPC64_DTPREL64:
989f9879
AM
14796 if (htab->elf.tls_sec != NULL)
14797 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 14798 /* Fall through. */
411e1bfb 14799
65f38f15
AM
14800 /* Relocations that may need to be propagated if this is a
14801 dynamic object. */
04c9666a 14802 case R_PPC64_REL30:
65f38f15
AM
14803 case R_PPC64_REL32:
14804 case R_PPC64_REL64:
14805 case R_PPC64_ADDR14:
14806 case R_PPC64_ADDR14_BRNTAKEN:
14807 case R_PPC64_ADDR14_BRTAKEN:
14808 case R_PPC64_ADDR16:
14809 case R_PPC64_ADDR16_DS:
14810 case R_PPC64_ADDR16_HA:
14811 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14812 case R_PPC64_ADDR16_HIGH:
14813 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14814 case R_PPC64_ADDR16_HIGHER:
14815 case R_PPC64_ADDR16_HIGHERA:
14816 case R_PPC64_ADDR16_HIGHEST:
14817 case R_PPC64_ADDR16_HIGHESTA:
14818 case R_PPC64_ADDR16_LO:
14819 case R_PPC64_ADDR16_LO_DS:
14820 case R_PPC64_ADDR24:
65f38f15
AM
14821 case R_PPC64_ADDR32:
14822 case R_PPC64_ADDR64:
14823 case R_PPC64_UADDR16:
14824 case R_PPC64_UADDR32:
14825 case R_PPC64_UADDR64:
411e1bfb 14826 dodyn:
5d1634d7 14827 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14828 break;
14829
41bd81ab
AM
14830 if (NO_OPD_RELOCS && is_opd)
14831 break;
14832
8a9e8e72 14833 if (bfd_link_pic (info)
b1b07054
AM
14834 ? ((h == NULL
14835 || h->dyn_relocs != NULL)
14836 && ((h != NULL && pc_dynrelocs (h))
14837 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
14838 : (h != NULL
14839 ? h->dyn_relocs != NULL
d311bc8b 14840 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 14841 {
b34976b6 14842 bfd_boolean skip, relocate;
65f38f15 14843 asection *sreloc;
1cf1f670 14844 bfd_vma out_off;
82e66161 14845 long indx = 0;
65f38f15
AM
14846
14847 /* When generating a dynamic object, these relocations
14848 are copied into the output file to be resolved at run
14849 time. */
14850
b34976b6
AM
14851 skip = FALSE;
14852 relocate = FALSE;
65f38f15 14853
1cf1f670
AM
14854 out_off = _bfd_elf_section_offset (output_bfd, info,
14855 input_section, rel->r_offset);
14856 if (out_off == (bfd_vma) -1)
b34976b6 14857 skip = TRUE;
1cf1f670 14858 else if (out_off == (bfd_vma) -2)
b34976b6 14859 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14860 out_off += (input_section->output_section->vma
14861 + input_section->output_offset);
14862 outrel.r_offset = out_off;
411e1bfb 14863 outrel.r_addend = rel->r_addend;
65f38f15 14864
1cf1f670
AM
14865 /* Optimize unaligned reloc use. */
14866 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14867 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14868 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14869 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14870 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14871 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14872 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14873 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14874 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14875
65f38f15 14876 if (skip)
0bb2d96a 14877 memset (&outrel, 0, sizeof outrel);
afe397ea 14878 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14879 && !is_opd
14880 && r_type != R_PPC64_TOC)
14acf4dc 14881 {
82e66161
AM
14882 indx = h->elf.dynindx;
14883 BFD_ASSERT (indx != -1);
14884 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 14885 }
65f38f15
AM
14886 else
14887 {
41bd81ab
AM
14888 /* This symbol is local, or marked to become local,
14889 or this is an opd section reloc which must point
14890 at a local function. */
65f38f15 14891 outrel.r_addend += relocation;
e86ce104 14892 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14893 {
3fad3c7c 14894 if (is_opd && h != NULL)
afbe61cf
AM
14895 {
14896 /* Lie about opd entries. This case occurs
14897 when building shared libraries and we
14898 reference a function in another shared
3fad3c7c
AM
14899 lib. The same thing happens for a weak
14900 definition in an application that's
14901 overridden by a strong definition in a
14902 shared lib. (I believe this is a generic
14903 bug in binutils handling of weak syms.)
14904 In these cases we won't use the opd
1e2f5b6e 14905 entry in this lib. */
b34976b6 14906 unresolved_reloc = FALSE;
afbe61cf 14907 }
25f23106
AM
14908 if (!is_opd
14909 && r_type == R_PPC64_ADDR64
14910 && (h != NULL
14911 ? h->elf.type == STT_GNU_IFUNC
14912 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14913 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14914 else
14915 {
14916 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14917
25f23106
AM
14918 /* We need to relocate .opd contents for ld.so.
14919 Prelink also wants simple and consistent rules
14920 for relocs. This make all RELATIVE relocs have
14921 *r_offset equal to r_addend. */
14922 relocate = TRUE;
14923 }
65f38f15
AM
14924 }
14925 else
14926 {
25f23106
AM
14927 if (h != NULL
14928 ? h->elf.type == STT_GNU_IFUNC
14929 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14930 {
25f53a85 14931 info->callbacks->einfo
695344c0 14932 /* xgettext:c-format */
174d0a74 14933 (_("%H: %s for indirect "
c1c8c1ef 14934 "function `%pT' unsupported\n"),
25f53a85 14935 input_bfd, input_section, rel->r_offset,
25f23106
AM
14936 ppc64_elf_howto_table[r_type]->name,
14937 sym_name);
14938 ret = FALSE;
14939 }
cf35638d 14940 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14941 ;
14942 else if (sec == NULL || sec->owner == NULL)
14943 {
14944 bfd_set_error (bfd_error_bad_value);
b34976b6 14945 return FALSE;
65f38f15
AM
14946 }
14947 else
14948 {
14949 asection *osec;
14950
14951 osec = sec->output_section;
14952 indx = elf_section_data (osec)->dynindx;
14953
74541ad4
AM
14954 if (indx == 0)
14955 {
14956 if ((osec->flags & SEC_READONLY) == 0
14957 && htab->elf.data_index_section != NULL)
14958 osec = htab->elf.data_index_section;
14959 else
14960 osec = htab->elf.text_index_section;
14961 indx = elf_section_data (osec)->dynindx;
14962 }
14963 BFD_ASSERT (indx != 0);
14964
65f38f15
AM
14965 /* We are turning this relocation into one
14966 against a section symbol, so subtract out
14967 the output section's address but not the
14968 offset of the input section in the output
14969 section. */
14970 outrel.r_addend -= osec->vma;
14971 }
14972
14973 outrel.r_info = ELF64_R_INFO (indx, r_type);
14974 }
14975 }
14976
14977 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14978 if (h != NULL
14979 ? h->elf.type == STT_GNU_IFUNC
14980 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
14981 {
14982 sreloc = htab->elf.irelplt;
14983 if (indx == 0)
14984 htab->local_ifunc_resolver = 1;
14985 else if (is_static_defined (&h->elf))
14986 htab->maybe_local_ifunc_resolver = 1;
14987 }
65f38f15
AM
14988 if (sreloc == NULL)
14989 abort ();
14990
dfbb6ac9
AM
14991 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14992 >= sreloc->size)
14993 abort ();
947216bf
AM
14994 loc = sreloc->contents;
14995 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14996 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14997
14998 /* If this reloc is against an external symbol, it will
14999 be computed at runtime, so there's no need to do
81407a69
AM
15000 anything now. However, for the sake of prelink ensure
15001 that the section contents are a known value. */
65f38f15 15002 if (! relocate)
81407a69
AM
15003 {
15004 unresolved_reloc = FALSE;
15005 /* The value chosen here is quite arbitrary as ld.so
15006 ignores section contents except for the special
15007 case of .opd where the contents might be accessed
15008 before relocation. Choose zero, as that won't
15009 cause reloc overflow. */
15010 relocation = 0;
15011 addend = 0;
15012 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15013 to improve backward compatibility with older
15014 versions of ld. */
15015 if (r_type == R_PPC64_ADDR64)
15016 addend = outrel.r_addend;
15017 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 15018 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 15019 addend = outrel.r_offset;
81407a69 15020 }
65f38f15 15021 }
5bd4f169
AM
15022 break;
15023
65f38f15
AM
15024 case R_PPC64_COPY:
15025 case R_PPC64_GLOB_DAT:
15026 case R_PPC64_JMP_SLOT:
25f23106 15027 case R_PPC64_JMP_IREL:
65f38f15
AM
15028 case R_PPC64_RELATIVE:
15029 /* We shouldn't ever see these dynamic relocs in relocatable
15030 files. */
ae9a127f 15031 /* Fall through. */
65f38f15
AM
15032
15033 case R_PPC64_PLTGOT16:
15034 case R_PPC64_PLTGOT16_DS:
15035 case R_PPC64_PLTGOT16_HA:
15036 case R_PPC64_PLTGOT16_HI:
15037 case R_PPC64_PLTGOT16_LO:
15038 case R_PPC64_PLTGOT16_LO_DS:
15039 case R_PPC64_PLTREL32:
15040 case R_PPC64_PLTREL64:
15041 /* These ones haven't been implemented yet. */
15042
25f53a85 15043 info->callbacks->einfo
695344c0 15044 /* xgettext:c-format */
c1c8c1ef 15045 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 15046 input_bfd,
4ce794b7 15047 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
15048
15049 bfd_set_error (bfd_error_invalid_operation);
b34976b6 15050 ret = FALSE;
c316a17c 15051 goto copy_reloc;
65f38f15 15052 }
5bd4f169 15053
67f0cbdb
AM
15054 /* Multi-instruction sequences that access the TOC can be
15055 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 15056 to nop; addi rb,r2,x; */
67f0cbdb
AM
15057 switch (r_type)
15058 {
15059 default:
15060 break;
15061
15062 case R_PPC64_GOT_TLSLD16_HI:
15063 case R_PPC64_GOT_TLSGD16_HI:
15064 case R_PPC64_GOT_TPREL16_HI:
15065 case R_PPC64_GOT_DTPREL16_HI:
15066 case R_PPC64_GOT16_HI:
15067 case R_PPC64_TOC16_HI:
15068 /* These relocs would only be useful if building up an
15069 offset to later add to r2, perhaps in an indexed
15070 addressing mode instruction. Don't try to optimize.
15071 Unfortunately, the possibility of someone building up an
15072 offset like this or even with the HA relocs, means that
15073 we need to check the high insn when optimizing the low
15074 insn. */
15075 break;
15076
15077 case R_PPC64_GOT_TLSLD16_HA:
15078 case R_PPC64_GOT_TLSGD16_HA:
15079 case R_PPC64_GOT_TPREL16_HA:
15080 case R_PPC64_GOT_DTPREL16_HA:
15081 case R_PPC64_GOT16_HA:
15082 case R_PPC64_TOC16_HA:
98528052 15083 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15084 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
15085 {
15086 bfd_byte *p = contents + (rel->r_offset & ~3);
15087 bfd_put_32 (input_bfd, NOP, p);
d830549d 15088 goto copy_reloc;
98528052 15089 }
67f0cbdb
AM
15090 break;
15091
15092 case R_PPC64_GOT_TLSLD16_LO:
15093 case R_PPC64_GOT_TLSGD16_LO:
15094 case R_PPC64_GOT_TPREL16_LO_DS:
15095 case R_PPC64_GOT_DTPREL16_LO_DS:
15096 case R_PPC64_GOT16_LO:
15097 case R_PPC64_GOT16_LO_DS:
15098 case R_PPC64_TOC16_LO:
15099 case R_PPC64_TOC16_LO_DS:
98528052 15100 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15101 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15102 {
15103 bfd_byte *p = contents + (rel->r_offset & ~3);
15104 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15105 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15106 {
15107 /* Transform addic to addi when we change reg. */
15108 insn &= ~((0x3f << 26) | (0x1f << 16));
15109 insn |= (14u << 26) | (2 << 16);
15110 }
15111 else
67f0cbdb 15112 {
98528052
AM
15113 insn &= ~(0x1f << 16);
15114 insn |= 2 << 16;
67f0cbdb 15115 }
560c8763 15116 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15117 }
15118 break;
9a23f96e
AM
15119
15120 case R_PPC64_TPREL16_HA:
15121 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15122 {
15123 bfd_byte *p = contents + (rel->r_offset & ~3);
15124 insn = bfd_get_32 (input_bfd, p);
15125 if ((insn & ((0x3f << 26) | 0x1f << 16))
15126 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15127 /* xgettext:c-format */
15128 info->callbacks->minfo
15129 (_("%H: warning: %s unexpected insn %#x.\n"),
d830549d
AM
15130 input_bfd, input_section, rel->r_offset,
15131 ppc64_elf_howto_table[r_type]->name, insn);
9a23f96e 15132 else
d830549d
AM
15133 {
15134 bfd_put_32 (input_bfd, NOP, p);
15135 goto copy_reloc;
15136 }
9a23f96e
AM
15137 }
15138 break;
15139
15140 case R_PPC64_TPREL16_LO:
15141 case R_PPC64_TPREL16_LO_DS:
15142 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15143 {
15144 bfd_byte *p = contents + (rel->r_offset & ~3);
15145 insn = bfd_get_32 (input_bfd, p);
15146 insn &= ~(0x1f << 16);
15147 insn |= 13 << 16;
15148 bfd_put_32 (input_bfd, insn, p);
15149 }
15150 break;
67f0cbdb
AM
15151 }
15152
65f38f15
AM
15153 /* Do any further special processing. */
15154 switch (r_type)
15155 {
15156 default:
15157 break;
15158
25f23106 15159 case R_PPC64_REL16_HA:
a680de9a 15160 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15161 case R_PPC64_ADDR16_HA:
15162 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15163 case R_PPC64_ADDR16_HIGHERA:
15164 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15165 case R_PPC64_TOC16_HA:
15166 case R_PPC64_SECTOFF_HA:
411e1bfb 15167 case R_PPC64_TPREL16_HA:
f9c6b907 15168 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15169 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15170 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15171 case R_PPC64_DTPREL16_HA:
15172 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15173 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15174 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15175 /* It's just possible that this symbol is a weak symbol
15176 that's not actually defined anywhere. In that case,
15177 'sec' would be NULL, and we should leave the symbol
15178 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15179 if (sec == NULL)
15180 break;
1a0670f3 15181 /* Fall through. */
5c5f6e17
AM
15182
15183 case R_PPC64_GOT16_HA:
15184 case R_PPC64_PLTGOT16_HA:
15185 case R_PPC64_PLT16_HA:
15186 case R_PPC64_GOT_TLSGD16_HA:
15187 case R_PPC64_GOT_TLSLD16_HA:
15188 case R_PPC64_GOT_TPREL16_HA:
15189 case R_PPC64_GOT_DTPREL16_HA:
15190 /* Add 0x10000 if sign bit in 0:15 is set.
15191 Bits 0:15 are not used. */
15192 addend += 0x8000;
65f38f15
AM
15193 break;
15194
15195 case R_PPC64_ADDR16_DS:
15196 case R_PPC64_ADDR16_LO_DS:
15197 case R_PPC64_GOT16_DS:
15198 case R_PPC64_GOT16_LO_DS:
15199 case R_PPC64_PLT16_LO_DS:
15200 case R_PPC64_SECTOFF_DS:
15201 case R_PPC64_SECTOFF_LO_DS:
15202 case R_PPC64_TOC16_DS:
15203 case R_PPC64_TOC16_LO_DS:
15204 case R_PPC64_PLTGOT16_DS:
15205 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15206 case R_PPC64_GOT_TPREL16_DS:
15207 case R_PPC64_GOT_TPREL16_LO_DS:
15208 case R_PPC64_GOT_DTPREL16_DS:
15209 case R_PPC64_GOT_DTPREL16_LO_DS:
15210 case R_PPC64_TPREL16_DS:
15211 case R_PPC64_TPREL16_LO_DS:
15212 case R_PPC64_DTPREL16_DS:
15213 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15214 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15215 mask = 3;
a680de9a
PB
15216 /* If this reloc is against an lq, lxv, or stxv insn, then
15217 the value must be a multiple of 16. This is somewhat of
15218 a hack, but the "correct" way to do this by defining _DQ
15219 forms of all the _DS relocs bloats all reloc switches in
15220 this file. It doesn't make much sense to use these
15221 relocs in data, so testing the insn should be safe. */
15222 if ((insn & (0x3f << 26)) == (56u << 26)
15223 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15224 mask = 15;
a680de9a
PB
15225 relocation += addend;
15226 addend = insn & (mask ^ 3);
15227 if ((relocation & mask) != 0)
65f38f15 15228 {
a680de9a 15229 relocation ^= relocation & mask;
25f53a85 15230 info->callbacks->einfo
695344c0 15231 /* xgettext:c-format */
174d0a74 15232 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 15233 input_bfd, input_section, rel->r_offset,
d830549d 15234 ppc64_elf_howto_table[r_type]->name,
adadcc0c 15235 mask + 1);
65f38f15 15236 bfd_set_error (bfd_error_bad_value);
b34976b6 15237 ret = FALSE;
c316a17c 15238 goto copy_reloc;
65f38f15
AM
15239 }
15240 break;
5bd4f169
AM
15241 }
15242
239e1f3a
AM
15243 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15244 because such sections are not SEC_ALLOC and thus ld.so will
15245 not process them. */
d830549d 15246 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 15247 if (unresolved_reloc
239e1f3a 15248 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15249 && h->elf.def_dynamic)
15250 && _bfd_elf_section_offset (output_bfd, info, input_section,
15251 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15252 {
25f53a85 15253 info->callbacks->einfo
695344c0 15254 /* xgettext:c-format */
c1c8c1ef 15255 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 15256 input_bfd, input_section, rel->r_offset,
b80eed39 15257 howto->name,
039b3fef 15258 h->elf.root.root.string);
b34976b6 15259 ret = FALSE;
9c07fe7c 15260 }
5bd4f169 15261
b80eed39
AM
15262 /* 16-bit fields in insns mostly have signed values, but a
15263 few insns have 16-bit unsigned values. Really, we should
15264 have different reloc types. */
15265 if (howto->complain_on_overflow != complain_overflow_dont
15266 && howto->dst_mask == 0xffff
15267 && (input_section->flags & SEC_CODE) != 0)
15268 {
15269 enum complain_overflow complain = complain_overflow_signed;
15270
15271 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15272 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15273 complain = complain_overflow_bitfield;
15274 else if (howto->rightshift == 0
15275 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15276 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15277 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15278 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15279 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15280 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15281 complain = complain_overflow_unsigned;
15282 if (howto->complain_on_overflow != complain)
15283 {
15284 alt_howto = *howto;
15285 alt_howto.complain_on_overflow = complain;
15286 howto = &alt_howto;
15287 }
15288 }
15289
a680de9a
PB
15290 if (r_type == R_PPC64_REL16DX_HA)
15291 {
15292 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15293 if (rel->r_offset + 4 > input_section->size)
15294 r = bfd_reloc_outofrange;
15295 else
15296 {
15297 relocation += addend;
15298 relocation -= (rel->r_offset
15299 + input_section->output_offset
15300 + input_section->output_section->vma);
3de43e7b 15301 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
15302 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15303 insn &= ~0x1fffc1;
3de43e7b 15304 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
15305 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15306 r = bfd_reloc_ok;
3de43e7b 15307 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
15308 r = bfd_reloc_overflow;
15309 }
15310 }
15311 else
15312 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15313 rel->r_offset, relocation, addend);
5bd4f169 15314
ef60b7ff 15315 if (r != bfd_reloc_ok)
5bd4f169 15316 {
bc30df16 15317 char *more_info = NULL;
b80eed39 15318 const char *reloc_name = howto->name;
bc30df16
AM
15319
15320 if (reloc_dest != DEST_NORMAL)
15321 {
15322 more_info = bfd_malloc (strlen (reloc_name) + 8);
15323 if (more_info != NULL)
15324 {
15325 strcpy (more_info, reloc_name);
15326 strcat (more_info, (reloc_dest == DEST_OPD
15327 ? " (OPD)" : " (stub)"));
15328 reloc_name = more_info;
15329 }
15330 }
15331
cd27b276 15332 if (r == bfd_reloc_overflow)
5bd4f169 15333 {
8131c122
AM
15334 /* On code like "if (foo) foo();" don't report overflow
15335 on a branch to zero when foo is undefined. */
15336 if (!warned
15337 && (reloc_dest == DEST_STUB
15338 || !(h != NULL
15339 && (h->elf.root.type == bfd_link_hash_undefweak
15340 || h->elf.root.type == bfd_link_hash_undefined)
15341 && is_branch_reloc (r_type))))
1a72702b
AM
15342 info->callbacks->reloc_overflow (info, &h->elf.root,
15343 sym_name, reloc_name,
15344 orig_rel.r_addend,
15345 input_bfd, input_section,
15346 rel->r_offset);
ef60b7ff
AM
15347 }
15348 else
15349 {
25f53a85 15350 info->callbacks->einfo
695344c0 15351 /* xgettext:c-format */
c1c8c1ef 15352 (_("%H: %s against `%pT': error %d\n"),
25f53a85 15353 input_bfd, input_section, rel->r_offset,
bc30df16 15354 reloc_name, sym_name, (int) r);
b34976b6 15355 ret = FALSE;
ef60b7ff 15356 }
bc30df16
AM
15357 if (more_info != NULL)
15358 free (more_info);
5bd4f169 15359 }
c316a17c
AM
15360 copy_reloc:
15361 if (wrel != rel)
15362 *wrel = *rel;
15363 }
15364
15365 if (wrel != rel)
15366 {
15367 Elf_Internal_Shdr *rel_hdr;
15368 size_t deleted = rel - wrel;
15369
15370 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15371 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15372 if (rel_hdr->sh_size == 0)
15373 {
15374 /* It is too late to remove an empty reloc section. Leave
15375 one NONE reloc.
15376 ??? What is wrong with an empty section??? */
15377 rel_hdr->sh_size = rel_hdr->sh_entsize;
15378 deleted -= 1;
15379 }
15380 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15381 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15382 input_section->reloc_count -= deleted;
5bd4f169
AM
15383 }
15384
645ea6a9
AM
15385 /* If we're emitting relocations, then shortly after this function
15386 returns, reloc offsets and addends for this section will be
15387 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15388 file rather than the input. Save a copy of the relocs for
15389 opd_entry_value. */
0e1862bb 15390 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15391 {
15392 bfd_size_type amt;
15393 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15394 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15395 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15396 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15397 if (rel == NULL)
15398 return FALSE;
15399 memcpy (rel, relocs, amt);
15400 }
5bd4f169
AM
15401 return ret;
15402}
15403
754021d0
AM
15404/* Adjust the value of any local symbols in opd sections. */
15405
6e0b88f1 15406static int
754021d0
AM
15407ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15408 const char *name ATTRIBUTE_UNUSED,
15409 Elf_Internal_Sym *elfsym,
15410 asection *input_sec,
15411 struct elf_link_hash_entry *h)
15412{
74f0fb50
AM
15413 struct _opd_sec_data *opd;
15414 long adjust;
754021d0
AM
15415 bfd_vma value;
15416
4025353c 15417 if (h != NULL)
6e0b88f1 15418 return 1;
4025353c 15419
74f0fb50
AM
15420 opd = get_opd_info (input_sec);
15421 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15422 return 1;
754021d0
AM
15423
15424 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15425 if (!bfd_link_relocatable (info))
754021d0
AM
15426 value -= input_sec->output_section->vma;
15427
51aecdc5 15428 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15429 if (adjust == -1)
6e0b88f1
AM
15430 return 2;
15431
15432 elfsym->st_value += adjust;
15433 return 1;
754021d0
AM
15434}
15435
5bd4f169
AM
15436/* Finish up dynamic symbol handling. We set the contents of various
15437 dynamic sections here. */
15438
b34976b6 15439static bfd_boolean
4ce794b7
AM
15440ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15441 struct bfd_link_info *info,
15442 struct elf_link_hash_entry *h,
4aef7643 15443 Elf_Internal_Sym *sym)
5bd4f169 15444{
65f38f15 15445 struct ppc_link_hash_table *htab;
8387904d
AM
15446 struct plt_entry *ent;
15447 Elf_Internal_Rela rela;
15448 bfd_byte *loc;
5bd4f169 15449
65f38f15 15450 htab = ppc_hash_table (info);
4dfe6ac6
NC
15451 if (htab == NULL)
15452 return FALSE;
5bd4f169 15453
8387904d
AM
15454 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15455 if (ent->plt.offset != (bfd_vma) -1)
15456 {
15457 /* This symbol has an entry in the procedure linkage
15458 table. Set it up. */
e054468f
AM
15459 if (!htab->elf.dynamic_sections_created
15460 || h->dynindx == -1)
15461 {
15462 BFD_ASSERT (h->type == STT_GNU_IFUNC
15463 && h->def_regular
15464 && (h->root.type == bfd_link_hash_defined
15465 || h->root.type == bfd_link_hash_defweak));
33e44f2e
AM
15466 rela.r_offset = (htab->elf.iplt->output_section->vma
15467 + htab->elf.iplt->output_offset
25f23106 15468 + ent->plt.offset);
ee67d69a
AM
15469 if (htab->opd_abi)
15470 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15471 else
15472 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
15473 rela.r_addend = (h->root.u.def.value
15474 + h->root.u.def.section->output_offset
15475 + h->root.u.def.section->output_section->vma
15476 + ent->addend);
33e44f2e
AM
15477 loc = (htab->elf.irelplt->contents
15478 + (htab->elf.irelplt->reloc_count++
25f23106 15479 * sizeof (Elf64_External_Rela)));
82e66161 15480 htab->local_ifunc_resolver = 1;
e054468f
AM
15481 }
15482 else
15483 {
33e44f2e
AM
15484 rela.r_offset = (htab->elf.splt->output_section->vma
15485 + htab->elf.splt->output_offset
25f23106 15486 + ent->plt.offset);
e054468f
AM
15487 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15488 rela.r_addend = ent->addend;
33e44f2e 15489 loc = (htab->elf.srelplt->contents
b9e5796b
AM
15490 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15491 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
82e66161
AM
15492 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
15493 htab->maybe_local_ifunc_resolver = 1;
e054468f 15494 }
8387904d 15495 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a345bc8d
AM
15496
15497 if (!htab->opd_abi)
15498 {
15499 if (!h->def_regular)
15500 {
15501 /* Mark the symbol as undefined, rather than as
15502 defined in glink. Leave the value if there were
15503 any relocations where pointer equality matters
15504 (this is a clue for the dynamic linker, to make
15505 function pointer comparisons work between an
15506 application and shared library), otherwise set it
15507 to zero. */
15508 sym->st_shndx = SHN_UNDEF;
15509 if (!h->pointer_equality_needed)
15510 sym->st_value = 0;
15511 else if (!h->ref_regular_nonweak)
15512 {
15513 /* This breaks function pointer comparisons, but
15514 that is better than breaking tests for a NULL
15515 function pointer. */
15516 sym->st_value = 0;
15517 }
15518 }
15519 }
8387904d 15520 }
5bd4f169 15521
f5385ebf 15522 if (h->needs_copy)
5bd4f169 15523 {
65f38f15 15524 /* This symbol needs a copy reloc. Set it up. */
5474d94f 15525 asection *srel;
5bd4f169 15526
65f38f15
AM
15527 if (h->dynindx == -1
15528 || (h->root.type != bfd_link_hash_defined
15529 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
15530 || htab->elf.srelbss == NULL
15531 || htab->elf.sreldynrelro == NULL)
65f38f15 15532 abort ();
5bd4f169
AM
15533
15534 rela.r_offset = (h->root.u.def.value
15535 + h->root.u.def.section->output_section->vma
15536 + h->root.u.def.section->output_offset);
15537 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15538 rela.r_addend = 0;
afbf7e8e 15539 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
15540 srel = htab->elf.sreldynrelro;
15541 else
15542 srel = htab->elf.srelbss;
15543 loc = srel->contents;
15544 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15545 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15546 }
15547
b34976b6 15548 return TRUE;
5bd4f169
AM
15549}
15550
65f38f15
AM
15551/* Used to decide how to sort relocs in an optimal manner for the
15552 dynamic linker, before writing them out. */
15553
15554static enum elf_reloc_type_class
7e612e98
AM
15555ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15556 const asection *rel_sec,
15557 const Elf_Internal_Rela *rela)
65f38f15 15558{
04c9666a 15559 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15560 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15561
33e44f2e 15562 if (rel_sec == htab->elf.irelplt)
7e612e98 15563 return reloc_class_ifunc;
a33d1f77 15564
4ce794b7 15565 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15566 switch (r_type)
65f38f15
AM
15567 {
15568 case R_PPC64_RELATIVE:
15569 return reloc_class_relative;
15570 case R_PPC64_JMP_SLOT:
15571 return reloc_class_plt;
15572 case R_PPC64_COPY:
15573 return reloc_class_copy;
15574 default:
15575 return reloc_class_normal;
15576 }
15577}
15578
5bd4f169
AM
15579/* Finish up the dynamic sections. */
15580
b34976b6 15581static bfd_boolean
4ce794b7
AM
15582ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15583 struct bfd_link_info *info)
5bd4f169 15584{
65f38f15
AM
15585 struct ppc_link_hash_table *htab;
15586 bfd *dynobj;
5bd4f169 15587 asection *sdyn;
5bd4f169 15588
65f38f15 15589 htab = ppc_hash_table (info);
4dfe6ac6
NC
15590 if (htab == NULL)
15591 return FALSE;
15592
65f38f15 15593 dynobj = htab->elf.dynobj;
3d4d4302 15594 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15595
65f38f15 15596 if (htab->elf.dynamic_sections_created)
5bd4f169 15597 {
5bd4f169
AM
15598 Elf64_External_Dyn *dyncon, *dynconend;
15599
33e44f2e 15600 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15601 abort ();
5bd4f169
AM
15602
15603 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15604 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15605 for (; dyncon < dynconend; dyncon++)
15606 {
15607 Elf_Internal_Dyn dyn;
19397422 15608 asection *s;
5bd4f169
AM
15609
15610 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15611
15612 switch (dyn.d_tag)
15613 {
65f38f15
AM
15614 default:
15615 continue;
5bd4f169 15616
5d1634d7 15617 case DT_PPC64_GLINK:
4ce794b7 15618 s = htab->glink;
6348e046 15619 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15620 /* We stupidly defined DT_PPC64_GLINK to be the start
15621 of glink rather than the first entry point, which is
15622 what ld.so needs, and now have a bigger stub to
15623 support automatic multiple TOCs. */
9e390558 15624 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
15625 break;
15626
19397422
AM
15627 case DT_PPC64_OPD:
15628 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15629 if (s == NULL)
15630 continue;
15631 dyn.d_un.d_ptr = s->vma;
19397422
AM
15632 break;
15633
e8910a83
AM
15634 case DT_PPC64_OPT:
15635 if (htab->do_multi_toc && htab->multi_toc_needed)
15636 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
15637 if (htab->has_plt_localentry0)
15638 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
15639 break;
15640
19397422
AM
15641 case DT_PPC64_OPDSZ:
15642 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15643 if (s == NULL)
15644 continue;
eea6121a 15645 dyn.d_un.d_val = s->size;
19397422
AM
15646 break;
15647
65f38f15 15648 case DT_PLTGOT:
33e44f2e 15649 s = htab->elf.splt;
6348e046 15650 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15651 break;
15652
15653 case DT_JMPREL:
33e44f2e 15654 s = htab->elf.srelplt;
6348e046 15655 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15656 break;
5bd4f169 15657
65f38f15 15658 case DT_PLTRELSZ:
33e44f2e 15659 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 15660 break;
82e66161
AM
15661
15662 case DT_TEXTREL:
15663 if (htab->local_ifunc_resolver)
15664 info->callbacks->einfo
15665 (_("%X%P: text relocations and GNU indirect "
15666 "functions will result in a segfault at runtime\n"));
15667 else if (htab->maybe_local_ifunc_resolver)
15668 info->callbacks->einfo
15669 (_("%P: warning: text relocations and GNU indirect "
15670 "functions may result in a segfault at runtime\n"));
15671 continue;
5bd4f169 15672 }
5bd4f169 15673
65f38f15 15674 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15675 }
5bd4f169
AM
15676 }
15677
6528b6eb
AM
15678 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15679 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15680 {
15681 /* Fill in the first entry in the global offset table.
15682 We use it to hold the link-time TOCbase. */
15683 bfd_put_64 (output_bfd,
60ee0d4a 15684 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15685 htab->elf.sgot->contents);
5d1634d7
AM
15686
15687 /* Set .got entry size. */
33e44f2e 15688 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
15689 }
15690
6528b6eb
AM
15691 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15692 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15693 {
15694 /* Set .plt entry size. */
33e44f2e 15695 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15696 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15697 }
15698
84f5d08e
AM
15699 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15700 brlt ourselves if emitrelocations. */
15701 if (htab->brlt != NULL
15702 && htab->brlt->reloc_count != 0
15703 && !_bfd_elf_link_output_relocs (output_bfd,
15704 htab->brlt,
d4730f92 15705 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15706 elf_section_data (htab->brlt)->relocs,
15707 NULL))
15708 return FALSE;
15709
176a0d42
AM
15710 if (htab->glink != NULL
15711 && htab->glink->reloc_count != 0
15712 && !_bfd_elf_link_output_relocs (output_bfd,
15713 htab->glink,
d4730f92 15714 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15715 elf_section_data (htab->glink)->relocs,
15716 NULL))
15717 return FALSE;
15718
58d180e8 15719 if (htab->glink_eh_frame != NULL
da44f4e5
AM
15720 && htab->glink_eh_frame->size != 0)
15721 {
15722 bfd_vma val;
15723 bfd_byte *p;
d4aaa2a0 15724 struct map_stub *group;
2e0ce1c8 15725 size_t align = 4;
da44f4e5 15726
2e0ce1c8
AM
15727 p = htab->glink_eh_frame->contents;
15728 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
d4aaa2a0
AM
15729
15730 for (group = htab->group; group != NULL; group = group->next)
15731 if (group->stub_sec != NULL)
da44f4e5 15732 {
da44f4e5 15733 /* Offset to stub section. */
d4aaa2a0
AM
15734 val = (group->stub_sec->output_section->vma
15735 + group->stub_sec->output_offset);
da44f4e5
AM
15736 val -= (htab->glink_eh_frame->output_section->vma
15737 + htab->glink_eh_frame->output_offset
d4aaa2a0 15738 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
15739 if (val + 0x80000000 > 0xffffffff)
15740 {
cf97bcb0
AM
15741 _bfd_error_handler
15742 (_("%s offset too large for .eh_frame sdata4 encoding"),
d4aaa2a0 15743 group->stub_sec->name);
da44f4e5
AM
15744 return FALSE;
15745 }
d4aaa2a0
AM
15746 bfd_put_32 (dynobj, val, p + 8);
15747 p += stub_eh_frame_size (group, align);
da44f4e5
AM
15748 }
15749 if (htab->glink != NULL && htab->glink->size != 0)
15750 {
da44f4e5
AM
15751 /* Offset to .glink. */
15752 val = (htab->glink->output_section->vma
15753 + htab->glink->output_offset
15754 + 8);
15755 val -= (htab->glink_eh_frame->output_section->vma
15756 + htab->glink_eh_frame->output_offset
d4aaa2a0 15757 + (p + 8 - htab->glink_eh_frame->contents));
da44f4e5
AM
15758 if (val + 0x80000000 > 0xffffffff)
15759 {
cf97bcb0
AM
15760 _bfd_error_handler
15761 (_("%s offset too large for .eh_frame sdata4 encoding"),
da44f4e5
AM
15762 htab->glink->name);
15763 return FALSE;
15764 }
d4aaa2a0
AM
15765 bfd_put_32 (dynobj, val, p + 8);
15766 p += (24 + align - 1) & -align;
da44f4e5
AM
15767 }
15768
15769 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15770 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15771 htab->glink_eh_frame,
15772 htab->glink_eh_frame->contents))
15773 return FALSE;
15774 }
58d180e8 15775
e717da7e 15776 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15777 since we didn't add them to DYNOBJ. We know dynobj is the first
15778 bfd. */
c72f2fb2 15779 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15780 {
15781 asection *s;
7b53ace3 15782
0c8d6e5c 15783 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15784 continue;
15785
e717da7e
AM
15786 s = ppc64_elf_tdata (dynobj)->got;
15787 if (s != NULL
eea6121a 15788 && s->size != 0
e717da7e
AM
15789 && s->output_section != bfd_abs_section_ptr
15790 && !bfd_set_section_contents (output_bfd, s->output_section,
15791 s->contents, s->output_offset,
eea6121a 15792 s->size))
e717da7e
AM
15793 return FALSE;
15794 s = ppc64_elf_tdata (dynobj)->relgot;
15795 if (s != NULL
eea6121a 15796 && s->size != 0
e717da7e
AM
15797 && s->output_section != bfd_abs_section_ptr
15798 && !bfd_set_section_contents (output_bfd, s->output_section,
15799 s->contents, s->output_offset,
eea6121a 15800 s->size))
e717da7e
AM
15801 return FALSE;
15802 }
f6c52c13 15803
b34976b6 15804 return TRUE;
5bd4f169
AM
15805}
15806
5bd4f169 15807#include "elf64-target.h"
7b8e7dad
AM
15808
15809/* FreeBSD support */
15810
15811#undef TARGET_LITTLE_SYM
15812#undef TARGET_LITTLE_NAME
15813
15814#undef TARGET_BIG_SYM
6d00b590 15815#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15816#undef TARGET_BIG_NAME
15817#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15818
15819#undef ELF_OSABI
15820#define ELF_OSABI ELFOSABI_FREEBSD
15821
15822#undef elf64_bed
15823#define elf64_bed elf64_powerpc_fbsd_bed
15824
15825#include "elf64-target.h"
This page took 4.678124 seconds and 4 git commands to generate.