]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
PowerPC64 add_symbol_adjust
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
6f2750fe 2 Copyright (C) 1999-2016 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
ad8e1ba5
AM
75#define elf_backend_can_gc_sections 1
76#define elf_backend_can_refcount 1
77#define elf_backend_rela_normal 1
64f52338 78#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 79#define elf_backend_default_execstack 0
ad8e1ba5 80
e717da7e 81#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 82#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 83#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
84#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
85#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
86#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
87#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 88#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 89#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
ad8e1ba5
AM
90
91#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
92#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
93#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 94#define elf_backend_write_core_note ppc64_elf_write_core_note
ad8e1ba5
AM
95#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
96#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 97#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 98#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 99#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 100#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 101#define elf_backend_check_relocs ppc64_elf_check_relocs
74f0fb50 102#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 103#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
104#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
105#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
106#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
107#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 108#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
109#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
110#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 111#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 112#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 113#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
114#define elf_backend_relocate_section ppc64_elf_relocate_section
115#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
116#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
117#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 118#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 119#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 120#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
ad8e1ba5 121
5bd4f169
AM
122/* The name of the dynamic interpreter. This is put in the .interp
123 section. */
124#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
125
126/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 127#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
5bd4f169
AM
128
129/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 130#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 131
a078d95a
AM
132/* Offsets to some stack save slots. */
133#define STK_LR 16
134#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 135/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
136 CR save slot. Used only by optimised __tls_get_addr call stub,
137 relying on __tls_get_addr_opt not saving CR.. */
138#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
139
5bd4f169 140/* TOC base pointers offset from start of TOC. */
411e1bfb 141#define TOC_BASE_OFF 0x8000
a27e685f
AM
142/* TOC base alignment. */
143#define TOC_BASE_ALIGN 256
411e1bfb
AM
144
145/* Offset of tp and dtp pointers from start of TLS block. */
146#define TP_OFFSET 0x7000
147#define DTP_OFFSET 0x8000
5bd4f169 148
ad8e1ba5
AM
149/* .plt call stub instructions. The normal stub is like this, but
150 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 151 insert an addi to adjust r11. */
a078d95a 152#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
153#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
154#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
155#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
156#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
157#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
158#define BCTR 0x4e800420 /* bctr */
159
71a39c98 160#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
ad8e1ba5
AM
161#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
162#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
163
71a39c98
AM
164#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
165#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
166#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
794e51c0
AM
167#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
168#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
169#define BNECTR 0x4ca20420 /* bnectr+ */
170#define BNECTR_P4 0x4ce20420 /* bnectr+ */
171
71a39c98 172#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442
AM
173#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
174#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
175
a078d95a 176#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
006589cf
AM
177#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
178#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 179
006589cf
AM
180#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
181#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
182#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
a345bc8d
AM
183#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
184#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
185
ee4bf8d2 186/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 187#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
188 /* 0: */
189 /* .quad plt0-1f */
190 /* __glink: */
191#define MFLR_R12 0x7d8802a6 /* mflr %12 */
192#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
193 /* 1: */
194#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 195 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 196#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
197#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
198 /* ld %12,0(%11) */
199 /* ld %2,8(%11) */
200 /* mtctr %12 */
201 /* ld %11,16(%11) */
ee4bf8d2 202 /* bctr */
b9e5796b
AM
203#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
204#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
205#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
206#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
207#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
208
209/* Pad with this. */
210#define NOP 0x60000000
211
721956f4
AM
212/* Some other nops. */
213#define CROR_151515 0x4def7b82
214#define CROR_313131 0x4ffffb82
215
cedb70c5 216/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
217#define LI_R0_0 0x38000000 /* li %r0,0 */
218#define B_DOT 0x48000000 /* b . */
219
220/* After that, we need two instructions to load the index, followed by
221 a branch. */
222#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 223#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 224
deb0e272
AM
225/* Instructions used by the save and restore reg functions. */
226#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
227#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
228#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
229#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
230#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
231#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
232#define LI_R12_0 0x39800000 /* li %r12,0 */
233#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
234#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
235#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
236#define BLR 0x4e800020 /* blr */
237
41bd81ab
AM
238/* Since .opd is an array of descriptors and each entry will end up
239 with identical R_PPC64_RELATIVE relocs, there is really no need to
240 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 241 relocate .opd without reloc entries. */
41bd81ab
AM
242#ifndef NO_OPD_RELOCS
243#define NO_OPD_RELOCS 0
244#endif
810d4e75 245
a4b6fadd
AM
246#ifndef ARRAY_SIZE
247#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
248#endif
249
810d4e75
AM
250static inline int
251abiversion (bfd *abfd)
252{
253 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
254}
255
256static inline void
257set_abiversion (bfd *abfd, int ver)
258{
259 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
260 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
261}
5bd4f169 262\f
f5e87a1d 263#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 264
5bd4f169 265/* Relocation HOWTO's. */
04c9666a 266static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
267
268static reloc_howto_type ppc64_elf_howto_raw[] = {
269 /* This reloc does nothing. */
270 HOWTO (R_PPC64_NONE, /* type */
271 0, /* rightshift */
6346d5ca
AM
272 3, /* size (0 = byte, 1 = short, 2 = long) */
273 0, /* bitsize */
b34976b6 274 FALSE, /* pc_relative */
5bd4f169 275 0, /* bitpos */
f5e87a1d 276 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
277 bfd_elf_generic_reloc, /* special_function */
278 "R_PPC64_NONE", /* name */
b34976b6 279 FALSE, /* partial_inplace */
d006db6c 280 0, /* src_mask */
5bd4f169 281 0, /* dst_mask */
b34976b6 282 FALSE), /* pcrel_offset */
5bd4f169
AM
283
284 /* A standard 32 bit relocation. */
285 HOWTO (R_PPC64_ADDR32, /* type */
286 0, /* rightshift */
287 2, /* size (0 = byte, 1 = short, 2 = long) */
288 32, /* bitsize */
b34976b6 289 FALSE, /* pc_relative */
5bd4f169
AM
290 0, /* bitpos */
291 complain_overflow_bitfield, /* complain_on_overflow */
292 bfd_elf_generic_reloc, /* special_function */
293 "R_PPC64_ADDR32", /* name */
b34976b6 294 FALSE, /* partial_inplace */
5bd4f169
AM
295 0, /* src_mask */
296 0xffffffff, /* dst_mask */
b34976b6 297 FALSE), /* pcrel_offset */
5bd4f169
AM
298
299 /* An absolute 26 bit branch; the lower two bits must be zero.
300 FIXME: we don't check that, we just clear them. */
301 HOWTO (R_PPC64_ADDR24, /* type */
302 0, /* rightshift */
303 2, /* size (0 = byte, 1 = short, 2 = long) */
304 26, /* bitsize */
b34976b6 305 FALSE, /* pc_relative */
5bd4f169
AM
306 0, /* bitpos */
307 complain_overflow_bitfield, /* complain_on_overflow */
308 bfd_elf_generic_reloc, /* special_function */
309 "R_PPC64_ADDR24", /* name */
b34976b6 310 FALSE, /* partial_inplace */
d006db6c 311 0, /* src_mask */
f5e87a1d 312 0x03fffffc, /* dst_mask */
b34976b6 313 FALSE), /* pcrel_offset */
5bd4f169
AM
314
315 /* A standard 16 bit relocation. */
316 HOWTO (R_PPC64_ADDR16, /* type */
317 0, /* rightshift */
318 1, /* size (0 = byte, 1 = short, 2 = long) */
319 16, /* bitsize */
b34976b6 320 FALSE, /* pc_relative */
5bd4f169
AM
321 0, /* bitpos */
322 complain_overflow_bitfield, /* complain_on_overflow */
323 bfd_elf_generic_reloc, /* special_function */
324 "R_PPC64_ADDR16", /* name */
b34976b6 325 FALSE, /* partial_inplace */
5bd4f169
AM
326 0, /* src_mask */
327 0xffff, /* dst_mask */
b34976b6 328 FALSE), /* pcrel_offset */
5bd4f169
AM
329
330 /* A 16 bit relocation without overflow. */
331 HOWTO (R_PPC64_ADDR16_LO, /* type */
332 0, /* rightshift */
333 1, /* size (0 = byte, 1 = short, 2 = long) */
334 16, /* bitsize */
b34976b6 335 FALSE, /* pc_relative */
5bd4f169
AM
336 0, /* bitpos */
337 complain_overflow_dont,/* complain_on_overflow */
338 bfd_elf_generic_reloc, /* special_function */
339 "R_PPC64_ADDR16_LO", /* name */
b34976b6 340 FALSE, /* partial_inplace */
5bd4f169
AM
341 0, /* src_mask */
342 0xffff, /* dst_mask */
b34976b6 343 FALSE), /* pcrel_offset */
5bd4f169
AM
344
345 /* Bits 16-31 of an address. */
346 HOWTO (R_PPC64_ADDR16_HI, /* type */
347 16, /* rightshift */
348 1, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
b34976b6 350 FALSE, /* pc_relative */
5bd4f169 351 0, /* bitpos */
f9c6b907 352 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
353 bfd_elf_generic_reloc, /* special_function */
354 "R_PPC64_ADDR16_HI", /* name */
b34976b6 355 FALSE, /* partial_inplace */
5bd4f169
AM
356 0, /* src_mask */
357 0xffff, /* dst_mask */
b34976b6 358 FALSE), /* pcrel_offset */
5bd4f169
AM
359
360 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
361 bits, treated as a signed number, is negative. */
362 HOWTO (R_PPC64_ADDR16_HA, /* type */
363 16, /* rightshift */
364 1, /* size (0 = byte, 1 = short, 2 = long) */
365 16, /* bitsize */
b34976b6 366 FALSE, /* pc_relative */
5bd4f169 367 0, /* bitpos */
f9c6b907 368 complain_overflow_signed, /* complain_on_overflow */
805fc799 369 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 370 "R_PPC64_ADDR16_HA", /* name */
b34976b6 371 FALSE, /* partial_inplace */
5bd4f169
AM
372 0, /* src_mask */
373 0xffff, /* dst_mask */
b34976b6 374 FALSE), /* pcrel_offset */
5bd4f169
AM
375
376 /* An absolute 16 bit branch; the lower two bits must be zero.
377 FIXME: we don't check that, we just clear them. */
378 HOWTO (R_PPC64_ADDR14, /* type */
379 0, /* rightshift */
380 2, /* size (0 = byte, 1 = short, 2 = long) */
381 16, /* bitsize */
b34976b6 382 FALSE, /* pc_relative */
5bd4f169 383 0, /* bitpos */
b80eed39 384 complain_overflow_signed, /* complain_on_overflow */
2441e016 385 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 386 "R_PPC64_ADDR14", /* name */
b34976b6 387 FALSE, /* partial_inplace */
d006db6c 388 0, /* src_mask */
f5e87a1d 389 0x0000fffc, /* dst_mask */
b34976b6 390 FALSE), /* pcrel_offset */
5bd4f169
AM
391
392 /* An absolute 16 bit branch, for which bit 10 should be set to
393 indicate that the branch is expected to be taken. The lower two
394 bits must be zero. */
395 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
396 0, /* rightshift */
397 2, /* size (0 = byte, 1 = short, 2 = long) */
398 16, /* bitsize */
b34976b6 399 FALSE, /* pc_relative */
5bd4f169 400 0, /* bitpos */
b80eed39 401 complain_overflow_signed, /* complain_on_overflow */
805fc799 402 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 403 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 404 FALSE, /* partial_inplace */
d006db6c 405 0, /* src_mask */
f5e87a1d 406 0x0000fffc, /* dst_mask */
b34976b6 407 FALSE), /* pcrel_offset */
5bd4f169
AM
408
409 /* An absolute 16 bit branch, for which bit 10 should be set to
410 indicate that the branch is not expected to be taken. The lower
411 two bits must be zero. */
412 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
413 0, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 16, /* bitsize */
b34976b6 416 FALSE, /* pc_relative */
5bd4f169 417 0, /* bitpos */
b80eed39 418 complain_overflow_signed, /* complain_on_overflow */
805fc799 419 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 420 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 421 FALSE, /* partial_inplace */
d006db6c 422 0, /* src_mask */
f5e87a1d 423 0x0000fffc, /* dst_mask */
b34976b6 424 FALSE), /* pcrel_offset */
5bd4f169
AM
425
426 /* A relative 26 bit branch; the lower two bits must be zero. */
427 HOWTO (R_PPC64_REL24, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 26, /* bitsize */
b34976b6 431 TRUE, /* pc_relative */
5bd4f169
AM
432 0, /* bitpos */
433 complain_overflow_signed, /* complain_on_overflow */
2441e016 434 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 435 "R_PPC64_REL24", /* name */
b34976b6 436 FALSE, /* partial_inplace */
d006db6c 437 0, /* src_mask */
f5e87a1d 438 0x03fffffc, /* dst_mask */
b34976b6 439 TRUE), /* pcrel_offset */
5bd4f169
AM
440
441 /* A relative 16 bit branch; the lower two bits must be zero. */
442 HOWTO (R_PPC64_REL14, /* type */
443 0, /* rightshift */
444 2, /* size (0 = byte, 1 = short, 2 = long) */
445 16, /* bitsize */
b34976b6 446 TRUE, /* pc_relative */
5bd4f169
AM
447 0, /* bitpos */
448 complain_overflow_signed, /* complain_on_overflow */
2441e016 449 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 450 "R_PPC64_REL14", /* name */
b34976b6 451 FALSE, /* partial_inplace */
d006db6c 452 0, /* src_mask */
f5e87a1d 453 0x0000fffc, /* dst_mask */
b34976b6 454 TRUE), /* pcrel_offset */
5bd4f169
AM
455
456 /* A relative 16 bit branch. Bit 10 should be set to indicate that
457 the branch is expected to be taken. The lower two bits must be
458 zero. */
459 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
460 0, /* rightshift */
461 2, /* size (0 = byte, 1 = short, 2 = long) */
462 16, /* bitsize */
b34976b6 463 TRUE, /* pc_relative */
5bd4f169
AM
464 0, /* bitpos */
465 complain_overflow_signed, /* complain_on_overflow */
805fc799 466 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 467 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 468 FALSE, /* partial_inplace */
d006db6c 469 0, /* src_mask */
f5e87a1d 470 0x0000fffc, /* dst_mask */
b34976b6 471 TRUE), /* pcrel_offset */
5bd4f169
AM
472
473 /* A relative 16 bit branch. Bit 10 should be set to indicate that
474 the branch is not expected to be taken. The lower two bits must
475 be zero. */
476 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
477 0, /* rightshift */
478 2, /* size (0 = byte, 1 = short, 2 = long) */
479 16, /* bitsize */
b34976b6 480 TRUE, /* pc_relative */
5bd4f169
AM
481 0, /* bitpos */
482 complain_overflow_signed, /* complain_on_overflow */
805fc799 483 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 484 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 485 FALSE, /* partial_inplace */
d006db6c 486 0, /* src_mask */
f5e87a1d 487 0x0000fffc, /* dst_mask */
b34976b6 488 TRUE), /* pcrel_offset */
5bd4f169
AM
489
490 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
491 symbol. */
492 HOWTO (R_PPC64_GOT16, /* type */
493 0, /* rightshift */
494 1, /* size (0 = byte, 1 = short, 2 = long) */
495 16, /* bitsize */
b34976b6 496 FALSE, /* pc_relative */
5bd4f169
AM
497 0, /* bitpos */
498 complain_overflow_signed, /* complain_on_overflow */
805fc799 499 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 500 "R_PPC64_GOT16", /* name */
b34976b6 501 FALSE, /* partial_inplace */
5bd4f169
AM
502 0, /* src_mask */
503 0xffff, /* dst_mask */
b34976b6 504 FALSE), /* pcrel_offset */
5bd4f169
AM
505
506 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
507 the symbol. */
508 HOWTO (R_PPC64_GOT16_LO, /* type */
509 0, /* rightshift */
510 1, /* size (0 = byte, 1 = short, 2 = long) */
511 16, /* bitsize */
b34976b6 512 FALSE, /* pc_relative */
5bd4f169
AM
513 0, /* bitpos */
514 complain_overflow_dont, /* complain_on_overflow */
805fc799 515 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 516 "R_PPC64_GOT16_LO", /* name */
b34976b6 517 FALSE, /* partial_inplace */
5bd4f169
AM
518 0, /* src_mask */
519 0xffff, /* dst_mask */
b34976b6 520 FALSE), /* pcrel_offset */
5bd4f169
AM
521
522 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
523 the symbol. */
524 HOWTO (R_PPC64_GOT16_HI, /* type */
525 16, /* rightshift */
526 1, /* size (0 = byte, 1 = short, 2 = long) */
527 16, /* bitsize */
b34976b6 528 FALSE, /* pc_relative */
5bd4f169 529 0, /* bitpos */
f9c6b907 530 complain_overflow_signed,/* complain_on_overflow */
805fc799 531 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 532 "R_PPC64_GOT16_HI", /* name */
b34976b6 533 FALSE, /* partial_inplace */
5bd4f169
AM
534 0, /* src_mask */
535 0xffff, /* dst_mask */
b34976b6 536 FALSE), /* pcrel_offset */
5bd4f169
AM
537
538 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
539 the symbol. */
540 HOWTO (R_PPC64_GOT16_HA, /* type */
541 16, /* rightshift */
542 1, /* size (0 = byte, 1 = short, 2 = long) */
543 16, /* bitsize */
b34976b6 544 FALSE, /* pc_relative */
5bd4f169 545 0, /* bitpos */
f9c6b907 546 complain_overflow_signed,/* complain_on_overflow */
805fc799 547 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 548 "R_PPC64_GOT16_HA", /* name */
b34976b6 549 FALSE, /* partial_inplace */
5bd4f169
AM
550 0, /* src_mask */
551 0xffff, /* dst_mask */
b34976b6 552 FALSE), /* pcrel_offset */
5bd4f169
AM
553
554 /* This is used only by the dynamic linker. The symbol should exist
555 both in the object being run and in some shared library. The
556 dynamic linker copies the data addressed by the symbol from the
557 shared library into the object, because the object being
558 run has to have the data at some particular address. */
559 HOWTO (R_PPC64_COPY, /* type */
560 0, /* rightshift */
f5e87a1d
AM
561 0, /* this one is variable size */
562 0, /* bitsize */
b34976b6 563 FALSE, /* pc_relative */
5bd4f169 564 0, /* bitpos */
f5e87a1d
AM
565 complain_overflow_dont, /* complain_on_overflow */
566 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 567 "R_PPC64_COPY", /* name */
b34976b6 568 FALSE, /* partial_inplace */
5bd4f169
AM
569 0, /* src_mask */
570 0, /* dst_mask */
b34976b6 571 FALSE), /* pcrel_offset */
5bd4f169
AM
572
573 /* Like R_PPC64_ADDR64, but used when setting global offset table
574 entries. */
575 HOWTO (R_PPC64_GLOB_DAT, /* type */
576 0, /* rightshift */
577 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
578 64, /* bitsize */
b34976b6 579 FALSE, /* pc_relative */
5bd4f169
AM
580 0, /* bitpos */
581 complain_overflow_dont, /* complain_on_overflow */
805fc799 582 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 583 "R_PPC64_GLOB_DAT", /* name */
b34976b6 584 FALSE, /* partial_inplace */
5bd4f169 585 0, /* src_mask */
f5e87a1d 586 ONES (64), /* dst_mask */
b34976b6 587 FALSE), /* pcrel_offset */
5bd4f169
AM
588
589 /* Created by the link editor. Marks a procedure linkage table
590 entry for a symbol. */
591 HOWTO (R_PPC64_JMP_SLOT, /* type */
592 0, /* rightshift */
593 0, /* size (0 = byte, 1 = short, 2 = long) */
594 0, /* bitsize */
b34976b6 595 FALSE, /* pc_relative */
5bd4f169
AM
596 0, /* bitpos */
597 complain_overflow_dont, /* complain_on_overflow */
805fc799 598 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 599 "R_PPC64_JMP_SLOT", /* name */
b34976b6 600 FALSE, /* partial_inplace */
5bd4f169
AM
601 0, /* src_mask */
602 0, /* dst_mask */
b34976b6 603 FALSE), /* pcrel_offset */
5bd4f169
AM
604
605 /* Used only by the dynamic linker. When the object is run, this
606 doubleword64 is set to the load address of the object, plus the
607 addend. */
608 HOWTO (R_PPC64_RELATIVE, /* type */
609 0, /* rightshift */
610 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
611 64, /* bitsize */
b34976b6 612 FALSE, /* pc_relative */
5bd4f169
AM
613 0, /* bitpos */
614 complain_overflow_dont, /* complain_on_overflow */
615 bfd_elf_generic_reloc, /* special_function */
616 "R_PPC64_RELATIVE", /* name */
b34976b6 617 FALSE, /* partial_inplace */
5bd4f169 618 0, /* src_mask */
f5e87a1d 619 ONES (64), /* dst_mask */
b34976b6 620 FALSE), /* pcrel_offset */
5bd4f169
AM
621
622 /* Like R_PPC64_ADDR32, but may be unaligned. */
623 HOWTO (R_PPC64_UADDR32, /* type */
624 0, /* rightshift */
625 2, /* size (0 = byte, 1 = short, 2 = long) */
626 32, /* bitsize */
b34976b6 627 FALSE, /* pc_relative */
5bd4f169
AM
628 0, /* bitpos */
629 complain_overflow_bitfield, /* complain_on_overflow */
630 bfd_elf_generic_reloc, /* special_function */
631 "R_PPC64_UADDR32", /* name */
b34976b6 632 FALSE, /* partial_inplace */
5bd4f169
AM
633 0, /* src_mask */
634 0xffffffff, /* dst_mask */
b34976b6 635 FALSE), /* pcrel_offset */
5bd4f169
AM
636
637 /* Like R_PPC64_ADDR16, but may be unaligned. */
638 HOWTO (R_PPC64_UADDR16, /* type */
639 0, /* rightshift */
640 1, /* size (0 = byte, 1 = short, 2 = long) */
641 16, /* bitsize */
b34976b6 642 FALSE, /* pc_relative */
5bd4f169
AM
643 0, /* bitpos */
644 complain_overflow_bitfield, /* complain_on_overflow */
645 bfd_elf_generic_reloc, /* special_function */
646 "R_PPC64_UADDR16", /* name */
b34976b6 647 FALSE, /* partial_inplace */
5bd4f169
AM
648 0, /* src_mask */
649 0xffff, /* dst_mask */
b34976b6 650 FALSE), /* pcrel_offset */
5bd4f169
AM
651
652 /* 32-bit PC relative. */
653 HOWTO (R_PPC64_REL32, /* type */
654 0, /* rightshift */
655 2, /* size (0 = byte, 1 = short, 2 = long) */
656 32, /* bitsize */
b34976b6 657 TRUE, /* pc_relative */
5bd4f169 658 0, /* bitpos */
5bd4f169
AM
659 complain_overflow_signed, /* complain_on_overflow */
660 bfd_elf_generic_reloc, /* special_function */
661 "R_PPC64_REL32", /* name */
b34976b6 662 FALSE, /* partial_inplace */
5bd4f169
AM
663 0, /* src_mask */
664 0xffffffff, /* dst_mask */
b34976b6 665 TRUE), /* pcrel_offset */
5bd4f169 666
10ed1bba 667 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
668 HOWTO (R_PPC64_PLT32, /* type */
669 0, /* rightshift */
670 2, /* size (0 = byte, 1 = short, 2 = long) */
671 32, /* bitsize */
b34976b6 672 FALSE, /* pc_relative */
5bd4f169
AM
673 0, /* bitpos */
674 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 675 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 676 "R_PPC64_PLT32", /* name */
b34976b6 677 FALSE, /* partial_inplace */
5bd4f169 678 0, /* src_mask */
f5e87a1d 679 0xffffffff, /* dst_mask */
b34976b6 680 FALSE), /* pcrel_offset */
5bd4f169
AM
681
682 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
683 FIXME: R_PPC64_PLTREL32 not supported. */
684 HOWTO (R_PPC64_PLTREL32, /* type */
685 0, /* rightshift */
686 2, /* size (0 = byte, 1 = short, 2 = long) */
687 32, /* bitsize */
b34976b6 688 TRUE, /* pc_relative */
5bd4f169
AM
689 0, /* bitpos */
690 complain_overflow_signed, /* complain_on_overflow */
3ce51288 691 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 692 "R_PPC64_PLTREL32", /* name */
b34976b6 693 FALSE, /* partial_inplace */
5bd4f169 694 0, /* src_mask */
f5e87a1d 695 0xffffffff, /* dst_mask */
b34976b6 696 TRUE), /* pcrel_offset */
5bd4f169
AM
697
698 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
699 the symbol. */
700 HOWTO (R_PPC64_PLT16_LO, /* type */
701 0, /* rightshift */
702 1, /* size (0 = byte, 1 = short, 2 = long) */
703 16, /* bitsize */
b34976b6 704 FALSE, /* pc_relative */
5bd4f169
AM
705 0, /* bitpos */
706 complain_overflow_dont, /* complain_on_overflow */
805fc799 707 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 708 "R_PPC64_PLT16_LO", /* name */
b34976b6 709 FALSE, /* partial_inplace */
5bd4f169
AM
710 0, /* src_mask */
711 0xffff, /* dst_mask */
b34976b6 712 FALSE), /* pcrel_offset */
5bd4f169
AM
713
714 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
715 the symbol. */
716 HOWTO (R_PPC64_PLT16_HI, /* type */
717 16, /* rightshift */
718 1, /* size (0 = byte, 1 = short, 2 = long) */
719 16, /* bitsize */
b34976b6 720 FALSE, /* pc_relative */
5bd4f169 721 0, /* bitpos */
f9c6b907 722 complain_overflow_signed, /* complain_on_overflow */
805fc799 723 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 724 "R_PPC64_PLT16_HI", /* name */
b34976b6 725 FALSE, /* partial_inplace */
5bd4f169
AM
726 0, /* src_mask */
727 0xffff, /* dst_mask */
b34976b6 728 FALSE), /* pcrel_offset */
5bd4f169
AM
729
730 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
731 the symbol. */
732 HOWTO (R_PPC64_PLT16_HA, /* type */
733 16, /* rightshift */
734 1, /* size (0 = byte, 1 = short, 2 = long) */
735 16, /* bitsize */
b34976b6 736 FALSE, /* pc_relative */
5bd4f169 737 0, /* bitpos */
f9c6b907 738 complain_overflow_signed, /* complain_on_overflow */
805fc799 739 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 740 "R_PPC64_PLT16_HA", /* name */
b34976b6 741 FALSE, /* partial_inplace */
5bd4f169
AM
742 0, /* src_mask */
743 0xffff, /* dst_mask */
b34976b6 744 FALSE), /* pcrel_offset */
5bd4f169 745
c061c2d8 746 /* 16-bit section relative relocation. */
5bd4f169
AM
747 HOWTO (R_PPC64_SECTOFF, /* type */
748 0, /* rightshift */
c061c2d8
AM
749 1, /* size (0 = byte, 1 = short, 2 = long) */
750 16, /* bitsize */
b34976b6 751 FALSE, /* pc_relative */
5bd4f169 752 0, /* bitpos */
b80eed39 753 complain_overflow_signed, /* complain_on_overflow */
805fc799 754 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 755 "R_PPC64_SECTOFF", /* name */
b34976b6 756 FALSE, /* partial_inplace */
5bd4f169 757 0, /* src_mask */
c061c2d8 758 0xffff, /* dst_mask */
b34976b6 759 FALSE), /* pcrel_offset */
5bd4f169 760
c061c2d8 761 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
762 HOWTO (R_PPC64_SECTOFF_LO, /* type */
763 0, /* rightshift */
764 1, /* size (0 = byte, 1 = short, 2 = long) */
765 16, /* bitsize */
b34976b6 766 FALSE, /* pc_relative */
5bd4f169
AM
767 0, /* bitpos */
768 complain_overflow_dont, /* complain_on_overflow */
805fc799 769 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 770 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 771 FALSE, /* partial_inplace */
5bd4f169
AM
772 0, /* src_mask */
773 0xffff, /* dst_mask */
b34976b6 774 FALSE), /* pcrel_offset */
5bd4f169
AM
775
776 /* 16-bit upper half section relative relocation. */
777 HOWTO (R_PPC64_SECTOFF_HI, /* type */
778 16, /* rightshift */
779 1, /* size (0 = byte, 1 = short, 2 = long) */
780 16, /* bitsize */
b34976b6 781 FALSE, /* pc_relative */
5bd4f169 782 0, /* bitpos */
f9c6b907 783 complain_overflow_signed, /* complain_on_overflow */
805fc799 784 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 785 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 786 FALSE, /* partial_inplace */
5bd4f169
AM
787 0, /* src_mask */
788 0xffff, /* dst_mask */
b34976b6 789 FALSE), /* pcrel_offset */
5bd4f169
AM
790
791 /* 16-bit upper half adjusted section relative relocation. */
792 HOWTO (R_PPC64_SECTOFF_HA, /* type */
793 16, /* rightshift */
794 1, /* size (0 = byte, 1 = short, 2 = long) */
795 16, /* bitsize */
b34976b6 796 FALSE, /* pc_relative */
5bd4f169 797 0, /* bitpos */
f9c6b907 798 complain_overflow_signed, /* complain_on_overflow */
805fc799 799 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 800 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 801 FALSE, /* partial_inplace */
5bd4f169
AM
802 0, /* src_mask */
803 0xffff, /* dst_mask */
b34976b6 804 FALSE), /* pcrel_offset */
5bd4f169 805
04c9666a
AM
806 /* Like R_PPC64_REL24 without touching the two least significant bits. */
807 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
808 2, /* rightshift */
809 2, /* size (0 = byte, 1 = short, 2 = long) */
810 30, /* bitsize */
b34976b6 811 TRUE, /* pc_relative */
5bd4f169
AM
812 0, /* bitpos */
813 complain_overflow_dont, /* complain_on_overflow */
814 bfd_elf_generic_reloc, /* special_function */
04c9666a 815 "R_PPC64_REL30", /* name */
b34976b6 816 FALSE, /* partial_inplace */
d006db6c 817 0, /* src_mask */
5bd4f169 818 0xfffffffc, /* dst_mask */
b34976b6 819 TRUE), /* pcrel_offset */
5bd4f169
AM
820
821 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
822
823 /* A standard 64-bit relocation. */
824 HOWTO (R_PPC64_ADDR64, /* type */
825 0, /* rightshift */
826 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
827 64, /* bitsize */
b34976b6 828 FALSE, /* pc_relative */
5bd4f169
AM
829 0, /* bitpos */
830 complain_overflow_dont, /* complain_on_overflow */
831 bfd_elf_generic_reloc, /* special_function */
832 "R_PPC64_ADDR64", /* name */
b34976b6 833 FALSE, /* partial_inplace */
5bd4f169 834 0, /* src_mask */
f5e87a1d 835 ONES (64), /* dst_mask */
b34976b6 836 FALSE), /* pcrel_offset */
5bd4f169
AM
837
838 /* The bits 32-47 of an address. */
839 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
840 32, /* rightshift */
841 1, /* size (0 = byte, 1 = short, 2 = long) */
842 16, /* bitsize */
b34976b6 843 FALSE, /* pc_relative */
5bd4f169
AM
844 0, /* bitpos */
845 complain_overflow_dont, /* complain_on_overflow */
846 bfd_elf_generic_reloc, /* special_function */
847 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 848 FALSE, /* partial_inplace */
5bd4f169
AM
849 0, /* src_mask */
850 0xffff, /* dst_mask */
b34976b6 851 FALSE), /* pcrel_offset */
5bd4f169
AM
852
853 /* The bits 32-47 of an address, plus 1 if the contents of the low
854 16 bits, treated as a signed number, is negative. */
855 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
856 32, /* rightshift */
857 1, /* size (0 = byte, 1 = short, 2 = long) */
858 16, /* bitsize */
b34976b6 859 FALSE, /* pc_relative */
5bd4f169
AM
860 0, /* bitpos */
861 complain_overflow_dont, /* complain_on_overflow */
805fc799 862 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 863 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 864 FALSE, /* partial_inplace */
5bd4f169
AM
865 0, /* src_mask */
866 0xffff, /* dst_mask */
b34976b6 867 FALSE), /* pcrel_offset */
5bd4f169
AM
868
869 /* The bits 48-63 of an address. */
870 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
871 48, /* rightshift */
872 1, /* size (0 = byte, 1 = short, 2 = long) */
873 16, /* bitsize */
b34976b6 874 FALSE, /* pc_relative */
5bd4f169
AM
875 0, /* bitpos */
876 complain_overflow_dont, /* complain_on_overflow */
877 bfd_elf_generic_reloc, /* special_function */
878 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 879 FALSE, /* partial_inplace */
5bd4f169
AM
880 0, /* src_mask */
881 0xffff, /* dst_mask */
b34976b6 882 FALSE), /* pcrel_offset */
5bd4f169
AM
883
884 /* The bits 48-63 of an address, plus 1 if the contents of the low
885 16 bits, treated as a signed number, is negative. */
886 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
887 48, /* rightshift */
888 1, /* size (0 = byte, 1 = short, 2 = long) */
889 16, /* bitsize */
b34976b6 890 FALSE, /* pc_relative */
5bd4f169
AM
891 0, /* bitpos */
892 complain_overflow_dont, /* complain_on_overflow */
805fc799 893 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 894 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 895 FALSE, /* partial_inplace */
5bd4f169
AM
896 0, /* src_mask */
897 0xffff, /* dst_mask */
b34976b6 898 FALSE), /* pcrel_offset */
5bd4f169
AM
899
900 /* Like ADDR64, but may be unaligned. */
901 HOWTO (R_PPC64_UADDR64, /* type */
902 0, /* rightshift */
903 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
904 64, /* bitsize */
b34976b6 905 FALSE, /* pc_relative */
5bd4f169
AM
906 0, /* bitpos */
907 complain_overflow_dont, /* complain_on_overflow */
908 bfd_elf_generic_reloc, /* special_function */
909 "R_PPC64_UADDR64", /* name */
b34976b6 910 FALSE, /* partial_inplace */
5bd4f169 911 0, /* src_mask */
f5e87a1d 912 ONES (64), /* dst_mask */
b34976b6 913 FALSE), /* pcrel_offset */
5bd4f169
AM
914
915 /* 64-bit relative relocation. */
916 HOWTO (R_PPC64_REL64, /* type */
917 0, /* rightshift */
918 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
919 64, /* bitsize */
b34976b6 920 TRUE, /* pc_relative */
5bd4f169
AM
921 0, /* bitpos */
922 complain_overflow_dont, /* complain_on_overflow */
923 bfd_elf_generic_reloc, /* special_function */
924 "R_PPC64_REL64", /* name */
b34976b6 925 FALSE, /* partial_inplace */
5bd4f169 926 0, /* src_mask */
f5e87a1d 927 ONES (64), /* dst_mask */
b34976b6 928 TRUE), /* pcrel_offset */
5bd4f169 929
cedb70c5 930 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
931 HOWTO (R_PPC64_PLT64, /* type */
932 0, /* rightshift */
933 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
934 64, /* bitsize */
b34976b6 935 FALSE, /* pc_relative */
5bd4f169
AM
936 0, /* bitpos */
937 complain_overflow_dont, /* complain_on_overflow */
805fc799 938 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 939 "R_PPC64_PLT64", /* name */
b34976b6 940 FALSE, /* partial_inplace */
5bd4f169 941 0, /* src_mask */
f5e87a1d 942 ONES (64), /* dst_mask */
b34976b6 943 FALSE), /* pcrel_offset */
5bd4f169
AM
944
945 /* 64-bit PC relative relocation to the symbol's procedure linkage
946 table. */
947 /* FIXME: R_PPC64_PLTREL64 not supported. */
948 HOWTO (R_PPC64_PLTREL64, /* type */
949 0, /* rightshift */
950 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
951 64, /* bitsize */
b34976b6 952 TRUE, /* pc_relative */
5bd4f169
AM
953 0, /* bitpos */
954 complain_overflow_dont, /* complain_on_overflow */
805fc799 955 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 956 "R_PPC64_PLTREL64", /* name */
b34976b6 957 FALSE, /* partial_inplace */
5bd4f169 958 0, /* src_mask */
f5e87a1d 959 ONES (64), /* dst_mask */
b34976b6 960 TRUE), /* pcrel_offset */
5bd4f169
AM
961
962 /* 16 bit TOC-relative relocation. */
963
964 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
965 HOWTO (R_PPC64_TOC16, /* type */
966 0, /* rightshift */
967 1, /* size (0 = byte, 1 = short, 2 = long) */
968 16, /* bitsize */
b34976b6 969 FALSE, /* pc_relative */
5bd4f169
AM
970 0, /* bitpos */
971 complain_overflow_signed, /* complain_on_overflow */
805fc799 972 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 973 "R_PPC64_TOC16", /* name */
b34976b6 974 FALSE, /* partial_inplace */
5bd4f169
AM
975 0, /* src_mask */
976 0xffff, /* dst_mask */
b34976b6 977 FALSE), /* pcrel_offset */
5bd4f169
AM
978
979 /* 16 bit TOC-relative relocation without overflow. */
980
981 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
982 HOWTO (R_PPC64_TOC16_LO, /* type */
983 0, /* rightshift */
984 1, /* size (0 = byte, 1 = short, 2 = long) */
985 16, /* bitsize */
b34976b6 986 FALSE, /* pc_relative */
5bd4f169
AM
987 0, /* bitpos */
988 complain_overflow_dont, /* complain_on_overflow */
805fc799 989 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 990 "R_PPC64_TOC16_LO", /* name */
b34976b6 991 FALSE, /* partial_inplace */
5bd4f169
AM
992 0, /* src_mask */
993 0xffff, /* dst_mask */
b34976b6 994 FALSE), /* pcrel_offset */
5bd4f169
AM
995
996 /* 16 bit TOC-relative relocation, high 16 bits. */
997
998 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
999 HOWTO (R_PPC64_TOC16_HI, /* type */
1000 16, /* rightshift */
1001 1, /* size (0 = byte, 1 = short, 2 = long) */
1002 16, /* bitsize */
b34976b6 1003 FALSE, /* pc_relative */
5bd4f169 1004 0, /* bitpos */
f9c6b907 1005 complain_overflow_signed, /* complain_on_overflow */
805fc799 1006 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 1007 "R_PPC64_TOC16_HI", /* name */
b34976b6 1008 FALSE, /* partial_inplace */
5bd4f169
AM
1009 0, /* src_mask */
1010 0xffff, /* dst_mask */
b34976b6 1011 FALSE), /* pcrel_offset */
5bd4f169
AM
1012
1013 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1014 contents of the low 16 bits, treated as a signed number, is
1015 negative. */
1016
1017 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1018 HOWTO (R_PPC64_TOC16_HA, /* type */
1019 16, /* rightshift */
1020 1, /* size (0 = byte, 1 = short, 2 = long) */
1021 16, /* bitsize */
b34976b6 1022 FALSE, /* pc_relative */
5bd4f169 1023 0, /* bitpos */
f9c6b907 1024 complain_overflow_signed, /* complain_on_overflow */
805fc799 1025 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 1026 "R_PPC64_TOC16_HA", /* name */
b34976b6 1027 FALSE, /* partial_inplace */
5bd4f169
AM
1028 0, /* src_mask */
1029 0xffff, /* dst_mask */
b34976b6 1030 FALSE), /* pcrel_offset */
5bd4f169
AM
1031
1032 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1033
1034 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1035 HOWTO (R_PPC64_TOC, /* type */
1036 0, /* rightshift */
1037 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1038 64, /* bitsize */
b34976b6 1039 FALSE, /* pc_relative */
5bd4f169 1040 0, /* bitpos */
b80eed39 1041 complain_overflow_dont, /* complain_on_overflow */
805fc799 1042 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1043 "R_PPC64_TOC", /* name */
b34976b6 1044 FALSE, /* partial_inplace */
5bd4f169 1045 0, /* src_mask */
f5e87a1d 1046 ONES (64), /* dst_mask */
b34976b6 1047 FALSE), /* pcrel_offset */
5bd4f169
AM
1048
1049 /* Like R_PPC64_GOT16, but also informs the link editor that the
1050 value to relocate may (!) refer to a PLT entry which the link
1051 editor (a) may replace with the symbol value. If the link editor
1052 is unable to fully resolve the symbol, it may (b) create a PLT
1053 entry and store the address to the new PLT entry in the GOT.
1054 This permits lazy resolution of function symbols at run time.
1055 The link editor may also skip all of this and just (c) emit a
1056 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1057 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1058 HOWTO (R_PPC64_PLTGOT16, /* type */
1059 0, /* rightshift */
1060 1, /* size (0 = byte, 1 = short, 2 = long) */
1061 16, /* bitsize */
b34976b6 1062 FALSE, /* pc_relative */
5bd4f169
AM
1063 0, /* bitpos */
1064 complain_overflow_signed, /* complain_on_overflow */
805fc799 1065 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1066 "R_PPC64_PLTGOT16", /* name */
1067 FALSE, /* partial_inplace */
1068 0, /* src_mask */
1069 0xffff, /* dst_mask */
1070 FALSE), /* pcrel_offset */
1071
1072 /* Like R_PPC64_PLTGOT16, but without overflow. */
1073 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1074 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1075 0, /* rightshift */
1076 1, /* size (0 = byte, 1 = short, 2 = long) */
1077 16, /* bitsize */
1078 FALSE, /* pc_relative */
1079 0, /* bitpos */
1080 complain_overflow_dont, /* complain_on_overflow */
1081 ppc64_elf_unhandled_reloc, /* special_function */
1082 "R_PPC64_PLTGOT16_LO", /* name */
1083 FALSE, /* partial_inplace */
1084 0, /* src_mask */
1085 0xffff, /* dst_mask */
1086 FALSE), /* pcrel_offset */
1087
1088 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1089 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1090 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1091 16, /* rightshift */
1092 1, /* size (0 = byte, 1 = short, 2 = long) */
1093 16, /* bitsize */
1094 FALSE, /* pc_relative */
1095 0, /* bitpos */
f9c6b907 1096 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1097 ppc64_elf_unhandled_reloc, /* special_function */
1098 "R_PPC64_PLTGOT16_HI", /* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xffff, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1103
1104 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1105 1 if the contents of the low 16 bits, treated as a signed number,
1106 is negative. */
1107 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1108 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1109 16, /* rightshift */
1110 1, /* size (0 = byte, 1 = short, 2 = long) */
1111 16, /* bitsize */
1112 FALSE, /* pc_relative */
1113 0, /* bitpos */
f9c6b907 1114 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1115 ppc64_elf_unhandled_reloc, /* special_function */
1116 "R_PPC64_PLTGOT16_HA", /* name */
1117 FALSE, /* partial_inplace */
1118 0, /* src_mask */
1119 0xffff, /* dst_mask */
1120 FALSE), /* pcrel_offset */
1121
1122 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1123 HOWTO (R_PPC64_ADDR16_DS, /* type */
1124 0, /* rightshift */
1125 1, /* size (0 = byte, 1 = short, 2 = long) */
1126 16, /* bitsize */
1127 FALSE, /* pc_relative */
1128 0, /* bitpos */
b80eed39 1129 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1130 bfd_elf_generic_reloc, /* special_function */
1131 "R_PPC64_ADDR16_DS", /* name */
1132 FALSE, /* partial_inplace */
1133 0, /* src_mask */
1134 0xfffc, /* dst_mask */
1135 FALSE), /* pcrel_offset */
1136
1137 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1138 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1139 0, /* rightshift */
1140 1, /* size (0 = byte, 1 = short, 2 = long) */
1141 16, /* bitsize */
1142 FALSE, /* pc_relative */
1143 0, /* bitpos */
1144 complain_overflow_dont,/* complain_on_overflow */
1145 bfd_elf_generic_reloc, /* special_function */
1146 "R_PPC64_ADDR16_LO_DS",/* name */
1147 FALSE, /* partial_inplace */
1148 0, /* src_mask */
1149 0xfffc, /* dst_mask */
1150 FALSE), /* pcrel_offset */
1151
1152 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1153 HOWTO (R_PPC64_GOT16_DS, /* type */
1154 0, /* rightshift */
1155 1, /* size (0 = byte, 1 = short, 2 = long) */
1156 16, /* bitsize */
1157 FALSE, /* pc_relative */
1158 0, /* bitpos */
1159 complain_overflow_signed, /* complain_on_overflow */
1160 ppc64_elf_unhandled_reloc, /* special_function */
1161 "R_PPC64_GOT16_DS", /* name */
1162 FALSE, /* partial_inplace */
1163 0, /* src_mask */
1164 0xfffc, /* dst_mask */
1165 FALSE), /* pcrel_offset */
1166
1167 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1168 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1169 0, /* rightshift */
1170 1, /* size (0 = byte, 1 = short, 2 = long) */
1171 16, /* bitsize */
1172 FALSE, /* pc_relative */
1173 0, /* bitpos */
1174 complain_overflow_dont, /* complain_on_overflow */
1175 ppc64_elf_unhandled_reloc, /* special_function */
1176 "R_PPC64_GOT16_LO_DS", /* name */
1177 FALSE, /* partial_inplace */
1178 0, /* src_mask */
1179 0xfffc, /* dst_mask */
1180 FALSE), /* pcrel_offset */
1181
1182 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1183 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1184 0, /* rightshift */
1185 1, /* size (0 = byte, 1 = short, 2 = long) */
1186 16, /* bitsize */
1187 FALSE, /* pc_relative */
1188 0, /* bitpos */
1189 complain_overflow_dont, /* complain_on_overflow */
1190 ppc64_elf_unhandled_reloc, /* special_function */
1191 "R_PPC64_PLT16_LO_DS", /* name */
1192 FALSE, /* partial_inplace */
1193 0, /* src_mask */
1194 0xfffc, /* dst_mask */
1195 FALSE), /* pcrel_offset */
1196
1197 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1198 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1199 0, /* rightshift */
1200 1, /* size (0 = byte, 1 = short, 2 = long) */
1201 16, /* bitsize */
1202 FALSE, /* pc_relative */
1203 0, /* bitpos */
b80eed39 1204 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1205 ppc64_elf_sectoff_reloc, /* special_function */
1206 "R_PPC64_SECTOFF_DS", /* name */
1207 FALSE, /* partial_inplace */
1208 0, /* src_mask */
1209 0xfffc, /* dst_mask */
1210 FALSE), /* pcrel_offset */
1211
1212 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1213 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1214 0, /* rightshift */
1215 1, /* size (0 = byte, 1 = short, 2 = long) */
1216 16, /* bitsize */
1217 FALSE, /* pc_relative */
1218 0, /* bitpos */
1219 complain_overflow_dont, /* complain_on_overflow */
1220 ppc64_elf_sectoff_reloc, /* special_function */
1221 "R_PPC64_SECTOFF_LO_DS",/* name */
1222 FALSE, /* partial_inplace */
1223 0, /* src_mask */
1224 0xfffc, /* dst_mask */
1225 FALSE), /* pcrel_offset */
1226
1227 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1228 HOWTO (R_PPC64_TOC16_DS, /* type */
1229 0, /* rightshift */
1230 1, /* size (0 = byte, 1 = short, 2 = long) */
1231 16, /* bitsize */
1232 FALSE, /* pc_relative */
1233 0, /* bitpos */
1234 complain_overflow_signed, /* complain_on_overflow */
1235 ppc64_elf_toc_reloc, /* special_function */
1236 "R_PPC64_TOC16_DS", /* name */
1237 FALSE, /* partial_inplace */
1238 0, /* src_mask */
1239 0xfffc, /* dst_mask */
1240 FALSE), /* pcrel_offset */
1241
1242 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1243 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1244 0, /* rightshift */
1245 1, /* size (0 = byte, 1 = short, 2 = long) */
1246 16, /* bitsize */
1247 FALSE, /* pc_relative */
1248 0, /* bitpos */
1249 complain_overflow_dont, /* complain_on_overflow */
1250 ppc64_elf_toc_reloc, /* special_function */
1251 "R_PPC64_TOC16_LO_DS", /* name */
1252 FALSE, /* partial_inplace */
1253 0, /* src_mask */
1254 0xfffc, /* dst_mask */
1255 FALSE), /* pcrel_offset */
1256
1257 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1258 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1259 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1260 0, /* rightshift */
1261 1, /* size (0 = byte, 1 = short, 2 = long) */
1262 16, /* bitsize */
1263 FALSE, /* pc_relative */
1264 0, /* bitpos */
1265 complain_overflow_signed, /* complain_on_overflow */
1266 ppc64_elf_unhandled_reloc, /* special_function */
1267 "R_PPC64_PLTGOT16_DS", /* name */
1268 FALSE, /* partial_inplace */
1269 0, /* src_mask */
1270 0xfffc, /* dst_mask */
1271 FALSE), /* pcrel_offset */
1272
1273 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1274 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1275 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1276 0, /* rightshift */
1277 1, /* size (0 = byte, 1 = short, 2 = long) */
1278 16, /* bitsize */
1279 FALSE, /* pc_relative */
1280 0, /* bitpos */
1281 complain_overflow_dont, /* complain_on_overflow */
1282 ppc64_elf_unhandled_reloc, /* special_function */
1283 "R_PPC64_PLTGOT16_LO_DS",/* name */
1284 FALSE, /* partial_inplace */
1285 0, /* src_mask */
1286 0xfffc, /* dst_mask */
1287 FALSE), /* pcrel_offset */
1288
727fc41e 1289 /* Marker relocs for TLS. */
411e1bfb
AM
1290 HOWTO (R_PPC64_TLS,
1291 0, /* rightshift */
1292 2, /* size (0 = byte, 1 = short, 2 = long) */
1293 32, /* bitsize */
1294 FALSE, /* pc_relative */
1295 0, /* bitpos */
1296 complain_overflow_dont, /* complain_on_overflow */
1297 bfd_elf_generic_reloc, /* special_function */
1298 "R_PPC64_TLS", /* name */
1299 FALSE, /* partial_inplace */
1300 0, /* src_mask */
1301 0, /* dst_mask */
1302 FALSE), /* pcrel_offset */
1303
727fc41e
AM
1304 HOWTO (R_PPC64_TLSGD,
1305 0, /* rightshift */
1306 2, /* size (0 = byte, 1 = short, 2 = long) */
1307 32, /* bitsize */
1308 FALSE, /* pc_relative */
1309 0, /* bitpos */
1310 complain_overflow_dont, /* complain_on_overflow */
1311 bfd_elf_generic_reloc, /* special_function */
1312 "R_PPC64_TLSGD", /* name */
1313 FALSE, /* partial_inplace */
1314 0, /* src_mask */
1315 0, /* dst_mask */
1316 FALSE), /* pcrel_offset */
1317
1318 HOWTO (R_PPC64_TLSLD,
1319 0, /* rightshift */
1320 2, /* size (0 = byte, 1 = short, 2 = long) */
1321 32, /* bitsize */
1322 FALSE, /* pc_relative */
1323 0, /* bitpos */
1324 complain_overflow_dont, /* complain_on_overflow */
1325 bfd_elf_generic_reloc, /* special_function */
1326 "R_PPC64_TLSLD", /* name */
1327 FALSE, /* partial_inplace */
1328 0, /* src_mask */
1329 0, /* dst_mask */
1330 FALSE), /* pcrel_offset */
1331
3b421ab3
AM
1332 HOWTO (R_PPC64_TOCSAVE,
1333 0, /* rightshift */
1334 2, /* size (0 = byte, 1 = short, 2 = long) */
1335 32, /* bitsize */
1336 FALSE, /* pc_relative */
1337 0, /* bitpos */
1338 complain_overflow_dont, /* complain_on_overflow */
1339 bfd_elf_generic_reloc, /* special_function */
1340 "R_PPC64_TOCSAVE", /* name */
1341 FALSE, /* partial_inplace */
1342 0, /* src_mask */
1343 0, /* dst_mask */
1344 FALSE), /* pcrel_offset */
1345
411e1bfb
AM
1346 /* Computes the load module index of the load module that contains the
1347 definition of its TLS sym. */
1348 HOWTO (R_PPC64_DTPMOD64,
1349 0, /* rightshift */
1350 4, /* size (0 = byte, 1 = short, 2 = long) */
1351 64, /* bitsize */
1352 FALSE, /* pc_relative */
1353 0, /* bitpos */
1354 complain_overflow_dont, /* complain_on_overflow */
1355 ppc64_elf_unhandled_reloc, /* special_function */
1356 "R_PPC64_DTPMOD64", /* name */
1357 FALSE, /* partial_inplace */
1358 0, /* src_mask */
1359 ONES (64), /* dst_mask */
1360 FALSE), /* pcrel_offset */
1361
1362 /* Computes a dtv-relative displacement, the difference between the value
1363 of sym+add and the base address of the thread-local storage block that
1364 contains the definition of sym, minus 0x8000. */
1365 HOWTO (R_PPC64_DTPREL64,
1366 0, /* rightshift */
1367 4, /* size (0 = byte, 1 = short, 2 = long) */
1368 64, /* bitsize */
1369 FALSE, /* pc_relative */
1370 0, /* bitpos */
1371 complain_overflow_dont, /* complain_on_overflow */
1372 ppc64_elf_unhandled_reloc, /* special_function */
1373 "R_PPC64_DTPREL64", /* name */
1374 FALSE, /* partial_inplace */
1375 0, /* src_mask */
1376 ONES (64), /* dst_mask */
1377 FALSE), /* pcrel_offset */
1378
1379 /* A 16 bit dtprel reloc. */
1380 HOWTO (R_PPC64_DTPREL16,
1381 0, /* rightshift */
1382 1, /* size (0 = byte, 1 = short, 2 = long) */
1383 16, /* bitsize */
1384 FALSE, /* pc_relative */
1385 0, /* bitpos */
1386 complain_overflow_signed, /* complain_on_overflow */
1387 ppc64_elf_unhandled_reloc, /* special_function */
1388 "R_PPC64_DTPREL16", /* name */
1389 FALSE, /* partial_inplace */
1390 0, /* src_mask */
1391 0xffff, /* dst_mask */
1392 FALSE), /* pcrel_offset */
1393
1394 /* Like DTPREL16, but no overflow. */
1395 HOWTO (R_PPC64_DTPREL16_LO,
1396 0, /* rightshift */
1397 1, /* size (0 = byte, 1 = short, 2 = long) */
1398 16, /* bitsize */
1399 FALSE, /* pc_relative */
1400 0, /* bitpos */
1401 complain_overflow_dont, /* complain_on_overflow */
1402 ppc64_elf_unhandled_reloc, /* special_function */
1403 "R_PPC64_DTPREL16_LO", /* name */
1404 FALSE, /* partial_inplace */
1405 0, /* src_mask */
1406 0xffff, /* dst_mask */
1407 FALSE), /* pcrel_offset */
1408
1409 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1410 HOWTO (R_PPC64_DTPREL16_HI,
1411 16, /* rightshift */
1412 1, /* size (0 = byte, 1 = short, 2 = long) */
1413 16, /* bitsize */
1414 FALSE, /* pc_relative */
1415 0, /* bitpos */
f9c6b907 1416 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1417 ppc64_elf_unhandled_reloc, /* special_function */
1418 "R_PPC64_DTPREL16_HI", /* name */
1419 FALSE, /* partial_inplace */
1420 0, /* src_mask */
1421 0xffff, /* dst_mask */
1422 FALSE), /* pcrel_offset */
1423
1424 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1425 HOWTO (R_PPC64_DTPREL16_HA,
1426 16, /* rightshift */
1427 1, /* size (0 = byte, 1 = short, 2 = long) */
1428 16, /* bitsize */
1429 FALSE, /* pc_relative */
1430 0, /* bitpos */
f9c6b907 1431 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1432 ppc64_elf_unhandled_reloc, /* special_function */
1433 "R_PPC64_DTPREL16_HA", /* name */
1434 FALSE, /* partial_inplace */
1435 0, /* src_mask */
1436 0xffff, /* dst_mask */
1437 FALSE), /* pcrel_offset */
1438
1439 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1440 HOWTO (R_PPC64_DTPREL16_HIGHER,
1441 32, /* rightshift */
1442 1, /* size (0 = byte, 1 = short, 2 = long) */
1443 16, /* bitsize */
1444 FALSE, /* pc_relative */
1445 0, /* bitpos */
1446 complain_overflow_dont, /* complain_on_overflow */
1447 ppc64_elf_unhandled_reloc, /* special_function */
1448 "R_PPC64_DTPREL16_HIGHER", /* name */
1449 FALSE, /* partial_inplace */
1450 0, /* src_mask */
1451 0xffff, /* dst_mask */
1452 FALSE), /* pcrel_offset */
1453
1454 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1455 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1456 32, /* rightshift */
1457 1, /* size (0 = byte, 1 = short, 2 = long) */
1458 16, /* bitsize */
1459 FALSE, /* pc_relative */
1460 0, /* bitpos */
1461 complain_overflow_dont, /* complain_on_overflow */
1462 ppc64_elf_unhandled_reloc, /* special_function */
1463 "R_PPC64_DTPREL16_HIGHERA", /* name */
1464 FALSE, /* partial_inplace */
1465 0, /* src_mask */
1466 0xffff, /* dst_mask */
1467 FALSE), /* pcrel_offset */
1468
1469 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1470 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1471 48, /* rightshift */
1472 1, /* size (0 = byte, 1 = short, 2 = long) */
1473 16, /* bitsize */
1474 FALSE, /* pc_relative */
1475 0, /* bitpos */
1476 complain_overflow_dont, /* complain_on_overflow */
1477 ppc64_elf_unhandled_reloc, /* special_function */
1478 "R_PPC64_DTPREL16_HIGHEST", /* name */
1479 FALSE, /* partial_inplace */
1480 0, /* src_mask */
1481 0xffff, /* dst_mask */
1482 FALSE), /* pcrel_offset */
1483
1484 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1485 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1486 48, /* rightshift */
1487 1, /* size (0 = byte, 1 = short, 2 = long) */
1488 16, /* bitsize */
1489 FALSE, /* pc_relative */
1490 0, /* bitpos */
1491 complain_overflow_dont, /* complain_on_overflow */
1492 ppc64_elf_unhandled_reloc, /* special_function */
1493 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1494 FALSE, /* partial_inplace */
1495 0, /* src_mask */
1496 0xffff, /* dst_mask */
1497 FALSE), /* pcrel_offset */
1498
1499 /* Like DTPREL16, but for insns with a DS field. */
1500 HOWTO (R_PPC64_DTPREL16_DS,
1501 0, /* rightshift */
1502 1, /* size (0 = byte, 1 = short, 2 = long) */
1503 16, /* bitsize */
1504 FALSE, /* pc_relative */
1505 0, /* bitpos */
1506 complain_overflow_signed, /* complain_on_overflow */
1507 ppc64_elf_unhandled_reloc, /* special_function */
1508 "R_PPC64_DTPREL16_DS", /* name */
1509 FALSE, /* partial_inplace */
1510 0, /* src_mask */
1511 0xfffc, /* dst_mask */
1512 FALSE), /* pcrel_offset */
1513
1514 /* Like DTPREL16_DS, but no overflow. */
1515 HOWTO (R_PPC64_DTPREL16_LO_DS,
1516 0, /* rightshift */
1517 1, /* size (0 = byte, 1 = short, 2 = long) */
1518 16, /* bitsize */
1519 FALSE, /* pc_relative */
1520 0, /* bitpos */
1521 complain_overflow_dont, /* complain_on_overflow */
1522 ppc64_elf_unhandled_reloc, /* special_function */
1523 "R_PPC64_DTPREL16_LO_DS", /* name */
1524 FALSE, /* partial_inplace */
1525 0, /* src_mask */
1526 0xfffc, /* dst_mask */
1527 FALSE), /* pcrel_offset */
1528
1529 /* Computes a tp-relative displacement, the difference between the value of
1530 sym+add and the value of the thread pointer (r13). */
1531 HOWTO (R_PPC64_TPREL64,
1532 0, /* rightshift */
1533 4, /* size (0 = byte, 1 = short, 2 = long) */
1534 64, /* bitsize */
1535 FALSE, /* pc_relative */
1536 0, /* bitpos */
1537 complain_overflow_dont, /* complain_on_overflow */
1538 ppc64_elf_unhandled_reloc, /* special_function */
1539 "R_PPC64_TPREL64", /* name */
1540 FALSE, /* partial_inplace */
1541 0, /* src_mask */
1542 ONES (64), /* dst_mask */
1543 FALSE), /* pcrel_offset */
1544
1545 /* A 16 bit tprel reloc. */
1546 HOWTO (R_PPC64_TPREL16,
1547 0, /* rightshift */
1548 1, /* size (0 = byte, 1 = short, 2 = long) */
1549 16, /* bitsize */
1550 FALSE, /* pc_relative */
1551 0, /* bitpos */
1552 complain_overflow_signed, /* complain_on_overflow */
1553 ppc64_elf_unhandled_reloc, /* special_function */
1554 "R_PPC64_TPREL16", /* name */
1555 FALSE, /* partial_inplace */
1556 0, /* src_mask */
1557 0xffff, /* dst_mask */
1558 FALSE), /* pcrel_offset */
1559
1560 /* Like TPREL16, but no overflow. */
1561 HOWTO (R_PPC64_TPREL16_LO,
1562 0, /* rightshift */
1563 1, /* size (0 = byte, 1 = short, 2 = long) */
1564 16, /* bitsize */
1565 FALSE, /* pc_relative */
1566 0, /* bitpos */
1567 complain_overflow_dont, /* complain_on_overflow */
1568 ppc64_elf_unhandled_reloc, /* special_function */
1569 "R_PPC64_TPREL16_LO", /* name */
1570 FALSE, /* partial_inplace */
1571 0, /* src_mask */
1572 0xffff, /* dst_mask */
1573 FALSE), /* pcrel_offset */
1574
1575 /* Like TPREL16_LO, but next higher group of 16 bits. */
1576 HOWTO (R_PPC64_TPREL16_HI,
1577 16, /* rightshift */
1578 1, /* size (0 = byte, 1 = short, 2 = long) */
1579 16, /* bitsize */
1580 FALSE, /* pc_relative */
1581 0, /* bitpos */
f9c6b907 1582 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1583 ppc64_elf_unhandled_reloc, /* special_function */
1584 "R_PPC64_TPREL16_HI", /* name */
1585 FALSE, /* partial_inplace */
1586 0, /* src_mask */
1587 0xffff, /* dst_mask */
1588 FALSE), /* pcrel_offset */
1589
1590 /* Like TPREL16_HI, but adjust for low 16 bits. */
1591 HOWTO (R_PPC64_TPREL16_HA,
1592 16, /* rightshift */
1593 1, /* size (0 = byte, 1 = short, 2 = long) */
1594 16, /* bitsize */
1595 FALSE, /* pc_relative */
1596 0, /* bitpos */
f9c6b907 1597 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1598 ppc64_elf_unhandled_reloc, /* special_function */
1599 "R_PPC64_TPREL16_HA", /* name */
1600 FALSE, /* partial_inplace */
1601 0, /* src_mask */
1602 0xffff, /* dst_mask */
1603 FALSE), /* pcrel_offset */
1604
1605 /* Like TPREL16_HI, but next higher group of 16 bits. */
1606 HOWTO (R_PPC64_TPREL16_HIGHER,
1607 32, /* rightshift */
1608 1, /* size (0 = byte, 1 = short, 2 = long) */
1609 16, /* bitsize */
1610 FALSE, /* pc_relative */
1611 0, /* bitpos */
1612 complain_overflow_dont, /* complain_on_overflow */
1613 ppc64_elf_unhandled_reloc, /* special_function */
1614 "R_PPC64_TPREL16_HIGHER", /* name */
1615 FALSE, /* partial_inplace */
1616 0, /* src_mask */
1617 0xffff, /* dst_mask */
1618 FALSE), /* pcrel_offset */
1619
1620 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1621 HOWTO (R_PPC64_TPREL16_HIGHERA,
1622 32, /* rightshift */
1623 1, /* size (0 = byte, 1 = short, 2 = long) */
1624 16, /* bitsize */
1625 FALSE, /* pc_relative */
1626 0, /* bitpos */
1627 complain_overflow_dont, /* complain_on_overflow */
1628 ppc64_elf_unhandled_reloc, /* special_function */
1629 "R_PPC64_TPREL16_HIGHERA", /* name */
1630 FALSE, /* partial_inplace */
1631 0, /* src_mask */
1632 0xffff, /* dst_mask */
1633 FALSE), /* pcrel_offset */
1634
1635 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1636 HOWTO (R_PPC64_TPREL16_HIGHEST,
1637 48, /* rightshift */
1638 1, /* size (0 = byte, 1 = short, 2 = long) */
1639 16, /* bitsize */
1640 FALSE, /* pc_relative */
1641 0, /* bitpos */
1642 complain_overflow_dont, /* complain_on_overflow */
1643 ppc64_elf_unhandled_reloc, /* special_function */
1644 "R_PPC64_TPREL16_HIGHEST", /* name */
1645 FALSE, /* partial_inplace */
1646 0, /* src_mask */
1647 0xffff, /* dst_mask */
1648 FALSE), /* pcrel_offset */
1649
1650 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1651 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1652 48, /* rightshift */
1653 1, /* size (0 = byte, 1 = short, 2 = long) */
1654 16, /* bitsize */
1655 FALSE, /* pc_relative */
1656 0, /* bitpos */
1657 complain_overflow_dont, /* complain_on_overflow */
1658 ppc64_elf_unhandled_reloc, /* special_function */
1659 "R_PPC64_TPREL16_HIGHESTA", /* name */
1660 FALSE, /* partial_inplace */
1661 0, /* src_mask */
1662 0xffff, /* dst_mask */
1663 FALSE), /* pcrel_offset */
1664
1665 /* Like TPREL16, but for insns with a DS field. */
1666 HOWTO (R_PPC64_TPREL16_DS,
1667 0, /* rightshift */
1668 1, /* size (0 = byte, 1 = short, 2 = long) */
1669 16, /* bitsize */
1670 FALSE, /* pc_relative */
1671 0, /* bitpos */
1672 complain_overflow_signed, /* complain_on_overflow */
1673 ppc64_elf_unhandled_reloc, /* special_function */
1674 "R_PPC64_TPREL16_DS", /* name */
1675 FALSE, /* partial_inplace */
1676 0, /* src_mask */
1677 0xfffc, /* dst_mask */
1678 FALSE), /* pcrel_offset */
1679
1680 /* Like TPREL16_DS, but no overflow. */
1681 HOWTO (R_PPC64_TPREL16_LO_DS,
1682 0, /* rightshift */
1683 1, /* size (0 = byte, 1 = short, 2 = long) */
1684 16, /* bitsize */
1685 FALSE, /* pc_relative */
1686 0, /* bitpos */
1687 complain_overflow_dont, /* complain_on_overflow */
1688 ppc64_elf_unhandled_reloc, /* special_function */
1689 "R_PPC64_TPREL16_LO_DS", /* name */
1690 FALSE, /* partial_inplace */
1691 0, /* src_mask */
1692 0xfffc, /* dst_mask */
1693 FALSE), /* pcrel_offset */
1694
1695 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1696 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1697 to the first entry relative to the TOC base (r2). */
1698 HOWTO (R_PPC64_GOT_TLSGD16,
1699 0, /* rightshift */
1700 1, /* size (0 = byte, 1 = short, 2 = long) */
1701 16, /* bitsize */
1702 FALSE, /* pc_relative */
1703 0, /* bitpos */
1704 complain_overflow_signed, /* complain_on_overflow */
1705 ppc64_elf_unhandled_reloc, /* special_function */
1706 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1707 FALSE, /* partial_inplace */
5bd4f169
AM
1708 0, /* src_mask */
1709 0xffff, /* dst_mask */
b34976b6 1710 FALSE), /* pcrel_offset */
5bd4f169 1711
411e1bfb
AM
1712 /* Like GOT_TLSGD16, but no overflow. */
1713 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1714 0, /* rightshift */
1715 1, /* size (0 = byte, 1 = short, 2 = long) */
1716 16, /* bitsize */
b34976b6 1717 FALSE, /* pc_relative */
5bd4f169
AM
1718 0, /* bitpos */
1719 complain_overflow_dont, /* complain_on_overflow */
805fc799 1720 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1721 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1722 FALSE, /* partial_inplace */
5bd4f169
AM
1723 0, /* src_mask */
1724 0xffff, /* dst_mask */
b34976b6 1725 FALSE), /* pcrel_offset */
5bd4f169 1726
411e1bfb
AM
1727 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1728 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1729 16, /* rightshift */
1730 1, /* size (0 = byte, 1 = short, 2 = long) */
1731 16, /* bitsize */
b34976b6 1732 FALSE, /* pc_relative */
5bd4f169 1733 0, /* bitpos */
f9c6b907 1734 complain_overflow_signed, /* complain_on_overflow */
805fc799 1735 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1736 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1737 FALSE, /* partial_inplace */
5bd4f169
AM
1738 0, /* src_mask */
1739 0xffff, /* dst_mask */
b34976b6 1740 FALSE), /* pcrel_offset */
5bd4f169 1741
411e1bfb
AM
1742 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1743 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1744 16, /* rightshift */
1745 1, /* size (0 = byte, 1 = short, 2 = long) */
1746 16, /* bitsize */
b34976b6 1747 FALSE, /* pc_relative */
5bd4f169 1748 0, /* bitpos */
f9c6b907 1749 complain_overflow_signed, /* complain_on_overflow */
805fc799 1750 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1751 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1752 FALSE, /* partial_inplace */
5bd4f169
AM
1753 0, /* src_mask */
1754 0xffff, /* dst_mask */
b34976b6 1755 FALSE), /* pcrel_offset */
5bd4f169 1756
411e1bfb
AM
1757 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1758 with values (sym+add)@dtpmod and zero, and computes the offset to the
1759 first entry relative to the TOC base (r2). */
1760 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1761 0, /* rightshift */
1762 1, /* size (0 = byte, 1 = short, 2 = long) */
1763 16, /* bitsize */
b34976b6 1764 FALSE, /* pc_relative */
5bd4f169 1765 0, /* bitpos */
411e1bfb
AM
1766 complain_overflow_signed, /* complain_on_overflow */
1767 ppc64_elf_unhandled_reloc, /* special_function */
1768 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1769 FALSE, /* partial_inplace */
d006db6c 1770 0, /* src_mask */
411e1bfb 1771 0xffff, /* dst_mask */
b34976b6 1772 FALSE), /* pcrel_offset */
5bd4f169 1773
411e1bfb
AM
1774 /* Like GOT_TLSLD16, but no overflow. */
1775 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1776 0, /* rightshift */
1777 1, /* size (0 = byte, 1 = short, 2 = long) */
1778 16, /* bitsize */
b34976b6 1779 FALSE, /* pc_relative */
5bd4f169 1780 0, /* bitpos */
411e1bfb
AM
1781 complain_overflow_dont, /* complain_on_overflow */
1782 ppc64_elf_unhandled_reloc, /* special_function */
1783 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1784 FALSE, /* partial_inplace */
d006db6c 1785 0, /* src_mask */
411e1bfb 1786 0xffff, /* dst_mask */
b34976b6 1787 FALSE), /* pcrel_offset */
5bd4f169 1788
411e1bfb
AM
1789 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1790 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1791 16, /* rightshift */
5bd4f169
AM
1792 1, /* size (0 = byte, 1 = short, 2 = long) */
1793 16, /* bitsize */
b34976b6 1794 FALSE, /* pc_relative */
5bd4f169 1795 0, /* bitpos */
f9c6b907 1796 complain_overflow_signed, /* complain_on_overflow */
805fc799 1797 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1798 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1799 FALSE, /* partial_inplace */
d006db6c 1800 0, /* src_mask */
411e1bfb 1801 0xffff, /* dst_mask */
b34976b6 1802 FALSE), /* pcrel_offset */
5bd4f169 1803
411e1bfb
AM
1804 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1805 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1806 16, /* rightshift */
5bd4f169
AM
1807 1, /* size (0 = byte, 1 = short, 2 = long) */
1808 16, /* bitsize */
b34976b6 1809 FALSE, /* pc_relative */
5bd4f169 1810 0, /* bitpos */
f9c6b907 1811 complain_overflow_signed, /* complain_on_overflow */
805fc799 1812 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1813 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1814 FALSE, /* partial_inplace */
d006db6c 1815 0, /* src_mask */
411e1bfb 1816 0xffff, /* dst_mask */
b34976b6 1817 FALSE), /* pcrel_offset */
5bd4f169 1818
411e1bfb
AM
1819 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1820 the offset to the entry relative to the TOC base (r2). */
1821 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1822 0, /* rightshift */
1823 1, /* size (0 = byte, 1 = short, 2 = long) */
1824 16, /* bitsize */
b34976b6 1825 FALSE, /* pc_relative */
5bd4f169 1826 0, /* bitpos */
411e1bfb 1827 complain_overflow_signed, /* complain_on_overflow */
805fc799 1828 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1829 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1830 FALSE, /* partial_inplace */
d006db6c 1831 0, /* src_mask */
5bd4f169 1832 0xfffc, /* dst_mask */
b34976b6 1833 FALSE), /* pcrel_offset */
5bd4f169 1834
411e1bfb
AM
1835 /* Like GOT_DTPREL16_DS, but no overflow. */
1836 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1837 0, /* rightshift */
c061c2d8
AM
1838 1, /* size (0 = byte, 1 = short, 2 = long) */
1839 16, /* bitsize */
b34976b6 1840 FALSE, /* pc_relative */
5bd4f169 1841 0, /* bitpos */
411e1bfb
AM
1842 complain_overflow_dont, /* complain_on_overflow */
1843 ppc64_elf_unhandled_reloc, /* special_function */
1844 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1845 FALSE, /* partial_inplace */
d006db6c 1846 0, /* src_mask */
c061c2d8 1847 0xfffc, /* dst_mask */
b34976b6 1848 FALSE), /* pcrel_offset */
5bd4f169 1849
411e1bfb
AM
1850 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1851 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1852 16, /* rightshift */
5bd4f169
AM
1853 1, /* size (0 = byte, 1 = short, 2 = long) */
1854 16, /* bitsize */
b34976b6 1855 FALSE, /* pc_relative */
5bd4f169 1856 0, /* bitpos */
f9c6b907 1857 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1858 ppc64_elf_unhandled_reloc, /* special_function */
1859 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1860 FALSE, /* partial_inplace */
d006db6c 1861 0, /* src_mask */
411e1bfb 1862 0xffff, /* dst_mask */
b34976b6 1863 FALSE), /* pcrel_offset */
5bd4f169 1864
411e1bfb
AM
1865 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1866 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1867 16, /* rightshift */
1868 1, /* size (0 = byte, 1 = short, 2 = long) */
1869 16, /* bitsize */
1870 FALSE, /* pc_relative */
1871 0, /* bitpos */
f9c6b907 1872 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1873 ppc64_elf_unhandled_reloc, /* special_function */
1874 "R_PPC64_GOT_DTPREL16_HA", /* name */
1875 FALSE, /* partial_inplace */
1876 0, /* src_mask */
1877 0xffff, /* dst_mask */
1878 FALSE), /* pcrel_offset */
1879
1880 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1881 offset to the entry relative to the TOC base (r2). */
1882 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1883 0, /* rightshift */
1884 1, /* size (0 = byte, 1 = short, 2 = long) */
1885 16, /* bitsize */
b34976b6 1886 FALSE, /* pc_relative */
5bd4f169
AM
1887 0, /* bitpos */
1888 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1889 ppc64_elf_unhandled_reloc, /* special_function */
1890 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1891 FALSE, /* partial_inplace */
d006db6c 1892 0, /* src_mask */
ad8e1ba5 1893 0xfffc, /* dst_mask */
b34976b6 1894 FALSE), /* pcrel_offset */
5bd4f169 1895
411e1bfb
AM
1896 /* Like GOT_TPREL16_DS, but no overflow. */
1897 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1898 0, /* rightshift */
1899 1, /* size (0 = byte, 1 = short, 2 = long) */
1900 16, /* bitsize */
b34976b6 1901 FALSE, /* pc_relative */
5bd4f169
AM
1902 0, /* bitpos */
1903 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1904 ppc64_elf_unhandled_reloc, /* special_function */
1905 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1906 FALSE, /* partial_inplace */
d006db6c 1907 0, /* src_mask */
ad8e1ba5 1908 0xfffc, /* dst_mask */
b34976b6 1909 FALSE), /* pcrel_offset */
5bd4f169 1910
411e1bfb
AM
1911 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1912 HOWTO (R_PPC64_GOT_TPREL16_HI,
1913 16, /* rightshift */
5bd4f169
AM
1914 1, /* size (0 = byte, 1 = short, 2 = long) */
1915 16, /* bitsize */
b34976b6 1916 FALSE, /* pc_relative */
5bd4f169 1917 0, /* bitpos */
f9c6b907 1918 complain_overflow_signed, /* complain_on_overflow */
805fc799 1919 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1920 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1921 FALSE, /* partial_inplace */
d006db6c 1922 0, /* src_mask */
411e1bfb 1923 0xffff, /* dst_mask */
b34976b6 1924 FALSE), /* pcrel_offset */
5bd4f169 1925
411e1bfb
AM
1926 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1927 HOWTO (R_PPC64_GOT_TPREL16_HA,
1928 16, /* rightshift */
5bd4f169
AM
1929 1, /* size (0 = byte, 1 = short, 2 = long) */
1930 16, /* bitsize */
b34976b6 1931 FALSE, /* pc_relative */
5bd4f169 1932 0, /* bitpos */
f9c6b907 1933 complain_overflow_signed, /* complain_on_overflow */
805fc799 1934 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1935 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1936 FALSE, /* partial_inplace */
d006db6c 1937 0, /* src_mask */
411e1bfb 1938 0xffff, /* dst_mask */
b34976b6 1939 FALSE), /* pcrel_offset */
5bd4f169 1940
25f23106
AM
1941 HOWTO (R_PPC64_JMP_IREL, /* type */
1942 0, /* rightshift */
1943 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1944 0, /* bitsize */
1945 FALSE, /* pc_relative */
1946 0, /* bitpos */
1947 complain_overflow_dont, /* complain_on_overflow */
1948 ppc64_elf_unhandled_reloc, /* special_function */
1949 "R_PPC64_JMP_IREL", /* name */
1950 FALSE, /* partial_inplace */
1951 0, /* src_mask */
1952 0, /* dst_mask */
1953 FALSE), /* pcrel_offset */
1954
e054468f
AM
1955 HOWTO (R_PPC64_IRELATIVE, /* type */
1956 0, /* rightshift */
1957 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1958 64, /* bitsize */
1959 FALSE, /* pc_relative */
1960 0, /* bitpos */
1961 complain_overflow_dont, /* complain_on_overflow */
1962 bfd_elf_generic_reloc, /* special_function */
1963 "R_PPC64_IRELATIVE", /* name */
1964 FALSE, /* partial_inplace */
1965 0, /* src_mask */
1966 ONES (64), /* dst_mask */
1967 FALSE), /* pcrel_offset */
1968
25f23106
AM
1969 /* A 16 bit relative relocation. */
1970 HOWTO (R_PPC64_REL16, /* type */
1971 0, /* rightshift */
1972 1, /* size (0 = byte, 1 = short, 2 = long) */
1973 16, /* bitsize */
1974 TRUE, /* pc_relative */
1975 0, /* bitpos */
b80eed39 1976 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1977 bfd_elf_generic_reloc, /* special_function */
1978 "R_PPC64_REL16", /* name */
1979 FALSE, /* partial_inplace */
1980 0, /* src_mask */
1981 0xffff, /* dst_mask */
1982 TRUE), /* pcrel_offset */
1983
1984 /* A 16 bit relative relocation without overflow. */
1985 HOWTO (R_PPC64_REL16_LO, /* type */
1986 0, /* rightshift */
1987 1, /* size (0 = byte, 1 = short, 2 = long) */
1988 16, /* bitsize */
1989 TRUE, /* pc_relative */
1990 0, /* bitpos */
1991 complain_overflow_dont,/* complain_on_overflow */
1992 bfd_elf_generic_reloc, /* special_function */
1993 "R_PPC64_REL16_LO", /* name */
1994 FALSE, /* partial_inplace */
1995 0, /* src_mask */
1996 0xffff, /* dst_mask */
1997 TRUE), /* pcrel_offset */
1998
1999 /* The high order 16 bits of a relative address. */
2000 HOWTO (R_PPC64_REL16_HI, /* type */
2001 16, /* rightshift */
2002 1, /* size (0 = byte, 1 = short, 2 = long) */
2003 16, /* bitsize */
2004 TRUE, /* pc_relative */
2005 0, /* bitpos */
f9c6b907 2006 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2007 bfd_elf_generic_reloc, /* special_function */
2008 "R_PPC64_REL16_HI", /* name */
2009 FALSE, /* partial_inplace */
2010 0, /* src_mask */
2011 0xffff, /* dst_mask */
2012 TRUE), /* pcrel_offset */
2013
2014 /* The high order 16 bits of a relative address, plus 1 if the contents of
2015 the low 16 bits, treated as a signed number, is negative. */
2016 HOWTO (R_PPC64_REL16_HA, /* type */
2017 16, /* rightshift */
2018 1, /* size (0 = byte, 1 = short, 2 = long) */
2019 16, /* bitsize */
2020 TRUE, /* pc_relative */
2021 0, /* bitpos */
f9c6b907 2022 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2023 ppc64_elf_ha_reloc, /* special_function */
2024 "R_PPC64_REL16_HA", /* name */
2025 FALSE, /* partial_inplace */
2026 0, /* src_mask */
2027 0xffff, /* dst_mask */
2028 TRUE), /* pcrel_offset */
2029
a680de9a
PB
2030 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2031 HOWTO (R_PPC64_REL16DX_HA, /* type */
2032 16, /* rightshift */
2033 2, /* size (0 = byte, 1 = short, 2 = long) */
2034 16, /* bitsize */
2035 TRUE, /* pc_relative */
2036 0, /* bitpos */
2037 complain_overflow_signed, /* complain_on_overflow */
2038 ppc64_elf_ha_reloc, /* special_function */
2039 "R_PPC64_REL16DX_HA", /* name */
2040 FALSE, /* partial_inplace */
2041 0, /* src_mask */
2042 0x1fffc1, /* dst_mask */
2043 TRUE), /* pcrel_offset */
2044
f9c6b907
AM
2045 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2046 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2047 16, /* rightshift */
2048 1, /* size (0 = byte, 1 = short, 2 = long) */
2049 16, /* bitsize */
2050 FALSE, /* pc_relative */
2051 0, /* bitpos */
2052 complain_overflow_dont, /* complain_on_overflow */
2053 bfd_elf_generic_reloc, /* special_function */
2054 "R_PPC64_ADDR16_HIGH", /* name */
2055 FALSE, /* partial_inplace */
2056 0, /* src_mask */
2057 0xffff, /* dst_mask */
2058 FALSE), /* pcrel_offset */
2059
2060 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2061 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2062 16, /* rightshift */
2063 1, /* size (0 = byte, 1 = short, 2 = long) */
2064 16, /* bitsize */
2065 FALSE, /* pc_relative */
2066 0, /* bitpos */
2067 complain_overflow_dont, /* complain_on_overflow */
2068 ppc64_elf_ha_reloc, /* special_function */
2069 "R_PPC64_ADDR16_HIGHA", /* name */
2070 FALSE, /* partial_inplace */
2071 0, /* src_mask */
2072 0xffff, /* dst_mask */
2073 FALSE), /* pcrel_offset */
2074
2075 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2076 HOWTO (R_PPC64_DTPREL16_HIGH,
2077 16, /* rightshift */
2078 1, /* size (0 = byte, 1 = short, 2 = long) */
2079 16, /* bitsize */
2080 FALSE, /* pc_relative */
2081 0, /* bitpos */
2082 complain_overflow_dont, /* complain_on_overflow */
2083 ppc64_elf_unhandled_reloc, /* special_function */
2084 "R_PPC64_DTPREL16_HIGH", /* name */
2085 FALSE, /* partial_inplace */
2086 0, /* src_mask */
2087 0xffff, /* dst_mask */
2088 FALSE), /* pcrel_offset */
2089
2090 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2091 HOWTO (R_PPC64_DTPREL16_HIGHA,
2092 16, /* rightshift */
2093 1, /* size (0 = byte, 1 = short, 2 = long) */
2094 16, /* bitsize */
2095 FALSE, /* pc_relative */
2096 0, /* bitpos */
2097 complain_overflow_dont, /* complain_on_overflow */
2098 ppc64_elf_unhandled_reloc, /* special_function */
2099 "R_PPC64_DTPREL16_HIGHA", /* name */
2100 FALSE, /* partial_inplace */
2101 0, /* src_mask */
2102 0xffff, /* dst_mask */
2103 FALSE), /* pcrel_offset */
2104
2105 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2106 HOWTO (R_PPC64_TPREL16_HIGH,
2107 16, /* rightshift */
2108 1, /* size (0 = byte, 1 = short, 2 = long) */
2109 16, /* bitsize */
2110 FALSE, /* pc_relative */
2111 0, /* bitpos */
2112 complain_overflow_dont, /* complain_on_overflow */
2113 ppc64_elf_unhandled_reloc, /* special_function */
2114 "R_PPC64_TPREL16_HIGH", /* name */
2115 FALSE, /* partial_inplace */
2116 0, /* src_mask */
2117 0xffff, /* dst_mask */
2118 FALSE), /* pcrel_offset */
2119
2120 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2121 HOWTO (R_PPC64_TPREL16_HIGHA,
2122 16, /* rightshift */
2123 1, /* size (0 = byte, 1 = short, 2 = long) */
2124 16, /* bitsize */
2125 FALSE, /* pc_relative */
2126 0, /* bitpos */
2127 complain_overflow_dont, /* complain_on_overflow */
2128 ppc64_elf_unhandled_reloc, /* special_function */
2129 "R_PPC64_TPREL16_HIGHA", /* name */
2130 FALSE, /* partial_inplace */
2131 0, /* src_mask */
2132 0xffff, /* dst_mask */
2133 FALSE), /* pcrel_offset */
2134
006589cf
AM
2135 /* Marker reloc on ELFv2 large-model function entry. */
2136 HOWTO (R_PPC64_ENTRY,
2137 0, /* rightshift */
2138 2, /* size (0 = byte, 1 = short, 2 = long) */
2139 32, /* bitsize */
2140 FALSE, /* pc_relative */
2141 0, /* bitpos */
2142 complain_overflow_dont, /* complain_on_overflow */
2143 bfd_elf_generic_reloc, /* special_function */
2144 "R_PPC64_ENTRY", /* name */
2145 FALSE, /* partial_inplace */
2146 0, /* src_mask */
2147 0, /* dst_mask */
2148 FALSE), /* pcrel_offset */
2149
45965137
AM
2150 /* Like ADDR64, but use local entry point of function. */
2151 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2152 0, /* rightshift */
2153 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2154 64, /* bitsize */
2155 FALSE, /* pc_relative */
2156 0, /* bitpos */
2157 complain_overflow_dont, /* complain_on_overflow */
2158 bfd_elf_generic_reloc, /* special_function */
2159 "R_PPC64_ADDR64_LOCAL", /* name */
2160 FALSE, /* partial_inplace */
2161 0, /* src_mask */
2162 ONES (64), /* dst_mask */
2163 FALSE), /* pcrel_offset */
2164
5bd4f169
AM
2165 /* GNU extension to record C++ vtable hierarchy. */
2166 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2167 0, /* rightshift */
2168 0, /* size (0 = byte, 1 = short, 2 = long) */
2169 0, /* bitsize */
b34976b6 2170 FALSE, /* pc_relative */
5bd4f169
AM
2171 0, /* bitpos */
2172 complain_overflow_dont, /* complain_on_overflow */
2173 NULL, /* special_function */
2174 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2175 FALSE, /* partial_inplace */
5bd4f169
AM
2176 0, /* src_mask */
2177 0, /* dst_mask */
b34976b6 2178 FALSE), /* pcrel_offset */
5bd4f169
AM
2179
2180 /* GNU extension to record C++ vtable member usage. */
2181 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2182 0, /* rightshift */
2183 0, /* size (0 = byte, 1 = short, 2 = long) */
2184 0, /* bitsize */
b34976b6 2185 FALSE, /* pc_relative */
5bd4f169
AM
2186 0, /* bitpos */
2187 complain_overflow_dont, /* complain_on_overflow */
2188 NULL, /* special_function */
2189 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2190 FALSE, /* partial_inplace */
5bd4f169
AM
2191 0, /* src_mask */
2192 0, /* dst_mask */
b34976b6 2193 FALSE), /* pcrel_offset */
5bd4f169
AM
2194};
2195
2196\f
2197/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2198 be done. */
2199
2200static void
4ce794b7 2201ppc_howto_init (void)
5bd4f169
AM
2202{
2203 unsigned int i, type;
2204
a4b6fadd 2205 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
2206 {
2207 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 2208 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
2209 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2210 }
2211}
2212
2213static reloc_howto_type *
4ce794b7
AM
2214ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2215 bfd_reloc_code_real_type code)
5bd4f169 2216{
411e1bfb 2217 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2218
2219 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2220 /* Initialize howto table if needed. */
2221 ppc_howto_init ();
2222
4ce794b7 2223 switch (code)
5bd4f169
AM
2224 {
2225 default:
4ce794b7 2226 return NULL;
5bd4f169 2227
411e1bfb
AM
2228 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2229 break;
2230 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2231 break;
2232 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2233 break;
2234 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2235 break;
2236 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2237 break;
2238 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2239 break;
f9c6b907
AM
2240 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2241 break;
411e1bfb 2242 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2243 break;
f9c6b907
AM
2244 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2245 break;
411e1bfb 2246 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2247 break;
411e1bfb 2248 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2249 break;
411e1bfb 2250 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2251 break;
411e1bfb 2252 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2253 break;
411e1bfb 2254 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2255 break;
411e1bfb 2256 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2257 break;
411e1bfb 2258 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2259 break;
411e1bfb 2260 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2261 break;
411e1bfb 2262 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2263 break;
411e1bfb 2264 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2265 break;
411e1bfb 2266 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2267 break;
411e1bfb 2268 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2269 break;
411e1bfb 2270 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2271 break;
411e1bfb 2272 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2273 break;
411e1bfb 2274 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2275 break;
411e1bfb 2276 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2277 break;
411e1bfb 2278 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2279 break;
411e1bfb 2280 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2281 break;
411e1bfb 2282 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2283 break;
411e1bfb 2284 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2285 break;
411e1bfb 2286 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2287 break;
411e1bfb 2288 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2289 break;
411e1bfb 2290 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2291 break;
411e1bfb 2292 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2293 break;
411e1bfb 2294 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2295 break;
411e1bfb 2296 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2297 break;
411e1bfb 2298 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2299 break;
411e1bfb 2300 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2301 break;
411e1bfb 2302 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2303 break;
411e1bfb 2304 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2305 break;
411e1bfb 2306 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2307 break;
411e1bfb 2308 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2309 break;
411e1bfb 2310 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2311 break;
411e1bfb 2312 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2313 break;
411e1bfb 2314 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2315 break;
411e1bfb 2316 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2317 break;
411e1bfb 2318 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2319 break;
411e1bfb 2320 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2321 break;
411e1bfb 2322 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2323 break;
411e1bfb 2324 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2325 break;
411e1bfb 2326 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2327 break;
411e1bfb 2328 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2329 break;
411e1bfb 2330 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2331 break;
411e1bfb 2332 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2333 break;
411e1bfb 2334 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2335 break;
411e1bfb 2336 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2337 break;
411e1bfb 2338 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2339 break;
411e1bfb 2340 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2341 break;
411e1bfb 2342 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2343 break;
411e1bfb 2344 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2345 break;
411e1bfb 2346 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2347 break;
411e1bfb 2348 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2349 break;
411e1bfb 2350 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2351 break;
727fc41e
AM
2352 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2353 break;
2354 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2355 break;
411e1bfb 2356 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2357 break;
411e1bfb 2358 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2359 break;
411e1bfb 2360 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2361 break;
411e1bfb 2362 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2363 break;
f9c6b907
AM
2364 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2365 break;
411e1bfb 2366 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2367 break;
f9c6b907
AM
2368 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2369 break;
411e1bfb 2370 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2371 break;
411e1bfb
AM
2372 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2373 break;
2374 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2375 break;
2376 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2377 break;
f9c6b907
AM
2378 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2379 break;
411e1bfb
AM
2380 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2381 break;
f9c6b907
AM
2382 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2383 break;
411e1bfb
AM
2384 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2385 break;
2386 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2387 break;
2388 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2389 break;
2390 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2391 break;
2392 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2393 break;
2394 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2395 break;
2396 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2397 break;
2398 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2399 break;
2400 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2401 break;
2402 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2403 break;
2404 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2405 break;
2406 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2407 break;
2408 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2409 break;
2410 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2411 break;
2412 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2413 break;
2414 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2415 break;
2416 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2417 break;
2418 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2419 break;
2420 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2421 break;
2422 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2423 break;
2424 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2425 break;
2426 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2427 break;
2428 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2429 break;
2430 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2431 break;
2432 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2433 break;
2434 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2435 break;
2436 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2437 break;
2438 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2439 break;
2440 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2441 break;
25f23106
AM
2442 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2443 break;
2444 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2445 break;
2446 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2447 break;
2448 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2449 break;
a680de9a
PB
2450 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2451 break;
006589cf
AM
2452 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2453 break;
45965137
AM
2454 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2455 break;
411e1bfb
AM
2456 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2457 break;
2458 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2459 break;
2460 }
2461
4ce794b7 2462 return ppc64_elf_howto_table[r];
5bd4f169
AM
2463};
2464
157090f7
AM
2465static reloc_howto_type *
2466ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2467 const char *r_name)
2468{
2469 unsigned int i;
2470
a4b6fadd 2471 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
2472 if (ppc64_elf_howto_raw[i].name != NULL
2473 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2474 return &ppc64_elf_howto_raw[i];
2475
2476 return NULL;
2477}
2478
5bd4f169
AM
2479/* Set the howto pointer for a PowerPC ELF reloc. */
2480
2481static void
4ce794b7
AM
2482ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2483 Elf_Internal_Rela *dst)
5bd4f169 2484{
65f38f15
AM
2485 unsigned int type;
2486
ef60b7ff 2487 /* Initialize howto table if needed. */
5bd4f169 2488 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2489 ppc_howto_init ();
2490
65f38f15 2491 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 2492 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 2493 {
695344c0 2494 /* xgettext:c-format */
4eca0228
AM
2495 _bfd_error_handler (_("%B: invalid relocation type %d"),
2496 abfd, (int) type);
3ec2b351 2497 type = R_PPC64_NONE;
d0fb9a8d 2498 }
65f38f15 2499 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2500}
2501
04c9666a 2502/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2503
2504static bfd_reloc_status_type
4ce794b7
AM
2505ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2506 void *data, asection *input_section,
2507 bfd *output_bfd, char **error_message)
5bd4f169 2508{
a680de9a
PB
2509 enum elf_ppc64_reloc_type r_type;
2510 long insn;
2511 bfd_size_type octets;
2512 bfd_vma value;
2513
805fc799
AM
2514 /* If this is a relocatable link (output_bfd test tells us), just
2515 call the generic function. Any adjustment will be done at final
2516 link time. */
2517 if (output_bfd != NULL)
cedb70c5 2518 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2519 input_section, output_bfd, error_message);
2520
2521 /* Adjust the addend for sign extension of the low 16 bits.
2522 We won't actually be using the low 16 bits, so trashing them
2523 doesn't matter. */
2524 reloc_entry->addend += 0x8000;
a680de9a
PB
2525 r_type = reloc_entry->howto->type;
2526 if (r_type != R_PPC64_REL16DX_HA)
2527 return bfd_reloc_continue;
2528
2529 value = 0;
2530 if (!bfd_is_com_section (symbol->section))
2531 value = symbol->value;
2532 value += (reloc_entry->addend
2533 + symbol->section->output_offset
2534 + symbol->section->output_section->vma);
2535 value -= (reloc_entry->address
2536 + input_section->output_offset
2537 + input_section->output_section->vma);
2538 value = (bfd_signed_vma) value >> 16;
2539
2540 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2541 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2542 insn &= ~0x1fffc1;
2543 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2544 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2545 if (value + 0x8000 > 0xffff)
2546 return bfd_reloc_overflow;
2547 return bfd_reloc_ok;
805fc799 2548}
5bd4f169 2549
2441e016
AM
2550static bfd_reloc_status_type
2551ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2552 void *data, asection *input_section,
2553 bfd *output_bfd, char **error_message)
2554{
2555 if (output_bfd != NULL)
2556 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2557 input_section, output_bfd, error_message);
2558
699733f6
AM
2559 if (strcmp (symbol->section->name, ".opd") == 0
2560 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2561 {
2562 bfd_vma dest = opd_entry_value (symbol->section,
2563 symbol->value + reloc_entry->addend,
aef36ac1 2564 NULL, NULL, FALSE);
2441e016
AM
2565 if (dest != (bfd_vma) -1)
2566 reloc_entry->addend = dest - (symbol->value
2567 + symbol->section->output_section->vma
2568 + symbol->section->output_offset);
2569 }
810d4e75
AM
2570 else
2571 {
2572 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2573
2574 if (symbol->section->owner != abfd
9f284bf9 2575 && symbol->section->owner != NULL
810d4e75
AM
2576 && abiversion (symbol->section->owner) >= 2)
2577 {
2578 unsigned int i;
2579
2580 for (i = 0; i < symbol->section->owner->symcount; ++i)
2581 {
2582 asymbol *symdef = symbol->section->owner->outsymbols[i];
2583
2584 if (strcmp (symdef->name, symbol->name) == 0)
2585 {
2586 elfsym = (elf_symbol_type *) symdef;
2587 break;
2588 }
2589 }
2590 }
2591 reloc_entry->addend
2592 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2593 }
2441e016
AM
2594 return bfd_reloc_continue;
2595}
2596
805fc799 2597static bfd_reloc_status_type
4ce794b7
AM
2598ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2599 void *data, asection *input_section,
2600 bfd *output_bfd, char **error_message)
805fc799
AM
2601{
2602 long insn;
04c9666a 2603 enum elf_ppc64_reloc_type r_type;
805fc799 2604 bfd_size_type octets;
794e51c0
AM
2605 /* Assume 'at' branch hints. */
2606 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2607
2608 /* If this is a relocatable link (output_bfd test tells us), just
2609 call the generic function. Any adjustment will be done at final
2610 link time. */
5bd4f169 2611 if (output_bfd != NULL)
cedb70c5 2612 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2613 input_section, output_bfd, error_message);
2614
2615 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2616 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2617 insn &= ~(0x01 << 21);
4ce794b7 2618 r_type = reloc_entry->howto->type;
805fc799
AM
2619 if (r_type == R_PPC64_ADDR14_BRTAKEN
2620 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2621 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2622
794e51c0 2623 if (is_isa_v2)
5bd4f169 2624 {
805fc799
AM
2625 /* Set 'a' bit. This is 0b00010 in BO field for branch
2626 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2627 for branch on CTR insns (BO == 1a00t or 1a01t). */
2628 if ((insn & (0x14 << 21)) == (0x04 << 21))
2629 insn |= 0x02 << 21;
2630 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2631 insn |= 0x08 << 21;
2632 else
2441e016 2633 goto out;
5bd4f169 2634 }
805fc799
AM
2635 else
2636 {
2637 bfd_vma target = 0;
2638 bfd_vma from;
5bd4f169 2639
805fc799
AM
2640 if (!bfd_is_com_section (symbol->section))
2641 target = symbol->value;
2642 target += symbol->section->output_section->vma;
2643 target += symbol->section->output_offset;
2644 target += reloc_entry->addend;
5bd4f169 2645
805fc799
AM
2646 from = (reloc_entry->address
2647 + input_section->output_offset
2648 + input_section->output_section->vma);
5bd4f169 2649
805fc799
AM
2650 /* Invert 'y' bit if not the default. */
2651 if ((bfd_signed_vma) (target - from) < 0)
2652 insn ^= 0x01 << 21;
2653 }
4ce794b7 2654 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2655 out:
2656 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2657 input_section, output_bfd, error_message);
805fc799 2658}
5bd4f169 2659
805fc799 2660static bfd_reloc_status_type
4ce794b7
AM
2661ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2662 void *data, asection *input_section,
2663 bfd *output_bfd, char **error_message)
805fc799
AM
2664{
2665 /* If this is a relocatable link (output_bfd test tells us), just
2666 call the generic function. Any adjustment will be done at final
2667 link time. */
2668 if (output_bfd != NULL)
cedb70c5 2669 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2670 input_section, output_bfd, error_message);
5bd4f169 2671
805fc799
AM
2672 /* Subtract the symbol section base address. */
2673 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2674 return bfd_reloc_continue;
2675}
2676
805fc799 2677static bfd_reloc_status_type
4ce794b7
AM
2678ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2679 void *data, asection *input_section,
2680 bfd *output_bfd, char **error_message)
805fc799
AM
2681{
2682 /* If this is a relocatable link (output_bfd test tells us), just
2683 call the generic function. Any adjustment will be done at final
2684 link time. */
2685 if (output_bfd != NULL)
cedb70c5 2686 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2687 input_section, output_bfd, error_message);
2688
2689 /* Subtract the symbol section base address. */
2690 reloc_entry->addend -= symbol->section->output_section->vma;
2691
2692 /* Adjust the addend for sign extension of the low 16 bits. */
2693 reloc_entry->addend += 0x8000;
2694 return bfd_reloc_continue;
2695}
2696
2697static bfd_reloc_status_type
4ce794b7
AM
2698ppc64_elf_toc_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 bfd_vma TOCstart;
2703
2704 /* If this is a relocatable link (output_bfd test tells us), just
2705 call the generic function. Any adjustment will be done at final
2706 link time. */
2707 if (output_bfd != NULL)
cedb70c5 2708 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2709 input_section, output_bfd, error_message);
2710
2711 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2712 if (TOCstart == 0)
1c865ab2 2713 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2714
2715 /* Subtract the TOC base address. */
2716 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2717 return bfd_reloc_continue;
2718}
2719
2720static bfd_reloc_status_type
4ce794b7
AM
2721ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2722 void *data, asection *input_section,
2723 bfd *output_bfd, char **error_message)
805fc799
AM
2724{
2725 bfd_vma TOCstart;
2726
2727 /* If this is a relocatable link (output_bfd test tells us), just
2728 call the generic function. Any adjustment will be done at final
2729 link time. */
2730 if (output_bfd != NULL)
cedb70c5 2731 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2732 input_section, output_bfd, error_message);
2733
2734 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2735 if (TOCstart == 0)
1c865ab2 2736 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2737
2738 /* Subtract the TOC base address. */
2739 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2740
2741 /* Adjust the addend for sign extension of the low 16 bits. */
2742 reloc_entry->addend += 0x8000;
2743 return bfd_reloc_continue;
2744}
2745
2746static bfd_reloc_status_type
4ce794b7
AM
2747ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2748 void *data, asection *input_section,
2749 bfd *output_bfd, char **error_message)
805fc799
AM
2750{
2751 bfd_vma TOCstart;
2752 bfd_size_type octets;
2753
2754 /* If this is a relocatable link (output_bfd test tells us), just
2755 call the generic function. Any adjustment will be done at final
2756 link time. */
2757 if (output_bfd != NULL)
cedb70c5 2758 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2759 input_section, output_bfd, error_message);
2760
2761 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2762 if (TOCstart == 0)
1c865ab2 2763 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2764
2765 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2766 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2767 return bfd_reloc_ok;
2768}
2769
2770static bfd_reloc_status_type
4ce794b7
AM
2771ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2772 void *data, asection *input_section,
2773 bfd *output_bfd, char **error_message)
805fc799
AM
2774{
2775 /* If this is a relocatable link (output_bfd test tells us), just
2776 call the generic function. Any adjustment will be done at final
2777 link time. */
2778 if (output_bfd != NULL)
cedb70c5 2779 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2780 input_section, output_bfd, error_message);
2781
2782 if (error_message != NULL)
2783 {
2784 static char buf[60];
2785 sprintf (buf, "generic linker can't handle %s",
2786 reloc_entry->howto->name);
2787 *error_message = buf;
2788 }
2789 return bfd_reloc_dangerous;
2790}
2791
927be08e
AM
2792/* Track GOT entries needed for a given symbol. We might need more
2793 than one got entry per symbol. */
2794struct got_entry
2795{
2796 struct got_entry *next;
2797
2798 /* The symbol addend that we'll be placing in the GOT. */
2799 bfd_vma addend;
2800
2801 /* Unlike other ELF targets, we use separate GOT entries for the same
2802 symbol referenced from different input files. This is to support
2803 automatic multiple TOC/GOT sections, where the TOC base can vary
2804 from one input file to another. After partitioning into TOC groups
2805 we merge entries within the group.
2806
2807 Point to the BFD owning this GOT entry. */
2808 bfd *owner;
2809
2810 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2811 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2812 unsigned char tls_type;
927be08e
AM
2813
2814 /* Non-zero if got.ent points to real entry. */
f961d9dd 2815 unsigned char is_indirect;
927be08e
AM
2816
2817 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2818 union
2819 {
2820 bfd_signed_vma refcount;
2821 bfd_vma offset;
2822 struct got_entry *ent;
2823 } got;
2824};
2825
2826/* The same for PLT. */
2827struct plt_entry
2828{
2829 struct plt_entry *next;
2830
2831 bfd_vma addend;
2832
2833 union
2834 {
2835 bfd_signed_vma refcount;
2836 bfd_vma offset;
2837 } plt;
2838};
2839
e717da7e
AM
2840struct ppc64_elf_obj_tdata
2841{
2842 struct elf_obj_tdata elf;
2843
2844 /* Shortcuts to dynamic linker sections. */
2845 asection *got;
2846 asection *relgot;
2847
b3fac117
AM
2848 /* Used during garbage collection. We attach global symbols defined
2849 on removed .opd entries to this section so that the sym is removed. */
2850 asection *deleted_section;
81688140 2851
927be08e 2852 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2853 sections means we potentially need one of these for each input bfd. */
927be08e 2854 struct got_entry tlsld_got;
8860955f 2855
729eabd5
AM
2856 union {
2857 /* A copy of relocs before they are modified for --emit-relocs. */
2858 Elf_Internal_Rela *relocs;
2859
2860 /* Section contents. */
2861 bfd_byte *contents;
2862 } opd;
d77c8a4b
AM
2863
2864 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2865 the reloc to be in the range -32768 to 32767. */
98528052
AM
2866 unsigned int has_small_toc_reloc : 1;
2867
560c8763
AM
2868 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2869 instruction not one we handle. */
2870 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2871};
2872
2873#define ppc64_elf_tdata(bfd) \
2874 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2875
2876#define ppc64_tlsld_got(bfd) \
2877 (&ppc64_elf_tdata (bfd)->tlsld_got)
2878
0c8d6e5c
AM
2879#define is_ppc64_elf(bfd) \
2880 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2881 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2882
e717da7e
AM
2883/* Override the generic function because we store some extras. */
2884
2885static bfd_boolean
2886ppc64_elf_mkobject (bfd *abfd)
2887{
0ffa91dd 2888 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2889 PPC64_ELF_DATA);
e717da7e
AM
2890}
2891
feee612b 2892/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 2893 default is 32 bit. Also select arch based on apuinfo. */
feee612b 2894
b34976b6 2895static bfd_boolean
4ce794b7 2896ppc64_elf_object_p (bfd *abfd)
feee612b 2897{
14b57c7c
AM
2898 if (!abfd->arch_info->the_default)
2899 return TRUE;
2900
2901 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
2902 {
2903 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2904
2905 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2906 {
2907 /* Relies on arch after 32 bit default being 64 bit default. */
2908 abfd->arch_info = abfd->arch_info->next;
2909 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2910 }
2911 }
14b57c7c 2912 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
2913}
2914
d37c89e5
AM
2915/* Support for core dump NOTE sections. */
2916
2917static bfd_boolean
2918ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2919{
eea6121a 2920 size_t offset, size;
d37c89e5
AM
2921
2922 if (note->descsz != 504)
2923 return FALSE;
2924
2925 /* pr_cursig */
228e534f 2926 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2927
2928 /* pr_pid */
228e534f 2929 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2930
2931 /* pr_reg */
2932 offset = 112;
eea6121a 2933 size = 384;
d37c89e5
AM
2934
2935 /* Make a ".reg/999" section. */
2936 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2937 size, note->descpos + offset);
d37c89e5
AM
2938}
2939
2940static bfd_boolean
2941ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2942{
2943 if (note->descsz != 136)
2944 return FALSE;
2945
228e534f 2946 elf_tdata (abfd)->core->pid
bc989cdc 2947 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2948 elf_tdata (abfd)->core->program
d37c89e5 2949 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2950 elf_tdata (abfd)->core->command
d37c89e5
AM
2951 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2952
2953 return TRUE;
2954}
2955
183e98be
AM
2956static char *
2957ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2958 ...)
2959{
2960 switch (note_type)
2961 {
2962 default:
2963 return NULL;
2964
2965 case NT_PRPSINFO:
2966 {
2967 char data[136];
2968 va_list ap;
2969
2970 va_start (ap, note_type);
75cd47ed 2971 memset (data, 0, sizeof (data));
183e98be
AM
2972 strncpy (data + 40, va_arg (ap, const char *), 16);
2973 strncpy (data + 56, va_arg (ap, const char *), 80);
2974 va_end (ap);
2975 return elfcore_write_note (abfd, buf, bufsiz,
2976 "CORE", note_type, data, sizeof (data));
2977 }
2978
2979 case NT_PRSTATUS:
2980 {
2981 char data[504];
2982 va_list ap;
2983 long pid;
2984 int cursig;
2985 const void *greg;
2986
2987 va_start (ap, note_type);
2988 memset (data, 0, 112);
2989 pid = va_arg (ap, long);
2990 bfd_put_32 (abfd, pid, data + 32);
2991 cursig = va_arg (ap, int);
2992 bfd_put_16 (abfd, cursig, data + 12);
2993 greg = va_arg (ap, const void *);
2994 memcpy (data + 112, greg, 384);
2995 memset (data + 496, 0, 8);
2996 va_end (ap);
2997 return elfcore_write_note (abfd, buf, bufsiz,
2998 "CORE", note_type, data, sizeof (data));
2999 }
3000 }
3001}
3002
5d35169e
AM
3003/* Add extra PPC sections. */
3004
b35d266b 3005static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 3006{
0112cd26
NC
3007 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3008 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3009 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3010 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3011 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3012 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3013 { NULL, 0, 0, 0, 0 }
5d35169e
AM
3014};
3015
7c8fe5c4
AM
3016enum _ppc64_sec_type {
3017 sec_normal = 0,
3018 sec_opd = 1,
3019 sec_toc = 2
3020};
3021
f0abc2a1
AM
3022struct _ppc64_elf_section_data
3023{
3024 struct bfd_elf_section_data elf;
411e1bfb 3025
f0abc2a1
AM
3026 union
3027 {
51aecdc5
AM
3028 /* An array with one entry for each opd function descriptor,
3029 and some spares since opd entries may be either 16 or 24 bytes. */
3030#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
3031 struct _opd_sec_data
3032 {
3033 /* Points to the function code section for local opd entries. */
3034 asection **func_sec;
3035
3036 /* After editing .opd, adjust references to opd local syms. */
3037 long *adjust;
3038 } opd;
7c8fe5c4 3039
3a71aa26
AM
3040 /* An array for toc sections, indexed by offset/8. */
3041 struct _toc_sec_data
3042 {
3043 /* Specifies the relocation symbol index used at a given toc offset. */
3044 unsigned *symndx;
3045
3046 /* And the relocation addend. */
3047 bfd_vma *add;
3048 } toc;
7c8fe5c4
AM
3049 } u;
3050
3051 enum _ppc64_sec_type sec_type:2;
411e1bfb 3052
7c8fe5c4
AM
3053 /* Flag set when small branches are detected. Used to
3054 select suitable defaults for the stub group size. */
3055 unsigned int has_14bit_branch:1;
f0abc2a1
AM
3056};
3057
3058#define ppc64_elf_section_data(sec) \
411e1bfb 3059 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
3060
3061static bfd_boolean
4ce794b7 3062ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 3063{
f592407e
AM
3064 if (!sec->used_by_bfd)
3065 {
3066 struct _ppc64_elf_section_data *sdata;
3067 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 3068
f592407e
AM
3069 sdata = bfd_zalloc (abfd, amt);
3070 if (sdata == NULL)
3071 return FALSE;
3072 sec->used_by_bfd = sdata;
3073 }
f0abc2a1
AM
3074
3075 return _bfd_elf_new_section_hook (abfd, sec);
3076}
4025353c 3077
74f0fb50 3078static struct _opd_sec_data *
4025353c
AM
3079get_opd_info (asection * sec)
3080{
3081 if (sec != NULL
3082 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3083 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3084 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3085 return NULL;
3086}
90e3cdf2
JJ
3087\f
3088/* Parameters for the qsort hook. */
90e3cdf2 3089static bfd_boolean synthetic_relocatable;
cd285db5 3090static asection *synthetic_opd;
90e3cdf2 3091
699733f6 3092/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3093
3094static int
3095compare_symbols (const void *ap, const void *bp)
3096{
3097 const asymbol *a = * (const asymbol **) ap;
3098 const asymbol *b = * (const asymbol **) bp;
3099
699733f6
AM
3100 /* Section symbols first. */
3101 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3102 return -1;
699733f6 3103 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3104 return 1;
3105
699733f6 3106 /* then .opd symbols. */
cd285db5
AM
3107 if (synthetic_opd != NULL)
3108 {
3109 if (strcmp (a->section->name, ".opd") == 0
3110 && strcmp (b->section->name, ".opd") != 0)
3111 return -1;
3112 if (strcmp (a->section->name, ".opd") != 0
3113 && strcmp (b->section->name, ".opd") == 0)
3114 return 1;
3115 }
90e3cdf2 3116
699733f6 3117 /* then other code symbols. */
90e3cdf2
JJ
3118 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3119 == (SEC_CODE | SEC_ALLOC)
3120 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3121 != (SEC_CODE | SEC_ALLOC))
3122 return -1;
3123
3124 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3125 != (SEC_CODE | SEC_ALLOC)
3126 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3127 == (SEC_CODE | SEC_ALLOC))
3128 return 1;
3129
3130 if (synthetic_relocatable)
3131 {
3132 if (a->section->id < b->section->id)
3133 return -1;
3134
3135 if (a->section->id > b->section->id)
3136 return 1;
3137 }
3138
3139 if (a->value + a->section->vma < b->value + b->section->vma)
3140 return -1;
3141
3142 if (a->value + a->section->vma > b->value + b->section->vma)
3143 return 1;
3144
4d35a0aa
AM
3145 /* For syms with the same value, prefer strong dynamic global function
3146 syms over other syms. */
3147 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3148 return -1;
3149
3150 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3151 return 1;
3152
3153 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3154 return -1;
3155
3156 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3157 return 1;
3158
3159 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3160 return -1;
3161
3162 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3163 return 1;
3164
3165 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3166 return -1;
3167
3168 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3169 return 1;
3170
90e3cdf2
JJ
3171 return 0;
3172}
3173
699733f6 3174/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3175
699733f6 3176static asymbol *
7292b3ac 3177sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3178{
699733f6 3179 long mid;
90e3cdf2 3180
7292b3ac 3181 if (id == (unsigned) -1)
699733f6
AM
3182 {
3183 while (lo < hi)
3184 {
3185 mid = (lo + hi) >> 1;
3186 if (syms[mid]->value + syms[mid]->section->vma < value)
3187 lo = mid + 1;
3188 else if (syms[mid]->value + syms[mid]->section->vma > value)
3189 hi = mid;
3190 else
3191 return syms[mid];
3192 }
3193 }
3194 else
3195 {
3196 while (lo < hi)
3197 {
3198 mid = (lo + hi) >> 1;
3199 if (syms[mid]->section->id < id)
3200 lo = mid + 1;
3201 else if (syms[mid]->section->id > id)
3202 hi = mid;
3203 else if (syms[mid]->value < value)
3204 lo = mid + 1;
3205 else if (syms[mid]->value > value)
3206 hi = mid;
3207 else
3208 return syms[mid];
3209 }
3210 }
3211 return NULL;
90e3cdf2
JJ
3212}
3213
468392fb
AM
3214static bfd_boolean
3215section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3216{
3217 bfd_vma vma = *(bfd_vma *) ptr;
3218 return ((section->flags & SEC_ALLOC) != 0
3219 && section->vma <= vma
3220 && vma < section->vma + section->size);
3221}
3222
699733f6 3223/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3224 entry syms. Also generate @plt symbols for the glink branch table.
3225 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3226
3227static long
a7535cf3
AM
3228ppc64_elf_get_synthetic_symtab (bfd *abfd,
3229 long static_count, asymbol **static_syms,
3230 long dyn_count, asymbol **dyn_syms,
c9727e01 3231 asymbol **ret)
90e3cdf2
JJ
3232{
3233 asymbol *s;
699733f6
AM
3234 long i;
3235 long count;
90e3cdf2 3236 char *names;
a7535cf3 3237 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3238 asection *opd = NULL;
90e3cdf2 3239 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3240 asymbol **syms;
ee67d69a 3241 int abi = abiversion (abfd);
90e3cdf2
JJ
3242
3243 *ret = NULL;
3244
ee67d69a
AM
3245 if (abi < 2)
3246 {
3247 opd = bfd_get_section_by_name (abfd, ".opd");
3248 if (opd == NULL && abi == 1)
3249 return 0;
3250 }
90e3cdf2 3251
a7535cf3 3252 symcount = static_count;
c9727e01 3253 if (!relocatable)
a7535cf3 3254 symcount += dyn_count;
90e3cdf2 3255 if (symcount == 0)
c9727e01 3256 return 0;
90e3cdf2 3257
a7535cf3
AM
3258 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3259 if (syms == NULL)
7356fed5 3260 return -1;
a7535cf3
AM
3261
3262 if (!relocatable && static_count != 0 && dyn_count != 0)
3263 {
3264 /* Use both symbol tables. */
3265 memcpy (syms, static_syms, static_count * sizeof (*syms));
3266 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3267 }
3268 else if (!relocatable && static_count == 0)
3269 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3270 else
3271 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3272
90e3cdf2 3273 synthetic_relocatable = relocatable;
cd285db5 3274 synthetic_opd = opd;
595da8c5 3275 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3276
c9727e01
AM
3277 if (!relocatable && symcount > 1)
3278 {
3279 long j;
3280 /* Trim duplicate syms, since we may have merged the normal and
3281 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3282 different values, so trim any with the same value. */
c9727e01
AM
3283 for (i = 1, j = 1; i < symcount; ++i)
3284 if (syms[i - 1]->value + syms[i - 1]->section->vma
3285 != syms[i]->value + syms[i]->section->vma)
3286 syms[j++] = syms[i];
3287 symcount = j;
3288 }
3289
699733f6 3290 i = 0;
cd285db5
AM
3291 /* Note that here and in compare_symbols we can't compare opd and
3292 sym->section directly. With separate debug info files, the
3293 symbols will be extracted from the debug file while abfd passed
3294 to this function is the real binary. */
3295 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3296 ++i;
3297 codesecsym = i;
90e3cdf2 3298
699733f6
AM
3299 for (; i < symcount; ++i)
3300 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3301 != (SEC_CODE | SEC_ALLOC))
3302 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3303 break;
3304 codesecsymend = i;
90e3cdf2 3305
699733f6
AM
3306 for (; i < symcount; ++i)
3307 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3308 break;
3309 secsymend = i;
90e3cdf2 3310
cd285db5
AM
3311 if (opd != NULL)
3312 for (; i < symcount; ++i)
3313 if (strcmp (syms[i]->section->name, ".opd") != 0)
3314 break;
699733f6 3315 opdsymend = i;
90e3cdf2 3316
699733f6
AM
3317 for (; i < symcount; ++i)
3318 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3319 != (SEC_CODE | SEC_ALLOC))
3320 break;
3321 symcount = i;
3322
c9727e01 3323 count = 0;
90e3cdf2 3324
699733f6 3325 if (relocatable)
90e3cdf2 3326 {
699733f6
AM
3327 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3328 arelent *r;
3329 size_t size;
3330 long relcount;
90e3cdf2 3331
468392fb
AM
3332 if (opdsymend == secsymend)
3333 goto done;
3334
699733f6 3335 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3336 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3337 if (relcount == 0)
c9727e01 3338 goto done;
90e3cdf2 3339
7356fed5
AM
3340 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3341 {
3342 count = -1;
3343 goto done;
3344 }
3345
699733f6 3346 size = 0;
595da8c5 3347 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3348 {
3349 asymbol *sym;
90e3cdf2 3350
595da8c5 3351 while (r < opd->relocation + relcount
699733f6
AM
3352 && r->address < syms[i]->value + opd->vma)
3353 ++r;
90e3cdf2 3354
595da8c5 3355 if (r == opd->relocation + relcount)
699733f6 3356 break;
90e3cdf2 3357
699733f6
AM
3358 if (r->address != syms[i]->value + opd->vma)
3359 continue;
90e3cdf2 3360
699733f6
AM
3361 if (r->howto->type != R_PPC64_ADDR64)
3362 continue;
90e3cdf2 3363
699733f6
AM
3364 sym = *r->sym_ptr_ptr;
3365 if (!sym_exists_at (syms, opdsymend, symcount,
3366 sym->section->id, sym->value + r->addend))
3367 {
3368 ++count;
3369 size += sizeof (asymbol);
3370 size += strlen (syms[i]->name) + 2;
3371 }
3372 }
90e3cdf2 3373
c4b0b099
AM
3374 if (size == 0)
3375 goto done;
699733f6
AM
3376 s = *ret = bfd_malloc (size);
3377 if (s == NULL)
3378 {
7356fed5 3379 count = -1;
c9727e01 3380 goto done;
699733f6 3381 }
90e3cdf2 3382
699733f6 3383 names = (char *) (s + count);
90e3cdf2 3384
595da8c5 3385 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3386 {
699733f6 3387 asymbol *sym;
90e3cdf2 3388
595da8c5 3389 while (r < opd->relocation + relcount
699733f6
AM
3390 && r->address < syms[i]->value + opd->vma)
3391 ++r;
90e3cdf2 3392
595da8c5 3393 if (r == opd->relocation + relcount)
699733f6
AM
3394 break;
3395
3396 if (r->address != syms[i]->value + opd->vma)
3397 continue;
3398
3399 if (r->howto->type != R_PPC64_ADDR64)
3400 continue;
90e3cdf2 3401
699733f6
AM
3402 sym = *r->sym_ptr_ptr;
3403 if (!sym_exists_at (syms, opdsymend, symcount,
3404 sym->section->id, sym->value + r->addend))
3405 {
3406 size_t len;
3407
3408 *s = *syms[i];
6ba2a415 3409 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3410 s->section = sym->section;
3411 s->value = sym->value + r->addend;
3412 s->name = names;
3413 *names++ = '.';
3414 len = strlen (syms[i]->name);
3415 memcpy (names, syms[i]->name, len + 1);
3416 names += len + 1;
6f610d07
UW
3417 /* Have udata.p point back to the original symbol this
3418 synthetic symbol was derived from. */
3419 s->udata.p = syms[i];
699733f6
AM
3420 s++;
3421 }
3422 }
3423 }
3424 else
90e3cdf2 3425 {
468392fb 3426 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3427 bfd_byte *contents = NULL;
699733f6 3428 size_t size;
468392fb
AM
3429 long plt_count = 0;
3430 bfd_vma glink_vma = 0, resolv_vma = 0;
3431 asection *dynamic, *glink = NULL, *relplt = NULL;
3432 arelent *p;
90e3cdf2 3433
ee67d69a 3434 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3435 {
c4b0b099
AM
3436 free_contents_and_exit_err:
3437 count = -1;
ee67d69a 3438 free_contents_and_exit:
699733f6 3439 if (contents)
ee67d69a 3440 free (contents);
c9727e01 3441 goto done;
699733f6 3442 }
90e3cdf2 3443
699733f6
AM
3444 size = 0;
3445 for (i = secsymend; i < opdsymend; ++i)
3446 {
3447 bfd_vma ent;
90e3cdf2 3448
5ef11c02
AM
3449 /* Ignore bogus symbols. */
3450 if (syms[i]->value > opd->size - 8)
3451 continue;
3452
699733f6
AM
3453 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3454 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3455 {
3456 ++count;
3457 size += sizeof (asymbol);
3458 size += strlen (syms[i]->name) + 2;
3459 }
3460 }
90e3cdf2 3461
468392fb 3462 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3463 if (dyn_count != 0
3464 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3465 {
3466 bfd_byte *dynbuf, *extdyn, *extdynend;
3467 size_t extdynsize;
3468 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3469
3470 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3471 goto free_contents_and_exit_err;
468392fb
AM
3472
3473 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3474 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3475
3476 extdyn = dynbuf;
3477 extdynend = extdyn + dynamic->size;
3478 for (; extdyn < extdynend; extdyn += extdynsize)
3479 {
3480 Elf_Internal_Dyn dyn;
3481 (*swap_dyn_in) (abfd, extdyn, &dyn);
3482
3483 if (dyn.d_tag == DT_NULL)
3484 break;
3485
3486 if (dyn.d_tag == DT_PPC64_GLINK)
3487 {
b9e5796b
AM
3488 /* The first glink stub starts at offset 32; see
3489 comment in ppc64_elf_finish_dynamic_sections. */
3490 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
468392fb
AM
3491 /* The .glink section usually does not survive the final
3492 link; search for the section (usually .text) where the
3493 glink stubs now reside. */
3494 glink = bfd_sections_find_if (abfd, section_covers_vma,
3495 &glink_vma);
3496 break;
3497 }
3498 }
3499
3500 free (dynbuf);
3501 }
3502
3503 if (glink != NULL)
3504 {
3505 /* Determine __glink trampoline by reading the relative branch
3506 from the first glink stub. */
3507 bfd_byte buf[4];
b9e5796b
AM
3508 unsigned int off = 0;
3509
3510 while (bfd_get_section_contents (abfd, glink, buf,
3511 glink_vma + off - glink->vma, 4))
468392fb
AM
3512 {
3513 unsigned int insn = bfd_get_32 (abfd, buf);
3514 insn ^= B_DOT;
3515 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3516 {
3517 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3518 break;
3519 }
3520 off += 4;
3521 if (off > 4)
3522 break;
468392fb
AM
3523 }
3524
3525 if (resolv_vma)
3526 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3527
066ee829
AM
3528 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3529 if (relplt != NULL)
3530 {
3531 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3532 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3533 goto free_contents_and_exit_err;
68ffbac6 3534
066ee829
AM
3535 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3536 size += plt_count * sizeof (asymbol);
468392fb 3537
066ee829
AM
3538 p = relplt->relocation;
3539 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3540 {
3541 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3542 if (p->addend != 0)
3543 size += sizeof ("+0x") - 1 + 16;
3544 }
066ee829 3545 }
468392fb
AM
3546 }
3547
c4b0b099
AM
3548 if (size == 0)
3549 goto free_contents_and_exit;
699733f6
AM
3550 s = *ret = bfd_malloc (size);
3551 if (s == NULL)
c4b0b099 3552 goto free_contents_and_exit_err;
90e3cdf2 3553
468392fb 3554 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3555
699733f6 3556 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3557 {
699733f6 3558 bfd_vma ent;
90e3cdf2 3559
5ef11c02
AM
3560 if (syms[i]->value > opd->size - 8)
3561 continue;
3562
699733f6
AM
3563 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3564 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3565 {
c9727e01 3566 long lo, hi;
699733f6 3567 size_t len;
c9727e01 3568 asection *sec = abfd->sections;
90e3cdf2 3569
699733f6
AM
3570 *s = *syms[i];
3571 lo = codesecsym;
3572 hi = codesecsymend;
3573 while (lo < hi)
3574 {
c9727e01 3575 long mid = (lo + hi) >> 1;
699733f6
AM
3576 if (syms[mid]->section->vma < ent)
3577 lo = mid + 1;
3578 else if (syms[mid]->section->vma > ent)
3579 hi = mid;
3580 else
c9727e01
AM
3581 {
3582 sec = syms[mid]->section;
3583 break;
3584 }
699733f6
AM
3585 }
3586
c9727e01 3587 if (lo >= hi && lo > codesecsym)
699733f6 3588 sec = syms[lo - 1]->section;
699733f6
AM
3589
3590 for (; sec != NULL; sec = sec->next)
3591 {
3592 if (sec->vma > ent)
3593 break;
63524580
JK
3594 /* SEC_LOAD may not be set if SEC is from a separate debug
3595 info file. */
3596 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3597 break;
3598 if ((sec->flags & SEC_CODE) != 0)
3599 s->section = sec;
3600 }
6ba2a415 3601 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3602 s->value = ent - s->section->vma;
3603 s->name = names;
3604 *names++ = '.';
3605 len = strlen (syms[i]->name);
3606 memcpy (names, syms[i]->name, len + 1);
3607 names += len + 1;
6f610d07
UW
3608 /* Have udata.p point back to the original symbol this
3609 synthetic symbol was derived from. */
3610 s->udata.p = syms[i];
699733f6 3611 s++;
90e3cdf2 3612 }
90e3cdf2 3613 }
699733f6 3614 free (contents);
468392fb
AM
3615
3616 if (glink != NULL && relplt != NULL)
3617 {
3618 if (resolv_vma)
3619 {
3620 /* Add a symbol for the main glink trampoline. */
86a4952b 3621 memset (s, 0, sizeof *s);
468392fb 3622 s->the_bfd = abfd;
6ba2a415 3623 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3624 s->section = glink;
3625 s->value = resolv_vma - glink->vma;
3626 s->name = names;
3627 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3628 names += sizeof ("__glink_PLTresolve");
3629 s++;
3630 count++;
3631 }
3632
3633 /* FIXME: It would be very much nicer to put sym@plt on the
3634 stub rather than on the glink branch table entry. The
3635 objdump disassembler would then use a sensible symbol
3636 name on plt calls. The difficulty in doing so is
3637 a) finding the stubs, and,
3638 b) matching stubs against plt entries, and,
3639 c) there can be multiple stubs for a given plt entry.
3640
3641 Solving (a) could be done by code scanning, but older
3642 ppc64 binaries used different stubs to current code.
3643 (b) is the tricky one since you need to known the toc
3644 pointer for at least one function that uses a pic stub to
3645 be able to calculate the plt address referenced.
3646 (c) means gdb would need to set multiple breakpoints (or
3647 find the glink branch itself) when setting breakpoints
3648 for pending shared library loads. */
3649 p = relplt->relocation;
3650 for (i = 0; i < plt_count; i++, p++)
3651 {
3652 size_t len;
3653
3654 *s = **p->sym_ptr_ptr;
3655 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3656 we are defining a symbol, ensure one of them is set. */
3657 if ((s->flags & BSF_LOCAL) == 0)
3658 s->flags |= BSF_GLOBAL;
6ba2a415 3659 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3660 s->section = glink;
3661 s->value = glink_vma - glink->vma;
3662 s->name = names;
3663 s->udata.p = NULL;
3664 len = strlen ((*p->sym_ptr_ptr)->name);
3665 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3666 names += len;
e054468f
AM
3667 if (p->addend != 0)
3668 {
3669 memcpy (names, "+0x", sizeof ("+0x") - 1);
3670 names += sizeof ("+0x") - 1;
3671 bfd_sprintf_vma (abfd, names, p->addend);
3672 names += strlen (names);
3673 }
468392fb
AM
3674 memcpy (names, "@plt", sizeof ("@plt"));
3675 names += sizeof ("@plt");
3676 s++;
b9e5796b
AM
3677 if (abi < 2)
3678 {
3679 glink_vma += 8;
3680 if (i >= 0x8000)
3681 glink_vma += 4;
3682 }
3683 else
468392fb
AM
3684 glink_vma += 4;
3685 }
3686 count += plt_count;
3687 }
90e3cdf2
JJ
3688 }
3689
c9727e01 3690 done:
a7535cf3 3691 free (syms);
90e3cdf2
JJ
3692 return count;
3693}
5bd4f169 3694\f
65f38f15
AM
3695/* The following functions are specific to the ELF linker, while
3696 functions above are used generally. Those named ppc64_elf_* are
3697 called by the main ELF linker code. They appear in this file more
3698 or less in the order in which they are called. eg.
3699 ppc64_elf_check_relocs is called early in the link process,
3700 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3701 called.
3702
3703 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3704 functions have both a function code symbol and a function descriptor
3705 symbol. A call to foo in a relocatable object file looks like:
3706
3707 . .text
3708 . x:
3709 . bl .foo
3710 . nop
3711
3712 The function definition in another object file might be:
3713
3714 . .section .opd
3715 . foo: .quad .foo
3716 . .quad .TOC.@tocbase
3717 . .quad 0
3718 .
3719 . .text
3720 . .foo: blr
3721
3722 When the linker resolves the call during a static link, the branch
3723 unsurprisingly just goes to .foo and the .opd information is unused.
3724 If the function definition is in a shared library, things are a little
3725 different: The call goes via a plt call stub, the opd information gets
3726 copied to the plt, and the linker patches the nop.
3727
3728 . x:
3729 . bl .foo_stub
3730 . ld 2,40(1)
3731 .
3732 .
3733 . .foo_stub:
71a39c98
AM
3734 . std 2,40(1) # in practice, the call stub
3735 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3736 . addi 11,11,Lfoo@toc@l # this is the general idea
3737 . ld 12,0(11)
3738 . ld 2,8(11)
3739 . mtctr 12
3740 . ld 11,16(11)
e86ce104
AM
3741 . bctr
3742 .
3743 . .section .plt
3744 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3745
3746 The "reloc ()" notation is supposed to indicate that the linker emits
3747 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3748 copying.
3749
3750 What are the difficulties here? Well, firstly, the relocations
3751 examined by the linker in check_relocs are against the function code
3752 sym .foo, while the dynamic relocation in the plt is emitted against
3753 the function descriptor symbol, foo. Somewhere along the line, we need
3754 to carefully copy dynamic link information from one symbol to the other.
3755 Secondly, the generic part of the elf linker will make .foo a dynamic
3756 symbol as is normal for most other backends. We need foo dynamic
3757 instead, at least for an application final link. However, when
3758 creating a shared library containing foo, we need to have both symbols
3759 dynamic so that references to .foo are satisfied during the early
3760 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3761 definition from some other object, eg. a static library.
3762
3763 Update: As of August 2004, we support a new convention. Function
3764 calls may use the function descriptor symbol, ie. "bl foo". This
3765 behaves exactly as "bl .foo". */
65f38f15 3766
1d483afe 3767/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3768 selects those that must be copied when linking a shared library,
3769 even when the symbol is local. */
65f38f15 3770
1d483afe
AM
3771static int
3772must_be_dyn_reloc (struct bfd_link_info *info,
3773 enum elf_ppc64_reloc_type r_type)
3774{
3775 switch (r_type)
3776 {
3777 default:
3778 return 1;
3779
3780 case R_PPC64_REL32:
3781 case R_PPC64_REL64:
3782 case R_PPC64_REL30:
3783 return 0;
3784
3785 case R_PPC64_TPREL16:
3786 case R_PPC64_TPREL16_LO:
3787 case R_PPC64_TPREL16_HI:
3788 case R_PPC64_TPREL16_HA:
3789 case R_PPC64_TPREL16_DS:
3790 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3791 case R_PPC64_TPREL16_HIGH:
3792 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3793 case R_PPC64_TPREL16_HIGHER:
3794 case R_PPC64_TPREL16_HIGHERA:
3795 case R_PPC64_TPREL16_HIGHEST:
3796 case R_PPC64_TPREL16_HIGHESTA:
3797 case R_PPC64_TPREL64:
0e1862bb 3798 return !bfd_link_executable (info);
1d483afe
AM
3799 }
3800}
65f38f15 3801
f4656909
AM
3802/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3803 copying dynamic variables from a shared lib into an app's dynbss
3804 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3805 shared lib. With code that gcc generates, it's vital that this be
3806 enabled; In the PowerPC64 ABI, the address of a function is actually
3807 the address of a function descriptor, which resides in the .opd
3808 section. gcc uses the descriptor directly rather than going via the
3809 GOT as some other ABI's do, which means that initialized function
3810 pointers must reference the descriptor. Thus, a function pointer
3811 initialized to the address of a function in a shared library will
3812 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3813 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3814 presents a problem as a plt entry for that function is also
3815 initialized from the function descriptor symbol and the copy reloc
3816 may not be initialized first. */
a23b6845 3817#define ELIMINATE_COPY_RELOCS 1
f4656909 3818
721956f4 3819/* Section name for stubs is the associated section name plus this
29942be8
NC
3820 string. */
3821#define STUB_SUFFIX ".stub"
721956f4
AM
3822
3823/* Linker stubs.
3824 ppc_stub_long_branch:
3825 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3826 destination, but a 24 bit branch in a stub section will reach.
3827 . b dest
3828
3829 ppc_stub_plt_branch:
3830 Similar to the above, but a 24 bit branch in the stub section won't
3831 reach its destination.
71a39c98
AM
3832 . addis %r11,%r2,xxx@toc@ha
3833 . ld %r12,xxx@toc@l(%r11)
3834 . mtctr %r12
721956f4
AM
3835 . bctr
3836
3837 ppc_stub_plt_call:
2c66dc6c
AM
3838 Used to call a function in a shared library. If it so happens that
3839 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3840 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3841 . std %r2,40(%r1)
71a39c98
AM
3842 . addis %r11,%r2,xxx@toc@ha
3843 . ld %r12,xxx+0@toc@l(%r11)
3844 . mtctr %r12
3845 . ld %r2,xxx+8@toc@l(%r11)
3846 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3847 . bctr
ad8e1ba5
AM
3848
3849 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3850 code to adjust the value and save r2 to support multiple toc sections.
3851 A ppc_stub_long_branch with an r2 offset looks like:
3852 . std %r2,40(%r1)
3853 . addis %r2,%r2,off@ha
3854 . addi %r2,%r2,off@l
3855 . b dest
3856
3857 A ppc_stub_plt_branch with an r2 offset looks like:
3858 . std %r2,40(%r1)
71a39c98
AM
3859 . addis %r11,%r2,xxx@toc@ha
3860 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3861 . addis %r2,%r2,off@ha
3862 . addi %r2,%r2,off@l
71a39c98 3863 . mtctr %r12
ad8e1ba5 3864 . bctr
ac2df442
AM
3865
3866 In cases where the "addis" instruction would add zero, the "addis" is
3867 omitted and following instructions modified slightly in some cases.
721956f4
AM
3868*/
3869
3870enum ppc_stub_type {
3871 ppc_stub_none,
3872 ppc_stub_long_branch,
ad8e1ba5 3873 ppc_stub_long_branch_r2off,
721956f4 3874 ppc_stub_plt_branch,
ad8e1ba5 3875 ppc_stub_plt_branch_r2off,
794e51c0 3876 ppc_stub_plt_call,
7341d5e2 3877 ppc_stub_plt_call_r2save,
a4b6fadd
AM
3878 ppc_stub_global_entry,
3879 ppc_stub_save_res
721956f4
AM
3880};
3881
6f20ed8a
AM
3882/* Information on stub grouping. */
3883struct map_stub
3884{
3885 /* The stub section. */
3886 asection *stub_sec;
3887 /* This is the section to which stubs in the group will be attached. */
3888 asection *link_sec;
a4b6fadd
AM
3889 /* Next group. */
3890 struct map_stub *next;
3891 /* Whether to emit a copy of register save/restore functions in this
3892 group. */
3893 int needs_save_res;
6f20ed8a
AM
3894};
3895
721956f4
AM
3896struct ppc_stub_hash_entry {
3897
3898 /* Base hash table entry structure. */
3899 struct bfd_hash_entry root;
3900
ad8e1ba5
AM
3901 enum ppc_stub_type stub_type;
3902
6f20ed8a
AM
3903 /* Group information. */
3904 struct map_stub *group;
721956f4
AM
3905
3906 /* Offset within stub_sec of the beginning of this stub. */
3907 bfd_vma stub_offset;
3908
3909 /* Given the symbol's value and its section we can determine its final
3910 value when building the stubs (so the stub knows where to jump. */
3911 bfd_vma target_value;
3912 asection *target_section;
3913
721956f4
AM
3914 /* The symbol table entry, if any, that this was derived from. */
3915 struct ppc_link_hash_entry *h;
e054468f 3916 struct plt_entry *plt_ent;
721956f4 3917
6911b7dc
AM
3918 /* Symbol st_other. */
3919 unsigned char other;
721956f4
AM
3920};
3921
3922struct ppc_branch_hash_entry {
3923
3924 /* Base hash table entry structure. */
3925 struct bfd_hash_entry root;
3926
c456f082 3927 /* Offset within branch lookup table. */
721956f4
AM
3928 unsigned int offset;
3929
3930 /* Generation marker. */
3931 unsigned int iter;
3932};
65f38f15 3933
19e08130
AM
3934/* Used to track dynamic relocations for local symbols. */
3935struct ppc_dyn_relocs
3936{
3937 struct ppc_dyn_relocs *next;
3938
3939 /* The input section of the reloc. */
3940 asection *sec;
3941
3942 /* Total number of relocs copied for the input section. */
3943 unsigned int count : 31;
3944
3945 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3946 unsigned int ifunc : 1;
3947};
3948
65f38f15
AM
3949struct ppc_link_hash_entry
3950{
3951 struct elf_link_hash_entry elf;
3952
b3fac117
AM
3953 union {
3954 /* A pointer to the most recently used stub hash entry against this
3955 symbol. */
3956 struct ppc_stub_hash_entry *stub_cache;
3957
3958 /* A pointer to the next symbol starting with a '.' */
3959 struct ppc_link_hash_entry *next_dot_sym;
3960 } u;
721956f4 3961
65f38f15 3962 /* Track dynamic relocs copied for this symbol. */
6061a67d 3963 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3964
d311bc8b
AM
3965 /* Chain of aliases referring to a weakdef. */
3966 struct ppc_link_hash_entry *weakref;
3967
721956f4 3968 /* Link between function code and descriptor symbols. */
34814b9f 3969 struct ppc_link_hash_entry *oh;
721956f4 3970
e86ce104
AM
3971 /* Flag function code and descriptor symbols. */
3972 unsigned int is_func:1;
3973 unsigned int is_func_descriptor:1;
908b32fc 3974 unsigned int fake:1;
411e1bfb 3975
c5614fa4
AM
3976 /* Whether global opd/toc sym has been adjusted or not.
3977 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3978 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3979 unsigned int adjust_done:1;
3980
99877b66
AM
3981 /* Set if we twiddled this symbol to weak at some stage. */
3982 unsigned int was_undefined:1;
3983
a4b6fadd
AM
3984 /* Set if this is an out-of-line register save/restore function,
3985 with non-standard calling convention. */
3986 unsigned int save_res:1;
3987
411e1bfb 3988 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3989 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3990 corresponding relocs are encountered during check_relocs.
3991 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3992 indicate the corresponding GOT entry type is not needed.
3993 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3994 a TPREL one. We use a separate flag rather than setting TPREL
3995 just for convenience in distinguishing the two cases. */
3996#define TLS_GD 1 /* GD reloc. */
3997#define TLS_LD 2 /* LD reloc. */
3998#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3999#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4000#define TLS_TLS 16 /* Any TLS reloc. */
4001#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4002#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 4003#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 4004 unsigned char tls_mask;
65f38f15
AM
4005};
4006
4007/* ppc64 ELF linker hash table. */
4008
4009struct ppc_link_hash_table
4010{
4011 struct elf_link_hash_table elf;
4012
721956f4
AM
4013 /* The stub hash table. */
4014 struct bfd_hash_table stub_hash_table;
4015
4016 /* Another hash table for plt_branch stubs. */
4017 struct bfd_hash_table branch_hash_table;
4018
3b421ab3
AM
4019 /* Hash table for function prologue tocsave. */
4020 htab_t tocsave_htab;
4021
e7d1c40c
AM
4022 /* Various options and other info passed from the linker. */
4023 struct ppc64_elf_params *params;
721956f4 4024
6f20ed8a
AM
4025 /* The size of sec_info below. */
4026 unsigned int sec_info_arr_size;
4027
4028 /* Per-section array of extra section info. Done this way rather
4029 than as part of ppc64_elf_section_data so we have the info for
4030 non-ppc64 sections. */
4031 struct
4032 {
4033 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 4034 bfd_vma toc_off;
6f20ed8a
AM
4035
4036 union
4037 {
4038 /* The section group that this section belongs to. */
4039 struct map_stub *group;
4040 /* A temp section list pointer. */
4041 asection *list;
4042 } u;
4043 } *sec_info;
721956f4 4044
a4b6fadd
AM
4045 /* Linked list of groups. */
4046 struct map_stub *group;
4047
ad8e1ba5
AM
4048 /* Temp used when calculating TOC pointers. */
4049 bfd_vma toc_curr;
bf102f86
AM
4050 bfd *toc_bfd;
4051 asection *toc_first_sec;
ad8e1ba5 4052
b3fac117
AM
4053 /* Used when adding symbols. */
4054 struct ppc_link_hash_entry *dot_syms;
4055
33e44f2e 4056 /* Shortcuts to get to dynamic linker sections. */
4ce794b7
AM
4057 asection *dynbss;
4058 asection *relbss;
4059 asection *glink;
82bd7b59 4060 asection *sfpr;
4ce794b7
AM
4061 asection *brlt;
4062 asection *relbrlt;
58d180e8 4063 asection *glink_eh_frame;
ec338859 4064
8387904d
AM
4065 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4066 struct ppc_link_hash_entry *tls_get_addr;
4067 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4068
927be08e
AM
4069 /* The size of reliplt used by got entry relocs. */
4070 bfd_size_type got_reli_size;
4071
9b5ecbd0 4072 /* Statistics. */
7341d5e2 4073 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4074
ee75fd95
AM
4075 /* Number of stubs against global syms. */
4076 unsigned long stub_globals;
4077
ee67d69a
AM
4078 /* Set if we're linking code with function descriptors. */
4079 unsigned int opd_abi:1;
4080
4c52953f 4081 /* Support for multiple toc sections. */
33c0ec9d 4082 unsigned int do_multi_toc:1;
4c52953f 4083 unsigned int multi_toc_needed:1;
927be08e 4084 unsigned int second_toc_pass:1;
67f0cbdb 4085 unsigned int do_toc_opt:1;
4c52953f 4086
5d1634d7 4087 /* Set on error. */
99877b66 4088 unsigned int stub_error:1;
721956f4 4089
f6c7c3e8 4090 /* Temp used by ppc64_elf_before_check_relocs. */
99877b66 4091 unsigned int twiddled_syms:1;
721956f4
AM
4092
4093 /* Incremented every time we size stubs. */
4094 unsigned int stub_iteration;
5d1634d7 4095
87d72d41
AM
4096 /* Small local sym cache. */
4097 struct sym_cache sym_cache;
65f38f15
AM
4098};
4099
4c52953f
AM
4100/* Rename some of the generic section flags to better document how they
4101 are used here. */
b0dddeec
AM
4102
4103/* Nonzero if this section has TLS related relocations. */
4104#define has_tls_reloc sec_flg0
4105
4106/* Nonzero if this section has a call to __tls_get_addr. */
4107#define has_tls_get_addr_call sec_flg1
4108
4109/* Nonzero if this section has any toc or got relocs. */
4110#define has_toc_reloc sec_flg2
4111
4112/* Nonzero if this section has a call to another section that uses
4113 the toc or got. */
d77c8a4b 4114#define makes_toc_func_call sec_flg3
b0dddeec
AM
4115
4116/* Recursion protection when determining above flag. */
d77c8a4b 4117#define call_check_in_progress sec_flg4
70cc837d 4118#define call_check_done sec_flg5
4c52953f 4119
65f38f15
AM
4120/* Get the ppc64 ELF linker hash table from a link_info structure. */
4121
4122#define ppc_hash_table(p) \
4dfe6ac6
NC
4123 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4124 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4125
721956f4
AM
4126#define ppc_stub_hash_lookup(table, string, create, copy) \
4127 ((struct ppc_stub_hash_entry *) \
4128 bfd_hash_lookup ((table), (string), (create), (copy)))
4129
4130#define ppc_branch_hash_lookup(table, string, create, copy) \
4131 ((struct ppc_branch_hash_entry *) \
4132 bfd_hash_lookup ((table), (string), (create), (copy)))
4133
4134/* Create an entry in the stub hash table. */
4135
4136static struct bfd_hash_entry *
4ce794b7
AM
4137stub_hash_newfunc (struct bfd_hash_entry *entry,
4138 struct bfd_hash_table *table,
4139 const char *string)
721956f4
AM
4140{
4141 /* Allocate the structure if it has not already been allocated by a
4142 subclass. */
4143 if (entry == NULL)
4144 {
4145 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4146 if (entry == NULL)
4147 return entry;
4148 }
4149
4150 /* Call the allocation method of the superclass. */
4151 entry = bfd_hash_newfunc (entry, table, string);
4152 if (entry != NULL)
4153 {
4154 struct ppc_stub_hash_entry *eh;
4155
4156 /* Initialize the local fields. */
4157 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4158 eh->stub_type = ppc_stub_none;
6f20ed8a 4159 eh->group = NULL;
721956f4
AM
4160 eh->stub_offset = 0;
4161 eh->target_value = 0;
4162 eh->target_section = NULL;
721956f4 4163 eh->h = NULL;
6911b7dc 4164 eh->plt_ent = NULL;
6911b7dc 4165 eh->other = 0;
721956f4
AM
4166 }
4167
4168 return entry;
4169}
4170
4171/* Create an entry in the branch hash table. */
4172
4173static struct bfd_hash_entry *
4ce794b7
AM
4174branch_hash_newfunc (struct bfd_hash_entry *entry,
4175 struct bfd_hash_table *table,
4176 const char *string)
721956f4
AM
4177{
4178 /* Allocate the structure if it has not already been allocated by a
4179 subclass. */
4180 if (entry == NULL)
4181 {
4182 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4183 if (entry == NULL)
4184 return entry;
4185 }
4186
4187 /* Call the allocation method of the superclass. */
4188 entry = bfd_hash_newfunc (entry, table, string);
4189 if (entry != NULL)
4190 {
4191 struct ppc_branch_hash_entry *eh;
4192
4193 /* Initialize the local fields. */
4194 eh = (struct ppc_branch_hash_entry *) entry;
4195 eh->offset = 0;
4196 eh->iter = 0;
4197 }
4198
4199 return entry;
4200}
4201
65f38f15
AM
4202/* Create an entry in a ppc64 ELF linker hash table. */
4203
4204static struct bfd_hash_entry *
4ce794b7
AM
4205link_hash_newfunc (struct bfd_hash_entry *entry,
4206 struct bfd_hash_table *table,
4207 const char *string)
65f38f15
AM
4208{
4209 /* Allocate the structure if it has not already been allocated by a
4210 subclass. */
4211 if (entry == NULL)
4212 {
4213 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4214 if (entry == NULL)
4215 return entry;
4216 }
4217
4218 /* Call the allocation method of the superclass. */
4219 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4220 if (entry != NULL)
4221 {
4222 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4223
b3fac117 4224 memset (&eh->u.stub_cache, 0,
908b32fc 4225 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4226 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4227
4228 /* When making function calls, old ABI code references function entry
4229 points (dot symbols), while new ABI code references the function
4230 descriptor symbol. We need to make any combination of reference and
4231 definition work together, without breaking archive linking.
4232
4233 For a defined function "foo" and an undefined call to "bar":
4234 An old object defines "foo" and ".foo", references ".bar" (possibly
4235 "bar" too).
4236 A new object defines "foo" and references "bar".
4237
4238 A new object thus has no problem with its undefined symbols being
4239 satisfied by definitions in an old object. On the other hand, the
4240 old object won't have ".bar" satisfied by a new object.
4241
4242 Keep a list of newly added dot-symbols. */
4243
4244 if (string[0] == '.')
4245 {
4246 struct ppc_link_hash_table *htab;
4247
4248 htab = (struct ppc_link_hash_table *) table;
4249 eh->u.next_dot_sym = htab->dot_syms;
4250 htab->dot_syms = eh;
4251 }
65f38f15
AM
4252 }
4253
4254 return entry;
4255}
4256
3b421ab3
AM
4257struct tocsave_entry {
4258 asection *sec;
4259 bfd_vma offset;
4260};
4261
4262static hashval_t
4263tocsave_htab_hash (const void *p)
4264{
4265 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4266 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4267}
4268
4269static int
4270tocsave_htab_eq (const void *p1, const void *p2)
4271{
4272 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4273 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4274 return e1->sec == e2->sec && e1->offset == e2->offset;
4275}
4276
68faa637
AM
4277/* Destroy a ppc64 ELF linker hash table. */
4278
4279static void
d495ab0d 4280ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4281{
d495ab0d 4282 struct ppc_link_hash_table *htab;
68faa637 4283
d495ab0d 4284 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4285 if (htab->tocsave_htab)
4286 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4287 bfd_hash_table_free (&htab->branch_hash_table);
4288 bfd_hash_table_free (&htab->stub_hash_table);
4289 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4290}
4291
65f38f15
AM
4292/* Create a ppc64 ELF linker hash table. */
4293
4294static struct bfd_link_hash_table *
4ce794b7 4295ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4296{
4297 struct ppc_link_hash_table *htab;
4298 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4299
4ce794b7 4300 htab = bfd_zmalloc (amt);
65f38f15
AM
4301 if (htab == NULL)
4302 return NULL;
4303
66eb6687 4304 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4305 sizeof (struct ppc_link_hash_entry),
4306 PPC64_ELF_DATA))
65f38f15 4307 {
e2d34d7d 4308 free (htab);
65f38f15
AM
4309 return NULL;
4310 }
4311
721956f4 4312 /* Init the stub hash table too. */
66eb6687
AM
4313 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4314 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4315 {
d495ab0d 4316 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4317 return NULL;
4318 }
721956f4
AM
4319
4320 /* And the branch hash table. */
66eb6687
AM
4321 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4322 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4323 {
4324 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4325 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4326 return NULL;
4327 }
721956f4 4328
3b421ab3
AM
4329 htab->tocsave_htab = htab_try_create (1024,
4330 tocsave_htab_hash,
4331 tocsave_htab_eq,
4332 NULL);
4333 if (htab->tocsave_htab == NULL)
2915c55b 4334 {
d495ab0d 4335 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4336 return NULL;
4337 }
d495ab0d 4338 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4339
3254fd24
AM
4340 /* Initializing two fields of the union is just cosmetic. We really
4341 only care about glist, but when compiled on a 32-bit host the
4342 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4343 debugger inspection of these fields look nicer. */
a6aa5195
AM
4344 htab->elf.init_got_refcount.refcount = 0;
4345 htab->elf.init_got_refcount.glist = NULL;
4346 htab->elf.init_plt_refcount.refcount = 0;
4347 htab->elf.init_plt_refcount.glist = NULL;
4348 htab->elf.init_got_offset.offset = 0;
4349 htab->elf.init_got_offset.glist = NULL;
4350 htab->elf.init_plt_offset.offset = 0;
4351 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4352
65f38f15
AM
4353 return &htab->elf.root;
4354}
4355
bfeb4a28
AM
4356/* Create sections for linker generated code. */
4357
4358static bfd_boolean
4359create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4360{
4361 struct ppc_link_hash_table *htab;
4362 flagword flags;
4363
4364 htab = ppc_hash_table (info);
4365
bfeb4a28
AM
4366 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4367 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4368 if (htab->params->save_restore_funcs)
4369 {
4370 /* Create .sfpr for code to save and restore fp regs. */
4371 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4372 flags);
4373 if (htab->sfpr == NULL
4374 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4375 return FALSE;
4376 }
4377
4378 if (bfd_link_relocatable (info))
4379 return TRUE;
bfeb4a28
AM
4380
4381 /* Create .glink for lazy dynamic linking support. */
4382 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4383 flags);
4384 if (htab->glink == NULL
4385 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4386 return FALSE;
4387
4388 if (!info->no_ld_generated_unwind_info)
4389 {
4390 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4391 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4392 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4393 ".eh_frame",
4394 flags);
4395 if (htab->glink_eh_frame == NULL
4396 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4397 return FALSE;
4398 }
4399
4400 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4401 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4402 if (htab->elf.iplt == NULL
4403 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4404 return FALSE;
4405
4406 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4407 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4408 htab->elf.irelplt
4409 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4410 if (htab->elf.irelplt == NULL
4411 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4412 return FALSE;
4413
4414 /* Create branch lookup table for plt_branch stubs. */
4415 flags = (SEC_ALLOC | SEC_LOAD
4416 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4417 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4418 flags);
4419 if (htab->brlt == NULL
4420 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4421 return FALSE;
4422
0e1862bb 4423 if (!bfd_link_pic (info))
bfeb4a28
AM
4424 return TRUE;
4425
4426 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4427 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4428 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4429 ".rela.branch_lt",
4430 flags);
4431 if (htab->relbrlt == NULL
4432 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4433 return FALSE;
4434
4435 return TRUE;
4436}
4437
e717da7e
AM
4438/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4439
bfeb4a28 4440bfd_boolean
e7d1c40c
AM
4441ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4442 struct ppc64_elf_params *params)
e717da7e
AM
4443{
4444 struct ppc_link_hash_table *htab;
4445
e7d1c40c 4446 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4447
4448/* Always hook our dynamic sections into the first bfd, which is the
4449 linker created stub bfd. This ensures that the GOT header is at
4450 the start of the output TOC section. */
4451 htab = ppc_hash_table (info);
e7d1c40c
AM
4452 htab->elf.dynobj = params->stub_bfd;
4453 htab->params = params;
bfeb4a28 4454
bfeb4a28 4455 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4456}
4457
721956f4
AM
4458/* Build a name for an entry in the stub hash table. */
4459
4460static char *
4ce794b7
AM
4461ppc_stub_name (const asection *input_section,
4462 const asection *sym_sec,
4463 const struct ppc_link_hash_entry *h,
4464 const Elf_Internal_Rela *rel)
721956f4
AM
4465{
4466 char *stub_name;
bcaa2f82 4467 ssize_t len;
721956f4
AM
4468
4469 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4470 offsets from a sym as a branch target? In fact, we could
4471 probably assume the addend is always zero. */
4472 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4473
4474 if (h)
4475 {
4476 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4477 stub_name = bfd_malloc (len);
46de2a7c
AM
4478 if (stub_name == NULL)
4479 return stub_name;
4480
bcaa2f82
AM
4481 len = sprintf (stub_name, "%08x.%s+%x",
4482 input_section->id & 0xffffffff,
4483 h->elf.root.root.string,
4484 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4485 }
4486 else
4487 {
ad8e1ba5 4488 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4489 stub_name = bfd_malloc (len);
46de2a7c
AM
4490 if (stub_name == NULL)
4491 return stub_name;
4492
bcaa2f82
AM
4493 len = sprintf (stub_name, "%08x.%x:%x+%x",
4494 input_section->id & 0xffffffff,
4495 sym_sec->id & 0xffffffff,
4496 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4497 (int) rel->r_addend & 0xffffffff);
721956f4 4498 }
bcaa2f82 4499 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4500 stub_name[len - 2] = 0;
721956f4
AM
4501 return stub_name;
4502}
4503
4504/* Look up an entry in the stub hash. Stub entries are cached because
4505 creating the stub name takes a bit of time. */
4506
4507static struct ppc_stub_hash_entry *
4ce794b7
AM
4508ppc_get_stub_entry (const asection *input_section,
4509 const asection *sym_sec,
039b3fef 4510 struct ppc_link_hash_entry *h,
4ce794b7
AM
4511 const Elf_Internal_Rela *rel,
4512 struct ppc_link_hash_table *htab)
721956f4
AM
4513{
4514 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4515 struct map_stub *group;
721956f4
AM
4516
4517 /* If this input section is part of a group of sections sharing one
4518 stub section, then use the id of the first section in the group.
4519 Stub names need to include a section id, as there may well be
4520 more than one stub used to reach say, printf, and we need to
4521 distinguish between them. */
6f20ed8a 4522 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4523 if (group == NULL)
4524 return NULL;
721956f4 4525
b3fac117
AM
4526 if (h != NULL && h->u.stub_cache != NULL
4527 && h->u.stub_cache->h == h
6f20ed8a 4528 && h->u.stub_cache->group == group)
721956f4 4529 {
b3fac117 4530 stub_entry = h->u.stub_cache;
721956f4
AM
4531 }
4532 else
4533 {
4534 char *stub_name;
4535
6f20ed8a 4536 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4537 if (stub_name == NULL)
4538 return NULL;
4539
4540 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4541 stub_name, FALSE, FALSE);
721956f4 4542 if (h != NULL)
b3fac117 4543 h->u.stub_cache = stub_entry;
721956f4
AM
4544
4545 free (stub_name);
4546 }
4547
4548 return stub_entry;
4549}
4550
4551/* Add a new stub entry to the stub hash. Not all fields of the new
4552 stub entry are initialised. */
4553
4554static struct ppc_stub_hash_entry *
4ce794b7
AM
4555ppc_add_stub (const char *stub_name,
4556 asection *section,
25f53a85 4557 struct bfd_link_info *info)
721956f4 4558{
25f53a85 4559 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4560 struct map_stub *group;
721956f4
AM
4561 asection *link_sec;
4562 asection *stub_sec;
4563 struct ppc_stub_hash_entry *stub_entry;
4564
6f20ed8a
AM
4565 group = htab->sec_info[section->id].u.group;
4566 link_sec = group->link_sec;
4567 stub_sec = group->stub_sec;
721956f4
AM
4568 if (stub_sec == NULL)
4569 {
6f20ed8a
AM
4570 size_t namelen;
4571 bfd_size_type len;
4572 char *s_name;
721956f4 4573
6f20ed8a
AM
4574 namelen = strlen (link_sec->name);
4575 len = namelen + sizeof (STUB_SUFFIX);
4576 s_name = bfd_alloc (htab->params->stub_bfd, len);
4577 if (s_name == NULL)
4578 return NULL;
721956f4 4579
6f20ed8a
AM
4580 memcpy (s_name, link_sec->name, namelen);
4581 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4582 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4583 if (stub_sec == NULL)
4584 return NULL;
4585 group->stub_sec = stub_sec;
721956f4
AM
4586 }
4587
4588 /* Enter this entry into the linker stub hash table. */
4589 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4590 TRUE, FALSE);
721956f4
AM
4591 if (stub_entry == NULL)
4592 {
695344c0 4593 /* xgettext:c-format */
8de848d8 4594 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4595 section->owner, stub_name);
721956f4
AM
4596 return NULL;
4597 }
4598
6f20ed8a 4599 stub_entry->group = group;
721956f4 4600 stub_entry->stub_offset = 0;
721956f4
AM
4601 return stub_entry;
4602}
4603
e717da7e
AM
4604/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4605 not already done. */
65f38f15 4606
b34976b6 4607static bfd_boolean
e717da7e 4608create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4609{
e717da7e
AM
4610 asection *got, *relgot;
4611 flagword flags;
4612 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4613
0c8d6e5c 4614 if (!is_ppc64_elf (abfd))
0ffa91dd 4615 return FALSE;
4dfe6ac6
NC
4616 if (htab == NULL)
4617 return FALSE;
0ffa91dd 4618
33e44f2e
AM
4619 if (!htab->elf.sgot
4620 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4621 return FALSE;
e717da7e
AM
4622
4623 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4624 | SEC_LINKER_CREATED);
4625
c456f082 4626 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4627 if (!got
e717da7e
AM
4628 || !bfd_set_section_alignment (abfd, got, 3))
4629 return FALSE;
65f38f15 4630
c456f082
AM
4631 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4632 flags | SEC_READONLY);
e717da7e 4633 if (!relgot
e717da7e 4634 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4635 return FALSE;
e717da7e
AM
4636
4637 ppc64_elf_tdata (abfd)->got = got;
4638 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4639 return TRUE;
65f38f15 4640}
5bd4f169 4641
82bd7b59 4642/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4643
b34976b6 4644static bfd_boolean
4ce794b7 4645ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4646{
65f38f15 4647 struct ppc_link_hash_table *htab;
5bd4f169 4648
65f38f15 4649 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4650 return FALSE;
65f38f15 4651
e717da7e 4652 htab = ppc_hash_table (info);
4dfe6ac6
NC
4653 if (htab == NULL)
4654 return FALSE;
4655
3d4d4302 4656 htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
0e1862bb 4657 if (!bfd_link_pic (info))
3d4d4302 4658 htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
65f38f15 4659
33e44f2e 4660 if (!htab->elf.sgot || !htab->elf.splt || !htab->elf.srelplt || !htab->dynbss
0e1862bb 4661 || (!bfd_link_pic (info) && !htab->relbss))
65f38f15
AM
4662 abort ();
4663
b34976b6 4664 return TRUE;
5bd4f169
AM
4665}
4666
b31867b6
AM
4667/* Follow indirect and warning symbol links. */
4668
4669static inline struct bfd_link_hash_entry *
4670follow_link (struct bfd_link_hash_entry *h)
4671{
4672 while (h->type == bfd_link_hash_indirect
4673 || h->type == bfd_link_hash_warning)
4674 h = h->u.i.link;
4675 return h;
4676}
4677
4678static inline struct elf_link_hash_entry *
4679elf_follow_link (struct elf_link_hash_entry *h)
4680{
4681 return (struct elf_link_hash_entry *) follow_link (&h->root);
4682}
4683
4684static inline struct ppc_link_hash_entry *
4685ppc_follow_link (struct ppc_link_hash_entry *h)
4686{
4687 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4688}
4689
40d16e0b
AM
4690/* Merge PLT info on FROM with that on TO. */
4691
4692static void
4693move_plt_plist (struct ppc_link_hash_entry *from,
4694 struct ppc_link_hash_entry *to)
4695{
4696 if (from->elf.plt.plist != NULL)
4697 {
4698 if (to->elf.plt.plist != NULL)
4699 {
4700 struct plt_entry **entp;
4701 struct plt_entry *ent;
4702
4703 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4704 {
4705 struct plt_entry *dent;
4706
4707 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4708 if (dent->addend == ent->addend)
4709 {
4710 dent->plt.refcount += ent->plt.refcount;
4711 *entp = ent->next;
4712 break;
4713 }
4714 if (dent == NULL)
4715 entp = &ent->next;
4716 }
4717 *entp = to->elf.plt.plist;
4718 }
4719
4720 to->elf.plt.plist = from->elf.plt.plist;
4721 from->elf.plt.plist = NULL;
4722 }
4723}
4724
65f38f15
AM
4725/* Copy the extra info we tack onto an elf_link_hash_entry. */
4726
4727static void
fcfa13d2
AM
4728ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4729 struct elf_link_hash_entry *dir,
4730 struct elf_link_hash_entry *ind)
65f38f15
AM
4731{
4732 struct ppc_link_hash_entry *edir, *eind;
4733
4734 edir = (struct ppc_link_hash_entry *) dir;
4735 eind = (struct ppc_link_hash_entry *) ind;
4736
c79d6685
AM
4737 edir->is_func |= eind->is_func;
4738 edir->is_func_descriptor |= eind->is_func_descriptor;
4739 edir->tls_mask |= eind->tls_mask;
4740 if (eind->oh != NULL)
4741 edir->oh = ppc_follow_link (eind->oh);
4742
474436e6
AM
4743 if (edir->elf.versioned != versioned_hidden)
4744 {
4745 /* If called to transfer flags for a weakdef during processing
4746 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4747 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4748 if (!(ELIMINATE_COPY_RELOCS
4749 && eind->elf.root.type != bfd_link_hash_indirect
4750 && edir->elf.dynamic_adjusted))
4751 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4752
4753 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4754 edir->elf.ref_regular |= eind->elf.ref_regular;
4755 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4756 edir->elf.needs_plt |= eind->elf.needs_plt;
4757 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4758 }
c79d6685 4759
d311bc8b
AM
4760 /* If we were called to copy over info for a weak sym, don't copy
4761 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4762 in order to simplify readonly_dynrelocs and save a field in the
4763 symbol hash entry, but that means dyn_relocs can't be used in any
4764 tests about a specific symbol, or affect other symbol flags which
4765 are then tested.
4766 Chain weakdefs so we can get from the weakdef back to an alias.
4767 The list is circular so that we don't need to use u.weakdef as
4768 well as this list to look at all aliases. */
4769 if (eind->elf.root.type != bfd_link_hash_indirect)
4770 {
4771 struct ppc_link_hash_entry *cur, *add, *next;
4772
4773 add = eind;
4774 do
4775 {
4776 cur = edir->weakref;
4777 if (cur != NULL)
4778 {
4779 do
4780 {
4781 /* We can be called twice for the same symbols.
4782 Don't make multiple loops. */
4783 if (cur == add)
4784 return;
4785 cur = cur->weakref;
4786 } while (cur != edir);
4787 }
4788 next = add->weakref;
4789 if (cur != add)
4790 {
4791 add->weakref = edir->weakref != NULL ? edir->weakref : edir;
4792 edir->weakref = add;
4793 }
4794 add = next;
4795 } while (add != NULL && add != eind);
4796 return;
4797 }
4798
411e1bfb 4799 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4800 if (eind->dyn_relocs != NULL)
65f38f15 4801 {
bbd7ec4a
AM
4802 if (edir->dyn_relocs != NULL)
4803 {
6061a67d
AM
4804 struct elf_dyn_relocs **pp;
4805 struct elf_dyn_relocs *p;
bbd7ec4a 4806
fcfa13d2 4807 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4808 list. Merge any entries against the same section. */
4809 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4810 {
6061a67d 4811 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4812
4813 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4814 if (q->sec == p->sec)
4815 {
4816 q->pc_count += p->pc_count;
4817 q->count += p->count;
4818 *pp = p->next;
4819 break;
4820 }
4821 if (q == NULL)
4822 pp = &p->next;
4823 }
4824 *pp = edir->dyn_relocs;
4825 }
4826
65f38f15
AM
4827 edir->dyn_relocs = eind->dyn_relocs;
4828 eind->dyn_relocs = NULL;
4829 }
65f38f15 4830
81848ca0
AM
4831 /* Copy over got entries that we may have already seen to the
4832 symbol which just became indirect. */
411e1bfb
AM
4833 if (eind->elf.got.glist != NULL)
4834 {
4835 if (edir->elf.got.glist != NULL)
4836 {
4837 struct got_entry **entp;
4838 struct got_entry *ent;
4839
4840 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4841 {
4842 struct got_entry *dent;
4843
4844 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4845 if (dent->addend == ent->addend
e717da7e 4846 && dent->owner == ent->owner
411e1bfb
AM
4847 && dent->tls_type == ent->tls_type)
4848 {
4849 dent->got.refcount += ent->got.refcount;
4850 *entp = ent->next;
4851 break;
4852 }
4853 if (dent == NULL)
4854 entp = &ent->next;
4855 }
4856 *entp = edir->elf.got.glist;
4857 }
4858
4859 edir->elf.got.glist = eind->elf.got.glist;
4860 eind->elf.got.glist = NULL;
4861 }
4862
4863 /* And plt entries. */
40d16e0b 4864 move_plt_plist (eind, edir);
411e1bfb 4865
fcfa13d2 4866 if (eind->elf.dynindx != -1)
411e1bfb 4867 {
fcfa13d2
AM
4868 if (edir->elf.dynindx != -1)
4869 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4870 edir->elf.dynstr_index);
411e1bfb
AM
4871 edir->elf.dynindx = eind->elf.dynindx;
4872 edir->elf.dynstr_index = eind->elf.dynstr_index;
4873 eind->elf.dynindx = -1;
4874 eind->elf.dynstr_index = 0;
4875 }
411e1bfb
AM
4876}
4877
8387904d
AM
4878/* Find the function descriptor hash entry from the given function code
4879 hash entry FH. Link the entries via their OH fields. */
4880
4881static struct ppc_link_hash_entry *
b31867b6 4882lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4883{
4884 struct ppc_link_hash_entry *fdh = fh->oh;
4885
4886 if (fdh == NULL)
4887 {
4888 const char *fd_name = fh->elf.root.root.string + 1;
4889
4890 fdh = (struct ppc_link_hash_entry *)
4891 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4892 if (fdh == NULL)
4893 return fdh;
4894
4895 fdh->is_func_descriptor = 1;
4896 fdh->oh = fh;
4897 fh->is_func = 1;
4898 fh->oh = fdh;
8387904d
AM
4899 }
4900
b31867b6 4901 return ppc_follow_link (fdh);
8387904d
AM
4902}
4903
bb700d78
AM
4904/* Make a fake function descriptor sym for the code sym FH. */
4905
4906static struct ppc_link_hash_entry *
4907make_fdh (struct bfd_link_info *info,
908b32fc 4908 struct ppc_link_hash_entry *fh)
bb700d78
AM
4909{
4910 bfd *abfd;
4911 asymbol *newsym;
4912 struct bfd_link_hash_entry *bh;
4913 struct ppc_link_hash_entry *fdh;
4914
4915 abfd = fh->elf.root.u.undef.abfd;
4916 newsym = bfd_make_empty_symbol (abfd);
4917 newsym->name = fh->elf.root.root.string + 1;
4918 newsym->section = bfd_und_section_ptr;
4919 newsym->value = 0;
908b32fc 4920 newsym->flags = BSF_WEAK;
bb700d78
AM
4921
4922 bh = NULL;
4923 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4924 newsym->flags, newsym->section,
4925 newsym->value, NULL, FALSE, FALSE,
4926 &bh))
4927 return NULL;
4928
4929 fdh = (struct ppc_link_hash_entry *) bh;
4930 fdh->elf.non_elf = 0;
908b32fc
AM
4931 fdh->fake = 1;
4932 fdh->is_func_descriptor = 1;
4933 fdh->oh = fh;
4934 fh->is_func = 1;
4935 fh->oh = fdh;
bb700d78
AM
4936 return fdh;
4937}
4938
8387904d
AM
4939/* Fix function descriptor symbols defined in .opd sections to be
4940 function type. */
555cd476
AM
4941
4942static bfd_boolean
c16153ae 4943ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4944 struct bfd_link_info *info,
555cd476 4945 Elf_Internal_Sym *isym,
6911b7dc 4946 const char **name,
555cd476
AM
4947 flagword *flags ATTRIBUTE_UNUSED,
4948 asection **sec,
b53dfeb2 4949 bfd_vma *value)
555cd476 4950{
a43942db 4951 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
f1885d1e
AM
4952 && (ibfd->flags & DYNAMIC) == 0
4953 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
a43942db 4954 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
f64b2e8d 4955
b53dfeb2 4956 if (*sec != NULL
f1885d1e 4957 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4958 {
4959 asection *code_sec;
4960
4961 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4962 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4963 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4964
4965 /* If the symbol is a function defined in .opd, and the function
4966 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4967 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4968 && (*sec)->reloc_count != 0
4969 && opd_entry_value (*sec, *value, &code_sec, NULL,
4970 FALSE) != (bfd_vma) -1
4971 && discarded_section (code_sec))
4972 {
4973 *sec = bfd_und_section_ptr;
4974 isym->st_shndx = SHN_UNDEF;
4975 }
4976 }
dbd1e97e
AM
4977 else if (*sec != NULL
4978 && strcmp ((*sec)->name, ".toc") == 0
4979 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4980 {
4981 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4982 if (htab != NULL)
4983 htab->params->object_in_toc = 1;
4984 }
433817dd 4985
6911b7dc
AM
4986 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4987 {
4988 if (abiversion (ibfd) == 0)
4989 set_abiversion (ibfd, 2);
4990 else if (abiversion (ibfd) == 1)
4991 {
4992 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4993 " for ABI version 1\n"), name);
4994 bfd_set_error (bfd_error_bad_value);
4995 return FALSE;
4996 }
4997 }
4998
555cd476
AM
4999 return TRUE;
5000}
5001
6911b7dc
AM
5002/* Merge non-visibility st_other attributes: local entry point. */
5003
5004static void
5005ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5006 const Elf_Internal_Sym *isym,
5007 bfd_boolean definition,
5008 bfd_boolean dynamic)
5009{
5010 if (definition && !dynamic)
5011 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5012 | ELF_ST_VISIBILITY (h->other));
5013}
5014
8387904d 5015/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5016 inclusion of a new ABI object archive that defines "bar".
5017 NAME is a symbol defined in an archive. Return a symbol in the hash
5018 table that might be satisfied by the archive symbols. */
8387904d
AM
5019
5020static struct elf_link_hash_entry *
5021ppc64_elf_archive_symbol_lookup (bfd *abfd,
5022 struct bfd_link_info *info,
5023 const char *name)
5024{
5025 struct elf_link_hash_entry *h;
5026 char *dot_name;
5027 size_t len;
5028
5029 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5030 if (h != NULL
5031 /* Don't return this sym if it is a fake function descriptor
5032 created by add_symbol_adjust. */
5033 && !(h->root.type == bfd_link_hash_undefweak
5034 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
5035 return h;
5036
5037 if (name[0] == '.')
5038 return h;
5039
5040 len = strlen (name);
5041 dot_name = bfd_alloc (abfd, len + 2);
5042 if (dot_name == NULL)
5043 return (struct elf_link_hash_entry *) 0 - 1;
5044 dot_name[0] = '.';
5045 memcpy (dot_name + 1, name, len + 1);
5046 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5047 bfd_release (abfd, dot_name);
5048 return h;
5049}
5050
5051/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5052 new ABI object defines "bar". Well, at least, undefined dot symbols
5053 are made weak. This stops later archive searches from including an
5054 object if we already have a function descriptor definition. It also
35b0ce59
AM
5055 prevents the linker complaining about undefined symbols.
5056 We also check and correct mismatched symbol visibility here. The
5057 most restrictive visibility of the function descriptor and the
5058 function entry symbol is used. */
8387904d
AM
5059
5060static bfd_boolean
b3fac117 5061add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5062{
8387904d
AM
5063 struct ppc_link_hash_table *htab;
5064 struct ppc_link_hash_entry *fdh;
5065
b3fac117
AM
5066 if (eh->elf.root.type == bfd_link_hash_warning)
5067 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5068
8400d40d
AM
5069 if (eh->elf.root.type == bfd_link_hash_indirect)
5070 return TRUE;
5071
b3fac117
AM
5072 if (eh->elf.root.root.string[0] != '.')
5073 abort ();
8387904d 5074
b3fac117 5075 htab = ppc_hash_table (info);
4dfe6ac6
NC
5076 if (htab == NULL)
5077 return FALSE;
5078
b31867b6
AM
5079 fdh = lookup_fdh (eh, htab);
5080 if (fdh == NULL)
5081 {
0e1862bb 5082 if (!bfd_link_relocatable (info)
b31867b6
AM
5083 && (eh->elf.root.type == bfd_link_hash_undefined
5084 || eh->elf.root.type == bfd_link_hash_undefweak)
5085 && eh->elf.ref_regular)
5086 {
5087 /* Make an undefweak function descriptor sym, which is enough to
5088 pull in an --as-needed shared lib, but won't cause link
5089 errors. Archives are handled elsewhere. */
5090 fdh = make_fdh (info, eh);
5091 if (fdh == NULL)
5092 return FALSE;
5093 fdh->elf.ref_regular = 1;
5094 }
bb700d78 5095 }
b31867b6 5096 else
8387904d 5097 {
35b0ce59
AM
5098 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5099 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5100 if (entry_vis < descr_vis)
5101 fdh->elf.other += entry_vis - descr_vis;
5102 else if (entry_vis > descr_vis)
5103 eh->elf.other += descr_vis - entry_vis;
5104
e87d886e
AM
5105 if ((fdh->elf.root.type == bfd_link_hash_defined
5106 || fdh->elf.root.type == bfd_link_hash_defweak)
5107 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
5108 {
5109 eh->elf.root.type = bfd_link_hash_undefweak;
5110 eh->was_undefined = 1;
5111 htab->twiddled_syms = 1;
5112 }
8387904d 5113 }
99877b66 5114
8387904d
AM
5115 return TRUE;
5116}
5117
f6c7c3e8
AM
5118/* Set up opd section info and abiversion for IBFD, and process list
5119 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5120
8387904d 5121static bfd_boolean
f6c7c3e8 5122ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5123{
99877b66 5124 struct ppc_link_hash_table *htab;
b3fac117 5125 struct ppc_link_hash_entry **p, *eh;
459609d6 5126 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5127
459609d6 5128 if (opd != NULL && opd->size != 0)
b3fac117 5129 {
459609d6
AM
5130 if (abiversion (ibfd) == 0)
5131 set_abiversion (ibfd, 1);
8a2058b5 5132 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5133 {
695344c0 5134 /* xgettext:c-format */
459609d6
AM
5135 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5136 " version %d\n"),
5137 ibfd, abiversion (ibfd));
5138 bfd_set_error (bfd_error_bad_value);
5139 return FALSE;
f6c7c3e8
AM
5140 }
5141
459609d6
AM
5142 if ((ibfd->flags & DYNAMIC) == 0
5143 && (opd->flags & SEC_RELOC) != 0
5144 && opd->reloc_count != 0
5145 && !bfd_is_abs_section (opd->output_section))
b3fac117 5146 {
459609d6
AM
5147 /* Garbage collection needs some extra help with .opd sections.
5148 We don't want to necessarily keep everything referenced by
5149 relocs in .opd, as that would keep all functions. Instead,
5150 if we reference an .opd symbol (a function descriptor), we
5151 want to keep the function code symbol's section. This is
5152 easy for global symbols, but for local syms we need to keep
5153 information about the associated function section. */
5154 bfd_size_type amt;
5155 asection **opd_sym_map;
5156
51aecdc5 5157 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
459609d6
AM
5158 opd_sym_map = bfd_zalloc (ibfd, amt);
5159 if (opd_sym_map == NULL)
b3fac117 5160 return FALSE;
459609d6
AM
5161 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5162 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5163 ppc64_elf_section_data (opd)->sec_type = sec_opd;
b3fac117
AM
5164 }
5165 }
5166
459609d6
AM
5167 if (!is_ppc64_elf (info->output_bfd))
5168 return TRUE;
5169 htab = ppc_hash_table (info);
5170 if (htab == NULL)
5171 return FALSE;
5172
5173 /* For input files without an explicit abiversion in e_flags
5174 we should have flagged any with symbol st_other bits set
5175 as ELFv1 and above flagged those with .opd as ELFv2.
5176 Set the output abiversion if not yet set, and for any input
5177 still ambiguous, take its abiversion from the output.
5178 Differences in ABI are reported later. */
5179 if (abiversion (info->output_bfd) == 0)
5180 set_abiversion (info->output_bfd, abiversion (ibfd));
5181 else if (abiversion (ibfd) == 0)
5182 set_abiversion (ibfd, abiversion (info->output_bfd));
5183
5184 p = &htab->dot_syms;
5185 while ((eh = *p) != NULL)
5186 {
5187 *p = NULL;
5188 if (&eh->elf == htab->elf.hgot)
5189 ;
5190 else if (htab->elf.hgot == NULL
5191 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5192 htab->elf.hgot = &eh->elf;
5193 else if (!add_symbol_adjust (eh, info))
5194 return FALSE;
5195 p = &eh->u.next_dot_sym;
5196 }
5197
99877b66 5198 /* We need to fix the undefs list for any syms we have twiddled to
57e7d118 5199 undefweak. */
99877b66
AM
5200 if (htab->twiddled_syms)
5201 {
77cfaee6 5202 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
5203 htab->twiddled_syms = 0;
5204 }
b3fac117 5205 return TRUE;
8387904d
AM
5206}
5207
97fed1c9
JJ
5208/* Undo hash table changes when an --as-needed input file is determined
5209 not to be needed. */
5210
5211static bfd_boolean
e5034e59
AM
5212ppc64_elf_notice_as_needed (bfd *ibfd,
5213 struct bfd_link_info *info,
5214 enum notice_asneeded_action act)
97fed1c9 5215{
e5034e59
AM
5216 if (act == notice_not_needed)
5217 {
5218 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5219
e5034e59
AM
5220 if (htab == NULL)
5221 return FALSE;
4dfe6ac6 5222
e5034e59
AM
5223 htab->dot_syms = NULL;
5224 }
5225 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5226}
5227
aa374f67
AM
5228/* If --just-symbols against a final linked binary, then assume we need
5229 toc adjusting stubs when calling functions defined there. */
5230
5231static void
5232ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5233{
5234 if ((sec->flags & SEC_CODE) != 0
5235 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5236 && is_ppc64_elf (sec->owner))
5237 {
2c3f079f
AM
5238 if (abiversion (sec->owner) >= 2
5239 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5240 sec->has_toc_reloc = 1;
5241 }
5242 _bfd_elf_link_just_syms (sec, info);
5243}
5244
e054468f 5245static struct plt_entry **
4ce794b7
AM
5246update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5247 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5248{
5249 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5250 struct plt_entry **local_plt;
f961d9dd 5251 unsigned char *local_got_tls_masks;
411e1bfb
AM
5252
5253 if (local_got_ents == NULL)
5254 {
5255 bfd_size_type size = symtab_hdr->sh_info;
5256
e054468f
AM
5257 size *= (sizeof (*local_got_ents)
5258 + sizeof (*local_plt)
5259 + sizeof (*local_got_tls_masks));
4ce794b7 5260 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5261 if (local_got_ents == NULL)
e054468f 5262 return NULL;
411e1bfb
AM
5263 elf_local_got_ents (abfd) = local_got_ents;
5264 }
5265
e054468f 5266 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5267 {
5268 struct got_entry *ent;
5269
5270 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5271 if (ent->addend == r_addend
5272 && ent->owner == abfd
5273 && ent->tls_type == tls_type)
411e1bfb
AM
5274 break;
5275 if (ent == NULL)
5276 {
5277 bfd_size_type amt = sizeof (*ent);
4ce794b7 5278 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5279 if (ent == NULL)
5280 return FALSE;
5281 ent->next = local_got_ents[r_symndx];
5282 ent->addend = r_addend;
e717da7e 5283 ent->owner = abfd;
411e1bfb 5284 ent->tls_type = tls_type;
927be08e 5285 ent->is_indirect = FALSE;
411e1bfb
AM
5286 ent->got.refcount = 0;
5287 local_got_ents[r_symndx] = ent;
5288 }
5289 ent->got.refcount += 1;
5290 }
5291
e054468f 5292 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5293 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5294 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5295
5296 return local_plt + r_symndx;
65f38f15
AM
5297}
5298
411e1bfb 5299static bfd_boolean
e054468f 5300update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5301{
411e1bfb 5302 struct plt_entry *ent;
1e2f5b6e 5303
e054468f 5304 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5305 if (ent->addend == addend)
5306 break;
5307 if (ent == NULL)
1e2f5b6e 5308 {
411e1bfb 5309 bfd_size_type amt = sizeof (*ent);
4ce794b7 5310 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5311 if (ent == NULL)
5312 return FALSE;
e054468f 5313 ent->next = *plist;
411e1bfb
AM
5314 ent->addend = addend;
5315 ent->plt.refcount = 0;
e054468f 5316 *plist = ent;
1e2f5b6e 5317 }
411e1bfb 5318 ent->plt.refcount += 1;
b34976b6 5319 return TRUE;
1e2f5b6e
AM
5320}
5321
e054468f
AM
5322static bfd_boolean
5323is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5324{
5325 return (r_type == R_PPC64_REL24
5326 || r_type == R_PPC64_REL14
5327 || r_type == R_PPC64_REL14_BRTAKEN
5328 || r_type == R_PPC64_REL14_BRNTAKEN
5329 || r_type == R_PPC64_ADDR24
5330 || r_type == R_PPC64_ADDR14
5331 || r_type == R_PPC64_ADDR14_BRTAKEN
5332 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5333}
5334
5bd4f169 5335/* Look through the relocs for a section during the first phase, and
65f38f15 5336 calculate needed space in the global offset table, procedure
5d1634d7 5337 linkage table, and dynamic reloc sections. */
5bd4f169 5338
b34976b6 5339static bfd_boolean
4ce794b7
AM
5340ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5341 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5342{
65f38f15 5343 struct ppc_link_hash_table *htab;
5bd4f169 5344 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5345 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5346 const Elf_Internal_Rela *rel;
5347 const Elf_Internal_Rela *rel_end;
5bd4f169 5348 asection *sreloc;
1e2f5b6e 5349 asection **opd_sym_map;
3a71aa26 5350 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 5351
0e1862bb 5352 if (bfd_link_relocatable (info))
b34976b6 5353 return TRUE;
5bd4f169 5354
680a3378
AM
5355 /* Don't do anything special with non-loaded, non-alloced sections.
5356 In particular, any relocs in such sections should not affect GOT
5357 and PLT reference counting (ie. we don't allow them to create GOT
5358 or PLT entries), there's no possibility or desire to optimize TLS
5359 relocs, and there's not much point in propagating relocs to shared
5360 libs that the dynamic linker won't relocate. */
5361 if ((sec->flags & SEC_ALLOC) == 0)
5362 return TRUE;
5363
0c8d6e5c 5364 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5365
65f38f15 5366 htab = ppc_hash_table (info);
4dfe6ac6
NC
5367 if (htab == NULL)
5368 return FALSE;
5369
3a71aa26
AM
5370 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5371 FALSE, FALSE, TRUE);
5372 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5373 FALSE, FALSE, TRUE);
0ffa91dd 5374 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5375 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5376 sreloc = NULL;
1e2f5b6e 5377 opd_sym_map = NULL;
f6c7c3e8
AM
5378 if (ppc64_elf_section_data (sec) != NULL
5379 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5380 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5bd4f169
AM
5381
5382 rel_end = relocs + sec->reloc_count;
5383 for (rel = relocs; rel < rel_end; rel++)
5384 {
5385 unsigned long r_symndx;
5386 struct elf_link_hash_entry *h;
04c9666a 5387 enum elf_ppc64_reloc_type r_type;
727fc41e 5388 int tls_type;
7c8fe5c4 5389 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5390 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5391
5392 r_symndx = ELF64_R_SYM (rel->r_info);
5393 if (r_symndx < symtab_hdr->sh_info)
5394 h = NULL;
5395 else
973a3492
L
5396 {
5397 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5398 h = elf_follow_link (h);
81fbe831
AM
5399
5400 /* PR15323, ref flags aren't set for references in the same
5401 object. */
5402 h->root.non_ir_ref = 1;
1c865ab2
AM
5403
5404 if (h == htab->elf.hgot)
5405 sec->has_toc_reloc = 1;
973a3492 5406 }
5bd4f169 5407
727fc41e 5408 tls_type = 0;
e054468f 5409 ifunc = NULL;
25f23106
AM
5410 if (h != NULL)
5411 {
5412 if (h->type == STT_GNU_IFUNC)
5413 {
5414 h->needs_plt = 1;
5415 ifunc = &h->plt.plist;
5416 }
5417 }
5418 else
5419 {
5420 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5421 abfd, r_symndx);
5422 if (isym == NULL)
5423 return FALSE;
5424
5425 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5426 {
5427 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5428 rel->r_addend, PLT_IFUNC);
5429 if (ifunc == NULL)
5430 return FALSE;
5431 }
5432 }
727fc41e 5433
cbf95972 5434 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5435 switch (r_type)
5bd4f169 5436 {
727fc41e
AM
5437 case R_PPC64_TLSGD:
5438 case R_PPC64_TLSLD:
5439 /* These special tls relocs tie a call to __tls_get_addr with
5440 its parameter symbol. */
5441 break;
5442
411e1bfb
AM
5443 case R_PPC64_GOT_TLSLD16:
5444 case R_PPC64_GOT_TLSLD16_LO:
5445 case R_PPC64_GOT_TLSLD16_HI:
5446 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5447 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5448 goto dogottls;
5449
5450 case R_PPC64_GOT_TLSGD16:
5451 case R_PPC64_GOT_TLSGD16_LO:
5452 case R_PPC64_GOT_TLSGD16_HI:
5453 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5454 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5455 goto dogottls;
5456
5457 case R_PPC64_GOT_TPREL16_DS:
5458 case R_PPC64_GOT_TPREL16_LO_DS:
5459 case R_PPC64_GOT_TPREL16_HI:
5460 case R_PPC64_GOT_TPREL16_HA:
0e1862bb 5461 if (bfd_link_pic (info))
411e1bfb
AM
5462 info->flags |= DF_STATIC_TLS;
5463 tls_type = TLS_TLS | TLS_TPREL;
5464 goto dogottls;
5465
5466 case R_PPC64_GOT_DTPREL16_DS:
5467 case R_PPC64_GOT_DTPREL16_LO_DS:
5468 case R_PPC64_GOT_DTPREL16_HI:
5469 case R_PPC64_GOT_DTPREL16_HA:
5470 tls_type = TLS_TLS | TLS_DTPREL;
5471 dogottls:
5472 sec->has_tls_reloc = 1;
1a0670f3 5473 /* Fall through */
411e1bfb 5474
5bd4f169 5475 case R_PPC64_GOT16:
5bd4f169 5476 case R_PPC64_GOT16_DS:
65f38f15
AM
5477 case R_PPC64_GOT16_HA:
5478 case R_PPC64_GOT16_HI:
5479 case R_PPC64_GOT16_LO:
5bd4f169 5480 case R_PPC64_GOT16_LO_DS:
65f38f15 5481 /* This symbol requires a global offset table entry. */
4c52953f 5482 sec->has_toc_reloc = 1;
33c0ec9d
AM
5483 if (r_type == R_PPC64_GOT_TLSLD16
5484 || r_type == R_PPC64_GOT_TLSGD16
5485 || r_type == R_PPC64_GOT_TPREL16_DS
5486 || r_type == R_PPC64_GOT_DTPREL16_DS
5487 || r_type == R_PPC64_GOT16
5488 || r_type == R_PPC64_GOT16_DS)
5489 {
5490 htab->do_multi_toc = 1;
d77c8a4b 5491 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5492 }
5493
e717da7e
AM
5494 if (ppc64_elf_tdata (abfd)->got == NULL
5495 && !create_got_section (abfd, info))
b34976b6 5496 return FALSE;
5bd4f169
AM
5497
5498 if (h != NULL)
5499 {
411e1bfb
AM
5500 struct ppc_link_hash_entry *eh;
5501 struct got_entry *ent;
65f38f15 5502
411e1bfb
AM
5503 eh = (struct ppc_link_hash_entry *) h;
5504 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5505 if (ent->addend == rel->r_addend
e717da7e 5506 && ent->owner == abfd
411e1bfb
AM
5507 && ent->tls_type == tls_type)
5508 break;
5509 if (ent == NULL)
5bd4f169 5510 {
411e1bfb 5511 bfd_size_type amt = sizeof (*ent);
4ce794b7 5512 ent = bfd_alloc (abfd, amt);
411e1bfb 5513 if (ent == NULL)
b34976b6 5514 return FALSE;
411e1bfb
AM
5515 ent->next = eh->elf.got.glist;
5516 ent->addend = rel->r_addend;
e717da7e 5517 ent->owner = abfd;
411e1bfb 5518 ent->tls_type = tls_type;
927be08e 5519 ent->is_indirect = FALSE;
411e1bfb
AM
5520 ent->got.refcount = 0;
5521 eh->elf.got.glist = ent;
5bd4f169 5522 }
411e1bfb 5523 ent->got.refcount += 1;
e7b938ca 5524 eh->tls_mask |= tls_type;
5bd4f169 5525 }
411e1bfb
AM
5526 else
5527 /* This is a global offset table entry for a local symbol. */
5528 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5529 rel->r_addend, tls_type))
5530 return FALSE;
a345bc8d
AM
5531
5532 /* We may also need a plt entry if the symbol turns out to be
5533 an ifunc. */
0e1862bb 5534 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5535 {
5536 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5537 return FALSE;
5538 }
5bd4f169
AM
5539 break;
5540
5bd4f169 5541 case R_PPC64_PLT16_HA:
65f38f15
AM
5542 case R_PPC64_PLT16_HI:
5543 case R_PPC64_PLT16_LO:
5544 case R_PPC64_PLT32:
5545 case R_PPC64_PLT64:
cbf95972
AM
5546 /* This symbol requires a procedure linkage table entry. */
5547 plt_list = ifunc;
5548 if (h != NULL)
e054468f 5549 {
e054468f
AM
5550 h->needs_plt = 1;
5551 if (h->root.root.string[0] == '.'
5552 && h->root.root.string[1] != '\0')
5553 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972
AM
5554 plt_list = &h->plt.plist;
5555 }
5556 if (plt_list == NULL)
5557 {
5558 /* It does not make sense to have a procedure linkage
5559 table entry for a non-ifunc local symbol. */
5560 info->callbacks->einfo
695344c0 5561 /* xgettext:c-format */
cbf95972
AM
5562 (_("%P: %H: %s reloc against local symbol\n"),
5563 abfd, sec, rel->r_offset,
5564 ppc64_elf_howto_table[r_type]->name);
5565 bfd_set_error (bfd_error_bad_value);
5566 return FALSE;
e054468f 5567 }
cbf95972
AM
5568 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5569 return FALSE;
5bd4f169
AM
5570 break;
5571
5572 /* The following relocations don't need to propagate the
5573 relocation if linking a shared object since they are
5574 section relative. */
5575 case R_PPC64_SECTOFF:
5576 case R_PPC64_SECTOFF_LO:
5577 case R_PPC64_SECTOFF_HI:
5578 case R_PPC64_SECTOFF_HA:
5579 case R_PPC64_SECTOFF_DS:
5580 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5581 case R_PPC64_DTPREL16:
5582 case R_PPC64_DTPREL16_LO:
5583 case R_PPC64_DTPREL16_HI:
5584 case R_PPC64_DTPREL16_HA:
5585 case R_PPC64_DTPREL16_DS:
5586 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5587 case R_PPC64_DTPREL16_HIGH:
5588 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5589 case R_PPC64_DTPREL16_HIGHER:
5590 case R_PPC64_DTPREL16_HIGHERA:
5591 case R_PPC64_DTPREL16_HIGHEST:
5592 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5593 break;
5594
ad8e1ba5 5595 /* Nor do these. */
25f23106
AM
5596 case R_PPC64_REL16:
5597 case R_PPC64_REL16_LO:
5598 case R_PPC64_REL16_HI:
5599 case R_PPC64_REL16_HA:
a680de9a 5600 case R_PPC64_REL16DX_HA:
25f23106
AM
5601 break;
5602
45965137
AM
5603 /* Not supported as a dynamic relocation. */
5604 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5605 if (bfd_link_pic (info))
45965137
AM
5606 {
5607 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5608 ppc_howto_init ();
695344c0 5609 /* xgettext:c-format */
45965137
AM
5610 info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5611 "in shared libraries and PIEs.\n"),
5612 abfd, sec, rel->r_offset,
5613 ppc64_elf_howto_table[r_type]->name);
5614 bfd_set_error (bfd_error_bad_value);
5615 return FALSE;
5616 }
5617 break;
5618
ad8e1ba5 5619 case R_PPC64_TOC16:
33c0ec9d
AM
5620 case R_PPC64_TOC16_DS:
5621 htab->do_multi_toc = 1;
d77c8a4b 5622 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5623 /* Fall through. */
ad8e1ba5
AM
5624 case R_PPC64_TOC16_LO:
5625 case R_PPC64_TOC16_HI:
5626 case R_PPC64_TOC16_HA:
ad8e1ba5 5627 case R_PPC64_TOC16_LO_DS:
4c52953f 5628 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5629 break;
5630
006589cf
AM
5631 /* Marker reloc. */
5632 case R_PPC64_ENTRY:
5633 break;
5634
5bd4f169
AM
5635 /* This relocation describes the C++ object vtable hierarchy.
5636 Reconstruct it for later use during GC. */
5637 case R_PPC64_GNU_VTINHERIT:
c152c796 5638 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5639 return FALSE;
5bd4f169
AM
5640 break;
5641
5642 /* This relocation describes which C++ vtable entries are actually
5643 used. Record for later use during GC. */
5644 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5645 BFD_ASSERT (h != NULL);
5646 if (h != NULL
5647 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5648 return FALSE;
5bd4f169
AM
5649 break;
5650
721956f4
AM
5651 case R_PPC64_REL14:
5652 case R_PPC64_REL14_BRTAKEN:
5653 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5654 {
5655 asection *dest = NULL;
5656
5657 /* Heuristic: If jumping outside our section, chances are
5658 we are going to need a stub. */
5659 if (h != NULL)
5660 {
5661 /* If the sym is weak it may be overridden later, so
5662 don't assume we know where a weak sym lives. */
5663 if (h->root.type == bfd_link_hash_defined)
5664 dest = h->root.u.def.section;
5665 }
5666 else
87d72d41
AM
5667 {
5668 Elf_Internal_Sym *isym;
5669
5670 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5671 abfd, r_symndx);
5672 if (isym == NULL)
5673 return FALSE;
5674
5675 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5676 }
5677
220c76dd 5678 if (dest != sec)
7c8fe5c4 5679 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5680 }
721956f4
AM
5681 /* Fall through. */
5682
5d1634d7 5683 case R_PPC64_REL24:
cbf95972
AM
5684 plt_list = ifunc;
5685 if (h != NULL)
5d1634d7 5686 {
e054468f
AM
5687 h->needs_plt = 1;
5688 if (h->root.root.string[0] == '.'
5689 && h->root.root.string[1] != '\0')
5690 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5691
3a71aa26 5692 if (h == tga || h == dottga)
cbf95972
AM
5693 {
5694 sec->has_tls_reloc = 1;
5695 if (rel != relocs
5696 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5697 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5698 /* We have a new-style __tls_get_addr call with
5699 a marker reloc. */
5700 ;
5701 else
5702 /* Mark this section as having an old-style call. */
5703 sec->has_tls_get_addr_call = 1;
5704 }
5705 plt_list = &h->plt.plist;
411e1bfb 5706 }
cbf95972
AM
5707
5708 /* We may need a .plt entry if the function this reloc
5709 refers to is in a shared lib. */
5710 if (plt_list
5711 && !update_plt_info (abfd, plt_list, rel->r_addend))
5712 return FALSE;
411e1bfb
AM
5713 break;
5714
cbf95972
AM
5715 case R_PPC64_ADDR14:
5716 case R_PPC64_ADDR14_BRNTAKEN:
5717 case R_PPC64_ADDR14_BRTAKEN:
5718 case R_PPC64_ADDR24:
5719 goto dodyn;
5720
411e1bfb
AM
5721 case R_PPC64_TPREL64:
5722 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
0e1862bb 5723 if (bfd_link_pic (info))
411e1bfb
AM
5724 info->flags |= DF_STATIC_TLS;
5725 goto dotlstoc;
5726
5727 case R_PPC64_DTPMOD64:
5728 if (rel + 1 < rel_end
5729 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5730 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5731 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5732 else
951fd09b 5733 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5734 goto dotlstoc;
5735
5736 case R_PPC64_DTPREL64:
5737 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5738 if (rel != relocs
5739 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5740 && rel[-1].r_offset == rel->r_offset - 8)
5741 /* This is the second reloc of a dtpmod, dtprel pair.
5742 Don't mark with TLS_DTPREL. */
5743 goto dodyn;
5744
5745 dotlstoc:
5746 sec->has_tls_reloc = 1;
5747 if (h != NULL)
5748 {
5749 struct ppc_link_hash_entry *eh;
5750 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5751 eh->tls_mask |= tls_type;
411e1bfb
AM
5752 }
5753 else
5754 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5755 rel->r_addend, tls_type))
5756 return FALSE;
5757
7c8fe5c4
AM
5758 ppc64_sec = ppc64_elf_section_data (sec);
5759 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5760 {
3a71aa26
AM
5761 bfd_size_type amt;
5762
e7b938ca 5763 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5764 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5765 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5766 if (ppc64_sec->u.toc.symndx == NULL)
5767 return FALSE;
5768 amt = sec->size * sizeof (bfd_vma) / 8;
5769 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5770 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5771 return FALSE;
7c8fe5c4
AM
5772 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5773 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5774 }
5775 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5776 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5777 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5778
5779 /* Mark the second slot of a GD or LD entry.
5780 -1 to indicate GD and -2 to indicate LD. */
5781 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5782 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5783 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5784 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5785 goto dodyn;
5786
5787 case R_PPC64_TPREL16:
5788 case R_PPC64_TPREL16_LO:
5789 case R_PPC64_TPREL16_HI:
5790 case R_PPC64_TPREL16_HA:
5791 case R_PPC64_TPREL16_DS:
5792 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5793 case R_PPC64_TPREL16_HIGH:
5794 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5795 case R_PPC64_TPREL16_HIGHER:
5796 case R_PPC64_TPREL16_HIGHERA:
5797 case R_PPC64_TPREL16_HIGHEST:
5798 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 5799 if (bfd_link_pic (info))
411e1bfb 5800 {
afb93314 5801 info->flags |= DF_STATIC_TLS;
411e1bfb 5802 goto dodyn;
5d1634d7
AM
5803 }
5804 break;
5805
e86ce104 5806 case R_PPC64_ADDR64:
1e2f5b6e 5807 if (opd_sym_map != NULL
1e2f5b6e 5808 && rel + 1 < rel_end
4ce794b7 5809 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5810 {
8387904d
AM
5811 if (h != NULL)
5812 {
5813 if (h->root.root.string[0] == '.'
5814 && h->root.root.string[1] != 0
b31867b6 5815 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5816 ;
5817 else
5818 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5819 }
5820 else
5821 {
5822 asection *s;
87d72d41 5823 Elf_Internal_Sym *isym;
1e2f5b6e 5824
87d72d41
AM
5825 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5826 abfd, r_symndx);
5827 if (isym == NULL)
8387904d 5828 return FALSE;
87d72d41
AM
5829
5830 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5831 if (s != NULL && s != sec)
51aecdc5 5832 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
8387904d 5833 }
1e2f5b6e 5834 }
e86ce104
AM
5835 /* Fall through. */
5836
65f38f15
AM
5837 case R_PPC64_ADDR16:
5838 case R_PPC64_ADDR16_DS:
5839 case R_PPC64_ADDR16_HA:
5840 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5841 case R_PPC64_ADDR16_HIGH:
5842 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5843 case R_PPC64_ADDR16_HIGHER:
5844 case R_PPC64_ADDR16_HIGHERA:
5845 case R_PPC64_ADDR16_HIGHEST:
5846 case R_PPC64_ADDR16_HIGHESTA:
5847 case R_PPC64_ADDR16_LO:
5848 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5849 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5850 && rel->r_addend == 0)
5851 {
5852 /* We may need a .plt entry if this reloc refers to a
5853 function in a shared lib. */
5854 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5855 return FALSE;
5856 h->pointer_equality_needed = 1;
5857 }
5858 /* Fall through. */
5859
5860 case R_PPC64_REL30:
5861 case R_PPC64_REL32:
5862 case R_PPC64_REL64:
65f38f15 5863 case R_PPC64_ADDR32:
65f38f15
AM
5864 case R_PPC64_UADDR16:
5865 case R_PPC64_UADDR32:
5866 case R_PPC64_UADDR64:
5bd4f169 5867 case R_PPC64_TOC:
0e1862bb 5868 if (h != NULL && !bfd_link_pic (info))
81848ca0 5869 /* We may need a copy reloc. */
f5385ebf 5870 h->non_got_ref = 1;
81848ca0 5871
41bd81ab 5872 /* Don't propagate .opd relocs. */
1e2f5b6e 5873 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5874 break;
e86ce104 5875
65f38f15
AM
5876 /* If we are creating a shared library, and this is a reloc
5877 against a global symbol, or a non PC relative reloc
5878 against a local symbol, then we need to copy the reloc
5879 into the shared library. However, if we are linking with
5880 -Bsymbolic, we do not need to copy a reloc against a
5881 global symbol which is defined in an object we are
5882 including in the link (i.e., DEF_REGULAR is set). At
5883 this point we have not seen all the input files, so it is
5884 possible that DEF_REGULAR is not set now but will be set
5885 later (it is never cleared). In case of a weak definition,
5886 DEF_REGULAR may be cleared later by a strong definition in
5887 a shared library. We account for that possibility below by
f4656909 5888 storing information in the dyn_relocs field of the hash
65f38f15
AM
5889 table entry. A similar situation occurs when creating
5890 shared libraries and symbol visibility changes render the
5891 symbol local.
5892
5893 If on the other hand, we are creating an executable, we
5894 may need to keep relocations for symbols satisfied by a
5895 dynamic library if we manage to avoid copy relocs for the
5896 symbol. */
411e1bfb 5897 dodyn:
0e1862bb 5898 if ((bfd_link_pic (info)
1d483afe 5899 && (must_be_dyn_reloc (info, r_type)
65f38f15 5900 || (h != NULL
198f1157 5901 && (!SYMBOLIC_BIND (info, h)
65f38f15 5902 || h->root.type == bfd_link_hash_defweak
f5385ebf 5903 || !h->def_regular))))
f4656909 5904 || (ELIMINATE_COPY_RELOCS
0e1862bb 5905 && !bfd_link_pic (info)
65f38f15
AM
5906 && h != NULL
5907 && (h->root.type == bfd_link_hash_defweak
25f23106 5908 || !h->def_regular))
0e1862bb 5909 || (!bfd_link_pic (info)
25f23106 5910 && ifunc != NULL))
5bd4f169 5911 {
65f38f15
AM
5912 /* We must copy these reloc types into the output file.
5913 Create a reloc section in dynobj and make room for
5914 this reloc. */
5bd4f169
AM
5915 if (sreloc == NULL)
5916 {
83bac4b0
NC
5917 sreloc = _bfd_elf_make_dynamic_reloc_section
5918 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5919
5bd4f169 5920 if (sreloc == NULL)
83bac4b0 5921 return FALSE;
5bd4f169
AM
5922 }
5923
65f38f15
AM
5924 /* If this is a global symbol, we count the number of
5925 relocations we need for this symbol. */
5926 if (h != NULL)
5927 {
19e08130
AM
5928 struct elf_dyn_relocs *p;
5929 struct elf_dyn_relocs **head;
5930
ec338859 5931 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5932 p = *head;
5933 if (p == NULL || p->sec != sec)
5934 {
5935 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5936 if (p == NULL)
5937 return FALSE;
5938 p->next = *head;
5939 *head = p;
5940 p->sec = sec;
5941 p->count = 0;
5942 p->pc_count = 0;
5943 }
5944 p->count += 1;
5945 if (!must_be_dyn_reloc (info, r_type))
5946 p->pc_count += 1;
65f38f15
AM
5947 }
5948 else
5949 {
ec338859
AM
5950 /* Track dynamic relocs needed for local syms too.
5951 We really need local syms available to do this
5952 easily. Oh well. */
19e08130
AM
5953 struct ppc_dyn_relocs *p;
5954 struct ppc_dyn_relocs **head;
5955 bfd_boolean is_ifunc;
ec338859 5956 asection *s;
6edfbbad 5957 void *vpp;
87d72d41 5958 Elf_Internal_Sym *isym;
6edfbbad 5959
87d72d41
AM
5960 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5961 abfd, r_symndx);
5962 if (isym == NULL)
b34976b6 5963 return FALSE;
ec338859 5964
87d72d41
AM
5965 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5966 if (s == NULL)
5967 s = sec;
5968
6edfbbad 5969 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5970 head = (struct ppc_dyn_relocs **) vpp;
5971 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5972 p = *head;
5973 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5974 p = p->next;
5975 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5976 {
5977 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5978 if (p == NULL)
5979 return FALSE;
5980 p->next = *head;
5981 *head = p;
5982 p->sec = sec;
5983 p->ifunc = is_ifunc;
5984 p->count = 0;
5985 }
5986 p->count += 1;
ec338859 5987 }
65f38f15 5988 }
5bd4f169 5989 break;
65f38f15
AM
5990
5991 default:
96e0dda4 5992 break;
5bd4f169
AM
5993 }
5994 }
5995
b34976b6 5996 return TRUE;
5bd4f169
AM
5997}
5998
ee67d69a
AM
5999/* Merge backend specific data from an object file to the output
6000 object file when linking. */
6001
6002static bfd_boolean
50e03d47 6003ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 6004{
50e03d47 6005 bfd *obfd = info->output_bfd;
ee67d69a
AM
6006 unsigned long iflags, oflags;
6007
6008 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6009 return TRUE;
6010
6011 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6012 return TRUE;
6013
50e03d47 6014 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6015 return FALSE;
6016
6017 iflags = elf_elfheader (ibfd)->e_flags;
6018 oflags = elf_elfheader (obfd)->e_flags;
6019
f6c7c3e8 6020 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6021 {
4eca0228 6022 _bfd_error_handler
695344c0 6023 /* xgettext:c-format */
ee67d69a
AM
6024 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6025 bfd_set_error (bfd_error_bad_value);
6026 return FALSE;
6027 }
f6c7c3e8 6028 else if (iflags != oflags && iflags != 0)
ee67d69a 6029 {
4eca0228 6030 _bfd_error_handler
695344c0 6031 /* xgettext:c-format */
ee67d69a
AM
6032 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6033 ibfd, iflags, oflags);
6034 bfd_set_error (bfd_error_bad_value);
6035 return FALSE;
6036 }
6037
50e03d47 6038 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
005d79fd 6039
ee67d69a 6040 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 6041 _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6042
6043 return TRUE;
6044}
6045
6046static bfd_boolean
6047ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6048{
6049 /* Print normal ELF private data. */
6050 _bfd_elf_print_private_bfd_data (abfd, ptr);
6051
6052 if (elf_elfheader (abfd)->e_flags != 0)
6053 {
6054 FILE *file = ptr;
6055
ee67d69a
AM
6056 fprintf (file, _("private flags = 0x%lx:"),
6057 elf_elfheader (abfd)->e_flags);
6058
6059 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6060 fprintf (file, _(" [abiv%ld]"),
6061 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6062 fputc ('\n', file);
6063 }
6064
6065 return TRUE;
6066}
6067
8387904d 6068/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6069 of the code entry point, and its section, which must be in the same
6070 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6071
6072static bfd_vma
6073opd_entry_value (asection *opd_sec,
6074 bfd_vma offset,
6075 asection **code_sec,
aef36ac1
AM
6076 bfd_vma *code_off,
6077 bfd_boolean in_code_sec)
8387904d
AM
6078{
6079 bfd *opd_bfd = opd_sec->owner;
8860955f 6080 Elf_Internal_Rela *relocs;
8387904d 6081 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6082 bfd_vma val;
8387904d 6083
9f296da3
AM
6084 /* No relocs implies we are linking a --just-symbols object, or looking
6085 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6086 if (opd_sec->reloc_count == 0)
6087 {
729eabd5 6088 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6089
729eabd5
AM
6090 if (contents == NULL)
6091 {
6092 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6093 return (bfd_vma) -1;
6094 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6095 }
ee1e4ede 6096
dbb3fbbb 6097 /* PR 17512: file: 64b9dfbb. */
451dfd38 6098 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6099 return (bfd_vma) -1;
6100
729eabd5 6101 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6102 if (code_sec != NULL)
6103 {
6104 asection *sec, *likely = NULL;
ee1e4ede 6105
aef36ac1 6106 if (in_code_sec)
4b85d634 6107 {
aef36ac1
AM
6108 sec = *code_sec;
6109 if (sec->vma <= val
6110 && val < sec->vma + sec->size)
6111 likely = sec;
6112 else
6113 val = -1;
6114 }
6115 else
6116 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6117 if (sec->vma <= val
6118 && (sec->flags & SEC_LOAD) != 0
6119 && (sec->flags & SEC_ALLOC) != 0)
6120 likely = sec;
6121 if (likely != NULL)
6122 {
6123 *code_sec = likely;
6124 if (code_off != NULL)
6125 *code_off = val - likely->vma;
4b85d634
AM
6126 }
6127 }
aef36ac1 6128 return val;
4b85d634
AM
6129 }
6130
0c8d6e5c 6131 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6132
729eabd5 6133 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6134 if (relocs == NULL)
6135 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6136 /* PR 17512: file: df8e1fd6. */
6137 if (relocs == NULL)
6138 return (bfd_vma) -1;
645ea6a9 6139
8387904d 6140 /* Go find the opd reloc at the sym address. */
8860955f 6141 lo = relocs;
8387904d 6142 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6143 val = (bfd_vma) -1;
8387904d
AM
6144 while (lo < hi)
6145 {
6146 look = lo + (hi - lo) / 2;
6147 if (look->r_offset < offset)
6148 lo = look + 1;
6149 else if (look->r_offset > offset)
6150 hi = look;
6151 else
6152 {
0ffa91dd
NC
6153 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6154
8387904d
AM
6155 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6156 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6157 {
6158 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6159 asection *sec = NULL;
8387904d 6160
b53dfeb2
AM
6161 if (symndx >= symtab_hdr->sh_info
6162 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6163 {
6164 struct elf_link_hash_entry **sym_hashes;
6165 struct elf_link_hash_entry *rh;
6166
6167 sym_hashes = elf_sym_hashes (opd_bfd);
6168 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6169 if (rh != NULL)
6170 {
6171 rh = elf_follow_link (rh);
bb854a36
AM
6172 if (rh->root.type != bfd_link_hash_defined
6173 && rh->root.type != bfd_link_hash_defweak)
6174 break;
6175 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6176 {
bb854a36
AM
6177 val = rh->root.u.def.value;
6178 sec = rh->root.u.def.section;
b53dfeb2
AM
6179 }
6180 }
6181 }
6182
6183 if (sec == NULL)
6184 {
6185 Elf_Internal_Sym *sym;
6186
6187 if (symndx < symtab_hdr->sh_info)
6188 {
6189 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6190 if (sym == NULL)
6191 {
6192 size_t symcnt = symtab_hdr->sh_info;
6193 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6194 symcnt, 0,
6195 NULL, NULL, NULL);
6196 if (sym == NULL)
6197 break;
6198 symtab_hdr->contents = (bfd_byte *) sym;
6199 }
6200 sym += symndx;
128205bb
AM
6201 }
6202 else
6203 {
b53dfeb2
AM
6204 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6205 1, symndx,
6206 NULL, NULL, NULL);
128205bb
AM
6207 if (sym == NULL)
6208 break;
128205bb 6209 }
b53dfeb2
AM
6210 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6211 if (sec == NULL)
6212 break;
6213 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6214 val = sym->st_value;
8387904d 6215 }
b53dfeb2 6216
8387904d
AM
6217 val += look->r_addend;
6218 if (code_off != NULL)
6219 *code_off = val;
6220 if (code_sec != NULL)
aef36ac1
AM
6221 {
6222 if (in_code_sec && *code_sec != sec)
6223 return -1;
6224 else
6225 *code_sec = sec;
6226 }
b53dfeb2 6227 if (sec->output_section != NULL)
8387904d 6228 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6229 }
6230 break;
6231 }
6232 }
645ea6a9 6233
645ea6a9 6234 return val;
8387904d
AM
6235}
6236
aef36ac1
AM
6237/* If the ELF symbol SYM might be a function in SEC, return the
6238 function size and set *CODE_OFF to the function's entry point,
6239 otherwise return zero. */
9f296da3 6240
aef36ac1
AM
6241static bfd_size_type
6242ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6243 bfd_vma *code_off)
9f296da3 6244{
aef36ac1
AM
6245 bfd_size_type size;
6246
6247 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6248 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6249 return 0;
6250
6251 size = 0;
6252 if (!(sym->flags & BSF_SYNTHETIC))
6253 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6254
6255 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6256 {
b07bca4e
AM
6257 struct _opd_sec_data *opd = get_opd_info (sym->section);
6258 bfd_vma symval = sym->value;
6259
6260 if (opd != NULL
6261 && opd->adjust != NULL
6262 && elf_section_data (sym->section)->relocs != NULL)
6263 {
6264 /* opd_entry_value will use cached relocs that have been
6265 adjusted, but with raw symbols. That means both local
6266 and global symbols need adjusting. */
6267 long adjust = opd->adjust[OPD_NDX (symval)];
6268 if (adjust == -1)
6269 return 0;
6270 symval += adjust;
6271 }
6272
6273 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6274 &sec, code_off, TRUE) == (bfd_vma) -1)
6275 return 0;
6276 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6277 symbol. This size has nothing to do with the code size of the
6278 function, which is what we're supposed to return, but the
6279 code size isn't available without looking up the dot-sym.
6280 However, doing that would be a waste of time particularly
6281 since elf_find_function will look at the dot-sym anyway.
6282 Now, elf_find_function will keep the largest size of any
6283 function sym found at the code address of interest, so return
6284 1 here to avoid it incorrectly caching a larger function size
6285 for a small function. This does mean we return the wrong
6286 size for a new-ABI function of size 24, but all that does is
6287 disable caching for such functions. */
6288 if (size == 24)
6289 size = 1;
9f296da3 6290 }
aef36ac1
AM
6291 else
6292 {
6293 if (sym->section != sec)
6294 return 0;
6295 *code_off = sym->value;
6296 }
6297 if (size == 0)
6298 size = 1;
6299 return size;
9f296da3
AM
6300}
6301
854b41e7
AM
6302/* Return true if symbol is defined in a regular object file. */
6303
6304static bfd_boolean
6305is_static_defined (struct elf_link_hash_entry *h)
6306{
6307 return ((h->root.type == bfd_link_hash_defined
6308 || h->root.type == bfd_link_hash_defweak)
6309 && h->root.u.def.section != NULL
6310 && h->root.u.def.section->output_section != NULL);
6311}
6312
b31867b6
AM
6313/* If FDH is a function descriptor symbol, return the associated code
6314 entry symbol if it is defined. Return NULL otherwise. */
6315
6316static struct ppc_link_hash_entry *
6317defined_code_entry (struct ppc_link_hash_entry *fdh)
6318{
6319 if (fdh->is_func_descriptor)
6320 {
6321 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6322 if (fh->elf.root.type == bfd_link_hash_defined
6323 || fh->elf.root.type == bfd_link_hash_defweak)
6324 return fh;
6325 }
6326 return NULL;
6327}
6328
6329/* If FH is a function code entry symbol, return the associated
6330 function descriptor symbol if it is defined. Return NULL otherwise. */
6331
6332static struct ppc_link_hash_entry *
6333defined_func_desc (struct ppc_link_hash_entry *fh)
6334{
6335 if (fh->oh != NULL
6336 && fh->oh->is_func_descriptor)
6337 {
6338 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6339 if (fdh->elf.root.type == bfd_link_hash_defined
6340 || fdh->elf.root.type == bfd_link_hash_defweak)
6341 return fdh;
6342 }
6343 return NULL;
6344}
6345
74f0fb50
AM
6346/* Mark all our entry sym sections, both opd and code section. */
6347
6348static void
6349ppc64_elf_gc_keep (struct bfd_link_info *info)
6350{
6351 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6352 struct bfd_sym_chain *sym;
6353
4dfe6ac6
NC
6354 if (htab == NULL)
6355 return;
6356
74f0fb50
AM
6357 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6358 {
b31867b6 6359 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6360 asection *sec;
6361
6362 eh = (struct ppc_link_hash_entry *)
b31867b6 6363 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6364 if (eh == NULL)
6365 continue;
6366 if (eh->elf.root.type != bfd_link_hash_defined
6367 && eh->elf.root.type != bfd_link_hash_defweak)
6368 continue;
6369
b31867b6
AM
6370 fh = defined_code_entry (eh);
6371 if (fh != NULL)
74f0fb50 6372 {
b31867b6 6373 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6374 sec->flags |= SEC_KEEP;
6375 }
6376 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6377 && opd_entry_value (eh->elf.root.u.def.section,
6378 eh->elf.root.u.def.value,
aef36ac1 6379 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6380 sec->flags |= SEC_KEEP;
6381
6382 sec = eh->elf.root.u.def.section;
6383 sec->flags |= SEC_KEEP;
6384 }
6385}
6386
64d03ab5
AM
6387/* Mark sections containing dynamically referenced symbols. When
6388 building shared libraries, we must assume that any visible symbol is
6389 referenced. */
6390
6391static bfd_boolean
6392ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6393{
6394 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6395 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6396 struct ppc_link_hash_entry *fdh;
b407645f 6397 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6398
64d03ab5 6399 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6400 fdh = defined_func_desc (eh);
6401 if (fdh != NULL)
6402 eh = fdh;
64d03ab5
AM
6403
6404 if ((eh->elf.root.type == bfd_link_hash_defined
6405 || eh->elf.root.type == bfd_link_hash_defweak)
6406 && (eh->elf.ref_dynamic
1c9177d9 6407 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6408 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6409 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6410 && (!bfd_link_executable (info)
b407645f
AM
6411 || info->export_dynamic
6412 || (eh->elf.dynamic
6413 && d != NULL
6414 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
4c58e0d8
AM
6415 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6416 || !bfd_hide_sym_by_version (info->version_info,
6417 eh->elf.root.root.string)))))
64d03ab5
AM
6418 {
6419 asection *code_sec;
b31867b6 6420 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6421
6422 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6423
6424 /* Function descriptor syms cause the associated
6425 function code sym section to be marked. */
b31867b6
AM
6426 fh = defined_code_entry (eh);
6427 if (fh != NULL)
6428 {
6429 code_sec = fh->elf.root.u.def.section;
6430 code_sec->flags |= SEC_KEEP;
6431 }
64d03ab5
AM
6432 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6433 && opd_entry_value (eh->elf.root.u.def.section,
6434 eh->elf.root.u.def.value,
aef36ac1 6435 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6436 code_sec->flags |= SEC_KEEP;
6437 }
6438
6439 return TRUE;
6440}
6441
5bd4f169
AM
6442/* Return the section that should be marked against GC for a given
6443 relocation. */
6444
6445static asection *
4ce794b7 6446ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6447 struct bfd_link_info *info,
4ce794b7
AM
6448 Elf_Internal_Rela *rel,
6449 struct elf_link_hash_entry *h,
6450 Elf_Internal_Sym *sym)
5bd4f169 6451{
ccfa59ea
AM
6452 asection *rsec;
6453
ccfa59ea
AM
6454 /* Syms return NULL if we're marking .opd, so we avoid marking all
6455 function sections, as all functions are referenced in .opd. */
6456 rsec = NULL;
6457 if (get_opd_info (sec) != NULL)
6458 return rsec;
1e2f5b6e 6459
5bd4f169
AM
6460 if (h != NULL)
6461 {
04c9666a 6462 enum elf_ppc64_reloc_type r_type;
b31867b6 6463 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6464
4ce794b7 6465 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6466 switch (r_type)
5bd4f169
AM
6467 {
6468 case R_PPC64_GNU_VTINHERIT:
6469 case R_PPC64_GNU_VTENTRY:
6470 break;
6471
6472 default:
6473 switch (h->root.type)
6474 {
6475 case bfd_link_hash_defined:
6476 case bfd_link_hash_defweak:
ccfa59ea 6477 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6478 fdh = defined_func_desc (eh);
6479 if (fdh != NULL)
6480 eh = fdh;
1e2f5b6e
AM
6481
6482 /* Function descriptor syms cause the associated
6483 function code sym section to be marked. */
b31867b6
AM
6484 fh = defined_code_entry (eh);
6485 if (fh != NULL)
ccfa59ea
AM
6486 {
6487 /* They also mark their opd section. */
74f0fb50 6488 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6489
b31867b6 6490 rsec = fh->elf.root.u.def.section;
ccfa59ea 6491 }
8387904d
AM
6492 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6493 && opd_entry_value (eh->elf.root.u.def.section,
6494 eh->elf.root.u.def.value,
aef36ac1 6495 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6496 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6497 else
1e2f5b6e
AM
6498 rsec = h->root.u.def.section;
6499 break;
5bd4f169
AM
6500
6501 case bfd_link_hash_common:
1e2f5b6e
AM
6502 rsec = h->root.u.c.p->section;
6503 break;
5bd4f169
AM
6504
6505 default:
fb34365b 6506 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6507 }
6508 }
6509 }
6510 else
6511 {
74f0fb50 6512 struct _opd_sec_data *opd;
1e2f5b6e
AM
6513
6514 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6515 opd = get_opd_info (rsec);
6516 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6517 {
74f0fb50 6518 rsec->gc_mark = 1;
ccfa59ea 6519
51aecdc5 6520 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6521 }
5bd4f169
AM
6522 }
6523
1e2f5b6e 6524 return rsec;
5bd4f169
AM
6525}
6526
65f38f15
AM
6527/* Update the .got, .plt. and dynamic reloc reference counts for the
6528 section being removed. */
5bd4f169 6529
b34976b6 6530static bfd_boolean
4ce794b7
AM
6531ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6532 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 6533{
411e1bfb 6534 struct ppc_link_hash_table *htab;
5bd4f169
AM
6535 Elf_Internal_Shdr *symtab_hdr;
6536 struct elf_link_hash_entry **sym_hashes;
411e1bfb 6537 struct got_entry **local_got_ents;
5bd4f169 6538 const Elf_Internal_Rela *rel, *relend;
5bd4f169 6539
0e1862bb 6540 if (bfd_link_relocatable (info))
7dda2462
TG
6541 return TRUE;
6542
680a3378
AM
6543 if ((sec->flags & SEC_ALLOC) == 0)
6544 return TRUE;
6545
ec338859
AM
6546 elf_section_data (sec)->local_dynrel = NULL;
6547
411e1bfb 6548 htab = ppc_hash_table (info);
4dfe6ac6
NC
6549 if (htab == NULL)
6550 return FALSE;
6551
0ffa91dd 6552 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 6553 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 6554 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
6555
6556 relend = relocs + sec->reloc_count;
6557 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
6558 {
6559 unsigned long r_symndx;
04c9666a 6560 enum elf_ppc64_reloc_type r_type;
58ac9f71 6561 struct elf_link_hash_entry *h = NULL;
cbf95972 6562 struct plt_entry **plt_list;
f961d9dd 6563 unsigned char tls_type = 0;
5bd4f169 6564
a33d1f77 6565 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 6566 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
6567 if (r_symndx >= symtab_hdr->sh_info)
6568 {
6569 struct ppc_link_hash_entry *eh;
6061a67d
AM
6570 struct elf_dyn_relocs **pp;
6571 struct elf_dyn_relocs *p;
58ac9f71
AM
6572
6573 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6574 h = elf_follow_link (h);
58ac9f71
AM
6575 eh = (struct ppc_link_hash_entry *) h;
6576
6577 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6578 if (p->sec == sec)
6579 {
6580 /* Everything must go for SEC. */
6581 *pp = p->next;
6582 break;
6583 }
6584 }
6585
a33d1f77
AM
6586 switch (r_type)
6587 {
411e1bfb
AM
6588 case R_PPC64_GOT_TLSLD16:
6589 case R_PPC64_GOT_TLSLD16_LO:
6590 case R_PPC64_GOT_TLSLD16_HI:
6591 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 6592 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6593 goto dogot;
6594
6595 case R_PPC64_GOT_TLSGD16:
6596 case R_PPC64_GOT_TLSGD16_LO:
6597 case R_PPC64_GOT_TLSGD16_HI:
6598 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6599 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6600 goto dogot;
6601
6602 case R_PPC64_GOT_TPREL16_DS:
6603 case R_PPC64_GOT_TPREL16_LO_DS:
6604 case R_PPC64_GOT_TPREL16_HI:
6605 case R_PPC64_GOT_TPREL16_HA:
6606 tls_type = TLS_TLS | TLS_TPREL;
6607 goto dogot;
6608
6609 case R_PPC64_GOT_DTPREL16_DS:
6610 case R_PPC64_GOT_DTPREL16_LO_DS:
6611 case R_PPC64_GOT_DTPREL16_HI:
6612 case R_PPC64_GOT_DTPREL16_HA:
6613 tls_type = TLS_TLS | TLS_DTPREL;
6614 goto dogot;
6615
a33d1f77
AM
6616 case R_PPC64_GOT16:
6617 case R_PPC64_GOT16_DS:
6618 case R_PPC64_GOT16_HA:
6619 case R_PPC64_GOT16_HI:
6620 case R_PPC64_GOT16_LO:
6621 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
6622 dogot:
6623 {
6624 struct got_entry *ent;
6625
58ac9f71
AM
6626 if (h != NULL)
6627 ent = h->got.glist;
411e1bfb
AM
6628 else
6629 ent = local_got_ents[r_symndx];
6630
6631 for (; ent != NULL; ent = ent->next)
6632 if (ent->addend == rel->r_addend
e717da7e 6633 && ent->owner == abfd
411e1bfb
AM
6634 && ent->tls_type == tls_type)
6635 break;
6636 if (ent == NULL)
6637 abort ();
6638 if (ent->got.refcount > 0)
6639 ent->got.refcount -= 1;
6640 }
a33d1f77 6641 break;
65f38f15 6642
a33d1f77
AM
6643 case R_PPC64_PLT16_HA:
6644 case R_PPC64_PLT16_HI:
6645 case R_PPC64_PLT16_LO:
6646 case R_PPC64_PLT32:
6647 case R_PPC64_PLT64:
721956f4
AM
6648 case R_PPC64_REL14:
6649 case R_PPC64_REL14_BRNTAKEN:
6650 case R_PPC64_REL14_BRTAKEN:
5d1634d7 6651 case R_PPC64_REL24:
cbf95972 6652 plt_list = NULL;
58ac9f71 6653 if (h != NULL)
cbf95972
AM
6654 plt_list = &h->plt.plist;
6655 else if (local_got_ents != NULL)
6656 {
6657 struct plt_entry **local_plt = (struct plt_entry **)
6658 (local_got_ents + symtab_hdr->sh_info);
6659 unsigned char *local_got_tls_masks = (unsigned char *)
6660 (local_plt + symtab_hdr->sh_info);
6661 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6662 plt_list = local_plt + r_symndx;
6663 }
6664 if (plt_list)
5d1634d7 6665 {
411e1bfb
AM
6666 struct plt_entry *ent;
6667
cbf95972 6668 for (ent = *plt_list; ent != NULL; ent = ent->next)
411e1bfb
AM
6669 if (ent->addend == rel->r_addend)
6670 break;
7c6c1722 6671 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 6672 ent->plt.refcount -= 1;
5d1634d7 6673 }
e86ce104 6674 break;
5d1634d7 6675
a33d1f77
AM
6676 default:
6677 break;
6678 }
6679 }
b34976b6 6680 return TRUE;
5bd4f169
AM
6681}
6682
deb0e272
AM
6683/* The maximum size of .sfpr. */
6684#define SFPR_MAX (218*4)
6685
6686struct sfpr_def_parms
6687{
699733f6
AM
6688 const char name[12];
6689 unsigned char lo, hi;
deb0e272
AM
6690 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6691 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6692};
6693
a4b6fadd
AM
6694/* Auto-generate _save*, _rest* functions in .sfpr.
6695 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6696 instead. */
deb0e272 6697
4dfe6ac6 6698static bfd_boolean
a4b6fadd
AM
6699sfpr_define (struct bfd_link_info *info,
6700 const struct sfpr_def_parms *parm,
6701 asection *stub_sec)
deb0e272
AM
6702{
6703 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6704 unsigned int i;
6705 size_t len = strlen (parm->name);
6706 bfd_boolean writing = FALSE;
699733f6 6707 char sym[16];
deb0e272 6708
4dfe6ac6
NC
6709 if (htab == NULL)
6710 return FALSE;
6711
deb0e272
AM
6712 memcpy (sym, parm->name, len);
6713 sym[len + 2] = 0;
6714
6715 for (i = parm->lo; i <= parm->hi; i++)
6716 {
a4b6fadd 6717 struct ppc_link_hash_entry *h;
deb0e272
AM
6718
6719 sym[len + 0] = i / 10 + '0';
6720 sym[len + 1] = i % 10 + '0';
a4b6fadd 6721 h = (struct ppc_link_hash_entry *)
b32547cd 6722 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6723 if (stub_sec != NULL)
deb0e272 6724 {
a4b6fadd
AM
6725 if (h != NULL
6726 && h->elf.root.type == bfd_link_hash_defined
6727 && h->elf.root.u.def.section == htab->sfpr)
6728 {
6729 struct elf_link_hash_entry *s;
6730 char buf[32];
6731 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6732 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6733 if (s == NULL)
6734 return FALSE;
6735 if (s->root.type == bfd_link_hash_new
6736 || (s->root.type = bfd_link_hash_defined
6737 && s->root.u.def.section == stub_sec))
6738 {
6739 s->root.type = bfd_link_hash_defined;
6740 s->root.u.def.section = stub_sec;
6741 s->root.u.def.value = (stub_sec->size
6742 + h->elf.root.u.def.value);
6743 s->ref_regular = 1;
6744 s->def_regular = 1;
6745 s->ref_regular_nonweak = 1;
6746 s->forced_local = 1;
6747 s->non_elf = 0;
6748 s->root.linker_def = 1;
6749 }
6750 }
6751 continue;
6752 }
6753 if (h != NULL)
6754 {
6755 h->save_res = 1;
6756 if (!h->elf.def_regular)
deb0e272 6757 {
a4b6fadd
AM
6758 h->elf.root.type = bfd_link_hash_defined;
6759 h->elf.root.u.def.section = htab->sfpr;
6760 h->elf.root.u.def.value = htab->sfpr->size;
6761 h->elf.type = STT_FUNC;
6762 h->elf.def_regular = 1;
b32547cd 6763 h->elf.non_elf = 0;
a4b6fadd
AM
6764 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6765 writing = TRUE;
deb0e272 6766 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6767 {
6768 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6769 if (htab->sfpr->contents == NULL)
6770 return FALSE;
6771 }
deb0e272
AM
6772 }
6773 }
6774 if (writing)
6775 {
6776 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6777 if (i != parm->hi)
6778 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6779 else
6780 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6781 htab->sfpr->size = p - htab->sfpr->contents;
6782 }
6783 }
6784
6785 return TRUE;
6786}
6787
6788static bfd_byte *
6789savegpr0 (bfd *abfd, bfd_byte *p, int r)
6790{
6791 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6792 return p + 4;
6793}
6794
6795static bfd_byte *
6796savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6797{
6798 p = savegpr0 (abfd, p, r);
a078d95a 6799 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6800 p = p + 4;
6801 bfd_put_32 (abfd, BLR, p);
6802 return p + 4;
6803}
6804
6805static bfd_byte *
6806restgpr0 (bfd *abfd, bfd_byte *p, int r)
6807{
6808 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6809 return p + 4;
6810}
6811
6812static bfd_byte *
6813restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6814{
a078d95a 6815 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6816 p = p + 4;
6817 p = restgpr0 (abfd, p, r);
6818 bfd_put_32 (abfd, MTLR_R0, p);
6819 p = p + 4;
6820 if (r == 29)
6821 {
6822 p = restgpr0 (abfd, p, 30);
6823 p = restgpr0 (abfd, p, 31);
6824 }
6825 bfd_put_32 (abfd, BLR, p);
6826 return p + 4;
6827}
6828
6829static bfd_byte *
6830savegpr1 (bfd *abfd, bfd_byte *p, int r)
6831{
6832 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6833 return p + 4;
6834}
6835
6836static bfd_byte *
6837savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6838{
6839 p = savegpr1 (abfd, p, r);
6840 bfd_put_32 (abfd, BLR, p);
6841 return p + 4;
6842}
6843
6844static bfd_byte *
6845restgpr1 (bfd *abfd, bfd_byte *p, int r)
6846{
6847 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6848 return p + 4;
6849}
6850
6851static bfd_byte *
6852restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6853{
6854 p = restgpr1 (abfd, p, r);
6855 bfd_put_32 (abfd, BLR, p);
6856 return p + 4;
6857}
6858
6859static bfd_byte *
6860savefpr (bfd *abfd, bfd_byte *p, int r)
6861{
6862 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6863 return p + 4;
6864}
6865
6866static bfd_byte *
6867savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6868{
6869 p = savefpr (abfd, p, r);
a078d95a 6870 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6871 p = p + 4;
6872 bfd_put_32 (abfd, BLR, p);
6873 return p + 4;
6874}
6875
6876static bfd_byte *
6877restfpr (bfd *abfd, bfd_byte *p, int r)
6878{
6879 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6880 return p + 4;
6881}
6882
6883static bfd_byte *
6884restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6885{
a078d95a 6886 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6887 p = p + 4;
6888 p = restfpr (abfd, p, r);
6889 bfd_put_32 (abfd, MTLR_R0, p);
6890 p = p + 4;
6891 if (r == 29)
6892 {
6893 p = restfpr (abfd, p, 30);
6894 p = restfpr (abfd, p, 31);
6895 }
6896 bfd_put_32 (abfd, BLR, p);
6897 return p + 4;
6898}
6899
6900static bfd_byte *
6901savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6902{
6903 p = savefpr (abfd, p, r);
6904 bfd_put_32 (abfd, BLR, p);
6905 return p + 4;
6906}
6907
6908static bfd_byte *
6909restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6910{
6911 p = restfpr (abfd, p, r);
6912 bfd_put_32 (abfd, BLR, p);
6913 return p + 4;
6914}
6915
6916static bfd_byte *
6917savevr (bfd *abfd, bfd_byte *p, int r)
6918{
6919 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6920 p = p + 4;
6921 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6922 return p + 4;
6923}
6924
6925static bfd_byte *
6926savevr_tail (bfd *abfd, bfd_byte *p, int r)
6927{
6928 p = savevr (abfd, p, r);
6929 bfd_put_32 (abfd, BLR, p);
6930 return p + 4;
6931}
6932
6933static bfd_byte *
6934restvr (bfd *abfd, bfd_byte *p, int r)
6935{
6936 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6937 p = p + 4;
6938 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6939 return p + 4;
6940}
6941
6942static bfd_byte *
6943restvr_tail (bfd *abfd, bfd_byte *p, int r)
6944{
6945 p = restvr (abfd, p, r);
6946 bfd_put_32 (abfd, BLR, p);
6947 return p + 4;
6948}
6949
e86ce104
AM
6950/* Called via elf_link_hash_traverse to transfer dynamic linking
6951 information on function code symbol entries to their corresponding
6952 function descriptor symbol entries. */
deb0e272 6953
b34976b6 6954static bfd_boolean
4ce794b7 6955func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6956{
e86ce104 6957 struct bfd_link_info *info;
65f38f15 6958 struct ppc_link_hash_table *htab;
411e1bfb 6959 struct plt_entry *ent;
50bc7936
AM
6960 struct ppc_link_hash_entry *fh;
6961 struct ppc_link_hash_entry *fdh;
6962 bfd_boolean force_local;
5bd4f169 6963
50bc7936
AM
6964 fh = (struct ppc_link_hash_entry *) h;
6965 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6966 return TRUE;
e86ce104 6967
4ce794b7 6968 info = inf;
65f38f15 6969 htab = ppc_hash_table (info);
4dfe6ac6
NC
6970 if (htab == NULL)
6971 return FALSE;
5bd4f169 6972
c09bdfe5
AM
6973 /* Resolve undefined references to dot-symbols as the value
6974 in the function descriptor, if we have one in a regular object.
6975 This is to satisfy cases like ".quad .foo". Calls to functions
6976 in dynamic objects are handled elsewhere. */
6977 if (fh->elf.root.type == bfd_link_hash_undefweak
6978 && fh->was_undefined
b31867b6
AM
6979 && (fdh = defined_func_desc (fh)) != NULL
6980 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6981 && opd_entry_value (fdh->elf.root.u.def.section,
6982 fdh->elf.root.u.def.value,
c09bdfe5 6983 &fh->elf.root.u.def.section,
aef36ac1 6984 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6985 {
b31867b6 6986 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6987 fh->elf.forced_local = 1;
b31867b6
AM
6988 fh->elf.def_regular = fdh->elf.def_regular;
6989 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6990 }
6991
e86ce104
AM
6992 /* If this is a function code symbol, transfer dynamic linking
6993 information to the function descriptor symbol. */
50bc7936 6994 if (!fh->is_func)
b34976b6 6995 return TRUE;
e86ce104 6996
50bc7936 6997 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6998 if (ent->plt.refcount > 0)
6999 break;
50bc7936
AM
7000 if (ent == NULL
7001 || fh->elf.root.root.string[0] != '.'
7002 || fh->elf.root.root.string[1] == '\0')
7003 return TRUE;
5bd4f169 7004
50bc7936
AM
7005 /* Find the corresponding function descriptor symbol. Create it
7006 as undefined if necessary. */
5bd4f169 7007
b31867b6 7008 fdh = lookup_fdh (fh, htab);
50bc7936 7009 if (fdh == NULL
0e1862bb 7010 && !bfd_link_executable (info)
50bc7936
AM
7011 && (fh->elf.root.type == bfd_link_hash_undefined
7012 || fh->elf.root.type == bfd_link_hash_undefweak))
7013 {
908b32fc 7014 fdh = make_fdh (info, fh);
bb700d78
AM
7015 if (fdh == NULL)
7016 return FALSE;
50bc7936 7017 }
648cca2c 7018
908b32fc 7019 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
7020 code symbol is strong undefined, make the fake sym the same.
7021 If the function code symbol is defined, then force the fake
7022 descriptor local; We can't support overriding of symbols in a
7023 shared library on a fake descriptor. */
908b32fc
AM
7024
7025 if (fdh != NULL
7026 && fdh->fake
433817dd 7027 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 7028 {
433817dd
AM
7029 if (fh->elf.root.type == bfd_link_hash_undefined)
7030 {
7031 fdh->elf.root.type = bfd_link_hash_undefined;
7032 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
7033 }
7034 else if (fh->elf.root.type == bfd_link_hash_defined
7035 || fh->elf.root.type == bfd_link_hash_defweak)
7036 {
7037 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7038 }
908b32fc
AM
7039 }
7040
50bc7936 7041 if (fdh != NULL
f5385ebf 7042 && !fdh->elf.forced_local
0e1862bb 7043 && (!bfd_link_executable (info)
f5385ebf
AM
7044 || fdh->elf.def_dynamic
7045 || fdh->elf.ref_dynamic
50bc7936
AM
7046 || (fdh->elf.root.type == bfd_link_hash_undefweak
7047 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
7048 {
7049 if (fdh->elf.dynindx == -1)
c152c796 7050 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 7051 return FALSE;
f5385ebf
AM
7052 fdh->elf.ref_regular |= fh->elf.ref_regular;
7053 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7054 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7055 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 7056 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 7057 {
40d16e0b 7058 move_plt_plist (fh, fdh);
f5385ebf 7059 fdh->elf.needs_plt = 1;
e86ce104 7060 }
50bc7936 7061 fdh->is_func_descriptor = 1;
34814b9f
AM
7062 fdh->oh = fh;
7063 fh->oh = fdh;
e86ce104
AM
7064 }
7065
50bc7936
AM
7066 /* Now that the info is on the function descriptor, clear the
7067 function code sym info. Any function code syms for which we
7068 don't have a definition in a regular file, we force local.
7069 This prevents a shared library from exporting syms that have
7070 been imported from another library. Function code syms that
7071 are really in the library we must leave global to prevent the
7072 linker dragging in a definition from a static library. */
93f3fa99
AM
7073 force_local = (!fh->elf.def_regular
7074 || fdh == NULL
7075 || !fdh->elf.def_regular
7076 || fdh->elf.forced_local);
50bc7936
AM
7077 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7078
b34976b6 7079 return TRUE;
e86ce104 7080}
40b8271b 7081
a4b6fadd
AM
7082static const struct sfpr_def_parms save_res_funcs[] =
7083 {
7084 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7085 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7086 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7087 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7088 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7089 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7090 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7091 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7092 { "._savef", 14, 31, savefpr, savefpr1_tail },
7093 { "._restf", 14, 31, restfpr, restfpr1_tail },
7094 { "_savevr_", 20, 31, savevr, savevr_tail },
7095 { "_restvr_", 20, 31, restvr, restvr_tail }
7096 };
7097
e86ce104 7098/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
7099 this hook to a) provide some gcc support functions, and b) transfer
7100 dynamic linking information gathered so far on function code symbol
7101 entries, to their corresponding function descriptor symbol entries. */
deb0e272 7102
b34976b6 7103static bfd_boolean
4ce794b7
AM
7104ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7105 struct bfd_link_info *info)
e86ce104
AM
7106{
7107 struct ppc_link_hash_table *htab;
7108
7109 htab = ppc_hash_table (info);
4dfe6ac6
NC
7110 if (htab == NULL)
7111 return FALSE;
7112
b32547cd
AM
7113 /* Provide any missing _save* and _rest* functions. */
7114 if (htab->sfpr != NULL)
7115 {
7116 unsigned int i;
7117
7118 htab->sfpr->size = 0;
7119 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7120 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7121 return FALSE;
7122 if (htab->sfpr->size == 0)
7123 htab->sfpr->flags |= SEC_EXCLUDE;
7124 }
7125
7126 if (bfd_link_relocatable (info))
7127 return TRUE;
7128
7129 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7130 {
7131 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7132 /* Make .TOC. defined so as to prevent it being made dynamic.
7133 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7134 if (!htab->elf.hgot->def_regular
7135 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7136 {
7137 htab->elf.hgot->root.type = bfd_link_hash_defined;
7138 htab->elf.hgot->root.u.def.value = 0;
7139 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7140 htab->elf.hgot->def_regular = 1;
7141 htab->elf.hgot->root.linker_def = 1;
7142 }
dba6fa9b 7143 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7144 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7145 | STV_HIDDEN);
7146 }
c66bb0ee 7147
4ce794b7 7148 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 7149
b34976b6 7150 return TRUE;
e86ce104
AM
7151}
7152
8a2058b5
AM
7153/* Return true if we have dynamic relocs against H that apply to
7154 read-only sections. */
a345bc8d
AM
7155
7156static bfd_boolean
7157readonly_dynrelocs (struct elf_link_hash_entry *h)
7158{
7159 struct ppc_link_hash_entry *eh;
7160 struct elf_dyn_relocs *p;
7161
7162 eh = (struct ppc_link_hash_entry *) h;
7163 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7164 {
7165 asection *s = p->sec->output_section;
7166
7167 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7168 return TRUE;
7169 }
7170 return FALSE;
7171}
7172
d311bc8b
AM
7173/* Return true if we have dynamic relocs against H or any of its weak
7174 aliases, that apply to read-only sections. */
7175
7176static bfd_boolean
7177alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7178{
7179 struct ppc_link_hash_entry *eh;
7180
7181 eh = (struct ppc_link_hash_entry *) h;
7182 do
7183 {
7184 if (readonly_dynrelocs (&eh->elf))
7185 return TRUE;
7186 eh = eh->weakref;
7187 } while (eh != NULL && &eh->elf != h);
7188
7189 return FALSE;
7190}
8a2058b5 7191
8a9e8e72
AM
7192/* Return whether EH has pc-relative dynamic relocs. */
7193
7194static bfd_boolean
7195pc_dynrelocs (struct ppc_link_hash_entry *eh)
7196{
7197 struct elf_dyn_relocs *p;
7198
7199 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7200 if (p->pc_count != 0)
7201 return TRUE;
7202 return FALSE;
7203}
7204
8a2058b5
AM
7205/* Return true if a global entry stub will be created for H. Valid
7206 for ELFv2 before plt entries have been allocated. */
7207
7208static bfd_boolean
7209global_entry_stub (struct elf_link_hash_entry *h)
7210{
7211 struct plt_entry *pent;
7212
7213 if (!h->pointer_equality_needed
7214 || h->def_regular)
7215 return FALSE;
7216
7217 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7218 if (pent->plt.refcount > 0
7219 && pent->addend == 0)
7220 return TRUE;
7221
7222 return FALSE;
7223}
7224
e86ce104
AM
7225/* Adjust a symbol defined by a dynamic object and referenced by a
7226 regular object. The current definition is in some section of the
7227 dynamic object, but we're not including those sections. We have to
7228 change the definition to something the rest of the link can
7229 understand. */
7230
b34976b6 7231static bfd_boolean
4ce794b7
AM
7232ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7233 struct elf_link_hash_entry *h)
e86ce104
AM
7234{
7235 struct ppc_link_hash_table *htab;
e86ce104 7236 asection *s;
e86ce104
AM
7237
7238 htab = ppc_hash_table (info);
4dfe6ac6
NC
7239 if (htab == NULL)
7240 return FALSE;
e86ce104
AM
7241
7242 /* Deal with function syms. */
7243 if (h->type == STT_FUNC
e054468f 7244 || h->type == STT_GNU_IFUNC
f5385ebf 7245 || h->needs_plt)
e86ce104
AM
7246 {
7247 /* Clear procedure linkage table information for any symbol that
7248 won't need a .plt entry. */
411e1bfb
AM
7249 struct plt_entry *ent;
7250 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7251 if (ent->plt.refcount > 0)
7252 break;
8387904d 7253 if (ent == NULL
e054468f
AM
7254 || (h->type != STT_GNU_IFUNC
7255 && (SYMBOL_CALLS_LOCAL (info, h)
7256 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
a4b6fadd
AM
7257 && h->root.type == bfd_link_hash_undefweak)))
7258 || ((struct ppc_link_hash_entry *) h)->save_res)
40b8271b 7259 {
411e1bfb 7260 h->plt.plist = NULL;
f5385ebf 7261 h->needs_plt = 0;
d1eca1e4 7262 h->pointer_equality_needed = 0;
40b8271b 7263 }
8a2058b5 7264 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7265 {
d1eca1e4
AM
7266 /* Taking a function's address in a read/write section
7267 doesn't require us to define the function symbol in the
7268 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7269 be used instead. The reason we prefer a few more dynamic
7270 relocs is that calling via a global entry stub costs a
7271 few more instructions, and pointer_equality_needed causes
7272 extra work in ld.so when resolving these symbols. */
7273 if (global_entry_stub (h)
d311bc8b 7274 && !alias_readonly_dynrelocs (h))
d1eca1e4
AM
7275 {
7276 h->pointer_equality_needed = 0;
8a2058b5
AM
7277 /* After adjust_dynamic_symbol, non_got_ref set in
7278 the non-pic case means that dyn_relocs for this
7279 symbol should be discarded. */
d1eca1e4
AM
7280 h->non_got_ref = 0;
7281 }
7282
a345bc8d
AM
7283 /* If making a plt entry, then we don't need copy relocs. */
7284 return TRUE;
7285 }
5bd4f169 7286 }
bbd7ec4a 7287 else
411e1bfb 7288 h->plt.plist = NULL;
5bd4f169
AM
7289
7290 /* If this is a weak symbol, and there is a real definition, the
7291 processor independent code will have arranged for us to see the
7292 real definition first, and we can just use the same value. */
f6e332e6 7293 if (h->u.weakdef != NULL)
5bd4f169 7294 {
f6e332e6
AM
7295 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7296 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7297 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7298 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 7299 if (ELIMINATE_COPY_RELOCS)
f6e332e6 7300 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 7301 return TRUE;
5bd4f169
AM
7302 }
7303
5bd4f169
AM
7304 /* If we are creating a shared library, we must presume that the
7305 only references to the symbol are via the global offset table.
7306 For such cases we need not do anything here; the relocations will
7307 be handled correctly by relocate_section. */
0e1862bb 7308 if (bfd_link_pic (info))
b34976b6 7309 return TRUE;
5bd4f169 7310
65f38f15
AM
7311 /* If there are no references to this symbol that do not use the
7312 GOT, we don't need to generate a copy reloc. */
f5385ebf 7313 if (!h->non_got_ref)
b34976b6 7314 return TRUE;
65f38f15 7315
b186458a 7316 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7317 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7318
d93d1c80
AM
7319 /* If -z nocopyreloc was given, don't generate them either. */
7320 || info->nocopyreloc
a127494f 7321
d93d1c80
AM
7322 /* If we didn't find any dynamic relocs in read-only sections, then
7323 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7324 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7325
d93d1c80
AM
7326 /* Protected variables do not work with .dynbss. The copy in
7327 .dynbss won't be used by the shared library with the protected
7328 definition for the variable. Text relocations are preferable
7329 to an incorrect program. */
7330 || h->protected_def)
a127494f
AM
7331 {
7332 h->non_got_ref = 0;
7333 return TRUE;
7334 }
7335
5d35169e 7336 if (h->plt.plist != NULL)
97b639ba
AM
7337 {
7338 /* We should never get here, but unfortunately there are versions
7339 of gcc out there that improperly (for this ABI) put initialized
7340 function pointers, vtable refs and suchlike in read-only
7341 sections. Allow them to proceed, but warn that this might
7342 break at runtime. */
25f53a85 7343 info->callbacks->einfo
bc30df16 7344 (_("%P: copy reloc against `%T' requires lazy plt linking; "
25f53a85 7345 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7346 h->root.root.string);
7347 }
5d35169e
AM
7348
7349 /* This is a reference to a symbol defined by a dynamic object which
7350 is not a function. */
7351
5bd4f169
AM
7352 /* We must allocate the symbol in our .dynbss section, which will
7353 become part of the .bss section of the executable. There will be
7354 an entry for this symbol in the .dynsym section. The dynamic
7355 object will contain position independent code, so all references
7356 from the dynamic object to this symbol will go through the global
7357 offset table. The dynamic linker will use the .dynsym entry to
7358 determine the address it must put in the global offset table, so
7359 both the dynamic object and the regular object will refer to the
7360 same memory location for the variable. */
5bd4f169 7361
04c9666a
AM
7362 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7363 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
7364 runtime process image. We need to remember the offset into the
7365 .rela.bss section we are going to use. */
1d7e9d18 7366 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7367 {
eea6121a 7368 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 7369 h->needs_copy = 1;
5bd4f169
AM
7370 }
7371
4ce794b7 7372 s = htab->dynbss;
5bd4f169 7373
6cabe1ea 7374 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7375}
7376
e86ce104
AM
7377/* If given a function descriptor symbol, hide both the function code
7378 sym and the descriptor. */
7379static void
4ce794b7
AM
7380ppc64_elf_hide_symbol (struct bfd_link_info *info,
7381 struct elf_link_hash_entry *h,
7382 bfd_boolean force_local)
e86ce104 7383{
34814b9f 7384 struct ppc_link_hash_entry *eh;
e86ce104
AM
7385 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7386
34814b9f
AM
7387 eh = (struct ppc_link_hash_entry *) h;
7388 if (eh->is_func_descriptor)
e86ce104 7389 {
34814b9f 7390 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7391
721956f4 7392 if (fh == NULL)
d1329ca3
AM
7393 {
7394 const char *p, *q;
7395 struct ppc_link_hash_table *htab;
7396 char save;
7397
7398 /* We aren't supposed to use alloca in BFD because on
7399 systems which do not have alloca the version in libiberty
7400 calls xmalloc, which might cause the program to crash
7401 when it runs out of memory. This function doesn't have a
7402 return status, so there's no way to gracefully return an
7403 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7404 accessed; It's either a string in an ELF string table,
7405 or allocated in an objalloc structure. */
d1329ca3 7406
34814b9f 7407 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7408 save = *p;
7409 *(char *) p = '.';
7410 htab = ppc_hash_table (info);
4dfe6ac6
NC
7411 if (htab == NULL)
7412 return;
7413
34814b9f
AM
7414 fh = (struct ppc_link_hash_entry *)
7415 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7416 *(char *) p = save;
7417
7418 /* Unfortunately, if it so happens that the string we were
7419 looking for was allocated immediately before this string,
7420 then we overwrote the string terminator. That's the only
7421 reason the lookup should fail. */
7422 if (fh == NULL)
7423 {
34814b9f
AM
7424 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7425 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7426 --q, --p;
34814b9f
AM
7427 if (q < eh->elf.root.root.string && *p == '.')
7428 fh = (struct ppc_link_hash_entry *)
7429 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7430 }
7431 if (fh != NULL)
7432 {
34814b9f
AM
7433 eh->oh = fh;
7434 fh->oh = eh;
d1329ca3
AM
7435 }
7436 }
e86ce104 7437 if (fh != NULL)
34814b9f 7438 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7439 }
7440}
7441
411e1bfb 7442static bfd_boolean
8843416a
AM
7443get_sym_h (struct elf_link_hash_entry **hp,
7444 Elf_Internal_Sym **symp,
7445 asection **symsecp,
f961d9dd 7446 unsigned char **tls_maskp,
8843416a
AM
7447 Elf_Internal_Sym **locsymsp,
7448 unsigned long r_symndx,
7449 bfd *ibfd)
411e1bfb 7450{
0ffa91dd 7451 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7452
7453 if (r_symndx >= symtab_hdr->sh_info)
7454 {
7455 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7456 struct elf_link_hash_entry *h;
7457
7458 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7459 h = elf_follow_link (h);
411e1bfb
AM
7460
7461 if (hp != NULL)
7462 *hp = h;
7463
7464 if (symp != NULL)
7465 *symp = NULL;
7466
7467 if (symsecp != NULL)
7468 {
7469 asection *symsec = NULL;
7470 if (h->root.type == bfd_link_hash_defined
7471 || h->root.type == bfd_link_hash_defweak)
7472 symsec = h->root.u.def.section;
7473 *symsecp = symsec;
7474 }
7475
e7b938ca 7476 if (tls_maskp != NULL)
411e1bfb
AM
7477 {
7478 struct ppc_link_hash_entry *eh;
7479
7480 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7481 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7482 }
7483 }
7484 else
7485 {
7486 Elf_Internal_Sym *sym;
7487 Elf_Internal_Sym *locsyms = *locsymsp;
7488
7489 if (locsyms == NULL)
7490 {
7491 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7492 if (locsyms == NULL)
7493 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7494 symtab_hdr->sh_info,
7495 0, NULL, NULL, NULL);
7496 if (locsyms == NULL)
7497 return FALSE;
7498 *locsymsp = locsyms;
7499 }
7500 sym = locsyms + r_symndx;
7501
7502 if (hp != NULL)
7503 *hp = NULL;
7504
7505 if (symp != NULL)
7506 *symp = sym;
7507
7508 if (symsecp != NULL)
cb33740c 7509 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7510
e7b938ca 7511 if (tls_maskp != NULL)
411e1bfb
AM
7512 {
7513 struct got_entry **lgot_ents;
f961d9dd 7514 unsigned char *tls_mask;
411e1bfb 7515
e7b938ca 7516 tls_mask = NULL;
411e1bfb
AM
7517 lgot_ents = elf_local_got_ents (ibfd);
7518 if (lgot_ents != NULL)
7519 {
e054468f
AM
7520 struct plt_entry **local_plt = (struct plt_entry **)
7521 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7522 unsigned char *lgot_masks = (unsigned char *)
e054468f 7523 (local_plt + symtab_hdr->sh_info);
e7b938ca 7524 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7525 }
e7b938ca 7526 *tls_maskp = tls_mask;
411e1bfb
AM
7527 }
7528 }
7529 return TRUE;
7530}
7531
e7b938ca 7532/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7533 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7534 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7535
7536static int
f961d9dd 7537get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7538 unsigned long *toc_symndx,
7539 bfd_vma *toc_addend,
0d4792f7 7540 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7541 const Elf_Internal_Rela *rel,
7542 bfd *ibfd)
411e1bfb
AM
7543{
7544 unsigned long r_symndx;
0d4792f7 7545 int next_r;
411e1bfb
AM
7546 struct elf_link_hash_entry *h;
7547 Elf_Internal_Sym *sym;
7548 asection *sec;
7549 bfd_vma off;
7550
7551 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7552 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7553 return 0;
411e1bfb 7554
e7b938ca 7555 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7556 || sec == NULL
6bee8834 7557 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7558 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7559 return 1;
411e1bfb
AM
7560
7561 /* Look inside a TOC section too. */
7562 if (h != NULL)
7563 {
7564 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7565 off = h->root.u.def.value;
7566 }
7567 else
7568 off = sym->st_value;
7569 off += rel->r_addend;
7570 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7571 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7572 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7573 if (toc_symndx != NULL)
7574 *toc_symndx = r_symndx;
3a71aa26
AM
7575 if (toc_addend != NULL)
7576 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7577 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7578 return 0;
854b41e7 7579 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7580 && (next_r == -1 || next_r == -2))
7581 return 1 - next_r;
951fd09b 7582 return 1;
411e1bfb
AM
7583}
7584
3b421ab3
AM
7585/* Find (or create) an entry in the tocsave hash table. */
7586
7587static struct tocsave_entry *
7588tocsave_find (struct ppc_link_hash_table *htab,
7589 enum insert_option insert,
7590 Elf_Internal_Sym **local_syms,
7591 const Elf_Internal_Rela *irela,
7592 bfd *ibfd)
7593{
7594 unsigned long r_indx;
7595 struct elf_link_hash_entry *h;
7596 Elf_Internal_Sym *sym;
7597 struct tocsave_entry ent, *p;
7598 hashval_t hash;
7599 struct tocsave_entry **slot;
7600
7601 r_indx = ELF64_R_SYM (irela->r_info);
7602 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7603 return NULL;
7604 if (ent.sec == NULL || ent.sec->output_section == NULL)
7605 {
4eca0228 7606 _bfd_error_handler
3b421ab3
AM
7607 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7608 return NULL;
7609 }
7610
7611 if (h != NULL)
7612 ent.offset = h->root.u.def.value;
7613 else
7614 ent.offset = sym->st_value;
7615 ent.offset += irela->r_addend;
7616
7617 hash = tocsave_htab_hash (&ent);
7618 slot = ((struct tocsave_entry **)
7619 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7620 if (slot == NULL)
7621 return NULL;
7622
7623 if (*slot == NULL)
7624 {
7625 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7626 if (p == NULL)
7627 return NULL;
7628 *p = ent;
7629 *slot = p;
7630 }
7631 return *slot;
7632}
7633
754021d0 7634/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7635 code for the old ABI, these will already have been done. */
754021d0
AM
7636
7637static bfd_boolean
7638adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7639{
7640 struct ppc_link_hash_entry *eh;
7641 asection *sym_sec;
74f0fb50 7642 struct _opd_sec_data *opd;
754021d0
AM
7643
7644 if (h->root.type == bfd_link_hash_indirect)
7645 return TRUE;
7646
754021d0
AM
7647 if (h->root.type != bfd_link_hash_defined
7648 && h->root.type != bfd_link_hash_defweak)
7649 return TRUE;
7650
7651 eh = (struct ppc_link_hash_entry *) h;
7652 if (eh->adjust_done)
7653 return TRUE;
7654
7655 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7656 opd = get_opd_info (sym_sec);
7657 if (opd != NULL && opd->adjust != NULL)
754021d0 7658 {
51aecdc5 7659 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7660 if (adjust == -1)
7661 {
7662 /* This entry has been deleted. */
b3fac117 7663 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7664 if (dsec == NULL)
7665 {
7666 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7667 if (discarded_section (dsec))
81688140 7668 {
b3fac117 7669 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7670 break;
7671 }
7672 }
4025353c 7673 eh->elf.root.u.def.value = 0;
81688140 7674 eh->elf.root.u.def.section = dsec;
4025353c
AM
7675 }
7676 else
7677 eh->elf.root.u.def.value += adjust;
754021d0
AM
7678 eh->adjust_done = 1;
7679 }
7680 return TRUE;
7681}
7682
8c1d1bb8 7683/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7684 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7685 have already been determined. */
7686
7687static bfd_boolean
7688dec_dynrel_count (bfd_vma r_info,
7689 asection *sec,
7690 struct bfd_link_info *info,
7691 Elf_Internal_Sym **local_syms,
7692 struct elf_link_hash_entry *h,
19e08130 7693 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7694{
7695 enum elf_ppc64_reloc_type r_type;
19e08130 7696 asection *sym_sec = NULL;
8c1d1bb8
AM
7697
7698 /* Can this reloc be dynamic? This switch, and later tests here
7699 should be kept in sync with the code in check_relocs. */
7700 r_type = ELF64_R_TYPE (r_info);
7701 switch (r_type)
7702 {
7703 default:
7704 return TRUE;
7705
7706 case R_PPC64_TPREL16:
7707 case R_PPC64_TPREL16_LO:
7708 case R_PPC64_TPREL16_HI:
7709 case R_PPC64_TPREL16_HA:
7710 case R_PPC64_TPREL16_DS:
7711 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7712 case R_PPC64_TPREL16_HIGH:
7713 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7714 case R_PPC64_TPREL16_HIGHER:
7715 case R_PPC64_TPREL16_HIGHERA:
7716 case R_PPC64_TPREL16_HIGHEST:
7717 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 7718 if (!bfd_link_pic (info))
8c1d1bb8
AM
7719 return TRUE;
7720
7721 case R_PPC64_TPREL64:
7722 case R_PPC64_DTPMOD64:
7723 case R_PPC64_DTPREL64:
7724 case R_PPC64_ADDR64:
7725 case R_PPC64_REL30:
7726 case R_PPC64_REL32:
7727 case R_PPC64_REL64:
7728 case R_PPC64_ADDR14:
7729 case R_PPC64_ADDR14_BRNTAKEN:
7730 case R_PPC64_ADDR14_BRTAKEN:
7731 case R_PPC64_ADDR16:
7732 case R_PPC64_ADDR16_DS:
7733 case R_PPC64_ADDR16_HA:
7734 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7735 case R_PPC64_ADDR16_HIGH:
7736 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7737 case R_PPC64_ADDR16_HIGHER:
7738 case R_PPC64_ADDR16_HIGHERA:
7739 case R_PPC64_ADDR16_HIGHEST:
7740 case R_PPC64_ADDR16_HIGHESTA:
7741 case R_PPC64_ADDR16_LO:
7742 case R_PPC64_ADDR16_LO_DS:
7743 case R_PPC64_ADDR24:
7744 case R_PPC64_ADDR32:
7745 case R_PPC64_UADDR16:
7746 case R_PPC64_UADDR32:
7747 case R_PPC64_UADDR64:
7748 case R_PPC64_TOC:
7749 break;
7750 }
7751
7752 if (local_syms != NULL)
7753 {
7754 unsigned long r_symndx;
8c1d1bb8
AM
7755 bfd *ibfd = sec->owner;
7756
7757 r_symndx = ELF64_R_SYM (r_info);
7758 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7759 return FALSE;
7760 }
7761
0e1862bb 7762 if ((bfd_link_pic (info)
1d483afe 7763 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7764 || (h != NULL
198f1157 7765 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7766 || h->root.type == bfd_link_hash_defweak
7767 || !h->def_regular))))
7768 || (ELIMINATE_COPY_RELOCS
0e1862bb 7769 && !bfd_link_pic (info)
8c1d1bb8
AM
7770 && h != NULL
7771 && (h->root.type == bfd_link_hash_defweak
7772 || !h->def_regular)))
7773 ;
7774 else
7775 return TRUE;
7776
7777 if (h != NULL)
6edfbbad 7778 {
19e08130
AM
7779 struct elf_dyn_relocs *p;
7780 struct elf_dyn_relocs **pp;
7781 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7782
7783 /* elf_gc_sweep may have already removed all dyn relocs associated
7784 with local syms for a given section. Also, symbol flags are
7785 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7786 report a dynreloc miscount. */
7787 if (*pp == NULL && info->gc_sections)
7788 return TRUE;
7789
7790 while ((p = *pp) != NULL)
60124e18 7791 {
19e08130
AM
7792 if (p->sec == sec)
7793 {
7794 if (!must_be_dyn_reloc (info, r_type))
7795 p->pc_count -= 1;
7796 p->count -= 1;
7797 if (p->count == 0)
7798 *pp = p->next;
7799 return TRUE;
7800 }
7801 pp = &p->next;
60124e18 7802 }
6edfbbad 7803 }
19e08130
AM
7804 else
7805 {
7806 struct ppc_dyn_relocs *p;
7807 struct ppc_dyn_relocs **pp;
7808 void *vpp;
7809 bfd_boolean is_ifunc;
8c1d1bb8 7810
19e08130
AM
7811 if (local_syms == NULL)
7812 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7813 if (sym_sec == NULL)
7814 sym_sec = sec;
c57da1a7 7815
19e08130
AM
7816 vpp = &elf_section_data (sym_sec)->local_dynrel;
7817 pp = (struct ppc_dyn_relocs **) vpp;
7818
7819 if (*pp == NULL && info->gc_sections)
7820 return TRUE;
7821
7822 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7823 while ((p = *pp) != NULL)
8c1d1bb8 7824 {
19e08130
AM
7825 if (p->sec == sec && p->ifunc == is_ifunc)
7826 {
7827 p->count -= 1;
7828 if (p->count == 0)
7829 *pp = p->next;
7830 return TRUE;
7831 }
7832 pp = &p->next;
8c1d1bb8 7833 }
8c1d1bb8
AM
7834 }
7835
695344c0 7836 /* xgettext:c-format */
8de848d8 7837 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7838 sec->owner, sec);
8c1d1bb8
AM
7839 bfd_set_error (bfd_error_bad_value);
7840 return FALSE;
7841}
7842
754021d0
AM
7843/* Remove unused Official Procedure Descriptor entries. Currently we
7844 only remove those associated with functions in discarded link-once
7845 sections, or weakly defined functions that have been overridden. It
7846 would be possible to remove many more entries for statically linked
7847 applications. */
7848
b34976b6 7849bfd_boolean
e7d1c40c 7850ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7851{
7852 bfd *ibfd;
754021d0 7853 bfd_boolean some_edited = FALSE;
3f764659 7854 asection *need_pad = NULL;
e7d1c40c
AM
7855 struct ppc_link_hash_table *htab;
7856
7857 htab = ppc_hash_table (info);
7858 if (htab == NULL)
7859 return FALSE;
1e2f5b6e 7860
c72f2fb2 7861 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7862 {
7863 asection *sec;
7864 Elf_Internal_Rela *relstart, *rel, *relend;
7865 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7866 Elf_Internal_Sym *local_syms;
74f0fb50 7867 struct _opd_sec_data *opd;
51aecdc5 7868 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7869 bfd_size_type cnt_16b = 0;
1e2f5b6e 7870
854b41e7
AM
7871 if (!is_ppc64_elf (ibfd))
7872 continue;
7873
1e2f5b6e 7874 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7875 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7876 continue;
7877
dbaa2011 7878 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7879 continue;
7880
1e2f5b6e
AM
7881 if (sec->output_section == bfd_abs_section_ptr)
7882 continue;
7883
7884 /* Look through the section relocs. */
7885 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7886 continue;
7887
6cdc0ccc 7888 local_syms = NULL;
0ffa91dd 7889 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7890
7891 /* Read the relocations. */
4ce794b7 7892 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7893 info->keep_memory);
1e2f5b6e 7894 if (relstart == NULL)
b34976b6 7895 return FALSE;
1e2f5b6e
AM
7896
7897 /* First run through the relocs to check they are sane, and to
7898 determine whether we need to edit this opd section. */
b34976b6 7899 need_edit = FALSE;
51aecdc5 7900 broken = FALSE;
3f764659 7901 need_pad = sec;
1e2f5b6e 7902 relend = relstart + sec->reloc_count;
50bc7936 7903 for (rel = relstart; rel < relend; )
1e2f5b6e 7904 {
04c9666a 7905 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7906 unsigned long r_symndx;
7907 asection *sym_sec;
7908 struct elf_link_hash_entry *h;
7909 Elf_Internal_Sym *sym;
51aecdc5 7910 bfd_vma offset;
1e2f5b6e 7911
51aecdc5 7912 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7913 only interested in the reloc pointing to a function entry
7914 point. */
51aecdc5
AM
7915 offset = rel->r_offset;
7916 if (rel + 1 == relend
7917 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7918 {
7919 /* If someone messes with .opd alignment then after a
7920 "ld -r" we might have padding in the middle of .opd.
7921 Also, there's nothing to prevent someone putting
7922 something silly in .opd with the assembler. No .opd
b34976b6 7923 optimization for them! */
3f764659 7924 broken_opd:
4eca0228 7925 _bfd_error_handler
d003868e 7926 (_("%B: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7927 broken = TRUE;
1e2f5b6e
AM
7928 break;
7929 }
7930
50bc7936
AM
7931 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7932 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7933 {
4eca0228 7934 _bfd_error_handler
695344c0 7935 /* xgettext:c-format */
d003868e
AM
7936 (_("%B: unexpected reloc type %u in .opd section"),
7937 ibfd, r_type);
51aecdc5 7938 broken = TRUE;
50bc7936
AM
7939 break;
7940 }
7941
1e2f5b6e 7942 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7943 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7944 r_symndx, ibfd))
50bc7936 7945 goto error_ret;
1e2f5b6e
AM
7946
7947 if (sym_sec == NULL || sym_sec->owner == NULL)
7948 {
411e1bfb
AM
7949 const char *sym_name;
7950 if (h != NULL)
7951 sym_name = h->root.root.string;
7952 else
26c61ae5
L
7953 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7954 sym_sec);
411e1bfb 7955
4eca0228 7956 _bfd_error_handler
695344c0 7957 /* xgettext:c-format */
d003868e
AM
7958 (_("%B: undefined sym `%s' in .opd section"),
7959 ibfd, sym_name);
51aecdc5 7960 broken = TRUE;
1e2f5b6e
AM
7961 break;
7962 }
7963
51020317
AM
7964 /* opd entries are always for functions defined in the
7965 current input bfd. If the symbol isn't defined in the
7966 input bfd, then we won't be using the function in this
7967 bfd; It must be defined in a linkonce section in another
7968 bfd, or is weak. It's also possible that we are
7969 discarding the function due to a linker script /DISCARD/,
7970 which we test for via the output_section. */
7971 if (sym_sec->owner != ibfd
7972 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7973 need_edit = TRUE;
1e2f5b6e 7974
50bc7936 7975 rel += 2;
51aecdc5
AM
7976 if (rel + 1 == relend
7977 || (rel + 2 < relend
7978 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7979 ++rel;
7980
7981 if (rel == relend)
3f764659
JJ
7982 {
7983 if (sec->size == offset + 24)
7984 {
7985 need_pad = NULL;
7986 break;
7987 }
51aecdc5 7988 if (sec->size == offset + 16)
3f764659
JJ
7989 {
7990 cnt_16b++;
7991 break;
7992 }
7993 goto broken_opd;
7994 }
3f764659
JJ
7995 else if (rel + 1 < relend
7996 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7997 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7998 {
51aecdc5
AM
7999 if (rel[0].r_offset == offset + 16)
8000 cnt_16b++;
8001 else if (rel[0].r_offset != offset + 24)
8002 goto broken_opd;
3f764659
JJ
8003 }
8004 else
8005 goto broken_opd;
1e2f5b6e
AM
8006 }
8007
e7d1c40c 8008 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 8009
51aecdc5 8010 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
8011 {
8012 Elf_Internal_Rela *write_rel;
d4730f92 8013 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 8014 bfd_byte *rptr, *wptr;
983bddc8 8015 bfd_byte *new_contents;
74f0fb50
AM
8016 bfd_size_type amt;
8017
983bddc8 8018 new_contents = NULL;
51aecdc5 8019 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 8020 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 8021 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
8022 if (opd->adjust == NULL)
8023 return FALSE;
8024 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
8025
8026 /* This seems a waste of time as input .opd sections are all
8027 zeros as generated by gcc, but I suppose there's no reason
8028 this will always be so. We might start putting something in
8029 the third word of .opd entries. */
8030 if ((sec->flags & SEC_IN_MEMORY) == 0)
8031 {
eea6121a
AM
8032 bfd_byte *loc;
8033 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 8034 {
eea6121a
AM
8035 if (loc != NULL)
8036 free (loc);
50bc7936 8037 error_ret:
6cdc0ccc
AM
8038 if (local_syms != NULL
8039 && symtab_hdr->contents != (unsigned char *) local_syms)
8040 free (local_syms);
6cdc0ccc
AM
8041 if (elf_section_data (sec)->relocs != relstart)
8042 free (relstart);
b34976b6 8043 return FALSE;
6cdc0ccc 8044 }
1e2f5b6e
AM
8045 sec->contents = loc;
8046 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8047 }
8048
8049 elf_section_data (sec)->relocs = relstart;
8050
3f764659 8051 new_contents = sec->contents;
3f764659
JJ
8052 if (add_aux_fields)
8053 {
8054 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8055 if (new_contents == NULL)
8056 return FALSE;
51aecdc5 8057 need_pad = NULL;
3f764659 8058 }
b4f4e59f
AM
8059 wptr = new_contents;
8060 rptr = sec->contents;
1e2f5b6e 8061 write_rel = relstart;
51aecdc5 8062 for (rel = relstart; rel < relend; )
1e2f5b6e 8063 {
50bc7936
AM
8064 unsigned long r_symndx;
8065 asection *sym_sec;
8066 struct elf_link_hash_entry *h;
51aecdc5 8067 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 8068 Elf_Internal_Sym *sym;
51aecdc5
AM
8069 long opd_ent_size;
8070 Elf_Internal_Rela *next_rel;
8071 bfd_boolean skip;
50bc7936
AM
8072
8073 r_symndx = ELF64_R_SYM (rel->r_info);
8074 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 8075 r_symndx, ibfd))
50bc7936
AM
8076 goto error_ret;
8077
51aecdc5
AM
8078 next_rel = rel + 2;
8079 if (next_rel + 1 == relend
8080 || (next_rel + 2 < relend
8081 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8082 ++next_rel;
8083
8084 /* See if the .opd entry is full 24 byte or
8085 16 byte (with fd_aux entry overlapped with next
8086 fd_func). */
8087 opd_ent_size = 24;
8088 if (next_rel == relend)
1e2f5b6e 8089 {
51aecdc5 8090 if (sec->size == rel->r_offset + 16)
3f764659 8091 opd_ent_size = 16;
51aecdc5
AM
8092 }
8093 else if (next_rel->r_offset == rel->r_offset + 16)
8094 opd_ent_size = 16;
3f764659 8095
51aecdc5
AM
8096 if (h != NULL
8097 && h->root.root.string[0] == '.')
8098 {
8099 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h, htab);
8100 if (fdh != NULL
8101 && fdh->elf.root.type != bfd_link_hash_defined
8102 && fdh->elf.root.type != bfd_link_hash_defweak)
8103 fdh = NULL;
8104 }
1e2f5b6e 8105
51aecdc5
AM
8106 skip = (sym_sec->owner != ibfd
8107 || sym_sec->output_section == bfd_abs_section_ptr);
8108 if (skip)
8109 {
8110 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8111 {
51aecdc5
AM
8112 /* Arrange for the function descriptor sym
8113 to be dropped. */
8114 fdh->elf.root.u.def.value = 0;
8115 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8116 }
51aecdc5 8117 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8118
0e1862bb 8119 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8120 rel = next_rel;
8121 else
8122 while (1)
8123 {
8124 if (!dec_dynrel_count (rel->r_info, sec, info,
8125 NULL, h, sym))
8126 goto error_ret;
754021d0 8127
51aecdc5
AM
8128 if (++rel == next_rel)
8129 break;
1e2f5b6e 8130
51aecdc5
AM
8131 r_symndx = ELF64_R_SYM (rel->r_info);
8132 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8133 r_symndx, ibfd))
8134 goto error_ret;
8135 }
50bc7936
AM
8136 }
8137 else
1e2f5b6e 8138 {
51aecdc5
AM
8139 /* We'll be keeping this opd entry. */
8140 long adjust;
8141
8142 if (fdh != NULL)
8143 {
8144 /* Redefine the function descriptor symbol to
8145 this location in the opd section. It is
8146 necessary to update the value here rather
8147 than using an array of adjustments as we do
8148 for local symbols, because various places
8149 in the generic ELF code use the value
8150 stored in u.def.value. */
8151 fdh->elf.root.u.def.value = wptr - new_contents;
8152 fdh->adjust_done = 1;
8153 }
8154
8155 /* Local syms are a bit tricky. We could
8156 tweak them as they can be cached, but
8157 we'd need to look through the local syms
8158 for the function descriptor sym which we
8159 don't have at the moment. So keep an
8160 array of adjustments. */
8161 adjust = (wptr - new_contents) - (rptr - sec->contents);
8162 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8163
8164 if (wptr != rptr)
8165 memcpy (wptr, rptr, opd_ent_size);
8166 wptr += opd_ent_size;
8167 if (add_aux_fields && opd_ent_size == 16)
8168 {
8169 memset (wptr, '\0', 8);
8170 wptr += 8;
8171 }
8172
50bc7936 8173 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8174 new opd entries. */
8175 for ( ; rel != next_rel; ++rel)
8176 {
8177 rel->r_offset += adjust;
8178 if (write_rel != rel)
8179 memcpy (write_rel, rel, sizeof (*rel));
8180 ++write_rel;
8181 }
1e2f5b6e 8182 }
51aecdc5
AM
8183
8184 rptr += opd_ent_size;
1e2f5b6e
AM
8185 }
8186
3f764659 8187 sec->size = wptr - new_contents;
1e2f5b6e 8188 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8189 if (add_aux_fields)
8190 {
8191 free (sec->contents);
8192 sec->contents = new_contents;
8193 }
8194
05bf9422 8195 /* Fudge the header size too, as this is used later in
cdcf6e38 8196 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8197 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8198 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8199 some_edited = TRUE;
1e2f5b6e 8200 }
6cdc0ccc 8201 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8202 free (relstart);
6cdc0ccc 8203
411e1bfb
AM
8204 if (local_syms != NULL
8205 && symtab_hdr->contents != (unsigned char *) local_syms)
8206 {
8207 if (!info->keep_memory)
8208 free (local_syms);
8209 else
8210 symtab_hdr->contents = (unsigned char *) local_syms;
8211 }
8212 }
8213
754021d0
AM
8214 if (some_edited)
8215 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8216
3f764659
JJ
8217 /* If we are doing a final link and the last .opd entry is just 16 byte
8218 long, add a 8 byte padding after it. */
0e1862bb 8219 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8220 {
8221 bfd_byte *p;
8222
8223 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8224 {
8225 BFD_ASSERT (need_pad->size > 0);
8226
8227 p = bfd_malloc (need_pad->size + 8);
8228 if (p == NULL)
8229 return FALSE;
699733f6 8230
3f764659
JJ
8231 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8232 p, 0, need_pad->size))
8233 return FALSE;
8234
8235 need_pad->contents = p;
8236 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8237 }
8238 else
8239 {
8240 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8241 if (p == NULL)
8242 return FALSE;
8243
8244 need_pad->contents = p;
8245 }
8246
8247 memset (need_pad->contents + need_pad->size, 0, 8);
8248 need_pad->size += 8;
8249 }
8250
411e1bfb
AM
8251 return TRUE;
8252}
8253
e1918d23 8254/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8255
e1918d23 8256asection *
e7d1c40c 8257ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8258{
411e1bfb
AM
8259 struct ppc_link_hash_table *htab;
8260
411e1bfb 8261 htab = ppc_hash_table (info);
4dfe6ac6
NC
8262 if (htab == NULL)
8263 return NULL;
8264
ee67d69a
AM
8265 if (abiversion (info->output_bfd) == 1)
8266 htab->opd_abi = 1;
8267
e7d1c40c 8268 if (htab->params->no_multi_toc)
33c0ec9d
AM
8269 htab->do_multi_toc = 0;
8270 else if (!htab->do_multi_toc)
e7d1c40c 8271 htab->params->no_multi_toc = 1;
33c0ec9d 8272
3a71aa26
AM
8273 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8274 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8275 FALSE, FALSE, TRUE));
a7f2871e
AM
8276 /* Move dynamic linking info to the function descriptor sym. */
8277 if (htab->tls_get_addr != NULL)
8278 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8279 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8280 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8281 FALSE, FALSE, TRUE));
7c9cf415 8282 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8283 {
8284 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8285
8286 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8287 FALSE, FALSE, TRUE);
8288 if (opt != NULL)
8289 func_desc_adjust (opt, info);
8290 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8291 FALSE, FALSE, TRUE);
8292 if (opt_fd != NULL
8293 && (opt_fd->root.type == bfd_link_hash_defined
8294 || opt_fd->root.type == bfd_link_hash_defweak))
8295 {
8296 /* If glibc supports an optimized __tls_get_addr call stub,
8297 signalled by the presence of __tls_get_addr_opt, and we'll
8298 be calling __tls_get_addr via a plt call stub, then
8299 make __tls_get_addr point to __tls_get_addr_opt. */
8300 tga_fd = &htab->tls_get_addr_fd->elf;
8301 if (htab->elf.dynamic_sections_created
8302 && tga_fd != NULL
8303 && (tga_fd->type == STT_FUNC
8304 || tga_fd->needs_plt)
8305 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8306 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8307 && tga_fd->root.type == bfd_link_hash_undefweak)))
8308 {
8309 struct plt_entry *ent;
8310
8311 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8312 if (ent->plt.refcount > 0)
8313 break;
8314 if (ent != NULL)
8315 {
8316 tga_fd->root.type = bfd_link_hash_indirect;
8317 tga_fd->root.u.i.link = &opt_fd->root;
8318 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
3bd43ebc 8319 opt_fd->forced_local = 0;
a7f2871e
AM
8320 if (opt_fd->dynindx != -1)
8321 {
8322 /* Use __tls_get_addr_opt in dynamic relocations. */
8323 opt_fd->dynindx = -1;
8324 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8325 opt_fd->dynstr_index);
8326 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8327 return NULL;
a7f2871e
AM
8328 }
8329 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8330 tga = &htab->tls_get_addr->elf;
8331 if (opt != NULL && tga != NULL)
8332 {
8333 tga->root.type = bfd_link_hash_indirect;
8334 tga->root.u.i.link = &opt->root;
8335 ppc64_elf_copy_indirect_symbol (info, opt, tga);
3bd43ebc 8336 opt->forced_local = 0;
a7f2871e
AM
8337 _bfd_elf_link_hash_hide_symbol (info, opt,
8338 tga->forced_local);
8339 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8340 }
8341 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8342 htab->tls_get_addr_fd->is_func_descriptor = 1;
8343 if (htab->tls_get_addr != NULL)
8344 {
8345 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8346 htab->tls_get_addr->is_func = 1;
8347 }
8348 }
8349 }
8350 }
7c9cf415
AM
8351 else if (htab->params->tls_get_addr_opt < 0)
8352 htab->params->tls_get_addr_opt = 0;
a7f2871e 8353 }
33c0ec9d 8354 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8355}
8387904d 8356
3a71aa26
AM
8357/* Return TRUE iff REL is a branch reloc with a global symbol matching
8358 HASH1 or HASH2. */
8387904d 8359
3a71aa26
AM
8360static bfd_boolean
8361branch_reloc_hash_match (const bfd *ibfd,
8362 const Elf_Internal_Rela *rel,
8363 const struct ppc_link_hash_entry *hash1,
8364 const struct ppc_link_hash_entry *hash2)
8365{
8366 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8367 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8368 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8369
e054468f 8370 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8371 {
3a71aa26
AM
8372 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8373 struct elf_link_hash_entry *h;
8387904d 8374
3a71aa26 8375 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8376 h = elf_follow_link (h);
3a71aa26
AM
8377 if (h == &hash1->elf || h == &hash2->elf)
8378 return TRUE;
a48ebf4d 8379 }
3a71aa26 8380 return FALSE;
951fd09b 8381}
411e1bfb 8382
951fd09b
AM
8383/* Run through all the TLS relocs looking for optimization
8384 opportunities. The linker has been hacked (see ppc64elf.em) to do
8385 a preliminary section layout so that we know the TLS segment
8386 offsets. We can't optimize earlier because some optimizations need
8387 to know the tp offset, and we need to optimize before allocating
8388 dynamic relocations. */
8389
8390bfd_boolean
33c0ec9d 8391ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8392{
8393 bfd *ibfd;
8394 asection *sec;
8395 struct ppc_link_hash_table *htab;
663a1470 8396 unsigned char *toc_ref;
102890f0 8397 int pass;
951fd09b 8398
3cbc1e5e 8399 if (!bfd_link_executable (info))
411e1bfb
AM
8400 return TRUE;
8401
951fd09b 8402 htab = ppc_hash_table (info);
4dfe6ac6
NC
8403 if (htab == NULL)
8404 return FALSE;
8405
663a1470
AM
8406 /* Make two passes over the relocs. On the first pass, mark toc
8407 entries involved with tls relocs, and check that tls relocs
8408 involved in setting up a tls_get_addr call are indeed followed by
8409 such a call. If they are not, we can't do any tls optimization.
8410 On the second pass twiddle tls_mask flags to notify
8411 relocate_section that optimization can be done, and adjust got
8412 and plt refcounts. */
8413 toc_ref = NULL;
8414 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8415 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8416 {
8417 Elf_Internal_Sym *locsyms = NULL;
8418 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8419
102890f0
AM
8420 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8421 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8422 {
8423 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8424 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8425
102890f0
AM
8426 /* Read the relocations. */
8427 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8428 info->keep_memory);
8429 if (relstart == NULL)
2915c55b
JK
8430 {
8431 free (toc_ref);
8432 return FALSE;
8433 }
411e1bfb 8434
102890f0
AM
8435 relend = relstart + sec->reloc_count;
8436 for (rel = relstart; rel < relend; rel++)
8437 {
8438 enum elf_ppc64_reloc_type r_type;
8439 unsigned long r_symndx;
8440 struct elf_link_hash_entry *h;
8441 Elf_Internal_Sym *sym;
8442 asection *sym_sec;
f961d9dd
AM
8443 unsigned char *tls_mask;
8444 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8445 bfd_vma value;
8446 bfd_boolean ok_tprel, is_local;
8447 long toc_ref_index = 0;
8448 int expecting_tls_get_addr = 0;
663a1470 8449 bfd_boolean ret = FALSE;
411e1bfb 8450
102890f0
AM
8451 r_symndx = ELF64_R_SYM (rel->r_info);
8452 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8453 r_symndx, ibfd))
8454 {
8455 err_free_rel:
8456 if (elf_section_data (sec)->relocs != relstart)
8457 free (relstart);
8458 if (toc_ref != NULL)
8459 free (toc_ref);
8460 if (locsyms != NULL
0ffa91dd 8461 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8462 != (unsigned char *) locsyms))
8463 free (locsyms);
663a1470 8464 return ret;
102890f0 8465 }
411e1bfb 8466
102890f0
AM
8467 if (h != NULL)
8468 {
766bc656
AM
8469 if (h->root.type == bfd_link_hash_defined
8470 || h->root.type == bfd_link_hash_defweak)
8471 value = h->root.u.def.value;
8472 else if (h->root.type == bfd_link_hash_undefweak)
8473 value = 0;
8474 else
663a1470
AM
8475 {
8476 found_tls_get_addr_arg = 0;
8477 continue;
8478 }
102890f0
AM
8479 }
8480 else
8481 /* Symbols referenced by TLS relocs must be of type
8482 STT_TLS. So no need for .opd local sym adjust. */
8483 value = sym->st_value;
8484
8485 ok_tprel = FALSE;
8486 is_local = FALSE;
8487 if (h == NULL
8488 || !h->def_dynamic)
8489 {
8490 is_local = TRUE;
766bc656
AM
8491 if (h != NULL
8492 && h->root.type == bfd_link_hash_undefweak)
8493 ok_tprel = TRUE;
c27b8c2a
AM
8494 else if (sym_sec != NULL
8495 && sym_sec->output_section != NULL)
766bc656
AM
8496 {
8497 value += sym_sec->output_offset;
8498 value += sym_sec->output_section->vma;
8499 value -= htab->elf.tls_sec->vma;
8500 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8501 < (bfd_vma) 1 << 32);
8502 }
102890f0 8503 }
951fd09b 8504
102890f0 8505 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8506 /* If this section has old-style __tls_get_addr calls
8507 without marker relocs, then check that each
8508 __tls_get_addr call reloc is preceded by a reloc
8509 that conceivably belongs to the __tls_get_addr arg
8510 setup insn. If we don't find matching arg setup
8511 relocs, don't do any tls optimization. */
8512 if (pass == 0
8513 && sec->has_tls_get_addr_call
8514 && h != NULL
8515 && (h == &htab->tls_get_addr->elf
8516 || h == &htab->tls_get_addr_fd->elf)
8517 && !found_tls_get_addr_arg
8518 && is_branch_reloc (r_type))
8519 {
25f53a85 8520 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8521 "TLS optimization disabled\n"),
8522 ibfd, sec, rel->r_offset);
8523 ret = TRUE;
8524 goto err_free_rel;
8525 }
8526
8527 found_tls_get_addr_arg = 0;
102890f0
AM
8528 switch (r_type)
8529 {
8530 case R_PPC64_GOT_TLSLD16:
8531 case R_PPC64_GOT_TLSLD16_LO:
8532 expecting_tls_get_addr = 1;
663a1470 8533 found_tls_get_addr_arg = 1;
1a0670f3 8534 /* Fall through. */
102890f0
AM
8535
8536 case R_PPC64_GOT_TLSLD16_HI:
8537 case R_PPC64_GOT_TLSLD16_HA:
8538 /* These relocs should never be against a symbol
8539 defined in a shared lib. Leave them alone if
8540 that turns out to be the case. */
8541 if (!is_local)
8542 continue;
411e1bfb 8543
102890f0 8544 /* LD -> LE */
411e1bfb 8545 tls_set = 0;
102890f0
AM
8546 tls_clear = TLS_LD;
8547 tls_type = TLS_TLS | TLS_LD;
8548 break;
411e1bfb 8549
102890f0
AM
8550 case R_PPC64_GOT_TLSGD16:
8551 case R_PPC64_GOT_TLSGD16_LO:
8552 expecting_tls_get_addr = 1;
663a1470 8553 found_tls_get_addr_arg = 1;
1a0670f3 8554 /* Fall through. */
102890f0
AM
8555
8556 case R_PPC64_GOT_TLSGD16_HI:
8557 case R_PPC64_GOT_TLSGD16_HA:
8558 if (ok_tprel)
8559 /* GD -> LE */
411e1bfb 8560 tls_set = 0;
102890f0
AM
8561 else
8562 /* GD -> IE */
8563 tls_set = TLS_TLS | TLS_TPRELGD;
8564 tls_clear = TLS_GD;
8565 tls_type = TLS_TLS | TLS_GD;
8566 break;
8567
8568 case R_PPC64_GOT_TPREL16_DS:
8569 case R_PPC64_GOT_TPREL16_LO_DS:
8570 case R_PPC64_GOT_TPREL16_HI:
8571 case R_PPC64_GOT_TPREL16_HA:
8572 if (ok_tprel)
8573 {
8574 /* IE -> LE */
8575 tls_set = 0;
8576 tls_clear = TLS_TPREL;
8577 tls_type = TLS_TLS | TLS_TPREL;
8578 break;
8579 }
411e1bfb
AM
8580 continue;
8581
727fc41e
AM
8582 case R_PPC64_TLSGD:
8583 case R_PPC64_TLSLD:
663a1470 8584 found_tls_get_addr_arg = 1;
1a0670f3 8585 /* Fall through. */
663a1470
AM
8586
8587 case R_PPC64_TLS:
8588 case R_PPC64_TOC16:
8589 case R_PPC64_TOC16_LO:
102890f0
AM
8590 if (sym_sec == NULL || sym_sec != toc)
8591 continue;
8592
8593 /* Mark this toc entry as referenced by a TLS
8594 code sequence. We can do that now in the
8595 case of R_PPC64_TLS, and after checking for
8596 tls_get_addr for the TOC16 relocs. */
8597 if (toc_ref == NULL)
663a1470
AM
8598 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8599 if (toc_ref == NULL)
8600 goto err_free_rel;
8601
102890f0
AM
8602 if (h != NULL)
8603 value = h->root.u.def.value;
8604 else
8605 value = sym->st_value;
8606 value += rel->r_addend;
73242275
AM
8607 if (value % 8 != 0)
8608 continue;
8609 BFD_ASSERT (value < toc->size
8610 && toc->output_offset % 8 == 0);
663a1470 8611 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8612 if (r_type == R_PPC64_TLS
8613 || r_type == R_PPC64_TLSGD
8614 || r_type == R_PPC64_TLSLD)
102890f0
AM
8615 {
8616 toc_ref[toc_ref_index] = 1;
8617 continue;
8618 }
8619
8620 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8621 continue;
8622
8623 tls_set = 0;
8624 tls_clear = 0;
8625 expecting_tls_get_addr = 2;
8626 break;
8627
8628 case R_PPC64_TPREL64:
8629 if (pass == 0
8630 || sec != toc
8631 || toc_ref == NULL
663a1470 8632 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8633 continue;
8634 if (ok_tprel)
8635 {
8636 /* IE -> LE */
8637 tls_set = TLS_EXPLICIT;
8638 tls_clear = TLS_TPREL;
8639 break;
8640 }
8641 continue;
8642
8643 case R_PPC64_DTPMOD64:
8644 if (pass == 0
8645 || sec != toc
8646 || toc_ref == NULL
663a1470 8647 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8648 continue;
8649 if (rel + 1 < relend
8650 && (rel[1].r_info
8651 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8652 && rel[1].r_offset == rel->r_offset + 8)
8653 {
8654 if (ok_tprel)
8655 /* GD -> LE */
8656 tls_set = TLS_EXPLICIT | TLS_GD;
8657 else
8658 /* GD -> IE */
8659 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8660 tls_clear = TLS_GD;
8661 }
8662 else
8663 {
8664 if (!is_local)
8665 continue;
8666
8667 /* LD -> LE */
8668 tls_set = TLS_EXPLICIT;
8669 tls_clear = TLS_LD;
8670 }
8671 break;
8672
8673 default:
8674 continue;
8675 }
8676
8677 if (pass == 0)
8678 {
727fc41e
AM
8679 if (!expecting_tls_get_addr
8680 || !sec->has_tls_get_addr_call)
102890f0
AM
8681 continue;
8682
3a71aa26
AM
8683 if (rel + 1 < relend
8684 && branch_reloc_hash_match (ibfd, rel + 1,
8685 htab->tls_get_addr,
8686 htab->tls_get_addr_fd))
102890f0 8687 {
3a71aa26 8688 if (expecting_tls_get_addr == 2)
102890f0 8689 {
3a71aa26 8690 /* Check for toc tls entries. */
f961d9dd 8691 unsigned char *toc_tls;
3a71aa26
AM
8692 int retval;
8693
8694 retval = get_tls_mask (&toc_tls, NULL, NULL,
8695 &locsyms,
8696 rel, ibfd);
8697 if (retval == 0)
8698 goto err_free_rel;
663a1470
AM
8699 if (toc_tls != NULL)
8700 {
8701 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8702 found_tls_get_addr_arg = 1;
8703 if (retval > 1)
8704 toc_ref[toc_ref_index] = 1;
8705 }
102890f0 8706 }
3a71aa26 8707 continue;
102890f0
AM
8708 }
8709
8710 if (expecting_tls_get_addr != 1)
8711 continue;
8712
8713 /* Uh oh, we didn't find the expected call. We
8714 could just mark this symbol to exclude it
8715 from tls optimization but it's safer to skip
663a1470 8716 the entire optimization. */
695344c0 8717 /* xgettext:c-format */
25f53a85 8718 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8719 "TLS optimization disabled\n"),
8720 ibfd, sec, rel->r_offset);
8721 ret = TRUE;
8722 goto err_free_rel;
102890f0
AM
8723 }
8724
85f7a9cb 8725 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8726 {
8727 struct plt_entry *ent;
8728 for (ent = htab->tls_get_addr->elf.plt.plist;
8729 ent != NULL;
8730 ent = ent->next)
8731 if (ent->addend == 0)
411e1bfb 8732 {
102890f0 8733 if (ent->plt.refcount > 0)
30038c59 8734 {
102890f0
AM
8735 ent->plt.refcount -= 1;
8736 expecting_tls_get_addr = 0;
30038c59 8737 }
102890f0 8738 break;
411e1bfb 8739 }
102890f0 8740 }
411e1bfb 8741
85f7a9cb 8742 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8743 {
8744 struct plt_entry *ent;
8745 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8746 ent != NULL;
8747 ent = ent->next)
8748 if (ent->addend == 0)
411e1bfb 8749 {
102890f0
AM
8750 if (ent->plt.refcount > 0)
8751 ent->plt.refcount -= 1;
8752 break;
411e1bfb 8753 }
102890f0 8754 }
411e1bfb 8755
102890f0 8756 if (tls_clear == 0)
30038c59
AM
8757 continue;
8758
102890f0
AM
8759 if ((tls_set & TLS_EXPLICIT) == 0)
8760 {
8761 struct got_entry *ent;
411e1bfb 8762
102890f0
AM
8763 /* Adjust got entry for this reloc. */
8764 if (h != NULL)
8765 ent = h->got.glist;
8766 else
8767 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8768
102890f0
AM
8769 for (; ent != NULL; ent = ent->next)
8770 if (ent->addend == rel->r_addend
8771 && ent->owner == ibfd
8772 && ent->tls_type == tls_type)
8773 break;
8774 if (ent == NULL)
8775 abort ();
411e1bfb 8776
102890f0
AM
8777 if (tls_set == 0)
8778 {
8779 /* We managed to get rid of a got entry. */
8780 if (ent->got.refcount > 0)
8781 ent->got.refcount -= 1;
8782 }
8783 }
8784 else
8785 {
8786 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8787 we'll lose one or two dyn relocs. */
8788 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8789 NULL, h, sym))
102890f0 8790 return FALSE;
411e1bfb 8791
102890f0
AM
8792 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8793 {
8794 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8795 NULL, h, sym))
102890f0
AM
8796 return FALSE;
8797 }
8798 }
411e1bfb 8799
102890f0
AM
8800 *tls_mask |= tls_set;
8801 *tls_mask &= ~tls_clear;
8802 }
8c1d1bb8 8803
102890f0
AM
8804 if (elf_section_data (sec)->relocs != relstart)
8805 free (relstart);
8806 }
411e1bfb 8807
663a1470
AM
8808 if (locsyms != NULL
8809 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8810 {
8811 if (!info->keep_memory)
8812 free (locsyms);
8813 else
8814 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8815 }
8816 }
411e1bfb 8817
663a1470
AM
8818 if (toc_ref != NULL)
8819 free (toc_ref);
b34976b6 8820 return TRUE;
1e2f5b6e 8821}
b34976b6 8822
c5614fa4
AM
8823/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8824 the values of any global symbols in a toc section that has been
8825 edited. Globals in toc sections should be a rarity, so this function
8826 sets a flag if any are found in toc sections other than the one just
8827 edited, so that futher hash table traversals can be avoided. */
8828
8829struct adjust_toc_info
8830{
8831 asection *toc;
8832 unsigned long *skip;
8833 bfd_boolean global_toc_syms;
8834};
8835
ba761f19
AM
8836enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8837
c5614fa4
AM
8838static bfd_boolean
8839adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8840{
8841 struct ppc_link_hash_entry *eh;
8842 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8843 unsigned long i;
c5614fa4 8844
c5614fa4
AM
8845 if (h->root.type != bfd_link_hash_defined
8846 && h->root.type != bfd_link_hash_defweak)
8847 return TRUE;
8848
8849 eh = (struct ppc_link_hash_entry *) h;
8850 if (eh->adjust_done)
8851 return TRUE;
8852
8853 if (eh->elf.root.u.def.section == toc_inf->toc)
8854 {
854b41e7
AM
8855 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8856 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8857 else
854b41e7
AM
8858 i = eh->elf.root.u.def.value >> 3;
8859
ba761f19 8860 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8861 {
4eca0228 8862 _bfd_error_handler
854b41e7
AM
8863 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8864 do
8865 ++i;
ba761f19 8866 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8867 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8868 }
854b41e7
AM
8869
8870 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8871 eh->adjust_done = 1;
8872 }
8873 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8874 toc_inf->global_toc_syms = TRUE;
8875
8876 return TRUE;
8877}
8878
560c8763
AM
8879/* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8880
8881static bfd_boolean
8882ok_lo_toc_insn (unsigned int insn)
8883{
8884 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8885 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8886 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8887 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8888 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8889 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8890 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8891 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8892 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8893 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8894 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8895 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8896 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8897 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8898 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8899 && (insn & 3) != 1)
8900 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8901 && ((insn & 3) == 0 || (insn & 3) == 3))
8902 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8903}
8904
c5614fa4
AM
8905/* Examine all relocs referencing .toc sections in order to remove
8906 unused .toc entries. */
8907
8908bfd_boolean
33c0ec9d 8909ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8910{
8911 bfd *ibfd;
8912 struct adjust_toc_info toc_inf;
67f0cbdb 8913 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8914
67f0cbdb 8915 htab->do_toc_opt = 1;
c5614fa4 8916 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8917 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8918 {
8919 asection *toc, *sec;
8920 Elf_Internal_Shdr *symtab_hdr;
8921 Elf_Internal_Sym *local_syms;
425b145b 8922 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8923 unsigned long *skip, *drop;
8924 unsigned char *used;
8925 unsigned char *keep, last, some_unused;
8926
854b41e7
AM
8927 if (!is_ppc64_elf (ibfd))
8928 continue;
8929
c5614fa4
AM
8930 toc = bfd_get_section_by_name (ibfd, ".toc");
8931 if (toc == NULL
92b7a70f 8932 || toc->size == 0
dbaa2011
AM
8933 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8934 || discarded_section (toc))
c5614fa4
AM
8935 continue;
8936
425b145b 8937 toc_relocs = NULL;
c5614fa4 8938 local_syms = NULL;
0ffa91dd 8939 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8940
8941 /* Look at sections dropped from the final link. */
8942 skip = NULL;
8943 relstart = NULL;
8944 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8945 {
8946 if (sec->reloc_count == 0
dbaa2011 8947 || !discarded_section (sec)
c5614fa4
AM
8948 || get_opd_info (sec)
8949 || (sec->flags & SEC_ALLOC) == 0
8950 || (sec->flags & SEC_DEBUGGING) != 0)
8951 continue;
8952
8953 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8954 if (relstart == NULL)
8955 goto error_ret;
8956
8957 /* Run through the relocs to see which toc entries might be
8958 unused. */
8959 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8960 {
8961 enum elf_ppc64_reloc_type r_type;
8962 unsigned long r_symndx;
8963 asection *sym_sec;
8964 struct elf_link_hash_entry *h;
8965 Elf_Internal_Sym *sym;
8966 bfd_vma val;
8967
8968 r_type = ELF64_R_TYPE (rel->r_info);
8969 switch (r_type)
8970 {
8971 default:
8972 continue;
8973
8974 case R_PPC64_TOC16:
8975 case R_PPC64_TOC16_LO:
8976 case R_PPC64_TOC16_HI:
8977 case R_PPC64_TOC16_HA:
8978 case R_PPC64_TOC16_DS:
8979 case R_PPC64_TOC16_LO_DS:
8980 break;
8981 }
8982
8983 r_symndx = ELF64_R_SYM (rel->r_info);
8984 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8985 r_symndx, ibfd))
8986 goto error_ret;
8987
8988 if (sym_sec != toc)
8989 continue;
8990
8991 if (h != NULL)
8992 val = h->root.u.def.value;
8993 else
8994 val = sym->st_value;
8995 val += rel->r_addend;
8996
8997 if (val >= toc->size)
8998 continue;
8999
9000 /* Anything in the toc ought to be aligned to 8 bytes.
9001 If not, don't mark as unused. */
9002 if (val & 7)
9003 continue;
9004
9005 if (skip == NULL)
9006 {
854b41e7 9007 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9008 if (skip == NULL)
9009 goto error_ret;
9010 }
9011
ba761f19 9012 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9013 }
9014
9015 if (elf_section_data (sec)->relocs != relstart)
9016 free (relstart);
9017 }
9018
ba761f19
AM
9019 /* For largetoc loads of address constants, we can convert
9020 . addis rx,2,addr@got@ha
9021 . ld ry,addr@got@l(rx)
9022 to
9023 . addis rx,2,addr@toc@ha
9024 . addi ry,rx,addr@toc@l
9025 when addr is within 2G of the toc pointer. This then means
9026 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9027
ba761f19
AM
9028 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9029 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9030 && toc->reloc_count != 0)
9031 {
9032 /* Read toc relocs. */
425b145b
AM
9033 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9034 info->keep_memory);
9035 if (toc_relocs == NULL)
ba761f19
AM
9036 goto error_ret;
9037
425b145b 9038 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9039 {
9040 enum elf_ppc64_reloc_type r_type;
9041 unsigned long r_symndx;
9042 asection *sym_sec;
9043 struct elf_link_hash_entry *h;
9044 Elf_Internal_Sym *sym;
9045 bfd_vma val, addr;
9046
9047 r_type = ELF64_R_TYPE (rel->r_info);
9048 if (r_type != R_PPC64_ADDR64)
9049 continue;
9050
9051 r_symndx = ELF64_R_SYM (rel->r_info);
9052 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9053 r_symndx, ibfd))
9054 goto error_ret;
9055
425b145b 9056 if (sym_sec == NULL
c27b8c2a 9057 || sym_sec->output_section == NULL
dbaa2011 9058 || discarded_section (sym_sec))
425b145b
AM
9059 continue;
9060
afe397ea 9061 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9062 continue;
9063
9064 if (h != NULL)
bddc25c9
AM
9065 {
9066 if (h->type == STT_GNU_IFUNC)
9067 continue;
9068 val = h->root.u.def.value;
9069 }
ba761f19 9070 else
bddc25c9
AM
9071 {
9072 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9073 continue;
9074 val = sym->st_value;
9075 }
ba761f19
AM
9076 val += rel->r_addend;
9077 val += sym_sec->output_section->vma + sym_sec->output_offset;
9078
9079 /* We don't yet know the exact toc pointer value, but we
9080 know it will be somewhere in the toc section. Don't
9081 optimize if the difference from any possible toc
9082 pointer is outside [ff..f80008000, 7fff7fff]. */
9083 addr = toc->output_section->vma + TOC_BASE_OFF;
9084 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9085 continue;
9086
9087 addr = toc->output_section->vma + toc->output_section->rawsize;
9088 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9089 continue;
9090
9091 if (skip == NULL)
9092 {
9093 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9094 if (skip == NULL)
9095 goto error_ret;
9096 }
9097
9098 skip[rel->r_offset >> 3]
425b145b 9099 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9100 }
ba761f19
AM
9101 }
9102
c5614fa4
AM
9103 if (skip == NULL)
9104 continue;
9105
9106 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9107 if (used == NULL)
9108 {
9109 error_ret:
9110 if (local_syms != NULL
9111 && symtab_hdr->contents != (unsigned char *) local_syms)
9112 free (local_syms);
9113 if (sec != NULL
9114 && relstart != NULL
9115 && elf_section_data (sec)->relocs != relstart)
9116 free (relstart);
425b145b
AM
9117 if (toc_relocs != NULL
9118 && elf_section_data (toc)->relocs != toc_relocs)
9119 free (toc_relocs);
c5614fa4
AM
9120 if (skip != NULL)
9121 free (skip);
9122 return FALSE;
9123 }
9124
30038c59
AM
9125 /* Now check all kept sections that might reference the toc.
9126 Check the toc itself last. */
9127 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9128 : ibfd->sections);
c5614fa4 9129 sec != NULL;
c5614fa4 9130 sec = (sec == toc ? NULL
c5614fa4 9131 : sec->next == NULL ? toc
30038c59 9132 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9133 : sec->next))
9134 {
9135 int repeat;
9136
9137 if (sec->reloc_count == 0
dbaa2011 9138 || discarded_section (sec)
c5614fa4
AM
9139 || get_opd_info (sec)
9140 || (sec->flags & SEC_ALLOC) == 0
9141 || (sec->flags & SEC_DEBUGGING) != 0)
9142 continue;
9143
854b41e7
AM
9144 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9145 info->keep_memory);
c5614fa4 9146 if (relstart == NULL)
2915c55b
JK
9147 {
9148 free (used);
9149 goto error_ret;
9150 }
c5614fa4
AM
9151
9152 /* Mark toc entries referenced as used. */
c5614fa4 9153 do
d4f1ee75
AM
9154 {
9155 repeat = 0;
9156 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9157 {
9158 enum elf_ppc64_reloc_type r_type;
9159 unsigned long r_symndx;
9160 asection *sym_sec;
9161 struct elf_link_hash_entry *h;
9162 Elf_Internal_Sym *sym;
9163 bfd_vma val;
9164 enum {no_check, check_lo, check_ha} insn_check;
98528052 9165
d4f1ee75
AM
9166 r_type = ELF64_R_TYPE (rel->r_info);
9167 switch (r_type)
9168 {
9169 default:
9170 insn_check = no_check;
9171 break;
98528052 9172
d4f1ee75
AM
9173 case R_PPC64_GOT_TLSLD16_HA:
9174 case R_PPC64_GOT_TLSGD16_HA:
9175 case R_PPC64_GOT_TPREL16_HA:
9176 case R_PPC64_GOT_DTPREL16_HA:
9177 case R_PPC64_GOT16_HA:
9178 case R_PPC64_TOC16_HA:
9179 insn_check = check_ha;
9180 break;
98528052 9181
d4f1ee75
AM
9182 case R_PPC64_GOT_TLSLD16_LO:
9183 case R_PPC64_GOT_TLSGD16_LO:
9184 case R_PPC64_GOT_TPREL16_LO_DS:
9185 case R_PPC64_GOT_DTPREL16_LO_DS:
9186 case R_PPC64_GOT16_LO:
9187 case R_PPC64_GOT16_LO_DS:
9188 case R_PPC64_TOC16_LO:
9189 case R_PPC64_TOC16_LO_DS:
9190 insn_check = check_lo;
9191 break;
9192 }
560c8763 9193
d4f1ee75
AM
9194 if (insn_check != no_check)
9195 {
9196 bfd_vma off = rel->r_offset & ~3;
9197 unsigned char buf[4];
9198 unsigned int insn;
c5614fa4 9199
d4f1ee75
AM
9200 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9201 {
9202 free (used);
9203 goto error_ret;
9204 }
9205 insn = bfd_get_32 (ibfd, buf);
9206 if (insn_check == check_lo
9207 ? !ok_lo_toc_insn (insn)
9208 : ((insn & ((0x3f << 26) | 0x1f << 16))
9209 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9210 {
9211 char str[12];
9212
9213 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9214 sprintf (str, "%#08x", insn);
9215 info->callbacks->einfo
695344c0 9216 /* xgettext:c-format */
d4f1ee75
AM
9217 (_("%P: %H: toc optimization is not supported for"
9218 " %s instruction.\n"),
9219 ibfd, sec, rel->r_offset & ~3, str);
9220 }
9221 }
c5614fa4 9222
d4f1ee75
AM
9223 switch (r_type)
9224 {
9225 case R_PPC64_TOC16:
9226 case R_PPC64_TOC16_LO:
9227 case R_PPC64_TOC16_HI:
9228 case R_PPC64_TOC16_HA:
9229 case R_PPC64_TOC16_DS:
9230 case R_PPC64_TOC16_LO_DS:
9231 /* In case we're taking addresses of toc entries. */
9232 case R_PPC64_ADDR64:
9233 break;
c5614fa4 9234
d4f1ee75
AM
9235 default:
9236 continue;
9237 }
c5614fa4 9238
d4f1ee75
AM
9239 r_symndx = ELF64_R_SYM (rel->r_info);
9240 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9241 r_symndx, ibfd))
9242 {
9243 free (used);
9244 goto error_ret;
9245 }
c5614fa4 9246
d4f1ee75
AM
9247 if (sym_sec != toc)
9248 continue;
c5614fa4 9249
d4f1ee75
AM
9250 if (h != NULL)
9251 val = h->root.u.def.value;
9252 else
9253 val = sym->st_value;
9254 val += rel->r_addend;
ba761f19 9255
d4f1ee75
AM
9256 if (val >= toc->size)
9257 continue;
ba761f19 9258
d4f1ee75
AM
9259 if ((skip[val >> 3] & can_optimize) != 0)
9260 {
9261 bfd_vma off;
9262 unsigned char opc;
9263
9264 switch (r_type)
9265 {
9266 case R_PPC64_TOC16_HA:
ba761f19 9267 break;
ba761f19 9268
d4f1ee75
AM
9269 case R_PPC64_TOC16_LO_DS:
9270 off = rel->r_offset;
9271 off += (bfd_big_endian (ibfd) ? -2 : 3);
9272 if (!bfd_get_section_contents (ibfd, sec, &opc,
9273 off, 1))
9274 {
9275 free (used);
9276 goto error_ret;
9277 }
9278 if ((opc & (0x3f << 2)) == (58u << 2))
9279 break;
1a0670f3 9280 /* Fall through. */
ba761f19 9281
d4f1ee75
AM
9282 default:
9283 /* Wrong sort of reloc, or not a ld. We may
9284 as well clear ref_from_discarded too. */
9285 skip[val >> 3] = 0;
9286 }
9287 }
9288
9289 if (sec != toc)
9290 used[val >> 3] = 1;
9291 /* For the toc section, we only mark as used if this
9292 entry itself isn't unused. */
9293 else if ((used[rel->r_offset >> 3]
9294 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9295 && !used[val >> 3])
9296 {
9297 /* Do all the relocs again, to catch reference
9298 chains. */
9299 repeat = 1;
9300 used[val >> 3] = 1;
9301 }
9302 }
9303 }
c5614fa4 9304 while (repeat);
854b41e7
AM
9305
9306 if (elf_section_data (sec)->relocs != relstart)
9307 free (relstart);
c5614fa4
AM
9308 }
9309
9310 /* Merge the used and skip arrays. Assume that TOC
9311 doublewords not appearing as either used or unused belong
9312 to to an entry more than one doubleword in size. */
9313 for (drop = skip, keep = used, last = 0, some_unused = 0;
9314 drop < skip + (toc->size + 7) / 8;
9315 ++drop, ++keep)
9316 {
9317 if (*keep)
9318 {
ba761f19
AM
9319 *drop &= ~ref_from_discarded;
9320 if ((*drop & can_optimize) != 0)
9321 some_unused = 1;
c5614fa4
AM
9322 last = 0;
9323 }
b140b010 9324 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9325 {
9326 some_unused = 1;
ba761f19 9327 last = ref_from_discarded;
c5614fa4
AM
9328 }
9329 else
9330 *drop = last;
9331 }
9332
9333 free (used);
9334
9335 if (some_unused)
9336 {
9337 bfd_byte *contents, *src;
9338 unsigned long off;
d62b3684 9339 Elf_Internal_Sym *sym;
ba761f19 9340 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9341
9342 /* Shuffle the toc contents, and at the same time convert the
9343 skip array from booleans into offsets. */
9344 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9345 goto error_ret;
9346
9347 elf_section_data (toc)->this_hdr.contents = contents;
9348
9349 for (src = contents, off = 0, drop = skip;
9350 src < contents + toc->size;
9351 src += 8, ++drop)
9352 {
ba761f19
AM
9353 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9354 off += 8;
c5614fa4
AM
9355 else if (off != 0)
9356 {
9357 *drop = off;
9358 memcpy (src - off, src, 8);
9359 }
9360 }
854b41e7 9361 *drop = off;
c5614fa4
AM
9362 toc->rawsize = toc->size;
9363 toc->size = src - contents - off;
9364
ba761f19
AM
9365 /* Adjust addends for relocs against the toc section sym,
9366 and optimize any accesses we can. */
c5614fa4
AM
9367 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9368 {
9369 if (sec->reloc_count == 0
dbaa2011 9370 || discarded_section (sec))
c5614fa4
AM
9371 continue;
9372
9373 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9374 info->keep_memory);
c5614fa4
AM
9375 if (relstart == NULL)
9376 goto error_ret;
9377
9378 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9379 {
9380 enum elf_ppc64_reloc_type r_type;
9381 unsigned long r_symndx;
9382 asection *sym_sec;
9383 struct elf_link_hash_entry *h;
854b41e7 9384 bfd_vma val;
c5614fa4
AM
9385
9386 r_type = ELF64_R_TYPE (rel->r_info);
9387 switch (r_type)
9388 {
9389 default:
9390 continue;
9391
9392 case R_PPC64_TOC16:
9393 case R_PPC64_TOC16_LO:
9394 case R_PPC64_TOC16_HI:
9395 case R_PPC64_TOC16_HA:
9396 case R_PPC64_TOC16_DS:
9397 case R_PPC64_TOC16_LO_DS:
9398 case R_PPC64_ADDR64:
9399 break;
9400 }
9401
9402 r_symndx = ELF64_R_SYM (rel->r_info);
9403 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9404 r_symndx, ibfd))
9405 goto error_ret;
9406
ba761f19 9407 if (sym_sec != toc)
c5614fa4
AM
9408 continue;
9409
ba761f19
AM
9410 if (h != NULL)
9411 val = h->root.u.def.value;
9412 else
9413 {
9414 val = sym->st_value;
9415 if (val != 0)
9416 local_toc_syms = TRUE;
9417 }
9418
9419 val += rel->r_addend;
854b41e7
AM
9420
9421 if (val > toc->rawsize)
9422 val = toc->rawsize;
ba761f19
AM
9423 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9424 continue;
9425 else if ((skip[val >> 3] & can_optimize) != 0)
9426 {
9427 Elf_Internal_Rela *tocrel
425b145b 9428 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9429 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9430
9431 switch (r_type)
9432 {
9433 case R_PPC64_TOC16_HA:
9434 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9435 break;
9436
9437 case R_PPC64_TOC16_LO_DS:
9438 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9439 break;
9440
9441 default:
28942f62
AM
9442 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9443 ppc_howto_init ();
b140b010 9444 info->callbacks->einfo
695344c0 9445 /* xgettext:c-format */
bc30df16 9446 (_("%P: %H: %s references "
b140b010
AM
9447 "optimized away TOC entry\n"),
9448 ibfd, sec, rel->r_offset,
9449 ppc64_elf_howto_table[r_type]->name);
9450 bfd_set_error (bfd_error_bad_value);
9451 goto error_ret;
ba761f19
AM
9452 }
9453 rel->r_addend = tocrel->r_addend;
9454 elf_section_data (sec)->relocs = relstart;
9455 continue;
9456 }
9457
9458 if (h != NULL || sym->st_value != 0)
9459 continue;
854b41e7
AM
9460
9461 rel->r_addend -= skip[val >> 3];
9462 elf_section_data (sec)->relocs = relstart;
c5614fa4 9463 }
854b41e7
AM
9464
9465 if (elf_section_data (sec)->relocs != relstart)
9466 free (relstart);
c5614fa4
AM
9467 }
9468
9469 /* We shouldn't have local or global symbols defined in the TOC,
9470 but handle them anyway. */
df22d223
AM
9471 if (local_syms != NULL)
9472 for (sym = local_syms;
9473 sym < local_syms + symtab_hdr->sh_info;
9474 ++sym)
9475 if (sym->st_value != 0
9476 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9477 {
9478 unsigned long i;
854b41e7 9479
df22d223
AM
9480 if (sym->st_value > toc->rawsize)
9481 i = toc->rawsize >> 3;
9482 else
9483 i = sym->st_value >> 3;
854b41e7 9484
df22d223
AM
9485 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9486 {
9487 if (local_toc_syms)
4eca0228 9488 _bfd_error_handler
df22d223
AM
9489 (_("%s defined on removed toc entry"),
9490 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9491 do
9492 ++i;
9493 while ((skip[i] & (ref_from_discarded | can_optimize)));
9494 sym->st_value = (bfd_vma) i << 3;
9495 }
d62b3684 9496
df22d223
AM
9497 sym->st_value -= skip[i];
9498 symtab_hdr->contents = (unsigned char *) local_syms;
9499 }
c5614fa4 9500
854b41e7 9501 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9502 if (toc_inf.global_toc_syms)
9503 {
9504 toc_inf.toc = toc;
9505 toc_inf.skip = skip;
9506 toc_inf.global_toc_syms = FALSE;
9507 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9508 &toc_inf);
9509 }
854b41e7
AM
9510
9511 if (toc->reloc_count != 0)
9512 {
d4730f92 9513 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9514 Elf_Internal_Rela *wrel;
9515 bfd_size_type sz;
9516
854b41e7 9517 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9518 if (toc_relocs == NULL)
9519 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9520 info->keep_memory);
9521 if (toc_relocs == NULL)
9522 goto error_ret;
9523
425b145b
AM
9524 wrel = toc_relocs;
9525 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9526 if ((skip[rel->r_offset >> 3]
9527 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9528 {
9529 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9530 wrel->r_info = rel->r_info;
9531 wrel->r_addend = rel->r_addend;
9532 ++wrel;
9533 }
9534 else if (!dec_dynrel_count (rel->r_info, toc, info,
9535 &local_syms, NULL, NULL))
9536 goto error_ret;
9537
425b145b
AM
9538 elf_section_data (toc)->relocs = toc_relocs;
9539 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9540 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9541 sz = rel_hdr->sh_entsize;
9542 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9543 }
c5614fa4 9544 }
28be611c
AM
9545 else if (toc_relocs != NULL
9546 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9547 free (toc_relocs);
c5614fa4
AM
9548
9549 if (local_syms != NULL
9550 && symtab_hdr->contents != (unsigned char *) local_syms)
9551 {
9552 if (!info->keep_memory)
9553 free (local_syms);
9554 else
9555 symtab_hdr->contents = (unsigned char *) local_syms;
9556 }
9557 free (skip);
9558 }
9559
9560 return TRUE;
9561}
9562
1bbe0902
AM
9563/* Return true iff input section I references the TOC using
9564 instructions limited to +/-32k offsets. */
9565
9566bfd_boolean
9567ppc64_elf_has_small_toc_reloc (asection *i)
9568{
9569 return (is_ppc64_elf (i->owner)
9570 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9571}
9572
927be08e
AM
9573/* Allocate space for one GOT entry. */
9574
9575static void
9576allocate_got (struct elf_link_hash_entry *h,
9577 struct bfd_link_info *info,
9578 struct got_entry *gent)
9579{
9580 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9581 bfd_boolean dyn;
9582 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9583 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9584 ? 16 : 8);
9585 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9586 ? 2 : 1) * sizeof (Elf64_External_Rela);
9587 asection *got = ppc64_elf_tdata (gent->owner)->got;
9588
9589 gent->got.offset = got->size;
9590 got->size += entsize;
9591
9592 dyn = htab->elf.dynamic_sections_created;
19e08130 9593 if (h->type == STT_GNU_IFUNC)
927be08e 9594 {
33e44f2e 9595 htab->elf.irelplt->size += rentsize;
19e08130 9596 htab->got_reli_size += rentsize;
927be08e 9597 }
0e1862bb 9598 else if ((bfd_link_pic (info)
19e08130
AM
9599 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9600 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9601 || h->root.type != bfd_link_hash_undefweak))
927be08e 9602 {
19e08130 9603 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9604 relgot->size += rentsize;
927be08e
AM
9605 }
9606}
9607
7865406b
AM
9608/* This function merges got entries in the same toc group. */
9609
9610static void
9611merge_got_entries (struct got_entry **pent)
9612{
9613 struct got_entry *ent, *ent2;
9614
9615 for (ent = *pent; ent != NULL; ent = ent->next)
9616 if (!ent->is_indirect)
9617 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9618 if (!ent2->is_indirect
9619 && ent2->addend == ent->addend
9620 && ent2->tls_type == ent->tls_type
9621 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9622 {
9623 ent2->is_indirect = TRUE;
9624 ent2->got.ent = ent;
9625 }
9626}
9627
65f38f15
AM
9628/* Allocate space in .plt, .got and associated reloc sections for
9629 dynamic relocs. */
5bd4f169 9630
b34976b6 9631static bfd_boolean
4ce794b7 9632allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9633{
65f38f15
AM
9634 struct bfd_link_info *info;
9635 struct ppc_link_hash_table *htab;
5bd4f169 9636 asection *s;
65f38f15 9637 struct ppc_link_hash_entry *eh;
0b8bcf0d 9638 struct got_entry **pgent, *gent;
5bd4f169 9639
e92d460e 9640 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9641 return TRUE;
5bd4f169 9642
65f38f15
AM
9643 info = (struct bfd_link_info *) inf;
9644 htab = ppc_hash_table (info);
4dfe6ac6
NC
9645 if (htab == NULL)
9646 return FALSE;
5bd4f169 9647
951fd09b
AM
9648 eh = (struct ppc_link_hash_entry *) h;
9649 /* Run through the TLS GD got entries first if we're changing them
9650 to TPREL. */
e7b938ca 9651 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9652 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9653 if (gent->got.refcount > 0
9654 && (gent->tls_type & TLS_GD) != 0)
9655 {
9656 /* This was a GD entry that has been converted to TPREL. If
9657 there happens to be a TPREL entry we can use that one. */
9658 struct got_entry *ent;
9659 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9660 if (ent->got.refcount > 0
9661 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9662 && ent->addend == gent->addend
9663 && ent->owner == gent->owner)
951fd09b
AM
9664 {
9665 gent->got.refcount = 0;
9666 break;
9667 }
9668
9669 /* If not, then we'll be using our own TPREL entry. */
9670 if (gent->got.refcount != 0)
9671 gent->tls_type = TLS_TLS | TLS_TPREL;
9672 }
9673
7865406b
AM
9674 /* Remove any list entry that won't generate a word in the GOT before
9675 we call merge_got_entries. Otherwise we risk merging to empty
9676 entries. */
0b8bcf0d
AM
9677 pgent = &h->got.glist;
9678 while ((gent = *pgent) != NULL)
411e1bfb 9679 if (gent->got.refcount > 0)
7865406b
AM
9680 {
9681 if ((gent->tls_type & TLS_LD) != 0
9682 && !h->def_dynamic)
9683 {
9684 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9685 *pgent = gent->next;
9686 }
9687 else
9688 pgent = &gent->next;
9689 }
9690 else
9691 *pgent = gent->next;
9692
9693 if (!htab->do_multi_toc)
9694 merge_got_entries (&h->got.glist);
9695
9696 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9697 if (!gent->is_indirect)
411e1bfb
AM
9698 {
9699 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
9700 Undefined weak syms won't yet be marked as dynamic,
9701 nor will all TLS symbols. */
411e1bfb 9702 if (h->dynindx == -1
b099ab9f 9703 && !h->forced_local
25f23106 9704 && h->type != STT_GNU_IFUNC
b099ab9f 9705 && htab->elf.dynamic_sections_created)
411e1bfb 9706 {
c152c796 9707 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
9708 return FALSE;
9709 }
65f38f15 9710
0c8d6e5c 9711 if (!is_ppc64_elf (gent->owner))
927be08e 9712 abort ();
0ffa91dd 9713
927be08e 9714 allocate_got (h, info, gent);
411e1bfb 9715 }
65f38f15 9716
8a2058b5
AM
9717 if (!htab->elf.dynamic_sections_created
9718 && h->type != STT_GNU_IFUNC)
9719 eh->dyn_relocs = NULL;
9720
9721 if (eh->dyn_relocs != NULL)
65f38f15 9722 {
8a2058b5
AM
9723 struct elf_dyn_relocs *p, **pp;
9724
57e7d118
AM
9725 /* In the shared -Bsymbolic case, discard space allocated for
9726 dynamic pc-relative relocs against symbols which turn out to
9727 be defined in regular objects. For the normal shared case,
9728 discard space for relocs that have become local due to symbol
9729 visibility changes. */
9730
9731 if (bfd_link_pic (info))
65f38f15 9732 {
57e7d118
AM
9733 /* Relocs that use pc_count are those that appear on a call
9734 insn, or certain REL relocs (see must_be_dyn_reloc) that
9735 can be generated via assembly. We want calls to
9736 protected symbols to resolve directly to the function
9737 rather than going via the plt. If people want function
9738 pointer comparisons to work as expected then they should
9739 avoid writing weird assembly. */
9740 if (SYMBOL_CALLS_LOCAL (info, h))
9741 {
57e7d118
AM
9742 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9743 {
9744 p->count -= p->pc_count;
9745 p->pc_count = 0;
9746 if (p->count == 0)
9747 *pp = p->next;
9748 else
9749 pp = &p->next;
9750 }
9751 }
65f38f15 9752
57e7d118
AM
9753 /* Also discard relocs on undefined weak syms with
9754 non-default visibility. */
9755 if (eh->dyn_relocs != NULL
9756 && h->root.type == bfd_link_hash_undefweak)
5bd4f169 9757 {
57e7d118
AM
9758 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9759 eh->dyn_relocs = NULL;
9760
9761 /* Make sure this symbol is output as a dynamic symbol.
9762 Undefined weak syms won't yet be marked as dynamic. */
9763 else if (h->dynindx == -1
9764 && !h->forced_local)
9765 {
9766 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9767 return FALSE;
9768 }
5bd4f169 9769 }
65f38f15 9770 }
57e7d118 9771 else if (h->type == STT_GNU_IFUNC)
dfbb6ac9 9772 {
8a2058b5
AM
9773 /* A plt entry is always created when making direct calls to
9774 an ifunc, even when building a static executable, but
9775 that doesn't cover all cases. We may have only an ifunc
9776 initialised function pointer for a given ifunc symbol.
9777
9778 For ELFv2, dynamic relocations are not required when
9779 generating a global entry PLT stub. */
9780 if (abiversion (info->output_bfd) >= 2)
9781 {
9782 if (global_entry_stub (h))
9783 eh->dyn_relocs = NULL;
9784 }
9785
9786 /* For ELFv1 we have function descriptors. Descriptors need
9787 to be treated like PLT entries and thus have dynamic
9788 relocations. One exception is when the function
9789 descriptor is copied into .dynbss (which should only
9790 happen with ancient versions of gcc). */
9791 else if (h->needs_copy)
dfbb6ac9 9792 eh->dyn_relocs = NULL;
57e7d118
AM
9793 }
9794 else if (ELIMINATE_COPY_RELOCS)
9795 {
8a2058b5 9796 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9797 symbols which turn out to need copy relocs or are not
9798 dynamic. */
dfbb6ac9 9799
8a2058b5
AM
9800 /* First make sure this symbol is output as a dynamic symbol.
9801 Undefined weak syms won't yet be marked as dynamic. */
9802 if (h->root.type == bfd_link_hash_undefweak
9803 && !h->non_got_ref
9804 && !h->def_regular
9805 && h->dynindx == -1
9806 && !h->forced_local
9807 && !bfd_elf_link_record_dynamic_symbol (info, h))
9808 return FALSE;
57e7d118 9809
8a2058b5
AM
9810 if (h->non_got_ref
9811 || h->def_regular
9812 || h->dynindx == -1)
9813 eh->dyn_relocs = NULL;
57e7d118
AM
9814 }
9815
9816 /* Finally, allocate space. */
9817 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9818 {
9819 asection *sreloc = elf_section_data (p->sec)->sreloc;
9820 if (eh->elf.type == STT_GNU_IFUNC)
9821 sreloc = htab->elf.irelplt;
9822 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9823 }
65f38f15 9824 }
57e7d118
AM
9825
9826 if ((htab->elf.dynamic_sections_created
9827 && h->dynindx != -1
9828 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
9829 || h->type == STT_GNU_IFUNC)
65f38f15 9830 {
57e7d118
AM
9831 struct plt_entry *pent;
9832 bfd_boolean doneone = FALSE;
9833 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9834 if (pent->plt.refcount > 0)
9835 {
9836 if (!htab->elf.dynamic_sections_created
9837 || h->dynindx == -1)
9838 {
9839 s = htab->elf.iplt;
9840 pent->plt.offset = s->size;
9841 s->size += PLT_ENTRY_SIZE (htab);
9842 s = htab->elf.irelplt;
9843 }
9844 else
9845 {
9846 /* If this is the first .plt entry, make room for the special
9847 first entry. */
9848 s = htab->elf.splt;
9849 if (s->size == 0)
9850 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9851
57e7d118 9852 pent->plt.offset = s->size;
65f38f15 9853
57e7d118
AM
9854 /* Make room for this entry. */
9855 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9856
57e7d118
AM
9857 /* Make room for the .glink code. */
9858 s = htab->glink;
9859 if (s->size == 0)
9860 s->size += GLINK_CALL_STUB_SIZE;
9861 if (htab->opd_abi)
9862 {
9863 /* We need bigger stubs past index 32767. */
9864 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9865 s->size += 4;
9866 s->size += 2*4;
9867 }
9868 else
9869 s->size += 4;
65f38f15 9870
57e7d118
AM
9871 /* We also need to make an entry in the .rela.plt section. */
9872 s = htab->elf.srelplt;
9873 }
9874 s->size += sizeof (Elf64_External_Rela);
9875 doneone = TRUE;
9876 }
9877 else
9878 pent->plt.offset = (bfd_vma) -1;
9879 if (!doneone)
9880 {
9881 h->plt.plist = NULL;
9882 h->needs_plt = 0;
9883 }
65f38f15 9884 }
57e7d118 9885 else
65f38f15 9886 {
57e7d118
AM
9887 h->plt.plist = NULL;
9888 h->needs_plt = 0;
65f38f15
AM
9889 }
9890
b34976b6 9891 return TRUE;
65f38f15
AM
9892}
9893
a345bc8d
AM
9894/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9895 to set up space for global entry stubs. These are put in glink,
9896 after the branch table. */
65f38f15 9897
b34976b6 9898static bfd_boolean
a345bc8d 9899size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9900{
a345bc8d
AM
9901 struct bfd_link_info *info;
9902 struct ppc_link_hash_table *htab;
9903 struct plt_entry *pent;
9904 asection *s;
65f38f15 9905
a345bc8d
AM
9906 if (h->root.type == bfd_link_hash_indirect)
9907 return TRUE;
65f38f15 9908
a345bc8d
AM
9909 if (!h->pointer_equality_needed)
9910 return TRUE;
65f38f15 9911
a345bc8d
AM
9912 if (h->def_regular)
9913 return TRUE;
65f38f15 9914
a345bc8d
AM
9915 info = inf;
9916 htab = ppc_hash_table (info);
9917 if (htab == NULL)
9918 return FALSE;
9919
9920 s = htab->glink;
9921 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9922 if (pent->plt.offset != (bfd_vma) -1
9923 && pent->addend == 0)
9924 {
afe397ea
AM
9925 /* For ELFv2, if this symbol is not defined in a regular file
9926 and we are not generating a shared library or pie, then we
9927 need to define the symbol in the executable on a call stub.
9928 This is to avoid text relocations. */
a345bc8d 9929 s->size = (s->size + 15) & -16;
8a2058b5 9930 h->root.type = bfd_link_hash_defined;
afe397ea
AM
9931 h->root.u.def.section = s;
9932 h->root.u.def.value = s->size;
a345bc8d
AM
9933 s->size += 16;
9934 break;
9935 }
9936 return TRUE;
9937}
9938
9939/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9940 read-only sections. */
9941
9942static bfd_boolean
9943maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9944{
9945 if (h->root.type == bfd_link_hash_indirect)
9946 return TRUE;
9947
9948 if (readonly_dynrelocs (h))
9949 {
9950 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9951
9952 /* Not an error, just cut short the traversal. */
9953 return FALSE;
65f38f15 9954 }
b34976b6 9955 return TRUE;
65f38f15
AM
9956}
9957
9958/* Set the sizes of the dynamic sections. */
9959
b34976b6 9960static bfd_boolean
ee67d69a 9961ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9962 struct bfd_link_info *info)
65f38f15
AM
9963{
9964 struct ppc_link_hash_table *htab;
9965 bfd *dynobj;
9966 asection *s;
b34976b6 9967 bfd_boolean relocs;
65f38f15 9968 bfd *ibfd;
7865406b 9969 struct got_entry *first_tlsld;
65f38f15
AM
9970
9971 htab = ppc_hash_table (info);
4dfe6ac6
NC
9972 if (htab == NULL)
9973 return FALSE;
9974
65f38f15
AM
9975 dynobj = htab->elf.dynobj;
9976 if (dynobj == NULL)
9977 abort ();
9978
9979 if (htab->elf.dynamic_sections_created)
9980 {
9981 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9982 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9983 {
3d4d4302 9984 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9985 if (s == NULL)
9986 abort ();
eea6121a 9987 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9988 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9989 }
9990 }
9991
9992 /* Set up .got offsets for local syms, and space for local dynamic
9993 relocs. */
c72f2fb2 9994 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 9995 {
411e1bfb
AM
9996 struct got_entry **lgot_ents;
9997 struct got_entry **end_lgot_ents;
e054468f
AM
9998 struct plt_entry **local_plt;
9999 struct plt_entry **end_local_plt;
f961d9dd 10000 unsigned char *lgot_masks;
65f38f15
AM
10001 bfd_size_type locsymcount;
10002 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10003
0c8d6e5c 10004 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10005 continue;
10006
10007 for (s = ibfd->sections; s != NULL; s = s->next)
10008 {
19e08130 10009 struct ppc_dyn_relocs *p;
65f38f15 10010
6edfbbad 10011 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10012 {
ec338859
AM
10013 if (!bfd_is_abs_section (p->sec)
10014 && bfd_is_abs_section (p->sec->output_section))
10015 {
10016 /* Input section has been discarded, either because
10017 it is a copy of a linkonce section or due to
10018 linker script /DISCARD/, so we'll be discarding
10019 the relocs too. */
10020 }
248866a8 10021 else if (p->count != 0)
ec338859 10022 {
19e08130
AM
10023 asection *srel = elf_section_data (p->sec)->sreloc;
10024 if (p->ifunc)
33e44f2e 10025 srel = htab->elf.irelplt;
eea6121a 10026 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10027 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10028 info->flags |= DF_TEXTREL;
ec338859 10029 }
65f38f15
AM
10030 }
10031 }
10032
411e1bfb
AM
10033 lgot_ents = elf_local_got_ents (ibfd);
10034 if (!lgot_ents)
65f38f15
AM
10035 continue;
10036
0ffa91dd 10037 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10038 locsymcount = symtab_hdr->sh_info;
411e1bfb 10039 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10040 local_plt = (struct plt_entry **) end_lgot_ents;
10041 end_local_plt = local_plt + locsymcount;
f961d9dd 10042 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10043 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10044 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10045 {
0b8bcf0d 10046 struct got_entry **pent, *ent;
411e1bfb 10047
0b8bcf0d
AM
10048 pent = lgot_ents;
10049 while ((ent = *pent) != NULL)
411e1bfb
AM
10050 if (ent->got.refcount > 0)
10051 {
e7b938ca 10052 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10053 {
927be08e 10054 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10055 *pent = ent->next;
411e1bfb
AM
10056 }
10057 else
10058 {
19e08130
AM
10059 unsigned int ent_size = 8;
10060 unsigned int rel_size = sizeof (Elf64_External_Rela);
10061
eea6121a 10062 ent->got.offset = s->size;
e7b938ca 10063 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10064 {
19e08130
AM
10065 ent_size *= 2;
10066 rel_size *= 2;
10067 }
10068 s->size += ent_size;
10069 if ((*lgot_masks & PLT_IFUNC) != 0)
10070 {
33e44f2e 10071 htab->elf.irelplt->size += rel_size;
19e08130
AM
10072 htab->got_reli_size += rel_size;
10073 }
0e1862bb 10074 else if (bfd_link_pic (info))
19e08130
AM
10075 {
10076 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10077 srel->size += rel_size;
927be08e 10078 }
0b8bcf0d 10079 pent = &ent->next;
411e1bfb
AM
10080 }
10081 }
10082 else
0b8bcf0d 10083 *pent = ent->next;
65f38f15 10084 }
e054468f
AM
10085
10086 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10087 for (; local_plt < end_local_plt; ++local_plt)
10088 {
10089 struct plt_entry *ent;
10090
10091 for (ent = *local_plt; ent != NULL; ent = ent->next)
10092 if (ent->plt.refcount > 0)
10093 {
33e44f2e 10094 s = htab->elf.iplt;
e054468f 10095 ent->plt.offset = s->size;
b9e5796b 10096 s->size += PLT_ENTRY_SIZE (htab);
e054468f 10097
33e44f2e 10098 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
10099 }
10100 else
10101 ent->plt.offset = (bfd_vma) -1;
10102 }
65f38f15
AM
10103 }
10104
10105 /* Allocate global sym .plt and .got entries, and space for global
10106 sym dynamic relocs. */
4ce794b7 10107 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d
AM
10108 /* Stash the end of glink branch table. */
10109 if (htab->glink != NULL)
10110 htab->glink->rawsize = htab->glink->size;
10111
0e1862bb 10112 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10113 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10114
7865406b 10115 first_tlsld = NULL;
c72f2fb2 10116 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10117 {
7865406b
AM
10118 struct got_entry *ent;
10119
0c8d6e5c 10120 if (!is_ppc64_elf (ibfd))
102890f0
AM
10121 continue;
10122
7865406b
AM
10123 ent = ppc64_tlsld_got (ibfd);
10124 if (ent->got.refcount > 0)
102890f0 10125 {
7865406b 10126 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10127 {
7865406b
AM
10128 ent->is_indirect = TRUE;
10129 ent->got.ent = first_tlsld;
10130 }
10131 else
10132 {
10133 if (first_tlsld == NULL)
10134 first_tlsld = ent;
10135 s = ppc64_elf_tdata (ibfd)->got;
10136 ent->got.offset = s->size;
10137 ent->owner = ibfd;
10138 s->size += 16;
0e1862bb 10139 if (bfd_link_pic (info))
7865406b
AM
10140 {
10141 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10142 srel->size += sizeof (Elf64_External_Rela);
10143 }
102890f0
AM
10144 }
10145 }
10146 else
7865406b 10147 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10148 }
10149
65f38f15
AM
10150 /* We now have determined the sizes of the various dynamic sections.
10151 Allocate memory for them. */
b34976b6 10152 relocs = FALSE;
65f38f15
AM
10153 for (s = dynobj->sections; s != NULL; s = s->next)
10154 {
10155 if ((s->flags & SEC_LINKER_CREATED) == 0)
10156 continue;
10157
4ce794b7 10158 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10159 /* These haven't been allocated yet; don't strip. */
10160 continue;
33e44f2e
AM
10161 else if (s == htab->elf.sgot
10162 || s == htab->elf.splt
10163 || s == htab->elf.iplt
c456f082
AM
10164 || s == htab->glink
10165 || s == htab->dynbss)
65f38f15
AM
10166 {
10167 /* Strip this section if we don't need it; see the
10168 comment below. */
5bd4f169 10169 }
58d180e8
AM
10170 else if (s == htab->glink_eh_frame)
10171 {
10172 if (!bfd_is_abs_section (s->output_section))
10173 /* Not sized yet. */
10174 continue;
10175 }
70cc837d 10176 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10177 {
c456f082 10178 if (s->size != 0)
5bd4f169 10179 {
33e44f2e 10180 if (s != htab->elf.srelplt)
b34976b6 10181 relocs = TRUE;
5bd4f169
AM
10182
10183 /* We use the reloc_count field as a counter if we need
10184 to copy relocs into the output file. */
10185 s->reloc_count = 0;
10186 }
10187 }
65f38f15 10188 else
5bd4f169
AM
10189 {
10190 /* It's not one of our sections, so don't allocate space. */
10191 continue;
10192 }
10193
eea6121a 10194 if (s->size == 0)
5bd4f169 10195 {
c456f082
AM
10196 /* If we don't need this section, strip it from the
10197 output file. This is mostly to handle .rela.bss and
10198 .rela.plt. We must create both sections in
10199 create_dynamic_sections, because they must be created
10200 before the linker maps input sections to output
10201 sections. The linker does that before
10202 adjust_dynamic_symbol is called, and it is that
10203 function which decides whether anything needs to go
10204 into these sections. */
8423293d 10205 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10206 continue;
10207 }
10208
c456f082 10209 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10210 continue;
10211
65f38f15
AM
10212 /* Allocate memory for the section contents. We use bfd_zalloc
10213 here in case unused entries are not reclaimed before the
10214 section's contents are written out. This should not happen,
411e1bfb
AM
10215 but this way if it does we get a R_PPC64_NONE reloc in .rela
10216 sections instead of garbage.
10217 We also rely on the section contents being zero when writing
10218 the GOT. */
eea6121a 10219 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10220 if (s->contents == NULL)
b34976b6 10221 return FALSE;
5bd4f169
AM
10222 }
10223
c72f2fb2 10224 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10225 {
0c8d6e5c 10226 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10227 continue;
10228
e717da7e 10229 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10230 if (s != NULL && s != htab->elf.sgot)
e717da7e 10231 {
eea6121a 10232 if (s->size == 0)
8423293d 10233 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10234 else
10235 {
eea6121a 10236 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10237 if (s->contents == NULL)
10238 return FALSE;
10239 }
10240 }
10241 s = ppc64_elf_tdata (ibfd)->relgot;
10242 if (s != NULL)
10243 {
eea6121a 10244 if (s->size == 0)
8423293d 10245 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10246 else
10247 {
eea6121a 10248 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10249 if (s->contents == NULL)
10250 return FALSE;
10251 relocs = TRUE;
10252 s->reloc_count = 0;
10253 }
10254 }
10255 }
10256
e86ce104 10257 if (htab->elf.dynamic_sections_created)
5bd4f169 10258 {
e8910a83
AM
10259 bfd_boolean tls_opt;
10260
5bd4f169
AM
10261 /* Add some entries to the .dynamic section. We fill in the
10262 values later, in ppc64_elf_finish_dynamic_sections, but we
10263 must add the entries now so that we get the correct size for
10264 the .dynamic section. The DT_DEBUG entry is filled in by the
10265 dynamic linker and used by the debugger. */
dc810e39 10266#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10267 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10268
0e1862bb 10269 if (bfd_link_executable (info))
5bd4f169 10270 {
dc810e39 10271 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10272 return FALSE;
5bd4f169
AM
10273 }
10274
33e44f2e 10275 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10276 {
dc810e39
AM
10277 if (!add_dynamic_entry (DT_PLTGOT, 0)
10278 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10279 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10280 || !add_dynamic_entry (DT_JMPREL, 0)
10281 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10282 return FALSE;
5bd4f169
AM
10283 }
10284
ee67d69a 10285 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10286 {
10287 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10288 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10289 return FALSE;
19397422
AM
10290 }
10291
7c9cf415 10292 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10293 && htab->tls_get_addr_fd != NULL
10294 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10295 if (tls_opt || !htab->opd_abi)
10296 {
10297 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10298 return FALSE;
10299 }
a7f2871e 10300
5bd4f169
AM
10301 if (relocs)
10302 {
dc810e39
AM
10303 if (!add_dynamic_entry (DT_RELA, 0)
10304 || !add_dynamic_entry (DT_RELASZ, 0)
10305 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10306 return FALSE;
5bd4f169 10307
65f38f15
AM
10308 /* If any dynamic relocs apply to a read-only section,
10309 then we need a DT_TEXTREL entry. */
248866a8 10310 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10311 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10312
65f38f15 10313 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10314 {
65f38f15 10315 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10316 return FALSE;
5bd4f169 10317 }
5bd4f169 10318 }
5bd4f169 10319 }
65f38f15 10320#undef add_dynamic_entry
5bd4f169 10321
b34976b6 10322 return TRUE;
5bd4f169
AM
10323}
10324
a345bc8d
AM
10325/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10326
10327static bfd_boolean
10328ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10329{
10330 if (h->plt.plist != NULL
10331 && !h->def_regular
10332 && !h->pointer_equality_needed)
10333 return FALSE;
10334
10335 return _bfd_elf_hash_symbol (h);
10336}
10337
721956f4 10338/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10339
4ce794b7
AM
10340static inline enum ppc_stub_type
10341ppc_type_of_stub (asection *input_sec,
10342 const Elf_Internal_Rela *rel,
10343 struct ppc_link_hash_entry **hash,
e054468f 10344 struct plt_entry **plt_ent,
6911b7dc
AM
10345 bfd_vma destination,
10346 unsigned long local_off)
5bd4f169 10347{
721956f4
AM
10348 struct ppc_link_hash_entry *h = *hash;
10349 bfd_vma location;
10350 bfd_vma branch_offset;
10351 bfd_vma max_branch_offset;
4ce794b7 10352 enum elf_ppc64_reloc_type r_type;
5bd4f169 10353
721956f4
AM
10354 if (h != NULL)
10355 {
e054468f 10356 struct plt_entry *ent;
7fe2b9a6 10357 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10358 if (h->oh != NULL
10359 && h->oh->is_func_descriptor)
7b8f6675
AM
10360 {
10361 fdh = ppc_follow_link (h->oh);
10362 *hash = fdh;
10363 }
8387904d 10364
e054468f
AM
10365 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10366 if (ent->addend == rel->r_addend
10367 && ent->plt.offset != (bfd_vma) -1)
10368 {
e054468f
AM
10369 *plt_ent = ent;
10370 return ppc_stub_plt_call;
10371 }
5bd4f169 10372
7fe2b9a6
AM
10373 /* Here, we know we don't have a plt entry. If we don't have a
10374 either a defined function descriptor or a defined entry symbol
10375 in a regular object file, then it is pointless trying to make
10376 any other type of stub. */
854b41e7
AM
10377 if (!is_static_defined (&fdh->elf)
10378 && !is_static_defined (&h->elf))
721956f4 10379 return ppc_stub_none;
5d1634d7 10380 }
e054468f
AM
10381 else if (elf_local_got_ents (input_sec->owner) != NULL)
10382 {
10383 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10384 struct plt_entry **local_plt = (struct plt_entry **)
10385 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10386 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10387
10388 if (local_plt[r_symndx] != NULL)
10389 {
10390 struct plt_entry *ent;
10391
10392 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10393 if (ent->addend == rel->r_addend
10394 && ent->plt.offset != (bfd_vma) -1)
10395 {
10396 *plt_ent = ent;
10397 return ppc_stub_plt_call;
10398 }
10399 }
10400 }
5d1634d7 10401
721956f4
AM
10402 /* Determine where the call point is. */
10403 location = (input_sec->output_offset
10404 + input_sec->output_section->vma
10405 + rel->r_offset);
5d1634d7 10406
721956f4
AM
10407 branch_offset = destination - location;
10408 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10409
721956f4
AM
10410 /* Determine if a long branch stub is needed. */
10411 max_branch_offset = 1 << 25;
4ce794b7 10412 if (r_type != R_PPC64_REL24)
721956f4 10413 max_branch_offset = 1 << 15;
5d1634d7 10414
6911b7dc 10415 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10416 /* We need a stub. Figure out whether a long_branch or plt_branch
10417 is needed later. */
10418 return ppc_stub_long_branch;
5d1634d7 10419
721956f4 10420 return ppc_stub_none;
5d1634d7
AM
10421}
10422
794e51c0
AM
10423/* With power7 weakly ordered memory model, it is possible for ld.so
10424 to update a plt entry in one thread and have another thread see a
10425 stale zero toc entry. To avoid this we need some sort of acquire
10426 barrier in the call stub. One solution is to make the load of the
10427 toc word seem to appear to depend on the load of the function entry
10428 word. Another solution is to test for r2 being zero, and branch to
10429 the appropriate glink entry if so.
10430
10431 . fake dep barrier compare
71a39c98
AM
10432 . ld 12,xxx(2) ld 12,xxx(2)
10433 . mtctr 12 mtctr 12
10434 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10435 . add 2,2,11 cmpldi 2,0
10436 . ld 2,xxx+8(2) bnectr+
10437 . bctr b <glink_entry>
10438
10439 The solution involving the compare turns out to be faster, so
10440 that's what we use unless the branch won't reach. */
10441
10442#define ALWAYS_USE_FAKE_DEP 0
10443#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10444
5d1634d7
AM
10445#define PPC_LO(v) ((v) & 0xffff)
10446#define PPC_HI(v) (((v) >> 16) & 0xffff)
10447#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10448
794e51c0
AM
10449static inline unsigned int
10450plt_stub_size (struct ppc_link_hash_table *htab,
10451 struct ppc_stub_hash_entry *stub_entry,
10452 bfd_vma off)
10453{
b9e5796b
AM
10454 unsigned size = 12;
10455
10456 if (ALWAYS_EMIT_R2SAVE
10457 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10458 size += 4;
10459 if (PPC_HA (off) != 0)
794e51c0 10460 size += 4;
b9e5796b
AM
10461 if (htab->opd_abi)
10462 {
10463 size += 4;
e7d1c40c 10464 if (htab->params->plt_static_chain)
b9e5796b 10465 size += 4;
bd4d2eaa
AM
10466 if (htab->params->plt_thread_safe
10467 && htab->elf.dynamic_sections_created
10468 && stub_entry->h != NULL
10469 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10470 size += 8;
e7d1c40c 10471 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10472 size += 4;
10473 }
794e51c0
AM
10474 if (stub_entry->h != NULL
10475 && (stub_entry->h == htab->tls_get_addr_fd
10476 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10477 && htab->params->tls_get_addr_opt)
794e51c0
AM
10478 size += 13 * 4;
10479 return size;
10480}
10481
10482/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10483 then return the padding needed to do so. */
10484static inline unsigned int
10485plt_stub_pad (struct ppc_link_hash_table *htab,
10486 struct ppc_stub_hash_entry *stub_entry,
10487 bfd_vma plt_off)
10488{
e7d1c40c 10489 int stub_align = 1 << htab->params->plt_stub_align;
794e51c0 10490 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10491 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0
AM
10492
10493 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10494 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10495 return stub_align - (stub_off & (stub_align - 1));
10496 return 0;
10497}
10498
10499/* Build a .plt call stub. */
10500
10501static inline bfd_byte *
10502build_plt_stub (struct ppc_link_hash_table *htab,
10503 struct ppc_stub_hash_entry *stub_entry,
10504 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10505{
e7d1c40c 10506 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10507 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10508 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10509 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10510 && htab->elf.dynamic_sections_created
10511 && stub_entry->h != NULL
10512 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10513 bfd_boolean use_fake_dep = plt_thread_safe;
10514 bfd_vma cmp_branch_off = 0;
10515
10516 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10517 && plt_load_toc
794e51c0 10518 && plt_thread_safe
bd4d2eaa
AM
10519 && !((stub_entry->h == htab->tls_get_addr_fd
10520 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10521 && htab->params->tls_get_addr_opt))
794e51c0
AM
10522 {
10523 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10524 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10525 / PLT_ENTRY_SIZE (htab));
794e51c0
AM
10526 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10527 bfd_vma to, from;
10528
68d62958
AM
10529 if (pltindex > 32768)
10530 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10531 to = (glinkoff
10532 + htab->glink->output_offset
10533 + htab->glink->output_section->vma);
6f20ed8a 10534 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10535 + 4 * (ALWAYS_EMIT_R2SAVE
10536 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10537 + 4 * (PPC_HA (offset) != 0)
10538 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10539 != PPC_HA (offset))
10540 + 4 * (plt_static_chain != 0)
10541 + 20
6f20ed8a
AM
10542 + stub_entry->group->stub_sec->output_offset
10543 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10544 cmp_branch_off = to - from;
10545 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10546 }
10547
ac2df442
AM
10548 if (PPC_HA (offset) != 0)
10549 {
176a0d42
AM
10550 if (r != NULL)
10551 {
794e51c0
AM
10552 if (ALWAYS_EMIT_R2SAVE
10553 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10554 r[0].r_offset += 4;
176a0d42 10555 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10556 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10557 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10558 r[1].r_addend = r[0].r_addend;
b9e5796b 10559 if (plt_load_toc)
176a0d42 10560 {
b9e5796b 10561 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10562 {
b9e5796b
AM
10563 r[2].r_offset = r[1].r_offset + 4;
10564 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10565 r[2].r_addend = r[0].r_addend;
10566 }
10567 else
10568 {
10569 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10570 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10571 r[2].r_addend = r[0].r_addend + 8;
10572 if (plt_static_chain)
10573 {
10574 r[3].r_offset = r[2].r_offset + 4;
10575 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10576 r[3].r_addend = r[0].r_addend + 16;
10577 }
c7131b65 10578 }
176a0d42
AM
10579 }
10580 }
794e51c0
AM
10581 if (ALWAYS_EMIT_R2SAVE
10582 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10583 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10584 if (plt_load_toc)
10585 {
10586 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10587 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10588 }
10589 else
10590 {
10591 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10592 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10593 }
b9e5796b
AM
10594 if (plt_load_toc
10595 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10596 {
71a39c98 10597 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10598 offset = 0;
10599 }
71a39c98 10600 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10601 if (plt_load_toc)
794e51c0 10602 {
b9e5796b
AM
10603 if (use_fake_dep)
10604 {
10605 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10606 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10607 }
10608 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10609 if (plt_static_chain)
10610 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10611 }
ac2df442
AM
10612 }
10613 else
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_DS);
b9e5796b 10621 if (plt_load_toc)
176a0d42 10622 {
b9e5796b 10623 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10624 {
b9e5796b
AM
10625 r[1].r_offset = r[0].r_offset + 4;
10626 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10627 r[1].r_addend = r[0].r_addend;
10628 }
10629 else
10630 {
10631 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10632 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10633 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10634 if (plt_static_chain)
10635 {
10636 r[2].r_offset = r[1].r_offset + 4;
10637 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10638 r[2].r_addend = r[0].r_addend + 8;
10639 }
c7131b65 10640 }
176a0d42
AM
10641 }
10642 }
794e51c0
AM
10643 if (ALWAYS_EMIT_R2SAVE
10644 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10645 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10646 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10647 if (plt_load_toc
10648 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10649 {
10650 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10651 offset = 0;
10652 }
71a39c98 10653 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10654 if (plt_load_toc)
794e51c0 10655 {
b9e5796b
AM
10656 if (use_fake_dep)
10657 {
10658 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10659 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10660 }
10661 if (plt_static_chain)
10662 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10663 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10664 }
ac2df442 10665 }
b9e5796b 10666 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10667 {
10668 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10669 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10670 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10671 }
10672 else
10673 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10674 return p;
10675}
10676
a7f2871e
AM
10677/* Build a special .plt call stub for __tls_get_addr. */
10678
10679#define LD_R11_0R3 0xe9630000
10680#define LD_R12_0R3 0xe9830000
10681#define MR_R0_R3 0x7c601b78
10682#define CMPDI_R11_0 0x2c2b0000
10683#define ADD_R3_R12_R13 0x7c6c6a14
10684#define BEQLR 0x4d820020
10685#define MR_R3_R0 0x7c030378
a7f2871e
AM
10686#define STD_R11_0R1 0xf9610000
10687#define BCTRL 0x4e800421
10688#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10689#define MTLR_R11 0x7d6803a6
10690
10691static inline bfd_byte *
794e51c0
AM
10692build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10693 struct ppc_stub_hash_entry *stub_entry,
10694 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10695{
e7d1c40c 10696 bfd *obfd = htab->params->stub_bfd;
794e51c0 10697
a7f2871e
AM
10698 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10699 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10700 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10701 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10702 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10703 bfd_put_32 (obfd, BEQLR, p), p += 4;
10704 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10705 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10706 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10707
10708 if (r != NULL)
10709 r[0].r_offset += 9 * 4;
794e51c0 10710 p = build_plt_stub (htab, stub_entry, p, offset, r);
a7f2871e
AM
10711 bfd_put_32 (obfd, BCTRL, p - 4);
10712
a078d95a 10713 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10714 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10715 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10716 bfd_put_32 (obfd, BLR, p), p += 4;
10717
10718 return p;
10719}
10720
176a0d42
AM
10721static Elf_Internal_Rela *
10722get_relocs (asection *sec, int count)
10723{
10724 Elf_Internal_Rela *relocs;
10725 struct bfd_elf_section_data *elfsec_data;
10726
10727 elfsec_data = elf_section_data (sec);
10728 relocs = elfsec_data->relocs;
10729 if (relocs == NULL)
10730 {
10731 bfd_size_type relsize;
10732 relsize = sec->reloc_count * sizeof (*relocs);
10733 relocs = bfd_alloc (sec->owner, relsize);
10734 if (relocs == NULL)
10735 return NULL;
10736 elfsec_data->relocs = relocs;
d4730f92
BS
10737 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10738 sizeof (Elf_Internal_Shdr));
10739 if (elfsec_data->rela.hdr == NULL)
10740 return NULL;
10741 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10742 * sizeof (Elf64_External_Rela));
10743 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10744 sec->reloc_count = 0;
10745 }
10746 relocs += sec->reloc_count;
10747 sec->reloc_count += count;
10748 return relocs;
10749}
10750
aa374f67 10751static bfd_vma
25f53a85 10752get_r2off (struct bfd_link_info *info,
aa374f67
AM
10753 struct ppc_stub_hash_entry *stub_entry)
10754{
25f53a85 10755 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10756 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10757
10758 if (r2off == 0)
10759 {
10760 /* Support linking -R objects. Get the toc pointer from the
10761 opd entry. */
10762 char buf[8];
b9e5796b
AM
10763 if (!htab->opd_abi)
10764 return r2off;
aa374f67
AM
10765 asection *opd = stub_entry->h->elf.root.u.def.section;
10766 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10767
10768 if (strcmp (opd->name, ".opd") != 0
10769 || opd->reloc_count != 0)
10770 {
bc30df16 10771 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
25f53a85 10772 stub_entry->h->elf.root.root.string);
aa374f67 10773 bfd_set_error (bfd_error_bad_value);
a7c49797 10774 return (bfd_vma) -1;
aa374f67
AM
10775 }
10776 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10777 return (bfd_vma) -1;
aa374f67 10778 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10779 r2off -= elf_gp (info->output_bfd);
aa374f67 10780 }
6f20ed8a 10781 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10782 return r2off;
10783}
10784
b34976b6 10785static bfd_boolean
4ce794b7 10786ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10787{
721956f4
AM
10788 struct ppc_stub_hash_entry *stub_entry;
10789 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10790 struct bfd_link_info *info;
10791 struct ppc_link_hash_table *htab;
721956f4
AM
10792 bfd_byte *loc;
10793 bfd_byte *p;
ee75fd95 10794 bfd_vma dest, off;
721956f4 10795 int size;
176a0d42 10796 Elf_Internal_Rela *r;
e054468f 10797 asection *plt;
5d1634d7 10798
721956f4
AM
10799 /* Massage our args to the form they really have. */
10800 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10801 info = in_arg;
5d1634d7 10802
5d1634d7 10803 htab = ppc_hash_table (info);
4dfe6ac6
NC
10804 if (htab == NULL)
10805 return FALSE;
5d1634d7 10806
721956f4 10807 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
10808 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10809 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10810
4ce794b7 10811 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10812 switch (stub_entry->stub_type)
5d1634d7 10813 {
721956f4 10814 case ppc_stub_long_branch:
ad8e1ba5 10815 case ppc_stub_long_branch_r2off:
721956f4 10816 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10817 dest = (stub_entry->target_value
10818 + stub_entry->target_section->output_offset
10819 + stub_entry->target_section->output_section->vma);
10820 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10821 off = dest;
5d1634d7 10822
721956f4
AM
10823 /* And this is where we are coming from. */
10824 off -= (stub_entry->stub_offset
6f20ed8a
AM
10825 + stub_entry->group->stub_sec->output_offset
10826 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 10827
ac2df442
AM
10828 size = 4;
10829 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10830 {
25f53a85 10831 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10832
a7c49797 10833 if (r2off == (bfd_vma) -1)
aa374f67
AM
10834 {
10835 htab->stub_error = TRUE;
10836 return FALSE;
10837 }
e7d1c40c 10838 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10839 loc += 4;
a7c49797 10840 size = 8;
ac2df442
AM
10841 if (PPC_HA (r2off) != 0)
10842 {
e7d1c40c
AM
10843 bfd_put_32 (htab->params->stub_bfd,
10844 ADDIS_R2_R2 | PPC_HA (r2off), loc);
ac2df442 10845 loc += 4;
a7c49797
AM
10846 size += 4;
10847 }
10848 if (PPC_LO (r2off) != 0)
10849 {
10850 bfd_put_32 (htab->params->stub_bfd,
10851 ADDI_R2_R2 | PPC_LO (r2off), loc);
10852 loc += 4;
10853 size += 4;
ac2df442 10854 }
ac2df442 10855 off -= size - 4;
ad8e1ba5 10856 }
e7d1c40c 10857 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 10858
5c3dead3
AM
10859 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10860 {
bc30df16
AM
10861 info->callbacks->einfo
10862 (_("%P: long branch stub `%s' offset overflow\n"),
10863 stub_entry->root.string);
5c3dead3
AM
10864 htab->stub_error = TRUE;
10865 return FALSE;
10866 }
ee75fd95
AM
10867
10868 if (info->emitrelocations)
10869 {
6f20ed8a 10870 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10871 if (r == NULL)
10872 return FALSE;
6f20ed8a 10873 r->r_offset = loc - stub_entry->group->stub_sec->contents;
ee75fd95
AM
10874 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10875 r->r_addend = dest;
10876 if (stub_entry->h != NULL)
10877 {
10878 struct elf_link_hash_entry **hashes;
10879 unsigned long symndx;
10880 struct ppc_link_hash_entry *h;
10881
e7d1c40c 10882 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10883 if (hashes == NULL)
10884 {
10885 bfd_size_type hsize;
10886
10887 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10888 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10889 if (hashes == NULL)
10890 return FALSE;
e7d1c40c 10891 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10892 htab->stub_globals = 1;
10893 }
10894 symndx = htab->stub_globals++;
10895 h = stub_entry->h;
10896 hashes[symndx] = &h->elf;
10897 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10898 if (h->oh != NULL && h->oh->is_func)
b31867b6 10899 h = ppc_follow_link (h->oh);
ee75fd95
AM
10900 if (h->elf.root.u.def.section != stub_entry->target_section)
10901 /* H is an opd symbol. The addend must be zero. */
10902 r->r_addend = 0;
10903 else
10904 {
10905 off = (h->elf.root.u.def.value
10906 + h->elf.root.u.def.section->output_offset
10907 + h->elf.root.u.def.section->output_section->vma);
10908 r->r_addend -= off;
10909 }
10910 }
10911 }
721956f4 10912 break;
e86ce104 10913
721956f4 10914 case ppc_stub_plt_branch:
ad8e1ba5 10915 case ppc_stub_plt_branch_r2off:
721956f4
AM
10916 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10917 stub_entry->root.string + 9,
b34976b6 10918 FALSE, FALSE);
721956f4
AM
10919 if (br_entry == NULL)
10920 {
8de848d8 10921 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 10922 stub_entry->root.string);
b34976b6
AM
10923 htab->stub_error = TRUE;
10924 return FALSE;
721956f4
AM
10925 }
10926
176a0d42
AM
10927 dest = (stub_entry->target_value
10928 + stub_entry->target_section->output_offset
10929 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10930 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10931 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10932
176a0d42 10933 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10934 htab->brlt->contents + br_entry->offset);
721956f4 10935
f94498ff 10936 if (br_entry->iter == htab->stub_iteration)
721956f4 10937 {
f94498ff 10938 br_entry->iter = 0;
84f5d08e 10939
f94498ff 10940 if (htab->relbrlt != NULL)
84f5d08e 10941 {
f94498ff
AM
10942 /* Create a reloc for the branch lookup table entry. */
10943 Elf_Internal_Rela rela;
10944 bfd_byte *rl;
10945
10946 rela.r_offset = (br_entry->offset
10947 + htab->brlt->output_offset
10948 + htab->brlt->output_section->vma);
10949 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10950 rela.r_addend = dest;
f94498ff
AM
10951
10952 rl = htab->relbrlt->contents;
10953 rl += (htab->relbrlt->reloc_count++
10954 * sizeof (Elf64_External_Rela));
10955 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10956 }
10957 else if (info->emitrelocations)
10958 {
176a0d42
AM
10959 r = get_relocs (htab->brlt, 1);
10960 if (r == NULL)
10961 return FALSE;
10962 /* brlt, being SEC_LINKER_CREATED does not go through the
10963 normal reloc processing. Symbols and offsets are not
10964 translated from input file to output file form, so
10965 set up the offset per the output file. */
f94498ff
AM
10966 r->r_offset = (br_entry->offset
10967 + htab->brlt->output_offset
10968 + htab->brlt->output_section->vma);
10969 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10970 r->r_addend = dest;
84f5d08e 10971 }
84f5d08e 10972 }
721956f4 10973
176a0d42
AM
10974 dest = (br_entry->offset
10975 + htab->brlt->output_offset
10976 + htab->brlt->output_section->vma);
10977
10978 off = (dest
4ce794b7 10979 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 10980 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 10981
ad8e1ba5 10982 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10983 {
25f53a85 10984 info->callbacks->einfo
bc30df16 10985 (_("%P: linkage table error against `%T'\n"),
721956f4 10986 stub_entry->root.string);
5d1634d7 10987 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10988 htab->stub_error = TRUE;
10989 return FALSE;
5d1634d7 10990 }
41bd81ab 10991
176a0d42
AM
10992 if (info->emitrelocations)
10993 {
6f20ed8a 10994 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
10995 if (r == NULL)
10996 return FALSE;
6f20ed8a 10997 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
10998 if (bfd_big_endian (info->output_bfd))
10999 r[0].r_offset += 2;
00f412ee 11000 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11001 r[0].r_offset += 4;
11002 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11003 r[0].r_addend = dest;
11004 if (PPC_HA (off) != 0)
11005 {
11006 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11007 r[1].r_offset = r[0].r_offset + 4;
11008 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11009 r[1].r_addend = r[0].r_addend;
11010 }
11011 }
11012
00f412ee 11013 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11014 {
176a0d42 11015 if (PPC_HA (off) != 0)
ac2df442
AM
11016 {
11017 size = 16;
e7d1c40c 11018 bfd_put_32 (htab->params->stub_bfd,
397998fc 11019 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 11020 loc += 4;
e7d1c40c 11021 bfd_put_32 (htab->params->stub_bfd,
397998fc 11022 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
11023 }
11024 else
11025 {
11026 size = 12;
e7d1c40c
AM
11027 bfd_put_32 (htab->params->stub_bfd,
11028 LD_R12_0R2 | PPC_LO (off), loc);
ac2df442 11029 }
ad8e1ba5
AM
11030 }
11031 else
11032 {
25f53a85 11033 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11034
a7c49797 11035 if (r2off == (bfd_vma) -1)
aa374f67
AM
11036 {
11037 htab->stub_error = TRUE;
11038 return FALSE;
11039 }
ad8e1ba5 11040
e7d1c40c 11041 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 11042 loc += 4;
00f412ee 11043 size = 16;
176a0d42 11044 if (PPC_HA (off) != 0)
ac2df442
AM
11045 {
11046 size += 4;
e7d1c40c 11047 bfd_put_32 (htab->params->stub_bfd,
397998fc 11048 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 11049 loc += 4;
e7d1c40c 11050 bfd_put_32 (htab->params->stub_bfd,
397998fc 11051 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
11052 }
11053 else
e7d1c40c 11054 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442
AM
11055
11056 if (PPC_HA (r2off) != 0)
11057 {
11058 size += 4;
00f412ee 11059 loc += 4;
e7d1c40c
AM
11060 bfd_put_32 (htab->params->stub_bfd,
11061 ADDIS_R2_R2 | PPC_HA (r2off), loc);
00f412ee
AM
11062 }
11063 if (PPC_LO (r2off) != 0)
11064 {
11065 size += 4;
ac2df442 11066 loc += 4;
e7d1c40c
AM
11067 bfd_put_32 (htab->params->stub_bfd,
11068 ADDI_R2_R2 | PPC_LO (r2off), loc);
ac2df442 11069 }
ad8e1ba5
AM
11070 }
11071 loc += 4;
e7d1c40c 11072 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
ad8e1ba5 11073 loc += 4;
e7d1c40c 11074 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
721956f4 11075 break;
5d1634d7 11076
721956f4 11077 case ppc_stub_plt_call:
794e51c0 11078 case ppc_stub_plt_call_r2save:
e054468f 11079 if (stub_entry->h != NULL
b31867b6
AM
11080 && stub_entry->h->is_func_descriptor
11081 && stub_entry->h->oh != NULL)
c862ae31 11082 {
b31867b6
AM
11083 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11084
11085 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11086 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
b31867b6
AM
11087 if (fh->elf.root.type == bfd_link_hash_undefined)
11088 fh->elf.root.type = bfd_link_hash_undefweak;
9507a174
AM
11089 /* Stop undo_symbol_twiddle changing it back to undefined. */
11090 fh->was_undefined = 0;
c862ae31
AM
11091 }
11092
721956f4 11093 /* Now build the stub. */
e054468f 11094 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 11095 if (dest >= (bfd_vma) -2)
721956f4
AM
11096 abort ();
11097
33e44f2e 11098 plt = htab->elf.splt;
25f23106
AM
11099 if (!htab->elf.dynamic_sections_created
11100 || stub_entry->h == NULL
11101 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11102 plt = htab->elf.iplt;
e054468f
AM
11103
11104 dest += plt->output_offset + plt->output_section->vma;
11105
11106 if (stub_entry->h == NULL
11107 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11108 {
11109 Elf_Internal_Rela rela;
11110 bfd_byte *rl;
11111
11112 rela.r_offset = dest;
ee67d69a
AM
11113 if (htab->opd_abi)
11114 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11115 else
11116 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
11117 rela.r_addend = (stub_entry->target_value
11118 + stub_entry->target_section->output_offset
11119 + stub_entry->target_section->output_section->vma);
11120
33e44f2e
AM
11121 rl = (htab->elf.irelplt->contents
11122 + (htab->elf.irelplt->reloc_count++
25f23106
AM
11123 * sizeof (Elf64_External_Rela)));
11124 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
11125 stub_entry->plt_ent->plt.offset |= 1;
11126 }
176a0d42
AM
11127
11128 off = (dest
e054468f 11129 - elf_gp (plt->output_section->owner)
6f20ed8a 11130 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11131
ad8e1ba5 11132 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11133 {
25f53a85 11134 info->callbacks->einfo
695344c0 11135 /* xgettext:c-format */
bc30df16 11136 (_("%P: linkage table error against `%T'\n"),
e054468f
AM
11137 stub_entry->h != NULL
11138 ? stub_entry->h->elf.root.root.string
11139 : "<local sym>");
721956f4 11140 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11141 htab->stub_error = TRUE;
11142 return FALSE;
721956f4
AM
11143 }
11144
e7d1c40c 11145 if (htab->params->plt_stub_align != 0)
794e51c0
AM
11146 {
11147 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11148
6f20ed8a
AM
11149 stub_entry->group->stub_sec->size += pad;
11150 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
11151 loc += pad;
11152 }
11153
176a0d42
AM
11154 r = NULL;
11155 if (info->emitrelocations)
11156 {
6f20ed8a 11157 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11158 ((PPC_HA (off) != 0)
11159 + (htab->opd_abi
e7d1c40c 11160 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11161 && PPC_HA (off + 16) == PPC_HA (off))
11162 : 1)));
176a0d42
AM
11163 if (r == NULL)
11164 return FALSE;
6f20ed8a 11165 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11166 if (bfd_big_endian (info->output_bfd))
11167 r[0].r_offset += 2;
176a0d42
AM
11168 r[0].r_addend = dest;
11169 }
a7f2871e
AM
11170 if (stub_entry->h != NULL
11171 && (stub_entry->h == htab->tls_get_addr_fd
11172 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11173 && htab->params->tls_get_addr_opt)
794e51c0 11174 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11175 else
794e51c0 11176 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11177 size = p - loc;
11178 break;
11179
a4b6fadd
AM
11180 case ppc_stub_save_res:
11181 return TRUE;
11182
721956f4
AM
11183 default:
11184 BFD_FAIL ();
b34976b6 11185 return FALSE;
721956f4
AM
11186 }
11187
6f20ed8a 11188 stub_entry->group->stub_sec->size += size;
97b639ba 11189
e7d1c40c 11190 if (htab->params->emit_stub_syms)
97b639ba
AM
11191 {
11192 struct elf_link_hash_entry *h;
ee75fd95
AM
11193 size_t len1, len2;
11194 char *name;
11195 const char *const stub_str[] = { "long_branch",
11196 "long_branch_r2off",
11197 "plt_branch",
11198 "plt_branch_r2off",
794e51c0 11199 "plt_call",
ee75fd95
AM
11200 "plt_call" };
11201
11202 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11203 len2 = strlen (stub_entry->root.string);
11204 name = bfd_malloc (len1 + len2 + 2);
11205 if (name == NULL)
11206 return FALSE;
11207 memcpy (name, stub_entry->root.string, 9);
11208 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11209 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11210 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11211 if (h == NULL)
11212 return FALSE;
11213 if (h->root.type == bfd_link_hash_new)
11214 {
11215 h->root.type = bfd_link_hash_defined;
6f20ed8a 11216 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11217 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11218 h->ref_regular = 1;
11219 h->def_regular = 1;
11220 h->ref_regular_nonweak = 1;
11221 h->forced_local = 1;
11222 h->non_elf = 0;
2ec55de3 11223 h->root.linker_def = 1;
97b639ba
AM
11224 }
11225 }
11226
b34976b6 11227 return TRUE;
721956f4
AM
11228}
11229
11230/* As above, but don't actually build the stub. Just bump offset so
11231 we know stub section sizes, and select plt_branch stubs where
11232 long_branch stubs won't do. */
11233
b34976b6 11234static bfd_boolean
4ce794b7 11235ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11236{
11237 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11238 struct bfd_link_info *info;
721956f4
AM
11239 struct ppc_link_hash_table *htab;
11240 bfd_vma off;
11241 int size;
11242
11243 /* Massage our args to the form they really have. */
11244 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11245 info = in_arg;
11246
11247 htab = ppc_hash_table (info);
4dfe6ac6
NC
11248 if (htab == NULL)
11249 return FALSE;
721956f4 11250
a4b6fadd
AM
11251 if (stub_entry->h != NULL
11252 && stub_entry->h->save_res
11253 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11254 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11255 {
11256 /* Don't make stubs to out-of-line register save/restore
11257 functions. Instead, emit copies of the functions. */
11258 stub_entry->group->needs_save_res = 1;
11259 stub_entry->stub_type = ppc_stub_save_res;
11260 return TRUE;
11261 }
11262
794e51c0
AM
11263 if (stub_entry->stub_type == ppc_stub_plt_call
11264 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11265 {
e054468f
AM
11266 asection *plt;
11267 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11268 if (off >= (bfd_vma) -2)
411e1bfb 11269 abort ();
33e44f2e 11270 plt = htab->elf.splt;
25f23106
AM
11271 if (!htab->elf.dynamic_sections_created
11272 || stub_entry->h == NULL
11273 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11274 plt = htab->elf.iplt;
e054468f
AM
11275 off += (plt->output_offset
11276 + plt->output_section->vma
11277 - elf_gp (plt->output_section->owner)
6f20ed8a 11278 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11279
794e51c0 11280 size = plt_stub_size (htab, stub_entry, off);
e7d1c40c 11281 if (htab->params->plt_stub_align)
794e51c0 11282 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11283 if (info->emitrelocations)
11284 {
6f20ed8a 11285 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11286 += ((PPC_HA (off) != 0)
11287 + (htab->opd_abi
e7d1c40c 11288 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11289 && PPC_HA (off + 16) == PPC_HA (off))
11290 : 1));
6f20ed8a 11291 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11292 }
721956f4
AM
11293 }
11294 else
11295 {
ad8e1ba5
AM
11296 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11297 variants. */
ac2df442 11298 bfd_vma r2off = 0;
6911b7dc 11299 bfd_vma local_off = 0;
ac2df442 11300
721956f4
AM
11301 off = (stub_entry->target_value
11302 + stub_entry->target_section->output_offset
11303 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11304 off -= (stub_entry->group->stub_sec->size
11305 + stub_entry->group->stub_sec->output_offset
11306 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11307
ad8e1ba5
AM
11308 /* Reset the stub type from the plt variant in case we now
11309 can reach with a shorter stub. */
11310 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11311 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11312
11313 size = 4;
11314 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11315 {
25f53a85 11316 r2off = get_r2off (info, stub_entry);
a7c49797 11317 if (r2off == (bfd_vma) -1)
aa374f67
AM
11318 {
11319 htab->stub_error = TRUE;
11320 return FALSE;
11321 }
a7c49797 11322 size = 8;
ac2df442 11323 if (PPC_HA (r2off) != 0)
a7c49797
AM
11324 size += 4;
11325 if (PPC_LO (r2off) != 0)
11326 size += 4;
ac2df442 11327 off -= size - 4;
ad8e1ba5
AM
11328 }
11329
6911b7dc
AM
11330 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11331
b9e5796b
AM
11332 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11333 Do the same for -R objects without function descriptors. */
11334 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11335 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11336 && r2off == 0
11337 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11338 {
11339 struct ppc_branch_hash_entry *br_entry;
11340
11341 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11342 stub_entry->root.string + 9,
b34976b6 11343 TRUE, FALSE);
721956f4
AM
11344 if (br_entry == NULL)
11345 {
8de848d8 11346 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 11347 stub_entry->root.string);
b34976b6
AM
11348 htab->stub_error = TRUE;
11349 return FALSE;
721956f4
AM
11350 }
11351
11352 if (br_entry->iter != htab->stub_iteration)
11353 {
11354 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11355 br_entry->offset = htab->brlt->size;
11356 htab->brlt->size += 8;
63bc6f6c 11357
ee75fd95 11358 if (htab->relbrlt != NULL)
eea6121a 11359 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11360 else if (info->emitrelocations)
11361 {
11362 htab->brlt->reloc_count += 1;
11363 htab->brlt->flags |= SEC_RELOC;
11364 }
721956f4 11365 }
ad8e1ba5
AM
11366
11367 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11368 off = (br_entry->offset
11369 + htab->brlt->output_offset
11370 + htab->brlt->output_section->vma
11371 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 11372 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11373
176a0d42
AM
11374 if (info->emitrelocations)
11375 {
6f20ed8a
AM
11376 stub_entry->group->stub_sec->reloc_count
11377 += 1 + (PPC_HA (off) != 0);
11378 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11379 }
11380
00f412ee 11381 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11382 {
11383 size = 12;
176a0d42 11384 if (PPC_HA (off) != 0)
ac2df442
AM
11385 size = 16;
11386 }
11387 else
11388 {
00f412ee 11389 size = 16;
176a0d42 11390 if (PPC_HA (off) != 0)
ac2df442
AM
11391 size += 4;
11392
11393 if (PPC_HA (r2off) != 0)
11394 size += 4;
00f412ee
AM
11395 if (PPC_LO (r2off) != 0)
11396 size += 4;
ac2df442 11397 }
721956f4 11398 }
84f5d08e
AM
11399 else if (info->emitrelocations)
11400 {
6f20ed8a
AM
11401 stub_entry->group->stub_sec->reloc_count += 1;
11402 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11403 }
721956f4
AM
11404 }
11405
6f20ed8a 11406 stub_entry->group->stub_sec->size += size;
b34976b6 11407 return TRUE;
721956f4
AM
11408}
11409
11410/* Set up various things so that we can make a list of input sections
11411 for each output section included in the link. Returns -1 on error,
cedb70c5 11412 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11413
11414int
e7d1c40c 11415ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11416{
6f20ed8a 11417 unsigned int id;
721956f4
AM
11418 bfd_size_type amt;
11419 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11420
4dfe6ac6
NC
11421 if (htab == NULL)
11422 return -1;
4c52953f 11423
6f20ed8a
AM
11424 htab->sec_info_arr_size = bfd_get_next_section_id ();
11425 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11426 htab->sec_info = bfd_zmalloc (amt);
11427 if (htab->sec_info == NULL)
721956f4
AM
11428 return -1;
11429
3d6f9012
AM
11430 /* Set toc_off for com, und, abs and ind sections. */
11431 for (id = 0; id < 3; id++)
6f20ed8a 11432 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11433
721956f4
AM
11434 return 1;
11435}
11436
927be08e
AM
11437/* Set up for first pass at multitoc partitioning. */
11438
11439void
11440ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11441{
11442 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11443
1c865ab2 11444 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11445 htab->toc_bfd = NULL;
11446 htab->toc_first_sec = NULL;
11447}
11448
e717da7e
AM
11449/* The linker repeatedly calls this function for each TOC input section
11450 and linker generated GOT section. Group input bfds such that the toc
927be08e 11451 within a group is less than 64k in size. */
ad8e1ba5 11452
927be08e 11453bfd_boolean
4ce794b7 11454ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11455{
11456 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11457 bfd_vma addr, off, limit;
ad8e1ba5 11458
4dfe6ac6
NC
11459 if (htab == NULL)
11460 return FALSE;
11461
927be08e 11462 if (!htab->second_toc_pass)
4c52953f 11463 {
927be08e 11464 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11465 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11466
11467 if (new_bfd)
bf102f86
AM
11468 {
11469 htab->toc_bfd = isec->owner;
11470 htab->toc_first_sec = isec;
11471 }
927be08e 11472
bf102f86
AM
11473 addr = isec->output_offset + isec->output_section->vma;
11474 off = addr - htab->toc_curr;
d77c8a4b
AM
11475 limit = 0x80008000;
11476 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11477 limit = 0x10000;
11478 if (off + isec->size > limit)
bf102f86
AM
11479 {
11480 addr = (htab->toc_first_sec->output_offset
11481 + htab->toc_first_sec->output_section->vma);
11482 htab->toc_curr = addr;
a27e685f 11483 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11484 }
99877b66 11485
927be08e
AM
11486 /* toc_curr is the base address of this toc group. Set elf_gp
11487 for the input section to be the offset relative to the
11488 output toc base plus 0x8000. Making the input elf_gp an
11489 offset allows us to move the toc as a whole without
11490 recalculating input elf_gp. */
11491 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11492 off += TOC_BASE_OFF;
11493
11494 /* Die if someone uses a linker script that doesn't keep input
11495 file .toc and .got together. */
a4fd3de5
AM
11496 if (new_bfd
11497 && elf_gp (isec->owner) != 0
927be08e
AM
11498 && elf_gp (isec->owner) != off)
11499 return FALSE;
11500
11501 elf_gp (isec->owner) = off;
11502 return TRUE;
4c52953f 11503 }
927be08e
AM
11504
11505 /* During the second pass toc_first_sec points to the start of
11506 a toc group, and toc_curr is used to track the old elf_gp.
11507 We use toc_bfd to ensure we only look at each bfd once. */
11508 if (htab->toc_bfd == isec->owner)
11509 return TRUE;
11510 htab->toc_bfd = isec->owner;
11511
11512 if (htab->toc_first_sec == NULL
11513 || htab->toc_curr != elf_gp (isec->owner))
11514 {
11515 htab->toc_curr = elf_gp (isec->owner);
11516 htab->toc_first_sec = isec;
11517 }
11518 addr = (htab->toc_first_sec->output_offset
11519 + htab->toc_first_sec->output_section->vma);
11520 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11521 elf_gp (isec->owner) = off;
11522
11523 return TRUE;
ad8e1ba5
AM
11524}
11525
927be08e
AM
11526/* Called via elf_link_hash_traverse to merge GOT entries for global
11527 symbol H. */
11528
11529static bfd_boolean
11530merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11531{
11532 if (h->root.type == bfd_link_hash_indirect)
11533 return TRUE;
11534
927be08e
AM
11535 merge_got_entries (&h->got.glist);
11536
11537 return TRUE;
11538}
11539
11540/* Called via elf_link_hash_traverse to allocate GOT entries for global
11541 symbol H. */
11542
11543static bfd_boolean
11544reallocate_got (struct elf_link_hash_entry *h, void *inf)
11545{
11546 struct got_entry *gent;
11547
11548 if (h->root.type == bfd_link_hash_indirect)
11549 return TRUE;
11550
927be08e
AM
11551 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11552 if (!gent->is_indirect)
11553 allocate_got (h, (struct bfd_link_info *) inf, gent);
11554 return TRUE;
11555}
11556
11557/* Called on the first multitoc pass after the last call to
11558 ppc64_elf_next_toc_section. This function removes duplicate GOT
11559 entries. */
11560
11561bfd_boolean
11562ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11563{
11564 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11565 struct bfd *ibfd, *ibfd2;
11566 bfd_boolean done_something;
11567
11568 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11569
7865406b
AM
11570 if (!htab->do_multi_toc)
11571 return FALSE;
11572
d0fae19d 11573 /* Merge global sym got entries within a toc group. */
927be08e
AM
11574 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11575
11576 /* And tlsld_got. */
c72f2fb2 11577 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11578 {
11579 struct got_entry *ent, *ent2;
11580
11581 if (!is_ppc64_elf (ibfd))
11582 continue;
11583
11584 ent = ppc64_tlsld_got (ibfd);
11585 if (!ent->is_indirect
11586 && ent->got.offset != (bfd_vma) -1)
11587 {
c72f2fb2 11588 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11589 {
11590 if (!is_ppc64_elf (ibfd2))
11591 continue;
11592
11593 ent2 = ppc64_tlsld_got (ibfd2);
11594 if (!ent2->is_indirect
11595 && ent2->got.offset != (bfd_vma) -1
11596 && elf_gp (ibfd2) == elf_gp (ibfd))
11597 {
11598 ent2->is_indirect = TRUE;
11599 ent2->got.ent = ent;
11600 }
11601 }
11602 }
11603 }
11604
11605 /* Zap sizes of got sections. */
33e44f2e
AM
11606 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11607 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11608 htab->got_reli_size = 0;
11609
c72f2fb2 11610 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11611 {
11612 asection *got, *relgot;
11613
11614 if (!is_ppc64_elf (ibfd))
11615 continue;
11616
11617 got = ppc64_elf_tdata (ibfd)->got;
11618 if (got != NULL)
11619 {
11620 got->rawsize = got->size;
11621 got->size = 0;
11622 relgot = ppc64_elf_tdata (ibfd)->relgot;
11623 relgot->rawsize = relgot->size;
11624 relgot->size = 0;
11625 }
11626 }
11627
11628 /* Now reallocate the got, local syms first. We don't need to
11629 allocate section contents again since we never increase size. */
c72f2fb2 11630 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11631 {
11632 struct got_entry **lgot_ents;
11633 struct got_entry **end_lgot_ents;
11634 struct plt_entry **local_plt;
11635 struct plt_entry **end_local_plt;
f961d9dd 11636 unsigned char *lgot_masks;
927be08e
AM
11637 bfd_size_type locsymcount;
11638 Elf_Internal_Shdr *symtab_hdr;
19e08130 11639 asection *s;
927be08e
AM
11640
11641 if (!is_ppc64_elf (ibfd))
11642 continue;
11643
11644 lgot_ents = elf_local_got_ents (ibfd);
11645 if (!lgot_ents)
11646 continue;
11647
11648 symtab_hdr = &elf_symtab_hdr (ibfd);
11649 locsymcount = symtab_hdr->sh_info;
11650 end_lgot_ents = lgot_ents + locsymcount;
11651 local_plt = (struct plt_entry **) end_lgot_ents;
11652 end_local_plt = local_plt + locsymcount;
f961d9dd 11653 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11654 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11655 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11656 {
11657 struct got_entry *ent;
11658
11659 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11660 {
19e08130
AM
11661 unsigned int ent_size = 8;
11662 unsigned int rel_size = sizeof (Elf64_External_Rela);
11663
d0fae19d
AM
11664 ent->got.offset = s->size;
11665 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11666 {
19e08130
AM
11667 ent_size *= 2;
11668 rel_size *= 2;
11669 }
11670 s->size += ent_size;
11671 if ((*lgot_masks & PLT_IFUNC) != 0)
11672 {
33e44f2e 11673 htab->elf.irelplt->size += rel_size;
19e08130
AM
11674 htab->got_reli_size += rel_size;
11675 }
0e1862bb 11676 else if (bfd_link_pic (info))
19e08130
AM
11677 {
11678 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11679 srel->size += rel_size;
d0fae19d
AM
11680 }
11681 }
927be08e
AM
11682 }
11683 }
11684
11685 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11686
c72f2fb2 11687 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11688 {
11689 struct got_entry *ent;
11690
11691 if (!is_ppc64_elf (ibfd))
11692 continue;
11693
11694 ent = ppc64_tlsld_got (ibfd);
11695 if (!ent->is_indirect
11696 && ent->got.offset != (bfd_vma) -1)
11697 {
11698 asection *s = ppc64_elf_tdata (ibfd)->got;
11699 ent->got.offset = s->size;
11700 s->size += 16;
0e1862bb 11701 if (bfd_link_pic (info))
927be08e
AM
11702 {
11703 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11704 srel->size += sizeof (Elf64_External_Rela);
11705 }
11706 }
11707 }
11708
33e44f2e 11709 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11710 if (!done_something)
c72f2fb2 11711 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11712 {
11713 asection *got;
11714
11715 if (!is_ppc64_elf (ibfd))
11716 continue;
11717
11718 got = ppc64_elf_tdata (ibfd)->got;
11719 if (got != NULL)
11720 {
11721 done_something = got->rawsize != got->size;
11722 if (done_something)
11723 break;
11724 }
11725 }
11726
11727 if (done_something)
e7d1c40c 11728 (*htab->params->layout_sections_again) ();
927be08e
AM
11729
11730 /* Set up for second pass over toc sections to recalculate elf_gp
11731 on input sections. */
11732 htab->toc_bfd = NULL;
11733 htab->toc_first_sec = NULL;
11734 htab->second_toc_pass = TRUE;
11735 return done_something;
11736}
11737
11738/* Called after second pass of multitoc partitioning. */
11739
11740void
11741ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11742{
11743 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11744
11745 /* After the second pass, toc_curr tracks the TOC offset used
11746 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11747 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11748}
11749
9b5ecbd0
AM
11750/* No toc references were found in ISEC. If the code in ISEC makes no
11751 calls, then there's no need to use toc adjusting stubs when branching
11752 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11753 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11754 needed, and 2 if a cyclical call-graph was found but no other reason
11755 for a stub was detected. If called from the top level, a return of
11756 2 means the same as a return of 0. */
9b5ecbd0
AM
11757
11758static int
4ce794b7 11759toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11760{
9b5ecbd0 11761 int ret;
70cc837d
AM
11762
11763 /* Mark this section as checked. */
11764 isec->call_check_done = 1;
9b5ecbd0 11765
772119ce
AM
11766 /* We know none of our code bearing sections will need toc stubs. */
11767 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11768 return 0;
11769
eea6121a 11770 if (isec->size == 0)
082c50f8
AM
11771 return 0;
11772
4c52953f
AM
11773 if (isec->output_section == NULL)
11774 return 0;
11775
4c52953f 11776 ret = 0;
70cc837d 11777 if (isec->reloc_count != 0)
9b5ecbd0 11778 {
70cc837d
AM
11779 Elf_Internal_Rela *relstart, *rel;
11780 Elf_Internal_Sym *local_syms;
11781 struct ppc_link_hash_table *htab;
2917689a 11782
70cc837d
AM
11783 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11784 info->keep_memory);
11785 if (relstart == NULL)
11786 return -1;
90aecf7a 11787
70cc837d
AM
11788 /* Look for branches to outside of this section. */
11789 local_syms = NULL;
11790 htab = ppc_hash_table (info);
11791 if (htab == NULL)
11792 return -1;
4c52953f 11793
70cc837d 11794 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11795 {
70cc837d
AM
11796 enum elf_ppc64_reloc_type r_type;
11797 unsigned long r_symndx;
11798 struct elf_link_hash_entry *h;
11799 struct ppc_link_hash_entry *eh;
11800 Elf_Internal_Sym *sym;
11801 asection *sym_sec;
11802 struct _opd_sec_data *opd;
11803 bfd_vma sym_value;
11804 bfd_vma dest;
11805
11806 r_type = ELF64_R_TYPE (rel->r_info);
11807 if (r_type != R_PPC64_REL24
11808 && r_type != R_PPC64_REL14
11809 && r_type != R_PPC64_REL14_BRTAKEN
11810 && r_type != R_PPC64_REL14_BRNTAKEN)
11811 continue;
4c52953f 11812
70cc837d
AM
11813 r_symndx = ELF64_R_SYM (rel->r_info);
11814 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11815 isec->owner))
4c52953f 11816 {
70cc837d
AM
11817 ret = -1;
11818 break;
11819 }
4c52953f 11820
70cc837d
AM
11821 /* Calls to dynamic lib functions go through a plt call stub
11822 that uses r2. */
11823 eh = (struct ppc_link_hash_entry *) h;
11824 if (eh != NULL
11825 && (eh->elf.plt.plist != NULL
11826 || (eh->oh != NULL
11827 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11828 {
11829 ret = 1;
11830 break;
4c52953f
AM
11831 }
11832
70cc837d
AM
11833 if (sym_sec == NULL)
11834 /* Ignore other undefined symbols. */
4c52953f 11835 continue;
4c52953f 11836
70cc837d
AM
11837 /* Assume branches to other sections not included in the
11838 link need stubs too, to cover -R and absolute syms. */
11839 if (sym_sec->output_section == NULL)
11840 {
11841 ret = 1;
11842 break;
11843 }
4c52953f 11844
70cc837d
AM
11845 if (h == NULL)
11846 sym_value = sym->st_value;
11847 else
11848 {
11849 if (h->root.type != bfd_link_hash_defined
11850 && h->root.type != bfd_link_hash_defweak)
11851 abort ();
11852 sym_value = h->root.u.def.value;
11853 }
11854 sym_value += rel->r_addend;
4c52953f 11855
70cc837d
AM
11856 /* If this branch reloc uses an opd sym, find the code section. */
11857 opd = get_opd_info (sym_sec);
11858 if (opd != NULL)
11859 {
11860 if (h == NULL && opd->adjust != NULL)
11861 {
11862 long adjust;
4c52953f 11863
92a9c616 11864 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11865 if (adjust == -1)
11866 /* Assume deleted functions won't ever be called. */
11867 continue;
11868 sym_value += adjust;
11869 }
4c52953f 11870
aef36ac1
AM
11871 dest = opd_entry_value (sym_sec, sym_value,
11872 &sym_sec, NULL, FALSE);
70cc837d
AM
11873 if (dest == (bfd_vma) -1)
11874 continue;
11875 }
11876 else
11877 dest = (sym_value
11878 + sym_sec->output_offset
11879 + sym_sec->output_section->vma);
4c52953f 11880
70cc837d
AM
11881 /* Ignore branch to self. */
11882 if (sym_sec == isec)
11883 continue;
4c52953f 11884
70cc837d
AM
11885 /* If the called function uses the toc, we need a stub. */
11886 if (sym_sec->has_toc_reloc
11887 || sym_sec->makes_toc_func_call)
4c52953f 11888 {
70cc837d 11889 ret = 1;
4c52953f
AM
11890 break;
11891 }
70cc837d
AM
11892
11893 /* Assume any branch that needs a long branch stub might in fact
11894 need a plt_branch stub. A plt_branch stub uses r2. */
11895 else if (dest - (isec->output_offset
11896 + isec->output_section->vma
6911b7dc
AM
11897 + rel->r_offset) + (1 << 25)
11898 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11899 ? h->other
11900 : sym->st_other))
4c52953f 11901 {
70cc837d
AM
11902 ret = 1;
11903 break;
11904 }
11905
11906 /* If calling back to a section in the process of being
11907 tested, we can't say for sure that no toc adjusting stubs
11908 are needed, so don't return zero. */
11909 else if (sym_sec->call_check_in_progress)
11910 ret = 2;
11911
11912 /* Branches to another section that itself doesn't have any TOC
11913 references are OK. Recursively call ourselves to check. */
11914 else if (!sym_sec->call_check_done)
11915 {
11916 int recur;
11917
11918 /* Mark current section as indeterminate, so that other
11919 sections that call back to current won't be marked as
11920 known. */
11921 isec->call_check_in_progress = 1;
11922 recur = toc_adjusting_stub_needed (info, sym_sec);
11923 isec->call_check_in_progress = 0;
11924
4c52953f
AM
11925 if (recur != 0)
11926 {
70cc837d
AM
11927 ret = recur;
11928 if (recur != 2)
11929 break;
4c52953f
AM
11930 }
11931 }
4c52953f 11932 }
70cc837d
AM
11933
11934 if (local_syms != NULL
11935 && (elf_symtab_hdr (isec->owner).contents
11936 != (unsigned char *) local_syms))
11937 free (local_syms);
11938 if (elf_section_data (isec)->relocs != relstart)
11939 free (relstart);
9b5ecbd0
AM
11940 }
11941
70cc837d
AM
11942 if ((ret & 1) == 0
11943 && isec->map_head.s != NULL
11944 && (strcmp (isec->output_section->name, ".init") == 0
11945 || strcmp (isec->output_section->name, ".fini") == 0))
11946 {
11947 if (isec->map_head.s->has_toc_reloc
11948 || isec->map_head.s->makes_toc_func_call)
11949 ret = 1;
11950 else if (!isec->map_head.s->call_check_done)
11951 {
11952 int recur;
11953 isec->call_check_in_progress = 1;
11954 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11955 isec->call_check_in_progress = 0;
11956 if (recur != 0)
11957 ret = recur;
11958 }
11959 }
11960
11961 if (ret == 1)
11962 isec->makes_toc_func_call = 1;
4c52953f 11963
9b5ecbd0
AM
11964 return ret;
11965}
11966
721956f4
AM
11967/* The linker repeatedly calls this function for each input section,
11968 in the order that input sections are linked into output sections.
11969 Build lists of input sections to determine groupings between which
11970 we may insert linker stubs. */
11971
9b5ecbd0 11972bfd_boolean
4ce794b7 11973ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11974{
11975 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11976
4dfe6ac6
NC
11977 if (htab == NULL)
11978 return FALSE;
11979
734b6cf9 11980 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 11981 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 11982 {
3d6f9012
AM
11983 /* This happens to make the list in reverse order,
11984 which is what we want. */
6f20ed8a
AM
11985 htab->sec_info[isec->id].u.list
11986 = htab->sec_info[isec->output_section->id].u.list;
11987 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 11988 }
ad8e1ba5 11989
4c52953f 11990 if (htab->multi_toc_needed)
9b5ecbd0 11991 {
8b974ba3
AM
11992 /* Analyse sections that aren't already flagged as needing a
11993 valid toc pointer. Exclude .fixup for the linux kernel.
11994 .fixup contains branches, but only back to the function that
11995 hit an exception. */
11996 if (!(isec->has_toc_reloc
11997 || (isec->flags & SEC_CODE) == 0
11998 || strcmp (isec->name, ".fixup") == 0
11999 || isec->call_check_done))
12000 {
12001 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12002 return FALSE;
8b974ba3
AM
12003 }
12004 /* Make all sections use the TOC assigned for this object file.
12005 This will be wrong for pasted sections; We fix that in
12006 check_pasted_section(). */
12007 if (elf_gp (isec->owner) != 0)
12008 htab->toc_curr = elf_gp (isec->owner);
12009 }
12010
6f20ed8a 12011 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12012 return TRUE;
721956f4
AM
12013}
12014
70cc837d
AM
12015/* Check that all .init and .fini sections use the same toc, if they
12016 have toc relocs. */
12017
12018static bfd_boolean
12019check_pasted_section (struct bfd_link_info *info, const char *name)
12020{
12021 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12022
12023 if (o != NULL)
12024 {
12025 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12026 bfd_vma toc_off = 0;
12027 asection *i;
12028
12029 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12030 if (i->has_toc_reloc)
12031 {
12032 if (toc_off == 0)
6f20ed8a
AM
12033 toc_off = htab->sec_info[i->id].toc_off;
12034 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12035 return FALSE;
12036 }
6683a28d
AM
12037
12038 if (toc_off == 0)
12039 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12040 if (i->makes_toc_func_call)
12041 {
6f20ed8a 12042 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12043 break;
12044 }
12045
70cc837d
AM
12046 /* Make sure the whole pasted function uses the same toc offset. */
12047 if (toc_off != 0)
12048 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12049 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12050 }
12051 return TRUE;
12052}
12053
12054bfd_boolean
12055ppc64_elf_check_init_fini (struct bfd_link_info *info)
12056{
12057 return (check_pasted_section (info, ".init")
12058 & check_pasted_section (info, ".fini"));
12059}
12060
721956f4
AM
12061/* See whether we can group stub sections together. Grouping stub
12062 sections may result in fewer stubs. More importantly, we need to
12063 put all .init* and .fini* stubs at the beginning of the .init or
12064 .fini output sections respectively, because glibc splits the
12065 _init and _fini functions into multiple parts. Putting a stub in
12066 the middle of a function is not a good idea. */
12067
6f20ed8a
AM
12068static bfd_boolean
12069group_sections (struct bfd_link_info *info,
4ce794b7
AM
12070 bfd_size_type stub_group_size,
12071 bfd_boolean stubs_always_before_branch)
721956f4 12072{
6f20ed8a
AM
12073 struct ppc_link_hash_table *htab;
12074 asection *osec;
7c8fe5c4
AM
12075 bfd_boolean suppress_size_errors;
12076
6f20ed8a
AM
12077 htab = ppc_hash_table (info);
12078 if (htab == NULL)
12079 return FALSE;
12080
7c8fe5c4 12081 suppress_size_errors = FALSE;
7c8fe5c4
AM
12082 if (stub_group_size == 1)
12083 {
12084 /* Default values. */
12085 if (stubs_always_before_branch)
09f92717 12086 stub_group_size = 0x1e00000;
7c8fe5c4 12087 else
09f92717 12088 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12089 suppress_size_errors = TRUE;
12090 }
12091
6f20ed8a 12092 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12093 {
6f20ed8a
AM
12094 asection *tail;
12095
12096 if (osec->id >= htab->sec_info_arr_size)
12097 continue;
12098
12099 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12100 while (tail != NULL)
721956f4 12101 {
734b6cf9
AM
12102 asection *curr;
12103 asection *prev;
12104 bfd_size_type total;
12105 bfd_boolean big_sec;
12106 bfd_vma curr_toc;
6f20ed8a 12107 struct map_stub *group;
09f92717 12108 bfd_size_type group_size;
734b6cf9
AM
12109
12110 curr = tail;
eea6121a 12111 total = tail->size;
09f92717
AM
12112 group_size = (ppc64_elf_section_data (tail) != NULL
12113 && ppc64_elf_section_data (tail)->has_14bit_branch
12114 ? stub_group_size >> 10 : stub_group_size);
12115
12116 big_sec = total > group_size;
7c8fe5c4 12117 if (big_sec && !suppress_size_errors)
695344c0 12118 /* xgettext:c-format */
4eca0228
AM
12119 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12120 tail->owner, tail);
6f20ed8a 12121 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12122
6f20ed8a 12123 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12124 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12125 < (ppc64_elf_section_data (prev) != NULL
12126 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12127 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12128 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12129 curr = prev;
12130
12131 /* OK, the size from the start of CURR to the end is less
09f92717 12132 than group_size and thus can be handled by one stub
734b6cf9 12133 section. (or the tail section is itself larger than
09f92717
AM
12134 group_size, in which case we may be toast.) We should
12135 really be keeping track of the total size of stubs added
12136 here, as stubs contribute to the final output section
12137 size. That's a little tricky, and this way will only
12138 break if stubs added make the total size more than 2^25,
12139 ie. for the default stub_group_size, if stubs total more
12140 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12141 group = bfd_alloc (curr->owner, sizeof (*group));
12142 if (group == NULL)
12143 return FALSE;
12144 group->link_sec = curr;
12145 group->stub_sec = NULL;
a4b6fadd
AM
12146 group->needs_save_res = 0;
12147 group->next = htab->group;
12148 htab->group = group;
734b6cf9 12149 do
721956f4 12150 {
6f20ed8a 12151 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12152 /* Set up this stub group. */
6f20ed8a 12153 htab->sec_info[tail->id].u.group = group;
721956f4 12154 }
734b6cf9
AM
12155 while (tail != curr && (tail = prev) != NULL);
12156
09f92717 12157 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12158 bytes before the stub section can be handled by it too.
12159 Don't do this if we have a really large section after the
12160 stubs, as adding more stubs increases the chance that
12161 branches may not reach into the stub section. */
12162 if (!stubs_always_before_branch && !big_sec)
12163 {
12164 total = 0;
12165 while (prev != NULL
12166 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12167 < (ppc64_elf_section_data (prev) != NULL
12168 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12169 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12170 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12171 {
12172 tail = prev;
6f20ed8a
AM
12173 prev = htab->sec_info[tail->id].u.list;
12174 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12175 }
12176 }
12177 tail = prev;
721956f4
AM
12178 }
12179 }
6f20ed8a 12180 return TRUE;
721956f4
AM
12181}
12182
58d180e8
AM
12183static const unsigned char glink_eh_frame_cie[] =
12184{
12185 0, 0, 0, 16, /* length. */
12186 0, 0, 0, 0, /* id. */
12187 1, /* CIE version. */
12188 'z', 'R', 0, /* Augmentation string. */
12189 4, /* Code alignment. */
12190 0x78, /* Data alignment. */
12191 65, /* RA reg. */
12192 1, /* Augmentation size. */
12193 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
da44f4e5
AM
12194 DW_CFA_def_cfa, 1, 0, /* def_cfa: r1 offset 0. */
12195 0, 0, 0, 0
58d180e8
AM
12196};
12197
d969d15f
AM
12198/* Stripping output sections is normally done before dynamic section
12199 symbols have been allocated. This function is called later, and
12200 handles cases like htab->brlt which is mapped to its own output
12201 section. */
12202
12203static void
12204maybe_strip_output (struct bfd_link_info *info, asection *isec)
12205{
12206 if (isec->size == 0
12207 && isec->output_section->size == 0
53d8967a 12208 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12209 && !bfd_section_removed_from_list (info->output_bfd,
12210 isec->output_section)
12211 && elf_section_data (isec->output_section)->dynindx == 0)
12212 {
12213 isec->output_section->flags |= SEC_EXCLUDE;
12214 bfd_section_list_remove (info->output_bfd, isec->output_section);
12215 info->output_bfd->section_count--;
12216 }
12217}
12218
721956f4
AM
12219/* Determine and set the size of the stub section for a final link.
12220
12221 The basic idea here is to examine all the relocations looking for
12222 PC-relative calls to a target that is unreachable with a "bl"
12223 instruction. */
12224
b34976b6 12225bfd_boolean
e7d1c40c 12226ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12227{
12228 bfd_size_type stub_group_size;
b34976b6 12229 bfd_boolean stubs_always_before_branch;
721956f4
AM
12230 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12231
4dfe6ac6
NC
12232 if (htab == NULL)
12233 return FALSE;
12234
0e1862bb 12235 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12236 htab->params->plt_thread_safe = 1;
b9e5796b 12237 if (!htab->opd_abi)
e7d1c40c
AM
12238 htab->params->plt_thread_safe = 0;
12239 else if (htab->params->plt_thread_safe == -1)
794e51c0 12240 {
e2458743 12241 static const char *const thread_starter[] =
794e51c0
AM
12242 {
12243 "pthread_create",
12244 /* libstdc++ */
12245 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12246 /* librt */
12247 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12248 "mq_notify", "create_timer",
12249 /* libanl */
12250 "getaddrinfo_a",
12251 /* libgomp */
2300b5a1 12252 "GOMP_parallel",
794e51c0 12253 "GOMP_parallel_start",
2300b5a1 12254 "GOMP_parallel_loop_static",
794e51c0 12255 "GOMP_parallel_loop_static_start",
2300b5a1 12256 "GOMP_parallel_loop_dynamic",
794e51c0 12257 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12258 "GOMP_parallel_loop_guided",
794e51c0 12259 "GOMP_parallel_loop_guided_start",
2300b5a1 12260 "GOMP_parallel_loop_runtime",
794e51c0 12261 "GOMP_parallel_loop_runtime_start",
2300b5a1 12262 "GOMP_parallel_sections",
68ffbac6 12263 "GOMP_parallel_sections_start",
f9dffbf0
AM
12264 /* libgo */
12265 "__go_go",
794e51c0
AM
12266 };
12267 unsigned i;
12268
a4b6fadd 12269 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12270 {
12271 struct elf_link_hash_entry *h;
12272 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12273 FALSE, FALSE, TRUE);
e7d1c40c
AM
12274 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12275 if (htab->params->plt_thread_safe)
794e51c0
AM
12276 break;
12277 }
12278 }
e7d1c40c
AM
12279 stubs_always_before_branch = htab->params->group_size < 0;
12280 if (htab->params->group_size < 0)
12281 stub_group_size = -htab->params->group_size;
721956f4 12282 else
e7d1c40c 12283 stub_group_size = htab->params->group_size;
721956f4 12284
6f20ed8a
AM
12285 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12286 return FALSE;
721956f4 12287
c9301e31
AM
12288#define STUB_SHRINK_ITER 20
12289 /* Loop until no stubs added. After iteration 20 of this loop we may
12290 exit on a stub section shrinking. This is to break out of a
12291 pathological case where adding stubs on one iteration decreases
12292 section gaps (perhaps due to alignment), which then requires
12293 fewer or smaller stubs on the next iteration. */
12294
721956f4
AM
12295 while (1)
12296 {
12297 bfd *input_bfd;
12298 unsigned int bfd_indx;
a4b6fadd 12299 struct map_stub *group;
721956f4 12300 asection *stub_sec;
721956f4
AM
12301
12302 htab->stub_iteration += 1;
721956f4
AM
12303
12304 for (input_bfd = info->input_bfds, bfd_indx = 0;
12305 input_bfd != NULL;
c72f2fb2 12306 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12307 {
12308 Elf_Internal_Shdr *symtab_hdr;
12309 asection *section;
6cdc0ccc 12310 Elf_Internal_Sym *local_syms = NULL;
721956f4 12311
0c8d6e5c 12312 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12313 continue;
12314
721956f4 12315 /* We'll need the symbol table in a second. */
0ffa91dd 12316 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12317 if (symtab_hdr->sh_info == 0)
12318 continue;
12319
721956f4
AM
12320 /* Walk over each section attached to the input bfd. */
12321 for (section = input_bfd->sections;
12322 section != NULL;
12323 section = section->next)
12324 {
721956f4 12325 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12326
12327 /* If there aren't any relocs, then there's nothing more
12328 to do. */
12329 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12330 || (section->flags & SEC_ALLOC) == 0
12331 || (section->flags & SEC_LOAD) == 0
12332 || (section->flags & SEC_CODE) == 0
721956f4
AM
12333 || section->reloc_count == 0)
12334 continue;
12335
12336 /* If this section is a link-once section that will be
12337 discarded, then don't create any stubs. */
12338 if (section->output_section == NULL
927be08e 12339 || section->output_section->owner != info->output_bfd)
721956f4
AM
12340 continue;
12341
1e2f5b6e
AM
12342 /* Get the relocs. */
12343 internal_relocs
4ce794b7 12344 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12345 info->keep_memory);
721956f4 12346 if (internal_relocs == NULL)
1e2f5b6e 12347 goto error_ret_free_local;
721956f4
AM
12348
12349 /* Now examine each relocation. */
12350 irela = internal_relocs;
12351 irelaend = irela + section->reloc_count;
12352 for (; irela < irelaend; irela++)
12353 {
4ce794b7
AM
12354 enum elf_ppc64_reloc_type r_type;
12355 unsigned int r_indx;
721956f4
AM
12356 enum ppc_stub_type stub_type;
12357 struct ppc_stub_hash_entry *stub_entry;
8387904d 12358 asection *sym_sec, *code_sec;
e054468f 12359 bfd_vma sym_value, code_value;
721956f4 12360 bfd_vma destination;
6911b7dc 12361 unsigned long local_off;
8843416a 12362 bfd_boolean ok_dest;
721956f4 12363 struct ppc_link_hash_entry *hash;
8387904d 12364 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12365 struct elf_link_hash_entry *h;
12366 Elf_Internal_Sym *sym;
721956f4
AM
12367 char *stub_name;
12368 const asection *id_sec;
74f0fb50 12369 struct _opd_sec_data *opd;
e054468f 12370 struct plt_entry *plt_ent;
721956f4
AM
12371
12372 r_type = ELF64_R_TYPE (irela->r_info);
12373 r_indx = ELF64_R_SYM (irela->r_info);
12374
4ce794b7 12375 if (r_type >= R_PPC64_max)
721956f4
AM
12376 {
12377 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12378 goto error_ret_free_internal;
721956f4
AM
12379 }
12380
12381 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12382 if (r_type != R_PPC64_REL24
12383 && r_type != R_PPC64_REL14
12384 && r_type != R_PPC64_REL14_BRTAKEN
12385 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12386 continue;
12387
12388 /* Now determine the call target, its name, value,
12389 section. */
411e1bfb
AM
12390 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12391 r_indx, input_bfd))
12392 goto error_ret_free_internal;
12393 hash = (struct ppc_link_hash_entry *) h;
12394
8843416a 12395 ok_dest = FALSE;
8387904d 12396 fdh = NULL;
7fe2b9a6 12397 sym_value = 0;
411e1bfb 12398 if (hash == NULL)
721956f4 12399 {
411e1bfb 12400 sym_value = sym->st_value;
c27b8c2a
AM
12401 if (sym_sec != NULL
12402 && sym_sec->output_section != NULL)
12403 ok_dest = TRUE;
721956f4 12404 }
7fe2b9a6
AM
12405 else if (hash->elf.root.type == bfd_link_hash_defined
12406 || hash->elf.root.type == bfd_link_hash_defweak)
12407 {
12408 sym_value = hash->elf.root.u.def.value;
12409 if (sym_sec->output_section != NULL)
12410 ok_dest = TRUE;
12411 }
12412 else if (hash->elf.root.type == bfd_link_hash_undefweak
12413 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12414 {
99877b66 12415 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12416 use the func descriptor sym instead if it is
12417 defined. */
ceb1f1ef 12418 if (hash->elf.root.root.string[0] == '.'
b31867b6 12419 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 12420 {
8387904d
AM
12421 if (fdh->elf.root.type == bfd_link_hash_defined
12422 || fdh->elf.root.type == bfd_link_hash_defweak)
12423 {
12424 sym_sec = fdh->elf.root.u.def.section;
12425 sym_value = fdh->elf.root.u.def.value;
12426 if (sym_sec->output_section != NULL)
12427 ok_dest = TRUE;
12428 }
99877b66
AM
12429 else
12430 fdh = NULL;
8387904d 12431 }
7fe2b9a6
AM
12432 }
12433 else
12434 {
12435 bfd_set_error (bfd_error_bad_value);
12436 goto error_ret_free_internal;
721956f4
AM
12437 }
12438
8843416a 12439 destination = 0;
6911b7dc 12440 local_off = 0;
8843416a
AM
12441 if (ok_dest)
12442 {
12443 sym_value += irela->r_addend;
12444 destination = (sym_value
12445 + sym_sec->output_offset
12446 + sym_sec->output_section->vma);
6911b7dc
AM
12447 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12448 ? hash->elf.other
12449 : sym->st_other);
8843416a
AM
12450 }
12451
8387904d 12452 code_sec = sym_sec;
e054468f 12453 code_value = sym_value;
74f0fb50
AM
12454 opd = get_opd_info (sym_sec);
12455 if (opd != NULL)
8387904d
AM
12456 {
12457 bfd_vma dest;
12458
74f0fb50 12459 if (hash == NULL && opd->adjust != NULL)
8387904d 12460 {
51aecdc5 12461 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12462 if (adjust == -1)
12463 continue;
e054468f 12464 code_value += adjust;
8387904d
AM
12465 sym_value += adjust;
12466 }
12467 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12468 &code_sec, &code_value, FALSE);
8387904d
AM
12469 if (dest != (bfd_vma) -1)
12470 {
12471 destination = dest;
12472 if (fdh != NULL)
12473 {
12474 /* Fixup old ABI sym to point at code
12475 entry. */
99877b66 12476 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12477 hash->elf.root.u.def.section = code_sec;
e054468f 12478 hash->elf.root.u.def.value = code_value;
8387904d
AM
12479 }
12480 }
12481 }
12482
721956f4 12483 /* Determine what (if any) linker stub is needed. */
e054468f 12484 plt_ent = NULL;
721956f4 12485 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12486 &plt_ent, destination,
12487 local_off);
ad8e1ba5
AM
12488
12489 if (stub_type != ppc_stub_plt_call)
12490 {
12491 /* Check whether we need a TOC adjusting stub.
12492 Since the linker pastes together pieces from
12493 different object files when creating the
12494 _init and _fini functions, it may be that a
12495 call to what looks like a local sym is in
12496 fact a call needing a TOC adjustment. */
8387904d
AM
12497 if (code_sec != NULL
12498 && code_sec->output_section != NULL
6f20ed8a
AM
12499 && (htab->sec_info[code_sec->id].toc_off
12500 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12501 && (code_sec->has_toc_reloc
12502 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12503 stub_type = ppc_stub_long_branch_r2off;
12504 }
12505
721956f4
AM
12506 if (stub_type == ppc_stub_none)
12507 continue;
12508
411e1bfb
AM
12509 /* __tls_get_addr calls might be eliminated. */
12510 if (stub_type != ppc_stub_plt_call
12511 && hash != NULL
8387904d
AM
12512 && (hash == htab->tls_get_addr
12513 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12514 && section->has_tls_reloc
12515 && irela != internal_relocs)
12516 {
12517 /* Get tls info. */
f961d9dd 12518 unsigned char *tls_mask;
411e1bfb 12519
3a71aa26 12520 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12521 irela - 1, input_bfd))
12522 goto error_ret_free_internal;
e7b938ca 12523 if (*tls_mask != 0)
411e1bfb
AM
12524 continue;
12525 }
12526
3b421ab3
AM
12527 if (stub_type == ppc_stub_plt_call
12528 && irela + 1 < irelaend
12529 && irela[1].r_offset == irela->r_offset + 4
794e51c0
AM
12530 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12531 {
12532 if (!tocsave_find (htab, INSERT,
12533 &local_syms, irela + 1, input_bfd))
12534 goto error_ret_free_internal;
12535 }
12536 else if (stub_type == ppc_stub_plt_call)
12537 stub_type = ppc_stub_plt_call_r2save;
3b421ab3 12538
721956f4 12539 /* Support for grouping stub sections. */
6f20ed8a 12540 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12541
12542 /* Get the name of this stub. */
12543 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12544 if (!stub_name)
12545 goto error_ret_free_internal;
12546
12547 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12548 stub_name, FALSE, FALSE);
721956f4
AM
12549 if (stub_entry != NULL)
12550 {
12551 /* The proper stub has already been created. */
12552 free (stub_name);
794e51c0
AM
12553 if (stub_type == ppc_stub_plt_call_r2save)
12554 stub_entry->stub_type = stub_type;
721956f4
AM
12555 continue;
12556 }
12557
25f53a85 12558 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12559 if (stub_entry == NULL)
12560 {
12561 free (stub_name);
6cdc0ccc
AM
12562 error_ret_free_internal:
12563 if (elf_section_data (section)->relocs == NULL)
12564 free (internal_relocs);
12565 error_ret_free_local:
12566 if (local_syms != NULL
12567 && (symtab_hdr->contents
12568 != (unsigned char *) local_syms))
12569 free (local_syms);
b34976b6 12570 return FALSE;
721956f4
AM
12571 }
12572
ad8e1ba5 12573 stub_entry->stub_type = stub_type;
794e51c0
AM
12574 if (stub_type != ppc_stub_plt_call
12575 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12576 {
12577 stub_entry->target_value = code_value;
12578 stub_entry->target_section = code_sec;
12579 }
12580 else
12581 {
12582 stub_entry->target_value = sym_value;
12583 stub_entry->target_section = sym_sec;
12584 }
721956f4 12585 stub_entry->h = hash;
e054468f 12586 stub_entry->plt_ent = plt_ent;
6911b7dc 12587 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12588
12589 if (stub_entry->h != NULL)
12590 htab->stub_globals += 1;
721956f4
AM
12591 }
12592
12593 /* We're done with the internal relocs, free them. */
6cdc0ccc 12594 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12595 free (internal_relocs);
721956f4 12596 }
6cdc0ccc
AM
12597
12598 if (local_syms != NULL
12599 && symtab_hdr->contents != (unsigned char *) local_syms)
12600 {
12601 if (!info->keep_memory)
12602 free (local_syms);
12603 else
12604 symtab_hdr->contents = (unsigned char *) local_syms;
12605 }
721956f4
AM
12606 }
12607
5c3dead3 12608 /* We may have added some stubs. Find out the new size of the
721956f4 12609 stub sections. */
e7d1c40c 12610 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
12611 stub_sec != NULL;
12612 stub_sec = stub_sec->next)
e717da7e 12613 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 12614 {
ea3d7d1c
AM
12615 if (htab->stub_iteration <= STUB_SHRINK_ITER
12616 || stub_sec->rawsize < stub_sec->size)
12617 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12618 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12619 stub_sec->size = 0;
12620 stub_sec->reloc_count = 0;
84f5d08e 12621 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12622 }
eea6121a
AM
12623
12624 htab->brlt->size = 0;
84f5d08e
AM
12625 htab->brlt->reloc_count = 0;
12626 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12627 if (htab->relbrlt != NULL)
eea6121a 12628 htab->relbrlt->size = 0;
721956f4 12629
63bc6f6c 12630 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12631
a4b6fadd
AM
12632 for (group = htab->group; group != NULL; group = group->next)
12633 if (group->needs_save_res)
12634 group->stub_sec->size += htab->sfpr->size;
12635
176a0d42
AM
12636 if (info->emitrelocations
12637 && htab->glink != NULL && htab->glink->size != 0)
12638 {
12639 htab->glink->reloc_count = 1;
12640 htab->glink->flags |= SEC_RELOC;
12641 }
12642
58d180e8
AM
12643 if (htab->glink_eh_frame != NULL
12644 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
9a2a56cc 12645 && htab->glink_eh_frame->output_section->size != 0)
58d180e8 12646 {
4bbe044a 12647 size_t size = 0, align;
58d180e8 12648
e7d1c40c 12649 for (stub_sec = htab->params->stub_bfd->sections;
58d180e8
AM
12650 stub_sec != NULL;
12651 stub_sec = stub_sec->next)
12652 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
da44f4e5 12653 size += 24;
58d180e8
AM
12654 if (htab->glink != NULL && htab->glink->size != 0)
12655 size += 24;
12656 if (size != 0)
12657 size += sizeof (glink_eh_frame_cie);
4bbe044a
AM
12658 align = 1;
12659 align <<= htab->glink_eh_frame->output_section->alignment_power;
12660 align -= 1;
12661 size = (size + align) & ~align;
58d180e8
AM
12662 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12663 htab->glink_eh_frame->size = size;
12664 }
12665
e7d1c40c
AM
12666 if (htab->params->plt_stub_align != 0)
12667 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
12668 stub_sec != NULL;
12669 stub_sec = stub_sec->next)
12670 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
12671 stub_sec->size = ((stub_sec->size
12672 + (1 << htab->params->plt_stub_align) - 1)
29f628db 12673 & -(1 << htab->params->plt_stub_align));
794e51c0 12674
e7d1c40c 12675 for (stub_sec = htab->params->stub_bfd->sections;
5c3dead3
AM
12676 stub_sec != NULL;
12677 stub_sec = stub_sec->next)
12678 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
c9301e31
AM
12679 && stub_sec->rawsize != stub_sec->size
12680 && (htab->stub_iteration <= STUB_SHRINK_ITER
12681 || stub_sec->rawsize < stub_sec->size))
5c3dead3
AM
12682 break;
12683
58d180e8
AM
12684 if (stub_sec == NULL
12685 && (htab->glink_eh_frame == NULL
12686 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12687 break;
12688
721956f4 12689 /* Ask the linker to do its stuff. */
e7d1c40c 12690 (*htab->params->layout_sections_again) ();
721956f4
AM
12691 }
12692
da44f4e5
AM
12693 if (htab->glink_eh_frame != NULL
12694 && htab->glink_eh_frame->size != 0)
12695 {
12696 bfd_vma val;
12697 bfd_byte *p, *last_fde;
12698 size_t last_fde_len, size, align, pad;
12699 asection *stub_sec;
12700
12701 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12702 if (p == NULL)
12703 return FALSE;
12704 htab->glink_eh_frame->contents = p;
12705 last_fde = p;
12706
12707 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12708 /* CIE length (rewrite in case little-endian). */
12709 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12710 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12711 p += sizeof (glink_eh_frame_cie);
12712
12713 for (stub_sec = htab->params->stub_bfd->sections;
12714 stub_sec != NULL;
12715 stub_sec = stub_sec->next)
12716 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12717 {
12718 last_fde = p;
12719 last_fde_len = 20;
12720 /* FDE length. */
12721 bfd_put_32 (htab->elf.dynobj, 20, p);
12722 p += 4;
12723 /* CIE pointer. */
12724 val = p - htab->glink_eh_frame->contents;
12725 bfd_put_32 (htab->elf.dynobj, val, p);
12726 p += 4;
12727 /* Offset to stub section, written later. */
12728 p += 4;
12729 /* stub section size. */
12730 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12731 p += 4;
12732 /* Augmentation. */
12733 p += 1;
12734 /* Pad. */
12735 p += 7;
12736 }
12737 if (htab->glink != NULL && htab->glink->size != 0)
12738 {
12739 last_fde = p;
12740 last_fde_len = 20;
12741 /* FDE length. */
12742 bfd_put_32 (htab->elf.dynobj, 20, p);
12743 p += 4;
12744 /* CIE pointer. */
12745 val = p - htab->glink_eh_frame->contents;
12746 bfd_put_32 (htab->elf.dynobj, val, p);
12747 p += 4;
12748 /* Offset to .glink, written later. */
12749 p += 4;
12750 /* .glink size. */
12751 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12752 p += 4;
12753 /* Augmentation. */
12754 p += 1;
12755
12756 *p++ = DW_CFA_advance_loc + 1;
12757 *p++ = DW_CFA_register;
12758 *p++ = 65;
9f08fa5c 12759 *p++ = htab->opd_abi ? 12 : 0;
da44f4e5
AM
12760 *p++ = DW_CFA_advance_loc + 4;
12761 *p++ = DW_CFA_restore_extended;
12762 *p++ = 65;
12763 }
12764 /* Subsume any padding into the last FDE if user .eh_frame
12765 sections are aligned more than glink_eh_frame. Otherwise any
12766 zero padding will be seen as a terminator. */
12767 size = p - htab->glink_eh_frame->contents;
12768 align = 1;
12769 align <<= htab->glink_eh_frame->output_section->alignment_power;
12770 align -= 1;
12771 pad = ((size + align) & ~align) - size;
12772 htab->glink_eh_frame->size = size + pad;
12773 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12774 }
12775
d969d15f
AM
12776 maybe_strip_output (info, htab->brlt);
12777 if (htab->glink_eh_frame != NULL)
12778 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12779
b34976b6 12780 return TRUE;
721956f4
AM
12781}
12782
12783/* Called after we have determined section placement. If sections
805fc799 12784 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12785
805fc799 12786bfd_vma
1c865ab2 12787ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12788{
805fc799 12789 asection *s;
a27e685f 12790 bfd_vma TOCstart, adjust;
721956f4 12791
43417696
AM
12792 if (info != NULL)
12793 {
12794 struct elf_link_hash_entry *h;
12795 struct elf_link_hash_table *htab = elf_hash_table (info);
12796
12797 if (is_elf_hash_table (htab)
12798 && htab->hgot != NULL)
12799 h = htab->hgot;
12800 else
12801 {
12802 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12803 if (is_elf_hash_table (htab))
12804 htab->hgot = h;
12805 }
12806 if (h != NULL
12807 && h->root.type == bfd_link_hash_defined
12808 && !h->root.linker_def
12809 && (!is_elf_hash_table (htab)
12810 || h->def_regular))
12811 {
12812 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12813 + h->root.u.def.section->output_offset
12814 + h->root.u.def.section->output_section->vma);
12815 _bfd_set_gp_value (obfd, TOCstart);
12816 return TOCstart;
12817 }
12818 }
12819
805fc799
AM
12820 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12821 order. The TOC starts where the first of these sections starts. */
12822 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12823 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12824 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12825 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12826 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12827 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12828 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12829 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12830 {
12831 /* This may happen for
12832 o references to TOC base (SYM@toc / TOC[tc0]) without a
12833 .toc directive
12834 o bad linker script
12835 o --gc-sections and empty TOC sections
12836
12837 FIXME: Warn user? */
12838
12839 /* Look for a likely section. We probably won't even be
12840 using TOCstart. */
12841 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12842 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12843 | SEC_EXCLUDE))
805fc799
AM
12844 == (SEC_ALLOC | SEC_SMALL_DATA))
12845 break;
721956f4 12846 if (s == NULL)
805fc799 12847 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12848 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12849 == (SEC_ALLOC | SEC_SMALL_DATA))
12850 break;
721956f4 12851 if (s == NULL)
805fc799 12852 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12853 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12854 == SEC_ALLOC)
805fc799 12855 break;
721956f4 12856 if (s == NULL)
805fc799 12857 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12858 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12859 break;
12860 }
721956f4 12861
805fc799
AM
12862 TOCstart = 0;
12863 if (s != NULL)
12864 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12865
a27e685f
AM
12866 /* Force alignment. */
12867 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12868 TOCstart -= adjust;
1c865ab2
AM
12869 _bfd_set_gp_value (obfd, TOCstart);
12870
810d4e75 12871 if (info != NULL && s != NULL)
1c865ab2
AM
12872 {
12873 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12874
810d4e75
AM
12875 if (htab != NULL)
12876 {
12877 if (htab->elf.hgot != NULL)
12878 {
a27e685f 12879 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12880 htab->elf.hgot->root.u.def.section = s;
12881 }
12882 }
12883 else
1c865ab2 12884 {
810d4e75
AM
12885 struct bfd_link_hash_entry *bh = NULL;
12886 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12887 s, TOC_BASE_OFF - adjust,
12888 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12889 }
12890 }
805fc799 12891 return TOCstart;
721956f4
AM
12892}
12893
a345bc8d
AM
12894/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12895 write out any global entry stubs. */
12896
12897static bfd_boolean
12898build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12899{
12900 struct bfd_link_info *info;
12901 struct ppc_link_hash_table *htab;
12902 struct plt_entry *pent;
12903 asection *s;
12904
12905 if (h->root.type == bfd_link_hash_indirect)
12906 return TRUE;
12907
12908 if (!h->pointer_equality_needed)
12909 return TRUE;
12910
12911 if (h->def_regular)
12912 return TRUE;
12913
12914 info = inf;
12915 htab = ppc_hash_table (info);
12916 if (htab == NULL)
12917 return FALSE;
12918
12919 s = htab->glink;
12920 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12921 if (pent->plt.offset != (bfd_vma) -1
12922 && pent->addend == 0)
12923 {
12924 bfd_byte *p;
12925 asection *plt;
12926 bfd_vma off;
12927
a345bc8d 12928 p = s->contents + h->root.u.def.value;
33e44f2e 12929 plt = htab->elf.splt;
a345bc8d
AM
12930 if (!htab->elf.dynamic_sections_created
12931 || h->dynindx == -1)
33e44f2e 12932 plt = htab->elf.iplt;
a345bc8d
AM
12933 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12934 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12935
12936 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12937 {
12938 info->callbacks->einfo
12939 (_("%P: linkage table error against `%T'\n"),
12940 h->root.root.string);
12941 bfd_set_error (bfd_error_bad_value);
12942 htab->stub_error = TRUE;
12943 }
12944
7341d5e2
AM
12945 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12946 if (htab->params->emit_stub_syms)
12947 {
12948 size_t len = strlen (h->root.root.string);
12949 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12950
12951 if (name == NULL)
12952 return FALSE;
12953
12954 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12955 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12956 if (h == NULL)
12957 return FALSE;
12958 if (h->root.type == bfd_link_hash_new)
12959 {
12960 h->root.type = bfd_link_hash_defined;
12961 h->root.u.def.section = s;
12962 h->root.u.def.value = p - s->contents;
12963 h->ref_regular = 1;
12964 h->def_regular = 1;
12965 h->ref_regular_nonweak = 1;
12966 h->forced_local = 1;
12967 h->non_elf = 0;
2ec55de3 12968 h->root.linker_def = 1;
7341d5e2
AM
12969 }
12970 }
12971
a345bc8d
AM
12972 if (PPC_HA (off) != 0)
12973 {
12974 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12975 p += 4;
12976 }
12977 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12978 p += 4;
12979 bfd_put_32 (s->owner, MTCTR_R12, p);
12980 p += 4;
12981 bfd_put_32 (s->owner, BCTR, p);
12982 break;
12983 }
12984 return TRUE;
12985}
12986
721956f4
AM
12987/* Build all the stubs associated with the current output file.
12988 The stubs are kept in a hash table attached to the main linker
12989 hash table. This function is called via gldelf64ppc_finish. */
12990
b34976b6 12991bfd_boolean
e7d1c40c 12992ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 12993 char **stats)
5d1634d7
AM
12994{
12995 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 12996 struct map_stub *group;
721956f4 12997 asection *stub_sec;
5d1634d7 12998 bfd_byte *p;
e717da7e 12999 int stub_sec_count = 0;
5d1634d7 13000
4dfe6ac6
NC
13001 if (htab == NULL)
13002 return FALSE;
13003
eea6121a 13004 /* Allocate memory to hold the linker stubs. */
e7d1c40c 13005 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
13006 stub_sec != NULL;
13007 stub_sec = stub_sec->next)
eea6121a
AM
13008 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
13009 && stub_sec->size != 0)
e717da7e 13010 {
e7d1c40c 13011 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
13012 if (stub_sec->contents == NULL)
13013 return FALSE;
eea6121a 13014 stub_sec->size = 0;
e717da7e 13015 }
5d1634d7 13016
23eb7e01 13017 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13018 {
9f951329 13019 unsigned int indx;
ad8e1ba5 13020 bfd_vma plt0;
9f951329 13021
721956f4 13022 /* Build the .glink plt call stub. */
e7d1c40c 13023 if (htab->params->emit_stub_syms)
97b639ba
AM
13024 {
13025 struct elf_link_hash_entry *h;
468392fb
AM
13026 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13027 TRUE, FALSE, FALSE);
97b639ba
AM
13028 if (h == NULL)
13029 return FALSE;
13030 if (h->root.type == bfd_link_hash_new)
13031 {
13032 h->root.type = bfd_link_hash_defined;
13033 h->root.u.def.section = htab->glink;
ee4bf8d2 13034 h->root.u.def.value = 8;
f5385ebf
AM
13035 h->ref_regular = 1;
13036 h->def_regular = 1;
13037 h->ref_regular_nonweak = 1;
13038 h->forced_local = 1;
13039 h->non_elf = 0;
2ec55de3 13040 h->root.linker_def = 1;
97b639ba
AM
13041 }
13042 }
33e44f2e
AM
13043 plt0 = (htab->elf.splt->output_section->vma
13044 + htab->elf.splt->output_offset
13045 - 16);
176a0d42
AM
13046 if (info->emitrelocations)
13047 {
13048 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13049 if (r == NULL)
13050 return FALSE;
13051 r->r_offset = (htab->glink->output_offset
13052 + htab->glink->output_section->vma);
13053 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13054 r->r_addend = plt0;
13055 }
4ce794b7 13056 p = htab->glink->contents;
176a0d42 13057 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13058 bfd_put_64 (htab->glink->owner, plt0, p);
13059 p += 8;
b9e5796b
AM
13060 if (htab->opd_abi)
13061 {
13062 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13063 p += 4;
13064 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13065 p += 4;
13066 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13067 p += 4;
13068 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13069 p += 4;
13070 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13071 p += 4;
13072 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13073 p += 4;
13074 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13075 p += 4;
13076 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13077 p += 4;
13078 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13079 p += 4;
13080 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13081 p += 4;
13082 }
13083 else
13084 {
13085 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13086 p += 4;
13087 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13088 p += 4;
13089 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13090 p += 4;
13091 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13092 p += 4;
13093 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13094 p += 4;
13095 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13096 p += 4;
13097 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13098 p += 4;
13099 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13100 p += 4;
13101 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13102 p += 4;
13103 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13104 p += 4;
13105 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13106 p += 4;
13107 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13108 p += 4;
13109 }
4ce794b7 13110 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 13111 p += 4;
ee4bf8d2
AM
13112 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13113 {
13114 bfd_put_32 (htab->glink->owner, NOP, p);
13115 p += 4;
13116 }
ad8e1ba5 13117
9f951329
AM
13118 /* Build the .glink lazy link call stubs. */
13119 indx = 0;
a345bc8d 13120 while (p < htab->glink->contents + htab->glink->rawsize)
9f951329 13121 {
b9e5796b 13122 if (htab->opd_abi)
9f951329 13123 {
b9e5796b
AM
13124 if (indx < 0x8000)
13125 {
13126 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13127 p += 4;
13128 }
13129 else
13130 {
13131 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13132 p += 4;
13133 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13134 p);
13135 p += 4;
13136 }
9f951329 13137 }
4ce794b7 13138 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13139 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13140 indx++;
9f951329
AM
13141 p += 4;
13142 }
a345bc8d
AM
13143
13144 /* Build .glink global entry stubs. */
13145 if (htab->glink->size > htab->glink->rawsize)
afe397ea 13146 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
5d1634d7 13147 }
5d1634d7 13148
7341d5e2 13149 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13150 {
4ce794b7 13151 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13152 htab->brlt->size);
4ce794b7 13153 if (htab->brlt->contents == NULL)
b34976b6 13154 return FALSE;
721956f4 13155 }
ee75fd95 13156 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13157 {
13158 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13159 htab->relbrlt->size);
63bc6f6c
AM
13160 if (htab->relbrlt->contents == NULL)
13161 return FALSE;
13162 }
5d1634d7 13163
721956f4
AM
13164 /* Build the stubs as directed by the stub hash table. */
13165 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13166
a4b6fadd
AM
13167 for (group = htab->group; group != NULL; group = group->next)
13168 if (group->needs_save_res)
13169 {
13170 stub_sec = group->stub_sec;
13171 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13172 htab->sfpr->size);
13173 if (htab->params->emit_stub_syms)
13174 {
13175 unsigned int i;
13176
13177 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13178 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13179 return FALSE;
13180 }
13181 stub_sec->size += htab->sfpr->size;
13182 }
13183
aa8a7074
AM
13184 if (htab->relbrlt != NULL)
13185 htab->relbrlt->reloc_count = 0;
13186
e7d1c40c
AM
13187 if (htab->params->plt_stub_align != 0)
13188 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
13189 stub_sec != NULL;
13190 stub_sec = stub_sec->next)
13191 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
13192 stub_sec->size = ((stub_sec->size
13193 + (1 << htab->params->plt_stub_align) - 1)
29f628db 13194 & -(1 << htab->params->plt_stub_align));
794e51c0 13195
e7d1c40c 13196 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
13197 stub_sec != NULL;
13198 stub_sec = stub_sec->next)
e717da7e
AM
13199 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13200 {
13201 stub_sec_count += 1;
c9301e31
AM
13202 if (stub_sec->rawsize != stub_sec->size
13203 && (htab->stub_iteration <= STUB_SHRINK_ITER
13204 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13205 break;
13206 }
5d1634d7 13207
da44f4e5
AM
13208 /* Note that the glink_eh_frame check here is not only testing that
13209 the generated size matched the calculated size but also that
13210 bfd_elf_discard_info didn't make any changes to the section. */
721956f4 13211 if (stub_sec != NULL
58d180e8
AM
13212 || (htab->glink_eh_frame != NULL
13213 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 13214 {
b34976b6 13215 htab->stub_error = TRUE;
8de848d8 13216 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 13217 }
721956f4 13218
d2a300cf
AM
13219 if (htab->stub_error)
13220 return FALSE;
13221
13222 if (stats != NULL)
13223 {
13224 *stats = bfd_malloc (500);
13225 if (*stats == NULL)
13226 return FALSE;
13227
ee75fd95 13228 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
13229 " branch %lu\n"
13230 " toc adjust %lu\n"
13231 " long branch %lu\n"
13232 " long toc adj %lu\n"
794e51c0 13233 " plt call %lu\n"
7341d5e2
AM
13234 " plt call toc %lu\n"
13235 " global entry %lu"),
e717da7e 13236 stub_sec_count,
ee75fd95 13237 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
13238 htab->stub_count[ppc_stub_long_branch - 1],
13239 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13240 htab->stub_count[ppc_stub_plt_branch - 1],
13241 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13242 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13243 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13244 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13245 }
13246 return TRUE;
5bd4f169
AM
13247}
13248
99877b66
AM
13249/* This function undoes the changes made by add_symbol_adjust. */
13250
13251static bfd_boolean
13252undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
13253{
13254 struct ppc_link_hash_entry *eh;
13255
13256 if (h->root.type == bfd_link_hash_indirect)
13257 return TRUE;
13258
99877b66
AM
13259 eh = (struct ppc_link_hash_entry *) h;
13260 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
13261 return TRUE;
13262
13263 eh->elf.root.type = bfd_link_hash_undefined;
13264 return TRUE;
13265}
13266
13267void
13268ppc64_elf_restore_symbols (struct bfd_link_info *info)
13269{
13270 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
13271
13272 if (htab != NULL)
13273 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
13274}
13275
60124e18
AM
13276/* What to do when ld finds relocations against symbols defined in
13277 discarded sections. */
13278
13279static unsigned int
13280ppc64_elf_action_discarded (asection *sec)
13281{
13282 if (strcmp (".opd", sec->name) == 0)
13283 return 0;
13284
13285 if (strcmp (".toc", sec->name) == 0)
13286 return 0;
13287
bce50a28
JJ
13288 if (strcmp (".toc1", sec->name) == 0)
13289 return 0;
13290
60124e18
AM
13291 return _bfd_elf_default_action_discarded (sec);
13292}
13293
5bd4f169
AM
13294/* The RELOCATE_SECTION function is called by the ELF backend linker
13295 to handle the relocations for a section.
13296
13297 The relocs are always passed as Rela structures; if the section
13298 actually uses Rel structures, the r_addend field will always be
13299 zero.
13300
13301 This function is responsible for adjust the section contents as
13302 necessary, and (if using Rela relocs and generating a
1049f94e 13303 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13304 necessary.
13305
13306 This function does not have to worry about setting the reloc
13307 address or the reloc symbol index.
13308
13309 LOCAL_SYMS is a pointer to the swapped in local symbols.
13310
13311 LOCAL_SECTIONS is an array giving the section in the input file
13312 corresponding to the st_shndx field of each local symbol.
13313
13314 The global hash table entry for the global symbols can be found
13315 via elf_sym_hashes (input_bfd).
13316
1049f94e 13317 When generating relocatable output, this function must handle
5bd4f169
AM
13318 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13319 going to be the section symbol corresponding to the output
13320 section, which means that the addend must be adjusted
13321 accordingly. */
13322
b34976b6 13323static bfd_boolean
4ce794b7
AM
13324ppc64_elf_relocate_section (bfd *output_bfd,
13325 struct bfd_link_info *info,
13326 bfd *input_bfd,
13327 asection *input_section,
13328 bfd_byte *contents,
13329 Elf_Internal_Rela *relocs,
13330 Elf_Internal_Sym *local_syms,
13331 asection **local_sections)
5bd4f169 13332{
65f38f15 13333 struct ppc_link_hash_table *htab;
5bd4f169
AM
13334 Elf_Internal_Shdr *symtab_hdr;
13335 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13336 Elf_Internal_Rela *rel;
c316a17c 13337 Elf_Internal_Rela *wrel;
5bd4f169 13338 Elf_Internal_Rela *relend;
411e1bfb
AM
13339 Elf_Internal_Rela outrel;
13340 bfd_byte *loc;
411e1bfb 13341 struct got_entry **local_got_ents;
5bd4f169 13342 bfd_vma TOCstart;
b34976b6
AM
13343 bfd_boolean ret = TRUE;
13344 bfd_boolean is_opd;
794e51c0
AM
13345 /* Assume 'at' branch hints. */
13346 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13347 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13348
65f38f15 13349 /* Initialize howto table if needed. */
5bd4f169 13350 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13351 ppc_howto_init ();
13352
65f38f15 13353 htab = ppc_hash_table (info);
4dfe6ac6
NC
13354 if (htab == NULL)
13355 return FALSE;
ee75fd95
AM
13356
13357 /* Don't relocate stub sections. */
e7d1c40c 13358 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13359 return TRUE;
13360
0c8d6e5c 13361 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13362
411e1bfb 13363 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13364 TOCstart = elf_gp (output_bfd);
0ffa91dd 13365 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13366 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13367 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13368
c316a17c 13369 rel = wrel = relocs;
5bd4f169 13370 relend = relocs + input_section->reloc_count;
c316a17c 13371 for (; rel < relend; wrel++, rel++)
5bd4f169 13372 {
04c9666a 13373 enum elf_ppc64_reloc_type r_type;
31c76678 13374 bfd_vma addend;
5bd4f169
AM
13375 bfd_reloc_status_type r;
13376 Elf_Internal_Sym *sym;
13377 asection *sec;
039b3fef
AM
13378 struct elf_link_hash_entry *h_elf;
13379 struct ppc_link_hash_entry *h;
13380 struct ppc_link_hash_entry *fdh;
5bd4f169 13381 const char *sym_name;
0d4792f7 13382 unsigned long r_symndx, toc_symndx;
3a71aa26 13383 bfd_vma toc_addend;
f961d9dd
AM
13384 unsigned char tls_mask, tls_gd, tls_type;
13385 unsigned char sym_type;
5bd4f169 13386 bfd_vma relocation;
b34976b6
AM
13387 bfd_boolean unresolved_reloc;
13388 bfd_boolean warned;
bc30df16 13389 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13390 unsigned int insn;
e11840f9 13391 unsigned int mask;
721956f4
AM
13392 struct ppc_stub_hash_entry *stub_entry;
13393 bfd_vma max_br_offset;
13394 bfd_vma from;
c316a17c 13395 Elf_Internal_Rela orig_rel;
b80eed39
AM
13396 reloc_howto_type *howto;
13397 struct reloc_howto_struct alt_howto;
5bd4f169 13398
c316a17c
AM
13399 again:
13400 orig_rel = *rel;
13401
4ce794b7 13402 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13403 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13404
13405 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13406 symbol of the previous ADDR64 reloc. The symbol gives us the
13407 proper TOC base to use. */
13408 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13409 && wrel != relocs
13410 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13411 && is_opd)
c316a17c 13412 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13413
4ce794b7
AM
13414 sym = NULL;
13415 sec = NULL;
039b3fef 13416 h_elf = NULL;
4ce794b7 13417 sym_name = NULL;
b34976b6
AM
13418 unresolved_reloc = FALSE;
13419 warned = FALSE;
65f38f15 13420
0b13192e 13421 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13422 {
13423 /* It's a local symbol. */
74f0fb50 13424 struct _opd_sec_data *opd;
4025353c 13425
5bd4f169
AM
13426 sym = local_syms + r_symndx;
13427 sec = local_sections[r_symndx];
26c61ae5 13428 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13429 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13430 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13431 opd = get_opd_info (sec);
13432 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13433 {
51aecdc5
AM
13434 long adjust = opd->adjust[OPD_NDX (sym->st_value
13435 + rel->r_addend)];
4025353c
AM
13436 if (adjust == -1)
13437 relocation = 0;
13438 else
4cc603a5
AM
13439 {
13440 /* If this is a relocation against the opd section sym
13441 and we have edited .opd, adjust the reloc addend so
13442 that ld -r and ld --emit-relocs output is correct.
13443 If it is a reloc against some other .opd symbol,
13444 then the symbol value will be adjusted later. */
13445 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13446 rel->r_addend += adjust;
13447 else
13448 relocation += adjust;
13449 }
1e2f5b6e 13450 }
5bd4f169
AM
13451 }
13452 else
13453 {
62d887d4
L
13454 bfd_boolean ignored;
13455
b2a8e766
AM
13456 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13457 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13458 h_elf, sec, relocation,
62d887d4 13459 unresolved_reloc, warned, ignored);
039b3fef
AM
13460 sym_name = h_elf->root.root.string;
13461 sym_type = h_elf->type;
b69fdb4e
AM
13462 if (sec != NULL
13463 && sec->owner == output_bfd
13464 && strcmp (sec->name, ".opd") == 0)
13465 {
13466 /* This is a symbol defined in a linker script. All
13467 such are defined in output sections, even those
13468 defined by simple assignment from a symbol defined in
13469 an input section. Transfer the symbol to an
13470 appropriate input .opd section, so that a branch to
13471 this symbol will be mapped to the location specified
13472 by the opd entry. */
13473 struct bfd_link_order *lo;
13474 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13475 if (lo->type == bfd_indirect_link_order)
13476 {
13477 asection *isec = lo->u.indirect.section;
13478 if (h_elf->root.u.def.value >= isec->output_offset
13479 && h_elf->root.u.def.value < (isec->output_offset
13480 + isec->size))
13481 {
13482 h_elf->root.u.def.value -= isec->output_offset;
13483 h_elf->root.u.def.section = isec;
13484 sec = isec;
13485 break;
13486 }
13487 }
13488 }
5bd4f169 13489 }
039b3fef 13490 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13491
dbaa2011 13492 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13493 {
13494 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13495 input_bfd, input_section,
13496 contents + rel->r_offset);
13497 wrel->r_offset = rel->r_offset;
13498 wrel->r_info = 0;
13499 wrel->r_addend = 0;
13500
13501 /* For ld -r, remove relocations in debug sections against
13502 sections defined in discarded sections. Not done for
13503 non-debug to preserve relocs in .eh_frame which the
13504 eh_frame editing code expects to be present. */
13505 if (bfd_link_relocatable (info)
13506 && (input_section->flags & SEC_DEBUGGING))
13507 wrel--;
13508
13509 continue;
13510 }
ab96bf03 13511
0e1862bb 13512 if (bfd_link_relocatable (info))
c316a17c 13513 goto copy_reloc;
ab96bf03 13514
f40da81b
AM
13515 if (h != NULL && &h->elf == htab->elf.hgot)
13516 {
6f20ed8a 13517 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13518 sec = bfd_abs_section_ptr;
13519 unresolved_reloc = FALSE;
13520 }
13521
951fd09b
AM
13522 /* TLS optimizations. Replace instruction sequences and relocs
13523 based on information we collected in tls_optimize. We edit
13524 RELOCS so that --emit-relocs will output something sensible
13525 for the final instruction stream. */
13526 tls_mask = 0;
13527 tls_gd = 0;
0d4792f7 13528 toc_symndx = 0;
727fc41e
AM
13529 if (h != NULL)
13530 tls_mask = h->tls_mask;
13531 else if (local_got_ents != NULL)
411e1bfb 13532 {
e054468f
AM
13533 struct plt_entry **local_plt = (struct plt_entry **)
13534 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13535 unsigned char *lgot_masks = (unsigned char *)
e054468f 13536 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13537 tls_mask = lgot_masks[r_symndx];
13538 }
13539 if (tls_mask == 0
13540 && (r_type == R_PPC64_TLS
13541 || r_type == R_PPC64_TLSGD
13542 || r_type == R_PPC64_TLSLD))
13543 {
13544 /* Check for toc tls entries. */
f961d9dd 13545 unsigned char *toc_tls;
0d4792f7 13546
727fc41e
AM
13547 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13548 &local_syms, rel, input_bfd))
13549 return FALSE;
0d4792f7 13550
727fc41e
AM
13551 if (toc_tls)
13552 tls_mask = *toc_tls;
0d4792f7
AM
13553 }
13554
13555 /* Check that tls relocs are used with tls syms, and non-tls
13556 relocs are used with non-tls syms. */
cf35638d 13557 if (r_symndx != STN_UNDEF
0d4792f7
AM
13558 && r_type != R_PPC64_NONE
13559 && (h == NULL
039b3fef
AM
13560 || h->elf.root.type == bfd_link_hash_defined
13561 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13562 && (IS_PPC64_TLS_RELOC (r_type)
13563 != (sym_type == STT_TLS
13564 || (sym_type == STT_SECTION
13565 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13566 {
727fc41e
AM
13567 if (tls_mask != 0
13568 && (r_type == R_PPC64_TLS
13569 || r_type == R_PPC64_TLSGD
13570 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13571 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13572 ;
13573 else
25f53a85 13574 info->callbacks->einfo
1d483afe 13575 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 13576 /* xgettext:c-format */
bc30df16 13577 ? _("%P: %H: %s used with TLS symbol `%T'\n")
695344c0 13578 /* xgettext:c-format */
bc30df16 13579 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
25f53a85 13580 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13581 ppc64_elf_howto_table[r_type]->name,
13582 sym_name);
411e1bfb
AM
13583 }
13584
13585 /* Ensure reloc mapping code below stays sane. */
13586 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13587 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13588 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13589 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13590 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13591 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13592 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13593 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13594 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13595 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13596 abort ();
0d4792f7 13597
411e1bfb
AM
13598 switch (r_type)
13599 {
13600 default:
411e1bfb
AM
13601 break;
13602
ba761f19 13603 case R_PPC64_LO_DS_OPT:
95f0d0d2 13604 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
13605 if ((insn & (0x3f << 26)) != 58u << 26)
13606 abort ();
13607 insn += (14u << 26) - (58u << 26);
95f0d0d2 13608 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
13609 r_type = R_PPC64_TOC16_LO;
13610 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13611 break;
13612
411e1bfb
AM
13613 case R_PPC64_TOC16:
13614 case R_PPC64_TOC16_LO:
13615 case R_PPC64_TOC16_DS:
13616 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13617 {
13618 /* Check for toc tls entries. */
f961d9dd 13619 unsigned char *toc_tls;
951fd09b 13620 int retval;
411e1bfb 13621
3a71aa26
AM
13622 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13623 &local_syms, rel, input_bfd);
951fd09b 13624 if (retval == 0)
411e1bfb
AM
13625 return FALSE;
13626
13627 if (toc_tls)
13628 {
951fd09b 13629 tls_mask = *toc_tls;
411e1bfb
AM
13630 if (r_type == R_PPC64_TOC16_DS
13631 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
13632 {
13633 if (tls_mask != 0
13634 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13635 goto toctprel;
13636 }
411e1bfb 13637 else
951fd09b
AM
13638 {
13639 /* If we found a GD reloc pair, then we might be
13640 doing a GD->IE transition. */
13641 if (retval == 2)
13642 {
13643 tls_gd = TLS_TPRELGD;
13644 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13645 goto tls_ldgd_opt;
951fd09b
AM
13646 }
13647 else if (retval == 3)
13648 {
13649 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 13650 goto tls_ldgd_opt;
951fd09b
AM
13651 }
13652 }
411e1bfb
AM
13653 }
13654 }
13655 break;
13656
9d6ded02
AM
13657 case R_PPC64_GOT_TPREL16_HI:
13658 case R_PPC64_GOT_TPREL16_HA:
13659 if (tls_mask != 0
13660 && (tls_mask & TLS_TPREL) == 0)
13661 {
13662 rel->r_offset -= d_offset;
95f0d0d2 13663 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
13664 r_type = R_PPC64_NONE;
13665 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13666 }
13667 break;
13668
411e1bfb
AM
13669 case R_PPC64_GOT_TPREL16_DS:
13670 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13671 if (tls_mask != 0
13672 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13673 {
81407a69 13674 toctprel:
95f0d0d2 13675 insn = bfd_get_32 (input_bfd,
c316a17c 13676 contents + rel->r_offset - d_offset);
411e1bfb
AM
13677 insn &= 31 << 21;
13678 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 13679 bfd_put_32 (input_bfd, insn,
c316a17c 13680 contents + rel->r_offset - d_offset);
411e1bfb 13681 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13682 if (toc_symndx != 0)
13683 {
13684 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13685 rel->r_addend = toc_addend;
0d4792f7
AM
13686 /* We changed the symbol. Start over in order to
13687 get h, sym, sec etc. right. */
c316a17c 13688 goto again;
0d4792f7
AM
13689 }
13690 else
13691 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13692 }
13693 break;
13694
13695 case R_PPC64_TLS:
951fd09b
AM
13696 if (tls_mask != 0
13697 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13698 {
95f0d0d2 13699 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
13700 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13701 if (insn == 0)
411e1bfb 13702 abort ();
95f0d0d2 13703 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 13704 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13705 PPC64_TPREL16_LO which is at low-order half-word. */
13706 rel->r_offset += d_offset;
0d4792f7
AM
13707 r_type = R_PPC64_TPREL16_LO;
13708 if (toc_symndx != 0)
13709 {
13710 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13711 rel->r_addend = toc_addend;
0d4792f7
AM
13712 /* We changed the symbol. Start over in order to
13713 get h, sym, sec etc. right. */
c316a17c 13714 goto again;
0d4792f7
AM
13715 }
13716 else
13717 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13718 }
13719 break;
13720
411e1bfb
AM
13721 case R_PPC64_GOT_TLSGD16_HI:
13722 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13723 tls_gd = TLS_TPRELGD;
13724 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13725 goto tls_gdld_hi;
13726 break;
13727
411e1bfb
AM
13728 case R_PPC64_GOT_TLSLD16_HI:
13729 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13730 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13731 {
951fd09b
AM
13732 tls_gdld_hi:
13733 if ((tls_mask & tls_gd) != 0)
13734 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13735 + R_PPC64_GOT_TPREL16_DS);
13736 else
411e1bfb 13737 {
4fe5ca5b 13738 rel->r_offset -= d_offset;
95f0d0d2 13739 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 13740 r_type = R_PPC64_NONE;
411e1bfb 13741 }
951fd09b 13742 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13743 }
13744 break;
13745
951fd09b
AM
13746 case R_PPC64_GOT_TLSGD16:
13747 case R_PPC64_GOT_TLSGD16_LO:
13748 tls_gd = TLS_TPRELGD;
13749 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13750 goto tls_ldgd_opt;
951fd09b 13751 break;
411e1bfb 13752
951fd09b
AM
13753 case R_PPC64_GOT_TLSLD16:
13754 case R_PPC64_GOT_TLSLD16_LO:
13755 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13756 {
3a71aa26 13757 unsigned int insn1, insn2, insn3;
102890f0
AM
13758 bfd_vma offset;
13759
13760 tls_ldgd_opt:
727fc41e
AM
13761 offset = (bfd_vma) -1;
13762 /* If not using the newer R_PPC64_TLSGD/LD to mark
13763 __tls_get_addr calls, we must trust that the call
13764 stays with its arg setup insns, ie. that the next
13765 reloc is the __tls_get_addr call associated with
13766 the current reloc. Edit both insns. */
13767 if (input_section->has_tls_get_addr_call
13768 && rel + 1 < relend
13769 && branch_reloc_hash_match (input_bfd, rel + 1,
13770 htab->tls_get_addr,
13771 htab->tls_get_addr_fd))
13772 offset = rel[1].r_offset;
b86ac8e3
AM
13773 /* We read the low GOT_TLS (or TOC16) insn because we
13774 need to keep the destination reg. It may be
13775 something other than the usual r3, and moved to r3
13776 before the call by intervening code. */
95f0d0d2 13777 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 13778 contents + rel->r_offset - d_offset);
102890f0 13779 if ((tls_mask & tls_gd) != 0)
411e1bfb 13780 {
102890f0 13781 /* IE */
b86ac8e3 13782 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13783 insn1 |= 58 << 26; /* ld */
13784 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13785 if (offset != (bfd_vma) -1)
f58d5a2d 13786 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13787 if ((tls_mask & TLS_EXPLICIT) == 0)
13788 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13789 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13790 else
102890f0
AM
13791 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13792 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13793 }
13794 else
13795 {
13796 /* LE */
b86ac8e3
AM
13797 insn1 &= 0x1f << 21;
13798 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13799 insn2 = 0x38630000; /* addi 3,3,0 */
13800 if (tls_gd == 0)
951fd09b 13801 {
102890f0 13802 /* Was an LD reloc. */
1d483afe
AM
13803 if (toc_symndx)
13804 sec = local_sections[toc_symndx];
13805 for (r_symndx = 0;
13806 r_symndx < symtab_hdr->sh_info;
13807 r_symndx++)
13808 if (local_sections[r_symndx] == sec)
13809 break;
13810 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13811 r_symndx = STN_UNDEF;
102890f0 13812 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13813 if (r_symndx != STN_UNDEF)
1d483afe
AM
13814 rel->r_addend -= (local_syms[r_symndx].st_value
13815 + sec->output_offset
13816 + sec->output_section->vma);
951fd09b 13817 }
102890f0 13818 else if (toc_symndx != 0)
3a71aa26
AM
13819 {
13820 r_symndx = toc_symndx;
13821 rel->r_addend = toc_addend;
13822 }
102890f0
AM
13823 r_type = R_PPC64_TPREL16_HA;
13824 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13825 if (offset != (bfd_vma) -1)
13826 {
13827 rel[1].r_info = ELF64_R_INFO (r_symndx,
13828 R_PPC64_TPREL16_LO);
13829 rel[1].r_offset = offset + d_offset;
13830 rel[1].r_addend = rel->r_addend;
13831 }
102890f0 13832 }
95f0d0d2 13833 bfd_put_32 (input_bfd, insn1,
3a71aa26 13834 contents + rel->r_offset - d_offset);
727fc41e
AM
13835 if (offset != (bfd_vma) -1)
13836 {
95f0d0d2 13837 insn3 = bfd_get_32 (input_bfd,
727fc41e
AM
13838 contents + offset + 4);
13839 if (insn3 == NOP
13840 || insn3 == CROR_151515 || insn3 == CROR_313131)
13841 {
13842 rel[1].r_offset += 4;
95f0d0d2 13843 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
727fc41e
AM
13844 insn2 = NOP;
13845 }
95f0d0d2 13846 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
13847 }
13848 if ((tls_mask & tls_gd) == 0
13849 && (tls_gd == 0 || toc_symndx != 0))
13850 {
13851 /* We changed the symbol. Start over in order
13852 to get h, sym, sec etc. right. */
c316a17c 13853 goto again;
727fc41e
AM
13854 }
13855 }
13856 break;
13857
13858 case R_PPC64_TLSGD:
13859 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13860 {
13861 unsigned int insn2, insn3;
13862 bfd_vma offset = rel->r_offset;
13863
13864 if ((tls_mask & TLS_TPRELGD) != 0)
13865 {
13866 /* IE */
13867 r_type = R_PPC64_NONE;
13868 insn2 = 0x7c636a14; /* add 3,3,13 */
13869 }
13870 else
13871 {
13872 /* LE */
13873 if (toc_symndx != 0)
13874 {
13875 r_symndx = toc_symndx;
13876 rel->r_addend = toc_addend;
13877 }
13878 r_type = R_PPC64_TPREL16_LO;
13879 rel->r_offset = offset + d_offset;
13880 insn2 = 0x38630000; /* addi 3,3,0 */
13881 }
13882 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13883 /* Zap the reloc on the _tls_get_addr call too. */
13884 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13885 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 13886 insn3 = bfd_get_32 (input_bfd,
3a71aa26 13887 contents + offset + 4);
102890f0
AM
13888 if (insn3 == NOP
13889 || insn3 == CROR_151515 || insn3 == CROR_313131)
13890 {
727fc41e 13891 rel->r_offset += 4;
95f0d0d2 13892 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
3a71aa26 13893 insn2 = NOP;
102890f0 13894 }
95f0d0d2 13895 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 13896 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13897 goto again;
411e1bfb 13898 }
411e1bfb
AM
13899 break;
13900
727fc41e
AM
13901 case R_PPC64_TLSLD:
13902 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13903 {
13904 unsigned int insn2, insn3;
13905 bfd_vma offset = rel->r_offset;
13906
13907 if (toc_symndx)
13908 sec = local_sections[toc_symndx];
13909 for (r_symndx = 0;
13910 r_symndx < symtab_hdr->sh_info;
13911 r_symndx++)
13912 if (local_sections[r_symndx] == sec)
13913 break;
13914 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13915 r_symndx = STN_UNDEF;
727fc41e 13916 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13917 if (r_symndx != STN_UNDEF)
727fc41e
AM
13918 rel->r_addend -= (local_syms[r_symndx].st_value
13919 + sec->output_offset
13920 + sec->output_section->vma);
13921
13922 r_type = R_PPC64_TPREL16_LO;
13923 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13924 rel->r_offset = offset + d_offset;
13925 /* Zap the reloc on the _tls_get_addr call too. */
13926 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13927 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 13928 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 13929 insn3 = bfd_get_32 (input_bfd,
727fc41e
AM
13930 contents + offset + 4);
13931 if (insn3 == NOP
13932 || insn3 == CROR_151515 || insn3 == CROR_313131)
13933 {
13934 rel->r_offset += 4;
95f0d0d2 13935 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
727fc41e
AM
13936 insn2 = NOP;
13937 }
95f0d0d2 13938 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 13939 goto again;
727fc41e
AM
13940 }
13941 break;
13942
411e1bfb 13943 case R_PPC64_DTPMOD64:
951fd09b
AM
13944 if (rel + 1 < relend
13945 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13946 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13947 {
951fd09b
AM
13948 if ((tls_mask & TLS_GD) == 0)
13949 {
13950 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13951 if ((tls_mask & TLS_TPRELGD) != 0)
13952 r_type = R_PPC64_TPREL64;
13953 else
13954 {
4ce794b7 13955 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13956 r_type = R_PPC64_NONE;
13957 }
13958 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13959 }
13960 }
13961 else
13962 {
13963 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13964 {
4ce794b7 13965 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13966 r_type = R_PPC64_NONE;
951fd09b 13967 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13968 }
411e1bfb
AM
13969 }
13970 break;
13971
13972 case R_PPC64_TPREL64:
951fd09b 13973 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13974 {
13975 r_type = R_PPC64_NONE;
13976 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13977 }
13978 break;
52a82034 13979
006589cf
AM
13980 case R_PPC64_ENTRY:
13981 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13982 if (!bfd_link_pic (info)
13983 && !info->traditional_format
13984 && relocation + 0x80008000 <= 0xffffffff)
13985 {
13986 unsigned int insn1, insn2;
13987
13988 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13989 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13990 if ((insn1 & ~0xfffc) == LD_R2_0R12
13991 && insn2 == ADD_R2_R2_R12)
13992 {
95f0d0d2 13993 bfd_put_32 (input_bfd,
006589cf
AM
13994 LIS_R2 + PPC_HA (relocation),
13995 contents + rel->r_offset);
95f0d0d2 13996 bfd_put_32 (input_bfd,
006589cf
AM
13997 ADDI_R2_R2 + PPC_LO (relocation),
13998 contents + rel->r_offset + 4);
13999 }
14000 }
14001 else
14002 {
14003 relocation -= (rel->r_offset
14004 + input_section->output_offset
14005 + input_section->output_section->vma);
14006 if (relocation + 0x80008000 <= 0xffffffff)
14007 {
14008 unsigned int insn1, insn2;
14009
14010 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14011 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14012 if ((insn1 & ~0xfffc) == LD_R2_0R12
14013 && insn2 == ADD_R2_R2_R12)
14014 {
95f0d0d2 14015 bfd_put_32 (input_bfd,
006589cf
AM
14016 ADDIS_R2_R12 + PPC_HA (relocation),
14017 contents + rel->r_offset);
95f0d0d2 14018 bfd_put_32 (input_bfd,
006589cf
AM
14019 ADDI_R2_R2 + PPC_LO (relocation),
14020 contents + rel->r_offset + 4);
14021 }
14022 }
14023 }
14024 break;
14025
52a82034
AM
14026 case R_PPC64_REL16_HA:
14027 /* If we are generating a non-PIC executable, edit
14028 . 0: addis 2,12,.TOC.-0b@ha
14029 . addi 2,2,.TOC.-0b@l
14030 used by ELFv2 global entry points to set up r2, to
14031 . lis 2,.TOC.@ha
14032 . addi 2,2,.TOC.@l
14033 if .TOC. is in range. */
0e1862bb 14034 if (!bfd_link_pic (info)
810d4e75 14035 && !info->traditional_format
006589cf 14036 && !htab->opd_abi
4f038ee5 14037 && rel->r_addend == d_offset
52a82034
AM
14038 && h != NULL && &h->elf == htab->elf.hgot
14039 && rel + 1 < relend
14040 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14041 && rel[1].r_offset == rel->r_offset + 4
14042 && rel[1].r_addend == rel->r_addend + 4
14043 && relocation + 0x80008000 <= 0xffffffff)
14044 {
14045 unsigned int insn1, insn2;
14046 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14047 insn1 = bfd_get_32 (input_bfd, contents + offset);
14048 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14049 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14050 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14051 {
14052 r_type = R_PPC64_ADDR16_HA;
14053 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14054 rel->r_addend -= d_offset;
14055 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14056 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14057 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14058 }
14059 }
14060 break;
411e1bfb
AM
14061 }
14062
14063 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14064 insn = 0;
b25116a9
AM
14065 max_br_offset = 1 << 25;
14066 addend = rel->r_addend;
bc30df16 14067 reloc_dest = DEST_NORMAL;
65f38f15 14068 switch (r_type)
5bd4f169
AM
14069 {
14070 default:
65f38f15 14071 break;
5bd4f169 14072
3b421ab3
AM
14073 case R_PPC64_TOCSAVE:
14074 if (relocation + addend == (rel->r_offset
14075 + input_section->output_offset
14076 + input_section->output_section->vma)
14077 && tocsave_find (htab, NO_INSERT,
14078 &local_syms, rel, input_bfd))
14079 {
14080 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14081 if (insn == NOP
14082 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14083 bfd_put_32 (input_bfd,
14084 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14085 contents + rel->r_offset);
14086 }
14087 break;
14088
65f38f15
AM
14089 /* Branch taken prediction relocations. */
14090 case R_PPC64_ADDR14_BRTAKEN:
14091 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14092 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14093 /* Fall through. */
65f38f15 14094
86c76c7b 14095 /* Branch not taken prediction relocations. */
65f38f15
AM
14096 case R_PPC64_ADDR14_BRNTAKEN:
14097 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14098 insn |= bfd_get_32 (input_bfd,
411e1bfb 14099 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14100 /* Fall through. */
86c76c7b 14101
b25116a9
AM
14102 case R_PPC64_REL14:
14103 max_br_offset = 1 << 15;
1a0670f3 14104 /* Fall through. */
5bd4f169 14105
65f38f15 14106 case R_PPC64_REL24:
ad8e1ba5
AM
14107 /* Calls to functions with a different TOC, such as calls to
14108 shared objects, need to alter the TOC pointer. This is
14109 done using a linkage stub. A REL24 branching to these
14110 linkage stubs needs to be followed by a nop, as the nop
14111 will be replaced with an instruction to restore the TOC
14112 base pointer. */
8387904d 14113 fdh = h;
b31867b6
AM
14114 if (h != NULL
14115 && h->oh != NULL
14116 && h->oh->is_func_descriptor)
14117 fdh = ppc_follow_link (h->oh);
31c76678
DK
14118 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14119 htab);
6abec6bc 14120 if (stub_entry != NULL
ad8e1ba5 14121 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 14122 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
14123 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14124 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 14125 {
b25116a9 14126 bfd_boolean can_plt_call = FALSE;
721956f4 14127
ba8ca3e7
AM
14128 /* All of these stubs will modify r2, so there must be a
14129 branch and link followed by a nop. The nop is
14130 replaced by an insn to restore r2. */
eea6121a 14131 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14132 {
ba8ca3e7
AM
14133 unsigned long br;
14134
14135 br = bfd_get_32 (input_bfd,
14136 contents + rel->r_offset);
14137 if ((br & 1) != 0)
41bd81ab 14138 {
ba8ca3e7
AM
14139 unsigned long nop;
14140
14141 nop = bfd_get_32 (input_bfd,
14142 contents + rel->r_offset + 4);
14143 if (nop == NOP
14144 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 14145 {
ba8ca3e7
AM
14146 if (h != NULL
14147 && (h == htab->tls_get_addr_fd
14148 || h == htab->tls_get_addr)
7c9cf415 14149 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14150 {
14151 /* Special stub used, leave nop alone. */
14152 }
14153 else
a078d95a
AM
14154 bfd_put_32 (input_bfd,
14155 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14156 contents + rel->r_offset + 4);
14157 can_plt_call = TRUE;
a7f2871e 14158 }
41bd81ab 14159 }
5bd4f169 14160 }
721956f4 14161
ba8ca3e7 14162 if (!can_plt_call && h != NULL)
721956f4 14163 {
ba8ca3e7
AM
14164 const char *name = h->elf.root.root.string;
14165
14166 if (*name == '.')
14167 ++name;
14168
14169 if (strncmp (name, "__libc_start_main", 17) == 0
14170 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14171 {
ba8ca3e7
AM
14172 /* Allow crt1 branch to go via a toc adjusting
14173 stub. Other calls that never return could do
14174 the same, if we could detect such. */
b25116a9 14175 can_plt_call = TRUE;
6ab189d5 14176 }
ba8ca3e7
AM
14177 }
14178
14179 if (!can_plt_call)
14180 {
14181 /* g++ as of 20130507 emits self-calls without a
14182 following nop. This is arguably wrong since we
14183 have conflicting information. On the one hand a
14184 global symbol and on the other a local call
14185 sequence, but don't error for this special case.
14186 It isn't possible to cheaply verify we have
14187 exactly such a call. Allow all calls to the same
14188 section. */
14189 asection *code_sec = sec;
14190
14191 if (get_opd_info (sec) != NULL)
ad8e1ba5 14192 {
ba8ca3e7
AM
14193 bfd_vma off = (relocation + addend
14194 - sec->output_section->vma
14195 - sec->output_offset);
bc30df16 14196
ba8ca3e7 14197 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14198 }
ba8ca3e7
AM
14199 if (code_sec == input_section)
14200 can_plt_call = TRUE;
14201 }
14202
14203 if (!can_plt_call)
14204 {
4805fc55
AM
14205 if (stub_entry->stub_type == ppc_stub_plt_call
14206 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14207 info->callbacks->einfo
695344c0 14208 /* xgettext:c-format */
4805fc55
AM
14209 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14210 "recompile with -fPIC\n"),
14211 input_bfd, input_section, rel->r_offset, sym_name);
14212 else
14213 info->callbacks->einfo
695344c0 14214 /* xgettext:c-format */
4805fc55
AM
14215 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14216 "(-mcmodel=small toc adjust stub)\n"),
14217 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14218
14219 bfd_set_error (bfd_error_bad_value);
14220 ret = FALSE;
721956f4
AM
14221 }
14222
b25116a9 14223 if (can_plt_call
794e51c0
AM
14224 && (stub_entry->stub_type == ppc_stub_plt_call
14225 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14226 unresolved_reloc = FALSE;
14227 }
14228
6abec6bc
AM
14229 if ((stub_entry == NULL
14230 || stub_entry->stub_type == ppc_stub_long_branch
14231 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14232 && get_opd_info (sec) != NULL)
14233 {
14234 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14235 bfd_vma off = (relocation + addend
14236 - sec->output_section->vma
14237 - sec->output_offset);
aef36ac1 14238 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14239 if (dest != (bfd_vma) -1)
14240 {
14241 relocation = dest;
14242 addend = 0;
bc30df16 14243 reloc_dest = DEST_OPD;
8387904d
AM
14244 }
14245 }
14246
b25116a9
AM
14247 /* If the branch is out of reach we ought to have a long
14248 branch stub. */
14249 from = (rel->r_offset
14250 + input_section->output_offset
14251 + input_section->output_section->vma);
14252
6911b7dc
AM
14253 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14254 ? fdh->elf.other
14255 : sym->st_other);
14256
6abec6bc
AM
14257 if (stub_entry != NULL
14258 && (stub_entry->stub_type == ppc_stub_long_branch
14259 || stub_entry->stub_type == ppc_stub_plt_branch)
14260 && (r_type == R_PPC64_ADDR14_BRTAKEN
14261 || r_type == R_PPC64_ADDR14_BRNTAKEN
14262 || (relocation + addend - from + max_br_offset
14263 < 2 * max_br_offset)))
14264 /* Don't use the stub if this branch is in range. */
14265 stub_entry = NULL;
b25116a9
AM
14266
14267 if (stub_entry != NULL)
14268 {
14269 /* Munge up the value and addend so that we call the stub
14270 rather than the procedure directly. */
a4b6fadd
AM
14271 asection *stub_sec = stub_entry->group->stub_sec;
14272
14273 if (stub_entry->stub_type == ppc_stub_save_res)
14274 relocation += (stub_sec->output_offset
14275 + stub_sec->output_section->vma
14276 + stub_sec->size - htab->sfpr->size
14277 - htab->sfpr->output_offset
14278 - htab->sfpr->output_section->vma);
14279 else
14280 relocation = (stub_entry->stub_offset
14281 + stub_sec->output_offset
14282 + stub_sec->output_section->vma);
b25116a9 14283 addend = 0;
bc30df16 14284 reloc_dest = DEST_STUB;
3b421ab3 14285
794e51c0
AM
14286 if ((stub_entry->stub_type == ppc_stub_plt_call
14287 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14288 && (ALWAYS_EMIT_R2SAVE
14289 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14290 && rel + 1 < relend
14291 && rel[1].r_offset == rel->r_offset + 4
14292 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14293 relocation += 4;
b25116a9
AM
14294 }
14295
14296 if (insn != 0)
14297 {
794e51c0 14298 if (is_isa_v2)
721956f4 14299 {
b25116a9
AM
14300 /* Set 'a' bit. This is 0b00010 in BO field for branch
14301 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14302 for branch on CTR insns (BO == 1a00t or 1a01t). */
14303 if ((insn & (0x14 << 21)) == (0x04 << 21))
14304 insn |= 0x02 << 21;
14305 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14306 insn |= 0x08 << 21;
14307 else
14308 break;
14309 }
14310 else
14311 {
14312 /* Invert 'y' bit if not the default. */
4cc603a5 14313 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14314 insn ^= 0x01 << 21;
721956f4 14315 }
b25116a9 14316
95f0d0d2 14317 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14318 }
e86ce104 14319
06da1e8e
AM
14320 /* NOP out calls to undefined weak functions.
14321 We can thus call a weak function without first
14322 checking whether the function is defined. */
b25116a9 14323 else if (h != NULL
039b3fef 14324 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14325 && h->elf.dynindx == -1
b25116a9
AM
14326 && r_type == R_PPC64_REL24
14327 && relocation == 0
4cc603a5 14328 && addend == 0)
e86ce104 14329 {
95f0d0d2 14330 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14331 goto copy_reloc;
e86ce104 14332 }
65f38f15
AM
14333 break;
14334 }
5bd4f169 14335
65f38f15 14336 /* Set `addend'. */
411e1bfb 14337 tls_type = 0;
65f38f15
AM
14338 switch (r_type)
14339 {
14340 default:
25f53a85 14341 info->callbacks->einfo
695344c0 14342 /* xgettext:c-format */
bc30df16 14343 (_("%P: %B: unknown relocation type %d for `%T'\n"),
d003868e 14344 input_bfd, (int) r_type, sym_name);
5bd4f169 14345
65f38f15 14346 bfd_set_error (bfd_error_bad_value);
b34976b6 14347 ret = FALSE;
c316a17c 14348 goto copy_reloc;
5bd4f169 14349
65f38f15 14350 case R_PPC64_NONE:
411e1bfb 14351 case R_PPC64_TLS:
727fc41e
AM
14352 case R_PPC64_TLSGD:
14353 case R_PPC64_TLSLD:
3b421ab3 14354 case R_PPC64_TOCSAVE:
04c9666a
AM
14355 case R_PPC64_GNU_VTINHERIT:
14356 case R_PPC64_GNU_VTENTRY:
006589cf 14357 case R_PPC64_ENTRY:
c316a17c 14358 goto copy_reloc;
5bd4f169
AM
14359
14360 /* GOT16 relocations. Like an ADDR16 using the symbol's
14361 address in the GOT as relocation value instead of the
411e1bfb 14362 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14363 symbol and put the symbol value there. */
411e1bfb
AM
14364 case R_PPC64_GOT_TLSGD16:
14365 case R_PPC64_GOT_TLSGD16_LO:
14366 case R_PPC64_GOT_TLSGD16_HI:
14367 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14368 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14369 goto dogot;
14370
14371 case R_PPC64_GOT_TLSLD16:
14372 case R_PPC64_GOT_TLSLD16_LO:
14373 case R_PPC64_GOT_TLSLD16_HI:
14374 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14375 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14376 goto dogot;
14377
14378 case R_PPC64_GOT_TPREL16_DS:
14379 case R_PPC64_GOT_TPREL16_LO_DS:
14380 case R_PPC64_GOT_TPREL16_HI:
14381 case R_PPC64_GOT_TPREL16_HA:
14382 tls_type = TLS_TLS | TLS_TPREL;
14383 goto dogot;
14384
14385 case R_PPC64_GOT_DTPREL16_DS:
14386 case R_PPC64_GOT_DTPREL16_LO_DS:
14387 case R_PPC64_GOT_DTPREL16_HI:
14388 case R_PPC64_GOT_DTPREL16_HA:
14389 tls_type = TLS_TLS | TLS_DTPREL;
14390 goto dogot;
14391
65f38f15
AM
14392 case R_PPC64_GOT16:
14393 case R_PPC64_GOT16_LO:
14394 case R_PPC64_GOT16_HI:
14395 case R_PPC64_GOT16_HA:
14396 case R_PPC64_GOT16_DS:
14397 case R_PPC64_GOT16_LO_DS:
411e1bfb 14398 dogot:
5bd4f169
AM
14399 {
14400 /* Relocation is to the entry for this symbol in the global
14401 offset table. */
e717da7e 14402 asection *got;
d881513a 14403 bfd_vma *offp;
5bd4f169 14404 bfd_vma off;
d881513a 14405 unsigned long indx = 0;
927be08e 14406 struct got_entry *ent;
65f38f15 14407
d881513a
AM
14408 if (tls_type == (TLS_TLS | TLS_LD)
14409 && (h == NULL
f5385ebf 14410 || !h->elf.def_dynamic))
927be08e 14411 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14412 else
5bd4f169 14413 {
5bd4f169 14414
d881513a
AM
14415 if (h != NULL)
14416 {
14417 bfd_boolean dyn = htab->elf.dynamic_sections_created;
0e1862bb 14418 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
039b3fef 14419 &h->elf)
0e1862bb 14420 || (bfd_link_pic (info)
afe397ea 14421 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
d881513a
AM
14422 /* This is actually a static link, or it is a
14423 -Bsymbolic link and the symbol is defined
14424 locally, or the symbol was forced to be local
14425 because of a version file. */
14426 ;
14427 else
14428 {
14acf4dc 14429 BFD_ASSERT (h->elf.dynindx != -1);
039b3fef 14430 indx = h->elf.dynindx;
d881513a
AM
14431 unresolved_reloc = FALSE;
14432 }
039b3fef 14433 ent = h->elf.got.glist;
d881513a 14434 }
411e1bfb 14435 else
5bd4f169 14436 {
d881513a
AM
14437 if (local_got_ents == NULL)
14438 abort ();
14439 ent = local_got_ents[r_symndx];
5bd4f169 14440 }
d881513a
AM
14441
14442 for (; ent != NULL; ent = ent->next)
31c76678 14443 if (ent->addend == orig_rel.r_addend
e717da7e 14444 && ent->owner == input_bfd
d881513a
AM
14445 && ent->tls_type == tls_type)
14446 break;
5bd4f169 14447 }
411e1bfb 14448
927be08e
AM
14449 if (ent == NULL)
14450 abort ();
14451 if (ent->is_indirect)
14452 ent = ent->got.ent;
14453 offp = &ent->got.offset;
14454 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14455 if (got == NULL)
14456 abort ();
14457
411e1bfb
AM
14458 /* The offset must always be a multiple of 8. We use the
14459 least significant bit to record whether we have already
14460 processed this entry. */
d881513a 14461 off = *offp;
411e1bfb
AM
14462 if ((off & 1) != 0)
14463 off &= ~1;
5bd4f169
AM
14464 else
14465 {
411e1bfb
AM
14466 /* Generate relocs for the dynamic linker, except in
14467 the case of TLSLD where we'll use one entry per
14468 module. */
25f23106
AM
14469 asection *relgot;
14470 bfd_boolean ifunc;
e717da7e 14471
d881513a 14472 *offp = off | 1;
25f23106
AM
14473 relgot = NULL;
14474 ifunc = (h != NULL
14475 ? h->elf.type == STT_GNU_IFUNC
14476 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14477 if (ifunc)
33e44f2e 14478 relgot = htab->elf.irelplt;
0e1862bb 14479 else if ((bfd_link_pic (info) || indx != 0)
19e08130
AM
14480 && (h == NULL
14481 || (tls_type == (TLS_TLS | TLS_LD)
14482 && !h->elf.def_dynamic)
14483 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14484 || h->elf.root.type != bfd_link_hash_undefweak))
14485 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14486 if (relgot != NULL)
5bd4f169 14487 {
e717da7e
AM
14488 outrel.r_offset = (got->output_section->vma
14489 + got->output_offset
411e1bfb 14490 + off);
4cc603a5 14491 outrel.r_addend = addend;
d881513a 14492 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14493 {
411e1bfb 14494 outrel.r_addend = 0;
e515b051 14495 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14496 if (tls_type == (TLS_TLS | TLS_GD))
14497 {
e717da7e
AM
14498 loc = relgot->contents;
14499 loc += (relgot->reloc_count++
d881513a
AM
14500 * sizeof (Elf64_External_Rela));
14501 bfd_elf64_swap_reloca_out (output_bfd,
14502 &outrel, loc);
e515b051 14503 outrel.r_offset += 8;
4cc603a5 14504 outrel.r_addend = addend;
d881513a
AM
14505 outrel.r_info
14506 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14507 }
411e1bfb 14508 }
951fd09b 14509 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14510 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14511 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14512 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14513 else if (indx != 0)
14514 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14515 else
81407a69 14516 {
25f23106
AM
14517 if (ifunc)
14518 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14519 else
14520 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14521
14522 /* Write the .got section contents for the sake
14523 of prelink. */
e717da7e 14524 loc = got->contents + off;
23fbd6fa
JJ
14525 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14526 loc);
81407a69 14527 }
81407a69
AM
14528
14529 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14530 {
14531 outrel.r_addend += relocation;
14532 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14533 {
14534 if (htab->elf.tls_sec == NULL)
14535 outrel.r_addend = 0;
14536 else
14537 outrel.r_addend -= htab->elf.tls_sec->vma;
14538 }
e515b051 14539 }
e717da7e
AM
14540 loc = relgot->contents;
14541 loc += (relgot->reloc_count++
411e1bfb
AM
14542 * sizeof (Elf64_External_Rela));
14543 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14544 }
14545
ad8e1ba5 14546 /* Init the .got section contents here if we're not
81407a69 14547 emitting a reloc. */
d881513a 14548 else
411e1bfb 14549 {
4cc603a5 14550 relocation += addend;
7b609f53
AM
14551 if (tls_type == (TLS_TLS | TLS_LD))
14552 relocation = 1;
14553 else if (tls_type != 0)
411e1bfb 14554 {
989f9879
AM
14555 if (htab->elf.tls_sec == NULL)
14556 relocation = 0;
14557 else
14558 {
14559 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14560 if (tls_type == (TLS_TLS | TLS_TPREL))
14561 relocation += DTP_OFFSET - TP_OFFSET;
14562 }
5bd4f169 14563
7b609f53
AM
14564 if (tls_type == (TLS_TLS | TLS_GD))
14565 {
14566 bfd_put_64 (output_bfd, relocation,
e717da7e 14567 got->contents + off + 8);
7b609f53
AM
14568 relocation = 1;
14569 }
411e1bfb 14570 }
7b609f53 14571
411e1bfb 14572 bfd_put_64 (output_bfd, relocation,
e717da7e 14573 got->contents + off);
5bd4f169
AM
14574 }
14575 }
14576
65f38f15
AM
14577 if (off >= (bfd_vma) -2)
14578 abort ();
14579
bf102f86 14580 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14581 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14582 }
65f38f15
AM
14583 break;
14584
14585 case R_PPC64_PLT16_HA:
14586 case R_PPC64_PLT16_HI:
14587 case R_PPC64_PLT16_LO:
14588 case R_PPC64_PLT32:
14589 case R_PPC64_PLT64:
14590 /* Relocation is to the entry for this symbol in the
14591 procedure linkage table. */
cbf95972
AM
14592 {
14593 struct plt_entry **plt_list = NULL;
14594 if (h != NULL)
14595 plt_list = &h->elf.plt.plist;
14596 else if (local_got_ents != NULL)
14597 {
14598 struct plt_entry **local_plt = (struct plt_entry **)
14599 (local_got_ents + symtab_hdr->sh_info);
14600 unsigned char *local_got_tls_masks = (unsigned char *)
14601 (local_plt + symtab_hdr->sh_info);
14602 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14603 plt_list = local_plt + r_symndx;
14604 }
14605 if (plt_list)
14606 {
14607 struct plt_entry *ent;
65f38f15 14608
cbf95972
AM
14609 for (ent = *plt_list; ent != NULL; ent = ent->next)
14610 if (ent->plt.offset != (bfd_vma) -1
14611 && ent->addend == orig_rel.r_addend)
14612 {
14613 asection *plt;
14614
14615 plt = htab->elf.splt;
14616 if (!htab->elf.dynamic_sections_created
14617 || h == NULL
14618 || h->elf.dynindx == -1)
14619 plt = htab->elf.iplt;
14620 relocation = (plt->output_section->vma
14621 + plt->output_offset
14622 + ent->plt.offset);
14623 addend = 0;
14624 unresolved_reloc = FALSE;
14625 break;
14626 }
14627 }
14628 }
65f38f15 14629 break;
5bd4f169 14630
0b13192e
AM
14631 case R_PPC64_TOC:
14632 /* Relocation value is TOC base. */
14633 relocation = TOCstart;
cf35638d 14634 if (r_symndx == STN_UNDEF)
6f20ed8a 14635 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14636 else if (unresolved_reloc)
14637 ;
6f20ed8a
AM
14638 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14639 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14640 else
14641 unresolved_reloc = TRUE;
ab96bf03 14642 goto dodyn;
0b13192e 14643
5bd4f169
AM
14644 /* TOC16 relocs. We want the offset relative to the TOC base,
14645 which is the address of the start of the TOC plus 0x8000.
14646 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14647 in this order. */
65f38f15
AM
14648 case R_PPC64_TOC16:
14649 case R_PPC64_TOC16_LO:
14650 case R_PPC64_TOC16_HI:
14651 case R_PPC64_TOC16_DS:
14652 case R_PPC64_TOC16_LO_DS:
14653 case R_PPC64_TOC16_HA:
6f20ed8a 14654 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14655 break;
14656
14657 /* Relocate against the beginning of the section. */
65f38f15
AM
14658 case R_PPC64_SECTOFF:
14659 case R_PPC64_SECTOFF_LO:
14660 case R_PPC64_SECTOFF_HI:
14661 case R_PPC64_SECTOFF_DS:
14662 case R_PPC64_SECTOFF_LO_DS:
14663 case R_PPC64_SECTOFF_HA:
4ce794b7 14664 if (sec != NULL)
65f38f15 14665 addend -= sec->output_section->vma;
5bd4f169
AM
14666 break;
14667
25f23106
AM
14668 case R_PPC64_REL16:
14669 case R_PPC64_REL16_LO:
14670 case R_PPC64_REL16_HI:
14671 case R_PPC64_REL16_HA:
a680de9a 14672 case R_PPC64_REL16DX_HA:
25f23106
AM
14673 break;
14674
721956f4
AM
14675 case R_PPC64_REL14:
14676 case R_PPC64_REL14_BRNTAKEN:
14677 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
14678 case R_PPC64_REL24:
14679 break;
14680
411e1bfb
AM
14681 case R_PPC64_TPREL16:
14682 case R_PPC64_TPREL16_LO:
14683 case R_PPC64_TPREL16_HI:
14684 case R_PPC64_TPREL16_HA:
14685 case R_PPC64_TPREL16_DS:
14686 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14687 case R_PPC64_TPREL16_HIGH:
14688 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14689 case R_PPC64_TPREL16_HIGHER:
14690 case R_PPC64_TPREL16_HIGHERA:
14691 case R_PPC64_TPREL16_HIGHEST:
14692 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14693 if (h != NULL
14694 && h->elf.root.type == bfd_link_hash_undefweak
14695 && h->elf.dynindx == -1)
14696 {
14697 /* Make this relocation against an undefined weak symbol
14698 resolve to zero. This is really just a tweak, since
14699 code using weak externs ought to check that they are
14700 defined before using them. */
14701 bfd_byte *p = contents + rel->r_offset - d_offset;
14702
95f0d0d2 14703 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
14704 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14705 if (insn != 0)
95f0d0d2 14706 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
14707 break;
14708 }
989f9879
AM
14709 if (htab->elf.tls_sec != NULL)
14710 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
0e1862bb 14711 if (bfd_link_pic (info))
411e1bfb
AM
14712 /* The TPREL16 relocs shouldn't really be used in shared
14713 libs as they will result in DT_TEXTREL being set, but
14714 support them anyway. */
14715 goto dodyn;
14716 break;
14717
14718 case R_PPC64_DTPREL16:
14719 case R_PPC64_DTPREL16_LO:
14720 case R_PPC64_DTPREL16_HI:
14721 case R_PPC64_DTPREL16_HA:
14722 case R_PPC64_DTPREL16_DS:
14723 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14724 case R_PPC64_DTPREL16_HIGH:
14725 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14726 case R_PPC64_DTPREL16_HIGHER:
14727 case R_PPC64_DTPREL16_HIGHERA:
14728 case R_PPC64_DTPREL16_HIGHEST:
14729 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14730 if (htab->elf.tls_sec != NULL)
14731 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14732 break;
14733
45965137
AM
14734 case R_PPC64_ADDR64_LOCAL:
14735 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14736 ? h->elf.other
14737 : sym->st_other);
14738 break;
14739
e515b051
AM
14740 case R_PPC64_DTPMOD64:
14741 relocation = 1;
14742 addend = 0;
14743 goto dodyn;
14744
411e1bfb 14745 case R_PPC64_TPREL64:
989f9879
AM
14746 if (htab->elf.tls_sec != NULL)
14747 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14748 goto dodyn;
14749
14750 case R_PPC64_DTPREL64:
989f9879
AM
14751 if (htab->elf.tls_sec != NULL)
14752 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 14753 /* Fall through. */
411e1bfb 14754
65f38f15
AM
14755 /* Relocations that may need to be propagated if this is a
14756 dynamic object. */
04c9666a 14757 case R_PPC64_REL30:
65f38f15
AM
14758 case R_PPC64_REL32:
14759 case R_PPC64_REL64:
14760 case R_PPC64_ADDR14:
14761 case R_PPC64_ADDR14_BRNTAKEN:
14762 case R_PPC64_ADDR14_BRTAKEN:
14763 case R_PPC64_ADDR16:
14764 case R_PPC64_ADDR16_DS:
14765 case R_PPC64_ADDR16_HA:
14766 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14767 case R_PPC64_ADDR16_HIGH:
14768 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14769 case R_PPC64_ADDR16_HIGHER:
14770 case R_PPC64_ADDR16_HIGHERA:
14771 case R_PPC64_ADDR16_HIGHEST:
14772 case R_PPC64_ADDR16_HIGHESTA:
14773 case R_PPC64_ADDR16_LO:
14774 case R_PPC64_ADDR16_LO_DS:
14775 case R_PPC64_ADDR24:
65f38f15
AM
14776 case R_PPC64_ADDR32:
14777 case R_PPC64_ADDR64:
14778 case R_PPC64_UADDR16:
14779 case R_PPC64_UADDR32:
14780 case R_PPC64_UADDR64:
411e1bfb 14781 dodyn:
5d1634d7 14782 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14783 break;
14784
41bd81ab
AM
14785 if (NO_OPD_RELOCS && is_opd)
14786 break;
14787
8a9e8e72
AM
14788 if (bfd_link_pic (info)
14789 ? ((h != NULL && pc_dynrelocs (h))
14790 || must_be_dyn_reloc (info, r_type))
14791 : (h != NULL
14792 ? h->dyn_relocs != NULL
d311bc8b 14793 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 14794 {
b34976b6 14795 bfd_boolean skip, relocate;
65f38f15 14796 asection *sreloc;
1cf1f670 14797 bfd_vma out_off;
65f38f15
AM
14798
14799 /* When generating a dynamic object, these relocations
14800 are copied into the output file to be resolved at run
14801 time. */
14802
b34976b6
AM
14803 skip = FALSE;
14804 relocate = FALSE;
65f38f15 14805
1cf1f670
AM
14806 out_off = _bfd_elf_section_offset (output_bfd, info,
14807 input_section, rel->r_offset);
14808 if (out_off == (bfd_vma) -1)
b34976b6 14809 skip = TRUE;
1cf1f670 14810 else if (out_off == (bfd_vma) -2)
b34976b6 14811 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14812 out_off += (input_section->output_section->vma
14813 + input_section->output_offset);
14814 outrel.r_offset = out_off;
411e1bfb 14815 outrel.r_addend = rel->r_addend;
65f38f15 14816
1cf1f670
AM
14817 /* Optimize unaligned reloc use. */
14818 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14819 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14820 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14821 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14822 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14823 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14824 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14825 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14826 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14827
65f38f15 14828 if (skip)
0bb2d96a 14829 memset (&outrel, 0, sizeof outrel);
afe397ea 14830 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14831 && !is_opd
14832 && r_type != R_PPC64_TOC)
14acf4dc
MR
14833 {
14834 BFD_ASSERT (h->elf.dynindx != -1);
14835 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14836 }
65f38f15
AM
14837 else
14838 {
41bd81ab
AM
14839 /* This symbol is local, or marked to become local,
14840 or this is an opd section reloc which must point
14841 at a local function. */
65f38f15 14842 outrel.r_addend += relocation;
e86ce104 14843 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14844 {
3fad3c7c 14845 if (is_opd && h != NULL)
afbe61cf
AM
14846 {
14847 /* Lie about opd entries. This case occurs
14848 when building shared libraries and we
14849 reference a function in another shared
3fad3c7c
AM
14850 lib. The same thing happens for a weak
14851 definition in an application that's
14852 overridden by a strong definition in a
14853 shared lib. (I believe this is a generic
14854 bug in binutils handling of weak syms.)
14855 In these cases we won't use the opd
1e2f5b6e 14856 entry in this lib. */
b34976b6 14857 unresolved_reloc = FALSE;
afbe61cf 14858 }
25f23106
AM
14859 if (!is_opd
14860 && r_type == R_PPC64_ADDR64
14861 && (h != NULL
14862 ? h->elf.type == STT_GNU_IFUNC
14863 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14864 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14865 else
14866 {
14867 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14868
25f23106
AM
14869 /* We need to relocate .opd contents for ld.so.
14870 Prelink also wants simple and consistent rules
14871 for relocs. This make all RELATIVE relocs have
14872 *r_offset equal to r_addend. */
14873 relocate = TRUE;
14874 }
65f38f15
AM
14875 }
14876 else
14877 {
14878 long indx = 0;
14879
25f23106
AM
14880 if (h != NULL
14881 ? h->elf.type == STT_GNU_IFUNC
14882 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14883 {
25f53a85 14884 info->callbacks->einfo
695344c0 14885 /* xgettext:c-format */
bc30df16
AM
14886 (_("%P: %H: %s for indirect "
14887 "function `%T' unsupported\n"),
25f53a85 14888 input_bfd, input_section, rel->r_offset,
25f23106
AM
14889 ppc64_elf_howto_table[r_type]->name,
14890 sym_name);
14891 ret = FALSE;
14892 }
cf35638d 14893 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14894 ;
14895 else if (sec == NULL || sec->owner == NULL)
14896 {
14897 bfd_set_error (bfd_error_bad_value);
b34976b6 14898 return FALSE;
65f38f15
AM
14899 }
14900 else
14901 {
14902 asection *osec;
14903
14904 osec = sec->output_section;
14905 indx = elf_section_data (osec)->dynindx;
14906
74541ad4
AM
14907 if (indx == 0)
14908 {
14909 if ((osec->flags & SEC_READONLY) == 0
14910 && htab->elf.data_index_section != NULL)
14911 osec = htab->elf.data_index_section;
14912 else
14913 osec = htab->elf.text_index_section;
14914 indx = elf_section_data (osec)->dynindx;
14915 }
14916 BFD_ASSERT (indx != 0);
14917
65f38f15
AM
14918 /* We are turning this relocation into one
14919 against a section symbol, so subtract out
14920 the output section's address but not the
14921 offset of the input section in the output
14922 section. */
14923 outrel.r_addend -= osec->vma;
14924 }
14925
14926 outrel.r_info = ELF64_R_INFO (indx, r_type);
14927 }
14928 }
14929
14930 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14931 if (h != NULL
14932 ? h->elf.type == STT_GNU_IFUNC
14933 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
33e44f2e 14934 sreloc = htab->elf.irelplt;
65f38f15
AM
14935 if (sreloc == NULL)
14936 abort ();
14937
dfbb6ac9
AM
14938 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14939 >= sreloc->size)
14940 abort ();
947216bf
AM
14941 loc = sreloc->contents;
14942 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14943 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14944
14945 /* If this reloc is against an external symbol, it will
14946 be computed at runtime, so there's no need to do
81407a69
AM
14947 anything now. However, for the sake of prelink ensure
14948 that the section contents are a known value. */
65f38f15 14949 if (! relocate)
81407a69
AM
14950 {
14951 unresolved_reloc = FALSE;
14952 /* The value chosen here is quite arbitrary as ld.so
14953 ignores section contents except for the special
14954 case of .opd where the contents might be accessed
14955 before relocation. Choose zero, as that won't
14956 cause reloc overflow. */
14957 relocation = 0;
14958 addend = 0;
14959 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14960 to improve backward compatibility with older
14961 versions of ld. */
14962 if (r_type == R_PPC64_ADDR64)
14963 addend = outrel.r_addend;
14964 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 14965 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
14966 addend = (input_section->output_section->vma
14967 + input_section->output_offset
14968 + rel->r_offset);
81407a69 14969 }
65f38f15 14970 }
5bd4f169
AM
14971 break;
14972
65f38f15
AM
14973 case R_PPC64_COPY:
14974 case R_PPC64_GLOB_DAT:
14975 case R_PPC64_JMP_SLOT:
25f23106 14976 case R_PPC64_JMP_IREL:
65f38f15
AM
14977 case R_PPC64_RELATIVE:
14978 /* We shouldn't ever see these dynamic relocs in relocatable
14979 files. */
ae9a127f 14980 /* Fall through. */
65f38f15
AM
14981
14982 case R_PPC64_PLTGOT16:
14983 case R_PPC64_PLTGOT16_DS:
14984 case R_PPC64_PLTGOT16_HA:
14985 case R_PPC64_PLTGOT16_HI:
14986 case R_PPC64_PLTGOT16_LO:
14987 case R_PPC64_PLTGOT16_LO_DS:
14988 case R_PPC64_PLTREL32:
14989 case R_PPC64_PLTREL64:
14990 /* These ones haven't been implemented yet. */
14991
25f53a85 14992 info->callbacks->einfo
695344c0 14993 /* xgettext:c-format */
bc30df16 14994 (_("%P: %B: %s is not supported for `%T'\n"),
d003868e 14995 input_bfd,
4ce794b7 14996 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
14997
14998 bfd_set_error (bfd_error_invalid_operation);
b34976b6 14999 ret = FALSE;
c316a17c 15000 goto copy_reloc;
65f38f15 15001 }
5bd4f169 15002
67f0cbdb
AM
15003 /* Multi-instruction sequences that access the TOC can be
15004 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15005 to nop; addi rb,r2,x; */
15006 switch (r_type)
15007 {
15008 default:
15009 break;
15010
15011 case R_PPC64_GOT_TLSLD16_HI:
15012 case R_PPC64_GOT_TLSGD16_HI:
15013 case R_PPC64_GOT_TPREL16_HI:
15014 case R_PPC64_GOT_DTPREL16_HI:
15015 case R_PPC64_GOT16_HI:
15016 case R_PPC64_TOC16_HI:
15017 /* These relocs would only be useful if building up an
15018 offset to later add to r2, perhaps in an indexed
15019 addressing mode instruction. Don't try to optimize.
15020 Unfortunately, the possibility of someone building up an
15021 offset like this or even with the HA relocs, means that
15022 we need to check the high insn when optimizing the low
15023 insn. */
15024 break;
15025
15026 case R_PPC64_GOT_TLSLD16_HA:
15027 case R_PPC64_GOT_TLSGD16_HA:
15028 case R_PPC64_GOT_TPREL16_HA:
15029 case R_PPC64_GOT_DTPREL16_HA:
15030 case R_PPC64_GOT16_HA:
15031 case R_PPC64_TOC16_HA:
98528052 15032 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15033 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
15034 {
15035 bfd_byte *p = contents + (rel->r_offset & ~3);
15036 bfd_put_32 (input_bfd, NOP, p);
15037 }
67f0cbdb
AM
15038 break;
15039
15040 case R_PPC64_GOT_TLSLD16_LO:
15041 case R_PPC64_GOT_TLSGD16_LO:
15042 case R_PPC64_GOT_TPREL16_LO_DS:
15043 case R_PPC64_GOT_DTPREL16_LO_DS:
15044 case R_PPC64_GOT16_LO:
15045 case R_PPC64_GOT16_LO_DS:
15046 case R_PPC64_TOC16_LO:
15047 case R_PPC64_TOC16_LO_DS:
98528052 15048 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15049 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15050 {
15051 bfd_byte *p = contents + (rel->r_offset & ~3);
15052 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15053 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15054 {
15055 /* Transform addic to addi when we change reg. */
15056 insn &= ~((0x3f << 26) | (0x1f << 16));
15057 insn |= (14u << 26) | (2 << 16);
15058 }
15059 else
67f0cbdb 15060 {
98528052
AM
15061 insn &= ~(0x1f << 16);
15062 insn |= 2 << 16;
67f0cbdb 15063 }
560c8763 15064 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15065 }
15066 break;
15067 }
15068
65f38f15 15069 /* Do any further special processing. */
b80eed39 15070 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15
AM
15071 switch (r_type)
15072 {
15073 default:
15074 break;
15075
25f23106 15076 case R_PPC64_REL16_HA:
a680de9a 15077 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15078 case R_PPC64_ADDR16_HA:
15079 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15080 case R_PPC64_ADDR16_HIGHERA:
15081 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15082 case R_PPC64_TOC16_HA:
15083 case R_PPC64_SECTOFF_HA:
411e1bfb 15084 case R_PPC64_TPREL16_HA:
f9c6b907 15085 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15086 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15087 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15088 case R_PPC64_DTPREL16_HA:
15089 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15090 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15091 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15092 /* It's just possible that this symbol is a weak symbol
15093 that's not actually defined anywhere. In that case,
15094 'sec' would be NULL, and we should leave the symbol
15095 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15096 if (sec == NULL)
15097 break;
1a0670f3 15098 /* Fall through. */
5c5f6e17
AM
15099
15100 case R_PPC64_GOT16_HA:
15101 case R_PPC64_PLTGOT16_HA:
15102 case R_PPC64_PLT16_HA:
15103 case R_PPC64_GOT_TLSGD16_HA:
15104 case R_PPC64_GOT_TLSLD16_HA:
15105 case R_PPC64_GOT_TPREL16_HA:
15106 case R_PPC64_GOT_DTPREL16_HA:
15107 /* Add 0x10000 if sign bit in 0:15 is set.
15108 Bits 0:15 are not used. */
15109 addend += 0x8000;
65f38f15
AM
15110 break;
15111
15112 case R_PPC64_ADDR16_DS:
15113 case R_PPC64_ADDR16_LO_DS:
15114 case R_PPC64_GOT16_DS:
15115 case R_PPC64_GOT16_LO_DS:
15116 case R_PPC64_PLT16_LO_DS:
15117 case R_PPC64_SECTOFF_DS:
15118 case R_PPC64_SECTOFF_LO_DS:
15119 case R_PPC64_TOC16_DS:
15120 case R_PPC64_TOC16_LO_DS:
15121 case R_PPC64_PLTGOT16_DS:
15122 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15123 case R_PPC64_GOT_TPREL16_DS:
15124 case R_PPC64_GOT_TPREL16_LO_DS:
15125 case R_PPC64_GOT_DTPREL16_DS:
15126 case R_PPC64_GOT_DTPREL16_LO_DS:
15127 case R_PPC64_TPREL16_DS:
15128 case R_PPC64_TPREL16_LO_DS:
15129 case R_PPC64_DTPREL16_DS:
15130 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15131 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15132 mask = 3;
a680de9a
PB
15133 /* If this reloc is against an lq, lxv, or stxv insn, then
15134 the value must be a multiple of 16. This is somewhat of
15135 a hack, but the "correct" way to do this by defining _DQ
15136 forms of all the _DS relocs bloats all reloc switches in
15137 this file. It doesn't make much sense to use these
15138 relocs in data, so testing the insn should be safe. */
15139 if ((insn & (0x3f << 26)) == (56u << 26)
15140 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15141 mask = 15;
a680de9a
PB
15142 relocation += addend;
15143 addend = insn & (mask ^ 3);
15144 if ((relocation & mask) != 0)
65f38f15 15145 {
a680de9a 15146 relocation ^= relocation & mask;
25f53a85 15147 info->callbacks->einfo
695344c0 15148 /* xgettext:c-format */
8de848d8 15149 (_("%P: %H: error: %s not a multiple of %u\n"),
25f53a85 15150 input_bfd, input_section, rel->r_offset,
b80eed39 15151 howto->name,
adadcc0c 15152 mask + 1);
65f38f15 15153 bfd_set_error (bfd_error_bad_value);
b34976b6 15154 ret = FALSE;
c316a17c 15155 goto copy_reloc;
65f38f15
AM
15156 }
15157 break;
5bd4f169
AM
15158 }
15159
239e1f3a
AM
15160 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15161 because such sections are not SEC_ALLOC and thus ld.so will
15162 not process them. */
65f38f15 15163 if (unresolved_reloc
239e1f3a 15164 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15165 && h->elf.def_dynamic)
15166 && _bfd_elf_section_offset (output_bfd, info, input_section,
15167 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15168 {
25f53a85 15169 info->callbacks->einfo
695344c0 15170 /* xgettext:c-format */
bc30df16 15171 (_("%P: %H: unresolvable %s against `%T'\n"),
25f53a85 15172 input_bfd, input_section, rel->r_offset,
b80eed39 15173 howto->name,
039b3fef 15174 h->elf.root.root.string);
b34976b6 15175 ret = FALSE;
9c07fe7c 15176 }
5bd4f169 15177
b80eed39
AM
15178 /* 16-bit fields in insns mostly have signed values, but a
15179 few insns have 16-bit unsigned values. Really, we should
15180 have different reloc types. */
15181 if (howto->complain_on_overflow != complain_overflow_dont
15182 && howto->dst_mask == 0xffff
15183 && (input_section->flags & SEC_CODE) != 0)
15184 {
15185 enum complain_overflow complain = complain_overflow_signed;
15186
15187 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15188 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15189 complain = complain_overflow_bitfield;
15190 else if (howto->rightshift == 0
15191 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15192 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15193 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15194 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15195 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15196 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15197 complain = complain_overflow_unsigned;
15198 if (howto->complain_on_overflow != complain)
15199 {
15200 alt_howto = *howto;
15201 alt_howto.complain_on_overflow = complain;
15202 howto = &alt_howto;
15203 }
15204 }
15205
a680de9a
PB
15206 if (r_type == R_PPC64_REL16DX_HA)
15207 {
15208 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15209 if (rel->r_offset + 4 > input_section->size)
15210 r = bfd_reloc_outofrange;
15211 else
15212 {
15213 relocation += addend;
15214 relocation -= (rel->r_offset
15215 + input_section->output_offset
15216 + input_section->output_section->vma);
15217 relocation = (bfd_signed_vma) relocation >> 16;
15218 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15219 insn &= ~0x1fffc1;
15220 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15221 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15222 r = bfd_reloc_ok;
15223 if (relocation + 0x8000 > 0xffff)
15224 r = bfd_reloc_overflow;
15225 }
15226 }
15227 else
15228 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15229 rel->r_offset, relocation, addend);
5bd4f169 15230
ef60b7ff 15231 if (r != bfd_reloc_ok)
5bd4f169 15232 {
bc30df16 15233 char *more_info = NULL;
b80eed39 15234 const char *reloc_name = howto->name;
bc30df16
AM
15235
15236 if (reloc_dest != DEST_NORMAL)
15237 {
15238 more_info = bfd_malloc (strlen (reloc_name) + 8);
15239 if (more_info != NULL)
15240 {
15241 strcpy (more_info, reloc_name);
15242 strcat (more_info, (reloc_dest == DEST_OPD
15243 ? " (OPD)" : " (stub)"));
15244 reloc_name = more_info;
15245 }
15246 }
15247
cd27b276 15248 if (r == bfd_reloc_overflow)
5bd4f169 15249 {
8131c122
AM
15250 /* On code like "if (foo) foo();" don't report overflow
15251 on a branch to zero when foo is undefined. */
15252 if (!warned
15253 && (reloc_dest == DEST_STUB
15254 || !(h != NULL
15255 && (h->elf.root.type == bfd_link_hash_undefweak
15256 || h->elf.root.type == bfd_link_hash_undefined)
15257 && is_branch_reloc (r_type))))
1a72702b
AM
15258 info->callbacks->reloc_overflow (info, &h->elf.root,
15259 sym_name, reloc_name,
15260 orig_rel.r_addend,
15261 input_bfd, input_section,
15262 rel->r_offset);
ef60b7ff
AM
15263 }
15264 else
15265 {
25f53a85 15266 info->callbacks->einfo
695344c0 15267 /* xgettext:c-format */
bc30df16 15268 (_("%P: %H: %s against `%T': error %d\n"),
25f53a85 15269 input_bfd, input_section, rel->r_offset,
bc30df16 15270 reloc_name, sym_name, (int) r);
b34976b6 15271 ret = FALSE;
ef60b7ff 15272 }
bc30df16
AM
15273 if (more_info != NULL)
15274 free (more_info);
5bd4f169 15275 }
c316a17c
AM
15276 copy_reloc:
15277 if (wrel != rel)
15278 *wrel = *rel;
15279 }
15280
15281 if (wrel != rel)
15282 {
15283 Elf_Internal_Shdr *rel_hdr;
15284 size_t deleted = rel - wrel;
15285
15286 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15287 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15288 if (rel_hdr->sh_size == 0)
15289 {
15290 /* It is too late to remove an empty reloc section. Leave
15291 one NONE reloc.
15292 ??? What is wrong with an empty section??? */
15293 rel_hdr->sh_size = rel_hdr->sh_entsize;
15294 deleted -= 1;
15295 }
15296 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15297 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15298 input_section->reloc_count -= deleted;
5bd4f169
AM
15299 }
15300
645ea6a9
AM
15301 /* If we're emitting relocations, then shortly after this function
15302 returns, reloc offsets and addends for this section will be
15303 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15304 file rather than the input. Save a copy of the relocs for
15305 opd_entry_value. */
0e1862bb 15306 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15307 {
15308 bfd_size_type amt;
15309 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15310 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15311 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15312 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15313 if (rel == NULL)
15314 return FALSE;
15315 memcpy (rel, relocs, amt);
15316 }
5bd4f169
AM
15317 return ret;
15318}
15319
754021d0
AM
15320/* Adjust the value of any local symbols in opd sections. */
15321
6e0b88f1 15322static int
754021d0
AM
15323ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15324 const char *name ATTRIBUTE_UNUSED,
15325 Elf_Internal_Sym *elfsym,
15326 asection *input_sec,
15327 struct elf_link_hash_entry *h)
15328{
74f0fb50
AM
15329 struct _opd_sec_data *opd;
15330 long adjust;
754021d0
AM
15331 bfd_vma value;
15332
4025353c 15333 if (h != NULL)
6e0b88f1 15334 return 1;
4025353c 15335
74f0fb50
AM
15336 opd = get_opd_info (input_sec);
15337 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15338 return 1;
754021d0
AM
15339
15340 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15341 if (!bfd_link_relocatable (info))
754021d0
AM
15342 value -= input_sec->output_section->vma;
15343
51aecdc5 15344 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15345 if (adjust == -1)
6e0b88f1
AM
15346 return 2;
15347
15348 elfsym->st_value += adjust;
15349 return 1;
754021d0
AM
15350}
15351
5bd4f169
AM
15352/* Finish up dynamic symbol handling. We set the contents of various
15353 dynamic sections here. */
15354
b34976b6 15355static bfd_boolean
4ce794b7
AM
15356ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15357 struct bfd_link_info *info,
15358 struct elf_link_hash_entry *h,
ab6dce23 15359 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5bd4f169 15360{
65f38f15 15361 struct ppc_link_hash_table *htab;
8387904d
AM
15362 struct plt_entry *ent;
15363 Elf_Internal_Rela rela;
15364 bfd_byte *loc;
5bd4f169 15365
65f38f15 15366 htab = ppc_hash_table (info);
4dfe6ac6
NC
15367 if (htab == NULL)
15368 return FALSE;
5bd4f169 15369
8387904d
AM
15370 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15371 if (ent->plt.offset != (bfd_vma) -1)
15372 {
15373 /* This symbol has an entry in the procedure linkage
15374 table. Set it up. */
e054468f
AM
15375 if (!htab->elf.dynamic_sections_created
15376 || h->dynindx == -1)
15377 {
15378 BFD_ASSERT (h->type == STT_GNU_IFUNC
15379 && h->def_regular
15380 && (h->root.type == bfd_link_hash_defined
15381 || h->root.type == bfd_link_hash_defweak));
33e44f2e
AM
15382 rela.r_offset = (htab->elf.iplt->output_section->vma
15383 + htab->elf.iplt->output_offset
25f23106 15384 + ent->plt.offset);
ee67d69a
AM
15385 if (htab->opd_abi)
15386 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15387 else
15388 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
15389 rela.r_addend = (h->root.u.def.value
15390 + h->root.u.def.section->output_offset
15391 + h->root.u.def.section->output_section->vma
15392 + ent->addend);
33e44f2e
AM
15393 loc = (htab->elf.irelplt->contents
15394 + (htab->elf.irelplt->reloc_count++
25f23106 15395 * sizeof (Elf64_External_Rela)));
e054468f
AM
15396 }
15397 else
15398 {
33e44f2e
AM
15399 rela.r_offset = (htab->elf.splt->output_section->vma
15400 + htab->elf.splt->output_offset
25f23106 15401 + ent->plt.offset);
e054468f
AM
15402 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15403 rela.r_addend = ent->addend;
33e44f2e 15404 loc = (htab->elf.srelplt->contents
b9e5796b
AM
15405 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15406 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
e054468f 15407 }
8387904d 15408 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a345bc8d
AM
15409
15410 if (!htab->opd_abi)
15411 {
15412 if (!h->def_regular)
15413 {
15414 /* Mark the symbol as undefined, rather than as
15415 defined in glink. Leave the value if there were
15416 any relocations where pointer equality matters
15417 (this is a clue for the dynamic linker, to make
15418 function pointer comparisons work between an
15419 application and shared library), otherwise set it
15420 to zero. */
15421 sym->st_shndx = SHN_UNDEF;
15422 if (!h->pointer_equality_needed)
15423 sym->st_value = 0;
15424 else if (!h->ref_regular_nonweak)
15425 {
15426 /* This breaks function pointer comparisons, but
15427 that is better than breaking tests for a NULL
15428 function pointer. */
15429 sym->st_value = 0;
15430 }
15431 }
15432 }
8387904d 15433 }
5bd4f169 15434
f5385ebf 15435 if (h->needs_copy)
5bd4f169 15436 {
65f38f15 15437 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 15438
65f38f15
AM
15439 if (h->dynindx == -1
15440 || (h->root.type != bfd_link_hash_defined
15441 && h->root.type != bfd_link_hash_defweak)
4ce794b7 15442 || htab->relbss == NULL)
65f38f15 15443 abort ();
5bd4f169
AM
15444
15445 rela.r_offset = (h->root.u.def.value
15446 + h->root.u.def.section->output_section->vma
15447 + h->root.u.def.section->output_offset);
15448 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15449 rela.r_addend = 0;
4ce794b7
AM
15450 loc = htab->relbss->contents;
15451 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15452 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15453 }
15454
b34976b6 15455 return TRUE;
5bd4f169
AM
15456}
15457
65f38f15
AM
15458/* Used to decide how to sort relocs in an optimal manner for the
15459 dynamic linker, before writing them out. */
15460
15461static enum elf_reloc_type_class
7e612e98
AM
15462ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15463 const asection *rel_sec,
15464 const Elf_Internal_Rela *rela)
65f38f15 15465{
04c9666a 15466 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15467 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15468
33e44f2e 15469 if (rel_sec == htab->elf.irelplt)
7e612e98 15470 return reloc_class_ifunc;
a33d1f77 15471
4ce794b7 15472 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15473 switch (r_type)
65f38f15
AM
15474 {
15475 case R_PPC64_RELATIVE:
15476 return reloc_class_relative;
15477 case R_PPC64_JMP_SLOT:
15478 return reloc_class_plt;
15479 case R_PPC64_COPY:
15480 return reloc_class_copy;
15481 default:
15482 return reloc_class_normal;
15483 }
15484}
15485
5bd4f169
AM
15486/* Finish up the dynamic sections. */
15487
b34976b6 15488static bfd_boolean
4ce794b7
AM
15489ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15490 struct bfd_link_info *info)
5bd4f169 15491{
65f38f15
AM
15492 struct ppc_link_hash_table *htab;
15493 bfd *dynobj;
5bd4f169 15494 asection *sdyn;
5bd4f169 15495
65f38f15 15496 htab = ppc_hash_table (info);
4dfe6ac6
NC
15497 if (htab == NULL)
15498 return FALSE;
15499
65f38f15 15500 dynobj = htab->elf.dynobj;
3d4d4302 15501 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15502
65f38f15 15503 if (htab->elf.dynamic_sections_created)
5bd4f169 15504 {
5bd4f169
AM
15505 Elf64_External_Dyn *dyncon, *dynconend;
15506
33e44f2e 15507 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15508 abort ();
5bd4f169
AM
15509
15510 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15511 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15512 for (; dyncon < dynconend; dyncon++)
15513 {
15514 Elf_Internal_Dyn dyn;
19397422 15515 asection *s;
5bd4f169
AM
15516
15517 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15518
15519 switch (dyn.d_tag)
15520 {
65f38f15
AM
15521 default:
15522 continue;
5bd4f169 15523
5d1634d7 15524 case DT_PPC64_GLINK:
4ce794b7 15525 s = htab->glink;
6348e046 15526 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15527 /* We stupidly defined DT_PPC64_GLINK to be the start
15528 of glink rather than the first entry point, which is
15529 what ld.so needs, and now have a bigger stub to
15530 support automatic multiple TOCs. */
b9e5796b 15531 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
5d1634d7
AM
15532 break;
15533
19397422
AM
15534 case DT_PPC64_OPD:
15535 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15536 if (s == NULL)
15537 continue;
15538 dyn.d_un.d_ptr = s->vma;
19397422
AM
15539 break;
15540
e8910a83
AM
15541 case DT_PPC64_OPT:
15542 if (htab->do_multi_toc && htab->multi_toc_needed)
15543 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15544 break;
15545
19397422
AM
15546 case DT_PPC64_OPDSZ:
15547 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15548 if (s == NULL)
15549 continue;
eea6121a 15550 dyn.d_un.d_val = s->size;
19397422
AM
15551 break;
15552
65f38f15 15553 case DT_PLTGOT:
33e44f2e 15554 s = htab->elf.splt;
6348e046 15555 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15556 break;
15557
15558 case DT_JMPREL:
33e44f2e 15559 s = htab->elf.srelplt;
6348e046 15560 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15561 break;
5bd4f169 15562
65f38f15 15563 case DT_PLTRELSZ:
33e44f2e 15564 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 15565 break;
5bd4f169 15566 }
5bd4f169 15567
65f38f15 15568 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15569 }
5bd4f169
AM
15570 }
15571
33e44f2e 15572 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
5d1634d7
AM
15573 {
15574 /* Fill in the first entry in the global offset table.
15575 We use it to hold the link-time TOCbase. */
15576 bfd_put_64 (output_bfd,
60ee0d4a 15577 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15578 htab->elf.sgot->contents);
5d1634d7
AM
15579
15580 /* Set .got entry size. */
33e44f2e 15581 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
15582 }
15583
33e44f2e 15584 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5d1634d7
AM
15585 {
15586 /* Set .plt entry size. */
33e44f2e 15587 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15588 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15589 }
15590
84f5d08e
AM
15591 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15592 brlt ourselves if emitrelocations. */
15593 if (htab->brlt != NULL
15594 && htab->brlt->reloc_count != 0
15595 && !_bfd_elf_link_output_relocs (output_bfd,
15596 htab->brlt,
d4730f92 15597 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15598 elf_section_data (htab->brlt)->relocs,
15599 NULL))
15600 return FALSE;
15601
176a0d42
AM
15602 if (htab->glink != NULL
15603 && htab->glink->reloc_count != 0
15604 && !_bfd_elf_link_output_relocs (output_bfd,
15605 htab->glink,
d4730f92 15606 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15607 elf_section_data (htab->glink)->relocs,
15608 NULL))
15609 return FALSE;
15610
58d180e8 15611 if (htab->glink_eh_frame != NULL
da44f4e5
AM
15612 && htab->glink_eh_frame->size != 0)
15613 {
15614 bfd_vma val;
15615 bfd_byte *p;
15616 asection *stub_sec;
15617
15618 p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15619 for (stub_sec = htab->params->stub_bfd->sections;
15620 stub_sec != NULL;
15621 stub_sec = stub_sec->next)
15622 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15623 {
15624 /* FDE length. */
15625 p += 4;
15626 /* CIE pointer. */
15627 p += 4;
15628 /* Offset to stub section. */
15629 val = (stub_sec->output_section->vma
15630 + stub_sec->output_offset);
15631 val -= (htab->glink_eh_frame->output_section->vma
15632 + htab->glink_eh_frame->output_offset
15633 + (p - htab->glink_eh_frame->contents));
15634 if (val + 0x80000000 > 0xffffffff)
15635 {
15636 info->callbacks->einfo
15637 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15638 stub_sec->name);
15639 return FALSE;
15640 }
15641 bfd_put_32 (dynobj, val, p);
15642 p += 4;
15643 /* stub section size. */
15644 p += 4;
15645 /* Augmentation. */
15646 p += 1;
15647 /* Pad. */
15648 p += 7;
15649 }
15650 if (htab->glink != NULL && htab->glink->size != 0)
15651 {
15652 /* FDE length. */
15653 p += 4;
15654 /* CIE pointer. */
15655 p += 4;
15656 /* Offset to .glink. */
15657 val = (htab->glink->output_section->vma
15658 + htab->glink->output_offset
15659 + 8);
15660 val -= (htab->glink_eh_frame->output_section->vma
15661 + htab->glink_eh_frame->output_offset
15662 + (p - htab->glink_eh_frame->contents));
15663 if (val + 0x80000000 > 0xffffffff)
15664 {
15665 info->callbacks->einfo
15666 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15667 htab->glink->name);
15668 return FALSE;
15669 }
15670 bfd_put_32 (dynobj, val, p);
15671 p += 4;
15672 /* .glink size. */
15673 p += 4;
15674 /* Augmentation. */
15675 p += 1;
15676 /* Ops. */
15677 p += 7;
15678 }
15679
15680 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15681 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15682 htab->glink_eh_frame,
15683 htab->glink_eh_frame->contents))
15684 return FALSE;
15685 }
58d180e8 15686
e717da7e 15687 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15688 since we didn't add them to DYNOBJ. We know dynobj is the first
15689 bfd. */
c72f2fb2 15690 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15691 {
15692 asection *s;
7b53ace3 15693
0c8d6e5c 15694 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15695 continue;
15696
e717da7e
AM
15697 s = ppc64_elf_tdata (dynobj)->got;
15698 if (s != NULL
eea6121a 15699 && s->size != 0
e717da7e
AM
15700 && s->output_section != bfd_abs_section_ptr
15701 && !bfd_set_section_contents (output_bfd, s->output_section,
15702 s->contents, s->output_offset,
eea6121a 15703 s->size))
e717da7e
AM
15704 return FALSE;
15705 s = ppc64_elf_tdata (dynobj)->relgot;
15706 if (s != NULL
eea6121a 15707 && s->size != 0
e717da7e
AM
15708 && s->output_section != bfd_abs_section_ptr
15709 && !bfd_set_section_contents (output_bfd, s->output_section,
15710 s->contents, s->output_offset,
eea6121a 15711 s->size))
e717da7e
AM
15712 return FALSE;
15713 }
f6c52c13 15714
b34976b6 15715 return TRUE;
5bd4f169
AM
15716}
15717
5bd4f169 15718#include "elf64-target.h"
7b8e7dad
AM
15719
15720/* FreeBSD support */
15721
15722#undef TARGET_LITTLE_SYM
15723#undef TARGET_LITTLE_NAME
15724
15725#undef TARGET_BIG_SYM
6d00b590 15726#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15727#undef TARGET_BIG_NAME
15728#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15729
15730#undef ELF_OSABI
15731#define ELF_OSABI ELFOSABI_FREEBSD
15732
15733#undef elf64_bed
15734#define elf64_bed elf64_powerpc_fbsd_bed
15735
15736#include "elf64-target.h"
15737