PowerPC/BFD: Convert `%P: %H:' to `%H:' in error messages
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
2571583a 2 Copyright (C) 1999-2017 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
74f0fb50 104#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 105#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
106#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
107#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
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
ad8e1ba5 124
5bd4f169
AM
125/* The name of the dynamic interpreter. This is put in the .interp
126 section. */
127#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
128
129/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 130#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
5bd4f169
AM
131
132/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 133#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 134
a078d95a
AM
135/* Offsets to some stack save slots. */
136#define STK_LR 16
137#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 138/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
139 CR save slot. Used only by optimised __tls_get_addr call stub,
140 relying on __tls_get_addr_opt not saving CR.. */
141#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
142
5bd4f169 143/* TOC base pointers offset from start of TOC. */
411e1bfb 144#define TOC_BASE_OFF 0x8000
a27e685f
AM
145/* TOC base alignment. */
146#define TOC_BASE_ALIGN 256
411e1bfb
AM
147
148/* Offset of tp and dtp pointers from start of TLS block. */
149#define TP_OFFSET 0x7000
150#define DTP_OFFSET 0x8000
5bd4f169 151
ad8e1ba5
AM
152/* .plt call stub instructions. The normal stub is like this, but
153 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 154 insert an addi to adjust r11. */
a078d95a 155#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
156#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
157#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
158#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
159#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
160#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
161#define BCTR 0x4e800420 /* bctr */
162
71a39c98 163#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
ad8e1ba5
AM
164#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
165#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
166
71a39c98
AM
167#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
168#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
169#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
794e51c0
AM
170#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
171#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
172#define BNECTR 0x4ca20420 /* bnectr+ */
173#define BNECTR_P4 0x4ce20420 /* bnectr+ */
174
71a39c98 175#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442
AM
176#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
177#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
178
a078d95a 179#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
006589cf
AM
180#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
181#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 182
006589cf
AM
183#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
184#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
185#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
a345bc8d
AM
186#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
187#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
188
ee4bf8d2 189/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 190#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
191 /* 0: */
192 /* .quad plt0-1f */
193 /* __glink: */
194#define MFLR_R12 0x7d8802a6 /* mflr %12 */
195#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
196 /* 1: */
197#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 198 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 199#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
200#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
201 /* ld %12,0(%11) */
202 /* ld %2,8(%11) */
203 /* mtctr %12 */
204 /* ld %11,16(%11) */
ee4bf8d2 205 /* bctr */
b9e5796b
AM
206#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
207#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
208#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
209#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
210#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
211
212/* Pad with this. */
213#define NOP 0x60000000
214
721956f4
AM
215/* Some other nops. */
216#define CROR_151515 0x4def7b82
217#define CROR_313131 0x4ffffb82
218
cedb70c5 219/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
220#define LI_R0_0 0x38000000 /* li %r0,0 */
221#define B_DOT 0x48000000 /* b . */
222
223/* After that, we need two instructions to load the index, followed by
224 a branch. */
225#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 226#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 227
deb0e272
AM
228/* Instructions used by the save and restore reg functions. */
229#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
230#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
231#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
232#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
233#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
234#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
235#define LI_R12_0 0x39800000 /* li %r12,0 */
236#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
237#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
238#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
239#define BLR 0x4e800020 /* blr */
240
41bd81ab
AM
241/* Since .opd is an array of descriptors and each entry will end up
242 with identical R_PPC64_RELATIVE relocs, there is really no need to
243 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 244 relocate .opd without reloc entries. */
41bd81ab
AM
245#ifndef NO_OPD_RELOCS
246#define NO_OPD_RELOCS 0
247#endif
810d4e75 248
a4b6fadd
AM
249#ifndef ARRAY_SIZE
250#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
251#endif
252
810d4e75
AM
253static inline int
254abiversion (bfd *abfd)
255{
256 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
257}
258
259static inline void
260set_abiversion (bfd *abfd, int ver)
261{
262 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
263 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
264}
5bd4f169 265\f
f5e87a1d 266#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 267
5bd4f169 268/* Relocation HOWTO's. */
04c9666a 269static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
270
271static reloc_howto_type ppc64_elf_howto_raw[] = {
272 /* This reloc does nothing. */
273 HOWTO (R_PPC64_NONE, /* type */
274 0, /* rightshift */
6346d5ca
AM
275 3, /* size (0 = byte, 1 = short, 2 = long) */
276 0, /* bitsize */
b34976b6 277 FALSE, /* pc_relative */
5bd4f169 278 0, /* bitpos */
f5e87a1d 279 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
280 bfd_elf_generic_reloc, /* special_function */
281 "R_PPC64_NONE", /* name */
b34976b6 282 FALSE, /* partial_inplace */
d006db6c 283 0, /* src_mask */
5bd4f169 284 0, /* dst_mask */
b34976b6 285 FALSE), /* pcrel_offset */
5bd4f169
AM
286
287 /* A standard 32 bit relocation. */
288 HOWTO (R_PPC64_ADDR32, /* type */
289 0, /* rightshift */
290 2, /* size (0 = byte, 1 = short, 2 = long) */
291 32, /* bitsize */
b34976b6 292 FALSE, /* pc_relative */
5bd4f169
AM
293 0, /* bitpos */
294 complain_overflow_bitfield, /* complain_on_overflow */
295 bfd_elf_generic_reloc, /* special_function */
296 "R_PPC64_ADDR32", /* name */
b34976b6 297 FALSE, /* partial_inplace */
5bd4f169
AM
298 0, /* src_mask */
299 0xffffffff, /* dst_mask */
b34976b6 300 FALSE), /* pcrel_offset */
5bd4f169
AM
301
302 /* An absolute 26 bit branch; the lower two bits must be zero.
303 FIXME: we don't check that, we just clear them. */
304 HOWTO (R_PPC64_ADDR24, /* type */
305 0, /* rightshift */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
307 26, /* bitsize */
b34976b6 308 FALSE, /* pc_relative */
5bd4f169
AM
309 0, /* bitpos */
310 complain_overflow_bitfield, /* complain_on_overflow */
311 bfd_elf_generic_reloc, /* special_function */
312 "R_PPC64_ADDR24", /* name */
b34976b6 313 FALSE, /* partial_inplace */
d006db6c 314 0, /* src_mask */
f5e87a1d 315 0x03fffffc, /* dst_mask */
b34976b6 316 FALSE), /* pcrel_offset */
5bd4f169
AM
317
318 /* A standard 16 bit relocation. */
319 HOWTO (R_PPC64_ADDR16, /* type */
320 0, /* rightshift */
321 1, /* size (0 = byte, 1 = short, 2 = long) */
322 16, /* bitsize */
b34976b6 323 FALSE, /* pc_relative */
5bd4f169
AM
324 0, /* bitpos */
325 complain_overflow_bitfield, /* complain_on_overflow */
326 bfd_elf_generic_reloc, /* special_function */
327 "R_PPC64_ADDR16", /* name */
b34976b6 328 FALSE, /* partial_inplace */
5bd4f169
AM
329 0, /* src_mask */
330 0xffff, /* dst_mask */
b34976b6 331 FALSE), /* pcrel_offset */
5bd4f169
AM
332
333 /* A 16 bit relocation without overflow. */
334 HOWTO (R_PPC64_ADDR16_LO, /* type */
335 0, /* rightshift */
336 1, /* size (0 = byte, 1 = short, 2 = long) */
337 16, /* bitsize */
b34976b6 338 FALSE, /* pc_relative */
5bd4f169
AM
339 0, /* bitpos */
340 complain_overflow_dont,/* complain_on_overflow */
341 bfd_elf_generic_reloc, /* special_function */
342 "R_PPC64_ADDR16_LO", /* name */
b34976b6 343 FALSE, /* partial_inplace */
5bd4f169
AM
344 0, /* src_mask */
345 0xffff, /* dst_mask */
b34976b6 346 FALSE), /* pcrel_offset */
5bd4f169
AM
347
348 /* Bits 16-31 of an address. */
349 HOWTO (R_PPC64_ADDR16_HI, /* type */
350 16, /* rightshift */
351 1, /* size (0 = byte, 1 = short, 2 = long) */
352 16, /* bitsize */
b34976b6 353 FALSE, /* pc_relative */
5bd4f169 354 0, /* bitpos */
f9c6b907 355 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
356 bfd_elf_generic_reloc, /* special_function */
357 "R_PPC64_ADDR16_HI", /* name */
b34976b6 358 FALSE, /* partial_inplace */
5bd4f169
AM
359 0, /* src_mask */
360 0xffff, /* dst_mask */
b34976b6 361 FALSE), /* pcrel_offset */
5bd4f169
AM
362
363 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
364 bits, treated as a signed number, is negative. */
365 HOWTO (R_PPC64_ADDR16_HA, /* type */
366 16, /* rightshift */
367 1, /* size (0 = byte, 1 = short, 2 = long) */
368 16, /* bitsize */
b34976b6 369 FALSE, /* pc_relative */
5bd4f169 370 0, /* bitpos */
f9c6b907 371 complain_overflow_signed, /* complain_on_overflow */
805fc799 372 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 373 "R_PPC64_ADDR16_HA", /* name */
b34976b6 374 FALSE, /* partial_inplace */
5bd4f169
AM
375 0, /* src_mask */
376 0xffff, /* dst_mask */
b34976b6 377 FALSE), /* pcrel_offset */
5bd4f169
AM
378
379 /* An absolute 16 bit branch; the lower two bits must be zero.
380 FIXME: we don't check that, we just clear them. */
381 HOWTO (R_PPC64_ADDR14, /* type */
382 0, /* rightshift */
383 2, /* size (0 = byte, 1 = short, 2 = long) */
384 16, /* bitsize */
b34976b6 385 FALSE, /* pc_relative */
5bd4f169 386 0, /* bitpos */
b80eed39 387 complain_overflow_signed, /* complain_on_overflow */
2441e016 388 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 389 "R_PPC64_ADDR14", /* name */
b34976b6 390 FALSE, /* partial_inplace */
d006db6c 391 0, /* src_mask */
f5e87a1d 392 0x0000fffc, /* dst_mask */
b34976b6 393 FALSE), /* pcrel_offset */
5bd4f169
AM
394
395 /* An absolute 16 bit branch, for which bit 10 should be set to
396 indicate that the branch is expected to be taken. The lower two
397 bits must be zero. */
398 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
399 0, /* rightshift */
400 2, /* size (0 = byte, 1 = short, 2 = long) */
401 16, /* bitsize */
b34976b6 402 FALSE, /* pc_relative */
5bd4f169 403 0, /* bitpos */
b80eed39 404 complain_overflow_signed, /* complain_on_overflow */
805fc799 405 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 406 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 407 FALSE, /* partial_inplace */
d006db6c 408 0, /* src_mask */
f5e87a1d 409 0x0000fffc, /* dst_mask */
b34976b6 410 FALSE), /* pcrel_offset */
5bd4f169
AM
411
412 /* An absolute 16 bit branch, for which bit 10 should be set to
413 indicate that the branch is not expected to be taken. The lower
414 two bits must be zero. */
415 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
416 0, /* rightshift */
417 2, /* size (0 = byte, 1 = short, 2 = long) */
418 16, /* bitsize */
b34976b6 419 FALSE, /* pc_relative */
5bd4f169 420 0, /* bitpos */
b80eed39 421 complain_overflow_signed, /* complain_on_overflow */
805fc799 422 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 423 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 424 FALSE, /* partial_inplace */
d006db6c 425 0, /* src_mask */
f5e87a1d 426 0x0000fffc, /* dst_mask */
b34976b6 427 FALSE), /* pcrel_offset */
5bd4f169
AM
428
429 /* A relative 26 bit branch; the lower two bits must be zero. */
430 HOWTO (R_PPC64_REL24, /* type */
431 0, /* rightshift */
432 2, /* size (0 = byte, 1 = short, 2 = long) */
433 26, /* bitsize */
b34976b6 434 TRUE, /* pc_relative */
5bd4f169
AM
435 0, /* bitpos */
436 complain_overflow_signed, /* complain_on_overflow */
2441e016 437 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 438 "R_PPC64_REL24", /* name */
b34976b6 439 FALSE, /* partial_inplace */
d006db6c 440 0, /* src_mask */
f5e87a1d 441 0x03fffffc, /* dst_mask */
b34976b6 442 TRUE), /* pcrel_offset */
5bd4f169
AM
443
444 /* A relative 16 bit branch; the lower two bits must be zero. */
445 HOWTO (R_PPC64_REL14, /* type */
446 0, /* rightshift */
447 2, /* size (0 = byte, 1 = short, 2 = long) */
448 16, /* bitsize */
b34976b6 449 TRUE, /* pc_relative */
5bd4f169
AM
450 0, /* bitpos */
451 complain_overflow_signed, /* complain_on_overflow */
2441e016 452 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 453 "R_PPC64_REL14", /* name */
b34976b6 454 FALSE, /* partial_inplace */
d006db6c 455 0, /* src_mask */
f5e87a1d 456 0x0000fffc, /* dst_mask */
b34976b6 457 TRUE), /* pcrel_offset */
5bd4f169
AM
458
459 /* A relative 16 bit branch. Bit 10 should be set to indicate that
460 the branch is expected to be taken. The lower two bits must be
461 zero. */
462 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
463 0, /* rightshift */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
465 16, /* bitsize */
b34976b6 466 TRUE, /* pc_relative */
5bd4f169
AM
467 0, /* bitpos */
468 complain_overflow_signed, /* complain_on_overflow */
805fc799 469 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 470 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 471 FALSE, /* partial_inplace */
d006db6c 472 0, /* src_mask */
f5e87a1d 473 0x0000fffc, /* dst_mask */
b34976b6 474 TRUE), /* pcrel_offset */
5bd4f169
AM
475
476 /* A relative 16 bit branch. Bit 10 should be set to indicate that
477 the branch is not expected to be taken. The lower two bits must
478 be zero. */
479 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
480 0, /* rightshift */
481 2, /* size (0 = byte, 1 = short, 2 = long) */
482 16, /* bitsize */
b34976b6 483 TRUE, /* pc_relative */
5bd4f169
AM
484 0, /* bitpos */
485 complain_overflow_signed, /* complain_on_overflow */
805fc799 486 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 487 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 488 FALSE, /* partial_inplace */
d006db6c 489 0, /* src_mask */
f5e87a1d 490 0x0000fffc, /* dst_mask */
b34976b6 491 TRUE), /* pcrel_offset */
5bd4f169
AM
492
493 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
494 symbol. */
495 HOWTO (R_PPC64_GOT16, /* type */
496 0, /* rightshift */
497 1, /* size (0 = byte, 1 = short, 2 = long) */
498 16, /* bitsize */
b34976b6 499 FALSE, /* pc_relative */
5bd4f169
AM
500 0, /* bitpos */
501 complain_overflow_signed, /* complain_on_overflow */
805fc799 502 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 503 "R_PPC64_GOT16", /* name */
b34976b6 504 FALSE, /* partial_inplace */
5bd4f169
AM
505 0, /* src_mask */
506 0xffff, /* dst_mask */
b34976b6 507 FALSE), /* pcrel_offset */
5bd4f169
AM
508
509 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
510 the symbol. */
511 HOWTO (R_PPC64_GOT16_LO, /* type */
512 0, /* rightshift */
513 1, /* size (0 = byte, 1 = short, 2 = long) */
514 16, /* bitsize */
b34976b6 515 FALSE, /* pc_relative */
5bd4f169
AM
516 0, /* bitpos */
517 complain_overflow_dont, /* complain_on_overflow */
805fc799 518 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 519 "R_PPC64_GOT16_LO", /* name */
b34976b6 520 FALSE, /* partial_inplace */
5bd4f169
AM
521 0, /* src_mask */
522 0xffff, /* dst_mask */
b34976b6 523 FALSE), /* pcrel_offset */
5bd4f169
AM
524
525 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
526 the symbol. */
527 HOWTO (R_PPC64_GOT16_HI, /* type */
528 16, /* rightshift */
529 1, /* size (0 = byte, 1 = short, 2 = long) */
530 16, /* bitsize */
b34976b6 531 FALSE, /* pc_relative */
5bd4f169 532 0, /* bitpos */
f9c6b907 533 complain_overflow_signed,/* complain_on_overflow */
805fc799 534 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 535 "R_PPC64_GOT16_HI", /* name */
b34976b6 536 FALSE, /* partial_inplace */
5bd4f169
AM
537 0, /* src_mask */
538 0xffff, /* dst_mask */
b34976b6 539 FALSE), /* pcrel_offset */
5bd4f169
AM
540
541 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
542 the symbol. */
543 HOWTO (R_PPC64_GOT16_HA, /* type */
544 16, /* rightshift */
545 1, /* size (0 = byte, 1 = short, 2 = long) */
546 16, /* bitsize */
b34976b6 547 FALSE, /* pc_relative */
5bd4f169 548 0, /* bitpos */
f9c6b907 549 complain_overflow_signed,/* complain_on_overflow */
805fc799 550 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 551 "R_PPC64_GOT16_HA", /* name */
b34976b6 552 FALSE, /* partial_inplace */
5bd4f169
AM
553 0, /* src_mask */
554 0xffff, /* dst_mask */
b34976b6 555 FALSE), /* pcrel_offset */
5bd4f169
AM
556
557 /* This is used only by the dynamic linker. The symbol should exist
558 both in the object being run and in some shared library. The
559 dynamic linker copies the data addressed by the symbol from the
560 shared library into the object, because the object being
561 run has to have the data at some particular address. */
562 HOWTO (R_PPC64_COPY, /* type */
563 0, /* rightshift */
f5e87a1d
AM
564 0, /* this one is variable size */
565 0, /* bitsize */
b34976b6 566 FALSE, /* pc_relative */
5bd4f169 567 0, /* bitpos */
f5e87a1d
AM
568 complain_overflow_dont, /* complain_on_overflow */
569 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 570 "R_PPC64_COPY", /* name */
b34976b6 571 FALSE, /* partial_inplace */
5bd4f169
AM
572 0, /* src_mask */
573 0, /* dst_mask */
b34976b6 574 FALSE), /* pcrel_offset */
5bd4f169
AM
575
576 /* Like R_PPC64_ADDR64, but used when setting global offset table
577 entries. */
578 HOWTO (R_PPC64_GLOB_DAT, /* type */
579 0, /* rightshift */
580 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
581 64, /* bitsize */
b34976b6 582 FALSE, /* pc_relative */
5bd4f169
AM
583 0, /* bitpos */
584 complain_overflow_dont, /* complain_on_overflow */
805fc799 585 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 586 "R_PPC64_GLOB_DAT", /* name */
b34976b6 587 FALSE, /* partial_inplace */
5bd4f169 588 0, /* src_mask */
f5e87a1d 589 ONES (64), /* dst_mask */
b34976b6 590 FALSE), /* pcrel_offset */
5bd4f169
AM
591
592 /* Created by the link editor. Marks a procedure linkage table
593 entry for a symbol. */
594 HOWTO (R_PPC64_JMP_SLOT, /* type */
595 0, /* rightshift */
596 0, /* size (0 = byte, 1 = short, 2 = long) */
597 0, /* bitsize */
b34976b6 598 FALSE, /* pc_relative */
5bd4f169
AM
599 0, /* bitpos */
600 complain_overflow_dont, /* complain_on_overflow */
805fc799 601 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 602 "R_PPC64_JMP_SLOT", /* name */
b34976b6 603 FALSE, /* partial_inplace */
5bd4f169
AM
604 0, /* src_mask */
605 0, /* dst_mask */
b34976b6 606 FALSE), /* pcrel_offset */
5bd4f169
AM
607
608 /* Used only by the dynamic linker. When the object is run, this
609 doubleword64 is set to the load address of the object, plus the
610 addend. */
611 HOWTO (R_PPC64_RELATIVE, /* type */
612 0, /* rightshift */
613 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
614 64, /* bitsize */
b34976b6 615 FALSE, /* pc_relative */
5bd4f169
AM
616 0, /* bitpos */
617 complain_overflow_dont, /* complain_on_overflow */
618 bfd_elf_generic_reloc, /* special_function */
619 "R_PPC64_RELATIVE", /* name */
b34976b6 620 FALSE, /* partial_inplace */
5bd4f169 621 0, /* src_mask */
f5e87a1d 622 ONES (64), /* dst_mask */
b34976b6 623 FALSE), /* pcrel_offset */
5bd4f169
AM
624
625 /* Like R_PPC64_ADDR32, but may be unaligned. */
626 HOWTO (R_PPC64_UADDR32, /* type */
627 0, /* rightshift */
628 2, /* size (0 = byte, 1 = short, 2 = long) */
629 32, /* bitsize */
b34976b6 630 FALSE, /* pc_relative */
5bd4f169
AM
631 0, /* bitpos */
632 complain_overflow_bitfield, /* complain_on_overflow */
633 bfd_elf_generic_reloc, /* special_function */
634 "R_PPC64_UADDR32", /* name */
b34976b6 635 FALSE, /* partial_inplace */
5bd4f169
AM
636 0, /* src_mask */
637 0xffffffff, /* dst_mask */
b34976b6 638 FALSE), /* pcrel_offset */
5bd4f169
AM
639
640 /* Like R_PPC64_ADDR16, but may be unaligned. */
641 HOWTO (R_PPC64_UADDR16, /* type */
642 0, /* rightshift */
643 1, /* size (0 = byte, 1 = short, 2 = long) */
644 16, /* bitsize */
b34976b6 645 FALSE, /* pc_relative */
5bd4f169
AM
646 0, /* bitpos */
647 complain_overflow_bitfield, /* complain_on_overflow */
648 bfd_elf_generic_reloc, /* special_function */
649 "R_PPC64_UADDR16", /* name */
b34976b6 650 FALSE, /* partial_inplace */
5bd4f169
AM
651 0, /* src_mask */
652 0xffff, /* dst_mask */
b34976b6 653 FALSE), /* pcrel_offset */
5bd4f169
AM
654
655 /* 32-bit PC relative. */
656 HOWTO (R_PPC64_REL32, /* type */
657 0, /* rightshift */
658 2, /* size (0 = byte, 1 = short, 2 = long) */
659 32, /* bitsize */
b34976b6 660 TRUE, /* pc_relative */
5bd4f169 661 0, /* bitpos */
5bd4f169
AM
662 complain_overflow_signed, /* complain_on_overflow */
663 bfd_elf_generic_reloc, /* special_function */
664 "R_PPC64_REL32", /* name */
b34976b6 665 FALSE, /* partial_inplace */
5bd4f169
AM
666 0, /* src_mask */
667 0xffffffff, /* dst_mask */
b34976b6 668 TRUE), /* pcrel_offset */
5bd4f169 669
10ed1bba 670 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
671 HOWTO (R_PPC64_PLT32, /* type */
672 0, /* rightshift */
673 2, /* size (0 = byte, 1 = short, 2 = long) */
674 32, /* bitsize */
b34976b6 675 FALSE, /* pc_relative */
5bd4f169
AM
676 0, /* bitpos */
677 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 678 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 679 "R_PPC64_PLT32", /* name */
b34976b6 680 FALSE, /* partial_inplace */
5bd4f169 681 0, /* src_mask */
f5e87a1d 682 0xffffffff, /* dst_mask */
b34976b6 683 FALSE), /* pcrel_offset */
5bd4f169
AM
684
685 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
686 FIXME: R_PPC64_PLTREL32 not supported. */
687 HOWTO (R_PPC64_PLTREL32, /* type */
688 0, /* rightshift */
689 2, /* size (0 = byte, 1 = short, 2 = long) */
690 32, /* bitsize */
b34976b6 691 TRUE, /* pc_relative */
5bd4f169
AM
692 0, /* bitpos */
693 complain_overflow_signed, /* complain_on_overflow */
3ce51288 694 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 695 "R_PPC64_PLTREL32", /* name */
b34976b6 696 FALSE, /* partial_inplace */
5bd4f169 697 0, /* src_mask */
f5e87a1d 698 0xffffffff, /* dst_mask */
b34976b6 699 TRUE), /* pcrel_offset */
5bd4f169
AM
700
701 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
702 the symbol. */
703 HOWTO (R_PPC64_PLT16_LO, /* type */
704 0, /* rightshift */
705 1, /* size (0 = byte, 1 = short, 2 = long) */
706 16, /* bitsize */
b34976b6 707 FALSE, /* pc_relative */
5bd4f169
AM
708 0, /* bitpos */
709 complain_overflow_dont, /* complain_on_overflow */
805fc799 710 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 711 "R_PPC64_PLT16_LO", /* name */
b34976b6 712 FALSE, /* partial_inplace */
5bd4f169
AM
713 0, /* src_mask */
714 0xffff, /* dst_mask */
b34976b6 715 FALSE), /* pcrel_offset */
5bd4f169
AM
716
717 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
718 the symbol. */
719 HOWTO (R_PPC64_PLT16_HI, /* type */
720 16, /* rightshift */
721 1, /* size (0 = byte, 1 = short, 2 = long) */
722 16, /* bitsize */
b34976b6 723 FALSE, /* pc_relative */
5bd4f169 724 0, /* bitpos */
f9c6b907 725 complain_overflow_signed, /* complain_on_overflow */
805fc799 726 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 727 "R_PPC64_PLT16_HI", /* name */
b34976b6 728 FALSE, /* partial_inplace */
5bd4f169
AM
729 0, /* src_mask */
730 0xffff, /* dst_mask */
b34976b6 731 FALSE), /* pcrel_offset */
5bd4f169
AM
732
733 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
734 the symbol. */
735 HOWTO (R_PPC64_PLT16_HA, /* type */
736 16, /* rightshift */
737 1, /* size (0 = byte, 1 = short, 2 = long) */
738 16, /* bitsize */
b34976b6 739 FALSE, /* pc_relative */
5bd4f169 740 0, /* bitpos */
f9c6b907 741 complain_overflow_signed, /* complain_on_overflow */
805fc799 742 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 743 "R_PPC64_PLT16_HA", /* name */
b34976b6 744 FALSE, /* partial_inplace */
5bd4f169
AM
745 0, /* src_mask */
746 0xffff, /* dst_mask */
b34976b6 747 FALSE), /* pcrel_offset */
5bd4f169 748
c061c2d8 749 /* 16-bit section relative relocation. */
5bd4f169
AM
750 HOWTO (R_PPC64_SECTOFF, /* type */
751 0, /* rightshift */
c061c2d8
AM
752 1, /* size (0 = byte, 1 = short, 2 = long) */
753 16, /* bitsize */
b34976b6 754 FALSE, /* pc_relative */
5bd4f169 755 0, /* bitpos */
b80eed39 756 complain_overflow_signed, /* complain_on_overflow */
805fc799 757 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 758 "R_PPC64_SECTOFF", /* name */
b34976b6 759 FALSE, /* partial_inplace */
5bd4f169 760 0, /* src_mask */
c061c2d8 761 0xffff, /* dst_mask */
b34976b6 762 FALSE), /* pcrel_offset */
5bd4f169 763
c061c2d8 764 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
765 HOWTO (R_PPC64_SECTOFF_LO, /* type */
766 0, /* rightshift */
767 1, /* size (0 = byte, 1 = short, 2 = long) */
768 16, /* bitsize */
b34976b6 769 FALSE, /* pc_relative */
5bd4f169
AM
770 0, /* bitpos */
771 complain_overflow_dont, /* complain_on_overflow */
805fc799 772 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 773 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 774 FALSE, /* partial_inplace */
5bd4f169
AM
775 0, /* src_mask */
776 0xffff, /* dst_mask */
b34976b6 777 FALSE), /* pcrel_offset */
5bd4f169
AM
778
779 /* 16-bit upper half section relative relocation. */
780 HOWTO (R_PPC64_SECTOFF_HI, /* type */
781 16, /* rightshift */
782 1, /* size (0 = byte, 1 = short, 2 = long) */
783 16, /* bitsize */
b34976b6 784 FALSE, /* pc_relative */
5bd4f169 785 0, /* bitpos */
f9c6b907 786 complain_overflow_signed, /* complain_on_overflow */
805fc799 787 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 788 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 789 FALSE, /* partial_inplace */
5bd4f169
AM
790 0, /* src_mask */
791 0xffff, /* dst_mask */
b34976b6 792 FALSE), /* pcrel_offset */
5bd4f169
AM
793
794 /* 16-bit upper half adjusted section relative relocation. */
795 HOWTO (R_PPC64_SECTOFF_HA, /* type */
796 16, /* rightshift */
797 1, /* size (0 = byte, 1 = short, 2 = long) */
798 16, /* bitsize */
b34976b6 799 FALSE, /* pc_relative */
5bd4f169 800 0, /* bitpos */
f9c6b907 801 complain_overflow_signed, /* complain_on_overflow */
805fc799 802 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 803 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 804 FALSE, /* partial_inplace */
5bd4f169
AM
805 0, /* src_mask */
806 0xffff, /* dst_mask */
b34976b6 807 FALSE), /* pcrel_offset */
5bd4f169 808
04c9666a
AM
809 /* Like R_PPC64_REL24 without touching the two least significant bits. */
810 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
811 2, /* rightshift */
812 2, /* size (0 = byte, 1 = short, 2 = long) */
813 30, /* bitsize */
b34976b6 814 TRUE, /* pc_relative */
5bd4f169
AM
815 0, /* bitpos */
816 complain_overflow_dont, /* complain_on_overflow */
817 bfd_elf_generic_reloc, /* special_function */
04c9666a 818 "R_PPC64_REL30", /* name */
b34976b6 819 FALSE, /* partial_inplace */
d006db6c 820 0, /* src_mask */
5bd4f169 821 0xfffffffc, /* dst_mask */
b34976b6 822 TRUE), /* pcrel_offset */
5bd4f169
AM
823
824 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
825
826 /* A standard 64-bit relocation. */
827 HOWTO (R_PPC64_ADDR64, /* type */
828 0, /* rightshift */
829 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
830 64, /* bitsize */
b34976b6 831 FALSE, /* pc_relative */
5bd4f169
AM
832 0, /* bitpos */
833 complain_overflow_dont, /* complain_on_overflow */
834 bfd_elf_generic_reloc, /* special_function */
835 "R_PPC64_ADDR64", /* name */
b34976b6 836 FALSE, /* partial_inplace */
5bd4f169 837 0, /* src_mask */
f5e87a1d 838 ONES (64), /* dst_mask */
b34976b6 839 FALSE), /* pcrel_offset */
5bd4f169
AM
840
841 /* The bits 32-47 of an address. */
842 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
843 32, /* rightshift */
844 1, /* size (0 = byte, 1 = short, 2 = long) */
845 16, /* bitsize */
b34976b6 846 FALSE, /* pc_relative */
5bd4f169
AM
847 0, /* bitpos */
848 complain_overflow_dont, /* complain_on_overflow */
849 bfd_elf_generic_reloc, /* special_function */
850 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 851 FALSE, /* partial_inplace */
5bd4f169
AM
852 0, /* src_mask */
853 0xffff, /* dst_mask */
b34976b6 854 FALSE), /* pcrel_offset */
5bd4f169
AM
855
856 /* The bits 32-47 of an address, plus 1 if the contents of the low
857 16 bits, treated as a signed number, is negative. */
858 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
859 32, /* rightshift */
860 1, /* size (0 = byte, 1 = short, 2 = long) */
861 16, /* bitsize */
b34976b6 862 FALSE, /* pc_relative */
5bd4f169
AM
863 0, /* bitpos */
864 complain_overflow_dont, /* complain_on_overflow */
805fc799 865 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 866 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 867 FALSE, /* partial_inplace */
5bd4f169
AM
868 0, /* src_mask */
869 0xffff, /* dst_mask */
b34976b6 870 FALSE), /* pcrel_offset */
5bd4f169
AM
871
872 /* The bits 48-63 of an address. */
873 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
874 48, /* rightshift */
875 1, /* size (0 = byte, 1 = short, 2 = long) */
876 16, /* bitsize */
b34976b6 877 FALSE, /* pc_relative */
5bd4f169
AM
878 0, /* bitpos */
879 complain_overflow_dont, /* complain_on_overflow */
880 bfd_elf_generic_reloc, /* special_function */
881 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 882 FALSE, /* partial_inplace */
5bd4f169
AM
883 0, /* src_mask */
884 0xffff, /* dst_mask */
b34976b6 885 FALSE), /* pcrel_offset */
5bd4f169
AM
886
887 /* The bits 48-63 of an address, plus 1 if the contents of the low
888 16 bits, treated as a signed number, is negative. */
889 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
890 48, /* rightshift */
891 1, /* size (0 = byte, 1 = short, 2 = long) */
892 16, /* bitsize */
b34976b6 893 FALSE, /* pc_relative */
5bd4f169
AM
894 0, /* bitpos */
895 complain_overflow_dont, /* complain_on_overflow */
805fc799 896 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 897 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 898 FALSE, /* partial_inplace */
5bd4f169
AM
899 0, /* src_mask */
900 0xffff, /* dst_mask */
b34976b6 901 FALSE), /* pcrel_offset */
5bd4f169
AM
902
903 /* Like ADDR64, but may be unaligned. */
904 HOWTO (R_PPC64_UADDR64, /* type */
905 0, /* rightshift */
906 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
907 64, /* bitsize */
b34976b6 908 FALSE, /* pc_relative */
5bd4f169
AM
909 0, /* bitpos */
910 complain_overflow_dont, /* complain_on_overflow */
911 bfd_elf_generic_reloc, /* special_function */
912 "R_PPC64_UADDR64", /* name */
b34976b6 913 FALSE, /* partial_inplace */
5bd4f169 914 0, /* src_mask */
f5e87a1d 915 ONES (64), /* dst_mask */
b34976b6 916 FALSE), /* pcrel_offset */
5bd4f169
AM
917
918 /* 64-bit relative relocation. */
919 HOWTO (R_PPC64_REL64, /* type */
920 0, /* rightshift */
921 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
922 64, /* bitsize */
b34976b6 923 TRUE, /* pc_relative */
5bd4f169
AM
924 0, /* bitpos */
925 complain_overflow_dont, /* complain_on_overflow */
926 bfd_elf_generic_reloc, /* special_function */
927 "R_PPC64_REL64", /* name */
b34976b6 928 FALSE, /* partial_inplace */
5bd4f169 929 0, /* src_mask */
f5e87a1d 930 ONES (64), /* dst_mask */
b34976b6 931 TRUE), /* pcrel_offset */
5bd4f169 932
cedb70c5 933 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
934 HOWTO (R_PPC64_PLT64, /* type */
935 0, /* rightshift */
936 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
937 64, /* bitsize */
b34976b6 938 FALSE, /* pc_relative */
5bd4f169
AM
939 0, /* bitpos */
940 complain_overflow_dont, /* complain_on_overflow */
805fc799 941 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 942 "R_PPC64_PLT64", /* name */
b34976b6 943 FALSE, /* partial_inplace */
5bd4f169 944 0, /* src_mask */
f5e87a1d 945 ONES (64), /* dst_mask */
b34976b6 946 FALSE), /* pcrel_offset */
5bd4f169
AM
947
948 /* 64-bit PC relative relocation to the symbol's procedure linkage
949 table. */
950 /* FIXME: R_PPC64_PLTREL64 not supported. */
951 HOWTO (R_PPC64_PLTREL64, /* type */
952 0, /* rightshift */
953 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
954 64, /* bitsize */
b34976b6 955 TRUE, /* pc_relative */
5bd4f169
AM
956 0, /* bitpos */
957 complain_overflow_dont, /* complain_on_overflow */
805fc799 958 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 959 "R_PPC64_PLTREL64", /* name */
b34976b6 960 FALSE, /* partial_inplace */
5bd4f169 961 0, /* src_mask */
f5e87a1d 962 ONES (64), /* dst_mask */
b34976b6 963 TRUE), /* pcrel_offset */
5bd4f169
AM
964
965 /* 16 bit TOC-relative relocation. */
966
967 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
968 HOWTO (R_PPC64_TOC16, /* type */
969 0, /* rightshift */
970 1, /* size (0 = byte, 1 = short, 2 = long) */
971 16, /* bitsize */
b34976b6 972 FALSE, /* pc_relative */
5bd4f169
AM
973 0, /* bitpos */
974 complain_overflow_signed, /* complain_on_overflow */
805fc799 975 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 976 "R_PPC64_TOC16", /* name */
b34976b6 977 FALSE, /* partial_inplace */
5bd4f169
AM
978 0, /* src_mask */
979 0xffff, /* dst_mask */
b34976b6 980 FALSE), /* pcrel_offset */
5bd4f169
AM
981
982 /* 16 bit TOC-relative relocation without overflow. */
983
984 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
985 HOWTO (R_PPC64_TOC16_LO, /* type */
986 0, /* rightshift */
987 1, /* size (0 = byte, 1 = short, 2 = long) */
988 16, /* bitsize */
b34976b6 989 FALSE, /* pc_relative */
5bd4f169
AM
990 0, /* bitpos */
991 complain_overflow_dont, /* complain_on_overflow */
805fc799 992 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 993 "R_PPC64_TOC16_LO", /* name */
b34976b6 994 FALSE, /* partial_inplace */
5bd4f169
AM
995 0, /* src_mask */
996 0xffff, /* dst_mask */
b34976b6 997 FALSE), /* pcrel_offset */
5bd4f169
AM
998
999 /* 16 bit TOC-relative relocation, high 16 bits. */
1000
1001 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1002 HOWTO (R_PPC64_TOC16_HI, /* type */
1003 16, /* rightshift */
1004 1, /* size (0 = byte, 1 = short, 2 = long) */
1005 16, /* bitsize */
b34976b6 1006 FALSE, /* pc_relative */
5bd4f169 1007 0, /* bitpos */
f9c6b907 1008 complain_overflow_signed, /* complain_on_overflow */
805fc799 1009 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 1010 "R_PPC64_TOC16_HI", /* name */
b34976b6 1011 FALSE, /* partial_inplace */
5bd4f169
AM
1012 0, /* src_mask */
1013 0xffff, /* dst_mask */
b34976b6 1014 FALSE), /* pcrel_offset */
5bd4f169
AM
1015
1016 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1017 contents of the low 16 bits, treated as a signed number, is
1018 negative. */
1019
1020 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1021 HOWTO (R_PPC64_TOC16_HA, /* type */
1022 16, /* rightshift */
1023 1, /* size (0 = byte, 1 = short, 2 = long) */
1024 16, /* bitsize */
b34976b6 1025 FALSE, /* pc_relative */
5bd4f169 1026 0, /* bitpos */
f9c6b907 1027 complain_overflow_signed, /* complain_on_overflow */
805fc799 1028 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 1029 "R_PPC64_TOC16_HA", /* name */
b34976b6 1030 FALSE, /* partial_inplace */
5bd4f169
AM
1031 0, /* src_mask */
1032 0xffff, /* dst_mask */
b34976b6 1033 FALSE), /* pcrel_offset */
5bd4f169
AM
1034
1035 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1036
1037 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1038 HOWTO (R_PPC64_TOC, /* type */
1039 0, /* rightshift */
1040 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1041 64, /* bitsize */
b34976b6 1042 FALSE, /* pc_relative */
5bd4f169 1043 0, /* bitpos */
b80eed39 1044 complain_overflow_dont, /* complain_on_overflow */
805fc799 1045 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1046 "R_PPC64_TOC", /* name */
b34976b6 1047 FALSE, /* partial_inplace */
5bd4f169 1048 0, /* src_mask */
f5e87a1d 1049 ONES (64), /* dst_mask */
b34976b6 1050 FALSE), /* pcrel_offset */
5bd4f169
AM
1051
1052 /* Like R_PPC64_GOT16, but also informs the link editor that the
1053 value to relocate may (!) refer to a PLT entry which the link
1054 editor (a) may replace with the symbol value. If the link editor
1055 is unable to fully resolve the symbol, it may (b) create a PLT
1056 entry and store the address to the new PLT entry in the GOT.
1057 This permits lazy resolution of function symbols at run time.
1058 The link editor may also skip all of this and just (c) emit a
1059 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1060 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1061 HOWTO (R_PPC64_PLTGOT16, /* type */
1062 0, /* rightshift */
1063 1, /* size (0 = byte, 1 = short, 2 = long) */
1064 16, /* bitsize */
b34976b6 1065 FALSE, /* pc_relative */
5bd4f169
AM
1066 0, /* bitpos */
1067 complain_overflow_signed, /* complain_on_overflow */
805fc799 1068 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1069 "R_PPC64_PLTGOT16", /* name */
1070 FALSE, /* partial_inplace */
1071 0, /* src_mask */
1072 0xffff, /* dst_mask */
1073 FALSE), /* pcrel_offset */
1074
1075 /* Like R_PPC64_PLTGOT16, but without overflow. */
1076 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1077 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1078 0, /* rightshift */
1079 1, /* size (0 = byte, 1 = short, 2 = long) */
1080 16, /* bitsize */
1081 FALSE, /* pc_relative */
1082 0, /* bitpos */
1083 complain_overflow_dont, /* complain_on_overflow */
1084 ppc64_elf_unhandled_reloc, /* special_function */
1085 "R_PPC64_PLTGOT16_LO", /* name */
1086 FALSE, /* partial_inplace */
1087 0, /* src_mask */
1088 0xffff, /* dst_mask */
1089 FALSE), /* pcrel_offset */
1090
1091 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1092 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1093 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1094 16, /* rightshift */
1095 1, /* size (0 = byte, 1 = short, 2 = long) */
1096 16, /* bitsize */
1097 FALSE, /* pc_relative */
1098 0, /* bitpos */
f9c6b907 1099 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1100 ppc64_elf_unhandled_reloc, /* special_function */
1101 "R_PPC64_PLTGOT16_HI", /* name */
1102 FALSE, /* partial_inplace */
1103 0, /* src_mask */
1104 0xffff, /* dst_mask */
1105 FALSE), /* pcrel_offset */
1106
1107 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1108 1 if the contents of the low 16 bits, treated as a signed number,
1109 is negative. */
1110 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1111 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1112 16, /* rightshift */
1113 1, /* size (0 = byte, 1 = short, 2 = long) */
1114 16, /* bitsize */
1115 FALSE, /* pc_relative */
1116 0, /* bitpos */
f9c6b907 1117 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1118 ppc64_elf_unhandled_reloc, /* special_function */
1119 "R_PPC64_PLTGOT16_HA", /* name */
1120 FALSE, /* partial_inplace */
1121 0, /* src_mask */
1122 0xffff, /* dst_mask */
1123 FALSE), /* pcrel_offset */
1124
1125 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1126 HOWTO (R_PPC64_ADDR16_DS, /* type */
1127 0, /* rightshift */
1128 1, /* size (0 = byte, 1 = short, 2 = long) */
1129 16, /* bitsize */
1130 FALSE, /* pc_relative */
1131 0, /* bitpos */
b80eed39 1132 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1133 bfd_elf_generic_reloc, /* special_function */
1134 "R_PPC64_ADDR16_DS", /* name */
1135 FALSE, /* partial_inplace */
1136 0, /* src_mask */
1137 0xfffc, /* dst_mask */
1138 FALSE), /* pcrel_offset */
1139
1140 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1141 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1142 0, /* rightshift */
1143 1, /* size (0 = byte, 1 = short, 2 = long) */
1144 16, /* bitsize */
1145 FALSE, /* pc_relative */
1146 0, /* bitpos */
1147 complain_overflow_dont,/* complain_on_overflow */
1148 bfd_elf_generic_reloc, /* special_function */
1149 "R_PPC64_ADDR16_LO_DS",/* name */
1150 FALSE, /* partial_inplace */
1151 0, /* src_mask */
1152 0xfffc, /* dst_mask */
1153 FALSE), /* pcrel_offset */
1154
1155 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1156 HOWTO (R_PPC64_GOT16_DS, /* type */
1157 0, /* rightshift */
1158 1, /* size (0 = byte, 1 = short, 2 = long) */
1159 16, /* bitsize */
1160 FALSE, /* pc_relative */
1161 0, /* bitpos */
1162 complain_overflow_signed, /* complain_on_overflow */
1163 ppc64_elf_unhandled_reloc, /* special_function */
1164 "R_PPC64_GOT16_DS", /* name */
1165 FALSE, /* partial_inplace */
1166 0, /* src_mask */
1167 0xfffc, /* dst_mask */
1168 FALSE), /* pcrel_offset */
1169
1170 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1171 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1172 0, /* rightshift */
1173 1, /* size (0 = byte, 1 = short, 2 = long) */
1174 16, /* bitsize */
1175 FALSE, /* pc_relative */
1176 0, /* bitpos */
1177 complain_overflow_dont, /* complain_on_overflow */
1178 ppc64_elf_unhandled_reloc, /* special_function */
1179 "R_PPC64_GOT16_LO_DS", /* name */
1180 FALSE, /* partial_inplace */
1181 0, /* src_mask */
1182 0xfffc, /* dst_mask */
1183 FALSE), /* pcrel_offset */
1184
1185 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1186 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1187 0, /* rightshift */
1188 1, /* size (0 = byte, 1 = short, 2 = long) */
1189 16, /* bitsize */
1190 FALSE, /* pc_relative */
1191 0, /* bitpos */
1192 complain_overflow_dont, /* complain_on_overflow */
1193 ppc64_elf_unhandled_reloc, /* special_function */
1194 "R_PPC64_PLT16_LO_DS", /* name */
1195 FALSE, /* partial_inplace */
1196 0, /* src_mask */
1197 0xfffc, /* dst_mask */
1198 FALSE), /* pcrel_offset */
1199
1200 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1201 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1202 0, /* rightshift */
1203 1, /* size (0 = byte, 1 = short, 2 = long) */
1204 16, /* bitsize */
1205 FALSE, /* pc_relative */
1206 0, /* bitpos */
b80eed39 1207 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1208 ppc64_elf_sectoff_reloc, /* special_function */
1209 "R_PPC64_SECTOFF_DS", /* name */
1210 FALSE, /* partial_inplace */
1211 0, /* src_mask */
1212 0xfffc, /* dst_mask */
1213 FALSE), /* pcrel_offset */
1214
1215 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1216 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1217 0, /* rightshift */
1218 1, /* size (0 = byte, 1 = short, 2 = long) */
1219 16, /* bitsize */
1220 FALSE, /* pc_relative */
1221 0, /* bitpos */
1222 complain_overflow_dont, /* complain_on_overflow */
1223 ppc64_elf_sectoff_reloc, /* special_function */
1224 "R_PPC64_SECTOFF_LO_DS",/* name */
1225 FALSE, /* partial_inplace */
1226 0, /* src_mask */
1227 0xfffc, /* dst_mask */
1228 FALSE), /* pcrel_offset */
1229
1230 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1231 HOWTO (R_PPC64_TOC16_DS, /* type */
1232 0, /* rightshift */
1233 1, /* size (0 = byte, 1 = short, 2 = long) */
1234 16, /* bitsize */
1235 FALSE, /* pc_relative */
1236 0, /* bitpos */
1237 complain_overflow_signed, /* complain_on_overflow */
1238 ppc64_elf_toc_reloc, /* special_function */
1239 "R_PPC64_TOC16_DS", /* name */
1240 FALSE, /* partial_inplace */
1241 0, /* src_mask */
1242 0xfffc, /* dst_mask */
1243 FALSE), /* pcrel_offset */
1244
1245 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1246 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1247 0, /* rightshift */
1248 1, /* size (0 = byte, 1 = short, 2 = long) */
1249 16, /* bitsize */
1250 FALSE, /* pc_relative */
1251 0, /* bitpos */
1252 complain_overflow_dont, /* complain_on_overflow */
1253 ppc64_elf_toc_reloc, /* special_function */
1254 "R_PPC64_TOC16_LO_DS", /* name */
1255 FALSE, /* partial_inplace */
1256 0, /* src_mask */
1257 0xfffc, /* dst_mask */
1258 FALSE), /* pcrel_offset */
1259
1260 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1261 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1262 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1263 0, /* rightshift */
1264 1, /* size (0 = byte, 1 = short, 2 = long) */
1265 16, /* bitsize */
1266 FALSE, /* pc_relative */
1267 0, /* bitpos */
1268 complain_overflow_signed, /* complain_on_overflow */
1269 ppc64_elf_unhandled_reloc, /* special_function */
1270 "R_PPC64_PLTGOT16_DS", /* name */
1271 FALSE, /* partial_inplace */
1272 0, /* src_mask */
1273 0xfffc, /* dst_mask */
1274 FALSE), /* pcrel_offset */
1275
1276 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1277 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1278 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1279 0, /* rightshift */
1280 1, /* size (0 = byte, 1 = short, 2 = long) */
1281 16, /* bitsize */
1282 FALSE, /* pc_relative */
1283 0, /* bitpos */
1284 complain_overflow_dont, /* complain_on_overflow */
1285 ppc64_elf_unhandled_reloc, /* special_function */
1286 "R_PPC64_PLTGOT16_LO_DS",/* name */
1287 FALSE, /* partial_inplace */
1288 0, /* src_mask */
1289 0xfffc, /* dst_mask */
1290 FALSE), /* pcrel_offset */
1291
727fc41e 1292 /* Marker relocs for TLS. */
411e1bfb
AM
1293 HOWTO (R_PPC64_TLS,
1294 0, /* rightshift */
1295 2, /* size (0 = byte, 1 = short, 2 = long) */
1296 32, /* bitsize */
1297 FALSE, /* pc_relative */
1298 0, /* bitpos */
1299 complain_overflow_dont, /* complain_on_overflow */
1300 bfd_elf_generic_reloc, /* special_function */
1301 "R_PPC64_TLS", /* name */
1302 FALSE, /* partial_inplace */
1303 0, /* src_mask */
1304 0, /* dst_mask */
1305 FALSE), /* pcrel_offset */
1306
727fc41e
AM
1307 HOWTO (R_PPC64_TLSGD,
1308 0, /* rightshift */
1309 2, /* size (0 = byte, 1 = short, 2 = long) */
1310 32, /* bitsize */
1311 FALSE, /* pc_relative */
1312 0, /* bitpos */
1313 complain_overflow_dont, /* complain_on_overflow */
1314 bfd_elf_generic_reloc, /* special_function */
1315 "R_PPC64_TLSGD", /* name */
1316 FALSE, /* partial_inplace */
1317 0, /* src_mask */
1318 0, /* dst_mask */
1319 FALSE), /* pcrel_offset */
1320
1321 HOWTO (R_PPC64_TLSLD,
1322 0, /* rightshift */
1323 2, /* size (0 = byte, 1 = short, 2 = long) */
1324 32, /* bitsize */
1325 FALSE, /* pc_relative */
1326 0, /* bitpos */
1327 complain_overflow_dont, /* complain_on_overflow */
1328 bfd_elf_generic_reloc, /* special_function */
1329 "R_PPC64_TLSLD", /* name */
1330 FALSE, /* partial_inplace */
1331 0, /* src_mask */
1332 0, /* dst_mask */
1333 FALSE), /* pcrel_offset */
1334
3b421ab3
AM
1335 HOWTO (R_PPC64_TOCSAVE,
1336 0, /* rightshift */
1337 2, /* size (0 = byte, 1 = short, 2 = long) */
1338 32, /* bitsize */
1339 FALSE, /* pc_relative */
1340 0, /* bitpos */
1341 complain_overflow_dont, /* complain_on_overflow */
1342 bfd_elf_generic_reloc, /* special_function */
1343 "R_PPC64_TOCSAVE", /* name */
1344 FALSE, /* partial_inplace */
1345 0, /* src_mask */
1346 0, /* dst_mask */
1347 FALSE), /* pcrel_offset */
1348
411e1bfb
AM
1349 /* Computes the load module index of the load module that contains the
1350 definition of its TLS sym. */
1351 HOWTO (R_PPC64_DTPMOD64,
1352 0, /* rightshift */
1353 4, /* size (0 = byte, 1 = short, 2 = long) */
1354 64, /* bitsize */
1355 FALSE, /* pc_relative */
1356 0, /* bitpos */
1357 complain_overflow_dont, /* complain_on_overflow */
1358 ppc64_elf_unhandled_reloc, /* special_function */
1359 "R_PPC64_DTPMOD64", /* name */
1360 FALSE, /* partial_inplace */
1361 0, /* src_mask */
1362 ONES (64), /* dst_mask */
1363 FALSE), /* pcrel_offset */
1364
1365 /* Computes a dtv-relative displacement, the difference between the value
1366 of sym+add and the base address of the thread-local storage block that
1367 contains the definition of sym, minus 0x8000. */
1368 HOWTO (R_PPC64_DTPREL64,
1369 0, /* rightshift */
1370 4, /* size (0 = byte, 1 = short, 2 = long) */
1371 64, /* bitsize */
1372 FALSE, /* pc_relative */
1373 0, /* bitpos */
1374 complain_overflow_dont, /* complain_on_overflow */
1375 ppc64_elf_unhandled_reloc, /* special_function */
1376 "R_PPC64_DTPREL64", /* name */
1377 FALSE, /* partial_inplace */
1378 0, /* src_mask */
1379 ONES (64), /* dst_mask */
1380 FALSE), /* pcrel_offset */
1381
1382 /* A 16 bit dtprel reloc. */
1383 HOWTO (R_PPC64_DTPREL16,
1384 0, /* rightshift */
1385 1, /* size (0 = byte, 1 = short, 2 = long) */
1386 16, /* bitsize */
1387 FALSE, /* pc_relative */
1388 0, /* bitpos */
1389 complain_overflow_signed, /* complain_on_overflow */
1390 ppc64_elf_unhandled_reloc, /* special_function */
1391 "R_PPC64_DTPREL16", /* name */
1392 FALSE, /* partial_inplace */
1393 0, /* src_mask */
1394 0xffff, /* dst_mask */
1395 FALSE), /* pcrel_offset */
1396
1397 /* Like DTPREL16, but no overflow. */
1398 HOWTO (R_PPC64_DTPREL16_LO,
1399 0, /* rightshift */
1400 1, /* size (0 = byte, 1 = short, 2 = long) */
1401 16, /* bitsize */
1402 FALSE, /* pc_relative */
1403 0, /* bitpos */
1404 complain_overflow_dont, /* complain_on_overflow */
1405 ppc64_elf_unhandled_reloc, /* special_function */
1406 "R_PPC64_DTPREL16_LO", /* name */
1407 FALSE, /* partial_inplace */
1408 0, /* src_mask */
1409 0xffff, /* dst_mask */
1410 FALSE), /* pcrel_offset */
1411
1412 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1413 HOWTO (R_PPC64_DTPREL16_HI,
1414 16, /* rightshift */
1415 1, /* size (0 = byte, 1 = short, 2 = long) */
1416 16, /* bitsize */
1417 FALSE, /* pc_relative */
1418 0, /* bitpos */
f9c6b907 1419 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1420 ppc64_elf_unhandled_reloc, /* special_function */
1421 "R_PPC64_DTPREL16_HI", /* name */
1422 FALSE, /* partial_inplace */
1423 0, /* src_mask */
1424 0xffff, /* dst_mask */
1425 FALSE), /* pcrel_offset */
1426
1427 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1428 HOWTO (R_PPC64_DTPREL16_HA,
1429 16, /* rightshift */
1430 1, /* size (0 = byte, 1 = short, 2 = long) */
1431 16, /* bitsize */
1432 FALSE, /* pc_relative */
1433 0, /* bitpos */
f9c6b907 1434 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1435 ppc64_elf_unhandled_reloc, /* special_function */
1436 "R_PPC64_DTPREL16_HA", /* name */
1437 FALSE, /* partial_inplace */
1438 0, /* src_mask */
1439 0xffff, /* dst_mask */
1440 FALSE), /* pcrel_offset */
1441
1442 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1443 HOWTO (R_PPC64_DTPREL16_HIGHER,
1444 32, /* rightshift */
1445 1, /* size (0 = byte, 1 = short, 2 = long) */
1446 16, /* bitsize */
1447 FALSE, /* pc_relative */
1448 0, /* bitpos */
1449 complain_overflow_dont, /* complain_on_overflow */
1450 ppc64_elf_unhandled_reloc, /* special_function */
1451 "R_PPC64_DTPREL16_HIGHER", /* name */
1452 FALSE, /* partial_inplace */
1453 0, /* src_mask */
1454 0xffff, /* dst_mask */
1455 FALSE), /* pcrel_offset */
1456
1457 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1458 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1459 32, /* rightshift */
1460 1, /* size (0 = byte, 1 = short, 2 = long) */
1461 16, /* bitsize */
1462 FALSE, /* pc_relative */
1463 0, /* bitpos */
1464 complain_overflow_dont, /* complain_on_overflow */
1465 ppc64_elf_unhandled_reloc, /* special_function */
1466 "R_PPC64_DTPREL16_HIGHERA", /* name */
1467 FALSE, /* partial_inplace */
1468 0, /* src_mask */
1469 0xffff, /* dst_mask */
1470 FALSE), /* pcrel_offset */
1471
1472 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1473 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1474 48, /* rightshift */
1475 1, /* size (0 = byte, 1 = short, 2 = long) */
1476 16, /* bitsize */
1477 FALSE, /* pc_relative */
1478 0, /* bitpos */
1479 complain_overflow_dont, /* complain_on_overflow */
1480 ppc64_elf_unhandled_reloc, /* special_function */
1481 "R_PPC64_DTPREL16_HIGHEST", /* name */
1482 FALSE, /* partial_inplace */
1483 0, /* src_mask */
1484 0xffff, /* dst_mask */
1485 FALSE), /* pcrel_offset */
1486
1487 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1488 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1489 48, /* rightshift */
1490 1, /* size (0 = byte, 1 = short, 2 = long) */
1491 16, /* bitsize */
1492 FALSE, /* pc_relative */
1493 0, /* bitpos */
1494 complain_overflow_dont, /* complain_on_overflow */
1495 ppc64_elf_unhandled_reloc, /* special_function */
1496 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1497 FALSE, /* partial_inplace */
1498 0, /* src_mask */
1499 0xffff, /* dst_mask */
1500 FALSE), /* pcrel_offset */
1501
1502 /* Like DTPREL16, but for insns with a DS field. */
1503 HOWTO (R_PPC64_DTPREL16_DS,
1504 0, /* rightshift */
1505 1, /* size (0 = byte, 1 = short, 2 = long) */
1506 16, /* bitsize */
1507 FALSE, /* pc_relative */
1508 0, /* bitpos */
1509 complain_overflow_signed, /* complain_on_overflow */
1510 ppc64_elf_unhandled_reloc, /* special_function */
1511 "R_PPC64_DTPREL16_DS", /* name */
1512 FALSE, /* partial_inplace */
1513 0, /* src_mask */
1514 0xfffc, /* dst_mask */
1515 FALSE), /* pcrel_offset */
1516
1517 /* Like DTPREL16_DS, but no overflow. */
1518 HOWTO (R_PPC64_DTPREL16_LO_DS,
1519 0, /* rightshift */
1520 1, /* size (0 = byte, 1 = short, 2 = long) */
1521 16, /* bitsize */
1522 FALSE, /* pc_relative */
1523 0, /* bitpos */
1524 complain_overflow_dont, /* complain_on_overflow */
1525 ppc64_elf_unhandled_reloc, /* special_function */
1526 "R_PPC64_DTPREL16_LO_DS", /* name */
1527 FALSE, /* partial_inplace */
1528 0, /* src_mask */
1529 0xfffc, /* dst_mask */
1530 FALSE), /* pcrel_offset */
1531
1532 /* Computes a tp-relative displacement, the difference between the value of
1533 sym+add and the value of the thread pointer (r13). */
1534 HOWTO (R_PPC64_TPREL64,
1535 0, /* rightshift */
1536 4, /* size (0 = byte, 1 = short, 2 = long) */
1537 64, /* bitsize */
1538 FALSE, /* pc_relative */
1539 0, /* bitpos */
1540 complain_overflow_dont, /* complain_on_overflow */
1541 ppc64_elf_unhandled_reloc, /* special_function */
1542 "R_PPC64_TPREL64", /* name */
1543 FALSE, /* partial_inplace */
1544 0, /* src_mask */
1545 ONES (64), /* dst_mask */
1546 FALSE), /* pcrel_offset */
1547
1548 /* A 16 bit tprel reloc. */
1549 HOWTO (R_PPC64_TPREL16,
1550 0, /* rightshift */
1551 1, /* size (0 = byte, 1 = short, 2 = long) */
1552 16, /* bitsize */
1553 FALSE, /* pc_relative */
1554 0, /* bitpos */
1555 complain_overflow_signed, /* complain_on_overflow */
1556 ppc64_elf_unhandled_reloc, /* special_function */
1557 "R_PPC64_TPREL16", /* name */
1558 FALSE, /* partial_inplace */
1559 0, /* src_mask */
1560 0xffff, /* dst_mask */
1561 FALSE), /* pcrel_offset */
1562
1563 /* Like TPREL16, but no overflow. */
1564 HOWTO (R_PPC64_TPREL16_LO,
1565 0, /* rightshift */
1566 1, /* size (0 = byte, 1 = short, 2 = long) */
1567 16, /* bitsize */
1568 FALSE, /* pc_relative */
1569 0, /* bitpos */
1570 complain_overflow_dont, /* complain_on_overflow */
1571 ppc64_elf_unhandled_reloc, /* special_function */
1572 "R_PPC64_TPREL16_LO", /* name */
1573 FALSE, /* partial_inplace */
1574 0, /* src_mask */
1575 0xffff, /* dst_mask */
1576 FALSE), /* pcrel_offset */
1577
1578 /* Like TPREL16_LO, but next higher group of 16 bits. */
1579 HOWTO (R_PPC64_TPREL16_HI,
1580 16, /* rightshift */
1581 1, /* size (0 = byte, 1 = short, 2 = long) */
1582 16, /* bitsize */
1583 FALSE, /* pc_relative */
1584 0, /* bitpos */
f9c6b907 1585 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1586 ppc64_elf_unhandled_reloc, /* special_function */
1587 "R_PPC64_TPREL16_HI", /* name */
1588 FALSE, /* partial_inplace */
1589 0, /* src_mask */
1590 0xffff, /* dst_mask */
1591 FALSE), /* pcrel_offset */
1592
1593 /* Like TPREL16_HI, but adjust for low 16 bits. */
1594 HOWTO (R_PPC64_TPREL16_HA,
1595 16, /* rightshift */
1596 1, /* size (0 = byte, 1 = short, 2 = long) */
1597 16, /* bitsize */
1598 FALSE, /* pc_relative */
1599 0, /* bitpos */
f9c6b907 1600 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1601 ppc64_elf_unhandled_reloc, /* special_function */
1602 "R_PPC64_TPREL16_HA", /* name */
1603 FALSE, /* partial_inplace */
1604 0, /* src_mask */
1605 0xffff, /* dst_mask */
1606 FALSE), /* pcrel_offset */
1607
1608 /* Like TPREL16_HI, but next higher group of 16 bits. */
1609 HOWTO (R_PPC64_TPREL16_HIGHER,
1610 32, /* rightshift */
1611 1, /* size (0 = byte, 1 = short, 2 = long) */
1612 16, /* bitsize */
1613 FALSE, /* pc_relative */
1614 0, /* bitpos */
1615 complain_overflow_dont, /* complain_on_overflow */
1616 ppc64_elf_unhandled_reloc, /* special_function */
1617 "R_PPC64_TPREL16_HIGHER", /* name */
1618 FALSE, /* partial_inplace */
1619 0, /* src_mask */
1620 0xffff, /* dst_mask */
1621 FALSE), /* pcrel_offset */
1622
1623 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1624 HOWTO (R_PPC64_TPREL16_HIGHERA,
1625 32, /* rightshift */
1626 1, /* size (0 = byte, 1 = short, 2 = long) */
1627 16, /* bitsize */
1628 FALSE, /* pc_relative */
1629 0, /* bitpos */
1630 complain_overflow_dont, /* complain_on_overflow */
1631 ppc64_elf_unhandled_reloc, /* special_function */
1632 "R_PPC64_TPREL16_HIGHERA", /* name */
1633 FALSE, /* partial_inplace */
1634 0, /* src_mask */
1635 0xffff, /* dst_mask */
1636 FALSE), /* pcrel_offset */
1637
1638 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1639 HOWTO (R_PPC64_TPREL16_HIGHEST,
1640 48, /* rightshift */
1641 1, /* size (0 = byte, 1 = short, 2 = long) */
1642 16, /* bitsize */
1643 FALSE, /* pc_relative */
1644 0, /* bitpos */
1645 complain_overflow_dont, /* complain_on_overflow */
1646 ppc64_elf_unhandled_reloc, /* special_function */
1647 "R_PPC64_TPREL16_HIGHEST", /* name */
1648 FALSE, /* partial_inplace */
1649 0, /* src_mask */
1650 0xffff, /* dst_mask */
1651 FALSE), /* pcrel_offset */
1652
1653 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1654 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1655 48, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1657 16, /* bitsize */
1658 FALSE, /* pc_relative */
1659 0, /* bitpos */
1660 complain_overflow_dont, /* complain_on_overflow */
1661 ppc64_elf_unhandled_reloc, /* special_function */
1662 "R_PPC64_TPREL16_HIGHESTA", /* name */
1663 FALSE, /* partial_inplace */
1664 0, /* src_mask */
1665 0xffff, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1667
1668 /* Like TPREL16, but for insns with a DS field. */
1669 HOWTO (R_PPC64_TPREL16_DS,
1670 0, /* rightshift */
1671 1, /* size (0 = byte, 1 = short, 2 = long) */
1672 16, /* bitsize */
1673 FALSE, /* pc_relative */
1674 0, /* bitpos */
1675 complain_overflow_signed, /* complain_on_overflow */
1676 ppc64_elf_unhandled_reloc, /* special_function */
1677 "R_PPC64_TPREL16_DS", /* name */
1678 FALSE, /* partial_inplace */
1679 0, /* src_mask */
1680 0xfffc, /* dst_mask */
1681 FALSE), /* pcrel_offset */
1682
1683 /* Like TPREL16_DS, but no overflow. */
1684 HOWTO (R_PPC64_TPREL16_LO_DS,
1685 0, /* rightshift */
1686 1, /* size (0 = byte, 1 = short, 2 = long) */
1687 16, /* bitsize */
1688 FALSE, /* pc_relative */
1689 0, /* bitpos */
1690 complain_overflow_dont, /* complain_on_overflow */
1691 ppc64_elf_unhandled_reloc, /* special_function */
1692 "R_PPC64_TPREL16_LO_DS", /* name */
1693 FALSE, /* partial_inplace */
1694 0, /* src_mask */
1695 0xfffc, /* dst_mask */
1696 FALSE), /* pcrel_offset */
1697
1698 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1699 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1700 to the first entry relative to the TOC base (r2). */
1701 HOWTO (R_PPC64_GOT_TLSGD16,
1702 0, /* rightshift */
1703 1, /* size (0 = byte, 1 = short, 2 = long) */
1704 16, /* bitsize */
1705 FALSE, /* pc_relative */
1706 0, /* bitpos */
1707 complain_overflow_signed, /* complain_on_overflow */
1708 ppc64_elf_unhandled_reloc, /* special_function */
1709 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1710 FALSE, /* partial_inplace */
5bd4f169
AM
1711 0, /* src_mask */
1712 0xffff, /* dst_mask */
b34976b6 1713 FALSE), /* pcrel_offset */
5bd4f169 1714
411e1bfb
AM
1715 /* Like GOT_TLSGD16, but no overflow. */
1716 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1717 0, /* rightshift */
1718 1, /* size (0 = byte, 1 = short, 2 = long) */
1719 16, /* bitsize */
b34976b6 1720 FALSE, /* pc_relative */
5bd4f169
AM
1721 0, /* bitpos */
1722 complain_overflow_dont, /* complain_on_overflow */
805fc799 1723 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1724 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1725 FALSE, /* partial_inplace */
5bd4f169
AM
1726 0, /* src_mask */
1727 0xffff, /* dst_mask */
b34976b6 1728 FALSE), /* pcrel_offset */
5bd4f169 1729
411e1bfb
AM
1730 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1731 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1732 16, /* rightshift */
1733 1, /* size (0 = byte, 1 = short, 2 = long) */
1734 16, /* bitsize */
b34976b6 1735 FALSE, /* pc_relative */
5bd4f169 1736 0, /* bitpos */
f9c6b907 1737 complain_overflow_signed, /* complain_on_overflow */
805fc799 1738 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1739 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1740 FALSE, /* partial_inplace */
5bd4f169
AM
1741 0, /* src_mask */
1742 0xffff, /* dst_mask */
b34976b6 1743 FALSE), /* pcrel_offset */
5bd4f169 1744
411e1bfb
AM
1745 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1746 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1747 16, /* rightshift */
1748 1, /* size (0 = byte, 1 = short, 2 = long) */
1749 16, /* bitsize */
b34976b6 1750 FALSE, /* pc_relative */
5bd4f169 1751 0, /* bitpos */
f9c6b907 1752 complain_overflow_signed, /* complain_on_overflow */
805fc799 1753 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1754 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1755 FALSE, /* partial_inplace */
5bd4f169
AM
1756 0, /* src_mask */
1757 0xffff, /* dst_mask */
b34976b6 1758 FALSE), /* pcrel_offset */
5bd4f169 1759
411e1bfb
AM
1760 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1761 with values (sym+add)@dtpmod and zero, and computes the offset to the
1762 first entry relative to the TOC base (r2). */
1763 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1764 0, /* rightshift */
1765 1, /* size (0 = byte, 1 = short, 2 = long) */
1766 16, /* bitsize */
b34976b6 1767 FALSE, /* pc_relative */
5bd4f169 1768 0, /* bitpos */
411e1bfb
AM
1769 complain_overflow_signed, /* complain_on_overflow */
1770 ppc64_elf_unhandled_reloc, /* special_function */
1771 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1772 FALSE, /* partial_inplace */
d006db6c 1773 0, /* src_mask */
411e1bfb 1774 0xffff, /* dst_mask */
b34976b6 1775 FALSE), /* pcrel_offset */
5bd4f169 1776
411e1bfb
AM
1777 /* Like GOT_TLSLD16, but no overflow. */
1778 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1779 0, /* rightshift */
1780 1, /* size (0 = byte, 1 = short, 2 = long) */
1781 16, /* bitsize */
b34976b6 1782 FALSE, /* pc_relative */
5bd4f169 1783 0, /* bitpos */
411e1bfb
AM
1784 complain_overflow_dont, /* complain_on_overflow */
1785 ppc64_elf_unhandled_reloc, /* special_function */
1786 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1787 FALSE, /* partial_inplace */
d006db6c 1788 0, /* src_mask */
411e1bfb 1789 0xffff, /* dst_mask */
b34976b6 1790 FALSE), /* pcrel_offset */
5bd4f169 1791
411e1bfb
AM
1792 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1793 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1794 16, /* rightshift */
5bd4f169
AM
1795 1, /* size (0 = byte, 1 = short, 2 = long) */
1796 16, /* bitsize */
b34976b6 1797 FALSE, /* pc_relative */
5bd4f169 1798 0, /* bitpos */
f9c6b907 1799 complain_overflow_signed, /* complain_on_overflow */
805fc799 1800 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1801 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1802 FALSE, /* partial_inplace */
d006db6c 1803 0, /* src_mask */
411e1bfb 1804 0xffff, /* dst_mask */
b34976b6 1805 FALSE), /* pcrel_offset */
5bd4f169 1806
411e1bfb
AM
1807 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1808 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1809 16, /* rightshift */
5bd4f169
AM
1810 1, /* size (0 = byte, 1 = short, 2 = long) */
1811 16, /* bitsize */
b34976b6 1812 FALSE, /* pc_relative */
5bd4f169 1813 0, /* bitpos */
f9c6b907 1814 complain_overflow_signed, /* complain_on_overflow */
805fc799 1815 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1816 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1817 FALSE, /* partial_inplace */
d006db6c 1818 0, /* src_mask */
411e1bfb 1819 0xffff, /* dst_mask */
b34976b6 1820 FALSE), /* pcrel_offset */
5bd4f169 1821
411e1bfb
AM
1822 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1823 the offset to the entry relative to the TOC base (r2). */
1824 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1825 0, /* rightshift */
1826 1, /* size (0 = byte, 1 = short, 2 = long) */
1827 16, /* bitsize */
b34976b6 1828 FALSE, /* pc_relative */
5bd4f169 1829 0, /* bitpos */
411e1bfb 1830 complain_overflow_signed, /* complain_on_overflow */
805fc799 1831 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1832 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1833 FALSE, /* partial_inplace */
d006db6c 1834 0, /* src_mask */
5bd4f169 1835 0xfffc, /* dst_mask */
b34976b6 1836 FALSE), /* pcrel_offset */
5bd4f169 1837
411e1bfb
AM
1838 /* Like GOT_DTPREL16_DS, but no overflow. */
1839 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1840 0, /* rightshift */
c061c2d8
AM
1841 1, /* size (0 = byte, 1 = short, 2 = long) */
1842 16, /* bitsize */
b34976b6 1843 FALSE, /* pc_relative */
5bd4f169 1844 0, /* bitpos */
411e1bfb
AM
1845 complain_overflow_dont, /* complain_on_overflow */
1846 ppc64_elf_unhandled_reloc, /* special_function */
1847 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1848 FALSE, /* partial_inplace */
d006db6c 1849 0, /* src_mask */
c061c2d8 1850 0xfffc, /* dst_mask */
b34976b6 1851 FALSE), /* pcrel_offset */
5bd4f169 1852
411e1bfb
AM
1853 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1854 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1855 16, /* rightshift */
5bd4f169
AM
1856 1, /* size (0 = byte, 1 = short, 2 = long) */
1857 16, /* bitsize */
b34976b6 1858 FALSE, /* pc_relative */
5bd4f169 1859 0, /* bitpos */
f9c6b907 1860 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1861 ppc64_elf_unhandled_reloc, /* special_function */
1862 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1863 FALSE, /* partial_inplace */
d006db6c 1864 0, /* src_mask */
411e1bfb 1865 0xffff, /* dst_mask */
b34976b6 1866 FALSE), /* pcrel_offset */
5bd4f169 1867
411e1bfb
AM
1868 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1869 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1870 16, /* rightshift */
1871 1, /* size (0 = byte, 1 = short, 2 = long) */
1872 16, /* bitsize */
1873 FALSE, /* pc_relative */
1874 0, /* bitpos */
f9c6b907 1875 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1876 ppc64_elf_unhandled_reloc, /* special_function */
1877 "R_PPC64_GOT_DTPREL16_HA", /* name */
1878 FALSE, /* partial_inplace */
1879 0, /* src_mask */
1880 0xffff, /* dst_mask */
1881 FALSE), /* pcrel_offset */
1882
1883 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1884 offset to the entry relative to the TOC base (r2). */
1885 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1886 0, /* rightshift */
1887 1, /* size (0 = byte, 1 = short, 2 = long) */
1888 16, /* bitsize */
b34976b6 1889 FALSE, /* pc_relative */
5bd4f169
AM
1890 0, /* bitpos */
1891 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1892 ppc64_elf_unhandled_reloc, /* special_function */
1893 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1894 FALSE, /* partial_inplace */
d006db6c 1895 0, /* src_mask */
ad8e1ba5 1896 0xfffc, /* dst_mask */
b34976b6 1897 FALSE), /* pcrel_offset */
5bd4f169 1898
411e1bfb
AM
1899 /* Like GOT_TPREL16_DS, but no overflow. */
1900 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1901 0, /* rightshift */
1902 1, /* size (0 = byte, 1 = short, 2 = long) */
1903 16, /* bitsize */
b34976b6 1904 FALSE, /* pc_relative */
5bd4f169
AM
1905 0, /* bitpos */
1906 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1907 ppc64_elf_unhandled_reloc, /* special_function */
1908 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1909 FALSE, /* partial_inplace */
d006db6c 1910 0, /* src_mask */
ad8e1ba5 1911 0xfffc, /* dst_mask */
b34976b6 1912 FALSE), /* pcrel_offset */
5bd4f169 1913
411e1bfb
AM
1914 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1915 HOWTO (R_PPC64_GOT_TPREL16_HI,
1916 16, /* rightshift */
5bd4f169
AM
1917 1, /* size (0 = byte, 1 = short, 2 = long) */
1918 16, /* bitsize */
b34976b6 1919 FALSE, /* pc_relative */
5bd4f169 1920 0, /* bitpos */
f9c6b907 1921 complain_overflow_signed, /* complain_on_overflow */
805fc799 1922 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1923 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1924 FALSE, /* partial_inplace */
d006db6c 1925 0, /* src_mask */
411e1bfb 1926 0xffff, /* dst_mask */
b34976b6 1927 FALSE), /* pcrel_offset */
5bd4f169 1928
411e1bfb
AM
1929 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1930 HOWTO (R_PPC64_GOT_TPREL16_HA,
1931 16, /* rightshift */
5bd4f169
AM
1932 1, /* size (0 = byte, 1 = short, 2 = long) */
1933 16, /* bitsize */
b34976b6 1934 FALSE, /* pc_relative */
5bd4f169 1935 0, /* bitpos */
f9c6b907 1936 complain_overflow_signed, /* complain_on_overflow */
805fc799 1937 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1938 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1939 FALSE, /* partial_inplace */
d006db6c 1940 0, /* src_mask */
411e1bfb 1941 0xffff, /* dst_mask */
b34976b6 1942 FALSE), /* pcrel_offset */
5bd4f169 1943
25f23106
AM
1944 HOWTO (R_PPC64_JMP_IREL, /* type */
1945 0, /* rightshift */
1946 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1947 0, /* bitsize */
1948 FALSE, /* pc_relative */
1949 0, /* bitpos */
1950 complain_overflow_dont, /* complain_on_overflow */
1951 ppc64_elf_unhandled_reloc, /* special_function */
1952 "R_PPC64_JMP_IREL", /* name */
1953 FALSE, /* partial_inplace */
1954 0, /* src_mask */
1955 0, /* dst_mask */
1956 FALSE), /* pcrel_offset */
1957
e054468f
AM
1958 HOWTO (R_PPC64_IRELATIVE, /* type */
1959 0, /* rightshift */
1960 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1961 64, /* bitsize */
1962 FALSE, /* pc_relative */
1963 0, /* bitpos */
1964 complain_overflow_dont, /* complain_on_overflow */
1965 bfd_elf_generic_reloc, /* special_function */
1966 "R_PPC64_IRELATIVE", /* name */
1967 FALSE, /* partial_inplace */
1968 0, /* src_mask */
1969 ONES (64), /* dst_mask */
1970 FALSE), /* pcrel_offset */
1971
25f23106
AM
1972 /* A 16 bit relative relocation. */
1973 HOWTO (R_PPC64_REL16, /* type */
1974 0, /* rightshift */
1975 1, /* size (0 = byte, 1 = short, 2 = long) */
1976 16, /* bitsize */
1977 TRUE, /* pc_relative */
1978 0, /* bitpos */
b80eed39 1979 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1980 bfd_elf_generic_reloc, /* special_function */
1981 "R_PPC64_REL16", /* name */
1982 FALSE, /* partial_inplace */
1983 0, /* src_mask */
1984 0xffff, /* dst_mask */
1985 TRUE), /* pcrel_offset */
1986
1987 /* A 16 bit relative relocation without overflow. */
1988 HOWTO (R_PPC64_REL16_LO, /* type */
1989 0, /* rightshift */
1990 1, /* size (0 = byte, 1 = short, 2 = long) */
1991 16, /* bitsize */
1992 TRUE, /* pc_relative */
1993 0, /* bitpos */
1994 complain_overflow_dont,/* complain_on_overflow */
1995 bfd_elf_generic_reloc, /* special_function */
1996 "R_PPC64_REL16_LO", /* name */
1997 FALSE, /* partial_inplace */
1998 0, /* src_mask */
1999 0xffff, /* dst_mask */
2000 TRUE), /* pcrel_offset */
2001
2002 /* The high order 16 bits of a relative address. */
2003 HOWTO (R_PPC64_REL16_HI, /* type */
2004 16, /* rightshift */
2005 1, /* size (0 = byte, 1 = short, 2 = long) */
2006 16, /* bitsize */
2007 TRUE, /* pc_relative */
2008 0, /* bitpos */
f9c6b907 2009 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2010 bfd_elf_generic_reloc, /* special_function */
2011 "R_PPC64_REL16_HI", /* name */
2012 FALSE, /* partial_inplace */
2013 0, /* src_mask */
2014 0xffff, /* dst_mask */
2015 TRUE), /* pcrel_offset */
2016
2017 /* The high order 16 bits of a relative address, plus 1 if the contents of
2018 the low 16 bits, treated as a signed number, is negative. */
2019 HOWTO (R_PPC64_REL16_HA, /* type */
2020 16, /* rightshift */
2021 1, /* size (0 = byte, 1 = short, 2 = long) */
2022 16, /* bitsize */
2023 TRUE, /* pc_relative */
2024 0, /* bitpos */
f9c6b907 2025 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2026 ppc64_elf_ha_reloc, /* special_function */
2027 "R_PPC64_REL16_HA", /* name */
2028 FALSE, /* partial_inplace */
2029 0, /* src_mask */
2030 0xffff, /* dst_mask */
2031 TRUE), /* pcrel_offset */
2032
a680de9a
PB
2033 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2034 HOWTO (R_PPC64_REL16DX_HA, /* type */
2035 16, /* rightshift */
2036 2, /* size (0 = byte, 1 = short, 2 = long) */
2037 16, /* bitsize */
2038 TRUE, /* pc_relative */
2039 0, /* bitpos */
2040 complain_overflow_signed, /* complain_on_overflow */
2041 ppc64_elf_ha_reloc, /* special_function */
2042 "R_PPC64_REL16DX_HA", /* name */
2043 FALSE, /* partial_inplace */
2044 0, /* src_mask */
2045 0x1fffc1, /* dst_mask */
2046 TRUE), /* pcrel_offset */
2047
f9c6b907
AM
2048 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2049 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2050 16, /* rightshift */
2051 1, /* size (0 = byte, 1 = short, 2 = long) */
2052 16, /* bitsize */
2053 FALSE, /* pc_relative */
2054 0, /* bitpos */
2055 complain_overflow_dont, /* complain_on_overflow */
2056 bfd_elf_generic_reloc, /* special_function */
2057 "R_PPC64_ADDR16_HIGH", /* name */
2058 FALSE, /* partial_inplace */
2059 0, /* src_mask */
2060 0xffff, /* dst_mask */
2061 FALSE), /* pcrel_offset */
2062
2063 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2064 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2065 16, /* rightshift */
2066 1, /* size (0 = byte, 1 = short, 2 = long) */
2067 16, /* bitsize */
2068 FALSE, /* pc_relative */
2069 0, /* bitpos */
2070 complain_overflow_dont, /* complain_on_overflow */
2071 ppc64_elf_ha_reloc, /* special_function */
2072 "R_PPC64_ADDR16_HIGHA", /* name */
2073 FALSE, /* partial_inplace */
2074 0, /* src_mask */
2075 0xffff, /* dst_mask */
2076 FALSE), /* pcrel_offset */
2077
2078 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2079 HOWTO (R_PPC64_DTPREL16_HIGH,
2080 16, /* rightshift */
2081 1, /* size (0 = byte, 1 = short, 2 = long) */
2082 16, /* bitsize */
2083 FALSE, /* pc_relative */
2084 0, /* bitpos */
2085 complain_overflow_dont, /* complain_on_overflow */
2086 ppc64_elf_unhandled_reloc, /* special_function */
2087 "R_PPC64_DTPREL16_HIGH", /* name */
2088 FALSE, /* partial_inplace */
2089 0, /* src_mask */
2090 0xffff, /* dst_mask */
2091 FALSE), /* pcrel_offset */
2092
2093 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2094 HOWTO (R_PPC64_DTPREL16_HIGHA,
2095 16, /* rightshift */
2096 1, /* size (0 = byte, 1 = short, 2 = long) */
2097 16, /* bitsize */
2098 FALSE, /* pc_relative */
2099 0, /* bitpos */
2100 complain_overflow_dont, /* complain_on_overflow */
2101 ppc64_elf_unhandled_reloc, /* special_function */
2102 "R_PPC64_DTPREL16_HIGHA", /* name */
2103 FALSE, /* partial_inplace */
2104 0, /* src_mask */
2105 0xffff, /* dst_mask */
2106 FALSE), /* pcrel_offset */
2107
2108 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2109 HOWTO (R_PPC64_TPREL16_HIGH,
2110 16, /* rightshift */
2111 1, /* size (0 = byte, 1 = short, 2 = long) */
2112 16, /* bitsize */
2113 FALSE, /* pc_relative */
2114 0, /* bitpos */
2115 complain_overflow_dont, /* complain_on_overflow */
2116 ppc64_elf_unhandled_reloc, /* special_function */
2117 "R_PPC64_TPREL16_HIGH", /* name */
2118 FALSE, /* partial_inplace */
2119 0, /* src_mask */
2120 0xffff, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2122
2123 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2124 HOWTO (R_PPC64_TPREL16_HIGHA,
2125 16, /* rightshift */
2126 1, /* size (0 = byte, 1 = short, 2 = long) */
2127 16, /* bitsize */
2128 FALSE, /* pc_relative */
2129 0, /* bitpos */
2130 complain_overflow_dont, /* complain_on_overflow */
2131 ppc64_elf_unhandled_reloc, /* special_function */
2132 "R_PPC64_TPREL16_HIGHA", /* name */
2133 FALSE, /* partial_inplace */
2134 0, /* src_mask */
2135 0xffff, /* dst_mask */
2136 FALSE), /* pcrel_offset */
2137
006589cf
AM
2138 /* Marker reloc on ELFv2 large-model function entry. */
2139 HOWTO (R_PPC64_ENTRY,
2140 0, /* rightshift */
2141 2, /* size (0 = byte, 1 = short, 2 = long) */
2142 32, /* bitsize */
2143 FALSE, /* pc_relative */
2144 0, /* bitpos */
2145 complain_overflow_dont, /* complain_on_overflow */
2146 bfd_elf_generic_reloc, /* special_function */
2147 "R_PPC64_ENTRY", /* name */
2148 FALSE, /* partial_inplace */
2149 0, /* src_mask */
2150 0, /* dst_mask */
2151 FALSE), /* pcrel_offset */
2152
45965137
AM
2153 /* Like ADDR64, but use local entry point of function. */
2154 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2155 0, /* rightshift */
2156 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2157 64, /* bitsize */
2158 FALSE, /* pc_relative */
2159 0, /* bitpos */
2160 complain_overflow_dont, /* complain_on_overflow */
2161 bfd_elf_generic_reloc, /* special_function */
2162 "R_PPC64_ADDR64_LOCAL", /* name */
2163 FALSE, /* partial_inplace */
2164 0, /* src_mask */
2165 ONES (64), /* dst_mask */
2166 FALSE), /* pcrel_offset */
2167
5bd4f169
AM
2168 /* GNU extension to record C++ vtable hierarchy. */
2169 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2170 0, /* rightshift */
2171 0, /* size (0 = byte, 1 = short, 2 = long) */
2172 0, /* bitsize */
b34976b6 2173 FALSE, /* pc_relative */
5bd4f169
AM
2174 0, /* bitpos */
2175 complain_overflow_dont, /* complain_on_overflow */
2176 NULL, /* special_function */
2177 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2178 FALSE, /* partial_inplace */
5bd4f169
AM
2179 0, /* src_mask */
2180 0, /* dst_mask */
b34976b6 2181 FALSE), /* pcrel_offset */
5bd4f169
AM
2182
2183 /* GNU extension to record C++ vtable member usage. */
2184 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2185 0, /* rightshift */
2186 0, /* size (0 = byte, 1 = short, 2 = long) */
2187 0, /* bitsize */
b34976b6 2188 FALSE, /* pc_relative */
5bd4f169
AM
2189 0, /* bitpos */
2190 complain_overflow_dont, /* complain_on_overflow */
2191 NULL, /* special_function */
2192 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2193 FALSE, /* partial_inplace */
5bd4f169
AM
2194 0, /* src_mask */
2195 0, /* dst_mask */
b34976b6 2196 FALSE), /* pcrel_offset */
5bd4f169
AM
2197};
2198
2199\f
2200/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2201 be done. */
2202
2203static void
4ce794b7 2204ppc_howto_init (void)
5bd4f169
AM
2205{
2206 unsigned int i, type;
2207
a4b6fadd 2208 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
2209 {
2210 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 2211 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
2212 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2213 }
2214}
2215
2216static reloc_howto_type *
4ce794b7
AM
2217ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2218 bfd_reloc_code_real_type code)
5bd4f169 2219{
411e1bfb 2220 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2221
2222 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2223 /* Initialize howto table if needed. */
2224 ppc_howto_init ();
2225
4ce794b7 2226 switch (code)
5bd4f169
AM
2227 {
2228 default:
4ce794b7 2229 return NULL;
5bd4f169 2230
411e1bfb
AM
2231 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2232 break;
2233 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2234 break;
2235 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2236 break;
2237 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2238 break;
2239 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2240 break;
2241 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2242 break;
f9c6b907
AM
2243 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2244 break;
411e1bfb 2245 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2246 break;
f9c6b907
AM
2247 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2248 break;
411e1bfb 2249 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2250 break;
411e1bfb 2251 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2252 break;
411e1bfb 2253 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2254 break;
411e1bfb 2255 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2256 break;
411e1bfb 2257 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2258 break;
411e1bfb 2259 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2260 break;
411e1bfb 2261 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2262 break;
411e1bfb 2263 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2264 break;
411e1bfb 2265 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2266 break;
411e1bfb 2267 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2268 break;
411e1bfb 2269 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2270 break;
411e1bfb 2271 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2272 break;
411e1bfb 2273 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2274 break;
411e1bfb 2275 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2276 break;
411e1bfb 2277 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2278 break;
411e1bfb 2279 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2280 break;
411e1bfb 2281 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2282 break;
411e1bfb 2283 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2284 break;
411e1bfb 2285 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2286 break;
411e1bfb 2287 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2288 break;
411e1bfb 2289 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2290 break;
411e1bfb 2291 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2292 break;
411e1bfb 2293 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2294 break;
411e1bfb 2295 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2296 break;
411e1bfb 2297 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2298 break;
411e1bfb 2299 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2300 break;
411e1bfb 2301 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2302 break;
411e1bfb 2303 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2304 break;
411e1bfb 2305 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2306 break;
411e1bfb 2307 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2308 break;
411e1bfb 2309 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2310 break;
411e1bfb 2311 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2312 break;
411e1bfb 2313 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2314 break;
411e1bfb 2315 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2316 break;
411e1bfb 2317 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2318 break;
411e1bfb 2319 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2320 break;
411e1bfb 2321 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2322 break;
411e1bfb 2323 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2324 break;
411e1bfb 2325 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2326 break;
411e1bfb 2327 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2328 break;
411e1bfb 2329 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2330 break;
411e1bfb 2331 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2332 break;
411e1bfb 2333 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2334 break;
411e1bfb 2335 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2336 break;
411e1bfb 2337 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2338 break;
411e1bfb 2339 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2340 break;
411e1bfb 2341 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2342 break;
411e1bfb 2343 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2344 break;
411e1bfb 2345 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2346 break;
411e1bfb 2347 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2348 break;
411e1bfb 2349 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2350 break;
411e1bfb 2351 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2352 break;
411e1bfb 2353 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2354 break;
727fc41e
AM
2355 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2356 break;
2357 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2358 break;
411e1bfb 2359 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2360 break;
411e1bfb 2361 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2362 break;
411e1bfb 2363 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2364 break;
411e1bfb 2365 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2366 break;
f9c6b907
AM
2367 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2368 break;
411e1bfb 2369 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2370 break;
f9c6b907
AM
2371 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2372 break;
411e1bfb 2373 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2374 break;
411e1bfb
AM
2375 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2376 break;
2377 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2378 break;
2379 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2380 break;
f9c6b907
AM
2381 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2382 break;
411e1bfb
AM
2383 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2384 break;
f9c6b907
AM
2385 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2386 break;
411e1bfb
AM
2387 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2388 break;
2389 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2390 break;
2391 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2392 break;
2393 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2394 break;
2395 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2396 break;
2397 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2398 break;
2399 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2400 break;
2401 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2402 break;
2403 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2404 break;
2405 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2406 break;
2407 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2408 break;
2409 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2410 break;
2411 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2412 break;
2413 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2414 break;
2415 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2416 break;
2417 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2418 break;
2419 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2420 break;
2421 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2422 break;
2423 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2424 break;
2425 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2426 break;
2427 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2428 break;
2429 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2430 break;
2431 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2432 break;
2433 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2434 break;
2435 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2436 break;
2437 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2438 break;
2439 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2440 break;
2441 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2442 break;
2443 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2444 break;
25f23106
AM
2445 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2446 break;
2447 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2448 break;
2449 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2450 break;
2451 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2452 break;
a680de9a
PB
2453 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2454 break;
006589cf
AM
2455 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2456 break;
45965137
AM
2457 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2458 break;
411e1bfb
AM
2459 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2460 break;
2461 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2462 break;
2463 }
2464
4ce794b7 2465 return ppc64_elf_howto_table[r];
5bd4f169
AM
2466};
2467
157090f7
AM
2468static reloc_howto_type *
2469ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2470 const char *r_name)
2471{
2472 unsigned int i;
2473
a4b6fadd 2474 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
2475 if (ppc64_elf_howto_raw[i].name != NULL
2476 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2477 return &ppc64_elf_howto_raw[i];
2478
2479 return NULL;
2480}
2481
5bd4f169
AM
2482/* Set the howto pointer for a PowerPC ELF reloc. */
2483
2484static void
4ce794b7
AM
2485ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2486 Elf_Internal_Rela *dst)
5bd4f169 2487{
65f38f15
AM
2488 unsigned int type;
2489
ef60b7ff 2490 /* Initialize howto table if needed. */
5bd4f169 2491 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2492 ppc_howto_init ();
2493
65f38f15 2494 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 2495 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 2496 {
695344c0 2497 /* xgettext:c-format */
4eca0228
AM
2498 _bfd_error_handler (_("%B: invalid relocation type %d"),
2499 abfd, (int) type);
3ec2b351 2500 type = R_PPC64_NONE;
d0fb9a8d 2501 }
65f38f15 2502 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2503}
2504
04c9666a 2505/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2506
2507static bfd_reloc_status_type
4ce794b7
AM
2508ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2509 void *data, asection *input_section,
2510 bfd *output_bfd, char **error_message)
5bd4f169 2511{
a680de9a
PB
2512 enum elf_ppc64_reloc_type r_type;
2513 long insn;
2514 bfd_size_type octets;
2515 bfd_vma value;
2516
805fc799
AM
2517 /* If this is a relocatable link (output_bfd test tells us), just
2518 call the generic function. Any adjustment will be done at final
2519 link time. */
2520 if (output_bfd != NULL)
cedb70c5 2521 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2522 input_section, output_bfd, error_message);
2523
2524 /* Adjust the addend for sign extension of the low 16 bits.
2525 We won't actually be using the low 16 bits, so trashing them
2526 doesn't matter. */
2527 reloc_entry->addend += 0x8000;
a680de9a
PB
2528 r_type = reloc_entry->howto->type;
2529 if (r_type != R_PPC64_REL16DX_HA)
2530 return bfd_reloc_continue;
2531
2532 value = 0;
2533 if (!bfd_is_com_section (symbol->section))
2534 value = symbol->value;
2535 value += (reloc_entry->addend
2536 + symbol->section->output_offset
2537 + symbol->section->output_section->vma);
2538 value -= (reloc_entry->address
2539 + input_section->output_offset
2540 + input_section->output_section->vma);
2541 value = (bfd_signed_vma) value >> 16;
2542
2543 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2544 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2545 insn &= ~0x1fffc1;
2546 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2547 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2548 if (value + 0x8000 > 0xffff)
2549 return bfd_reloc_overflow;
2550 return bfd_reloc_ok;
805fc799 2551}
5bd4f169 2552
2441e016
AM
2553static bfd_reloc_status_type
2554ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2555 void *data, asection *input_section,
2556 bfd *output_bfd, char **error_message)
2557{
2558 if (output_bfd != NULL)
2559 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2560 input_section, output_bfd, error_message);
2561
699733f6
AM
2562 if (strcmp (symbol->section->name, ".opd") == 0
2563 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2564 {
2565 bfd_vma dest = opd_entry_value (symbol->section,
2566 symbol->value + reloc_entry->addend,
aef36ac1 2567 NULL, NULL, FALSE);
2441e016
AM
2568 if (dest != (bfd_vma) -1)
2569 reloc_entry->addend = dest - (symbol->value
2570 + symbol->section->output_section->vma
2571 + symbol->section->output_offset);
2572 }
810d4e75
AM
2573 else
2574 {
2575 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2576
2577 if (symbol->section->owner != abfd
9f284bf9 2578 && symbol->section->owner != NULL
810d4e75
AM
2579 && abiversion (symbol->section->owner) >= 2)
2580 {
2581 unsigned int i;
2582
2583 for (i = 0; i < symbol->section->owner->symcount; ++i)
2584 {
2585 asymbol *symdef = symbol->section->owner->outsymbols[i];
2586
2587 if (strcmp (symdef->name, symbol->name) == 0)
2588 {
2589 elfsym = (elf_symbol_type *) symdef;
2590 break;
2591 }
2592 }
2593 }
2594 reloc_entry->addend
2595 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2596 }
2441e016
AM
2597 return bfd_reloc_continue;
2598}
2599
805fc799 2600static bfd_reloc_status_type
4ce794b7
AM
2601ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2602 void *data, asection *input_section,
2603 bfd *output_bfd, char **error_message)
805fc799
AM
2604{
2605 long insn;
04c9666a 2606 enum elf_ppc64_reloc_type r_type;
805fc799 2607 bfd_size_type octets;
794e51c0
AM
2608 /* Assume 'at' branch hints. */
2609 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2610
2611 /* If this is a relocatable link (output_bfd test tells us), just
2612 call the generic function. Any adjustment will be done at final
2613 link time. */
5bd4f169 2614 if (output_bfd != NULL)
cedb70c5 2615 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2616 input_section, output_bfd, error_message);
2617
2618 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2619 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2620 insn &= ~(0x01 << 21);
4ce794b7 2621 r_type = reloc_entry->howto->type;
805fc799
AM
2622 if (r_type == R_PPC64_ADDR14_BRTAKEN
2623 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2624 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2625
794e51c0 2626 if (is_isa_v2)
5bd4f169 2627 {
805fc799
AM
2628 /* Set 'a' bit. This is 0b00010 in BO field for branch
2629 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2630 for branch on CTR insns (BO == 1a00t or 1a01t). */
2631 if ((insn & (0x14 << 21)) == (0x04 << 21))
2632 insn |= 0x02 << 21;
2633 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2634 insn |= 0x08 << 21;
2635 else
2441e016 2636 goto out;
5bd4f169 2637 }
805fc799
AM
2638 else
2639 {
2640 bfd_vma target = 0;
2641 bfd_vma from;
5bd4f169 2642
805fc799
AM
2643 if (!bfd_is_com_section (symbol->section))
2644 target = symbol->value;
2645 target += symbol->section->output_section->vma;
2646 target += symbol->section->output_offset;
2647 target += reloc_entry->addend;
5bd4f169 2648
805fc799
AM
2649 from = (reloc_entry->address
2650 + input_section->output_offset
2651 + input_section->output_section->vma);
5bd4f169 2652
805fc799
AM
2653 /* Invert 'y' bit if not the default. */
2654 if ((bfd_signed_vma) (target - from) < 0)
2655 insn ^= 0x01 << 21;
2656 }
4ce794b7 2657 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2658 out:
2659 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2660 input_section, output_bfd, error_message);
805fc799 2661}
5bd4f169 2662
805fc799 2663static bfd_reloc_status_type
4ce794b7
AM
2664ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2665 void *data, asection *input_section,
2666 bfd *output_bfd, char **error_message)
805fc799
AM
2667{
2668 /* If this is a relocatable link (output_bfd test tells us), just
2669 call the generic function. Any adjustment will be done at final
2670 link time. */
2671 if (output_bfd != NULL)
cedb70c5 2672 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2673 input_section, output_bfd, error_message);
5bd4f169 2674
805fc799
AM
2675 /* Subtract the symbol section base address. */
2676 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2677 return bfd_reloc_continue;
2678}
2679
805fc799 2680static bfd_reloc_status_type
4ce794b7
AM
2681ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2682 void *data, asection *input_section,
2683 bfd *output_bfd, char **error_message)
805fc799
AM
2684{
2685 /* If this is a relocatable link (output_bfd test tells us), just
2686 call the generic function. Any adjustment will be done at final
2687 link time. */
2688 if (output_bfd != NULL)
cedb70c5 2689 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2690 input_section, output_bfd, error_message);
2691
2692 /* Subtract the symbol section base address. */
2693 reloc_entry->addend -= symbol->section->output_section->vma;
2694
2695 /* Adjust the addend for sign extension of the low 16 bits. */
2696 reloc_entry->addend += 0x8000;
2697 return bfd_reloc_continue;
2698}
2699
2700static bfd_reloc_status_type
4ce794b7
AM
2701ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2702 void *data, asection *input_section,
2703 bfd *output_bfd, char **error_message)
805fc799
AM
2704{
2705 bfd_vma TOCstart;
2706
2707 /* If this is a relocatable link (output_bfd test tells us), just
2708 call the generic function. Any adjustment will be done at final
2709 link time. */
2710 if (output_bfd != NULL)
cedb70c5 2711 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2712 input_section, output_bfd, error_message);
2713
2714 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2715 if (TOCstart == 0)
1c865ab2 2716 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2717
2718 /* Subtract the TOC base address. */
2719 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2720 return bfd_reloc_continue;
2721}
2722
2723static bfd_reloc_status_type
4ce794b7
AM
2724ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2725 void *data, asection *input_section,
2726 bfd *output_bfd, char **error_message)
805fc799
AM
2727{
2728 bfd_vma TOCstart;
2729
2730 /* If this is a relocatable link (output_bfd test tells us), just
2731 call the generic function. Any adjustment will be done at final
2732 link time. */
2733 if (output_bfd != NULL)
cedb70c5 2734 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2735 input_section, output_bfd, error_message);
2736
2737 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2738 if (TOCstart == 0)
1c865ab2 2739 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2740
2741 /* Subtract the TOC base address. */
2742 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2743
2744 /* Adjust the addend for sign extension of the low 16 bits. */
2745 reloc_entry->addend += 0x8000;
2746 return bfd_reloc_continue;
2747}
2748
2749static bfd_reloc_status_type
4ce794b7
AM
2750ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2751 void *data, asection *input_section,
2752 bfd *output_bfd, char **error_message)
805fc799
AM
2753{
2754 bfd_vma TOCstart;
2755 bfd_size_type octets;
2756
2757 /* If this is a relocatable link (output_bfd test tells us), just
2758 call the generic function. Any adjustment will be done at final
2759 link time. */
2760 if (output_bfd != NULL)
cedb70c5 2761 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2762 input_section, output_bfd, error_message);
2763
2764 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2765 if (TOCstart == 0)
1c865ab2 2766 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2767
2768 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2769 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2770 return bfd_reloc_ok;
2771}
2772
2773static bfd_reloc_status_type
4ce794b7
AM
2774ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2775 void *data, asection *input_section,
2776 bfd *output_bfd, char **error_message)
805fc799
AM
2777{
2778 /* If this is a relocatable link (output_bfd test tells us), just
2779 call the generic function. Any adjustment will be done at final
2780 link time. */
2781 if (output_bfd != NULL)
cedb70c5 2782 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2783 input_section, output_bfd, error_message);
2784
2785 if (error_message != NULL)
2786 {
2787 static char buf[60];
2788 sprintf (buf, "generic linker can't handle %s",
2789 reloc_entry->howto->name);
2790 *error_message = buf;
2791 }
2792 return bfd_reloc_dangerous;
2793}
2794
927be08e
AM
2795/* Track GOT entries needed for a given symbol. We might need more
2796 than one got entry per symbol. */
2797struct got_entry
2798{
2799 struct got_entry *next;
2800
2801 /* The symbol addend that we'll be placing in the GOT. */
2802 bfd_vma addend;
2803
2804 /* Unlike other ELF targets, we use separate GOT entries for the same
2805 symbol referenced from different input files. This is to support
2806 automatic multiple TOC/GOT sections, where the TOC base can vary
2807 from one input file to another. After partitioning into TOC groups
2808 we merge entries within the group.
2809
2810 Point to the BFD owning this GOT entry. */
2811 bfd *owner;
2812
2813 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2814 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2815 unsigned char tls_type;
927be08e
AM
2816
2817 /* Non-zero if got.ent points to real entry. */
f961d9dd 2818 unsigned char is_indirect;
927be08e
AM
2819
2820 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2821 union
2822 {
2823 bfd_signed_vma refcount;
2824 bfd_vma offset;
2825 struct got_entry *ent;
2826 } got;
2827};
2828
2829/* The same for PLT. */
2830struct plt_entry
2831{
2832 struct plt_entry *next;
2833
2834 bfd_vma addend;
2835
2836 union
2837 {
2838 bfd_signed_vma refcount;
2839 bfd_vma offset;
2840 } plt;
2841};
2842
e717da7e
AM
2843struct ppc64_elf_obj_tdata
2844{
2845 struct elf_obj_tdata elf;
2846
2847 /* Shortcuts to dynamic linker sections. */
2848 asection *got;
2849 asection *relgot;
2850
b3fac117
AM
2851 /* Used during garbage collection. We attach global symbols defined
2852 on removed .opd entries to this section so that the sym is removed. */
2853 asection *deleted_section;
81688140 2854
927be08e 2855 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2856 sections means we potentially need one of these for each input bfd. */
927be08e 2857 struct got_entry tlsld_got;
8860955f 2858
729eabd5
AM
2859 union {
2860 /* A copy of relocs before they are modified for --emit-relocs. */
2861 Elf_Internal_Rela *relocs;
2862
2863 /* Section contents. */
2864 bfd_byte *contents;
2865 } opd;
d77c8a4b
AM
2866
2867 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2868 the reloc to be in the range -32768 to 32767. */
98528052
AM
2869 unsigned int has_small_toc_reloc : 1;
2870
560c8763
AM
2871 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2872 instruction not one we handle. */
2873 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2874};
2875
2876#define ppc64_elf_tdata(bfd) \
2877 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2878
2879#define ppc64_tlsld_got(bfd) \
2880 (&ppc64_elf_tdata (bfd)->tlsld_got)
2881
0c8d6e5c
AM
2882#define is_ppc64_elf(bfd) \
2883 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2884 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2885
e717da7e
AM
2886/* Override the generic function because we store some extras. */
2887
2888static bfd_boolean
2889ppc64_elf_mkobject (bfd *abfd)
2890{
0ffa91dd 2891 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2892 PPC64_ELF_DATA);
e717da7e
AM
2893}
2894
feee612b 2895/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 2896 default is 32 bit. Also select arch based on apuinfo. */
feee612b 2897
b34976b6 2898static bfd_boolean
4ce794b7 2899ppc64_elf_object_p (bfd *abfd)
feee612b 2900{
14b57c7c
AM
2901 if (!abfd->arch_info->the_default)
2902 return TRUE;
2903
2904 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
2905 {
2906 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2907
2908 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2909 {
2910 /* Relies on arch after 32 bit default being 64 bit default. */
2911 abfd->arch_info = abfd->arch_info->next;
2912 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2913 }
2914 }
14b57c7c 2915 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
2916}
2917
d37c89e5
AM
2918/* Support for core dump NOTE sections. */
2919
2920static bfd_boolean
2921ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2922{
eea6121a 2923 size_t offset, size;
d37c89e5
AM
2924
2925 if (note->descsz != 504)
2926 return FALSE;
2927
2928 /* pr_cursig */
228e534f 2929 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2930
2931 /* pr_pid */
228e534f 2932 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2933
2934 /* pr_reg */
2935 offset = 112;
eea6121a 2936 size = 384;
d37c89e5
AM
2937
2938 /* Make a ".reg/999" section. */
2939 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2940 size, note->descpos + offset);
d37c89e5
AM
2941}
2942
2943static bfd_boolean
2944ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2945{
2946 if (note->descsz != 136)
2947 return FALSE;
2948
228e534f 2949 elf_tdata (abfd)->core->pid
bc989cdc 2950 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2951 elf_tdata (abfd)->core->program
d37c89e5 2952 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2953 elf_tdata (abfd)->core->command
d37c89e5
AM
2954 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2955
2956 return TRUE;
2957}
2958
183e98be
AM
2959static char *
2960ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2961 ...)
2962{
2963 switch (note_type)
2964 {
2965 default:
2966 return NULL;
2967
2968 case NT_PRPSINFO:
2969 {
2970 char data[136];
2971 va_list ap;
2972
2973 va_start (ap, note_type);
75cd47ed 2974 memset (data, 0, sizeof (data));
183e98be
AM
2975 strncpy (data + 40, va_arg (ap, const char *), 16);
2976 strncpy (data + 56, va_arg (ap, const char *), 80);
2977 va_end (ap);
2978 return elfcore_write_note (abfd, buf, bufsiz,
2979 "CORE", note_type, data, sizeof (data));
2980 }
2981
2982 case NT_PRSTATUS:
2983 {
2984 char data[504];
2985 va_list ap;
2986 long pid;
2987 int cursig;
2988 const void *greg;
2989
2990 va_start (ap, note_type);
2991 memset (data, 0, 112);
2992 pid = va_arg (ap, long);
2993 bfd_put_32 (abfd, pid, data + 32);
2994 cursig = va_arg (ap, int);
2995 bfd_put_16 (abfd, cursig, data + 12);
2996 greg = va_arg (ap, const void *);
2997 memcpy (data + 112, greg, 384);
2998 memset (data + 496, 0, 8);
2999 va_end (ap);
3000 return elfcore_write_note (abfd, buf, bufsiz,
3001 "CORE", note_type, data, sizeof (data));
3002 }
3003 }
3004}
3005
5d35169e
AM
3006/* Add extra PPC sections. */
3007
b35d266b 3008static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 3009{
0112cd26
NC
3010 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3011 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3012 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3013 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3014 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3015 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3016 { NULL, 0, 0, 0, 0 }
5d35169e
AM
3017};
3018
7c8fe5c4
AM
3019enum _ppc64_sec_type {
3020 sec_normal = 0,
3021 sec_opd = 1,
3022 sec_toc = 2
3023};
3024
f0abc2a1
AM
3025struct _ppc64_elf_section_data
3026{
3027 struct bfd_elf_section_data elf;
411e1bfb 3028
f0abc2a1
AM
3029 union
3030 {
51aecdc5
AM
3031 /* An array with one entry for each opd function descriptor,
3032 and some spares since opd entries may be either 16 or 24 bytes. */
3033#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
3034 struct _opd_sec_data
3035 {
3036 /* Points to the function code section for local opd entries. */
3037 asection **func_sec;
3038
3039 /* After editing .opd, adjust references to opd local syms. */
3040 long *adjust;
3041 } opd;
7c8fe5c4 3042
3a71aa26
AM
3043 /* An array for toc sections, indexed by offset/8. */
3044 struct _toc_sec_data
3045 {
3046 /* Specifies the relocation symbol index used at a given toc offset. */
3047 unsigned *symndx;
3048
3049 /* And the relocation addend. */
3050 bfd_vma *add;
3051 } toc;
7c8fe5c4
AM
3052 } u;
3053
3054 enum _ppc64_sec_type sec_type:2;
411e1bfb 3055
7c8fe5c4
AM
3056 /* Flag set when small branches are detected. Used to
3057 select suitable defaults for the stub group size. */
3058 unsigned int has_14bit_branch:1;
f0abc2a1
AM
3059};
3060
3061#define ppc64_elf_section_data(sec) \
411e1bfb 3062 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
3063
3064static bfd_boolean
4ce794b7 3065ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 3066{
f592407e
AM
3067 if (!sec->used_by_bfd)
3068 {
3069 struct _ppc64_elf_section_data *sdata;
3070 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 3071
f592407e
AM
3072 sdata = bfd_zalloc (abfd, amt);
3073 if (sdata == NULL)
3074 return FALSE;
3075 sec->used_by_bfd = sdata;
3076 }
f0abc2a1
AM
3077
3078 return _bfd_elf_new_section_hook (abfd, sec);
3079}
4025353c 3080
74f0fb50 3081static struct _opd_sec_data *
4025353c
AM
3082get_opd_info (asection * sec)
3083{
3084 if (sec != NULL
3085 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3086 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3087 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3088 return NULL;
3089}
90e3cdf2
JJ
3090\f
3091/* Parameters for the qsort hook. */
90e3cdf2 3092static bfd_boolean synthetic_relocatable;
cd285db5 3093static asection *synthetic_opd;
90e3cdf2 3094
699733f6 3095/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3096
3097static int
3098compare_symbols (const void *ap, const void *bp)
3099{
3100 const asymbol *a = * (const asymbol **) ap;
3101 const asymbol *b = * (const asymbol **) bp;
3102
699733f6
AM
3103 /* Section symbols first. */
3104 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3105 return -1;
699733f6 3106 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3107 return 1;
3108
699733f6 3109 /* then .opd symbols. */
cd285db5
AM
3110 if (synthetic_opd != NULL)
3111 {
3112 if (strcmp (a->section->name, ".opd") == 0
3113 && strcmp (b->section->name, ".opd") != 0)
3114 return -1;
3115 if (strcmp (a->section->name, ".opd") != 0
3116 && strcmp (b->section->name, ".opd") == 0)
3117 return 1;
3118 }
90e3cdf2 3119
699733f6 3120 /* then other code symbols. */
90e3cdf2
JJ
3121 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3122 == (SEC_CODE | SEC_ALLOC)
3123 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3124 != (SEC_CODE | SEC_ALLOC))
3125 return -1;
3126
3127 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3128 != (SEC_CODE | SEC_ALLOC)
3129 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3130 == (SEC_CODE | SEC_ALLOC))
3131 return 1;
3132
3133 if (synthetic_relocatable)
3134 {
3135 if (a->section->id < b->section->id)
3136 return -1;
3137
3138 if (a->section->id > b->section->id)
3139 return 1;
3140 }
3141
3142 if (a->value + a->section->vma < b->value + b->section->vma)
3143 return -1;
3144
3145 if (a->value + a->section->vma > b->value + b->section->vma)
3146 return 1;
3147
4d35a0aa
AM
3148 /* For syms with the same value, prefer strong dynamic global function
3149 syms over other syms. */
3150 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3151 return -1;
3152
3153 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3154 return 1;
3155
3156 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3157 return -1;
3158
3159 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3160 return 1;
3161
3162 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3163 return -1;
3164
3165 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3166 return 1;
3167
3168 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3169 return -1;
3170
3171 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3172 return 1;
3173
90e3cdf2
JJ
3174 return 0;
3175}
3176
699733f6 3177/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3178
699733f6 3179static asymbol *
7292b3ac 3180sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3181{
699733f6 3182 long mid;
90e3cdf2 3183
7292b3ac 3184 if (id == (unsigned) -1)
699733f6
AM
3185 {
3186 while (lo < hi)
3187 {
3188 mid = (lo + hi) >> 1;
3189 if (syms[mid]->value + syms[mid]->section->vma < value)
3190 lo = mid + 1;
3191 else if (syms[mid]->value + syms[mid]->section->vma > value)
3192 hi = mid;
3193 else
3194 return syms[mid];
3195 }
3196 }
3197 else
3198 {
3199 while (lo < hi)
3200 {
3201 mid = (lo + hi) >> 1;
3202 if (syms[mid]->section->id < id)
3203 lo = mid + 1;
3204 else if (syms[mid]->section->id > id)
3205 hi = mid;
3206 else if (syms[mid]->value < value)
3207 lo = mid + 1;
3208 else if (syms[mid]->value > value)
3209 hi = mid;
3210 else
3211 return syms[mid];
3212 }
3213 }
3214 return NULL;
90e3cdf2
JJ
3215}
3216
468392fb
AM
3217static bfd_boolean
3218section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3219{
3220 bfd_vma vma = *(bfd_vma *) ptr;
3221 return ((section->flags & SEC_ALLOC) != 0
3222 && section->vma <= vma
3223 && vma < section->vma + section->size);
3224}
3225
699733f6 3226/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3227 entry syms. Also generate @plt symbols for the glink branch table.
3228 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3229
3230static long
a7535cf3
AM
3231ppc64_elf_get_synthetic_symtab (bfd *abfd,
3232 long static_count, asymbol **static_syms,
3233 long dyn_count, asymbol **dyn_syms,
c9727e01 3234 asymbol **ret)
90e3cdf2
JJ
3235{
3236 asymbol *s;
699733f6
AM
3237 long i;
3238 long count;
90e3cdf2 3239 char *names;
a7535cf3 3240 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3241 asection *opd = NULL;
90e3cdf2 3242 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3243 asymbol **syms;
ee67d69a 3244 int abi = abiversion (abfd);
90e3cdf2
JJ
3245
3246 *ret = NULL;
3247
ee67d69a
AM
3248 if (abi < 2)
3249 {
3250 opd = bfd_get_section_by_name (abfd, ".opd");
3251 if (opd == NULL && abi == 1)
3252 return 0;
3253 }
90e3cdf2 3254
a7535cf3 3255 symcount = static_count;
c9727e01 3256 if (!relocatable)
a7535cf3 3257 symcount += dyn_count;
90e3cdf2 3258 if (symcount == 0)
c9727e01 3259 return 0;
90e3cdf2 3260
a7535cf3
AM
3261 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3262 if (syms == NULL)
7356fed5 3263 return -1;
a7535cf3
AM
3264
3265 if (!relocatable && static_count != 0 && dyn_count != 0)
3266 {
3267 /* Use both symbol tables. */
3268 memcpy (syms, static_syms, static_count * sizeof (*syms));
3269 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3270 }
3271 else if (!relocatable && static_count == 0)
3272 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3273 else
3274 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3275
90e3cdf2 3276 synthetic_relocatable = relocatable;
cd285db5 3277 synthetic_opd = opd;
595da8c5 3278 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3279
c9727e01
AM
3280 if (!relocatable && symcount > 1)
3281 {
3282 long j;
3283 /* Trim duplicate syms, since we may have merged the normal and
3284 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3285 different values, so trim any with the same value. */
c9727e01
AM
3286 for (i = 1, j = 1; i < symcount; ++i)
3287 if (syms[i - 1]->value + syms[i - 1]->section->vma
3288 != syms[i]->value + syms[i]->section->vma)
3289 syms[j++] = syms[i];
3290 symcount = j;
3291 }
3292
699733f6 3293 i = 0;
cd285db5
AM
3294 /* Note that here and in compare_symbols we can't compare opd and
3295 sym->section directly. With separate debug info files, the
3296 symbols will be extracted from the debug file while abfd passed
3297 to this function is the real binary. */
3298 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3299 ++i;
3300 codesecsym = i;
90e3cdf2 3301
699733f6
AM
3302 for (; i < symcount; ++i)
3303 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3304 != (SEC_CODE | SEC_ALLOC))
3305 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3306 break;
3307 codesecsymend = i;
90e3cdf2 3308
699733f6
AM
3309 for (; i < symcount; ++i)
3310 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3311 break;
3312 secsymend = i;
90e3cdf2 3313
cd285db5
AM
3314 if (opd != NULL)
3315 for (; i < symcount; ++i)
3316 if (strcmp (syms[i]->section->name, ".opd") != 0)
3317 break;
699733f6 3318 opdsymend = i;
90e3cdf2 3319
699733f6
AM
3320 for (; i < symcount; ++i)
3321 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3322 != (SEC_CODE | SEC_ALLOC))
3323 break;
3324 symcount = i;
3325
c9727e01 3326 count = 0;
90e3cdf2 3327
699733f6 3328 if (relocatable)
90e3cdf2 3329 {
699733f6
AM
3330 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3331 arelent *r;
3332 size_t size;
3333 long relcount;
90e3cdf2 3334
468392fb
AM
3335 if (opdsymend == secsymend)
3336 goto done;
3337
699733f6 3338 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3339 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3340 if (relcount == 0)
c9727e01 3341 goto done;
90e3cdf2 3342
7356fed5
AM
3343 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3344 {
3345 count = -1;
3346 goto done;
3347 }
3348
699733f6 3349 size = 0;
595da8c5 3350 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3351 {
3352 asymbol *sym;
90e3cdf2 3353
595da8c5 3354 while (r < opd->relocation + relcount
699733f6
AM
3355 && r->address < syms[i]->value + opd->vma)
3356 ++r;
90e3cdf2 3357
595da8c5 3358 if (r == opd->relocation + relcount)
699733f6 3359 break;
90e3cdf2 3360
699733f6
AM
3361 if (r->address != syms[i]->value + opd->vma)
3362 continue;
90e3cdf2 3363
699733f6
AM
3364 if (r->howto->type != R_PPC64_ADDR64)
3365 continue;
90e3cdf2 3366
699733f6
AM
3367 sym = *r->sym_ptr_ptr;
3368 if (!sym_exists_at (syms, opdsymend, symcount,
3369 sym->section->id, sym->value + r->addend))
3370 {
3371 ++count;
3372 size += sizeof (asymbol);
3373 size += strlen (syms[i]->name) + 2;
3374 }
3375 }
90e3cdf2 3376
c4b0b099
AM
3377 if (size == 0)
3378 goto done;
699733f6
AM
3379 s = *ret = bfd_malloc (size);
3380 if (s == NULL)
3381 {
7356fed5 3382 count = -1;
c9727e01 3383 goto done;
699733f6 3384 }
90e3cdf2 3385
699733f6 3386 names = (char *) (s + count);
90e3cdf2 3387
595da8c5 3388 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3389 {
699733f6 3390 asymbol *sym;
90e3cdf2 3391
595da8c5 3392 while (r < opd->relocation + relcount
699733f6
AM
3393 && r->address < syms[i]->value + opd->vma)
3394 ++r;
90e3cdf2 3395
595da8c5 3396 if (r == opd->relocation + relcount)
699733f6
AM
3397 break;
3398
3399 if (r->address != syms[i]->value + opd->vma)
3400 continue;
3401
3402 if (r->howto->type != R_PPC64_ADDR64)
3403 continue;
90e3cdf2 3404
699733f6
AM
3405 sym = *r->sym_ptr_ptr;
3406 if (!sym_exists_at (syms, opdsymend, symcount,
3407 sym->section->id, sym->value + r->addend))
3408 {
3409 size_t len;
3410
3411 *s = *syms[i];
6ba2a415 3412 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3413 s->section = sym->section;
3414 s->value = sym->value + r->addend;
3415 s->name = names;
3416 *names++ = '.';
3417 len = strlen (syms[i]->name);
3418 memcpy (names, syms[i]->name, len + 1);
3419 names += len + 1;
6f610d07
UW
3420 /* Have udata.p point back to the original symbol this
3421 synthetic symbol was derived from. */
3422 s->udata.p = syms[i];
699733f6
AM
3423 s++;
3424 }
3425 }
3426 }
3427 else
90e3cdf2 3428 {
468392fb 3429 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3430 bfd_byte *contents = NULL;
699733f6 3431 size_t size;
468392fb
AM
3432 long plt_count = 0;
3433 bfd_vma glink_vma = 0, resolv_vma = 0;
3434 asection *dynamic, *glink = NULL, *relplt = NULL;
3435 arelent *p;
90e3cdf2 3436
ee67d69a 3437 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3438 {
c4b0b099
AM
3439 free_contents_and_exit_err:
3440 count = -1;
ee67d69a 3441 free_contents_and_exit:
699733f6 3442 if (contents)
ee67d69a 3443 free (contents);
c9727e01 3444 goto done;
699733f6 3445 }
90e3cdf2 3446
699733f6
AM
3447 size = 0;
3448 for (i = secsymend; i < opdsymend; ++i)
3449 {
3450 bfd_vma ent;
90e3cdf2 3451
5ef11c02
AM
3452 /* Ignore bogus symbols. */
3453 if (syms[i]->value > opd->size - 8)
3454 continue;
3455
699733f6
AM
3456 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3457 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3458 {
3459 ++count;
3460 size += sizeof (asymbol);
3461 size += strlen (syms[i]->name) + 2;
3462 }
3463 }
90e3cdf2 3464
468392fb 3465 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3466 if (dyn_count != 0
3467 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3468 {
3469 bfd_byte *dynbuf, *extdyn, *extdynend;
3470 size_t extdynsize;
3471 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3472
3473 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3474 goto free_contents_and_exit_err;
468392fb
AM
3475
3476 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3477 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3478
3479 extdyn = dynbuf;
3480 extdynend = extdyn + dynamic->size;
3481 for (; extdyn < extdynend; extdyn += extdynsize)
3482 {
3483 Elf_Internal_Dyn dyn;
3484 (*swap_dyn_in) (abfd, extdyn, &dyn);
3485
3486 if (dyn.d_tag == DT_NULL)
3487 break;
3488
3489 if (dyn.d_tag == DT_PPC64_GLINK)
3490 {
b9e5796b
AM
3491 /* The first glink stub starts at offset 32; see
3492 comment in ppc64_elf_finish_dynamic_sections. */
3493 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
468392fb
AM
3494 /* The .glink section usually does not survive the final
3495 link; search for the section (usually .text) where the
3496 glink stubs now reside. */
3497 glink = bfd_sections_find_if (abfd, section_covers_vma,
3498 &glink_vma);
3499 break;
3500 }
3501 }
3502
3503 free (dynbuf);
3504 }
3505
3506 if (glink != NULL)
3507 {
3508 /* Determine __glink trampoline by reading the relative branch
3509 from the first glink stub. */
3510 bfd_byte buf[4];
b9e5796b
AM
3511 unsigned int off = 0;
3512
3513 while (bfd_get_section_contents (abfd, glink, buf,
3514 glink_vma + off - glink->vma, 4))
468392fb
AM
3515 {
3516 unsigned int insn = bfd_get_32 (abfd, buf);
3517 insn ^= B_DOT;
3518 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3519 {
3520 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3521 break;
3522 }
3523 off += 4;
3524 if (off > 4)
3525 break;
468392fb
AM
3526 }
3527
3528 if (resolv_vma)
3529 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3530
066ee829
AM
3531 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3532 if (relplt != NULL)
3533 {
3534 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3535 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3536 goto free_contents_and_exit_err;
68ffbac6 3537
066ee829
AM
3538 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3539 size += plt_count * sizeof (asymbol);
468392fb 3540
066ee829
AM
3541 p = relplt->relocation;
3542 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3543 {
3544 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3545 if (p->addend != 0)
3546 size += sizeof ("+0x") - 1 + 16;
3547 }
066ee829 3548 }
468392fb
AM
3549 }
3550
c4b0b099
AM
3551 if (size == 0)
3552 goto free_contents_and_exit;
699733f6
AM
3553 s = *ret = bfd_malloc (size);
3554 if (s == NULL)
c4b0b099 3555 goto free_contents_and_exit_err;
90e3cdf2 3556
468392fb 3557 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3558
699733f6 3559 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3560 {
699733f6 3561 bfd_vma ent;
90e3cdf2 3562
5ef11c02
AM
3563 if (syms[i]->value > opd->size - 8)
3564 continue;
3565
699733f6
AM
3566 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3567 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3568 {
c9727e01 3569 long lo, hi;
699733f6 3570 size_t len;
c9727e01 3571 asection *sec = abfd->sections;
90e3cdf2 3572
699733f6
AM
3573 *s = *syms[i];
3574 lo = codesecsym;
3575 hi = codesecsymend;
3576 while (lo < hi)
3577 {
c9727e01 3578 long mid = (lo + hi) >> 1;
699733f6
AM
3579 if (syms[mid]->section->vma < ent)
3580 lo = mid + 1;
3581 else if (syms[mid]->section->vma > ent)
3582 hi = mid;
3583 else
c9727e01
AM
3584 {
3585 sec = syms[mid]->section;
3586 break;
3587 }
699733f6
AM
3588 }
3589
c9727e01 3590 if (lo >= hi && lo > codesecsym)
699733f6 3591 sec = syms[lo - 1]->section;
699733f6
AM
3592
3593 for (; sec != NULL; sec = sec->next)
3594 {
3595 if (sec->vma > ent)
3596 break;
63524580
JK
3597 /* SEC_LOAD may not be set if SEC is from a separate debug
3598 info file. */
3599 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3600 break;
3601 if ((sec->flags & SEC_CODE) != 0)
3602 s->section = sec;
3603 }
6ba2a415 3604 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3605 s->value = ent - s->section->vma;
3606 s->name = names;
3607 *names++ = '.';
3608 len = strlen (syms[i]->name);
3609 memcpy (names, syms[i]->name, len + 1);
3610 names += len + 1;
6f610d07
UW
3611 /* Have udata.p point back to the original symbol this
3612 synthetic symbol was derived from. */
3613 s->udata.p = syms[i];
699733f6 3614 s++;
90e3cdf2 3615 }
90e3cdf2 3616 }
699733f6 3617 free (contents);
468392fb
AM
3618
3619 if (glink != NULL && relplt != NULL)
3620 {
3621 if (resolv_vma)
3622 {
3623 /* Add a symbol for the main glink trampoline. */
86a4952b 3624 memset (s, 0, sizeof *s);
468392fb 3625 s->the_bfd = abfd;
6ba2a415 3626 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3627 s->section = glink;
3628 s->value = resolv_vma - glink->vma;
3629 s->name = names;
3630 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3631 names += sizeof ("__glink_PLTresolve");
3632 s++;
3633 count++;
3634 }
3635
3636 /* FIXME: It would be very much nicer to put sym@plt on the
3637 stub rather than on the glink branch table entry. The
3638 objdump disassembler would then use a sensible symbol
3639 name on plt calls. The difficulty in doing so is
3640 a) finding the stubs, and,
3641 b) matching stubs against plt entries, and,
3642 c) there can be multiple stubs for a given plt entry.
3643
3644 Solving (a) could be done by code scanning, but older
3645 ppc64 binaries used different stubs to current code.
3646 (b) is the tricky one since you need to known the toc
3647 pointer for at least one function that uses a pic stub to
3648 be able to calculate the plt address referenced.
3649 (c) means gdb would need to set multiple breakpoints (or
3650 find the glink branch itself) when setting breakpoints
3651 for pending shared library loads. */
3652 p = relplt->relocation;
3653 for (i = 0; i < plt_count; i++, p++)
3654 {
3655 size_t len;
3656
3657 *s = **p->sym_ptr_ptr;
3658 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3659 we are defining a symbol, ensure one of them is set. */
3660 if ((s->flags & BSF_LOCAL) == 0)
3661 s->flags |= BSF_GLOBAL;
6ba2a415 3662 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3663 s->section = glink;
3664 s->value = glink_vma - glink->vma;
3665 s->name = names;
3666 s->udata.p = NULL;
3667 len = strlen ((*p->sym_ptr_ptr)->name);
3668 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3669 names += len;
e054468f
AM
3670 if (p->addend != 0)
3671 {
3672 memcpy (names, "+0x", sizeof ("+0x") - 1);
3673 names += sizeof ("+0x") - 1;
3674 bfd_sprintf_vma (abfd, names, p->addend);
3675 names += strlen (names);
3676 }
468392fb
AM
3677 memcpy (names, "@plt", sizeof ("@plt"));
3678 names += sizeof ("@plt");
3679 s++;
b9e5796b
AM
3680 if (abi < 2)
3681 {
3682 glink_vma += 8;
3683 if (i >= 0x8000)
3684 glink_vma += 4;
3685 }
3686 else
468392fb
AM
3687 glink_vma += 4;
3688 }
3689 count += plt_count;
3690 }
90e3cdf2
JJ
3691 }
3692
c9727e01 3693 done:
a7535cf3 3694 free (syms);
90e3cdf2
JJ
3695 return count;
3696}
5bd4f169 3697\f
65f38f15
AM
3698/* The following functions are specific to the ELF linker, while
3699 functions above are used generally. Those named ppc64_elf_* are
3700 called by the main ELF linker code. They appear in this file more
3701 or less in the order in which they are called. eg.
3702 ppc64_elf_check_relocs is called early in the link process,
3703 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3704 called.
3705
3706 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3707 functions have both a function code symbol and a function descriptor
3708 symbol. A call to foo in a relocatable object file looks like:
3709
3710 . .text
3711 . x:
3712 . bl .foo
3713 . nop
3714
3715 The function definition in another object file might be:
3716
3717 . .section .opd
3718 . foo: .quad .foo
3719 . .quad .TOC.@tocbase
3720 . .quad 0
3721 .
3722 . .text
3723 . .foo: blr
3724
3725 When the linker resolves the call during a static link, the branch
3726 unsurprisingly just goes to .foo and the .opd information is unused.
3727 If the function definition is in a shared library, things are a little
3728 different: The call goes via a plt call stub, the opd information gets
3729 copied to the plt, and the linker patches the nop.
3730
3731 . x:
3732 . bl .foo_stub
3733 . ld 2,40(1)
3734 .
3735 .
3736 . .foo_stub:
71a39c98
AM
3737 . std 2,40(1) # in practice, the call stub
3738 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3739 . addi 11,11,Lfoo@toc@l # this is the general idea
3740 . ld 12,0(11)
3741 . ld 2,8(11)
3742 . mtctr 12
3743 . ld 11,16(11)
e86ce104
AM
3744 . bctr
3745 .
3746 . .section .plt
3747 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3748
3749 The "reloc ()" notation is supposed to indicate that the linker emits
3750 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3751 copying.
3752
3753 What are the difficulties here? Well, firstly, the relocations
3754 examined by the linker in check_relocs are against the function code
3755 sym .foo, while the dynamic relocation in the plt is emitted against
3756 the function descriptor symbol, foo. Somewhere along the line, we need
3757 to carefully copy dynamic link information from one symbol to the other.
3758 Secondly, the generic part of the elf linker will make .foo a dynamic
3759 symbol as is normal for most other backends. We need foo dynamic
3760 instead, at least for an application final link. However, when
3761 creating a shared library containing foo, we need to have both symbols
3762 dynamic so that references to .foo are satisfied during the early
3763 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3764 definition from some other object, eg. a static library.
3765
3766 Update: As of August 2004, we support a new convention. Function
3767 calls may use the function descriptor symbol, ie. "bl foo". This
3768 behaves exactly as "bl .foo". */
65f38f15 3769
1d483afe 3770/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3771 selects those that must be copied when linking a shared library,
3772 even when the symbol is local. */
65f38f15 3773
1d483afe
AM
3774static int
3775must_be_dyn_reloc (struct bfd_link_info *info,
3776 enum elf_ppc64_reloc_type r_type)
3777{
3778 switch (r_type)
3779 {
3780 default:
3781 return 1;
3782
3783 case R_PPC64_REL32:
3784 case R_PPC64_REL64:
3785 case R_PPC64_REL30:
3786 return 0;
3787
3788 case R_PPC64_TPREL16:
3789 case R_PPC64_TPREL16_LO:
3790 case R_PPC64_TPREL16_HI:
3791 case R_PPC64_TPREL16_HA:
3792 case R_PPC64_TPREL16_DS:
3793 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3794 case R_PPC64_TPREL16_HIGH:
3795 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3796 case R_PPC64_TPREL16_HIGHER:
3797 case R_PPC64_TPREL16_HIGHERA:
3798 case R_PPC64_TPREL16_HIGHEST:
3799 case R_PPC64_TPREL16_HIGHESTA:
3800 case R_PPC64_TPREL64:
0e1862bb 3801 return !bfd_link_executable (info);
1d483afe
AM
3802 }
3803}
65f38f15 3804
f4656909
AM
3805/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3806 copying dynamic variables from a shared lib into an app's dynbss
3807 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3808 shared lib. With code that gcc generates, it's vital that this be
3809 enabled; In the PowerPC64 ABI, the address of a function is actually
3810 the address of a function descriptor, which resides in the .opd
3811 section. gcc uses the descriptor directly rather than going via the
3812 GOT as some other ABI's do, which means that initialized function
3813 pointers must reference the descriptor. Thus, a function pointer
3814 initialized to the address of a function in a shared library will
3815 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3816 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3817 presents a problem as a plt entry for that function is also
3818 initialized from the function descriptor symbol and the copy reloc
3819 may not be initialized first. */
a23b6845 3820#define ELIMINATE_COPY_RELOCS 1
f4656909 3821
721956f4 3822/* Section name for stubs is the associated section name plus this
29942be8
NC
3823 string. */
3824#define STUB_SUFFIX ".stub"
721956f4
AM
3825
3826/* Linker stubs.
3827 ppc_stub_long_branch:
3828 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3829 destination, but a 24 bit branch in a stub section will reach.
3830 . b dest
3831
3832 ppc_stub_plt_branch:
3833 Similar to the above, but a 24 bit branch in the stub section won't
3834 reach its destination.
71a39c98
AM
3835 . addis %r11,%r2,xxx@toc@ha
3836 . ld %r12,xxx@toc@l(%r11)
3837 . mtctr %r12
721956f4
AM
3838 . bctr
3839
3840 ppc_stub_plt_call:
2c66dc6c
AM
3841 Used to call a function in a shared library. If it so happens that
3842 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3843 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3844 . std %r2,40(%r1)
71a39c98
AM
3845 . addis %r11,%r2,xxx@toc@ha
3846 . ld %r12,xxx+0@toc@l(%r11)
3847 . mtctr %r12
3848 . ld %r2,xxx+8@toc@l(%r11)
3849 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3850 . bctr
ad8e1ba5
AM
3851
3852 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3853 code to adjust the value and save r2 to support multiple toc sections.
3854 A ppc_stub_long_branch with an r2 offset looks like:
3855 . std %r2,40(%r1)
3856 . addis %r2,%r2,off@ha
3857 . addi %r2,%r2,off@l
3858 . b dest
3859
3860 A ppc_stub_plt_branch with an r2 offset looks like:
3861 . std %r2,40(%r1)
71a39c98
AM
3862 . addis %r11,%r2,xxx@toc@ha
3863 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3864 . addis %r2,%r2,off@ha
3865 . addi %r2,%r2,off@l
71a39c98 3866 . mtctr %r12
ad8e1ba5 3867 . bctr
ac2df442
AM
3868
3869 In cases where the "addis" instruction would add zero, the "addis" is
3870 omitted and following instructions modified slightly in some cases.
721956f4
AM
3871*/
3872
3873enum ppc_stub_type {
3874 ppc_stub_none,
3875 ppc_stub_long_branch,
ad8e1ba5 3876 ppc_stub_long_branch_r2off,
721956f4 3877 ppc_stub_plt_branch,
ad8e1ba5 3878 ppc_stub_plt_branch_r2off,
794e51c0 3879 ppc_stub_plt_call,
7341d5e2 3880 ppc_stub_plt_call_r2save,
a4b6fadd
AM
3881 ppc_stub_global_entry,
3882 ppc_stub_save_res
721956f4
AM
3883};
3884
6f20ed8a
AM
3885/* Information on stub grouping. */
3886struct map_stub
3887{
3888 /* The stub section. */
3889 asection *stub_sec;
3890 /* This is the section to which stubs in the group will be attached. */
3891 asection *link_sec;
a4b6fadd
AM
3892 /* Next group. */
3893 struct map_stub *next;
3894 /* Whether to emit a copy of register save/restore functions in this
3895 group. */
3896 int needs_save_res;
6f20ed8a
AM
3897};
3898
721956f4
AM
3899struct ppc_stub_hash_entry {
3900
3901 /* Base hash table entry structure. */
3902 struct bfd_hash_entry root;
3903
ad8e1ba5
AM
3904 enum ppc_stub_type stub_type;
3905
6f20ed8a
AM
3906 /* Group information. */
3907 struct map_stub *group;
721956f4
AM
3908
3909 /* Offset within stub_sec of the beginning of this stub. */
3910 bfd_vma stub_offset;
3911
3912 /* Given the symbol's value and its section we can determine its final
3913 value when building the stubs (so the stub knows where to jump. */
3914 bfd_vma target_value;
3915 asection *target_section;
3916
721956f4
AM
3917 /* The symbol table entry, if any, that this was derived from. */
3918 struct ppc_link_hash_entry *h;
e054468f 3919 struct plt_entry *plt_ent;
721956f4 3920
6911b7dc
AM
3921 /* Symbol st_other. */
3922 unsigned char other;
721956f4
AM
3923};
3924
3925struct ppc_branch_hash_entry {
3926
3927 /* Base hash table entry structure. */
3928 struct bfd_hash_entry root;
3929
c456f082 3930 /* Offset within branch lookup table. */
721956f4
AM
3931 unsigned int offset;
3932
3933 /* Generation marker. */
3934 unsigned int iter;
3935};
65f38f15 3936
19e08130
AM
3937/* Used to track dynamic relocations for local symbols. */
3938struct ppc_dyn_relocs
3939{
3940 struct ppc_dyn_relocs *next;
3941
3942 /* The input section of the reloc. */
3943 asection *sec;
3944
3945 /* Total number of relocs copied for the input section. */
3946 unsigned int count : 31;
3947
3948 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3949 unsigned int ifunc : 1;
3950};
3951
65f38f15
AM
3952struct ppc_link_hash_entry
3953{
3954 struct elf_link_hash_entry elf;
3955
b3fac117
AM
3956 union {
3957 /* A pointer to the most recently used stub hash entry against this
3958 symbol. */
3959 struct ppc_stub_hash_entry *stub_cache;
3960
3961 /* A pointer to the next symbol starting with a '.' */
3962 struct ppc_link_hash_entry *next_dot_sym;
3963 } u;
721956f4 3964
65f38f15 3965 /* Track dynamic relocs copied for this symbol. */
6061a67d 3966 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3967
d311bc8b
AM
3968 /* Chain of aliases referring to a weakdef. */
3969 struct ppc_link_hash_entry *weakref;
3970
721956f4 3971 /* Link between function code and descriptor symbols. */
34814b9f 3972 struct ppc_link_hash_entry *oh;
721956f4 3973
e86ce104
AM
3974 /* Flag function code and descriptor symbols. */
3975 unsigned int is_func:1;
3976 unsigned int is_func_descriptor:1;
908b32fc 3977 unsigned int fake:1;
411e1bfb 3978
c5614fa4
AM
3979 /* Whether global opd/toc sym has been adjusted or not.
3980 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3981 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3982 unsigned int adjust_done:1;
3983
a4b6fadd
AM
3984 /* Set if this is an out-of-line register save/restore function,
3985 with non-standard calling convention. */
3986 unsigned int save_res:1;
3987
411e1bfb 3988 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3989 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3990 corresponding relocs are encountered during check_relocs.
3991 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3992 indicate the corresponding GOT entry type is not needed.
3993 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3994 a TPREL one. We use a separate flag rather than setting TPREL
3995 just for convenience in distinguishing the two cases. */
3996#define TLS_GD 1 /* GD reloc. */
3997#define TLS_LD 2 /* LD reloc. */
3998#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3999#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4000#define TLS_TLS 16 /* Any TLS reloc. */
4001#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4002#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 4003#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 4004 unsigned char tls_mask;
65f38f15
AM
4005};
4006
4007/* ppc64 ELF linker hash table. */
4008
4009struct ppc_link_hash_table
4010{
4011 struct elf_link_hash_table elf;
4012
721956f4
AM
4013 /* The stub hash table. */
4014 struct bfd_hash_table stub_hash_table;
4015
4016 /* Another hash table for plt_branch stubs. */
4017 struct bfd_hash_table branch_hash_table;
4018
3b421ab3
AM
4019 /* Hash table for function prologue tocsave. */
4020 htab_t tocsave_htab;
4021
e7d1c40c
AM
4022 /* Various options and other info passed from the linker. */
4023 struct ppc64_elf_params *params;
721956f4 4024
6f20ed8a
AM
4025 /* The size of sec_info below. */
4026 unsigned int sec_info_arr_size;
4027
4028 /* Per-section array of extra section info. Done this way rather
4029 than as part of ppc64_elf_section_data so we have the info for
4030 non-ppc64 sections. */
4031 struct
4032 {
4033 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 4034 bfd_vma toc_off;
6f20ed8a
AM
4035
4036 union
4037 {
4038 /* The section group that this section belongs to. */
4039 struct map_stub *group;
4040 /* A temp section list pointer. */
4041 asection *list;
4042 } u;
4043 } *sec_info;
721956f4 4044
a4b6fadd
AM
4045 /* Linked list of groups. */
4046 struct map_stub *group;
4047
ad8e1ba5
AM
4048 /* Temp used when calculating TOC pointers. */
4049 bfd_vma toc_curr;
bf102f86
AM
4050 bfd *toc_bfd;
4051 asection *toc_first_sec;
ad8e1ba5 4052
b3fac117
AM
4053 /* Used when adding symbols. */
4054 struct ppc_link_hash_entry *dot_syms;
4055
33e44f2e 4056 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 4057 asection *glink;
82bd7b59 4058 asection *sfpr;
4ce794b7
AM
4059 asection *brlt;
4060 asection *relbrlt;
58d180e8 4061 asection *glink_eh_frame;
ec338859 4062
8387904d
AM
4063 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4064 struct ppc_link_hash_entry *tls_get_addr;
4065 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4066
927be08e
AM
4067 /* The size of reliplt used by got entry relocs. */
4068 bfd_size_type got_reli_size;
4069
9b5ecbd0 4070 /* Statistics. */
7341d5e2 4071 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4072
ee75fd95
AM
4073 /* Number of stubs against global syms. */
4074 unsigned long stub_globals;
4075
ee67d69a
AM
4076 /* Set if we're linking code with function descriptors. */
4077 unsigned int opd_abi:1;
4078
4c52953f 4079 /* Support for multiple toc sections. */
33c0ec9d 4080 unsigned int do_multi_toc:1;
4c52953f 4081 unsigned int multi_toc_needed:1;
927be08e 4082 unsigned int second_toc_pass:1;
67f0cbdb 4083 unsigned int do_toc_opt:1;
4c52953f 4084
5d1634d7 4085 /* Set on error. */
99877b66 4086 unsigned int stub_error:1;
721956f4 4087
8c5b4e52
AM
4088 /* Whether func_desc_adjust needs to be run over symbols. */
4089 unsigned int need_func_desc_adj:1;
721956f4
AM
4090
4091 /* Incremented every time we size stubs. */
4092 unsigned int stub_iteration;
5d1634d7 4093
87d72d41
AM
4094 /* Small local sym cache. */
4095 struct sym_cache sym_cache;
65f38f15
AM
4096};
4097
4c52953f
AM
4098/* Rename some of the generic section flags to better document how they
4099 are used here. */
b0dddeec
AM
4100
4101/* Nonzero if this section has TLS related relocations. */
4102#define has_tls_reloc sec_flg0
4103
4104/* Nonzero if this section has a call to __tls_get_addr. */
4105#define has_tls_get_addr_call sec_flg1
4106
4107/* Nonzero if this section has any toc or got relocs. */
4108#define has_toc_reloc sec_flg2
4109
4110/* Nonzero if this section has a call to another section that uses
4111 the toc or got. */
d77c8a4b 4112#define makes_toc_func_call sec_flg3
b0dddeec
AM
4113
4114/* Recursion protection when determining above flag. */
d77c8a4b 4115#define call_check_in_progress sec_flg4
70cc837d 4116#define call_check_done sec_flg5
4c52953f 4117
65f38f15
AM
4118/* Get the ppc64 ELF linker hash table from a link_info structure. */
4119
4120#define ppc_hash_table(p) \
4dfe6ac6
NC
4121 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4122 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4123
721956f4
AM
4124#define ppc_stub_hash_lookup(table, string, create, copy) \
4125 ((struct ppc_stub_hash_entry *) \
4126 bfd_hash_lookup ((table), (string), (create), (copy)))
4127
4128#define ppc_branch_hash_lookup(table, string, create, copy) \
4129 ((struct ppc_branch_hash_entry *) \
4130 bfd_hash_lookup ((table), (string), (create), (copy)))
4131
4132/* Create an entry in the stub hash table. */
4133
4134static struct bfd_hash_entry *
4ce794b7
AM
4135stub_hash_newfunc (struct bfd_hash_entry *entry,
4136 struct bfd_hash_table *table,
4137 const char *string)
721956f4
AM
4138{
4139 /* Allocate the structure if it has not already been allocated by a
4140 subclass. */
4141 if (entry == NULL)
4142 {
4143 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4144 if (entry == NULL)
4145 return entry;
4146 }
4147
4148 /* Call the allocation method of the superclass. */
4149 entry = bfd_hash_newfunc (entry, table, string);
4150 if (entry != NULL)
4151 {
4152 struct ppc_stub_hash_entry *eh;
4153
4154 /* Initialize the local fields. */
4155 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4156 eh->stub_type = ppc_stub_none;
6f20ed8a 4157 eh->group = NULL;
721956f4
AM
4158 eh->stub_offset = 0;
4159 eh->target_value = 0;
4160 eh->target_section = NULL;
721956f4 4161 eh->h = NULL;
6911b7dc 4162 eh->plt_ent = NULL;
6911b7dc 4163 eh->other = 0;
721956f4
AM
4164 }
4165
4166 return entry;
4167}
4168
4169/* Create an entry in the branch hash table. */
4170
4171static struct bfd_hash_entry *
4ce794b7
AM
4172branch_hash_newfunc (struct bfd_hash_entry *entry,
4173 struct bfd_hash_table *table,
4174 const char *string)
721956f4
AM
4175{
4176 /* Allocate the structure if it has not already been allocated by a
4177 subclass. */
4178 if (entry == NULL)
4179 {
4180 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4181 if (entry == NULL)
4182 return entry;
4183 }
4184
4185 /* Call the allocation method of the superclass. */
4186 entry = bfd_hash_newfunc (entry, table, string);
4187 if (entry != NULL)
4188 {
4189 struct ppc_branch_hash_entry *eh;
4190
4191 /* Initialize the local fields. */
4192 eh = (struct ppc_branch_hash_entry *) entry;
4193 eh->offset = 0;
4194 eh->iter = 0;
4195 }
4196
4197 return entry;
4198}
4199
65f38f15
AM
4200/* Create an entry in a ppc64 ELF linker hash table. */
4201
4202static struct bfd_hash_entry *
4ce794b7
AM
4203link_hash_newfunc (struct bfd_hash_entry *entry,
4204 struct bfd_hash_table *table,
4205 const char *string)
65f38f15
AM
4206{
4207 /* Allocate the structure if it has not already been allocated by a
4208 subclass. */
4209 if (entry == NULL)
4210 {
4211 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4212 if (entry == NULL)
4213 return entry;
4214 }
4215
4216 /* Call the allocation method of the superclass. */
4217 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4218 if (entry != NULL)
4219 {
4220 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4221
b3fac117 4222 memset (&eh->u.stub_cache, 0,
908b32fc 4223 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4224 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4225
4226 /* When making function calls, old ABI code references function entry
4227 points (dot symbols), while new ABI code references the function
4228 descriptor symbol. We need to make any combination of reference and
4229 definition work together, without breaking archive linking.
4230
4231 For a defined function "foo" and an undefined call to "bar":
4232 An old object defines "foo" and ".foo", references ".bar" (possibly
4233 "bar" too).
4234 A new object defines "foo" and references "bar".
4235
4236 A new object thus has no problem with its undefined symbols being
4237 satisfied by definitions in an old object. On the other hand, the
4238 old object won't have ".bar" satisfied by a new object.
4239
4240 Keep a list of newly added dot-symbols. */
4241
4242 if (string[0] == '.')
4243 {
4244 struct ppc_link_hash_table *htab;
4245
4246 htab = (struct ppc_link_hash_table *) table;
4247 eh->u.next_dot_sym = htab->dot_syms;
4248 htab->dot_syms = eh;
4249 }
65f38f15
AM
4250 }
4251
4252 return entry;
4253}
4254
3b421ab3
AM
4255struct tocsave_entry {
4256 asection *sec;
4257 bfd_vma offset;
4258};
4259
4260static hashval_t
4261tocsave_htab_hash (const void *p)
4262{
4263 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4264 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4265}
4266
4267static int
4268tocsave_htab_eq (const void *p1, const void *p2)
4269{
4270 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4271 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4272 return e1->sec == e2->sec && e1->offset == e2->offset;
4273}
4274
68faa637
AM
4275/* Destroy a ppc64 ELF linker hash table. */
4276
4277static void
d495ab0d 4278ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4279{
d495ab0d 4280 struct ppc_link_hash_table *htab;
68faa637 4281
d495ab0d 4282 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4283 if (htab->tocsave_htab)
4284 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4285 bfd_hash_table_free (&htab->branch_hash_table);
4286 bfd_hash_table_free (&htab->stub_hash_table);
4287 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4288}
4289
65f38f15
AM
4290/* Create a ppc64 ELF linker hash table. */
4291
4292static struct bfd_link_hash_table *
4ce794b7 4293ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4294{
4295 struct ppc_link_hash_table *htab;
4296 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4297
4ce794b7 4298 htab = bfd_zmalloc (amt);
65f38f15
AM
4299 if (htab == NULL)
4300 return NULL;
4301
66eb6687 4302 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4303 sizeof (struct ppc_link_hash_entry),
4304 PPC64_ELF_DATA))
65f38f15 4305 {
e2d34d7d 4306 free (htab);
65f38f15
AM
4307 return NULL;
4308 }
4309
721956f4 4310 /* Init the stub hash table too. */
66eb6687
AM
4311 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4312 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4313 {
d495ab0d 4314 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4315 return NULL;
4316 }
721956f4
AM
4317
4318 /* And the branch hash table. */
66eb6687
AM
4319 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4320 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4321 {
4322 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4323 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4324 return NULL;
4325 }
721956f4 4326
3b421ab3
AM
4327 htab->tocsave_htab = htab_try_create (1024,
4328 tocsave_htab_hash,
4329 tocsave_htab_eq,
4330 NULL);
4331 if (htab->tocsave_htab == NULL)
2915c55b 4332 {
d495ab0d 4333 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4334 return NULL;
4335 }
d495ab0d 4336 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4337
3254fd24
AM
4338 /* Initializing two fields of the union is just cosmetic. We really
4339 only care about glist, but when compiled on a 32-bit host the
4340 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4341 debugger inspection of these fields look nicer. */
a6aa5195
AM
4342 htab->elf.init_got_refcount.refcount = 0;
4343 htab->elf.init_got_refcount.glist = NULL;
4344 htab->elf.init_plt_refcount.refcount = 0;
4345 htab->elf.init_plt_refcount.glist = NULL;
4346 htab->elf.init_got_offset.offset = 0;
4347 htab->elf.init_got_offset.glist = NULL;
4348 htab->elf.init_plt_offset.offset = 0;
4349 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4350
65f38f15
AM
4351 return &htab->elf.root;
4352}
4353
bfeb4a28
AM
4354/* Create sections for linker generated code. */
4355
4356static bfd_boolean
4357create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4358{
4359 struct ppc_link_hash_table *htab;
4360 flagword flags;
4361
4362 htab = ppc_hash_table (info);
4363
bfeb4a28
AM
4364 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4365 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4366 if (htab->params->save_restore_funcs)
4367 {
4368 /* Create .sfpr for code to save and restore fp regs. */
4369 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4370 flags);
4371 if (htab->sfpr == NULL
4372 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4373 return FALSE;
4374 }
4375
4376 if (bfd_link_relocatable (info))
4377 return TRUE;
bfeb4a28
AM
4378
4379 /* Create .glink for lazy dynamic linking support. */
4380 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4381 flags);
4382 if (htab->glink == NULL
4383 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4384 return FALSE;
4385
4386 if (!info->no_ld_generated_unwind_info)
4387 {
4388 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4389 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4390 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4391 ".eh_frame",
4392 flags);
4393 if (htab->glink_eh_frame == NULL
4394 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4395 return FALSE;
4396 }
4397
4398 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4399 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4400 if (htab->elf.iplt == NULL
4401 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4402 return FALSE;
4403
4404 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4405 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4406 htab->elf.irelplt
4407 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4408 if (htab->elf.irelplt == NULL
4409 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4410 return FALSE;
4411
4412 /* Create branch lookup table for plt_branch stubs. */
4413 flags = (SEC_ALLOC | SEC_LOAD
4414 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4415 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4416 flags);
4417 if (htab->brlt == NULL
4418 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4419 return FALSE;
4420
0e1862bb 4421 if (!bfd_link_pic (info))
bfeb4a28
AM
4422 return TRUE;
4423
4424 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4425 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4426 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4427 ".rela.branch_lt",
4428 flags);
4429 if (htab->relbrlt == NULL
4430 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4431 return FALSE;
4432
4433 return TRUE;
4434}
4435
e717da7e
AM
4436/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4437
bfeb4a28 4438bfd_boolean
e7d1c40c
AM
4439ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4440 struct ppc64_elf_params *params)
e717da7e
AM
4441{
4442 struct ppc_link_hash_table *htab;
4443
e7d1c40c 4444 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4445
4446/* Always hook our dynamic sections into the first bfd, which is the
4447 linker created stub bfd. This ensures that the GOT header is at
4448 the start of the output TOC section. */
4449 htab = ppc_hash_table (info);
e7d1c40c
AM
4450 htab->elf.dynobj = params->stub_bfd;
4451 htab->params = params;
bfeb4a28 4452
bfeb4a28 4453 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4454}
4455
721956f4
AM
4456/* Build a name for an entry in the stub hash table. */
4457
4458static char *
4ce794b7
AM
4459ppc_stub_name (const asection *input_section,
4460 const asection *sym_sec,
4461 const struct ppc_link_hash_entry *h,
4462 const Elf_Internal_Rela *rel)
721956f4
AM
4463{
4464 char *stub_name;
bcaa2f82 4465 ssize_t len;
721956f4
AM
4466
4467 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4468 offsets from a sym as a branch target? In fact, we could
4469 probably assume the addend is always zero. */
4470 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4471
4472 if (h)
4473 {
4474 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4475 stub_name = bfd_malloc (len);
46de2a7c
AM
4476 if (stub_name == NULL)
4477 return stub_name;
4478
bcaa2f82
AM
4479 len = sprintf (stub_name, "%08x.%s+%x",
4480 input_section->id & 0xffffffff,
4481 h->elf.root.root.string,
4482 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4483 }
4484 else
4485 {
ad8e1ba5 4486 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4487 stub_name = bfd_malloc (len);
46de2a7c
AM
4488 if (stub_name == NULL)
4489 return stub_name;
4490
bcaa2f82
AM
4491 len = sprintf (stub_name, "%08x.%x:%x+%x",
4492 input_section->id & 0xffffffff,
4493 sym_sec->id & 0xffffffff,
4494 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4495 (int) rel->r_addend & 0xffffffff);
721956f4 4496 }
bcaa2f82 4497 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4498 stub_name[len - 2] = 0;
721956f4
AM
4499 return stub_name;
4500}
4501
4502/* Look up an entry in the stub hash. Stub entries are cached because
4503 creating the stub name takes a bit of time. */
4504
4505static struct ppc_stub_hash_entry *
4ce794b7
AM
4506ppc_get_stub_entry (const asection *input_section,
4507 const asection *sym_sec,
039b3fef 4508 struct ppc_link_hash_entry *h,
4ce794b7
AM
4509 const Elf_Internal_Rela *rel,
4510 struct ppc_link_hash_table *htab)
721956f4
AM
4511{
4512 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4513 struct map_stub *group;
721956f4
AM
4514
4515 /* If this input section is part of a group of sections sharing one
4516 stub section, then use the id of the first section in the group.
4517 Stub names need to include a section id, as there may well be
4518 more than one stub used to reach say, printf, and we need to
4519 distinguish between them. */
6f20ed8a 4520 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4521 if (group == NULL)
4522 return NULL;
721956f4 4523
b3fac117
AM
4524 if (h != NULL && h->u.stub_cache != NULL
4525 && h->u.stub_cache->h == h
6f20ed8a 4526 && h->u.stub_cache->group == group)
721956f4 4527 {
b3fac117 4528 stub_entry = h->u.stub_cache;
721956f4
AM
4529 }
4530 else
4531 {
4532 char *stub_name;
4533
6f20ed8a 4534 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4535 if (stub_name == NULL)
4536 return NULL;
4537
4538 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4539 stub_name, FALSE, FALSE);
721956f4 4540 if (h != NULL)
b3fac117 4541 h->u.stub_cache = stub_entry;
721956f4
AM
4542
4543 free (stub_name);
4544 }
4545
4546 return stub_entry;
4547}
4548
4549/* Add a new stub entry to the stub hash. Not all fields of the new
4550 stub entry are initialised. */
4551
4552static struct ppc_stub_hash_entry *
4ce794b7
AM
4553ppc_add_stub (const char *stub_name,
4554 asection *section,
25f53a85 4555 struct bfd_link_info *info)
721956f4 4556{
25f53a85 4557 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4558 struct map_stub *group;
721956f4
AM
4559 asection *link_sec;
4560 asection *stub_sec;
4561 struct ppc_stub_hash_entry *stub_entry;
4562
6f20ed8a
AM
4563 group = htab->sec_info[section->id].u.group;
4564 link_sec = group->link_sec;
4565 stub_sec = group->stub_sec;
721956f4
AM
4566 if (stub_sec == NULL)
4567 {
6f20ed8a
AM
4568 size_t namelen;
4569 bfd_size_type len;
4570 char *s_name;
721956f4 4571
6f20ed8a
AM
4572 namelen = strlen (link_sec->name);
4573 len = namelen + sizeof (STUB_SUFFIX);
4574 s_name = bfd_alloc (htab->params->stub_bfd, len);
4575 if (s_name == NULL)
4576 return NULL;
721956f4 4577
6f20ed8a
AM
4578 memcpy (s_name, link_sec->name, namelen);
4579 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4580 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4581 if (stub_sec == NULL)
4582 return NULL;
4583 group->stub_sec = stub_sec;
721956f4
AM
4584 }
4585
4586 /* Enter this entry into the linker stub hash table. */
4587 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4588 TRUE, FALSE);
721956f4
AM
4589 if (stub_entry == NULL)
4590 {
695344c0 4591 /* xgettext:c-format */
8de848d8 4592 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4593 section->owner, stub_name);
721956f4
AM
4594 return NULL;
4595 }
4596
6f20ed8a 4597 stub_entry->group = group;
721956f4 4598 stub_entry->stub_offset = 0;
721956f4
AM
4599 return stub_entry;
4600}
4601
e717da7e
AM
4602/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4603 not already done. */
65f38f15 4604
b34976b6 4605static bfd_boolean
e717da7e 4606create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4607{
e717da7e
AM
4608 asection *got, *relgot;
4609 flagword flags;
4610 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4611
0c8d6e5c 4612 if (!is_ppc64_elf (abfd))
0ffa91dd 4613 return FALSE;
4dfe6ac6
NC
4614 if (htab == NULL)
4615 return FALSE;
0ffa91dd 4616
33e44f2e
AM
4617 if (!htab->elf.sgot
4618 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4619 return FALSE;
e717da7e
AM
4620
4621 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4622 | SEC_LINKER_CREATED);
4623
c456f082 4624 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4625 if (!got
e717da7e
AM
4626 || !bfd_set_section_alignment (abfd, got, 3))
4627 return FALSE;
65f38f15 4628
c456f082
AM
4629 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4630 flags | SEC_READONLY);
e717da7e 4631 if (!relgot
e717da7e 4632 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4633 return FALSE;
e717da7e
AM
4634
4635 ppc64_elf_tdata (abfd)->got = got;
4636 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4637 return TRUE;
65f38f15 4638}
5bd4f169 4639
b31867b6
AM
4640/* Follow indirect and warning symbol links. */
4641
4642static inline struct bfd_link_hash_entry *
4643follow_link (struct bfd_link_hash_entry *h)
4644{
4645 while (h->type == bfd_link_hash_indirect
4646 || h->type == bfd_link_hash_warning)
4647 h = h->u.i.link;
4648 return h;
4649}
4650
4651static inline struct elf_link_hash_entry *
4652elf_follow_link (struct elf_link_hash_entry *h)
4653{
4654 return (struct elf_link_hash_entry *) follow_link (&h->root);
4655}
4656
4657static inline struct ppc_link_hash_entry *
4658ppc_follow_link (struct ppc_link_hash_entry *h)
4659{
4660 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4661}
4662
40d16e0b
AM
4663/* Merge PLT info on FROM with that on TO. */
4664
4665static void
4666move_plt_plist (struct ppc_link_hash_entry *from,
4667 struct ppc_link_hash_entry *to)
4668{
4669 if (from->elf.plt.plist != NULL)
4670 {
4671 if (to->elf.plt.plist != NULL)
4672 {
4673 struct plt_entry **entp;
4674 struct plt_entry *ent;
4675
4676 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4677 {
4678 struct plt_entry *dent;
4679
4680 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4681 if (dent->addend == ent->addend)
4682 {
4683 dent->plt.refcount += ent->plt.refcount;
4684 *entp = ent->next;
4685 break;
4686 }
4687 if (dent == NULL)
4688 entp = &ent->next;
4689 }
4690 *entp = to->elf.plt.plist;
4691 }
4692
4693 to->elf.plt.plist = from->elf.plt.plist;
4694 from->elf.plt.plist = NULL;
4695 }
4696}
4697
65f38f15
AM
4698/* Copy the extra info we tack onto an elf_link_hash_entry. */
4699
4700static void
fcfa13d2
AM
4701ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4702 struct elf_link_hash_entry *dir,
4703 struct elf_link_hash_entry *ind)
65f38f15
AM
4704{
4705 struct ppc_link_hash_entry *edir, *eind;
4706
4707 edir = (struct ppc_link_hash_entry *) dir;
4708 eind = (struct ppc_link_hash_entry *) ind;
4709
c79d6685
AM
4710 edir->is_func |= eind->is_func;
4711 edir->is_func_descriptor |= eind->is_func_descriptor;
4712 edir->tls_mask |= eind->tls_mask;
4713 if (eind->oh != NULL)
4714 edir->oh = ppc_follow_link (eind->oh);
4715
e81830c5
AM
4716 /* If called to transfer flags for a weakdef during processing
4717 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4718 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4719 if (!(ELIMINATE_COPY_RELOCS
4720 && eind->elf.root.type != bfd_link_hash_indirect
4721 && edir->elf.dynamic_adjusted))
4722 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4723
474436e6 4724 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4725 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4726 edir->elf.ref_regular |= eind->elf.ref_regular;
4727 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4728 edir->elf.needs_plt |= eind->elf.needs_plt;
4729 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4730
d311bc8b
AM
4731 /* If we were called to copy over info for a weak sym, don't copy
4732 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4733 in order to simplify readonly_dynrelocs and save a field in the
4734 symbol hash entry, but that means dyn_relocs can't be used in any
4735 tests about a specific symbol, or affect other symbol flags which
4736 are then tested.
4737 Chain weakdefs so we can get from the weakdef back to an alias.
4738 The list is circular so that we don't need to use u.weakdef as
4739 well as this list to look at all aliases. */
4740 if (eind->elf.root.type != bfd_link_hash_indirect)
4741 {
4742 struct ppc_link_hash_entry *cur, *add, *next;
4743
4744 add = eind;
4745 do
4746 {
4747 cur = edir->weakref;
4748 if (cur != NULL)
4749 {
4750 do
4751 {
4752 /* We can be called twice for the same symbols.
4753 Don't make multiple loops. */
4754 if (cur == add)
4755 return;
4756 cur = cur->weakref;
4757 } while (cur != edir);
4758 }
4759 next = add->weakref;
4760 if (cur != add)
4761 {
4762 add->weakref = edir->weakref != NULL ? edir->weakref : edir;
4763 edir->weakref = add;
4764 }
4765 add = next;
4766 } while (add != NULL && add != eind);
4767 return;
4768 }
4769
411e1bfb 4770 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4771 if (eind->dyn_relocs != NULL)
65f38f15 4772 {
bbd7ec4a
AM
4773 if (edir->dyn_relocs != NULL)
4774 {
6061a67d
AM
4775 struct elf_dyn_relocs **pp;
4776 struct elf_dyn_relocs *p;
bbd7ec4a 4777
fcfa13d2 4778 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4779 list. Merge any entries against the same section. */
4780 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4781 {
6061a67d 4782 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4783
4784 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4785 if (q->sec == p->sec)
4786 {
4787 q->pc_count += p->pc_count;
4788 q->count += p->count;
4789 *pp = p->next;
4790 break;
4791 }
4792 if (q == NULL)
4793 pp = &p->next;
4794 }
4795 *pp = edir->dyn_relocs;
4796 }
4797
65f38f15
AM
4798 edir->dyn_relocs = eind->dyn_relocs;
4799 eind->dyn_relocs = NULL;
4800 }
65f38f15 4801
81848ca0
AM
4802 /* Copy over got entries that we may have already seen to the
4803 symbol which just became indirect. */
411e1bfb
AM
4804 if (eind->elf.got.glist != NULL)
4805 {
4806 if (edir->elf.got.glist != NULL)
4807 {
4808 struct got_entry **entp;
4809 struct got_entry *ent;
4810
4811 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4812 {
4813 struct got_entry *dent;
4814
4815 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4816 if (dent->addend == ent->addend
e717da7e 4817 && dent->owner == ent->owner
411e1bfb
AM
4818 && dent->tls_type == ent->tls_type)
4819 {
4820 dent->got.refcount += ent->got.refcount;
4821 *entp = ent->next;
4822 break;
4823 }
4824 if (dent == NULL)
4825 entp = &ent->next;
4826 }
4827 *entp = edir->elf.got.glist;
4828 }
4829
4830 edir->elf.got.glist = eind->elf.got.glist;
4831 eind->elf.got.glist = NULL;
4832 }
4833
4834 /* And plt entries. */
40d16e0b 4835 move_plt_plist (eind, edir);
411e1bfb 4836
fcfa13d2 4837 if (eind->elf.dynindx != -1)
411e1bfb 4838 {
fcfa13d2
AM
4839 if (edir->elf.dynindx != -1)
4840 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4841 edir->elf.dynstr_index);
411e1bfb
AM
4842 edir->elf.dynindx = eind->elf.dynindx;
4843 edir->elf.dynstr_index = eind->elf.dynstr_index;
4844 eind->elf.dynindx = -1;
4845 eind->elf.dynstr_index = 0;
4846 }
411e1bfb
AM
4847}
4848
8387904d
AM
4849/* Find the function descriptor hash entry from the given function code
4850 hash entry FH. Link the entries via their OH fields. */
4851
4852static struct ppc_link_hash_entry *
b31867b6 4853lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4854{
4855 struct ppc_link_hash_entry *fdh = fh->oh;
4856
4857 if (fdh == NULL)
4858 {
4859 const char *fd_name = fh->elf.root.root.string + 1;
4860
4861 fdh = (struct ppc_link_hash_entry *)
4862 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4863 if (fdh == NULL)
4864 return fdh;
4865
4866 fdh->is_func_descriptor = 1;
4867 fdh->oh = fh;
4868 fh->is_func = 1;
4869 fh->oh = fdh;
8387904d
AM
4870 }
4871
8c5b4e52
AM
4872 fdh = ppc_follow_link (fdh);
4873 fdh->is_func_descriptor = 1;
4874 fdh->oh = fh;
4875 return fdh;
8387904d
AM
4876}
4877
8c5b4e52 4878/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4879
4880static struct ppc_link_hash_entry *
4881make_fdh (struct bfd_link_info *info,
908b32fc 4882 struct ppc_link_hash_entry *fh)
bb700d78 4883{
8c5b4e52
AM
4884 bfd *abfd = fh->elf.root.u.undef.abfd;
4885 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4886 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4887 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4888 ? BSF_WEAK
4889 : BSF_GLOBAL);
4890
4891 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4892 fh->elf.root.root.string + 1,
4893 flags, bfd_und_section_ptr, 0,
4894 NULL, FALSE, FALSE, &bh))
bb700d78
AM
4895 return NULL;
4896
4897 fdh = (struct ppc_link_hash_entry *) bh;
4898 fdh->elf.non_elf = 0;
908b32fc
AM
4899 fdh->fake = 1;
4900 fdh->is_func_descriptor = 1;
4901 fdh->oh = fh;
4902 fh->is_func = 1;
4903 fh->oh = fdh;
bb700d78
AM
4904 return fdh;
4905}
4906
8387904d
AM
4907/* Fix function descriptor symbols defined in .opd sections to be
4908 function type. */
555cd476
AM
4909
4910static bfd_boolean
c16153ae 4911ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4912 struct bfd_link_info *info,
555cd476 4913 Elf_Internal_Sym *isym,
6911b7dc 4914 const char **name,
555cd476
AM
4915 flagword *flags ATTRIBUTE_UNUSED,
4916 asection **sec,
b53dfeb2 4917 bfd_vma *value)
555cd476 4918{
a43942db 4919 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
f1885d1e
AM
4920 && (ibfd->flags & DYNAMIC) == 0
4921 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
a43942db 4922 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
f64b2e8d 4923
b53dfeb2 4924 if (*sec != NULL
f1885d1e 4925 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4926 {
4927 asection *code_sec;
4928
4929 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4930 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4931 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4932
4933 /* If the symbol is a function defined in .opd, and the function
4934 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4935 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4936 && (*sec)->reloc_count != 0
4937 && opd_entry_value (*sec, *value, &code_sec, NULL,
4938 FALSE) != (bfd_vma) -1
4939 && discarded_section (code_sec))
4940 {
4941 *sec = bfd_und_section_ptr;
4942 isym->st_shndx = SHN_UNDEF;
4943 }
4944 }
dbd1e97e
AM
4945 else if (*sec != NULL
4946 && strcmp ((*sec)->name, ".toc") == 0
4947 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4948 {
4949 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4950 if (htab != NULL)
4951 htab->params->object_in_toc = 1;
4952 }
433817dd 4953
6911b7dc
AM
4954 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4955 {
4956 if (abiversion (ibfd) == 0)
4957 set_abiversion (ibfd, 2);
4958 else if (abiversion (ibfd) == 1)
4959 {
4960 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4961 " for ABI version 1\n"), name);
4962 bfd_set_error (bfd_error_bad_value);
4963 return FALSE;
4964 }
4965 }
4966
555cd476
AM
4967 return TRUE;
4968}
4969
6911b7dc
AM
4970/* Merge non-visibility st_other attributes: local entry point. */
4971
4972static void
4973ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4974 const Elf_Internal_Sym *isym,
4975 bfd_boolean definition,
4976 bfd_boolean dynamic)
4977{
4978 if (definition && !dynamic)
4979 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4980 | ELF_ST_VISIBILITY (h->other));
4981}
4982
8c5b4e52
AM
4983/* Hook called on merging a symbol. We use this to clear "fake" since
4984 we now have a real symbol. */
4985
4986static bfd_boolean
4987ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4988 const Elf_Internal_Sym *isym ATTRIBUTE_UNUSED,
4989 asection **psec ATTRIBUTE_UNUSED,
4990 bfd_boolean newdef ATTRIBUTE_UNUSED,
4991 bfd_boolean olddef ATTRIBUTE_UNUSED,
4992 bfd *oldbfd ATTRIBUTE_UNUSED,
4993 const asection *oldsec ATTRIBUTE_UNUSED)
4994{
4995 ((struct ppc_link_hash_entry *) h)->fake = 0;
4996 return TRUE;
4997}
4998
8387904d 4999/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5000 inclusion of a new ABI object archive that defines "bar".
5001 NAME is a symbol defined in an archive. Return a symbol in the hash
5002 table that might be satisfied by the archive symbols. */
8387904d
AM
5003
5004static struct elf_link_hash_entry *
5005ppc64_elf_archive_symbol_lookup (bfd *abfd,
5006 struct bfd_link_info *info,
5007 const char *name)
5008{
5009 struct elf_link_hash_entry *h;
5010 char *dot_name;
5011 size_t len;
5012
5013 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5014 if (h != NULL
5015 /* Don't return this sym if it is a fake function descriptor
5016 created by add_symbol_adjust. */
8c5b4e52 5017 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
5018 return h;
5019
5020 if (name[0] == '.')
5021 return h;
5022
5023 len = strlen (name);
5024 dot_name = bfd_alloc (abfd, len + 2);
5025 if (dot_name == NULL)
5026 return (struct elf_link_hash_entry *) 0 - 1;
5027 dot_name[0] = '.';
5028 memcpy (dot_name + 1, name, len + 1);
5029 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5030 bfd_release (abfd, dot_name);
5031 return h;
5032}
5033
5034/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5035 new ABI object defines "bar". Well, at least, undefined dot symbols
5036 are made weak. This stops later archive searches from including an
5037 object if we already have a function descriptor definition. It also
35b0ce59
AM
5038 prevents the linker complaining about undefined symbols.
5039 We also check and correct mismatched symbol visibility here. The
5040 most restrictive visibility of the function descriptor and the
5041 function entry symbol is used. */
8387904d
AM
5042
5043static bfd_boolean
b3fac117 5044add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5045{
8387904d
AM
5046 struct ppc_link_hash_table *htab;
5047 struct ppc_link_hash_entry *fdh;
5048
b3fac117
AM
5049 if (eh->elf.root.type == bfd_link_hash_warning)
5050 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5051
8400d40d
AM
5052 if (eh->elf.root.type == bfd_link_hash_indirect)
5053 return TRUE;
5054
b3fac117
AM
5055 if (eh->elf.root.root.string[0] != '.')
5056 abort ();
8387904d 5057
b3fac117 5058 htab = ppc_hash_table (info);
4dfe6ac6
NC
5059 if (htab == NULL)
5060 return FALSE;
5061
b31867b6 5062 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
5063 if (fdh == NULL
5064 && !bfd_link_relocatable (info)
5065 && (eh->elf.root.type == bfd_link_hash_undefined
5066 || eh->elf.root.type == bfd_link_hash_undefweak)
5067 && eh->elf.ref_regular)
5068 {
5069 /* Make an undefined function descriptor sym, in order to
5070 pull in an --as-needed shared lib. Archives are handled
5071 elsewhere. */
5072 fdh = make_fdh (info, eh);
5073 if (fdh == NULL)
5074 return FALSE;
bb700d78 5075 }
8c5b4e52
AM
5076
5077 if (fdh != NULL)
8387904d 5078 {
35b0ce59
AM
5079 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5080 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
5081
5082 /* Make both descriptor and entry symbol have the most
5083 constraining visibility of either symbol. */
35b0ce59
AM
5084 if (entry_vis < descr_vis)
5085 fdh->elf.other += entry_vis - descr_vis;
5086 else if (entry_vis > descr_vis)
5087 eh->elf.other += descr_vis - entry_vis;
5088
8c5b4e52
AM
5089 /* Propagate reference flags from entry symbol to function
5090 descriptor symbol. */
5091 fdh->elf.root.non_ir_ref |= eh->elf.root.non_ir_ref;
5092 fdh->elf.ref_regular |= eh->elf.ref_regular;
5093 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5094
5095 if (!fdh->elf.forced_local
5096 && fdh->elf.dynindx == -1
5097 && fdh->elf.versioned != versioned_hidden
5098 && (bfd_link_dll (info)
5099 || fdh->elf.def_dynamic
5100 || fdh->elf.ref_dynamic)
5101 && (eh->elf.ref_regular
5102 || eh->elf.def_regular))
5103 {
5104 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5105 return FALSE;
35b0ce59 5106 }
8387904d 5107 }
99877b66 5108
8387904d
AM
5109 return TRUE;
5110}
5111
f6c7c3e8
AM
5112/* Set up opd section info and abiversion for IBFD, and process list
5113 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5114
8387904d 5115static bfd_boolean
f6c7c3e8 5116ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5117{
99877b66 5118 struct ppc_link_hash_table *htab;
b3fac117 5119 struct ppc_link_hash_entry **p, *eh;
459609d6 5120 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5121
459609d6 5122 if (opd != NULL && opd->size != 0)
b3fac117 5123 {
459609d6
AM
5124 if (abiversion (ibfd) == 0)
5125 set_abiversion (ibfd, 1);
8a2058b5 5126 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5127 {
695344c0 5128 /* xgettext:c-format */
459609d6
AM
5129 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5130 " version %d\n"),
5131 ibfd, abiversion (ibfd));
5132 bfd_set_error (bfd_error_bad_value);
5133 return FALSE;
f6c7c3e8
AM
5134 }
5135
459609d6
AM
5136 if ((ibfd->flags & DYNAMIC) == 0
5137 && (opd->flags & SEC_RELOC) != 0
5138 && opd->reloc_count != 0
5139 && !bfd_is_abs_section (opd->output_section))
b3fac117 5140 {
459609d6
AM
5141 /* Garbage collection needs some extra help with .opd sections.
5142 We don't want to necessarily keep everything referenced by
5143 relocs in .opd, as that would keep all functions. Instead,
5144 if we reference an .opd symbol (a function descriptor), we
5145 want to keep the function code symbol's section. This is
5146 easy for global symbols, but for local syms we need to keep
5147 information about the associated function section. */
5148 bfd_size_type amt;
5149 asection **opd_sym_map;
5150
51aecdc5 5151 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
459609d6
AM
5152 opd_sym_map = bfd_zalloc (ibfd, amt);
5153 if (opd_sym_map == NULL)
b3fac117 5154 return FALSE;
459609d6
AM
5155 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5156 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5157 ppc64_elf_section_data (opd)->sec_type = sec_opd;
b3fac117
AM
5158 }
5159 }
5160
459609d6
AM
5161 if (!is_ppc64_elf (info->output_bfd))
5162 return TRUE;
5163 htab = ppc_hash_table (info);
5164 if (htab == NULL)
5165 return FALSE;
5166
5167 /* For input files without an explicit abiversion in e_flags
5168 we should have flagged any with symbol st_other bits set
5169 as ELFv1 and above flagged those with .opd as ELFv2.
5170 Set the output abiversion if not yet set, and for any input
5171 still ambiguous, take its abiversion from the output.
5172 Differences in ABI are reported later. */
5173 if (abiversion (info->output_bfd) == 0)
5174 set_abiversion (info->output_bfd, abiversion (ibfd));
5175 else if (abiversion (ibfd) == 0)
5176 set_abiversion (ibfd, abiversion (info->output_bfd));
5177
5178 p = &htab->dot_syms;
5179 while ((eh = *p) != NULL)
5180 {
5181 *p = NULL;
5182 if (&eh->elf == htab->elf.hgot)
5183 ;
5184 else if (htab->elf.hgot == NULL
5185 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5186 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
5187 else if (abiversion (ibfd) <= 1)
5188 {
5189 htab->need_func_desc_adj = 1;
5190 if (!add_symbol_adjust (eh, info))
5191 return FALSE;
5192 }
459609d6
AM
5193 p = &eh->u.next_dot_sym;
5194 }
b3fac117 5195 return TRUE;
8387904d
AM
5196}
5197
97fed1c9
JJ
5198/* Undo hash table changes when an --as-needed input file is determined
5199 not to be needed. */
5200
5201static bfd_boolean
e5034e59
AM
5202ppc64_elf_notice_as_needed (bfd *ibfd,
5203 struct bfd_link_info *info,
5204 enum notice_asneeded_action act)
97fed1c9 5205{
e5034e59
AM
5206 if (act == notice_not_needed)
5207 {
5208 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5209
e5034e59
AM
5210 if (htab == NULL)
5211 return FALSE;
4dfe6ac6 5212
e5034e59
AM
5213 htab->dot_syms = NULL;
5214 }
5215 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5216}
5217
aa374f67
AM
5218/* If --just-symbols against a final linked binary, then assume we need
5219 toc adjusting stubs when calling functions defined there. */
5220
5221static void
5222ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5223{
5224 if ((sec->flags & SEC_CODE) != 0
5225 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5226 && is_ppc64_elf (sec->owner))
5227 {
2c3f079f
AM
5228 if (abiversion (sec->owner) >= 2
5229 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5230 sec->has_toc_reloc = 1;
5231 }
5232 _bfd_elf_link_just_syms (sec, info);
5233}
5234
e054468f 5235static struct plt_entry **
4ce794b7
AM
5236update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5237 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5238{
5239 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5240 struct plt_entry **local_plt;
f961d9dd 5241 unsigned char *local_got_tls_masks;
411e1bfb
AM
5242
5243 if (local_got_ents == NULL)
5244 {
5245 bfd_size_type size = symtab_hdr->sh_info;
5246
e054468f
AM
5247 size *= (sizeof (*local_got_ents)
5248 + sizeof (*local_plt)
5249 + sizeof (*local_got_tls_masks));
4ce794b7 5250 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5251 if (local_got_ents == NULL)
e054468f 5252 return NULL;
411e1bfb
AM
5253 elf_local_got_ents (abfd) = local_got_ents;
5254 }
5255
e054468f 5256 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5257 {
5258 struct got_entry *ent;
5259
5260 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5261 if (ent->addend == r_addend
5262 && ent->owner == abfd
5263 && ent->tls_type == tls_type)
411e1bfb
AM
5264 break;
5265 if (ent == NULL)
5266 {
5267 bfd_size_type amt = sizeof (*ent);
4ce794b7 5268 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5269 if (ent == NULL)
5270 return FALSE;
5271 ent->next = local_got_ents[r_symndx];
5272 ent->addend = r_addend;
e717da7e 5273 ent->owner = abfd;
411e1bfb 5274 ent->tls_type = tls_type;
927be08e 5275 ent->is_indirect = FALSE;
411e1bfb
AM
5276 ent->got.refcount = 0;
5277 local_got_ents[r_symndx] = ent;
5278 }
5279 ent->got.refcount += 1;
5280 }
5281
e054468f 5282 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5283 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5284 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5285
5286 return local_plt + r_symndx;
65f38f15
AM
5287}
5288
411e1bfb 5289static bfd_boolean
e054468f 5290update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5291{
411e1bfb 5292 struct plt_entry *ent;
1e2f5b6e 5293
e054468f 5294 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5295 if (ent->addend == addend)
5296 break;
5297 if (ent == NULL)
1e2f5b6e 5298 {
411e1bfb 5299 bfd_size_type amt = sizeof (*ent);
4ce794b7 5300 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5301 if (ent == NULL)
5302 return FALSE;
e054468f 5303 ent->next = *plist;
411e1bfb
AM
5304 ent->addend = addend;
5305 ent->plt.refcount = 0;
e054468f 5306 *plist = ent;
1e2f5b6e 5307 }
411e1bfb 5308 ent->plt.refcount += 1;
b34976b6 5309 return TRUE;
1e2f5b6e
AM
5310}
5311
e054468f
AM
5312static bfd_boolean
5313is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5314{
5315 return (r_type == R_PPC64_REL24
5316 || r_type == R_PPC64_REL14
5317 || r_type == R_PPC64_REL14_BRTAKEN
5318 || r_type == R_PPC64_REL14_BRNTAKEN
5319 || r_type == R_PPC64_ADDR24
5320 || r_type == R_PPC64_ADDR14
5321 || r_type == R_PPC64_ADDR14_BRTAKEN
5322 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5323}
5324
5bd4f169 5325/* Look through the relocs for a section during the first phase, and
65f38f15 5326 calculate needed space in the global offset table, procedure
5d1634d7 5327 linkage table, and dynamic reloc sections. */
5bd4f169 5328
b34976b6 5329static bfd_boolean
4ce794b7
AM
5330ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5331 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5332{
65f38f15 5333 struct ppc_link_hash_table *htab;
5bd4f169 5334 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5335 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5336 const Elf_Internal_Rela *rel;
5337 const Elf_Internal_Rela *rel_end;
5bd4f169 5338 asection *sreloc;
1e2f5b6e 5339 asection **opd_sym_map;
3a71aa26 5340 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 5341
0e1862bb 5342 if (bfd_link_relocatable (info))
b34976b6 5343 return TRUE;
5bd4f169 5344
680a3378
AM
5345 /* Don't do anything special with non-loaded, non-alloced sections.
5346 In particular, any relocs in such sections should not affect GOT
5347 and PLT reference counting (ie. we don't allow them to create GOT
5348 or PLT entries), there's no possibility or desire to optimize TLS
5349 relocs, and there's not much point in propagating relocs to shared
5350 libs that the dynamic linker won't relocate. */
5351 if ((sec->flags & SEC_ALLOC) == 0)
5352 return TRUE;
5353
0c8d6e5c 5354 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5355
65f38f15 5356 htab = ppc_hash_table (info);
4dfe6ac6
NC
5357 if (htab == NULL)
5358 return FALSE;
5359
3a71aa26
AM
5360 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5361 FALSE, FALSE, TRUE);
5362 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5363 FALSE, FALSE, TRUE);
0ffa91dd 5364 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5365 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5366 sreloc = NULL;
1e2f5b6e 5367 opd_sym_map = NULL;
f6c7c3e8
AM
5368 if (ppc64_elf_section_data (sec) != NULL
5369 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5370 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5bd4f169
AM
5371
5372 rel_end = relocs + sec->reloc_count;
5373 for (rel = relocs; rel < rel_end; rel++)
5374 {
5375 unsigned long r_symndx;
5376 struct elf_link_hash_entry *h;
04c9666a 5377 enum elf_ppc64_reloc_type r_type;
727fc41e 5378 int tls_type;
7c8fe5c4 5379 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5380 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5381
5382 r_symndx = ELF64_R_SYM (rel->r_info);
5383 if (r_symndx < symtab_hdr->sh_info)
5384 h = NULL;
5385 else
973a3492 5386 {
8c5b4e52
AM
5387 struct ppc_link_hash_entry *eh;
5388
973a3492 5389 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5390 h = elf_follow_link (h);
8c5b4e52 5391 eh = (struct ppc_link_hash_entry *) h;
81fbe831
AM
5392
5393 /* PR15323, ref flags aren't set for references in the same
5394 object. */
5395 h->root.non_ir_ref = 1;
8c5b4e52
AM
5396 if (eh->is_func && eh->oh != NULL)
5397 eh->oh->elf.root.non_ir_ref = 1;
1c865ab2
AM
5398
5399 if (h == htab->elf.hgot)
5400 sec->has_toc_reloc = 1;
973a3492 5401 }
5bd4f169 5402
727fc41e 5403 tls_type = 0;
e054468f 5404 ifunc = NULL;
25f23106
AM
5405 if (h != NULL)
5406 {
5407 if (h->type == STT_GNU_IFUNC)
5408 {
5409 h->needs_plt = 1;
5410 ifunc = &h->plt.plist;
5411 }
5412 }
5413 else
5414 {
5415 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5416 abfd, r_symndx);
5417 if (isym == NULL)
5418 return FALSE;
5419
5420 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5421 {
5422 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5423 rel->r_addend, PLT_IFUNC);
5424 if (ifunc == NULL)
5425 return FALSE;
5426 }
5427 }
727fc41e 5428
cbf95972 5429 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5430 switch (r_type)
5bd4f169 5431 {
727fc41e
AM
5432 case R_PPC64_TLSGD:
5433 case R_PPC64_TLSLD:
5434 /* These special tls relocs tie a call to __tls_get_addr with
5435 its parameter symbol. */
5436 break;
5437
411e1bfb
AM
5438 case R_PPC64_GOT_TLSLD16:
5439 case R_PPC64_GOT_TLSLD16_LO:
5440 case R_PPC64_GOT_TLSLD16_HI:
5441 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5442 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5443 goto dogottls;
5444
5445 case R_PPC64_GOT_TLSGD16:
5446 case R_PPC64_GOT_TLSGD16_LO:
5447 case R_PPC64_GOT_TLSGD16_HI:
5448 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5449 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5450 goto dogottls;
5451
5452 case R_PPC64_GOT_TPREL16_DS:
5453 case R_PPC64_GOT_TPREL16_LO_DS:
5454 case R_PPC64_GOT_TPREL16_HI:
5455 case R_PPC64_GOT_TPREL16_HA:
0e1862bb 5456 if (bfd_link_pic (info))
411e1bfb
AM
5457 info->flags |= DF_STATIC_TLS;
5458 tls_type = TLS_TLS | TLS_TPREL;
5459 goto dogottls;
5460
5461 case R_PPC64_GOT_DTPREL16_DS:
5462 case R_PPC64_GOT_DTPREL16_LO_DS:
5463 case R_PPC64_GOT_DTPREL16_HI:
5464 case R_PPC64_GOT_DTPREL16_HA:
5465 tls_type = TLS_TLS | TLS_DTPREL;
5466 dogottls:
5467 sec->has_tls_reloc = 1;
1a0670f3 5468 /* Fall through */
411e1bfb 5469
5bd4f169 5470 case R_PPC64_GOT16:
5bd4f169 5471 case R_PPC64_GOT16_DS:
65f38f15
AM
5472 case R_PPC64_GOT16_HA:
5473 case R_PPC64_GOT16_HI:
5474 case R_PPC64_GOT16_LO:
5bd4f169 5475 case R_PPC64_GOT16_LO_DS:
65f38f15 5476 /* This symbol requires a global offset table entry. */
4c52953f 5477 sec->has_toc_reloc = 1;
33c0ec9d
AM
5478 if (r_type == R_PPC64_GOT_TLSLD16
5479 || r_type == R_PPC64_GOT_TLSGD16
5480 || r_type == R_PPC64_GOT_TPREL16_DS
5481 || r_type == R_PPC64_GOT_DTPREL16_DS
5482 || r_type == R_PPC64_GOT16
5483 || r_type == R_PPC64_GOT16_DS)
5484 {
5485 htab->do_multi_toc = 1;
d77c8a4b 5486 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5487 }
5488
e717da7e
AM
5489 if (ppc64_elf_tdata (abfd)->got == NULL
5490 && !create_got_section (abfd, info))
b34976b6 5491 return FALSE;
5bd4f169
AM
5492
5493 if (h != NULL)
5494 {
411e1bfb
AM
5495 struct ppc_link_hash_entry *eh;
5496 struct got_entry *ent;
65f38f15 5497
411e1bfb
AM
5498 eh = (struct ppc_link_hash_entry *) h;
5499 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5500 if (ent->addend == rel->r_addend
e717da7e 5501 && ent->owner == abfd
411e1bfb
AM
5502 && ent->tls_type == tls_type)
5503 break;
5504 if (ent == NULL)
5bd4f169 5505 {
411e1bfb 5506 bfd_size_type amt = sizeof (*ent);
4ce794b7 5507 ent = bfd_alloc (abfd, amt);
411e1bfb 5508 if (ent == NULL)
b34976b6 5509 return FALSE;
411e1bfb
AM
5510 ent->next = eh->elf.got.glist;
5511 ent->addend = rel->r_addend;
e717da7e 5512 ent->owner = abfd;
411e1bfb 5513 ent->tls_type = tls_type;
927be08e 5514 ent->is_indirect = FALSE;
411e1bfb
AM
5515 ent->got.refcount = 0;
5516 eh->elf.got.glist = ent;
5bd4f169 5517 }
411e1bfb 5518 ent->got.refcount += 1;
e7b938ca 5519 eh->tls_mask |= tls_type;
5bd4f169 5520 }
411e1bfb
AM
5521 else
5522 /* This is a global offset table entry for a local symbol. */
5523 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5524 rel->r_addend, tls_type))
5525 return FALSE;
a345bc8d
AM
5526
5527 /* We may also need a plt entry if the symbol turns out to be
5528 an ifunc. */
0e1862bb 5529 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5530 {
5531 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5532 return FALSE;
5533 }
5bd4f169
AM
5534 break;
5535
5bd4f169 5536 case R_PPC64_PLT16_HA:
65f38f15
AM
5537 case R_PPC64_PLT16_HI:
5538 case R_PPC64_PLT16_LO:
5539 case R_PPC64_PLT32:
5540 case R_PPC64_PLT64:
cbf95972
AM
5541 /* This symbol requires a procedure linkage table entry. */
5542 plt_list = ifunc;
5543 if (h != NULL)
e054468f 5544 {
e054468f
AM
5545 h->needs_plt = 1;
5546 if (h->root.root.string[0] == '.'
5547 && h->root.root.string[1] != '\0')
5548 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972
AM
5549 plt_list = &h->plt.plist;
5550 }
5551 if (plt_list == NULL)
5552 {
5553 /* It does not make sense to have a procedure linkage
5554 table entry for a non-ifunc local symbol. */
5555 info->callbacks->einfo
695344c0 5556 /* xgettext:c-format */
174d0a74 5557 (_("%H: %s reloc against local symbol\n"),
cbf95972
AM
5558 abfd, sec, rel->r_offset,
5559 ppc64_elf_howto_table[r_type]->name);
5560 bfd_set_error (bfd_error_bad_value);
5561 return FALSE;
e054468f 5562 }
cbf95972
AM
5563 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5564 return FALSE;
5bd4f169
AM
5565 break;
5566
5567 /* The following relocations don't need to propagate the
5568 relocation if linking a shared object since they are
5569 section relative. */
5570 case R_PPC64_SECTOFF:
5571 case R_PPC64_SECTOFF_LO:
5572 case R_PPC64_SECTOFF_HI:
5573 case R_PPC64_SECTOFF_HA:
5574 case R_PPC64_SECTOFF_DS:
5575 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5576 case R_PPC64_DTPREL16:
5577 case R_PPC64_DTPREL16_LO:
5578 case R_PPC64_DTPREL16_HI:
5579 case R_PPC64_DTPREL16_HA:
5580 case R_PPC64_DTPREL16_DS:
5581 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5582 case R_PPC64_DTPREL16_HIGH:
5583 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5584 case R_PPC64_DTPREL16_HIGHER:
5585 case R_PPC64_DTPREL16_HIGHERA:
5586 case R_PPC64_DTPREL16_HIGHEST:
5587 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5588 break;
5589
ad8e1ba5 5590 /* Nor do these. */
25f23106
AM
5591 case R_PPC64_REL16:
5592 case R_PPC64_REL16_LO:
5593 case R_PPC64_REL16_HI:
5594 case R_PPC64_REL16_HA:
a680de9a 5595 case R_PPC64_REL16DX_HA:
25f23106
AM
5596 break;
5597
45965137
AM
5598 /* Not supported as a dynamic relocation. */
5599 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5600 if (bfd_link_pic (info))
45965137
AM
5601 {
5602 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5603 ppc_howto_init ();
695344c0 5604 /* xgettext:c-format */
174d0a74 5605 info->callbacks->einfo (_("%H: %s reloc unsupported "
45965137
AM
5606 "in shared libraries and PIEs.\n"),
5607 abfd, sec, rel->r_offset,
5608 ppc64_elf_howto_table[r_type]->name);
5609 bfd_set_error (bfd_error_bad_value);
5610 return FALSE;
5611 }
5612 break;
5613
ad8e1ba5 5614 case R_PPC64_TOC16:
33c0ec9d
AM
5615 case R_PPC64_TOC16_DS:
5616 htab->do_multi_toc = 1;
d77c8a4b 5617 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5618 /* Fall through. */
ad8e1ba5
AM
5619 case R_PPC64_TOC16_LO:
5620 case R_PPC64_TOC16_HI:
5621 case R_PPC64_TOC16_HA:
ad8e1ba5 5622 case R_PPC64_TOC16_LO_DS:
4c52953f 5623 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5624 break;
5625
006589cf
AM
5626 /* Marker reloc. */
5627 case R_PPC64_ENTRY:
5628 break;
5629
5bd4f169
AM
5630 /* This relocation describes the C++ object vtable hierarchy.
5631 Reconstruct it for later use during GC. */
5632 case R_PPC64_GNU_VTINHERIT:
c152c796 5633 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5634 return FALSE;
5bd4f169
AM
5635 break;
5636
5637 /* This relocation describes which C++ vtable entries are actually
5638 used. Record for later use during GC. */
5639 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5640 BFD_ASSERT (h != NULL);
5641 if (h != NULL
5642 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5643 return FALSE;
5bd4f169
AM
5644 break;
5645
721956f4
AM
5646 case R_PPC64_REL14:
5647 case R_PPC64_REL14_BRTAKEN:
5648 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5649 {
5650 asection *dest = NULL;
5651
5652 /* Heuristic: If jumping outside our section, chances are
5653 we are going to need a stub. */
5654 if (h != NULL)
5655 {
5656 /* If the sym is weak it may be overridden later, so
5657 don't assume we know where a weak sym lives. */
5658 if (h->root.type == bfd_link_hash_defined)
5659 dest = h->root.u.def.section;
5660 }
5661 else
87d72d41
AM
5662 {
5663 Elf_Internal_Sym *isym;
5664
5665 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5666 abfd, r_symndx);
5667 if (isym == NULL)
5668 return FALSE;
5669
5670 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5671 }
5672
220c76dd 5673 if (dest != sec)
7c8fe5c4 5674 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5675 }
721956f4
AM
5676 /* Fall through. */
5677
5d1634d7 5678 case R_PPC64_REL24:
cbf95972
AM
5679 plt_list = ifunc;
5680 if (h != NULL)
5d1634d7 5681 {
e054468f
AM
5682 h->needs_plt = 1;
5683 if (h->root.root.string[0] == '.'
5684 && h->root.root.string[1] != '\0')
5685 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5686
3a71aa26 5687 if (h == tga || h == dottga)
cbf95972
AM
5688 {
5689 sec->has_tls_reloc = 1;
5690 if (rel != relocs
5691 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5692 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5693 /* We have a new-style __tls_get_addr call with
5694 a marker reloc. */
5695 ;
5696 else
5697 /* Mark this section as having an old-style call. */
5698 sec->has_tls_get_addr_call = 1;
5699 }
5700 plt_list = &h->plt.plist;
411e1bfb 5701 }
cbf95972
AM
5702
5703 /* We may need a .plt entry if the function this reloc
5704 refers to is in a shared lib. */
5705 if (plt_list
5706 && !update_plt_info (abfd, plt_list, rel->r_addend))
5707 return FALSE;
411e1bfb
AM
5708 break;
5709
cbf95972
AM
5710 case R_PPC64_ADDR14:
5711 case R_PPC64_ADDR14_BRNTAKEN:
5712 case R_PPC64_ADDR14_BRTAKEN:
5713 case R_PPC64_ADDR24:
5714 goto dodyn;
5715
411e1bfb
AM
5716 case R_PPC64_TPREL64:
5717 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
0e1862bb 5718 if (bfd_link_pic (info))
411e1bfb
AM
5719 info->flags |= DF_STATIC_TLS;
5720 goto dotlstoc;
5721
5722 case R_PPC64_DTPMOD64:
5723 if (rel + 1 < rel_end
5724 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5725 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5726 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5727 else
951fd09b 5728 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5729 goto dotlstoc;
5730
5731 case R_PPC64_DTPREL64:
5732 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5733 if (rel != relocs
5734 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5735 && rel[-1].r_offset == rel->r_offset - 8)
5736 /* This is the second reloc of a dtpmod, dtprel pair.
5737 Don't mark with TLS_DTPREL. */
5738 goto dodyn;
5739
5740 dotlstoc:
5741 sec->has_tls_reloc = 1;
5742 if (h != NULL)
5743 {
5744 struct ppc_link_hash_entry *eh;
5745 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5746 eh->tls_mask |= tls_type;
411e1bfb
AM
5747 }
5748 else
5749 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5750 rel->r_addend, tls_type))
5751 return FALSE;
5752
7c8fe5c4
AM
5753 ppc64_sec = ppc64_elf_section_data (sec);
5754 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5755 {
3a71aa26
AM
5756 bfd_size_type amt;
5757
e7b938ca 5758 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5759 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5760 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5761 if (ppc64_sec->u.toc.symndx == NULL)
5762 return FALSE;
5763 amt = sec->size * sizeof (bfd_vma) / 8;
5764 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5765 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5766 return FALSE;
7c8fe5c4
AM
5767 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5768 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5769 }
5770 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5771 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5772 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5773
5774 /* Mark the second slot of a GD or LD entry.
5775 -1 to indicate GD and -2 to indicate LD. */
5776 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5777 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5778 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5779 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5780 goto dodyn;
5781
5782 case R_PPC64_TPREL16:
5783 case R_PPC64_TPREL16_LO:
5784 case R_PPC64_TPREL16_HI:
5785 case R_PPC64_TPREL16_HA:
5786 case R_PPC64_TPREL16_DS:
5787 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5788 case R_PPC64_TPREL16_HIGH:
5789 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5790 case R_PPC64_TPREL16_HIGHER:
5791 case R_PPC64_TPREL16_HIGHERA:
5792 case R_PPC64_TPREL16_HIGHEST:
5793 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 5794 if (bfd_link_pic (info))
411e1bfb 5795 {
afb93314 5796 info->flags |= DF_STATIC_TLS;
411e1bfb 5797 goto dodyn;
5d1634d7
AM
5798 }
5799 break;
5800
e86ce104 5801 case R_PPC64_ADDR64:
1e2f5b6e 5802 if (opd_sym_map != NULL
1e2f5b6e 5803 && rel + 1 < rel_end
4ce794b7 5804 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5805 {
8387904d 5806 if (h != NULL)
8c5b4e52 5807 ((struct ppc_link_hash_entry *) h)->is_func = 1;
8387904d
AM
5808 else
5809 {
5810 asection *s;
87d72d41 5811 Elf_Internal_Sym *isym;
1e2f5b6e 5812
87d72d41
AM
5813 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5814 abfd, r_symndx);
5815 if (isym == NULL)
8387904d 5816 return FALSE;
87d72d41
AM
5817
5818 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5819 if (s != NULL && s != sec)
51aecdc5 5820 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
8387904d 5821 }
1e2f5b6e 5822 }
e86ce104
AM
5823 /* Fall through. */
5824
65f38f15
AM
5825 case R_PPC64_ADDR16:
5826 case R_PPC64_ADDR16_DS:
5827 case R_PPC64_ADDR16_HA:
5828 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5829 case R_PPC64_ADDR16_HIGH:
5830 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5831 case R_PPC64_ADDR16_HIGHER:
5832 case R_PPC64_ADDR16_HIGHERA:
5833 case R_PPC64_ADDR16_HIGHEST:
5834 case R_PPC64_ADDR16_HIGHESTA:
5835 case R_PPC64_ADDR16_LO:
5836 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5837 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5838 && rel->r_addend == 0)
5839 {
5840 /* We may need a .plt entry if this reloc refers to a
5841 function in a shared lib. */
5842 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5843 return FALSE;
5844 h->pointer_equality_needed = 1;
5845 }
5846 /* Fall through. */
5847
5848 case R_PPC64_REL30:
5849 case R_PPC64_REL32:
5850 case R_PPC64_REL64:
65f38f15 5851 case R_PPC64_ADDR32:
65f38f15
AM
5852 case R_PPC64_UADDR16:
5853 case R_PPC64_UADDR32:
5854 case R_PPC64_UADDR64:
5bd4f169 5855 case R_PPC64_TOC:
0e1862bb 5856 if (h != NULL && !bfd_link_pic (info))
81848ca0 5857 /* We may need a copy reloc. */
f5385ebf 5858 h->non_got_ref = 1;
81848ca0 5859
41bd81ab 5860 /* Don't propagate .opd relocs. */
1e2f5b6e 5861 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5862 break;
e86ce104 5863
65f38f15
AM
5864 /* If we are creating a shared library, and this is a reloc
5865 against a global symbol, or a non PC relative reloc
5866 against a local symbol, then we need to copy the reloc
5867 into the shared library. However, if we are linking with
5868 -Bsymbolic, we do not need to copy a reloc against a
5869 global symbol which is defined in an object we are
5870 including in the link (i.e., DEF_REGULAR is set). At
5871 this point we have not seen all the input files, so it is
5872 possible that DEF_REGULAR is not set now but will be set
5873 later (it is never cleared). In case of a weak definition,
5874 DEF_REGULAR may be cleared later by a strong definition in
5875 a shared library. We account for that possibility below by
f4656909 5876 storing information in the dyn_relocs field of the hash
65f38f15
AM
5877 table entry. A similar situation occurs when creating
5878 shared libraries and symbol visibility changes render the
5879 symbol local.
5880
5881 If on the other hand, we are creating an executable, we
5882 may need to keep relocations for symbols satisfied by a
5883 dynamic library if we manage to avoid copy relocs for the
5884 symbol. */
411e1bfb 5885 dodyn:
0e1862bb 5886 if ((bfd_link_pic (info)
1d483afe 5887 && (must_be_dyn_reloc (info, r_type)
65f38f15 5888 || (h != NULL
198f1157 5889 && (!SYMBOLIC_BIND (info, h)
65f38f15 5890 || h->root.type == bfd_link_hash_defweak
f5385ebf 5891 || !h->def_regular))))
f4656909 5892 || (ELIMINATE_COPY_RELOCS
0e1862bb 5893 && !bfd_link_pic (info)
65f38f15
AM
5894 && h != NULL
5895 && (h->root.type == bfd_link_hash_defweak
25f23106 5896 || !h->def_regular))
0e1862bb 5897 || (!bfd_link_pic (info)
25f23106 5898 && ifunc != NULL))
5bd4f169 5899 {
65f38f15
AM
5900 /* We must copy these reloc types into the output file.
5901 Create a reloc section in dynobj and make room for
5902 this reloc. */
5bd4f169
AM
5903 if (sreloc == NULL)
5904 {
83bac4b0
NC
5905 sreloc = _bfd_elf_make_dynamic_reloc_section
5906 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5907
5bd4f169 5908 if (sreloc == NULL)
83bac4b0 5909 return FALSE;
5bd4f169
AM
5910 }
5911
65f38f15
AM
5912 /* If this is a global symbol, we count the number of
5913 relocations we need for this symbol. */
5914 if (h != NULL)
5915 {
19e08130
AM
5916 struct elf_dyn_relocs *p;
5917 struct elf_dyn_relocs **head;
5918
ec338859 5919 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5920 p = *head;
5921 if (p == NULL || p->sec != sec)
5922 {
5923 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5924 if (p == NULL)
5925 return FALSE;
5926 p->next = *head;
5927 *head = p;
5928 p->sec = sec;
5929 p->count = 0;
5930 p->pc_count = 0;
5931 }
5932 p->count += 1;
5933 if (!must_be_dyn_reloc (info, r_type))
5934 p->pc_count += 1;
65f38f15
AM
5935 }
5936 else
5937 {
ec338859
AM
5938 /* Track dynamic relocs needed for local syms too.
5939 We really need local syms available to do this
5940 easily. Oh well. */
19e08130
AM
5941 struct ppc_dyn_relocs *p;
5942 struct ppc_dyn_relocs **head;
5943 bfd_boolean is_ifunc;
ec338859 5944 asection *s;
6edfbbad 5945 void *vpp;
87d72d41 5946 Elf_Internal_Sym *isym;
6edfbbad 5947
87d72d41
AM
5948 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5949 abfd, r_symndx);
5950 if (isym == NULL)
b34976b6 5951 return FALSE;
ec338859 5952
87d72d41
AM
5953 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5954 if (s == NULL)
5955 s = sec;
5956
6edfbbad 5957 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5958 head = (struct ppc_dyn_relocs **) vpp;
5959 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5960 p = *head;
5961 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5962 p = p->next;
5963 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5964 {
5965 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5966 if (p == NULL)
5967 return FALSE;
5968 p->next = *head;
5969 *head = p;
5970 p->sec = sec;
5971 p->ifunc = is_ifunc;
5972 p->count = 0;
5973 }
5974 p->count += 1;
ec338859 5975 }
65f38f15 5976 }
5bd4f169 5977 break;
65f38f15
AM
5978
5979 default:
96e0dda4 5980 break;
5bd4f169
AM
5981 }
5982 }
5983
b34976b6 5984 return TRUE;
5bd4f169
AM
5985}
5986
ee67d69a
AM
5987/* Merge backend specific data from an object file to the output
5988 object file when linking. */
5989
5990static bfd_boolean
50e03d47 5991ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5992{
50e03d47 5993 bfd *obfd = info->output_bfd;
ee67d69a
AM
5994 unsigned long iflags, oflags;
5995
5996 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5997 return TRUE;
5998
5999 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6000 return TRUE;
6001
50e03d47 6002 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6003 return FALSE;
6004
6005 iflags = elf_elfheader (ibfd)->e_flags;
6006 oflags = elf_elfheader (obfd)->e_flags;
6007
f6c7c3e8 6008 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6009 {
4eca0228 6010 _bfd_error_handler
695344c0 6011 /* xgettext:c-format */
ee67d69a
AM
6012 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6013 bfd_set_error (bfd_error_bad_value);
6014 return FALSE;
6015 }
f6c7c3e8 6016 else if (iflags != oflags && iflags != 0)
ee67d69a 6017 {
4eca0228 6018 _bfd_error_handler
695344c0 6019 /* xgettext:c-format */
ee67d69a
AM
6020 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6021 ibfd, iflags, oflags);
6022 bfd_set_error (bfd_error_bad_value);
6023 return FALSE;
6024 }
6025
50e03d47 6026 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
005d79fd 6027
ee67d69a 6028 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 6029 _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6030
6031 return TRUE;
6032}
6033
6034static bfd_boolean
6035ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6036{
6037 /* Print normal ELF private data. */
6038 _bfd_elf_print_private_bfd_data (abfd, ptr);
6039
6040 if (elf_elfheader (abfd)->e_flags != 0)
6041 {
6042 FILE *file = ptr;
6043
ee67d69a
AM
6044 fprintf (file, _("private flags = 0x%lx:"),
6045 elf_elfheader (abfd)->e_flags);
6046
6047 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6048 fprintf (file, _(" [abiv%ld]"),
6049 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6050 fputc ('\n', file);
6051 }
6052
6053 return TRUE;
6054}
6055
8387904d 6056/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6057 of the code entry point, and its section, which must be in the same
6058 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6059
6060static bfd_vma
6061opd_entry_value (asection *opd_sec,
6062 bfd_vma offset,
6063 asection **code_sec,
aef36ac1
AM
6064 bfd_vma *code_off,
6065 bfd_boolean in_code_sec)
8387904d
AM
6066{
6067 bfd *opd_bfd = opd_sec->owner;
8860955f 6068 Elf_Internal_Rela *relocs;
8387904d 6069 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6070 bfd_vma val;
8387904d 6071
9f296da3
AM
6072 /* No relocs implies we are linking a --just-symbols object, or looking
6073 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6074 if (opd_sec->reloc_count == 0)
6075 {
729eabd5 6076 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6077
729eabd5
AM
6078 if (contents == NULL)
6079 {
6080 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6081 return (bfd_vma) -1;
6082 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6083 }
ee1e4ede 6084
dbb3fbbb 6085 /* PR 17512: file: 64b9dfbb. */
451dfd38 6086 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6087 return (bfd_vma) -1;
6088
729eabd5 6089 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6090 if (code_sec != NULL)
6091 {
6092 asection *sec, *likely = NULL;
ee1e4ede 6093
aef36ac1 6094 if (in_code_sec)
4b85d634 6095 {
aef36ac1
AM
6096 sec = *code_sec;
6097 if (sec->vma <= val
6098 && val < sec->vma + sec->size)
6099 likely = sec;
6100 else
6101 val = -1;
6102 }
6103 else
6104 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6105 if (sec->vma <= val
6106 && (sec->flags & SEC_LOAD) != 0
6107 && (sec->flags & SEC_ALLOC) != 0)
6108 likely = sec;
6109 if (likely != NULL)
6110 {
6111 *code_sec = likely;
6112 if (code_off != NULL)
6113 *code_off = val - likely->vma;
4b85d634
AM
6114 }
6115 }
aef36ac1 6116 return val;
4b85d634
AM
6117 }
6118
0c8d6e5c 6119 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6120
729eabd5 6121 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6122 if (relocs == NULL)
6123 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6124 /* PR 17512: file: df8e1fd6. */
6125 if (relocs == NULL)
6126 return (bfd_vma) -1;
645ea6a9 6127
8387904d 6128 /* Go find the opd reloc at the sym address. */
8860955f 6129 lo = relocs;
8387904d 6130 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6131 val = (bfd_vma) -1;
8387904d
AM
6132 while (lo < hi)
6133 {
6134 look = lo + (hi - lo) / 2;
6135 if (look->r_offset < offset)
6136 lo = look + 1;
6137 else if (look->r_offset > offset)
6138 hi = look;
6139 else
6140 {
0ffa91dd
NC
6141 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6142
8387904d
AM
6143 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6144 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6145 {
6146 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6147 asection *sec = NULL;
8387904d 6148
b53dfeb2
AM
6149 if (symndx >= symtab_hdr->sh_info
6150 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6151 {
6152 struct elf_link_hash_entry **sym_hashes;
6153 struct elf_link_hash_entry *rh;
6154
6155 sym_hashes = elf_sym_hashes (opd_bfd);
6156 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6157 if (rh != NULL)
6158 {
6159 rh = elf_follow_link (rh);
bb854a36
AM
6160 if (rh->root.type != bfd_link_hash_defined
6161 && rh->root.type != bfd_link_hash_defweak)
6162 break;
6163 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6164 {
bb854a36
AM
6165 val = rh->root.u.def.value;
6166 sec = rh->root.u.def.section;
b53dfeb2
AM
6167 }
6168 }
6169 }
6170
6171 if (sec == NULL)
6172 {
6173 Elf_Internal_Sym *sym;
6174
6175 if (symndx < symtab_hdr->sh_info)
6176 {
6177 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6178 if (sym == NULL)
6179 {
6180 size_t symcnt = symtab_hdr->sh_info;
6181 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6182 symcnt, 0,
6183 NULL, NULL, NULL);
6184 if (sym == NULL)
6185 break;
6186 symtab_hdr->contents = (bfd_byte *) sym;
6187 }
6188 sym += symndx;
128205bb
AM
6189 }
6190 else
6191 {
b53dfeb2
AM
6192 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6193 1, symndx,
6194 NULL, NULL, NULL);
128205bb
AM
6195 if (sym == NULL)
6196 break;
128205bb 6197 }
b53dfeb2
AM
6198 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6199 if (sec == NULL)
6200 break;
6201 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6202 val = sym->st_value;
8387904d 6203 }
b53dfeb2 6204
8387904d
AM
6205 val += look->r_addend;
6206 if (code_off != NULL)
6207 *code_off = val;
6208 if (code_sec != NULL)
aef36ac1
AM
6209 {
6210 if (in_code_sec && *code_sec != sec)
6211 return -1;
6212 else
6213 *code_sec = sec;
6214 }
b53dfeb2 6215 if (sec->output_section != NULL)
8387904d 6216 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6217 }
6218 break;
6219 }
6220 }
645ea6a9 6221
645ea6a9 6222 return val;
8387904d
AM
6223}
6224
aef36ac1
AM
6225/* If the ELF symbol SYM might be a function in SEC, return the
6226 function size and set *CODE_OFF to the function's entry point,
6227 otherwise return zero. */
9f296da3 6228
aef36ac1
AM
6229static bfd_size_type
6230ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6231 bfd_vma *code_off)
9f296da3 6232{
aef36ac1
AM
6233 bfd_size_type size;
6234
6235 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6236 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6237 return 0;
6238
6239 size = 0;
6240 if (!(sym->flags & BSF_SYNTHETIC))
6241 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6242
6243 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6244 {
b07bca4e
AM
6245 struct _opd_sec_data *opd = get_opd_info (sym->section);
6246 bfd_vma symval = sym->value;
6247
6248 if (opd != NULL
6249 && opd->adjust != NULL
6250 && elf_section_data (sym->section)->relocs != NULL)
6251 {
6252 /* opd_entry_value will use cached relocs that have been
6253 adjusted, but with raw symbols. That means both local
6254 and global symbols need adjusting. */
6255 long adjust = opd->adjust[OPD_NDX (symval)];
6256 if (adjust == -1)
6257 return 0;
6258 symval += adjust;
6259 }
6260
6261 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6262 &sec, code_off, TRUE) == (bfd_vma) -1)
6263 return 0;
6264 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6265 symbol. This size has nothing to do with the code size of the
6266 function, which is what we're supposed to return, but the
6267 code size isn't available without looking up the dot-sym.
6268 However, doing that would be a waste of time particularly
6269 since elf_find_function will look at the dot-sym anyway.
6270 Now, elf_find_function will keep the largest size of any
6271 function sym found at the code address of interest, so return
6272 1 here to avoid it incorrectly caching a larger function size
6273 for a small function. This does mean we return the wrong
6274 size for a new-ABI function of size 24, but all that does is
6275 disable caching for such functions. */
6276 if (size == 24)
6277 size = 1;
9f296da3 6278 }
aef36ac1
AM
6279 else
6280 {
6281 if (sym->section != sec)
6282 return 0;
6283 *code_off = sym->value;
6284 }
6285 if (size == 0)
6286 size = 1;
6287 return size;
9f296da3
AM
6288}
6289
854b41e7
AM
6290/* Return true if symbol is defined in a regular object file. */
6291
6292static bfd_boolean
6293is_static_defined (struct elf_link_hash_entry *h)
6294{
6295 return ((h->root.type == bfd_link_hash_defined
6296 || h->root.type == bfd_link_hash_defweak)
6297 && h->root.u.def.section != NULL
6298 && h->root.u.def.section->output_section != NULL);
6299}
6300
b31867b6
AM
6301/* If FDH is a function descriptor symbol, return the associated code
6302 entry symbol if it is defined. Return NULL otherwise. */
6303
6304static struct ppc_link_hash_entry *
6305defined_code_entry (struct ppc_link_hash_entry *fdh)
6306{
6307 if (fdh->is_func_descriptor)
6308 {
6309 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6310 if (fh->elf.root.type == bfd_link_hash_defined
6311 || fh->elf.root.type == bfd_link_hash_defweak)
6312 return fh;
6313 }
6314 return NULL;
6315}
6316
6317/* If FH is a function code entry symbol, return the associated
6318 function descriptor symbol if it is defined. Return NULL otherwise. */
6319
6320static struct ppc_link_hash_entry *
6321defined_func_desc (struct ppc_link_hash_entry *fh)
6322{
6323 if (fh->oh != NULL
6324 && fh->oh->is_func_descriptor)
6325 {
6326 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6327 if (fdh->elf.root.type == bfd_link_hash_defined
6328 || fdh->elf.root.type == bfd_link_hash_defweak)
6329 return fdh;
6330 }
6331 return NULL;
6332}
6333
8c5b4e52
AM
6334static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6335
6336/* Garbage collect sections, after first dealing with dot-symbols. */
6337
6338static bfd_boolean
6339ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6340{
6341 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6342
6343 if (htab != NULL && htab->need_func_desc_adj)
6344 {
6345 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6346 htab->need_func_desc_adj = 0;
6347 }
6348 return bfd_elf_gc_sections (abfd, info);
6349}
6350
74f0fb50
AM
6351/* Mark all our entry sym sections, both opd and code section. */
6352
6353static void
6354ppc64_elf_gc_keep (struct bfd_link_info *info)
6355{
6356 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6357 struct bfd_sym_chain *sym;
6358
4dfe6ac6
NC
6359 if (htab == NULL)
6360 return;
6361
74f0fb50
AM
6362 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6363 {
b31867b6 6364 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6365 asection *sec;
6366
6367 eh = (struct ppc_link_hash_entry *)
b31867b6 6368 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6369 if (eh == NULL)
6370 continue;
6371 if (eh->elf.root.type != bfd_link_hash_defined
6372 && eh->elf.root.type != bfd_link_hash_defweak)
6373 continue;
6374
b31867b6
AM
6375 fh = defined_code_entry (eh);
6376 if (fh != NULL)
74f0fb50 6377 {
b31867b6 6378 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6379 sec->flags |= SEC_KEEP;
6380 }
6381 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6382 && opd_entry_value (eh->elf.root.u.def.section,
6383 eh->elf.root.u.def.value,
aef36ac1 6384 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6385 sec->flags |= SEC_KEEP;
6386
6387 sec = eh->elf.root.u.def.section;
6388 sec->flags |= SEC_KEEP;
6389 }
6390}
6391
64d03ab5
AM
6392/* Mark sections containing dynamically referenced symbols. When
6393 building shared libraries, we must assume that any visible symbol is
6394 referenced. */
6395
6396static bfd_boolean
6397ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6398{
6399 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6400 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6401 struct ppc_link_hash_entry *fdh;
b407645f 6402 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6403
64d03ab5 6404 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6405 fdh = defined_func_desc (eh);
6406 if (fdh != NULL)
6407 eh = fdh;
64d03ab5
AM
6408
6409 if ((eh->elf.root.type == bfd_link_hash_defined
6410 || eh->elf.root.type == bfd_link_hash_defweak)
6411 && (eh->elf.ref_dynamic
1c9177d9 6412 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6413 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6414 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6415 && (!bfd_link_executable (info)
e278ae05 6416 || info->gc_keep_exported
b407645f
AM
6417 || info->export_dynamic
6418 || (eh->elf.dynamic
6419 && d != NULL
6420 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
e278ae05 6421 && (eh->elf.versioned >= versioned
4c58e0d8
AM
6422 || !bfd_hide_sym_by_version (info->version_info,
6423 eh->elf.root.root.string)))))
64d03ab5
AM
6424 {
6425 asection *code_sec;
b31867b6 6426 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6427
6428 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6429
6430 /* Function descriptor syms cause the associated
6431 function code sym section to be marked. */
b31867b6
AM
6432 fh = defined_code_entry (eh);
6433 if (fh != NULL)
6434 {
6435 code_sec = fh->elf.root.u.def.section;
6436 code_sec->flags |= SEC_KEEP;
6437 }
64d03ab5
AM
6438 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6439 && opd_entry_value (eh->elf.root.u.def.section,
6440 eh->elf.root.u.def.value,
aef36ac1 6441 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6442 code_sec->flags |= SEC_KEEP;
6443 }
6444
6445 return TRUE;
6446}
6447
5bd4f169
AM
6448/* Return the section that should be marked against GC for a given
6449 relocation. */
6450
6451static asection *
4ce794b7 6452ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6453 struct bfd_link_info *info,
4ce794b7
AM
6454 Elf_Internal_Rela *rel,
6455 struct elf_link_hash_entry *h,
6456 Elf_Internal_Sym *sym)
5bd4f169 6457{
ccfa59ea
AM
6458 asection *rsec;
6459
ccfa59ea
AM
6460 /* Syms return NULL if we're marking .opd, so we avoid marking all
6461 function sections, as all functions are referenced in .opd. */
6462 rsec = NULL;
6463 if (get_opd_info (sec) != NULL)
6464 return rsec;
1e2f5b6e 6465
5bd4f169
AM
6466 if (h != NULL)
6467 {
04c9666a 6468 enum elf_ppc64_reloc_type r_type;
b31867b6 6469 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6470
4ce794b7 6471 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6472 switch (r_type)
5bd4f169
AM
6473 {
6474 case R_PPC64_GNU_VTINHERIT:
6475 case R_PPC64_GNU_VTENTRY:
6476 break;
6477
6478 default:
6479 switch (h->root.type)
6480 {
6481 case bfd_link_hash_defined:
6482 case bfd_link_hash_defweak:
ccfa59ea 6483 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6484 fdh = defined_func_desc (eh);
6485 if (fdh != NULL)
8c5b4e52
AM
6486 {
6487 /* -mcall-aixdesc code references the dot-symbol on
6488 a call reloc. Mark the function descriptor too
6489 against garbage collection. */
6490 fdh->elf.mark = 1;
6491 if (fdh->elf.u.weakdef != NULL)
6492 fdh->elf.u.weakdef->mark = 1;
6493 eh = fdh;
6494 }
1e2f5b6e
AM
6495
6496 /* Function descriptor syms cause the associated
6497 function code sym section to be marked. */
b31867b6
AM
6498 fh = defined_code_entry (eh);
6499 if (fh != NULL)
ccfa59ea
AM
6500 {
6501 /* They also mark their opd section. */
74f0fb50 6502 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6503
b31867b6 6504 rsec = fh->elf.root.u.def.section;
ccfa59ea 6505 }
8387904d
AM
6506 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6507 && opd_entry_value (eh->elf.root.u.def.section,
6508 eh->elf.root.u.def.value,
aef36ac1 6509 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6510 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6511 else
1e2f5b6e
AM
6512 rsec = h->root.u.def.section;
6513 break;
5bd4f169
AM
6514
6515 case bfd_link_hash_common:
1e2f5b6e
AM
6516 rsec = h->root.u.c.p->section;
6517 break;
5bd4f169
AM
6518
6519 default:
fb34365b 6520 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6521 }
6522 }
6523 }
6524 else
6525 {
74f0fb50 6526 struct _opd_sec_data *opd;
1e2f5b6e
AM
6527
6528 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6529 opd = get_opd_info (rsec);
6530 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6531 {
74f0fb50 6532 rsec->gc_mark = 1;
ccfa59ea 6533
51aecdc5 6534 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6535 }
5bd4f169
AM
6536 }
6537
1e2f5b6e 6538 return rsec;
5bd4f169
AM
6539}
6540
65f38f15
AM
6541/* Update the .got, .plt. and dynamic reloc reference counts for the
6542 section being removed. */
5bd4f169 6543
b34976b6 6544static bfd_boolean
4ce794b7
AM
6545ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6546 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 6547{
411e1bfb 6548 struct ppc_link_hash_table *htab;
5bd4f169
AM
6549 Elf_Internal_Shdr *symtab_hdr;
6550 struct elf_link_hash_entry **sym_hashes;
411e1bfb 6551 struct got_entry **local_got_ents;
5bd4f169 6552 const Elf_Internal_Rela *rel, *relend;
5bd4f169 6553
0e1862bb 6554 if (bfd_link_relocatable (info))
7dda2462
TG
6555 return TRUE;
6556
680a3378
AM
6557 if ((sec->flags & SEC_ALLOC) == 0)
6558 return TRUE;
6559
ec338859
AM
6560 elf_section_data (sec)->local_dynrel = NULL;
6561
411e1bfb 6562 htab = ppc_hash_table (info);
4dfe6ac6
NC
6563 if (htab == NULL)
6564 return FALSE;
6565
0ffa91dd 6566 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 6567 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 6568 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
6569
6570 relend = relocs + sec->reloc_count;
6571 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
6572 {
6573 unsigned long r_symndx;
04c9666a 6574 enum elf_ppc64_reloc_type r_type;
58ac9f71 6575 struct elf_link_hash_entry *h = NULL;
cbf95972 6576 struct plt_entry **plt_list;
f961d9dd 6577 unsigned char tls_type = 0;
5bd4f169 6578
a33d1f77 6579 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 6580 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
6581 if (r_symndx >= symtab_hdr->sh_info)
6582 {
6583 struct ppc_link_hash_entry *eh;
6061a67d
AM
6584 struct elf_dyn_relocs **pp;
6585 struct elf_dyn_relocs *p;
58ac9f71
AM
6586
6587 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6588 h = elf_follow_link (h);
58ac9f71
AM
6589 eh = (struct ppc_link_hash_entry *) h;
6590
6591 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6592 if (p->sec == sec)
6593 {
6594 /* Everything must go for SEC. */
6595 *pp = p->next;
6596 break;
6597 }
6598 }
6599
a33d1f77
AM
6600 switch (r_type)
6601 {
411e1bfb
AM
6602 case R_PPC64_GOT_TLSLD16:
6603 case R_PPC64_GOT_TLSLD16_LO:
6604 case R_PPC64_GOT_TLSLD16_HI:
6605 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 6606 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6607 goto dogot;
6608
6609 case R_PPC64_GOT_TLSGD16:
6610 case R_PPC64_GOT_TLSGD16_LO:
6611 case R_PPC64_GOT_TLSGD16_HI:
6612 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6613 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6614 goto dogot;
6615
6616 case R_PPC64_GOT_TPREL16_DS:
6617 case R_PPC64_GOT_TPREL16_LO_DS:
6618 case R_PPC64_GOT_TPREL16_HI:
6619 case R_PPC64_GOT_TPREL16_HA:
6620 tls_type = TLS_TLS | TLS_TPREL;
6621 goto dogot;
6622
6623 case R_PPC64_GOT_DTPREL16_DS:
6624 case R_PPC64_GOT_DTPREL16_LO_DS:
6625 case R_PPC64_GOT_DTPREL16_HI:
6626 case R_PPC64_GOT_DTPREL16_HA:
6627 tls_type = TLS_TLS | TLS_DTPREL;
6628 goto dogot;
6629
a33d1f77
AM
6630 case R_PPC64_GOT16:
6631 case R_PPC64_GOT16_DS:
6632 case R_PPC64_GOT16_HA:
6633 case R_PPC64_GOT16_HI:
6634 case R_PPC64_GOT16_LO:
6635 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
6636 dogot:
6637 {
6638 struct got_entry *ent;
6639
58ac9f71
AM
6640 if (h != NULL)
6641 ent = h->got.glist;
411e1bfb
AM
6642 else
6643 ent = local_got_ents[r_symndx];
6644
6645 for (; ent != NULL; ent = ent->next)
6646 if (ent->addend == rel->r_addend
e717da7e 6647 && ent->owner == abfd
411e1bfb
AM
6648 && ent->tls_type == tls_type)
6649 break;
6650 if (ent == NULL)
6651 abort ();
6652 if (ent->got.refcount > 0)
6653 ent->got.refcount -= 1;
6654 }
a33d1f77 6655 break;
65f38f15 6656
a33d1f77
AM
6657 case R_PPC64_PLT16_HA:
6658 case R_PPC64_PLT16_HI:
6659 case R_PPC64_PLT16_LO:
6660 case R_PPC64_PLT32:
6661 case R_PPC64_PLT64:
721956f4
AM
6662 case R_PPC64_REL14:
6663 case R_PPC64_REL14_BRNTAKEN:
6664 case R_PPC64_REL14_BRTAKEN:
5d1634d7 6665 case R_PPC64_REL24:
cbf95972 6666 plt_list = NULL;
58ac9f71 6667 if (h != NULL)
cbf95972
AM
6668 plt_list = &h->plt.plist;
6669 else if (local_got_ents != NULL)
6670 {
6671 struct plt_entry **local_plt = (struct plt_entry **)
6672 (local_got_ents + symtab_hdr->sh_info);
6673 unsigned char *local_got_tls_masks = (unsigned char *)
6674 (local_plt + symtab_hdr->sh_info);
6675 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6676 plt_list = local_plt + r_symndx;
6677 }
6678 if (plt_list)
5d1634d7 6679 {
411e1bfb
AM
6680 struct plt_entry *ent;
6681
cbf95972 6682 for (ent = *plt_list; ent != NULL; ent = ent->next)
411e1bfb
AM
6683 if (ent->addend == rel->r_addend)
6684 break;
7c6c1722 6685 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 6686 ent->plt.refcount -= 1;
5d1634d7 6687 }
e86ce104 6688 break;
5d1634d7 6689
a33d1f77
AM
6690 default:
6691 break;
6692 }
6693 }
b34976b6 6694 return TRUE;
5bd4f169
AM
6695}
6696
deb0e272
AM
6697/* The maximum size of .sfpr. */
6698#define SFPR_MAX (218*4)
6699
6700struct sfpr_def_parms
6701{
699733f6
AM
6702 const char name[12];
6703 unsigned char lo, hi;
deb0e272
AM
6704 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6705 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6706};
6707
a4b6fadd
AM
6708/* Auto-generate _save*, _rest* functions in .sfpr.
6709 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6710 instead. */
deb0e272 6711
4dfe6ac6 6712static bfd_boolean
a4b6fadd
AM
6713sfpr_define (struct bfd_link_info *info,
6714 const struct sfpr_def_parms *parm,
6715 asection *stub_sec)
deb0e272
AM
6716{
6717 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6718 unsigned int i;
6719 size_t len = strlen (parm->name);
6720 bfd_boolean writing = FALSE;
699733f6 6721 char sym[16];
deb0e272 6722
4dfe6ac6
NC
6723 if (htab == NULL)
6724 return FALSE;
6725
deb0e272
AM
6726 memcpy (sym, parm->name, len);
6727 sym[len + 2] = 0;
6728
6729 for (i = parm->lo; i <= parm->hi; i++)
6730 {
a4b6fadd 6731 struct ppc_link_hash_entry *h;
deb0e272
AM
6732
6733 sym[len + 0] = i / 10 + '0';
6734 sym[len + 1] = i % 10 + '0';
a4b6fadd 6735 h = (struct ppc_link_hash_entry *)
b32547cd 6736 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6737 if (stub_sec != NULL)
deb0e272 6738 {
a4b6fadd
AM
6739 if (h != NULL
6740 && h->elf.root.type == bfd_link_hash_defined
6741 && h->elf.root.u.def.section == htab->sfpr)
6742 {
6743 struct elf_link_hash_entry *s;
6744 char buf[32];
6745 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6746 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6747 if (s == NULL)
6748 return FALSE;
6749 if (s->root.type == bfd_link_hash_new
6750 || (s->root.type = bfd_link_hash_defined
6751 && s->root.u.def.section == stub_sec))
6752 {
6753 s->root.type = bfd_link_hash_defined;
6754 s->root.u.def.section = stub_sec;
6755 s->root.u.def.value = (stub_sec->size
6756 + h->elf.root.u.def.value);
6757 s->ref_regular = 1;
6758 s->def_regular = 1;
6759 s->ref_regular_nonweak = 1;
6760 s->forced_local = 1;
6761 s->non_elf = 0;
6762 s->root.linker_def = 1;
6763 }
6764 }
6765 continue;
6766 }
6767 if (h != NULL)
6768 {
6769 h->save_res = 1;
6770 if (!h->elf.def_regular)
deb0e272 6771 {
a4b6fadd
AM
6772 h->elf.root.type = bfd_link_hash_defined;
6773 h->elf.root.u.def.section = htab->sfpr;
6774 h->elf.root.u.def.value = htab->sfpr->size;
6775 h->elf.type = STT_FUNC;
6776 h->elf.def_regular = 1;
b32547cd 6777 h->elf.non_elf = 0;
a4b6fadd
AM
6778 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6779 writing = TRUE;
deb0e272 6780 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6781 {
6782 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6783 if (htab->sfpr->contents == NULL)
6784 return FALSE;
6785 }
deb0e272
AM
6786 }
6787 }
6788 if (writing)
6789 {
6790 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6791 if (i != parm->hi)
6792 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6793 else
6794 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6795 htab->sfpr->size = p - htab->sfpr->contents;
6796 }
6797 }
6798
6799 return TRUE;
6800}
6801
6802static bfd_byte *
6803savegpr0 (bfd *abfd, bfd_byte *p, int r)
6804{
6805 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6806 return p + 4;
6807}
6808
6809static bfd_byte *
6810savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6811{
6812 p = savegpr0 (abfd, p, r);
a078d95a 6813 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6814 p = p + 4;
6815 bfd_put_32 (abfd, BLR, p);
6816 return p + 4;
6817}
6818
6819static bfd_byte *
6820restgpr0 (bfd *abfd, bfd_byte *p, int r)
6821{
6822 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6823 return p + 4;
6824}
6825
6826static bfd_byte *
6827restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6828{
a078d95a 6829 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6830 p = p + 4;
6831 p = restgpr0 (abfd, p, r);
6832 bfd_put_32 (abfd, MTLR_R0, p);
6833 p = p + 4;
6834 if (r == 29)
6835 {
6836 p = restgpr0 (abfd, p, 30);
6837 p = restgpr0 (abfd, p, 31);
6838 }
6839 bfd_put_32 (abfd, BLR, p);
6840 return p + 4;
6841}
6842
6843static bfd_byte *
6844savegpr1 (bfd *abfd, bfd_byte *p, int r)
6845{
6846 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6847 return p + 4;
6848}
6849
6850static bfd_byte *
6851savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6852{
6853 p = savegpr1 (abfd, p, r);
6854 bfd_put_32 (abfd, BLR, p);
6855 return p + 4;
6856}
6857
6858static bfd_byte *
6859restgpr1 (bfd *abfd, bfd_byte *p, int r)
6860{
6861 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6862 return p + 4;
6863}
6864
6865static bfd_byte *
6866restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6867{
6868 p = restgpr1 (abfd, p, r);
6869 bfd_put_32 (abfd, BLR, p);
6870 return p + 4;
6871}
6872
6873static bfd_byte *
6874savefpr (bfd *abfd, bfd_byte *p, int r)
6875{
6876 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6877 return p + 4;
6878}
6879
6880static bfd_byte *
6881savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6882{
6883 p = savefpr (abfd, p, r);
a078d95a 6884 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6885 p = p + 4;
6886 bfd_put_32 (abfd, BLR, p);
6887 return p + 4;
6888}
6889
6890static bfd_byte *
6891restfpr (bfd *abfd, bfd_byte *p, int r)
6892{
6893 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6894 return p + 4;
6895}
6896
6897static bfd_byte *
6898restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6899{
a078d95a 6900 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6901 p = p + 4;
6902 p = restfpr (abfd, p, r);
6903 bfd_put_32 (abfd, MTLR_R0, p);
6904 p = p + 4;
6905 if (r == 29)
6906 {
6907 p = restfpr (abfd, p, 30);
6908 p = restfpr (abfd, p, 31);
6909 }
6910 bfd_put_32 (abfd, BLR, p);
6911 return p + 4;
6912}
6913
6914static bfd_byte *
6915savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6916{
6917 p = savefpr (abfd, p, r);
6918 bfd_put_32 (abfd, BLR, p);
6919 return p + 4;
6920}
6921
6922static bfd_byte *
6923restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6924{
6925 p = restfpr (abfd, p, r);
6926 bfd_put_32 (abfd, BLR, p);
6927 return p + 4;
6928}
6929
6930static bfd_byte *
6931savevr (bfd *abfd, bfd_byte *p, int r)
6932{
6933 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6934 p = p + 4;
6935 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6936 return p + 4;
6937}
6938
6939static bfd_byte *
6940savevr_tail (bfd *abfd, bfd_byte *p, int r)
6941{
6942 p = savevr (abfd, p, r);
6943 bfd_put_32 (abfd, BLR, p);
6944 return p + 4;
6945}
6946
6947static bfd_byte *
6948restvr (bfd *abfd, bfd_byte *p, int r)
6949{
6950 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6951 p = p + 4;
6952 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6953 return p + 4;
6954}
6955
6956static bfd_byte *
6957restvr_tail (bfd *abfd, bfd_byte *p, int r)
6958{
6959 p = restvr (abfd, p, r);
6960 bfd_put_32 (abfd, BLR, p);
6961 return p + 4;
6962}
6963
e86ce104
AM
6964/* Called via elf_link_hash_traverse to transfer dynamic linking
6965 information on function code symbol entries to their corresponding
6966 function descriptor symbol entries. */
deb0e272 6967
b34976b6 6968static bfd_boolean
4ce794b7 6969func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6970{
e86ce104 6971 struct bfd_link_info *info;
65f38f15 6972 struct ppc_link_hash_table *htab;
50bc7936
AM
6973 struct ppc_link_hash_entry *fh;
6974 struct ppc_link_hash_entry *fdh;
6975 bfd_boolean force_local;
5bd4f169 6976
50bc7936
AM
6977 fh = (struct ppc_link_hash_entry *) h;
6978 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6979 return TRUE;
e86ce104 6980
8c5b4e52
AM
6981 if (!fh->is_func)
6982 return TRUE;
6983
6984 if (fh->elf.root.root.string[0] != '.'
6985 || fh->elf.root.root.string[1] == '\0')
6986 return TRUE;
6987
4ce794b7 6988 info = inf;
65f38f15 6989 htab = ppc_hash_table (info);
4dfe6ac6
NC
6990 if (htab == NULL)
6991 return FALSE;
5bd4f169 6992
8c5b4e52
AM
6993 /* Find the corresponding function descriptor symbol. */
6994 fdh = lookup_fdh (fh, htab);
6995
c09bdfe5
AM
6996 /* Resolve undefined references to dot-symbols as the value
6997 in the function descriptor, if we have one in a regular object.
6998 This is to satisfy cases like ".quad .foo". Calls to functions
6999 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
7000 if ((fh->elf.root.type == bfd_link_hash_undefined
7001 || fh->elf.root.type == bfd_link_hash_undefweak)
7002 && (fdh->elf.root.type == bfd_link_hash_defined
7003 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
7004 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7005 && opd_entry_value (fdh->elf.root.u.def.section,
7006 fdh->elf.root.u.def.value,
c09bdfe5 7007 &fh->elf.root.u.def.section,
aef36ac1 7008 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 7009 {
b31867b6 7010 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 7011 fh->elf.forced_local = 1;
b31867b6
AM
7012 fh->elf.def_regular = fdh->elf.def_regular;
7013 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
7014 }
7015
8c5b4e52
AM
7016 if (!fh->elf.dynamic)
7017 {
7018 struct plt_entry *ent;
5bd4f169 7019
8c5b4e52
AM
7020 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7021 if (ent->plt.refcount > 0)
7022 break;
7023 if (ent == NULL)
7024 return TRUE;
7025 }
5bd4f169 7026
8c5b4e52 7027 /* Create a descriptor as undefined if necessary. */
50bc7936 7028 if (fdh == NULL
0e1862bb 7029 && !bfd_link_executable (info)
50bc7936
AM
7030 && (fh->elf.root.type == bfd_link_hash_undefined
7031 || fh->elf.root.type == bfd_link_hash_undefweak))
7032 {
908b32fc 7033 fdh = make_fdh (info, fh);
bb700d78
AM
7034 if (fdh == NULL)
7035 return FALSE;
50bc7936 7036 }
648cca2c 7037
8c5b4e52 7038 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
7039 if (fdh != NULL
7040 && fdh->fake
8c5b4e52
AM
7041 && (fh->elf.root.type == bfd_link_hash_defined
7042 || fh->elf.root.type == bfd_link_hash_defweak))
7043 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 7044
8c5b4e52
AM
7045 /* Transfer dynamic linking information to the function descriptor. */
7046 if (fdh != NULL)
7047 {
f5385ebf
AM
7048 fdh->elf.ref_regular |= fh->elf.ref_regular;
7049 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7050 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7051 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
7052 fdh->elf.dynamic |= fh->elf.dynamic;
7053 fdh->elf.needs_plt |= (fh->elf.needs_plt
7054 || fh->elf.type == STT_FUNC
7055 || fh->elf.type == STT_GNU_IFUNC);
7056 move_plt_plist (fh, fdh);
7057
7058 if (!fdh->elf.forced_local
7059 && fh->elf.dynindx != -1)
7060 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7061 return FALSE;
e86ce104
AM
7062 }
7063
50bc7936
AM
7064 /* Now that the info is on the function descriptor, clear the
7065 function code sym info. Any function code syms for which we
7066 don't have a definition in a regular file, we force local.
7067 This prevents a shared library from exporting syms that have
7068 been imported from another library. Function code syms that
7069 are really in the library we must leave global to prevent the
7070 linker dragging in a definition from a static library. */
93f3fa99
AM
7071 force_local = (!fh->elf.def_regular
7072 || fdh == NULL
7073 || !fdh->elf.def_regular
7074 || fdh->elf.forced_local);
50bc7936
AM
7075 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7076
b34976b6 7077 return TRUE;
e86ce104 7078}
40b8271b 7079
a4b6fadd
AM
7080static const struct sfpr_def_parms save_res_funcs[] =
7081 {
7082 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7083 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7084 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7085 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7086 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7087 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7088 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7089 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7090 { "._savef", 14, 31, savefpr, savefpr1_tail },
7091 { "._restf", 14, 31, restfpr, restfpr1_tail },
7092 { "_savevr_", 20, 31, savevr, savevr_tail },
7093 { "_restvr_", 20, 31, restvr, restvr_tail }
7094 };
7095
e86ce104 7096/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
7097 this hook to a) provide some gcc support functions, and b) transfer
7098 dynamic linking information gathered so far on function code symbol
7099 entries, to their corresponding function descriptor symbol entries. */
deb0e272 7100
b34976b6 7101static bfd_boolean
4ce794b7
AM
7102ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7103 struct bfd_link_info *info)
e86ce104
AM
7104{
7105 struct ppc_link_hash_table *htab;
7106
7107 htab = ppc_hash_table (info);
4dfe6ac6
NC
7108 if (htab == NULL)
7109 return FALSE;
7110
b32547cd
AM
7111 /* Provide any missing _save* and _rest* functions. */
7112 if (htab->sfpr != NULL)
7113 {
7114 unsigned int i;
7115
7116 htab->sfpr->size = 0;
7117 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7118 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7119 return FALSE;
7120 if (htab->sfpr->size == 0)
7121 htab->sfpr->flags |= SEC_EXCLUDE;
7122 }
7123
7124 if (bfd_link_relocatable (info))
7125 return TRUE;
7126
7127 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7128 {
7129 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7130 /* Make .TOC. defined so as to prevent it being made dynamic.
7131 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7132 if (!htab->elf.hgot->def_regular
7133 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7134 {
7135 htab->elf.hgot->root.type = bfd_link_hash_defined;
7136 htab->elf.hgot->root.u.def.value = 0;
7137 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7138 htab->elf.hgot->def_regular = 1;
7139 htab->elf.hgot->root.linker_def = 1;
7140 }
dba6fa9b 7141 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7142 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7143 | STV_HIDDEN);
7144 }
c66bb0ee 7145
8c5b4e52
AM
7146 if (htab->need_func_desc_adj)
7147 {
7148 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7149 htab->need_func_desc_adj = 0;
7150 }
805fc799 7151
b34976b6 7152 return TRUE;
e86ce104
AM
7153}
7154
8a2058b5
AM
7155/* Return true if we have dynamic relocs against H that apply to
7156 read-only sections. */
a345bc8d
AM
7157
7158static bfd_boolean
7159readonly_dynrelocs (struct elf_link_hash_entry *h)
7160{
7161 struct ppc_link_hash_entry *eh;
7162 struct elf_dyn_relocs *p;
7163
7164 eh = (struct ppc_link_hash_entry *) h;
7165 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7166 {
7167 asection *s = p->sec->output_section;
7168
7169 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7170 return TRUE;
7171 }
7172 return FALSE;
7173}
7174
d311bc8b
AM
7175/* Return true if we have dynamic relocs against H or any of its weak
7176 aliases, that apply to read-only sections. */
7177
7178static bfd_boolean
7179alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7180{
7181 struct ppc_link_hash_entry *eh;
7182
7183 eh = (struct ppc_link_hash_entry *) h;
7184 do
7185 {
7186 if (readonly_dynrelocs (&eh->elf))
7187 return TRUE;
7188 eh = eh->weakref;
7189 } while (eh != NULL && &eh->elf != h);
7190
7191 return FALSE;
7192}
8a2058b5 7193
8a9e8e72
AM
7194/* Return whether EH has pc-relative dynamic relocs. */
7195
7196static bfd_boolean
7197pc_dynrelocs (struct ppc_link_hash_entry *eh)
7198{
7199 struct elf_dyn_relocs *p;
7200
7201 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7202 if (p->pc_count != 0)
7203 return TRUE;
7204 return FALSE;
7205}
7206
8a2058b5
AM
7207/* Return true if a global entry stub will be created for H. Valid
7208 for ELFv2 before plt entries have been allocated. */
7209
7210static bfd_boolean
7211global_entry_stub (struct elf_link_hash_entry *h)
7212{
7213 struct plt_entry *pent;
7214
7215 if (!h->pointer_equality_needed
7216 || h->def_regular)
7217 return FALSE;
7218
7219 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7220 if (pent->plt.refcount > 0
7221 && pent->addend == 0)
7222 return TRUE;
7223
7224 return FALSE;
7225}
7226
e86ce104
AM
7227/* Adjust a symbol defined by a dynamic object and referenced by a
7228 regular object. The current definition is in some section of the
7229 dynamic object, but we're not including those sections. We have to
7230 change the definition to something the rest of the link can
7231 understand. */
7232
b34976b6 7233static bfd_boolean
4ce794b7
AM
7234ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7235 struct elf_link_hash_entry *h)
e86ce104
AM
7236{
7237 struct ppc_link_hash_table *htab;
5474d94f 7238 asection *s, *srel;
e86ce104
AM
7239
7240 htab = ppc_hash_table (info);
4dfe6ac6
NC
7241 if (htab == NULL)
7242 return FALSE;
e86ce104
AM
7243
7244 /* Deal with function syms. */
7245 if (h->type == STT_FUNC
e054468f 7246 || h->type == STT_GNU_IFUNC
f5385ebf 7247 || h->needs_plt)
e86ce104
AM
7248 {
7249 /* Clear procedure linkage table information for any symbol that
7250 won't need a .plt entry. */
411e1bfb
AM
7251 struct plt_entry *ent;
7252 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7253 if (ent->plt.refcount > 0)
7254 break;
8387904d 7255 if (ent == NULL
e054468f
AM
7256 || (h->type != STT_GNU_IFUNC
7257 && (SYMBOL_CALLS_LOCAL (info, h)
7258 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
a4b6fadd
AM
7259 && h->root.type == bfd_link_hash_undefweak)))
7260 || ((struct ppc_link_hash_entry *) h)->save_res)
40b8271b 7261 {
411e1bfb 7262 h->plt.plist = NULL;
f5385ebf 7263 h->needs_plt = 0;
d1eca1e4 7264 h->pointer_equality_needed = 0;
40b8271b 7265 }
8a2058b5 7266 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7267 {
d1eca1e4
AM
7268 /* Taking a function's address in a read/write section
7269 doesn't require us to define the function symbol in the
7270 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7271 be used instead. The reason we prefer a few more dynamic
7272 relocs is that calling via a global entry stub costs a
7273 few more instructions, and pointer_equality_needed causes
7274 extra work in ld.so when resolving these symbols. */
7275 if (global_entry_stub (h)
d311bc8b 7276 && !alias_readonly_dynrelocs (h))
d1eca1e4
AM
7277 {
7278 h->pointer_equality_needed = 0;
8a2058b5
AM
7279 /* After adjust_dynamic_symbol, non_got_ref set in
7280 the non-pic case means that dyn_relocs for this
7281 symbol should be discarded. */
d1eca1e4
AM
7282 h->non_got_ref = 0;
7283 }
7284
a345bc8d
AM
7285 /* If making a plt entry, then we don't need copy relocs. */
7286 return TRUE;
7287 }
5bd4f169 7288 }
bbd7ec4a 7289 else
411e1bfb 7290 h->plt.plist = NULL;
5bd4f169
AM
7291
7292 /* If this is a weak symbol, and there is a real definition, the
7293 processor independent code will have arranged for us to see the
7294 real definition first, and we can just use the same value. */
f6e332e6 7295 if (h->u.weakdef != NULL)
5bd4f169 7296 {
f6e332e6
AM
7297 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7298 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7299 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7300 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 7301 if (ELIMINATE_COPY_RELOCS)
f6e332e6 7302 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 7303 return TRUE;
5bd4f169
AM
7304 }
7305
5bd4f169
AM
7306 /* If we are creating a shared library, we must presume that the
7307 only references to the symbol are via the global offset table.
7308 For such cases we need not do anything here; the relocations will
7309 be handled correctly by relocate_section. */
0e1862bb 7310 if (bfd_link_pic (info))
b34976b6 7311 return TRUE;
5bd4f169 7312
65f38f15
AM
7313 /* If there are no references to this symbol that do not use the
7314 GOT, we don't need to generate a copy reloc. */
f5385ebf 7315 if (!h->non_got_ref)
b34976b6 7316 return TRUE;
65f38f15 7317
b186458a 7318 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7319 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7320
d93d1c80
AM
7321 /* If -z nocopyreloc was given, don't generate them either. */
7322 || info->nocopyreloc
a127494f 7323
d93d1c80
AM
7324 /* If we didn't find any dynamic relocs in read-only sections, then
7325 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7326 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7327
d93d1c80
AM
7328 /* Protected variables do not work with .dynbss. The copy in
7329 .dynbss won't be used by the shared library with the protected
7330 definition for the variable. Text relocations are preferable
7331 to an incorrect program. */
7332 || h->protected_def)
a127494f
AM
7333 {
7334 h->non_got_ref = 0;
7335 return TRUE;
7336 }
7337
5d35169e 7338 if (h->plt.plist != NULL)
97b639ba
AM
7339 {
7340 /* We should never get here, but unfortunately there are versions
7341 of gcc out there that improperly (for this ABI) put initialized
7342 function pointers, vtable refs and suchlike in read-only
7343 sections. Allow them to proceed, but warn that this might
7344 break at runtime. */
25f53a85 7345 info->callbacks->einfo
bc30df16 7346 (_("%P: copy reloc against `%T' requires lazy plt linking; "
25f53a85 7347 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7348 h->root.root.string);
7349 }
5d35169e
AM
7350
7351 /* This is a reference to a symbol defined by a dynamic object which
7352 is not a function. */
7353
5bd4f169
AM
7354 /* We must allocate the symbol in our .dynbss section, which will
7355 become part of the .bss section of the executable. There will be
7356 an entry for this symbol in the .dynsym section. The dynamic
7357 object will contain position independent code, so all references
7358 from the dynamic object to this symbol will go through the global
7359 offset table. The dynamic linker will use the .dynsym entry to
7360 determine the address it must put in the global offset table, so
7361 both the dynamic object and the regular object will refer to the
7362 same memory location for the variable. */
5bd4f169 7363
04c9666a
AM
7364 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7365 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
7366 runtime process image. We need to remember the offset into the
7367 .rela.bss section we are going to use. */
5474d94f
AM
7368 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7369 {
7370 s = htab->elf.sdynrelro;
7371 srel = htab->elf.sreldynrelro;
7372 }
7373 else
7374 {
7375 s = htab->elf.sdynbss;
7376 srel = htab->elf.srelbss;
7377 }
1d7e9d18 7378 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7379 {
5474d94f 7380 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 7381 h->needs_copy = 1;
5bd4f169
AM
7382 }
7383
6cabe1ea 7384 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7385}
7386
e86ce104
AM
7387/* If given a function descriptor symbol, hide both the function code
7388 sym and the descriptor. */
7389static void
4ce794b7
AM
7390ppc64_elf_hide_symbol (struct bfd_link_info *info,
7391 struct elf_link_hash_entry *h,
7392 bfd_boolean force_local)
e86ce104 7393{
34814b9f 7394 struct ppc_link_hash_entry *eh;
e86ce104
AM
7395 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7396
34814b9f
AM
7397 eh = (struct ppc_link_hash_entry *) h;
7398 if (eh->is_func_descriptor)
e86ce104 7399 {
34814b9f 7400 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7401
721956f4 7402 if (fh == NULL)
d1329ca3
AM
7403 {
7404 const char *p, *q;
b8ac2841 7405 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
7406 char save;
7407
7408 /* We aren't supposed to use alloca in BFD because on
7409 systems which do not have alloca the version in libiberty
7410 calls xmalloc, which might cause the program to crash
7411 when it runs out of memory. This function doesn't have a
7412 return status, so there's no way to gracefully return an
7413 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7414 accessed; It's either a string in an ELF string table,
7415 or allocated in an objalloc structure. */
d1329ca3 7416
34814b9f 7417 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7418 save = *p;
7419 *(char *) p = '.';
34814b9f 7420 fh = (struct ppc_link_hash_entry *)
b8ac2841 7421 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7422 *(char *) p = save;
7423
7424 /* Unfortunately, if it so happens that the string we were
7425 looking for was allocated immediately before this string,
7426 then we overwrote the string terminator. That's the only
7427 reason the lookup should fail. */
7428 if (fh == NULL)
7429 {
34814b9f
AM
7430 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7431 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7432 --q, --p;
34814b9f
AM
7433 if (q < eh->elf.root.root.string && *p == '.')
7434 fh = (struct ppc_link_hash_entry *)
b8ac2841 7435 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7436 }
7437 if (fh != NULL)
7438 {
34814b9f
AM
7439 eh->oh = fh;
7440 fh->oh = eh;
d1329ca3
AM
7441 }
7442 }
e86ce104 7443 if (fh != NULL)
34814b9f 7444 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7445 }
7446}
7447
411e1bfb 7448static bfd_boolean
8843416a
AM
7449get_sym_h (struct elf_link_hash_entry **hp,
7450 Elf_Internal_Sym **symp,
7451 asection **symsecp,
f961d9dd 7452 unsigned char **tls_maskp,
8843416a
AM
7453 Elf_Internal_Sym **locsymsp,
7454 unsigned long r_symndx,
7455 bfd *ibfd)
411e1bfb 7456{
0ffa91dd 7457 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7458
7459 if (r_symndx >= symtab_hdr->sh_info)
7460 {
7461 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7462 struct elf_link_hash_entry *h;
7463
7464 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7465 h = elf_follow_link (h);
411e1bfb
AM
7466
7467 if (hp != NULL)
7468 *hp = h;
7469
7470 if (symp != NULL)
7471 *symp = NULL;
7472
7473 if (symsecp != NULL)
7474 {
7475 asection *symsec = NULL;
7476 if (h->root.type == bfd_link_hash_defined
7477 || h->root.type == bfd_link_hash_defweak)
7478 symsec = h->root.u.def.section;
7479 *symsecp = symsec;
7480 }
7481
e7b938ca 7482 if (tls_maskp != NULL)
411e1bfb
AM
7483 {
7484 struct ppc_link_hash_entry *eh;
7485
7486 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7487 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7488 }
7489 }
7490 else
7491 {
7492 Elf_Internal_Sym *sym;
7493 Elf_Internal_Sym *locsyms = *locsymsp;
7494
7495 if (locsyms == NULL)
7496 {
7497 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7498 if (locsyms == NULL)
7499 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7500 symtab_hdr->sh_info,
7501 0, NULL, NULL, NULL);
7502 if (locsyms == NULL)
7503 return FALSE;
7504 *locsymsp = locsyms;
7505 }
7506 sym = locsyms + r_symndx;
7507
7508 if (hp != NULL)
7509 *hp = NULL;
7510
7511 if (symp != NULL)
7512 *symp = sym;
7513
7514 if (symsecp != NULL)
cb33740c 7515 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7516
e7b938ca 7517 if (tls_maskp != NULL)
411e1bfb
AM
7518 {
7519 struct got_entry **lgot_ents;
f961d9dd 7520 unsigned char *tls_mask;
411e1bfb 7521
e7b938ca 7522 tls_mask = NULL;
411e1bfb
AM
7523 lgot_ents = elf_local_got_ents (ibfd);
7524 if (lgot_ents != NULL)
7525 {
e054468f
AM
7526 struct plt_entry **local_plt = (struct plt_entry **)
7527 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7528 unsigned char *lgot_masks = (unsigned char *)
e054468f 7529 (local_plt + symtab_hdr->sh_info);
e7b938ca 7530 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7531 }
e7b938ca 7532 *tls_maskp = tls_mask;
411e1bfb
AM
7533 }
7534 }
7535 return TRUE;
7536}
7537
e7b938ca 7538/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7539 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7540 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7541
7542static int
f961d9dd 7543get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7544 unsigned long *toc_symndx,
7545 bfd_vma *toc_addend,
0d4792f7 7546 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7547 const Elf_Internal_Rela *rel,
7548 bfd *ibfd)
411e1bfb
AM
7549{
7550 unsigned long r_symndx;
0d4792f7 7551 int next_r;
411e1bfb
AM
7552 struct elf_link_hash_entry *h;
7553 Elf_Internal_Sym *sym;
7554 asection *sec;
7555 bfd_vma off;
7556
7557 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7558 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7559 return 0;
411e1bfb 7560
e7b938ca 7561 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7562 || sec == NULL
6bee8834 7563 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7564 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7565 return 1;
411e1bfb
AM
7566
7567 /* Look inside a TOC section too. */
7568 if (h != NULL)
7569 {
7570 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7571 off = h->root.u.def.value;
7572 }
7573 else
7574 off = sym->st_value;
7575 off += rel->r_addend;
7576 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7577 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7578 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7579 if (toc_symndx != NULL)
7580 *toc_symndx = r_symndx;
3a71aa26
AM
7581 if (toc_addend != NULL)
7582 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7583 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7584 return 0;
854b41e7 7585 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7586 && (next_r == -1 || next_r == -2))
7587 return 1 - next_r;
951fd09b 7588 return 1;
411e1bfb
AM
7589}
7590
3b421ab3
AM
7591/* Find (or create) an entry in the tocsave hash table. */
7592
7593static struct tocsave_entry *
7594tocsave_find (struct ppc_link_hash_table *htab,
7595 enum insert_option insert,
7596 Elf_Internal_Sym **local_syms,
7597 const Elf_Internal_Rela *irela,
7598 bfd *ibfd)
7599{
7600 unsigned long r_indx;
7601 struct elf_link_hash_entry *h;
7602 Elf_Internal_Sym *sym;
7603 struct tocsave_entry ent, *p;
7604 hashval_t hash;
7605 struct tocsave_entry **slot;
7606
7607 r_indx = ELF64_R_SYM (irela->r_info);
7608 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7609 return NULL;
7610 if (ent.sec == NULL || ent.sec->output_section == NULL)
7611 {
4eca0228 7612 _bfd_error_handler
3b421ab3
AM
7613 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7614 return NULL;
7615 }
7616
7617 if (h != NULL)
7618 ent.offset = h->root.u.def.value;
7619 else
7620 ent.offset = sym->st_value;
7621 ent.offset += irela->r_addend;
7622
7623 hash = tocsave_htab_hash (&ent);
7624 slot = ((struct tocsave_entry **)
7625 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7626 if (slot == NULL)
7627 return NULL;
7628
7629 if (*slot == NULL)
7630 {
7631 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7632 if (p == NULL)
7633 return NULL;
7634 *p = ent;
7635 *slot = p;
7636 }
7637 return *slot;
7638}
7639
754021d0 7640/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7641 code for the old ABI, these will already have been done. */
754021d0
AM
7642
7643static bfd_boolean
7644adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7645{
7646 struct ppc_link_hash_entry *eh;
7647 asection *sym_sec;
74f0fb50 7648 struct _opd_sec_data *opd;
754021d0
AM
7649
7650 if (h->root.type == bfd_link_hash_indirect)
7651 return TRUE;
7652
754021d0
AM
7653 if (h->root.type != bfd_link_hash_defined
7654 && h->root.type != bfd_link_hash_defweak)
7655 return TRUE;
7656
7657 eh = (struct ppc_link_hash_entry *) h;
7658 if (eh->adjust_done)
7659 return TRUE;
7660
7661 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7662 opd = get_opd_info (sym_sec);
7663 if (opd != NULL && opd->adjust != NULL)
754021d0 7664 {
51aecdc5 7665 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7666 if (adjust == -1)
7667 {
7668 /* This entry has been deleted. */
b3fac117 7669 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7670 if (dsec == NULL)
7671 {
7672 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7673 if (discarded_section (dsec))
81688140 7674 {
b3fac117 7675 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7676 break;
7677 }
7678 }
4025353c 7679 eh->elf.root.u.def.value = 0;
81688140 7680 eh->elf.root.u.def.section = dsec;
4025353c
AM
7681 }
7682 else
7683 eh->elf.root.u.def.value += adjust;
754021d0
AM
7684 eh->adjust_done = 1;
7685 }
7686 return TRUE;
7687}
7688
8c1d1bb8 7689/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7690 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7691 have already been determined. */
7692
7693static bfd_boolean
7694dec_dynrel_count (bfd_vma r_info,
7695 asection *sec,
7696 struct bfd_link_info *info,
7697 Elf_Internal_Sym **local_syms,
7698 struct elf_link_hash_entry *h,
19e08130 7699 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7700{
7701 enum elf_ppc64_reloc_type r_type;
19e08130 7702 asection *sym_sec = NULL;
8c1d1bb8
AM
7703
7704 /* Can this reloc be dynamic? This switch, and later tests here
7705 should be kept in sync with the code in check_relocs. */
7706 r_type = ELF64_R_TYPE (r_info);
7707 switch (r_type)
7708 {
7709 default:
7710 return TRUE;
7711
7712 case R_PPC64_TPREL16:
7713 case R_PPC64_TPREL16_LO:
7714 case R_PPC64_TPREL16_HI:
7715 case R_PPC64_TPREL16_HA:
7716 case R_PPC64_TPREL16_DS:
7717 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7718 case R_PPC64_TPREL16_HIGH:
7719 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7720 case R_PPC64_TPREL16_HIGHER:
7721 case R_PPC64_TPREL16_HIGHERA:
7722 case R_PPC64_TPREL16_HIGHEST:
7723 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 7724 if (!bfd_link_pic (info))
8c1d1bb8
AM
7725 return TRUE;
7726
7727 case R_PPC64_TPREL64:
7728 case R_PPC64_DTPMOD64:
7729 case R_PPC64_DTPREL64:
7730 case R_PPC64_ADDR64:
7731 case R_PPC64_REL30:
7732 case R_PPC64_REL32:
7733 case R_PPC64_REL64:
7734 case R_PPC64_ADDR14:
7735 case R_PPC64_ADDR14_BRNTAKEN:
7736 case R_PPC64_ADDR14_BRTAKEN:
7737 case R_PPC64_ADDR16:
7738 case R_PPC64_ADDR16_DS:
7739 case R_PPC64_ADDR16_HA:
7740 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7741 case R_PPC64_ADDR16_HIGH:
7742 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7743 case R_PPC64_ADDR16_HIGHER:
7744 case R_PPC64_ADDR16_HIGHERA:
7745 case R_PPC64_ADDR16_HIGHEST:
7746 case R_PPC64_ADDR16_HIGHESTA:
7747 case R_PPC64_ADDR16_LO:
7748 case R_PPC64_ADDR16_LO_DS:
7749 case R_PPC64_ADDR24:
7750 case R_PPC64_ADDR32:
7751 case R_PPC64_UADDR16:
7752 case R_PPC64_UADDR32:
7753 case R_PPC64_UADDR64:
7754 case R_PPC64_TOC:
7755 break;
7756 }
7757
7758 if (local_syms != NULL)
7759 {
7760 unsigned long r_symndx;
8c1d1bb8
AM
7761 bfd *ibfd = sec->owner;
7762
7763 r_symndx = ELF64_R_SYM (r_info);
7764 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7765 return FALSE;
7766 }
7767
0e1862bb 7768 if ((bfd_link_pic (info)
1d483afe 7769 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7770 || (h != NULL
198f1157 7771 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7772 || h->root.type == bfd_link_hash_defweak
7773 || !h->def_regular))))
7774 || (ELIMINATE_COPY_RELOCS
0e1862bb 7775 && !bfd_link_pic (info)
8c1d1bb8
AM
7776 && h != NULL
7777 && (h->root.type == bfd_link_hash_defweak
7778 || !h->def_regular)))
7779 ;
7780 else
7781 return TRUE;
7782
7783 if (h != NULL)
6edfbbad 7784 {
19e08130
AM
7785 struct elf_dyn_relocs *p;
7786 struct elf_dyn_relocs **pp;
7787 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7788
7789 /* elf_gc_sweep may have already removed all dyn relocs associated
7790 with local syms for a given section. Also, symbol flags are
7791 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7792 report a dynreloc miscount. */
7793 if (*pp == NULL && info->gc_sections)
7794 return TRUE;
7795
7796 while ((p = *pp) != NULL)
60124e18 7797 {
19e08130
AM
7798 if (p->sec == sec)
7799 {
7800 if (!must_be_dyn_reloc (info, r_type))
7801 p->pc_count -= 1;
7802 p->count -= 1;
7803 if (p->count == 0)
7804 *pp = p->next;
7805 return TRUE;
7806 }
7807 pp = &p->next;
60124e18 7808 }
6edfbbad 7809 }
19e08130
AM
7810 else
7811 {
7812 struct ppc_dyn_relocs *p;
7813 struct ppc_dyn_relocs **pp;
7814 void *vpp;
7815 bfd_boolean is_ifunc;
8c1d1bb8 7816
19e08130
AM
7817 if (local_syms == NULL)
7818 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7819 if (sym_sec == NULL)
7820 sym_sec = sec;
c57da1a7 7821
19e08130
AM
7822 vpp = &elf_section_data (sym_sec)->local_dynrel;
7823 pp = (struct ppc_dyn_relocs **) vpp;
7824
7825 if (*pp == NULL && info->gc_sections)
7826 return TRUE;
7827
7828 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7829 while ((p = *pp) != NULL)
8c1d1bb8 7830 {
19e08130
AM
7831 if (p->sec == sec && p->ifunc == is_ifunc)
7832 {
7833 p->count -= 1;
7834 if (p->count == 0)
7835 *pp = p->next;
7836 return TRUE;
7837 }
7838 pp = &p->next;
8c1d1bb8 7839 }
8c1d1bb8
AM
7840 }
7841
695344c0 7842 /* xgettext:c-format */
8de848d8 7843 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7844 sec->owner, sec);
8c1d1bb8
AM
7845 bfd_set_error (bfd_error_bad_value);
7846 return FALSE;
7847}
7848
754021d0
AM
7849/* Remove unused Official Procedure Descriptor entries. Currently we
7850 only remove those associated with functions in discarded link-once
7851 sections, or weakly defined functions that have been overridden. It
7852 would be possible to remove many more entries for statically linked
7853 applications. */
7854
b34976b6 7855bfd_boolean
e7d1c40c 7856ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7857{
7858 bfd *ibfd;
754021d0 7859 bfd_boolean some_edited = FALSE;
3f764659 7860 asection *need_pad = NULL;
e7d1c40c
AM
7861 struct ppc_link_hash_table *htab;
7862
7863 htab = ppc_hash_table (info);
7864 if (htab == NULL)
7865 return FALSE;
1e2f5b6e 7866
c72f2fb2 7867 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7868 {
7869 asection *sec;
7870 Elf_Internal_Rela *relstart, *rel, *relend;
7871 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7872 Elf_Internal_Sym *local_syms;
74f0fb50 7873 struct _opd_sec_data *opd;
51aecdc5 7874 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7875 bfd_size_type cnt_16b = 0;
1e2f5b6e 7876
854b41e7
AM
7877 if (!is_ppc64_elf (ibfd))
7878 continue;
7879
1e2f5b6e 7880 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7881 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7882 continue;
7883
dbaa2011 7884 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7885 continue;
7886
1e2f5b6e
AM
7887 if (sec->output_section == bfd_abs_section_ptr)
7888 continue;
7889
7890 /* Look through the section relocs. */
7891 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7892 continue;
7893
6cdc0ccc 7894 local_syms = NULL;
0ffa91dd 7895 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7896
7897 /* Read the relocations. */
4ce794b7 7898 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7899 info->keep_memory);
1e2f5b6e 7900 if (relstart == NULL)
b34976b6 7901 return FALSE;
1e2f5b6e
AM
7902
7903 /* First run through the relocs to check they are sane, and to
7904 determine whether we need to edit this opd section. */
b34976b6 7905 need_edit = FALSE;
51aecdc5 7906 broken = FALSE;
3f764659 7907 need_pad = sec;
1e2f5b6e 7908 relend = relstart + sec->reloc_count;
50bc7936 7909 for (rel = relstart; rel < relend; )
1e2f5b6e 7910 {
04c9666a 7911 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7912 unsigned long r_symndx;
7913 asection *sym_sec;
7914 struct elf_link_hash_entry *h;
7915 Elf_Internal_Sym *sym;
51aecdc5 7916 bfd_vma offset;
1e2f5b6e 7917
51aecdc5 7918 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7919 only interested in the reloc pointing to a function entry
7920 point. */
51aecdc5
AM
7921 offset = rel->r_offset;
7922 if (rel + 1 == relend
7923 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7924 {
7925 /* If someone messes with .opd alignment then after a
7926 "ld -r" we might have padding in the middle of .opd.
7927 Also, there's nothing to prevent someone putting
7928 something silly in .opd with the assembler. No .opd
b34976b6 7929 optimization for them! */
3f764659 7930 broken_opd:
4eca0228 7931 _bfd_error_handler
d003868e 7932 (_("%B: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7933 broken = TRUE;
1e2f5b6e
AM
7934 break;
7935 }
7936
50bc7936
AM
7937 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7938 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7939 {
4eca0228 7940 _bfd_error_handler
695344c0 7941 /* xgettext:c-format */
d003868e
AM
7942 (_("%B: unexpected reloc type %u in .opd section"),
7943 ibfd, r_type);
51aecdc5 7944 broken = TRUE;
50bc7936
AM
7945 break;
7946 }
7947
1e2f5b6e 7948 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7949 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7950 r_symndx, ibfd))
50bc7936 7951 goto error_ret;
1e2f5b6e
AM
7952
7953 if (sym_sec == NULL || sym_sec->owner == NULL)
7954 {
411e1bfb
AM
7955 const char *sym_name;
7956 if (h != NULL)
7957 sym_name = h->root.root.string;
7958 else
26c61ae5
L
7959 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7960 sym_sec);
411e1bfb 7961
4eca0228 7962 _bfd_error_handler
695344c0 7963 /* xgettext:c-format */
d003868e
AM
7964 (_("%B: undefined sym `%s' in .opd section"),
7965 ibfd, sym_name);
51aecdc5 7966 broken = TRUE;
1e2f5b6e
AM
7967 break;
7968 }
7969
51020317
AM
7970 /* opd entries are always for functions defined in the
7971 current input bfd. If the symbol isn't defined in the
7972 input bfd, then we won't be using the function in this
7973 bfd; It must be defined in a linkonce section in another
7974 bfd, or is weak. It's also possible that we are
7975 discarding the function due to a linker script /DISCARD/,
7976 which we test for via the output_section. */
7977 if (sym_sec->owner != ibfd
7978 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7979 need_edit = TRUE;
1e2f5b6e 7980
50bc7936 7981 rel += 2;
51aecdc5
AM
7982 if (rel + 1 == relend
7983 || (rel + 2 < relend
7984 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7985 ++rel;
7986
7987 if (rel == relend)
3f764659
JJ
7988 {
7989 if (sec->size == offset + 24)
7990 {
7991 need_pad = NULL;
7992 break;
7993 }
51aecdc5 7994 if (sec->size == offset + 16)
3f764659
JJ
7995 {
7996 cnt_16b++;
7997 break;
7998 }
7999 goto broken_opd;
8000 }
3f764659
JJ
8001 else if (rel + 1 < relend
8002 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8003 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8004 {
51aecdc5
AM
8005 if (rel[0].r_offset == offset + 16)
8006 cnt_16b++;
8007 else if (rel[0].r_offset != offset + 24)
8008 goto broken_opd;
3f764659
JJ
8009 }
8010 else
8011 goto broken_opd;
1e2f5b6e
AM
8012 }
8013
e7d1c40c 8014 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 8015
51aecdc5 8016 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
8017 {
8018 Elf_Internal_Rela *write_rel;
d4730f92 8019 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 8020 bfd_byte *rptr, *wptr;
983bddc8 8021 bfd_byte *new_contents;
74f0fb50
AM
8022 bfd_size_type amt;
8023
983bddc8 8024 new_contents = NULL;
51aecdc5 8025 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 8026 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 8027 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
8028 if (opd->adjust == NULL)
8029 return FALSE;
8030 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
8031
8032 /* This seems a waste of time as input .opd sections are all
8033 zeros as generated by gcc, but I suppose there's no reason
8034 this will always be so. We might start putting something in
8035 the third word of .opd entries. */
8036 if ((sec->flags & SEC_IN_MEMORY) == 0)
8037 {
eea6121a
AM
8038 bfd_byte *loc;
8039 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 8040 {
eea6121a
AM
8041 if (loc != NULL)
8042 free (loc);
50bc7936 8043 error_ret:
6cdc0ccc
AM
8044 if (local_syms != NULL
8045 && symtab_hdr->contents != (unsigned char *) local_syms)
8046 free (local_syms);
6cdc0ccc
AM
8047 if (elf_section_data (sec)->relocs != relstart)
8048 free (relstart);
b34976b6 8049 return FALSE;
6cdc0ccc 8050 }
1e2f5b6e
AM
8051 sec->contents = loc;
8052 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8053 }
8054
8055 elf_section_data (sec)->relocs = relstart;
8056
3f764659 8057 new_contents = sec->contents;
3f764659
JJ
8058 if (add_aux_fields)
8059 {
8060 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8061 if (new_contents == NULL)
8062 return FALSE;
51aecdc5 8063 need_pad = NULL;
3f764659 8064 }
b4f4e59f
AM
8065 wptr = new_contents;
8066 rptr = sec->contents;
1e2f5b6e 8067 write_rel = relstart;
51aecdc5 8068 for (rel = relstart; rel < relend; )
1e2f5b6e 8069 {
50bc7936
AM
8070 unsigned long r_symndx;
8071 asection *sym_sec;
8072 struct elf_link_hash_entry *h;
51aecdc5 8073 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 8074 Elf_Internal_Sym *sym;
51aecdc5
AM
8075 long opd_ent_size;
8076 Elf_Internal_Rela *next_rel;
8077 bfd_boolean skip;
50bc7936
AM
8078
8079 r_symndx = ELF64_R_SYM (rel->r_info);
8080 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 8081 r_symndx, ibfd))
50bc7936
AM
8082 goto error_ret;
8083
51aecdc5
AM
8084 next_rel = rel + 2;
8085 if (next_rel + 1 == relend
8086 || (next_rel + 2 < relend
8087 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8088 ++next_rel;
8089
8090 /* See if the .opd entry is full 24 byte or
8091 16 byte (with fd_aux entry overlapped with next
8092 fd_func). */
8093 opd_ent_size = 24;
8094 if (next_rel == relend)
1e2f5b6e 8095 {
51aecdc5 8096 if (sec->size == rel->r_offset + 16)
3f764659 8097 opd_ent_size = 16;
51aecdc5
AM
8098 }
8099 else if (next_rel->r_offset == rel->r_offset + 16)
8100 opd_ent_size = 16;
3f764659 8101
51aecdc5
AM
8102 if (h != NULL
8103 && h->root.root.string[0] == '.')
8104 {
8c5b4e52
AM
8105 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8106 if (fdh != NULL)
8107 {
8108 fdh = ppc_follow_link (fdh);
8109 if (fdh->elf.root.type != bfd_link_hash_defined
8110 && fdh->elf.root.type != bfd_link_hash_defweak)
8111 fdh = NULL;
8112 }
51aecdc5 8113 }
1e2f5b6e 8114
51aecdc5
AM
8115 skip = (sym_sec->owner != ibfd
8116 || sym_sec->output_section == bfd_abs_section_ptr);
8117 if (skip)
8118 {
8119 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8120 {
51aecdc5
AM
8121 /* Arrange for the function descriptor sym
8122 to be dropped. */
8123 fdh->elf.root.u.def.value = 0;
8124 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8125 }
51aecdc5 8126 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8127
0e1862bb 8128 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8129 rel = next_rel;
8130 else
8131 while (1)
8132 {
8133 if (!dec_dynrel_count (rel->r_info, sec, info,
8134 NULL, h, sym))
8135 goto error_ret;
754021d0 8136
51aecdc5
AM
8137 if (++rel == next_rel)
8138 break;
1e2f5b6e 8139
51aecdc5
AM
8140 r_symndx = ELF64_R_SYM (rel->r_info);
8141 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8142 r_symndx, ibfd))
8143 goto error_ret;
8144 }
50bc7936
AM
8145 }
8146 else
1e2f5b6e 8147 {
51aecdc5
AM
8148 /* We'll be keeping this opd entry. */
8149 long adjust;
8150
8151 if (fdh != NULL)
8152 {
8153 /* Redefine the function descriptor symbol to
8154 this location in the opd section. It is
8155 necessary to update the value here rather
8156 than using an array of adjustments as we do
8157 for local symbols, because various places
8158 in the generic ELF code use the value
8159 stored in u.def.value. */
8160 fdh->elf.root.u.def.value = wptr - new_contents;
8161 fdh->adjust_done = 1;
8162 }
8163
8164 /* Local syms are a bit tricky. We could
8165 tweak them as they can be cached, but
8166 we'd need to look through the local syms
8167 for the function descriptor sym which we
8168 don't have at the moment. So keep an
8169 array of adjustments. */
8170 adjust = (wptr - new_contents) - (rptr - sec->contents);
8171 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8172
8173 if (wptr != rptr)
8174 memcpy (wptr, rptr, opd_ent_size);
8175 wptr += opd_ent_size;
8176 if (add_aux_fields && opd_ent_size == 16)
8177 {
8178 memset (wptr, '\0', 8);
8179 wptr += 8;
8180 }
8181
50bc7936 8182 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8183 new opd entries. */
8184 for ( ; rel != next_rel; ++rel)
8185 {
8186 rel->r_offset += adjust;
8187 if (write_rel != rel)
8188 memcpy (write_rel, rel, sizeof (*rel));
8189 ++write_rel;
8190 }
1e2f5b6e 8191 }
51aecdc5
AM
8192
8193 rptr += opd_ent_size;
1e2f5b6e
AM
8194 }
8195
3f764659 8196 sec->size = wptr - new_contents;
1e2f5b6e 8197 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8198 if (add_aux_fields)
8199 {
8200 free (sec->contents);
8201 sec->contents = new_contents;
8202 }
8203
05bf9422 8204 /* Fudge the header size too, as this is used later in
cdcf6e38 8205 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8206 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8207 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8208 some_edited = TRUE;
1e2f5b6e 8209 }
6cdc0ccc 8210 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8211 free (relstart);
6cdc0ccc 8212
411e1bfb
AM
8213 if (local_syms != NULL
8214 && symtab_hdr->contents != (unsigned char *) local_syms)
8215 {
8216 if (!info->keep_memory)
8217 free (local_syms);
8218 else
8219 symtab_hdr->contents = (unsigned char *) local_syms;
8220 }
8221 }
8222
754021d0
AM
8223 if (some_edited)
8224 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8225
3f764659
JJ
8226 /* If we are doing a final link and the last .opd entry is just 16 byte
8227 long, add a 8 byte padding after it. */
0e1862bb 8228 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8229 {
8230 bfd_byte *p;
8231
8232 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8233 {
8234 BFD_ASSERT (need_pad->size > 0);
8235
8236 p = bfd_malloc (need_pad->size + 8);
8237 if (p == NULL)
8238 return FALSE;
699733f6 8239
3f764659
JJ
8240 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8241 p, 0, need_pad->size))
8242 return FALSE;
8243
8244 need_pad->contents = p;
8245 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8246 }
8247 else
8248 {
8249 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8250 if (p == NULL)
8251 return FALSE;
8252
8253 need_pad->contents = p;
8254 }
8255
8256 memset (need_pad->contents + need_pad->size, 0, 8);
8257 need_pad->size += 8;
8258 }
8259
411e1bfb
AM
8260 return TRUE;
8261}
8262
e1918d23 8263/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8264
e1918d23 8265asection *
e7d1c40c 8266ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8267{
411e1bfb
AM
8268 struct ppc_link_hash_table *htab;
8269
411e1bfb 8270 htab = ppc_hash_table (info);
4dfe6ac6
NC
8271 if (htab == NULL)
8272 return NULL;
8273
ee67d69a
AM
8274 if (abiversion (info->output_bfd) == 1)
8275 htab->opd_abi = 1;
8276
e7d1c40c 8277 if (htab->params->no_multi_toc)
33c0ec9d
AM
8278 htab->do_multi_toc = 0;
8279 else if (!htab->do_multi_toc)
e7d1c40c 8280 htab->params->no_multi_toc = 1;
33c0ec9d 8281
3a71aa26
AM
8282 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8283 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8284 FALSE, FALSE, TRUE));
a7f2871e
AM
8285 /* Move dynamic linking info to the function descriptor sym. */
8286 if (htab->tls_get_addr != NULL)
8287 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8288 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8289 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8290 FALSE, FALSE, TRUE));
7c9cf415 8291 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8292 {
8293 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8294
8295 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8296 FALSE, FALSE, TRUE);
8297 if (opt != NULL)
8298 func_desc_adjust (opt, info);
8299 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8300 FALSE, FALSE, TRUE);
8301 if (opt_fd != NULL
8302 && (opt_fd->root.type == bfd_link_hash_defined
8303 || opt_fd->root.type == bfd_link_hash_defweak))
8304 {
8305 /* If glibc supports an optimized __tls_get_addr call stub,
8306 signalled by the presence of __tls_get_addr_opt, and we'll
8307 be calling __tls_get_addr via a plt call stub, then
8308 make __tls_get_addr point to __tls_get_addr_opt. */
8309 tga_fd = &htab->tls_get_addr_fd->elf;
8310 if (htab->elf.dynamic_sections_created
8311 && tga_fd != NULL
8312 && (tga_fd->type == STT_FUNC
8313 || tga_fd->needs_plt)
8314 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8315 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8316 && tga_fd->root.type == bfd_link_hash_undefweak)))
8317 {
8318 struct plt_entry *ent;
8319
8320 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8321 if (ent->plt.refcount > 0)
8322 break;
8323 if (ent != NULL)
8324 {
8325 tga_fd->root.type = bfd_link_hash_indirect;
8326 tga_fd->root.u.i.link = &opt_fd->root;
8327 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 8328 opt_fd->mark = 1;
a7f2871e
AM
8329 if (opt_fd->dynindx != -1)
8330 {
8331 /* Use __tls_get_addr_opt in dynamic relocations. */
8332 opt_fd->dynindx = -1;
8333 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8334 opt_fd->dynstr_index);
8335 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8336 return NULL;
a7f2871e
AM
8337 }
8338 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8339 tga = &htab->tls_get_addr->elf;
8340 if (opt != NULL && tga != NULL)
8341 {
8342 tga->root.type = bfd_link_hash_indirect;
8343 tga->root.u.i.link = &opt->root;
8344 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 8345 opt->mark = 1;
a7f2871e
AM
8346 _bfd_elf_link_hash_hide_symbol (info, opt,
8347 tga->forced_local);
8348 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8349 }
8350 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8351 htab->tls_get_addr_fd->is_func_descriptor = 1;
8352 if (htab->tls_get_addr != NULL)
8353 {
8354 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8355 htab->tls_get_addr->is_func = 1;
8356 }
8357 }
8358 }
8359 }
7c9cf415
AM
8360 else if (htab->params->tls_get_addr_opt < 0)
8361 htab->params->tls_get_addr_opt = 0;
a7f2871e 8362 }
33c0ec9d 8363 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8364}
8387904d 8365
3a71aa26
AM
8366/* Return TRUE iff REL is a branch reloc with a global symbol matching
8367 HASH1 or HASH2. */
8387904d 8368
3a71aa26
AM
8369static bfd_boolean
8370branch_reloc_hash_match (const bfd *ibfd,
8371 const Elf_Internal_Rela *rel,
8372 const struct ppc_link_hash_entry *hash1,
8373 const struct ppc_link_hash_entry *hash2)
8374{
8375 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8376 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8377 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8378
e054468f 8379 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8380 {
3a71aa26
AM
8381 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8382 struct elf_link_hash_entry *h;
8387904d 8383
3a71aa26 8384 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8385 h = elf_follow_link (h);
3a71aa26
AM
8386 if (h == &hash1->elf || h == &hash2->elf)
8387 return TRUE;
a48ebf4d 8388 }
3a71aa26 8389 return FALSE;
951fd09b 8390}
411e1bfb 8391
951fd09b
AM
8392/* Run through all the TLS relocs looking for optimization
8393 opportunities. The linker has been hacked (see ppc64elf.em) to do
8394 a preliminary section layout so that we know the TLS segment
8395 offsets. We can't optimize earlier because some optimizations need
8396 to know the tp offset, and we need to optimize before allocating
8397 dynamic relocations. */
8398
8399bfd_boolean
33c0ec9d 8400ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8401{
8402 bfd *ibfd;
8403 asection *sec;
8404 struct ppc_link_hash_table *htab;
663a1470 8405 unsigned char *toc_ref;
102890f0 8406 int pass;
951fd09b 8407
3cbc1e5e 8408 if (!bfd_link_executable (info))
411e1bfb
AM
8409 return TRUE;
8410
951fd09b 8411 htab = ppc_hash_table (info);
4dfe6ac6
NC
8412 if (htab == NULL)
8413 return FALSE;
8414
663a1470
AM
8415 /* Make two passes over the relocs. On the first pass, mark toc
8416 entries involved with tls relocs, and check that tls relocs
8417 involved in setting up a tls_get_addr call are indeed followed by
8418 such a call. If they are not, we can't do any tls optimization.
8419 On the second pass twiddle tls_mask flags to notify
8420 relocate_section that optimization can be done, and adjust got
8421 and plt refcounts. */
8422 toc_ref = NULL;
8423 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8424 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8425 {
8426 Elf_Internal_Sym *locsyms = NULL;
8427 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8428
102890f0
AM
8429 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8430 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8431 {
8432 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8433 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8434
102890f0
AM
8435 /* Read the relocations. */
8436 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8437 info->keep_memory);
8438 if (relstart == NULL)
2915c55b
JK
8439 {
8440 free (toc_ref);
8441 return FALSE;
8442 }
411e1bfb 8443
102890f0
AM
8444 relend = relstart + sec->reloc_count;
8445 for (rel = relstart; rel < relend; rel++)
8446 {
8447 enum elf_ppc64_reloc_type r_type;
8448 unsigned long r_symndx;
8449 struct elf_link_hash_entry *h;
8450 Elf_Internal_Sym *sym;
8451 asection *sym_sec;
f961d9dd
AM
8452 unsigned char *tls_mask;
8453 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8454 bfd_vma value;
8455 bfd_boolean ok_tprel, is_local;
8456 long toc_ref_index = 0;
8457 int expecting_tls_get_addr = 0;
663a1470 8458 bfd_boolean ret = FALSE;
411e1bfb 8459
102890f0
AM
8460 r_symndx = ELF64_R_SYM (rel->r_info);
8461 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8462 r_symndx, ibfd))
8463 {
8464 err_free_rel:
8465 if (elf_section_data (sec)->relocs != relstart)
8466 free (relstart);
8467 if (toc_ref != NULL)
8468 free (toc_ref);
8469 if (locsyms != NULL
0ffa91dd 8470 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8471 != (unsigned char *) locsyms))
8472 free (locsyms);
663a1470 8473 return ret;
102890f0 8474 }
411e1bfb 8475
102890f0
AM
8476 if (h != NULL)
8477 {
766bc656
AM
8478 if (h->root.type == bfd_link_hash_defined
8479 || h->root.type == bfd_link_hash_defweak)
8480 value = h->root.u.def.value;
8481 else if (h->root.type == bfd_link_hash_undefweak)
8482 value = 0;
8483 else
663a1470
AM
8484 {
8485 found_tls_get_addr_arg = 0;
8486 continue;
8487 }
102890f0
AM
8488 }
8489 else
8490 /* Symbols referenced by TLS relocs must be of type
8491 STT_TLS. So no need for .opd local sym adjust. */
8492 value = sym->st_value;
8493
8494 ok_tprel = FALSE;
8495 is_local = FALSE;
8496 if (h == NULL
8497 || !h->def_dynamic)
8498 {
8499 is_local = TRUE;
766bc656
AM
8500 if (h != NULL
8501 && h->root.type == bfd_link_hash_undefweak)
8502 ok_tprel = TRUE;
c27b8c2a
AM
8503 else if (sym_sec != NULL
8504 && sym_sec->output_section != NULL)
766bc656
AM
8505 {
8506 value += sym_sec->output_offset;
8507 value += sym_sec->output_section->vma;
8508 value -= htab->elf.tls_sec->vma;
8509 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8510 < (bfd_vma) 1 << 32);
8511 }
102890f0 8512 }
951fd09b 8513
102890f0 8514 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8515 /* If this section has old-style __tls_get_addr calls
8516 without marker relocs, then check that each
8517 __tls_get_addr call reloc is preceded by a reloc
8518 that conceivably belongs to the __tls_get_addr arg
8519 setup insn. If we don't find matching arg setup
8520 relocs, don't do any tls optimization. */
8521 if (pass == 0
8522 && sec->has_tls_get_addr_call
8523 && h != NULL
8524 && (h == &htab->tls_get_addr->elf
8525 || h == &htab->tls_get_addr_fd->elf)
8526 && !found_tls_get_addr_arg
8527 && is_branch_reloc (r_type))
8528 {
25f53a85 8529 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8530 "TLS optimization disabled\n"),
8531 ibfd, sec, rel->r_offset);
8532 ret = TRUE;
8533 goto err_free_rel;
8534 }
8535
8536 found_tls_get_addr_arg = 0;
102890f0
AM
8537 switch (r_type)
8538 {
8539 case R_PPC64_GOT_TLSLD16:
8540 case R_PPC64_GOT_TLSLD16_LO:
8541 expecting_tls_get_addr = 1;
663a1470 8542 found_tls_get_addr_arg = 1;
1a0670f3 8543 /* Fall through. */
102890f0
AM
8544
8545 case R_PPC64_GOT_TLSLD16_HI:
8546 case R_PPC64_GOT_TLSLD16_HA:
8547 /* These relocs should never be against a symbol
8548 defined in a shared lib. Leave them alone if
8549 that turns out to be the case. */
8550 if (!is_local)
8551 continue;
411e1bfb 8552
102890f0 8553 /* LD -> LE */
411e1bfb 8554 tls_set = 0;
102890f0
AM
8555 tls_clear = TLS_LD;
8556 tls_type = TLS_TLS | TLS_LD;
8557 break;
411e1bfb 8558
102890f0
AM
8559 case R_PPC64_GOT_TLSGD16:
8560 case R_PPC64_GOT_TLSGD16_LO:
8561 expecting_tls_get_addr = 1;
663a1470 8562 found_tls_get_addr_arg = 1;
1a0670f3 8563 /* Fall through. */
102890f0
AM
8564
8565 case R_PPC64_GOT_TLSGD16_HI:
8566 case R_PPC64_GOT_TLSGD16_HA:
8567 if (ok_tprel)
8568 /* GD -> LE */
411e1bfb 8569 tls_set = 0;
102890f0
AM
8570 else
8571 /* GD -> IE */
8572 tls_set = TLS_TLS | TLS_TPRELGD;
8573 tls_clear = TLS_GD;
8574 tls_type = TLS_TLS | TLS_GD;
8575 break;
8576
8577 case R_PPC64_GOT_TPREL16_DS:
8578 case R_PPC64_GOT_TPREL16_LO_DS:
8579 case R_PPC64_GOT_TPREL16_HI:
8580 case R_PPC64_GOT_TPREL16_HA:
8581 if (ok_tprel)
8582 {
8583 /* IE -> LE */
8584 tls_set = 0;
8585 tls_clear = TLS_TPREL;
8586 tls_type = TLS_TLS | TLS_TPREL;
8587 break;
8588 }
411e1bfb
AM
8589 continue;
8590
727fc41e
AM
8591 case R_PPC64_TLSGD:
8592 case R_PPC64_TLSLD:
663a1470 8593 found_tls_get_addr_arg = 1;
1a0670f3 8594 /* Fall through. */
663a1470
AM
8595
8596 case R_PPC64_TLS:
8597 case R_PPC64_TOC16:
8598 case R_PPC64_TOC16_LO:
102890f0
AM
8599 if (sym_sec == NULL || sym_sec != toc)
8600 continue;
8601
8602 /* Mark this toc entry as referenced by a TLS
8603 code sequence. We can do that now in the
8604 case of R_PPC64_TLS, and after checking for
8605 tls_get_addr for the TOC16 relocs. */
8606 if (toc_ref == NULL)
663a1470
AM
8607 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8608 if (toc_ref == NULL)
8609 goto err_free_rel;
8610
102890f0
AM
8611 if (h != NULL)
8612 value = h->root.u.def.value;
8613 else
8614 value = sym->st_value;
8615 value += rel->r_addend;
73242275
AM
8616 if (value % 8 != 0)
8617 continue;
8618 BFD_ASSERT (value < toc->size
8619 && toc->output_offset % 8 == 0);
663a1470 8620 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8621 if (r_type == R_PPC64_TLS
8622 || r_type == R_PPC64_TLSGD
8623 || r_type == R_PPC64_TLSLD)
102890f0
AM
8624 {
8625 toc_ref[toc_ref_index] = 1;
8626 continue;
8627 }
8628
8629 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8630 continue;
8631
8632 tls_set = 0;
8633 tls_clear = 0;
8634 expecting_tls_get_addr = 2;
8635 break;
8636
8637 case R_PPC64_TPREL64:
8638 if (pass == 0
8639 || sec != toc
8640 || toc_ref == NULL
663a1470 8641 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8642 continue;
8643 if (ok_tprel)
8644 {
8645 /* IE -> LE */
8646 tls_set = TLS_EXPLICIT;
8647 tls_clear = TLS_TPREL;
8648 break;
8649 }
8650 continue;
8651
8652 case R_PPC64_DTPMOD64:
8653 if (pass == 0
8654 || sec != toc
8655 || toc_ref == NULL
663a1470 8656 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8657 continue;
8658 if (rel + 1 < relend
8659 && (rel[1].r_info
8660 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8661 && rel[1].r_offset == rel->r_offset + 8)
8662 {
8663 if (ok_tprel)
8664 /* GD -> LE */
8665 tls_set = TLS_EXPLICIT | TLS_GD;
8666 else
8667 /* GD -> IE */
8668 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8669 tls_clear = TLS_GD;
8670 }
8671 else
8672 {
8673 if (!is_local)
8674 continue;
8675
8676 /* LD -> LE */
8677 tls_set = TLS_EXPLICIT;
8678 tls_clear = TLS_LD;
8679 }
8680 break;
8681
8682 default:
8683 continue;
8684 }
8685
8686 if (pass == 0)
8687 {
727fc41e
AM
8688 if (!expecting_tls_get_addr
8689 || !sec->has_tls_get_addr_call)
102890f0
AM
8690 continue;
8691
3a71aa26
AM
8692 if (rel + 1 < relend
8693 && branch_reloc_hash_match (ibfd, rel + 1,
8694 htab->tls_get_addr,
8695 htab->tls_get_addr_fd))
102890f0 8696 {
3a71aa26 8697 if (expecting_tls_get_addr == 2)
102890f0 8698 {
3a71aa26 8699 /* Check for toc tls entries. */
f961d9dd 8700 unsigned char *toc_tls;
3a71aa26
AM
8701 int retval;
8702
8703 retval = get_tls_mask (&toc_tls, NULL, NULL,
8704 &locsyms,
8705 rel, ibfd);
8706 if (retval == 0)
8707 goto err_free_rel;
663a1470
AM
8708 if (toc_tls != NULL)
8709 {
8710 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8711 found_tls_get_addr_arg = 1;
8712 if (retval > 1)
8713 toc_ref[toc_ref_index] = 1;
8714 }
102890f0 8715 }
3a71aa26 8716 continue;
102890f0
AM
8717 }
8718
8719 if (expecting_tls_get_addr != 1)
8720 continue;
8721
8722 /* Uh oh, we didn't find the expected call. We
8723 could just mark this symbol to exclude it
8724 from tls optimization but it's safer to skip
663a1470 8725 the entire optimization. */
695344c0 8726 /* xgettext:c-format */
25f53a85 8727 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8728 "TLS optimization disabled\n"),
8729 ibfd, sec, rel->r_offset);
8730 ret = TRUE;
8731 goto err_free_rel;
102890f0
AM
8732 }
8733
85f7a9cb 8734 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8735 {
8736 struct plt_entry *ent;
8737 for (ent = htab->tls_get_addr->elf.plt.plist;
8738 ent != NULL;
8739 ent = ent->next)
8740 if (ent->addend == 0)
411e1bfb 8741 {
102890f0 8742 if (ent->plt.refcount > 0)
30038c59 8743 {
102890f0
AM
8744 ent->plt.refcount -= 1;
8745 expecting_tls_get_addr = 0;
30038c59 8746 }
102890f0 8747 break;
411e1bfb 8748 }
102890f0 8749 }
411e1bfb 8750
85f7a9cb 8751 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8752 {
8753 struct plt_entry *ent;
8754 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8755 ent != NULL;
8756 ent = ent->next)
8757 if (ent->addend == 0)
411e1bfb 8758 {
102890f0
AM
8759 if (ent->plt.refcount > 0)
8760 ent->plt.refcount -= 1;
8761 break;
411e1bfb 8762 }
102890f0 8763 }
411e1bfb 8764
102890f0 8765 if (tls_clear == 0)
30038c59
AM
8766 continue;
8767
102890f0
AM
8768 if ((tls_set & TLS_EXPLICIT) == 0)
8769 {
8770 struct got_entry *ent;
411e1bfb 8771
102890f0
AM
8772 /* Adjust got entry for this reloc. */
8773 if (h != NULL)
8774 ent = h->got.glist;
8775 else
8776 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8777
102890f0
AM
8778 for (; ent != NULL; ent = ent->next)
8779 if (ent->addend == rel->r_addend
8780 && ent->owner == ibfd
8781 && ent->tls_type == tls_type)
8782 break;
8783 if (ent == NULL)
8784 abort ();
411e1bfb 8785
102890f0
AM
8786 if (tls_set == 0)
8787 {
8788 /* We managed to get rid of a got entry. */
8789 if (ent->got.refcount > 0)
8790 ent->got.refcount -= 1;
8791 }
8792 }
8793 else
8794 {
8795 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8796 we'll lose one or two dyn relocs. */
8797 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8798 NULL, h, sym))
102890f0 8799 return FALSE;
411e1bfb 8800
102890f0
AM
8801 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8802 {
8803 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8804 NULL, h, sym))
102890f0
AM
8805 return FALSE;
8806 }
8807 }
411e1bfb 8808
102890f0
AM
8809 *tls_mask |= tls_set;
8810 *tls_mask &= ~tls_clear;
8811 }
8c1d1bb8 8812
102890f0
AM
8813 if (elf_section_data (sec)->relocs != relstart)
8814 free (relstart);
8815 }
411e1bfb 8816
663a1470
AM
8817 if (locsyms != NULL
8818 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8819 {
8820 if (!info->keep_memory)
8821 free (locsyms);
8822 else
8823 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8824 }
8825 }
411e1bfb 8826
663a1470
AM
8827 if (toc_ref != NULL)
8828 free (toc_ref);
b34976b6 8829 return TRUE;
1e2f5b6e 8830}
b34976b6 8831
c5614fa4
AM
8832/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8833 the values of any global symbols in a toc section that has been
8834 edited. Globals in toc sections should be a rarity, so this function
8835 sets a flag if any are found in toc sections other than the one just
8836 edited, so that futher hash table traversals can be avoided. */
8837
8838struct adjust_toc_info
8839{
8840 asection *toc;
8841 unsigned long *skip;
8842 bfd_boolean global_toc_syms;
8843};
8844
ba761f19
AM
8845enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8846
c5614fa4
AM
8847static bfd_boolean
8848adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8849{
8850 struct ppc_link_hash_entry *eh;
8851 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8852 unsigned long i;
c5614fa4 8853
c5614fa4
AM
8854 if (h->root.type != bfd_link_hash_defined
8855 && h->root.type != bfd_link_hash_defweak)
8856 return TRUE;
8857
8858 eh = (struct ppc_link_hash_entry *) h;
8859 if (eh->adjust_done)
8860 return TRUE;
8861
8862 if (eh->elf.root.u.def.section == toc_inf->toc)
8863 {
854b41e7
AM
8864 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8865 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8866 else
854b41e7
AM
8867 i = eh->elf.root.u.def.value >> 3;
8868
ba761f19 8869 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8870 {
4eca0228 8871 _bfd_error_handler
854b41e7
AM
8872 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8873 do
8874 ++i;
ba761f19 8875 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8876 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8877 }
854b41e7
AM
8878
8879 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8880 eh->adjust_done = 1;
8881 }
8882 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8883 toc_inf->global_toc_syms = TRUE;
8884
8885 return TRUE;
8886}
8887
39eeab25
AM
8888/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8889 on a _LO variety toc/got reloc. */
560c8763
AM
8890
8891static bfd_boolean
39eeab25 8892ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8893{
39eeab25
AM
8894 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8895 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8896 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8897 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8898 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8899 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8900 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8901 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8902 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8903 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8904 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8905 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8906 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8907 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8908 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8909 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8910 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8911 /* Exclude lfqu by testing reloc. If relocs are ever
8912 defined for the reduced D field in psq_lu then those
8913 will need testing too. */
8914 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8915 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8916 && (insn & 1) == 0)
8917 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8918 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8919 /* Exclude stfqu. psq_stu as above for psq_lu. */
8920 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8921 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8922 && (insn & 1) == 0));
560c8763
AM
8923}
8924
c5614fa4
AM
8925/* Examine all relocs referencing .toc sections in order to remove
8926 unused .toc entries. */
8927
8928bfd_boolean
33c0ec9d 8929ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8930{
8931 bfd *ibfd;
8932 struct adjust_toc_info toc_inf;
67f0cbdb 8933 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8934
67f0cbdb 8935 htab->do_toc_opt = 1;
c5614fa4 8936 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8937 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8938 {
8939 asection *toc, *sec;
8940 Elf_Internal_Shdr *symtab_hdr;
8941 Elf_Internal_Sym *local_syms;
425b145b 8942 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8943 unsigned long *skip, *drop;
8944 unsigned char *used;
8945 unsigned char *keep, last, some_unused;
8946
854b41e7
AM
8947 if (!is_ppc64_elf (ibfd))
8948 continue;
8949
c5614fa4
AM
8950 toc = bfd_get_section_by_name (ibfd, ".toc");
8951 if (toc == NULL
92b7a70f 8952 || toc->size == 0
dbaa2011
AM
8953 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8954 || discarded_section (toc))
c5614fa4
AM
8955 continue;
8956
425b145b 8957 toc_relocs = NULL;
c5614fa4 8958 local_syms = NULL;
0ffa91dd 8959 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8960
8961 /* Look at sections dropped from the final link. */
8962 skip = NULL;
8963 relstart = NULL;
8964 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8965 {
8966 if (sec->reloc_count == 0
dbaa2011 8967 || !discarded_section (sec)
c5614fa4
AM
8968 || get_opd_info (sec)
8969 || (sec->flags & SEC_ALLOC) == 0
8970 || (sec->flags & SEC_DEBUGGING) != 0)
8971 continue;
8972
8973 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8974 if (relstart == NULL)
8975 goto error_ret;
8976
8977 /* Run through the relocs to see which toc entries might be
8978 unused. */
8979 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8980 {
8981 enum elf_ppc64_reloc_type r_type;
8982 unsigned long r_symndx;
8983 asection *sym_sec;
8984 struct elf_link_hash_entry *h;
8985 Elf_Internal_Sym *sym;
8986 bfd_vma val;
8987
8988 r_type = ELF64_R_TYPE (rel->r_info);
8989 switch (r_type)
8990 {
8991 default:
8992 continue;
8993
8994 case R_PPC64_TOC16:
8995 case R_PPC64_TOC16_LO:
8996 case R_PPC64_TOC16_HI:
8997 case R_PPC64_TOC16_HA:
8998 case R_PPC64_TOC16_DS:
8999 case R_PPC64_TOC16_LO_DS:
9000 break;
9001 }
9002
9003 r_symndx = ELF64_R_SYM (rel->r_info);
9004 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9005 r_symndx, ibfd))
9006 goto error_ret;
9007
9008 if (sym_sec != toc)
9009 continue;
9010
9011 if (h != NULL)
9012 val = h->root.u.def.value;
9013 else
9014 val = sym->st_value;
9015 val += rel->r_addend;
9016
9017 if (val >= toc->size)
9018 continue;
9019
9020 /* Anything in the toc ought to be aligned to 8 bytes.
9021 If not, don't mark as unused. */
9022 if (val & 7)
9023 continue;
9024
9025 if (skip == NULL)
9026 {
854b41e7 9027 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9028 if (skip == NULL)
9029 goto error_ret;
9030 }
9031
ba761f19 9032 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9033 }
9034
9035 if (elf_section_data (sec)->relocs != relstart)
9036 free (relstart);
9037 }
9038
ba761f19
AM
9039 /* For largetoc loads of address constants, we can convert
9040 . addis rx,2,addr@got@ha
9041 . ld ry,addr@got@l(rx)
9042 to
9043 . addis rx,2,addr@toc@ha
9044 . addi ry,rx,addr@toc@l
9045 when addr is within 2G of the toc pointer. This then means
9046 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9047
ba761f19
AM
9048 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9049 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9050 && toc->reloc_count != 0)
9051 {
9052 /* Read toc relocs. */
425b145b
AM
9053 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9054 info->keep_memory);
9055 if (toc_relocs == NULL)
ba761f19
AM
9056 goto error_ret;
9057
425b145b 9058 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9059 {
9060 enum elf_ppc64_reloc_type r_type;
9061 unsigned long r_symndx;
9062 asection *sym_sec;
9063 struct elf_link_hash_entry *h;
9064 Elf_Internal_Sym *sym;
9065 bfd_vma val, addr;
9066
9067 r_type = ELF64_R_TYPE (rel->r_info);
9068 if (r_type != R_PPC64_ADDR64)
9069 continue;
9070
9071 r_symndx = ELF64_R_SYM (rel->r_info);
9072 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9073 r_symndx, ibfd))
9074 goto error_ret;
9075
425b145b 9076 if (sym_sec == NULL
c27b8c2a 9077 || sym_sec->output_section == NULL
dbaa2011 9078 || discarded_section (sym_sec))
425b145b
AM
9079 continue;
9080
afe397ea 9081 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9082 continue;
9083
9084 if (h != NULL)
bddc25c9
AM
9085 {
9086 if (h->type == STT_GNU_IFUNC)
9087 continue;
9088 val = h->root.u.def.value;
9089 }
ba761f19 9090 else
bddc25c9
AM
9091 {
9092 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9093 continue;
9094 val = sym->st_value;
9095 }
ba761f19
AM
9096 val += rel->r_addend;
9097 val += sym_sec->output_section->vma + sym_sec->output_offset;
9098
9099 /* We don't yet know the exact toc pointer value, but we
9100 know it will be somewhere in the toc section. Don't
9101 optimize if the difference from any possible toc
9102 pointer is outside [ff..f80008000, 7fff7fff]. */
9103 addr = toc->output_section->vma + TOC_BASE_OFF;
9104 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9105 continue;
9106
9107 addr = toc->output_section->vma + toc->output_section->rawsize;
9108 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9109 continue;
9110
9111 if (skip == NULL)
9112 {
9113 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9114 if (skip == NULL)
9115 goto error_ret;
9116 }
9117
9118 skip[rel->r_offset >> 3]
425b145b 9119 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9120 }
ba761f19
AM
9121 }
9122
c5614fa4
AM
9123 if (skip == NULL)
9124 continue;
9125
9126 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9127 if (used == NULL)
9128 {
9129 error_ret:
9130 if (local_syms != NULL
9131 && symtab_hdr->contents != (unsigned char *) local_syms)
9132 free (local_syms);
9133 if (sec != NULL
9134 && relstart != NULL
9135 && elf_section_data (sec)->relocs != relstart)
9136 free (relstart);
425b145b
AM
9137 if (toc_relocs != NULL
9138 && elf_section_data (toc)->relocs != toc_relocs)
9139 free (toc_relocs);
c5614fa4
AM
9140 if (skip != NULL)
9141 free (skip);
9142 return FALSE;
9143 }
9144
30038c59
AM
9145 /* Now check all kept sections that might reference the toc.
9146 Check the toc itself last. */
9147 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9148 : ibfd->sections);
c5614fa4 9149 sec != NULL;
c5614fa4 9150 sec = (sec == toc ? NULL
c5614fa4 9151 : sec->next == NULL ? toc
30038c59 9152 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9153 : sec->next))
9154 {
9155 int repeat;
9156
9157 if (sec->reloc_count == 0
dbaa2011 9158 || discarded_section (sec)
c5614fa4
AM
9159 || get_opd_info (sec)
9160 || (sec->flags & SEC_ALLOC) == 0
9161 || (sec->flags & SEC_DEBUGGING) != 0)
9162 continue;
9163
854b41e7
AM
9164 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9165 info->keep_memory);
c5614fa4 9166 if (relstart == NULL)
2915c55b
JK
9167 {
9168 free (used);
9169 goto error_ret;
9170 }
c5614fa4
AM
9171
9172 /* Mark toc entries referenced as used. */
c5614fa4 9173 do
d4f1ee75
AM
9174 {
9175 repeat = 0;
9176 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9177 {
9178 enum elf_ppc64_reloc_type r_type;
9179 unsigned long r_symndx;
9180 asection *sym_sec;
9181 struct elf_link_hash_entry *h;
9182 Elf_Internal_Sym *sym;
9183 bfd_vma val;
9184 enum {no_check, check_lo, check_ha} insn_check;
98528052 9185
d4f1ee75
AM
9186 r_type = ELF64_R_TYPE (rel->r_info);
9187 switch (r_type)
9188 {
9189 default:
9190 insn_check = no_check;
9191 break;
98528052 9192
d4f1ee75
AM
9193 case R_PPC64_GOT_TLSLD16_HA:
9194 case R_PPC64_GOT_TLSGD16_HA:
9195 case R_PPC64_GOT_TPREL16_HA:
9196 case R_PPC64_GOT_DTPREL16_HA:
9197 case R_PPC64_GOT16_HA:
9198 case R_PPC64_TOC16_HA:
9199 insn_check = check_ha;
9200 break;
98528052 9201
d4f1ee75
AM
9202 case R_PPC64_GOT_TLSLD16_LO:
9203 case R_PPC64_GOT_TLSGD16_LO:
9204 case R_PPC64_GOT_TPREL16_LO_DS:
9205 case R_PPC64_GOT_DTPREL16_LO_DS:
9206 case R_PPC64_GOT16_LO:
9207 case R_PPC64_GOT16_LO_DS:
9208 case R_PPC64_TOC16_LO:
9209 case R_PPC64_TOC16_LO_DS:
9210 insn_check = check_lo;
9211 break;
9212 }
560c8763 9213
d4f1ee75
AM
9214 if (insn_check != no_check)
9215 {
9216 bfd_vma off = rel->r_offset & ~3;
9217 unsigned char buf[4];
9218 unsigned int insn;
c5614fa4 9219
d4f1ee75
AM
9220 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9221 {
9222 free (used);
9223 goto error_ret;
9224 }
9225 insn = bfd_get_32 (ibfd, buf);
9226 if (insn_check == check_lo
39eeab25 9227 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
9228 : ((insn & ((0x3f << 26) | 0x1f << 16))
9229 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9230 {
9231 char str[12];
9232
9233 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9234 sprintf (str, "%#08x", insn);
9235 info->callbacks->einfo
695344c0 9236 /* xgettext:c-format */
174d0a74 9237 (_("%H: toc optimization is not supported for"
d4f1ee75
AM
9238 " %s instruction.\n"),
9239 ibfd, sec, rel->r_offset & ~3, str);
9240 }
9241 }
c5614fa4 9242
d4f1ee75
AM
9243 switch (r_type)
9244 {
9245 case R_PPC64_TOC16:
9246 case R_PPC64_TOC16_LO:
9247 case R_PPC64_TOC16_HI:
9248 case R_PPC64_TOC16_HA:
9249 case R_PPC64_TOC16_DS:
9250 case R_PPC64_TOC16_LO_DS:
9251 /* In case we're taking addresses of toc entries. */
9252 case R_PPC64_ADDR64:
9253 break;
c5614fa4 9254
d4f1ee75
AM
9255 default:
9256 continue;
9257 }
c5614fa4 9258
d4f1ee75
AM
9259 r_symndx = ELF64_R_SYM (rel->r_info);
9260 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9261 r_symndx, ibfd))
9262 {
9263 free (used);
9264 goto error_ret;
9265 }
c5614fa4 9266
d4f1ee75
AM
9267 if (sym_sec != toc)
9268 continue;
c5614fa4 9269
d4f1ee75
AM
9270 if (h != NULL)
9271 val = h->root.u.def.value;
9272 else
9273 val = sym->st_value;
9274 val += rel->r_addend;
ba761f19 9275
d4f1ee75
AM
9276 if (val >= toc->size)
9277 continue;
ba761f19 9278
d4f1ee75
AM
9279 if ((skip[val >> 3] & can_optimize) != 0)
9280 {
9281 bfd_vma off;
9282 unsigned char opc;
9283
9284 switch (r_type)
9285 {
9286 case R_PPC64_TOC16_HA:
ba761f19 9287 break;
ba761f19 9288
d4f1ee75
AM
9289 case R_PPC64_TOC16_LO_DS:
9290 off = rel->r_offset;
9291 off += (bfd_big_endian (ibfd) ? -2 : 3);
9292 if (!bfd_get_section_contents (ibfd, sec, &opc,
9293 off, 1))
9294 {
9295 free (used);
9296 goto error_ret;
9297 }
9298 if ((opc & (0x3f << 2)) == (58u << 2))
9299 break;
1a0670f3 9300 /* Fall through. */
ba761f19 9301
d4f1ee75
AM
9302 default:
9303 /* Wrong sort of reloc, or not a ld. We may
9304 as well clear ref_from_discarded too. */
9305 skip[val >> 3] = 0;
9306 }
9307 }
9308
9309 if (sec != toc)
9310 used[val >> 3] = 1;
9311 /* For the toc section, we only mark as used if this
9312 entry itself isn't unused. */
9313 else if ((used[rel->r_offset >> 3]
9314 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9315 && !used[val >> 3])
9316 {
9317 /* Do all the relocs again, to catch reference
9318 chains. */
9319 repeat = 1;
9320 used[val >> 3] = 1;
9321 }
9322 }
9323 }
c5614fa4 9324 while (repeat);
854b41e7
AM
9325
9326 if (elf_section_data (sec)->relocs != relstart)
9327 free (relstart);
c5614fa4
AM
9328 }
9329
9330 /* Merge the used and skip arrays. Assume that TOC
9331 doublewords not appearing as either used or unused belong
9332 to to an entry more than one doubleword in size. */
9333 for (drop = skip, keep = used, last = 0, some_unused = 0;
9334 drop < skip + (toc->size + 7) / 8;
9335 ++drop, ++keep)
9336 {
9337 if (*keep)
9338 {
ba761f19
AM
9339 *drop &= ~ref_from_discarded;
9340 if ((*drop & can_optimize) != 0)
9341 some_unused = 1;
c5614fa4
AM
9342 last = 0;
9343 }
b140b010 9344 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9345 {
9346 some_unused = 1;
ba761f19 9347 last = ref_from_discarded;
c5614fa4
AM
9348 }
9349 else
9350 *drop = last;
9351 }
9352
9353 free (used);
9354
9355 if (some_unused)
9356 {
9357 bfd_byte *contents, *src;
9358 unsigned long off;
d62b3684 9359 Elf_Internal_Sym *sym;
ba761f19 9360 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9361
9362 /* Shuffle the toc contents, and at the same time convert the
9363 skip array from booleans into offsets. */
9364 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9365 goto error_ret;
9366
9367 elf_section_data (toc)->this_hdr.contents = contents;
9368
9369 for (src = contents, off = 0, drop = skip;
9370 src < contents + toc->size;
9371 src += 8, ++drop)
9372 {
ba761f19
AM
9373 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9374 off += 8;
c5614fa4
AM
9375 else if (off != 0)
9376 {
9377 *drop = off;
9378 memcpy (src - off, src, 8);
9379 }
9380 }
854b41e7 9381 *drop = off;
c5614fa4
AM
9382 toc->rawsize = toc->size;
9383 toc->size = src - contents - off;
9384
ba761f19
AM
9385 /* Adjust addends for relocs against the toc section sym,
9386 and optimize any accesses we can. */
c5614fa4
AM
9387 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9388 {
9389 if (sec->reloc_count == 0
dbaa2011 9390 || discarded_section (sec))
c5614fa4
AM
9391 continue;
9392
9393 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9394 info->keep_memory);
c5614fa4
AM
9395 if (relstart == NULL)
9396 goto error_ret;
9397
9398 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9399 {
9400 enum elf_ppc64_reloc_type r_type;
9401 unsigned long r_symndx;
9402 asection *sym_sec;
9403 struct elf_link_hash_entry *h;
854b41e7 9404 bfd_vma val;
c5614fa4
AM
9405
9406 r_type = ELF64_R_TYPE (rel->r_info);
9407 switch (r_type)
9408 {
9409 default:
9410 continue;
9411
9412 case R_PPC64_TOC16:
9413 case R_PPC64_TOC16_LO:
9414 case R_PPC64_TOC16_HI:
9415 case R_PPC64_TOC16_HA:
9416 case R_PPC64_TOC16_DS:
9417 case R_PPC64_TOC16_LO_DS:
9418 case R_PPC64_ADDR64:
9419 break;
9420 }
9421
9422 r_symndx = ELF64_R_SYM (rel->r_info);
9423 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9424 r_symndx, ibfd))
9425 goto error_ret;
9426
ba761f19 9427 if (sym_sec != toc)
c5614fa4
AM
9428 continue;
9429
ba761f19
AM
9430 if (h != NULL)
9431 val = h->root.u.def.value;
9432 else
9433 {
9434 val = sym->st_value;
9435 if (val != 0)
9436 local_toc_syms = TRUE;
9437 }
9438
9439 val += rel->r_addend;
854b41e7
AM
9440
9441 if (val > toc->rawsize)
9442 val = toc->rawsize;
ba761f19
AM
9443 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9444 continue;
9445 else if ((skip[val >> 3] & can_optimize) != 0)
9446 {
9447 Elf_Internal_Rela *tocrel
425b145b 9448 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9449 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9450
9451 switch (r_type)
9452 {
9453 case R_PPC64_TOC16_HA:
9454 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9455 break;
9456
9457 case R_PPC64_TOC16_LO_DS:
9458 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9459 break;
9460
9461 default:
28942f62
AM
9462 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9463 ppc_howto_init ();
b140b010 9464 info->callbacks->einfo
695344c0 9465 /* xgettext:c-format */
174d0a74 9466 (_("%H: %s references "
b140b010
AM
9467 "optimized away TOC entry\n"),
9468 ibfd, sec, rel->r_offset,
9469 ppc64_elf_howto_table[r_type]->name);
9470 bfd_set_error (bfd_error_bad_value);
9471 goto error_ret;
ba761f19
AM
9472 }
9473 rel->r_addend = tocrel->r_addend;
9474 elf_section_data (sec)->relocs = relstart;
9475 continue;
9476 }
9477
9478 if (h != NULL || sym->st_value != 0)
9479 continue;
854b41e7
AM
9480
9481 rel->r_addend -= skip[val >> 3];
9482 elf_section_data (sec)->relocs = relstart;
c5614fa4 9483 }
854b41e7
AM
9484
9485 if (elf_section_data (sec)->relocs != relstart)
9486 free (relstart);
c5614fa4
AM
9487 }
9488
9489 /* We shouldn't have local or global symbols defined in the TOC,
9490 but handle them anyway. */
df22d223
AM
9491 if (local_syms != NULL)
9492 for (sym = local_syms;
9493 sym < local_syms + symtab_hdr->sh_info;
9494 ++sym)
9495 if (sym->st_value != 0
9496 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9497 {
9498 unsigned long i;
854b41e7 9499
df22d223
AM
9500 if (sym->st_value > toc->rawsize)
9501 i = toc->rawsize >> 3;
9502 else
9503 i = sym->st_value >> 3;
854b41e7 9504
df22d223
AM
9505 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9506 {
9507 if (local_toc_syms)
4eca0228 9508 _bfd_error_handler
df22d223
AM
9509 (_("%s defined on removed toc entry"),
9510 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9511 do
9512 ++i;
9513 while ((skip[i] & (ref_from_discarded | can_optimize)));
9514 sym->st_value = (bfd_vma) i << 3;
9515 }
d62b3684 9516
df22d223
AM
9517 sym->st_value -= skip[i];
9518 symtab_hdr->contents = (unsigned char *) local_syms;
9519 }
c5614fa4 9520
854b41e7 9521 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9522 if (toc_inf.global_toc_syms)
9523 {
9524 toc_inf.toc = toc;
9525 toc_inf.skip = skip;
9526 toc_inf.global_toc_syms = FALSE;
9527 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9528 &toc_inf);
9529 }
854b41e7
AM
9530
9531 if (toc->reloc_count != 0)
9532 {
d4730f92 9533 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9534 Elf_Internal_Rela *wrel;
9535 bfd_size_type sz;
9536
854b41e7 9537 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9538 if (toc_relocs == NULL)
9539 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9540 info->keep_memory);
9541 if (toc_relocs == NULL)
9542 goto error_ret;
9543
425b145b
AM
9544 wrel = toc_relocs;
9545 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9546 if ((skip[rel->r_offset >> 3]
9547 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9548 {
9549 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9550 wrel->r_info = rel->r_info;
9551 wrel->r_addend = rel->r_addend;
9552 ++wrel;
9553 }
9554 else if (!dec_dynrel_count (rel->r_info, toc, info,
9555 &local_syms, NULL, NULL))
9556 goto error_ret;
9557
425b145b
AM
9558 elf_section_data (toc)->relocs = toc_relocs;
9559 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9560 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9561 sz = rel_hdr->sh_entsize;
9562 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9563 }
c5614fa4 9564 }
28be611c
AM
9565 else if (toc_relocs != NULL
9566 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9567 free (toc_relocs);
c5614fa4
AM
9568
9569 if (local_syms != NULL
9570 && symtab_hdr->contents != (unsigned char *) local_syms)
9571 {
9572 if (!info->keep_memory)
9573 free (local_syms);
9574 else
9575 symtab_hdr->contents = (unsigned char *) local_syms;
9576 }
9577 free (skip);
9578 }
9579
9580 return TRUE;
9581}
9582
1bbe0902
AM
9583/* Return true iff input section I references the TOC using
9584 instructions limited to +/-32k offsets. */
9585
9586bfd_boolean
9587ppc64_elf_has_small_toc_reloc (asection *i)
9588{
9589 return (is_ppc64_elf (i->owner)
9590 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9591}
9592
927be08e
AM
9593/* Allocate space for one GOT entry. */
9594
9595static void
9596allocate_got (struct elf_link_hash_entry *h,
9597 struct bfd_link_info *info,
9598 struct got_entry *gent)
9599{
9600 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9601 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9602 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9603 ? 16 : 8);
9604 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9605 ? 2 : 1) * sizeof (Elf64_External_Rela);
9606 asection *got = ppc64_elf_tdata (gent->owner)->got;
9607
9608 gent->got.offset = got->size;
9609 got->size += entsize;
9610
19e08130 9611 if (h->type == STT_GNU_IFUNC)
927be08e 9612 {
33e44f2e 9613 htab->elf.irelplt->size += rentsize;
19e08130 9614 htab->got_reli_size += rentsize;
927be08e 9615 }
0e1862bb 9616 else if ((bfd_link_pic (info)
f0158f44
AM
9617 || (htab->elf.dynamic_sections_created
9618 && h->dynindx != -1
9619 && !SYMBOL_REFERENCES_LOCAL (info, h)))
19e08130
AM
9620 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9621 || h->root.type != bfd_link_hash_undefweak))
927be08e 9622 {
19e08130 9623 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9624 relgot->size += rentsize;
927be08e
AM
9625 }
9626}
9627
7865406b
AM
9628/* This function merges got entries in the same toc group. */
9629
9630static void
9631merge_got_entries (struct got_entry **pent)
9632{
9633 struct got_entry *ent, *ent2;
9634
9635 for (ent = *pent; ent != NULL; ent = ent->next)
9636 if (!ent->is_indirect)
9637 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9638 if (!ent2->is_indirect
9639 && ent2->addend == ent->addend
9640 && ent2->tls_type == ent->tls_type
9641 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9642 {
9643 ent2->is_indirect = TRUE;
9644 ent2->got.ent = ent;
9645 }
9646}
9647
f0158f44
AM
9648/* If H is undefined weak, make it dynamic if that makes sense. */
9649
9650static bfd_boolean
9651ensure_undefweak_dynamic (struct bfd_link_info *info,
9652 struct elf_link_hash_entry *h)
9653{
9654 struct elf_link_hash_table *htab = elf_hash_table (info);
9655
9656 if (htab->dynamic_sections_created
9657 && h->root.type == bfd_link_hash_undefweak
9658 && h->dynindx == -1
9659 && !h->forced_local
9660 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9661 return bfd_elf_link_record_dynamic_symbol (info, h);
9662 return TRUE;
9663}
9664
65f38f15
AM
9665/* Allocate space in .plt, .got and associated reloc sections for
9666 dynamic relocs. */
5bd4f169 9667
b34976b6 9668static bfd_boolean
4ce794b7 9669allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9670{
65f38f15
AM
9671 struct bfd_link_info *info;
9672 struct ppc_link_hash_table *htab;
5bd4f169 9673 asection *s;
65f38f15 9674 struct ppc_link_hash_entry *eh;
0b8bcf0d 9675 struct got_entry **pgent, *gent;
5bd4f169 9676
e92d460e 9677 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9678 return TRUE;
5bd4f169 9679
65f38f15
AM
9680 info = (struct bfd_link_info *) inf;
9681 htab = ppc_hash_table (info);
4dfe6ac6
NC
9682 if (htab == NULL)
9683 return FALSE;
5bd4f169 9684
951fd09b
AM
9685 eh = (struct ppc_link_hash_entry *) h;
9686 /* Run through the TLS GD got entries first if we're changing them
9687 to TPREL. */
e7b938ca 9688 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9689 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9690 if (gent->got.refcount > 0
9691 && (gent->tls_type & TLS_GD) != 0)
9692 {
9693 /* This was a GD entry that has been converted to TPREL. If
9694 there happens to be a TPREL entry we can use that one. */
9695 struct got_entry *ent;
9696 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9697 if (ent->got.refcount > 0
9698 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9699 && ent->addend == gent->addend
9700 && ent->owner == gent->owner)
951fd09b
AM
9701 {
9702 gent->got.refcount = 0;
9703 break;
9704 }
9705
9706 /* If not, then we'll be using our own TPREL entry. */
9707 if (gent->got.refcount != 0)
9708 gent->tls_type = TLS_TLS | TLS_TPREL;
9709 }
9710
7865406b
AM
9711 /* Remove any list entry that won't generate a word in the GOT before
9712 we call merge_got_entries. Otherwise we risk merging to empty
9713 entries. */
0b8bcf0d
AM
9714 pgent = &h->got.glist;
9715 while ((gent = *pgent) != NULL)
411e1bfb 9716 if (gent->got.refcount > 0)
7865406b
AM
9717 {
9718 if ((gent->tls_type & TLS_LD) != 0
9719 && !h->def_dynamic)
9720 {
9721 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9722 *pgent = gent->next;
9723 }
9724 else
9725 pgent = &gent->next;
9726 }
9727 else
9728 *pgent = gent->next;
9729
9730 if (!htab->do_multi_toc)
9731 merge_got_entries (&h->got.glist);
9732
9733 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9734 if (!gent->is_indirect)
411e1bfb
AM
9735 {
9736 /* Make sure this symbol is output as a dynamic symbol.
f0158f44
AM
9737 Undefined weak syms won't yet be marked as dynamic. */
9738 if (!ensure_undefweak_dynamic (info, h))
9739 return FALSE;
65f38f15 9740
0c8d6e5c 9741 if (!is_ppc64_elf (gent->owner))
927be08e 9742 abort ();
0ffa91dd 9743
927be08e 9744 allocate_got (h, info, gent);
411e1bfb 9745 }
65f38f15 9746
8a2058b5
AM
9747 if (!htab->elf.dynamic_sections_created
9748 && h->type != STT_GNU_IFUNC)
9749 eh->dyn_relocs = NULL;
9750
9751 if (eh->dyn_relocs != NULL)
65f38f15 9752 {
8a2058b5
AM
9753 struct elf_dyn_relocs *p, **pp;
9754
57e7d118
AM
9755 /* In the shared -Bsymbolic case, discard space allocated for
9756 dynamic pc-relative relocs against symbols which turn out to
9757 be defined in regular objects. For the normal shared case,
9758 discard space for relocs that have become local due to symbol
9759 visibility changes. */
9760
9761 if (bfd_link_pic (info))
65f38f15 9762 {
57e7d118
AM
9763 /* Relocs that use pc_count are those that appear on a call
9764 insn, or certain REL relocs (see must_be_dyn_reloc) that
9765 can be generated via assembly. We want calls to
9766 protected symbols to resolve directly to the function
9767 rather than going via the plt. If people want function
9768 pointer comparisons to work as expected then they should
9769 avoid writing weird assembly. */
9770 if (SYMBOL_CALLS_LOCAL (info, h))
9771 {
57e7d118
AM
9772 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9773 {
9774 p->count -= p->pc_count;
9775 p->pc_count = 0;
9776 if (p->count == 0)
9777 *pp = p->next;
9778 else
9779 pp = &p->next;
9780 }
9781 }
65f38f15 9782
57e7d118
AM
9783 /* Also discard relocs on undefined weak syms with
9784 non-default visibility. */
9785 if (eh->dyn_relocs != NULL
9786 && h->root.type == bfd_link_hash_undefweak)
5bd4f169 9787 {
57e7d118
AM
9788 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9789 eh->dyn_relocs = NULL;
9790
9791 /* Make sure this symbol is output as a dynamic symbol.
9792 Undefined weak syms won't yet be marked as dynamic. */
f0158f44
AM
9793 else if (!ensure_undefweak_dynamic (info, h))
9794 return FALSE;
5bd4f169 9795 }
65f38f15 9796 }
57e7d118 9797 else if (h->type == STT_GNU_IFUNC)
dfbb6ac9 9798 {
8a2058b5
AM
9799 /* A plt entry is always created when making direct calls to
9800 an ifunc, even when building a static executable, but
9801 that doesn't cover all cases. We may have only an ifunc
9802 initialised function pointer for a given ifunc symbol.
9803
9804 For ELFv2, dynamic relocations are not required when
9805 generating a global entry PLT stub. */
9806 if (abiversion (info->output_bfd) >= 2)
9807 {
9808 if (global_entry_stub (h))
9809 eh->dyn_relocs = NULL;
9810 }
9811
9812 /* For ELFv1 we have function descriptors. Descriptors need
9813 to be treated like PLT entries and thus have dynamic
9814 relocations. One exception is when the function
9815 descriptor is copied into .dynbss (which should only
9816 happen with ancient versions of gcc). */
9817 else if (h->needs_copy)
dfbb6ac9 9818 eh->dyn_relocs = NULL;
57e7d118
AM
9819 }
9820 else if (ELIMINATE_COPY_RELOCS)
9821 {
8a2058b5 9822 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9823 symbols which turn out to need copy relocs or are not
9824 dynamic. */
f0158f44
AM
9825 if (!h->non_got_ref
9826 && !h->def_regular)
9827 {
9828 /* Make sure this symbol is output as a dynamic symbol.
9829 Undefined weak syms won't yet be marked as dynamic. */
9830 if (!ensure_undefweak_dynamic (info, h))
9831 return FALSE;
dfbb6ac9 9832
f0158f44
AM
9833 if (h->dynindx == -1)
9834 eh->dyn_relocs = NULL;
9835 }
9836 else
8a2058b5 9837 eh->dyn_relocs = NULL;
57e7d118
AM
9838 }
9839
9840 /* Finally, allocate space. */
9841 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9842 {
9843 asection *sreloc = elf_section_data (p->sec)->sreloc;
9844 if (eh->elf.type == STT_GNU_IFUNC)
9845 sreloc = htab->elf.irelplt;
9846 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9847 }
65f38f15 9848 }
57e7d118
AM
9849
9850 if ((htab->elf.dynamic_sections_created
f0158f44 9851 && h->dynindx != -1)
57e7d118 9852 || h->type == STT_GNU_IFUNC)
65f38f15 9853 {
57e7d118
AM
9854 struct plt_entry *pent;
9855 bfd_boolean doneone = FALSE;
9856 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9857 if (pent->plt.refcount > 0)
9858 {
9859 if (!htab->elf.dynamic_sections_created
9860 || h->dynindx == -1)
9861 {
9862 s = htab->elf.iplt;
9863 pent->plt.offset = s->size;
9864 s->size += PLT_ENTRY_SIZE (htab);
9865 s = htab->elf.irelplt;
9866 }
9867 else
9868 {
9869 /* If this is the first .plt entry, make room for the special
9870 first entry. */
9871 s = htab->elf.splt;
9872 if (s->size == 0)
9873 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9874
57e7d118 9875 pent->plt.offset = s->size;
65f38f15 9876
57e7d118
AM
9877 /* Make room for this entry. */
9878 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9879
57e7d118
AM
9880 /* Make room for the .glink code. */
9881 s = htab->glink;
9882 if (s->size == 0)
9883 s->size += GLINK_CALL_STUB_SIZE;
9884 if (htab->opd_abi)
9885 {
9886 /* We need bigger stubs past index 32767. */
9887 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9888 s->size += 4;
9889 s->size += 2*4;
9890 }
9891 else
9892 s->size += 4;
65f38f15 9893
57e7d118
AM
9894 /* We also need to make an entry in the .rela.plt section. */
9895 s = htab->elf.srelplt;
9896 }
9897 s->size += sizeof (Elf64_External_Rela);
9898 doneone = TRUE;
9899 }
9900 else
9901 pent->plt.offset = (bfd_vma) -1;
9902 if (!doneone)
9903 {
9904 h->plt.plist = NULL;
9905 h->needs_plt = 0;
9906 }
65f38f15 9907 }
57e7d118 9908 else
65f38f15 9909 {
57e7d118
AM
9910 h->plt.plist = NULL;
9911 h->needs_plt = 0;
65f38f15
AM
9912 }
9913
b34976b6 9914 return TRUE;
65f38f15
AM
9915}
9916
a345bc8d
AM
9917/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9918 to set up space for global entry stubs. These are put in glink,
9919 after the branch table. */
65f38f15 9920
b34976b6 9921static bfd_boolean
a345bc8d 9922size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9923{
a345bc8d
AM
9924 struct bfd_link_info *info;
9925 struct ppc_link_hash_table *htab;
9926 struct plt_entry *pent;
9927 asection *s;
65f38f15 9928
a345bc8d
AM
9929 if (h->root.type == bfd_link_hash_indirect)
9930 return TRUE;
65f38f15 9931
a345bc8d
AM
9932 if (!h->pointer_equality_needed)
9933 return TRUE;
65f38f15 9934
a345bc8d
AM
9935 if (h->def_regular)
9936 return TRUE;
65f38f15 9937
a345bc8d
AM
9938 info = inf;
9939 htab = ppc_hash_table (info);
9940 if (htab == NULL)
9941 return FALSE;
9942
9943 s = htab->glink;
9944 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9945 if (pent->plt.offset != (bfd_vma) -1
9946 && pent->addend == 0)
9947 {
afe397ea
AM
9948 /* For ELFv2, if this symbol is not defined in a regular file
9949 and we are not generating a shared library or pie, then we
9950 need to define the symbol in the executable on a call stub.
9951 This is to avoid text relocations. */
a345bc8d 9952 s->size = (s->size + 15) & -16;
8a2058b5 9953 h->root.type = bfd_link_hash_defined;
afe397ea
AM
9954 h->root.u.def.section = s;
9955 h->root.u.def.value = s->size;
a345bc8d
AM
9956 s->size += 16;
9957 break;
9958 }
9959 return TRUE;
9960}
9961
9962/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9963 read-only sections. */
9964
9965static bfd_boolean
9966maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9967{
9968 if (h->root.type == bfd_link_hash_indirect)
9969 return TRUE;
9970
9971 if (readonly_dynrelocs (h))
9972 {
9973 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9974
9975 /* Not an error, just cut short the traversal. */
9976 return FALSE;
65f38f15 9977 }
b34976b6 9978 return TRUE;
65f38f15
AM
9979}
9980
9981/* Set the sizes of the dynamic sections. */
9982
b34976b6 9983static bfd_boolean
ee67d69a 9984ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9985 struct bfd_link_info *info)
65f38f15
AM
9986{
9987 struct ppc_link_hash_table *htab;
9988 bfd *dynobj;
9989 asection *s;
b34976b6 9990 bfd_boolean relocs;
65f38f15 9991 bfd *ibfd;
7865406b 9992 struct got_entry *first_tlsld;
65f38f15
AM
9993
9994 htab = ppc_hash_table (info);
4dfe6ac6
NC
9995 if (htab == NULL)
9996 return FALSE;
9997
65f38f15
AM
9998 dynobj = htab->elf.dynobj;
9999 if (dynobj == NULL)
10000 abort ();
10001
10002 if (htab->elf.dynamic_sections_created)
10003 {
10004 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10005 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10006 {
3d4d4302 10007 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10008 if (s == NULL)
10009 abort ();
eea6121a 10010 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10011 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10012 }
10013 }
10014
10015 /* Set up .got offsets for local syms, and space for local dynamic
10016 relocs. */
c72f2fb2 10017 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10018 {
411e1bfb
AM
10019 struct got_entry **lgot_ents;
10020 struct got_entry **end_lgot_ents;
e054468f
AM
10021 struct plt_entry **local_plt;
10022 struct plt_entry **end_local_plt;
f961d9dd 10023 unsigned char *lgot_masks;
65f38f15
AM
10024 bfd_size_type locsymcount;
10025 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10026
0c8d6e5c 10027 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10028 continue;
10029
10030 for (s = ibfd->sections; s != NULL; s = s->next)
10031 {
19e08130 10032 struct ppc_dyn_relocs *p;
65f38f15 10033
6edfbbad 10034 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10035 {
ec338859
AM
10036 if (!bfd_is_abs_section (p->sec)
10037 && bfd_is_abs_section (p->sec->output_section))
10038 {
10039 /* Input section has been discarded, either because
10040 it is a copy of a linkonce section or due to
10041 linker script /DISCARD/, so we'll be discarding
10042 the relocs too. */
10043 }
248866a8 10044 else if (p->count != 0)
ec338859 10045 {
19e08130
AM
10046 asection *srel = elf_section_data (p->sec)->sreloc;
10047 if (p->ifunc)
33e44f2e 10048 srel = htab->elf.irelplt;
eea6121a 10049 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10050 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10051 info->flags |= DF_TEXTREL;
ec338859 10052 }
65f38f15
AM
10053 }
10054 }
10055
411e1bfb
AM
10056 lgot_ents = elf_local_got_ents (ibfd);
10057 if (!lgot_ents)
65f38f15
AM
10058 continue;
10059
0ffa91dd 10060 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10061 locsymcount = symtab_hdr->sh_info;
411e1bfb 10062 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10063 local_plt = (struct plt_entry **) end_lgot_ents;
10064 end_local_plt = local_plt + locsymcount;
f961d9dd 10065 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10066 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10067 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10068 {
0b8bcf0d 10069 struct got_entry **pent, *ent;
411e1bfb 10070
0b8bcf0d
AM
10071 pent = lgot_ents;
10072 while ((ent = *pent) != NULL)
411e1bfb
AM
10073 if (ent->got.refcount > 0)
10074 {
e7b938ca 10075 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10076 {
927be08e 10077 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10078 *pent = ent->next;
411e1bfb
AM
10079 }
10080 else
10081 {
19e08130
AM
10082 unsigned int ent_size = 8;
10083 unsigned int rel_size = sizeof (Elf64_External_Rela);
10084
eea6121a 10085 ent->got.offset = s->size;
e7b938ca 10086 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10087 {
19e08130
AM
10088 ent_size *= 2;
10089 rel_size *= 2;
10090 }
10091 s->size += ent_size;
10092 if ((*lgot_masks & PLT_IFUNC) != 0)
10093 {
33e44f2e 10094 htab->elf.irelplt->size += rel_size;
19e08130
AM
10095 htab->got_reli_size += rel_size;
10096 }
0e1862bb 10097 else if (bfd_link_pic (info))
19e08130
AM
10098 {
10099 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10100 srel->size += rel_size;
927be08e 10101 }
0b8bcf0d 10102 pent = &ent->next;
411e1bfb
AM
10103 }
10104 }
10105 else
0b8bcf0d 10106 *pent = ent->next;
65f38f15 10107 }
e054468f
AM
10108
10109 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10110 for (; local_plt < end_local_plt; ++local_plt)
10111 {
10112 struct plt_entry *ent;
10113
10114 for (ent = *local_plt; ent != NULL; ent = ent->next)
10115 if (ent->plt.refcount > 0)
10116 {
33e44f2e 10117 s = htab->elf.iplt;
e054468f 10118 ent->plt.offset = s->size;
b9e5796b 10119 s->size += PLT_ENTRY_SIZE (htab);
e054468f 10120
33e44f2e 10121 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
10122 }
10123 else
10124 ent->plt.offset = (bfd_vma) -1;
10125 }
65f38f15
AM
10126 }
10127
10128 /* Allocate global sym .plt and .got entries, and space for global
10129 sym dynamic relocs. */
4ce794b7 10130 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d
AM
10131 /* Stash the end of glink branch table. */
10132 if (htab->glink != NULL)
10133 htab->glink->rawsize = htab->glink->size;
10134
0e1862bb 10135 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10136 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10137
7865406b 10138 first_tlsld = NULL;
c72f2fb2 10139 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10140 {
7865406b
AM
10141 struct got_entry *ent;
10142
0c8d6e5c 10143 if (!is_ppc64_elf (ibfd))
102890f0
AM
10144 continue;
10145
7865406b
AM
10146 ent = ppc64_tlsld_got (ibfd);
10147 if (ent->got.refcount > 0)
102890f0 10148 {
7865406b 10149 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10150 {
7865406b
AM
10151 ent->is_indirect = TRUE;
10152 ent->got.ent = first_tlsld;
10153 }
10154 else
10155 {
10156 if (first_tlsld == NULL)
10157 first_tlsld = ent;
10158 s = ppc64_elf_tdata (ibfd)->got;
10159 ent->got.offset = s->size;
10160 ent->owner = ibfd;
10161 s->size += 16;
0e1862bb 10162 if (bfd_link_pic (info))
7865406b
AM
10163 {
10164 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10165 srel->size += sizeof (Elf64_External_Rela);
10166 }
102890f0
AM
10167 }
10168 }
10169 else
7865406b 10170 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10171 }
10172
65f38f15
AM
10173 /* We now have determined the sizes of the various dynamic sections.
10174 Allocate memory for them. */
b34976b6 10175 relocs = FALSE;
65f38f15
AM
10176 for (s = dynobj->sections; s != NULL; s = s->next)
10177 {
10178 if ((s->flags & SEC_LINKER_CREATED) == 0)
10179 continue;
10180
4ce794b7 10181 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10182 /* These haven't been allocated yet; don't strip. */
10183 continue;
33e44f2e
AM
10184 else if (s == htab->elf.sgot
10185 || s == htab->elf.splt
10186 || s == htab->elf.iplt
c456f082 10187 || s == htab->glink
5474d94f
AM
10188 || s == htab->elf.sdynbss
10189 || s == htab->elf.sdynrelro)
65f38f15
AM
10190 {
10191 /* Strip this section if we don't need it; see the
10192 comment below. */
5bd4f169 10193 }
58d180e8
AM
10194 else if (s == htab->glink_eh_frame)
10195 {
10196 if (!bfd_is_abs_section (s->output_section))
10197 /* Not sized yet. */
10198 continue;
10199 }
70cc837d 10200 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10201 {
c456f082 10202 if (s->size != 0)
5bd4f169 10203 {
33e44f2e 10204 if (s != htab->elf.srelplt)
b34976b6 10205 relocs = TRUE;
5bd4f169
AM
10206
10207 /* We use the reloc_count field as a counter if we need
10208 to copy relocs into the output file. */
10209 s->reloc_count = 0;
10210 }
10211 }
65f38f15 10212 else
5bd4f169
AM
10213 {
10214 /* It's not one of our sections, so don't allocate space. */
10215 continue;
10216 }
10217
eea6121a 10218 if (s->size == 0)
5bd4f169 10219 {
c456f082
AM
10220 /* If we don't need this section, strip it from the
10221 output file. This is mostly to handle .rela.bss and
10222 .rela.plt. We must create both sections in
10223 create_dynamic_sections, because they must be created
10224 before the linker maps input sections to output
10225 sections. The linker does that before
10226 adjust_dynamic_symbol is called, and it is that
10227 function which decides whether anything needs to go
10228 into these sections. */
8423293d 10229 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10230 continue;
10231 }
10232
c456f082 10233 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10234 continue;
10235
65f38f15
AM
10236 /* Allocate memory for the section contents. We use bfd_zalloc
10237 here in case unused entries are not reclaimed before the
10238 section's contents are written out. This should not happen,
411e1bfb
AM
10239 but this way if it does we get a R_PPC64_NONE reloc in .rela
10240 sections instead of garbage.
10241 We also rely on the section contents being zero when writing
5474d94f 10242 the GOT and .dynrelro. */
eea6121a 10243 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10244 if (s->contents == NULL)
b34976b6 10245 return FALSE;
5bd4f169
AM
10246 }
10247
c72f2fb2 10248 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10249 {
0c8d6e5c 10250 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10251 continue;
10252
e717da7e 10253 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10254 if (s != NULL && s != htab->elf.sgot)
e717da7e 10255 {
eea6121a 10256 if (s->size == 0)
8423293d 10257 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10258 else
10259 {
eea6121a 10260 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10261 if (s->contents == NULL)
10262 return FALSE;
10263 }
10264 }
10265 s = ppc64_elf_tdata (ibfd)->relgot;
10266 if (s != NULL)
10267 {
eea6121a 10268 if (s->size == 0)
8423293d 10269 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10270 else
10271 {
eea6121a 10272 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10273 if (s->contents == NULL)
10274 return FALSE;
10275 relocs = TRUE;
10276 s->reloc_count = 0;
10277 }
10278 }
10279 }
10280
e86ce104 10281 if (htab->elf.dynamic_sections_created)
5bd4f169 10282 {
e8910a83
AM
10283 bfd_boolean tls_opt;
10284
5bd4f169
AM
10285 /* Add some entries to the .dynamic section. We fill in the
10286 values later, in ppc64_elf_finish_dynamic_sections, but we
10287 must add the entries now so that we get the correct size for
10288 the .dynamic section. The DT_DEBUG entry is filled in by the
10289 dynamic linker and used by the debugger. */
dc810e39 10290#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10291 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10292
0e1862bb 10293 if (bfd_link_executable (info))
5bd4f169 10294 {
dc810e39 10295 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10296 return FALSE;
5bd4f169
AM
10297 }
10298
33e44f2e 10299 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10300 {
dc810e39
AM
10301 if (!add_dynamic_entry (DT_PLTGOT, 0)
10302 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10303 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10304 || !add_dynamic_entry (DT_JMPREL, 0)
10305 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10306 return FALSE;
5bd4f169
AM
10307 }
10308
ee67d69a 10309 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10310 {
10311 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10312 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10313 return FALSE;
19397422
AM
10314 }
10315
7c9cf415 10316 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10317 && htab->tls_get_addr_fd != NULL
10318 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10319 if (tls_opt || !htab->opd_abi)
10320 {
10321 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10322 return FALSE;
10323 }
a7f2871e 10324
5bd4f169
AM
10325 if (relocs)
10326 {
dc810e39
AM
10327 if (!add_dynamic_entry (DT_RELA, 0)
10328 || !add_dynamic_entry (DT_RELASZ, 0)
10329 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10330 return FALSE;
5bd4f169 10331
65f38f15
AM
10332 /* If any dynamic relocs apply to a read-only section,
10333 then we need a DT_TEXTREL entry. */
248866a8 10334 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10335 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10336
65f38f15 10337 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10338 {
65f38f15 10339 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10340 return FALSE;
5bd4f169 10341 }
5bd4f169 10342 }
5bd4f169 10343 }
65f38f15 10344#undef add_dynamic_entry
5bd4f169 10345
b34976b6 10346 return TRUE;
5bd4f169
AM
10347}
10348
a345bc8d
AM
10349/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10350
10351static bfd_boolean
10352ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10353{
10354 if (h->plt.plist != NULL
10355 && !h->def_regular
10356 && !h->pointer_equality_needed)
10357 return FALSE;
10358
10359 return _bfd_elf_hash_symbol (h);
10360}
10361
721956f4 10362/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10363
4ce794b7
AM
10364static inline enum ppc_stub_type
10365ppc_type_of_stub (asection *input_sec,
10366 const Elf_Internal_Rela *rel,
10367 struct ppc_link_hash_entry **hash,
e054468f 10368 struct plt_entry **plt_ent,
6911b7dc
AM
10369 bfd_vma destination,
10370 unsigned long local_off)
5bd4f169 10371{
721956f4
AM
10372 struct ppc_link_hash_entry *h = *hash;
10373 bfd_vma location;
10374 bfd_vma branch_offset;
10375 bfd_vma max_branch_offset;
4ce794b7 10376 enum elf_ppc64_reloc_type r_type;
5bd4f169 10377
721956f4
AM
10378 if (h != NULL)
10379 {
e054468f 10380 struct plt_entry *ent;
7fe2b9a6 10381 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10382 if (h->oh != NULL
10383 && h->oh->is_func_descriptor)
7b8f6675
AM
10384 {
10385 fdh = ppc_follow_link (h->oh);
10386 *hash = fdh;
10387 }
8387904d 10388
e054468f
AM
10389 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10390 if (ent->addend == rel->r_addend
10391 && ent->plt.offset != (bfd_vma) -1)
10392 {
e054468f
AM
10393 *plt_ent = ent;
10394 return ppc_stub_plt_call;
10395 }
5bd4f169 10396
7fe2b9a6
AM
10397 /* Here, we know we don't have a plt entry. If we don't have a
10398 either a defined function descriptor or a defined entry symbol
10399 in a regular object file, then it is pointless trying to make
10400 any other type of stub. */
854b41e7
AM
10401 if (!is_static_defined (&fdh->elf)
10402 && !is_static_defined (&h->elf))
721956f4 10403 return ppc_stub_none;
5d1634d7 10404 }
e054468f
AM
10405 else if (elf_local_got_ents (input_sec->owner) != NULL)
10406 {
10407 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10408 struct plt_entry **local_plt = (struct plt_entry **)
10409 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10410 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10411
10412 if (local_plt[r_symndx] != NULL)
10413 {
10414 struct plt_entry *ent;
10415
10416 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10417 if (ent->addend == rel->r_addend
10418 && ent->plt.offset != (bfd_vma) -1)
10419 {
10420 *plt_ent = ent;
10421 return ppc_stub_plt_call;
10422 }
10423 }
10424 }
5d1634d7 10425
721956f4
AM
10426 /* Determine where the call point is. */
10427 location = (input_sec->output_offset
10428 + input_sec->output_section->vma
10429 + rel->r_offset);
5d1634d7 10430
721956f4
AM
10431 branch_offset = destination - location;
10432 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10433
721956f4
AM
10434 /* Determine if a long branch stub is needed. */
10435 max_branch_offset = 1 << 25;
4ce794b7 10436 if (r_type != R_PPC64_REL24)
721956f4 10437 max_branch_offset = 1 << 15;
5d1634d7 10438
6911b7dc 10439 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10440 /* We need a stub. Figure out whether a long_branch or plt_branch
10441 is needed later. */
10442 return ppc_stub_long_branch;
5d1634d7 10443
721956f4 10444 return ppc_stub_none;
5d1634d7
AM
10445}
10446
794e51c0
AM
10447/* With power7 weakly ordered memory model, it is possible for ld.so
10448 to update a plt entry in one thread and have another thread see a
10449 stale zero toc entry. To avoid this we need some sort of acquire
10450 barrier in the call stub. One solution is to make the load of the
10451 toc word seem to appear to depend on the load of the function entry
10452 word. Another solution is to test for r2 being zero, and branch to
10453 the appropriate glink entry if so.
10454
10455 . fake dep barrier compare
71a39c98
AM
10456 . ld 12,xxx(2) ld 12,xxx(2)
10457 . mtctr 12 mtctr 12
10458 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10459 . add 2,2,11 cmpldi 2,0
10460 . ld 2,xxx+8(2) bnectr+
10461 . bctr b <glink_entry>
10462
10463 The solution involving the compare turns out to be faster, so
10464 that's what we use unless the branch won't reach. */
10465
10466#define ALWAYS_USE_FAKE_DEP 0
10467#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10468
5d1634d7
AM
10469#define PPC_LO(v) ((v) & 0xffff)
10470#define PPC_HI(v) (((v) >> 16) & 0xffff)
10471#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10472
794e51c0
AM
10473static inline unsigned int
10474plt_stub_size (struct ppc_link_hash_table *htab,
10475 struct ppc_stub_hash_entry *stub_entry,
10476 bfd_vma off)
10477{
b9e5796b
AM
10478 unsigned size = 12;
10479
10480 if (ALWAYS_EMIT_R2SAVE
10481 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10482 size += 4;
10483 if (PPC_HA (off) != 0)
794e51c0 10484 size += 4;
b9e5796b
AM
10485 if (htab->opd_abi)
10486 {
10487 size += 4;
e7d1c40c 10488 if (htab->params->plt_static_chain)
b9e5796b 10489 size += 4;
bd4d2eaa
AM
10490 if (htab->params->plt_thread_safe
10491 && htab->elf.dynamic_sections_created
10492 && stub_entry->h != NULL
10493 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10494 size += 8;
e7d1c40c 10495 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10496 size += 4;
10497 }
794e51c0
AM
10498 if (stub_entry->h != NULL
10499 && (stub_entry->h == htab->tls_get_addr_fd
10500 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10501 && htab->params->tls_get_addr_opt)
794e51c0
AM
10502 size += 13 * 4;
10503 return size;
10504}
10505
10506/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10507 then return the padding needed to do so. */
10508static inline unsigned int
10509plt_stub_pad (struct ppc_link_hash_table *htab,
10510 struct ppc_stub_hash_entry *stub_entry,
10511 bfd_vma plt_off)
10512{
e7d1c40c 10513 int stub_align = 1 << htab->params->plt_stub_align;
794e51c0 10514 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10515 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0
AM
10516
10517 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10518 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10519 return stub_align - (stub_off & (stub_align - 1));
10520 return 0;
10521}
10522
10523/* Build a .plt call stub. */
10524
10525static inline bfd_byte *
10526build_plt_stub (struct ppc_link_hash_table *htab,
10527 struct ppc_stub_hash_entry *stub_entry,
10528 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10529{
e7d1c40c 10530 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10531 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10532 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10533 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10534 && htab->elf.dynamic_sections_created
10535 && stub_entry->h != NULL
10536 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10537 bfd_boolean use_fake_dep = plt_thread_safe;
10538 bfd_vma cmp_branch_off = 0;
10539
10540 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10541 && plt_load_toc
794e51c0 10542 && plt_thread_safe
bd4d2eaa
AM
10543 && !((stub_entry->h == htab->tls_get_addr_fd
10544 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10545 && htab->params->tls_get_addr_opt))
794e51c0
AM
10546 {
10547 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10548 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10549 / PLT_ENTRY_SIZE (htab));
794e51c0
AM
10550 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10551 bfd_vma to, from;
10552
68d62958
AM
10553 if (pltindex > 32768)
10554 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10555 to = (glinkoff
10556 + htab->glink->output_offset
10557 + htab->glink->output_section->vma);
6f20ed8a 10558 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10559 + 4 * (ALWAYS_EMIT_R2SAVE
10560 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10561 + 4 * (PPC_HA (offset) != 0)
10562 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10563 != PPC_HA (offset))
10564 + 4 * (plt_static_chain != 0)
10565 + 20
6f20ed8a
AM
10566 + stub_entry->group->stub_sec->output_offset
10567 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10568 cmp_branch_off = to - from;
10569 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10570 }
10571
ac2df442
AM
10572 if (PPC_HA (offset) != 0)
10573 {
176a0d42
AM
10574 if (r != NULL)
10575 {
794e51c0
AM
10576 if (ALWAYS_EMIT_R2SAVE
10577 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10578 r[0].r_offset += 4;
176a0d42 10579 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10580 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10581 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10582 r[1].r_addend = r[0].r_addend;
b9e5796b 10583 if (plt_load_toc)
176a0d42 10584 {
b9e5796b 10585 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10586 {
b9e5796b
AM
10587 r[2].r_offset = r[1].r_offset + 4;
10588 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10589 r[2].r_addend = r[0].r_addend;
10590 }
10591 else
10592 {
10593 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10594 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10595 r[2].r_addend = r[0].r_addend + 8;
10596 if (plt_static_chain)
10597 {
10598 r[3].r_offset = r[2].r_offset + 4;
10599 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10600 r[3].r_addend = r[0].r_addend + 16;
10601 }
c7131b65 10602 }
176a0d42
AM
10603 }
10604 }
794e51c0
AM
10605 if (ALWAYS_EMIT_R2SAVE
10606 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10607 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10608 if (plt_load_toc)
10609 {
10610 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10611 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10612 }
10613 else
10614 {
10615 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10616 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10617 }
b9e5796b
AM
10618 if (plt_load_toc
10619 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10620 {
71a39c98 10621 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10622 offset = 0;
10623 }
71a39c98 10624 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10625 if (plt_load_toc)
794e51c0 10626 {
b9e5796b
AM
10627 if (use_fake_dep)
10628 {
10629 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10630 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10631 }
10632 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10633 if (plt_static_chain)
10634 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10635 }
ac2df442
AM
10636 }
10637 else
10638 {
176a0d42
AM
10639 if (r != NULL)
10640 {
794e51c0
AM
10641 if (ALWAYS_EMIT_R2SAVE
10642 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10643 r[0].r_offset += 4;
176a0d42 10644 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10645 if (plt_load_toc)
176a0d42 10646 {
b9e5796b 10647 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10648 {
b9e5796b
AM
10649 r[1].r_offset = r[0].r_offset + 4;
10650 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10651 r[1].r_addend = r[0].r_addend;
10652 }
10653 else
10654 {
10655 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10656 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10657 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10658 if (plt_static_chain)
10659 {
10660 r[2].r_offset = r[1].r_offset + 4;
10661 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10662 r[2].r_addend = r[0].r_addend + 8;
10663 }
c7131b65 10664 }
176a0d42
AM
10665 }
10666 }
794e51c0
AM
10667 if (ALWAYS_EMIT_R2SAVE
10668 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10669 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10670 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10671 if (plt_load_toc
10672 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10673 {
10674 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10675 offset = 0;
10676 }
71a39c98 10677 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10678 if (plt_load_toc)
794e51c0 10679 {
b9e5796b
AM
10680 if (use_fake_dep)
10681 {
10682 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10683 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10684 }
10685 if (plt_static_chain)
10686 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10687 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10688 }
ac2df442 10689 }
b9e5796b 10690 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10691 {
10692 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10693 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10694 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10695 }
10696 else
10697 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10698 return p;
10699}
10700
a7f2871e
AM
10701/* Build a special .plt call stub for __tls_get_addr. */
10702
10703#define LD_R11_0R3 0xe9630000
10704#define LD_R12_0R3 0xe9830000
10705#define MR_R0_R3 0x7c601b78
10706#define CMPDI_R11_0 0x2c2b0000
10707#define ADD_R3_R12_R13 0x7c6c6a14
10708#define BEQLR 0x4d820020
10709#define MR_R3_R0 0x7c030378
a7f2871e
AM
10710#define STD_R11_0R1 0xf9610000
10711#define BCTRL 0x4e800421
10712#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10713#define MTLR_R11 0x7d6803a6
10714
10715static inline bfd_byte *
794e51c0
AM
10716build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10717 struct ppc_stub_hash_entry *stub_entry,
10718 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10719{
e7d1c40c 10720 bfd *obfd = htab->params->stub_bfd;
794e51c0 10721
a7f2871e
AM
10722 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10723 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10724 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10725 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10726 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10727 bfd_put_32 (obfd, BEQLR, p), p += 4;
10728 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10729 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10730 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10731
10732 if (r != NULL)
10733 r[0].r_offset += 9 * 4;
794e51c0 10734 p = build_plt_stub (htab, stub_entry, p, offset, r);
a7f2871e
AM
10735 bfd_put_32 (obfd, BCTRL, p - 4);
10736
a078d95a 10737 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10738 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10739 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10740 bfd_put_32 (obfd, BLR, p), p += 4;
10741
10742 return p;
10743}
10744
176a0d42
AM
10745static Elf_Internal_Rela *
10746get_relocs (asection *sec, int count)
10747{
10748 Elf_Internal_Rela *relocs;
10749 struct bfd_elf_section_data *elfsec_data;
10750
10751 elfsec_data = elf_section_data (sec);
10752 relocs = elfsec_data->relocs;
10753 if (relocs == NULL)
10754 {
10755 bfd_size_type relsize;
10756 relsize = sec->reloc_count * sizeof (*relocs);
10757 relocs = bfd_alloc (sec->owner, relsize);
10758 if (relocs == NULL)
10759 return NULL;
10760 elfsec_data->relocs = relocs;
d4730f92
BS
10761 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10762 sizeof (Elf_Internal_Shdr));
10763 if (elfsec_data->rela.hdr == NULL)
10764 return NULL;
10765 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10766 * sizeof (Elf64_External_Rela));
10767 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10768 sec->reloc_count = 0;
10769 }
10770 relocs += sec->reloc_count;
10771 sec->reloc_count += count;
10772 return relocs;
10773}
10774
aa374f67 10775static bfd_vma
25f53a85 10776get_r2off (struct bfd_link_info *info,
aa374f67
AM
10777 struct ppc_stub_hash_entry *stub_entry)
10778{
25f53a85 10779 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10780 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10781
10782 if (r2off == 0)
10783 {
10784 /* Support linking -R objects. Get the toc pointer from the
10785 opd entry. */
10786 char buf[8];
b9e5796b
AM
10787 if (!htab->opd_abi)
10788 return r2off;
aa374f67
AM
10789 asection *opd = stub_entry->h->elf.root.u.def.section;
10790 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10791
10792 if (strcmp (opd->name, ".opd") != 0
10793 || opd->reloc_count != 0)
10794 {
bc30df16 10795 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
25f53a85 10796 stub_entry->h->elf.root.root.string);
aa374f67 10797 bfd_set_error (bfd_error_bad_value);
a7c49797 10798 return (bfd_vma) -1;
aa374f67
AM
10799 }
10800 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10801 return (bfd_vma) -1;
aa374f67 10802 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10803 r2off -= elf_gp (info->output_bfd);
aa374f67 10804 }
6f20ed8a 10805 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10806 return r2off;
10807}
10808
b34976b6 10809static bfd_boolean
4ce794b7 10810ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10811{
721956f4
AM
10812 struct ppc_stub_hash_entry *stub_entry;
10813 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10814 struct bfd_link_info *info;
10815 struct ppc_link_hash_table *htab;
721956f4
AM
10816 bfd_byte *loc;
10817 bfd_byte *p;
ee75fd95 10818 bfd_vma dest, off;
721956f4 10819 int size;
176a0d42 10820 Elf_Internal_Rela *r;
e054468f 10821 asection *plt;
5d1634d7 10822
721956f4
AM
10823 /* Massage our args to the form they really have. */
10824 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10825 info = in_arg;
5d1634d7 10826
5d1634d7 10827 htab = ppc_hash_table (info);
4dfe6ac6
NC
10828 if (htab == NULL)
10829 return FALSE;
5d1634d7 10830
721956f4 10831 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
10832 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10833 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10834
4ce794b7 10835 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10836 switch (stub_entry->stub_type)
5d1634d7 10837 {
721956f4 10838 case ppc_stub_long_branch:
ad8e1ba5 10839 case ppc_stub_long_branch_r2off:
721956f4 10840 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10841 dest = (stub_entry->target_value
10842 + stub_entry->target_section->output_offset
10843 + stub_entry->target_section->output_section->vma);
10844 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10845 off = dest;
5d1634d7 10846
721956f4
AM
10847 /* And this is where we are coming from. */
10848 off -= (stub_entry->stub_offset
6f20ed8a
AM
10849 + stub_entry->group->stub_sec->output_offset
10850 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 10851
ac2df442
AM
10852 size = 4;
10853 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10854 {
25f53a85 10855 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10856
a7c49797 10857 if (r2off == (bfd_vma) -1)
aa374f67
AM
10858 {
10859 htab->stub_error = TRUE;
10860 return FALSE;
10861 }
e7d1c40c 10862 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10863 loc += 4;
a7c49797 10864 size = 8;
ac2df442
AM
10865 if (PPC_HA (r2off) != 0)
10866 {
e7d1c40c
AM
10867 bfd_put_32 (htab->params->stub_bfd,
10868 ADDIS_R2_R2 | PPC_HA (r2off), loc);
ac2df442 10869 loc += 4;
a7c49797
AM
10870 size += 4;
10871 }
10872 if (PPC_LO (r2off) != 0)
10873 {
10874 bfd_put_32 (htab->params->stub_bfd,
10875 ADDI_R2_R2 | PPC_LO (r2off), loc);
10876 loc += 4;
10877 size += 4;
ac2df442 10878 }
ac2df442 10879 off -= size - 4;
ad8e1ba5 10880 }
e7d1c40c 10881 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 10882
5c3dead3
AM
10883 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10884 {
bc30df16
AM
10885 info->callbacks->einfo
10886 (_("%P: long branch stub `%s' offset overflow\n"),
10887 stub_entry->root.string);
5c3dead3
AM
10888 htab->stub_error = TRUE;
10889 return FALSE;
10890 }
ee75fd95
AM
10891
10892 if (info->emitrelocations)
10893 {
6f20ed8a 10894 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10895 if (r == NULL)
10896 return FALSE;
6f20ed8a 10897 r->r_offset = loc - stub_entry->group->stub_sec->contents;
ee75fd95
AM
10898 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10899 r->r_addend = dest;
10900 if (stub_entry->h != NULL)
10901 {
10902 struct elf_link_hash_entry **hashes;
10903 unsigned long symndx;
10904 struct ppc_link_hash_entry *h;
10905
e7d1c40c 10906 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10907 if (hashes == NULL)
10908 {
10909 bfd_size_type hsize;
10910
10911 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10912 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10913 if (hashes == NULL)
10914 return FALSE;
e7d1c40c 10915 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10916 htab->stub_globals = 1;
10917 }
10918 symndx = htab->stub_globals++;
10919 h = stub_entry->h;
10920 hashes[symndx] = &h->elf;
10921 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10922 if (h->oh != NULL && h->oh->is_func)
b31867b6 10923 h = ppc_follow_link (h->oh);
ee75fd95
AM
10924 if (h->elf.root.u.def.section != stub_entry->target_section)
10925 /* H is an opd symbol. The addend must be zero. */
10926 r->r_addend = 0;
10927 else
10928 {
10929 off = (h->elf.root.u.def.value
10930 + h->elf.root.u.def.section->output_offset
10931 + h->elf.root.u.def.section->output_section->vma);
10932 r->r_addend -= off;
10933 }
10934 }
10935 }
721956f4 10936 break;
e86ce104 10937
721956f4 10938 case ppc_stub_plt_branch:
ad8e1ba5 10939 case ppc_stub_plt_branch_r2off:
721956f4
AM
10940 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10941 stub_entry->root.string + 9,
b34976b6 10942 FALSE, FALSE);
721956f4
AM
10943 if (br_entry == NULL)
10944 {
8de848d8 10945 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 10946 stub_entry->root.string);
b34976b6
AM
10947 htab->stub_error = TRUE;
10948 return FALSE;
721956f4
AM
10949 }
10950
176a0d42
AM
10951 dest = (stub_entry->target_value
10952 + stub_entry->target_section->output_offset
10953 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10954 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10955 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10956
176a0d42 10957 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10958 htab->brlt->contents + br_entry->offset);
721956f4 10959
f94498ff 10960 if (br_entry->iter == htab->stub_iteration)
721956f4 10961 {
f94498ff 10962 br_entry->iter = 0;
84f5d08e 10963
f94498ff 10964 if (htab->relbrlt != NULL)
84f5d08e 10965 {
f94498ff
AM
10966 /* Create a reloc for the branch lookup table entry. */
10967 Elf_Internal_Rela rela;
10968 bfd_byte *rl;
10969
10970 rela.r_offset = (br_entry->offset
10971 + htab->brlt->output_offset
10972 + htab->brlt->output_section->vma);
10973 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10974 rela.r_addend = dest;
f94498ff
AM
10975
10976 rl = htab->relbrlt->contents;
10977 rl += (htab->relbrlt->reloc_count++
10978 * sizeof (Elf64_External_Rela));
10979 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10980 }
10981 else if (info->emitrelocations)
10982 {
176a0d42
AM
10983 r = get_relocs (htab->brlt, 1);
10984 if (r == NULL)
10985 return FALSE;
10986 /* brlt, being SEC_LINKER_CREATED does not go through the
10987 normal reloc processing. Symbols and offsets are not
10988 translated from input file to output file form, so
10989 set up the offset per the output file. */
f94498ff
AM
10990 r->r_offset = (br_entry->offset
10991 + htab->brlt->output_offset
10992 + htab->brlt->output_section->vma);
10993 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10994 r->r_addend = dest;
84f5d08e 10995 }
84f5d08e 10996 }
721956f4 10997
176a0d42
AM
10998 dest = (br_entry->offset
10999 + htab->brlt->output_offset
11000 + htab->brlt->output_section->vma);
11001
11002 off = (dest
4ce794b7 11003 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 11004 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11005
ad8e1ba5 11006 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11007 {
25f53a85 11008 info->callbacks->einfo
bc30df16 11009 (_("%P: linkage table error against `%T'\n"),
721956f4 11010 stub_entry->root.string);
5d1634d7 11011 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11012 htab->stub_error = TRUE;
11013 return FALSE;
5d1634d7 11014 }
41bd81ab 11015
176a0d42
AM
11016 if (info->emitrelocations)
11017 {
6f20ed8a 11018 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11019 if (r == NULL)
11020 return FALSE;
6f20ed8a 11021 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11022 if (bfd_big_endian (info->output_bfd))
11023 r[0].r_offset += 2;
00f412ee 11024 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11025 r[0].r_offset += 4;
11026 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11027 r[0].r_addend = dest;
11028 if (PPC_HA (off) != 0)
11029 {
11030 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11031 r[1].r_offset = r[0].r_offset + 4;
11032 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11033 r[1].r_addend = r[0].r_addend;
11034 }
11035 }
11036
00f412ee 11037 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11038 {
176a0d42 11039 if (PPC_HA (off) != 0)
ac2df442
AM
11040 {
11041 size = 16;
e7d1c40c 11042 bfd_put_32 (htab->params->stub_bfd,
397998fc 11043 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 11044 loc += 4;
e7d1c40c 11045 bfd_put_32 (htab->params->stub_bfd,
397998fc 11046 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
11047 }
11048 else
11049 {
11050 size = 12;
e7d1c40c
AM
11051 bfd_put_32 (htab->params->stub_bfd,
11052 LD_R12_0R2 | PPC_LO (off), loc);
ac2df442 11053 }
ad8e1ba5
AM
11054 }
11055 else
11056 {
25f53a85 11057 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11058
a7c49797 11059 if (r2off == (bfd_vma) -1)
aa374f67
AM
11060 {
11061 htab->stub_error = TRUE;
11062 return FALSE;
11063 }
ad8e1ba5 11064
e7d1c40c 11065 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 11066 loc += 4;
00f412ee 11067 size = 16;
176a0d42 11068 if (PPC_HA (off) != 0)
ac2df442
AM
11069 {
11070 size += 4;
e7d1c40c 11071 bfd_put_32 (htab->params->stub_bfd,
397998fc 11072 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 11073 loc += 4;
e7d1c40c 11074 bfd_put_32 (htab->params->stub_bfd,
397998fc 11075 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
11076 }
11077 else
e7d1c40c 11078 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442
AM
11079
11080 if (PPC_HA (r2off) != 0)
11081 {
11082 size += 4;
00f412ee 11083 loc += 4;
e7d1c40c
AM
11084 bfd_put_32 (htab->params->stub_bfd,
11085 ADDIS_R2_R2 | PPC_HA (r2off), loc);
00f412ee
AM
11086 }
11087 if (PPC_LO (r2off) != 0)
11088 {
11089 size += 4;
ac2df442 11090 loc += 4;
e7d1c40c
AM
11091 bfd_put_32 (htab->params->stub_bfd,
11092 ADDI_R2_R2 | PPC_LO (r2off), loc);
ac2df442 11093 }
ad8e1ba5
AM
11094 }
11095 loc += 4;
e7d1c40c 11096 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
ad8e1ba5 11097 loc += 4;
e7d1c40c 11098 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
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
AM
11149 stub_entry->plt_ent->plt.offset |= 1;
11150 }
176a0d42
AM
11151
11152 off = (dest
e054468f 11153 - elf_gp (plt->output_section->owner)
6f20ed8a 11154 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11155
ad8e1ba5 11156 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11157 {
25f53a85 11158 info->callbacks->einfo
695344c0 11159 /* xgettext:c-format */
bc30df16 11160 (_("%P: linkage table error against `%T'\n"),
e054468f
AM
11161 stub_entry->h != NULL
11162 ? stub_entry->h->elf.root.root.string
11163 : "<local sym>");
721956f4 11164 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11165 htab->stub_error = TRUE;
11166 return FALSE;
721956f4
AM
11167 }
11168
e7d1c40c 11169 if (htab->params->plt_stub_align != 0)
794e51c0
AM
11170 {
11171 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11172
6f20ed8a
AM
11173 stub_entry->group->stub_sec->size += pad;
11174 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
11175 loc += pad;
11176 }
11177
176a0d42
AM
11178 r = NULL;
11179 if (info->emitrelocations)
11180 {
6f20ed8a 11181 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11182 ((PPC_HA (off) != 0)
11183 + (htab->opd_abi
e7d1c40c 11184 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11185 && PPC_HA (off + 16) == PPC_HA (off))
11186 : 1)));
176a0d42
AM
11187 if (r == NULL)
11188 return FALSE;
6f20ed8a 11189 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11190 if (bfd_big_endian (info->output_bfd))
11191 r[0].r_offset += 2;
176a0d42
AM
11192 r[0].r_addend = dest;
11193 }
a7f2871e
AM
11194 if (stub_entry->h != NULL
11195 && (stub_entry->h == htab->tls_get_addr_fd
11196 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11197 && htab->params->tls_get_addr_opt)
794e51c0 11198 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11199 else
794e51c0 11200 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11201 size = p - loc;
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
6f20ed8a 11212 stub_entry->group->stub_sec->size += size;
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
11301 - elf_gp (plt->output_section->owner)
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);
e7d1c40c 11305 if (htab->params->plt_stub_align)
794e51c0 11306 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11307 if (info->emitrelocations)
11308 {
6f20ed8a 11309 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11310 += ((PPC_HA (off) != 0)
11311 + (htab->opd_abi
e7d1c40c 11312 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11313 && PPC_HA (off + 16) == PPC_HA (off))
11314 : 1));
6f20ed8a 11315 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11316 }
721956f4
AM
11317 }
11318 else
11319 {
ad8e1ba5
AM
11320 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11321 variants. */
ac2df442 11322 bfd_vma r2off = 0;
6911b7dc 11323 bfd_vma local_off = 0;
ac2df442 11324
721956f4
AM
11325 off = (stub_entry->target_value
11326 + stub_entry->target_section->output_offset
11327 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11328 off -= (stub_entry->group->stub_sec->size
11329 + stub_entry->group->stub_sec->output_offset
11330 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11331
ad8e1ba5
AM
11332 /* Reset the stub type from the plt variant in case we now
11333 can reach with a shorter stub. */
11334 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11335 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11336
11337 size = 4;
11338 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11339 {
25f53a85 11340 r2off = get_r2off (info, stub_entry);
a7c49797 11341 if (r2off == (bfd_vma) -1)
aa374f67
AM
11342 {
11343 htab->stub_error = TRUE;
11344 return FALSE;
11345 }
a7c49797 11346 size = 8;
ac2df442 11347 if (PPC_HA (r2off) != 0)
a7c49797
AM
11348 size += 4;
11349 if (PPC_LO (r2off) != 0)
11350 size += 4;
ac2df442 11351 off -= size - 4;
ad8e1ba5
AM
11352 }
11353
6911b7dc
AM
11354 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11355
b9e5796b
AM
11356 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11357 Do the same for -R objects without function descriptors. */
11358 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11359 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11360 && r2off == 0
11361 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11362 {
11363 struct ppc_branch_hash_entry *br_entry;
11364
11365 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11366 stub_entry->root.string + 9,
b34976b6 11367 TRUE, FALSE);
721956f4
AM
11368 if (br_entry == NULL)
11369 {
8de848d8 11370 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 11371 stub_entry->root.string);
b34976b6
AM
11372 htab->stub_error = TRUE;
11373 return FALSE;
721956f4
AM
11374 }
11375
11376 if (br_entry->iter != htab->stub_iteration)
11377 {
11378 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11379 br_entry->offset = htab->brlt->size;
11380 htab->brlt->size += 8;
63bc6f6c 11381
ee75fd95 11382 if (htab->relbrlt != NULL)
eea6121a 11383 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11384 else if (info->emitrelocations)
11385 {
11386 htab->brlt->reloc_count += 1;
11387 htab->brlt->flags |= SEC_RELOC;
11388 }
721956f4 11389 }
ad8e1ba5
AM
11390
11391 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11392 off = (br_entry->offset
11393 + htab->brlt->output_offset
11394 + htab->brlt->output_section->vma
11395 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 11396 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11397
176a0d42
AM
11398 if (info->emitrelocations)
11399 {
6f20ed8a
AM
11400 stub_entry->group->stub_sec->reloc_count
11401 += 1 + (PPC_HA (off) != 0);
11402 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11403 }
11404
00f412ee 11405 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11406 {
11407 size = 12;
176a0d42 11408 if (PPC_HA (off) != 0)
ac2df442
AM
11409 size = 16;
11410 }
11411 else
11412 {
00f412ee 11413 size = 16;
176a0d42 11414 if (PPC_HA (off) != 0)
ac2df442
AM
11415 size += 4;
11416
11417 if (PPC_HA (r2off) != 0)
11418 size += 4;
00f412ee
AM
11419 if (PPC_LO (r2off) != 0)
11420 size += 4;
ac2df442 11421 }
721956f4 11422 }
84f5d08e
AM
11423 else if (info->emitrelocations)
11424 {
6f20ed8a
AM
11425 stub_entry->group->stub_sec->reloc_count += 1;
11426 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11427 }
721956f4
AM
11428 }
11429
6f20ed8a 11430 stub_entry->group->stub_sec->size += size;
b34976b6 11431 return TRUE;
721956f4
AM
11432}
11433
11434/* Set up various things so that we can make a list of input sections
11435 for each output section included in the link. Returns -1 on error,
cedb70c5 11436 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11437
11438int
e7d1c40c 11439ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11440{
6f20ed8a 11441 unsigned int id;
721956f4
AM
11442 bfd_size_type amt;
11443 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11444
4dfe6ac6
NC
11445 if (htab == NULL)
11446 return -1;
4c52953f 11447
6f20ed8a
AM
11448 htab->sec_info_arr_size = bfd_get_next_section_id ();
11449 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11450 htab->sec_info = bfd_zmalloc (amt);
11451 if (htab->sec_info == NULL)
721956f4
AM
11452 return -1;
11453
3d6f9012
AM
11454 /* Set toc_off for com, und, abs and ind sections. */
11455 for (id = 0; id < 3; id++)
6f20ed8a 11456 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11457
721956f4
AM
11458 return 1;
11459}
11460
927be08e
AM
11461/* Set up for first pass at multitoc partitioning. */
11462
11463void
11464ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11465{
11466 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11467
1c865ab2 11468 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11469 htab->toc_bfd = NULL;
11470 htab->toc_first_sec = NULL;
11471}
11472
e717da7e
AM
11473/* The linker repeatedly calls this function for each TOC input section
11474 and linker generated GOT section. Group input bfds such that the toc
927be08e 11475 within a group is less than 64k in size. */
ad8e1ba5 11476
927be08e 11477bfd_boolean
4ce794b7 11478ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11479{
11480 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11481 bfd_vma addr, off, limit;
ad8e1ba5 11482
4dfe6ac6
NC
11483 if (htab == NULL)
11484 return FALSE;
11485
927be08e 11486 if (!htab->second_toc_pass)
4c52953f 11487 {
927be08e 11488 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11489 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11490
11491 if (new_bfd)
bf102f86
AM
11492 {
11493 htab->toc_bfd = isec->owner;
11494 htab->toc_first_sec = isec;
11495 }
927be08e 11496
bf102f86
AM
11497 addr = isec->output_offset + isec->output_section->vma;
11498 off = addr - htab->toc_curr;
d77c8a4b
AM
11499 limit = 0x80008000;
11500 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11501 limit = 0x10000;
11502 if (off + isec->size > limit)
bf102f86
AM
11503 {
11504 addr = (htab->toc_first_sec->output_offset
11505 + htab->toc_first_sec->output_section->vma);
11506 htab->toc_curr = addr;
a27e685f 11507 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11508 }
99877b66 11509
927be08e
AM
11510 /* toc_curr is the base address of this toc group. Set elf_gp
11511 for the input section to be the offset relative to the
11512 output toc base plus 0x8000. Making the input elf_gp an
11513 offset allows us to move the toc as a whole without
11514 recalculating input elf_gp. */
11515 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11516 off += TOC_BASE_OFF;
11517
11518 /* Die if someone uses a linker script that doesn't keep input
11519 file .toc and .got together. */
a4fd3de5
AM
11520 if (new_bfd
11521 && elf_gp (isec->owner) != 0
927be08e
AM
11522 && elf_gp (isec->owner) != off)
11523 return FALSE;
11524
11525 elf_gp (isec->owner) = off;
11526 return TRUE;
4c52953f 11527 }
927be08e
AM
11528
11529 /* During the second pass toc_first_sec points to the start of
11530 a toc group, and toc_curr is used to track the old elf_gp.
11531 We use toc_bfd to ensure we only look at each bfd once. */
11532 if (htab->toc_bfd == isec->owner)
11533 return TRUE;
11534 htab->toc_bfd = isec->owner;
11535
11536 if (htab->toc_first_sec == NULL
11537 || htab->toc_curr != elf_gp (isec->owner))
11538 {
11539 htab->toc_curr = elf_gp (isec->owner);
11540 htab->toc_first_sec = isec;
11541 }
11542 addr = (htab->toc_first_sec->output_offset
11543 + htab->toc_first_sec->output_section->vma);
11544 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11545 elf_gp (isec->owner) = off;
11546
11547 return TRUE;
ad8e1ba5
AM
11548}
11549
927be08e
AM
11550/* Called via elf_link_hash_traverse to merge GOT entries for global
11551 symbol H. */
11552
11553static bfd_boolean
11554merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11555{
11556 if (h->root.type == bfd_link_hash_indirect)
11557 return TRUE;
11558
927be08e
AM
11559 merge_got_entries (&h->got.glist);
11560
11561 return TRUE;
11562}
11563
11564/* Called via elf_link_hash_traverse to allocate GOT entries for global
11565 symbol H. */
11566
11567static bfd_boolean
11568reallocate_got (struct elf_link_hash_entry *h, void *inf)
11569{
11570 struct got_entry *gent;
11571
11572 if (h->root.type == bfd_link_hash_indirect)
11573 return TRUE;
11574
927be08e
AM
11575 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11576 if (!gent->is_indirect)
11577 allocate_got (h, (struct bfd_link_info *) inf, gent);
11578 return TRUE;
11579}
11580
11581/* Called on the first multitoc pass after the last call to
11582 ppc64_elf_next_toc_section. This function removes duplicate GOT
11583 entries. */
11584
11585bfd_boolean
11586ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11587{
11588 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11589 struct bfd *ibfd, *ibfd2;
11590 bfd_boolean done_something;
11591
11592 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11593
7865406b
AM
11594 if (!htab->do_multi_toc)
11595 return FALSE;
11596
d0fae19d 11597 /* Merge global sym got entries within a toc group. */
927be08e
AM
11598 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11599
11600 /* And tlsld_got. */
c72f2fb2 11601 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11602 {
11603 struct got_entry *ent, *ent2;
11604
11605 if (!is_ppc64_elf (ibfd))
11606 continue;
11607
11608 ent = ppc64_tlsld_got (ibfd);
11609 if (!ent->is_indirect
11610 && ent->got.offset != (bfd_vma) -1)
11611 {
c72f2fb2 11612 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11613 {
11614 if (!is_ppc64_elf (ibfd2))
11615 continue;
11616
11617 ent2 = ppc64_tlsld_got (ibfd2);
11618 if (!ent2->is_indirect
11619 && ent2->got.offset != (bfd_vma) -1
11620 && elf_gp (ibfd2) == elf_gp (ibfd))
11621 {
11622 ent2->is_indirect = TRUE;
11623 ent2->got.ent = ent;
11624 }
11625 }
11626 }
11627 }
11628
11629 /* Zap sizes of got sections. */
33e44f2e
AM
11630 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11631 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11632 htab->got_reli_size = 0;
11633
c72f2fb2 11634 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11635 {
11636 asection *got, *relgot;
11637
11638 if (!is_ppc64_elf (ibfd))
11639 continue;
11640
11641 got = ppc64_elf_tdata (ibfd)->got;
11642 if (got != NULL)
11643 {
11644 got->rawsize = got->size;
11645 got->size = 0;
11646 relgot = ppc64_elf_tdata (ibfd)->relgot;
11647 relgot->rawsize = relgot->size;
11648 relgot->size = 0;
11649 }
11650 }
11651
11652 /* Now reallocate the got, local syms first. We don't need to
11653 allocate section contents again since we never increase size. */
c72f2fb2 11654 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11655 {
11656 struct got_entry **lgot_ents;
11657 struct got_entry **end_lgot_ents;
11658 struct plt_entry **local_plt;
11659 struct plt_entry **end_local_plt;
f961d9dd 11660 unsigned char *lgot_masks;
927be08e
AM
11661 bfd_size_type locsymcount;
11662 Elf_Internal_Shdr *symtab_hdr;
19e08130 11663 asection *s;
927be08e
AM
11664
11665 if (!is_ppc64_elf (ibfd))
11666 continue;
11667
11668 lgot_ents = elf_local_got_ents (ibfd);
11669 if (!lgot_ents)
11670 continue;
11671
11672 symtab_hdr = &elf_symtab_hdr (ibfd);
11673 locsymcount = symtab_hdr->sh_info;
11674 end_lgot_ents = lgot_ents + locsymcount;
11675 local_plt = (struct plt_entry **) end_lgot_ents;
11676 end_local_plt = local_plt + locsymcount;
f961d9dd 11677 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11678 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11679 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11680 {
11681 struct got_entry *ent;
11682
11683 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11684 {
19e08130
AM
11685 unsigned int ent_size = 8;
11686 unsigned int rel_size = sizeof (Elf64_External_Rela);
11687
d0fae19d
AM
11688 ent->got.offset = s->size;
11689 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11690 {
19e08130
AM
11691 ent_size *= 2;
11692 rel_size *= 2;
11693 }
11694 s->size += ent_size;
11695 if ((*lgot_masks & PLT_IFUNC) != 0)
11696 {
33e44f2e 11697 htab->elf.irelplt->size += rel_size;
19e08130
AM
11698 htab->got_reli_size += rel_size;
11699 }
0e1862bb 11700 else if (bfd_link_pic (info))
19e08130
AM
11701 {
11702 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11703 srel->size += rel_size;
d0fae19d
AM
11704 }
11705 }
927be08e
AM
11706 }
11707 }
11708
11709 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11710
c72f2fb2 11711 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11712 {
11713 struct got_entry *ent;
11714
11715 if (!is_ppc64_elf (ibfd))
11716 continue;
11717
11718 ent = ppc64_tlsld_got (ibfd);
11719 if (!ent->is_indirect
11720 && ent->got.offset != (bfd_vma) -1)
11721 {
11722 asection *s = ppc64_elf_tdata (ibfd)->got;
11723 ent->got.offset = s->size;
11724 s->size += 16;
0e1862bb 11725 if (bfd_link_pic (info))
927be08e
AM
11726 {
11727 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11728 srel->size += sizeof (Elf64_External_Rela);
11729 }
11730 }
11731 }
11732
33e44f2e 11733 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11734 if (!done_something)
c72f2fb2 11735 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11736 {
11737 asection *got;
11738
11739 if (!is_ppc64_elf (ibfd))
11740 continue;
11741
11742 got = ppc64_elf_tdata (ibfd)->got;
11743 if (got != NULL)
11744 {
11745 done_something = got->rawsize != got->size;
11746 if (done_something)
11747 break;
11748 }
11749 }
11750
11751 if (done_something)
e7d1c40c 11752 (*htab->params->layout_sections_again) ();
927be08e
AM
11753
11754 /* Set up for second pass over toc sections to recalculate elf_gp
11755 on input sections. */
11756 htab->toc_bfd = NULL;
11757 htab->toc_first_sec = NULL;
11758 htab->second_toc_pass = TRUE;
11759 return done_something;
11760}
11761
11762/* Called after second pass of multitoc partitioning. */
11763
11764void
11765ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11766{
11767 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11768
11769 /* After the second pass, toc_curr tracks the TOC offset used
11770 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11771 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11772}
11773
9b5ecbd0
AM
11774/* No toc references were found in ISEC. If the code in ISEC makes no
11775 calls, then there's no need to use toc adjusting stubs when branching
11776 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11777 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11778 needed, and 2 if a cyclical call-graph was found but no other reason
11779 for a stub was detected. If called from the top level, a return of
11780 2 means the same as a return of 0. */
9b5ecbd0
AM
11781
11782static int
4ce794b7 11783toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11784{
9b5ecbd0 11785 int ret;
70cc837d
AM
11786
11787 /* Mark this section as checked. */
11788 isec->call_check_done = 1;
9b5ecbd0 11789
772119ce
AM
11790 /* We know none of our code bearing sections will need toc stubs. */
11791 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11792 return 0;
11793
eea6121a 11794 if (isec->size == 0)
082c50f8
AM
11795 return 0;
11796
4c52953f
AM
11797 if (isec->output_section == NULL)
11798 return 0;
11799
4c52953f 11800 ret = 0;
70cc837d 11801 if (isec->reloc_count != 0)
9b5ecbd0 11802 {
70cc837d
AM
11803 Elf_Internal_Rela *relstart, *rel;
11804 Elf_Internal_Sym *local_syms;
11805 struct ppc_link_hash_table *htab;
2917689a 11806
70cc837d
AM
11807 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11808 info->keep_memory);
11809 if (relstart == NULL)
11810 return -1;
90aecf7a 11811
70cc837d
AM
11812 /* Look for branches to outside of this section. */
11813 local_syms = NULL;
11814 htab = ppc_hash_table (info);
11815 if (htab == NULL)
11816 return -1;
4c52953f 11817
70cc837d 11818 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11819 {
70cc837d
AM
11820 enum elf_ppc64_reloc_type r_type;
11821 unsigned long r_symndx;
11822 struct elf_link_hash_entry *h;
11823 struct ppc_link_hash_entry *eh;
11824 Elf_Internal_Sym *sym;
11825 asection *sym_sec;
11826 struct _opd_sec_data *opd;
11827 bfd_vma sym_value;
11828 bfd_vma dest;
11829
11830 r_type = ELF64_R_TYPE (rel->r_info);
11831 if (r_type != R_PPC64_REL24
11832 && r_type != R_PPC64_REL14
11833 && r_type != R_PPC64_REL14_BRTAKEN
11834 && r_type != R_PPC64_REL14_BRNTAKEN)
11835 continue;
4c52953f 11836
70cc837d
AM
11837 r_symndx = ELF64_R_SYM (rel->r_info);
11838 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11839 isec->owner))
4c52953f 11840 {
70cc837d
AM
11841 ret = -1;
11842 break;
11843 }
4c52953f 11844
70cc837d
AM
11845 /* Calls to dynamic lib functions go through a plt call stub
11846 that uses r2. */
11847 eh = (struct ppc_link_hash_entry *) h;
11848 if (eh != NULL
11849 && (eh->elf.plt.plist != NULL
11850 || (eh->oh != NULL
11851 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11852 {
11853 ret = 1;
11854 break;
4c52953f
AM
11855 }
11856
70cc837d
AM
11857 if (sym_sec == NULL)
11858 /* Ignore other undefined symbols. */
4c52953f 11859 continue;
4c52953f 11860
70cc837d
AM
11861 /* Assume branches to other sections not included in the
11862 link need stubs too, to cover -R and absolute syms. */
11863 if (sym_sec->output_section == NULL)
11864 {
11865 ret = 1;
11866 break;
11867 }
4c52953f 11868
70cc837d
AM
11869 if (h == NULL)
11870 sym_value = sym->st_value;
11871 else
11872 {
11873 if (h->root.type != bfd_link_hash_defined
11874 && h->root.type != bfd_link_hash_defweak)
11875 abort ();
11876 sym_value = h->root.u.def.value;
11877 }
11878 sym_value += rel->r_addend;
4c52953f 11879
70cc837d
AM
11880 /* If this branch reloc uses an opd sym, find the code section. */
11881 opd = get_opd_info (sym_sec);
11882 if (opd != NULL)
11883 {
11884 if (h == NULL && opd->adjust != NULL)
11885 {
11886 long adjust;
4c52953f 11887
92a9c616 11888 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11889 if (adjust == -1)
11890 /* Assume deleted functions won't ever be called. */
11891 continue;
11892 sym_value += adjust;
11893 }
4c52953f 11894
aef36ac1
AM
11895 dest = opd_entry_value (sym_sec, sym_value,
11896 &sym_sec, NULL, FALSE);
70cc837d
AM
11897 if (dest == (bfd_vma) -1)
11898 continue;
11899 }
11900 else
11901 dest = (sym_value
11902 + sym_sec->output_offset
11903 + sym_sec->output_section->vma);
4c52953f 11904
70cc837d
AM
11905 /* Ignore branch to self. */
11906 if (sym_sec == isec)
11907 continue;
4c52953f 11908
70cc837d
AM
11909 /* If the called function uses the toc, we need a stub. */
11910 if (sym_sec->has_toc_reloc
11911 || sym_sec->makes_toc_func_call)
4c52953f 11912 {
70cc837d 11913 ret = 1;
4c52953f
AM
11914 break;
11915 }
70cc837d
AM
11916
11917 /* Assume any branch that needs a long branch stub might in fact
11918 need a plt_branch stub. A plt_branch stub uses r2. */
11919 else if (dest - (isec->output_offset
11920 + isec->output_section->vma
6911b7dc
AM
11921 + rel->r_offset) + (1 << 25)
11922 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11923 ? h->other
11924 : sym->st_other))
4c52953f 11925 {
70cc837d
AM
11926 ret = 1;
11927 break;
11928 }
11929
11930 /* If calling back to a section in the process of being
11931 tested, we can't say for sure that no toc adjusting stubs
11932 are needed, so don't return zero. */
11933 else if (sym_sec->call_check_in_progress)
11934 ret = 2;
11935
11936 /* Branches to another section that itself doesn't have any TOC
11937 references are OK. Recursively call ourselves to check. */
11938 else if (!sym_sec->call_check_done)
11939 {
11940 int recur;
11941
11942 /* Mark current section as indeterminate, so that other
11943 sections that call back to current won't be marked as
11944 known. */
11945 isec->call_check_in_progress = 1;
11946 recur = toc_adjusting_stub_needed (info, sym_sec);
11947 isec->call_check_in_progress = 0;
11948
4c52953f
AM
11949 if (recur != 0)
11950 {
70cc837d
AM
11951 ret = recur;
11952 if (recur != 2)
11953 break;
4c52953f
AM
11954 }
11955 }
4c52953f 11956 }
70cc837d
AM
11957
11958 if (local_syms != NULL
11959 && (elf_symtab_hdr (isec->owner).contents
11960 != (unsigned char *) local_syms))
11961 free (local_syms);
11962 if (elf_section_data (isec)->relocs != relstart)
11963 free (relstart);
9b5ecbd0
AM
11964 }
11965
70cc837d
AM
11966 if ((ret & 1) == 0
11967 && isec->map_head.s != NULL
11968 && (strcmp (isec->output_section->name, ".init") == 0
11969 || strcmp (isec->output_section->name, ".fini") == 0))
11970 {
11971 if (isec->map_head.s->has_toc_reloc
11972 || isec->map_head.s->makes_toc_func_call)
11973 ret = 1;
11974 else if (!isec->map_head.s->call_check_done)
11975 {
11976 int recur;
11977 isec->call_check_in_progress = 1;
11978 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11979 isec->call_check_in_progress = 0;
11980 if (recur != 0)
11981 ret = recur;
11982 }
11983 }
11984
11985 if (ret == 1)
11986 isec->makes_toc_func_call = 1;
4c52953f 11987
9b5ecbd0
AM
11988 return ret;
11989}
11990
721956f4
AM
11991/* The linker repeatedly calls this function for each input section,
11992 in the order that input sections are linked into output sections.
11993 Build lists of input sections to determine groupings between which
11994 we may insert linker stubs. */
11995
9b5ecbd0 11996bfd_boolean
4ce794b7 11997ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11998{
11999 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12000
4dfe6ac6
NC
12001 if (htab == NULL)
12002 return FALSE;
12003
734b6cf9 12004 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12005 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12006 {
3d6f9012
AM
12007 /* This happens to make the list in reverse order,
12008 which is what we want. */
6f20ed8a
AM
12009 htab->sec_info[isec->id].u.list
12010 = htab->sec_info[isec->output_section->id].u.list;
12011 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12012 }
ad8e1ba5 12013
4c52953f 12014 if (htab->multi_toc_needed)
9b5ecbd0 12015 {
8b974ba3
AM
12016 /* Analyse sections that aren't already flagged as needing a
12017 valid toc pointer. Exclude .fixup for the linux kernel.
12018 .fixup contains branches, but only back to the function that
12019 hit an exception. */
12020 if (!(isec->has_toc_reloc
12021 || (isec->flags & SEC_CODE) == 0
12022 || strcmp (isec->name, ".fixup") == 0
12023 || isec->call_check_done))
12024 {
12025 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12026 return FALSE;
8b974ba3
AM
12027 }
12028 /* Make all sections use the TOC assigned for this object file.
12029 This will be wrong for pasted sections; We fix that in
12030 check_pasted_section(). */
12031 if (elf_gp (isec->owner) != 0)
12032 htab->toc_curr = elf_gp (isec->owner);
12033 }
12034
6f20ed8a 12035 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12036 return TRUE;
721956f4
AM
12037}
12038
70cc837d
AM
12039/* Check that all .init and .fini sections use the same toc, if they
12040 have toc relocs. */
12041
12042static bfd_boolean
12043check_pasted_section (struct bfd_link_info *info, const char *name)
12044{
12045 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12046
12047 if (o != NULL)
12048 {
12049 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12050 bfd_vma toc_off = 0;
12051 asection *i;
12052
12053 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12054 if (i->has_toc_reloc)
12055 {
12056 if (toc_off == 0)
6f20ed8a
AM
12057 toc_off = htab->sec_info[i->id].toc_off;
12058 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12059 return FALSE;
12060 }
6683a28d
AM
12061
12062 if (toc_off == 0)
12063 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12064 if (i->makes_toc_func_call)
12065 {
6f20ed8a 12066 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12067 break;
12068 }
12069
70cc837d
AM
12070 /* Make sure the whole pasted function uses the same toc offset. */
12071 if (toc_off != 0)
12072 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12073 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12074 }
12075 return TRUE;
12076}
12077
12078bfd_boolean
12079ppc64_elf_check_init_fini (struct bfd_link_info *info)
12080{
12081 return (check_pasted_section (info, ".init")
12082 & check_pasted_section (info, ".fini"));
12083}
12084
721956f4
AM
12085/* See whether we can group stub sections together. Grouping stub
12086 sections may result in fewer stubs. More importantly, we need to
12087 put all .init* and .fini* stubs at the beginning of the .init or
12088 .fini output sections respectively, because glibc splits the
12089 _init and _fini functions into multiple parts. Putting a stub in
12090 the middle of a function is not a good idea. */
12091
6f20ed8a
AM
12092static bfd_boolean
12093group_sections (struct bfd_link_info *info,
4ce794b7
AM
12094 bfd_size_type stub_group_size,
12095 bfd_boolean stubs_always_before_branch)
721956f4 12096{
6f20ed8a
AM
12097 struct ppc_link_hash_table *htab;
12098 asection *osec;
7c8fe5c4
AM
12099 bfd_boolean suppress_size_errors;
12100
6f20ed8a
AM
12101 htab = ppc_hash_table (info);
12102 if (htab == NULL)
12103 return FALSE;
12104
7c8fe5c4 12105 suppress_size_errors = FALSE;
7c8fe5c4
AM
12106 if (stub_group_size == 1)
12107 {
12108 /* Default values. */
12109 if (stubs_always_before_branch)
09f92717 12110 stub_group_size = 0x1e00000;
7c8fe5c4 12111 else
09f92717 12112 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12113 suppress_size_errors = TRUE;
12114 }
12115
6f20ed8a 12116 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12117 {
6f20ed8a
AM
12118 asection *tail;
12119
12120 if (osec->id >= htab->sec_info_arr_size)
12121 continue;
12122
12123 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12124 while (tail != NULL)
721956f4 12125 {
734b6cf9
AM
12126 asection *curr;
12127 asection *prev;
12128 bfd_size_type total;
12129 bfd_boolean big_sec;
12130 bfd_vma curr_toc;
6f20ed8a 12131 struct map_stub *group;
09f92717 12132 bfd_size_type group_size;
734b6cf9
AM
12133
12134 curr = tail;
eea6121a 12135 total = tail->size;
09f92717
AM
12136 group_size = (ppc64_elf_section_data (tail) != NULL
12137 && ppc64_elf_section_data (tail)->has_14bit_branch
12138 ? stub_group_size >> 10 : stub_group_size);
12139
12140 big_sec = total > group_size;
7c8fe5c4 12141 if (big_sec && !suppress_size_errors)
695344c0 12142 /* xgettext:c-format */
4eca0228
AM
12143 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12144 tail->owner, tail);
6f20ed8a 12145 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12146
6f20ed8a 12147 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12148 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12149 < (ppc64_elf_section_data (prev) != NULL
12150 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12151 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12152 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12153 curr = prev;
12154
12155 /* OK, the size from the start of CURR to the end is less
09f92717 12156 than group_size and thus can be handled by one stub
734b6cf9 12157 section. (or the tail section is itself larger than
09f92717
AM
12158 group_size, in which case we may be toast.) We should
12159 really be keeping track of the total size of stubs added
12160 here, as stubs contribute to the final output section
12161 size. That's a little tricky, and this way will only
12162 break if stubs added make the total size more than 2^25,
12163 ie. for the default stub_group_size, if stubs total more
12164 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12165 group = bfd_alloc (curr->owner, sizeof (*group));
12166 if (group == NULL)
12167 return FALSE;
12168 group->link_sec = curr;
12169 group->stub_sec = NULL;
a4b6fadd
AM
12170 group->needs_save_res = 0;
12171 group->next = htab->group;
12172 htab->group = group;
734b6cf9 12173 do
721956f4 12174 {
6f20ed8a 12175 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12176 /* Set up this stub group. */
6f20ed8a 12177 htab->sec_info[tail->id].u.group = group;
721956f4 12178 }
734b6cf9
AM
12179 while (tail != curr && (tail = prev) != NULL);
12180
09f92717 12181 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12182 bytes before the stub section can be handled by it too.
12183 Don't do this if we have a really large section after the
12184 stubs, as adding more stubs increases the chance that
12185 branches may not reach into the stub section. */
12186 if (!stubs_always_before_branch && !big_sec)
12187 {
12188 total = 0;
12189 while (prev != NULL
12190 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12191 < (ppc64_elf_section_data (prev) != NULL
12192 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12193 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12194 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12195 {
12196 tail = prev;
6f20ed8a
AM
12197 prev = htab->sec_info[tail->id].u.list;
12198 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12199 }
12200 }
12201 tail = prev;
721956f4
AM
12202 }
12203 }
6f20ed8a 12204 return TRUE;
721956f4
AM
12205}
12206
58d180e8
AM
12207static const unsigned char glink_eh_frame_cie[] =
12208{
12209 0, 0, 0, 16, /* length. */
12210 0, 0, 0, 0, /* id. */
12211 1, /* CIE version. */
12212 'z', 'R', 0, /* Augmentation string. */
12213 4, /* Code alignment. */
12214 0x78, /* Data alignment. */
12215 65, /* RA reg. */
12216 1, /* Augmentation size. */
12217 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
da44f4e5
AM
12218 DW_CFA_def_cfa, 1, 0, /* def_cfa: r1 offset 0. */
12219 0, 0, 0, 0
58d180e8
AM
12220};
12221
d969d15f
AM
12222/* Stripping output sections is normally done before dynamic section
12223 symbols have been allocated. This function is called later, and
12224 handles cases like htab->brlt which is mapped to its own output
12225 section. */
12226
12227static void
12228maybe_strip_output (struct bfd_link_info *info, asection *isec)
12229{
12230 if (isec->size == 0
12231 && isec->output_section->size == 0
53d8967a 12232 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12233 && !bfd_section_removed_from_list (info->output_bfd,
12234 isec->output_section)
12235 && elf_section_data (isec->output_section)->dynindx == 0)
12236 {
12237 isec->output_section->flags |= SEC_EXCLUDE;
12238 bfd_section_list_remove (info->output_bfd, isec->output_section);
12239 info->output_bfd->section_count--;
12240 }
12241}
12242
721956f4
AM
12243/* Determine and set the size of the stub section for a final link.
12244
12245 The basic idea here is to examine all the relocations looking for
12246 PC-relative calls to a target that is unreachable with a "bl"
12247 instruction. */
12248
b34976b6 12249bfd_boolean
e7d1c40c 12250ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12251{
12252 bfd_size_type stub_group_size;
b34976b6 12253 bfd_boolean stubs_always_before_branch;
721956f4
AM
12254 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12255
4dfe6ac6
NC
12256 if (htab == NULL)
12257 return FALSE;
12258
0e1862bb 12259 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12260 htab->params->plt_thread_safe = 1;
b9e5796b 12261 if (!htab->opd_abi)
e7d1c40c
AM
12262 htab->params->plt_thread_safe = 0;
12263 else if (htab->params->plt_thread_safe == -1)
794e51c0 12264 {
e2458743 12265 static const char *const thread_starter[] =
794e51c0
AM
12266 {
12267 "pthread_create",
12268 /* libstdc++ */
12269 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12270 /* librt */
12271 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12272 "mq_notify", "create_timer",
12273 /* libanl */
12274 "getaddrinfo_a",
12275 /* libgomp */
2300b5a1 12276 "GOMP_parallel",
794e51c0 12277 "GOMP_parallel_start",
2300b5a1 12278 "GOMP_parallel_loop_static",
794e51c0 12279 "GOMP_parallel_loop_static_start",
2300b5a1 12280 "GOMP_parallel_loop_dynamic",
794e51c0 12281 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12282 "GOMP_parallel_loop_guided",
794e51c0 12283 "GOMP_parallel_loop_guided_start",
2300b5a1 12284 "GOMP_parallel_loop_runtime",
794e51c0 12285 "GOMP_parallel_loop_runtime_start",
2300b5a1 12286 "GOMP_parallel_sections",
68ffbac6 12287 "GOMP_parallel_sections_start",
f9dffbf0
AM
12288 /* libgo */
12289 "__go_go",
794e51c0
AM
12290 };
12291 unsigned i;
12292
a4b6fadd 12293 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12294 {
12295 struct elf_link_hash_entry *h;
12296 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12297 FALSE, FALSE, TRUE);
e7d1c40c
AM
12298 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12299 if (htab->params->plt_thread_safe)
794e51c0
AM
12300 break;
12301 }
12302 }
e7d1c40c
AM
12303 stubs_always_before_branch = htab->params->group_size < 0;
12304 if (htab->params->group_size < 0)
12305 stub_group_size = -htab->params->group_size;
721956f4 12306 else
e7d1c40c 12307 stub_group_size = htab->params->group_size;
721956f4 12308
6f20ed8a
AM
12309 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12310 return FALSE;
721956f4 12311
c9301e31
AM
12312#define STUB_SHRINK_ITER 20
12313 /* Loop until no stubs added. After iteration 20 of this loop we may
12314 exit on a stub section shrinking. This is to break out of a
12315 pathological case where adding stubs on one iteration decreases
12316 section gaps (perhaps due to alignment), which then requires
12317 fewer or smaller stubs on the next iteration. */
12318
721956f4
AM
12319 while (1)
12320 {
12321 bfd *input_bfd;
12322 unsigned int bfd_indx;
a4b6fadd 12323 struct map_stub *group;
721956f4 12324 asection *stub_sec;
721956f4
AM
12325
12326 htab->stub_iteration += 1;
721956f4
AM
12327
12328 for (input_bfd = info->input_bfds, bfd_indx = 0;
12329 input_bfd != NULL;
c72f2fb2 12330 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12331 {
12332 Elf_Internal_Shdr *symtab_hdr;
12333 asection *section;
6cdc0ccc 12334 Elf_Internal_Sym *local_syms = NULL;
721956f4 12335
0c8d6e5c 12336 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12337 continue;
12338
721956f4 12339 /* We'll need the symbol table in a second. */
0ffa91dd 12340 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12341 if (symtab_hdr->sh_info == 0)
12342 continue;
12343
721956f4
AM
12344 /* Walk over each section attached to the input bfd. */
12345 for (section = input_bfd->sections;
12346 section != NULL;
12347 section = section->next)
12348 {
721956f4 12349 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12350
12351 /* If there aren't any relocs, then there's nothing more
12352 to do. */
12353 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12354 || (section->flags & SEC_ALLOC) == 0
12355 || (section->flags & SEC_LOAD) == 0
12356 || (section->flags & SEC_CODE) == 0
721956f4
AM
12357 || section->reloc_count == 0)
12358 continue;
12359
12360 /* If this section is a link-once section that will be
12361 discarded, then don't create any stubs. */
12362 if (section->output_section == NULL
927be08e 12363 || section->output_section->owner != info->output_bfd)
721956f4
AM
12364 continue;
12365
1e2f5b6e
AM
12366 /* Get the relocs. */
12367 internal_relocs
4ce794b7 12368 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12369 info->keep_memory);
721956f4 12370 if (internal_relocs == NULL)
1e2f5b6e 12371 goto error_ret_free_local;
721956f4
AM
12372
12373 /* Now examine each relocation. */
12374 irela = internal_relocs;
12375 irelaend = irela + section->reloc_count;
12376 for (; irela < irelaend; irela++)
12377 {
4ce794b7
AM
12378 enum elf_ppc64_reloc_type r_type;
12379 unsigned int r_indx;
721956f4
AM
12380 enum ppc_stub_type stub_type;
12381 struct ppc_stub_hash_entry *stub_entry;
8387904d 12382 asection *sym_sec, *code_sec;
e054468f 12383 bfd_vma sym_value, code_value;
721956f4 12384 bfd_vma destination;
6911b7dc 12385 unsigned long local_off;
8843416a 12386 bfd_boolean ok_dest;
721956f4 12387 struct ppc_link_hash_entry *hash;
8387904d 12388 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12389 struct elf_link_hash_entry *h;
12390 Elf_Internal_Sym *sym;
721956f4
AM
12391 char *stub_name;
12392 const asection *id_sec;
74f0fb50 12393 struct _opd_sec_data *opd;
e054468f 12394 struct plt_entry *plt_ent;
721956f4
AM
12395
12396 r_type = ELF64_R_TYPE (irela->r_info);
12397 r_indx = ELF64_R_SYM (irela->r_info);
12398
4ce794b7 12399 if (r_type >= R_PPC64_max)
721956f4
AM
12400 {
12401 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12402 goto error_ret_free_internal;
721956f4
AM
12403 }
12404
12405 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12406 if (r_type != R_PPC64_REL24
12407 && r_type != R_PPC64_REL14
12408 && r_type != R_PPC64_REL14_BRTAKEN
12409 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12410 continue;
12411
12412 /* Now determine the call target, its name, value,
12413 section. */
411e1bfb
AM
12414 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12415 r_indx, input_bfd))
12416 goto error_ret_free_internal;
12417 hash = (struct ppc_link_hash_entry *) h;
12418
8843416a 12419 ok_dest = FALSE;
8387904d 12420 fdh = NULL;
7fe2b9a6 12421 sym_value = 0;
411e1bfb 12422 if (hash == NULL)
721956f4 12423 {
411e1bfb 12424 sym_value = sym->st_value;
c27b8c2a
AM
12425 if (sym_sec != NULL
12426 && sym_sec->output_section != NULL)
12427 ok_dest = TRUE;
721956f4 12428 }
7fe2b9a6
AM
12429 else if (hash->elf.root.type == bfd_link_hash_defined
12430 || hash->elf.root.type == bfd_link_hash_defweak)
12431 {
12432 sym_value = hash->elf.root.u.def.value;
12433 if (sym_sec->output_section != NULL)
12434 ok_dest = TRUE;
12435 }
12436 else if (hash->elf.root.type == bfd_link_hash_undefweak
12437 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12438 {
99877b66 12439 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12440 use the func descriptor sym instead if it is
12441 defined. */
ceb1f1ef 12442 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12443 && hash->oh != NULL)
8387904d 12444 {
8c5b4e52 12445 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12446 if (fdh->elf.root.type == bfd_link_hash_defined
12447 || fdh->elf.root.type == bfd_link_hash_defweak)
12448 {
12449 sym_sec = fdh->elf.root.u.def.section;
12450 sym_value = fdh->elf.root.u.def.value;
12451 if (sym_sec->output_section != NULL)
12452 ok_dest = TRUE;
12453 }
99877b66
AM
12454 else
12455 fdh = NULL;
8387904d 12456 }
7fe2b9a6
AM
12457 }
12458 else
12459 {
12460 bfd_set_error (bfd_error_bad_value);
12461 goto error_ret_free_internal;
721956f4
AM
12462 }
12463
8843416a 12464 destination = 0;
6911b7dc 12465 local_off = 0;
8843416a
AM
12466 if (ok_dest)
12467 {
12468 sym_value += irela->r_addend;
12469 destination = (sym_value
12470 + sym_sec->output_offset
12471 + sym_sec->output_section->vma);
6911b7dc
AM
12472 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12473 ? hash->elf.other
12474 : sym->st_other);
8843416a
AM
12475 }
12476
8387904d 12477 code_sec = sym_sec;
e054468f 12478 code_value = sym_value;
74f0fb50
AM
12479 opd = get_opd_info (sym_sec);
12480 if (opd != NULL)
8387904d
AM
12481 {
12482 bfd_vma dest;
12483
74f0fb50 12484 if (hash == NULL && opd->adjust != NULL)
8387904d 12485 {
51aecdc5 12486 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12487 if (adjust == -1)
12488 continue;
e054468f 12489 code_value += adjust;
8387904d
AM
12490 sym_value += adjust;
12491 }
12492 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12493 &code_sec, &code_value, FALSE);
8387904d
AM
12494 if (dest != (bfd_vma) -1)
12495 {
12496 destination = dest;
12497 if (fdh != NULL)
12498 {
12499 /* Fixup old ABI sym to point at code
12500 entry. */
99877b66 12501 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12502 hash->elf.root.u.def.section = code_sec;
e054468f 12503 hash->elf.root.u.def.value = code_value;
8387904d
AM
12504 }
12505 }
12506 }
12507
721956f4 12508 /* Determine what (if any) linker stub is needed. */
e054468f 12509 plt_ent = NULL;
721956f4 12510 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12511 &plt_ent, destination,
12512 local_off);
ad8e1ba5
AM
12513
12514 if (stub_type != ppc_stub_plt_call)
12515 {
12516 /* Check whether we need a TOC adjusting stub.
12517 Since the linker pastes together pieces from
12518 different object files when creating the
12519 _init and _fini functions, it may be that a
12520 call to what looks like a local sym is in
12521 fact a call needing a TOC adjustment. */
8387904d
AM
12522 if (code_sec != NULL
12523 && code_sec->output_section != NULL
6f20ed8a
AM
12524 && (htab->sec_info[code_sec->id].toc_off
12525 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12526 && (code_sec->has_toc_reloc
12527 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12528 stub_type = ppc_stub_long_branch_r2off;
12529 }
12530
721956f4
AM
12531 if (stub_type == ppc_stub_none)
12532 continue;
12533
411e1bfb
AM
12534 /* __tls_get_addr calls might be eliminated. */
12535 if (stub_type != ppc_stub_plt_call
12536 && hash != NULL
8387904d
AM
12537 && (hash == htab->tls_get_addr
12538 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12539 && section->has_tls_reloc
12540 && irela != internal_relocs)
12541 {
12542 /* Get tls info. */
f961d9dd 12543 unsigned char *tls_mask;
411e1bfb 12544
3a71aa26 12545 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12546 irela - 1, input_bfd))
12547 goto error_ret_free_internal;
e7b938ca 12548 if (*tls_mask != 0)
411e1bfb
AM
12549 continue;
12550 }
12551
3b421ab3
AM
12552 if (stub_type == ppc_stub_plt_call
12553 && irela + 1 < irelaend
12554 && irela[1].r_offset == irela->r_offset + 4
794e51c0
AM
12555 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12556 {
12557 if (!tocsave_find (htab, INSERT,
12558 &local_syms, irela + 1, input_bfd))
12559 goto error_ret_free_internal;
12560 }
12561 else if (stub_type == ppc_stub_plt_call)
12562 stub_type = ppc_stub_plt_call_r2save;
3b421ab3 12563
721956f4 12564 /* Support for grouping stub sections. */
6f20ed8a 12565 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12566
12567 /* Get the name of this stub. */
12568 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12569 if (!stub_name)
12570 goto error_ret_free_internal;
12571
12572 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12573 stub_name, FALSE, FALSE);
721956f4
AM
12574 if (stub_entry != NULL)
12575 {
12576 /* The proper stub has already been created. */
12577 free (stub_name);
794e51c0
AM
12578 if (stub_type == ppc_stub_plt_call_r2save)
12579 stub_entry->stub_type = stub_type;
721956f4
AM
12580 continue;
12581 }
12582
25f53a85 12583 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12584 if (stub_entry == NULL)
12585 {
12586 free (stub_name);
6cdc0ccc
AM
12587 error_ret_free_internal:
12588 if (elf_section_data (section)->relocs == NULL)
12589 free (internal_relocs);
12590 error_ret_free_local:
12591 if (local_syms != NULL
12592 && (symtab_hdr->contents
12593 != (unsigned char *) local_syms))
12594 free (local_syms);
b34976b6 12595 return FALSE;
721956f4
AM
12596 }
12597
ad8e1ba5 12598 stub_entry->stub_type = stub_type;
794e51c0
AM
12599 if (stub_type != ppc_stub_plt_call
12600 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12601 {
12602 stub_entry->target_value = code_value;
12603 stub_entry->target_section = code_sec;
12604 }
12605 else
12606 {
12607 stub_entry->target_value = sym_value;
12608 stub_entry->target_section = sym_sec;
12609 }
721956f4 12610 stub_entry->h = hash;
e054468f 12611 stub_entry->plt_ent = plt_ent;
6911b7dc 12612 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12613
12614 if (stub_entry->h != NULL)
12615 htab->stub_globals += 1;
721956f4
AM
12616 }
12617
12618 /* We're done with the internal relocs, free them. */
6cdc0ccc 12619 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12620 free (internal_relocs);
721956f4 12621 }
6cdc0ccc
AM
12622
12623 if (local_syms != NULL
12624 && symtab_hdr->contents != (unsigned char *) local_syms)
12625 {
12626 if (!info->keep_memory)
12627 free (local_syms);
12628 else
12629 symtab_hdr->contents = (unsigned char *) local_syms;
12630 }
721956f4
AM
12631 }
12632
5c3dead3 12633 /* We may have added some stubs. Find out the new size of the
721956f4 12634 stub sections. */
e7d1c40c 12635 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
12636 stub_sec != NULL;
12637 stub_sec = stub_sec->next)
e717da7e 12638 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 12639 {
ea3d7d1c
AM
12640 if (htab->stub_iteration <= STUB_SHRINK_ITER
12641 || stub_sec->rawsize < stub_sec->size)
12642 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12643 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12644 stub_sec->size = 0;
12645 stub_sec->reloc_count = 0;
84f5d08e 12646 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12647 }
eea6121a
AM
12648
12649 htab->brlt->size = 0;
84f5d08e
AM
12650 htab->brlt->reloc_count = 0;
12651 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12652 if (htab->relbrlt != NULL)
eea6121a 12653 htab->relbrlt->size = 0;
721956f4 12654
63bc6f6c 12655 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12656
a4b6fadd
AM
12657 for (group = htab->group; group != NULL; group = group->next)
12658 if (group->needs_save_res)
12659 group->stub_sec->size += htab->sfpr->size;
12660
176a0d42
AM
12661 if (info->emitrelocations
12662 && htab->glink != NULL && htab->glink->size != 0)
12663 {
12664 htab->glink->reloc_count = 1;
12665 htab->glink->flags |= SEC_RELOC;
12666 }
12667
58d180e8
AM
12668 if (htab->glink_eh_frame != NULL
12669 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
9a2a56cc 12670 && htab->glink_eh_frame->output_section->size != 0)
58d180e8 12671 {
4bbe044a 12672 size_t size = 0, align;
58d180e8 12673
e7d1c40c 12674 for (stub_sec = htab->params->stub_bfd->sections;
58d180e8
AM
12675 stub_sec != NULL;
12676 stub_sec = stub_sec->next)
12677 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
da44f4e5 12678 size += 24;
58d180e8
AM
12679 if (htab->glink != NULL && htab->glink->size != 0)
12680 size += 24;
12681 if (size != 0)
12682 size += sizeof (glink_eh_frame_cie);
4bbe044a
AM
12683 align = 1;
12684 align <<= htab->glink_eh_frame->output_section->alignment_power;
12685 align -= 1;
12686 size = (size + align) & ~align;
58d180e8
AM
12687 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12688 htab->glink_eh_frame->size = size;
12689 }
12690
e7d1c40c
AM
12691 if (htab->params->plt_stub_align != 0)
12692 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
12693 stub_sec != NULL;
12694 stub_sec = stub_sec->next)
12695 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
12696 stub_sec->size = ((stub_sec->size
12697 + (1 << htab->params->plt_stub_align) - 1)
29f628db 12698 & -(1 << htab->params->plt_stub_align));
794e51c0 12699
e7d1c40c 12700 for (stub_sec = htab->params->stub_bfd->sections;
5c3dead3
AM
12701 stub_sec != NULL;
12702 stub_sec = stub_sec->next)
12703 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
c9301e31
AM
12704 && stub_sec->rawsize != stub_sec->size
12705 && (htab->stub_iteration <= STUB_SHRINK_ITER
12706 || stub_sec->rawsize < stub_sec->size))
5c3dead3
AM
12707 break;
12708
58d180e8
AM
12709 if (stub_sec == NULL
12710 && (htab->glink_eh_frame == NULL
12711 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12712 break;
12713
721956f4 12714 /* Ask the linker to do its stuff. */
e7d1c40c 12715 (*htab->params->layout_sections_again) ();
721956f4
AM
12716 }
12717
da44f4e5
AM
12718 if (htab->glink_eh_frame != NULL
12719 && htab->glink_eh_frame->size != 0)
12720 {
12721 bfd_vma val;
12722 bfd_byte *p, *last_fde;
12723 size_t last_fde_len, size, align, pad;
12724 asection *stub_sec;
12725
12726 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12727 if (p == NULL)
12728 return FALSE;
12729 htab->glink_eh_frame->contents = p;
12730 last_fde = p;
12731
12732 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12733 /* CIE length (rewrite in case little-endian). */
12734 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12735 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12736 p += sizeof (glink_eh_frame_cie);
12737
12738 for (stub_sec = htab->params->stub_bfd->sections;
12739 stub_sec != NULL;
12740 stub_sec = stub_sec->next)
12741 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12742 {
12743 last_fde = p;
12744 last_fde_len = 20;
12745 /* FDE length. */
12746 bfd_put_32 (htab->elf.dynobj, 20, p);
12747 p += 4;
12748 /* CIE pointer. */
12749 val = p - htab->glink_eh_frame->contents;
12750 bfd_put_32 (htab->elf.dynobj, val, p);
12751 p += 4;
12752 /* Offset to stub section, written later. */
12753 p += 4;
12754 /* stub section size. */
12755 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12756 p += 4;
12757 /* Augmentation. */
12758 p += 1;
12759 /* Pad. */
12760 p += 7;
12761 }
12762 if (htab->glink != NULL && htab->glink->size != 0)
12763 {
12764 last_fde = p;
12765 last_fde_len = 20;
12766 /* FDE length. */
12767 bfd_put_32 (htab->elf.dynobj, 20, p);
12768 p += 4;
12769 /* CIE pointer. */
12770 val = p - htab->glink_eh_frame->contents;
12771 bfd_put_32 (htab->elf.dynobj, val, p);
12772 p += 4;
12773 /* Offset to .glink, written later. */
12774 p += 4;
12775 /* .glink size. */
12776 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12777 p += 4;
12778 /* Augmentation. */
12779 p += 1;
12780
12781 *p++ = DW_CFA_advance_loc + 1;
12782 *p++ = DW_CFA_register;
12783 *p++ = 65;
9f08fa5c 12784 *p++ = htab->opd_abi ? 12 : 0;
da44f4e5
AM
12785 *p++ = DW_CFA_advance_loc + 4;
12786 *p++ = DW_CFA_restore_extended;
12787 *p++ = 65;
12788 }
12789 /* Subsume any padding into the last FDE if user .eh_frame
12790 sections are aligned more than glink_eh_frame. Otherwise any
12791 zero padding will be seen as a terminator. */
12792 size = p - htab->glink_eh_frame->contents;
12793 align = 1;
12794 align <<= htab->glink_eh_frame->output_section->alignment_power;
12795 align -= 1;
12796 pad = ((size + align) & ~align) - size;
12797 htab->glink_eh_frame->size = size + pad;
12798 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12799 }
12800
d969d15f
AM
12801 maybe_strip_output (info, htab->brlt);
12802 if (htab->glink_eh_frame != NULL)
12803 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12804
b34976b6 12805 return TRUE;
721956f4
AM
12806}
12807
12808/* Called after we have determined section placement. If sections
805fc799 12809 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12810
805fc799 12811bfd_vma
1c865ab2 12812ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12813{
805fc799 12814 asection *s;
a27e685f 12815 bfd_vma TOCstart, adjust;
721956f4 12816
43417696
AM
12817 if (info != NULL)
12818 {
12819 struct elf_link_hash_entry *h;
12820 struct elf_link_hash_table *htab = elf_hash_table (info);
12821
12822 if (is_elf_hash_table (htab)
12823 && htab->hgot != NULL)
12824 h = htab->hgot;
12825 else
12826 {
12827 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12828 if (is_elf_hash_table (htab))
12829 htab->hgot = h;
12830 }
12831 if (h != NULL
12832 && h->root.type == bfd_link_hash_defined
12833 && !h->root.linker_def
12834 && (!is_elf_hash_table (htab)
12835 || h->def_regular))
12836 {
12837 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12838 + h->root.u.def.section->output_offset
12839 + h->root.u.def.section->output_section->vma);
12840 _bfd_set_gp_value (obfd, TOCstart);
12841 return TOCstart;
12842 }
12843 }
12844
805fc799
AM
12845 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12846 order. The TOC starts where the first of these sections starts. */
12847 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12848 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12849 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12850 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12851 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12852 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12853 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12854 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12855 {
12856 /* This may happen for
12857 o references to TOC base (SYM@toc / TOC[tc0]) without a
12858 .toc directive
12859 o bad linker script
12860 o --gc-sections and empty TOC sections
12861
12862 FIXME: Warn user? */
12863
12864 /* Look for a likely section. We probably won't even be
12865 using TOCstart. */
12866 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12867 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12868 | SEC_EXCLUDE))
805fc799
AM
12869 == (SEC_ALLOC | SEC_SMALL_DATA))
12870 break;
721956f4 12871 if (s == NULL)
805fc799 12872 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12873 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12874 == (SEC_ALLOC | SEC_SMALL_DATA))
12875 break;
721956f4 12876 if (s == NULL)
805fc799 12877 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12878 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12879 == SEC_ALLOC)
805fc799 12880 break;
721956f4 12881 if (s == NULL)
805fc799 12882 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12883 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12884 break;
12885 }
721956f4 12886
805fc799
AM
12887 TOCstart = 0;
12888 if (s != NULL)
12889 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12890
a27e685f
AM
12891 /* Force alignment. */
12892 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12893 TOCstart -= adjust;
1c865ab2
AM
12894 _bfd_set_gp_value (obfd, TOCstart);
12895
810d4e75 12896 if (info != NULL && s != NULL)
1c865ab2
AM
12897 {
12898 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12899
810d4e75
AM
12900 if (htab != NULL)
12901 {
12902 if (htab->elf.hgot != NULL)
12903 {
a27e685f 12904 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12905 htab->elf.hgot->root.u.def.section = s;
12906 }
12907 }
12908 else
1c865ab2 12909 {
810d4e75
AM
12910 struct bfd_link_hash_entry *bh = NULL;
12911 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12912 s, TOC_BASE_OFF - adjust,
12913 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12914 }
12915 }
805fc799 12916 return TOCstart;
721956f4
AM
12917}
12918
a345bc8d
AM
12919/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12920 write out any global entry stubs. */
12921
12922static bfd_boolean
12923build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12924{
12925 struct bfd_link_info *info;
12926 struct ppc_link_hash_table *htab;
12927 struct plt_entry *pent;
12928 asection *s;
12929
12930 if (h->root.type == bfd_link_hash_indirect)
12931 return TRUE;
12932
12933 if (!h->pointer_equality_needed)
12934 return TRUE;
12935
12936 if (h->def_regular)
12937 return TRUE;
12938
12939 info = inf;
12940 htab = ppc_hash_table (info);
12941 if (htab == NULL)
12942 return FALSE;
12943
12944 s = htab->glink;
12945 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12946 if (pent->plt.offset != (bfd_vma) -1
12947 && pent->addend == 0)
12948 {
12949 bfd_byte *p;
12950 asection *plt;
12951 bfd_vma off;
12952
a345bc8d 12953 p = s->contents + h->root.u.def.value;
33e44f2e 12954 plt = htab->elf.splt;
a345bc8d
AM
12955 if (!htab->elf.dynamic_sections_created
12956 || h->dynindx == -1)
33e44f2e 12957 plt = htab->elf.iplt;
a345bc8d
AM
12958 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12959 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12960
12961 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12962 {
12963 info->callbacks->einfo
12964 (_("%P: linkage table error against `%T'\n"),
12965 h->root.root.string);
12966 bfd_set_error (bfd_error_bad_value);
12967 htab->stub_error = TRUE;
12968 }
12969
7341d5e2
AM
12970 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12971 if (htab->params->emit_stub_syms)
12972 {
12973 size_t len = strlen (h->root.root.string);
12974 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12975
12976 if (name == NULL)
12977 return FALSE;
12978
12979 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12980 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12981 if (h == NULL)
12982 return FALSE;
12983 if (h->root.type == bfd_link_hash_new)
12984 {
12985 h->root.type = bfd_link_hash_defined;
12986 h->root.u.def.section = s;
12987 h->root.u.def.value = p - s->contents;
12988 h->ref_regular = 1;
12989 h->def_regular = 1;
12990 h->ref_regular_nonweak = 1;
12991 h->forced_local = 1;
12992 h->non_elf = 0;
2ec55de3 12993 h->root.linker_def = 1;
7341d5e2
AM
12994 }
12995 }
12996
a345bc8d
AM
12997 if (PPC_HA (off) != 0)
12998 {
12999 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13000 p += 4;
13001 }
13002 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13003 p += 4;
13004 bfd_put_32 (s->owner, MTCTR_R12, p);
13005 p += 4;
13006 bfd_put_32 (s->owner, BCTR, p);
13007 break;
13008 }
13009 return TRUE;
13010}
13011
721956f4
AM
13012/* Build all the stubs associated with the current output file.
13013 The stubs are kept in a hash table attached to the main linker
13014 hash table. This function is called via gldelf64ppc_finish. */
13015
b34976b6 13016bfd_boolean
e7d1c40c 13017ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13018 char **stats)
5d1634d7
AM
13019{
13020 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13021 struct map_stub *group;
721956f4 13022 asection *stub_sec;
5d1634d7 13023 bfd_byte *p;
e717da7e 13024 int stub_sec_count = 0;
5d1634d7 13025
4dfe6ac6
NC
13026 if (htab == NULL)
13027 return FALSE;
13028
eea6121a 13029 /* Allocate memory to hold the linker stubs. */
e7d1c40c 13030 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
13031 stub_sec != NULL;
13032 stub_sec = stub_sec->next)
eea6121a
AM
13033 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
13034 && stub_sec->size != 0)
e717da7e 13035 {
e7d1c40c 13036 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
13037 if (stub_sec->contents == NULL)
13038 return FALSE;
eea6121a 13039 stub_sec->size = 0;
e717da7e 13040 }
5d1634d7 13041
23eb7e01 13042 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13043 {
9f951329 13044 unsigned int indx;
ad8e1ba5 13045 bfd_vma plt0;
9f951329 13046
721956f4 13047 /* Build the .glink plt call stub. */
e7d1c40c 13048 if (htab->params->emit_stub_syms)
97b639ba
AM
13049 {
13050 struct elf_link_hash_entry *h;
468392fb
AM
13051 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13052 TRUE, FALSE, FALSE);
97b639ba
AM
13053 if (h == NULL)
13054 return FALSE;
13055 if (h->root.type == bfd_link_hash_new)
13056 {
13057 h->root.type = bfd_link_hash_defined;
13058 h->root.u.def.section = htab->glink;
ee4bf8d2 13059 h->root.u.def.value = 8;
f5385ebf
AM
13060 h->ref_regular = 1;
13061 h->def_regular = 1;
13062 h->ref_regular_nonweak = 1;
13063 h->forced_local = 1;
13064 h->non_elf = 0;
2ec55de3 13065 h->root.linker_def = 1;
97b639ba
AM
13066 }
13067 }
33e44f2e
AM
13068 plt0 = (htab->elf.splt->output_section->vma
13069 + htab->elf.splt->output_offset
13070 - 16);
176a0d42
AM
13071 if (info->emitrelocations)
13072 {
13073 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13074 if (r == NULL)
13075 return FALSE;
13076 r->r_offset = (htab->glink->output_offset
13077 + htab->glink->output_section->vma);
13078 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13079 r->r_addend = plt0;
13080 }
4ce794b7 13081 p = htab->glink->contents;
176a0d42 13082 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13083 bfd_put_64 (htab->glink->owner, plt0, p);
13084 p += 8;
b9e5796b
AM
13085 if (htab->opd_abi)
13086 {
13087 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13088 p += 4;
13089 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13090 p += 4;
13091 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13092 p += 4;
13093 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13094 p += 4;
13095 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13096 p += 4;
13097 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13098 p += 4;
13099 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13100 p += 4;
13101 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13102 p += 4;
13103 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13104 p += 4;
13105 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13106 p += 4;
13107 }
13108 else
13109 {
13110 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13111 p += 4;
13112 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13113 p += 4;
13114 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13115 p += 4;
13116 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13117 p += 4;
13118 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13119 p += 4;
13120 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13121 p += 4;
13122 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13123 p += 4;
13124 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13125 p += 4;
13126 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13127 p += 4;
13128 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13129 p += 4;
13130 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13131 p += 4;
13132 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13133 p += 4;
13134 }
4ce794b7 13135 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 13136 p += 4;
ee4bf8d2
AM
13137 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13138 {
13139 bfd_put_32 (htab->glink->owner, NOP, p);
13140 p += 4;
13141 }
ad8e1ba5 13142
9f951329
AM
13143 /* Build the .glink lazy link call stubs. */
13144 indx = 0;
a345bc8d 13145 while (p < htab->glink->contents + htab->glink->rawsize)
9f951329 13146 {
b9e5796b 13147 if (htab->opd_abi)
9f951329 13148 {
b9e5796b
AM
13149 if (indx < 0x8000)
13150 {
13151 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13152 p += 4;
13153 }
13154 else
13155 {
13156 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13157 p += 4;
13158 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13159 p);
13160 p += 4;
13161 }
9f951329 13162 }
4ce794b7 13163 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13164 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13165 indx++;
9f951329
AM
13166 p += 4;
13167 }
a345bc8d
AM
13168
13169 /* Build .glink global entry stubs. */
13170 if (htab->glink->size > htab->glink->rawsize)
afe397ea 13171 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
5d1634d7 13172 }
5d1634d7 13173
7341d5e2 13174 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13175 {
4ce794b7 13176 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13177 htab->brlt->size);
4ce794b7 13178 if (htab->brlt->contents == NULL)
b34976b6 13179 return FALSE;
721956f4 13180 }
ee75fd95 13181 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13182 {
13183 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13184 htab->relbrlt->size);
63bc6f6c
AM
13185 if (htab->relbrlt->contents == NULL)
13186 return FALSE;
13187 }
5d1634d7 13188
721956f4
AM
13189 /* Build the stubs as directed by the stub hash table. */
13190 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13191
a4b6fadd
AM
13192 for (group = htab->group; group != NULL; group = group->next)
13193 if (group->needs_save_res)
13194 {
13195 stub_sec = group->stub_sec;
13196 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13197 htab->sfpr->size);
13198 if (htab->params->emit_stub_syms)
13199 {
13200 unsigned int i;
13201
13202 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13203 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13204 return FALSE;
13205 }
13206 stub_sec->size += htab->sfpr->size;
13207 }
13208
aa8a7074
AM
13209 if (htab->relbrlt != NULL)
13210 htab->relbrlt->reloc_count = 0;
13211
e7d1c40c
AM
13212 if (htab->params->plt_stub_align != 0)
13213 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
13214 stub_sec != NULL;
13215 stub_sec = stub_sec->next)
13216 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
13217 stub_sec->size = ((stub_sec->size
13218 + (1 << htab->params->plt_stub_align) - 1)
29f628db 13219 & -(1 << htab->params->plt_stub_align));
794e51c0 13220
e7d1c40c 13221 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
13222 stub_sec != NULL;
13223 stub_sec = stub_sec->next)
e717da7e
AM
13224 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13225 {
13226 stub_sec_count += 1;
c9301e31
AM
13227 if (stub_sec->rawsize != stub_sec->size
13228 && (htab->stub_iteration <= STUB_SHRINK_ITER
13229 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13230 break;
13231 }
5d1634d7 13232
da44f4e5
AM
13233 /* Note that the glink_eh_frame check here is not only testing that
13234 the generated size matched the calculated size but also that
13235 bfd_elf_discard_info didn't make any changes to the section. */
721956f4 13236 if (stub_sec != NULL
58d180e8
AM
13237 || (htab->glink_eh_frame != NULL
13238 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 13239 {
b34976b6 13240 htab->stub_error = TRUE;
8de848d8 13241 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 13242 }
721956f4 13243
d2a300cf
AM
13244 if (htab->stub_error)
13245 return FALSE;
13246
13247 if (stats != NULL)
13248 {
13249 *stats = bfd_malloc (500);
13250 if (*stats == NULL)
13251 return FALSE;
13252
ee75fd95 13253 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
13254 " branch %lu\n"
13255 " toc adjust %lu\n"
13256 " long branch %lu\n"
13257 " long toc adj %lu\n"
794e51c0 13258 " plt call %lu\n"
7341d5e2
AM
13259 " plt call toc %lu\n"
13260 " global entry %lu"),
e717da7e 13261 stub_sec_count,
ee75fd95 13262 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
13263 htab->stub_count[ppc_stub_long_branch - 1],
13264 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13265 htab->stub_count[ppc_stub_plt_branch - 1],
13266 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13267 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13268 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13269 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13270 }
13271 return TRUE;
5bd4f169
AM
13272}
13273
60124e18
AM
13274/* What to do when ld finds relocations against symbols defined in
13275 discarded sections. */
13276
13277static unsigned int
13278ppc64_elf_action_discarded (asection *sec)
13279{
13280 if (strcmp (".opd", sec->name) == 0)
13281 return 0;
13282
13283 if (strcmp (".toc", sec->name) == 0)
13284 return 0;
13285
bce50a28
JJ
13286 if (strcmp (".toc1", sec->name) == 0)
13287 return 0;
13288
60124e18
AM
13289 return _bfd_elf_default_action_discarded (sec);
13290}
13291
5bd4f169
AM
13292/* The RELOCATE_SECTION function is called by the ELF backend linker
13293 to handle the relocations for a section.
13294
13295 The relocs are always passed as Rela structures; if the section
13296 actually uses Rel structures, the r_addend field will always be
13297 zero.
13298
13299 This function is responsible for adjust the section contents as
13300 necessary, and (if using Rela relocs and generating a
1049f94e 13301 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13302 necessary.
13303
13304 This function does not have to worry about setting the reloc
13305 address or the reloc symbol index.
13306
13307 LOCAL_SYMS is a pointer to the swapped in local symbols.
13308
13309 LOCAL_SECTIONS is an array giving the section in the input file
13310 corresponding to the st_shndx field of each local symbol.
13311
13312 The global hash table entry for the global symbols can be found
13313 via elf_sym_hashes (input_bfd).
13314
1049f94e 13315 When generating relocatable output, this function must handle
5bd4f169
AM
13316 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13317 going to be the section symbol corresponding to the output
13318 section, which means that the addend must be adjusted
13319 accordingly. */
13320
b34976b6 13321static bfd_boolean
4ce794b7
AM
13322ppc64_elf_relocate_section (bfd *output_bfd,
13323 struct bfd_link_info *info,
13324 bfd *input_bfd,
13325 asection *input_section,
13326 bfd_byte *contents,
13327 Elf_Internal_Rela *relocs,
13328 Elf_Internal_Sym *local_syms,
13329 asection **local_sections)
5bd4f169 13330{
65f38f15 13331 struct ppc_link_hash_table *htab;
5bd4f169
AM
13332 Elf_Internal_Shdr *symtab_hdr;
13333 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13334 Elf_Internal_Rela *rel;
c316a17c 13335 Elf_Internal_Rela *wrel;
5bd4f169 13336 Elf_Internal_Rela *relend;
411e1bfb
AM
13337 Elf_Internal_Rela outrel;
13338 bfd_byte *loc;
411e1bfb 13339 struct got_entry **local_got_ents;
5bd4f169 13340 bfd_vma TOCstart;
b34976b6
AM
13341 bfd_boolean ret = TRUE;
13342 bfd_boolean is_opd;
794e51c0
AM
13343 /* Assume 'at' branch hints. */
13344 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13345 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13346
65f38f15 13347 /* Initialize howto table if needed. */
5bd4f169 13348 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13349 ppc_howto_init ();
13350
65f38f15 13351 htab = ppc_hash_table (info);
4dfe6ac6
NC
13352 if (htab == NULL)
13353 return FALSE;
ee75fd95
AM
13354
13355 /* Don't relocate stub sections. */
e7d1c40c 13356 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13357 return TRUE;
13358
0c8d6e5c 13359 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13360
411e1bfb 13361 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13362 TOCstart = elf_gp (output_bfd);
0ffa91dd 13363 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13364 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13365 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13366
c316a17c 13367 rel = wrel = relocs;
5bd4f169 13368 relend = relocs + input_section->reloc_count;
c316a17c 13369 for (; rel < relend; wrel++, rel++)
5bd4f169 13370 {
04c9666a 13371 enum elf_ppc64_reloc_type r_type;
31c76678 13372 bfd_vma addend;
5bd4f169
AM
13373 bfd_reloc_status_type r;
13374 Elf_Internal_Sym *sym;
13375 asection *sec;
039b3fef
AM
13376 struct elf_link_hash_entry *h_elf;
13377 struct ppc_link_hash_entry *h;
13378 struct ppc_link_hash_entry *fdh;
5bd4f169 13379 const char *sym_name;
0d4792f7 13380 unsigned long r_symndx, toc_symndx;
3a71aa26 13381 bfd_vma toc_addend;
f961d9dd
AM
13382 unsigned char tls_mask, tls_gd, tls_type;
13383 unsigned char sym_type;
5bd4f169 13384 bfd_vma relocation;
b34976b6
AM
13385 bfd_boolean unresolved_reloc;
13386 bfd_boolean warned;
bc30df16 13387 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13388 unsigned int insn;
e11840f9 13389 unsigned int mask;
721956f4
AM
13390 struct ppc_stub_hash_entry *stub_entry;
13391 bfd_vma max_br_offset;
13392 bfd_vma from;
c316a17c 13393 Elf_Internal_Rela orig_rel;
b80eed39
AM
13394 reloc_howto_type *howto;
13395 struct reloc_howto_struct alt_howto;
5bd4f169 13396
c316a17c
AM
13397 again:
13398 orig_rel = *rel;
13399
4ce794b7 13400 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13401 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13402
13403 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13404 symbol of the previous ADDR64 reloc. The symbol gives us the
13405 proper TOC base to use. */
13406 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13407 && wrel != relocs
13408 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13409 && is_opd)
c316a17c 13410 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13411
4ce794b7
AM
13412 sym = NULL;
13413 sec = NULL;
039b3fef 13414 h_elf = NULL;
4ce794b7 13415 sym_name = NULL;
b34976b6
AM
13416 unresolved_reloc = FALSE;
13417 warned = FALSE;
65f38f15 13418
0b13192e 13419 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13420 {
13421 /* It's a local symbol. */
74f0fb50 13422 struct _opd_sec_data *opd;
4025353c 13423
5bd4f169
AM
13424 sym = local_syms + r_symndx;
13425 sec = local_sections[r_symndx];
26c61ae5 13426 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13427 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13428 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13429 opd = get_opd_info (sec);
13430 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13431 {
51aecdc5
AM
13432 long adjust = opd->adjust[OPD_NDX (sym->st_value
13433 + rel->r_addend)];
4025353c
AM
13434 if (adjust == -1)
13435 relocation = 0;
13436 else
4cc603a5
AM
13437 {
13438 /* If this is a relocation against the opd section sym
13439 and we have edited .opd, adjust the reloc addend so
13440 that ld -r and ld --emit-relocs output is correct.
13441 If it is a reloc against some other .opd symbol,
13442 then the symbol value will be adjusted later. */
13443 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13444 rel->r_addend += adjust;
13445 else
13446 relocation += adjust;
13447 }
1e2f5b6e 13448 }
5bd4f169
AM
13449 }
13450 else
13451 {
62d887d4
L
13452 bfd_boolean ignored;
13453
b2a8e766
AM
13454 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13455 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13456 h_elf, sec, relocation,
62d887d4 13457 unresolved_reloc, warned, ignored);
039b3fef
AM
13458 sym_name = h_elf->root.root.string;
13459 sym_type = h_elf->type;
b69fdb4e
AM
13460 if (sec != NULL
13461 && sec->owner == output_bfd
13462 && strcmp (sec->name, ".opd") == 0)
13463 {
13464 /* This is a symbol defined in a linker script. All
13465 such are defined in output sections, even those
13466 defined by simple assignment from a symbol defined in
13467 an input section. Transfer the symbol to an
13468 appropriate input .opd section, so that a branch to
13469 this symbol will be mapped to the location specified
13470 by the opd entry. */
13471 struct bfd_link_order *lo;
13472 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13473 if (lo->type == bfd_indirect_link_order)
13474 {
13475 asection *isec = lo->u.indirect.section;
13476 if (h_elf->root.u.def.value >= isec->output_offset
13477 && h_elf->root.u.def.value < (isec->output_offset
13478 + isec->size))
13479 {
13480 h_elf->root.u.def.value -= isec->output_offset;
13481 h_elf->root.u.def.section = isec;
13482 sec = isec;
13483 break;
13484 }
13485 }
13486 }
5bd4f169 13487 }
039b3fef 13488 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13489
dbaa2011 13490 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13491 {
13492 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13493 input_bfd, input_section,
13494 contents + rel->r_offset);
13495 wrel->r_offset = rel->r_offset;
13496 wrel->r_info = 0;
13497 wrel->r_addend = 0;
13498
13499 /* For ld -r, remove relocations in debug sections against
13500 sections defined in discarded sections. Not done for
13501 non-debug to preserve relocs in .eh_frame which the
13502 eh_frame editing code expects to be present. */
13503 if (bfd_link_relocatable (info)
13504 && (input_section->flags & SEC_DEBUGGING))
13505 wrel--;
13506
13507 continue;
13508 }
ab96bf03 13509
0e1862bb 13510 if (bfd_link_relocatable (info))
c316a17c 13511 goto copy_reloc;
ab96bf03 13512
f40da81b
AM
13513 if (h != NULL && &h->elf == htab->elf.hgot)
13514 {
6f20ed8a 13515 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13516 sec = bfd_abs_section_ptr;
13517 unresolved_reloc = FALSE;
13518 }
13519
951fd09b
AM
13520 /* TLS optimizations. Replace instruction sequences and relocs
13521 based on information we collected in tls_optimize. We edit
13522 RELOCS so that --emit-relocs will output something sensible
13523 for the final instruction stream. */
13524 tls_mask = 0;
13525 tls_gd = 0;
0d4792f7 13526 toc_symndx = 0;
727fc41e
AM
13527 if (h != NULL)
13528 tls_mask = h->tls_mask;
13529 else if (local_got_ents != NULL)
411e1bfb 13530 {
e054468f
AM
13531 struct plt_entry **local_plt = (struct plt_entry **)
13532 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13533 unsigned char *lgot_masks = (unsigned char *)
e054468f 13534 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13535 tls_mask = lgot_masks[r_symndx];
13536 }
13537 if (tls_mask == 0
13538 && (r_type == R_PPC64_TLS
13539 || r_type == R_PPC64_TLSGD
13540 || r_type == R_PPC64_TLSLD))
13541 {
13542 /* Check for toc tls entries. */
f961d9dd 13543 unsigned char *toc_tls;
0d4792f7 13544
727fc41e
AM
13545 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13546 &local_syms, rel, input_bfd))
13547 return FALSE;
0d4792f7 13548
727fc41e
AM
13549 if (toc_tls)
13550 tls_mask = *toc_tls;
0d4792f7
AM
13551 }
13552
13553 /* Check that tls relocs are used with tls syms, and non-tls
13554 relocs are used with non-tls syms. */
cf35638d 13555 if (r_symndx != STN_UNDEF
0d4792f7
AM
13556 && r_type != R_PPC64_NONE
13557 && (h == NULL
039b3fef
AM
13558 || h->elf.root.type == bfd_link_hash_defined
13559 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13560 && (IS_PPC64_TLS_RELOC (r_type)
13561 != (sym_type == STT_TLS
13562 || (sym_type == STT_SECTION
13563 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13564 {
727fc41e
AM
13565 if (tls_mask != 0
13566 && (r_type == R_PPC64_TLS
13567 || r_type == R_PPC64_TLSGD
13568 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13569 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13570 ;
13571 else
25f53a85 13572 info->callbacks->einfo
1d483afe 13573 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 13574 /* xgettext:c-format */
174d0a74 13575 ? _("%H: %s used with TLS symbol `%T'\n")
695344c0 13576 /* xgettext:c-format */
174d0a74 13577 : _("%H: %s used with non-TLS symbol `%T'\n"),
25f53a85 13578 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13579 ppc64_elf_howto_table[r_type]->name,
13580 sym_name);
411e1bfb
AM
13581 }
13582
13583 /* Ensure reloc mapping code below stays sane. */
13584 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13585 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13586 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13587 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13588 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13589 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13590 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13591 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13592 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13593 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13594 abort ();
0d4792f7 13595
411e1bfb
AM
13596 switch (r_type)
13597 {
13598 default:
411e1bfb
AM
13599 break;
13600
ba761f19 13601 case R_PPC64_LO_DS_OPT:
95f0d0d2 13602 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
13603 if ((insn & (0x3f << 26)) != 58u << 26)
13604 abort ();
13605 insn += (14u << 26) - (58u << 26);
95f0d0d2 13606 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
13607 r_type = R_PPC64_TOC16_LO;
13608 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13609 break;
13610
411e1bfb
AM
13611 case R_PPC64_TOC16:
13612 case R_PPC64_TOC16_LO:
13613 case R_PPC64_TOC16_DS:
13614 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13615 {
13616 /* Check for toc tls entries. */
f961d9dd 13617 unsigned char *toc_tls;
951fd09b 13618 int retval;
411e1bfb 13619
3a71aa26
AM
13620 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13621 &local_syms, rel, input_bfd);
951fd09b 13622 if (retval == 0)
411e1bfb
AM
13623 return FALSE;
13624
13625 if (toc_tls)
13626 {
951fd09b 13627 tls_mask = *toc_tls;
411e1bfb
AM
13628 if (r_type == R_PPC64_TOC16_DS
13629 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
13630 {
13631 if (tls_mask != 0
13632 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13633 goto toctprel;
13634 }
411e1bfb 13635 else
951fd09b
AM
13636 {
13637 /* If we found a GD reloc pair, then we might be
13638 doing a GD->IE transition. */
13639 if (retval == 2)
13640 {
13641 tls_gd = TLS_TPRELGD;
13642 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13643 goto tls_ldgd_opt;
951fd09b
AM
13644 }
13645 else if (retval == 3)
13646 {
13647 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 13648 goto tls_ldgd_opt;
951fd09b
AM
13649 }
13650 }
411e1bfb
AM
13651 }
13652 }
13653 break;
13654
9d6ded02
AM
13655 case R_PPC64_GOT_TPREL16_HI:
13656 case R_PPC64_GOT_TPREL16_HA:
13657 if (tls_mask != 0
13658 && (tls_mask & TLS_TPREL) == 0)
13659 {
13660 rel->r_offset -= d_offset;
95f0d0d2 13661 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
13662 r_type = R_PPC64_NONE;
13663 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13664 }
13665 break;
13666
411e1bfb
AM
13667 case R_PPC64_GOT_TPREL16_DS:
13668 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13669 if (tls_mask != 0
13670 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13671 {
81407a69 13672 toctprel:
95f0d0d2 13673 insn = bfd_get_32 (input_bfd,
c316a17c 13674 contents + rel->r_offset - d_offset);
411e1bfb
AM
13675 insn &= 31 << 21;
13676 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 13677 bfd_put_32 (input_bfd, insn,
c316a17c 13678 contents + rel->r_offset - d_offset);
411e1bfb 13679 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13680 if (toc_symndx != 0)
13681 {
13682 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13683 rel->r_addend = toc_addend;
0d4792f7
AM
13684 /* We changed the symbol. Start over in order to
13685 get h, sym, sec etc. right. */
c316a17c 13686 goto again;
0d4792f7
AM
13687 }
13688 else
13689 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13690 }
13691 break;
13692
13693 case R_PPC64_TLS:
951fd09b
AM
13694 if (tls_mask != 0
13695 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13696 {
95f0d0d2 13697 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
13698 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13699 if (insn == 0)
411e1bfb 13700 abort ();
95f0d0d2 13701 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 13702 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13703 PPC64_TPREL16_LO which is at low-order half-word. */
13704 rel->r_offset += d_offset;
0d4792f7
AM
13705 r_type = R_PPC64_TPREL16_LO;
13706 if (toc_symndx != 0)
13707 {
13708 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13709 rel->r_addend = toc_addend;
0d4792f7
AM
13710 /* We changed the symbol. Start over in order to
13711 get h, sym, sec etc. right. */
c316a17c 13712 goto again;
0d4792f7
AM
13713 }
13714 else
13715 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13716 }
13717 break;
13718
411e1bfb
AM
13719 case R_PPC64_GOT_TLSGD16_HI:
13720 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13721 tls_gd = TLS_TPRELGD;
13722 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13723 goto tls_gdld_hi;
13724 break;
13725
411e1bfb
AM
13726 case R_PPC64_GOT_TLSLD16_HI:
13727 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13728 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13729 {
951fd09b
AM
13730 tls_gdld_hi:
13731 if ((tls_mask & tls_gd) != 0)
13732 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13733 + R_PPC64_GOT_TPREL16_DS);
13734 else
411e1bfb 13735 {
4fe5ca5b 13736 rel->r_offset -= d_offset;
95f0d0d2 13737 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 13738 r_type = R_PPC64_NONE;
411e1bfb 13739 }
951fd09b 13740 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13741 }
13742 break;
13743
951fd09b
AM
13744 case R_PPC64_GOT_TLSGD16:
13745 case R_PPC64_GOT_TLSGD16_LO:
13746 tls_gd = TLS_TPRELGD;
13747 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13748 goto tls_ldgd_opt;
951fd09b 13749 break;
411e1bfb 13750
951fd09b
AM
13751 case R_PPC64_GOT_TLSLD16:
13752 case R_PPC64_GOT_TLSLD16_LO:
13753 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13754 {
3a71aa26 13755 unsigned int insn1, insn2, insn3;
102890f0
AM
13756 bfd_vma offset;
13757
13758 tls_ldgd_opt:
727fc41e
AM
13759 offset = (bfd_vma) -1;
13760 /* If not using the newer R_PPC64_TLSGD/LD to mark
13761 __tls_get_addr calls, we must trust that the call
13762 stays with its arg setup insns, ie. that the next
13763 reloc is the __tls_get_addr call associated with
13764 the current reloc. Edit both insns. */
13765 if (input_section->has_tls_get_addr_call
13766 && rel + 1 < relend
13767 && branch_reloc_hash_match (input_bfd, rel + 1,
13768 htab->tls_get_addr,
13769 htab->tls_get_addr_fd))
13770 offset = rel[1].r_offset;
b86ac8e3
AM
13771 /* We read the low GOT_TLS (or TOC16) insn because we
13772 need to keep the destination reg. It may be
13773 something other than the usual r3, and moved to r3
13774 before the call by intervening code. */
95f0d0d2 13775 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 13776 contents + rel->r_offset - d_offset);
102890f0 13777 if ((tls_mask & tls_gd) != 0)
411e1bfb 13778 {
102890f0 13779 /* IE */
b86ac8e3 13780 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13781 insn1 |= 58 << 26; /* ld */
13782 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13783 if (offset != (bfd_vma) -1)
f58d5a2d 13784 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13785 if ((tls_mask & TLS_EXPLICIT) == 0)
13786 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13787 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13788 else
102890f0
AM
13789 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13790 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13791 }
13792 else
13793 {
13794 /* LE */
b86ac8e3
AM
13795 insn1 &= 0x1f << 21;
13796 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13797 insn2 = 0x38630000; /* addi 3,3,0 */
13798 if (tls_gd == 0)
951fd09b 13799 {
102890f0 13800 /* Was an LD reloc. */
1d483afe
AM
13801 if (toc_symndx)
13802 sec = local_sections[toc_symndx];
13803 for (r_symndx = 0;
13804 r_symndx < symtab_hdr->sh_info;
13805 r_symndx++)
13806 if (local_sections[r_symndx] == sec)
13807 break;
13808 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13809 r_symndx = STN_UNDEF;
102890f0 13810 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13811 if (r_symndx != STN_UNDEF)
1d483afe
AM
13812 rel->r_addend -= (local_syms[r_symndx].st_value
13813 + sec->output_offset
13814 + sec->output_section->vma);
951fd09b 13815 }
102890f0 13816 else if (toc_symndx != 0)
3a71aa26
AM
13817 {
13818 r_symndx = toc_symndx;
13819 rel->r_addend = toc_addend;
13820 }
102890f0
AM
13821 r_type = R_PPC64_TPREL16_HA;
13822 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13823 if (offset != (bfd_vma) -1)
13824 {
13825 rel[1].r_info = ELF64_R_INFO (r_symndx,
13826 R_PPC64_TPREL16_LO);
13827 rel[1].r_offset = offset + d_offset;
13828 rel[1].r_addend = rel->r_addend;
13829 }
102890f0 13830 }
95f0d0d2 13831 bfd_put_32 (input_bfd, insn1,
3a71aa26 13832 contents + rel->r_offset - d_offset);
727fc41e
AM
13833 if (offset != (bfd_vma) -1)
13834 {
95f0d0d2 13835 insn3 = bfd_get_32 (input_bfd,
727fc41e
AM
13836 contents + offset + 4);
13837 if (insn3 == NOP
13838 || insn3 == CROR_151515 || insn3 == CROR_313131)
13839 {
13840 rel[1].r_offset += 4;
95f0d0d2 13841 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
727fc41e
AM
13842 insn2 = NOP;
13843 }
95f0d0d2 13844 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
13845 }
13846 if ((tls_mask & tls_gd) == 0
13847 && (tls_gd == 0 || toc_symndx != 0))
13848 {
13849 /* We changed the symbol. Start over in order
13850 to get h, sym, sec etc. right. */
c316a17c 13851 goto again;
727fc41e
AM
13852 }
13853 }
13854 break;
13855
13856 case R_PPC64_TLSGD:
13857 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13858 {
13859 unsigned int insn2, insn3;
13860 bfd_vma offset = rel->r_offset;
13861
13862 if ((tls_mask & TLS_TPRELGD) != 0)
13863 {
13864 /* IE */
13865 r_type = R_PPC64_NONE;
13866 insn2 = 0x7c636a14; /* add 3,3,13 */
13867 }
13868 else
13869 {
13870 /* LE */
13871 if (toc_symndx != 0)
13872 {
13873 r_symndx = toc_symndx;
13874 rel->r_addend = toc_addend;
13875 }
13876 r_type = R_PPC64_TPREL16_LO;
13877 rel->r_offset = offset + d_offset;
13878 insn2 = 0x38630000; /* addi 3,3,0 */
13879 }
13880 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13881 /* Zap the reloc on the _tls_get_addr call too. */
13882 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13883 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 13884 insn3 = bfd_get_32 (input_bfd,
3a71aa26 13885 contents + offset + 4);
102890f0
AM
13886 if (insn3 == NOP
13887 || insn3 == CROR_151515 || insn3 == CROR_313131)
13888 {
727fc41e 13889 rel->r_offset += 4;
95f0d0d2 13890 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
3a71aa26 13891 insn2 = NOP;
102890f0 13892 }
95f0d0d2 13893 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 13894 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13895 goto again;
411e1bfb 13896 }
411e1bfb
AM
13897 break;
13898
727fc41e
AM
13899 case R_PPC64_TLSLD:
13900 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13901 {
13902 unsigned int insn2, insn3;
13903 bfd_vma offset = rel->r_offset;
13904
13905 if (toc_symndx)
13906 sec = local_sections[toc_symndx];
13907 for (r_symndx = 0;
13908 r_symndx < symtab_hdr->sh_info;
13909 r_symndx++)
13910 if (local_sections[r_symndx] == sec)
13911 break;
13912 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13913 r_symndx = STN_UNDEF;
727fc41e 13914 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13915 if (r_symndx != STN_UNDEF)
727fc41e
AM
13916 rel->r_addend -= (local_syms[r_symndx].st_value
13917 + sec->output_offset
13918 + sec->output_section->vma);
13919
13920 r_type = R_PPC64_TPREL16_LO;
13921 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13922 rel->r_offset = offset + d_offset;
13923 /* Zap the reloc on the _tls_get_addr call too. */
13924 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13925 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 13926 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 13927 insn3 = bfd_get_32 (input_bfd,
727fc41e
AM
13928 contents + offset + 4);
13929 if (insn3 == NOP
13930 || insn3 == CROR_151515 || insn3 == CROR_313131)
13931 {
13932 rel->r_offset += 4;
95f0d0d2 13933 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
727fc41e
AM
13934 insn2 = NOP;
13935 }
95f0d0d2 13936 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 13937 goto again;
727fc41e
AM
13938 }
13939 break;
13940
411e1bfb 13941 case R_PPC64_DTPMOD64:
951fd09b
AM
13942 if (rel + 1 < relend
13943 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13944 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13945 {
951fd09b
AM
13946 if ((tls_mask & TLS_GD) == 0)
13947 {
13948 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13949 if ((tls_mask & TLS_TPRELGD) != 0)
13950 r_type = R_PPC64_TPREL64;
13951 else
13952 {
4ce794b7 13953 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13954 r_type = R_PPC64_NONE;
13955 }
13956 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13957 }
13958 }
13959 else
13960 {
13961 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13962 {
4ce794b7 13963 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13964 r_type = R_PPC64_NONE;
951fd09b 13965 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13966 }
411e1bfb
AM
13967 }
13968 break;
13969
13970 case R_PPC64_TPREL64:
951fd09b 13971 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13972 {
13973 r_type = R_PPC64_NONE;
13974 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13975 }
13976 break;
52a82034 13977
006589cf
AM
13978 case R_PPC64_ENTRY:
13979 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13980 if (!bfd_link_pic (info)
13981 && !info->traditional_format
13982 && relocation + 0x80008000 <= 0xffffffff)
13983 {
13984 unsigned int insn1, insn2;
13985
13986 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13987 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13988 if ((insn1 & ~0xfffc) == LD_R2_0R12
13989 && insn2 == ADD_R2_R2_R12)
13990 {
95f0d0d2 13991 bfd_put_32 (input_bfd,
006589cf
AM
13992 LIS_R2 + PPC_HA (relocation),
13993 contents + rel->r_offset);
95f0d0d2 13994 bfd_put_32 (input_bfd,
006589cf
AM
13995 ADDI_R2_R2 + PPC_LO (relocation),
13996 contents + rel->r_offset + 4);
13997 }
13998 }
13999 else
14000 {
14001 relocation -= (rel->r_offset
14002 + input_section->output_offset
14003 + input_section->output_section->vma);
14004 if (relocation + 0x80008000 <= 0xffffffff)
14005 {
14006 unsigned int insn1, insn2;
14007
14008 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14009 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14010 if ((insn1 & ~0xfffc) == LD_R2_0R12
14011 && insn2 == ADD_R2_R2_R12)
14012 {
95f0d0d2 14013 bfd_put_32 (input_bfd,
006589cf
AM
14014 ADDIS_R2_R12 + PPC_HA (relocation),
14015 contents + rel->r_offset);
95f0d0d2 14016 bfd_put_32 (input_bfd,
006589cf
AM
14017 ADDI_R2_R2 + PPC_LO (relocation),
14018 contents + rel->r_offset + 4);
14019 }
14020 }
14021 }
14022 break;
14023
52a82034
AM
14024 case R_PPC64_REL16_HA:
14025 /* If we are generating a non-PIC executable, edit
14026 . 0: addis 2,12,.TOC.-0b@ha
14027 . addi 2,2,.TOC.-0b@l
14028 used by ELFv2 global entry points to set up r2, to
14029 . lis 2,.TOC.@ha
14030 . addi 2,2,.TOC.@l
14031 if .TOC. is in range. */
0e1862bb 14032 if (!bfd_link_pic (info)
810d4e75 14033 && !info->traditional_format
006589cf 14034 && !htab->opd_abi
4f038ee5 14035 && rel->r_addend == d_offset
52a82034
AM
14036 && h != NULL && &h->elf == htab->elf.hgot
14037 && rel + 1 < relend
14038 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14039 && rel[1].r_offset == rel->r_offset + 4
14040 && rel[1].r_addend == rel->r_addend + 4
14041 && relocation + 0x80008000 <= 0xffffffff)
14042 {
14043 unsigned int insn1, insn2;
14044 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14045 insn1 = bfd_get_32 (input_bfd, contents + offset);
14046 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14047 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14048 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14049 {
14050 r_type = R_PPC64_ADDR16_HA;
14051 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14052 rel->r_addend -= d_offset;
14053 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14054 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14055 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14056 }
14057 }
14058 break;
411e1bfb
AM
14059 }
14060
14061 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14062 insn = 0;
b25116a9
AM
14063 max_br_offset = 1 << 25;
14064 addend = rel->r_addend;
bc30df16 14065 reloc_dest = DEST_NORMAL;
65f38f15 14066 switch (r_type)
5bd4f169
AM
14067 {
14068 default:
65f38f15 14069 break;
5bd4f169 14070
3b421ab3
AM
14071 case R_PPC64_TOCSAVE:
14072 if (relocation + addend == (rel->r_offset
14073 + input_section->output_offset
14074 + input_section->output_section->vma)
14075 && tocsave_find (htab, NO_INSERT,
14076 &local_syms, rel, input_bfd))
14077 {
14078 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14079 if (insn == NOP
14080 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14081 bfd_put_32 (input_bfd,
14082 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14083 contents + rel->r_offset);
14084 }
14085 break;
14086
65f38f15
AM
14087 /* Branch taken prediction relocations. */
14088 case R_PPC64_ADDR14_BRTAKEN:
14089 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14090 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14091 /* Fall through. */
65f38f15 14092
86c76c7b 14093 /* Branch not taken prediction relocations. */
65f38f15
AM
14094 case R_PPC64_ADDR14_BRNTAKEN:
14095 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14096 insn |= bfd_get_32 (input_bfd,
411e1bfb 14097 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14098 /* Fall through. */
86c76c7b 14099
b25116a9
AM
14100 case R_PPC64_REL14:
14101 max_br_offset = 1 << 15;
1a0670f3 14102 /* Fall through. */
5bd4f169 14103
65f38f15 14104 case R_PPC64_REL24:
ad8e1ba5
AM
14105 /* Calls to functions with a different TOC, such as calls to
14106 shared objects, need to alter the TOC pointer. This is
14107 done using a linkage stub. A REL24 branching to these
14108 linkage stubs needs to be followed by a nop, as the nop
14109 will be replaced with an instruction to restore the TOC
14110 base pointer. */
8387904d 14111 fdh = h;
b31867b6
AM
14112 if (h != NULL
14113 && h->oh != NULL
14114 && h->oh->is_func_descriptor)
14115 fdh = ppc_follow_link (h->oh);
31c76678
DK
14116 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14117 htab);
6abec6bc 14118 if (stub_entry != NULL
ad8e1ba5 14119 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 14120 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
14121 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14122 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 14123 {
b25116a9 14124 bfd_boolean can_plt_call = FALSE;
721956f4 14125
ba8ca3e7
AM
14126 /* All of these stubs will modify r2, so there must be a
14127 branch and link followed by a nop. The nop is
14128 replaced by an insn to restore r2. */
eea6121a 14129 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14130 {
ba8ca3e7
AM
14131 unsigned long br;
14132
14133 br = bfd_get_32 (input_bfd,
14134 contents + rel->r_offset);
14135 if ((br & 1) != 0)
41bd81ab 14136 {
ba8ca3e7
AM
14137 unsigned long nop;
14138
14139 nop = bfd_get_32 (input_bfd,
14140 contents + rel->r_offset + 4);
14141 if (nop == NOP
14142 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 14143 {
ba8ca3e7
AM
14144 if (h != NULL
14145 && (h == htab->tls_get_addr_fd
14146 || h == htab->tls_get_addr)
7c9cf415 14147 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14148 {
14149 /* Special stub used, leave nop alone. */
14150 }
14151 else
a078d95a
AM
14152 bfd_put_32 (input_bfd,
14153 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14154 contents + rel->r_offset + 4);
14155 can_plt_call = TRUE;
a7f2871e 14156 }
41bd81ab 14157 }
5bd4f169 14158 }
721956f4 14159
ba8ca3e7 14160 if (!can_plt_call && h != NULL)
721956f4 14161 {
ba8ca3e7
AM
14162 const char *name = h->elf.root.root.string;
14163
14164 if (*name == '.')
14165 ++name;
14166
14167 if (strncmp (name, "__libc_start_main", 17) == 0
14168 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14169 {
ba8ca3e7
AM
14170 /* Allow crt1 branch to go via a toc adjusting
14171 stub. Other calls that never return could do
14172 the same, if we could detect such. */
b25116a9 14173 can_plt_call = TRUE;
6ab189d5 14174 }
ba8ca3e7
AM
14175 }
14176
14177 if (!can_plt_call)
14178 {
14179 /* g++ as of 20130507 emits self-calls without a
14180 following nop. This is arguably wrong since we
14181 have conflicting information. On the one hand a
14182 global symbol and on the other a local call
14183 sequence, but don't error for this special case.
14184 It isn't possible to cheaply verify we have
14185 exactly such a call. Allow all calls to the same
14186 section. */
14187 asection *code_sec = sec;
14188
14189 if (get_opd_info (sec) != NULL)
ad8e1ba5 14190 {
ba8ca3e7
AM
14191 bfd_vma off = (relocation + addend
14192 - sec->output_section->vma
14193 - sec->output_offset);
bc30df16 14194
ba8ca3e7 14195 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14196 }
ba8ca3e7
AM
14197 if (code_sec == input_section)
14198 can_plt_call = TRUE;
14199 }
14200
14201 if (!can_plt_call)
14202 {
4805fc55
AM
14203 if (stub_entry->stub_type == ppc_stub_plt_call
14204 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14205 info->callbacks->einfo
695344c0 14206 /* xgettext:c-format */
174d0a74 14207 (_("%H: call to `%T' lacks nop, can't restore toc; "
4805fc55
AM
14208 "recompile with -fPIC\n"),
14209 input_bfd, input_section, rel->r_offset, sym_name);
14210 else
14211 info->callbacks->einfo
695344c0 14212 /* xgettext:c-format */
174d0a74 14213 (_("%H: call to `%T' lacks nop, can't restore toc; "
4805fc55
AM
14214 "(-mcmodel=small toc adjust stub)\n"),
14215 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14216
14217 bfd_set_error (bfd_error_bad_value);
14218 ret = FALSE;
721956f4
AM
14219 }
14220
b25116a9 14221 if (can_plt_call
794e51c0
AM
14222 && (stub_entry->stub_type == ppc_stub_plt_call
14223 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14224 unresolved_reloc = FALSE;
14225 }
14226
6abec6bc
AM
14227 if ((stub_entry == NULL
14228 || stub_entry->stub_type == ppc_stub_long_branch
14229 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14230 && get_opd_info (sec) != NULL)
14231 {
14232 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14233 bfd_vma off = (relocation + addend
14234 - sec->output_section->vma
14235 - sec->output_offset);
aef36ac1 14236 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14237 if (dest != (bfd_vma) -1)
14238 {
14239 relocation = dest;
14240 addend = 0;
bc30df16 14241 reloc_dest = DEST_OPD;
8387904d
AM
14242 }
14243 }
14244
b25116a9
AM
14245 /* If the branch is out of reach we ought to have a long
14246 branch stub. */
14247 from = (rel->r_offset
14248 + input_section->output_offset
14249 + input_section->output_section->vma);
14250
6911b7dc
AM
14251 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14252 ? fdh->elf.other
14253 : sym->st_other);
14254
6abec6bc
AM
14255 if (stub_entry != NULL
14256 && (stub_entry->stub_type == ppc_stub_long_branch
14257 || stub_entry->stub_type == ppc_stub_plt_branch)
14258 && (r_type == R_PPC64_ADDR14_BRTAKEN
14259 || r_type == R_PPC64_ADDR14_BRNTAKEN
14260 || (relocation + addend - from + max_br_offset
14261 < 2 * max_br_offset)))
14262 /* Don't use the stub if this branch is in range. */
14263 stub_entry = NULL;
b25116a9
AM
14264
14265 if (stub_entry != NULL)
14266 {
14267 /* Munge up the value and addend so that we call the stub
14268 rather than the procedure directly. */
a4b6fadd
AM
14269 asection *stub_sec = stub_entry->group->stub_sec;
14270
14271 if (stub_entry->stub_type == ppc_stub_save_res)
14272 relocation += (stub_sec->output_offset
14273 + stub_sec->output_section->vma
14274 + stub_sec->size - htab->sfpr->size
14275 - htab->sfpr->output_offset
14276 - htab->sfpr->output_section->vma);
14277 else
14278 relocation = (stub_entry->stub_offset
14279 + stub_sec->output_offset
14280 + stub_sec->output_section->vma);
b25116a9 14281 addend = 0;
bc30df16 14282 reloc_dest = DEST_STUB;
3b421ab3 14283
794e51c0
AM
14284 if ((stub_entry->stub_type == ppc_stub_plt_call
14285 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14286 && (ALWAYS_EMIT_R2SAVE
14287 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14288 && rel + 1 < relend
14289 && rel[1].r_offset == rel->r_offset + 4
14290 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14291 relocation += 4;
b25116a9
AM
14292 }
14293
14294 if (insn != 0)
14295 {
794e51c0 14296 if (is_isa_v2)
721956f4 14297 {
b25116a9
AM
14298 /* Set 'a' bit. This is 0b00010 in BO field for branch
14299 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14300 for branch on CTR insns (BO == 1a00t or 1a01t). */
14301 if ((insn & (0x14 << 21)) == (0x04 << 21))
14302 insn |= 0x02 << 21;
14303 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14304 insn |= 0x08 << 21;
14305 else
14306 break;
14307 }
14308 else
14309 {
14310 /* Invert 'y' bit if not the default. */
4cc603a5 14311 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14312 insn ^= 0x01 << 21;
721956f4 14313 }
b25116a9 14314
95f0d0d2 14315 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14316 }
e86ce104 14317
06da1e8e
AM
14318 /* NOP out calls to undefined weak functions.
14319 We can thus call a weak function without first
14320 checking whether the function is defined. */
b25116a9 14321 else if (h != NULL
039b3fef 14322 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14323 && h->elf.dynindx == -1
b25116a9
AM
14324 && r_type == R_PPC64_REL24
14325 && relocation == 0
4cc603a5 14326 && addend == 0)
e86ce104 14327 {
95f0d0d2 14328 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14329 goto copy_reloc;
e86ce104 14330 }
65f38f15
AM
14331 break;
14332 }
5bd4f169 14333
65f38f15 14334 /* Set `addend'. */
411e1bfb 14335 tls_type = 0;
65f38f15
AM
14336 switch (r_type)
14337 {
14338 default:
25f53a85 14339 info->callbacks->einfo
695344c0 14340 /* xgettext:c-format */
bc30df16 14341 (_("%P: %B: unknown relocation type %d for `%T'\n"),
d003868e 14342 input_bfd, (int) r_type, sym_name);
5bd4f169 14343
65f38f15 14344 bfd_set_error (bfd_error_bad_value);
b34976b6 14345 ret = FALSE;
c316a17c 14346 goto copy_reloc;
5bd4f169 14347
65f38f15 14348 case R_PPC64_NONE:
411e1bfb 14349 case R_PPC64_TLS:
727fc41e
AM
14350 case R_PPC64_TLSGD:
14351 case R_PPC64_TLSLD:
3b421ab3 14352 case R_PPC64_TOCSAVE:
04c9666a
AM
14353 case R_PPC64_GNU_VTINHERIT:
14354 case R_PPC64_GNU_VTENTRY:
006589cf 14355 case R_PPC64_ENTRY:
c316a17c 14356 goto copy_reloc;
5bd4f169
AM
14357
14358 /* GOT16 relocations. Like an ADDR16 using the symbol's
14359 address in the GOT as relocation value instead of the
411e1bfb 14360 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14361 symbol and put the symbol value there. */
411e1bfb
AM
14362 case R_PPC64_GOT_TLSGD16:
14363 case R_PPC64_GOT_TLSGD16_LO:
14364 case R_PPC64_GOT_TLSGD16_HI:
14365 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14366 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14367 goto dogot;
14368
14369 case R_PPC64_GOT_TLSLD16:
14370 case R_PPC64_GOT_TLSLD16_LO:
14371 case R_PPC64_GOT_TLSLD16_HI:
14372 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14373 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14374 goto dogot;
14375
14376 case R_PPC64_GOT_TPREL16_DS:
14377 case R_PPC64_GOT_TPREL16_LO_DS:
14378 case R_PPC64_GOT_TPREL16_HI:
14379 case R_PPC64_GOT_TPREL16_HA:
14380 tls_type = TLS_TLS | TLS_TPREL;
14381 goto dogot;
14382
14383 case R_PPC64_GOT_DTPREL16_DS:
14384 case R_PPC64_GOT_DTPREL16_LO_DS:
14385 case R_PPC64_GOT_DTPREL16_HI:
14386 case R_PPC64_GOT_DTPREL16_HA:
14387 tls_type = TLS_TLS | TLS_DTPREL;
14388 goto dogot;
14389
65f38f15
AM
14390 case R_PPC64_GOT16:
14391 case R_PPC64_GOT16_LO:
14392 case R_PPC64_GOT16_HI:
14393 case R_PPC64_GOT16_HA:
14394 case R_PPC64_GOT16_DS:
14395 case R_PPC64_GOT16_LO_DS:
411e1bfb 14396 dogot:
5bd4f169
AM
14397 {
14398 /* Relocation is to the entry for this symbol in the global
14399 offset table. */
e717da7e 14400 asection *got;
d881513a 14401 bfd_vma *offp;
5bd4f169 14402 bfd_vma off;
d881513a 14403 unsigned long indx = 0;
927be08e 14404 struct got_entry *ent;
65f38f15 14405
d881513a
AM
14406 if (tls_type == (TLS_TLS | TLS_LD)
14407 && (h == NULL
f5385ebf 14408 || !h->elf.def_dynamic))
927be08e 14409 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14410 else
5bd4f169 14411 {
d881513a
AM
14412 if (h != NULL)
14413 {
f0158f44
AM
14414 if (!htab->elf.dynamic_sections_created
14415 || h->elf.dynindx == -1
14416 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14417 || (ELF_ST_VISIBILITY (h->elf.other) != STV_DEFAULT
14418 && h->elf.root.type == bfd_link_hash_undefweak))
d881513a
AM
14419 /* This is actually a static link, or it is a
14420 -Bsymbolic link and the symbol is defined
14421 locally, or the symbol was forced to be local
14422 because of a version file. */
14423 ;
14424 else
14425 {
039b3fef 14426 indx = h->elf.dynindx;
d881513a
AM
14427 unresolved_reloc = FALSE;
14428 }
039b3fef 14429 ent = h->elf.got.glist;
d881513a 14430 }
411e1bfb 14431 else
5bd4f169 14432 {
d881513a
AM
14433 if (local_got_ents == NULL)
14434 abort ();
14435 ent = local_got_ents[r_symndx];
5bd4f169 14436 }
d881513a
AM
14437
14438 for (; ent != NULL; ent = ent->next)
31c76678 14439 if (ent->addend == orig_rel.r_addend
e717da7e 14440 && ent->owner == input_bfd
d881513a
AM
14441 && ent->tls_type == tls_type)
14442 break;
5bd4f169 14443 }
411e1bfb 14444
927be08e
AM
14445 if (ent == NULL)
14446 abort ();
14447 if (ent->is_indirect)
14448 ent = ent->got.ent;
14449 offp = &ent->got.offset;
14450 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14451 if (got == NULL)
14452 abort ();
14453
411e1bfb
AM
14454 /* The offset must always be a multiple of 8. We use the
14455 least significant bit to record whether we have already
14456 processed this entry. */
d881513a 14457 off = *offp;
411e1bfb
AM
14458 if ((off & 1) != 0)
14459 off &= ~1;
5bd4f169
AM
14460 else
14461 {
411e1bfb
AM
14462 /* Generate relocs for the dynamic linker, except in
14463 the case of TLSLD where we'll use one entry per
14464 module. */
25f23106
AM
14465 asection *relgot;
14466 bfd_boolean ifunc;
e717da7e 14467
d881513a 14468 *offp = off | 1;
25f23106
AM
14469 relgot = NULL;
14470 ifunc = (h != NULL
14471 ? h->elf.type == STT_GNU_IFUNC
14472 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14473 if (ifunc)
33e44f2e 14474 relgot = htab->elf.irelplt;
f0158f44
AM
14475 else if (indx != 0
14476 || (bfd_link_pic (info)
14477 && (h == NULL
14478 || (ELF_ST_VISIBILITY (h->elf.other)
14479 == STV_DEFAULT)
14480 || h->elf.root.type != bfd_link_hash_undefweak
14481 || (tls_type == (TLS_TLS | TLS_LD)
14482 && !h->elf.def_dynamic))))
19e08130 14483 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14484 if (relgot != NULL)
5bd4f169 14485 {
e717da7e
AM
14486 outrel.r_offset = (got->output_section->vma
14487 + got->output_offset
411e1bfb 14488 + off);
4cc603a5 14489 outrel.r_addend = addend;
d881513a 14490 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14491 {
411e1bfb 14492 outrel.r_addend = 0;
e515b051 14493 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14494 if (tls_type == (TLS_TLS | TLS_GD))
14495 {
e717da7e
AM
14496 loc = relgot->contents;
14497 loc += (relgot->reloc_count++
d881513a
AM
14498 * sizeof (Elf64_External_Rela));
14499 bfd_elf64_swap_reloca_out (output_bfd,
14500 &outrel, loc);
e515b051 14501 outrel.r_offset += 8;
4cc603a5 14502 outrel.r_addend = addend;
d881513a
AM
14503 outrel.r_info
14504 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14505 }
411e1bfb 14506 }
951fd09b 14507 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14508 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14509 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14510 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14511 else if (indx != 0)
14512 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14513 else
81407a69 14514 {
25f23106
AM
14515 if (ifunc)
14516 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14517 else
14518 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14519
14520 /* Write the .got section contents for the sake
14521 of prelink. */
e717da7e 14522 loc = got->contents + off;
23fbd6fa
JJ
14523 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14524 loc);
81407a69 14525 }
81407a69
AM
14526
14527 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14528 {
14529 outrel.r_addend += relocation;
14530 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14531 {
14532 if (htab->elf.tls_sec == NULL)
14533 outrel.r_addend = 0;
14534 else
14535 outrel.r_addend -= htab->elf.tls_sec->vma;
14536 }
e515b051 14537 }
e717da7e
AM
14538 loc = relgot->contents;
14539 loc += (relgot->reloc_count++
411e1bfb
AM
14540 * sizeof (Elf64_External_Rela));
14541 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14542 }
14543
ad8e1ba5 14544 /* Init the .got section contents here if we're not
81407a69 14545 emitting a reloc. */
d881513a 14546 else
411e1bfb 14547 {
f0158f44
AM
14548 int tlsopt
14549 = (htab->params->tls_get_addr_opt
14550 && htab->tls_get_addr_fd != NULL
14551 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
14552
4cc603a5 14553 relocation += addend;
f0158f44 14554 if (tls_type != 0)
411e1bfb 14555 {
989f9879
AM
14556 if (htab->elf.tls_sec == NULL)
14557 relocation = 0;
14558 else
14559 {
f0158f44
AM
14560 if (tls_type & TLS_LD)
14561 relocation = 0;
14562 else
14563 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14564 if ((tls_type & TLS_TPREL)
14565 || (tlsopt && !(tls_type & TLS_DTPREL)))
989f9879
AM
14566 relocation += DTP_OFFSET - TP_OFFSET;
14567 }
5bd4f169 14568
f0158f44 14569 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
14570 {
14571 bfd_put_64 (output_bfd, relocation,
e717da7e 14572 got->contents + off + 8);
f0158f44 14573 relocation = !tlsopt;
7b609f53 14574 }
411e1bfb
AM
14575 }
14576 bfd_put_64 (output_bfd, relocation,
e717da7e 14577 got->contents + off);
5bd4f169
AM
14578 }
14579 }
14580
65f38f15
AM
14581 if (off >= (bfd_vma) -2)
14582 abort ();
14583
bf102f86 14584 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14585 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14586 }
65f38f15
AM
14587 break;
14588
14589 case R_PPC64_PLT16_HA:
14590 case R_PPC64_PLT16_HI:
14591 case R_PPC64_PLT16_LO:
14592 case R_PPC64_PLT32:
14593 case R_PPC64_PLT64:
14594 /* Relocation is to the entry for this symbol in the
14595 procedure linkage table. */
cbf95972
AM
14596 {
14597 struct plt_entry **plt_list = NULL;
14598 if (h != NULL)
14599 plt_list = &h->elf.plt.plist;
14600 else if (local_got_ents != NULL)
14601 {
14602 struct plt_entry **local_plt = (struct plt_entry **)
14603 (local_got_ents + symtab_hdr->sh_info);
14604 unsigned char *local_got_tls_masks = (unsigned char *)
14605 (local_plt + symtab_hdr->sh_info);
14606 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14607 plt_list = local_plt + r_symndx;
14608 }
14609 if (plt_list)
14610 {
14611 struct plt_entry *ent;
65f38f15 14612
cbf95972
AM
14613 for (ent = *plt_list; ent != NULL; ent = ent->next)
14614 if (ent->plt.offset != (bfd_vma) -1
14615 && ent->addend == orig_rel.r_addend)
14616 {
14617 asection *plt;
14618
14619 plt = htab->elf.splt;
14620 if (!htab->elf.dynamic_sections_created
14621 || h == NULL
14622 || h->elf.dynindx == -1)
14623 plt = htab->elf.iplt;
14624 relocation = (plt->output_section->vma
14625 + plt->output_offset
14626 + ent->plt.offset);
14627 addend = 0;
14628 unresolved_reloc = FALSE;
14629 break;
14630 }
14631 }
14632 }
65f38f15 14633 break;
5bd4f169 14634
0b13192e
AM
14635 case R_PPC64_TOC:
14636 /* Relocation value is TOC base. */
14637 relocation = TOCstart;
cf35638d 14638 if (r_symndx == STN_UNDEF)
6f20ed8a 14639 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14640 else if (unresolved_reloc)
14641 ;
6f20ed8a
AM
14642 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14643 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14644 else
14645 unresolved_reloc = TRUE;
ab96bf03 14646 goto dodyn;
0b13192e 14647
5bd4f169
AM
14648 /* TOC16 relocs. We want the offset relative to the TOC base,
14649 which is the address of the start of the TOC plus 0x8000.
14650 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14651 in this order. */
65f38f15
AM
14652 case R_PPC64_TOC16:
14653 case R_PPC64_TOC16_LO:
14654 case R_PPC64_TOC16_HI:
14655 case R_PPC64_TOC16_DS:
14656 case R_PPC64_TOC16_LO_DS:
14657 case R_PPC64_TOC16_HA:
6f20ed8a 14658 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14659 break;
14660
14661 /* Relocate against the beginning of the section. */
65f38f15
AM
14662 case R_PPC64_SECTOFF:
14663 case R_PPC64_SECTOFF_LO:
14664 case R_PPC64_SECTOFF_HI:
14665 case R_PPC64_SECTOFF_DS:
14666 case R_PPC64_SECTOFF_LO_DS:
14667 case R_PPC64_SECTOFF_HA:
4ce794b7 14668 if (sec != NULL)
65f38f15 14669 addend -= sec->output_section->vma;
5bd4f169
AM
14670 break;
14671
25f23106
AM
14672 case R_PPC64_REL16:
14673 case R_PPC64_REL16_LO:
14674 case R_PPC64_REL16_HI:
14675 case R_PPC64_REL16_HA:
a680de9a 14676 case R_PPC64_REL16DX_HA:
25f23106
AM
14677 break;
14678
721956f4
AM
14679 case R_PPC64_REL14:
14680 case R_PPC64_REL14_BRNTAKEN:
14681 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
14682 case R_PPC64_REL24:
14683 break;
14684
411e1bfb
AM
14685 case R_PPC64_TPREL16:
14686 case R_PPC64_TPREL16_LO:
14687 case R_PPC64_TPREL16_HI:
14688 case R_PPC64_TPREL16_HA:
14689 case R_PPC64_TPREL16_DS:
14690 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14691 case R_PPC64_TPREL16_HIGH:
14692 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14693 case R_PPC64_TPREL16_HIGHER:
14694 case R_PPC64_TPREL16_HIGHERA:
14695 case R_PPC64_TPREL16_HIGHEST:
14696 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14697 if (h != NULL
14698 && h->elf.root.type == bfd_link_hash_undefweak
14699 && h->elf.dynindx == -1)
14700 {
14701 /* Make this relocation against an undefined weak symbol
14702 resolve to zero. This is really just a tweak, since
14703 code using weak externs ought to check that they are
14704 defined before using them. */
14705 bfd_byte *p = contents + rel->r_offset - d_offset;
14706
95f0d0d2 14707 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
14708 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14709 if (insn != 0)
95f0d0d2 14710 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
14711 break;
14712 }
989f9879
AM
14713 if (htab->elf.tls_sec != NULL)
14714 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
0e1862bb 14715 if (bfd_link_pic (info))
411e1bfb
AM
14716 /* The TPREL16 relocs shouldn't really be used in shared
14717 libs as they will result in DT_TEXTREL being set, but
14718 support them anyway. */
14719 goto dodyn;
14720 break;
14721
14722 case R_PPC64_DTPREL16:
14723 case R_PPC64_DTPREL16_LO:
14724 case R_PPC64_DTPREL16_HI:
14725 case R_PPC64_DTPREL16_HA:
14726 case R_PPC64_DTPREL16_DS:
14727 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14728 case R_PPC64_DTPREL16_HIGH:
14729 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14730 case R_PPC64_DTPREL16_HIGHER:
14731 case R_PPC64_DTPREL16_HIGHERA:
14732 case R_PPC64_DTPREL16_HIGHEST:
14733 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14734 if (htab->elf.tls_sec != NULL)
14735 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14736 break;
14737
45965137
AM
14738 case R_PPC64_ADDR64_LOCAL:
14739 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14740 ? h->elf.other
14741 : sym->st_other);
14742 break;
14743
e515b051
AM
14744 case R_PPC64_DTPMOD64:
14745 relocation = 1;
14746 addend = 0;
14747 goto dodyn;
14748
411e1bfb 14749 case R_PPC64_TPREL64:
989f9879
AM
14750 if (htab->elf.tls_sec != NULL)
14751 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14752 goto dodyn;
14753
14754 case R_PPC64_DTPREL64:
989f9879
AM
14755 if (htab->elf.tls_sec != NULL)
14756 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 14757 /* Fall through. */
411e1bfb 14758
65f38f15
AM
14759 /* Relocations that may need to be propagated if this is a
14760 dynamic object. */
04c9666a 14761 case R_PPC64_REL30:
65f38f15
AM
14762 case R_PPC64_REL32:
14763 case R_PPC64_REL64:
14764 case R_PPC64_ADDR14:
14765 case R_PPC64_ADDR14_BRNTAKEN:
14766 case R_PPC64_ADDR14_BRTAKEN:
14767 case R_PPC64_ADDR16:
14768 case R_PPC64_ADDR16_DS:
14769 case R_PPC64_ADDR16_HA:
14770 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14771 case R_PPC64_ADDR16_HIGH:
14772 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14773 case R_PPC64_ADDR16_HIGHER:
14774 case R_PPC64_ADDR16_HIGHERA:
14775 case R_PPC64_ADDR16_HIGHEST:
14776 case R_PPC64_ADDR16_HIGHESTA:
14777 case R_PPC64_ADDR16_LO:
14778 case R_PPC64_ADDR16_LO_DS:
14779 case R_PPC64_ADDR24:
65f38f15
AM
14780 case R_PPC64_ADDR32:
14781 case R_PPC64_ADDR64:
14782 case R_PPC64_UADDR16:
14783 case R_PPC64_UADDR32:
14784 case R_PPC64_UADDR64:
411e1bfb 14785 dodyn:
5d1634d7 14786 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14787 break;
14788
41bd81ab
AM
14789 if (NO_OPD_RELOCS && is_opd)
14790 break;
14791
8a9e8e72
AM
14792 if (bfd_link_pic (info)
14793 ? ((h != NULL && pc_dynrelocs (h))
14794 || must_be_dyn_reloc (info, r_type))
14795 : (h != NULL
14796 ? h->dyn_relocs != NULL
d311bc8b 14797 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 14798 {
b34976b6 14799 bfd_boolean skip, relocate;
65f38f15 14800 asection *sreloc;
1cf1f670 14801 bfd_vma out_off;
65f38f15
AM
14802
14803 /* When generating a dynamic object, these relocations
14804 are copied into the output file to be resolved at run
14805 time. */
14806
b34976b6
AM
14807 skip = FALSE;
14808 relocate = FALSE;
65f38f15 14809
1cf1f670
AM
14810 out_off = _bfd_elf_section_offset (output_bfd, info,
14811 input_section, rel->r_offset);
14812 if (out_off == (bfd_vma) -1)
b34976b6 14813 skip = TRUE;
1cf1f670 14814 else if (out_off == (bfd_vma) -2)
b34976b6 14815 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14816 out_off += (input_section->output_section->vma
14817 + input_section->output_offset);
14818 outrel.r_offset = out_off;
411e1bfb 14819 outrel.r_addend = rel->r_addend;
65f38f15 14820
1cf1f670
AM
14821 /* Optimize unaligned reloc use. */
14822 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14823 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14824 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14825 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14826 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14827 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14828 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14829 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14830 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14831
65f38f15 14832 if (skip)
0bb2d96a 14833 memset (&outrel, 0, sizeof outrel);
afe397ea 14834 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14835 && !is_opd
14836 && r_type != R_PPC64_TOC)
14acf4dc
MR
14837 {
14838 BFD_ASSERT (h->elf.dynindx != -1);
14839 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14840 }
65f38f15
AM
14841 else
14842 {
41bd81ab
AM
14843 /* This symbol is local, or marked to become local,
14844 or this is an opd section reloc which must point
14845 at a local function. */
65f38f15 14846 outrel.r_addend += relocation;
e86ce104 14847 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14848 {
3fad3c7c 14849 if (is_opd && h != NULL)
afbe61cf
AM
14850 {
14851 /* Lie about opd entries. This case occurs
14852 when building shared libraries and we
14853 reference a function in another shared
3fad3c7c
AM
14854 lib. The same thing happens for a weak
14855 definition in an application that's
14856 overridden by a strong definition in a
14857 shared lib. (I believe this is a generic
14858 bug in binutils handling of weak syms.)
14859 In these cases we won't use the opd
1e2f5b6e 14860 entry in this lib. */
b34976b6 14861 unresolved_reloc = FALSE;
afbe61cf 14862 }
25f23106
AM
14863 if (!is_opd
14864 && r_type == R_PPC64_ADDR64
14865 && (h != NULL
14866 ? h->elf.type == STT_GNU_IFUNC
14867 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14868 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14869 else
14870 {
14871 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14872
25f23106
AM
14873 /* We need to relocate .opd contents for ld.so.
14874 Prelink also wants simple and consistent rules
14875 for relocs. This make all RELATIVE relocs have
14876 *r_offset equal to r_addend. */
14877 relocate = TRUE;
14878 }
65f38f15
AM
14879 }
14880 else
14881 {
14882 long indx = 0;
14883
25f23106
AM
14884 if (h != NULL
14885 ? h->elf.type == STT_GNU_IFUNC
14886 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14887 {
25f53a85 14888 info->callbacks->einfo
695344c0 14889 /* xgettext:c-format */
174d0a74 14890 (_("%H: %s for indirect "
bc30df16 14891 "function `%T' unsupported\n"),
25f53a85 14892 input_bfd, input_section, rel->r_offset,
25f23106
AM
14893 ppc64_elf_howto_table[r_type]->name,
14894 sym_name);
14895 ret = FALSE;
14896 }
cf35638d 14897 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14898 ;
14899 else if (sec == NULL || sec->owner == NULL)
14900 {
14901 bfd_set_error (bfd_error_bad_value);
b34976b6 14902 return FALSE;
65f38f15
AM
14903 }
14904 else
14905 {
14906 asection *osec;
14907
14908 osec = sec->output_section;
14909 indx = elf_section_data (osec)->dynindx;
14910
74541ad4
AM
14911 if (indx == 0)
14912 {
14913 if ((osec->flags & SEC_READONLY) == 0
14914 && htab->elf.data_index_section != NULL)
14915 osec = htab->elf.data_index_section;
14916 else
14917 osec = htab->elf.text_index_section;
14918 indx = elf_section_data (osec)->dynindx;
14919 }
14920 BFD_ASSERT (indx != 0);
14921
65f38f15
AM
14922 /* We are turning this relocation into one
14923 against a section symbol, so subtract out
14924 the output section's address but not the
14925 offset of the input section in the output
14926 section. */
14927 outrel.r_addend -= osec->vma;
14928 }
14929
14930 outrel.r_info = ELF64_R_INFO (indx, r_type);
14931 }
14932 }
14933
14934 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14935 if (h != NULL
14936 ? h->elf.type == STT_GNU_IFUNC
14937 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
33e44f2e 14938 sreloc = htab->elf.irelplt;
65f38f15
AM
14939 if (sreloc == NULL)
14940 abort ();
14941
dfbb6ac9
AM
14942 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14943 >= sreloc->size)
14944 abort ();
947216bf
AM
14945 loc = sreloc->contents;
14946 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14947 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14948
14949 /* If this reloc is against an external symbol, it will
14950 be computed at runtime, so there's no need to do
81407a69
AM
14951 anything now. However, for the sake of prelink ensure
14952 that the section contents are a known value. */
65f38f15 14953 if (! relocate)
81407a69
AM
14954 {
14955 unresolved_reloc = FALSE;
14956 /* The value chosen here is quite arbitrary as ld.so
14957 ignores section contents except for the special
14958 case of .opd where the contents might be accessed
14959 before relocation. Choose zero, as that won't
14960 cause reloc overflow. */
14961 relocation = 0;
14962 addend = 0;
14963 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14964 to improve backward compatibility with older
14965 versions of ld. */
14966 if (r_type == R_PPC64_ADDR64)
14967 addend = outrel.r_addend;
14968 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 14969 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 14970 addend = outrel.r_offset;
81407a69 14971 }
65f38f15 14972 }
f0158f44
AM
14973 else if (r_type == R_PPC64_DTPMOD64
14974 && htab->params->tls_get_addr_opt
14975 && htab->tls_get_addr_fd != NULL
14976 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
14977 {
14978 /* Set up for __tls_get_addr_opt stub, when this entry
14979 does not have dynamic relocs. */
14980 relocation = 0;
14981 /* Set up the next word for local dynamic. If it turns
14982 out to be global dynamic, the reloc will overwrite
14983 this value. */
14984 if (rel->r_offset + 16 <= input_section->size)
14985 bfd_put_64 (input_bfd, DTP_OFFSET - TP_OFFSET,
14986 contents + rel->r_offset + 8);
14987 }
14988 else if (r_type == R_PPC64_DTPREL64
14989 && htab->params->tls_get_addr_opt
14990 && htab->tls_get_addr_fd != NULL
14991 && htab->tls_get_addr_fd->elf.plt.plist != NULL
14992 && rel > relocs
14993 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
14994 && rel[-1].r_offset + 8 == rel->r_offset)
14995 {
14996 /* __tls_get_addr_opt stub value. */
14997 addend += DTP_OFFSET - TP_OFFSET;
14998 }
5bd4f169
AM
14999 break;
15000
65f38f15
AM
15001 case R_PPC64_COPY:
15002 case R_PPC64_GLOB_DAT:
15003 case R_PPC64_JMP_SLOT:
25f23106 15004 case R_PPC64_JMP_IREL:
65f38f15
AM
15005 case R_PPC64_RELATIVE:
15006 /* We shouldn't ever see these dynamic relocs in relocatable
15007 files. */
ae9a127f 15008 /* Fall through. */
65f38f15
AM
15009
15010 case R_PPC64_PLTGOT16:
15011 case R_PPC64_PLTGOT16_DS:
15012 case R_PPC64_PLTGOT16_HA:
15013 case R_PPC64_PLTGOT16_HI:
15014 case R_PPC64_PLTGOT16_LO:
15015 case R_PPC64_PLTGOT16_LO_DS:
15016 case R_PPC64_PLTREL32:
15017 case R_PPC64_PLTREL64:
15018 /* These ones haven't been implemented yet. */
15019
25f53a85 15020 info->callbacks->einfo
695344c0 15021 /* xgettext:c-format */
bc30df16 15022 (_("%P: %B: %s is not supported for `%T'\n"),
d003868e 15023 input_bfd,
4ce794b7 15024 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
15025
15026 bfd_set_error (bfd_error_invalid_operation);
b34976b6 15027 ret = FALSE;
c316a17c 15028 goto copy_reloc;
65f38f15 15029 }
5bd4f169 15030
67f0cbdb
AM
15031 /* Multi-instruction sequences that access the TOC can be
15032 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15033 to nop; addi rb,r2,x; */
15034 switch (r_type)
15035 {
15036 default:
15037 break;
15038
15039 case R_PPC64_GOT_TLSLD16_HI:
15040 case R_PPC64_GOT_TLSGD16_HI:
15041 case R_PPC64_GOT_TPREL16_HI:
15042 case R_PPC64_GOT_DTPREL16_HI:
15043 case R_PPC64_GOT16_HI:
15044 case R_PPC64_TOC16_HI:
15045 /* These relocs would only be useful if building up an
15046 offset to later add to r2, perhaps in an indexed
15047 addressing mode instruction. Don't try to optimize.
15048 Unfortunately, the possibility of someone building up an
15049 offset like this or even with the HA relocs, means that
15050 we need to check the high insn when optimizing the low
15051 insn. */
15052 break;
15053
15054 case R_PPC64_GOT_TLSLD16_HA:
15055 case R_PPC64_GOT_TLSGD16_HA:
15056 case R_PPC64_GOT_TPREL16_HA:
15057 case R_PPC64_GOT_DTPREL16_HA:
15058 case R_PPC64_GOT16_HA:
15059 case R_PPC64_TOC16_HA:
98528052 15060 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15061 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
15062 {
15063 bfd_byte *p = contents + (rel->r_offset & ~3);
15064 bfd_put_32 (input_bfd, NOP, p);
15065 }
67f0cbdb
AM
15066 break;
15067
15068 case R_PPC64_GOT_TLSLD16_LO:
15069 case R_PPC64_GOT_TLSGD16_LO:
15070 case R_PPC64_GOT_TPREL16_LO_DS:
15071 case R_PPC64_GOT_DTPREL16_LO_DS:
15072 case R_PPC64_GOT16_LO:
15073 case R_PPC64_GOT16_LO_DS:
15074 case R_PPC64_TOC16_LO:
15075 case R_PPC64_TOC16_LO_DS:
98528052 15076 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15077 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15078 {
15079 bfd_byte *p = contents + (rel->r_offset & ~3);
15080 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15081 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15082 {
15083 /* Transform addic to addi when we change reg. */
15084 insn &= ~((0x3f << 26) | (0x1f << 16));
15085 insn |= (14u << 26) | (2 << 16);
15086 }
15087 else
67f0cbdb 15088 {
98528052
AM
15089 insn &= ~(0x1f << 16);
15090 insn |= 2 << 16;
67f0cbdb 15091 }
560c8763 15092 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15093 }
15094 break;
15095 }
15096
65f38f15 15097 /* Do any further special processing. */
b80eed39 15098 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15
AM
15099 switch (r_type)
15100 {
15101 default:
15102 break;
15103
25f23106 15104 case R_PPC64_REL16_HA:
a680de9a 15105 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15106 case R_PPC64_ADDR16_HA:
15107 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15108 case R_PPC64_ADDR16_HIGHERA:
15109 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15110 case R_PPC64_TOC16_HA:
15111 case R_PPC64_SECTOFF_HA:
411e1bfb 15112 case R_PPC64_TPREL16_HA:
f9c6b907 15113 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15114 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15115 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15116 case R_PPC64_DTPREL16_HA:
15117 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15118 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15119 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15120 /* It's just possible that this symbol is a weak symbol
15121 that's not actually defined anywhere. In that case,
15122 'sec' would be NULL, and we should leave the symbol
15123 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15124 if (sec == NULL)
15125 break;
1a0670f3 15126 /* Fall through. */
5c5f6e17
AM
15127
15128 case R_PPC64_GOT16_HA:
15129 case R_PPC64_PLTGOT16_HA:
15130 case R_PPC64_PLT16_HA:
15131 case R_PPC64_GOT_TLSGD16_HA:
15132 case R_PPC64_GOT_TLSLD16_HA:
15133 case R_PPC64_GOT_TPREL16_HA:
15134 case R_PPC64_GOT_DTPREL16_HA:
15135 /* Add 0x10000 if sign bit in 0:15 is set.
15136 Bits 0:15 are not used. */
15137 addend += 0x8000;
65f38f15
AM
15138 break;
15139
15140 case R_PPC64_ADDR16_DS:
15141 case R_PPC64_ADDR16_LO_DS:
15142 case R_PPC64_GOT16_DS:
15143 case R_PPC64_GOT16_LO_DS:
15144 case R_PPC64_PLT16_LO_DS:
15145 case R_PPC64_SECTOFF_DS:
15146 case R_PPC64_SECTOFF_LO_DS:
15147 case R_PPC64_TOC16_DS:
15148 case R_PPC64_TOC16_LO_DS:
15149 case R_PPC64_PLTGOT16_DS:
15150 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15151 case R_PPC64_GOT_TPREL16_DS:
15152 case R_PPC64_GOT_TPREL16_LO_DS:
15153 case R_PPC64_GOT_DTPREL16_DS:
15154 case R_PPC64_GOT_DTPREL16_LO_DS:
15155 case R_PPC64_TPREL16_DS:
15156 case R_PPC64_TPREL16_LO_DS:
15157 case R_PPC64_DTPREL16_DS:
15158 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15159 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15160 mask = 3;
a680de9a
PB
15161 /* If this reloc is against an lq, lxv, or stxv insn, then
15162 the value must be a multiple of 16. This is somewhat of
15163 a hack, but the "correct" way to do this by defining _DQ
15164 forms of all the _DS relocs bloats all reloc switches in
15165 this file. It doesn't make much sense to use these
15166 relocs in data, so testing the insn should be safe. */
15167 if ((insn & (0x3f << 26)) == (56u << 26)
15168 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15169 mask = 15;
a680de9a
PB
15170 relocation += addend;
15171 addend = insn & (mask ^ 3);
15172 if ((relocation & mask) != 0)
65f38f15 15173 {
a680de9a 15174 relocation ^= relocation & mask;
25f53a85 15175 info->callbacks->einfo
695344c0 15176 /* xgettext:c-format */
174d0a74 15177 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 15178 input_bfd, input_section, rel->r_offset,
b80eed39 15179 howto->name,
adadcc0c 15180 mask + 1);
65f38f15 15181 bfd_set_error (bfd_error_bad_value);
b34976b6 15182 ret = FALSE;
c316a17c 15183 goto copy_reloc;
65f38f15
AM
15184 }
15185 break;
5bd4f169
AM
15186 }
15187
239e1f3a
AM
15188 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15189 because such sections are not SEC_ALLOC and thus ld.so will
15190 not process them. */
65f38f15 15191 if (unresolved_reloc
239e1f3a 15192 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15193 && h->elf.def_dynamic)
15194 && _bfd_elf_section_offset (output_bfd, info, input_section,
15195 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15196 {
25f53a85 15197 info->callbacks->einfo
695344c0 15198 /* xgettext:c-format */
174d0a74 15199 (_("%H: unresolvable %s against `%T'\n"),
25f53a85 15200 input_bfd, input_section, rel->r_offset,
b80eed39 15201 howto->name,
039b3fef 15202 h->elf.root.root.string);
b34976b6 15203 ret = FALSE;
9c07fe7c 15204 }
5bd4f169 15205
b80eed39
AM
15206 /* 16-bit fields in insns mostly have signed values, but a
15207 few insns have 16-bit unsigned values. Really, we should
15208 have different reloc types. */
15209 if (howto->complain_on_overflow != complain_overflow_dont
15210 && howto->dst_mask == 0xffff
15211 && (input_section->flags & SEC_CODE) != 0)
15212 {
15213 enum complain_overflow complain = complain_overflow_signed;
15214
15215 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15216 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15217 complain = complain_overflow_bitfield;
15218 else if (howto->rightshift == 0
15219 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15220 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15221 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15222 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15223 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15224 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15225 complain = complain_overflow_unsigned;
15226 if (howto->complain_on_overflow != complain)
15227 {
15228 alt_howto = *howto;
15229 alt_howto.complain_on_overflow = complain;
15230 howto = &alt_howto;
15231 }
15232 }
15233
a680de9a
PB
15234 if (r_type == R_PPC64_REL16DX_HA)
15235 {
15236 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15237 if (rel->r_offset + 4 > input_section->size)
15238 r = bfd_reloc_outofrange;
15239 else
15240 {
15241 relocation += addend;
15242 relocation -= (rel->r_offset
15243 + input_section->output_offset
15244 + input_section->output_section->vma);
15245 relocation = (bfd_signed_vma) relocation >> 16;
15246 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15247 insn &= ~0x1fffc1;
15248 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15249 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15250 r = bfd_reloc_ok;
15251 if (relocation + 0x8000 > 0xffff)
15252 r = bfd_reloc_overflow;
15253 }
15254 }
15255 else
15256 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15257 rel->r_offset, relocation, addend);
5bd4f169 15258
ef60b7ff 15259 if (r != bfd_reloc_ok)
5bd4f169 15260 {
bc30df16 15261 char *more_info = NULL;
b80eed39 15262 const char *reloc_name = howto->name;
bc30df16
AM
15263
15264 if (reloc_dest != DEST_NORMAL)
15265 {
15266 more_info = bfd_malloc (strlen (reloc_name) + 8);
15267 if (more_info != NULL)
15268 {
15269 strcpy (more_info, reloc_name);
15270 strcat (more_info, (reloc_dest == DEST_OPD
15271 ? " (OPD)" : " (stub)"));
15272 reloc_name = more_info;
15273 }
15274 }
15275
cd27b276 15276 if (r == bfd_reloc_overflow)
5bd4f169 15277 {
8131c122
AM
15278 /* On code like "if (foo) foo();" don't report overflow
15279 on a branch to zero when foo is undefined. */
15280 if (!warned
15281 && (reloc_dest == DEST_STUB
15282 || !(h != NULL
15283 && (h->elf.root.type == bfd_link_hash_undefweak
15284 || h->elf.root.type == bfd_link_hash_undefined)
15285 && is_branch_reloc (r_type))))
1a72702b
AM
15286 info->callbacks->reloc_overflow (info, &h->elf.root,
15287 sym_name, reloc_name,
15288 orig_rel.r_addend,
15289 input_bfd, input_section,
15290 rel->r_offset);
ef60b7ff
AM
15291 }
15292 else
15293 {
25f53a85 15294 info->callbacks->einfo
695344c0 15295 /* xgettext:c-format */
174d0a74 15296 (_("%H: %s against `%T': error %d\n"),
25f53a85 15297 input_bfd, input_section, rel->r_offset,
bc30df16 15298 reloc_name, sym_name, (int) r);
b34976b6 15299 ret = FALSE;
ef60b7ff 15300 }
bc30df16
AM
15301 if (more_info != NULL)
15302 free (more_info);
5bd4f169 15303 }
c316a17c
AM
15304 copy_reloc:
15305 if (wrel != rel)
15306 *wrel = *rel;
15307 }
15308
15309 if (wrel != rel)
15310 {
15311 Elf_Internal_Shdr *rel_hdr;
15312 size_t deleted = rel - wrel;
15313
15314 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15315 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15316 if (rel_hdr->sh_size == 0)
15317 {
15318 /* It is too late to remove an empty reloc section. Leave
15319 one NONE reloc.
15320 ??? What is wrong with an empty section??? */
15321 rel_hdr->sh_size = rel_hdr->sh_entsize;
15322 deleted -= 1;
15323 }
15324 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15325 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15326 input_section->reloc_count -= deleted;
5bd4f169
AM
15327 }
15328
645ea6a9
AM
15329 /* If we're emitting relocations, then shortly after this function
15330 returns, reloc offsets and addends for this section will be
15331 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15332 file rather than the input. Save a copy of the relocs for
15333 opd_entry_value. */
0e1862bb 15334 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15335 {
15336 bfd_size_type amt;
15337 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15338 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15339 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15340 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15341 if (rel == NULL)
15342 return FALSE;
15343 memcpy (rel, relocs, amt);
15344 }
5bd4f169
AM
15345 return ret;
15346}
15347
754021d0
AM
15348/* Adjust the value of any local symbols in opd sections. */
15349
6e0b88f1 15350static int
754021d0
AM
15351ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15352 const char *name ATTRIBUTE_UNUSED,
15353 Elf_Internal_Sym *elfsym,
15354 asection *input_sec,
15355 struct elf_link_hash_entry *h)
15356{
74f0fb50
AM
15357 struct _opd_sec_data *opd;
15358 long adjust;
754021d0
AM
15359 bfd_vma value;
15360
4025353c 15361 if (h != NULL)
6e0b88f1 15362 return 1;
4025353c 15363
74f0fb50
AM
15364 opd = get_opd_info (input_sec);
15365 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15366 return 1;
754021d0
AM
15367
15368 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15369 if (!bfd_link_relocatable (info))
754021d0
AM
15370 value -= input_sec->output_section->vma;
15371
51aecdc5 15372 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15373 if (adjust == -1)
6e0b88f1
AM
15374 return 2;
15375
15376 elfsym->st_value += adjust;
15377 return 1;
754021d0
AM
15378}
15379
5bd4f169
AM
15380/* Finish up dynamic symbol handling. We set the contents of various
15381 dynamic sections here. */
15382
b34976b6 15383static bfd_boolean
4ce794b7
AM
15384ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15385 struct bfd_link_info *info,
15386 struct elf_link_hash_entry *h,
ab6dce23 15387 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5bd4f169 15388{
65f38f15 15389 struct ppc_link_hash_table *htab;
8387904d
AM
15390 struct plt_entry *ent;
15391 Elf_Internal_Rela rela;
15392 bfd_byte *loc;
5bd4f169 15393
65f38f15 15394 htab = ppc_hash_table (info);
4dfe6ac6
NC
15395 if (htab == NULL)
15396 return FALSE;
5bd4f169 15397
8387904d
AM
15398 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15399 if (ent->plt.offset != (bfd_vma) -1)
15400 {
15401 /* This symbol has an entry in the procedure linkage
15402 table. Set it up. */
e054468f
AM
15403 if (!htab->elf.dynamic_sections_created
15404 || h->dynindx == -1)
15405 {
15406 BFD_ASSERT (h->type == STT_GNU_IFUNC
15407 && h->def_regular
15408 && (h->root.type == bfd_link_hash_defined
15409 || h->root.type == bfd_link_hash_defweak));
33e44f2e
AM
15410 rela.r_offset = (htab->elf.iplt->output_section->vma
15411 + htab->elf.iplt->output_offset
25f23106 15412 + ent->plt.offset);
ee67d69a
AM
15413 if (htab->opd_abi)
15414 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15415 else
15416 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
15417 rela.r_addend = (h->root.u.def.value
15418 + h->root.u.def.section->output_offset
15419 + h->root.u.def.section->output_section->vma
15420 + ent->addend);
33e44f2e
AM
15421 loc = (htab->elf.irelplt->contents
15422 + (htab->elf.irelplt->reloc_count++
25f23106 15423 * sizeof (Elf64_External_Rela)));
e054468f
AM
15424 }
15425 else
15426 {
33e44f2e
AM
15427 rela.r_offset = (htab->elf.splt->output_section->vma
15428 + htab->elf.splt->output_offset
25f23106 15429 + ent->plt.offset);
e054468f
AM
15430 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15431 rela.r_addend = ent->addend;
33e44f2e 15432 loc = (htab->elf.srelplt->contents
b9e5796b
AM
15433 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15434 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
e054468f 15435 }
8387904d 15436 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a345bc8d
AM
15437
15438 if (!htab->opd_abi)
15439 {
15440 if (!h->def_regular)
15441 {
15442 /* Mark the symbol as undefined, rather than as
15443 defined in glink. Leave the value if there were
15444 any relocations where pointer equality matters
15445 (this is a clue for the dynamic linker, to make
15446 function pointer comparisons work between an
15447 application and shared library), otherwise set it
15448 to zero. */
15449 sym->st_shndx = SHN_UNDEF;
15450 if (!h->pointer_equality_needed)
15451 sym->st_value = 0;
15452 else if (!h->ref_regular_nonweak)
15453 {
15454 /* This breaks function pointer comparisons, but
15455 that is better than breaking tests for a NULL
15456 function pointer. */
15457 sym->st_value = 0;
15458 }
15459 }
15460 }
8387904d 15461 }
5bd4f169 15462
f5385ebf 15463 if (h->needs_copy)
5bd4f169 15464 {
65f38f15 15465 /* This symbol needs a copy reloc. Set it up. */
5474d94f 15466 asection *srel;
5bd4f169 15467
65f38f15
AM
15468 if (h->dynindx == -1
15469 || (h->root.type != bfd_link_hash_defined
15470 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
15471 || htab->elf.srelbss == NULL
15472 || htab->elf.sreldynrelro == NULL)
65f38f15 15473 abort ();
5bd4f169
AM
15474
15475 rela.r_offset = (h->root.u.def.value
15476 + h->root.u.def.section->output_section->vma
15477 + h->root.u.def.section->output_offset);
15478 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15479 rela.r_addend = 0;
5474d94f
AM
15480 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
15481 srel = htab->elf.sreldynrelro;
15482 else
15483 srel = htab->elf.srelbss;
15484 loc = srel->contents;
15485 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15486 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15487 }
15488
b34976b6 15489 return TRUE;
5bd4f169
AM
15490}
15491
65f38f15
AM
15492/* Used to decide how to sort relocs in an optimal manner for the
15493 dynamic linker, before writing them out. */
15494
15495static enum elf_reloc_type_class
7e612e98
AM
15496ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15497 const asection *rel_sec,
15498 const Elf_Internal_Rela *rela)
65f38f15 15499{
04c9666a 15500 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15501 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15502
33e44f2e 15503 if (rel_sec == htab->elf.irelplt)
7e612e98 15504 return reloc_class_ifunc;
a33d1f77 15505
4ce794b7 15506 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15507 switch (r_type)
65f38f15
AM
15508 {
15509 case R_PPC64_RELATIVE:
15510 return reloc_class_relative;
15511 case R_PPC64_JMP_SLOT:
15512 return reloc_class_plt;
15513 case R_PPC64_COPY:
15514 return reloc_class_copy;
15515 default:
15516 return reloc_class_normal;
15517 }
15518}
15519
5bd4f169
AM
15520/* Finish up the dynamic sections. */
15521
b34976b6 15522static bfd_boolean
4ce794b7
AM
15523ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15524 struct bfd_link_info *info)
5bd4f169 15525{
65f38f15
AM
15526 struct ppc_link_hash_table *htab;
15527 bfd *dynobj;
5bd4f169 15528 asection *sdyn;
5bd4f169 15529
65f38f15 15530 htab = ppc_hash_table (info);
4dfe6ac6
NC
15531 if (htab == NULL)
15532 return FALSE;
15533
65f38f15 15534 dynobj = htab->elf.dynobj;
3d4d4302 15535 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15536
65f38f15 15537 if (htab->elf.dynamic_sections_created)
5bd4f169 15538 {
5bd4f169
AM
15539 Elf64_External_Dyn *dyncon, *dynconend;
15540
33e44f2e 15541 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15542 abort ();
5bd4f169
AM
15543
15544 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15545 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15546 for (; dyncon < dynconend; dyncon++)
15547 {
15548 Elf_Internal_Dyn dyn;
19397422 15549 asection *s;
5bd4f169
AM
15550
15551 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15552
15553 switch (dyn.d_tag)
15554 {
65f38f15
AM
15555 default:
15556 continue;
5bd4f169 15557
5d1634d7 15558 case DT_PPC64_GLINK:
4ce794b7 15559 s = htab->glink;
6348e046 15560 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15561 /* We stupidly defined DT_PPC64_GLINK to be the start
15562 of glink rather than the first entry point, which is
15563 what ld.so needs, and now have a bigger stub to
15564 support automatic multiple TOCs. */
b9e5796b 15565 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
5d1634d7
AM
15566 break;
15567
19397422
AM
15568 case DT_PPC64_OPD:
15569 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15570 if (s == NULL)
15571 continue;
15572 dyn.d_un.d_ptr = s->vma;
19397422
AM
15573 break;
15574
e8910a83
AM
15575 case DT_PPC64_OPT:
15576 if (htab->do_multi_toc && htab->multi_toc_needed)
15577 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15578 break;
15579
19397422
AM
15580 case DT_PPC64_OPDSZ:
15581 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15582 if (s == NULL)
15583 continue;
eea6121a 15584 dyn.d_un.d_val = s->size;
19397422
AM
15585 break;
15586
65f38f15 15587 case DT_PLTGOT:
33e44f2e 15588 s = htab->elf.splt;
6348e046 15589 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15590 break;
15591
15592 case DT_JMPREL:
33e44f2e 15593 s = htab->elf.srelplt;
6348e046 15594 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15595 break;
5bd4f169 15596
65f38f15 15597 case DT_PLTRELSZ:
33e44f2e 15598 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 15599 break;
5bd4f169 15600 }
5bd4f169 15601
65f38f15 15602 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15603 }
5bd4f169
AM
15604 }
15605
33e44f2e 15606 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
5d1634d7
AM
15607 {
15608 /* Fill in the first entry in the global offset table.
15609 We use it to hold the link-time TOCbase. */
15610 bfd_put_64 (output_bfd,
60ee0d4a 15611 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15612 htab->elf.sgot->contents);
5d1634d7
AM
15613
15614 /* Set .got entry size. */
33e44f2e 15615 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
15616 }
15617
33e44f2e 15618 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5d1634d7
AM
15619 {
15620 /* Set .plt entry size. */
33e44f2e 15621 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15622 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15623 }
15624
84f5d08e
AM
15625 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15626 brlt ourselves if emitrelocations. */
15627 if (htab->brlt != NULL
15628 && htab->brlt->reloc_count != 0
15629 && !_bfd_elf_link_output_relocs (output_bfd,
15630 htab->brlt,
d4730f92 15631 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15632 elf_section_data (htab->brlt)->relocs,
15633 NULL))
15634 return FALSE;
15635
176a0d42
AM
15636 if (htab->glink != NULL
15637 && htab->glink->reloc_count != 0
15638 && !_bfd_elf_link_output_relocs (output_bfd,
15639 htab->glink,
d4730f92 15640 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15641 elf_section_data (htab->glink)->relocs,
15642 NULL))
15643 return FALSE;
15644
58d180e8 15645 if (htab->glink_eh_frame != NULL
da44f4e5
AM
15646 && htab->glink_eh_frame->size != 0)
15647 {
15648 bfd_vma val;
15649 bfd_byte *p;
15650 asection *stub_sec;
15651
15652 p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15653 for (stub_sec = htab->params->stub_bfd->sections;
15654 stub_sec != NULL;
15655 stub_sec = stub_sec->next)
15656 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15657 {
15658 /* FDE length. */
15659 p += 4;
15660 /* CIE pointer. */
15661 p += 4;
15662 /* Offset to stub section. */
15663 val = (stub_sec->output_section->vma
15664 + stub_sec->output_offset);
15665 val -= (htab->glink_eh_frame->output_section->vma
15666 + htab->glink_eh_frame->output_offset
15667 + (p - htab->glink_eh_frame->contents));
15668 if (val + 0x80000000 > 0xffffffff)
15669 {
15670 info->callbacks->einfo
15671 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15672 stub_sec->name);
15673 return FALSE;
15674 }
15675 bfd_put_32 (dynobj, val, p);
15676 p += 4;
15677 /* stub section size. */
15678 p += 4;
15679 /* Augmentation. */
15680 p += 1;
15681 /* Pad. */
15682 p += 7;
15683 }
15684 if (htab->glink != NULL && htab->glink->size != 0)
15685 {
15686 /* FDE length. */
15687 p += 4;
15688 /* CIE pointer. */
15689 p += 4;
15690 /* Offset to .glink. */
15691 val = (htab->glink->output_section->vma
15692 + htab->glink->output_offset
15693 + 8);
15694 val -= (htab->glink_eh_frame->output_section->vma
15695 + htab->glink_eh_frame->output_offset
15696 + (p - htab->glink_eh_frame->contents));
15697 if (val + 0x80000000 > 0xffffffff)
15698 {
15699 info->callbacks->einfo
15700 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15701 htab->glink->name);
15702 return FALSE;
15703 }
15704 bfd_put_32 (dynobj, val, p);
15705 p += 4;
15706 /* .glink size. */
15707 p += 4;
15708 /* Augmentation. */
15709 p += 1;
15710 /* Ops. */
15711 p += 7;
15712 }
15713
15714 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15715 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15716 htab->glink_eh_frame,
15717 htab->glink_eh_frame->contents))
15718 return FALSE;
15719 }
58d180e8 15720
e717da7e 15721 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15722 since we didn't add them to DYNOBJ. We know dynobj is the first
15723 bfd. */
c72f2fb2 15724 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15725 {
15726 asection *s;
7b53ace3 15727
0c8d6e5c 15728 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15729 continue;
15730
e717da7e
AM
15731 s = ppc64_elf_tdata (dynobj)->got;
15732 if (s != NULL
eea6121a 15733 && s->size != 0
e717da7e
AM
15734 && s->output_section != bfd_abs_section_ptr
15735 && !bfd_set_section_contents (output_bfd, s->output_section,
15736 s->contents, s->output_offset,
eea6121a 15737 s->size))
e717da7e
AM
15738 return FALSE;
15739 s = ppc64_elf_tdata (dynobj)->relgot;
15740 if (s != NULL
eea6121a 15741 && s->size != 0
e717da7e
AM
15742 && s->output_section != bfd_abs_section_ptr
15743 && !bfd_set_section_contents (output_bfd, s->output_section,
15744 s->contents, s->output_offset,
eea6121a 15745 s->size))
e717da7e
AM
15746 return FALSE;
15747 }
f6c52c13 15748
b34976b6 15749 return TRUE;
5bd4f169
AM
15750}
15751
5bd4f169 15752#include "elf64-target.h"
7b8e7dad
AM
15753
15754/* FreeBSD support */
15755
15756#undef TARGET_LITTLE_SYM
15757#undef TARGET_LITTLE_NAME
15758
15759#undef TARGET_BIG_SYM
6d00b590 15760#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15761#undef TARGET_BIG_NAME
15762#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15763
15764#undef ELF_OSABI
15765#define ELF_OSABI ELFOSABI_FREEBSD
15766
15767#undef elf64_bed
15768#define elf64_bed elf64_powerpc_fbsd_bed
15769
15770#include "elf64-target.h"
15771
This page took 2.661231 seconds and 4 git commands to generate.