Automatic date update in version.in
[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
7ba71655
AM
2048 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2049 HOWTO (R_PPC64_16DX_HA, /* type */
2050 16, /* rightshift */
2051 2, /* size (0 = byte, 1 = short, 2 = long) */
2052 16, /* bitsize */
2053 FALSE, /* pc_relative */
2054 0, /* bitpos */
2055 complain_overflow_signed, /* complain_on_overflow */
2056 ppc64_elf_ha_reloc, /* special_function */
2057 "R_PPC64_16DX_HA", /* name */
2058 FALSE, /* partial_inplace */
2059 0, /* src_mask */
2060 0x1fffc1, /* dst_mask */
2061 FALSE), /* pcrel_offset */
2062
f9c6b907
AM
2063 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2064 HOWTO (R_PPC64_ADDR16_HIGH, /* 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 bfd_elf_generic_reloc, /* special_function */
2072 "R_PPC64_ADDR16_HIGH", /* name */
2073 FALSE, /* partial_inplace */
2074 0, /* src_mask */
2075 0xffff, /* dst_mask */
2076 FALSE), /* pcrel_offset */
2077
2078 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2079 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
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_ha_reloc, /* special_function */
2087 "R_PPC64_ADDR16_HIGHA", /* name */
2088 FALSE, /* partial_inplace */
2089 0, /* src_mask */
2090 0xffff, /* dst_mask */
2091 FALSE), /* pcrel_offset */
2092
2093 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2094 HOWTO (R_PPC64_DTPREL16_HIGH,
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_HIGH", /* name */
2103 FALSE, /* partial_inplace */
2104 0, /* src_mask */
2105 0xffff, /* dst_mask */
2106 FALSE), /* pcrel_offset */
2107
2108 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2109 HOWTO (R_PPC64_DTPREL16_HIGHA,
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_DTPREL16_HIGHA", /* name */
2118 FALSE, /* partial_inplace */
2119 0, /* src_mask */
2120 0xffff, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2122
2123 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2124 HOWTO (R_PPC64_TPREL16_HIGH,
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_HIGH", /* name */
2133 FALSE, /* partial_inplace */
2134 0, /* src_mask */
2135 0xffff, /* dst_mask */
2136 FALSE), /* pcrel_offset */
2137
2138 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2139 HOWTO (R_PPC64_TPREL16_HIGHA,
2140 16, /* rightshift */
2141 1, /* size (0 = byte, 1 = short, 2 = long) */
2142 16, /* bitsize */
2143 FALSE, /* pc_relative */
2144 0, /* bitpos */
2145 complain_overflow_dont, /* complain_on_overflow */
2146 ppc64_elf_unhandled_reloc, /* special_function */
2147 "R_PPC64_TPREL16_HIGHA", /* name */
2148 FALSE, /* partial_inplace */
2149 0, /* src_mask */
2150 0xffff, /* dst_mask */
2151 FALSE), /* pcrel_offset */
2152
006589cf
AM
2153 /* Marker reloc on ELFv2 large-model function entry. */
2154 HOWTO (R_PPC64_ENTRY,
2155 0, /* rightshift */
2156 2, /* size (0 = byte, 1 = short, 2 = long) */
2157 32, /* 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_ENTRY", /* name */
2163 FALSE, /* partial_inplace */
2164 0, /* src_mask */
2165 0, /* dst_mask */
2166 FALSE), /* pcrel_offset */
2167
45965137
AM
2168 /* Like ADDR64, but use local entry point of function. */
2169 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2170 0, /* rightshift */
2171 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2172 64, /* bitsize */
2173 FALSE, /* pc_relative */
2174 0, /* bitpos */
2175 complain_overflow_dont, /* complain_on_overflow */
2176 bfd_elf_generic_reloc, /* special_function */
2177 "R_PPC64_ADDR64_LOCAL", /* name */
2178 FALSE, /* partial_inplace */
2179 0, /* src_mask */
2180 ONES (64), /* dst_mask */
2181 FALSE), /* pcrel_offset */
2182
5bd4f169
AM
2183 /* GNU extension to record C++ vtable hierarchy. */
2184 HOWTO (R_PPC64_GNU_VTINHERIT, /* 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_VTINHERIT", /* 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 /* GNU extension to record C++ vtable member usage. */
2199 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2200 0, /* rightshift */
2201 0, /* size (0 = byte, 1 = short, 2 = long) */
2202 0, /* bitsize */
b34976b6 2203 FALSE, /* pc_relative */
5bd4f169
AM
2204 0, /* bitpos */
2205 complain_overflow_dont, /* complain_on_overflow */
2206 NULL, /* special_function */
2207 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2208 FALSE, /* partial_inplace */
5bd4f169
AM
2209 0, /* src_mask */
2210 0, /* dst_mask */
b34976b6 2211 FALSE), /* pcrel_offset */
5bd4f169
AM
2212};
2213
2214\f
2215/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2216 be done. */
2217
2218static void
4ce794b7 2219ppc_howto_init (void)
5bd4f169
AM
2220{
2221 unsigned int i, type;
2222
a4b6fadd 2223 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
2224 {
2225 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 2226 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
2227 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2228 }
2229}
2230
2231static reloc_howto_type *
4ce794b7
AM
2232ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2233 bfd_reloc_code_real_type code)
5bd4f169 2234{
411e1bfb 2235 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2236
2237 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2238 /* Initialize howto table if needed. */
2239 ppc_howto_init ();
2240
4ce794b7 2241 switch (code)
5bd4f169
AM
2242 {
2243 default:
4ce794b7 2244 return NULL;
5bd4f169 2245
411e1bfb
AM
2246 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2247 break;
2248 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2249 break;
2250 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2251 break;
2252 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2253 break;
2254 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2255 break;
2256 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2257 break;
f9c6b907
AM
2258 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2259 break;
411e1bfb 2260 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2261 break;
f9c6b907
AM
2262 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2263 break;
411e1bfb 2264 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2265 break;
411e1bfb 2266 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2267 break;
411e1bfb 2268 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2269 break;
411e1bfb 2270 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2271 break;
411e1bfb 2272 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2273 break;
411e1bfb 2274 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2275 break;
411e1bfb 2276 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2277 break;
411e1bfb 2278 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2279 break;
411e1bfb 2280 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2281 break;
411e1bfb 2282 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2283 break;
411e1bfb 2284 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2285 break;
411e1bfb 2286 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2287 break;
411e1bfb 2288 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2289 break;
411e1bfb 2290 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2291 break;
411e1bfb 2292 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2293 break;
411e1bfb 2294 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2295 break;
411e1bfb 2296 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2297 break;
411e1bfb 2298 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2299 break;
411e1bfb 2300 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2301 break;
411e1bfb 2302 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2303 break;
411e1bfb 2304 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2305 break;
411e1bfb 2306 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2307 break;
411e1bfb 2308 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2309 break;
411e1bfb 2310 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2311 break;
411e1bfb 2312 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2313 break;
411e1bfb 2314 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2315 break;
411e1bfb 2316 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2317 break;
411e1bfb 2318 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2319 break;
411e1bfb 2320 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2321 break;
411e1bfb 2322 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2323 break;
411e1bfb 2324 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2325 break;
411e1bfb 2326 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2327 break;
411e1bfb 2328 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2329 break;
411e1bfb 2330 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2331 break;
411e1bfb 2332 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2333 break;
411e1bfb 2334 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2335 break;
411e1bfb 2336 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2337 break;
411e1bfb 2338 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2339 break;
411e1bfb 2340 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2341 break;
411e1bfb 2342 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2343 break;
411e1bfb 2344 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2345 break;
411e1bfb 2346 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2347 break;
411e1bfb 2348 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2349 break;
411e1bfb 2350 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2351 break;
411e1bfb 2352 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2353 break;
411e1bfb 2354 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2355 break;
411e1bfb 2356 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2357 break;
411e1bfb 2358 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2359 break;
411e1bfb 2360 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2361 break;
411e1bfb 2362 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2363 break;
411e1bfb 2364 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2365 break;
411e1bfb 2366 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2367 break;
411e1bfb 2368 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2369 break;
727fc41e
AM
2370 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2371 break;
2372 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2373 break;
411e1bfb 2374 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2375 break;
411e1bfb 2376 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2377 break;
411e1bfb 2378 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2379 break;
411e1bfb 2380 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2381 break;
f9c6b907
AM
2382 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2383 break;
411e1bfb 2384 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2385 break;
f9c6b907
AM
2386 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2387 break;
411e1bfb 2388 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2389 break;
411e1bfb
AM
2390 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2391 break;
2392 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2393 break;
2394 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2395 break;
f9c6b907
AM
2396 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2397 break;
411e1bfb
AM
2398 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2399 break;
f9c6b907
AM
2400 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2401 break;
411e1bfb
AM
2402 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2403 break;
2404 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2405 break;
2406 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2407 break;
2408 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2409 break;
2410 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2411 break;
2412 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2413 break;
2414 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2415 break;
2416 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2417 break;
2418 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2419 break;
2420 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2421 break;
2422 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2423 break;
2424 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2425 break;
2426 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2427 break;
2428 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2429 break;
2430 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2431 break;
2432 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2433 break;
2434 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2435 break;
2436 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2437 break;
2438 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2439 break;
2440 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2441 break;
2442 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2443 break;
2444 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2445 break;
2446 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2447 break;
2448 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2449 break;
2450 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2451 break;
2452 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2453 break;
2454 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2455 break;
2456 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2457 break;
2458 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2459 break;
25f23106
AM
2460 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2461 break;
2462 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2463 break;
2464 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2465 break;
2466 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2467 break;
7ba71655
AM
2468 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2469 break;
a680de9a
PB
2470 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2471 break;
006589cf
AM
2472 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2473 break;
45965137
AM
2474 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2475 break;
411e1bfb
AM
2476 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2477 break;
2478 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2479 break;
2480 }
2481
4ce794b7 2482 return ppc64_elf_howto_table[r];
5bd4f169
AM
2483};
2484
157090f7
AM
2485static reloc_howto_type *
2486ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2487 const char *r_name)
2488{
2489 unsigned int i;
2490
a4b6fadd 2491 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
2492 if (ppc64_elf_howto_raw[i].name != NULL
2493 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2494 return &ppc64_elf_howto_raw[i];
2495
2496 return NULL;
2497}
2498
5bd4f169
AM
2499/* Set the howto pointer for a PowerPC ELF reloc. */
2500
2501static void
4aef7643 2502ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 2503 Elf_Internal_Rela *dst)
5bd4f169 2504{
65f38f15
AM
2505 unsigned int type;
2506
ef60b7ff 2507 /* Initialize howto table if needed. */
5bd4f169 2508 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2509 ppc_howto_init ();
2510
65f38f15 2511 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 2512 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 2513 {
695344c0 2514 /* xgettext:c-format */
4eca0228
AM
2515 _bfd_error_handler (_("%B: invalid relocation type %d"),
2516 abfd, (int) type);
3ec2b351 2517 type = R_PPC64_NONE;
d0fb9a8d 2518 }
65f38f15 2519 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2520}
2521
04c9666a 2522/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2523
2524static bfd_reloc_status_type
4ce794b7
AM
2525ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2526 void *data, asection *input_section,
2527 bfd *output_bfd, char **error_message)
5bd4f169 2528{
a680de9a
PB
2529 enum elf_ppc64_reloc_type r_type;
2530 long insn;
2531 bfd_size_type octets;
3de43e7b 2532 bfd_vma value;
a680de9a 2533
805fc799
AM
2534 /* If this is a relocatable link (output_bfd test tells us), just
2535 call the generic function. Any adjustment will be done at final
2536 link time. */
2537 if (output_bfd != NULL)
cedb70c5 2538 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2539 input_section, output_bfd, error_message);
2540
2541 /* Adjust the addend for sign extension of the low 16 bits.
2542 We won't actually be using the low 16 bits, so trashing them
2543 doesn't matter. */
2544 reloc_entry->addend += 0x8000;
a680de9a
PB
2545 r_type = reloc_entry->howto->type;
2546 if (r_type != R_PPC64_REL16DX_HA)
2547 return bfd_reloc_continue;
2548
2549 value = 0;
2550 if (!bfd_is_com_section (symbol->section))
2551 value = symbol->value;
2552 value += (reloc_entry->addend
2553 + symbol->section->output_offset
2554 + symbol->section->output_section->vma);
2555 value -= (reloc_entry->address
2556 + input_section->output_offset
2557 + input_section->output_section->vma);
3de43e7b 2558 value = (bfd_signed_vma) value >> 16;
a680de9a
PB
2559
2560 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2561 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2562 insn &= ~0x1fffc1;
3de43e7b 2563 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 2564 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 2565 if (value + 0x8000 > 0xffff)
a680de9a
PB
2566 return bfd_reloc_overflow;
2567 return bfd_reloc_ok;
805fc799 2568}
5bd4f169 2569
2441e016
AM
2570static bfd_reloc_status_type
2571ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2572 void *data, asection *input_section,
2573 bfd *output_bfd, char **error_message)
2574{
2575 if (output_bfd != NULL)
2576 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2577 input_section, output_bfd, error_message);
2578
699733f6
AM
2579 if (strcmp (symbol->section->name, ".opd") == 0
2580 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2581 {
2582 bfd_vma dest = opd_entry_value (symbol->section,
2583 symbol->value + reloc_entry->addend,
aef36ac1 2584 NULL, NULL, FALSE);
2441e016
AM
2585 if (dest != (bfd_vma) -1)
2586 reloc_entry->addend = dest - (symbol->value
2587 + symbol->section->output_section->vma
2588 + symbol->section->output_offset);
2589 }
810d4e75
AM
2590 else
2591 {
2592 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2593
2594 if (symbol->section->owner != abfd
9f284bf9 2595 && symbol->section->owner != NULL
810d4e75
AM
2596 && abiversion (symbol->section->owner) >= 2)
2597 {
2598 unsigned int i;
2599
2600 for (i = 0; i < symbol->section->owner->symcount; ++i)
2601 {
2602 asymbol *symdef = symbol->section->owner->outsymbols[i];
2603
2604 if (strcmp (symdef->name, symbol->name) == 0)
2605 {
2606 elfsym = (elf_symbol_type *) symdef;
2607 break;
2608 }
2609 }
2610 }
2611 reloc_entry->addend
2612 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2613 }
2441e016
AM
2614 return bfd_reloc_continue;
2615}
2616
805fc799 2617static bfd_reloc_status_type
4ce794b7
AM
2618ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2619 void *data, asection *input_section,
2620 bfd *output_bfd, char **error_message)
805fc799
AM
2621{
2622 long insn;
04c9666a 2623 enum elf_ppc64_reloc_type r_type;
805fc799 2624 bfd_size_type octets;
794e51c0
AM
2625 /* Assume 'at' branch hints. */
2626 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2627
2628 /* If this is a relocatable link (output_bfd test tells us), just
2629 call the generic function. Any adjustment will be done at final
2630 link time. */
5bd4f169 2631 if (output_bfd != NULL)
cedb70c5 2632 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2633 input_section, output_bfd, error_message);
2634
2635 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2636 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2637 insn &= ~(0x01 << 21);
4ce794b7 2638 r_type = reloc_entry->howto->type;
805fc799
AM
2639 if (r_type == R_PPC64_ADDR14_BRTAKEN
2640 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2641 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2642
794e51c0 2643 if (is_isa_v2)
5bd4f169 2644 {
805fc799
AM
2645 /* Set 'a' bit. This is 0b00010 in BO field for branch
2646 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2647 for branch on CTR insns (BO == 1a00t or 1a01t). */
2648 if ((insn & (0x14 << 21)) == (0x04 << 21))
2649 insn |= 0x02 << 21;
2650 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2651 insn |= 0x08 << 21;
2652 else
2441e016 2653 goto out;
5bd4f169 2654 }
805fc799
AM
2655 else
2656 {
2657 bfd_vma target = 0;
2658 bfd_vma from;
5bd4f169 2659
805fc799
AM
2660 if (!bfd_is_com_section (symbol->section))
2661 target = symbol->value;
2662 target += symbol->section->output_section->vma;
2663 target += symbol->section->output_offset;
2664 target += reloc_entry->addend;
5bd4f169 2665
805fc799
AM
2666 from = (reloc_entry->address
2667 + input_section->output_offset
2668 + input_section->output_section->vma);
5bd4f169 2669
805fc799
AM
2670 /* Invert 'y' bit if not the default. */
2671 if ((bfd_signed_vma) (target - from) < 0)
2672 insn ^= 0x01 << 21;
2673 }
4ce794b7 2674 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2675 out:
2676 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2677 input_section, output_bfd, error_message);
805fc799 2678}
5bd4f169 2679
805fc799 2680static bfd_reloc_status_type
4ce794b7
AM
2681ppc64_elf_sectoff_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 2690 input_section, output_bfd, error_message);
5bd4f169 2691
805fc799
AM
2692 /* Subtract the symbol section base address. */
2693 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2694 return bfd_reloc_continue;
2695}
2696
805fc799 2697static bfd_reloc_status_type
4ce794b7
AM
2698ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2699 void *data, asection *input_section,
2700 bfd *output_bfd, char **error_message)
805fc799
AM
2701{
2702 /* If this is a relocatable link (output_bfd test tells us), just
2703 call the generic function. Any adjustment will be done at final
2704 link time. */
2705 if (output_bfd != NULL)
cedb70c5 2706 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2707 input_section, output_bfd, error_message);
2708
2709 /* Subtract the symbol section base address. */
2710 reloc_entry->addend -= symbol->section->output_section->vma;
2711
2712 /* Adjust the addend for sign extension of the low 16 bits. */
2713 reloc_entry->addend += 0x8000;
2714 return bfd_reloc_continue;
2715}
2716
2717static bfd_reloc_status_type
4ce794b7
AM
2718ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2719 void *data, asection *input_section,
2720 bfd *output_bfd, char **error_message)
805fc799
AM
2721{
2722 bfd_vma TOCstart;
2723
2724 /* If this is a relocatable link (output_bfd test tells us), just
2725 call the generic function. Any adjustment will be done at final
2726 link time. */
2727 if (output_bfd != NULL)
cedb70c5 2728 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2729 input_section, output_bfd, error_message);
2730
2731 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2732 if (TOCstart == 0)
1c865ab2 2733 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2734
2735 /* Subtract the TOC base address. */
2736 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2737 return bfd_reloc_continue;
2738}
2739
2740static bfd_reloc_status_type
4ce794b7
AM
2741ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2742 void *data, asection *input_section,
2743 bfd *output_bfd, char **error_message)
805fc799
AM
2744{
2745 bfd_vma TOCstart;
2746
2747 /* If this is a relocatable link (output_bfd test tells us), just
2748 call the generic function. Any adjustment will be done at final
2749 link time. */
2750 if (output_bfd != NULL)
cedb70c5 2751 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2752 input_section, output_bfd, error_message);
2753
2754 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2755 if (TOCstart == 0)
1c865ab2 2756 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2757
2758 /* Subtract the TOC base address. */
2759 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2760
2761 /* Adjust the addend for sign extension of the low 16 bits. */
2762 reloc_entry->addend += 0x8000;
2763 return bfd_reloc_continue;
2764}
2765
2766static bfd_reloc_status_type
4ce794b7
AM
2767ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2768 void *data, asection *input_section,
2769 bfd *output_bfd, char **error_message)
805fc799
AM
2770{
2771 bfd_vma TOCstart;
2772 bfd_size_type octets;
2773
2774 /* If this is a relocatable link (output_bfd test tells us), just
2775 call the generic function. Any adjustment will be done at final
2776 link time. */
2777 if (output_bfd != NULL)
cedb70c5 2778 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2779 input_section, output_bfd, error_message);
2780
2781 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2782 if (TOCstart == 0)
1c865ab2 2783 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2784
2785 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2786 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2787 return bfd_reloc_ok;
2788}
2789
2790static bfd_reloc_status_type
4ce794b7
AM
2791ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2792 void *data, asection *input_section,
2793 bfd *output_bfd, char **error_message)
805fc799
AM
2794{
2795 /* If this is a relocatable link (output_bfd test tells us), just
2796 call the generic function. Any adjustment will be done at final
2797 link time. */
2798 if (output_bfd != NULL)
cedb70c5 2799 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2800 input_section, output_bfd, error_message);
2801
2802 if (error_message != NULL)
2803 {
2804 static char buf[60];
2805 sprintf (buf, "generic linker can't handle %s",
2806 reloc_entry->howto->name);
2807 *error_message = buf;
2808 }
2809 return bfd_reloc_dangerous;
2810}
2811
927be08e
AM
2812/* Track GOT entries needed for a given symbol. We might need more
2813 than one got entry per symbol. */
2814struct got_entry
2815{
2816 struct got_entry *next;
2817
2818 /* The symbol addend that we'll be placing in the GOT. */
2819 bfd_vma addend;
2820
2821 /* Unlike other ELF targets, we use separate GOT entries for the same
2822 symbol referenced from different input files. This is to support
2823 automatic multiple TOC/GOT sections, where the TOC base can vary
2824 from one input file to another. After partitioning into TOC groups
2825 we merge entries within the group.
2826
2827 Point to the BFD owning this GOT entry. */
2828 bfd *owner;
2829
2830 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2831 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2832 unsigned char tls_type;
927be08e
AM
2833
2834 /* Non-zero if got.ent points to real entry. */
f961d9dd 2835 unsigned char is_indirect;
927be08e
AM
2836
2837 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2838 union
2839 {
2840 bfd_signed_vma refcount;
2841 bfd_vma offset;
2842 struct got_entry *ent;
2843 } got;
2844};
2845
2846/* The same for PLT. */
2847struct plt_entry
2848{
2849 struct plt_entry *next;
2850
2851 bfd_vma addend;
2852
2853 union
2854 {
2855 bfd_signed_vma refcount;
2856 bfd_vma offset;
2857 } plt;
2858};
2859
e717da7e
AM
2860struct ppc64_elf_obj_tdata
2861{
2862 struct elf_obj_tdata elf;
2863
2864 /* Shortcuts to dynamic linker sections. */
2865 asection *got;
2866 asection *relgot;
2867
b3fac117
AM
2868 /* Used during garbage collection. We attach global symbols defined
2869 on removed .opd entries to this section so that the sym is removed. */
2870 asection *deleted_section;
81688140 2871
927be08e 2872 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2873 sections means we potentially need one of these for each input bfd. */
927be08e 2874 struct got_entry tlsld_got;
8860955f 2875
729eabd5
AM
2876 union {
2877 /* A copy of relocs before they are modified for --emit-relocs. */
2878 Elf_Internal_Rela *relocs;
2879
2880 /* Section contents. */
2881 bfd_byte *contents;
2882 } opd;
d77c8a4b
AM
2883
2884 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2885 the reloc to be in the range -32768 to 32767. */
98528052
AM
2886 unsigned int has_small_toc_reloc : 1;
2887
560c8763
AM
2888 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2889 instruction not one we handle. */
2890 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2891};
2892
2893#define ppc64_elf_tdata(bfd) \
2894 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2895
2896#define ppc64_tlsld_got(bfd) \
2897 (&ppc64_elf_tdata (bfd)->tlsld_got)
2898
0c8d6e5c
AM
2899#define is_ppc64_elf(bfd) \
2900 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2901 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2902
e717da7e
AM
2903/* Override the generic function because we store some extras. */
2904
2905static bfd_boolean
2906ppc64_elf_mkobject (bfd *abfd)
2907{
0ffa91dd 2908 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2909 PPC64_ELF_DATA);
e717da7e
AM
2910}
2911
feee612b 2912/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 2913 default is 32 bit. Also select arch based on apuinfo. */
feee612b 2914
b34976b6 2915static bfd_boolean
4ce794b7 2916ppc64_elf_object_p (bfd *abfd)
feee612b 2917{
14b57c7c
AM
2918 if (!abfd->arch_info->the_default)
2919 return TRUE;
2920
2921 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
2922 {
2923 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2924
2925 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2926 {
2927 /* Relies on arch after 32 bit default being 64 bit default. */
2928 abfd->arch_info = abfd->arch_info->next;
2929 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2930 }
2931 }
14b57c7c 2932 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
2933}
2934
d37c89e5
AM
2935/* Support for core dump NOTE sections. */
2936
2937static bfd_boolean
2938ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2939{
eea6121a 2940 size_t offset, size;
d37c89e5
AM
2941
2942 if (note->descsz != 504)
2943 return FALSE;
2944
2945 /* pr_cursig */
228e534f 2946 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2947
2948 /* pr_pid */
228e534f 2949 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2950
2951 /* pr_reg */
2952 offset = 112;
eea6121a 2953 size = 384;
d37c89e5
AM
2954
2955 /* Make a ".reg/999" section. */
2956 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2957 size, note->descpos + offset);
d37c89e5
AM
2958}
2959
2960static bfd_boolean
2961ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2962{
2963 if (note->descsz != 136)
2964 return FALSE;
2965
228e534f 2966 elf_tdata (abfd)->core->pid
bc989cdc 2967 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2968 elf_tdata (abfd)->core->program
d37c89e5 2969 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2970 elf_tdata (abfd)->core->command
d37c89e5
AM
2971 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2972
2973 return TRUE;
2974}
2975
183e98be
AM
2976static char *
2977ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2978 ...)
2979{
2980 switch (note_type)
2981 {
2982 default:
2983 return NULL;
2984
2985 case NT_PRPSINFO:
2986 {
2987 char data[136];
2988 va_list ap;
2989
2990 va_start (ap, note_type);
75cd47ed 2991 memset (data, 0, sizeof (data));
183e98be
AM
2992 strncpy (data + 40, va_arg (ap, const char *), 16);
2993 strncpy (data + 56, va_arg (ap, const char *), 80);
2994 va_end (ap);
2995 return elfcore_write_note (abfd, buf, bufsiz,
2996 "CORE", note_type, data, sizeof (data));
2997 }
2998
2999 case NT_PRSTATUS:
3000 {
3001 char data[504];
3002 va_list ap;
3003 long pid;
3004 int cursig;
3005 const void *greg;
3006
3007 va_start (ap, note_type);
3008 memset (data, 0, 112);
3009 pid = va_arg (ap, long);
3010 bfd_put_32 (abfd, pid, data + 32);
3011 cursig = va_arg (ap, int);
3012 bfd_put_16 (abfd, cursig, data + 12);
3013 greg = va_arg (ap, const void *);
3014 memcpy (data + 112, greg, 384);
3015 memset (data + 496, 0, 8);
3016 va_end (ap);
3017 return elfcore_write_note (abfd, buf, bufsiz,
3018 "CORE", note_type, data, sizeof (data));
3019 }
3020 }
3021}
3022
5d35169e
AM
3023/* Add extra PPC sections. */
3024
b35d266b 3025static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 3026{
0112cd26
NC
3027 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3028 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3029 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3030 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3031 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3032 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3033 { NULL, 0, 0, 0, 0 }
5d35169e
AM
3034};
3035
7c8fe5c4
AM
3036enum _ppc64_sec_type {
3037 sec_normal = 0,
3038 sec_opd = 1,
3039 sec_toc = 2
3040};
3041
f0abc2a1
AM
3042struct _ppc64_elf_section_data
3043{
3044 struct bfd_elf_section_data elf;
411e1bfb 3045
f0abc2a1
AM
3046 union
3047 {
51aecdc5
AM
3048 /* An array with one entry for each opd function descriptor,
3049 and some spares since opd entries may be either 16 or 24 bytes. */
3050#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
3051 struct _opd_sec_data
3052 {
3053 /* Points to the function code section for local opd entries. */
3054 asection **func_sec;
3055
3056 /* After editing .opd, adjust references to opd local syms. */
3057 long *adjust;
3058 } opd;
7c8fe5c4 3059
3a71aa26
AM
3060 /* An array for toc sections, indexed by offset/8. */
3061 struct _toc_sec_data
3062 {
3063 /* Specifies the relocation symbol index used at a given toc offset. */
3064 unsigned *symndx;
3065
3066 /* And the relocation addend. */
3067 bfd_vma *add;
3068 } toc;
7c8fe5c4
AM
3069 } u;
3070
3071 enum _ppc64_sec_type sec_type:2;
411e1bfb 3072
7c8fe5c4
AM
3073 /* Flag set when small branches are detected. Used to
3074 select suitable defaults for the stub group size. */
3075 unsigned int has_14bit_branch:1;
f0abc2a1
AM
3076};
3077
3078#define ppc64_elf_section_data(sec) \
411e1bfb 3079 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
3080
3081static bfd_boolean
4ce794b7 3082ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 3083{
f592407e
AM
3084 if (!sec->used_by_bfd)
3085 {
3086 struct _ppc64_elf_section_data *sdata;
3087 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 3088
f592407e
AM
3089 sdata = bfd_zalloc (abfd, amt);
3090 if (sdata == NULL)
3091 return FALSE;
3092 sec->used_by_bfd = sdata;
3093 }
f0abc2a1
AM
3094
3095 return _bfd_elf_new_section_hook (abfd, sec);
3096}
4025353c 3097
74f0fb50 3098static struct _opd_sec_data *
4025353c
AM
3099get_opd_info (asection * sec)
3100{
3101 if (sec != NULL
3102 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3103 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3104 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3105 return NULL;
3106}
90e3cdf2
JJ
3107\f
3108/* Parameters for the qsort hook. */
90e3cdf2 3109static bfd_boolean synthetic_relocatable;
cd285db5 3110static asection *synthetic_opd;
90e3cdf2 3111
699733f6 3112/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3113
3114static int
3115compare_symbols (const void *ap, const void *bp)
3116{
3117 const asymbol *a = * (const asymbol **) ap;
3118 const asymbol *b = * (const asymbol **) bp;
3119
699733f6
AM
3120 /* Section symbols first. */
3121 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3122 return -1;
699733f6 3123 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3124 return 1;
3125
699733f6 3126 /* then .opd symbols. */
cd285db5
AM
3127 if (synthetic_opd != NULL)
3128 {
3129 if (strcmp (a->section->name, ".opd") == 0
3130 && strcmp (b->section->name, ".opd") != 0)
3131 return -1;
3132 if (strcmp (a->section->name, ".opd") != 0
3133 && strcmp (b->section->name, ".opd") == 0)
3134 return 1;
3135 }
90e3cdf2 3136
699733f6 3137 /* then other code symbols. */
90e3cdf2
JJ
3138 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3139 == (SEC_CODE | SEC_ALLOC)
3140 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3141 != (SEC_CODE | SEC_ALLOC))
3142 return -1;
3143
3144 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3145 != (SEC_CODE | SEC_ALLOC)
3146 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3147 == (SEC_CODE | SEC_ALLOC))
3148 return 1;
3149
3150 if (synthetic_relocatable)
3151 {
3152 if (a->section->id < b->section->id)
3153 return -1;
3154
3155 if (a->section->id > b->section->id)
3156 return 1;
3157 }
3158
3159 if (a->value + a->section->vma < b->value + b->section->vma)
3160 return -1;
3161
3162 if (a->value + a->section->vma > b->value + b->section->vma)
3163 return 1;
3164
4d35a0aa
AM
3165 /* For syms with the same value, prefer strong dynamic global function
3166 syms over other syms. */
3167 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3168 return -1;
3169
3170 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3171 return 1;
3172
3173 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3174 return -1;
3175
3176 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3177 return 1;
3178
3179 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3180 return -1;
3181
3182 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3183 return 1;
3184
3185 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3186 return -1;
3187
3188 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3189 return 1;
3190
aaed6f5b 3191 return a > b;
90e3cdf2
JJ
3192}
3193
699733f6 3194/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3195
699733f6 3196static asymbol *
7292b3ac 3197sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3198{
699733f6 3199 long mid;
90e3cdf2 3200
7292b3ac 3201 if (id == (unsigned) -1)
699733f6
AM
3202 {
3203 while (lo < hi)
3204 {
3205 mid = (lo + hi) >> 1;
3206 if (syms[mid]->value + syms[mid]->section->vma < value)
3207 lo = mid + 1;
3208 else if (syms[mid]->value + syms[mid]->section->vma > value)
3209 hi = mid;
3210 else
3211 return syms[mid];
3212 }
3213 }
3214 else
3215 {
3216 while (lo < hi)
3217 {
3218 mid = (lo + hi) >> 1;
3219 if (syms[mid]->section->id < id)
3220 lo = mid + 1;
3221 else if (syms[mid]->section->id > id)
3222 hi = mid;
3223 else if (syms[mid]->value < value)
3224 lo = mid + 1;
3225 else if (syms[mid]->value > value)
3226 hi = mid;
3227 else
3228 return syms[mid];
3229 }
3230 }
3231 return NULL;
90e3cdf2
JJ
3232}
3233
468392fb
AM
3234static bfd_boolean
3235section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3236{
3237 bfd_vma vma = *(bfd_vma *) ptr;
3238 return ((section->flags & SEC_ALLOC) != 0
3239 && section->vma <= vma
3240 && vma < section->vma + section->size);
3241}
3242
699733f6 3243/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3244 entry syms. Also generate @plt symbols for the glink branch table.
3245 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3246
3247static long
a7535cf3
AM
3248ppc64_elf_get_synthetic_symtab (bfd *abfd,
3249 long static_count, asymbol **static_syms,
3250 long dyn_count, asymbol **dyn_syms,
c9727e01 3251 asymbol **ret)
90e3cdf2
JJ
3252{
3253 asymbol *s;
699733f6
AM
3254 long i;
3255 long count;
90e3cdf2 3256 char *names;
a7535cf3 3257 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3258 asection *opd = NULL;
90e3cdf2 3259 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3260 asymbol **syms;
ee67d69a 3261 int abi = abiversion (abfd);
90e3cdf2
JJ
3262
3263 *ret = NULL;
3264
ee67d69a
AM
3265 if (abi < 2)
3266 {
3267 opd = bfd_get_section_by_name (abfd, ".opd");
3268 if (opd == NULL && abi == 1)
3269 return 0;
3270 }
90e3cdf2 3271
a7535cf3 3272 symcount = static_count;
c9727e01 3273 if (!relocatable)
a7535cf3 3274 symcount += dyn_count;
90e3cdf2 3275 if (symcount == 0)
c9727e01 3276 return 0;
90e3cdf2 3277
a7535cf3
AM
3278 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3279 if (syms == NULL)
7356fed5 3280 return -1;
a7535cf3
AM
3281
3282 if (!relocatable && static_count != 0 && dyn_count != 0)
3283 {
3284 /* Use both symbol tables. */
3285 memcpy (syms, static_syms, static_count * sizeof (*syms));
3286 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3287 }
3288 else if (!relocatable && static_count == 0)
3289 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3290 else
3291 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3292
90e3cdf2 3293 synthetic_relocatable = relocatable;
cd285db5 3294 synthetic_opd = opd;
595da8c5 3295 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3296
c9727e01
AM
3297 if (!relocatable && symcount > 1)
3298 {
3299 long j;
3300 /* Trim duplicate syms, since we may have merged the normal and
3301 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3302 different values, so trim any with the same value. */
c9727e01
AM
3303 for (i = 1, j = 1; i < symcount; ++i)
3304 if (syms[i - 1]->value + syms[i - 1]->section->vma
3305 != syms[i]->value + syms[i]->section->vma)
3306 syms[j++] = syms[i];
3307 symcount = j;
3308 }
3309
699733f6 3310 i = 0;
cd285db5
AM
3311 /* Note that here and in compare_symbols we can't compare opd and
3312 sym->section directly. With separate debug info files, the
3313 symbols will be extracted from the debug file while abfd passed
3314 to this function is the real binary. */
3315 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3316 ++i;
3317 codesecsym = i;
90e3cdf2 3318
699733f6
AM
3319 for (; i < symcount; ++i)
3320 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3321 != (SEC_CODE | SEC_ALLOC))
3322 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3323 break;
3324 codesecsymend = i;
90e3cdf2 3325
699733f6
AM
3326 for (; i < symcount; ++i)
3327 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3328 break;
3329 secsymend = i;
90e3cdf2 3330
cd285db5
AM
3331 if (opd != NULL)
3332 for (; i < symcount; ++i)
3333 if (strcmp (syms[i]->section->name, ".opd") != 0)
3334 break;
699733f6 3335 opdsymend = i;
90e3cdf2 3336
699733f6
AM
3337 for (; i < symcount; ++i)
3338 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3339 != (SEC_CODE | SEC_ALLOC))
3340 break;
3341 symcount = i;
3342
c9727e01 3343 count = 0;
90e3cdf2 3344
699733f6 3345 if (relocatable)
90e3cdf2 3346 {
699733f6
AM
3347 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3348 arelent *r;
3349 size_t size;
3350 long relcount;
90e3cdf2 3351
468392fb
AM
3352 if (opdsymend == secsymend)
3353 goto done;
3354
699733f6 3355 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3356 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3357 if (relcount == 0)
c9727e01 3358 goto done;
90e3cdf2 3359
7356fed5
AM
3360 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3361 {
3362 count = -1;
3363 goto done;
3364 }
3365
699733f6 3366 size = 0;
595da8c5 3367 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3368 {
3369 asymbol *sym;
90e3cdf2 3370
595da8c5 3371 while (r < opd->relocation + relcount
699733f6
AM
3372 && r->address < syms[i]->value + opd->vma)
3373 ++r;
90e3cdf2 3374
595da8c5 3375 if (r == opd->relocation + relcount)
699733f6 3376 break;
90e3cdf2 3377
699733f6
AM
3378 if (r->address != syms[i]->value + opd->vma)
3379 continue;
90e3cdf2 3380
699733f6
AM
3381 if (r->howto->type != R_PPC64_ADDR64)
3382 continue;
90e3cdf2 3383
699733f6
AM
3384 sym = *r->sym_ptr_ptr;
3385 if (!sym_exists_at (syms, opdsymend, symcount,
3386 sym->section->id, sym->value + r->addend))
3387 {
3388 ++count;
3389 size += sizeof (asymbol);
3390 size += strlen (syms[i]->name) + 2;
3391 }
3392 }
90e3cdf2 3393
c4b0b099
AM
3394 if (size == 0)
3395 goto done;
699733f6
AM
3396 s = *ret = bfd_malloc (size);
3397 if (s == NULL)
3398 {
7356fed5 3399 count = -1;
c9727e01 3400 goto done;
699733f6 3401 }
90e3cdf2 3402
699733f6 3403 names = (char *) (s + count);
90e3cdf2 3404
595da8c5 3405 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3406 {
699733f6 3407 asymbol *sym;
90e3cdf2 3408
595da8c5 3409 while (r < opd->relocation + relcount
699733f6
AM
3410 && r->address < syms[i]->value + opd->vma)
3411 ++r;
90e3cdf2 3412
595da8c5 3413 if (r == opd->relocation + relcount)
699733f6
AM
3414 break;
3415
3416 if (r->address != syms[i]->value + opd->vma)
3417 continue;
3418
3419 if (r->howto->type != R_PPC64_ADDR64)
3420 continue;
90e3cdf2 3421
699733f6
AM
3422 sym = *r->sym_ptr_ptr;
3423 if (!sym_exists_at (syms, opdsymend, symcount,
3424 sym->section->id, sym->value + r->addend))
3425 {
3426 size_t len;
3427
3428 *s = *syms[i];
6ba2a415 3429 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3430 s->section = sym->section;
3431 s->value = sym->value + r->addend;
3432 s->name = names;
3433 *names++ = '.';
3434 len = strlen (syms[i]->name);
3435 memcpy (names, syms[i]->name, len + 1);
3436 names += len + 1;
6f610d07
UW
3437 /* Have udata.p point back to the original symbol this
3438 synthetic symbol was derived from. */
3439 s->udata.p = syms[i];
699733f6
AM
3440 s++;
3441 }
3442 }
3443 }
3444 else
90e3cdf2 3445 {
468392fb 3446 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3447 bfd_byte *contents = NULL;
699733f6 3448 size_t size;
468392fb
AM
3449 long plt_count = 0;
3450 bfd_vma glink_vma = 0, resolv_vma = 0;
3451 asection *dynamic, *glink = NULL, *relplt = NULL;
3452 arelent *p;
90e3cdf2 3453
ee67d69a 3454 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3455 {
c4b0b099
AM
3456 free_contents_and_exit_err:
3457 count = -1;
ee67d69a 3458 free_contents_and_exit:
699733f6 3459 if (contents)
ee67d69a 3460 free (contents);
c9727e01 3461 goto done;
699733f6 3462 }
90e3cdf2 3463
699733f6
AM
3464 size = 0;
3465 for (i = secsymend; i < opdsymend; ++i)
3466 {
3467 bfd_vma ent;
90e3cdf2 3468
5ef11c02
AM
3469 /* Ignore bogus symbols. */
3470 if (syms[i]->value > opd->size - 8)
3471 continue;
3472
699733f6
AM
3473 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3474 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3475 {
3476 ++count;
3477 size += sizeof (asymbol);
3478 size += strlen (syms[i]->name) + 2;
3479 }
3480 }
90e3cdf2 3481
468392fb 3482 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3483 if (dyn_count != 0
3484 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3485 {
3486 bfd_byte *dynbuf, *extdyn, *extdynend;
3487 size_t extdynsize;
3488 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3489
3490 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3491 goto free_contents_and_exit_err;
468392fb
AM
3492
3493 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3494 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3495
3496 extdyn = dynbuf;
3497 extdynend = extdyn + dynamic->size;
3498 for (; extdyn < extdynend; extdyn += extdynsize)
3499 {
3500 Elf_Internal_Dyn dyn;
3501 (*swap_dyn_in) (abfd, extdyn, &dyn);
3502
3503 if (dyn.d_tag == DT_NULL)
3504 break;
3505
3506 if (dyn.d_tag == DT_PPC64_GLINK)
3507 {
b9e5796b
AM
3508 /* The first glink stub starts at offset 32; see
3509 comment in ppc64_elf_finish_dynamic_sections. */
3510 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
468392fb
AM
3511 /* The .glink section usually does not survive the final
3512 link; search for the section (usually .text) where the
3513 glink stubs now reside. */
3514 glink = bfd_sections_find_if (abfd, section_covers_vma,
3515 &glink_vma);
3516 break;
3517 }
3518 }
3519
3520 free (dynbuf);
3521 }
3522
3523 if (glink != NULL)
3524 {
3525 /* Determine __glink trampoline by reading the relative branch
3526 from the first glink stub. */
3527 bfd_byte buf[4];
b9e5796b
AM
3528 unsigned int off = 0;
3529
3530 while (bfd_get_section_contents (abfd, glink, buf,
3531 glink_vma + off - glink->vma, 4))
468392fb
AM
3532 {
3533 unsigned int insn = bfd_get_32 (abfd, buf);
3534 insn ^= B_DOT;
3535 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3536 {
3537 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3538 break;
3539 }
3540 off += 4;
3541 if (off > 4)
3542 break;
468392fb
AM
3543 }
3544
3545 if (resolv_vma)
3546 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3547
066ee829
AM
3548 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3549 if (relplt != NULL)
3550 {
3551 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3552 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3553 goto free_contents_and_exit_err;
68ffbac6 3554
066ee829
AM
3555 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3556 size += plt_count * sizeof (asymbol);
468392fb 3557
066ee829
AM
3558 p = relplt->relocation;
3559 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3560 {
3561 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3562 if (p->addend != 0)
3563 size += sizeof ("+0x") - 1 + 16;
3564 }
066ee829 3565 }
468392fb
AM
3566 }
3567
c4b0b099
AM
3568 if (size == 0)
3569 goto free_contents_and_exit;
699733f6
AM
3570 s = *ret = bfd_malloc (size);
3571 if (s == NULL)
c4b0b099 3572 goto free_contents_and_exit_err;
90e3cdf2 3573
468392fb 3574 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3575
699733f6 3576 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3577 {
699733f6 3578 bfd_vma ent;
90e3cdf2 3579
5ef11c02
AM
3580 if (syms[i]->value > opd->size - 8)
3581 continue;
3582
699733f6
AM
3583 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3584 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3585 {
c9727e01 3586 long lo, hi;
699733f6 3587 size_t len;
c9727e01 3588 asection *sec = abfd->sections;
90e3cdf2 3589
699733f6
AM
3590 *s = *syms[i];
3591 lo = codesecsym;
3592 hi = codesecsymend;
3593 while (lo < hi)
3594 {
c9727e01 3595 long mid = (lo + hi) >> 1;
699733f6
AM
3596 if (syms[mid]->section->vma < ent)
3597 lo = mid + 1;
3598 else if (syms[mid]->section->vma > ent)
3599 hi = mid;
3600 else
c9727e01
AM
3601 {
3602 sec = syms[mid]->section;
3603 break;
3604 }
699733f6
AM
3605 }
3606
c9727e01 3607 if (lo >= hi && lo > codesecsym)
699733f6 3608 sec = syms[lo - 1]->section;
699733f6
AM
3609
3610 for (; sec != NULL; sec = sec->next)
3611 {
3612 if (sec->vma > ent)
3613 break;
63524580
JK
3614 /* SEC_LOAD may not be set if SEC is from a separate debug
3615 info file. */
3616 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3617 break;
3618 if ((sec->flags & SEC_CODE) != 0)
3619 s->section = sec;
3620 }
6ba2a415 3621 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3622 s->value = ent - s->section->vma;
3623 s->name = names;
3624 *names++ = '.';
3625 len = strlen (syms[i]->name);
3626 memcpy (names, syms[i]->name, len + 1);
3627 names += len + 1;
6f610d07
UW
3628 /* Have udata.p point back to the original symbol this
3629 synthetic symbol was derived from. */
3630 s->udata.p = syms[i];
699733f6 3631 s++;
90e3cdf2 3632 }
90e3cdf2 3633 }
699733f6 3634 free (contents);
468392fb
AM
3635
3636 if (glink != NULL && relplt != NULL)
3637 {
3638 if (resolv_vma)
3639 {
3640 /* Add a symbol for the main glink trampoline. */
86a4952b 3641 memset (s, 0, sizeof *s);
468392fb 3642 s->the_bfd = abfd;
6ba2a415 3643 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3644 s->section = glink;
3645 s->value = resolv_vma - glink->vma;
3646 s->name = names;
3647 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3648 names += sizeof ("__glink_PLTresolve");
3649 s++;
3650 count++;
3651 }
3652
3653 /* FIXME: It would be very much nicer to put sym@plt on the
3654 stub rather than on the glink branch table entry. The
3655 objdump disassembler would then use a sensible symbol
3656 name on plt calls. The difficulty in doing so is
3657 a) finding the stubs, and,
3658 b) matching stubs against plt entries, and,
3659 c) there can be multiple stubs for a given plt entry.
3660
3661 Solving (a) could be done by code scanning, but older
3662 ppc64 binaries used different stubs to current code.
3663 (b) is the tricky one since you need to known the toc
3664 pointer for at least one function that uses a pic stub to
3665 be able to calculate the plt address referenced.
3666 (c) means gdb would need to set multiple breakpoints (or
3667 find the glink branch itself) when setting breakpoints
3668 for pending shared library loads. */
3669 p = relplt->relocation;
3670 for (i = 0; i < plt_count; i++, p++)
3671 {
3672 size_t len;
3673
3674 *s = **p->sym_ptr_ptr;
3675 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3676 we are defining a symbol, ensure one of them is set. */
3677 if ((s->flags & BSF_LOCAL) == 0)
3678 s->flags |= BSF_GLOBAL;
6ba2a415 3679 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3680 s->section = glink;
3681 s->value = glink_vma - glink->vma;
3682 s->name = names;
3683 s->udata.p = NULL;
3684 len = strlen ((*p->sym_ptr_ptr)->name);
3685 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3686 names += len;
e054468f
AM
3687 if (p->addend != 0)
3688 {
3689 memcpy (names, "+0x", sizeof ("+0x") - 1);
3690 names += sizeof ("+0x") - 1;
3691 bfd_sprintf_vma (abfd, names, p->addend);
3692 names += strlen (names);
3693 }
468392fb
AM
3694 memcpy (names, "@plt", sizeof ("@plt"));
3695 names += sizeof ("@plt");
3696 s++;
b9e5796b
AM
3697 if (abi < 2)
3698 {
3699 glink_vma += 8;
3700 if (i >= 0x8000)
3701 glink_vma += 4;
3702 }
3703 else
468392fb
AM
3704 glink_vma += 4;
3705 }
3706 count += plt_count;
3707 }
90e3cdf2
JJ
3708 }
3709
c9727e01 3710 done:
a7535cf3 3711 free (syms);
90e3cdf2
JJ
3712 return count;
3713}
5bd4f169 3714\f
65f38f15
AM
3715/* The following functions are specific to the ELF linker, while
3716 functions above are used generally. Those named ppc64_elf_* are
3717 called by the main ELF linker code. They appear in this file more
3718 or less in the order in which they are called. eg.
3719 ppc64_elf_check_relocs is called early in the link process,
3720 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3721 called.
3722
3723 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3724 functions have both a function code symbol and a function descriptor
3725 symbol. A call to foo in a relocatable object file looks like:
3726
3727 . .text
3728 . x:
3729 . bl .foo
3730 . nop
3731
3732 The function definition in another object file might be:
3733
3734 . .section .opd
3735 . foo: .quad .foo
3736 . .quad .TOC.@tocbase
3737 . .quad 0
3738 .
3739 . .text
3740 . .foo: blr
3741
3742 When the linker resolves the call during a static link, the branch
3743 unsurprisingly just goes to .foo and the .opd information is unused.
3744 If the function definition is in a shared library, things are a little
3745 different: The call goes via a plt call stub, the opd information gets
3746 copied to the plt, and the linker patches the nop.
3747
3748 . x:
3749 . bl .foo_stub
3750 . ld 2,40(1)
3751 .
3752 .
3753 . .foo_stub:
71a39c98
AM
3754 . std 2,40(1) # in practice, the call stub
3755 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3756 . addi 11,11,Lfoo@toc@l # this is the general idea
3757 . ld 12,0(11)
3758 . ld 2,8(11)
3759 . mtctr 12
3760 . ld 11,16(11)
e86ce104
AM
3761 . bctr
3762 .
3763 . .section .plt
3764 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3765
3766 The "reloc ()" notation is supposed to indicate that the linker emits
3767 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3768 copying.
3769
3770 What are the difficulties here? Well, firstly, the relocations
3771 examined by the linker in check_relocs are against the function code
3772 sym .foo, while the dynamic relocation in the plt is emitted against
3773 the function descriptor symbol, foo. Somewhere along the line, we need
3774 to carefully copy dynamic link information from one symbol to the other.
3775 Secondly, the generic part of the elf linker will make .foo a dynamic
3776 symbol as is normal for most other backends. We need foo dynamic
3777 instead, at least for an application final link. However, when
3778 creating a shared library containing foo, we need to have both symbols
3779 dynamic so that references to .foo are satisfied during the early
3780 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3781 definition from some other object, eg. a static library.
3782
3783 Update: As of August 2004, we support a new convention. Function
3784 calls may use the function descriptor symbol, ie. "bl foo". This
3785 behaves exactly as "bl .foo". */
65f38f15 3786
1d483afe 3787/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3788 selects those that must be copied when linking a shared library,
3789 even when the symbol is local. */
65f38f15 3790
1d483afe
AM
3791static int
3792must_be_dyn_reloc (struct bfd_link_info *info,
3793 enum elf_ppc64_reloc_type r_type)
3794{
3795 switch (r_type)
3796 {
3797 default:
3798 return 1;
3799
3800 case R_PPC64_REL32:
3801 case R_PPC64_REL64:
3802 case R_PPC64_REL30:
3803 return 0;
3804
3805 case R_PPC64_TPREL16:
3806 case R_PPC64_TPREL16_LO:
3807 case R_PPC64_TPREL16_HI:
3808 case R_PPC64_TPREL16_HA:
3809 case R_PPC64_TPREL16_DS:
3810 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3811 case R_PPC64_TPREL16_HIGH:
3812 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3813 case R_PPC64_TPREL16_HIGHER:
3814 case R_PPC64_TPREL16_HIGHERA:
3815 case R_PPC64_TPREL16_HIGHEST:
3816 case R_PPC64_TPREL16_HIGHESTA:
3817 case R_PPC64_TPREL64:
0e1862bb 3818 return !bfd_link_executable (info);
1d483afe
AM
3819 }
3820}
65f38f15 3821
f4656909
AM
3822/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3823 copying dynamic variables from a shared lib into an app's dynbss
3824 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3825 shared lib. With code that gcc generates, it's vital that this be
3826 enabled; In the PowerPC64 ABI, the address of a function is actually
3827 the address of a function descriptor, which resides in the .opd
3828 section. gcc uses the descriptor directly rather than going via the
3829 GOT as some other ABI's do, which means that initialized function
3830 pointers must reference the descriptor. Thus, a function pointer
3831 initialized to the address of a function in a shared library will
3832 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3833 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3834 presents a problem as a plt entry for that function is also
3835 initialized from the function descriptor symbol and the copy reloc
3836 may not be initialized first. */
a23b6845 3837#define ELIMINATE_COPY_RELOCS 1
f4656909 3838
721956f4 3839/* Section name for stubs is the associated section name plus this
29942be8
NC
3840 string. */
3841#define STUB_SUFFIX ".stub"
721956f4
AM
3842
3843/* Linker stubs.
3844 ppc_stub_long_branch:
3845 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3846 destination, but a 24 bit branch in a stub section will reach.
3847 . b dest
3848
3849 ppc_stub_plt_branch:
3850 Similar to the above, but a 24 bit branch in the stub section won't
3851 reach its destination.
71a39c98
AM
3852 . addis %r11,%r2,xxx@toc@ha
3853 . ld %r12,xxx@toc@l(%r11)
3854 . mtctr %r12
721956f4
AM
3855 . bctr
3856
3857 ppc_stub_plt_call:
2c66dc6c
AM
3858 Used to call a function in a shared library. If it so happens that
3859 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3860 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3861 . std %r2,40(%r1)
71a39c98
AM
3862 . addis %r11,%r2,xxx@toc@ha
3863 . ld %r12,xxx+0@toc@l(%r11)
3864 . mtctr %r12
3865 . ld %r2,xxx+8@toc@l(%r11)
3866 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3867 . bctr
ad8e1ba5
AM
3868
3869 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3870 code to adjust the value and save r2 to support multiple toc sections.
3871 A ppc_stub_long_branch with an r2 offset looks like:
3872 . std %r2,40(%r1)
3873 . addis %r2,%r2,off@ha
3874 . addi %r2,%r2,off@l
3875 . b dest
3876
3877 A ppc_stub_plt_branch with an r2 offset looks like:
3878 . std %r2,40(%r1)
71a39c98
AM
3879 . addis %r11,%r2,xxx@toc@ha
3880 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3881 . addis %r2,%r2,off@ha
3882 . addi %r2,%r2,off@l
71a39c98 3883 . mtctr %r12
ad8e1ba5 3884 . bctr
ac2df442
AM
3885
3886 In cases where the "addis" instruction would add zero, the "addis" is
3887 omitted and following instructions modified slightly in some cases.
721956f4
AM
3888*/
3889
3890enum ppc_stub_type {
3891 ppc_stub_none,
3892 ppc_stub_long_branch,
ad8e1ba5 3893 ppc_stub_long_branch_r2off,
721956f4 3894 ppc_stub_plt_branch,
ad8e1ba5 3895 ppc_stub_plt_branch_r2off,
794e51c0 3896 ppc_stub_plt_call,
7341d5e2 3897 ppc_stub_plt_call_r2save,
a4b6fadd
AM
3898 ppc_stub_global_entry,
3899 ppc_stub_save_res
721956f4
AM
3900};
3901
6f20ed8a
AM
3902/* Information on stub grouping. */
3903struct map_stub
3904{
3905 /* The stub section. */
3906 asection *stub_sec;
3907 /* This is the section to which stubs in the group will be attached. */
3908 asection *link_sec;
a4b6fadd
AM
3909 /* Next group. */
3910 struct map_stub *next;
3911 /* Whether to emit a copy of register save/restore functions in this
3912 group. */
3913 int needs_save_res;
6f20ed8a
AM
3914};
3915
721956f4
AM
3916struct ppc_stub_hash_entry {
3917
3918 /* Base hash table entry structure. */
3919 struct bfd_hash_entry root;
3920
ad8e1ba5
AM
3921 enum ppc_stub_type stub_type;
3922
6f20ed8a
AM
3923 /* Group information. */
3924 struct map_stub *group;
721956f4
AM
3925
3926 /* Offset within stub_sec of the beginning of this stub. */
3927 bfd_vma stub_offset;
3928
3929 /* Given the symbol's value and its section we can determine its final
3930 value when building the stubs (so the stub knows where to jump. */
3931 bfd_vma target_value;
3932 asection *target_section;
3933
721956f4
AM
3934 /* The symbol table entry, if any, that this was derived from. */
3935 struct ppc_link_hash_entry *h;
e054468f 3936 struct plt_entry *plt_ent;
721956f4 3937
6911b7dc
AM
3938 /* Symbol st_other. */
3939 unsigned char other;
721956f4
AM
3940};
3941
3942struct ppc_branch_hash_entry {
3943
3944 /* Base hash table entry structure. */
3945 struct bfd_hash_entry root;
3946
c456f082 3947 /* Offset within branch lookup table. */
721956f4
AM
3948 unsigned int offset;
3949
3950 /* Generation marker. */
3951 unsigned int iter;
3952};
65f38f15 3953
19e08130
AM
3954/* Used to track dynamic relocations for local symbols. */
3955struct ppc_dyn_relocs
3956{
3957 struct ppc_dyn_relocs *next;
3958
3959 /* The input section of the reloc. */
3960 asection *sec;
3961
3962 /* Total number of relocs copied for the input section. */
3963 unsigned int count : 31;
3964
3965 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3966 unsigned int ifunc : 1;
3967};
3968
65f38f15
AM
3969struct ppc_link_hash_entry
3970{
3971 struct elf_link_hash_entry elf;
3972
b3fac117
AM
3973 union {
3974 /* A pointer to the most recently used stub hash entry against this
3975 symbol. */
3976 struct ppc_stub_hash_entry *stub_cache;
3977
3978 /* A pointer to the next symbol starting with a '.' */
3979 struct ppc_link_hash_entry *next_dot_sym;
3980 } u;
721956f4 3981
65f38f15 3982 /* Track dynamic relocs copied for this symbol. */
6061a67d 3983 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3984
d311bc8b
AM
3985 /* Chain of aliases referring to a weakdef. */
3986 struct ppc_link_hash_entry *weakref;
3987
721956f4 3988 /* Link between function code and descriptor symbols. */
34814b9f 3989 struct ppc_link_hash_entry *oh;
721956f4 3990
e86ce104
AM
3991 /* Flag function code and descriptor symbols. */
3992 unsigned int is_func:1;
3993 unsigned int is_func_descriptor:1;
908b32fc 3994 unsigned int fake:1;
411e1bfb 3995
c5614fa4
AM
3996 /* Whether global opd/toc sym has been adjusted or not.
3997 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3998 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3999 unsigned int adjust_done:1;
4000
a4b6fadd
AM
4001 /* Set if this is an out-of-line register save/restore function,
4002 with non-standard calling convention. */
4003 unsigned int save_res:1;
4004
411e1bfb 4005 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
4006 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
4007 corresponding relocs are encountered during check_relocs.
4008 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
4009 indicate the corresponding GOT entry type is not needed.
4010 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
4011 a TPREL one. We use a separate flag rather than setting TPREL
4012 just for convenience in distinguishing the two cases. */
4013#define TLS_GD 1 /* GD reloc. */
4014#define TLS_LD 2 /* LD reloc. */
4015#define TLS_TPREL 4 /* TPREL reloc, => IE. */
4016#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4017#define TLS_TLS 16 /* Any TLS reloc. */
4018#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4019#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 4020#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 4021 unsigned char tls_mask;
65f38f15
AM
4022};
4023
4024/* ppc64 ELF linker hash table. */
4025
4026struct ppc_link_hash_table
4027{
4028 struct elf_link_hash_table elf;
4029
721956f4
AM
4030 /* The stub hash table. */
4031 struct bfd_hash_table stub_hash_table;
4032
4033 /* Another hash table for plt_branch stubs. */
4034 struct bfd_hash_table branch_hash_table;
4035
3b421ab3
AM
4036 /* Hash table for function prologue tocsave. */
4037 htab_t tocsave_htab;
4038
e7d1c40c
AM
4039 /* Various options and other info passed from the linker. */
4040 struct ppc64_elf_params *params;
721956f4 4041
6f20ed8a
AM
4042 /* The size of sec_info below. */
4043 unsigned int sec_info_arr_size;
4044
4045 /* Per-section array of extra section info. Done this way rather
4046 than as part of ppc64_elf_section_data so we have the info for
4047 non-ppc64 sections. */
4048 struct
4049 {
4050 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 4051 bfd_vma toc_off;
6f20ed8a
AM
4052
4053 union
4054 {
4055 /* The section group that this section belongs to. */
4056 struct map_stub *group;
4057 /* A temp section list pointer. */
4058 asection *list;
4059 } u;
4060 } *sec_info;
721956f4 4061
a4b6fadd
AM
4062 /* Linked list of groups. */
4063 struct map_stub *group;
4064
ad8e1ba5
AM
4065 /* Temp used when calculating TOC pointers. */
4066 bfd_vma toc_curr;
bf102f86
AM
4067 bfd *toc_bfd;
4068 asection *toc_first_sec;
ad8e1ba5 4069
b3fac117
AM
4070 /* Used when adding symbols. */
4071 struct ppc_link_hash_entry *dot_syms;
4072
33e44f2e 4073 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 4074 asection *glink;
82bd7b59 4075 asection *sfpr;
4ce794b7
AM
4076 asection *brlt;
4077 asection *relbrlt;
58d180e8 4078 asection *glink_eh_frame;
ec338859 4079
8387904d
AM
4080 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4081 struct ppc_link_hash_entry *tls_get_addr;
4082 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4083
927be08e
AM
4084 /* The size of reliplt used by got entry relocs. */
4085 bfd_size_type got_reli_size;
4086
9b5ecbd0 4087 /* Statistics. */
7341d5e2 4088 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4089
ee75fd95
AM
4090 /* Number of stubs against global syms. */
4091 unsigned long stub_globals;
4092
ee67d69a
AM
4093 /* Set if we're linking code with function descriptors. */
4094 unsigned int opd_abi:1;
4095
4c52953f 4096 /* Support for multiple toc sections. */
33c0ec9d 4097 unsigned int do_multi_toc:1;
4c52953f 4098 unsigned int multi_toc_needed:1;
927be08e 4099 unsigned int second_toc_pass:1;
67f0cbdb 4100 unsigned int do_toc_opt:1;
4c52953f 4101
5d1634d7 4102 /* Set on error. */
99877b66 4103 unsigned int stub_error:1;
721956f4 4104
8c5b4e52
AM
4105 /* Whether func_desc_adjust needs to be run over symbols. */
4106 unsigned int need_func_desc_adj:1;
721956f4 4107
82e66161
AM
4108 /* Whether there exist local gnu indirect function resolvers,
4109 referenced by dynamic relocations. */
4110 unsigned int local_ifunc_resolver:1;
4111 unsigned int maybe_local_ifunc_resolver:1;
4112
721956f4
AM
4113 /* Incremented every time we size stubs. */
4114 unsigned int stub_iteration;
5d1634d7 4115
87d72d41
AM
4116 /* Small local sym cache. */
4117 struct sym_cache sym_cache;
65f38f15
AM
4118};
4119
4c52953f
AM
4120/* Rename some of the generic section flags to better document how they
4121 are used here. */
b0dddeec
AM
4122
4123/* Nonzero if this section has TLS related relocations. */
4124#define has_tls_reloc sec_flg0
4125
4126/* Nonzero if this section has a call to __tls_get_addr. */
4127#define has_tls_get_addr_call sec_flg1
4128
4129/* Nonzero if this section has any toc or got relocs. */
4130#define has_toc_reloc sec_flg2
4131
4132/* Nonzero if this section has a call to another section that uses
4133 the toc or got. */
d77c8a4b 4134#define makes_toc_func_call sec_flg3
b0dddeec
AM
4135
4136/* Recursion protection when determining above flag. */
d77c8a4b 4137#define call_check_in_progress sec_flg4
70cc837d 4138#define call_check_done sec_flg5
4c52953f 4139
65f38f15
AM
4140/* Get the ppc64 ELF linker hash table from a link_info structure. */
4141
4142#define ppc_hash_table(p) \
4dfe6ac6
NC
4143 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4144 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4145
721956f4
AM
4146#define ppc_stub_hash_lookup(table, string, create, copy) \
4147 ((struct ppc_stub_hash_entry *) \
4148 bfd_hash_lookup ((table), (string), (create), (copy)))
4149
4150#define ppc_branch_hash_lookup(table, string, create, copy) \
4151 ((struct ppc_branch_hash_entry *) \
4152 bfd_hash_lookup ((table), (string), (create), (copy)))
4153
4154/* Create an entry in the stub hash table. */
4155
4156static struct bfd_hash_entry *
4ce794b7
AM
4157stub_hash_newfunc (struct bfd_hash_entry *entry,
4158 struct bfd_hash_table *table,
4159 const char *string)
721956f4
AM
4160{
4161 /* Allocate the structure if it has not already been allocated by a
4162 subclass. */
4163 if (entry == NULL)
4164 {
4165 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4166 if (entry == NULL)
4167 return entry;
4168 }
4169
4170 /* Call the allocation method of the superclass. */
4171 entry = bfd_hash_newfunc (entry, table, string);
4172 if (entry != NULL)
4173 {
4174 struct ppc_stub_hash_entry *eh;
4175
4176 /* Initialize the local fields. */
4177 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4178 eh->stub_type = ppc_stub_none;
6f20ed8a 4179 eh->group = NULL;
721956f4
AM
4180 eh->stub_offset = 0;
4181 eh->target_value = 0;
4182 eh->target_section = NULL;
721956f4 4183 eh->h = NULL;
6911b7dc 4184 eh->plt_ent = NULL;
6911b7dc 4185 eh->other = 0;
721956f4
AM
4186 }
4187
4188 return entry;
4189}
4190
4191/* Create an entry in the branch hash table. */
4192
4193static struct bfd_hash_entry *
4ce794b7
AM
4194branch_hash_newfunc (struct bfd_hash_entry *entry,
4195 struct bfd_hash_table *table,
4196 const char *string)
721956f4
AM
4197{
4198 /* Allocate the structure if it has not already been allocated by a
4199 subclass. */
4200 if (entry == NULL)
4201 {
4202 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4203 if (entry == NULL)
4204 return entry;
4205 }
4206
4207 /* Call the allocation method of the superclass. */
4208 entry = bfd_hash_newfunc (entry, table, string);
4209 if (entry != NULL)
4210 {
4211 struct ppc_branch_hash_entry *eh;
4212
4213 /* Initialize the local fields. */
4214 eh = (struct ppc_branch_hash_entry *) entry;
4215 eh->offset = 0;
4216 eh->iter = 0;
4217 }
4218
4219 return entry;
4220}
4221
65f38f15
AM
4222/* Create an entry in a ppc64 ELF linker hash table. */
4223
4224static struct bfd_hash_entry *
4ce794b7
AM
4225link_hash_newfunc (struct bfd_hash_entry *entry,
4226 struct bfd_hash_table *table,
4227 const char *string)
65f38f15
AM
4228{
4229 /* Allocate the structure if it has not already been allocated by a
4230 subclass. */
4231 if (entry == NULL)
4232 {
4233 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4234 if (entry == NULL)
4235 return entry;
4236 }
4237
4238 /* Call the allocation method of the superclass. */
4239 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4240 if (entry != NULL)
4241 {
4242 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4243
b3fac117 4244 memset (&eh->u.stub_cache, 0,
908b32fc 4245 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4246 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4247
4248 /* When making function calls, old ABI code references function entry
4249 points (dot symbols), while new ABI code references the function
4250 descriptor symbol. We need to make any combination of reference and
4251 definition work together, without breaking archive linking.
4252
4253 For a defined function "foo" and an undefined call to "bar":
4254 An old object defines "foo" and ".foo", references ".bar" (possibly
4255 "bar" too).
4256 A new object defines "foo" and references "bar".
4257
4258 A new object thus has no problem with its undefined symbols being
4259 satisfied by definitions in an old object. On the other hand, the
4260 old object won't have ".bar" satisfied by a new object.
4261
4262 Keep a list of newly added dot-symbols. */
4263
4264 if (string[0] == '.')
4265 {
4266 struct ppc_link_hash_table *htab;
4267
4268 htab = (struct ppc_link_hash_table *) table;
4269 eh->u.next_dot_sym = htab->dot_syms;
4270 htab->dot_syms = eh;
4271 }
65f38f15
AM
4272 }
4273
4274 return entry;
4275}
4276
3b421ab3
AM
4277struct tocsave_entry {
4278 asection *sec;
4279 bfd_vma offset;
4280};
4281
4282static hashval_t
4283tocsave_htab_hash (const void *p)
4284{
4285 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 4286 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
4287}
4288
4289static int
4290tocsave_htab_eq (const void *p1, const void *p2)
4291{
4292 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4293 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4294 return e1->sec == e2->sec && e1->offset == e2->offset;
4295}
4296
68faa637
AM
4297/* Destroy a ppc64 ELF linker hash table. */
4298
4299static void
d495ab0d 4300ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4301{
d495ab0d 4302 struct ppc_link_hash_table *htab;
68faa637 4303
d495ab0d 4304 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4305 if (htab->tocsave_htab)
4306 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4307 bfd_hash_table_free (&htab->branch_hash_table);
4308 bfd_hash_table_free (&htab->stub_hash_table);
4309 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4310}
4311
65f38f15
AM
4312/* Create a ppc64 ELF linker hash table. */
4313
4314static struct bfd_link_hash_table *
4ce794b7 4315ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4316{
4317 struct ppc_link_hash_table *htab;
4318 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4319
4ce794b7 4320 htab = bfd_zmalloc (amt);
65f38f15
AM
4321 if (htab == NULL)
4322 return NULL;
4323
66eb6687 4324 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4325 sizeof (struct ppc_link_hash_entry),
4326 PPC64_ELF_DATA))
65f38f15 4327 {
e2d34d7d 4328 free (htab);
65f38f15
AM
4329 return NULL;
4330 }
4331
721956f4 4332 /* Init the stub hash table too. */
66eb6687
AM
4333 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4334 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4335 {
d495ab0d 4336 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4337 return NULL;
4338 }
721956f4
AM
4339
4340 /* And the branch hash table. */
66eb6687
AM
4341 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4342 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4343 {
4344 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4345 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4346 return NULL;
4347 }
721956f4 4348
3b421ab3
AM
4349 htab->tocsave_htab = htab_try_create (1024,
4350 tocsave_htab_hash,
4351 tocsave_htab_eq,
4352 NULL);
4353 if (htab->tocsave_htab == NULL)
2915c55b 4354 {
d495ab0d 4355 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4356 return NULL;
4357 }
d495ab0d 4358 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4359
3254fd24
AM
4360 /* Initializing two fields of the union is just cosmetic. We really
4361 only care about glist, but when compiled on a 32-bit host the
4362 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4363 debugger inspection of these fields look nicer. */
a6aa5195
AM
4364 htab->elf.init_got_refcount.refcount = 0;
4365 htab->elf.init_got_refcount.glist = NULL;
4366 htab->elf.init_plt_refcount.refcount = 0;
4367 htab->elf.init_plt_refcount.glist = NULL;
4368 htab->elf.init_got_offset.offset = 0;
4369 htab->elf.init_got_offset.glist = NULL;
4370 htab->elf.init_plt_offset.offset = 0;
4371 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4372
65f38f15
AM
4373 return &htab->elf.root;
4374}
4375
bfeb4a28
AM
4376/* Create sections for linker generated code. */
4377
4378static bfd_boolean
4379create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4380{
4381 struct ppc_link_hash_table *htab;
4382 flagword flags;
4383
4384 htab = ppc_hash_table (info);
4385
bfeb4a28
AM
4386 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4387 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4388 if (htab->params->save_restore_funcs)
4389 {
4390 /* Create .sfpr for code to save and restore fp regs. */
4391 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4392 flags);
4393 if (htab->sfpr == NULL
4394 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4395 return FALSE;
4396 }
4397
4398 if (bfd_link_relocatable (info))
4399 return TRUE;
bfeb4a28
AM
4400
4401 /* Create .glink for lazy dynamic linking support. */
4402 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4403 flags);
4404 if (htab->glink == NULL
4405 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4406 return FALSE;
4407
4408 if (!info->no_ld_generated_unwind_info)
4409 {
4410 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4411 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4412 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4413 ".eh_frame",
4414 flags);
4415 if (htab->glink_eh_frame == NULL
4416 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4417 return FALSE;
4418 }
4419
4420 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4421 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4422 if (htab->elf.iplt == NULL
4423 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4424 return FALSE;
4425
4426 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4427 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4428 htab->elf.irelplt
4429 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4430 if (htab->elf.irelplt == NULL
4431 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4432 return FALSE;
4433
4434 /* Create branch lookup table for plt_branch stubs. */
4435 flags = (SEC_ALLOC | SEC_LOAD
4436 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4437 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4438 flags);
4439 if (htab->brlt == NULL
4440 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4441 return FALSE;
4442
0e1862bb 4443 if (!bfd_link_pic (info))
bfeb4a28
AM
4444 return TRUE;
4445
4446 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4447 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4448 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4449 ".rela.branch_lt",
4450 flags);
4451 if (htab->relbrlt == NULL
4452 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4453 return FALSE;
4454
4455 return TRUE;
4456}
4457
e717da7e
AM
4458/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4459
bfeb4a28 4460bfd_boolean
e7d1c40c
AM
4461ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4462 struct ppc64_elf_params *params)
e717da7e
AM
4463{
4464 struct ppc_link_hash_table *htab;
4465
e7d1c40c 4466 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4467
4468/* Always hook our dynamic sections into the first bfd, which is the
4469 linker created stub bfd. This ensures that the GOT header is at
4470 the start of the output TOC section. */
4471 htab = ppc_hash_table (info);
e7d1c40c
AM
4472 htab->elf.dynobj = params->stub_bfd;
4473 htab->params = params;
bfeb4a28 4474
bfeb4a28 4475 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4476}
4477
721956f4
AM
4478/* Build a name for an entry in the stub hash table. */
4479
4480static char *
4ce794b7
AM
4481ppc_stub_name (const asection *input_section,
4482 const asection *sym_sec,
4483 const struct ppc_link_hash_entry *h,
4484 const Elf_Internal_Rela *rel)
721956f4
AM
4485{
4486 char *stub_name;
bcaa2f82 4487 ssize_t len;
721956f4
AM
4488
4489 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4490 offsets from a sym as a branch target? In fact, we could
4491 probably assume the addend is always zero. */
4492 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4493
4494 if (h)
4495 {
4496 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4497 stub_name = bfd_malloc (len);
46de2a7c
AM
4498 if (stub_name == NULL)
4499 return stub_name;
4500
bcaa2f82
AM
4501 len = sprintf (stub_name, "%08x.%s+%x",
4502 input_section->id & 0xffffffff,
4503 h->elf.root.root.string,
4504 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4505 }
4506 else
4507 {
ad8e1ba5 4508 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4509 stub_name = bfd_malloc (len);
46de2a7c
AM
4510 if (stub_name == NULL)
4511 return stub_name;
4512
bcaa2f82
AM
4513 len = sprintf (stub_name, "%08x.%x:%x+%x",
4514 input_section->id & 0xffffffff,
4515 sym_sec->id & 0xffffffff,
4516 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4517 (int) rel->r_addend & 0xffffffff);
721956f4 4518 }
bcaa2f82 4519 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4520 stub_name[len - 2] = 0;
721956f4
AM
4521 return stub_name;
4522}
4523
4524/* Look up an entry in the stub hash. Stub entries are cached because
4525 creating the stub name takes a bit of time. */
4526
4527static struct ppc_stub_hash_entry *
4ce794b7
AM
4528ppc_get_stub_entry (const asection *input_section,
4529 const asection *sym_sec,
039b3fef 4530 struct ppc_link_hash_entry *h,
4ce794b7
AM
4531 const Elf_Internal_Rela *rel,
4532 struct ppc_link_hash_table *htab)
721956f4
AM
4533{
4534 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4535 struct map_stub *group;
721956f4
AM
4536
4537 /* If this input section is part of a group of sections sharing one
4538 stub section, then use the id of the first section in the group.
4539 Stub names need to include a section id, as there may well be
4540 more than one stub used to reach say, printf, and we need to
4541 distinguish between them. */
6f20ed8a 4542 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4543 if (group == NULL)
4544 return NULL;
721956f4 4545
b3fac117
AM
4546 if (h != NULL && h->u.stub_cache != NULL
4547 && h->u.stub_cache->h == h
6f20ed8a 4548 && h->u.stub_cache->group == group)
721956f4 4549 {
b3fac117 4550 stub_entry = h->u.stub_cache;
721956f4
AM
4551 }
4552 else
4553 {
4554 char *stub_name;
4555
6f20ed8a 4556 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4557 if (stub_name == NULL)
4558 return NULL;
4559
4560 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4561 stub_name, FALSE, FALSE);
721956f4 4562 if (h != NULL)
b3fac117 4563 h->u.stub_cache = stub_entry;
721956f4
AM
4564
4565 free (stub_name);
4566 }
4567
4568 return stub_entry;
4569}
4570
4571/* Add a new stub entry to the stub hash. Not all fields of the new
4572 stub entry are initialised. */
4573
4574static struct ppc_stub_hash_entry *
4ce794b7
AM
4575ppc_add_stub (const char *stub_name,
4576 asection *section,
25f53a85 4577 struct bfd_link_info *info)
721956f4 4578{
25f53a85 4579 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4580 struct map_stub *group;
721956f4
AM
4581 asection *link_sec;
4582 asection *stub_sec;
4583 struct ppc_stub_hash_entry *stub_entry;
4584
6f20ed8a
AM
4585 group = htab->sec_info[section->id].u.group;
4586 link_sec = group->link_sec;
4587 stub_sec = group->stub_sec;
721956f4
AM
4588 if (stub_sec == NULL)
4589 {
6f20ed8a
AM
4590 size_t namelen;
4591 bfd_size_type len;
4592 char *s_name;
721956f4 4593
6f20ed8a
AM
4594 namelen = strlen (link_sec->name);
4595 len = namelen + sizeof (STUB_SUFFIX);
4596 s_name = bfd_alloc (htab->params->stub_bfd, len);
4597 if (s_name == NULL)
4598 return NULL;
721956f4 4599
6f20ed8a
AM
4600 memcpy (s_name, link_sec->name, namelen);
4601 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4602 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4603 if (stub_sec == NULL)
4604 return NULL;
4605 group->stub_sec = stub_sec;
721956f4
AM
4606 }
4607
4608 /* Enter this entry into the linker stub hash table. */
4609 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4610 TRUE, FALSE);
721956f4
AM
4611 if (stub_entry == NULL)
4612 {
695344c0 4613 /* xgettext:c-format */
8de848d8 4614 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4615 section->owner, stub_name);
721956f4
AM
4616 return NULL;
4617 }
4618
6f20ed8a 4619 stub_entry->group = group;
721956f4 4620 stub_entry->stub_offset = 0;
721956f4
AM
4621 return stub_entry;
4622}
4623
e717da7e
AM
4624/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4625 not already done. */
65f38f15 4626
b34976b6 4627static bfd_boolean
e717da7e 4628create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4629{
e717da7e
AM
4630 asection *got, *relgot;
4631 flagword flags;
4632 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4633
0c8d6e5c 4634 if (!is_ppc64_elf (abfd))
0ffa91dd 4635 return FALSE;
4dfe6ac6
NC
4636 if (htab == NULL)
4637 return FALSE;
0ffa91dd 4638
33e44f2e
AM
4639 if (!htab->elf.sgot
4640 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4641 return FALSE;
e717da7e
AM
4642
4643 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4644 | SEC_LINKER_CREATED);
4645
c456f082 4646 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4647 if (!got
e717da7e
AM
4648 || !bfd_set_section_alignment (abfd, got, 3))
4649 return FALSE;
65f38f15 4650
c456f082
AM
4651 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4652 flags | SEC_READONLY);
e717da7e 4653 if (!relgot
e717da7e 4654 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4655 return FALSE;
e717da7e
AM
4656
4657 ppc64_elf_tdata (abfd)->got = got;
4658 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4659 return TRUE;
65f38f15 4660}
5bd4f169 4661
b31867b6
AM
4662/* Follow indirect and warning symbol links. */
4663
4664static inline struct bfd_link_hash_entry *
4665follow_link (struct bfd_link_hash_entry *h)
4666{
4667 while (h->type == bfd_link_hash_indirect
4668 || h->type == bfd_link_hash_warning)
4669 h = h->u.i.link;
4670 return h;
4671}
4672
4673static inline struct elf_link_hash_entry *
4674elf_follow_link (struct elf_link_hash_entry *h)
4675{
4676 return (struct elf_link_hash_entry *) follow_link (&h->root);
4677}
4678
4679static inline struct ppc_link_hash_entry *
4680ppc_follow_link (struct ppc_link_hash_entry *h)
4681{
4682 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4683}
4684
40d16e0b
AM
4685/* Merge PLT info on FROM with that on TO. */
4686
4687static void
4688move_plt_plist (struct ppc_link_hash_entry *from,
4689 struct ppc_link_hash_entry *to)
4690{
4691 if (from->elf.plt.plist != NULL)
4692 {
4693 if (to->elf.plt.plist != NULL)
4694 {
4695 struct plt_entry **entp;
4696 struct plt_entry *ent;
4697
4698 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4699 {
4700 struct plt_entry *dent;
4701
4702 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4703 if (dent->addend == ent->addend)
4704 {
4705 dent->plt.refcount += ent->plt.refcount;
4706 *entp = ent->next;
4707 break;
4708 }
4709 if (dent == NULL)
4710 entp = &ent->next;
4711 }
4712 *entp = to->elf.plt.plist;
4713 }
4714
4715 to->elf.plt.plist = from->elf.plt.plist;
4716 from->elf.plt.plist = NULL;
4717 }
4718}
4719
65f38f15
AM
4720/* Copy the extra info we tack onto an elf_link_hash_entry. */
4721
4722static void
fcfa13d2
AM
4723ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4724 struct elf_link_hash_entry *dir,
4725 struct elf_link_hash_entry *ind)
65f38f15
AM
4726{
4727 struct ppc_link_hash_entry *edir, *eind;
4728
4729 edir = (struct ppc_link_hash_entry *) dir;
4730 eind = (struct ppc_link_hash_entry *) ind;
4731
c79d6685
AM
4732 edir->is_func |= eind->is_func;
4733 edir->is_func_descriptor |= eind->is_func_descriptor;
4734 edir->tls_mask |= eind->tls_mask;
4735 if (eind->oh != NULL)
4736 edir->oh = ppc_follow_link (eind->oh);
4737
e81830c5
AM
4738 /* If called to transfer flags for a weakdef during processing
4739 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4740 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4741 if (!(ELIMINATE_COPY_RELOCS
4742 && eind->elf.root.type != bfd_link_hash_indirect
4743 && edir->elf.dynamic_adjusted))
4744 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4745
474436e6 4746 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4747 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4748 edir->elf.ref_regular |= eind->elf.ref_regular;
4749 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4750 edir->elf.needs_plt |= eind->elf.needs_plt;
4751 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4752
d311bc8b
AM
4753 /* If we were called to copy over info for a weak sym, don't copy
4754 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4755 in order to simplify readonly_dynrelocs and save a field in the
4756 symbol hash entry, but that means dyn_relocs can't be used in any
4757 tests about a specific symbol, or affect other symbol flags which
4758 are then tested.
4759 Chain weakdefs so we can get from the weakdef back to an alias.
4760 The list is circular so that we don't need to use u.weakdef as
4761 well as this list to look at all aliases. */
4762 if (eind->elf.root.type != bfd_link_hash_indirect)
4763 {
4764 struct ppc_link_hash_entry *cur, *add, *next;
4765
4766 add = eind;
4767 do
4768 {
4769 cur = edir->weakref;
4770 if (cur != NULL)
4771 {
4772 do
4773 {
4774 /* We can be called twice for the same symbols.
4775 Don't make multiple loops. */
4776 if (cur == add)
4777 return;
4778 cur = cur->weakref;
4779 } while (cur != edir);
4780 }
4781 next = add->weakref;
4782 if (cur != add)
4783 {
4784 add->weakref = edir->weakref != NULL ? edir->weakref : edir;
4785 edir->weakref = add;
4786 }
4787 add = next;
4788 } while (add != NULL && add != eind);
4789 return;
4790 }
4791
411e1bfb 4792 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4793 if (eind->dyn_relocs != NULL)
65f38f15 4794 {
bbd7ec4a
AM
4795 if (edir->dyn_relocs != NULL)
4796 {
6061a67d
AM
4797 struct elf_dyn_relocs **pp;
4798 struct elf_dyn_relocs *p;
bbd7ec4a 4799
fcfa13d2 4800 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4801 list. Merge any entries against the same section. */
4802 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4803 {
6061a67d 4804 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4805
4806 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4807 if (q->sec == p->sec)
4808 {
4809 q->pc_count += p->pc_count;
4810 q->count += p->count;
4811 *pp = p->next;
4812 break;
4813 }
4814 if (q == NULL)
4815 pp = &p->next;
4816 }
4817 *pp = edir->dyn_relocs;
4818 }
4819
65f38f15
AM
4820 edir->dyn_relocs = eind->dyn_relocs;
4821 eind->dyn_relocs = NULL;
4822 }
65f38f15 4823
81848ca0
AM
4824 /* Copy over got entries that we may have already seen to the
4825 symbol which just became indirect. */
411e1bfb
AM
4826 if (eind->elf.got.glist != NULL)
4827 {
4828 if (edir->elf.got.glist != NULL)
4829 {
4830 struct got_entry **entp;
4831 struct got_entry *ent;
4832
4833 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4834 {
4835 struct got_entry *dent;
4836
4837 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4838 if (dent->addend == ent->addend
e717da7e 4839 && dent->owner == ent->owner
411e1bfb
AM
4840 && dent->tls_type == ent->tls_type)
4841 {
4842 dent->got.refcount += ent->got.refcount;
4843 *entp = ent->next;
4844 break;
4845 }
4846 if (dent == NULL)
4847 entp = &ent->next;
4848 }
4849 *entp = edir->elf.got.glist;
4850 }
4851
4852 edir->elf.got.glist = eind->elf.got.glist;
4853 eind->elf.got.glist = NULL;
4854 }
4855
4856 /* And plt entries. */
40d16e0b 4857 move_plt_plist (eind, edir);
411e1bfb 4858
fcfa13d2 4859 if (eind->elf.dynindx != -1)
411e1bfb 4860 {
fcfa13d2
AM
4861 if (edir->elf.dynindx != -1)
4862 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4863 edir->elf.dynstr_index);
411e1bfb
AM
4864 edir->elf.dynindx = eind->elf.dynindx;
4865 edir->elf.dynstr_index = eind->elf.dynstr_index;
4866 eind->elf.dynindx = -1;
4867 eind->elf.dynstr_index = 0;
4868 }
411e1bfb
AM
4869}
4870
8387904d
AM
4871/* Find the function descriptor hash entry from the given function code
4872 hash entry FH. Link the entries via their OH fields. */
4873
4874static struct ppc_link_hash_entry *
b31867b6 4875lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4876{
4877 struct ppc_link_hash_entry *fdh = fh->oh;
4878
4879 if (fdh == NULL)
4880 {
4881 const char *fd_name = fh->elf.root.root.string + 1;
4882
4883 fdh = (struct ppc_link_hash_entry *)
4884 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4885 if (fdh == NULL)
4886 return fdh;
4887
4888 fdh->is_func_descriptor = 1;
4889 fdh->oh = fh;
4890 fh->is_func = 1;
4891 fh->oh = fdh;
8387904d
AM
4892 }
4893
8c5b4e52
AM
4894 fdh = ppc_follow_link (fdh);
4895 fdh->is_func_descriptor = 1;
4896 fdh->oh = fh;
4897 return fdh;
8387904d
AM
4898}
4899
8c5b4e52 4900/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4901
4902static struct ppc_link_hash_entry *
4903make_fdh (struct bfd_link_info *info,
908b32fc 4904 struct ppc_link_hash_entry *fh)
bb700d78 4905{
8c5b4e52
AM
4906 bfd *abfd = fh->elf.root.u.undef.abfd;
4907 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4908 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4909 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4910 ? BSF_WEAK
4911 : BSF_GLOBAL);
4912
4913 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4914 fh->elf.root.root.string + 1,
4915 flags, bfd_und_section_ptr, 0,
4916 NULL, FALSE, FALSE, &bh))
bb700d78
AM
4917 return NULL;
4918
4919 fdh = (struct ppc_link_hash_entry *) bh;
4920 fdh->elf.non_elf = 0;
908b32fc
AM
4921 fdh->fake = 1;
4922 fdh->is_func_descriptor = 1;
4923 fdh->oh = fh;
4924 fh->is_func = 1;
4925 fh->oh = fdh;
bb700d78
AM
4926 return fdh;
4927}
4928
8387904d
AM
4929/* Fix function descriptor symbols defined in .opd sections to be
4930 function type. */
555cd476
AM
4931
4932static bfd_boolean
c16153ae 4933ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4934 struct bfd_link_info *info,
555cd476 4935 Elf_Internal_Sym *isym,
6911b7dc 4936 const char **name,
555cd476
AM
4937 flagword *flags ATTRIBUTE_UNUSED,
4938 asection **sec,
b53dfeb2 4939 bfd_vma *value)
555cd476 4940{
a43942db 4941 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
f1885d1e
AM
4942 && (ibfd->flags & DYNAMIC) == 0
4943 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
a43942db 4944 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
f64b2e8d 4945
b53dfeb2 4946 if (*sec != NULL
f1885d1e 4947 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4948 {
4949 asection *code_sec;
4950
4951 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4952 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4953 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4954
4955 /* If the symbol is a function defined in .opd, and the function
4956 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4957 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4958 && (*sec)->reloc_count != 0
4959 && opd_entry_value (*sec, *value, &code_sec, NULL,
4960 FALSE) != (bfd_vma) -1
4961 && discarded_section (code_sec))
4962 {
4963 *sec = bfd_und_section_ptr;
4964 isym->st_shndx = SHN_UNDEF;
4965 }
4966 }
dbd1e97e
AM
4967 else if (*sec != NULL
4968 && strcmp ((*sec)->name, ".toc") == 0
4969 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4970 {
4971 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4972 if (htab != NULL)
4973 htab->params->object_in_toc = 1;
4974 }
433817dd 4975
6911b7dc
AM
4976 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4977 {
4978 if (abiversion (ibfd) == 0)
4979 set_abiversion (ibfd, 2);
4980 else if (abiversion (ibfd) == 1)
4981 {
4982 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4983 " for ABI version 1\n"), name);
4984 bfd_set_error (bfd_error_bad_value);
4985 return FALSE;
4986 }
4987 }
4988
555cd476
AM
4989 return TRUE;
4990}
4991
6911b7dc
AM
4992/* Merge non-visibility st_other attributes: local entry point. */
4993
4994static void
4995ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4996 const Elf_Internal_Sym *isym,
4997 bfd_boolean definition,
4998 bfd_boolean dynamic)
4999{
5000 if (definition && !dynamic)
5001 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5002 | ELF_ST_VISIBILITY (h->other));
5003}
5004
8c5b4e52
AM
5005/* Hook called on merging a symbol. We use this to clear "fake" since
5006 we now have a real symbol. */
5007
5008static bfd_boolean
5009ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5010 const Elf_Internal_Sym *isym ATTRIBUTE_UNUSED,
5011 asection **psec ATTRIBUTE_UNUSED,
5012 bfd_boolean newdef ATTRIBUTE_UNUSED,
5013 bfd_boolean olddef ATTRIBUTE_UNUSED,
5014 bfd *oldbfd ATTRIBUTE_UNUSED,
5015 const asection *oldsec ATTRIBUTE_UNUSED)
5016{
5017 ((struct ppc_link_hash_entry *) h)->fake = 0;
5018 return TRUE;
5019}
5020
8387904d 5021/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5022 inclusion of a new ABI object archive that defines "bar".
5023 NAME is a symbol defined in an archive. Return a symbol in the hash
5024 table that might be satisfied by the archive symbols. */
8387904d
AM
5025
5026static struct elf_link_hash_entry *
5027ppc64_elf_archive_symbol_lookup (bfd *abfd,
5028 struct bfd_link_info *info,
5029 const char *name)
5030{
5031 struct elf_link_hash_entry *h;
5032 char *dot_name;
5033 size_t len;
5034
5035 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5036 if (h != NULL
5037 /* Don't return this sym if it is a fake function descriptor
5038 created by add_symbol_adjust. */
8c5b4e52 5039 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
5040 return h;
5041
5042 if (name[0] == '.')
5043 return h;
5044
5045 len = strlen (name);
5046 dot_name = bfd_alloc (abfd, len + 2);
5047 if (dot_name == NULL)
5048 return (struct elf_link_hash_entry *) 0 - 1;
5049 dot_name[0] = '.';
5050 memcpy (dot_name + 1, name, len + 1);
5051 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5052 bfd_release (abfd, dot_name);
5053 return h;
5054}
5055
5056/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5057 new ABI object defines "bar". Well, at least, undefined dot symbols
5058 are made weak. This stops later archive searches from including an
5059 object if we already have a function descriptor definition. It also
35b0ce59
AM
5060 prevents the linker complaining about undefined symbols.
5061 We also check and correct mismatched symbol visibility here. The
5062 most restrictive visibility of the function descriptor and the
5063 function entry symbol is used. */
8387904d
AM
5064
5065static bfd_boolean
b3fac117 5066add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5067{
8387904d
AM
5068 struct ppc_link_hash_table *htab;
5069 struct ppc_link_hash_entry *fdh;
5070
b3fac117
AM
5071 if (eh->elf.root.type == bfd_link_hash_warning)
5072 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5073
8400d40d
AM
5074 if (eh->elf.root.type == bfd_link_hash_indirect)
5075 return TRUE;
5076
b3fac117
AM
5077 if (eh->elf.root.root.string[0] != '.')
5078 abort ();
8387904d 5079
b3fac117 5080 htab = ppc_hash_table (info);
4dfe6ac6
NC
5081 if (htab == NULL)
5082 return FALSE;
5083
b31867b6 5084 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
5085 if (fdh == NULL
5086 && !bfd_link_relocatable (info)
5087 && (eh->elf.root.type == bfd_link_hash_undefined
5088 || eh->elf.root.type == bfd_link_hash_undefweak)
5089 && eh->elf.ref_regular)
5090 {
5091 /* Make an undefined function descriptor sym, in order to
5092 pull in an --as-needed shared lib. Archives are handled
5093 elsewhere. */
5094 fdh = make_fdh (info, eh);
5095 if (fdh == NULL)
5096 return FALSE;
bb700d78 5097 }
8c5b4e52
AM
5098
5099 if (fdh != NULL)
8387904d 5100 {
35b0ce59
AM
5101 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5102 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
5103
5104 /* Make both descriptor and entry symbol have the most
5105 constraining visibility of either symbol. */
35b0ce59
AM
5106 if (entry_vis < descr_vis)
5107 fdh->elf.other += entry_vis - descr_vis;
5108 else if (entry_vis > descr_vis)
5109 eh->elf.other += descr_vis - entry_vis;
5110
8c5b4e52
AM
5111 /* Propagate reference flags from entry symbol to function
5112 descriptor symbol. */
5113 fdh->elf.root.non_ir_ref |= eh->elf.root.non_ir_ref;
5114 fdh->elf.ref_regular |= eh->elf.ref_regular;
5115 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5116
5117 if (!fdh->elf.forced_local
5118 && fdh->elf.dynindx == -1
5119 && fdh->elf.versioned != versioned_hidden
5120 && (bfd_link_dll (info)
5121 || fdh->elf.def_dynamic
5122 || fdh->elf.ref_dynamic)
5123 && (eh->elf.ref_regular
5124 || eh->elf.def_regular))
5125 {
5126 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5127 return FALSE;
35b0ce59 5128 }
8387904d 5129 }
99877b66 5130
8387904d
AM
5131 return TRUE;
5132}
5133
f6c7c3e8
AM
5134/* Set up opd section info and abiversion for IBFD, and process list
5135 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5136
8387904d 5137static bfd_boolean
f6c7c3e8 5138ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5139{
99877b66 5140 struct ppc_link_hash_table *htab;
b3fac117 5141 struct ppc_link_hash_entry **p, *eh;
459609d6 5142 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5143
459609d6 5144 if (opd != NULL && opd->size != 0)
b3fac117 5145 {
459609d6
AM
5146 if (abiversion (ibfd) == 0)
5147 set_abiversion (ibfd, 1);
8a2058b5 5148 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5149 {
695344c0 5150 /* xgettext:c-format */
459609d6
AM
5151 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5152 " version %d\n"),
5153 ibfd, abiversion (ibfd));
5154 bfd_set_error (bfd_error_bad_value);
5155 return FALSE;
f6c7c3e8
AM
5156 }
5157
459609d6
AM
5158 if ((ibfd->flags & DYNAMIC) == 0
5159 && (opd->flags & SEC_RELOC) != 0
5160 && opd->reloc_count != 0
5161 && !bfd_is_abs_section (opd->output_section))
b3fac117 5162 {
459609d6
AM
5163 /* Garbage collection needs some extra help with .opd sections.
5164 We don't want to necessarily keep everything referenced by
5165 relocs in .opd, as that would keep all functions. Instead,
5166 if we reference an .opd symbol (a function descriptor), we
5167 want to keep the function code symbol's section. This is
5168 easy for global symbols, but for local syms we need to keep
5169 information about the associated function section. */
5170 bfd_size_type amt;
5171 asection **opd_sym_map;
5172
51aecdc5 5173 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
459609d6
AM
5174 opd_sym_map = bfd_zalloc (ibfd, amt);
5175 if (opd_sym_map == NULL)
b3fac117 5176 return FALSE;
459609d6
AM
5177 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5178 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5179 ppc64_elf_section_data (opd)->sec_type = sec_opd;
b3fac117
AM
5180 }
5181 }
5182
459609d6
AM
5183 if (!is_ppc64_elf (info->output_bfd))
5184 return TRUE;
5185 htab = ppc_hash_table (info);
5186 if (htab == NULL)
5187 return FALSE;
5188
5189 /* For input files without an explicit abiversion in e_flags
5190 we should have flagged any with symbol st_other bits set
5191 as ELFv1 and above flagged those with .opd as ELFv2.
5192 Set the output abiversion if not yet set, and for any input
5193 still ambiguous, take its abiversion from the output.
5194 Differences in ABI are reported later. */
5195 if (abiversion (info->output_bfd) == 0)
5196 set_abiversion (info->output_bfd, abiversion (ibfd));
5197 else if (abiversion (ibfd) == 0)
5198 set_abiversion (ibfd, abiversion (info->output_bfd));
5199
5200 p = &htab->dot_syms;
5201 while ((eh = *p) != NULL)
5202 {
5203 *p = NULL;
5204 if (&eh->elf == htab->elf.hgot)
5205 ;
5206 else if (htab->elf.hgot == NULL
5207 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5208 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
5209 else if (abiversion (ibfd) <= 1)
5210 {
5211 htab->need_func_desc_adj = 1;
5212 if (!add_symbol_adjust (eh, info))
5213 return FALSE;
5214 }
459609d6
AM
5215 p = &eh->u.next_dot_sym;
5216 }
b3fac117 5217 return TRUE;
8387904d
AM
5218}
5219
97fed1c9
JJ
5220/* Undo hash table changes when an --as-needed input file is determined
5221 not to be needed. */
5222
5223static bfd_boolean
e5034e59
AM
5224ppc64_elf_notice_as_needed (bfd *ibfd,
5225 struct bfd_link_info *info,
5226 enum notice_asneeded_action act)
97fed1c9 5227{
e5034e59
AM
5228 if (act == notice_not_needed)
5229 {
5230 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5231
e5034e59
AM
5232 if (htab == NULL)
5233 return FALSE;
4dfe6ac6 5234
e5034e59
AM
5235 htab->dot_syms = NULL;
5236 }
5237 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5238}
5239
aa374f67
AM
5240/* If --just-symbols against a final linked binary, then assume we need
5241 toc adjusting stubs when calling functions defined there. */
5242
5243static void
5244ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5245{
5246 if ((sec->flags & SEC_CODE) != 0
5247 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5248 && is_ppc64_elf (sec->owner))
5249 {
2c3f079f
AM
5250 if (abiversion (sec->owner) >= 2
5251 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5252 sec->has_toc_reloc = 1;
5253 }
5254 _bfd_elf_link_just_syms (sec, info);
5255}
5256
e054468f 5257static struct plt_entry **
4ce794b7
AM
5258update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5259 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5260{
5261 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5262 struct plt_entry **local_plt;
f961d9dd 5263 unsigned char *local_got_tls_masks;
411e1bfb
AM
5264
5265 if (local_got_ents == NULL)
5266 {
5267 bfd_size_type size = symtab_hdr->sh_info;
5268
e054468f
AM
5269 size *= (sizeof (*local_got_ents)
5270 + sizeof (*local_plt)
5271 + sizeof (*local_got_tls_masks));
4ce794b7 5272 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5273 if (local_got_ents == NULL)
e054468f 5274 return NULL;
411e1bfb
AM
5275 elf_local_got_ents (abfd) = local_got_ents;
5276 }
5277
e054468f 5278 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5279 {
5280 struct got_entry *ent;
5281
5282 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5283 if (ent->addend == r_addend
5284 && ent->owner == abfd
5285 && ent->tls_type == tls_type)
411e1bfb
AM
5286 break;
5287 if (ent == NULL)
5288 {
5289 bfd_size_type amt = sizeof (*ent);
4ce794b7 5290 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5291 if (ent == NULL)
5292 return FALSE;
5293 ent->next = local_got_ents[r_symndx];
5294 ent->addend = r_addend;
e717da7e 5295 ent->owner = abfd;
411e1bfb 5296 ent->tls_type = tls_type;
927be08e 5297 ent->is_indirect = FALSE;
411e1bfb
AM
5298 ent->got.refcount = 0;
5299 local_got_ents[r_symndx] = ent;
5300 }
5301 ent->got.refcount += 1;
5302 }
5303
e054468f 5304 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5305 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5306 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5307
5308 return local_plt + r_symndx;
65f38f15
AM
5309}
5310
411e1bfb 5311static bfd_boolean
e054468f 5312update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5313{
411e1bfb 5314 struct plt_entry *ent;
1e2f5b6e 5315
e054468f 5316 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5317 if (ent->addend == addend)
5318 break;
5319 if (ent == NULL)
1e2f5b6e 5320 {
411e1bfb 5321 bfd_size_type amt = sizeof (*ent);
4ce794b7 5322 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5323 if (ent == NULL)
5324 return FALSE;
e054468f 5325 ent->next = *plist;
411e1bfb
AM
5326 ent->addend = addend;
5327 ent->plt.refcount = 0;
e054468f 5328 *plist = ent;
1e2f5b6e 5329 }
411e1bfb 5330 ent->plt.refcount += 1;
b34976b6 5331 return TRUE;
1e2f5b6e
AM
5332}
5333
e054468f
AM
5334static bfd_boolean
5335is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5336{
5337 return (r_type == R_PPC64_REL24
5338 || r_type == R_PPC64_REL14
5339 || r_type == R_PPC64_REL14_BRTAKEN
5340 || r_type == R_PPC64_REL14_BRNTAKEN
5341 || r_type == R_PPC64_ADDR24
5342 || r_type == R_PPC64_ADDR14
5343 || r_type == R_PPC64_ADDR14_BRTAKEN
5344 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5345}
5346
5bd4f169 5347/* Look through the relocs for a section during the first phase, and
65f38f15 5348 calculate needed space in the global offset table, procedure
5d1634d7 5349 linkage table, and dynamic reloc sections. */
5bd4f169 5350
b34976b6 5351static bfd_boolean
4ce794b7
AM
5352ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5353 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5354{
65f38f15 5355 struct ppc_link_hash_table *htab;
5bd4f169 5356 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5357 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5358 const Elf_Internal_Rela *rel;
5359 const Elf_Internal_Rela *rel_end;
5bd4f169 5360 asection *sreloc;
1e2f5b6e 5361 asection **opd_sym_map;
3a71aa26 5362 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 5363
0e1862bb 5364 if (bfd_link_relocatable (info))
b34976b6 5365 return TRUE;
5bd4f169 5366
680a3378
AM
5367 /* Don't do anything special with non-loaded, non-alloced sections.
5368 In particular, any relocs in such sections should not affect GOT
5369 and PLT reference counting (ie. we don't allow them to create GOT
5370 or PLT entries), there's no possibility or desire to optimize TLS
5371 relocs, and there's not much point in propagating relocs to shared
5372 libs that the dynamic linker won't relocate. */
5373 if ((sec->flags & SEC_ALLOC) == 0)
5374 return TRUE;
5375
0c8d6e5c 5376 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5377
65f38f15 5378 htab = ppc_hash_table (info);
4dfe6ac6
NC
5379 if (htab == NULL)
5380 return FALSE;
5381
3a71aa26
AM
5382 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5383 FALSE, FALSE, TRUE);
5384 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5385 FALSE, FALSE, TRUE);
0ffa91dd 5386 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5387 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5388 sreloc = NULL;
1e2f5b6e 5389 opd_sym_map = NULL;
f6c7c3e8
AM
5390 if (ppc64_elf_section_data (sec) != NULL
5391 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5392 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5bd4f169
AM
5393
5394 rel_end = relocs + sec->reloc_count;
5395 for (rel = relocs; rel < rel_end; rel++)
5396 {
5397 unsigned long r_symndx;
5398 struct elf_link_hash_entry *h;
04c9666a 5399 enum elf_ppc64_reloc_type r_type;
727fc41e 5400 int tls_type;
7c8fe5c4 5401 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5402 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5403
5404 r_symndx = ELF64_R_SYM (rel->r_info);
5405 if (r_symndx < symtab_hdr->sh_info)
5406 h = NULL;
5407 else
973a3492 5408 {
8c5b4e52
AM
5409 struct ppc_link_hash_entry *eh;
5410
973a3492 5411 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5412 h = elf_follow_link (h);
8c5b4e52 5413 eh = (struct ppc_link_hash_entry *) h;
81fbe831
AM
5414
5415 /* PR15323, ref flags aren't set for references in the same
5416 object. */
5417 h->root.non_ir_ref = 1;
8c5b4e52
AM
5418 if (eh->is_func && eh->oh != NULL)
5419 eh->oh->elf.root.non_ir_ref = 1;
1c865ab2
AM
5420
5421 if (h == htab->elf.hgot)
5422 sec->has_toc_reloc = 1;
973a3492 5423 }
5bd4f169 5424
727fc41e 5425 tls_type = 0;
e054468f 5426 ifunc = NULL;
25f23106
AM
5427 if (h != NULL)
5428 {
5429 if (h->type == STT_GNU_IFUNC)
5430 {
5431 h->needs_plt = 1;
5432 ifunc = &h->plt.plist;
5433 }
5434 }
5435 else
5436 {
5437 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5438 abfd, r_symndx);
5439 if (isym == NULL)
5440 return FALSE;
5441
5442 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5443 {
5444 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5445 rel->r_addend, PLT_IFUNC);
5446 if (ifunc == NULL)
5447 return FALSE;
5448 }
5449 }
727fc41e 5450
cbf95972 5451 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5452 switch (r_type)
5bd4f169 5453 {
727fc41e
AM
5454 case R_PPC64_TLSGD:
5455 case R_PPC64_TLSLD:
5456 /* These special tls relocs tie a call to __tls_get_addr with
5457 its parameter symbol. */
5458 break;
5459
411e1bfb
AM
5460 case R_PPC64_GOT_TLSLD16:
5461 case R_PPC64_GOT_TLSLD16_LO:
5462 case R_PPC64_GOT_TLSLD16_HI:
5463 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5464 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5465 goto dogottls;
5466
5467 case R_PPC64_GOT_TLSGD16:
5468 case R_PPC64_GOT_TLSGD16_LO:
5469 case R_PPC64_GOT_TLSGD16_HI:
5470 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5471 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5472 goto dogottls;
5473
5474 case R_PPC64_GOT_TPREL16_DS:
5475 case R_PPC64_GOT_TPREL16_LO_DS:
5476 case R_PPC64_GOT_TPREL16_HI:
5477 case R_PPC64_GOT_TPREL16_HA:
0e1862bb 5478 if (bfd_link_pic (info))
411e1bfb
AM
5479 info->flags |= DF_STATIC_TLS;
5480 tls_type = TLS_TLS | TLS_TPREL;
5481 goto dogottls;
5482
5483 case R_PPC64_GOT_DTPREL16_DS:
5484 case R_PPC64_GOT_DTPREL16_LO_DS:
5485 case R_PPC64_GOT_DTPREL16_HI:
5486 case R_PPC64_GOT_DTPREL16_HA:
5487 tls_type = TLS_TLS | TLS_DTPREL;
5488 dogottls:
5489 sec->has_tls_reloc = 1;
1a0670f3 5490 /* Fall through */
411e1bfb 5491
5bd4f169 5492 case R_PPC64_GOT16:
5bd4f169 5493 case R_PPC64_GOT16_DS:
65f38f15
AM
5494 case R_PPC64_GOT16_HA:
5495 case R_PPC64_GOT16_HI:
5496 case R_PPC64_GOT16_LO:
5bd4f169 5497 case R_PPC64_GOT16_LO_DS:
65f38f15 5498 /* This symbol requires a global offset table entry. */
4c52953f 5499 sec->has_toc_reloc = 1;
33c0ec9d
AM
5500 if (r_type == R_PPC64_GOT_TLSLD16
5501 || r_type == R_PPC64_GOT_TLSGD16
5502 || r_type == R_PPC64_GOT_TPREL16_DS
5503 || r_type == R_PPC64_GOT_DTPREL16_DS
5504 || r_type == R_PPC64_GOT16
5505 || r_type == R_PPC64_GOT16_DS)
5506 {
5507 htab->do_multi_toc = 1;
d77c8a4b 5508 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5509 }
5510
e717da7e
AM
5511 if (ppc64_elf_tdata (abfd)->got == NULL
5512 && !create_got_section (abfd, info))
b34976b6 5513 return FALSE;
5bd4f169
AM
5514
5515 if (h != NULL)
5516 {
411e1bfb
AM
5517 struct ppc_link_hash_entry *eh;
5518 struct got_entry *ent;
65f38f15 5519
411e1bfb
AM
5520 eh = (struct ppc_link_hash_entry *) h;
5521 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5522 if (ent->addend == rel->r_addend
e717da7e 5523 && ent->owner == abfd
411e1bfb
AM
5524 && ent->tls_type == tls_type)
5525 break;
5526 if (ent == NULL)
5bd4f169 5527 {
411e1bfb 5528 bfd_size_type amt = sizeof (*ent);
4ce794b7 5529 ent = bfd_alloc (abfd, amt);
411e1bfb 5530 if (ent == NULL)
b34976b6 5531 return FALSE;
411e1bfb
AM
5532 ent->next = eh->elf.got.glist;
5533 ent->addend = rel->r_addend;
e717da7e 5534 ent->owner = abfd;
411e1bfb 5535 ent->tls_type = tls_type;
927be08e 5536 ent->is_indirect = FALSE;
411e1bfb
AM
5537 ent->got.refcount = 0;
5538 eh->elf.got.glist = ent;
5bd4f169 5539 }
411e1bfb 5540 ent->got.refcount += 1;
e7b938ca 5541 eh->tls_mask |= tls_type;
5bd4f169 5542 }
411e1bfb
AM
5543 else
5544 /* This is a global offset table entry for a local symbol. */
5545 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5546 rel->r_addend, tls_type))
5547 return FALSE;
a345bc8d
AM
5548
5549 /* We may also need a plt entry if the symbol turns out to be
5550 an ifunc. */
0e1862bb 5551 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5552 {
5553 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5554 return FALSE;
5555 }
5bd4f169
AM
5556 break;
5557
5bd4f169 5558 case R_PPC64_PLT16_HA:
65f38f15
AM
5559 case R_PPC64_PLT16_HI:
5560 case R_PPC64_PLT16_LO:
5561 case R_PPC64_PLT32:
5562 case R_PPC64_PLT64:
cbf95972
AM
5563 /* This symbol requires a procedure linkage table entry. */
5564 plt_list = ifunc;
5565 if (h != NULL)
e054468f 5566 {
e054468f
AM
5567 h->needs_plt = 1;
5568 if (h->root.root.string[0] == '.'
5569 && h->root.root.string[1] != '\0')
5570 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972
AM
5571 plt_list = &h->plt.plist;
5572 }
5573 if (plt_list == NULL)
5574 {
5575 /* It does not make sense to have a procedure linkage
5576 table entry for a non-ifunc local symbol. */
5577 info->callbacks->einfo
695344c0 5578 /* xgettext:c-format */
174d0a74 5579 (_("%H: %s reloc against local symbol\n"),
cbf95972
AM
5580 abfd, sec, rel->r_offset,
5581 ppc64_elf_howto_table[r_type]->name);
5582 bfd_set_error (bfd_error_bad_value);
5583 return FALSE;
e054468f 5584 }
cbf95972
AM
5585 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5586 return FALSE;
5bd4f169
AM
5587 break;
5588
5589 /* The following relocations don't need to propagate the
5590 relocation if linking a shared object since they are
5591 section relative. */
5592 case R_PPC64_SECTOFF:
5593 case R_PPC64_SECTOFF_LO:
5594 case R_PPC64_SECTOFF_HI:
5595 case R_PPC64_SECTOFF_HA:
5596 case R_PPC64_SECTOFF_DS:
5597 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5598 case R_PPC64_DTPREL16:
5599 case R_PPC64_DTPREL16_LO:
5600 case R_PPC64_DTPREL16_HI:
5601 case R_PPC64_DTPREL16_HA:
5602 case R_PPC64_DTPREL16_DS:
5603 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5604 case R_PPC64_DTPREL16_HIGH:
5605 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5606 case R_PPC64_DTPREL16_HIGHER:
5607 case R_PPC64_DTPREL16_HIGHERA:
5608 case R_PPC64_DTPREL16_HIGHEST:
5609 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5610 break;
5611
ad8e1ba5 5612 /* Nor do these. */
25f23106
AM
5613 case R_PPC64_REL16:
5614 case R_PPC64_REL16_LO:
5615 case R_PPC64_REL16_HI:
5616 case R_PPC64_REL16_HA:
a680de9a 5617 case R_PPC64_REL16DX_HA:
25f23106
AM
5618 break;
5619
45965137
AM
5620 /* Not supported as a dynamic relocation. */
5621 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5622 if (bfd_link_pic (info))
45965137
AM
5623 {
5624 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5625 ppc_howto_init ();
695344c0 5626 /* xgettext:c-format */
174d0a74 5627 info->callbacks->einfo (_("%H: %s reloc unsupported "
45965137
AM
5628 "in shared libraries and PIEs.\n"),
5629 abfd, sec, rel->r_offset,
5630 ppc64_elf_howto_table[r_type]->name);
5631 bfd_set_error (bfd_error_bad_value);
5632 return FALSE;
5633 }
5634 break;
5635
ad8e1ba5 5636 case R_PPC64_TOC16:
33c0ec9d
AM
5637 case R_PPC64_TOC16_DS:
5638 htab->do_multi_toc = 1;
d77c8a4b 5639 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5640 /* Fall through. */
ad8e1ba5
AM
5641 case R_PPC64_TOC16_LO:
5642 case R_PPC64_TOC16_HI:
5643 case R_PPC64_TOC16_HA:
ad8e1ba5 5644 case R_PPC64_TOC16_LO_DS:
4c52953f 5645 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5646 break;
5647
006589cf
AM
5648 /* Marker reloc. */
5649 case R_PPC64_ENTRY:
5650 break;
5651
5bd4f169
AM
5652 /* This relocation describes the C++ object vtable hierarchy.
5653 Reconstruct it for later use during GC. */
5654 case R_PPC64_GNU_VTINHERIT:
c152c796 5655 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5656 return FALSE;
5bd4f169
AM
5657 break;
5658
5659 /* This relocation describes which C++ vtable entries are actually
5660 used. Record for later use during GC. */
5661 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5662 BFD_ASSERT (h != NULL);
5663 if (h != NULL
5664 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5665 return FALSE;
5bd4f169
AM
5666 break;
5667
721956f4
AM
5668 case R_PPC64_REL14:
5669 case R_PPC64_REL14_BRTAKEN:
5670 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5671 {
5672 asection *dest = NULL;
5673
5674 /* Heuristic: If jumping outside our section, chances are
5675 we are going to need a stub. */
5676 if (h != NULL)
5677 {
5678 /* If the sym is weak it may be overridden later, so
5679 don't assume we know where a weak sym lives. */
5680 if (h->root.type == bfd_link_hash_defined)
5681 dest = h->root.u.def.section;
5682 }
5683 else
87d72d41
AM
5684 {
5685 Elf_Internal_Sym *isym;
5686
5687 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5688 abfd, r_symndx);
5689 if (isym == NULL)
5690 return FALSE;
5691
5692 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5693 }
5694
220c76dd 5695 if (dest != sec)
7c8fe5c4 5696 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5697 }
721956f4
AM
5698 /* Fall through. */
5699
5d1634d7 5700 case R_PPC64_REL24:
cbf95972
AM
5701 plt_list = ifunc;
5702 if (h != NULL)
5d1634d7 5703 {
e054468f
AM
5704 h->needs_plt = 1;
5705 if (h->root.root.string[0] == '.'
5706 && h->root.root.string[1] != '\0')
5707 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5708
3a71aa26 5709 if (h == tga || h == dottga)
cbf95972
AM
5710 {
5711 sec->has_tls_reloc = 1;
5712 if (rel != relocs
5713 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5714 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5715 /* We have a new-style __tls_get_addr call with
5716 a marker reloc. */
5717 ;
5718 else
5719 /* Mark this section as having an old-style call. */
5720 sec->has_tls_get_addr_call = 1;
5721 }
5722 plt_list = &h->plt.plist;
411e1bfb 5723 }
cbf95972
AM
5724
5725 /* We may need a .plt entry if the function this reloc
5726 refers to is in a shared lib. */
5727 if (plt_list
5728 && !update_plt_info (abfd, plt_list, rel->r_addend))
5729 return FALSE;
411e1bfb
AM
5730 break;
5731
cbf95972
AM
5732 case R_PPC64_ADDR14:
5733 case R_PPC64_ADDR14_BRNTAKEN:
5734 case R_PPC64_ADDR14_BRTAKEN:
5735 case R_PPC64_ADDR24:
5736 goto dodyn;
5737
411e1bfb
AM
5738 case R_PPC64_TPREL64:
5739 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
0e1862bb 5740 if (bfd_link_pic (info))
411e1bfb
AM
5741 info->flags |= DF_STATIC_TLS;
5742 goto dotlstoc;
5743
5744 case R_PPC64_DTPMOD64:
5745 if (rel + 1 < rel_end
5746 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5747 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5748 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5749 else
951fd09b 5750 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5751 goto dotlstoc;
5752
5753 case R_PPC64_DTPREL64:
5754 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5755 if (rel != relocs
5756 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5757 && rel[-1].r_offset == rel->r_offset - 8)
5758 /* This is the second reloc of a dtpmod, dtprel pair.
5759 Don't mark with TLS_DTPREL. */
5760 goto dodyn;
5761
5762 dotlstoc:
5763 sec->has_tls_reloc = 1;
5764 if (h != NULL)
5765 {
5766 struct ppc_link_hash_entry *eh;
5767 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5768 eh->tls_mask |= tls_type;
411e1bfb
AM
5769 }
5770 else
5771 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5772 rel->r_addend, tls_type))
5773 return FALSE;
5774
7c8fe5c4
AM
5775 ppc64_sec = ppc64_elf_section_data (sec);
5776 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5777 {
3a71aa26
AM
5778 bfd_size_type amt;
5779
e7b938ca 5780 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5781 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5782 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5783 if (ppc64_sec->u.toc.symndx == NULL)
5784 return FALSE;
5785 amt = sec->size * sizeof (bfd_vma) / 8;
5786 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5787 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5788 return FALSE;
7c8fe5c4
AM
5789 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5790 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5791 }
5792 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5793 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5794 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5795
5796 /* Mark the second slot of a GD or LD entry.
5797 -1 to indicate GD and -2 to indicate LD. */
5798 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5799 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5800 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5801 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5802 goto dodyn;
5803
5804 case R_PPC64_TPREL16:
5805 case R_PPC64_TPREL16_LO:
5806 case R_PPC64_TPREL16_HI:
5807 case R_PPC64_TPREL16_HA:
5808 case R_PPC64_TPREL16_DS:
5809 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5810 case R_PPC64_TPREL16_HIGH:
5811 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5812 case R_PPC64_TPREL16_HIGHER:
5813 case R_PPC64_TPREL16_HIGHERA:
5814 case R_PPC64_TPREL16_HIGHEST:
5815 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 5816 if (bfd_link_pic (info))
411e1bfb 5817 {
afb93314 5818 info->flags |= DF_STATIC_TLS;
411e1bfb 5819 goto dodyn;
5d1634d7
AM
5820 }
5821 break;
5822
e86ce104 5823 case R_PPC64_ADDR64:
1e2f5b6e 5824 if (opd_sym_map != NULL
1e2f5b6e 5825 && rel + 1 < rel_end
4ce794b7 5826 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5827 {
8387904d 5828 if (h != NULL)
8c5b4e52 5829 ((struct ppc_link_hash_entry *) h)->is_func = 1;
8387904d
AM
5830 else
5831 {
5832 asection *s;
87d72d41 5833 Elf_Internal_Sym *isym;
1e2f5b6e 5834
87d72d41
AM
5835 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5836 abfd, r_symndx);
5837 if (isym == NULL)
8387904d 5838 return FALSE;
87d72d41
AM
5839
5840 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5841 if (s != NULL && s != sec)
51aecdc5 5842 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
8387904d 5843 }
1e2f5b6e 5844 }
e86ce104
AM
5845 /* Fall through. */
5846
65f38f15
AM
5847 case R_PPC64_ADDR16:
5848 case R_PPC64_ADDR16_DS:
5849 case R_PPC64_ADDR16_HA:
5850 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5851 case R_PPC64_ADDR16_HIGH:
5852 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5853 case R_PPC64_ADDR16_HIGHER:
5854 case R_PPC64_ADDR16_HIGHERA:
5855 case R_PPC64_ADDR16_HIGHEST:
5856 case R_PPC64_ADDR16_HIGHESTA:
5857 case R_PPC64_ADDR16_LO:
5858 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5859 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5860 && rel->r_addend == 0)
5861 {
5862 /* We may need a .plt entry if this reloc refers to a
5863 function in a shared lib. */
5864 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5865 return FALSE;
5866 h->pointer_equality_needed = 1;
5867 }
5868 /* Fall through. */
5869
5870 case R_PPC64_REL30:
5871 case R_PPC64_REL32:
5872 case R_PPC64_REL64:
65f38f15 5873 case R_PPC64_ADDR32:
65f38f15
AM
5874 case R_PPC64_UADDR16:
5875 case R_PPC64_UADDR32:
5876 case R_PPC64_UADDR64:
5bd4f169 5877 case R_PPC64_TOC:
0e1862bb 5878 if (h != NULL && !bfd_link_pic (info))
81848ca0 5879 /* We may need a copy reloc. */
f5385ebf 5880 h->non_got_ref = 1;
81848ca0 5881
41bd81ab 5882 /* Don't propagate .opd relocs. */
1e2f5b6e 5883 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5884 break;
e86ce104 5885
65f38f15
AM
5886 /* If we are creating a shared library, and this is a reloc
5887 against a global symbol, or a non PC relative reloc
5888 against a local symbol, then we need to copy the reloc
5889 into the shared library. However, if we are linking with
5890 -Bsymbolic, we do not need to copy a reloc against a
5891 global symbol which is defined in an object we are
5892 including in the link (i.e., DEF_REGULAR is set). At
5893 this point we have not seen all the input files, so it is
5894 possible that DEF_REGULAR is not set now but will be set
5895 later (it is never cleared). In case of a weak definition,
5896 DEF_REGULAR may be cleared later by a strong definition in
5897 a shared library. We account for that possibility below by
f4656909 5898 storing information in the dyn_relocs field of the hash
65f38f15
AM
5899 table entry. A similar situation occurs when creating
5900 shared libraries and symbol visibility changes render the
5901 symbol local.
5902
5903 If on the other hand, we are creating an executable, we
5904 may need to keep relocations for symbols satisfied by a
5905 dynamic library if we manage to avoid copy relocs for the
5906 symbol. */
411e1bfb 5907 dodyn:
0e1862bb 5908 if ((bfd_link_pic (info)
1d483afe 5909 && (must_be_dyn_reloc (info, r_type)
65f38f15 5910 || (h != NULL
198f1157 5911 && (!SYMBOLIC_BIND (info, h)
65f38f15 5912 || h->root.type == bfd_link_hash_defweak
f5385ebf 5913 || !h->def_regular))))
f4656909 5914 || (ELIMINATE_COPY_RELOCS
0e1862bb 5915 && !bfd_link_pic (info)
65f38f15
AM
5916 && h != NULL
5917 && (h->root.type == bfd_link_hash_defweak
25f23106 5918 || !h->def_regular))
0e1862bb 5919 || (!bfd_link_pic (info)
25f23106 5920 && ifunc != NULL))
5bd4f169 5921 {
65f38f15
AM
5922 /* We must copy these reloc types into the output file.
5923 Create a reloc section in dynobj and make room for
5924 this reloc. */
5bd4f169
AM
5925 if (sreloc == NULL)
5926 {
83bac4b0
NC
5927 sreloc = _bfd_elf_make_dynamic_reloc_section
5928 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5929
5bd4f169 5930 if (sreloc == NULL)
83bac4b0 5931 return FALSE;
5bd4f169
AM
5932 }
5933
65f38f15
AM
5934 /* If this is a global symbol, we count the number of
5935 relocations we need for this symbol. */
5936 if (h != NULL)
5937 {
19e08130
AM
5938 struct elf_dyn_relocs *p;
5939 struct elf_dyn_relocs **head;
5940
ec338859 5941 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5942 p = *head;
5943 if (p == NULL || p->sec != sec)
5944 {
5945 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5946 if (p == NULL)
5947 return FALSE;
5948 p->next = *head;
5949 *head = p;
5950 p->sec = sec;
5951 p->count = 0;
5952 p->pc_count = 0;
5953 }
5954 p->count += 1;
5955 if (!must_be_dyn_reloc (info, r_type))
5956 p->pc_count += 1;
65f38f15
AM
5957 }
5958 else
5959 {
ec338859
AM
5960 /* Track dynamic relocs needed for local syms too.
5961 We really need local syms available to do this
5962 easily. Oh well. */
19e08130
AM
5963 struct ppc_dyn_relocs *p;
5964 struct ppc_dyn_relocs **head;
5965 bfd_boolean is_ifunc;
ec338859 5966 asection *s;
6edfbbad 5967 void *vpp;
87d72d41 5968 Elf_Internal_Sym *isym;
6edfbbad 5969
87d72d41
AM
5970 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5971 abfd, r_symndx);
5972 if (isym == NULL)
b34976b6 5973 return FALSE;
ec338859 5974
87d72d41
AM
5975 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5976 if (s == NULL)
5977 s = sec;
5978
6edfbbad 5979 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5980 head = (struct ppc_dyn_relocs **) vpp;
5981 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5982 p = *head;
5983 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5984 p = p->next;
5985 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5986 {
5987 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5988 if (p == NULL)
5989 return FALSE;
5990 p->next = *head;
5991 *head = p;
5992 p->sec = sec;
5993 p->ifunc = is_ifunc;
5994 p->count = 0;
5995 }
5996 p->count += 1;
ec338859 5997 }
65f38f15 5998 }
5bd4f169 5999 break;
65f38f15
AM
6000
6001 default:
96e0dda4 6002 break;
5bd4f169
AM
6003 }
6004 }
6005
b34976b6 6006 return TRUE;
5bd4f169
AM
6007}
6008
ee67d69a
AM
6009/* Merge backend specific data from an object file to the output
6010 object file when linking. */
6011
6012static bfd_boolean
50e03d47 6013ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 6014{
50e03d47 6015 bfd *obfd = info->output_bfd;
ee67d69a
AM
6016 unsigned long iflags, oflags;
6017
6018 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6019 return TRUE;
6020
6021 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6022 return TRUE;
6023
50e03d47 6024 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6025 return FALSE;
6026
6027 iflags = elf_elfheader (ibfd)->e_flags;
6028 oflags = elf_elfheader (obfd)->e_flags;
6029
f6c7c3e8 6030 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6031 {
4eca0228 6032 _bfd_error_handler
695344c0 6033 /* xgettext:c-format */
ee67d69a
AM
6034 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6035 bfd_set_error (bfd_error_bad_value);
6036 return FALSE;
6037 }
f6c7c3e8 6038 else if (iflags != oflags && iflags != 0)
ee67d69a 6039 {
4eca0228 6040 _bfd_error_handler
695344c0 6041 /* xgettext:c-format */
ee67d69a
AM
6042 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6043 ibfd, iflags, oflags);
6044 bfd_set_error (bfd_error_bad_value);
6045 return FALSE;
6046 }
6047
50e03d47 6048 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
005d79fd 6049
ee67d69a 6050 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 6051 _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6052
6053 return TRUE;
6054}
6055
6056static bfd_boolean
6057ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6058{
6059 /* Print normal ELF private data. */
6060 _bfd_elf_print_private_bfd_data (abfd, ptr);
6061
6062 if (elf_elfheader (abfd)->e_flags != 0)
6063 {
6064 FILE *file = ptr;
6065
ee67d69a
AM
6066 fprintf (file, _("private flags = 0x%lx:"),
6067 elf_elfheader (abfd)->e_flags);
6068
6069 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6070 fprintf (file, _(" [abiv%ld]"),
6071 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6072 fputc ('\n', file);
6073 }
6074
6075 return TRUE;
6076}
6077
8387904d 6078/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6079 of the code entry point, and its section, which must be in the same
6080 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6081
6082static bfd_vma
6083opd_entry_value (asection *opd_sec,
6084 bfd_vma offset,
6085 asection **code_sec,
aef36ac1
AM
6086 bfd_vma *code_off,
6087 bfd_boolean in_code_sec)
8387904d
AM
6088{
6089 bfd *opd_bfd = opd_sec->owner;
8860955f 6090 Elf_Internal_Rela *relocs;
8387904d 6091 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6092 bfd_vma val;
8387904d 6093
9f296da3
AM
6094 /* No relocs implies we are linking a --just-symbols object, or looking
6095 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6096 if (opd_sec->reloc_count == 0)
6097 {
729eabd5 6098 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6099
729eabd5
AM
6100 if (contents == NULL)
6101 {
6102 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6103 return (bfd_vma) -1;
6104 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6105 }
ee1e4ede 6106
dbb3fbbb 6107 /* PR 17512: file: 64b9dfbb. */
451dfd38 6108 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6109 return (bfd_vma) -1;
6110
729eabd5 6111 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6112 if (code_sec != NULL)
6113 {
6114 asection *sec, *likely = NULL;
ee1e4ede 6115
aef36ac1 6116 if (in_code_sec)
4b85d634 6117 {
aef36ac1
AM
6118 sec = *code_sec;
6119 if (sec->vma <= val
6120 && val < sec->vma + sec->size)
6121 likely = sec;
6122 else
6123 val = -1;
6124 }
6125 else
6126 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6127 if (sec->vma <= val
6128 && (sec->flags & SEC_LOAD) != 0
6129 && (sec->flags & SEC_ALLOC) != 0)
6130 likely = sec;
6131 if (likely != NULL)
6132 {
6133 *code_sec = likely;
6134 if (code_off != NULL)
6135 *code_off = val - likely->vma;
4b85d634
AM
6136 }
6137 }
aef36ac1 6138 return val;
4b85d634
AM
6139 }
6140
0c8d6e5c 6141 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6142
729eabd5 6143 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6144 if (relocs == NULL)
6145 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6146 /* PR 17512: file: df8e1fd6. */
6147 if (relocs == NULL)
6148 return (bfd_vma) -1;
645ea6a9 6149
8387904d 6150 /* Go find the opd reloc at the sym address. */
8860955f 6151 lo = relocs;
8387904d 6152 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6153 val = (bfd_vma) -1;
8387904d
AM
6154 while (lo < hi)
6155 {
6156 look = lo + (hi - lo) / 2;
6157 if (look->r_offset < offset)
6158 lo = look + 1;
6159 else if (look->r_offset > offset)
6160 hi = look;
6161 else
6162 {
0ffa91dd
NC
6163 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6164
8387904d
AM
6165 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6166 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6167 {
6168 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6169 asection *sec = NULL;
8387904d 6170
b53dfeb2
AM
6171 if (symndx >= symtab_hdr->sh_info
6172 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6173 {
6174 struct elf_link_hash_entry **sym_hashes;
6175 struct elf_link_hash_entry *rh;
6176
6177 sym_hashes = elf_sym_hashes (opd_bfd);
6178 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6179 if (rh != NULL)
6180 {
6181 rh = elf_follow_link (rh);
bb854a36
AM
6182 if (rh->root.type != bfd_link_hash_defined
6183 && rh->root.type != bfd_link_hash_defweak)
6184 break;
6185 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6186 {
bb854a36
AM
6187 val = rh->root.u.def.value;
6188 sec = rh->root.u.def.section;
b53dfeb2
AM
6189 }
6190 }
6191 }
6192
6193 if (sec == NULL)
6194 {
6195 Elf_Internal_Sym *sym;
6196
6197 if (symndx < symtab_hdr->sh_info)
6198 {
6199 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6200 if (sym == NULL)
6201 {
6202 size_t symcnt = symtab_hdr->sh_info;
6203 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6204 symcnt, 0,
6205 NULL, NULL, NULL);
6206 if (sym == NULL)
6207 break;
6208 symtab_hdr->contents = (bfd_byte *) sym;
6209 }
6210 sym += symndx;
128205bb
AM
6211 }
6212 else
6213 {
b53dfeb2
AM
6214 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6215 1, symndx,
6216 NULL, NULL, NULL);
128205bb
AM
6217 if (sym == NULL)
6218 break;
128205bb 6219 }
b53dfeb2
AM
6220 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6221 if (sec == NULL)
6222 break;
6223 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6224 val = sym->st_value;
8387904d 6225 }
b53dfeb2 6226
8387904d
AM
6227 val += look->r_addend;
6228 if (code_off != NULL)
6229 *code_off = val;
6230 if (code_sec != NULL)
aef36ac1
AM
6231 {
6232 if (in_code_sec && *code_sec != sec)
6233 return -1;
6234 else
6235 *code_sec = sec;
6236 }
b53dfeb2 6237 if (sec->output_section != NULL)
8387904d 6238 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6239 }
6240 break;
6241 }
6242 }
645ea6a9 6243
645ea6a9 6244 return val;
8387904d
AM
6245}
6246
aef36ac1
AM
6247/* If the ELF symbol SYM might be a function in SEC, return the
6248 function size and set *CODE_OFF to the function's entry point,
6249 otherwise return zero. */
9f296da3 6250
aef36ac1
AM
6251static bfd_size_type
6252ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6253 bfd_vma *code_off)
9f296da3 6254{
aef36ac1
AM
6255 bfd_size_type size;
6256
6257 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6258 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6259 return 0;
6260
6261 size = 0;
6262 if (!(sym->flags & BSF_SYNTHETIC))
6263 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6264
6265 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6266 {
b07bca4e
AM
6267 struct _opd_sec_data *opd = get_opd_info (sym->section);
6268 bfd_vma symval = sym->value;
6269
6270 if (opd != NULL
6271 && opd->adjust != NULL
6272 && elf_section_data (sym->section)->relocs != NULL)
6273 {
6274 /* opd_entry_value will use cached relocs that have been
6275 adjusted, but with raw symbols. That means both local
6276 and global symbols need adjusting. */
6277 long adjust = opd->adjust[OPD_NDX (symval)];
6278 if (adjust == -1)
6279 return 0;
6280 symval += adjust;
6281 }
6282
6283 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6284 &sec, code_off, TRUE) == (bfd_vma) -1)
6285 return 0;
6286 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6287 symbol. This size has nothing to do with the code size of the
6288 function, which is what we're supposed to return, but the
6289 code size isn't available without looking up the dot-sym.
6290 However, doing that would be a waste of time particularly
6291 since elf_find_function will look at the dot-sym anyway.
6292 Now, elf_find_function will keep the largest size of any
6293 function sym found at the code address of interest, so return
6294 1 here to avoid it incorrectly caching a larger function size
6295 for a small function. This does mean we return the wrong
6296 size for a new-ABI function of size 24, but all that does is
6297 disable caching for such functions. */
6298 if (size == 24)
6299 size = 1;
9f296da3 6300 }
aef36ac1
AM
6301 else
6302 {
6303 if (sym->section != sec)
6304 return 0;
6305 *code_off = sym->value;
6306 }
6307 if (size == 0)
6308 size = 1;
6309 return size;
9f296da3
AM
6310}
6311
854b41e7
AM
6312/* Return true if symbol is defined in a regular object file. */
6313
6314static bfd_boolean
6315is_static_defined (struct elf_link_hash_entry *h)
6316{
6317 return ((h->root.type == bfd_link_hash_defined
6318 || h->root.type == bfd_link_hash_defweak)
6319 && h->root.u.def.section != NULL
6320 && h->root.u.def.section->output_section != NULL);
6321}
6322
b31867b6
AM
6323/* If FDH is a function descriptor symbol, return the associated code
6324 entry symbol if it is defined. Return NULL otherwise. */
6325
6326static struct ppc_link_hash_entry *
6327defined_code_entry (struct ppc_link_hash_entry *fdh)
6328{
6329 if (fdh->is_func_descriptor)
6330 {
6331 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6332 if (fh->elf.root.type == bfd_link_hash_defined
6333 || fh->elf.root.type == bfd_link_hash_defweak)
6334 return fh;
6335 }
6336 return NULL;
6337}
6338
6339/* If FH is a function code entry symbol, return the associated
6340 function descriptor symbol if it is defined. Return NULL otherwise. */
6341
6342static struct ppc_link_hash_entry *
6343defined_func_desc (struct ppc_link_hash_entry *fh)
6344{
6345 if (fh->oh != NULL
6346 && fh->oh->is_func_descriptor)
6347 {
6348 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6349 if (fdh->elf.root.type == bfd_link_hash_defined
6350 || fdh->elf.root.type == bfd_link_hash_defweak)
6351 return fdh;
6352 }
6353 return NULL;
6354}
6355
8c5b4e52
AM
6356static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6357
6358/* Garbage collect sections, after first dealing with dot-symbols. */
6359
6360static bfd_boolean
6361ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6362{
6363 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6364
6365 if (htab != NULL && htab->need_func_desc_adj)
6366 {
6367 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6368 htab->need_func_desc_adj = 0;
6369 }
6370 return bfd_elf_gc_sections (abfd, info);
6371}
6372
74f0fb50
AM
6373/* Mark all our entry sym sections, both opd and code section. */
6374
6375static void
6376ppc64_elf_gc_keep (struct bfd_link_info *info)
6377{
6378 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6379 struct bfd_sym_chain *sym;
6380
4dfe6ac6
NC
6381 if (htab == NULL)
6382 return;
6383
74f0fb50
AM
6384 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6385 {
b31867b6 6386 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6387 asection *sec;
6388
6389 eh = (struct ppc_link_hash_entry *)
b31867b6 6390 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6391 if (eh == NULL)
6392 continue;
6393 if (eh->elf.root.type != bfd_link_hash_defined
6394 && eh->elf.root.type != bfd_link_hash_defweak)
6395 continue;
6396
b31867b6
AM
6397 fh = defined_code_entry (eh);
6398 if (fh != NULL)
74f0fb50 6399 {
b31867b6 6400 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6401 sec->flags |= SEC_KEEP;
6402 }
6403 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6404 && opd_entry_value (eh->elf.root.u.def.section,
6405 eh->elf.root.u.def.value,
aef36ac1 6406 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6407 sec->flags |= SEC_KEEP;
6408
6409 sec = eh->elf.root.u.def.section;
6410 sec->flags |= SEC_KEEP;
6411 }
6412}
6413
64d03ab5
AM
6414/* Mark sections containing dynamically referenced symbols. When
6415 building shared libraries, we must assume that any visible symbol is
6416 referenced. */
6417
6418static bfd_boolean
6419ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6420{
6421 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6422 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6423 struct ppc_link_hash_entry *fdh;
b407645f 6424 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6425
64d03ab5 6426 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6427 fdh = defined_func_desc (eh);
6428 if (fdh != NULL)
6429 eh = fdh;
64d03ab5
AM
6430
6431 if ((eh->elf.root.type == bfd_link_hash_defined
6432 || eh->elf.root.type == bfd_link_hash_defweak)
6433 && (eh->elf.ref_dynamic
1c9177d9 6434 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6435 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6436 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6437 && (!bfd_link_executable (info)
e278ae05 6438 || info->gc_keep_exported
b407645f
AM
6439 || info->export_dynamic
6440 || (eh->elf.dynamic
6441 && d != NULL
6442 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
e278ae05 6443 && (eh->elf.versioned >= versioned
4c58e0d8
AM
6444 || !bfd_hide_sym_by_version (info->version_info,
6445 eh->elf.root.root.string)))))
64d03ab5
AM
6446 {
6447 asection *code_sec;
b31867b6 6448 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6449
6450 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6451
6452 /* Function descriptor syms cause the associated
6453 function code sym section to be marked. */
b31867b6
AM
6454 fh = defined_code_entry (eh);
6455 if (fh != NULL)
6456 {
6457 code_sec = fh->elf.root.u.def.section;
6458 code_sec->flags |= SEC_KEEP;
6459 }
64d03ab5
AM
6460 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6461 && opd_entry_value (eh->elf.root.u.def.section,
6462 eh->elf.root.u.def.value,
aef36ac1 6463 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6464 code_sec->flags |= SEC_KEEP;
6465 }
6466
6467 return TRUE;
6468}
6469
5bd4f169
AM
6470/* Return the section that should be marked against GC for a given
6471 relocation. */
6472
6473static asection *
4ce794b7 6474ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6475 struct bfd_link_info *info,
4ce794b7
AM
6476 Elf_Internal_Rela *rel,
6477 struct elf_link_hash_entry *h,
6478 Elf_Internal_Sym *sym)
5bd4f169 6479{
ccfa59ea
AM
6480 asection *rsec;
6481
ccfa59ea
AM
6482 /* Syms return NULL if we're marking .opd, so we avoid marking all
6483 function sections, as all functions are referenced in .opd. */
6484 rsec = NULL;
6485 if (get_opd_info (sec) != NULL)
6486 return rsec;
1e2f5b6e 6487
5bd4f169
AM
6488 if (h != NULL)
6489 {
04c9666a 6490 enum elf_ppc64_reloc_type r_type;
b31867b6 6491 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6492
4ce794b7 6493 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6494 switch (r_type)
5bd4f169
AM
6495 {
6496 case R_PPC64_GNU_VTINHERIT:
6497 case R_PPC64_GNU_VTENTRY:
6498 break;
6499
6500 default:
6501 switch (h->root.type)
6502 {
6503 case bfd_link_hash_defined:
6504 case bfd_link_hash_defweak:
ccfa59ea 6505 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6506 fdh = defined_func_desc (eh);
6507 if (fdh != NULL)
8c5b4e52
AM
6508 {
6509 /* -mcall-aixdesc code references the dot-symbol on
6510 a call reloc. Mark the function descriptor too
6511 against garbage collection. */
6512 fdh->elf.mark = 1;
6513 if (fdh->elf.u.weakdef != NULL)
6514 fdh->elf.u.weakdef->mark = 1;
6515 eh = fdh;
6516 }
1e2f5b6e
AM
6517
6518 /* Function descriptor syms cause the associated
6519 function code sym section to be marked. */
b31867b6
AM
6520 fh = defined_code_entry (eh);
6521 if (fh != NULL)
ccfa59ea
AM
6522 {
6523 /* They also mark their opd section. */
74f0fb50 6524 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6525
b31867b6 6526 rsec = fh->elf.root.u.def.section;
ccfa59ea 6527 }
8387904d
AM
6528 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6529 && opd_entry_value (eh->elf.root.u.def.section,
6530 eh->elf.root.u.def.value,
aef36ac1 6531 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6532 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6533 else
1e2f5b6e
AM
6534 rsec = h->root.u.def.section;
6535 break;
5bd4f169
AM
6536
6537 case bfd_link_hash_common:
1e2f5b6e
AM
6538 rsec = h->root.u.c.p->section;
6539 break;
5bd4f169
AM
6540
6541 default:
fb34365b 6542 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6543 }
6544 }
6545 }
6546 else
6547 {
74f0fb50 6548 struct _opd_sec_data *opd;
1e2f5b6e
AM
6549
6550 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6551 opd = get_opd_info (rsec);
6552 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6553 {
74f0fb50 6554 rsec->gc_mark = 1;
ccfa59ea 6555
51aecdc5 6556 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6557 }
5bd4f169
AM
6558 }
6559
1e2f5b6e 6560 return rsec;
5bd4f169
AM
6561}
6562
65f38f15
AM
6563/* Update the .got, .plt. and dynamic reloc reference counts for the
6564 section being removed. */
5bd4f169 6565
b34976b6 6566static bfd_boolean
4ce794b7
AM
6567ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6568 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 6569{
411e1bfb 6570 struct ppc_link_hash_table *htab;
5bd4f169
AM
6571 Elf_Internal_Shdr *symtab_hdr;
6572 struct elf_link_hash_entry **sym_hashes;
411e1bfb 6573 struct got_entry **local_got_ents;
5bd4f169 6574 const Elf_Internal_Rela *rel, *relend;
5bd4f169 6575
0e1862bb 6576 if (bfd_link_relocatable (info))
7dda2462
TG
6577 return TRUE;
6578
680a3378
AM
6579 if ((sec->flags & SEC_ALLOC) == 0)
6580 return TRUE;
6581
ec338859
AM
6582 elf_section_data (sec)->local_dynrel = NULL;
6583
411e1bfb 6584 htab = ppc_hash_table (info);
4dfe6ac6
NC
6585 if (htab == NULL)
6586 return FALSE;
6587
0ffa91dd 6588 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 6589 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 6590 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
6591
6592 relend = relocs + sec->reloc_count;
6593 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
6594 {
6595 unsigned long r_symndx;
04c9666a 6596 enum elf_ppc64_reloc_type r_type;
58ac9f71 6597 struct elf_link_hash_entry *h = NULL;
8dea77f0 6598 struct plt_entry **plt_list = NULL;
f961d9dd 6599 unsigned char tls_type = 0;
5bd4f169 6600
a33d1f77 6601 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 6602 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
6603 if (r_symndx >= symtab_hdr->sh_info)
6604 {
6605 struct ppc_link_hash_entry *eh;
6061a67d
AM
6606 struct elf_dyn_relocs **pp;
6607 struct elf_dyn_relocs *p;
58ac9f71
AM
6608
6609 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6610 h = elf_follow_link (h);
58ac9f71
AM
6611 eh = (struct ppc_link_hash_entry *) h;
6612
6613 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6614 if (p->sec == sec)
6615 {
6616 /* Everything must go for SEC. */
6617 *pp = p->next;
6618 break;
6619 }
6620 }
6621
a33d1f77
AM
6622 switch (r_type)
6623 {
411e1bfb
AM
6624 case R_PPC64_GOT_TLSLD16:
6625 case R_PPC64_GOT_TLSLD16_LO:
6626 case R_PPC64_GOT_TLSLD16_HI:
6627 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 6628 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6629 goto dogot;
6630
6631 case R_PPC64_GOT_TLSGD16:
6632 case R_PPC64_GOT_TLSGD16_LO:
6633 case R_PPC64_GOT_TLSGD16_HI:
6634 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6635 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6636 goto dogot;
6637
6638 case R_PPC64_GOT_TPREL16_DS:
6639 case R_PPC64_GOT_TPREL16_LO_DS:
6640 case R_PPC64_GOT_TPREL16_HI:
6641 case R_PPC64_GOT_TPREL16_HA:
6642 tls_type = TLS_TLS | TLS_TPREL;
6643 goto dogot;
6644
6645 case R_PPC64_GOT_DTPREL16_DS:
6646 case R_PPC64_GOT_DTPREL16_LO_DS:
6647 case R_PPC64_GOT_DTPREL16_HI:
6648 case R_PPC64_GOT_DTPREL16_HA:
6649 tls_type = TLS_TLS | TLS_DTPREL;
6650 goto dogot;
6651
a33d1f77
AM
6652 case R_PPC64_GOT16:
6653 case R_PPC64_GOT16_DS:
6654 case R_PPC64_GOT16_HA:
6655 case R_PPC64_GOT16_HI:
6656 case R_PPC64_GOT16_LO:
6657 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
6658 dogot:
6659 {
6660 struct got_entry *ent;
6661
58ac9f71
AM
6662 if (h != NULL)
6663 ent = h->got.glist;
411e1bfb
AM
6664 else
6665 ent = local_got_ents[r_symndx];
6666
6667 for (; ent != NULL; ent = ent->next)
6668 if (ent->addend == rel->r_addend
e717da7e 6669 && ent->owner == abfd
411e1bfb
AM
6670 && ent->tls_type == tls_type)
6671 break;
6672 if (ent == NULL)
6673 abort ();
6674 if (ent->got.refcount > 0)
6675 ent->got.refcount -= 1;
6676 }
8dea77f0
AM
6677 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
6678 plt_list = &h->plt.plist;
a33d1f77 6679 break;
65f38f15 6680
a33d1f77
AM
6681 case R_PPC64_PLT16_HA:
6682 case R_PPC64_PLT16_HI:
6683 case R_PPC64_PLT16_LO:
6684 case R_PPC64_PLT32:
6685 case R_PPC64_PLT64:
721956f4
AM
6686 case R_PPC64_REL14:
6687 case R_PPC64_REL14_BRNTAKEN:
6688 case R_PPC64_REL14_BRTAKEN:
5d1634d7 6689 case R_PPC64_REL24:
58ac9f71 6690 if (h != NULL)
cbf95972
AM
6691 plt_list = &h->plt.plist;
6692 else if (local_got_ents != NULL)
6693 {
6694 struct plt_entry **local_plt = (struct plt_entry **)
6695 (local_got_ents + symtab_hdr->sh_info);
6696 unsigned char *local_got_tls_masks = (unsigned char *)
6697 (local_plt + symtab_hdr->sh_info);
6698 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6699 plt_list = local_plt + r_symndx;
6700 }
8dea77f0 6701 break;
411e1bfb 6702
8dea77f0
AM
6703 case R_PPC64_ADDR64:
6704 case R_PPC64_ADDR16:
6705 case R_PPC64_ADDR16_DS:
6706 case R_PPC64_ADDR16_HA:
6707 case R_PPC64_ADDR16_HI:
6708 case R_PPC64_ADDR16_HIGH:
6709 case R_PPC64_ADDR16_HIGHA:
6710 case R_PPC64_ADDR16_HIGHER:
6711 case R_PPC64_ADDR16_HIGHERA:
6712 case R_PPC64_ADDR16_HIGHEST:
6713 case R_PPC64_ADDR16_HIGHESTA:
6714 case R_PPC64_ADDR16_LO:
6715 case R_PPC64_ADDR16_LO_DS:
6716 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
6717 && rel->r_addend == 0)
6718 plt_list = &h->plt.plist;
e86ce104 6719 break;
5d1634d7 6720
a33d1f77
AM
6721 default:
6722 break;
6723 }
8dea77f0
AM
6724 if (plt_list != NULL)
6725 {
6726 struct plt_entry *ent;
6727
6728 for (ent = *plt_list; ent != NULL; ent = ent->next)
6729 if (ent->addend == rel->r_addend)
6730 break;
6731 if (ent != NULL && ent->plt.refcount > 0)
6732 ent->plt.refcount -= 1;
6733 }
a33d1f77 6734 }
b34976b6 6735 return TRUE;
5bd4f169
AM
6736}
6737
deb0e272
AM
6738/* The maximum size of .sfpr. */
6739#define SFPR_MAX (218*4)
6740
6741struct sfpr_def_parms
6742{
699733f6
AM
6743 const char name[12];
6744 unsigned char lo, hi;
deb0e272
AM
6745 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6746 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6747};
6748
a4b6fadd
AM
6749/* Auto-generate _save*, _rest* functions in .sfpr.
6750 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6751 instead. */
deb0e272 6752
4dfe6ac6 6753static bfd_boolean
a4b6fadd
AM
6754sfpr_define (struct bfd_link_info *info,
6755 const struct sfpr_def_parms *parm,
6756 asection *stub_sec)
deb0e272
AM
6757{
6758 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6759 unsigned int i;
6760 size_t len = strlen (parm->name);
6761 bfd_boolean writing = FALSE;
699733f6 6762 char sym[16];
deb0e272 6763
4dfe6ac6
NC
6764 if (htab == NULL)
6765 return FALSE;
6766
deb0e272
AM
6767 memcpy (sym, parm->name, len);
6768 sym[len + 2] = 0;
6769
6770 for (i = parm->lo; i <= parm->hi; i++)
6771 {
a4b6fadd 6772 struct ppc_link_hash_entry *h;
deb0e272
AM
6773
6774 sym[len + 0] = i / 10 + '0';
6775 sym[len + 1] = i % 10 + '0';
a4b6fadd 6776 h = (struct ppc_link_hash_entry *)
b32547cd 6777 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6778 if (stub_sec != NULL)
deb0e272 6779 {
a4b6fadd
AM
6780 if (h != NULL
6781 && h->elf.root.type == bfd_link_hash_defined
6782 && h->elf.root.u.def.section == htab->sfpr)
6783 {
6784 struct elf_link_hash_entry *s;
6785 char buf[32];
6786 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6787 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6788 if (s == NULL)
6789 return FALSE;
6790 if (s->root.type == bfd_link_hash_new
6791 || (s->root.type = bfd_link_hash_defined
6792 && s->root.u.def.section == stub_sec))
6793 {
6794 s->root.type = bfd_link_hash_defined;
6795 s->root.u.def.section = stub_sec;
6796 s->root.u.def.value = (stub_sec->size
6797 + h->elf.root.u.def.value);
6798 s->ref_regular = 1;
6799 s->def_regular = 1;
6800 s->ref_regular_nonweak = 1;
6801 s->forced_local = 1;
6802 s->non_elf = 0;
6803 s->root.linker_def = 1;
6804 }
6805 }
6806 continue;
6807 }
6808 if (h != NULL)
6809 {
6810 h->save_res = 1;
6811 if (!h->elf.def_regular)
deb0e272 6812 {
a4b6fadd
AM
6813 h->elf.root.type = bfd_link_hash_defined;
6814 h->elf.root.u.def.section = htab->sfpr;
6815 h->elf.root.u.def.value = htab->sfpr->size;
6816 h->elf.type = STT_FUNC;
6817 h->elf.def_regular = 1;
b32547cd 6818 h->elf.non_elf = 0;
a4b6fadd
AM
6819 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6820 writing = TRUE;
deb0e272 6821 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6822 {
6823 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6824 if (htab->sfpr->contents == NULL)
6825 return FALSE;
6826 }
deb0e272
AM
6827 }
6828 }
6829 if (writing)
6830 {
6831 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6832 if (i != parm->hi)
6833 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6834 else
6835 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6836 htab->sfpr->size = p - htab->sfpr->contents;
6837 }
6838 }
6839
6840 return TRUE;
6841}
6842
6843static bfd_byte *
6844savegpr0 (bfd *abfd, bfd_byte *p, int r)
6845{
6846 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6847 return p + 4;
6848}
6849
6850static bfd_byte *
6851savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6852{
6853 p = savegpr0 (abfd, p, r);
a078d95a 6854 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6855 p = p + 4;
6856 bfd_put_32 (abfd, BLR, p);
6857 return p + 4;
6858}
6859
6860static bfd_byte *
6861restgpr0 (bfd *abfd, bfd_byte *p, int r)
6862{
6863 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6864 return p + 4;
6865}
6866
6867static bfd_byte *
6868restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6869{
a078d95a 6870 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6871 p = p + 4;
6872 p = restgpr0 (abfd, p, r);
6873 bfd_put_32 (abfd, MTLR_R0, p);
6874 p = p + 4;
6875 if (r == 29)
6876 {
6877 p = restgpr0 (abfd, p, 30);
6878 p = restgpr0 (abfd, p, 31);
6879 }
6880 bfd_put_32 (abfd, BLR, p);
6881 return p + 4;
6882}
6883
6884static bfd_byte *
6885savegpr1 (bfd *abfd, bfd_byte *p, int r)
6886{
6887 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6888 return p + 4;
6889}
6890
6891static bfd_byte *
6892savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6893{
6894 p = savegpr1 (abfd, p, r);
6895 bfd_put_32 (abfd, BLR, p);
6896 return p + 4;
6897}
6898
6899static bfd_byte *
6900restgpr1 (bfd *abfd, bfd_byte *p, int r)
6901{
6902 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6903 return p + 4;
6904}
6905
6906static bfd_byte *
6907restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6908{
6909 p = restgpr1 (abfd, p, r);
6910 bfd_put_32 (abfd, BLR, p);
6911 return p + 4;
6912}
6913
6914static bfd_byte *
6915savefpr (bfd *abfd, bfd_byte *p, int r)
6916{
6917 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6918 return p + 4;
6919}
6920
6921static bfd_byte *
6922savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6923{
6924 p = savefpr (abfd, p, r);
a078d95a 6925 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6926 p = p + 4;
6927 bfd_put_32 (abfd, BLR, p);
6928 return p + 4;
6929}
6930
6931static bfd_byte *
6932restfpr (bfd *abfd, bfd_byte *p, int r)
6933{
6934 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6935 return p + 4;
6936}
6937
6938static bfd_byte *
6939restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6940{
a078d95a 6941 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6942 p = p + 4;
6943 p = restfpr (abfd, p, r);
6944 bfd_put_32 (abfd, MTLR_R0, p);
6945 p = p + 4;
6946 if (r == 29)
6947 {
6948 p = restfpr (abfd, p, 30);
6949 p = restfpr (abfd, p, 31);
6950 }
6951 bfd_put_32 (abfd, BLR, p);
6952 return p + 4;
6953}
6954
6955static bfd_byte *
6956savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6957{
6958 p = savefpr (abfd, p, r);
6959 bfd_put_32 (abfd, BLR, p);
6960 return p + 4;
6961}
6962
6963static bfd_byte *
6964restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6965{
6966 p = restfpr (abfd, p, r);
6967 bfd_put_32 (abfd, BLR, p);
6968 return p + 4;
6969}
6970
6971static bfd_byte *
6972savevr (bfd *abfd, bfd_byte *p, int r)
6973{
6974 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6975 p = p + 4;
6976 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6977 return p + 4;
6978}
6979
6980static bfd_byte *
6981savevr_tail (bfd *abfd, bfd_byte *p, int r)
6982{
6983 p = savevr (abfd, p, r);
6984 bfd_put_32 (abfd, BLR, p);
6985 return p + 4;
6986}
6987
6988static bfd_byte *
6989restvr (bfd *abfd, bfd_byte *p, int r)
6990{
6991 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6992 p = p + 4;
6993 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6994 return p + 4;
6995}
6996
6997static bfd_byte *
6998restvr_tail (bfd *abfd, bfd_byte *p, int r)
6999{
7000 p = restvr (abfd, p, r);
7001 bfd_put_32 (abfd, BLR, p);
7002 return p + 4;
7003}
7004
e86ce104
AM
7005/* Called via elf_link_hash_traverse to transfer dynamic linking
7006 information on function code symbol entries to their corresponding
7007 function descriptor symbol entries. */
deb0e272 7008
b34976b6 7009static bfd_boolean
4ce794b7 7010func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 7011{
e86ce104 7012 struct bfd_link_info *info;
65f38f15 7013 struct ppc_link_hash_table *htab;
50bc7936
AM
7014 struct ppc_link_hash_entry *fh;
7015 struct ppc_link_hash_entry *fdh;
7016 bfd_boolean force_local;
5bd4f169 7017
50bc7936
AM
7018 fh = (struct ppc_link_hash_entry *) h;
7019 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 7020 return TRUE;
e86ce104 7021
8c5b4e52
AM
7022 if (!fh->is_func)
7023 return TRUE;
7024
7025 if (fh->elf.root.root.string[0] != '.'
7026 || fh->elf.root.root.string[1] == '\0')
7027 return TRUE;
7028
4ce794b7 7029 info = inf;
65f38f15 7030 htab = ppc_hash_table (info);
4dfe6ac6
NC
7031 if (htab == NULL)
7032 return FALSE;
5bd4f169 7033
8c5b4e52
AM
7034 /* Find the corresponding function descriptor symbol. */
7035 fdh = lookup_fdh (fh, htab);
7036
c09bdfe5
AM
7037 /* Resolve undefined references to dot-symbols as the value
7038 in the function descriptor, if we have one in a regular object.
7039 This is to satisfy cases like ".quad .foo". Calls to functions
7040 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
7041 if ((fh->elf.root.type == bfd_link_hash_undefined
7042 || fh->elf.root.type == bfd_link_hash_undefweak)
7043 && (fdh->elf.root.type == bfd_link_hash_defined
7044 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
7045 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7046 && opd_entry_value (fdh->elf.root.u.def.section,
7047 fdh->elf.root.u.def.value,
c09bdfe5 7048 &fh->elf.root.u.def.section,
aef36ac1 7049 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 7050 {
b31867b6 7051 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 7052 fh->elf.forced_local = 1;
b31867b6
AM
7053 fh->elf.def_regular = fdh->elf.def_regular;
7054 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
7055 }
7056
8c5b4e52
AM
7057 if (!fh->elf.dynamic)
7058 {
7059 struct plt_entry *ent;
5bd4f169 7060
8c5b4e52
AM
7061 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7062 if (ent->plt.refcount > 0)
7063 break;
7064 if (ent == NULL)
7065 return TRUE;
7066 }
5bd4f169 7067
8c5b4e52 7068 /* Create a descriptor as undefined if necessary. */
50bc7936 7069 if (fdh == NULL
0e1862bb 7070 && !bfd_link_executable (info)
50bc7936
AM
7071 && (fh->elf.root.type == bfd_link_hash_undefined
7072 || fh->elf.root.type == bfd_link_hash_undefweak))
7073 {
908b32fc 7074 fdh = make_fdh (info, fh);
bb700d78
AM
7075 if (fdh == NULL)
7076 return FALSE;
50bc7936 7077 }
648cca2c 7078
8c5b4e52 7079 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
7080 if (fdh != NULL
7081 && fdh->fake
8c5b4e52
AM
7082 && (fh->elf.root.type == bfd_link_hash_defined
7083 || fh->elf.root.type == bfd_link_hash_defweak))
7084 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 7085
8c5b4e52
AM
7086 /* Transfer dynamic linking information to the function descriptor. */
7087 if (fdh != NULL)
7088 {
f5385ebf
AM
7089 fdh->elf.ref_regular |= fh->elf.ref_regular;
7090 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7091 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7092 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
7093 fdh->elf.dynamic |= fh->elf.dynamic;
7094 fdh->elf.needs_plt |= (fh->elf.needs_plt
7095 || fh->elf.type == STT_FUNC
7096 || fh->elf.type == STT_GNU_IFUNC);
7097 move_plt_plist (fh, fdh);
7098
7099 if (!fdh->elf.forced_local
7100 && fh->elf.dynindx != -1)
7101 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7102 return FALSE;
e86ce104
AM
7103 }
7104
50bc7936
AM
7105 /* Now that the info is on the function descriptor, clear the
7106 function code sym info. Any function code syms for which we
7107 don't have a definition in a regular file, we force local.
7108 This prevents a shared library from exporting syms that have
7109 been imported from another library. Function code syms that
7110 are really in the library we must leave global to prevent the
7111 linker dragging in a definition from a static library. */
93f3fa99
AM
7112 force_local = (!fh->elf.def_regular
7113 || fdh == NULL
7114 || !fdh->elf.def_regular
7115 || fdh->elf.forced_local);
50bc7936
AM
7116 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7117
b34976b6 7118 return TRUE;
e86ce104 7119}
40b8271b 7120
a4b6fadd
AM
7121static const struct sfpr_def_parms save_res_funcs[] =
7122 {
7123 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7124 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7125 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7126 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7127 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7128 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7129 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7130 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7131 { "._savef", 14, 31, savefpr, savefpr1_tail },
7132 { "._restf", 14, 31, restfpr, restfpr1_tail },
7133 { "_savevr_", 20, 31, savevr, savevr_tail },
7134 { "_restvr_", 20, 31, restvr, restvr_tail }
7135 };
7136
e86ce104 7137/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
7138 this hook to a) provide some gcc support functions, and b) transfer
7139 dynamic linking information gathered so far on function code symbol
7140 entries, to their corresponding function descriptor symbol entries. */
deb0e272 7141
b34976b6 7142static bfd_boolean
4ce794b7
AM
7143ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7144 struct bfd_link_info *info)
e86ce104
AM
7145{
7146 struct ppc_link_hash_table *htab;
7147
7148 htab = ppc_hash_table (info);
4dfe6ac6
NC
7149 if (htab == NULL)
7150 return FALSE;
7151
b32547cd
AM
7152 /* Provide any missing _save* and _rest* functions. */
7153 if (htab->sfpr != NULL)
7154 {
7155 unsigned int i;
7156
7157 htab->sfpr->size = 0;
7158 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7159 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7160 return FALSE;
7161 if (htab->sfpr->size == 0)
7162 htab->sfpr->flags |= SEC_EXCLUDE;
7163 }
7164
7165 if (bfd_link_relocatable (info))
7166 return TRUE;
7167
7168 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7169 {
7170 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7171 /* Make .TOC. defined so as to prevent it being made dynamic.
7172 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7173 if (!htab->elf.hgot->def_regular
7174 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7175 {
7176 htab->elf.hgot->root.type = bfd_link_hash_defined;
7177 htab->elf.hgot->root.u.def.value = 0;
7178 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7179 htab->elf.hgot->def_regular = 1;
7180 htab->elf.hgot->root.linker_def = 1;
7181 }
dba6fa9b 7182 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7183 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7184 | STV_HIDDEN);
7185 }
c66bb0ee 7186
8c5b4e52
AM
7187 if (htab->need_func_desc_adj)
7188 {
7189 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7190 htab->need_func_desc_adj = 0;
7191 }
805fc799 7192
b34976b6 7193 return TRUE;
e86ce104
AM
7194}
7195
8a2058b5
AM
7196/* Return true if we have dynamic relocs against H that apply to
7197 read-only sections. */
a345bc8d
AM
7198
7199static bfd_boolean
7200readonly_dynrelocs (struct elf_link_hash_entry *h)
7201{
7202 struct ppc_link_hash_entry *eh;
7203 struct elf_dyn_relocs *p;
7204
7205 eh = (struct ppc_link_hash_entry *) h;
7206 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7207 {
7208 asection *s = p->sec->output_section;
7209
7210 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7211 return TRUE;
7212 }
7213 return FALSE;
7214}
7215
d311bc8b
AM
7216/* Return true if we have dynamic relocs against H or any of its weak
7217 aliases, that apply to read-only sections. */
7218
7219static bfd_boolean
7220alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7221{
7222 struct ppc_link_hash_entry *eh;
7223
7224 eh = (struct ppc_link_hash_entry *) h;
7225 do
7226 {
7227 if (readonly_dynrelocs (&eh->elf))
7228 return TRUE;
7229 eh = eh->weakref;
7230 } while (eh != NULL && &eh->elf != h);
7231
7232 return FALSE;
7233}
8a2058b5 7234
8a9e8e72
AM
7235/* Return whether EH has pc-relative dynamic relocs. */
7236
7237static bfd_boolean
7238pc_dynrelocs (struct ppc_link_hash_entry *eh)
7239{
7240 struct elf_dyn_relocs *p;
7241
7242 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7243 if (p->pc_count != 0)
7244 return TRUE;
7245 return FALSE;
7246}
7247
8a2058b5
AM
7248/* Return true if a global entry stub will be created for H. Valid
7249 for ELFv2 before plt entries have been allocated. */
7250
7251static bfd_boolean
7252global_entry_stub (struct elf_link_hash_entry *h)
7253{
7254 struct plt_entry *pent;
7255
7256 if (!h->pointer_equality_needed
7257 || h->def_regular)
7258 return FALSE;
7259
7260 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7261 if (pent->plt.refcount > 0
7262 && pent->addend == 0)
7263 return TRUE;
7264
7265 return FALSE;
7266}
7267
e86ce104
AM
7268/* Adjust a symbol defined by a dynamic object and referenced by a
7269 regular object. The current definition is in some section of the
7270 dynamic object, but we're not including those sections. We have to
7271 change the definition to something the rest of the link can
7272 understand. */
7273
b34976b6 7274static bfd_boolean
4ce794b7
AM
7275ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7276 struct elf_link_hash_entry *h)
e86ce104
AM
7277{
7278 struct ppc_link_hash_table *htab;
5474d94f 7279 asection *s, *srel;
e86ce104
AM
7280
7281 htab = ppc_hash_table (info);
4dfe6ac6
NC
7282 if (htab == NULL)
7283 return FALSE;
e86ce104
AM
7284
7285 /* Deal with function syms. */
7286 if (h->type == STT_FUNC
e054468f 7287 || h->type == STT_GNU_IFUNC
f5385ebf 7288 || h->needs_plt)
e86ce104
AM
7289 {
7290 /* Clear procedure linkage table information for any symbol that
7291 won't need a .plt entry. */
411e1bfb
AM
7292 struct plt_entry *ent;
7293 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7294 if (ent->plt.refcount > 0)
7295 break;
8387904d 7296 if (ent == NULL
e054468f
AM
7297 || (h->type != STT_GNU_IFUNC
7298 && (SYMBOL_CALLS_LOCAL (info, h)
7299 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
a4b6fadd
AM
7300 && h->root.type == bfd_link_hash_undefweak)))
7301 || ((struct ppc_link_hash_entry *) h)->save_res)
40b8271b 7302 {
411e1bfb 7303 h->plt.plist = NULL;
f5385ebf 7304 h->needs_plt = 0;
d1eca1e4 7305 h->pointer_equality_needed = 0;
40b8271b 7306 }
8a2058b5 7307 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7308 {
d1eca1e4
AM
7309 /* Taking a function's address in a read/write section
7310 doesn't require us to define the function symbol in the
7311 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7312 be used instead. The reason we prefer a few more dynamic
7313 relocs is that calling via a global entry stub costs a
7314 few more instructions, and pointer_equality_needed causes
7315 extra work in ld.so when resolving these symbols. */
7316 if (global_entry_stub (h)
d311bc8b 7317 && !alias_readonly_dynrelocs (h))
d1eca1e4
AM
7318 {
7319 h->pointer_equality_needed = 0;
8a2058b5
AM
7320 /* After adjust_dynamic_symbol, non_got_ref set in
7321 the non-pic case means that dyn_relocs for this
7322 symbol should be discarded. */
d1eca1e4
AM
7323 h->non_got_ref = 0;
7324 }
7325
a345bc8d
AM
7326 /* If making a plt entry, then we don't need copy relocs. */
7327 return TRUE;
7328 }
5bd4f169 7329 }
bbd7ec4a 7330 else
411e1bfb 7331 h->plt.plist = NULL;
5bd4f169
AM
7332
7333 /* If this is a weak symbol, and there is a real definition, the
7334 processor independent code will have arranged for us to see the
7335 real definition first, and we can just use the same value. */
f6e332e6 7336 if (h->u.weakdef != NULL)
5bd4f169 7337 {
f6e332e6
AM
7338 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7339 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7340 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7341 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 7342 if (ELIMINATE_COPY_RELOCS)
f6e332e6 7343 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 7344 return TRUE;
5bd4f169
AM
7345 }
7346
5bd4f169
AM
7347 /* If we are creating a shared library, we must presume that the
7348 only references to the symbol are via the global offset table.
7349 For such cases we need not do anything here; the relocations will
7350 be handled correctly by relocate_section. */
0e1862bb 7351 if (bfd_link_pic (info))
b34976b6 7352 return TRUE;
5bd4f169 7353
65f38f15
AM
7354 /* If there are no references to this symbol that do not use the
7355 GOT, we don't need to generate a copy reloc. */
f5385ebf 7356 if (!h->non_got_ref)
b34976b6 7357 return TRUE;
65f38f15 7358
b186458a 7359 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7360 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7361
d93d1c80
AM
7362 /* If -z nocopyreloc was given, don't generate them either. */
7363 || info->nocopyreloc
a127494f 7364
d93d1c80
AM
7365 /* If we didn't find any dynamic relocs in read-only sections, then
7366 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7367 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7368
d93d1c80
AM
7369 /* Protected variables do not work with .dynbss. The copy in
7370 .dynbss won't be used by the shared library with the protected
7371 definition for the variable. Text relocations are preferable
7372 to an incorrect program. */
7373 || h->protected_def)
a127494f
AM
7374 {
7375 h->non_got_ref = 0;
7376 return TRUE;
7377 }
7378
5d35169e 7379 if (h->plt.plist != NULL)
97b639ba
AM
7380 {
7381 /* We should never get here, but unfortunately there are versions
7382 of gcc out there that improperly (for this ABI) put initialized
7383 function pointers, vtable refs and suchlike in read-only
7384 sections. Allow them to proceed, but warn that this might
7385 break at runtime. */
25f53a85 7386 info->callbacks->einfo
bc30df16 7387 (_("%P: copy reloc against `%T' requires lazy plt linking; "
25f53a85 7388 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7389 h->root.root.string);
7390 }
5d35169e
AM
7391
7392 /* This is a reference to a symbol defined by a dynamic object which
7393 is not a function. */
7394
5bd4f169
AM
7395 /* We must allocate the symbol in our .dynbss section, which will
7396 become part of the .bss section of the executable. There will be
7397 an entry for this symbol in the .dynsym section. The dynamic
7398 object will contain position independent code, so all references
7399 from the dynamic object to this symbol will go through the global
7400 offset table. The dynamic linker will use the .dynsym entry to
7401 determine the address it must put in the global offset table, so
7402 both the dynamic object and the regular object will refer to the
7403 same memory location for the variable. */
5bd4f169 7404
04c9666a
AM
7405 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7406 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
7407 runtime process image. We need to remember the offset into the
7408 .rela.bss section we are going to use. */
5474d94f
AM
7409 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7410 {
7411 s = htab->elf.sdynrelro;
7412 srel = htab->elf.sreldynrelro;
7413 }
7414 else
7415 {
7416 s = htab->elf.sdynbss;
7417 srel = htab->elf.srelbss;
7418 }
1d7e9d18 7419 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7420 {
5474d94f 7421 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 7422 h->needs_copy = 1;
5bd4f169
AM
7423 }
7424
6cabe1ea 7425 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7426}
7427
e86ce104
AM
7428/* If given a function descriptor symbol, hide both the function code
7429 sym and the descriptor. */
7430static void
4ce794b7
AM
7431ppc64_elf_hide_symbol (struct bfd_link_info *info,
7432 struct elf_link_hash_entry *h,
7433 bfd_boolean force_local)
e86ce104 7434{
34814b9f 7435 struct ppc_link_hash_entry *eh;
e86ce104
AM
7436 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7437
34814b9f
AM
7438 eh = (struct ppc_link_hash_entry *) h;
7439 if (eh->is_func_descriptor)
e86ce104 7440 {
34814b9f 7441 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7442
721956f4 7443 if (fh == NULL)
d1329ca3
AM
7444 {
7445 const char *p, *q;
b8ac2841 7446 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
7447 char save;
7448
7449 /* We aren't supposed to use alloca in BFD because on
7450 systems which do not have alloca the version in libiberty
7451 calls xmalloc, which might cause the program to crash
7452 when it runs out of memory. This function doesn't have a
7453 return status, so there's no way to gracefully return an
7454 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7455 accessed; It's either a string in an ELF string table,
7456 or allocated in an objalloc structure. */
d1329ca3 7457
34814b9f 7458 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7459 save = *p;
7460 *(char *) p = '.';
34814b9f 7461 fh = (struct ppc_link_hash_entry *)
b8ac2841 7462 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7463 *(char *) p = save;
7464
7465 /* Unfortunately, if it so happens that the string we were
7466 looking for was allocated immediately before this string,
7467 then we overwrote the string terminator. That's the only
7468 reason the lookup should fail. */
7469 if (fh == NULL)
7470 {
34814b9f
AM
7471 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7472 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7473 --q, --p;
34814b9f
AM
7474 if (q < eh->elf.root.root.string && *p == '.')
7475 fh = (struct ppc_link_hash_entry *)
b8ac2841 7476 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7477 }
7478 if (fh != NULL)
7479 {
34814b9f
AM
7480 eh->oh = fh;
7481 fh->oh = eh;
d1329ca3
AM
7482 }
7483 }
e86ce104 7484 if (fh != NULL)
34814b9f 7485 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7486 }
7487}
7488
411e1bfb 7489static bfd_boolean
8843416a
AM
7490get_sym_h (struct elf_link_hash_entry **hp,
7491 Elf_Internal_Sym **symp,
7492 asection **symsecp,
f961d9dd 7493 unsigned char **tls_maskp,
8843416a
AM
7494 Elf_Internal_Sym **locsymsp,
7495 unsigned long r_symndx,
7496 bfd *ibfd)
411e1bfb 7497{
0ffa91dd 7498 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7499
7500 if (r_symndx >= symtab_hdr->sh_info)
7501 {
7502 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7503 struct elf_link_hash_entry *h;
7504
7505 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7506 h = elf_follow_link (h);
411e1bfb
AM
7507
7508 if (hp != NULL)
7509 *hp = h;
7510
7511 if (symp != NULL)
7512 *symp = NULL;
7513
7514 if (symsecp != NULL)
7515 {
7516 asection *symsec = NULL;
7517 if (h->root.type == bfd_link_hash_defined
7518 || h->root.type == bfd_link_hash_defweak)
7519 symsec = h->root.u.def.section;
7520 *symsecp = symsec;
7521 }
7522
e7b938ca 7523 if (tls_maskp != NULL)
411e1bfb
AM
7524 {
7525 struct ppc_link_hash_entry *eh;
7526
7527 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7528 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7529 }
7530 }
7531 else
7532 {
7533 Elf_Internal_Sym *sym;
7534 Elf_Internal_Sym *locsyms = *locsymsp;
7535
7536 if (locsyms == NULL)
7537 {
7538 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7539 if (locsyms == NULL)
7540 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7541 symtab_hdr->sh_info,
7542 0, NULL, NULL, NULL);
7543 if (locsyms == NULL)
7544 return FALSE;
7545 *locsymsp = locsyms;
7546 }
7547 sym = locsyms + r_symndx;
7548
7549 if (hp != NULL)
7550 *hp = NULL;
7551
7552 if (symp != NULL)
7553 *symp = sym;
7554
7555 if (symsecp != NULL)
cb33740c 7556 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7557
e7b938ca 7558 if (tls_maskp != NULL)
411e1bfb
AM
7559 {
7560 struct got_entry **lgot_ents;
f961d9dd 7561 unsigned char *tls_mask;
411e1bfb 7562
e7b938ca 7563 tls_mask = NULL;
411e1bfb
AM
7564 lgot_ents = elf_local_got_ents (ibfd);
7565 if (lgot_ents != NULL)
7566 {
e054468f
AM
7567 struct plt_entry **local_plt = (struct plt_entry **)
7568 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7569 unsigned char *lgot_masks = (unsigned char *)
e054468f 7570 (local_plt + symtab_hdr->sh_info);
e7b938ca 7571 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7572 }
e7b938ca 7573 *tls_maskp = tls_mask;
411e1bfb
AM
7574 }
7575 }
7576 return TRUE;
7577}
7578
e7b938ca 7579/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7580 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7581 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7582
7583static int
f961d9dd 7584get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7585 unsigned long *toc_symndx,
7586 bfd_vma *toc_addend,
0d4792f7 7587 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7588 const Elf_Internal_Rela *rel,
7589 bfd *ibfd)
411e1bfb
AM
7590{
7591 unsigned long r_symndx;
0d4792f7 7592 int next_r;
411e1bfb
AM
7593 struct elf_link_hash_entry *h;
7594 Elf_Internal_Sym *sym;
7595 asection *sec;
7596 bfd_vma off;
7597
7598 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7599 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7600 return 0;
411e1bfb 7601
e7b938ca 7602 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7603 || sec == NULL
6bee8834 7604 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7605 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7606 return 1;
411e1bfb
AM
7607
7608 /* Look inside a TOC section too. */
7609 if (h != NULL)
7610 {
7611 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7612 off = h->root.u.def.value;
7613 }
7614 else
7615 off = sym->st_value;
7616 off += rel->r_addend;
7617 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7618 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7619 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7620 if (toc_symndx != NULL)
7621 *toc_symndx = r_symndx;
3a71aa26
AM
7622 if (toc_addend != NULL)
7623 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7624 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7625 return 0;
854b41e7 7626 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7627 && (next_r == -1 || next_r == -2))
7628 return 1 - next_r;
951fd09b 7629 return 1;
411e1bfb
AM
7630}
7631
3b421ab3
AM
7632/* Find (or create) an entry in the tocsave hash table. */
7633
7634static struct tocsave_entry *
7635tocsave_find (struct ppc_link_hash_table *htab,
7636 enum insert_option insert,
7637 Elf_Internal_Sym **local_syms,
7638 const Elf_Internal_Rela *irela,
7639 bfd *ibfd)
7640{
7641 unsigned long r_indx;
7642 struct elf_link_hash_entry *h;
7643 Elf_Internal_Sym *sym;
7644 struct tocsave_entry ent, *p;
7645 hashval_t hash;
7646 struct tocsave_entry **slot;
7647
7648 r_indx = ELF64_R_SYM (irela->r_info);
7649 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7650 return NULL;
7651 if (ent.sec == NULL || ent.sec->output_section == NULL)
7652 {
4eca0228 7653 _bfd_error_handler
10463f39 7654 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7655 return NULL;
7656 }
7657
7658 if (h != NULL)
7659 ent.offset = h->root.u.def.value;
7660 else
7661 ent.offset = sym->st_value;
7662 ent.offset += irela->r_addend;
7663
7664 hash = tocsave_htab_hash (&ent);
7665 slot = ((struct tocsave_entry **)
7666 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7667 if (slot == NULL)
7668 return NULL;
7669
7670 if (*slot == NULL)
7671 {
7672 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7673 if (p == NULL)
7674 return NULL;
7675 *p = ent;
7676 *slot = p;
7677 }
7678 return *slot;
7679}
7680
754021d0 7681/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7682 code for the old ABI, these will already have been done. */
754021d0
AM
7683
7684static bfd_boolean
7685adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7686{
7687 struct ppc_link_hash_entry *eh;
7688 asection *sym_sec;
74f0fb50 7689 struct _opd_sec_data *opd;
754021d0
AM
7690
7691 if (h->root.type == bfd_link_hash_indirect)
7692 return TRUE;
7693
754021d0
AM
7694 if (h->root.type != bfd_link_hash_defined
7695 && h->root.type != bfd_link_hash_defweak)
7696 return TRUE;
7697
7698 eh = (struct ppc_link_hash_entry *) h;
7699 if (eh->adjust_done)
7700 return TRUE;
7701
7702 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7703 opd = get_opd_info (sym_sec);
7704 if (opd != NULL && opd->adjust != NULL)
754021d0 7705 {
51aecdc5 7706 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7707 if (adjust == -1)
7708 {
7709 /* This entry has been deleted. */
b3fac117 7710 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7711 if (dsec == NULL)
7712 {
7713 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7714 if (discarded_section (dsec))
81688140 7715 {
b3fac117 7716 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7717 break;
7718 }
7719 }
4025353c 7720 eh->elf.root.u.def.value = 0;
81688140 7721 eh->elf.root.u.def.section = dsec;
4025353c
AM
7722 }
7723 else
7724 eh->elf.root.u.def.value += adjust;
754021d0
AM
7725 eh->adjust_done = 1;
7726 }
7727 return TRUE;
7728}
7729
8c1d1bb8 7730/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7731 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7732 have already been determined. */
7733
7734static bfd_boolean
7735dec_dynrel_count (bfd_vma r_info,
7736 asection *sec,
7737 struct bfd_link_info *info,
7738 Elf_Internal_Sym **local_syms,
7739 struct elf_link_hash_entry *h,
19e08130 7740 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7741{
7742 enum elf_ppc64_reloc_type r_type;
19e08130 7743 asection *sym_sec = NULL;
8c1d1bb8
AM
7744
7745 /* Can this reloc be dynamic? This switch, and later tests here
7746 should be kept in sync with the code in check_relocs. */
7747 r_type = ELF64_R_TYPE (r_info);
7748 switch (r_type)
7749 {
7750 default:
7751 return TRUE;
7752
7753 case R_PPC64_TPREL16:
7754 case R_PPC64_TPREL16_LO:
7755 case R_PPC64_TPREL16_HI:
7756 case R_PPC64_TPREL16_HA:
7757 case R_PPC64_TPREL16_DS:
7758 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7759 case R_PPC64_TPREL16_HIGH:
7760 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7761 case R_PPC64_TPREL16_HIGHER:
7762 case R_PPC64_TPREL16_HIGHERA:
7763 case R_PPC64_TPREL16_HIGHEST:
7764 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 7765 if (!bfd_link_pic (info))
8c1d1bb8
AM
7766 return TRUE;
7767
7768 case R_PPC64_TPREL64:
7769 case R_PPC64_DTPMOD64:
7770 case R_PPC64_DTPREL64:
7771 case R_PPC64_ADDR64:
7772 case R_PPC64_REL30:
7773 case R_PPC64_REL32:
7774 case R_PPC64_REL64:
7775 case R_PPC64_ADDR14:
7776 case R_PPC64_ADDR14_BRNTAKEN:
7777 case R_PPC64_ADDR14_BRTAKEN:
7778 case R_PPC64_ADDR16:
7779 case R_PPC64_ADDR16_DS:
7780 case R_PPC64_ADDR16_HA:
7781 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7782 case R_PPC64_ADDR16_HIGH:
7783 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7784 case R_PPC64_ADDR16_HIGHER:
7785 case R_PPC64_ADDR16_HIGHERA:
7786 case R_PPC64_ADDR16_HIGHEST:
7787 case R_PPC64_ADDR16_HIGHESTA:
7788 case R_PPC64_ADDR16_LO:
7789 case R_PPC64_ADDR16_LO_DS:
7790 case R_PPC64_ADDR24:
7791 case R_PPC64_ADDR32:
7792 case R_PPC64_UADDR16:
7793 case R_PPC64_UADDR32:
7794 case R_PPC64_UADDR64:
7795 case R_PPC64_TOC:
7796 break;
7797 }
7798
7799 if (local_syms != NULL)
7800 {
7801 unsigned long r_symndx;
8c1d1bb8
AM
7802 bfd *ibfd = sec->owner;
7803
7804 r_symndx = ELF64_R_SYM (r_info);
7805 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7806 return FALSE;
7807 }
7808
0e1862bb 7809 if ((bfd_link_pic (info)
1d483afe 7810 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7811 || (h != NULL
198f1157 7812 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7813 || h->root.type == bfd_link_hash_defweak
7814 || !h->def_regular))))
7815 || (ELIMINATE_COPY_RELOCS
0e1862bb 7816 && !bfd_link_pic (info)
8c1d1bb8
AM
7817 && h != NULL
7818 && (h->root.type == bfd_link_hash_defweak
7819 || !h->def_regular)))
7820 ;
7821 else
7822 return TRUE;
7823
7824 if (h != NULL)
6edfbbad 7825 {
19e08130
AM
7826 struct elf_dyn_relocs *p;
7827 struct elf_dyn_relocs **pp;
7828 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7829
7830 /* elf_gc_sweep may have already removed all dyn relocs associated
7831 with local syms for a given section. Also, symbol flags are
7832 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7833 report a dynreloc miscount. */
7834 if (*pp == NULL && info->gc_sections)
7835 return TRUE;
7836
7837 while ((p = *pp) != NULL)
60124e18 7838 {
19e08130
AM
7839 if (p->sec == sec)
7840 {
7841 if (!must_be_dyn_reloc (info, r_type))
7842 p->pc_count -= 1;
7843 p->count -= 1;
7844 if (p->count == 0)
7845 *pp = p->next;
7846 return TRUE;
7847 }
7848 pp = &p->next;
60124e18 7849 }
6edfbbad 7850 }
19e08130
AM
7851 else
7852 {
7853 struct ppc_dyn_relocs *p;
7854 struct ppc_dyn_relocs **pp;
7855 void *vpp;
7856 bfd_boolean is_ifunc;
8c1d1bb8 7857
19e08130
AM
7858 if (local_syms == NULL)
7859 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7860 if (sym_sec == NULL)
7861 sym_sec = sec;
c57da1a7 7862
19e08130
AM
7863 vpp = &elf_section_data (sym_sec)->local_dynrel;
7864 pp = (struct ppc_dyn_relocs **) vpp;
7865
7866 if (*pp == NULL && info->gc_sections)
7867 return TRUE;
7868
7869 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7870 while ((p = *pp) != NULL)
8c1d1bb8 7871 {
19e08130
AM
7872 if (p->sec == sec && p->ifunc == is_ifunc)
7873 {
7874 p->count -= 1;
7875 if (p->count == 0)
7876 *pp = p->next;
7877 return TRUE;
7878 }
7879 pp = &p->next;
8c1d1bb8 7880 }
8c1d1bb8
AM
7881 }
7882
695344c0 7883 /* xgettext:c-format */
8de848d8 7884 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7885 sec->owner, sec);
8c1d1bb8
AM
7886 bfd_set_error (bfd_error_bad_value);
7887 return FALSE;
7888}
7889
754021d0
AM
7890/* Remove unused Official Procedure Descriptor entries. Currently we
7891 only remove those associated with functions in discarded link-once
7892 sections, or weakly defined functions that have been overridden. It
7893 would be possible to remove many more entries for statically linked
7894 applications. */
7895
b34976b6 7896bfd_boolean
e7d1c40c 7897ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7898{
7899 bfd *ibfd;
754021d0 7900 bfd_boolean some_edited = FALSE;
3f764659 7901 asection *need_pad = NULL;
e7d1c40c
AM
7902 struct ppc_link_hash_table *htab;
7903
7904 htab = ppc_hash_table (info);
7905 if (htab == NULL)
7906 return FALSE;
1e2f5b6e 7907
c72f2fb2 7908 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7909 {
7910 asection *sec;
7911 Elf_Internal_Rela *relstart, *rel, *relend;
7912 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7913 Elf_Internal_Sym *local_syms;
74f0fb50 7914 struct _opd_sec_data *opd;
51aecdc5 7915 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7916 bfd_size_type cnt_16b = 0;
1e2f5b6e 7917
854b41e7
AM
7918 if (!is_ppc64_elf (ibfd))
7919 continue;
7920
1e2f5b6e 7921 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7922 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7923 continue;
7924
dbaa2011 7925 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7926 continue;
7927
1e2f5b6e
AM
7928 if (sec->output_section == bfd_abs_section_ptr)
7929 continue;
7930
7931 /* Look through the section relocs. */
7932 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7933 continue;
7934
6cdc0ccc 7935 local_syms = NULL;
0ffa91dd 7936 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7937
7938 /* Read the relocations. */
4ce794b7 7939 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7940 info->keep_memory);
1e2f5b6e 7941 if (relstart == NULL)
b34976b6 7942 return FALSE;
1e2f5b6e
AM
7943
7944 /* First run through the relocs to check they are sane, and to
7945 determine whether we need to edit this opd section. */
b34976b6 7946 need_edit = FALSE;
51aecdc5 7947 broken = FALSE;
3f764659 7948 need_pad = sec;
1e2f5b6e 7949 relend = relstart + sec->reloc_count;
50bc7936 7950 for (rel = relstart; rel < relend; )
1e2f5b6e 7951 {
04c9666a 7952 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7953 unsigned long r_symndx;
7954 asection *sym_sec;
7955 struct elf_link_hash_entry *h;
7956 Elf_Internal_Sym *sym;
51aecdc5 7957 bfd_vma offset;
1e2f5b6e 7958
51aecdc5 7959 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7960 only interested in the reloc pointing to a function entry
7961 point. */
51aecdc5
AM
7962 offset = rel->r_offset;
7963 if (rel + 1 == relend
7964 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7965 {
7966 /* If someone messes with .opd alignment then after a
7967 "ld -r" we might have padding in the middle of .opd.
7968 Also, there's nothing to prevent someone putting
7969 something silly in .opd with the assembler. No .opd
b34976b6 7970 optimization for them! */
3f764659 7971 broken_opd:
4eca0228 7972 _bfd_error_handler
d003868e 7973 (_("%B: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7974 broken = TRUE;
1e2f5b6e
AM
7975 break;
7976 }
7977
50bc7936
AM
7978 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7979 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7980 {
4eca0228 7981 _bfd_error_handler
695344c0 7982 /* xgettext:c-format */
d003868e
AM
7983 (_("%B: unexpected reloc type %u in .opd section"),
7984 ibfd, r_type);
51aecdc5 7985 broken = TRUE;
50bc7936
AM
7986 break;
7987 }
7988
1e2f5b6e 7989 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7990 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7991 r_symndx, ibfd))
50bc7936 7992 goto error_ret;
1e2f5b6e
AM
7993
7994 if (sym_sec == NULL || sym_sec->owner == NULL)
7995 {
411e1bfb
AM
7996 const char *sym_name;
7997 if (h != NULL)
7998 sym_name = h->root.root.string;
7999 else
26c61ae5
L
8000 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
8001 sym_sec);
411e1bfb 8002
4eca0228 8003 _bfd_error_handler
695344c0 8004 /* xgettext:c-format */
d003868e
AM
8005 (_("%B: undefined sym `%s' in .opd section"),
8006 ibfd, sym_name);
51aecdc5 8007 broken = TRUE;
1e2f5b6e
AM
8008 break;
8009 }
8010
51020317
AM
8011 /* opd entries are always for functions defined in the
8012 current input bfd. If the symbol isn't defined in the
8013 input bfd, then we won't be using the function in this
8014 bfd; It must be defined in a linkonce section in another
8015 bfd, or is weak. It's also possible that we are
8016 discarding the function due to a linker script /DISCARD/,
8017 which we test for via the output_section. */
8018 if (sym_sec->owner != ibfd
8019 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 8020 need_edit = TRUE;
1e2f5b6e 8021
50bc7936 8022 rel += 2;
51aecdc5
AM
8023 if (rel + 1 == relend
8024 || (rel + 2 < relend
8025 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8026 ++rel;
8027
8028 if (rel == relend)
3f764659
JJ
8029 {
8030 if (sec->size == offset + 24)
8031 {
8032 need_pad = NULL;
8033 break;
8034 }
51aecdc5 8035 if (sec->size == offset + 16)
3f764659
JJ
8036 {
8037 cnt_16b++;
8038 break;
8039 }
8040 goto broken_opd;
8041 }
3f764659
JJ
8042 else if (rel + 1 < relend
8043 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8044 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8045 {
51aecdc5
AM
8046 if (rel[0].r_offset == offset + 16)
8047 cnt_16b++;
8048 else if (rel[0].r_offset != offset + 24)
8049 goto broken_opd;
3f764659
JJ
8050 }
8051 else
8052 goto broken_opd;
1e2f5b6e
AM
8053 }
8054
e7d1c40c 8055 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 8056
51aecdc5 8057 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
8058 {
8059 Elf_Internal_Rela *write_rel;
d4730f92 8060 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 8061 bfd_byte *rptr, *wptr;
983bddc8 8062 bfd_byte *new_contents;
74f0fb50
AM
8063 bfd_size_type amt;
8064
983bddc8 8065 new_contents = NULL;
51aecdc5 8066 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 8067 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 8068 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
8069 if (opd->adjust == NULL)
8070 return FALSE;
8071 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
8072
8073 /* This seems a waste of time as input .opd sections are all
8074 zeros as generated by gcc, but I suppose there's no reason
8075 this will always be so. We might start putting something in
8076 the third word of .opd entries. */
8077 if ((sec->flags & SEC_IN_MEMORY) == 0)
8078 {
eea6121a
AM
8079 bfd_byte *loc;
8080 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 8081 {
eea6121a
AM
8082 if (loc != NULL)
8083 free (loc);
50bc7936 8084 error_ret:
6cdc0ccc
AM
8085 if (local_syms != NULL
8086 && symtab_hdr->contents != (unsigned char *) local_syms)
8087 free (local_syms);
6cdc0ccc
AM
8088 if (elf_section_data (sec)->relocs != relstart)
8089 free (relstart);
b34976b6 8090 return FALSE;
6cdc0ccc 8091 }
1e2f5b6e
AM
8092 sec->contents = loc;
8093 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8094 }
8095
8096 elf_section_data (sec)->relocs = relstart;
8097
3f764659 8098 new_contents = sec->contents;
3f764659
JJ
8099 if (add_aux_fields)
8100 {
8101 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8102 if (new_contents == NULL)
8103 return FALSE;
51aecdc5 8104 need_pad = NULL;
3f764659 8105 }
b4f4e59f
AM
8106 wptr = new_contents;
8107 rptr = sec->contents;
1e2f5b6e 8108 write_rel = relstart;
51aecdc5 8109 for (rel = relstart; rel < relend; )
1e2f5b6e 8110 {
50bc7936
AM
8111 unsigned long r_symndx;
8112 asection *sym_sec;
8113 struct elf_link_hash_entry *h;
51aecdc5 8114 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 8115 Elf_Internal_Sym *sym;
51aecdc5
AM
8116 long opd_ent_size;
8117 Elf_Internal_Rela *next_rel;
8118 bfd_boolean skip;
50bc7936
AM
8119
8120 r_symndx = ELF64_R_SYM (rel->r_info);
8121 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 8122 r_symndx, ibfd))
50bc7936
AM
8123 goto error_ret;
8124
51aecdc5
AM
8125 next_rel = rel + 2;
8126 if (next_rel + 1 == relend
8127 || (next_rel + 2 < relend
8128 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8129 ++next_rel;
8130
8131 /* See if the .opd entry is full 24 byte or
8132 16 byte (with fd_aux entry overlapped with next
8133 fd_func). */
8134 opd_ent_size = 24;
8135 if (next_rel == relend)
1e2f5b6e 8136 {
51aecdc5 8137 if (sec->size == rel->r_offset + 16)
3f764659 8138 opd_ent_size = 16;
51aecdc5
AM
8139 }
8140 else if (next_rel->r_offset == rel->r_offset + 16)
8141 opd_ent_size = 16;
3f764659 8142
51aecdc5
AM
8143 if (h != NULL
8144 && h->root.root.string[0] == '.')
8145 {
8c5b4e52
AM
8146 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8147 if (fdh != NULL)
8148 {
8149 fdh = ppc_follow_link (fdh);
8150 if (fdh->elf.root.type != bfd_link_hash_defined
8151 && fdh->elf.root.type != bfd_link_hash_defweak)
8152 fdh = NULL;
8153 }
51aecdc5 8154 }
1e2f5b6e 8155
51aecdc5
AM
8156 skip = (sym_sec->owner != ibfd
8157 || sym_sec->output_section == bfd_abs_section_ptr);
8158 if (skip)
8159 {
8160 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8161 {
51aecdc5
AM
8162 /* Arrange for the function descriptor sym
8163 to be dropped. */
8164 fdh->elf.root.u.def.value = 0;
8165 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8166 }
51aecdc5 8167 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8168
0e1862bb 8169 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8170 rel = next_rel;
8171 else
8172 while (1)
8173 {
8174 if (!dec_dynrel_count (rel->r_info, sec, info,
8175 NULL, h, sym))
8176 goto error_ret;
754021d0 8177
51aecdc5
AM
8178 if (++rel == next_rel)
8179 break;
1e2f5b6e 8180
51aecdc5
AM
8181 r_symndx = ELF64_R_SYM (rel->r_info);
8182 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8183 r_symndx, ibfd))
8184 goto error_ret;
8185 }
50bc7936
AM
8186 }
8187 else
1e2f5b6e 8188 {
51aecdc5
AM
8189 /* We'll be keeping this opd entry. */
8190 long adjust;
8191
8192 if (fdh != NULL)
8193 {
8194 /* Redefine the function descriptor symbol to
8195 this location in the opd section. It is
8196 necessary to update the value here rather
8197 than using an array of adjustments as we do
8198 for local symbols, because various places
8199 in the generic ELF code use the value
8200 stored in u.def.value. */
8201 fdh->elf.root.u.def.value = wptr - new_contents;
8202 fdh->adjust_done = 1;
8203 }
8204
8205 /* Local syms are a bit tricky. We could
8206 tweak them as they can be cached, but
8207 we'd need to look through the local syms
8208 for the function descriptor sym which we
8209 don't have at the moment. So keep an
8210 array of adjustments. */
8211 adjust = (wptr - new_contents) - (rptr - sec->contents);
8212 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8213
8214 if (wptr != rptr)
8215 memcpy (wptr, rptr, opd_ent_size);
8216 wptr += opd_ent_size;
8217 if (add_aux_fields && opd_ent_size == 16)
8218 {
8219 memset (wptr, '\0', 8);
8220 wptr += 8;
8221 }
8222
50bc7936 8223 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8224 new opd entries. */
8225 for ( ; rel != next_rel; ++rel)
8226 {
8227 rel->r_offset += adjust;
8228 if (write_rel != rel)
8229 memcpy (write_rel, rel, sizeof (*rel));
8230 ++write_rel;
8231 }
1e2f5b6e 8232 }
51aecdc5
AM
8233
8234 rptr += opd_ent_size;
1e2f5b6e
AM
8235 }
8236
3f764659 8237 sec->size = wptr - new_contents;
1e2f5b6e 8238 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8239 if (add_aux_fields)
8240 {
8241 free (sec->contents);
8242 sec->contents = new_contents;
8243 }
8244
05bf9422 8245 /* Fudge the header size too, as this is used later in
cdcf6e38 8246 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8247 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8248 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8249 some_edited = TRUE;
1e2f5b6e 8250 }
6cdc0ccc 8251 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8252 free (relstart);
6cdc0ccc 8253
411e1bfb
AM
8254 if (local_syms != NULL
8255 && symtab_hdr->contents != (unsigned char *) local_syms)
8256 {
8257 if (!info->keep_memory)
8258 free (local_syms);
8259 else
8260 symtab_hdr->contents = (unsigned char *) local_syms;
8261 }
8262 }
8263
754021d0
AM
8264 if (some_edited)
8265 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8266
3f764659
JJ
8267 /* If we are doing a final link and the last .opd entry is just 16 byte
8268 long, add a 8 byte padding after it. */
0e1862bb 8269 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8270 {
8271 bfd_byte *p;
8272
8273 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8274 {
8275 BFD_ASSERT (need_pad->size > 0);
8276
8277 p = bfd_malloc (need_pad->size + 8);
8278 if (p == NULL)
8279 return FALSE;
699733f6 8280
3f764659
JJ
8281 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8282 p, 0, need_pad->size))
8283 return FALSE;
8284
8285 need_pad->contents = p;
8286 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8287 }
8288 else
8289 {
8290 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8291 if (p == NULL)
8292 return FALSE;
8293
8294 need_pad->contents = p;
8295 }
8296
8297 memset (need_pad->contents + need_pad->size, 0, 8);
8298 need_pad->size += 8;
8299 }
8300
411e1bfb
AM
8301 return TRUE;
8302}
8303
e1918d23 8304/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8305
e1918d23 8306asection *
e7d1c40c 8307ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8308{
411e1bfb
AM
8309 struct ppc_link_hash_table *htab;
8310
411e1bfb 8311 htab = ppc_hash_table (info);
4dfe6ac6
NC
8312 if (htab == NULL)
8313 return NULL;
8314
ee67d69a
AM
8315 if (abiversion (info->output_bfd) == 1)
8316 htab->opd_abi = 1;
8317
e7d1c40c 8318 if (htab->params->no_multi_toc)
33c0ec9d
AM
8319 htab->do_multi_toc = 0;
8320 else if (!htab->do_multi_toc)
e7d1c40c 8321 htab->params->no_multi_toc = 1;
33c0ec9d 8322
3a71aa26
AM
8323 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8324 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8325 FALSE, FALSE, TRUE));
a7f2871e
AM
8326 /* Move dynamic linking info to the function descriptor sym. */
8327 if (htab->tls_get_addr != NULL)
8328 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8329 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8330 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8331 FALSE, FALSE, TRUE));
7c9cf415 8332 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8333 {
8334 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8335
8336 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8337 FALSE, FALSE, TRUE);
8338 if (opt != NULL)
8339 func_desc_adjust (opt, info);
8340 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8341 FALSE, FALSE, TRUE);
8342 if (opt_fd != NULL
8343 && (opt_fd->root.type == bfd_link_hash_defined
8344 || opt_fd->root.type == bfd_link_hash_defweak))
8345 {
8346 /* If glibc supports an optimized __tls_get_addr call stub,
8347 signalled by the presence of __tls_get_addr_opt, and we'll
8348 be calling __tls_get_addr via a plt call stub, then
8349 make __tls_get_addr point to __tls_get_addr_opt. */
8350 tga_fd = &htab->tls_get_addr_fd->elf;
8351 if (htab->elf.dynamic_sections_created
8352 && tga_fd != NULL
8353 && (tga_fd->type == STT_FUNC
8354 || tga_fd->needs_plt)
8355 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8356 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8357 && tga_fd->root.type == bfd_link_hash_undefweak)))
8358 {
8359 struct plt_entry *ent;
8360
8361 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8362 if (ent->plt.refcount > 0)
8363 break;
8364 if (ent != NULL)
8365 {
8366 tga_fd->root.type = bfd_link_hash_indirect;
8367 tga_fd->root.u.i.link = &opt_fd->root;
8368 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
b531344c 8369 opt_fd->mark = 1;
a7f2871e
AM
8370 if (opt_fd->dynindx != -1)
8371 {
8372 /* Use __tls_get_addr_opt in dynamic relocations. */
8373 opt_fd->dynindx = -1;
8374 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8375 opt_fd->dynstr_index);
8376 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8377 return NULL;
a7f2871e
AM
8378 }
8379 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8380 tga = &htab->tls_get_addr->elf;
8381 if (opt != NULL && tga != NULL)
8382 {
8383 tga->root.type = bfd_link_hash_indirect;
8384 tga->root.u.i.link = &opt->root;
8385 ppc64_elf_copy_indirect_symbol (info, opt, tga);
b531344c 8386 opt->mark = 1;
a7f2871e
AM
8387 _bfd_elf_link_hash_hide_symbol (info, opt,
8388 tga->forced_local);
8389 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8390 }
8391 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8392 htab->tls_get_addr_fd->is_func_descriptor = 1;
8393 if (htab->tls_get_addr != NULL)
8394 {
8395 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8396 htab->tls_get_addr->is_func = 1;
8397 }
8398 }
8399 }
8400 }
7c9cf415
AM
8401 else if (htab->params->tls_get_addr_opt < 0)
8402 htab->params->tls_get_addr_opt = 0;
a7f2871e 8403 }
33c0ec9d 8404 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8405}
8387904d 8406
3a71aa26
AM
8407/* Return TRUE iff REL is a branch reloc with a global symbol matching
8408 HASH1 or HASH2. */
8387904d 8409
3a71aa26
AM
8410static bfd_boolean
8411branch_reloc_hash_match (const bfd *ibfd,
8412 const Elf_Internal_Rela *rel,
8413 const struct ppc_link_hash_entry *hash1,
8414 const struct ppc_link_hash_entry *hash2)
8415{
8416 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8417 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8418 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8419
e054468f 8420 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8421 {
3a71aa26
AM
8422 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8423 struct elf_link_hash_entry *h;
8387904d 8424
3a71aa26 8425 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8426 h = elf_follow_link (h);
3a71aa26
AM
8427 if (h == &hash1->elf || h == &hash2->elf)
8428 return TRUE;
a48ebf4d 8429 }
3a71aa26 8430 return FALSE;
951fd09b 8431}
411e1bfb 8432
951fd09b
AM
8433/* Run through all the TLS relocs looking for optimization
8434 opportunities. The linker has been hacked (see ppc64elf.em) to do
8435 a preliminary section layout so that we know the TLS segment
8436 offsets. We can't optimize earlier because some optimizations need
8437 to know the tp offset, and we need to optimize before allocating
8438 dynamic relocations. */
8439
8440bfd_boolean
33c0ec9d 8441ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8442{
8443 bfd *ibfd;
8444 asection *sec;
8445 struct ppc_link_hash_table *htab;
663a1470 8446 unsigned char *toc_ref;
102890f0 8447 int pass;
951fd09b 8448
3cbc1e5e 8449 if (!bfd_link_executable (info))
411e1bfb
AM
8450 return TRUE;
8451
951fd09b 8452 htab = ppc_hash_table (info);
4dfe6ac6
NC
8453 if (htab == NULL)
8454 return FALSE;
8455
663a1470
AM
8456 /* Make two passes over the relocs. On the first pass, mark toc
8457 entries involved with tls relocs, and check that tls relocs
8458 involved in setting up a tls_get_addr call are indeed followed by
8459 such a call. If they are not, we can't do any tls optimization.
8460 On the second pass twiddle tls_mask flags to notify
8461 relocate_section that optimization can be done, and adjust got
8462 and plt refcounts. */
8463 toc_ref = NULL;
8464 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8465 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8466 {
8467 Elf_Internal_Sym *locsyms = NULL;
8468 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8469
102890f0
AM
8470 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8471 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8472 {
8473 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8474 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8475
102890f0
AM
8476 /* Read the relocations. */
8477 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8478 info->keep_memory);
8479 if (relstart == NULL)
2915c55b
JK
8480 {
8481 free (toc_ref);
8482 return FALSE;
8483 }
411e1bfb 8484
102890f0
AM
8485 relend = relstart + sec->reloc_count;
8486 for (rel = relstart; rel < relend; rel++)
8487 {
8488 enum elf_ppc64_reloc_type r_type;
8489 unsigned long r_symndx;
8490 struct elf_link_hash_entry *h;
8491 Elf_Internal_Sym *sym;
8492 asection *sym_sec;
f961d9dd
AM
8493 unsigned char *tls_mask;
8494 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8495 bfd_vma value;
8496 bfd_boolean ok_tprel, is_local;
8497 long toc_ref_index = 0;
8498 int expecting_tls_get_addr = 0;
663a1470 8499 bfd_boolean ret = FALSE;
411e1bfb 8500
102890f0
AM
8501 r_symndx = ELF64_R_SYM (rel->r_info);
8502 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8503 r_symndx, ibfd))
8504 {
8505 err_free_rel:
8506 if (elf_section_data (sec)->relocs != relstart)
8507 free (relstart);
8508 if (toc_ref != NULL)
8509 free (toc_ref);
8510 if (locsyms != NULL
0ffa91dd 8511 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8512 != (unsigned char *) locsyms))
8513 free (locsyms);
663a1470 8514 return ret;
102890f0 8515 }
411e1bfb 8516
102890f0
AM
8517 if (h != NULL)
8518 {
766bc656
AM
8519 if (h->root.type == bfd_link_hash_defined
8520 || h->root.type == bfd_link_hash_defweak)
8521 value = h->root.u.def.value;
8522 else if (h->root.type == bfd_link_hash_undefweak)
8523 value = 0;
8524 else
663a1470
AM
8525 {
8526 found_tls_get_addr_arg = 0;
8527 continue;
8528 }
102890f0
AM
8529 }
8530 else
8531 /* Symbols referenced by TLS relocs must be of type
8532 STT_TLS. So no need for .opd local sym adjust. */
8533 value = sym->st_value;
8534
8535 ok_tprel = FALSE;
8536 is_local = FALSE;
8537 if (h == NULL
8538 || !h->def_dynamic)
8539 {
8540 is_local = TRUE;
766bc656
AM
8541 if (h != NULL
8542 && h->root.type == bfd_link_hash_undefweak)
8543 ok_tprel = TRUE;
c27b8c2a
AM
8544 else if (sym_sec != NULL
8545 && sym_sec->output_section != NULL)
766bc656
AM
8546 {
8547 value += sym_sec->output_offset;
8548 value += sym_sec->output_section->vma;
8549 value -= htab->elf.tls_sec->vma;
8550 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8551 < (bfd_vma) 1 << 32);
8552 }
102890f0 8553 }
951fd09b 8554
102890f0 8555 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8556 /* If this section has old-style __tls_get_addr calls
8557 without marker relocs, then check that each
8558 __tls_get_addr call reloc is preceded by a reloc
8559 that conceivably belongs to the __tls_get_addr arg
8560 setup insn. If we don't find matching arg setup
8561 relocs, don't do any tls optimization. */
8562 if (pass == 0
8563 && sec->has_tls_get_addr_call
8564 && h != NULL
8565 && (h == &htab->tls_get_addr->elf
8566 || h == &htab->tls_get_addr_fd->elf)
8567 && !found_tls_get_addr_arg
8568 && is_branch_reloc (r_type))
8569 {
25f53a85 8570 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8571 "TLS optimization disabled\n"),
8572 ibfd, sec, rel->r_offset);
8573 ret = TRUE;
8574 goto err_free_rel;
8575 }
8576
8577 found_tls_get_addr_arg = 0;
102890f0
AM
8578 switch (r_type)
8579 {
8580 case R_PPC64_GOT_TLSLD16:
8581 case R_PPC64_GOT_TLSLD16_LO:
8582 expecting_tls_get_addr = 1;
663a1470 8583 found_tls_get_addr_arg = 1;
1a0670f3 8584 /* Fall through. */
102890f0
AM
8585
8586 case R_PPC64_GOT_TLSLD16_HI:
8587 case R_PPC64_GOT_TLSLD16_HA:
8588 /* These relocs should never be against a symbol
8589 defined in a shared lib. Leave them alone if
8590 that turns out to be the case. */
8591 if (!is_local)
8592 continue;
411e1bfb 8593
102890f0 8594 /* LD -> LE */
411e1bfb 8595 tls_set = 0;
102890f0
AM
8596 tls_clear = TLS_LD;
8597 tls_type = TLS_TLS | TLS_LD;
8598 break;
411e1bfb 8599
102890f0
AM
8600 case R_PPC64_GOT_TLSGD16:
8601 case R_PPC64_GOT_TLSGD16_LO:
8602 expecting_tls_get_addr = 1;
663a1470 8603 found_tls_get_addr_arg = 1;
1a0670f3 8604 /* Fall through. */
102890f0
AM
8605
8606 case R_PPC64_GOT_TLSGD16_HI:
8607 case R_PPC64_GOT_TLSGD16_HA:
8608 if (ok_tprel)
8609 /* GD -> LE */
411e1bfb 8610 tls_set = 0;
102890f0
AM
8611 else
8612 /* GD -> IE */
8613 tls_set = TLS_TLS | TLS_TPRELGD;
8614 tls_clear = TLS_GD;
8615 tls_type = TLS_TLS | TLS_GD;
8616 break;
8617
8618 case R_PPC64_GOT_TPREL16_DS:
8619 case R_PPC64_GOT_TPREL16_LO_DS:
8620 case R_PPC64_GOT_TPREL16_HI:
8621 case R_PPC64_GOT_TPREL16_HA:
8622 if (ok_tprel)
8623 {
8624 /* IE -> LE */
8625 tls_set = 0;
8626 tls_clear = TLS_TPREL;
8627 tls_type = TLS_TLS | TLS_TPREL;
8628 break;
8629 }
411e1bfb
AM
8630 continue;
8631
727fc41e
AM
8632 case R_PPC64_TLSGD:
8633 case R_PPC64_TLSLD:
663a1470 8634 found_tls_get_addr_arg = 1;
1a0670f3 8635 /* Fall through. */
663a1470
AM
8636
8637 case R_PPC64_TLS:
8638 case R_PPC64_TOC16:
8639 case R_PPC64_TOC16_LO:
102890f0
AM
8640 if (sym_sec == NULL || sym_sec != toc)
8641 continue;
8642
8643 /* Mark this toc entry as referenced by a TLS
8644 code sequence. We can do that now in the
8645 case of R_PPC64_TLS, and after checking for
8646 tls_get_addr for the TOC16 relocs. */
8647 if (toc_ref == NULL)
663a1470
AM
8648 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8649 if (toc_ref == NULL)
8650 goto err_free_rel;
8651
102890f0
AM
8652 if (h != NULL)
8653 value = h->root.u.def.value;
8654 else
8655 value = sym->st_value;
8656 value += rel->r_addend;
73242275
AM
8657 if (value % 8 != 0)
8658 continue;
8659 BFD_ASSERT (value < toc->size
8660 && toc->output_offset % 8 == 0);
663a1470 8661 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8662 if (r_type == R_PPC64_TLS
8663 || r_type == R_PPC64_TLSGD
8664 || r_type == R_PPC64_TLSLD)
102890f0
AM
8665 {
8666 toc_ref[toc_ref_index] = 1;
8667 continue;
8668 }
8669
8670 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8671 continue;
8672
8673 tls_set = 0;
8674 tls_clear = 0;
8675 expecting_tls_get_addr = 2;
8676 break;
8677
8678 case R_PPC64_TPREL64:
8679 if (pass == 0
8680 || sec != toc
8681 || toc_ref == NULL
663a1470 8682 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8683 continue;
8684 if (ok_tprel)
8685 {
8686 /* IE -> LE */
8687 tls_set = TLS_EXPLICIT;
8688 tls_clear = TLS_TPREL;
8689 break;
8690 }
8691 continue;
8692
8693 case R_PPC64_DTPMOD64:
8694 if (pass == 0
8695 || sec != toc
8696 || toc_ref == NULL
663a1470 8697 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8698 continue;
8699 if (rel + 1 < relend
8700 && (rel[1].r_info
8701 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8702 && rel[1].r_offset == rel->r_offset + 8)
8703 {
8704 if (ok_tprel)
8705 /* GD -> LE */
8706 tls_set = TLS_EXPLICIT | TLS_GD;
8707 else
8708 /* GD -> IE */
8709 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8710 tls_clear = TLS_GD;
8711 }
8712 else
8713 {
8714 if (!is_local)
8715 continue;
8716
8717 /* LD -> LE */
8718 tls_set = TLS_EXPLICIT;
8719 tls_clear = TLS_LD;
8720 }
8721 break;
8722
8723 default:
8724 continue;
8725 }
8726
8727 if (pass == 0)
8728 {
727fc41e
AM
8729 if (!expecting_tls_get_addr
8730 || !sec->has_tls_get_addr_call)
102890f0
AM
8731 continue;
8732
3a71aa26
AM
8733 if (rel + 1 < relend
8734 && branch_reloc_hash_match (ibfd, rel + 1,
8735 htab->tls_get_addr,
8736 htab->tls_get_addr_fd))
102890f0 8737 {
3a71aa26 8738 if (expecting_tls_get_addr == 2)
102890f0 8739 {
3a71aa26 8740 /* Check for toc tls entries. */
f961d9dd 8741 unsigned char *toc_tls;
3a71aa26
AM
8742 int retval;
8743
8744 retval = get_tls_mask (&toc_tls, NULL, NULL,
8745 &locsyms,
8746 rel, ibfd);
8747 if (retval == 0)
8748 goto err_free_rel;
663a1470
AM
8749 if (toc_tls != NULL)
8750 {
8751 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8752 found_tls_get_addr_arg = 1;
8753 if (retval > 1)
8754 toc_ref[toc_ref_index] = 1;
8755 }
102890f0 8756 }
3a71aa26 8757 continue;
102890f0
AM
8758 }
8759
8760 if (expecting_tls_get_addr != 1)
8761 continue;
8762
8763 /* Uh oh, we didn't find the expected call. We
8764 could just mark this symbol to exclude it
8765 from tls optimization but it's safer to skip
663a1470 8766 the entire optimization. */
695344c0 8767 /* xgettext:c-format */
25f53a85 8768 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8769 "TLS optimization disabled\n"),
8770 ibfd, sec, rel->r_offset);
8771 ret = TRUE;
8772 goto err_free_rel;
102890f0
AM
8773 }
8774
85f7a9cb 8775 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8776 {
8777 struct plt_entry *ent;
8778 for (ent = htab->tls_get_addr->elf.plt.plist;
8779 ent != NULL;
8780 ent = ent->next)
8781 if (ent->addend == 0)
411e1bfb 8782 {
102890f0 8783 if (ent->plt.refcount > 0)
30038c59 8784 {
102890f0
AM
8785 ent->plt.refcount -= 1;
8786 expecting_tls_get_addr = 0;
30038c59 8787 }
102890f0 8788 break;
411e1bfb 8789 }
102890f0 8790 }
411e1bfb 8791
85f7a9cb 8792 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8793 {
8794 struct plt_entry *ent;
8795 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8796 ent != NULL;
8797 ent = ent->next)
8798 if (ent->addend == 0)
411e1bfb 8799 {
102890f0
AM
8800 if (ent->plt.refcount > 0)
8801 ent->plt.refcount -= 1;
8802 break;
411e1bfb 8803 }
102890f0 8804 }
411e1bfb 8805
102890f0 8806 if (tls_clear == 0)
30038c59
AM
8807 continue;
8808
102890f0
AM
8809 if ((tls_set & TLS_EXPLICIT) == 0)
8810 {
8811 struct got_entry *ent;
411e1bfb 8812
102890f0
AM
8813 /* Adjust got entry for this reloc. */
8814 if (h != NULL)
8815 ent = h->got.glist;
8816 else
8817 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8818
102890f0
AM
8819 for (; ent != NULL; ent = ent->next)
8820 if (ent->addend == rel->r_addend
8821 && ent->owner == ibfd
8822 && ent->tls_type == tls_type)
8823 break;
8824 if (ent == NULL)
8825 abort ();
411e1bfb 8826
102890f0
AM
8827 if (tls_set == 0)
8828 {
8829 /* We managed to get rid of a got entry. */
8830 if (ent->got.refcount > 0)
8831 ent->got.refcount -= 1;
8832 }
8833 }
8834 else
8835 {
8836 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8837 we'll lose one or two dyn relocs. */
8838 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8839 NULL, h, sym))
102890f0 8840 return FALSE;
411e1bfb 8841
102890f0
AM
8842 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8843 {
8844 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8845 NULL, h, sym))
102890f0
AM
8846 return FALSE;
8847 }
8848 }
411e1bfb 8849
102890f0
AM
8850 *tls_mask |= tls_set;
8851 *tls_mask &= ~tls_clear;
8852 }
8c1d1bb8 8853
102890f0
AM
8854 if (elf_section_data (sec)->relocs != relstart)
8855 free (relstart);
8856 }
411e1bfb 8857
663a1470
AM
8858 if (locsyms != NULL
8859 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8860 {
8861 if (!info->keep_memory)
8862 free (locsyms);
8863 else
8864 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8865 }
8866 }
411e1bfb 8867
663a1470
AM
8868 if (toc_ref != NULL)
8869 free (toc_ref);
b34976b6 8870 return TRUE;
1e2f5b6e 8871}
b34976b6 8872
c5614fa4
AM
8873/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8874 the values of any global symbols in a toc section that has been
8875 edited. Globals in toc sections should be a rarity, so this function
8876 sets a flag if any are found in toc sections other than the one just
8877 edited, so that futher hash table traversals can be avoided. */
8878
8879struct adjust_toc_info
8880{
8881 asection *toc;
8882 unsigned long *skip;
8883 bfd_boolean global_toc_syms;
8884};
8885
ba761f19
AM
8886enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8887
c5614fa4
AM
8888static bfd_boolean
8889adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8890{
8891 struct ppc_link_hash_entry *eh;
8892 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8893 unsigned long i;
c5614fa4 8894
c5614fa4
AM
8895 if (h->root.type != bfd_link_hash_defined
8896 && h->root.type != bfd_link_hash_defweak)
8897 return TRUE;
8898
8899 eh = (struct ppc_link_hash_entry *) h;
8900 if (eh->adjust_done)
8901 return TRUE;
8902
8903 if (eh->elf.root.u.def.section == toc_inf->toc)
8904 {
854b41e7
AM
8905 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8906 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8907 else
854b41e7
AM
8908 i = eh->elf.root.u.def.value >> 3;
8909
ba761f19 8910 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8911 {
4eca0228 8912 _bfd_error_handler
854b41e7
AM
8913 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8914 do
8915 ++i;
ba761f19 8916 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8917 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8918 }
854b41e7
AM
8919
8920 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8921 eh->adjust_done = 1;
8922 }
8923 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8924 toc_inf->global_toc_syms = TRUE;
8925
8926 return TRUE;
8927}
8928
39eeab25
AM
8929/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8930 on a _LO variety toc/got reloc. */
560c8763
AM
8931
8932static bfd_boolean
39eeab25 8933ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8934{
39eeab25
AM
8935 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8936 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8937 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8938 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8939 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8940 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8941 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8942 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8943 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8944 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8945 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8946 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8947 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8948 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8949 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8950 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8951 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8952 /* Exclude lfqu by testing reloc. If relocs are ever
8953 defined for the reduced D field in psq_lu then those
8954 will need testing too. */
8955 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8956 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8957 && (insn & 1) == 0)
8958 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8959 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8960 /* Exclude stfqu. psq_stu as above for psq_lu. */
8961 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8962 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8963 && (insn & 1) == 0));
560c8763
AM
8964}
8965
c5614fa4
AM
8966/* Examine all relocs referencing .toc sections in order to remove
8967 unused .toc entries. */
8968
8969bfd_boolean
33c0ec9d 8970ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8971{
8972 bfd *ibfd;
8973 struct adjust_toc_info toc_inf;
67f0cbdb 8974 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8975
67f0cbdb 8976 htab->do_toc_opt = 1;
c5614fa4 8977 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8978 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8979 {
8980 asection *toc, *sec;
8981 Elf_Internal_Shdr *symtab_hdr;
8982 Elf_Internal_Sym *local_syms;
425b145b 8983 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8984 unsigned long *skip, *drop;
8985 unsigned char *used;
8986 unsigned char *keep, last, some_unused;
8987
854b41e7
AM
8988 if (!is_ppc64_elf (ibfd))
8989 continue;
8990
c5614fa4
AM
8991 toc = bfd_get_section_by_name (ibfd, ".toc");
8992 if (toc == NULL
92b7a70f 8993 || toc->size == 0
dbaa2011
AM
8994 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8995 || discarded_section (toc))
c5614fa4
AM
8996 continue;
8997
425b145b 8998 toc_relocs = NULL;
c5614fa4 8999 local_syms = NULL;
0ffa91dd 9000 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
9001
9002 /* Look at sections dropped from the final link. */
9003 skip = NULL;
9004 relstart = NULL;
9005 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9006 {
9007 if (sec->reloc_count == 0
dbaa2011 9008 || !discarded_section (sec)
c5614fa4
AM
9009 || get_opd_info (sec)
9010 || (sec->flags & SEC_ALLOC) == 0
9011 || (sec->flags & SEC_DEBUGGING) != 0)
9012 continue;
9013
9014 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9015 if (relstart == NULL)
9016 goto error_ret;
9017
9018 /* Run through the relocs to see which toc entries might be
9019 unused. */
9020 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9021 {
9022 enum elf_ppc64_reloc_type r_type;
9023 unsigned long r_symndx;
9024 asection *sym_sec;
9025 struct elf_link_hash_entry *h;
9026 Elf_Internal_Sym *sym;
9027 bfd_vma val;
9028
9029 r_type = ELF64_R_TYPE (rel->r_info);
9030 switch (r_type)
9031 {
9032 default:
9033 continue;
9034
9035 case R_PPC64_TOC16:
9036 case R_PPC64_TOC16_LO:
9037 case R_PPC64_TOC16_HI:
9038 case R_PPC64_TOC16_HA:
9039 case R_PPC64_TOC16_DS:
9040 case R_PPC64_TOC16_LO_DS:
9041 break;
9042 }
9043
9044 r_symndx = ELF64_R_SYM (rel->r_info);
9045 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9046 r_symndx, ibfd))
9047 goto error_ret;
9048
9049 if (sym_sec != toc)
9050 continue;
9051
9052 if (h != NULL)
9053 val = h->root.u.def.value;
9054 else
9055 val = sym->st_value;
9056 val += rel->r_addend;
9057
9058 if (val >= toc->size)
9059 continue;
9060
9061 /* Anything in the toc ought to be aligned to 8 bytes.
9062 If not, don't mark as unused. */
9063 if (val & 7)
9064 continue;
9065
9066 if (skip == NULL)
9067 {
854b41e7 9068 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9069 if (skip == NULL)
9070 goto error_ret;
9071 }
9072
ba761f19 9073 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9074 }
9075
9076 if (elf_section_data (sec)->relocs != relstart)
9077 free (relstart);
9078 }
9079
ba761f19
AM
9080 /* For largetoc loads of address constants, we can convert
9081 . addis rx,2,addr@got@ha
9082 . ld ry,addr@got@l(rx)
9083 to
9084 . addis rx,2,addr@toc@ha
9085 . addi ry,rx,addr@toc@l
9086 when addr is within 2G of the toc pointer. This then means
9087 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9088
ba761f19
AM
9089 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9090 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9091 && toc->reloc_count != 0)
9092 {
9093 /* Read toc relocs. */
425b145b
AM
9094 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9095 info->keep_memory);
9096 if (toc_relocs == NULL)
ba761f19
AM
9097 goto error_ret;
9098
425b145b 9099 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9100 {
9101 enum elf_ppc64_reloc_type r_type;
9102 unsigned long r_symndx;
9103 asection *sym_sec;
9104 struct elf_link_hash_entry *h;
9105 Elf_Internal_Sym *sym;
9106 bfd_vma val, addr;
9107
9108 r_type = ELF64_R_TYPE (rel->r_info);
9109 if (r_type != R_PPC64_ADDR64)
9110 continue;
9111
9112 r_symndx = ELF64_R_SYM (rel->r_info);
9113 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9114 r_symndx, ibfd))
9115 goto error_ret;
9116
425b145b 9117 if (sym_sec == NULL
c27b8c2a 9118 || sym_sec->output_section == NULL
dbaa2011 9119 || discarded_section (sym_sec))
425b145b
AM
9120 continue;
9121
afe397ea 9122 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9123 continue;
9124
9125 if (h != NULL)
bddc25c9
AM
9126 {
9127 if (h->type == STT_GNU_IFUNC)
9128 continue;
9129 val = h->root.u.def.value;
9130 }
ba761f19 9131 else
bddc25c9
AM
9132 {
9133 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9134 continue;
9135 val = sym->st_value;
9136 }
ba761f19
AM
9137 val += rel->r_addend;
9138 val += sym_sec->output_section->vma + sym_sec->output_offset;
9139
9140 /* We don't yet know the exact toc pointer value, but we
9141 know it will be somewhere in the toc section. Don't
9142 optimize if the difference from any possible toc
9143 pointer is outside [ff..f80008000, 7fff7fff]. */
9144 addr = toc->output_section->vma + TOC_BASE_OFF;
9145 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9146 continue;
9147
9148 addr = toc->output_section->vma + toc->output_section->rawsize;
9149 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9150 continue;
9151
9152 if (skip == NULL)
9153 {
9154 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9155 if (skip == NULL)
9156 goto error_ret;
9157 }
9158
9159 skip[rel->r_offset >> 3]
425b145b 9160 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9161 }
ba761f19
AM
9162 }
9163
c5614fa4
AM
9164 if (skip == NULL)
9165 continue;
9166
9167 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9168 if (used == NULL)
9169 {
9170 error_ret:
9171 if (local_syms != NULL
9172 && symtab_hdr->contents != (unsigned char *) local_syms)
9173 free (local_syms);
9174 if (sec != NULL
9175 && relstart != NULL
9176 && elf_section_data (sec)->relocs != relstart)
9177 free (relstart);
425b145b
AM
9178 if (toc_relocs != NULL
9179 && elf_section_data (toc)->relocs != toc_relocs)
9180 free (toc_relocs);
c5614fa4
AM
9181 if (skip != NULL)
9182 free (skip);
9183 return FALSE;
9184 }
9185
30038c59
AM
9186 /* Now check all kept sections that might reference the toc.
9187 Check the toc itself last. */
9188 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9189 : ibfd->sections);
c5614fa4 9190 sec != NULL;
c5614fa4 9191 sec = (sec == toc ? NULL
c5614fa4 9192 : sec->next == NULL ? toc
30038c59 9193 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9194 : sec->next))
9195 {
9196 int repeat;
9197
9198 if (sec->reloc_count == 0
dbaa2011 9199 || discarded_section (sec)
c5614fa4
AM
9200 || get_opd_info (sec)
9201 || (sec->flags & SEC_ALLOC) == 0
9202 || (sec->flags & SEC_DEBUGGING) != 0)
9203 continue;
9204
854b41e7
AM
9205 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9206 info->keep_memory);
c5614fa4 9207 if (relstart == NULL)
2915c55b
JK
9208 {
9209 free (used);
9210 goto error_ret;
9211 }
c5614fa4
AM
9212
9213 /* Mark toc entries referenced as used. */
c5614fa4 9214 do
d4f1ee75
AM
9215 {
9216 repeat = 0;
9217 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9218 {
9219 enum elf_ppc64_reloc_type r_type;
9220 unsigned long r_symndx;
9221 asection *sym_sec;
9222 struct elf_link_hash_entry *h;
9223 Elf_Internal_Sym *sym;
9224 bfd_vma val;
9225 enum {no_check, check_lo, check_ha} insn_check;
98528052 9226
d4f1ee75
AM
9227 r_type = ELF64_R_TYPE (rel->r_info);
9228 switch (r_type)
9229 {
9230 default:
9231 insn_check = no_check;
9232 break;
98528052 9233
d4f1ee75
AM
9234 case R_PPC64_GOT_TLSLD16_HA:
9235 case R_PPC64_GOT_TLSGD16_HA:
9236 case R_PPC64_GOT_TPREL16_HA:
9237 case R_PPC64_GOT_DTPREL16_HA:
9238 case R_PPC64_GOT16_HA:
9239 case R_PPC64_TOC16_HA:
9240 insn_check = check_ha;
9241 break;
98528052 9242
d4f1ee75
AM
9243 case R_PPC64_GOT_TLSLD16_LO:
9244 case R_PPC64_GOT_TLSGD16_LO:
9245 case R_PPC64_GOT_TPREL16_LO_DS:
9246 case R_PPC64_GOT_DTPREL16_LO_DS:
9247 case R_PPC64_GOT16_LO:
9248 case R_PPC64_GOT16_LO_DS:
9249 case R_PPC64_TOC16_LO:
9250 case R_PPC64_TOC16_LO_DS:
9251 insn_check = check_lo;
9252 break;
9253 }
560c8763 9254
d4f1ee75
AM
9255 if (insn_check != no_check)
9256 {
9257 bfd_vma off = rel->r_offset & ~3;
9258 unsigned char buf[4];
9259 unsigned int insn;
c5614fa4 9260
d4f1ee75
AM
9261 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9262 {
9263 free (used);
9264 goto error_ret;
9265 }
9266 insn = bfd_get_32 (ibfd, buf);
9267 if (insn_check == check_lo
39eeab25 9268 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
9269 : ((insn & ((0x3f << 26) | 0x1f << 16))
9270 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9271 {
9272 char str[12];
9273
9274 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9275 sprintf (str, "%#08x", insn);
9276 info->callbacks->einfo
695344c0 9277 /* xgettext:c-format */
174d0a74 9278 (_("%H: toc optimization is not supported for"
d4f1ee75
AM
9279 " %s instruction.\n"),
9280 ibfd, sec, rel->r_offset & ~3, str);
9281 }
9282 }
c5614fa4 9283
d4f1ee75
AM
9284 switch (r_type)
9285 {
9286 case R_PPC64_TOC16:
9287 case R_PPC64_TOC16_LO:
9288 case R_PPC64_TOC16_HI:
9289 case R_PPC64_TOC16_HA:
9290 case R_PPC64_TOC16_DS:
9291 case R_PPC64_TOC16_LO_DS:
9292 /* In case we're taking addresses of toc entries. */
9293 case R_PPC64_ADDR64:
9294 break;
c5614fa4 9295
d4f1ee75
AM
9296 default:
9297 continue;
9298 }
c5614fa4 9299
d4f1ee75
AM
9300 r_symndx = ELF64_R_SYM (rel->r_info);
9301 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9302 r_symndx, ibfd))
9303 {
9304 free (used);
9305 goto error_ret;
9306 }
c5614fa4 9307
d4f1ee75
AM
9308 if (sym_sec != toc)
9309 continue;
c5614fa4 9310
d4f1ee75
AM
9311 if (h != NULL)
9312 val = h->root.u.def.value;
9313 else
9314 val = sym->st_value;
9315 val += rel->r_addend;
ba761f19 9316
d4f1ee75
AM
9317 if (val >= toc->size)
9318 continue;
ba761f19 9319
d4f1ee75
AM
9320 if ((skip[val >> 3] & can_optimize) != 0)
9321 {
9322 bfd_vma off;
9323 unsigned char opc;
9324
9325 switch (r_type)
9326 {
9327 case R_PPC64_TOC16_HA:
ba761f19 9328 break;
ba761f19 9329
d4f1ee75
AM
9330 case R_PPC64_TOC16_LO_DS:
9331 off = rel->r_offset;
9332 off += (bfd_big_endian (ibfd) ? -2 : 3);
9333 if (!bfd_get_section_contents (ibfd, sec, &opc,
9334 off, 1))
9335 {
9336 free (used);
9337 goto error_ret;
9338 }
9339 if ((opc & (0x3f << 2)) == (58u << 2))
9340 break;
1a0670f3 9341 /* Fall through. */
ba761f19 9342
d4f1ee75
AM
9343 default:
9344 /* Wrong sort of reloc, or not a ld. We may
9345 as well clear ref_from_discarded too. */
9346 skip[val >> 3] = 0;
9347 }
9348 }
9349
9350 if (sec != toc)
9351 used[val >> 3] = 1;
9352 /* For the toc section, we only mark as used if this
9353 entry itself isn't unused. */
9354 else if ((used[rel->r_offset >> 3]
9355 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9356 && !used[val >> 3])
9357 {
9358 /* Do all the relocs again, to catch reference
9359 chains. */
9360 repeat = 1;
9361 used[val >> 3] = 1;
9362 }
9363 }
9364 }
c5614fa4 9365 while (repeat);
854b41e7
AM
9366
9367 if (elf_section_data (sec)->relocs != relstart)
9368 free (relstart);
c5614fa4
AM
9369 }
9370
9371 /* Merge the used and skip arrays. Assume that TOC
9372 doublewords not appearing as either used or unused belong
9373 to to an entry more than one doubleword in size. */
9374 for (drop = skip, keep = used, last = 0, some_unused = 0;
9375 drop < skip + (toc->size + 7) / 8;
9376 ++drop, ++keep)
9377 {
9378 if (*keep)
9379 {
ba761f19
AM
9380 *drop &= ~ref_from_discarded;
9381 if ((*drop & can_optimize) != 0)
9382 some_unused = 1;
c5614fa4
AM
9383 last = 0;
9384 }
b140b010 9385 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9386 {
9387 some_unused = 1;
ba761f19 9388 last = ref_from_discarded;
c5614fa4
AM
9389 }
9390 else
9391 *drop = last;
9392 }
9393
9394 free (used);
9395
9396 if (some_unused)
9397 {
9398 bfd_byte *contents, *src;
9399 unsigned long off;
d62b3684 9400 Elf_Internal_Sym *sym;
ba761f19 9401 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9402
9403 /* Shuffle the toc contents, and at the same time convert the
9404 skip array from booleans into offsets. */
9405 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9406 goto error_ret;
9407
9408 elf_section_data (toc)->this_hdr.contents = contents;
9409
9410 for (src = contents, off = 0, drop = skip;
9411 src < contents + toc->size;
9412 src += 8, ++drop)
9413 {
ba761f19
AM
9414 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9415 off += 8;
c5614fa4
AM
9416 else if (off != 0)
9417 {
9418 *drop = off;
9419 memcpy (src - off, src, 8);
9420 }
9421 }
854b41e7 9422 *drop = off;
c5614fa4
AM
9423 toc->rawsize = toc->size;
9424 toc->size = src - contents - off;
9425
ba761f19
AM
9426 /* Adjust addends for relocs against the toc section sym,
9427 and optimize any accesses we can. */
c5614fa4
AM
9428 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9429 {
9430 if (sec->reloc_count == 0
dbaa2011 9431 || discarded_section (sec))
c5614fa4
AM
9432 continue;
9433
9434 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9435 info->keep_memory);
c5614fa4
AM
9436 if (relstart == NULL)
9437 goto error_ret;
9438
9439 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9440 {
9441 enum elf_ppc64_reloc_type r_type;
9442 unsigned long r_symndx;
9443 asection *sym_sec;
9444 struct elf_link_hash_entry *h;
854b41e7 9445 bfd_vma val;
c5614fa4
AM
9446
9447 r_type = ELF64_R_TYPE (rel->r_info);
9448 switch (r_type)
9449 {
9450 default:
9451 continue;
9452
9453 case R_PPC64_TOC16:
9454 case R_PPC64_TOC16_LO:
9455 case R_PPC64_TOC16_HI:
9456 case R_PPC64_TOC16_HA:
9457 case R_PPC64_TOC16_DS:
9458 case R_PPC64_TOC16_LO_DS:
9459 case R_PPC64_ADDR64:
9460 break;
9461 }
9462
9463 r_symndx = ELF64_R_SYM (rel->r_info);
9464 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9465 r_symndx, ibfd))
9466 goto error_ret;
9467
ba761f19 9468 if (sym_sec != toc)
c5614fa4
AM
9469 continue;
9470
ba761f19
AM
9471 if (h != NULL)
9472 val = h->root.u.def.value;
9473 else
9474 {
9475 val = sym->st_value;
9476 if (val != 0)
9477 local_toc_syms = TRUE;
9478 }
9479
9480 val += rel->r_addend;
854b41e7
AM
9481
9482 if (val > toc->rawsize)
9483 val = toc->rawsize;
ba761f19
AM
9484 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9485 continue;
9486 else if ((skip[val >> 3] & can_optimize) != 0)
9487 {
9488 Elf_Internal_Rela *tocrel
425b145b 9489 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9490 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9491
9492 switch (r_type)
9493 {
9494 case R_PPC64_TOC16_HA:
9495 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9496 break;
9497
9498 case R_PPC64_TOC16_LO_DS:
9499 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9500 break;
9501
9502 default:
28942f62
AM
9503 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9504 ppc_howto_init ();
b140b010 9505 info->callbacks->einfo
695344c0 9506 /* xgettext:c-format */
174d0a74 9507 (_("%H: %s references "
b140b010
AM
9508 "optimized away TOC entry\n"),
9509 ibfd, sec, rel->r_offset,
9510 ppc64_elf_howto_table[r_type]->name);
9511 bfd_set_error (bfd_error_bad_value);
9512 goto error_ret;
ba761f19
AM
9513 }
9514 rel->r_addend = tocrel->r_addend;
9515 elf_section_data (sec)->relocs = relstart;
9516 continue;
9517 }
9518
9519 if (h != NULL || sym->st_value != 0)
9520 continue;
854b41e7
AM
9521
9522 rel->r_addend -= skip[val >> 3];
9523 elf_section_data (sec)->relocs = relstart;
c5614fa4 9524 }
854b41e7
AM
9525
9526 if (elf_section_data (sec)->relocs != relstart)
9527 free (relstart);
c5614fa4
AM
9528 }
9529
9530 /* We shouldn't have local or global symbols defined in the TOC,
9531 but handle them anyway. */
df22d223
AM
9532 if (local_syms != NULL)
9533 for (sym = local_syms;
9534 sym < local_syms + symtab_hdr->sh_info;
9535 ++sym)
9536 if (sym->st_value != 0
9537 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9538 {
9539 unsigned long i;
854b41e7 9540
df22d223
AM
9541 if (sym->st_value > toc->rawsize)
9542 i = toc->rawsize >> 3;
9543 else
9544 i = sym->st_value >> 3;
854b41e7 9545
df22d223
AM
9546 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9547 {
9548 if (local_toc_syms)
4eca0228 9549 _bfd_error_handler
df22d223
AM
9550 (_("%s defined on removed toc entry"),
9551 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9552 do
9553 ++i;
9554 while ((skip[i] & (ref_from_discarded | can_optimize)));
9555 sym->st_value = (bfd_vma) i << 3;
9556 }
d62b3684 9557
df22d223
AM
9558 sym->st_value -= skip[i];
9559 symtab_hdr->contents = (unsigned char *) local_syms;
9560 }
c5614fa4 9561
854b41e7 9562 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9563 if (toc_inf.global_toc_syms)
9564 {
9565 toc_inf.toc = toc;
9566 toc_inf.skip = skip;
9567 toc_inf.global_toc_syms = FALSE;
9568 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9569 &toc_inf);
9570 }
854b41e7
AM
9571
9572 if (toc->reloc_count != 0)
9573 {
d4730f92 9574 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9575 Elf_Internal_Rela *wrel;
9576 bfd_size_type sz;
9577
854b41e7 9578 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9579 if (toc_relocs == NULL)
9580 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9581 info->keep_memory);
9582 if (toc_relocs == NULL)
9583 goto error_ret;
9584
425b145b
AM
9585 wrel = toc_relocs;
9586 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9587 if ((skip[rel->r_offset >> 3]
9588 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9589 {
9590 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9591 wrel->r_info = rel->r_info;
9592 wrel->r_addend = rel->r_addend;
9593 ++wrel;
9594 }
9595 else if (!dec_dynrel_count (rel->r_info, toc, info,
9596 &local_syms, NULL, NULL))
9597 goto error_ret;
9598
425b145b
AM
9599 elf_section_data (toc)->relocs = toc_relocs;
9600 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9601 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9602 sz = rel_hdr->sh_entsize;
9603 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9604 }
c5614fa4 9605 }
28be611c
AM
9606 else if (toc_relocs != NULL
9607 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9608 free (toc_relocs);
c5614fa4
AM
9609
9610 if (local_syms != NULL
9611 && symtab_hdr->contents != (unsigned char *) local_syms)
9612 {
9613 if (!info->keep_memory)
9614 free (local_syms);
9615 else
9616 symtab_hdr->contents = (unsigned char *) local_syms;
9617 }
9618 free (skip);
9619 }
9620
9621 return TRUE;
9622}
9623
1bbe0902
AM
9624/* Return true iff input section I references the TOC using
9625 instructions limited to +/-32k offsets. */
9626
9627bfd_boolean
9628ppc64_elf_has_small_toc_reloc (asection *i)
9629{
9630 return (is_ppc64_elf (i->owner)
9631 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9632}
9633
927be08e
AM
9634/* Allocate space for one GOT entry. */
9635
9636static void
9637allocate_got (struct elf_link_hash_entry *h,
9638 struct bfd_link_info *info,
9639 struct got_entry *gent)
9640{
9641 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
9642 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9643 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9644 ? 16 : 8);
9645 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9646 ? 2 : 1) * sizeof (Elf64_External_Rela);
9647 asection *got = ppc64_elf_tdata (gent->owner)->got;
9648
9649 gent->got.offset = got->size;
9650 got->size += entsize;
9651
19e08130 9652 if (h->type == STT_GNU_IFUNC)
927be08e 9653 {
33e44f2e 9654 htab->elf.irelplt->size += rentsize;
19e08130 9655 htab->got_reli_size += rentsize;
927be08e 9656 }
0e1862bb 9657 else if ((bfd_link_pic (info)
f0158f44
AM
9658 || (htab->elf.dynamic_sections_created
9659 && h->dynindx != -1
9660 && !SYMBOL_REFERENCES_LOCAL (info, h)))
19e08130
AM
9661 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9662 || h->root.type != bfd_link_hash_undefweak))
927be08e 9663 {
19e08130 9664 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9665 relgot->size += rentsize;
927be08e
AM
9666 }
9667}
9668
7865406b
AM
9669/* This function merges got entries in the same toc group. */
9670
9671static void
9672merge_got_entries (struct got_entry **pent)
9673{
9674 struct got_entry *ent, *ent2;
9675
9676 for (ent = *pent; ent != NULL; ent = ent->next)
9677 if (!ent->is_indirect)
9678 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9679 if (!ent2->is_indirect
9680 && ent2->addend == ent->addend
9681 && ent2->tls_type == ent->tls_type
9682 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9683 {
9684 ent2->is_indirect = TRUE;
9685 ent2->got.ent = ent;
9686 }
9687}
9688
f0158f44
AM
9689/* If H is undefined weak, make it dynamic if that makes sense. */
9690
9691static bfd_boolean
9692ensure_undefweak_dynamic (struct bfd_link_info *info,
9693 struct elf_link_hash_entry *h)
9694{
9695 struct elf_link_hash_table *htab = elf_hash_table (info);
9696
9697 if (htab->dynamic_sections_created
9698 && h->root.type == bfd_link_hash_undefweak
9699 && h->dynindx == -1
9700 && !h->forced_local
9701 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9702 return bfd_elf_link_record_dynamic_symbol (info, h);
9703 return TRUE;
9704}
9705
65f38f15
AM
9706/* Allocate space in .plt, .got and associated reloc sections for
9707 dynamic relocs. */
5bd4f169 9708
b34976b6 9709static bfd_boolean
4ce794b7 9710allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9711{
65f38f15
AM
9712 struct bfd_link_info *info;
9713 struct ppc_link_hash_table *htab;
5bd4f169 9714 asection *s;
65f38f15 9715 struct ppc_link_hash_entry *eh;
0b8bcf0d 9716 struct got_entry **pgent, *gent;
5bd4f169 9717
e92d460e 9718 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9719 return TRUE;
5bd4f169 9720
65f38f15
AM
9721 info = (struct bfd_link_info *) inf;
9722 htab = ppc_hash_table (info);
4dfe6ac6
NC
9723 if (htab == NULL)
9724 return FALSE;
5bd4f169 9725
951fd09b
AM
9726 eh = (struct ppc_link_hash_entry *) h;
9727 /* Run through the TLS GD got entries first if we're changing them
9728 to TPREL. */
e7b938ca 9729 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9730 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9731 if (gent->got.refcount > 0
9732 && (gent->tls_type & TLS_GD) != 0)
9733 {
9734 /* This was a GD entry that has been converted to TPREL. If
9735 there happens to be a TPREL entry we can use that one. */
9736 struct got_entry *ent;
9737 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9738 if (ent->got.refcount > 0
9739 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9740 && ent->addend == gent->addend
9741 && ent->owner == gent->owner)
951fd09b
AM
9742 {
9743 gent->got.refcount = 0;
9744 break;
9745 }
9746
9747 /* If not, then we'll be using our own TPREL entry. */
9748 if (gent->got.refcount != 0)
9749 gent->tls_type = TLS_TLS | TLS_TPREL;
9750 }
9751
7865406b
AM
9752 /* Remove any list entry that won't generate a word in the GOT before
9753 we call merge_got_entries. Otherwise we risk merging to empty
9754 entries. */
0b8bcf0d
AM
9755 pgent = &h->got.glist;
9756 while ((gent = *pgent) != NULL)
411e1bfb 9757 if (gent->got.refcount > 0)
7865406b
AM
9758 {
9759 if ((gent->tls_type & TLS_LD) != 0
9760 && !h->def_dynamic)
9761 {
9762 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9763 *pgent = gent->next;
9764 }
9765 else
9766 pgent = &gent->next;
9767 }
9768 else
9769 *pgent = gent->next;
9770
9771 if (!htab->do_multi_toc)
9772 merge_got_entries (&h->got.glist);
9773
9774 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9775 if (!gent->is_indirect)
411e1bfb
AM
9776 {
9777 /* Make sure this symbol is output as a dynamic symbol.
f0158f44
AM
9778 Undefined weak syms won't yet be marked as dynamic. */
9779 if (!ensure_undefweak_dynamic (info, h))
9780 return FALSE;
65f38f15 9781
0c8d6e5c 9782 if (!is_ppc64_elf (gent->owner))
927be08e 9783 abort ();
0ffa91dd 9784
927be08e 9785 allocate_got (h, info, gent);
411e1bfb 9786 }
65f38f15 9787
8a2058b5
AM
9788 if (!htab->elf.dynamic_sections_created
9789 && h->type != STT_GNU_IFUNC)
9790 eh->dyn_relocs = NULL;
9791
9792 if (eh->dyn_relocs != NULL)
65f38f15 9793 {
8a2058b5
AM
9794 struct elf_dyn_relocs *p, **pp;
9795
57e7d118
AM
9796 /* In the shared -Bsymbolic case, discard space allocated for
9797 dynamic pc-relative relocs against symbols which turn out to
9798 be defined in regular objects. For the normal shared case,
9799 discard space for relocs that have become local due to symbol
9800 visibility changes. */
9801
9802 if (bfd_link_pic (info))
65f38f15 9803 {
57e7d118
AM
9804 /* Relocs that use pc_count are those that appear on a call
9805 insn, or certain REL relocs (see must_be_dyn_reloc) that
9806 can be generated via assembly. We want calls to
9807 protected symbols to resolve directly to the function
9808 rather than going via the plt. If people want function
9809 pointer comparisons to work as expected then they should
9810 avoid writing weird assembly. */
9811 if (SYMBOL_CALLS_LOCAL (info, h))
9812 {
57e7d118
AM
9813 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9814 {
9815 p->count -= p->pc_count;
9816 p->pc_count = 0;
9817 if (p->count == 0)
9818 *pp = p->next;
9819 else
9820 pp = &p->next;
9821 }
9822 }
65f38f15 9823
57e7d118
AM
9824 /* Also discard relocs on undefined weak syms with
9825 non-default visibility. */
9826 if (eh->dyn_relocs != NULL
9827 && h->root.type == bfd_link_hash_undefweak)
5bd4f169 9828 {
57e7d118
AM
9829 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9830 eh->dyn_relocs = NULL;
9831
9832 /* Make sure this symbol is output as a dynamic symbol.
9833 Undefined weak syms won't yet be marked as dynamic. */
f0158f44
AM
9834 else if (!ensure_undefweak_dynamic (info, h))
9835 return FALSE;
5bd4f169 9836 }
65f38f15 9837 }
57e7d118 9838 else if (h->type == STT_GNU_IFUNC)
dfbb6ac9 9839 {
8a2058b5
AM
9840 /* A plt entry is always created when making direct calls to
9841 an ifunc, even when building a static executable, but
9842 that doesn't cover all cases. We may have only an ifunc
9843 initialised function pointer for a given ifunc symbol.
9844
9845 For ELFv2, dynamic relocations are not required when
9846 generating a global entry PLT stub. */
9847 if (abiversion (info->output_bfd) >= 2)
9848 {
9849 if (global_entry_stub (h))
9850 eh->dyn_relocs = NULL;
9851 }
9852
9853 /* For ELFv1 we have function descriptors. Descriptors need
9854 to be treated like PLT entries and thus have dynamic
9855 relocations. One exception is when the function
9856 descriptor is copied into .dynbss (which should only
9857 happen with ancient versions of gcc). */
9858 else if (h->needs_copy)
dfbb6ac9 9859 eh->dyn_relocs = NULL;
57e7d118
AM
9860 }
9861 else if (ELIMINATE_COPY_RELOCS)
9862 {
8a2058b5 9863 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9864 symbols which turn out to need copy relocs or are not
9865 dynamic. */
f0158f44
AM
9866 if (!h->non_got_ref
9867 && !h->def_regular)
9868 {
9869 /* Make sure this symbol is output as a dynamic symbol.
9870 Undefined weak syms won't yet be marked as dynamic. */
9871 if (!ensure_undefweak_dynamic (info, h))
9872 return FALSE;
dfbb6ac9 9873
f0158f44
AM
9874 if (h->dynindx == -1)
9875 eh->dyn_relocs = NULL;
9876 }
9877 else
8a2058b5 9878 eh->dyn_relocs = NULL;
57e7d118
AM
9879 }
9880
9881 /* Finally, allocate space. */
9882 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9883 {
9884 asection *sreloc = elf_section_data (p->sec)->sreloc;
9885 if (eh->elf.type == STT_GNU_IFUNC)
9886 sreloc = htab->elf.irelplt;
9887 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9888 }
65f38f15 9889 }
57e7d118
AM
9890
9891 if ((htab->elf.dynamic_sections_created
f0158f44 9892 && h->dynindx != -1)
57e7d118 9893 || h->type == STT_GNU_IFUNC)
65f38f15 9894 {
57e7d118
AM
9895 struct plt_entry *pent;
9896 bfd_boolean doneone = FALSE;
9897 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9898 if (pent->plt.refcount > 0)
9899 {
9900 if (!htab->elf.dynamic_sections_created
9901 || h->dynindx == -1)
9902 {
9903 s = htab->elf.iplt;
9904 pent->plt.offset = s->size;
9905 s->size += PLT_ENTRY_SIZE (htab);
9906 s = htab->elf.irelplt;
9907 }
9908 else
9909 {
9910 /* If this is the first .plt entry, make room for the special
9911 first entry. */
9912 s = htab->elf.splt;
9913 if (s->size == 0)
9914 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9915
57e7d118 9916 pent->plt.offset = s->size;
65f38f15 9917
57e7d118
AM
9918 /* Make room for this entry. */
9919 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9920
57e7d118
AM
9921 /* Make room for the .glink code. */
9922 s = htab->glink;
9923 if (s->size == 0)
9924 s->size += GLINK_CALL_STUB_SIZE;
9925 if (htab->opd_abi)
9926 {
9927 /* We need bigger stubs past index 32767. */
9928 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9929 s->size += 4;
9930 s->size += 2*4;
9931 }
9932 else
9933 s->size += 4;
65f38f15 9934
57e7d118
AM
9935 /* We also need to make an entry in the .rela.plt section. */
9936 s = htab->elf.srelplt;
9937 }
9938 s->size += sizeof (Elf64_External_Rela);
9939 doneone = TRUE;
9940 }
9941 else
9942 pent->plt.offset = (bfd_vma) -1;
9943 if (!doneone)
9944 {
9945 h->plt.plist = NULL;
9946 h->needs_plt = 0;
9947 }
65f38f15 9948 }
57e7d118 9949 else
65f38f15 9950 {
57e7d118
AM
9951 h->plt.plist = NULL;
9952 h->needs_plt = 0;
65f38f15
AM
9953 }
9954
b34976b6 9955 return TRUE;
65f38f15
AM
9956}
9957
a345bc8d
AM
9958/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9959 to set up space for global entry stubs. These are put in glink,
9960 after the branch table. */
65f38f15 9961
b34976b6 9962static bfd_boolean
a345bc8d 9963size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9964{
a345bc8d
AM
9965 struct bfd_link_info *info;
9966 struct ppc_link_hash_table *htab;
9967 struct plt_entry *pent;
9968 asection *s;
65f38f15 9969
a345bc8d
AM
9970 if (h->root.type == bfd_link_hash_indirect)
9971 return TRUE;
65f38f15 9972
a345bc8d
AM
9973 if (!h->pointer_equality_needed)
9974 return TRUE;
65f38f15 9975
a345bc8d
AM
9976 if (h->def_regular)
9977 return TRUE;
65f38f15 9978
a345bc8d
AM
9979 info = inf;
9980 htab = ppc_hash_table (info);
9981 if (htab == NULL)
9982 return FALSE;
9983
9984 s = htab->glink;
9985 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9986 if (pent->plt.offset != (bfd_vma) -1
9987 && pent->addend == 0)
9988 {
afe397ea
AM
9989 /* For ELFv2, if this symbol is not defined in a regular file
9990 and we are not generating a shared library or pie, then we
9991 need to define the symbol in the executable on a call stub.
9992 This is to avoid text relocations. */
a345bc8d 9993 s->size = (s->size + 15) & -16;
8a2058b5 9994 h->root.type = bfd_link_hash_defined;
afe397ea
AM
9995 h->root.u.def.section = s;
9996 h->root.u.def.value = s->size;
a345bc8d
AM
9997 s->size += 16;
9998 break;
9999 }
10000 return TRUE;
10001}
10002
10003/* Set DF_TEXTREL if we find any dynamic relocs that apply to
10004 read-only sections. */
10005
10006static bfd_boolean
10007maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
10008{
10009 if (h->root.type == bfd_link_hash_indirect)
10010 return TRUE;
10011
10012 if (readonly_dynrelocs (h))
10013 {
10014 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
10015
10016 /* Not an error, just cut short the traversal. */
10017 return FALSE;
65f38f15 10018 }
b34976b6 10019 return TRUE;
65f38f15
AM
10020}
10021
10022/* Set the sizes of the dynamic sections. */
10023
b34976b6 10024static bfd_boolean
ee67d69a 10025ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 10026 struct bfd_link_info *info)
65f38f15
AM
10027{
10028 struct ppc_link_hash_table *htab;
10029 bfd *dynobj;
10030 asection *s;
b34976b6 10031 bfd_boolean relocs;
65f38f15 10032 bfd *ibfd;
7865406b 10033 struct got_entry *first_tlsld;
65f38f15
AM
10034
10035 htab = ppc_hash_table (info);
4dfe6ac6
NC
10036 if (htab == NULL)
10037 return FALSE;
10038
65f38f15
AM
10039 dynobj = htab->elf.dynobj;
10040 if (dynobj == NULL)
10041 abort ();
10042
10043 if (htab->elf.dynamic_sections_created)
10044 {
10045 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10046 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10047 {
3d4d4302 10048 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10049 if (s == NULL)
10050 abort ();
eea6121a 10051 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10052 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10053 }
10054 }
10055
10056 /* Set up .got offsets for local syms, and space for local dynamic
10057 relocs. */
c72f2fb2 10058 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10059 {
411e1bfb
AM
10060 struct got_entry **lgot_ents;
10061 struct got_entry **end_lgot_ents;
e054468f
AM
10062 struct plt_entry **local_plt;
10063 struct plt_entry **end_local_plt;
f961d9dd 10064 unsigned char *lgot_masks;
65f38f15
AM
10065 bfd_size_type locsymcount;
10066 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10067
0c8d6e5c 10068 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10069 continue;
10070
10071 for (s = ibfd->sections; s != NULL; s = s->next)
10072 {
19e08130 10073 struct ppc_dyn_relocs *p;
65f38f15 10074
6edfbbad 10075 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10076 {
ec338859
AM
10077 if (!bfd_is_abs_section (p->sec)
10078 && bfd_is_abs_section (p->sec->output_section))
10079 {
10080 /* Input section has been discarded, either because
10081 it is a copy of a linkonce section or due to
10082 linker script /DISCARD/, so we'll be discarding
10083 the relocs too. */
10084 }
248866a8 10085 else if (p->count != 0)
ec338859 10086 {
19e08130
AM
10087 asection *srel = elf_section_data (p->sec)->sreloc;
10088 if (p->ifunc)
33e44f2e 10089 srel = htab->elf.irelplt;
eea6121a 10090 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10091 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10092 info->flags |= DF_TEXTREL;
ec338859 10093 }
65f38f15
AM
10094 }
10095 }
10096
411e1bfb
AM
10097 lgot_ents = elf_local_got_ents (ibfd);
10098 if (!lgot_ents)
65f38f15
AM
10099 continue;
10100
0ffa91dd 10101 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10102 locsymcount = symtab_hdr->sh_info;
411e1bfb 10103 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10104 local_plt = (struct plt_entry **) end_lgot_ents;
10105 end_local_plt = local_plt + locsymcount;
f961d9dd 10106 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10107 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10108 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10109 {
0b8bcf0d 10110 struct got_entry **pent, *ent;
411e1bfb 10111
0b8bcf0d
AM
10112 pent = lgot_ents;
10113 while ((ent = *pent) != NULL)
411e1bfb
AM
10114 if (ent->got.refcount > 0)
10115 {
e7b938ca 10116 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10117 {
927be08e 10118 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10119 *pent = ent->next;
411e1bfb
AM
10120 }
10121 else
10122 {
19e08130
AM
10123 unsigned int ent_size = 8;
10124 unsigned int rel_size = sizeof (Elf64_External_Rela);
10125
eea6121a 10126 ent->got.offset = s->size;
e7b938ca 10127 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10128 {
19e08130
AM
10129 ent_size *= 2;
10130 rel_size *= 2;
10131 }
10132 s->size += ent_size;
10133 if ((*lgot_masks & PLT_IFUNC) != 0)
10134 {
33e44f2e 10135 htab->elf.irelplt->size += rel_size;
19e08130
AM
10136 htab->got_reli_size += rel_size;
10137 }
0e1862bb 10138 else if (bfd_link_pic (info))
19e08130
AM
10139 {
10140 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10141 srel->size += rel_size;
927be08e 10142 }
0b8bcf0d 10143 pent = &ent->next;
411e1bfb
AM
10144 }
10145 }
10146 else
0b8bcf0d 10147 *pent = ent->next;
65f38f15 10148 }
e054468f
AM
10149
10150 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10151 for (; local_plt < end_local_plt; ++local_plt)
10152 {
10153 struct plt_entry *ent;
10154
10155 for (ent = *local_plt; ent != NULL; ent = ent->next)
10156 if (ent->plt.refcount > 0)
10157 {
33e44f2e 10158 s = htab->elf.iplt;
e054468f 10159 ent->plt.offset = s->size;
b9e5796b 10160 s->size += PLT_ENTRY_SIZE (htab);
e054468f 10161
33e44f2e 10162 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
10163 }
10164 else
10165 ent->plt.offset = (bfd_vma) -1;
10166 }
65f38f15
AM
10167 }
10168
10169 /* Allocate global sym .plt and .got entries, and space for global
10170 sym dynamic relocs. */
4ce794b7 10171 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d
AM
10172 /* Stash the end of glink branch table. */
10173 if (htab->glink != NULL)
10174 htab->glink->rawsize = htab->glink->size;
10175
0e1862bb 10176 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10177 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10178
7865406b 10179 first_tlsld = NULL;
c72f2fb2 10180 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10181 {
7865406b
AM
10182 struct got_entry *ent;
10183
0c8d6e5c 10184 if (!is_ppc64_elf (ibfd))
102890f0
AM
10185 continue;
10186
7865406b
AM
10187 ent = ppc64_tlsld_got (ibfd);
10188 if (ent->got.refcount > 0)
102890f0 10189 {
7865406b 10190 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10191 {
7865406b
AM
10192 ent->is_indirect = TRUE;
10193 ent->got.ent = first_tlsld;
10194 }
10195 else
10196 {
10197 if (first_tlsld == NULL)
10198 first_tlsld = ent;
10199 s = ppc64_elf_tdata (ibfd)->got;
10200 ent->got.offset = s->size;
10201 ent->owner = ibfd;
10202 s->size += 16;
0e1862bb 10203 if (bfd_link_pic (info))
7865406b
AM
10204 {
10205 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10206 srel->size += sizeof (Elf64_External_Rela);
10207 }
102890f0
AM
10208 }
10209 }
10210 else
7865406b 10211 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10212 }
10213
65f38f15
AM
10214 /* We now have determined the sizes of the various dynamic sections.
10215 Allocate memory for them. */
b34976b6 10216 relocs = FALSE;
65f38f15
AM
10217 for (s = dynobj->sections; s != NULL; s = s->next)
10218 {
10219 if ((s->flags & SEC_LINKER_CREATED) == 0)
10220 continue;
10221
4ce794b7 10222 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10223 /* These haven't been allocated yet; don't strip. */
10224 continue;
33e44f2e
AM
10225 else if (s == htab->elf.sgot
10226 || s == htab->elf.splt
10227 || s == htab->elf.iplt
c456f082 10228 || s == htab->glink
5474d94f
AM
10229 || s == htab->elf.sdynbss
10230 || s == htab->elf.sdynrelro)
65f38f15
AM
10231 {
10232 /* Strip this section if we don't need it; see the
10233 comment below. */
5bd4f169 10234 }
58d180e8
AM
10235 else if (s == htab->glink_eh_frame)
10236 {
10237 if (!bfd_is_abs_section (s->output_section))
10238 /* Not sized yet. */
10239 continue;
10240 }
70cc837d 10241 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10242 {
c456f082 10243 if (s->size != 0)
5bd4f169 10244 {
33e44f2e 10245 if (s != htab->elf.srelplt)
b34976b6 10246 relocs = TRUE;
5bd4f169
AM
10247
10248 /* We use the reloc_count field as a counter if we need
10249 to copy relocs into the output file. */
10250 s->reloc_count = 0;
10251 }
10252 }
65f38f15 10253 else
5bd4f169
AM
10254 {
10255 /* It's not one of our sections, so don't allocate space. */
10256 continue;
10257 }
10258
eea6121a 10259 if (s->size == 0)
5bd4f169 10260 {
c456f082
AM
10261 /* If we don't need this section, strip it from the
10262 output file. This is mostly to handle .rela.bss and
10263 .rela.plt. We must create both sections in
10264 create_dynamic_sections, because they must be created
10265 before the linker maps input sections to output
10266 sections. The linker does that before
10267 adjust_dynamic_symbol is called, and it is that
10268 function which decides whether anything needs to go
10269 into these sections. */
8423293d 10270 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10271 continue;
10272 }
10273
c456f082 10274 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10275 continue;
10276
65f38f15
AM
10277 /* Allocate memory for the section contents. We use bfd_zalloc
10278 here in case unused entries are not reclaimed before the
10279 section's contents are written out. This should not happen,
411e1bfb
AM
10280 but this way if it does we get a R_PPC64_NONE reloc in .rela
10281 sections instead of garbage.
10282 We also rely on the section contents being zero when writing
5474d94f 10283 the GOT and .dynrelro. */
eea6121a 10284 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10285 if (s->contents == NULL)
b34976b6 10286 return FALSE;
5bd4f169
AM
10287 }
10288
c72f2fb2 10289 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10290 {
0c8d6e5c 10291 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10292 continue;
10293
e717da7e 10294 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10295 if (s != NULL && s != htab->elf.sgot)
e717da7e 10296 {
eea6121a 10297 if (s->size == 0)
8423293d 10298 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10299 else
10300 {
eea6121a 10301 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10302 if (s->contents == NULL)
10303 return FALSE;
10304 }
10305 }
10306 s = ppc64_elf_tdata (ibfd)->relgot;
10307 if (s != NULL)
10308 {
eea6121a 10309 if (s->size == 0)
8423293d 10310 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10311 else
10312 {
eea6121a 10313 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10314 if (s->contents == NULL)
10315 return FALSE;
10316 relocs = TRUE;
10317 s->reloc_count = 0;
10318 }
10319 }
10320 }
10321
e86ce104 10322 if (htab->elf.dynamic_sections_created)
5bd4f169 10323 {
e8910a83
AM
10324 bfd_boolean tls_opt;
10325
5bd4f169
AM
10326 /* Add some entries to the .dynamic section. We fill in the
10327 values later, in ppc64_elf_finish_dynamic_sections, but we
10328 must add the entries now so that we get the correct size for
10329 the .dynamic section. The DT_DEBUG entry is filled in by the
10330 dynamic linker and used by the debugger. */
dc810e39 10331#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10332 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10333
0e1862bb 10334 if (bfd_link_executable (info))
5bd4f169 10335 {
dc810e39 10336 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10337 return FALSE;
5bd4f169
AM
10338 }
10339
33e44f2e 10340 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10341 {
dc810e39
AM
10342 if (!add_dynamic_entry (DT_PLTGOT, 0)
10343 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10344 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10345 || !add_dynamic_entry (DT_JMPREL, 0)
10346 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10347 return FALSE;
5bd4f169
AM
10348 }
10349
ee67d69a 10350 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10351 {
10352 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10353 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10354 return FALSE;
19397422
AM
10355 }
10356
7c9cf415 10357 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10358 && htab->tls_get_addr_fd != NULL
10359 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10360 if (tls_opt || !htab->opd_abi)
10361 {
10362 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10363 return FALSE;
10364 }
a7f2871e 10365
5bd4f169
AM
10366 if (relocs)
10367 {
dc810e39
AM
10368 if (!add_dynamic_entry (DT_RELA, 0)
10369 || !add_dynamic_entry (DT_RELASZ, 0)
10370 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10371 return FALSE;
5bd4f169 10372
65f38f15
AM
10373 /* If any dynamic relocs apply to a read-only section,
10374 then we need a DT_TEXTREL entry. */
248866a8 10375 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10376 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10377
65f38f15 10378 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10379 {
65f38f15 10380 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10381 return FALSE;
5bd4f169 10382 }
5bd4f169 10383 }
5bd4f169 10384 }
65f38f15 10385#undef add_dynamic_entry
5bd4f169 10386
b34976b6 10387 return TRUE;
5bd4f169
AM
10388}
10389
a345bc8d
AM
10390/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10391
10392static bfd_boolean
10393ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10394{
10395 if (h->plt.plist != NULL
10396 && !h->def_regular
10397 && !h->pointer_equality_needed)
10398 return FALSE;
10399
10400 return _bfd_elf_hash_symbol (h);
10401}
10402
721956f4 10403/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10404
4ce794b7
AM
10405static inline enum ppc_stub_type
10406ppc_type_of_stub (asection *input_sec,
10407 const Elf_Internal_Rela *rel,
10408 struct ppc_link_hash_entry **hash,
e054468f 10409 struct plt_entry **plt_ent,
6911b7dc
AM
10410 bfd_vma destination,
10411 unsigned long local_off)
5bd4f169 10412{
721956f4
AM
10413 struct ppc_link_hash_entry *h = *hash;
10414 bfd_vma location;
10415 bfd_vma branch_offset;
10416 bfd_vma max_branch_offset;
4ce794b7 10417 enum elf_ppc64_reloc_type r_type;
5bd4f169 10418
721956f4
AM
10419 if (h != NULL)
10420 {
e054468f 10421 struct plt_entry *ent;
7fe2b9a6 10422 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10423 if (h->oh != NULL
10424 && h->oh->is_func_descriptor)
7b8f6675
AM
10425 {
10426 fdh = ppc_follow_link (h->oh);
10427 *hash = fdh;
10428 }
8387904d 10429
e054468f
AM
10430 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10431 if (ent->addend == rel->r_addend
10432 && ent->plt.offset != (bfd_vma) -1)
10433 {
e054468f
AM
10434 *plt_ent = ent;
10435 return ppc_stub_plt_call;
10436 }
5bd4f169 10437
7fe2b9a6
AM
10438 /* Here, we know we don't have a plt entry. If we don't have a
10439 either a defined function descriptor or a defined entry symbol
10440 in a regular object file, then it is pointless trying to make
10441 any other type of stub. */
854b41e7
AM
10442 if (!is_static_defined (&fdh->elf)
10443 && !is_static_defined (&h->elf))
721956f4 10444 return ppc_stub_none;
5d1634d7 10445 }
e054468f
AM
10446 else if (elf_local_got_ents (input_sec->owner) != NULL)
10447 {
10448 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10449 struct plt_entry **local_plt = (struct plt_entry **)
10450 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10451 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10452
10453 if (local_plt[r_symndx] != NULL)
10454 {
10455 struct plt_entry *ent;
10456
10457 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10458 if (ent->addend == rel->r_addend
10459 && ent->plt.offset != (bfd_vma) -1)
10460 {
10461 *plt_ent = ent;
10462 return ppc_stub_plt_call;
10463 }
10464 }
10465 }
5d1634d7 10466
721956f4
AM
10467 /* Determine where the call point is. */
10468 location = (input_sec->output_offset
10469 + input_sec->output_section->vma
10470 + rel->r_offset);
5d1634d7 10471
721956f4
AM
10472 branch_offset = destination - location;
10473 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10474
721956f4
AM
10475 /* Determine if a long branch stub is needed. */
10476 max_branch_offset = 1 << 25;
4ce794b7 10477 if (r_type != R_PPC64_REL24)
721956f4 10478 max_branch_offset = 1 << 15;
5d1634d7 10479
6911b7dc 10480 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10481 /* We need a stub. Figure out whether a long_branch or plt_branch
10482 is needed later. */
10483 return ppc_stub_long_branch;
5d1634d7 10484
721956f4 10485 return ppc_stub_none;
5d1634d7
AM
10486}
10487
794e51c0
AM
10488/* With power7 weakly ordered memory model, it is possible for ld.so
10489 to update a plt entry in one thread and have another thread see a
10490 stale zero toc entry. To avoid this we need some sort of acquire
10491 barrier in the call stub. One solution is to make the load of the
10492 toc word seem to appear to depend on the load of the function entry
10493 word. Another solution is to test for r2 being zero, and branch to
10494 the appropriate glink entry if so.
10495
10496 . fake dep barrier compare
71a39c98
AM
10497 . ld 12,xxx(2) ld 12,xxx(2)
10498 . mtctr 12 mtctr 12
10499 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10500 . add 2,2,11 cmpldi 2,0
10501 . ld 2,xxx+8(2) bnectr+
10502 . bctr b <glink_entry>
10503
10504 The solution involving the compare turns out to be faster, so
10505 that's what we use unless the branch won't reach. */
10506
10507#define ALWAYS_USE_FAKE_DEP 0
10508#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10509
5d1634d7
AM
10510#define PPC_LO(v) ((v) & 0xffff)
10511#define PPC_HI(v) (((v) >> 16) & 0xffff)
10512#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10513
794e51c0
AM
10514static inline unsigned int
10515plt_stub_size (struct ppc_link_hash_table *htab,
10516 struct ppc_stub_hash_entry *stub_entry,
10517 bfd_vma off)
10518{
b9e5796b
AM
10519 unsigned size = 12;
10520
10521 if (ALWAYS_EMIT_R2SAVE
10522 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10523 size += 4;
10524 if (PPC_HA (off) != 0)
794e51c0 10525 size += 4;
b9e5796b
AM
10526 if (htab->opd_abi)
10527 {
10528 size += 4;
e7d1c40c 10529 if (htab->params->plt_static_chain)
b9e5796b 10530 size += 4;
bd4d2eaa
AM
10531 if (htab->params->plt_thread_safe
10532 && htab->elf.dynamic_sections_created
10533 && stub_entry->h != NULL
10534 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10535 size += 8;
e7d1c40c 10536 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10537 size += 4;
10538 }
794e51c0
AM
10539 if (stub_entry->h != NULL
10540 && (stub_entry->h == htab->tls_get_addr_fd
10541 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10542 && htab->params->tls_get_addr_opt)
794e51c0
AM
10543 size += 13 * 4;
10544 return size;
10545}
10546
10547/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10548 then return the padding needed to do so. */
10549static inline unsigned int
10550plt_stub_pad (struct ppc_link_hash_table *htab,
10551 struct ppc_stub_hash_entry *stub_entry,
10552 bfd_vma plt_off)
10553{
e7d1c40c 10554 int stub_align = 1 << htab->params->plt_stub_align;
794e51c0 10555 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10556 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0
AM
10557
10558 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10559 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10560 return stub_align - (stub_off & (stub_align - 1));
10561 return 0;
10562}
10563
10564/* Build a .plt call stub. */
10565
10566static inline bfd_byte *
10567build_plt_stub (struct ppc_link_hash_table *htab,
10568 struct ppc_stub_hash_entry *stub_entry,
10569 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10570{
e7d1c40c 10571 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10572 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10573 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10574 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10575 && htab->elf.dynamic_sections_created
10576 && stub_entry->h != NULL
10577 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10578 bfd_boolean use_fake_dep = plt_thread_safe;
10579 bfd_vma cmp_branch_off = 0;
10580
10581 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10582 && plt_load_toc
794e51c0 10583 && plt_thread_safe
bd4d2eaa
AM
10584 && !((stub_entry->h == htab->tls_get_addr_fd
10585 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10586 && htab->params->tls_get_addr_opt))
794e51c0
AM
10587 {
10588 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10589 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10590 / PLT_ENTRY_SIZE (htab));
794e51c0
AM
10591 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10592 bfd_vma to, from;
10593
68d62958
AM
10594 if (pltindex > 32768)
10595 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10596 to = (glinkoff
10597 + htab->glink->output_offset
10598 + htab->glink->output_section->vma);
6f20ed8a 10599 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10600 + 4 * (ALWAYS_EMIT_R2SAVE
10601 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10602 + 4 * (PPC_HA (offset) != 0)
10603 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10604 != PPC_HA (offset))
10605 + 4 * (plt_static_chain != 0)
10606 + 20
6f20ed8a
AM
10607 + stub_entry->group->stub_sec->output_offset
10608 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10609 cmp_branch_off = to - from;
10610 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10611 }
10612
ac2df442
AM
10613 if (PPC_HA (offset) != 0)
10614 {
176a0d42
AM
10615 if (r != NULL)
10616 {
794e51c0
AM
10617 if (ALWAYS_EMIT_R2SAVE
10618 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10619 r[0].r_offset += 4;
176a0d42 10620 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10621 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10622 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10623 r[1].r_addend = r[0].r_addend;
b9e5796b 10624 if (plt_load_toc)
176a0d42 10625 {
b9e5796b 10626 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10627 {
b9e5796b
AM
10628 r[2].r_offset = r[1].r_offset + 4;
10629 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10630 r[2].r_addend = r[0].r_addend;
10631 }
10632 else
10633 {
10634 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10635 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10636 r[2].r_addend = r[0].r_addend + 8;
10637 if (plt_static_chain)
10638 {
10639 r[3].r_offset = r[2].r_offset + 4;
10640 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10641 r[3].r_addend = r[0].r_addend + 16;
10642 }
c7131b65 10643 }
176a0d42
AM
10644 }
10645 }
794e51c0
AM
10646 if (ALWAYS_EMIT_R2SAVE
10647 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10648 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10649 if (plt_load_toc)
10650 {
10651 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10652 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10653 }
10654 else
10655 {
10656 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10657 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10658 }
b9e5796b
AM
10659 if (plt_load_toc
10660 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10661 {
71a39c98 10662 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10663 offset = 0;
10664 }
71a39c98 10665 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10666 if (plt_load_toc)
794e51c0 10667 {
b9e5796b
AM
10668 if (use_fake_dep)
10669 {
10670 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10671 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10672 }
10673 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10674 if (plt_static_chain)
10675 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10676 }
ac2df442
AM
10677 }
10678 else
10679 {
176a0d42
AM
10680 if (r != NULL)
10681 {
794e51c0
AM
10682 if (ALWAYS_EMIT_R2SAVE
10683 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10684 r[0].r_offset += 4;
176a0d42 10685 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10686 if (plt_load_toc)
176a0d42 10687 {
b9e5796b 10688 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10689 {
b9e5796b
AM
10690 r[1].r_offset = r[0].r_offset + 4;
10691 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10692 r[1].r_addend = r[0].r_addend;
10693 }
10694 else
10695 {
10696 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10697 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10698 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10699 if (plt_static_chain)
10700 {
10701 r[2].r_offset = r[1].r_offset + 4;
10702 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10703 r[2].r_addend = r[0].r_addend + 8;
10704 }
c7131b65 10705 }
176a0d42
AM
10706 }
10707 }
794e51c0
AM
10708 if (ALWAYS_EMIT_R2SAVE
10709 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10710 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10711 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10712 if (plt_load_toc
10713 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10714 {
10715 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10716 offset = 0;
10717 }
71a39c98 10718 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10719 if (plt_load_toc)
794e51c0 10720 {
b9e5796b
AM
10721 if (use_fake_dep)
10722 {
10723 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10724 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10725 }
10726 if (plt_static_chain)
10727 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10728 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10729 }
ac2df442 10730 }
b9e5796b 10731 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10732 {
10733 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10734 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10735 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10736 }
10737 else
10738 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10739 return p;
10740}
10741
a7f2871e
AM
10742/* Build a special .plt call stub for __tls_get_addr. */
10743
10744#define LD_R11_0R3 0xe9630000
10745#define LD_R12_0R3 0xe9830000
10746#define MR_R0_R3 0x7c601b78
10747#define CMPDI_R11_0 0x2c2b0000
10748#define ADD_R3_R12_R13 0x7c6c6a14
10749#define BEQLR 0x4d820020
10750#define MR_R3_R0 0x7c030378
a7f2871e
AM
10751#define STD_R11_0R1 0xf9610000
10752#define BCTRL 0x4e800421
10753#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10754#define MTLR_R11 0x7d6803a6
10755
10756static inline bfd_byte *
794e51c0
AM
10757build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10758 struct ppc_stub_hash_entry *stub_entry,
10759 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10760{
e7d1c40c 10761 bfd *obfd = htab->params->stub_bfd;
794e51c0 10762
a7f2871e
AM
10763 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10764 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10765 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10766 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10767 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10768 bfd_put_32 (obfd, BEQLR, p), p += 4;
10769 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10770 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10771 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10772
10773 if (r != NULL)
10774 r[0].r_offset += 9 * 4;
794e51c0 10775 p = build_plt_stub (htab, stub_entry, p, offset, r);
a7f2871e
AM
10776 bfd_put_32 (obfd, BCTRL, p - 4);
10777
a078d95a 10778 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10779 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10780 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10781 bfd_put_32 (obfd, BLR, p), p += 4;
10782
10783 return p;
10784}
10785
176a0d42
AM
10786static Elf_Internal_Rela *
10787get_relocs (asection *sec, int count)
10788{
10789 Elf_Internal_Rela *relocs;
10790 struct bfd_elf_section_data *elfsec_data;
10791
10792 elfsec_data = elf_section_data (sec);
10793 relocs = elfsec_data->relocs;
10794 if (relocs == NULL)
10795 {
10796 bfd_size_type relsize;
10797 relsize = sec->reloc_count * sizeof (*relocs);
10798 relocs = bfd_alloc (sec->owner, relsize);
10799 if (relocs == NULL)
10800 return NULL;
10801 elfsec_data->relocs = relocs;
d4730f92
BS
10802 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10803 sizeof (Elf_Internal_Shdr));
10804 if (elfsec_data->rela.hdr == NULL)
10805 return NULL;
10806 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10807 * sizeof (Elf64_External_Rela));
10808 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10809 sec->reloc_count = 0;
10810 }
10811 relocs += sec->reloc_count;
10812 sec->reloc_count += count;
10813 return relocs;
10814}
10815
aa374f67 10816static bfd_vma
25f53a85 10817get_r2off (struct bfd_link_info *info,
aa374f67
AM
10818 struct ppc_stub_hash_entry *stub_entry)
10819{
25f53a85 10820 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10821 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10822
10823 if (r2off == 0)
10824 {
10825 /* Support linking -R objects. Get the toc pointer from the
10826 opd entry. */
10827 char buf[8];
b9e5796b
AM
10828 if (!htab->opd_abi)
10829 return r2off;
aa374f67
AM
10830 asection *opd = stub_entry->h->elf.root.u.def.section;
10831 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10832
10833 if (strcmp (opd->name, ".opd") != 0
10834 || opd->reloc_count != 0)
10835 {
bc30df16 10836 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
25f53a85 10837 stub_entry->h->elf.root.root.string);
aa374f67 10838 bfd_set_error (bfd_error_bad_value);
a7c49797 10839 return (bfd_vma) -1;
aa374f67
AM
10840 }
10841 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10842 return (bfd_vma) -1;
aa374f67 10843 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10844 r2off -= elf_gp (info->output_bfd);
aa374f67 10845 }
6f20ed8a 10846 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10847 return r2off;
10848}
10849
b34976b6 10850static bfd_boolean
4ce794b7 10851ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10852{
721956f4
AM
10853 struct ppc_stub_hash_entry *stub_entry;
10854 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10855 struct bfd_link_info *info;
10856 struct ppc_link_hash_table *htab;
721956f4
AM
10857 bfd_byte *loc;
10858 bfd_byte *p;
ee75fd95 10859 bfd_vma dest, off;
721956f4 10860 int size;
176a0d42 10861 Elf_Internal_Rela *r;
e054468f 10862 asection *plt;
5d1634d7 10863
721956f4
AM
10864 /* Massage our args to the form they really have. */
10865 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10866 info = in_arg;
5d1634d7 10867
5d1634d7 10868 htab = ppc_hash_table (info);
4dfe6ac6
NC
10869 if (htab == NULL)
10870 return FALSE;
5d1634d7 10871
721956f4 10872 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
10873 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10874 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10875
4ce794b7 10876 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10877 switch (stub_entry->stub_type)
5d1634d7 10878 {
721956f4 10879 case ppc_stub_long_branch:
ad8e1ba5 10880 case ppc_stub_long_branch_r2off:
721956f4 10881 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10882 dest = (stub_entry->target_value
10883 + stub_entry->target_section->output_offset
10884 + stub_entry->target_section->output_section->vma);
10885 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10886 off = dest;
5d1634d7 10887
721956f4
AM
10888 /* And this is where we are coming from. */
10889 off -= (stub_entry->stub_offset
6f20ed8a
AM
10890 + stub_entry->group->stub_sec->output_offset
10891 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 10892
ac2df442
AM
10893 size = 4;
10894 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10895 {
25f53a85 10896 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10897
a7c49797 10898 if (r2off == (bfd_vma) -1)
aa374f67
AM
10899 {
10900 htab->stub_error = TRUE;
10901 return FALSE;
10902 }
e7d1c40c 10903 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10904 loc += 4;
a7c49797 10905 size = 8;
ac2df442
AM
10906 if (PPC_HA (r2off) != 0)
10907 {
e7d1c40c
AM
10908 bfd_put_32 (htab->params->stub_bfd,
10909 ADDIS_R2_R2 | PPC_HA (r2off), loc);
ac2df442 10910 loc += 4;
a7c49797
AM
10911 size += 4;
10912 }
10913 if (PPC_LO (r2off) != 0)
10914 {
10915 bfd_put_32 (htab->params->stub_bfd,
10916 ADDI_R2_R2 | PPC_LO (r2off), loc);
10917 loc += 4;
10918 size += 4;
ac2df442 10919 }
ac2df442 10920 off -= size - 4;
ad8e1ba5 10921 }
e7d1c40c 10922 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 10923
5c3dead3
AM
10924 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10925 {
bc30df16
AM
10926 info->callbacks->einfo
10927 (_("%P: long branch stub `%s' offset overflow\n"),
10928 stub_entry->root.string);
5c3dead3
AM
10929 htab->stub_error = TRUE;
10930 return FALSE;
10931 }
ee75fd95
AM
10932
10933 if (info->emitrelocations)
10934 {
6f20ed8a 10935 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10936 if (r == NULL)
10937 return FALSE;
6f20ed8a 10938 r->r_offset = loc - stub_entry->group->stub_sec->contents;
ee75fd95
AM
10939 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10940 r->r_addend = dest;
10941 if (stub_entry->h != NULL)
10942 {
10943 struct elf_link_hash_entry **hashes;
10944 unsigned long symndx;
10945 struct ppc_link_hash_entry *h;
10946
e7d1c40c 10947 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10948 if (hashes == NULL)
10949 {
10950 bfd_size_type hsize;
10951
10952 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10953 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10954 if (hashes == NULL)
10955 return FALSE;
e7d1c40c 10956 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10957 htab->stub_globals = 1;
10958 }
10959 symndx = htab->stub_globals++;
10960 h = stub_entry->h;
10961 hashes[symndx] = &h->elf;
10962 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10963 if (h->oh != NULL && h->oh->is_func)
b31867b6 10964 h = ppc_follow_link (h->oh);
ee75fd95
AM
10965 if (h->elf.root.u.def.section != stub_entry->target_section)
10966 /* H is an opd symbol. The addend must be zero. */
10967 r->r_addend = 0;
10968 else
10969 {
10970 off = (h->elf.root.u.def.value
10971 + h->elf.root.u.def.section->output_offset
10972 + h->elf.root.u.def.section->output_section->vma);
10973 r->r_addend -= off;
10974 }
10975 }
10976 }
721956f4 10977 break;
e86ce104 10978
721956f4 10979 case ppc_stub_plt_branch:
ad8e1ba5 10980 case ppc_stub_plt_branch_r2off:
721956f4
AM
10981 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10982 stub_entry->root.string + 9,
b34976b6 10983 FALSE, FALSE);
721956f4
AM
10984 if (br_entry == NULL)
10985 {
8de848d8 10986 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 10987 stub_entry->root.string);
b34976b6
AM
10988 htab->stub_error = TRUE;
10989 return FALSE;
721956f4
AM
10990 }
10991
176a0d42
AM
10992 dest = (stub_entry->target_value
10993 + stub_entry->target_section->output_offset
10994 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10995 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10996 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10997
176a0d42 10998 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10999 htab->brlt->contents + br_entry->offset);
721956f4 11000
f94498ff 11001 if (br_entry->iter == htab->stub_iteration)
721956f4 11002 {
f94498ff 11003 br_entry->iter = 0;
84f5d08e 11004
f94498ff 11005 if (htab->relbrlt != NULL)
84f5d08e 11006 {
f94498ff
AM
11007 /* Create a reloc for the branch lookup table entry. */
11008 Elf_Internal_Rela rela;
11009 bfd_byte *rl;
11010
11011 rela.r_offset = (br_entry->offset
11012 + htab->brlt->output_offset
11013 + htab->brlt->output_section->vma);
11014 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 11015 rela.r_addend = dest;
f94498ff
AM
11016
11017 rl = htab->relbrlt->contents;
11018 rl += (htab->relbrlt->reloc_count++
11019 * sizeof (Elf64_External_Rela));
11020 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11021 }
11022 else if (info->emitrelocations)
11023 {
176a0d42
AM
11024 r = get_relocs (htab->brlt, 1);
11025 if (r == NULL)
11026 return FALSE;
11027 /* brlt, being SEC_LINKER_CREATED does not go through the
11028 normal reloc processing. Symbols and offsets are not
11029 translated from input file to output file form, so
11030 set up the offset per the output file. */
f94498ff
AM
11031 r->r_offset = (br_entry->offset
11032 + htab->brlt->output_offset
11033 + htab->brlt->output_section->vma);
11034 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 11035 r->r_addend = dest;
84f5d08e 11036 }
84f5d08e 11037 }
721956f4 11038
176a0d42
AM
11039 dest = (br_entry->offset
11040 + htab->brlt->output_offset
11041 + htab->brlt->output_section->vma);
11042
11043 off = (dest
4ce794b7 11044 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 11045 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11046
ad8e1ba5 11047 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11048 {
25f53a85 11049 info->callbacks->einfo
bc30df16 11050 (_("%P: linkage table error against `%T'\n"),
721956f4 11051 stub_entry->root.string);
5d1634d7 11052 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11053 htab->stub_error = TRUE;
11054 return FALSE;
5d1634d7 11055 }
41bd81ab 11056
176a0d42
AM
11057 if (info->emitrelocations)
11058 {
6f20ed8a 11059 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11060 if (r == NULL)
11061 return FALSE;
6f20ed8a 11062 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11063 if (bfd_big_endian (info->output_bfd))
11064 r[0].r_offset += 2;
00f412ee 11065 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11066 r[0].r_offset += 4;
11067 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11068 r[0].r_addend = dest;
11069 if (PPC_HA (off) != 0)
11070 {
11071 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11072 r[1].r_offset = r[0].r_offset + 4;
11073 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11074 r[1].r_addend = r[0].r_addend;
11075 }
11076 }
11077
00f412ee 11078 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11079 {
176a0d42 11080 if (PPC_HA (off) != 0)
ac2df442
AM
11081 {
11082 size = 16;
e7d1c40c 11083 bfd_put_32 (htab->params->stub_bfd,
397998fc 11084 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 11085 loc += 4;
e7d1c40c 11086 bfd_put_32 (htab->params->stub_bfd,
397998fc 11087 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
11088 }
11089 else
11090 {
11091 size = 12;
e7d1c40c
AM
11092 bfd_put_32 (htab->params->stub_bfd,
11093 LD_R12_0R2 | PPC_LO (off), loc);
ac2df442 11094 }
ad8e1ba5
AM
11095 }
11096 else
11097 {
25f53a85 11098 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11099
a7c49797 11100 if (r2off == (bfd_vma) -1)
aa374f67
AM
11101 {
11102 htab->stub_error = TRUE;
11103 return FALSE;
11104 }
ad8e1ba5 11105
e7d1c40c 11106 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 11107 loc += 4;
00f412ee 11108 size = 16;
176a0d42 11109 if (PPC_HA (off) != 0)
ac2df442
AM
11110 {
11111 size += 4;
e7d1c40c 11112 bfd_put_32 (htab->params->stub_bfd,
397998fc 11113 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 11114 loc += 4;
e7d1c40c 11115 bfd_put_32 (htab->params->stub_bfd,
397998fc 11116 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
11117 }
11118 else
e7d1c40c 11119 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442
AM
11120
11121 if (PPC_HA (r2off) != 0)
11122 {
11123 size += 4;
00f412ee 11124 loc += 4;
e7d1c40c
AM
11125 bfd_put_32 (htab->params->stub_bfd,
11126 ADDIS_R2_R2 | PPC_HA (r2off), loc);
00f412ee
AM
11127 }
11128 if (PPC_LO (r2off) != 0)
11129 {
11130 size += 4;
ac2df442 11131 loc += 4;
e7d1c40c
AM
11132 bfd_put_32 (htab->params->stub_bfd,
11133 ADDI_R2_R2 | PPC_LO (r2off), loc);
ac2df442 11134 }
ad8e1ba5
AM
11135 }
11136 loc += 4;
e7d1c40c 11137 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
ad8e1ba5 11138 loc += 4;
e7d1c40c 11139 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
721956f4 11140 break;
5d1634d7 11141
721956f4 11142 case ppc_stub_plt_call:
794e51c0 11143 case ppc_stub_plt_call_r2save:
e054468f 11144 if (stub_entry->h != NULL
b31867b6
AM
11145 && stub_entry->h->is_func_descriptor
11146 && stub_entry->h->oh != NULL)
c862ae31 11147 {
b31867b6
AM
11148 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11149
11150 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11151 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11152 if (fh->elf.root.type == bfd_link_hash_undefined
11153 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11154 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11155 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11156 }
11157
721956f4 11158 /* Now build the stub. */
e054468f 11159 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 11160 if (dest >= (bfd_vma) -2)
721956f4
AM
11161 abort ();
11162
33e44f2e 11163 plt = htab->elf.splt;
25f23106
AM
11164 if (!htab->elf.dynamic_sections_created
11165 || stub_entry->h == NULL
11166 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11167 plt = htab->elf.iplt;
e054468f
AM
11168
11169 dest += plt->output_offset + plt->output_section->vma;
11170
11171 if (stub_entry->h == NULL
11172 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11173 {
11174 Elf_Internal_Rela rela;
11175 bfd_byte *rl;
11176
11177 rela.r_offset = dest;
ee67d69a
AM
11178 if (htab->opd_abi)
11179 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11180 else
11181 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
11182 rela.r_addend = (stub_entry->target_value
11183 + stub_entry->target_section->output_offset
11184 + stub_entry->target_section->output_section->vma);
11185
33e44f2e
AM
11186 rl = (htab->elf.irelplt->contents
11187 + (htab->elf.irelplt->reloc_count++
25f23106
AM
11188 * sizeof (Elf64_External_Rela)));
11189 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f 11190 stub_entry->plt_ent->plt.offset |= 1;
82e66161 11191 htab->local_ifunc_resolver = 1;
e054468f 11192 }
176a0d42
AM
11193
11194 off = (dest
e054468f 11195 - elf_gp (plt->output_section->owner)
6f20ed8a 11196 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11197
ad8e1ba5 11198 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11199 {
25f53a85 11200 info->callbacks->einfo
695344c0 11201 /* xgettext:c-format */
bc30df16 11202 (_("%P: linkage table error against `%T'\n"),
e054468f
AM
11203 stub_entry->h != NULL
11204 ? stub_entry->h->elf.root.root.string
11205 : "<local sym>");
721956f4 11206 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11207 htab->stub_error = TRUE;
11208 return FALSE;
721956f4
AM
11209 }
11210
e7d1c40c 11211 if (htab->params->plt_stub_align != 0)
794e51c0
AM
11212 {
11213 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11214
6f20ed8a
AM
11215 stub_entry->group->stub_sec->size += pad;
11216 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
11217 loc += pad;
11218 }
11219
176a0d42
AM
11220 r = NULL;
11221 if (info->emitrelocations)
11222 {
6f20ed8a 11223 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11224 ((PPC_HA (off) != 0)
11225 + (htab->opd_abi
e7d1c40c 11226 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11227 && PPC_HA (off + 16) == PPC_HA (off))
11228 : 1)));
176a0d42
AM
11229 if (r == NULL)
11230 return FALSE;
6f20ed8a 11231 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11232 if (bfd_big_endian (info->output_bfd))
11233 r[0].r_offset += 2;
176a0d42
AM
11234 r[0].r_addend = dest;
11235 }
a7f2871e
AM
11236 if (stub_entry->h != NULL
11237 && (stub_entry->h == htab->tls_get_addr_fd
11238 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11239 && htab->params->tls_get_addr_opt)
794e51c0 11240 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11241 else
794e51c0 11242 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11243 size = p - loc;
11244 break;
11245
a4b6fadd
AM
11246 case ppc_stub_save_res:
11247 return TRUE;
11248
721956f4
AM
11249 default:
11250 BFD_FAIL ();
b34976b6 11251 return FALSE;
721956f4
AM
11252 }
11253
6f20ed8a 11254 stub_entry->group->stub_sec->size += size;
97b639ba 11255
e7d1c40c 11256 if (htab->params->emit_stub_syms)
97b639ba
AM
11257 {
11258 struct elf_link_hash_entry *h;
ee75fd95
AM
11259 size_t len1, len2;
11260 char *name;
11261 const char *const stub_str[] = { "long_branch",
11262 "long_branch_r2off",
11263 "plt_branch",
11264 "plt_branch_r2off",
794e51c0 11265 "plt_call",
ee75fd95
AM
11266 "plt_call" };
11267
11268 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11269 len2 = strlen (stub_entry->root.string);
11270 name = bfd_malloc (len1 + len2 + 2);
11271 if (name == NULL)
11272 return FALSE;
11273 memcpy (name, stub_entry->root.string, 9);
11274 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11275 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11276 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11277 if (h == NULL)
11278 return FALSE;
11279 if (h->root.type == bfd_link_hash_new)
11280 {
11281 h->root.type = bfd_link_hash_defined;
6f20ed8a 11282 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11283 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11284 h->ref_regular = 1;
11285 h->def_regular = 1;
11286 h->ref_regular_nonweak = 1;
11287 h->forced_local = 1;
11288 h->non_elf = 0;
2ec55de3 11289 h->root.linker_def = 1;
97b639ba
AM
11290 }
11291 }
11292
b34976b6 11293 return TRUE;
721956f4
AM
11294}
11295
11296/* As above, but don't actually build the stub. Just bump offset so
11297 we know stub section sizes, and select plt_branch stubs where
11298 long_branch stubs won't do. */
11299
b34976b6 11300static bfd_boolean
4ce794b7 11301ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11302{
11303 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11304 struct bfd_link_info *info;
721956f4
AM
11305 struct ppc_link_hash_table *htab;
11306 bfd_vma off;
11307 int size;
11308
11309 /* Massage our args to the form they really have. */
11310 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11311 info = in_arg;
11312
11313 htab = ppc_hash_table (info);
4dfe6ac6
NC
11314 if (htab == NULL)
11315 return FALSE;
721956f4 11316
a4b6fadd
AM
11317 if (stub_entry->h != NULL
11318 && stub_entry->h->save_res
11319 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11320 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11321 {
11322 /* Don't make stubs to out-of-line register save/restore
11323 functions. Instead, emit copies of the functions. */
11324 stub_entry->group->needs_save_res = 1;
11325 stub_entry->stub_type = ppc_stub_save_res;
11326 return TRUE;
11327 }
11328
794e51c0
AM
11329 if (stub_entry->stub_type == ppc_stub_plt_call
11330 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11331 {
e054468f
AM
11332 asection *plt;
11333 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11334 if (off >= (bfd_vma) -2)
411e1bfb 11335 abort ();
33e44f2e 11336 plt = htab->elf.splt;
25f23106
AM
11337 if (!htab->elf.dynamic_sections_created
11338 || stub_entry->h == NULL
11339 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11340 plt = htab->elf.iplt;
e054468f
AM
11341 off += (plt->output_offset
11342 + plt->output_section->vma
11343 - elf_gp (plt->output_section->owner)
6f20ed8a 11344 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11345
794e51c0 11346 size = plt_stub_size (htab, stub_entry, off);
e7d1c40c 11347 if (htab->params->plt_stub_align)
794e51c0 11348 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11349 if (info->emitrelocations)
11350 {
6f20ed8a 11351 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11352 += ((PPC_HA (off) != 0)
11353 + (htab->opd_abi
e7d1c40c 11354 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11355 && PPC_HA (off + 16) == PPC_HA (off))
11356 : 1));
6f20ed8a 11357 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11358 }
721956f4
AM
11359 }
11360 else
11361 {
ad8e1ba5
AM
11362 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11363 variants. */
ac2df442 11364 bfd_vma r2off = 0;
6911b7dc 11365 bfd_vma local_off = 0;
ac2df442 11366
721956f4
AM
11367 off = (stub_entry->target_value
11368 + stub_entry->target_section->output_offset
11369 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11370 off -= (stub_entry->group->stub_sec->size
11371 + stub_entry->group->stub_sec->output_offset
11372 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11373
ad8e1ba5
AM
11374 /* Reset the stub type from the plt variant in case we now
11375 can reach with a shorter stub. */
11376 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11377 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11378
11379 size = 4;
11380 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11381 {
25f53a85 11382 r2off = get_r2off (info, stub_entry);
a7c49797 11383 if (r2off == (bfd_vma) -1)
aa374f67
AM
11384 {
11385 htab->stub_error = TRUE;
11386 return FALSE;
11387 }
a7c49797 11388 size = 8;
ac2df442 11389 if (PPC_HA (r2off) != 0)
a7c49797
AM
11390 size += 4;
11391 if (PPC_LO (r2off) != 0)
11392 size += 4;
ac2df442 11393 off -= size - 4;
ad8e1ba5
AM
11394 }
11395
6911b7dc
AM
11396 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11397
b9e5796b
AM
11398 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11399 Do the same for -R objects without function descriptors. */
11400 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11401 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11402 && r2off == 0
11403 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11404 {
11405 struct ppc_branch_hash_entry *br_entry;
11406
11407 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11408 stub_entry->root.string + 9,
b34976b6 11409 TRUE, FALSE);
721956f4
AM
11410 if (br_entry == NULL)
11411 {
8de848d8 11412 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 11413 stub_entry->root.string);
b34976b6
AM
11414 htab->stub_error = TRUE;
11415 return FALSE;
721956f4
AM
11416 }
11417
11418 if (br_entry->iter != htab->stub_iteration)
11419 {
11420 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11421 br_entry->offset = htab->brlt->size;
11422 htab->brlt->size += 8;
63bc6f6c 11423
ee75fd95 11424 if (htab->relbrlt != NULL)
eea6121a 11425 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11426 else if (info->emitrelocations)
11427 {
11428 htab->brlt->reloc_count += 1;
11429 htab->brlt->flags |= SEC_RELOC;
11430 }
721956f4 11431 }
ad8e1ba5
AM
11432
11433 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11434 off = (br_entry->offset
11435 + htab->brlt->output_offset
11436 + htab->brlt->output_section->vma
11437 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 11438 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11439
176a0d42
AM
11440 if (info->emitrelocations)
11441 {
6f20ed8a
AM
11442 stub_entry->group->stub_sec->reloc_count
11443 += 1 + (PPC_HA (off) != 0);
11444 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11445 }
11446
00f412ee 11447 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11448 {
11449 size = 12;
176a0d42 11450 if (PPC_HA (off) != 0)
ac2df442
AM
11451 size = 16;
11452 }
11453 else
11454 {
00f412ee 11455 size = 16;
176a0d42 11456 if (PPC_HA (off) != 0)
ac2df442
AM
11457 size += 4;
11458
11459 if (PPC_HA (r2off) != 0)
11460 size += 4;
00f412ee
AM
11461 if (PPC_LO (r2off) != 0)
11462 size += 4;
ac2df442 11463 }
721956f4 11464 }
84f5d08e
AM
11465 else if (info->emitrelocations)
11466 {
6f20ed8a
AM
11467 stub_entry->group->stub_sec->reloc_count += 1;
11468 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11469 }
721956f4
AM
11470 }
11471
6f20ed8a 11472 stub_entry->group->stub_sec->size += size;
b34976b6 11473 return TRUE;
721956f4
AM
11474}
11475
11476/* Set up various things so that we can make a list of input sections
11477 for each output section included in the link. Returns -1 on error,
cedb70c5 11478 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11479
11480int
e7d1c40c 11481ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11482{
6f20ed8a 11483 unsigned int id;
721956f4
AM
11484 bfd_size_type amt;
11485 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11486
4dfe6ac6
NC
11487 if (htab == NULL)
11488 return -1;
4c52953f 11489
6f20ed8a
AM
11490 htab->sec_info_arr_size = bfd_get_next_section_id ();
11491 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11492 htab->sec_info = bfd_zmalloc (amt);
11493 if (htab->sec_info == NULL)
721956f4
AM
11494 return -1;
11495
3d6f9012
AM
11496 /* Set toc_off for com, und, abs and ind sections. */
11497 for (id = 0; id < 3; id++)
6f20ed8a 11498 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11499
721956f4
AM
11500 return 1;
11501}
11502
927be08e
AM
11503/* Set up for first pass at multitoc partitioning. */
11504
11505void
11506ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11507{
11508 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11509
1c865ab2 11510 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11511 htab->toc_bfd = NULL;
11512 htab->toc_first_sec = NULL;
11513}
11514
e717da7e
AM
11515/* The linker repeatedly calls this function for each TOC input section
11516 and linker generated GOT section. Group input bfds such that the toc
927be08e 11517 within a group is less than 64k in size. */
ad8e1ba5 11518
927be08e 11519bfd_boolean
4ce794b7 11520ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11521{
11522 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11523 bfd_vma addr, off, limit;
ad8e1ba5 11524
4dfe6ac6
NC
11525 if (htab == NULL)
11526 return FALSE;
11527
927be08e 11528 if (!htab->second_toc_pass)
4c52953f 11529 {
927be08e 11530 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11531 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11532
11533 if (new_bfd)
bf102f86
AM
11534 {
11535 htab->toc_bfd = isec->owner;
11536 htab->toc_first_sec = isec;
11537 }
927be08e 11538
bf102f86
AM
11539 addr = isec->output_offset + isec->output_section->vma;
11540 off = addr - htab->toc_curr;
d77c8a4b
AM
11541 limit = 0x80008000;
11542 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11543 limit = 0x10000;
11544 if (off + isec->size > limit)
bf102f86
AM
11545 {
11546 addr = (htab->toc_first_sec->output_offset
11547 + htab->toc_first_sec->output_section->vma);
11548 htab->toc_curr = addr;
a27e685f 11549 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11550 }
99877b66 11551
927be08e
AM
11552 /* toc_curr is the base address of this toc group. Set elf_gp
11553 for the input section to be the offset relative to the
11554 output toc base plus 0x8000. Making the input elf_gp an
11555 offset allows us to move the toc as a whole without
11556 recalculating input elf_gp. */
11557 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11558 off += TOC_BASE_OFF;
11559
11560 /* Die if someone uses a linker script that doesn't keep input
11561 file .toc and .got together. */
a4fd3de5
AM
11562 if (new_bfd
11563 && elf_gp (isec->owner) != 0
927be08e
AM
11564 && elf_gp (isec->owner) != off)
11565 return FALSE;
11566
11567 elf_gp (isec->owner) = off;
11568 return TRUE;
4c52953f 11569 }
927be08e
AM
11570
11571 /* During the second pass toc_first_sec points to the start of
11572 a toc group, and toc_curr is used to track the old elf_gp.
11573 We use toc_bfd to ensure we only look at each bfd once. */
11574 if (htab->toc_bfd == isec->owner)
11575 return TRUE;
11576 htab->toc_bfd = isec->owner;
11577
11578 if (htab->toc_first_sec == NULL
11579 || htab->toc_curr != elf_gp (isec->owner))
11580 {
11581 htab->toc_curr = elf_gp (isec->owner);
11582 htab->toc_first_sec = isec;
11583 }
11584 addr = (htab->toc_first_sec->output_offset
11585 + htab->toc_first_sec->output_section->vma);
11586 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11587 elf_gp (isec->owner) = off;
11588
11589 return TRUE;
ad8e1ba5
AM
11590}
11591
927be08e
AM
11592/* Called via elf_link_hash_traverse to merge GOT entries for global
11593 symbol H. */
11594
11595static bfd_boolean
11596merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11597{
11598 if (h->root.type == bfd_link_hash_indirect)
11599 return TRUE;
11600
927be08e
AM
11601 merge_got_entries (&h->got.glist);
11602
11603 return TRUE;
11604}
11605
11606/* Called via elf_link_hash_traverse to allocate GOT entries for global
11607 symbol H. */
11608
11609static bfd_boolean
11610reallocate_got (struct elf_link_hash_entry *h, void *inf)
11611{
11612 struct got_entry *gent;
11613
11614 if (h->root.type == bfd_link_hash_indirect)
11615 return TRUE;
11616
927be08e
AM
11617 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11618 if (!gent->is_indirect)
11619 allocate_got (h, (struct bfd_link_info *) inf, gent);
11620 return TRUE;
11621}
11622
11623/* Called on the first multitoc pass after the last call to
11624 ppc64_elf_next_toc_section. This function removes duplicate GOT
11625 entries. */
11626
11627bfd_boolean
11628ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11629{
11630 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11631 struct bfd *ibfd, *ibfd2;
11632 bfd_boolean done_something;
11633
11634 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11635
7865406b
AM
11636 if (!htab->do_multi_toc)
11637 return FALSE;
11638
d0fae19d 11639 /* Merge global sym got entries within a toc group. */
927be08e
AM
11640 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11641
11642 /* And tlsld_got. */
c72f2fb2 11643 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11644 {
11645 struct got_entry *ent, *ent2;
11646
11647 if (!is_ppc64_elf (ibfd))
11648 continue;
11649
11650 ent = ppc64_tlsld_got (ibfd);
11651 if (!ent->is_indirect
11652 && ent->got.offset != (bfd_vma) -1)
11653 {
c72f2fb2 11654 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11655 {
11656 if (!is_ppc64_elf (ibfd2))
11657 continue;
11658
11659 ent2 = ppc64_tlsld_got (ibfd2);
11660 if (!ent2->is_indirect
11661 && ent2->got.offset != (bfd_vma) -1
11662 && elf_gp (ibfd2) == elf_gp (ibfd))
11663 {
11664 ent2->is_indirect = TRUE;
11665 ent2->got.ent = ent;
11666 }
11667 }
11668 }
11669 }
11670
11671 /* Zap sizes of got sections. */
33e44f2e
AM
11672 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11673 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11674 htab->got_reli_size = 0;
11675
c72f2fb2 11676 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11677 {
11678 asection *got, *relgot;
11679
11680 if (!is_ppc64_elf (ibfd))
11681 continue;
11682
11683 got = ppc64_elf_tdata (ibfd)->got;
11684 if (got != NULL)
11685 {
11686 got->rawsize = got->size;
11687 got->size = 0;
11688 relgot = ppc64_elf_tdata (ibfd)->relgot;
11689 relgot->rawsize = relgot->size;
11690 relgot->size = 0;
11691 }
11692 }
11693
11694 /* Now reallocate the got, local syms first. We don't need to
11695 allocate section contents again since we never increase size. */
c72f2fb2 11696 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11697 {
11698 struct got_entry **lgot_ents;
11699 struct got_entry **end_lgot_ents;
11700 struct plt_entry **local_plt;
11701 struct plt_entry **end_local_plt;
f961d9dd 11702 unsigned char *lgot_masks;
927be08e
AM
11703 bfd_size_type locsymcount;
11704 Elf_Internal_Shdr *symtab_hdr;
19e08130 11705 asection *s;
927be08e
AM
11706
11707 if (!is_ppc64_elf (ibfd))
11708 continue;
11709
11710 lgot_ents = elf_local_got_ents (ibfd);
11711 if (!lgot_ents)
11712 continue;
11713
11714 symtab_hdr = &elf_symtab_hdr (ibfd);
11715 locsymcount = symtab_hdr->sh_info;
11716 end_lgot_ents = lgot_ents + locsymcount;
11717 local_plt = (struct plt_entry **) end_lgot_ents;
11718 end_local_plt = local_plt + locsymcount;
f961d9dd 11719 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11720 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11721 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11722 {
11723 struct got_entry *ent;
11724
11725 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11726 {
19e08130
AM
11727 unsigned int ent_size = 8;
11728 unsigned int rel_size = sizeof (Elf64_External_Rela);
11729
d0fae19d
AM
11730 ent->got.offset = s->size;
11731 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11732 {
19e08130
AM
11733 ent_size *= 2;
11734 rel_size *= 2;
11735 }
11736 s->size += ent_size;
11737 if ((*lgot_masks & PLT_IFUNC) != 0)
11738 {
33e44f2e 11739 htab->elf.irelplt->size += rel_size;
19e08130
AM
11740 htab->got_reli_size += rel_size;
11741 }
0e1862bb 11742 else if (bfd_link_pic (info))
19e08130
AM
11743 {
11744 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11745 srel->size += rel_size;
d0fae19d
AM
11746 }
11747 }
927be08e
AM
11748 }
11749 }
11750
11751 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11752
c72f2fb2 11753 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11754 {
11755 struct got_entry *ent;
11756
11757 if (!is_ppc64_elf (ibfd))
11758 continue;
11759
11760 ent = ppc64_tlsld_got (ibfd);
11761 if (!ent->is_indirect
11762 && ent->got.offset != (bfd_vma) -1)
11763 {
11764 asection *s = ppc64_elf_tdata (ibfd)->got;
11765 ent->got.offset = s->size;
11766 s->size += 16;
0e1862bb 11767 if (bfd_link_pic (info))
927be08e
AM
11768 {
11769 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11770 srel->size += sizeof (Elf64_External_Rela);
11771 }
11772 }
11773 }
11774
33e44f2e 11775 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11776 if (!done_something)
c72f2fb2 11777 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11778 {
11779 asection *got;
11780
11781 if (!is_ppc64_elf (ibfd))
11782 continue;
11783
11784 got = ppc64_elf_tdata (ibfd)->got;
11785 if (got != NULL)
11786 {
11787 done_something = got->rawsize != got->size;
11788 if (done_something)
11789 break;
11790 }
11791 }
11792
11793 if (done_something)
e7d1c40c 11794 (*htab->params->layout_sections_again) ();
927be08e
AM
11795
11796 /* Set up for second pass over toc sections to recalculate elf_gp
11797 on input sections. */
11798 htab->toc_bfd = NULL;
11799 htab->toc_first_sec = NULL;
11800 htab->second_toc_pass = TRUE;
11801 return done_something;
11802}
11803
11804/* Called after second pass of multitoc partitioning. */
11805
11806void
11807ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11808{
11809 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11810
11811 /* After the second pass, toc_curr tracks the TOC offset used
11812 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11813 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11814}
11815
9b5ecbd0
AM
11816/* No toc references were found in ISEC. If the code in ISEC makes no
11817 calls, then there's no need to use toc adjusting stubs when branching
11818 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11819 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11820 needed, and 2 if a cyclical call-graph was found but no other reason
11821 for a stub was detected. If called from the top level, a return of
11822 2 means the same as a return of 0. */
9b5ecbd0
AM
11823
11824static int
4ce794b7 11825toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11826{
9b5ecbd0 11827 int ret;
70cc837d
AM
11828
11829 /* Mark this section as checked. */
11830 isec->call_check_done = 1;
9b5ecbd0 11831
772119ce
AM
11832 /* We know none of our code bearing sections will need toc stubs. */
11833 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11834 return 0;
11835
eea6121a 11836 if (isec->size == 0)
082c50f8
AM
11837 return 0;
11838
4c52953f
AM
11839 if (isec->output_section == NULL)
11840 return 0;
11841
4c52953f 11842 ret = 0;
70cc837d 11843 if (isec->reloc_count != 0)
9b5ecbd0 11844 {
70cc837d
AM
11845 Elf_Internal_Rela *relstart, *rel;
11846 Elf_Internal_Sym *local_syms;
11847 struct ppc_link_hash_table *htab;
2917689a 11848
70cc837d
AM
11849 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11850 info->keep_memory);
11851 if (relstart == NULL)
11852 return -1;
90aecf7a 11853
70cc837d
AM
11854 /* Look for branches to outside of this section. */
11855 local_syms = NULL;
11856 htab = ppc_hash_table (info);
11857 if (htab == NULL)
11858 return -1;
4c52953f 11859
70cc837d 11860 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11861 {
70cc837d
AM
11862 enum elf_ppc64_reloc_type r_type;
11863 unsigned long r_symndx;
11864 struct elf_link_hash_entry *h;
11865 struct ppc_link_hash_entry *eh;
11866 Elf_Internal_Sym *sym;
11867 asection *sym_sec;
11868 struct _opd_sec_data *opd;
11869 bfd_vma sym_value;
11870 bfd_vma dest;
11871
11872 r_type = ELF64_R_TYPE (rel->r_info);
11873 if (r_type != R_PPC64_REL24
11874 && r_type != R_PPC64_REL14
11875 && r_type != R_PPC64_REL14_BRTAKEN
11876 && r_type != R_PPC64_REL14_BRNTAKEN)
11877 continue;
4c52953f 11878
70cc837d
AM
11879 r_symndx = ELF64_R_SYM (rel->r_info);
11880 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11881 isec->owner))
4c52953f 11882 {
70cc837d
AM
11883 ret = -1;
11884 break;
11885 }
4c52953f 11886
70cc837d
AM
11887 /* Calls to dynamic lib functions go through a plt call stub
11888 that uses r2. */
11889 eh = (struct ppc_link_hash_entry *) h;
11890 if (eh != NULL
11891 && (eh->elf.plt.plist != NULL
11892 || (eh->oh != NULL
11893 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11894 {
11895 ret = 1;
11896 break;
4c52953f
AM
11897 }
11898
70cc837d
AM
11899 if (sym_sec == NULL)
11900 /* Ignore other undefined symbols. */
4c52953f 11901 continue;
4c52953f 11902
70cc837d
AM
11903 /* Assume branches to other sections not included in the
11904 link need stubs too, to cover -R and absolute syms. */
11905 if (sym_sec->output_section == NULL)
11906 {
11907 ret = 1;
11908 break;
11909 }
4c52953f 11910
70cc837d
AM
11911 if (h == NULL)
11912 sym_value = sym->st_value;
11913 else
11914 {
11915 if (h->root.type != bfd_link_hash_defined
11916 && h->root.type != bfd_link_hash_defweak)
11917 abort ();
11918 sym_value = h->root.u.def.value;
11919 }
11920 sym_value += rel->r_addend;
4c52953f 11921
70cc837d
AM
11922 /* If this branch reloc uses an opd sym, find the code section. */
11923 opd = get_opd_info (sym_sec);
11924 if (opd != NULL)
11925 {
11926 if (h == NULL && opd->adjust != NULL)
11927 {
11928 long adjust;
4c52953f 11929
92a9c616 11930 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11931 if (adjust == -1)
11932 /* Assume deleted functions won't ever be called. */
11933 continue;
11934 sym_value += adjust;
11935 }
4c52953f 11936
aef36ac1
AM
11937 dest = opd_entry_value (sym_sec, sym_value,
11938 &sym_sec, NULL, FALSE);
70cc837d
AM
11939 if (dest == (bfd_vma) -1)
11940 continue;
11941 }
11942 else
11943 dest = (sym_value
11944 + sym_sec->output_offset
11945 + sym_sec->output_section->vma);
4c52953f 11946
70cc837d
AM
11947 /* Ignore branch to self. */
11948 if (sym_sec == isec)
11949 continue;
4c52953f 11950
70cc837d
AM
11951 /* If the called function uses the toc, we need a stub. */
11952 if (sym_sec->has_toc_reloc
11953 || sym_sec->makes_toc_func_call)
4c52953f 11954 {
70cc837d 11955 ret = 1;
4c52953f
AM
11956 break;
11957 }
70cc837d
AM
11958
11959 /* Assume any branch that needs a long branch stub might in fact
11960 need a plt_branch stub. A plt_branch stub uses r2. */
11961 else if (dest - (isec->output_offset
11962 + isec->output_section->vma
6911b7dc
AM
11963 + rel->r_offset) + (1 << 25)
11964 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11965 ? h->other
11966 : sym->st_other))
4c52953f 11967 {
70cc837d
AM
11968 ret = 1;
11969 break;
11970 }
11971
11972 /* If calling back to a section in the process of being
11973 tested, we can't say for sure that no toc adjusting stubs
11974 are needed, so don't return zero. */
11975 else if (sym_sec->call_check_in_progress)
11976 ret = 2;
11977
11978 /* Branches to another section that itself doesn't have any TOC
11979 references are OK. Recursively call ourselves to check. */
11980 else if (!sym_sec->call_check_done)
11981 {
11982 int recur;
11983
11984 /* Mark current section as indeterminate, so that other
11985 sections that call back to current won't be marked as
11986 known. */
11987 isec->call_check_in_progress = 1;
11988 recur = toc_adjusting_stub_needed (info, sym_sec);
11989 isec->call_check_in_progress = 0;
11990
4c52953f
AM
11991 if (recur != 0)
11992 {
70cc837d
AM
11993 ret = recur;
11994 if (recur != 2)
11995 break;
4c52953f
AM
11996 }
11997 }
4c52953f 11998 }
70cc837d
AM
11999
12000 if (local_syms != NULL
12001 && (elf_symtab_hdr (isec->owner).contents
12002 != (unsigned char *) local_syms))
12003 free (local_syms);
12004 if (elf_section_data (isec)->relocs != relstart)
12005 free (relstart);
9b5ecbd0
AM
12006 }
12007
70cc837d
AM
12008 if ((ret & 1) == 0
12009 && isec->map_head.s != NULL
12010 && (strcmp (isec->output_section->name, ".init") == 0
12011 || strcmp (isec->output_section->name, ".fini") == 0))
12012 {
12013 if (isec->map_head.s->has_toc_reloc
12014 || isec->map_head.s->makes_toc_func_call)
12015 ret = 1;
12016 else if (!isec->map_head.s->call_check_done)
12017 {
12018 int recur;
12019 isec->call_check_in_progress = 1;
12020 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12021 isec->call_check_in_progress = 0;
12022 if (recur != 0)
12023 ret = recur;
12024 }
12025 }
12026
12027 if (ret == 1)
12028 isec->makes_toc_func_call = 1;
4c52953f 12029
9b5ecbd0
AM
12030 return ret;
12031}
12032
721956f4
AM
12033/* The linker repeatedly calls this function for each input section,
12034 in the order that input sections are linked into output sections.
12035 Build lists of input sections to determine groupings between which
12036 we may insert linker stubs. */
12037
9b5ecbd0 12038bfd_boolean
4ce794b7 12039ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12040{
12041 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12042
4dfe6ac6
NC
12043 if (htab == NULL)
12044 return FALSE;
12045
734b6cf9 12046 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12047 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12048 {
3d6f9012
AM
12049 /* This happens to make the list in reverse order,
12050 which is what we want. */
6f20ed8a
AM
12051 htab->sec_info[isec->id].u.list
12052 = htab->sec_info[isec->output_section->id].u.list;
12053 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12054 }
ad8e1ba5 12055
4c52953f 12056 if (htab->multi_toc_needed)
9b5ecbd0 12057 {
8b974ba3
AM
12058 /* Analyse sections that aren't already flagged as needing a
12059 valid toc pointer. Exclude .fixup for the linux kernel.
12060 .fixup contains branches, but only back to the function that
12061 hit an exception. */
12062 if (!(isec->has_toc_reloc
12063 || (isec->flags & SEC_CODE) == 0
12064 || strcmp (isec->name, ".fixup") == 0
12065 || isec->call_check_done))
12066 {
12067 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12068 return FALSE;
8b974ba3
AM
12069 }
12070 /* Make all sections use the TOC assigned for this object file.
12071 This will be wrong for pasted sections; We fix that in
12072 check_pasted_section(). */
12073 if (elf_gp (isec->owner) != 0)
12074 htab->toc_curr = elf_gp (isec->owner);
12075 }
12076
6f20ed8a 12077 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12078 return TRUE;
721956f4
AM
12079}
12080
70cc837d
AM
12081/* Check that all .init and .fini sections use the same toc, if they
12082 have toc relocs. */
12083
12084static bfd_boolean
12085check_pasted_section (struct bfd_link_info *info, const char *name)
12086{
12087 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12088
12089 if (o != NULL)
12090 {
12091 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12092 bfd_vma toc_off = 0;
12093 asection *i;
12094
12095 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12096 if (i->has_toc_reloc)
12097 {
12098 if (toc_off == 0)
6f20ed8a
AM
12099 toc_off = htab->sec_info[i->id].toc_off;
12100 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12101 return FALSE;
12102 }
6683a28d
AM
12103
12104 if (toc_off == 0)
12105 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12106 if (i->makes_toc_func_call)
12107 {
6f20ed8a 12108 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12109 break;
12110 }
12111
70cc837d
AM
12112 /* Make sure the whole pasted function uses the same toc offset. */
12113 if (toc_off != 0)
12114 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12115 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12116 }
12117 return TRUE;
12118}
12119
12120bfd_boolean
12121ppc64_elf_check_init_fini (struct bfd_link_info *info)
12122{
12123 return (check_pasted_section (info, ".init")
12124 & check_pasted_section (info, ".fini"));
12125}
12126
721956f4
AM
12127/* See whether we can group stub sections together. Grouping stub
12128 sections may result in fewer stubs. More importantly, we need to
12129 put all .init* and .fini* stubs at the beginning of the .init or
12130 .fini output sections respectively, because glibc splits the
12131 _init and _fini functions into multiple parts. Putting a stub in
12132 the middle of a function is not a good idea. */
12133
6f20ed8a
AM
12134static bfd_boolean
12135group_sections (struct bfd_link_info *info,
4ce794b7
AM
12136 bfd_size_type stub_group_size,
12137 bfd_boolean stubs_always_before_branch)
721956f4 12138{
6f20ed8a
AM
12139 struct ppc_link_hash_table *htab;
12140 asection *osec;
7c8fe5c4
AM
12141 bfd_boolean suppress_size_errors;
12142
6f20ed8a
AM
12143 htab = ppc_hash_table (info);
12144 if (htab == NULL)
12145 return FALSE;
12146
7c8fe5c4 12147 suppress_size_errors = FALSE;
7c8fe5c4
AM
12148 if (stub_group_size == 1)
12149 {
12150 /* Default values. */
12151 if (stubs_always_before_branch)
09f92717 12152 stub_group_size = 0x1e00000;
7c8fe5c4 12153 else
09f92717 12154 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12155 suppress_size_errors = TRUE;
12156 }
12157
6f20ed8a 12158 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12159 {
6f20ed8a
AM
12160 asection *tail;
12161
12162 if (osec->id >= htab->sec_info_arr_size)
12163 continue;
12164
12165 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12166 while (tail != NULL)
721956f4 12167 {
734b6cf9
AM
12168 asection *curr;
12169 asection *prev;
12170 bfd_size_type total;
12171 bfd_boolean big_sec;
12172 bfd_vma curr_toc;
6f20ed8a 12173 struct map_stub *group;
09f92717 12174 bfd_size_type group_size;
734b6cf9
AM
12175
12176 curr = tail;
eea6121a 12177 total = tail->size;
09f92717
AM
12178 group_size = (ppc64_elf_section_data (tail) != NULL
12179 && ppc64_elf_section_data (tail)->has_14bit_branch
12180 ? stub_group_size >> 10 : stub_group_size);
12181
12182 big_sec = total > group_size;
7c8fe5c4 12183 if (big_sec && !suppress_size_errors)
695344c0 12184 /* xgettext:c-format */
4eca0228
AM
12185 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12186 tail->owner, tail);
6f20ed8a 12187 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12188
6f20ed8a 12189 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12190 && ((total += curr->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 curr = prev;
12196
12197 /* OK, the size from the start of CURR to the end is less
09f92717 12198 than group_size and thus can be handled by one stub
734b6cf9 12199 section. (or the tail section is itself larger than
09f92717
AM
12200 group_size, in which case we may be toast.) We should
12201 really be keeping track of the total size of stubs added
12202 here, as stubs contribute to the final output section
12203 size. That's a little tricky, and this way will only
12204 break if stubs added make the total size more than 2^25,
12205 ie. for the default stub_group_size, if stubs total more
12206 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12207 group = bfd_alloc (curr->owner, sizeof (*group));
12208 if (group == NULL)
12209 return FALSE;
12210 group->link_sec = curr;
12211 group->stub_sec = NULL;
a4b6fadd
AM
12212 group->needs_save_res = 0;
12213 group->next = htab->group;
12214 htab->group = group;
734b6cf9 12215 do
721956f4 12216 {
6f20ed8a 12217 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12218 /* Set up this stub group. */
6f20ed8a 12219 htab->sec_info[tail->id].u.group = group;
721956f4 12220 }
734b6cf9
AM
12221 while (tail != curr && (tail = prev) != NULL);
12222
09f92717 12223 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12224 bytes before the stub section can be handled by it too.
12225 Don't do this if we have a really large section after the
12226 stubs, as adding more stubs increases the chance that
12227 branches may not reach into the stub section. */
12228 if (!stubs_always_before_branch && !big_sec)
12229 {
12230 total = 0;
12231 while (prev != NULL
12232 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12233 < (ppc64_elf_section_data (prev) != NULL
12234 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12235 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12236 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12237 {
12238 tail = prev;
6f20ed8a
AM
12239 prev = htab->sec_info[tail->id].u.list;
12240 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12241 }
12242 }
12243 tail = prev;
721956f4
AM
12244 }
12245 }
6f20ed8a 12246 return TRUE;
721956f4
AM
12247}
12248
58d180e8
AM
12249static const unsigned char glink_eh_frame_cie[] =
12250{
12251 0, 0, 0, 16, /* length. */
12252 0, 0, 0, 0, /* id. */
12253 1, /* CIE version. */
12254 'z', 'R', 0, /* Augmentation string. */
12255 4, /* Code alignment. */
12256 0x78, /* Data alignment. */
12257 65, /* RA reg. */
12258 1, /* Augmentation size. */
12259 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 12260 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
12261};
12262
d969d15f
AM
12263/* Stripping output sections is normally done before dynamic section
12264 symbols have been allocated. This function is called later, and
12265 handles cases like htab->brlt which is mapped to its own output
12266 section. */
12267
12268static void
12269maybe_strip_output (struct bfd_link_info *info, asection *isec)
12270{
12271 if (isec->size == 0
12272 && isec->output_section->size == 0
53d8967a 12273 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12274 && !bfd_section_removed_from_list (info->output_bfd,
12275 isec->output_section)
12276 && elf_section_data (isec->output_section)->dynindx == 0)
12277 {
12278 isec->output_section->flags |= SEC_EXCLUDE;
12279 bfd_section_list_remove (info->output_bfd, isec->output_section);
12280 info->output_bfd->section_count--;
12281 }
12282}
12283
721956f4
AM
12284/* Determine and set the size of the stub section for a final link.
12285
12286 The basic idea here is to examine all the relocations looking for
12287 PC-relative calls to a target that is unreachable with a "bl"
12288 instruction. */
12289
b34976b6 12290bfd_boolean
e7d1c40c 12291ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12292{
12293 bfd_size_type stub_group_size;
b34976b6 12294 bfd_boolean stubs_always_before_branch;
721956f4
AM
12295 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12296
4dfe6ac6
NC
12297 if (htab == NULL)
12298 return FALSE;
12299
0e1862bb 12300 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12301 htab->params->plt_thread_safe = 1;
b9e5796b 12302 if (!htab->opd_abi)
e7d1c40c
AM
12303 htab->params->plt_thread_safe = 0;
12304 else if (htab->params->plt_thread_safe == -1)
794e51c0 12305 {
e2458743 12306 static const char *const thread_starter[] =
794e51c0
AM
12307 {
12308 "pthread_create",
12309 /* libstdc++ */
12310 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12311 /* librt */
12312 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12313 "mq_notify", "create_timer",
12314 /* libanl */
12315 "getaddrinfo_a",
12316 /* libgomp */
2300b5a1 12317 "GOMP_parallel",
794e51c0 12318 "GOMP_parallel_start",
2300b5a1 12319 "GOMP_parallel_loop_static",
794e51c0 12320 "GOMP_parallel_loop_static_start",
2300b5a1 12321 "GOMP_parallel_loop_dynamic",
794e51c0 12322 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12323 "GOMP_parallel_loop_guided",
794e51c0 12324 "GOMP_parallel_loop_guided_start",
2300b5a1 12325 "GOMP_parallel_loop_runtime",
794e51c0 12326 "GOMP_parallel_loop_runtime_start",
2300b5a1 12327 "GOMP_parallel_sections",
68ffbac6 12328 "GOMP_parallel_sections_start",
f9dffbf0
AM
12329 /* libgo */
12330 "__go_go",
794e51c0
AM
12331 };
12332 unsigned i;
12333
a4b6fadd 12334 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12335 {
12336 struct elf_link_hash_entry *h;
12337 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12338 FALSE, FALSE, TRUE);
e7d1c40c
AM
12339 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12340 if (htab->params->plt_thread_safe)
794e51c0
AM
12341 break;
12342 }
12343 }
e7d1c40c
AM
12344 stubs_always_before_branch = htab->params->group_size < 0;
12345 if (htab->params->group_size < 0)
12346 stub_group_size = -htab->params->group_size;
721956f4 12347 else
e7d1c40c 12348 stub_group_size = htab->params->group_size;
721956f4 12349
6f20ed8a
AM
12350 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12351 return FALSE;
721956f4 12352
c9301e31
AM
12353#define STUB_SHRINK_ITER 20
12354 /* Loop until no stubs added. After iteration 20 of this loop we may
12355 exit on a stub section shrinking. This is to break out of a
12356 pathological case where adding stubs on one iteration decreases
12357 section gaps (perhaps due to alignment), which then requires
12358 fewer or smaller stubs on the next iteration. */
12359
721956f4
AM
12360 while (1)
12361 {
12362 bfd *input_bfd;
12363 unsigned int bfd_indx;
a4b6fadd 12364 struct map_stub *group;
721956f4 12365 asection *stub_sec;
721956f4
AM
12366
12367 htab->stub_iteration += 1;
721956f4
AM
12368
12369 for (input_bfd = info->input_bfds, bfd_indx = 0;
12370 input_bfd != NULL;
c72f2fb2 12371 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12372 {
12373 Elf_Internal_Shdr *symtab_hdr;
12374 asection *section;
6cdc0ccc 12375 Elf_Internal_Sym *local_syms = NULL;
721956f4 12376
0c8d6e5c 12377 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12378 continue;
12379
721956f4 12380 /* We'll need the symbol table in a second. */
0ffa91dd 12381 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12382 if (symtab_hdr->sh_info == 0)
12383 continue;
12384
721956f4
AM
12385 /* Walk over each section attached to the input bfd. */
12386 for (section = input_bfd->sections;
12387 section != NULL;
12388 section = section->next)
12389 {
721956f4 12390 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12391
12392 /* If there aren't any relocs, then there's nothing more
12393 to do. */
12394 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12395 || (section->flags & SEC_ALLOC) == 0
12396 || (section->flags & SEC_LOAD) == 0
12397 || (section->flags & SEC_CODE) == 0
721956f4
AM
12398 || section->reloc_count == 0)
12399 continue;
12400
12401 /* If this section is a link-once section that will be
12402 discarded, then don't create any stubs. */
12403 if (section->output_section == NULL
927be08e 12404 || section->output_section->owner != info->output_bfd)
721956f4
AM
12405 continue;
12406
1e2f5b6e
AM
12407 /* Get the relocs. */
12408 internal_relocs
4ce794b7 12409 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12410 info->keep_memory);
721956f4 12411 if (internal_relocs == NULL)
1e2f5b6e 12412 goto error_ret_free_local;
721956f4
AM
12413
12414 /* Now examine each relocation. */
12415 irela = internal_relocs;
12416 irelaend = irela + section->reloc_count;
12417 for (; irela < irelaend; irela++)
12418 {
4ce794b7
AM
12419 enum elf_ppc64_reloc_type r_type;
12420 unsigned int r_indx;
721956f4
AM
12421 enum ppc_stub_type stub_type;
12422 struct ppc_stub_hash_entry *stub_entry;
8387904d 12423 asection *sym_sec, *code_sec;
e054468f 12424 bfd_vma sym_value, code_value;
721956f4 12425 bfd_vma destination;
6911b7dc 12426 unsigned long local_off;
8843416a 12427 bfd_boolean ok_dest;
721956f4 12428 struct ppc_link_hash_entry *hash;
8387904d 12429 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12430 struct elf_link_hash_entry *h;
12431 Elf_Internal_Sym *sym;
721956f4
AM
12432 char *stub_name;
12433 const asection *id_sec;
74f0fb50 12434 struct _opd_sec_data *opd;
e054468f 12435 struct plt_entry *plt_ent;
721956f4
AM
12436
12437 r_type = ELF64_R_TYPE (irela->r_info);
12438 r_indx = ELF64_R_SYM (irela->r_info);
12439
4ce794b7 12440 if (r_type >= R_PPC64_max)
721956f4
AM
12441 {
12442 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12443 goto error_ret_free_internal;
721956f4
AM
12444 }
12445
12446 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12447 if (r_type != R_PPC64_REL24
12448 && r_type != R_PPC64_REL14
12449 && r_type != R_PPC64_REL14_BRTAKEN
12450 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12451 continue;
12452
12453 /* Now determine the call target, its name, value,
12454 section. */
411e1bfb
AM
12455 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12456 r_indx, input_bfd))
12457 goto error_ret_free_internal;
12458 hash = (struct ppc_link_hash_entry *) h;
12459
8843416a 12460 ok_dest = FALSE;
8387904d 12461 fdh = NULL;
7fe2b9a6 12462 sym_value = 0;
411e1bfb 12463 if (hash == NULL)
721956f4 12464 {
411e1bfb 12465 sym_value = sym->st_value;
c27b8c2a
AM
12466 if (sym_sec != NULL
12467 && sym_sec->output_section != NULL)
12468 ok_dest = TRUE;
721956f4 12469 }
7fe2b9a6
AM
12470 else if (hash->elf.root.type == bfd_link_hash_defined
12471 || hash->elf.root.type == bfd_link_hash_defweak)
12472 {
12473 sym_value = hash->elf.root.u.def.value;
12474 if (sym_sec->output_section != NULL)
12475 ok_dest = TRUE;
12476 }
12477 else if (hash->elf.root.type == bfd_link_hash_undefweak
12478 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12479 {
99877b66 12480 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12481 use the func descriptor sym instead if it is
12482 defined. */
ceb1f1ef 12483 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12484 && hash->oh != NULL)
8387904d 12485 {
8c5b4e52 12486 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12487 if (fdh->elf.root.type == bfd_link_hash_defined
12488 || fdh->elf.root.type == bfd_link_hash_defweak)
12489 {
12490 sym_sec = fdh->elf.root.u.def.section;
12491 sym_value = fdh->elf.root.u.def.value;
12492 if (sym_sec->output_section != NULL)
12493 ok_dest = TRUE;
12494 }
99877b66
AM
12495 else
12496 fdh = NULL;
8387904d 12497 }
7fe2b9a6
AM
12498 }
12499 else
12500 {
12501 bfd_set_error (bfd_error_bad_value);
12502 goto error_ret_free_internal;
721956f4
AM
12503 }
12504
8843416a 12505 destination = 0;
6911b7dc 12506 local_off = 0;
8843416a
AM
12507 if (ok_dest)
12508 {
12509 sym_value += irela->r_addend;
12510 destination = (sym_value
12511 + sym_sec->output_offset
12512 + sym_sec->output_section->vma);
6911b7dc
AM
12513 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12514 ? hash->elf.other
12515 : sym->st_other);
8843416a
AM
12516 }
12517
8387904d 12518 code_sec = sym_sec;
e054468f 12519 code_value = sym_value;
74f0fb50
AM
12520 opd = get_opd_info (sym_sec);
12521 if (opd != NULL)
8387904d
AM
12522 {
12523 bfd_vma dest;
12524
74f0fb50 12525 if (hash == NULL && opd->adjust != NULL)
8387904d 12526 {
51aecdc5 12527 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12528 if (adjust == -1)
12529 continue;
e054468f 12530 code_value += adjust;
8387904d
AM
12531 sym_value += adjust;
12532 }
12533 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12534 &code_sec, &code_value, FALSE);
8387904d
AM
12535 if (dest != (bfd_vma) -1)
12536 {
12537 destination = dest;
12538 if (fdh != NULL)
12539 {
12540 /* Fixup old ABI sym to point at code
12541 entry. */
99877b66 12542 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12543 hash->elf.root.u.def.section = code_sec;
e054468f 12544 hash->elf.root.u.def.value = code_value;
8387904d
AM
12545 }
12546 }
12547 }
12548
721956f4 12549 /* Determine what (if any) linker stub is needed. */
e054468f 12550 plt_ent = NULL;
721956f4 12551 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12552 &plt_ent, destination,
12553 local_off);
ad8e1ba5
AM
12554
12555 if (stub_type != ppc_stub_plt_call)
12556 {
12557 /* Check whether we need a TOC adjusting stub.
12558 Since the linker pastes together pieces from
12559 different object files when creating the
12560 _init and _fini functions, it may be that a
12561 call to what looks like a local sym is in
12562 fact a call needing a TOC adjustment. */
8387904d
AM
12563 if (code_sec != NULL
12564 && code_sec->output_section != NULL
6f20ed8a
AM
12565 && (htab->sec_info[code_sec->id].toc_off
12566 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12567 && (code_sec->has_toc_reloc
12568 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12569 stub_type = ppc_stub_long_branch_r2off;
12570 }
12571
721956f4
AM
12572 if (stub_type == ppc_stub_none)
12573 continue;
12574
411e1bfb
AM
12575 /* __tls_get_addr calls might be eliminated. */
12576 if (stub_type != ppc_stub_plt_call
12577 && hash != NULL
8387904d
AM
12578 && (hash == htab->tls_get_addr
12579 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12580 && section->has_tls_reloc
12581 && irela != internal_relocs)
12582 {
12583 /* Get tls info. */
f961d9dd 12584 unsigned char *tls_mask;
411e1bfb 12585
3a71aa26 12586 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12587 irela - 1, input_bfd))
12588 goto error_ret_free_internal;
e7b938ca 12589 if (*tls_mask != 0)
411e1bfb
AM
12590 continue;
12591 }
12592
3b421ab3
AM
12593 if (stub_type == ppc_stub_plt_call
12594 && irela + 1 < irelaend
12595 && irela[1].r_offset == irela->r_offset + 4
794e51c0
AM
12596 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12597 {
12598 if (!tocsave_find (htab, INSERT,
12599 &local_syms, irela + 1, input_bfd))
12600 goto error_ret_free_internal;
12601 }
12602 else if (stub_type == ppc_stub_plt_call)
12603 stub_type = ppc_stub_plt_call_r2save;
3b421ab3 12604
721956f4 12605 /* Support for grouping stub sections. */
6f20ed8a 12606 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12607
12608 /* Get the name of this stub. */
12609 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12610 if (!stub_name)
12611 goto error_ret_free_internal;
12612
12613 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12614 stub_name, FALSE, FALSE);
721956f4
AM
12615 if (stub_entry != NULL)
12616 {
12617 /* The proper stub has already been created. */
12618 free (stub_name);
794e51c0
AM
12619 if (stub_type == ppc_stub_plt_call_r2save)
12620 stub_entry->stub_type = stub_type;
721956f4
AM
12621 continue;
12622 }
12623
25f53a85 12624 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12625 if (stub_entry == NULL)
12626 {
12627 free (stub_name);
6cdc0ccc
AM
12628 error_ret_free_internal:
12629 if (elf_section_data (section)->relocs == NULL)
12630 free (internal_relocs);
12631 error_ret_free_local:
12632 if (local_syms != NULL
12633 && (symtab_hdr->contents
12634 != (unsigned char *) local_syms))
12635 free (local_syms);
b34976b6 12636 return FALSE;
721956f4
AM
12637 }
12638
ad8e1ba5 12639 stub_entry->stub_type = stub_type;
794e51c0
AM
12640 if (stub_type != ppc_stub_plt_call
12641 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12642 {
12643 stub_entry->target_value = code_value;
12644 stub_entry->target_section = code_sec;
12645 }
12646 else
12647 {
12648 stub_entry->target_value = sym_value;
12649 stub_entry->target_section = sym_sec;
12650 }
721956f4 12651 stub_entry->h = hash;
e054468f 12652 stub_entry->plt_ent = plt_ent;
6911b7dc 12653 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12654
12655 if (stub_entry->h != NULL)
12656 htab->stub_globals += 1;
721956f4
AM
12657 }
12658
12659 /* We're done with the internal relocs, free them. */
6cdc0ccc 12660 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12661 free (internal_relocs);
721956f4 12662 }
6cdc0ccc
AM
12663
12664 if (local_syms != NULL
12665 && symtab_hdr->contents != (unsigned char *) local_syms)
12666 {
12667 if (!info->keep_memory)
12668 free (local_syms);
12669 else
12670 symtab_hdr->contents = (unsigned char *) local_syms;
12671 }
721956f4
AM
12672 }
12673
5c3dead3 12674 /* We may have added some stubs. Find out the new size of the
721956f4 12675 stub sections. */
e7d1c40c 12676 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
12677 stub_sec != NULL;
12678 stub_sec = stub_sec->next)
e717da7e 12679 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 12680 {
ea3d7d1c
AM
12681 if (htab->stub_iteration <= STUB_SHRINK_ITER
12682 || stub_sec->rawsize < stub_sec->size)
12683 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12684 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12685 stub_sec->size = 0;
12686 stub_sec->reloc_count = 0;
84f5d08e 12687 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12688 }
eea6121a
AM
12689
12690 htab->brlt->size = 0;
84f5d08e
AM
12691 htab->brlt->reloc_count = 0;
12692 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12693 if (htab->relbrlt != NULL)
eea6121a 12694 htab->relbrlt->size = 0;
721956f4 12695
63bc6f6c 12696 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12697
a4b6fadd
AM
12698 for (group = htab->group; group != NULL; group = group->next)
12699 if (group->needs_save_res)
12700 group->stub_sec->size += htab->sfpr->size;
12701
176a0d42
AM
12702 if (info->emitrelocations
12703 && htab->glink != NULL && htab->glink->size != 0)
12704 {
12705 htab->glink->reloc_count = 1;
12706 htab->glink->flags |= SEC_RELOC;
12707 }
12708
58d180e8
AM
12709 if (htab->glink_eh_frame != NULL
12710 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
9a2a56cc 12711 && htab->glink_eh_frame->output_section->size != 0)
58d180e8 12712 {
2e0ce1c8 12713 size_t size = 0, align = 4;
58d180e8 12714
e7d1c40c 12715 for (stub_sec = htab->params->stub_bfd->sections;
58d180e8
AM
12716 stub_sec != NULL;
12717 stub_sec = stub_sec->next)
12718 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
2e0ce1c8 12719 size += (17 + align - 1) & -align;
58d180e8 12720 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 12721 size += (24 + align - 1) & -align;
58d180e8 12722 if (size != 0)
2e0ce1c8
AM
12723 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12724 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12725 size = (size + align - 1) & -align;
58d180e8
AM
12726 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12727 htab->glink_eh_frame->size = size;
12728 }
12729
e7d1c40c
AM
12730 if (htab->params->plt_stub_align != 0)
12731 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
12732 stub_sec != NULL;
12733 stub_sec = stub_sec->next)
12734 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
12735 stub_sec->size = ((stub_sec->size
12736 + (1 << htab->params->plt_stub_align) - 1)
29f628db 12737 & -(1 << htab->params->plt_stub_align));
794e51c0 12738
e7d1c40c 12739 for (stub_sec = htab->params->stub_bfd->sections;
5c3dead3
AM
12740 stub_sec != NULL;
12741 stub_sec = stub_sec->next)
12742 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
c9301e31
AM
12743 && stub_sec->rawsize != stub_sec->size
12744 && (htab->stub_iteration <= STUB_SHRINK_ITER
12745 || stub_sec->rawsize < stub_sec->size))
5c3dead3
AM
12746 break;
12747
58d180e8
AM
12748 if (stub_sec == NULL
12749 && (htab->glink_eh_frame == NULL
12750 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12751 break;
12752
721956f4 12753 /* Ask the linker to do its stuff. */
e7d1c40c 12754 (*htab->params->layout_sections_again) ();
721956f4
AM
12755 }
12756
da44f4e5
AM
12757 if (htab->glink_eh_frame != NULL
12758 && htab->glink_eh_frame->size != 0)
12759 {
12760 bfd_vma val;
12761 bfd_byte *p, *last_fde;
12762 size_t last_fde_len, size, align, pad;
12763 asection *stub_sec;
12764
12765 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12766 if (p == NULL)
12767 return FALSE;
12768 htab->glink_eh_frame->contents = p;
12769 last_fde = p;
2e0ce1c8 12770 align = 4;
da44f4e5
AM
12771
12772 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12773 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 12774 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 12775 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 12776 p += last_fde_len + 4;
da44f4e5
AM
12777
12778 for (stub_sec = htab->params->stub_bfd->sections;
12779 stub_sec != NULL;
12780 stub_sec = stub_sec->next)
12781 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12782 {
12783 last_fde = p;
2e0ce1c8 12784 last_fde_len = ((17 + align - 1) & -align) - 4;
da44f4e5 12785 /* FDE length. */
2e0ce1c8 12786 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12787 p += 4;
12788 /* CIE pointer. */
12789 val = p - htab->glink_eh_frame->contents;
12790 bfd_put_32 (htab->elf.dynobj, val, p);
12791 p += 4;
12792 /* Offset to stub section, written later. */
12793 p += 4;
12794 /* stub section size. */
12795 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12796 p += 4;
12797 /* Augmentation. */
12798 p += 1;
12799 /* Pad. */
2e0ce1c8 12800 p += ((17 + align - 1) & -align) - 17;
da44f4e5
AM
12801 }
12802 if (htab->glink != NULL && htab->glink->size != 0)
12803 {
12804 last_fde = p;
2e0ce1c8 12805 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 12806 /* FDE length. */
2e0ce1c8 12807 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
12808 p += 4;
12809 /* CIE pointer. */
12810 val = p - htab->glink_eh_frame->contents;
12811 bfd_put_32 (htab->elf.dynobj, val, p);
12812 p += 4;
12813 /* Offset to .glink, written later. */
12814 p += 4;
12815 /* .glink size. */
12816 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12817 p += 4;
12818 /* Augmentation. */
12819 p += 1;
12820
12821 *p++ = DW_CFA_advance_loc + 1;
12822 *p++ = DW_CFA_register;
12823 *p++ = 65;
9f08fa5c 12824 *p++ = htab->opd_abi ? 12 : 0;
da44f4e5
AM
12825 *p++ = DW_CFA_advance_loc + 4;
12826 *p++ = DW_CFA_restore_extended;
12827 *p++ = 65;
2e0ce1c8 12828 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
12829 }
12830 /* Subsume any padding into the last FDE if user .eh_frame
12831 sections are aligned more than glink_eh_frame. Otherwise any
12832 zero padding will be seen as a terminator. */
2e0ce1c8 12833 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 12834 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 12835 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
12836 htab->glink_eh_frame->size = size + pad;
12837 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12838 }
12839
d969d15f
AM
12840 maybe_strip_output (info, htab->brlt);
12841 if (htab->glink_eh_frame != NULL)
12842 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12843
b34976b6 12844 return TRUE;
721956f4
AM
12845}
12846
12847/* Called after we have determined section placement. If sections
805fc799 12848 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12849
805fc799 12850bfd_vma
1c865ab2 12851ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12852{
805fc799 12853 asection *s;
a27e685f 12854 bfd_vma TOCstart, adjust;
721956f4 12855
43417696
AM
12856 if (info != NULL)
12857 {
12858 struct elf_link_hash_entry *h;
12859 struct elf_link_hash_table *htab = elf_hash_table (info);
12860
12861 if (is_elf_hash_table (htab)
12862 && htab->hgot != NULL)
12863 h = htab->hgot;
12864 else
12865 {
12866 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12867 if (is_elf_hash_table (htab))
12868 htab->hgot = h;
12869 }
12870 if (h != NULL
12871 && h->root.type == bfd_link_hash_defined
12872 && !h->root.linker_def
12873 && (!is_elf_hash_table (htab)
12874 || h->def_regular))
12875 {
12876 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12877 + h->root.u.def.section->output_offset
12878 + h->root.u.def.section->output_section->vma);
12879 _bfd_set_gp_value (obfd, TOCstart);
12880 return TOCstart;
12881 }
12882 }
12883
805fc799
AM
12884 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12885 order. The TOC starts where the first of these sections starts. */
12886 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12887 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12888 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12889 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12890 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12891 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12892 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12893 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12894 {
12895 /* This may happen for
12896 o references to TOC base (SYM@toc / TOC[tc0]) without a
12897 .toc directive
12898 o bad linker script
12899 o --gc-sections and empty TOC sections
12900
12901 FIXME: Warn user? */
12902
12903 /* Look for a likely section. We probably won't even be
12904 using TOCstart. */
12905 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12906 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12907 | SEC_EXCLUDE))
805fc799
AM
12908 == (SEC_ALLOC | SEC_SMALL_DATA))
12909 break;
721956f4 12910 if (s == NULL)
805fc799 12911 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12912 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12913 == (SEC_ALLOC | SEC_SMALL_DATA))
12914 break;
721956f4 12915 if (s == NULL)
805fc799 12916 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12917 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12918 == SEC_ALLOC)
805fc799 12919 break;
721956f4 12920 if (s == NULL)
805fc799 12921 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12922 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12923 break;
12924 }
721956f4 12925
805fc799
AM
12926 TOCstart = 0;
12927 if (s != NULL)
12928 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12929
a27e685f
AM
12930 /* Force alignment. */
12931 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12932 TOCstart -= adjust;
1c865ab2
AM
12933 _bfd_set_gp_value (obfd, TOCstart);
12934
810d4e75 12935 if (info != NULL && s != NULL)
1c865ab2
AM
12936 {
12937 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12938
810d4e75
AM
12939 if (htab != NULL)
12940 {
12941 if (htab->elf.hgot != NULL)
12942 {
a27e685f 12943 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12944 htab->elf.hgot->root.u.def.section = s;
12945 }
12946 }
12947 else
1c865ab2 12948 {
810d4e75
AM
12949 struct bfd_link_hash_entry *bh = NULL;
12950 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12951 s, TOC_BASE_OFF - adjust,
12952 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12953 }
12954 }
805fc799 12955 return TOCstart;
721956f4
AM
12956}
12957
a345bc8d
AM
12958/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12959 write out any global entry stubs. */
12960
12961static bfd_boolean
12962build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12963{
12964 struct bfd_link_info *info;
12965 struct ppc_link_hash_table *htab;
12966 struct plt_entry *pent;
12967 asection *s;
12968
12969 if (h->root.type == bfd_link_hash_indirect)
12970 return TRUE;
12971
12972 if (!h->pointer_equality_needed)
12973 return TRUE;
12974
12975 if (h->def_regular)
12976 return TRUE;
12977
12978 info = inf;
12979 htab = ppc_hash_table (info);
12980 if (htab == NULL)
12981 return FALSE;
12982
12983 s = htab->glink;
12984 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12985 if (pent->plt.offset != (bfd_vma) -1
12986 && pent->addend == 0)
12987 {
12988 bfd_byte *p;
12989 asection *plt;
12990 bfd_vma off;
12991
a345bc8d 12992 p = s->contents + h->root.u.def.value;
33e44f2e 12993 plt = htab->elf.splt;
a345bc8d
AM
12994 if (!htab->elf.dynamic_sections_created
12995 || h->dynindx == -1)
33e44f2e 12996 plt = htab->elf.iplt;
a345bc8d
AM
12997 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12998 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12999
13000 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13001 {
13002 info->callbacks->einfo
13003 (_("%P: linkage table error against `%T'\n"),
13004 h->root.root.string);
13005 bfd_set_error (bfd_error_bad_value);
13006 htab->stub_error = TRUE;
13007 }
13008
7341d5e2
AM
13009 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13010 if (htab->params->emit_stub_syms)
13011 {
13012 size_t len = strlen (h->root.root.string);
13013 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13014
13015 if (name == NULL)
13016 return FALSE;
13017
13018 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13019 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13020 if (h == NULL)
13021 return FALSE;
13022 if (h->root.type == bfd_link_hash_new)
13023 {
13024 h->root.type = bfd_link_hash_defined;
13025 h->root.u.def.section = s;
13026 h->root.u.def.value = p - s->contents;
13027 h->ref_regular = 1;
13028 h->def_regular = 1;
13029 h->ref_regular_nonweak = 1;
13030 h->forced_local = 1;
13031 h->non_elf = 0;
2ec55de3 13032 h->root.linker_def = 1;
7341d5e2
AM
13033 }
13034 }
13035
a345bc8d
AM
13036 if (PPC_HA (off) != 0)
13037 {
13038 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13039 p += 4;
13040 }
13041 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13042 p += 4;
13043 bfd_put_32 (s->owner, MTCTR_R12, p);
13044 p += 4;
13045 bfd_put_32 (s->owner, BCTR, p);
13046 break;
13047 }
13048 return TRUE;
13049}
13050
721956f4
AM
13051/* Build all the stubs associated with the current output file.
13052 The stubs are kept in a hash table attached to the main linker
13053 hash table. This function is called via gldelf64ppc_finish. */
13054
b34976b6 13055bfd_boolean
e7d1c40c 13056ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13057 char **stats)
5d1634d7
AM
13058{
13059 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13060 struct map_stub *group;
721956f4 13061 asection *stub_sec;
5d1634d7 13062 bfd_byte *p;
e717da7e 13063 int stub_sec_count = 0;
5d1634d7 13064
4dfe6ac6
NC
13065 if (htab == NULL)
13066 return FALSE;
13067
eea6121a 13068 /* Allocate memory to hold the linker stubs. */
e7d1c40c 13069 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
13070 stub_sec != NULL;
13071 stub_sec = stub_sec->next)
eea6121a
AM
13072 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
13073 && stub_sec->size != 0)
e717da7e 13074 {
e7d1c40c 13075 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
13076 if (stub_sec->contents == NULL)
13077 return FALSE;
eea6121a 13078 stub_sec->size = 0;
e717da7e 13079 }
5d1634d7 13080
23eb7e01 13081 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13082 {
9f951329 13083 unsigned int indx;
ad8e1ba5 13084 bfd_vma plt0;
9f951329 13085
721956f4 13086 /* Build the .glink plt call stub. */
e7d1c40c 13087 if (htab->params->emit_stub_syms)
97b639ba
AM
13088 {
13089 struct elf_link_hash_entry *h;
468392fb
AM
13090 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13091 TRUE, FALSE, FALSE);
97b639ba
AM
13092 if (h == NULL)
13093 return FALSE;
13094 if (h->root.type == bfd_link_hash_new)
13095 {
13096 h->root.type = bfd_link_hash_defined;
13097 h->root.u.def.section = htab->glink;
ee4bf8d2 13098 h->root.u.def.value = 8;
f5385ebf
AM
13099 h->ref_regular = 1;
13100 h->def_regular = 1;
13101 h->ref_regular_nonweak = 1;
13102 h->forced_local = 1;
13103 h->non_elf = 0;
2ec55de3 13104 h->root.linker_def = 1;
97b639ba
AM
13105 }
13106 }
33e44f2e
AM
13107 plt0 = (htab->elf.splt->output_section->vma
13108 + htab->elf.splt->output_offset
13109 - 16);
176a0d42
AM
13110 if (info->emitrelocations)
13111 {
13112 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13113 if (r == NULL)
13114 return FALSE;
13115 r->r_offset = (htab->glink->output_offset
13116 + htab->glink->output_section->vma);
13117 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13118 r->r_addend = plt0;
13119 }
4ce794b7 13120 p = htab->glink->contents;
176a0d42 13121 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13122 bfd_put_64 (htab->glink->owner, plt0, p);
13123 p += 8;
b9e5796b
AM
13124 if (htab->opd_abi)
13125 {
13126 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13127 p += 4;
13128 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13129 p += 4;
13130 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13131 p += 4;
13132 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13133 p += 4;
13134 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13135 p += 4;
13136 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13137 p += 4;
13138 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13139 p += 4;
13140 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13141 p += 4;
13142 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13143 p += 4;
13144 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13145 p += 4;
13146 }
13147 else
13148 {
13149 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13150 p += 4;
13151 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13152 p += 4;
13153 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13154 p += 4;
13155 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13156 p += 4;
13157 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13158 p += 4;
13159 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13160 p += 4;
13161 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13162 p += 4;
13163 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13164 p += 4;
13165 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13166 p += 4;
13167 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13168 p += 4;
13169 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13170 p += 4;
13171 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13172 p += 4;
13173 }
4ce794b7 13174 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 13175 p += 4;
ee4bf8d2
AM
13176 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13177 {
13178 bfd_put_32 (htab->glink->owner, NOP, p);
13179 p += 4;
13180 }
ad8e1ba5 13181
9f951329
AM
13182 /* Build the .glink lazy link call stubs. */
13183 indx = 0;
a345bc8d 13184 while (p < htab->glink->contents + htab->glink->rawsize)
9f951329 13185 {
b9e5796b 13186 if (htab->opd_abi)
9f951329 13187 {
b9e5796b
AM
13188 if (indx < 0x8000)
13189 {
13190 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13191 p += 4;
13192 }
13193 else
13194 {
13195 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13196 p += 4;
13197 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13198 p);
13199 p += 4;
13200 }
9f951329 13201 }
4ce794b7 13202 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13203 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13204 indx++;
9f951329
AM
13205 p += 4;
13206 }
a345bc8d
AM
13207
13208 /* Build .glink global entry stubs. */
13209 if (htab->glink->size > htab->glink->rawsize)
afe397ea 13210 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
5d1634d7 13211 }
5d1634d7 13212
7341d5e2 13213 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13214 {
4ce794b7 13215 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13216 htab->brlt->size);
4ce794b7 13217 if (htab->brlt->contents == NULL)
b34976b6 13218 return FALSE;
721956f4 13219 }
ee75fd95 13220 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13221 {
13222 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13223 htab->relbrlt->size);
63bc6f6c
AM
13224 if (htab->relbrlt->contents == NULL)
13225 return FALSE;
13226 }
5d1634d7 13227
721956f4
AM
13228 /* Build the stubs as directed by the stub hash table. */
13229 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13230
a4b6fadd
AM
13231 for (group = htab->group; group != NULL; group = group->next)
13232 if (group->needs_save_res)
13233 {
13234 stub_sec = group->stub_sec;
13235 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13236 htab->sfpr->size);
13237 if (htab->params->emit_stub_syms)
13238 {
13239 unsigned int i;
13240
13241 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13242 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13243 return FALSE;
13244 }
13245 stub_sec->size += htab->sfpr->size;
13246 }
13247
aa8a7074
AM
13248 if (htab->relbrlt != NULL)
13249 htab->relbrlt->reloc_count = 0;
13250
e7d1c40c
AM
13251 if (htab->params->plt_stub_align != 0)
13252 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
13253 stub_sec != NULL;
13254 stub_sec = stub_sec->next)
13255 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
13256 stub_sec->size = ((stub_sec->size
13257 + (1 << htab->params->plt_stub_align) - 1)
29f628db 13258 & -(1 << htab->params->plt_stub_align));
794e51c0 13259
e7d1c40c 13260 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
13261 stub_sec != NULL;
13262 stub_sec = stub_sec->next)
e717da7e
AM
13263 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13264 {
13265 stub_sec_count += 1;
c9301e31
AM
13266 if (stub_sec->rawsize != stub_sec->size
13267 && (htab->stub_iteration <= STUB_SHRINK_ITER
13268 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13269 break;
13270 }
5d1634d7 13271
da44f4e5
AM
13272 /* Note that the glink_eh_frame check here is not only testing that
13273 the generated size matched the calculated size but also that
13274 bfd_elf_discard_info didn't make any changes to the section. */
721956f4 13275 if (stub_sec != NULL
58d180e8
AM
13276 || (htab->glink_eh_frame != NULL
13277 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 13278 {
b34976b6 13279 htab->stub_error = TRUE;
8de848d8 13280 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 13281 }
721956f4 13282
d2a300cf
AM
13283 if (htab->stub_error)
13284 return FALSE;
13285
13286 if (stats != NULL)
13287 {
13288 *stats = bfd_malloc (500);
13289 if (*stats == NULL)
13290 return FALSE;
13291
ee75fd95 13292 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
13293 " branch %lu\n"
13294 " toc adjust %lu\n"
13295 " long branch %lu\n"
13296 " long toc adj %lu\n"
794e51c0 13297 " plt call %lu\n"
7341d5e2
AM
13298 " plt call toc %lu\n"
13299 " global entry %lu"),
e717da7e 13300 stub_sec_count,
ee75fd95 13301 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
13302 htab->stub_count[ppc_stub_long_branch - 1],
13303 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13304 htab->stub_count[ppc_stub_plt_branch - 1],
13305 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13306 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13307 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13308 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13309 }
13310 return TRUE;
5bd4f169
AM
13311}
13312
60124e18
AM
13313/* What to do when ld finds relocations against symbols defined in
13314 discarded sections. */
13315
13316static unsigned int
13317ppc64_elf_action_discarded (asection *sec)
13318{
13319 if (strcmp (".opd", sec->name) == 0)
13320 return 0;
13321
13322 if (strcmp (".toc", sec->name) == 0)
13323 return 0;
13324
bce50a28
JJ
13325 if (strcmp (".toc1", sec->name) == 0)
13326 return 0;
13327
60124e18
AM
13328 return _bfd_elf_default_action_discarded (sec);
13329}
13330
5bd4f169
AM
13331/* The RELOCATE_SECTION function is called by the ELF backend linker
13332 to handle the relocations for a section.
13333
13334 The relocs are always passed as Rela structures; if the section
13335 actually uses Rel structures, the r_addend field will always be
13336 zero.
13337
13338 This function is responsible for adjust the section contents as
13339 necessary, and (if using Rela relocs and generating a
1049f94e 13340 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13341 necessary.
13342
13343 This function does not have to worry about setting the reloc
13344 address or the reloc symbol index.
13345
13346 LOCAL_SYMS is a pointer to the swapped in local symbols.
13347
13348 LOCAL_SECTIONS is an array giving the section in the input file
13349 corresponding to the st_shndx field of each local symbol.
13350
13351 The global hash table entry for the global symbols can be found
13352 via elf_sym_hashes (input_bfd).
13353
1049f94e 13354 When generating relocatable output, this function must handle
5bd4f169
AM
13355 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13356 going to be the section symbol corresponding to the output
13357 section, which means that the addend must be adjusted
13358 accordingly. */
13359
b34976b6 13360static bfd_boolean
4ce794b7
AM
13361ppc64_elf_relocate_section (bfd *output_bfd,
13362 struct bfd_link_info *info,
13363 bfd *input_bfd,
13364 asection *input_section,
13365 bfd_byte *contents,
13366 Elf_Internal_Rela *relocs,
13367 Elf_Internal_Sym *local_syms,
13368 asection **local_sections)
5bd4f169 13369{
65f38f15 13370 struct ppc_link_hash_table *htab;
5bd4f169
AM
13371 Elf_Internal_Shdr *symtab_hdr;
13372 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13373 Elf_Internal_Rela *rel;
c316a17c 13374 Elf_Internal_Rela *wrel;
5bd4f169 13375 Elf_Internal_Rela *relend;
411e1bfb
AM
13376 Elf_Internal_Rela outrel;
13377 bfd_byte *loc;
411e1bfb 13378 struct got_entry **local_got_ents;
5bd4f169 13379 bfd_vma TOCstart;
b34976b6
AM
13380 bfd_boolean ret = TRUE;
13381 bfd_boolean is_opd;
794e51c0
AM
13382 /* Assume 'at' branch hints. */
13383 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13384 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13385
65f38f15 13386 /* Initialize howto table if needed. */
5bd4f169 13387 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13388 ppc_howto_init ();
13389
65f38f15 13390 htab = ppc_hash_table (info);
4dfe6ac6
NC
13391 if (htab == NULL)
13392 return FALSE;
ee75fd95
AM
13393
13394 /* Don't relocate stub sections. */
e7d1c40c 13395 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13396 return TRUE;
13397
0c8d6e5c 13398 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13399
411e1bfb 13400 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13401 TOCstart = elf_gp (output_bfd);
0ffa91dd 13402 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13403 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13404 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13405
c316a17c 13406 rel = wrel = relocs;
5bd4f169 13407 relend = relocs + input_section->reloc_count;
c316a17c 13408 for (; rel < relend; wrel++, rel++)
5bd4f169 13409 {
04c9666a 13410 enum elf_ppc64_reloc_type r_type;
31c76678 13411 bfd_vma addend;
5bd4f169
AM
13412 bfd_reloc_status_type r;
13413 Elf_Internal_Sym *sym;
13414 asection *sec;
039b3fef
AM
13415 struct elf_link_hash_entry *h_elf;
13416 struct ppc_link_hash_entry *h;
13417 struct ppc_link_hash_entry *fdh;
5bd4f169 13418 const char *sym_name;
0d4792f7 13419 unsigned long r_symndx, toc_symndx;
3a71aa26 13420 bfd_vma toc_addend;
f961d9dd
AM
13421 unsigned char tls_mask, tls_gd, tls_type;
13422 unsigned char sym_type;
5bd4f169 13423 bfd_vma relocation;
b34976b6
AM
13424 bfd_boolean unresolved_reloc;
13425 bfd_boolean warned;
bc30df16 13426 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13427 unsigned int insn;
e11840f9 13428 unsigned int mask;
721956f4
AM
13429 struct ppc_stub_hash_entry *stub_entry;
13430 bfd_vma max_br_offset;
13431 bfd_vma from;
c316a17c 13432 Elf_Internal_Rela orig_rel;
b80eed39
AM
13433 reloc_howto_type *howto;
13434 struct reloc_howto_struct alt_howto;
5bd4f169 13435
c316a17c
AM
13436 again:
13437 orig_rel = *rel;
13438
4ce794b7 13439 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13440 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13441
13442 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13443 symbol of the previous ADDR64 reloc. The symbol gives us the
13444 proper TOC base to use. */
13445 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13446 && wrel != relocs
13447 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13448 && is_opd)
c316a17c 13449 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13450
4ce794b7
AM
13451 sym = NULL;
13452 sec = NULL;
039b3fef 13453 h_elf = NULL;
4ce794b7 13454 sym_name = NULL;
b34976b6
AM
13455 unresolved_reloc = FALSE;
13456 warned = FALSE;
65f38f15 13457
0b13192e 13458 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13459 {
13460 /* It's a local symbol. */
74f0fb50 13461 struct _opd_sec_data *opd;
4025353c 13462
5bd4f169
AM
13463 sym = local_syms + r_symndx;
13464 sec = local_sections[r_symndx];
26c61ae5 13465 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13466 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13467 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13468 opd = get_opd_info (sec);
13469 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13470 {
51aecdc5
AM
13471 long adjust = opd->adjust[OPD_NDX (sym->st_value
13472 + rel->r_addend)];
4025353c
AM
13473 if (adjust == -1)
13474 relocation = 0;
13475 else
4cc603a5
AM
13476 {
13477 /* If this is a relocation against the opd section sym
13478 and we have edited .opd, adjust the reloc addend so
13479 that ld -r and ld --emit-relocs output is correct.
13480 If it is a reloc against some other .opd symbol,
13481 then the symbol value will be adjusted later. */
13482 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13483 rel->r_addend += adjust;
13484 else
13485 relocation += adjust;
13486 }
1e2f5b6e 13487 }
5bd4f169
AM
13488 }
13489 else
13490 {
62d887d4
L
13491 bfd_boolean ignored;
13492
b2a8e766
AM
13493 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13494 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13495 h_elf, sec, relocation,
62d887d4 13496 unresolved_reloc, warned, ignored);
039b3fef
AM
13497 sym_name = h_elf->root.root.string;
13498 sym_type = h_elf->type;
b69fdb4e
AM
13499 if (sec != NULL
13500 && sec->owner == output_bfd
13501 && strcmp (sec->name, ".opd") == 0)
13502 {
13503 /* This is a symbol defined in a linker script. All
13504 such are defined in output sections, even those
13505 defined by simple assignment from a symbol defined in
13506 an input section. Transfer the symbol to an
13507 appropriate input .opd section, so that a branch to
13508 this symbol will be mapped to the location specified
13509 by the opd entry. */
13510 struct bfd_link_order *lo;
13511 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13512 if (lo->type == bfd_indirect_link_order)
13513 {
13514 asection *isec = lo->u.indirect.section;
13515 if (h_elf->root.u.def.value >= isec->output_offset
13516 && h_elf->root.u.def.value < (isec->output_offset
13517 + isec->size))
13518 {
13519 h_elf->root.u.def.value -= isec->output_offset;
13520 h_elf->root.u.def.section = isec;
13521 sec = isec;
13522 break;
13523 }
13524 }
13525 }
5bd4f169 13526 }
039b3fef 13527 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13528
dbaa2011 13529 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13530 {
13531 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13532 input_bfd, input_section,
13533 contents + rel->r_offset);
13534 wrel->r_offset = rel->r_offset;
13535 wrel->r_info = 0;
13536 wrel->r_addend = 0;
13537
13538 /* For ld -r, remove relocations in debug sections against
13539 sections defined in discarded sections. Not done for
13540 non-debug to preserve relocs in .eh_frame which the
13541 eh_frame editing code expects to be present. */
13542 if (bfd_link_relocatable (info)
13543 && (input_section->flags & SEC_DEBUGGING))
13544 wrel--;
13545
13546 continue;
13547 }
ab96bf03 13548
0e1862bb 13549 if (bfd_link_relocatable (info))
c316a17c 13550 goto copy_reloc;
ab96bf03 13551
f40da81b
AM
13552 if (h != NULL && &h->elf == htab->elf.hgot)
13553 {
6f20ed8a 13554 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13555 sec = bfd_abs_section_ptr;
13556 unresolved_reloc = FALSE;
13557 }
13558
951fd09b
AM
13559 /* TLS optimizations. Replace instruction sequences and relocs
13560 based on information we collected in tls_optimize. We edit
13561 RELOCS so that --emit-relocs will output something sensible
13562 for the final instruction stream. */
13563 tls_mask = 0;
13564 tls_gd = 0;
0d4792f7 13565 toc_symndx = 0;
727fc41e
AM
13566 if (h != NULL)
13567 tls_mask = h->tls_mask;
13568 else if (local_got_ents != NULL)
411e1bfb 13569 {
e054468f
AM
13570 struct plt_entry **local_plt = (struct plt_entry **)
13571 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13572 unsigned char *lgot_masks = (unsigned char *)
e054468f 13573 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13574 tls_mask = lgot_masks[r_symndx];
13575 }
13576 if (tls_mask == 0
13577 && (r_type == R_PPC64_TLS
13578 || r_type == R_PPC64_TLSGD
13579 || r_type == R_PPC64_TLSLD))
13580 {
13581 /* Check for toc tls entries. */
f961d9dd 13582 unsigned char *toc_tls;
0d4792f7 13583
727fc41e
AM
13584 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13585 &local_syms, rel, input_bfd))
13586 return FALSE;
0d4792f7 13587
727fc41e
AM
13588 if (toc_tls)
13589 tls_mask = *toc_tls;
0d4792f7
AM
13590 }
13591
13592 /* Check that tls relocs are used with tls syms, and non-tls
13593 relocs are used with non-tls syms. */
cf35638d 13594 if (r_symndx != STN_UNDEF
0d4792f7
AM
13595 && r_type != R_PPC64_NONE
13596 && (h == NULL
039b3fef
AM
13597 || h->elf.root.type == bfd_link_hash_defined
13598 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13599 && (IS_PPC64_TLS_RELOC (r_type)
13600 != (sym_type == STT_TLS
13601 || (sym_type == STT_SECTION
13602 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13603 {
727fc41e
AM
13604 if (tls_mask != 0
13605 && (r_type == R_PPC64_TLS
13606 || r_type == R_PPC64_TLSGD
13607 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13608 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13609 ;
13610 else
25f53a85 13611 info->callbacks->einfo
1d483afe 13612 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 13613 /* xgettext:c-format */
174d0a74 13614 ? _("%H: %s used with TLS symbol `%T'\n")
695344c0 13615 /* xgettext:c-format */
174d0a74 13616 : _("%H: %s used with non-TLS symbol `%T'\n"),
25f53a85 13617 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13618 ppc64_elf_howto_table[r_type]->name,
13619 sym_name);
411e1bfb
AM
13620 }
13621
13622 /* Ensure reloc mapping code below stays sane. */
13623 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13624 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13625 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13626 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13627 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13628 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13629 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13630 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13631 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13632 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13633 abort ();
0d4792f7 13634
411e1bfb
AM
13635 switch (r_type)
13636 {
13637 default:
411e1bfb
AM
13638 break;
13639
ba761f19 13640 case R_PPC64_LO_DS_OPT:
95f0d0d2 13641 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
13642 if ((insn & (0x3f << 26)) != 58u << 26)
13643 abort ();
13644 insn += (14u << 26) - (58u << 26);
95f0d0d2 13645 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
13646 r_type = R_PPC64_TOC16_LO;
13647 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13648 break;
13649
411e1bfb
AM
13650 case R_PPC64_TOC16:
13651 case R_PPC64_TOC16_LO:
13652 case R_PPC64_TOC16_DS:
13653 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13654 {
13655 /* Check for toc tls entries. */
f961d9dd 13656 unsigned char *toc_tls;
951fd09b 13657 int retval;
411e1bfb 13658
3a71aa26
AM
13659 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13660 &local_syms, rel, input_bfd);
951fd09b 13661 if (retval == 0)
411e1bfb
AM
13662 return FALSE;
13663
13664 if (toc_tls)
13665 {
951fd09b 13666 tls_mask = *toc_tls;
411e1bfb
AM
13667 if (r_type == R_PPC64_TOC16_DS
13668 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
13669 {
13670 if (tls_mask != 0
13671 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13672 goto toctprel;
13673 }
411e1bfb 13674 else
951fd09b
AM
13675 {
13676 /* If we found a GD reloc pair, then we might be
13677 doing a GD->IE transition. */
13678 if (retval == 2)
13679 {
13680 tls_gd = TLS_TPRELGD;
13681 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13682 goto tls_ldgd_opt;
951fd09b
AM
13683 }
13684 else if (retval == 3)
13685 {
13686 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 13687 goto tls_ldgd_opt;
951fd09b
AM
13688 }
13689 }
411e1bfb
AM
13690 }
13691 }
13692 break;
13693
9d6ded02
AM
13694 case R_PPC64_GOT_TPREL16_HI:
13695 case R_PPC64_GOT_TPREL16_HA:
13696 if (tls_mask != 0
13697 && (tls_mask & TLS_TPREL) == 0)
13698 {
13699 rel->r_offset -= d_offset;
95f0d0d2 13700 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
13701 r_type = R_PPC64_NONE;
13702 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13703 }
13704 break;
13705
411e1bfb
AM
13706 case R_PPC64_GOT_TPREL16_DS:
13707 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13708 if (tls_mask != 0
13709 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13710 {
81407a69 13711 toctprel:
95f0d0d2 13712 insn = bfd_get_32 (input_bfd,
c316a17c 13713 contents + rel->r_offset - d_offset);
411e1bfb
AM
13714 insn &= 31 << 21;
13715 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 13716 bfd_put_32 (input_bfd, insn,
c316a17c 13717 contents + rel->r_offset - d_offset);
411e1bfb 13718 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13719 if (toc_symndx != 0)
13720 {
13721 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13722 rel->r_addend = toc_addend;
0d4792f7
AM
13723 /* We changed the symbol. Start over in order to
13724 get h, sym, sec etc. right. */
c316a17c 13725 goto again;
0d4792f7
AM
13726 }
13727 else
13728 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13729 }
13730 break;
13731
13732 case R_PPC64_TLS:
951fd09b
AM
13733 if (tls_mask != 0
13734 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13735 {
95f0d0d2 13736 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
13737 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13738 if (insn == 0)
411e1bfb 13739 abort ();
95f0d0d2 13740 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 13741 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13742 PPC64_TPREL16_LO which is at low-order half-word. */
13743 rel->r_offset += d_offset;
0d4792f7
AM
13744 r_type = R_PPC64_TPREL16_LO;
13745 if (toc_symndx != 0)
13746 {
13747 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13748 rel->r_addend = toc_addend;
0d4792f7
AM
13749 /* We changed the symbol. Start over in order to
13750 get h, sym, sec etc. right. */
c316a17c 13751 goto again;
0d4792f7
AM
13752 }
13753 else
13754 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13755 }
13756 break;
13757
411e1bfb
AM
13758 case R_PPC64_GOT_TLSGD16_HI:
13759 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13760 tls_gd = TLS_TPRELGD;
13761 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13762 goto tls_gdld_hi;
13763 break;
13764
411e1bfb
AM
13765 case R_PPC64_GOT_TLSLD16_HI:
13766 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13767 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13768 {
951fd09b
AM
13769 tls_gdld_hi:
13770 if ((tls_mask & tls_gd) != 0)
13771 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13772 + R_PPC64_GOT_TPREL16_DS);
13773 else
411e1bfb 13774 {
4fe5ca5b 13775 rel->r_offset -= d_offset;
95f0d0d2 13776 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 13777 r_type = R_PPC64_NONE;
411e1bfb 13778 }
951fd09b 13779 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13780 }
13781 break;
13782
951fd09b
AM
13783 case R_PPC64_GOT_TLSGD16:
13784 case R_PPC64_GOT_TLSGD16_LO:
13785 tls_gd = TLS_TPRELGD;
13786 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13787 goto tls_ldgd_opt;
951fd09b 13788 break;
411e1bfb 13789
951fd09b
AM
13790 case R_PPC64_GOT_TLSLD16:
13791 case R_PPC64_GOT_TLSLD16_LO:
13792 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13793 {
3a71aa26 13794 unsigned int insn1, insn2, insn3;
102890f0
AM
13795 bfd_vma offset;
13796
13797 tls_ldgd_opt:
727fc41e
AM
13798 offset = (bfd_vma) -1;
13799 /* If not using the newer R_PPC64_TLSGD/LD to mark
13800 __tls_get_addr calls, we must trust that the call
13801 stays with its arg setup insns, ie. that the next
13802 reloc is the __tls_get_addr call associated with
13803 the current reloc. Edit both insns. */
13804 if (input_section->has_tls_get_addr_call
13805 && rel + 1 < relend
13806 && branch_reloc_hash_match (input_bfd, rel + 1,
13807 htab->tls_get_addr,
13808 htab->tls_get_addr_fd))
13809 offset = rel[1].r_offset;
b86ac8e3
AM
13810 /* We read the low GOT_TLS (or TOC16) insn because we
13811 need to keep the destination reg. It may be
13812 something other than the usual r3, and moved to r3
13813 before the call by intervening code. */
95f0d0d2 13814 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 13815 contents + rel->r_offset - d_offset);
102890f0 13816 if ((tls_mask & tls_gd) != 0)
411e1bfb 13817 {
102890f0 13818 /* IE */
b86ac8e3 13819 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13820 insn1 |= 58 << 26; /* ld */
13821 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13822 if (offset != (bfd_vma) -1)
f58d5a2d 13823 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13824 if ((tls_mask & TLS_EXPLICIT) == 0)
13825 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13826 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13827 else
102890f0
AM
13828 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13829 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13830 }
13831 else
13832 {
13833 /* LE */
b86ac8e3
AM
13834 insn1 &= 0x1f << 21;
13835 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13836 insn2 = 0x38630000; /* addi 3,3,0 */
13837 if (tls_gd == 0)
951fd09b 13838 {
102890f0 13839 /* Was an LD reloc. */
1d483afe
AM
13840 if (toc_symndx)
13841 sec = local_sections[toc_symndx];
13842 for (r_symndx = 0;
13843 r_symndx < symtab_hdr->sh_info;
13844 r_symndx++)
13845 if (local_sections[r_symndx] == sec)
13846 break;
13847 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13848 r_symndx = STN_UNDEF;
102890f0 13849 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13850 if (r_symndx != STN_UNDEF)
1d483afe
AM
13851 rel->r_addend -= (local_syms[r_symndx].st_value
13852 + sec->output_offset
13853 + sec->output_section->vma);
951fd09b 13854 }
102890f0 13855 else if (toc_symndx != 0)
3a71aa26
AM
13856 {
13857 r_symndx = toc_symndx;
13858 rel->r_addend = toc_addend;
13859 }
102890f0
AM
13860 r_type = R_PPC64_TPREL16_HA;
13861 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13862 if (offset != (bfd_vma) -1)
13863 {
13864 rel[1].r_info = ELF64_R_INFO (r_symndx,
13865 R_PPC64_TPREL16_LO);
13866 rel[1].r_offset = offset + d_offset;
13867 rel[1].r_addend = rel->r_addend;
13868 }
102890f0 13869 }
95f0d0d2 13870 bfd_put_32 (input_bfd, insn1,
3a71aa26 13871 contents + rel->r_offset - d_offset);
727fc41e
AM
13872 if (offset != (bfd_vma) -1)
13873 {
95f0d0d2 13874 insn3 = bfd_get_32 (input_bfd,
727fc41e
AM
13875 contents + offset + 4);
13876 if (insn3 == NOP
13877 || insn3 == CROR_151515 || insn3 == CROR_313131)
13878 {
13879 rel[1].r_offset += 4;
95f0d0d2 13880 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
727fc41e
AM
13881 insn2 = NOP;
13882 }
95f0d0d2 13883 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
13884 }
13885 if ((tls_mask & tls_gd) == 0
13886 && (tls_gd == 0 || toc_symndx != 0))
13887 {
13888 /* We changed the symbol. Start over in order
13889 to get h, sym, sec etc. right. */
c316a17c 13890 goto again;
727fc41e
AM
13891 }
13892 }
13893 break;
13894
13895 case R_PPC64_TLSGD:
13896 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13897 {
13898 unsigned int insn2, insn3;
13899 bfd_vma offset = rel->r_offset;
13900
13901 if ((tls_mask & TLS_TPRELGD) != 0)
13902 {
13903 /* IE */
13904 r_type = R_PPC64_NONE;
13905 insn2 = 0x7c636a14; /* add 3,3,13 */
13906 }
13907 else
13908 {
13909 /* LE */
13910 if (toc_symndx != 0)
13911 {
13912 r_symndx = toc_symndx;
13913 rel->r_addend = toc_addend;
13914 }
13915 r_type = R_PPC64_TPREL16_LO;
13916 rel->r_offset = offset + d_offset;
13917 insn2 = 0x38630000; /* addi 3,3,0 */
13918 }
13919 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13920 /* Zap the reloc on the _tls_get_addr call too. */
13921 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13922 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 13923 insn3 = bfd_get_32 (input_bfd,
3a71aa26 13924 contents + offset + 4);
102890f0
AM
13925 if (insn3 == NOP
13926 || insn3 == CROR_151515 || insn3 == CROR_313131)
13927 {
727fc41e 13928 rel->r_offset += 4;
95f0d0d2 13929 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
3a71aa26 13930 insn2 = NOP;
102890f0 13931 }
95f0d0d2 13932 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 13933 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13934 goto again;
411e1bfb 13935 }
411e1bfb
AM
13936 break;
13937
727fc41e
AM
13938 case R_PPC64_TLSLD:
13939 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13940 {
13941 unsigned int insn2, insn3;
13942 bfd_vma offset = rel->r_offset;
13943
13944 if (toc_symndx)
13945 sec = local_sections[toc_symndx];
13946 for (r_symndx = 0;
13947 r_symndx < symtab_hdr->sh_info;
13948 r_symndx++)
13949 if (local_sections[r_symndx] == sec)
13950 break;
13951 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13952 r_symndx = STN_UNDEF;
727fc41e 13953 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13954 if (r_symndx != STN_UNDEF)
727fc41e
AM
13955 rel->r_addend -= (local_syms[r_symndx].st_value
13956 + sec->output_offset
13957 + sec->output_section->vma);
13958
13959 r_type = R_PPC64_TPREL16_LO;
13960 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13961 rel->r_offset = offset + d_offset;
13962 /* Zap the reloc on the _tls_get_addr call too. */
13963 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13964 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 13965 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 13966 insn3 = bfd_get_32 (input_bfd,
727fc41e
AM
13967 contents + offset + 4);
13968 if (insn3 == NOP
13969 || insn3 == CROR_151515 || insn3 == CROR_313131)
13970 {
13971 rel->r_offset += 4;
95f0d0d2 13972 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
727fc41e
AM
13973 insn2 = NOP;
13974 }
95f0d0d2 13975 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 13976 goto again;
727fc41e
AM
13977 }
13978 break;
13979
411e1bfb 13980 case R_PPC64_DTPMOD64:
951fd09b
AM
13981 if (rel + 1 < relend
13982 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13983 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13984 {
951fd09b
AM
13985 if ((tls_mask & TLS_GD) == 0)
13986 {
13987 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13988 if ((tls_mask & TLS_TPRELGD) != 0)
13989 r_type = R_PPC64_TPREL64;
13990 else
13991 {
4ce794b7 13992 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13993 r_type = R_PPC64_NONE;
13994 }
13995 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13996 }
13997 }
13998 else
13999 {
14000 if ((tls_mask & TLS_LD) == 0)
411e1bfb 14001 {
4ce794b7 14002 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 14003 r_type = R_PPC64_NONE;
951fd09b 14004 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 14005 }
411e1bfb
AM
14006 }
14007 break;
14008
14009 case R_PPC64_TPREL64:
951fd09b 14010 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
14011 {
14012 r_type = R_PPC64_NONE;
14013 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14014 }
14015 break;
52a82034 14016
006589cf
AM
14017 case R_PPC64_ENTRY:
14018 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14019 if (!bfd_link_pic (info)
14020 && !info->traditional_format
14021 && relocation + 0x80008000 <= 0xffffffff)
14022 {
14023 unsigned int insn1, insn2;
14024
14025 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14026 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14027 if ((insn1 & ~0xfffc) == LD_R2_0R12
14028 && insn2 == ADD_R2_R2_R12)
14029 {
95f0d0d2 14030 bfd_put_32 (input_bfd,
006589cf
AM
14031 LIS_R2 + PPC_HA (relocation),
14032 contents + rel->r_offset);
95f0d0d2 14033 bfd_put_32 (input_bfd,
006589cf
AM
14034 ADDI_R2_R2 + PPC_LO (relocation),
14035 contents + rel->r_offset + 4);
14036 }
14037 }
14038 else
14039 {
14040 relocation -= (rel->r_offset
14041 + input_section->output_offset
14042 + input_section->output_section->vma);
14043 if (relocation + 0x80008000 <= 0xffffffff)
14044 {
14045 unsigned int insn1, insn2;
14046
14047 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14048 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14049 if ((insn1 & ~0xfffc) == LD_R2_0R12
14050 && insn2 == ADD_R2_R2_R12)
14051 {
95f0d0d2 14052 bfd_put_32 (input_bfd,
006589cf
AM
14053 ADDIS_R2_R12 + PPC_HA (relocation),
14054 contents + rel->r_offset);
95f0d0d2 14055 bfd_put_32 (input_bfd,
006589cf
AM
14056 ADDI_R2_R2 + PPC_LO (relocation),
14057 contents + rel->r_offset + 4);
14058 }
14059 }
14060 }
14061 break;
14062
52a82034
AM
14063 case R_PPC64_REL16_HA:
14064 /* If we are generating a non-PIC executable, edit
14065 . 0: addis 2,12,.TOC.-0b@ha
14066 . addi 2,2,.TOC.-0b@l
14067 used by ELFv2 global entry points to set up r2, to
14068 . lis 2,.TOC.@ha
14069 . addi 2,2,.TOC.@l
14070 if .TOC. is in range. */
0e1862bb 14071 if (!bfd_link_pic (info)
810d4e75 14072 && !info->traditional_format
006589cf 14073 && !htab->opd_abi
4f038ee5 14074 && rel->r_addend == d_offset
52a82034
AM
14075 && h != NULL && &h->elf == htab->elf.hgot
14076 && rel + 1 < relend
14077 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14078 && rel[1].r_offset == rel->r_offset + 4
14079 && rel[1].r_addend == rel->r_addend + 4
14080 && relocation + 0x80008000 <= 0xffffffff)
14081 {
14082 unsigned int insn1, insn2;
14083 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14084 insn1 = bfd_get_32 (input_bfd, contents + offset);
14085 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14086 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14087 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14088 {
14089 r_type = R_PPC64_ADDR16_HA;
14090 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14091 rel->r_addend -= d_offset;
14092 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14093 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14094 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14095 }
14096 }
14097 break;
411e1bfb
AM
14098 }
14099
14100 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14101 insn = 0;
b25116a9
AM
14102 max_br_offset = 1 << 25;
14103 addend = rel->r_addend;
bc30df16 14104 reloc_dest = DEST_NORMAL;
65f38f15 14105 switch (r_type)
5bd4f169
AM
14106 {
14107 default:
65f38f15 14108 break;
5bd4f169 14109
3b421ab3
AM
14110 case R_PPC64_TOCSAVE:
14111 if (relocation + addend == (rel->r_offset
14112 + input_section->output_offset
14113 + input_section->output_section->vma)
14114 && tocsave_find (htab, NO_INSERT,
14115 &local_syms, rel, input_bfd))
14116 {
14117 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14118 if (insn == NOP
14119 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14120 bfd_put_32 (input_bfd,
14121 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14122 contents + rel->r_offset);
14123 }
14124 break;
14125
65f38f15
AM
14126 /* Branch taken prediction relocations. */
14127 case R_PPC64_ADDR14_BRTAKEN:
14128 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14129 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14130 /* Fall through. */
65f38f15 14131
86c76c7b 14132 /* Branch not taken prediction relocations. */
65f38f15
AM
14133 case R_PPC64_ADDR14_BRNTAKEN:
14134 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14135 insn |= bfd_get_32 (input_bfd,
411e1bfb 14136 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14137 /* Fall through. */
86c76c7b 14138
b25116a9
AM
14139 case R_PPC64_REL14:
14140 max_br_offset = 1 << 15;
1a0670f3 14141 /* Fall through. */
5bd4f169 14142
65f38f15 14143 case R_PPC64_REL24:
ad8e1ba5
AM
14144 /* Calls to functions with a different TOC, such as calls to
14145 shared objects, need to alter the TOC pointer. This is
14146 done using a linkage stub. A REL24 branching to these
14147 linkage stubs needs to be followed by a nop, as the nop
14148 will be replaced with an instruction to restore the TOC
14149 base pointer. */
8387904d 14150 fdh = h;
b31867b6
AM
14151 if (h != NULL
14152 && h->oh != NULL
14153 && h->oh->is_func_descriptor)
14154 fdh = ppc_follow_link (h->oh);
31c76678
DK
14155 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14156 htab);
6abec6bc 14157 if (stub_entry != NULL
ad8e1ba5 14158 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 14159 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
14160 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14161 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 14162 {
b25116a9 14163 bfd_boolean can_plt_call = FALSE;
721956f4 14164
ba8ca3e7
AM
14165 /* All of these stubs will modify r2, so there must be a
14166 branch and link followed by a nop. The nop is
14167 replaced by an insn to restore r2. */
eea6121a 14168 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14169 {
ba8ca3e7
AM
14170 unsigned long br;
14171
14172 br = bfd_get_32 (input_bfd,
14173 contents + rel->r_offset);
14174 if ((br & 1) != 0)
41bd81ab 14175 {
ba8ca3e7
AM
14176 unsigned long nop;
14177
14178 nop = bfd_get_32 (input_bfd,
14179 contents + rel->r_offset + 4);
14180 if (nop == NOP
14181 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 14182 {
ba8ca3e7
AM
14183 if (h != NULL
14184 && (h == htab->tls_get_addr_fd
14185 || h == htab->tls_get_addr)
7c9cf415 14186 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14187 {
14188 /* Special stub used, leave nop alone. */
14189 }
14190 else
a078d95a
AM
14191 bfd_put_32 (input_bfd,
14192 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14193 contents + rel->r_offset + 4);
14194 can_plt_call = TRUE;
a7f2871e 14195 }
41bd81ab 14196 }
5bd4f169 14197 }
721956f4 14198
ba8ca3e7 14199 if (!can_plt_call && h != NULL)
721956f4 14200 {
ba8ca3e7
AM
14201 const char *name = h->elf.root.root.string;
14202
14203 if (*name == '.')
14204 ++name;
14205
14206 if (strncmp (name, "__libc_start_main", 17) == 0
14207 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14208 {
ba8ca3e7
AM
14209 /* Allow crt1 branch to go via a toc adjusting
14210 stub. Other calls that never return could do
14211 the same, if we could detect such. */
b25116a9 14212 can_plt_call = TRUE;
6ab189d5 14213 }
ba8ca3e7
AM
14214 }
14215
14216 if (!can_plt_call)
14217 {
14218 /* g++ as of 20130507 emits self-calls without a
14219 following nop. This is arguably wrong since we
14220 have conflicting information. On the one hand a
14221 global symbol and on the other a local call
14222 sequence, but don't error for this special case.
14223 It isn't possible to cheaply verify we have
14224 exactly such a call. Allow all calls to the same
14225 section. */
14226 asection *code_sec = sec;
14227
14228 if (get_opd_info (sec) != NULL)
ad8e1ba5 14229 {
ba8ca3e7
AM
14230 bfd_vma off = (relocation + addend
14231 - sec->output_section->vma
14232 - sec->output_offset);
bc30df16 14233
ba8ca3e7 14234 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14235 }
ba8ca3e7
AM
14236 if (code_sec == input_section)
14237 can_plt_call = TRUE;
14238 }
14239
14240 if (!can_plt_call)
14241 {
4805fc55
AM
14242 if (stub_entry->stub_type == ppc_stub_plt_call
14243 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14244 info->callbacks->einfo
695344c0 14245 /* xgettext:c-format */
174d0a74 14246 (_("%H: call to `%T' lacks nop, can't restore toc; "
4805fc55
AM
14247 "recompile with -fPIC\n"),
14248 input_bfd, input_section, rel->r_offset, sym_name);
14249 else
14250 info->callbacks->einfo
695344c0 14251 /* xgettext:c-format */
174d0a74 14252 (_("%H: call to `%T' lacks nop, can't restore toc; "
4805fc55
AM
14253 "(-mcmodel=small toc adjust stub)\n"),
14254 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14255
14256 bfd_set_error (bfd_error_bad_value);
14257 ret = FALSE;
721956f4
AM
14258 }
14259
b25116a9 14260 if (can_plt_call
794e51c0
AM
14261 && (stub_entry->stub_type == ppc_stub_plt_call
14262 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14263 unresolved_reloc = FALSE;
14264 }
14265
6abec6bc
AM
14266 if ((stub_entry == NULL
14267 || stub_entry->stub_type == ppc_stub_long_branch
14268 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14269 && get_opd_info (sec) != NULL)
14270 {
14271 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14272 bfd_vma off = (relocation + addend
14273 - sec->output_section->vma
14274 - sec->output_offset);
aef36ac1 14275 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14276 if (dest != (bfd_vma) -1)
14277 {
14278 relocation = dest;
14279 addend = 0;
bc30df16 14280 reloc_dest = DEST_OPD;
8387904d
AM
14281 }
14282 }
14283
b25116a9
AM
14284 /* If the branch is out of reach we ought to have a long
14285 branch stub. */
14286 from = (rel->r_offset
14287 + input_section->output_offset
14288 + input_section->output_section->vma);
14289
6911b7dc
AM
14290 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14291 ? fdh->elf.other
14292 : sym->st_other);
14293
6abec6bc
AM
14294 if (stub_entry != NULL
14295 && (stub_entry->stub_type == ppc_stub_long_branch
14296 || stub_entry->stub_type == ppc_stub_plt_branch)
14297 && (r_type == R_PPC64_ADDR14_BRTAKEN
14298 || r_type == R_PPC64_ADDR14_BRNTAKEN
14299 || (relocation + addend - from + max_br_offset
14300 < 2 * max_br_offset)))
14301 /* Don't use the stub if this branch is in range. */
14302 stub_entry = NULL;
b25116a9
AM
14303
14304 if (stub_entry != NULL)
14305 {
14306 /* Munge up the value and addend so that we call the stub
14307 rather than the procedure directly. */
a4b6fadd
AM
14308 asection *stub_sec = stub_entry->group->stub_sec;
14309
14310 if (stub_entry->stub_type == ppc_stub_save_res)
14311 relocation += (stub_sec->output_offset
14312 + stub_sec->output_section->vma
14313 + stub_sec->size - htab->sfpr->size
14314 - htab->sfpr->output_offset
14315 - htab->sfpr->output_section->vma);
14316 else
14317 relocation = (stub_entry->stub_offset
14318 + stub_sec->output_offset
14319 + stub_sec->output_section->vma);
b25116a9 14320 addend = 0;
bc30df16 14321 reloc_dest = DEST_STUB;
3b421ab3 14322
794e51c0
AM
14323 if ((stub_entry->stub_type == ppc_stub_plt_call
14324 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14325 && (ALWAYS_EMIT_R2SAVE
14326 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14327 && rel + 1 < relend
14328 && rel[1].r_offset == rel->r_offset + 4
14329 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14330 relocation += 4;
b25116a9
AM
14331 }
14332
14333 if (insn != 0)
14334 {
794e51c0 14335 if (is_isa_v2)
721956f4 14336 {
b25116a9
AM
14337 /* Set 'a' bit. This is 0b00010 in BO field for branch
14338 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14339 for branch on CTR insns (BO == 1a00t or 1a01t). */
14340 if ((insn & (0x14 << 21)) == (0x04 << 21))
14341 insn |= 0x02 << 21;
14342 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14343 insn |= 0x08 << 21;
14344 else
14345 break;
14346 }
14347 else
14348 {
14349 /* Invert 'y' bit if not the default. */
4cc603a5 14350 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14351 insn ^= 0x01 << 21;
721956f4 14352 }
b25116a9 14353
95f0d0d2 14354 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14355 }
e86ce104 14356
06da1e8e
AM
14357 /* NOP out calls to undefined weak functions.
14358 We can thus call a weak function without first
14359 checking whether the function is defined. */
b25116a9 14360 else if (h != NULL
039b3fef 14361 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14362 && h->elf.dynindx == -1
b25116a9
AM
14363 && r_type == R_PPC64_REL24
14364 && relocation == 0
4cc603a5 14365 && addend == 0)
e86ce104 14366 {
95f0d0d2 14367 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14368 goto copy_reloc;
e86ce104 14369 }
65f38f15
AM
14370 break;
14371 }
5bd4f169 14372
65f38f15 14373 /* Set `addend'. */
411e1bfb 14374 tls_type = 0;
65f38f15
AM
14375 switch (r_type)
14376 {
14377 default:
25f53a85 14378 info->callbacks->einfo
695344c0 14379 /* xgettext:c-format */
bc30df16 14380 (_("%P: %B: unknown relocation type %d for `%T'\n"),
d003868e 14381 input_bfd, (int) r_type, sym_name);
5bd4f169 14382
65f38f15 14383 bfd_set_error (bfd_error_bad_value);
b34976b6 14384 ret = FALSE;
c316a17c 14385 goto copy_reloc;
5bd4f169 14386
65f38f15 14387 case R_PPC64_NONE:
411e1bfb 14388 case R_PPC64_TLS:
727fc41e
AM
14389 case R_PPC64_TLSGD:
14390 case R_PPC64_TLSLD:
3b421ab3 14391 case R_PPC64_TOCSAVE:
04c9666a
AM
14392 case R_PPC64_GNU_VTINHERIT:
14393 case R_PPC64_GNU_VTENTRY:
006589cf 14394 case R_PPC64_ENTRY:
c316a17c 14395 goto copy_reloc;
5bd4f169
AM
14396
14397 /* GOT16 relocations. Like an ADDR16 using the symbol's
14398 address in the GOT as relocation value instead of the
411e1bfb 14399 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14400 symbol and put the symbol value there. */
411e1bfb
AM
14401 case R_PPC64_GOT_TLSGD16:
14402 case R_PPC64_GOT_TLSGD16_LO:
14403 case R_PPC64_GOT_TLSGD16_HI:
14404 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14405 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14406 goto dogot;
14407
14408 case R_PPC64_GOT_TLSLD16:
14409 case R_PPC64_GOT_TLSLD16_LO:
14410 case R_PPC64_GOT_TLSLD16_HI:
14411 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14412 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14413 goto dogot;
14414
14415 case R_PPC64_GOT_TPREL16_DS:
14416 case R_PPC64_GOT_TPREL16_LO_DS:
14417 case R_PPC64_GOT_TPREL16_HI:
14418 case R_PPC64_GOT_TPREL16_HA:
14419 tls_type = TLS_TLS | TLS_TPREL;
14420 goto dogot;
14421
14422 case R_PPC64_GOT_DTPREL16_DS:
14423 case R_PPC64_GOT_DTPREL16_LO_DS:
14424 case R_PPC64_GOT_DTPREL16_HI:
14425 case R_PPC64_GOT_DTPREL16_HA:
14426 tls_type = TLS_TLS | TLS_DTPREL;
14427 goto dogot;
14428
65f38f15
AM
14429 case R_PPC64_GOT16:
14430 case R_PPC64_GOT16_LO:
14431 case R_PPC64_GOT16_HI:
14432 case R_PPC64_GOT16_HA:
14433 case R_PPC64_GOT16_DS:
14434 case R_PPC64_GOT16_LO_DS:
411e1bfb 14435 dogot:
5bd4f169
AM
14436 {
14437 /* Relocation is to the entry for this symbol in the global
14438 offset table. */
e717da7e 14439 asection *got;
d881513a 14440 bfd_vma *offp;
5bd4f169 14441 bfd_vma off;
d881513a 14442 unsigned long indx = 0;
927be08e 14443 struct got_entry *ent;
65f38f15 14444
d881513a
AM
14445 if (tls_type == (TLS_TLS | TLS_LD)
14446 && (h == NULL
f5385ebf 14447 || !h->elf.def_dynamic))
927be08e 14448 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14449 else
5bd4f169 14450 {
d881513a
AM
14451 if (h != NULL)
14452 {
f0158f44
AM
14453 if (!htab->elf.dynamic_sections_created
14454 || h->elf.dynindx == -1
14455 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14456 || (ELF_ST_VISIBILITY (h->elf.other) != STV_DEFAULT
14457 && h->elf.root.type == bfd_link_hash_undefweak))
d881513a
AM
14458 /* This is actually a static link, or it is a
14459 -Bsymbolic link and the symbol is defined
14460 locally, or the symbol was forced to be local
14461 because of a version file. */
14462 ;
14463 else
14464 {
039b3fef 14465 indx = h->elf.dynindx;
d881513a
AM
14466 unresolved_reloc = FALSE;
14467 }
039b3fef 14468 ent = h->elf.got.glist;
d881513a 14469 }
411e1bfb 14470 else
5bd4f169 14471 {
d881513a
AM
14472 if (local_got_ents == NULL)
14473 abort ();
14474 ent = local_got_ents[r_symndx];
5bd4f169 14475 }
d881513a
AM
14476
14477 for (; ent != NULL; ent = ent->next)
31c76678 14478 if (ent->addend == orig_rel.r_addend
e717da7e 14479 && ent->owner == input_bfd
d881513a
AM
14480 && ent->tls_type == tls_type)
14481 break;
5bd4f169 14482 }
411e1bfb 14483
927be08e
AM
14484 if (ent == NULL)
14485 abort ();
14486 if (ent->is_indirect)
14487 ent = ent->got.ent;
14488 offp = &ent->got.offset;
14489 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14490 if (got == NULL)
14491 abort ();
14492
411e1bfb
AM
14493 /* The offset must always be a multiple of 8. We use the
14494 least significant bit to record whether we have already
14495 processed this entry. */
d881513a 14496 off = *offp;
411e1bfb
AM
14497 if ((off & 1) != 0)
14498 off &= ~1;
5bd4f169
AM
14499 else
14500 {
411e1bfb
AM
14501 /* Generate relocs for the dynamic linker, except in
14502 the case of TLSLD where we'll use one entry per
14503 module. */
25f23106
AM
14504 asection *relgot;
14505 bfd_boolean ifunc;
e717da7e 14506
d881513a 14507 *offp = off | 1;
25f23106
AM
14508 relgot = NULL;
14509 ifunc = (h != NULL
14510 ? h->elf.type == STT_GNU_IFUNC
14511 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14512 if (ifunc)
82e66161
AM
14513 {
14514 relgot = htab->elf.irelplt;
14515 if (indx == 0)
14516 htab->local_ifunc_resolver = 1;
14517 else if (is_static_defined (&h->elf))
14518 htab->maybe_local_ifunc_resolver = 1;
14519 }
f0158f44
AM
14520 else if (indx != 0
14521 || (bfd_link_pic (info)
14522 && (h == NULL
14523 || (ELF_ST_VISIBILITY (h->elf.other)
14524 == STV_DEFAULT)
14525 || h->elf.root.type != bfd_link_hash_undefweak
14526 || (tls_type == (TLS_TLS | TLS_LD)
14527 && !h->elf.def_dynamic))))
19e08130 14528 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14529 if (relgot != NULL)
5bd4f169 14530 {
e717da7e
AM
14531 outrel.r_offset = (got->output_section->vma
14532 + got->output_offset
411e1bfb 14533 + off);
4cc603a5 14534 outrel.r_addend = addend;
d881513a 14535 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14536 {
411e1bfb 14537 outrel.r_addend = 0;
e515b051 14538 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14539 if (tls_type == (TLS_TLS | TLS_GD))
14540 {
e717da7e
AM
14541 loc = relgot->contents;
14542 loc += (relgot->reloc_count++
d881513a
AM
14543 * sizeof (Elf64_External_Rela));
14544 bfd_elf64_swap_reloca_out (output_bfd,
14545 &outrel, loc);
e515b051 14546 outrel.r_offset += 8;
4cc603a5 14547 outrel.r_addend = addend;
d881513a
AM
14548 outrel.r_info
14549 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14550 }
411e1bfb 14551 }
951fd09b 14552 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14553 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14554 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14555 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14556 else if (indx != 0)
14557 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14558 else
81407a69 14559 {
25f23106
AM
14560 if (ifunc)
14561 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14562 else
14563 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14564
14565 /* Write the .got section contents for the sake
14566 of prelink. */
e717da7e 14567 loc = got->contents + off;
23fbd6fa
JJ
14568 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14569 loc);
81407a69 14570 }
81407a69
AM
14571
14572 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14573 {
14574 outrel.r_addend += relocation;
14575 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14576 {
14577 if (htab->elf.tls_sec == NULL)
14578 outrel.r_addend = 0;
14579 else
14580 outrel.r_addend -= htab->elf.tls_sec->vma;
14581 }
e515b051 14582 }
e717da7e
AM
14583 loc = relgot->contents;
14584 loc += (relgot->reloc_count++
411e1bfb
AM
14585 * sizeof (Elf64_External_Rela));
14586 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14587 }
14588
ad8e1ba5 14589 /* Init the .got section contents here if we're not
81407a69 14590 emitting a reloc. */
d881513a 14591 else
411e1bfb 14592 {
f0158f44
AM
14593 int tlsopt
14594 = (htab->params->tls_get_addr_opt
14595 && htab->tls_get_addr_fd != NULL
14596 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
14597
4cc603a5 14598 relocation += addend;
f0158f44 14599 if (tls_type != 0)
411e1bfb 14600 {
989f9879
AM
14601 if (htab->elf.tls_sec == NULL)
14602 relocation = 0;
14603 else
14604 {
f0158f44
AM
14605 if (tls_type & TLS_LD)
14606 relocation = 0;
14607 else
14608 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14609 if ((tls_type & TLS_TPREL)
14610 || (tlsopt && !(tls_type & TLS_DTPREL)))
989f9879
AM
14611 relocation += DTP_OFFSET - TP_OFFSET;
14612 }
5bd4f169 14613
f0158f44 14614 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
14615 {
14616 bfd_put_64 (output_bfd, relocation,
e717da7e 14617 got->contents + off + 8);
f0158f44 14618 relocation = !tlsopt;
7b609f53 14619 }
411e1bfb
AM
14620 }
14621 bfd_put_64 (output_bfd, relocation,
e717da7e 14622 got->contents + off);
5bd4f169
AM
14623 }
14624 }
14625
65f38f15
AM
14626 if (off >= (bfd_vma) -2)
14627 abort ();
14628
bf102f86 14629 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14630 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14631 }
65f38f15
AM
14632 break;
14633
14634 case R_PPC64_PLT16_HA:
14635 case R_PPC64_PLT16_HI:
14636 case R_PPC64_PLT16_LO:
14637 case R_PPC64_PLT32:
14638 case R_PPC64_PLT64:
14639 /* Relocation is to the entry for this symbol in the
14640 procedure linkage table. */
cbf95972
AM
14641 {
14642 struct plt_entry **plt_list = NULL;
14643 if (h != NULL)
14644 plt_list = &h->elf.plt.plist;
14645 else if (local_got_ents != NULL)
14646 {
14647 struct plt_entry **local_plt = (struct plt_entry **)
14648 (local_got_ents + symtab_hdr->sh_info);
14649 unsigned char *local_got_tls_masks = (unsigned char *)
14650 (local_plt + symtab_hdr->sh_info);
14651 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14652 plt_list = local_plt + r_symndx;
14653 }
14654 if (plt_list)
14655 {
14656 struct plt_entry *ent;
65f38f15 14657
cbf95972
AM
14658 for (ent = *plt_list; ent != NULL; ent = ent->next)
14659 if (ent->plt.offset != (bfd_vma) -1
14660 && ent->addend == orig_rel.r_addend)
14661 {
14662 asection *plt;
14663
14664 plt = htab->elf.splt;
14665 if (!htab->elf.dynamic_sections_created
14666 || h == NULL
14667 || h->elf.dynindx == -1)
14668 plt = htab->elf.iplt;
14669 relocation = (plt->output_section->vma
14670 + plt->output_offset
14671 + ent->plt.offset);
14672 addend = 0;
14673 unresolved_reloc = FALSE;
14674 break;
14675 }
14676 }
14677 }
65f38f15 14678 break;
5bd4f169 14679
0b13192e
AM
14680 case R_PPC64_TOC:
14681 /* Relocation value is TOC base. */
14682 relocation = TOCstart;
cf35638d 14683 if (r_symndx == STN_UNDEF)
6f20ed8a 14684 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14685 else if (unresolved_reloc)
14686 ;
6f20ed8a
AM
14687 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14688 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14689 else
14690 unresolved_reloc = TRUE;
ab96bf03 14691 goto dodyn;
0b13192e 14692
5bd4f169
AM
14693 /* TOC16 relocs. We want the offset relative to the TOC base,
14694 which is the address of the start of the TOC plus 0x8000.
14695 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14696 in this order. */
65f38f15
AM
14697 case R_PPC64_TOC16:
14698 case R_PPC64_TOC16_LO:
14699 case R_PPC64_TOC16_HI:
14700 case R_PPC64_TOC16_DS:
14701 case R_PPC64_TOC16_LO_DS:
14702 case R_PPC64_TOC16_HA:
6f20ed8a 14703 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14704 break;
14705
14706 /* Relocate against the beginning of the section. */
65f38f15
AM
14707 case R_PPC64_SECTOFF:
14708 case R_PPC64_SECTOFF_LO:
14709 case R_PPC64_SECTOFF_HI:
14710 case R_PPC64_SECTOFF_DS:
14711 case R_PPC64_SECTOFF_LO_DS:
14712 case R_PPC64_SECTOFF_HA:
4ce794b7 14713 if (sec != NULL)
65f38f15 14714 addend -= sec->output_section->vma;
5bd4f169
AM
14715 break;
14716
25f23106
AM
14717 case R_PPC64_REL16:
14718 case R_PPC64_REL16_LO:
14719 case R_PPC64_REL16_HI:
14720 case R_PPC64_REL16_HA:
a680de9a 14721 case R_PPC64_REL16DX_HA:
25f23106
AM
14722 break;
14723
721956f4
AM
14724 case R_PPC64_REL14:
14725 case R_PPC64_REL14_BRNTAKEN:
14726 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
14727 case R_PPC64_REL24:
14728 break;
14729
411e1bfb
AM
14730 case R_PPC64_TPREL16:
14731 case R_PPC64_TPREL16_LO:
14732 case R_PPC64_TPREL16_HI:
14733 case R_PPC64_TPREL16_HA:
14734 case R_PPC64_TPREL16_DS:
14735 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14736 case R_PPC64_TPREL16_HIGH:
14737 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14738 case R_PPC64_TPREL16_HIGHER:
14739 case R_PPC64_TPREL16_HIGHERA:
14740 case R_PPC64_TPREL16_HIGHEST:
14741 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14742 if (h != NULL
14743 && h->elf.root.type == bfd_link_hash_undefweak
14744 && h->elf.dynindx == -1)
14745 {
14746 /* Make this relocation against an undefined weak symbol
14747 resolve to zero. This is really just a tweak, since
14748 code using weak externs ought to check that they are
14749 defined before using them. */
14750 bfd_byte *p = contents + rel->r_offset - d_offset;
14751
95f0d0d2 14752 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
14753 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14754 if (insn != 0)
95f0d0d2 14755 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
14756 break;
14757 }
989f9879
AM
14758 if (htab->elf.tls_sec != NULL)
14759 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
0e1862bb 14760 if (bfd_link_pic (info))
411e1bfb
AM
14761 /* The TPREL16 relocs shouldn't really be used in shared
14762 libs as they will result in DT_TEXTREL being set, but
14763 support them anyway. */
14764 goto dodyn;
14765 break;
14766
14767 case R_PPC64_DTPREL16:
14768 case R_PPC64_DTPREL16_LO:
14769 case R_PPC64_DTPREL16_HI:
14770 case R_PPC64_DTPREL16_HA:
14771 case R_PPC64_DTPREL16_DS:
14772 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14773 case R_PPC64_DTPREL16_HIGH:
14774 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14775 case R_PPC64_DTPREL16_HIGHER:
14776 case R_PPC64_DTPREL16_HIGHERA:
14777 case R_PPC64_DTPREL16_HIGHEST:
14778 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14779 if (htab->elf.tls_sec != NULL)
14780 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14781 break;
14782
45965137
AM
14783 case R_PPC64_ADDR64_LOCAL:
14784 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14785 ? h->elf.other
14786 : sym->st_other);
14787 break;
14788
e515b051
AM
14789 case R_PPC64_DTPMOD64:
14790 relocation = 1;
14791 addend = 0;
14792 goto dodyn;
14793
411e1bfb 14794 case R_PPC64_TPREL64:
989f9879
AM
14795 if (htab->elf.tls_sec != NULL)
14796 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14797 goto dodyn;
14798
14799 case R_PPC64_DTPREL64:
989f9879
AM
14800 if (htab->elf.tls_sec != NULL)
14801 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 14802 /* Fall through. */
411e1bfb 14803
65f38f15
AM
14804 /* Relocations that may need to be propagated if this is a
14805 dynamic object. */
04c9666a 14806 case R_PPC64_REL30:
65f38f15
AM
14807 case R_PPC64_REL32:
14808 case R_PPC64_REL64:
14809 case R_PPC64_ADDR14:
14810 case R_PPC64_ADDR14_BRNTAKEN:
14811 case R_PPC64_ADDR14_BRTAKEN:
14812 case R_PPC64_ADDR16:
14813 case R_PPC64_ADDR16_DS:
14814 case R_PPC64_ADDR16_HA:
14815 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14816 case R_PPC64_ADDR16_HIGH:
14817 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14818 case R_PPC64_ADDR16_HIGHER:
14819 case R_PPC64_ADDR16_HIGHERA:
14820 case R_PPC64_ADDR16_HIGHEST:
14821 case R_PPC64_ADDR16_HIGHESTA:
14822 case R_PPC64_ADDR16_LO:
14823 case R_PPC64_ADDR16_LO_DS:
14824 case R_PPC64_ADDR24:
65f38f15
AM
14825 case R_PPC64_ADDR32:
14826 case R_PPC64_ADDR64:
14827 case R_PPC64_UADDR16:
14828 case R_PPC64_UADDR32:
14829 case R_PPC64_UADDR64:
411e1bfb 14830 dodyn:
5d1634d7 14831 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14832 break;
14833
41bd81ab
AM
14834 if (NO_OPD_RELOCS && is_opd)
14835 break;
14836
8a9e8e72 14837 if (bfd_link_pic (info)
b1b07054
AM
14838 ? ((h == NULL
14839 || h->dyn_relocs != NULL)
14840 && ((h != NULL && pc_dynrelocs (h))
14841 || must_be_dyn_reloc (info, r_type)))
8a9e8e72
AM
14842 : (h != NULL
14843 ? h->dyn_relocs != NULL
d311bc8b 14844 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 14845 {
b34976b6 14846 bfd_boolean skip, relocate;
65f38f15 14847 asection *sreloc;
1cf1f670 14848 bfd_vma out_off;
82e66161 14849 long indx = 0;
65f38f15
AM
14850
14851 /* When generating a dynamic object, these relocations
14852 are copied into the output file to be resolved at run
14853 time. */
14854
b34976b6
AM
14855 skip = FALSE;
14856 relocate = FALSE;
65f38f15 14857
1cf1f670
AM
14858 out_off = _bfd_elf_section_offset (output_bfd, info,
14859 input_section, rel->r_offset);
14860 if (out_off == (bfd_vma) -1)
b34976b6 14861 skip = TRUE;
1cf1f670 14862 else if (out_off == (bfd_vma) -2)
b34976b6 14863 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14864 out_off += (input_section->output_section->vma
14865 + input_section->output_offset);
14866 outrel.r_offset = out_off;
411e1bfb 14867 outrel.r_addend = rel->r_addend;
65f38f15 14868
1cf1f670
AM
14869 /* Optimize unaligned reloc use. */
14870 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14871 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14872 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14873 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14874 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14875 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14876 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14877 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14878 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14879
65f38f15 14880 if (skip)
0bb2d96a 14881 memset (&outrel, 0, sizeof outrel);
afe397ea 14882 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14883 && !is_opd
14884 && r_type != R_PPC64_TOC)
14acf4dc 14885 {
82e66161
AM
14886 indx = h->elf.dynindx;
14887 BFD_ASSERT (indx != -1);
14888 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 14889 }
65f38f15
AM
14890 else
14891 {
41bd81ab
AM
14892 /* This symbol is local, or marked to become local,
14893 or this is an opd section reloc which must point
14894 at a local function. */
65f38f15 14895 outrel.r_addend += relocation;
e86ce104 14896 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14897 {
3fad3c7c 14898 if (is_opd && h != NULL)
afbe61cf
AM
14899 {
14900 /* Lie about opd entries. This case occurs
14901 when building shared libraries and we
14902 reference a function in another shared
3fad3c7c
AM
14903 lib. The same thing happens for a weak
14904 definition in an application that's
14905 overridden by a strong definition in a
14906 shared lib. (I believe this is a generic
14907 bug in binutils handling of weak syms.)
14908 In these cases we won't use the opd
1e2f5b6e 14909 entry in this lib. */
b34976b6 14910 unresolved_reloc = FALSE;
afbe61cf 14911 }
25f23106
AM
14912 if (!is_opd
14913 && r_type == R_PPC64_ADDR64
14914 && (h != NULL
14915 ? h->elf.type == STT_GNU_IFUNC
14916 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14917 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14918 else
14919 {
14920 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14921
25f23106
AM
14922 /* We need to relocate .opd contents for ld.so.
14923 Prelink also wants simple and consistent rules
14924 for relocs. This make all RELATIVE relocs have
14925 *r_offset equal to r_addend. */
14926 relocate = TRUE;
14927 }
65f38f15
AM
14928 }
14929 else
14930 {
25f23106
AM
14931 if (h != NULL
14932 ? h->elf.type == STT_GNU_IFUNC
14933 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14934 {
25f53a85 14935 info->callbacks->einfo
695344c0 14936 /* xgettext:c-format */
174d0a74 14937 (_("%H: %s for indirect "
bc30df16 14938 "function `%T' unsupported\n"),
25f53a85 14939 input_bfd, input_section, rel->r_offset,
25f23106
AM
14940 ppc64_elf_howto_table[r_type]->name,
14941 sym_name);
14942 ret = FALSE;
14943 }
cf35638d 14944 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14945 ;
14946 else if (sec == NULL || sec->owner == NULL)
14947 {
14948 bfd_set_error (bfd_error_bad_value);
b34976b6 14949 return FALSE;
65f38f15
AM
14950 }
14951 else
14952 {
14953 asection *osec;
14954
14955 osec = sec->output_section;
14956 indx = elf_section_data (osec)->dynindx;
14957
74541ad4
AM
14958 if (indx == 0)
14959 {
14960 if ((osec->flags & SEC_READONLY) == 0
14961 && htab->elf.data_index_section != NULL)
14962 osec = htab->elf.data_index_section;
14963 else
14964 osec = htab->elf.text_index_section;
14965 indx = elf_section_data (osec)->dynindx;
14966 }
14967 BFD_ASSERT (indx != 0);
14968
65f38f15
AM
14969 /* We are turning this relocation into one
14970 against a section symbol, so subtract out
14971 the output section's address but not the
14972 offset of the input section in the output
14973 section. */
14974 outrel.r_addend -= osec->vma;
14975 }
14976
14977 outrel.r_info = ELF64_R_INFO (indx, r_type);
14978 }
14979 }
14980
14981 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14982 if (h != NULL
14983 ? h->elf.type == STT_GNU_IFUNC
14984 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
14985 {
14986 sreloc = htab->elf.irelplt;
14987 if (indx == 0)
14988 htab->local_ifunc_resolver = 1;
14989 else if (is_static_defined (&h->elf))
14990 htab->maybe_local_ifunc_resolver = 1;
14991 }
65f38f15
AM
14992 if (sreloc == NULL)
14993 abort ();
14994
dfbb6ac9
AM
14995 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14996 >= sreloc->size)
14997 abort ();
947216bf
AM
14998 loc = sreloc->contents;
14999 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
15000 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15001
15002 /* If this reloc is against an external symbol, it will
15003 be computed at runtime, so there's no need to do
81407a69
AM
15004 anything now. However, for the sake of prelink ensure
15005 that the section contents are a known value. */
65f38f15 15006 if (! relocate)
81407a69
AM
15007 {
15008 unresolved_reloc = FALSE;
15009 /* The value chosen here is quite arbitrary as ld.so
15010 ignores section contents except for the special
15011 case of .opd where the contents might be accessed
15012 before relocation. Choose zero, as that won't
15013 cause reloc overflow. */
15014 relocation = 0;
15015 addend = 0;
15016 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15017 to improve backward compatibility with older
15018 versions of ld. */
15019 if (r_type == R_PPC64_ADDR64)
15020 addend = outrel.r_addend;
15021 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 15022 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 15023 addend = outrel.r_offset;
81407a69 15024 }
65f38f15 15025 }
f0158f44
AM
15026 else if (r_type == R_PPC64_DTPMOD64
15027 && htab->params->tls_get_addr_opt
15028 && htab->tls_get_addr_fd != NULL
15029 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
15030 {
15031 /* Set up for __tls_get_addr_opt stub, when this entry
15032 does not have dynamic relocs. */
15033 relocation = 0;
15034 /* Set up the next word for local dynamic. If it turns
15035 out to be global dynamic, the reloc will overwrite
15036 this value. */
15037 if (rel->r_offset + 16 <= input_section->size)
15038 bfd_put_64 (input_bfd, DTP_OFFSET - TP_OFFSET,
15039 contents + rel->r_offset + 8);
15040 }
15041 else if (r_type == R_PPC64_DTPREL64
15042 && htab->params->tls_get_addr_opt
15043 && htab->tls_get_addr_fd != NULL
15044 && htab->tls_get_addr_fd->elf.plt.plist != NULL
15045 && rel > relocs
15046 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
15047 && rel[-1].r_offset + 8 == rel->r_offset)
15048 {
15049 /* __tls_get_addr_opt stub value. */
15050 addend += DTP_OFFSET - TP_OFFSET;
15051 }
5bd4f169
AM
15052 break;
15053
65f38f15
AM
15054 case R_PPC64_COPY:
15055 case R_PPC64_GLOB_DAT:
15056 case R_PPC64_JMP_SLOT:
25f23106 15057 case R_PPC64_JMP_IREL:
65f38f15
AM
15058 case R_PPC64_RELATIVE:
15059 /* We shouldn't ever see these dynamic relocs in relocatable
15060 files. */
ae9a127f 15061 /* Fall through. */
65f38f15
AM
15062
15063 case R_PPC64_PLTGOT16:
15064 case R_PPC64_PLTGOT16_DS:
15065 case R_PPC64_PLTGOT16_HA:
15066 case R_PPC64_PLTGOT16_HI:
15067 case R_PPC64_PLTGOT16_LO:
15068 case R_PPC64_PLTGOT16_LO_DS:
15069 case R_PPC64_PLTREL32:
15070 case R_PPC64_PLTREL64:
15071 /* These ones haven't been implemented yet. */
15072
25f53a85 15073 info->callbacks->einfo
695344c0 15074 /* xgettext:c-format */
bc30df16 15075 (_("%P: %B: %s is not supported for `%T'\n"),
d003868e 15076 input_bfd,
4ce794b7 15077 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
15078
15079 bfd_set_error (bfd_error_invalid_operation);
b34976b6 15080 ret = FALSE;
c316a17c 15081 goto copy_reloc;
65f38f15 15082 }
5bd4f169 15083
67f0cbdb
AM
15084 /* Multi-instruction sequences that access the TOC can be
15085 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15086 to nop; addi rb,r2,x; */
15087 switch (r_type)
15088 {
15089 default:
15090 break;
15091
15092 case R_PPC64_GOT_TLSLD16_HI:
15093 case R_PPC64_GOT_TLSGD16_HI:
15094 case R_PPC64_GOT_TPREL16_HI:
15095 case R_PPC64_GOT_DTPREL16_HI:
15096 case R_PPC64_GOT16_HI:
15097 case R_PPC64_TOC16_HI:
15098 /* These relocs would only be useful if building up an
15099 offset to later add to r2, perhaps in an indexed
15100 addressing mode instruction. Don't try to optimize.
15101 Unfortunately, the possibility of someone building up an
15102 offset like this or even with the HA relocs, means that
15103 we need to check the high insn when optimizing the low
15104 insn. */
15105 break;
15106
15107 case R_PPC64_GOT_TLSLD16_HA:
15108 case R_PPC64_GOT_TLSGD16_HA:
15109 case R_PPC64_GOT_TPREL16_HA:
15110 case R_PPC64_GOT_DTPREL16_HA:
15111 case R_PPC64_GOT16_HA:
15112 case R_PPC64_TOC16_HA:
98528052 15113 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15114 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
15115 {
15116 bfd_byte *p = contents + (rel->r_offset & ~3);
15117 bfd_put_32 (input_bfd, NOP, p);
15118 }
67f0cbdb
AM
15119 break;
15120
15121 case R_PPC64_GOT_TLSLD16_LO:
15122 case R_PPC64_GOT_TLSGD16_LO:
15123 case R_PPC64_GOT_TPREL16_LO_DS:
15124 case R_PPC64_GOT_DTPREL16_LO_DS:
15125 case R_PPC64_GOT16_LO:
15126 case R_PPC64_GOT16_LO_DS:
15127 case R_PPC64_TOC16_LO:
15128 case R_PPC64_TOC16_LO_DS:
98528052 15129 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15130 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15131 {
15132 bfd_byte *p = contents + (rel->r_offset & ~3);
15133 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15134 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15135 {
15136 /* Transform addic to addi when we change reg. */
15137 insn &= ~((0x3f << 26) | (0x1f << 16));
15138 insn |= (14u << 26) | (2 << 16);
15139 }
15140 else
67f0cbdb 15141 {
98528052
AM
15142 insn &= ~(0x1f << 16);
15143 insn |= 2 << 16;
67f0cbdb 15144 }
560c8763 15145 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15146 }
15147 break;
15148 }
15149
65f38f15 15150 /* Do any further special processing. */
b80eed39 15151 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15
AM
15152 switch (r_type)
15153 {
15154 default:
15155 break;
15156
25f23106 15157 case R_PPC64_REL16_HA:
a680de9a 15158 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15159 case R_PPC64_ADDR16_HA:
15160 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15161 case R_PPC64_ADDR16_HIGHERA:
15162 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15163 case R_PPC64_TOC16_HA:
15164 case R_PPC64_SECTOFF_HA:
411e1bfb 15165 case R_PPC64_TPREL16_HA:
f9c6b907 15166 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15167 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15168 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15169 case R_PPC64_DTPREL16_HA:
15170 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15171 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15172 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15173 /* It's just possible that this symbol is a weak symbol
15174 that's not actually defined anywhere. In that case,
15175 'sec' would be NULL, and we should leave the symbol
15176 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15177 if (sec == NULL)
15178 break;
1a0670f3 15179 /* Fall through. */
5c5f6e17
AM
15180
15181 case R_PPC64_GOT16_HA:
15182 case R_PPC64_PLTGOT16_HA:
15183 case R_PPC64_PLT16_HA:
15184 case R_PPC64_GOT_TLSGD16_HA:
15185 case R_PPC64_GOT_TLSLD16_HA:
15186 case R_PPC64_GOT_TPREL16_HA:
15187 case R_PPC64_GOT_DTPREL16_HA:
15188 /* Add 0x10000 if sign bit in 0:15 is set.
15189 Bits 0:15 are not used. */
15190 addend += 0x8000;
65f38f15
AM
15191 break;
15192
15193 case R_PPC64_ADDR16_DS:
15194 case R_PPC64_ADDR16_LO_DS:
15195 case R_PPC64_GOT16_DS:
15196 case R_PPC64_GOT16_LO_DS:
15197 case R_PPC64_PLT16_LO_DS:
15198 case R_PPC64_SECTOFF_DS:
15199 case R_PPC64_SECTOFF_LO_DS:
15200 case R_PPC64_TOC16_DS:
15201 case R_PPC64_TOC16_LO_DS:
15202 case R_PPC64_PLTGOT16_DS:
15203 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15204 case R_PPC64_GOT_TPREL16_DS:
15205 case R_PPC64_GOT_TPREL16_LO_DS:
15206 case R_PPC64_GOT_DTPREL16_DS:
15207 case R_PPC64_GOT_DTPREL16_LO_DS:
15208 case R_PPC64_TPREL16_DS:
15209 case R_PPC64_TPREL16_LO_DS:
15210 case R_PPC64_DTPREL16_DS:
15211 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15212 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15213 mask = 3;
a680de9a
PB
15214 /* If this reloc is against an lq, lxv, or stxv insn, then
15215 the value must be a multiple of 16. This is somewhat of
15216 a hack, but the "correct" way to do this by defining _DQ
15217 forms of all the _DS relocs bloats all reloc switches in
15218 this file. It doesn't make much sense to use these
15219 relocs in data, so testing the insn should be safe. */
15220 if ((insn & (0x3f << 26)) == (56u << 26)
15221 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15222 mask = 15;
a680de9a
PB
15223 relocation += addend;
15224 addend = insn & (mask ^ 3);
15225 if ((relocation & mask) != 0)
65f38f15 15226 {
a680de9a 15227 relocation ^= relocation & mask;
25f53a85 15228 info->callbacks->einfo
695344c0 15229 /* xgettext:c-format */
174d0a74 15230 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 15231 input_bfd, input_section, rel->r_offset,
b80eed39 15232 howto->name,
adadcc0c 15233 mask + 1);
65f38f15 15234 bfd_set_error (bfd_error_bad_value);
b34976b6 15235 ret = FALSE;
c316a17c 15236 goto copy_reloc;
65f38f15
AM
15237 }
15238 break;
5bd4f169
AM
15239 }
15240
239e1f3a
AM
15241 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15242 because such sections are not SEC_ALLOC and thus ld.so will
15243 not process them. */
65f38f15 15244 if (unresolved_reloc
239e1f3a 15245 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15246 && h->elf.def_dynamic)
15247 && _bfd_elf_section_offset (output_bfd, info, input_section,
15248 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15249 {
25f53a85 15250 info->callbacks->einfo
695344c0 15251 /* xgettext:c-format */
174d0a74 15252 (_("%H: unresolvable %s against `%T'\n"),
25f53a85 15253 input_bfd, input_section, rel->r_offset,
b80eed39 15254 howto->name,
039b3fef 15255 h->elf.root.root.string);
b34976b6 15256 ret = FALSE;
9c07fe7c 15257 }
5bd4f169 15258
b80eed39
AM
15259 /* 16-bit fields in insns mostly have signed values, but a
15260 few insns have 16-bit unsigned values. Really, we should
15261 have different reloc types. */
15262 if (howto->complain_on_overflow != complain_overflow_dont
15263 && howto->dst_mask == 0xffff
15264 && (input_section->flags & SEC_CODE) != 0)
15265 {
15266 enum complain_overflow complain = complain_overflow_signed;
15267
15268 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15269 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15270 complain = complain_overflow_bitfield;
15271 else if (howto->rightshift == 0
15272 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15273 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15274 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15275 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15276 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15277 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15278 complain = complain_overflow_unsigned;
15279 if (howto->complain_on_overflow != complain)
15280 {
15281 alt_howto = *howto;
15282 alt_howto.complain_on_overflow = complain;
15283 howto = &alt_howto;
15284 }
15285 }
15286
a680de9a
PB
15287 if (r_type == R_PPC64_REL16DX_HA)
15288 {
15289 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15290 if (rel->r_offset + 4 > input_section->size)
15291 r = bfd_reloc_outofrange;
15292 else
15293 {
15294 relocation += addend;
15295 relocation -= (rel->r_offset
15296 + input_section->output_offset
15297 + input_section->output_section->vma);
3de43e7b 15298 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
15299 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15300 insn &= ~0x1fffc1;
3de43e7b 15301 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
15302 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15303 r = bfd_reloc_ok;
3de43e7b 15304 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
15305 r = bfd_reloc_overflow;
15306 }
15307 }
15308 else
15309 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15310 rel->r_offset, relocation, addend);
5bd4f169 15311
ef60b7ff 15312 if (r != bfd_reloc_ok)
5bd4f169 15313 {
bc30df16 15314 char *more_info = NULL;
b80eed39 15315 const char *reloc_name = howto->name;
bc30df16
AM
15316
15317 if (reloc_dest != DEST_NORMAL)
15318 {
15319 more_info = bfd_malloc (strlen (reloc_name) + 8);
15320 if (more_info != NULL)
15321 {
15322 strcpy (more_info, reloc_name);
15323 strcat (more_info, (reloc_dest == DEST_OPD
15324 ? " (OPD)" : " (stub)"));
15325 reloc_name = more_info;
15326 }
15327 }
15328
cd27b276 15329 if (r == bfd_reloc_overflow)
5bd4f169 15330 {
8131c122
AM
15331 /* On code like "if (foo) foo();" don't report overflow
15332 on a branch to zero when foo is undefined. */
15333 if (!warned
15334 && (reloc_dest == DEST_STUB
15335 || !(h != NULL
15336 && (h->elf.root.type == bfd_link_hash_undefweak
15337 || h->elf.root.type == bfd_link_hash_undefined)
15338 && is_branch_reloc (r_type))))
1a72702b
AM
15339 info->callbacks->reloc_overflow (info, &h->elf.root,
15340 sym_name, reloc_name,
15341 orig_rel.r_addend,
15342 input_bfd, input_section,
15343 rel->r_offset);
ef60b7ff
AM
15344 }
15345 else
15346 {
25f53a85 15347 info->callbacks->einfo
695344c0 15348 /* xgettext:c-format */
174d0a74 15349 (_("%H: %s against `%T': error %d\n"),
25f53a85 15350 input_bfd, input_section, rel->r_offset,
bc30df16 15351 reloc_name, sym_name, (int) r);
b34976b6 15352 ret = FALSE;
ef60b7ff 15353 }
bc30df16
AM
15354 if (more_info != NULL)
15355 free (more_info);
5bd4f169 15356 }
c316a17c
AM
15357 copy_reloc:
15358 if (wrel != rel)
15359 *wrel = *rel;
15360 }
15361
15362 if (wrel != rel)
15363 {
15364 Elf_Internal_Shdr *rel_hdr;
15365 size_t deleted = rel - wrel;
15366
15367 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15368 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15369 if (rel_hdr->sh_size == 0)
15370 {
15371 /* It is too late to remove an empty reloc section. Leave
15372 one NONE reloc.
15373 ??? What is wrong with an empty section??? */
15374 rel_hdr->sh_size = rel_hdr->sh_entsize;
15375 deleted -= 1;
15376 }
15377 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15378 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15379 input_section->reloc_count -= deleted;
5bd4f169
AM
15380 }
15381
645ea6a9
AM
15382 /* If we're emitting relocations, then shortly after this function
15383 returns, reloc offsets and addends for this section will be
15384 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15385 file rather than the input. Save a copy of the relocs for
15386 opd_entry_value. */
0e1862bb 15387 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15388 {
15389 bfd_size_type amt;
15390 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15391 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15392 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15393 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15394 if (rel == NULL)
15395 return FALSE;
15396 memcpy (rel, relocs, amt);
15397 }
5bd4f169
AM
15398 return ret;
15399}
15400
754021d0
AM
15401/* Adjust the value of any local symbols in opd sections. */
15402
6e0b88f1 15403static int
754021d0
AM
15404ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15405 const char *name ATTRIBUTE_UNUSED,
15406 Elf_Internal_Sym *elfsym,
15407 asection *input_sec,
15408 struct elf_link_hash_entry *h)
15409{
74f0fb50
AM
15410 struct _opd_sec_data *opd;
15411 long adjust;
754021d0
AM
15412 bfd_vma value;
15413
4025353c 15414 if (h != NULL)
6e0b88f1 15415 return 1;
4025353c 15416
74f0fb50
AM
15417 opd = get_opd_info (input_sec);
15418 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15419 return 1;
754021d0
AM
15420
15421 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15422 if (!bfd_link_relocatable (info))
754021d0
AM
15423 value -= input_sec->output_section->vma;
15424
51aecdc5 15425 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15426 if (adjust == -1)
6e0b88f1
AM
15427 return 2;
15428
15429 elfsym->st_value += adjust;
15430 return 1;
754021d0
AM
15431}
15432
5bd4f169
AM
15433/* Finish up dynamic symbol handling. We set the contents of various
15434 dynamic sections here. */
15435
b34976b6 15436static bfd_boolean
4ce794b7
AM
15437ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15438 struct bfd_link_info *info,
15439 struct elf_link_hash_entry *h,
4aef7643 15440 Elf_Internal_Sym *sym)
5bd4f169 15441{
65f38f15 15442 struct ppc_link_hash_table *htab;
8387904d
AM
15443 struct plt_entry *ent;
15444 Elf_Internal_Rela rela;
15445 bfd_byte *loc;
5bd4f169 15446
65f38f15 15447 htab = ppc_hash_table (info);
4dfe6ac6
NC
15448 if (htab == NULL)
15449 return FALSE;
5bd4f169 15450
8387904d
AM
15451 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15452 if (ent->plt.offset != (bfd_vma) -1)
15453 {
15454 /* This symbol has an entry in the procedure linkage
15455 table. Set it up. */
e054468f
AM
15456 if (!htab->elf.dynamic_sections_created
15457 || h->dynindx == -1)
15458 {
15459 BFD_ASSERT (h->type == STT_GNU_IFUNC
15460 && h->def_regular
15461 && (h->root.type == bfd_link_hash_defined
15462 || h->root.type == bfd_link_hash_defweak));
33e44f2e
AM
15463 rela.r_offset = (htab->elf.iplt->output_section->vma
15464 + htab->elf.iplt->output_offset
25f23106 15465 + ent->plt.offset);
ee67d69a
AM
15466 if (htab->opd_abi)
15467 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15468 else
15469 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
15470 rela.r_addend = (h->root.u.def.value
15471 + h->root.u.def.section->output_offset
15472 + h->root.u.def.section->output_section->vma
15473 + ent->addend);
33e44f2e
AM
15474 loc = (htab->elf.irelplt->contents
15475 + (htab->elf.irelplt->reloc_count++
25f23106 15476 * sizeof (Elf64_External_Rela)));
82e66161 15477 htab->local_ifunc_resolver = 1;
e054468f
AM
15478 }
15479 else
15480 {
33e44f2e
AM
15481 rela.r_offset = (htab->elf.splt->output_section->vma
15482 + htab->elf.splt->output_offset
25f23106 15483 + ent->plt.offset);
e054468f
AM
15484 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15485 rela.r_addend = ent->addend;
33e44f2e 15486 loc = (htab->elf.srelplt->contents
b9e5796b
AM
15487 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15488 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
82e66161
AM
15489 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
15490 htab->maybe_local_ifunc_resolver = 1;
e054468f 15491 }
8387904d 15492 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a345bc8d
AM
15493
15494 if (!htab->opd_abi)
15495 {
15496 if (!h->def_regular)
15497 {
15498 /* Mark the symbol as undefined, rather than as
15499 defined in glink. Leave the value if there were
15500 any relocations where pointer equality matters
15501 (this is a clue for the dynamic linker, to make
15502 function pointer comparisons work between an
15503 application and shared library), otherwise set it
15504 to zero. */
15505 sym->st_shndx = SHN_UNDEF;
15506 if (!h->pointer_equality_needed)
15507 sym->st_value = 0;
15508 else if (!h->ref_regular_nonweak)
15509 {
15510 /* This breaks function pointer comparisons, but
15511 that is better than breaking tests for a NULL
15512 function pointer. */
15513 sym->st_value = 0;
15514 }
15515 }
15516 }
8387904d 15517 }
5bd4f169 15518
f5385ebf 15519 if (h->needs_copy)
5bd4f169 15520 {
65f38f15 15521 /* This symbol needs a copy reloc. Set it up. */
5474d94f 15522 asection *srel;
5bd4f169 15523
65f38f15
AM
15524 if (h->dynindx == -1
15525 || (h->root.type != bfd_link_hash_defined
15526 && h->root.type != bfd_link_hash_defweak)
5474d94f
AM
15527 || htab->elf.srelbss == NULL
15528 || htab->elf.sreldynrelro == NULL)
65f38f15 15529 abort ();
5bd4f169
AM
15530
15531 rela.r_offset = (h->root.u.def.value
15532 + h->root.u.def.section->output_section->vma
15533 + h->root.u.def.section->output_offset);
15534 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15535 rela.r_addend = 0;
afbf7e8e 15536 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
15537 srel = htab->elf.sreldynrelro;
15538 else
15539 srel = htab->elf.srelbss;
15540 loc = srel->contents;
15541 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15542 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15543 }
15544
b34976b6 15545 return TRUE;
5bd4f169
AM
15546}
15547
65f38f15
AM
15548/* Used to decide how to sort relocs in an optimal manner for the
15549 dynamic linker, before writing them out. */
15550
15551static enum elf_reloc_type_class
7e612e98
AM
15552ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15553 const asection *rel_sec,
15554 const Elf_Internal_Rela *rela)
65f38f15 15555{
04c9666a 15556 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15557 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15558
33e44f2e 15559 if (rel_sec == htab->elf.irelplt)
7e612e98 15560 return reloc_class_ifunc;
a33d1f77 15561
4ce794b7 15562 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15563 switch (r_type)
65f38f15
AM
15564 {
15565 case R_PPC64_RELATIVE:
15566 return reloc_class_relative;
15567 case R_PPC64_JMP_SLOT:
15568 return reloc_class_plt;
15569 case R_PPC64_COPY:
15570 return reloc_class_copy;
15571 default:
15572 return reloc_class_normal;
15573 }
15574}
15575
5bd4f169
AM
15576/* Finish up the dynamic sections. */
15577
b34976b6 15578static bfd_boolean
4ce794b7
AM
15579ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15580 struct bfd_link_info *info)
5bd4f169 15581{
65f38f15
AM
15582 struct ppc_link_hash_table *htab;
15583 bfd *dynobj;
5bd4f169 15584 asection *sdyn;
5bd4f169 15585
65f38f15 15586 htab = ppc_hash_table (info);
4dfe6ac6
NC
15587 if (htab == NULL)
15588 return FALSE;
15589
65f38f15 15590 dynobj = htab->elf.dynobj;
3d4d4302 15591 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15592
65f38f15 15593 if (htab->elf.dynamic_sections_created)
5bd4f169 15594 {
5bd4f169
AM
15595 Elf64_External_Dyn *dyncon, *dynconend;
15596
33e44f2e 15597 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15598 abort ();
5bd4f169
AM
15599
15600 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15601 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15602 for (; dyncon < dynconend; dyncon++)
15603 {
15604 Elf_Internal_Dyn dyn;
19397422 15605 asection *s;
5bd4f169
AM
15606
15607 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15608
15609 switch (dyn.d_tag)
15610 {
65f38f15
AM
15611 default:
15612 continue;
5bd4f169 15613
5d1634d7 15614 case DT_PPC64_GLINK:
4ce794b7 15615 s = htab->glink;
6348e046 15616 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15617 /* We stupidly defined DT_PPC64_GLINK to be the start
15618 of glink rather than the first entry point, which is
15619 what ld.so needs, and now have a bigger stub to
15620 support automatic multiple TOCs. */
b9e5796b 15621 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
5d1634d7
AM
15622 break;
15623
19397422
AM
15624 case DT_PPC64_OPD:
15625 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15626 if (s == NULL)
15627 continue;
15628 dyn.d_un.d_ptr = s->vma;
19397422
AM
15629 break;
15630
e8910a83
AM
15631 case DT_PPC64_OPT:
15632 if (htab->do_multi_toc && htab->multi_toc_needed)
15633 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15634 break;
15635
19397422
AM
15636 case DT_PPC64_OPDSZ:
15637 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15638 if (s == NULL)
15639 continue;
eea6121a 15640 dyn.d_un.d_val = s->size;
19397422
AM
15641 break;
15642
65f38f15 15643 case DT_PLTGOT:
33e44f2e 15644 s = htab->elf.splt;
6348e046 15645 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15646 break;
15647
15648 case DT_JMPREL:
33e44f2e 15649 s = htab->elf.srelplt;
6348e046 15650 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15651 break;
5bd4f169 15652
65f38f15 15653 case DT_PLTRELSZ:
33e44f2e 15654 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 15655 break;
82e66161
AM
15656
15657 case DT_TEXTREL:
15658 if (htab->local_ifunc_resolver)
15659 info->callbacks->einfo
15660 (_("%X%P: text relocations and GNU indirect "
15661 "functions will result in a segfault at runtime\n"));
15662 else if (htab->maybe_local_ifunc_resolver)
15663 info->callbacks->einfo
15664 (_("%P: warning: text relocations and GNU indirect "
15665 "functions may result in a segfault at runtime\n"));
15666 continue;
5bd4f169 15667 }
5bd4f169 15668
65f38f15 15669 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15670 }
5bd4f169
AM
15671 }
15672
6528b6eb
AM
15673 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15674 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15675 {
15676 /* Fill in the first entry in the global offset table.
15677 We use it to hold the link-time TOCbase. */
15678 bfd_put_64 (output_bfd,
60ee0d4a 15679 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15680 htab->elf.sgot->contents);
5d1634d7
AM
15681
15682 /* Set .got entry size. */
33e44f2e 15683 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
15684 }
15685
6528b6eb
AM
15686 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15687 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
15688 {
15689 /* Set .plt entry size. */
33e44f2e 15690 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15691 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15692 }
15693
84f5d08e
AM
15694 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15695 brlt ourselves if emitrelocations. */
15696 if (htab->brlt != NULL
15697 && htab->brlt->reloc_count != 0
15698 && !_bfd_elf_link_output_relocs (output_bfd,
15699 htab->brlt,
d4730f92 15700 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15701 elf_section_data (htab->brlt)->relocs,
15702 NULL))
15703 return FALSE;
15704
176a0d42
AM
15705 if (htab->glink != NULL
15706 && htab->glink->reloc_count != 0
15707 && !_bfd_elf_link_output_relocs (output_bfd,
15708 htab->glink,
d4730f92 15709 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15710 elf_section_data (htab->glink)->relocs,
15711 NULL))
15712 return FALSE;
15713
58d180e8 15714 if (htab->glink_eh_frame != NULL
da44f4e5
AM
15715 && htab->glink_eh_frame->size != 0)
15716 {
15717 bfd_vma val;
15718 bfd_byte *p;
15719 asection *stub_sec;
2e0ce1c8 15720 size_t align = 4;
da44f4e5 15721
2e0ce1c8
AM
15722 p = htab->glink_eh_frame->contents;
15723 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
da44f4e5
AM
15724 for (stub_sec = htab->params->stub_bfd->sections;
15725 stub_sec != NULL;
15726 stub_sec = stub_sec->next)
15727 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15728 {
15729 /* FDE length. */
15730 p += 4;
15731 /* CIE pointer. */
15732 p += 4;
15733 /* Offset to stub section. */
15734 val = (stub_sec->output_section->vma
15735 + stub_sec->output_offset);
15736 val -= (htab->glink_eh_frame->output_section->vma
15737 + htab->glink_eh_frame->output_offset
15738 + (p - htab->glink_eh_frame->contents));
15739 if (val + 0x80000000 > 0xffffffff)
15740 {
15741 info->callbacks->einfo
15742 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15743 stub_sec->name);
15744 return FALSE;
15745 }
15746 bfd_put_32 (dynobj, val, p);
15747 p += 4;
15748 /* stub section size. */
15749 p += 4;
15750 /* Augmentation. */
15751 p += 1;
15752 /* Pad. */
2e0ce1c8 15753 p += ((17 + align - 1) & -align) - 17;
da44f4e5
AM
15754 }
15755 if (htab->glink != NULL && htab->glink->size != 0)
15756 {
15757 /* FDE length. */
15758 p += 4;
15759 /* CIE pointer. */
15760 p += 4;
15761 /* Offset to .glink. */
15762 val = (htab->glink->output_section->vma
15763 + htab->glink->output_offset
15764 + 8);
15765 val -= (htab->glink_eh_frame->output_section->vma
15766 + htab->glink_eh_frame->output_offset
15767 + (p - htab->glink_eh_frame->contents));
15768 if (val + 0x80000000 > 0xffffffff)
15769 {
15770 info->callbacks->einfo
15771 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15772 htab->glink->name);
15773 return FALSE;
15774 }
15775 bfd_put_32 (dynobj, val, p);
15776 p += 4;
15777 /* .glink size. */
15778 p += 4;
15779 /* Augmentation. */
15780 p += 1;
15781 /* Ops. */
15782 p += 7;
2e0ce1c8 15783 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
15784 }
15785
15786 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15787 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15788 htab->glink_eh_frame,
15789 htab->glink_eh_frame->contents))
15790 return FALSE;
15791 }
58d180e8 15792
e717da7e 15793 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15794 since we didn't add them to DYNOBJ. We know dynobj is the first
15795 bfd. */
c72f2fb2 15796 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15797 {
15798 asection *s;
7b53ace3 15799
0c8d6e5c 15800 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15801 continue;
15802
e717da7e
AM
15803 s = ppc64_elf_tdata (dynobj)->got;
15804 if (s != NULL
eea6121a 15805 && s->size != 0
e717da7e
AM
15806 && s->output_section != bfd_abs_section_ptr
15807 && !bfd_set_section_contents (output_bfd, s->output_section,
15808 s->contents, s->output_offset,
eea6121a 15809 s->size))
e717da7e
AM
15810 return FALSE;
15811 s = ppc64_elf_tdata (dynobj)->relgot;
15812 if (s != NULL
eea6121a 15813 && s->size != 0
e717da7e
AM
15814 && s->output_section != bfd_abs_section_ptr
15815 && !bfd_set_section_contents (output_bfd, s->output_section,
15816 s->contents, s->output_offset,
eea6121a 15817 s->size))
e717da7e
AM
15818 return FALSE;
15819 }
f6c52c13 15820
b34976b6 15821 return TRUE;
5bd4f169
AM
15822}
15823
5bd4f169 15824#include "elf64-target.h"
7b8e7dad
AM
15825
15826/* FreeBSD support */
15827
15828#undef TARGET_LITTLE_SYM
15829#undef TARGET_LITTLE_NAME
15830
15831#undef TARGET_BIG_SYM
6d00b590 15832#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15833#undef TARGET_BIG_NAME
15834#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15835
15836#undef ELF_OSABI
15837#define ELF_OSABI ELFOSABI_FREEBSD
15838
15839#undef elf64_bed
15840#define elf64_bed elf64_powerpc_fbsd_bed
15841
15842#include "elf64-target.h"
15843
This page took 2.387433 seconds and 4 git commands to generate.