Rework RISC-V relocations
[deliverable/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
8c5b4e52 90#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
91
92#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
93#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
94#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 95#define elf_backend_write_core_note ppc64_elf_write_core_note
ad8e1ba5
AM
96#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
97#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 98#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 99#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 100#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 101#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 102#define elf_backend_check_relocs ppc64_elf_check_relocs
74f0fb50 103#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 104#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
105#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
106#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
107#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
108#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 109#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
ad8e1ba5
AM
110#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
111#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 112#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 113#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 114#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
115#define elf_backend_relocate_section ppc64_elf_relocate_section
116#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
117#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
118#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 119#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 120#define elf_backend_special_sections ppc64_elf_special_sections
6911b7dc 121#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 122#define elf_backend_merge_symbol ppc64_elf_merge_symbol
ad8e1ba5 123
5bd4f169
AM
124/* The name of the dynamic interpreter. This is put in the .interp
125 section. */
126#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
127
128/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 129#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
5bd4f169
AM
130
131/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 132#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 133
a078d95a
AM
134/* Offsets to some stack save slots. */
135#define STK_LR 16
136#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 137/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
138 CR save slot. Used only by optimised __tls_get_addr call stub,
139 relying on __tls_get_addr_opt not saving CR.. */
140#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
141
5bd4f169 142/* TOC base pointers offset from start of TOC. */
411e1bfb 143#define TOC_BASE_OFF 0x8000
a27e685f
AM
144/* TOC base alignment. */
145#define TOC_BASE_ALIGN 256
411e1bfb
AM
146
147/* Offset of tp and dtp pointers from start of TLS block. */
148#define TP_OFFSET 0x7000
149#define DTP_OFFSET 0x8000
5bd4f169 150
ad8e1ba5
AM
151/* .plt call stub instructions. The normal stub is like this, but
152 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 153 insert an addi to adjust r11. */
a078d95a 154#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
155#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
156#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
157#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
158#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
159#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
160#define BCTR 0x4e800420 /* bctr */
161
71a39c98 162#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
ad8e1ba5
AM
163#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
164#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
165
71a39c98
AM
166#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
167#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
168#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
794e51c0
AM
169#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
170#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
171#define BNECTR 0x4ca20420 /* bnectr+ */
172#define BNECTR_P4 0x4ce20420 /* bnectr+ */
173
71a39c98 174#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442
AM
175#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
176#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
177
a078d95a 178#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
006589cf
AM
179#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
180#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 181
006589cf
AM
182#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
183#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
184#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
a345bc8d
AM
185#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
186#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
187
ee4bf8d2 188/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 189#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
190 /* 0: */
191 /* .quad plt0-1f */
192 /* __glink: */
193#define MFLR_R12 0x7d8802a6 /* mflr %12 */
194#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
195 /* 1: */
196#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 197 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 198#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
199#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
200 /* ld %12,0(%11) */
201 /* ld %2,8(%11) */
202 /* mtctr %12 */
203 /* ld %11,16(%11) */
ee4bf8d2 204 /* bctr */
b9e5796b
AM
205#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
206#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
207#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
208#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
209#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
5d1634d7
AM
210
211/* Pad with this. */
212#define NOP 0x60000000
213
721956f4
AM
214/* Some other nops. */
215#define CROR_151515 0x4def7b82
216#define CROR_313131 0x4ffffb82
217
cedb70c5 218/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
219#define LI_R0_0 0x38000000 /* li %r0,0 */
220#define B_DOT 0x48000000 /* b . */
221
222/* After that, we need two instructions to load the index, followed by
223 a branch. */
224#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 225#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 226
deb0e272
AM
227/* Instructions used by the save and restore reg functions. */
228#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
229#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
230#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
231#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
232#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
233#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
234#define LI_R12_0 0x39800000 /* li %r12,0 */
235#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
236#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
237#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
238#define BLR 0x4e800020 /* blr */
239
41bd81ab
AM
240/* Since .opd is an array of descriptors and each entry will end up
241 with identical R_PPC64_RELATIVE relocs, there is really no need to
242 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 243 relocate .opd without reloc entries. */
41bd81ab
AM
244#ifndef NO_OPD_RELOCS
245#define NO_OPD_RELOCS 0
246#endif
810d4e75 247
a4b6fadd
AM
248#ifndef ARRAY_SIZE
249#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
250#endif
251
810d4e75
AM
252static inline int
253abiversion (bfd *abfd)
254{
255 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
256}
257
258static inline void
259set_abiversion (bfd *abfd, int ver)
260{
261 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
262 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
263}
5bd4f169 264\f
f5e87a1d 265#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 266
5bd4f169 267/* Relocation HOWTO's. */
04c9666a 268static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
269
270static reloc_howto_type ppc64_elf_howto_raw[] = {
271 /* This reloc does nothing. */
272 HOWTO (R_PPC64_NONE, /* type */
273 0, /* rightshift */
6346d5ca
AM
274 3, /* size (0 = byte, 1 = short, 2 = long) */
275 0, /* bitsize */
b34976b6 276 FALSE, /* pc_relative */
5bd4f169 277 0, /* bitpos */
f5e87a1d 278 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
279 bfd_elf_generic_reloc, /* special_function */
280 "R_PPC64_NONE", /* name */
b34976b6 281 FALSE, /* partial_inplace */
d006db6c 282 0, /* src_mask */
5bd4f169 283 0, /* dst_mask */
b34976b6 284 FALSE), /* pcrel_offset */
5bd4f169
AM
285
286 /* A standard 32 bit relocation. */
287 HOWTO (R_PPC64_ADDR32, /* type */
288 0, /* rightshift */
289 2, /* size (0 = byte, 1 = short, 2 = long) */
290 32, /* bitsize */
b34976b6 291 FALSE, /* pc_relative */
5bd4f169
AM
292 0, /* bitpos */
293 complain_overflow_bitfield, /* complain_on_overflow */
294 bfd_elf_generic_reloc, /* special_function */
295 "R_PPC64_ADDR32", /* name */
b34976b6 296 FALSE, /* partial_inplace */
5bd4f169
AM
297 0, /* src_mask */
298 0xffffffff, /* dst_mask */
b34976b6 299 FALSE), /* pcrel_offset */
5bd4f169
AM
300
301 /* An absolute 26 bit branch; the lower two bits must be zero.
302 FIXME: we don't check that, we just clear them. */
303 HOWTO (R_PPC64_ADDR24, /* type */
304 0, /* rightshift */
305 2, /* size (0 = byte, 1 = short, 2 = long) */
306 26, /* bitsize */
b34976b6 307 FALSE, /* pc_relative */
5bd4f169
AM
308 0, /* bitpos */
309 complain_overflow_bitfield, /* complain_on_overflow */
310 bfd_elf_generic_reloc, /* special_function */
311 "R_PPC64_ADDR24", /* name */
b34976b6 312 FALSE, /* partial_inplace */
d006db6c 313 0, /* src_mask */
f5e87a1d 314 0x03fffffc, /* dst_mask */
b34976b6 315 FALSE), /* pcrel_offset */
5bd4f169
AM
316
317 /* A standard 16 bit relocation. */
318 HOWTO (R_PPC64_ADDR16, /* type */
319 0, /* rightshift */
320 1, /* size (0 = byte, 1 = short, 2 = long) */
321 16, /* bitsize */
b34976b6 322 FALSE, /* pc_relative */
5bd4f169
AM
323 0, /* bitpos */
324 complain_overflow_bitfield, /* complain_on_overflow */
325 bfd_elf_generic_reloc, /* special_function */
326 "R_PPC64_ADDR16", /* name */
b34976b6 327 FALSE, /* partial_inplace */
5bd4f169
AM
328 0, /* src_mask */
329 0xffff, /* dst_mask */
b34976b6 330 FALSE), /* pcrel_offset */
5bd4f169
AM
331
332 /* A 16 bit relocation without overflow. */
333 HOWTO (R_PPC64_ADDR16_LO, /* type */
334 0, /* rightshift */
335 1, /* size (0 = byte, 1 = short, 2 = long) */
336 16, /* bitsize */
b34976b6 337 FALSE, /* pc_relative */
5bd4f169
AM
338 0, /* bitpos */
339 complain_overflow_dont,/* complain_on_overflow */
340 bfd_elf_generic_reloc, /* special_function */
341 "R_PPC64_ADDR16_LO", /* name */
b34976b6 342 FALSE, /* partial_inplace */
5bd4f169
AM
343 0, /* src_mask */
344 0xffff, /* dst_mask */
b34976b6 345 FALSE), /* pcrel_offset */
5bd4f169
AM
346
347 /* Bits 16-31 of an address. */
348 HOWTO (R_PPC64_ADDR16_HI, /* type */
349 16, /* rightshift */
350 1, /* size (0 = byte, 1 = short, 2 = long) */
351 16, /* bitsize */
b34976b6 352 FALSE, /* pc_relative */
5bd4f169 353 0, /* bitpos */
f9c6b907 354 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
355 bfd_elf_generic_reloc, /* special_function */
356 "R_PPC64_ADDR16_HI", /* name */
b34976b6 357 FALSE, /* partial_inplace */
5bd4f169
AM
358 0, /* src_mask */
359 0xffff, /* dst_mask */
b34976b6 360 FALSE), /* pcrel_offset */
5bd4f169
AM
361
362 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
363 bits, treated as a signed number, is negative. */
364 HOWTO (R_PPC64_ADDR16_HA, /* type */
365 16, /* rightshift */
366 1, /* size (0 = byte, 1 = short, 2 = long) */
367 16, /* bitsize */
b34976b6 368 FALSE, /* pc_relative */
5bd4f169 369 0, /* bitpos */
f9c6b907 370 complain_overflow_signed, /* complain_on_overflow */
805fc799 371 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 372 "R_PPC64_ADDR16_HA", /* name */
b34976b6 373 FALSE, /* partial_inplace */
5bd4f169
AM
374 0, /* src_mask */
375 0xffff, /* dst_mask */
b34976b6 376 FALSE), /* pcrel_offset */
5bd4f169
AM
377
378 /* An absolute 16 bit branch; the lower two bits must be zero.
379 FIXME: we don't check that, we just clear them. */
380 HOWTO (R_PPC64_ADDR14, /* type */
381 0, /* rightshift */
382 2, /* size (0 = byte, 1 = short, 2 = long) */
383 16, /* bitsize */
b34976b6 384 FALSE, /* pc_relative */
5bd4f169 385 0, /* bitpos */
b80eed39 386 complain_overflow_signed, /* complain_on_overflow */
2441e016 387 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 388 "R_PPC64_ADDR14", /* name */
b34976b6 389 FALSE, /* partial_inplace */
d006db6c 390 0, /* src_mask */
f5e87a1d 391 0x0000fffc, /* dst_mask */
b34976b6 392 FALSE), /* pcrel_offset */
5bd4f169
AM
393
394 /* An absolute 16 bit branch, for which bit 10 should be set to
395 indicate that the branch is expected to be taken. The lower two
396 bits must be zero. */
397 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
398 0, /* rightshift */
399 2, /* size (0 = byte, 1 = short, 2 = long) */
400 16, /* bitsize */
b34976b6 401 FALSE, /* pc_relative */
5bd4f169 402 0, /* bitpos */
b80eed39 403 complain_overflow_signed, /* complain_on_overflow */
805fc799 404 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 405 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 406 FALSE, /* partial_inplace */
d006db6c 407 0, /* src_mask */
f5e87a1d 408 0x0000fffc, /* dst_mask */
b34976b6 409 FALSE), /* pcrel_offset */
5bd4f169
AM
410
411 /* An absolute 16 bit branch, for which bit 10 should be set to
412 indicate that the branch is not expected to be taken. The lower
413 two bits must be zero. */
414 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
415 0, /* rightshift */
416 2, /* size (0 = byte, 1 = short, 2 = long) */
417 16, /* bitsize */
b34976b6 418 FALSE, /* pc_relative */
5bd4f169 419 0, /* bitpos */
b80eed39 420 complain_overflow_signed, /* complain_on_overflow */
805fc799 421 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 422 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 423 FALSE, /* partial_inplace */
d006db6c 424 0, /* src_mask */
f5e87a1d 425 0x0000fffc, /* dst_mask */
b34976b6 426 FALSE), /* pcrel_offset */
5bd4f169
AM
427
428 /* A relative 26 bit branch; the lower two bits must be zero. */
429 HOWTO (R_PPC64_REL24, /* type */
430 0, /* rightshift */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
432 26, /* bitsize */
b34976b6 433 TRUE, /* pc_relative */
5bd4f169
AM
434 0, /* bitpos */
435 complain_overflow_signed, /* complain_on_overflow */
2441e016 436 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 437 "R_PPC64_REL24", /* name */
b34976b6 438 FALSE, /* partial_inplace */
d006db6c 439 0, /* src_mask */
f5e87a1d 440 0x03fffffc, /* dst_mask */
b34976b6 441 TRUE), /* pcrel_offset */
5bd4f169
AM
442
443 /* A relative 16 bit branch; the lower two bits must be zero. */
444 HOWTO (R_PPC64_REL14, /* type */
445 0, /* rightshift */
446 2, /* size (0 = byte, 1 = short, 2 = long) */
447 16, /* bitsize */
b34976b6 448 TRUE, /* pc_relative */
5bd4f169
AM
449 0, /* bitpos */
450 complain_overflow_signed, /* complain_on_overflow */
2441e016 451 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 452 "R_PPC64_REL14", /* name */
b34976b6 453 FALSE, /* partial_inplace */
d006db6c 454 0, /* src_mask */
f5e87a1d 455 0x0000fffc, /* dst_mask */
b34976b6 456 TRUE), /* pcrel_offset */
5bd4f169
AM
457
458 /* A relative 16 bit branch. Bit 10 should be set to indicate that
459 the branch is expected to be taken. The lower two bits must be
460 zero. */
461 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
462 0, /* rightshift */
463 2, /* size (0 = byte, 1 = short, 2 = long) */
464 16, /* bitsize */
b34976b6 465 TRUE, /* pc_relative */
5bd4f169
AM
466 0, /* bitpos */
467 complain_overflow_signed, /* complain_on_overflow */
805fc799 468 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 469 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 470 FALSE, /* partial_inplace */
d006db6c 471 0, /* src_mask */
f5e87a1d 472 0x0000fffc, /* dst_mask */
b34976b6 473 TRUE), /* pcrel_offset */
5bd4f169
AM
474
475 /* A relative 16 bit branch. Bit 10 should be set to indicate that
476 the branch is not expected to be taken. The lower two bits must
477 be zero. */
478 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
479 0, /* rightshift */
480 2, /* size (0 = byte, 1 = short, 2 = long) */
481 16, /* bitsize */
b34976b6 482 TRUE, /* pc_relative */
5bd4f169
AM
483 0, /* bitpos */
484 complain_overflow_signed, /* complain_on_overflow */
805fc799 485 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 486 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 487 FALSE, /* partial_inplace */
d006db6c 488 0, /* src_mask */
f5e87a1d 489 0x0000fffc, /* dst_mask */
b34976b6 490 TRUE), /* pcrel_offset */
5bd4f169
AM
491
492 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
493 symbol. */
494 HOWTO (R_PPC64_GOT16, /* type */
495 0, /* rightshift */
496 1, /* size (0 = byte, 1 = short, 2 = long) */
497 16, /* bitsize */
b34976b6 498 FALSE, /* pc_relative */
5bd4f169
AM
499 0, /* bitpos */
500 complain_overflow_signed, /* complain_on_overflow */
805fc799 501 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 502 "R_PPC64_GOT16", /* name */
b34976b6 503 FALSE, /* partial_inplace */
5bd4f169
AM
504 0, /* src_mask */
505 0xffff, /* dst_mask */
b34976b6 506 FALSE), /* pcrel_offset */
5bd4f169
AM
507
508 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
509 the symbol. */
510 HOWTO (R_PPC64_GOT16_LO, /* type */
511 0, /* rightshift */
512 1, /* size (0 = byte, 1 = short, 2 = long) */
513 16, /* bitsize */
b34976b6 514 FALSE, /* pc_relative */
5bd4f169
AM
515 0, /* bitpos */
516 complain_overflow_dont, /* complain_on_overflow */
805fc799 517 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 518 "R_PPC64_GOT16_LO", /* name */
b34976b6 519 FALSE, /* partial_inplace */
5bd4f169
AM
520 0, /* src_mask */
521 0xffff, /* dst_mask */
b34976b6 522 FALSE), /* pcrel_offset */
5bd4f169
AM
523
524 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
525 the symbol. */
526 HOWTO (R_PPC64_GOT16_HI, /* type */
527 16, /* rightshift */
528 1, /* size (0 = byte, 1 = short, 2 = long) */
529 16, /* bitsize */
b34976b6 530 FALSE, /* pc_relative */
5bd4f169 531 0, /* bitpos */
f9c6b907 532 complain_overflow_signed,/* complain_on_overflow */
805fc799 533 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 534 "R_PPC64_GOT16_HI", /* name */
b34976b6 535 FALSE, /* partial_inplace */
5bd4f169
AM
536 0, /* src_mask */
537 0xffff, /* dst_mask */
b34976b6 538 FALSE), /* pcrel_offset */
5bd4f169
AM
539
540 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
541 the symbol. */
542 HOWTO (R_PPC64_GOT16_HA, /* type */
543 16, /* rightshift */
544 1, /* size (0 = byte, 1 = short, 2 = long) */
545 16, /* bitsize */
b34976b6 546 FALSE, /* pc_relative */
5bd4f169 547 0, /* bitpos */
f9c6b907 548 complain_overflow_signed,/* complain_on_overflow */
805fc799 549 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 550 "R_PPC64_GOT16_HA", /* name */
b34976b6 551 FALSE, /* partial_inplace */
5bd4f169
AM
552 0, /* src_mask */
553 0xffff, /* dst_mask */
b34976b6 554 FALSE), /* pcrel_offset */
5bd4f169
AM
555
556 /* This is used only by the dynamic linker. The symbol should exist
557 both in the object being run and in some shared library. The
558 dynamic linker copies the data addressed by the symbol from the
559 shared library into the object, because the object being
560 run has to have the data at some particular address. */
561 HOWTO (R_PPC64_COPY, /* type */
562 0, /* rightshift */
f5e87a1d
AM
563 0, /* this one is variable size */
564 0, /* bitsize */
b34976b6 565 FALSE, /* pc_relative */
5bd4f169 566 0, /* bitpos */
f5e87a1d
AM
567 complain_overflow_dont, /* complain_on_overflow */
568 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 569 "R_PPC64_COPY", /* name */
b34976b6 570 FALSE, /* partial_inplace */
5bd4f169
AM
571 0, /* src_mask */
572 0, /* dst_mask */
b34976b6 573 FALSE), /* pcrel_offset */
5bd4f169
AM
574
575 /* Like R_PPC64_ADDR64, but used when setting global offset table
576 entries. */
577 HOWTO (R_PPC64_GLOB_DAT, /* type */
578 0, /* rightshift */
579 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
580 64, /* bitsize */
b34976b6 581 FALSE, /* pc_relative */
5bd4f169
AM
582 0, /* bitpos */
583 complain_overflow_dont, /* complain_on_overflow */
805fc799 584 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 585 "R_PPC64_GLOB_DAT", /* name */
b34976b6 586 FALSE, /* partial_inplace */
5bd4f169 587 0, /* src_mask */
f5e87a1d 588 ONES (64), /* dst_mask */
b34976b6 589 FALSE), /* pcrel_offset */
5bd4f169
AM
590
591 /* Created by the link editor. Marks a procedure linkage table
592 entry for a symbol. */
593 HOWTO (R_PPC64_JMP_SLOT, /* type */
594 0, /* rightshift */
595 0, /* size (0 = byte, 1 = short, 2 = long) */
596 0, /* bitsize */
b34976b6 597 FALSE, /* pc_relative */
5bd4f169
AM
598 0, /* bitpos */
599 complain_overflow_dont, /* complain_on_overflow */
805fc799 600 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 601 "R_PPC64_JMP_SLOT", /* name */
b34976b6 602 FALSE, /* partial_inplace */
5bd4f169
AM
603 0, /* src_mask */
604 0, /* dst_mask */
b34976b6 605 FALSE), /* pcrel_offset */
5bd4f169
AM
606
607 /* Used only by the dynamic linker. When the object is run, this
608 doubleword64 is set to the load address of the object, plus the
609 addend. */
610 HOWTO (R_PPC64_RELATIVE, /* type */
611 0, /* rightshift */
612 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
613 64, /* bitsize */
b34976b6 614 FALSE, /* pc_relative */
5bd4f169
AM
615 0, /* bitpos */
616 complain_overflow_dont, /* complain_on_overflow */
617 bfd_elf_generic_reloc, /* special_function */
618 "R_PPC64_RELATIVE", /* name */
b34976b6 619 FALSE, /* partial_inplace */
5bd4f169 620 0, /* src_mask */
f5e87a1d 621 ONES (64), /* dst_mask */
b34976b6 622 FALSE), /* pcrel_offset */
5bd4f169
AM
623
624 /* Like R_PPC64_ADDR32, but may be unaligned. */
625 HOWTO (R_PPC64_UADDR32, /* type */
626 0, /* rightshift */
627 2, /* size (0 = byte, 1 = short, 2 = long) */
628 32, /* bitsize */
b34976b6 629 FALSE, /* pc_relative */
5bd4f169
AM
630 0, /* bitpos */
631 complain_overflow_bitfield, /* complain_on_overflow */
632 bfd_elf_generic_reloc, /* special_function */
633 "R_PPC64_UADDR32", /* name */
b34976b6 634 FALSE, /* partial_inplace */
5bd4f169
AM
635 0, /* src_mask */
636 0xffffffff, /* dst_mask */
b34976b6 637 FALSE), /* pcrel_offset */
5bd4f169
AM
638
639 /* Like R_PPC64_ADDR16, but may be unaligned. */
640 HOWTO (R_PPC64_UADDR16, /* type */
641 0, /* rightshift */
642 1, /* size (0 = byte, 1 = short, 2 = long) */
643 16, /* bitsize */
b34976b6 644 FALSE, /* pc_relative */
5bd4f169
AM
645 0, /* bitpos */
646 complain_overflow_bitfield, /* complain_on_overflow */
647 bfd_elf_generic_reloc, /* special_function */
648 "R_PPC64_UADDR16", /* name */
b34976b6 649 FALSE, /* partial_inplace */
5bd4f169
AM
650 0, /* src_mask */
651 0xffff, /* dst_mask */
b34976b6 652 FALSE), /* pcrel_offset */
5bd4f169
AM
653
654 /* 32-bit PC relative. */
655 HOWTO (R_PPC64_REL32, /* type */
656 0, /* rightshift */
657 2, /* size (0 = byte, 1 = short, 2 = long) */
658 32, /* bitsize */
b34976b6 659 TRUE, /* pc_relative */
5bd4f169 660 0, /* bitpos */
5bd4f169
AM
661 complain_overflow_signed, /* complain_on_overflow */
662 bfd_elf_generic_reloc, /* special_function */
663 "R_PPC64_REL32", /* name */
b34976b6 664 FALSE, /* partial_inplace */
5bd4f169
AM
665 0, /* src_mask */
666 0xffffffff, /* dst_mask */
b34976b6 667 TRUE), /* pcrel_offset */
5bd4f169 668
10ed1bba 669 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
670 HOWTO (R_PPC64_PLT32, /* type */
671 0, /* rightshift */
672 2, /* size (0 = byte, 1 = short, 2 = long) */
673 32, /* bitsize */
b34976b6 674 FALSE, /* pc_relative */
5bd4f169
AM
675 0, /* bitpos */
676 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 677 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 678 "R_PPC64_PLT32", /* name */
b34976b6 679 FALSE, /* partial_inplace */
5bd4f169 680 0, /* src_mask */
f5e87a1d 681 0xffffffff, /* dst_mask */
b34976b6 682 FALSE), /* pcrel_offset */
5bd4f169
AM
683
684 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
685 FIXME: R_PPC64_PLTREL32 not supported. */
686 HOWTO (R_PPC64_PLTREL32, /* type */
687 0, /* rightshift */
688 2, /* size (0 = byte, 1 = short, 2 = long) */
689 32, /* bitsize */
b34976b6 690 TRUE, /* pc_relative */
5bd4f169
AM
691 0, /* bitpos */
692 complain_overflow_signed, /* complain_on_overflow */
3ce51288 693 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 694 "R_PPC64_PLTREL32", /* name */
b34976b6 695 FALSE, /* partial_inplace */
5bd4f169 696 0, /* src_mask */
f5e87a1d 697 0xffffffff, /* dst_mask */
b34976b6 698 TRUE), /* pcrel_offset */
5bd4f169
AM
699
700 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
701 the symbol. */
702 HOWTO (R_PPC64_PLT16_LO, /* type */
703 0, /* rightshift */
704 1, /* size (0 = byte, 1 = short, 2 = long) */
705 16, /* bitsize */
b34976b6 706 FALSE, /* pc_relative */
5bd4f169
AM
707 0, /* bitpos */
708 complain_overflow_dont, /* complain_on_overflow */
805fc799 709 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 710 "R_PPC64_PLT16_LO", /* name */
b34976b6 711 FALSE, /* partial_inplace */
5bd4f169
AM
712 0, /* src_mask */
713 0xffff, /* dst_mask */
b34976b6 714 FALSE), /* pcrel_offset */
5bd4f169
AM
715
716 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
717 the symbol. */
718 HOWTO (R_PPC64_PLT16_HI, /* type */
719 16, /* rightshift */
720 1, /* size (0 = byte, 1 = short, 2 = long) */
721 16, /* bitsize */
b34976b6 722 FALSE, /* pc_relative */
5bd4f169 723 0, /* bitpos */
f9c6b907 724 complain_overflow_signed, /* complain_on_overflow */
805fc799 725 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 726 "R_PPC64_PLT16_HI", /* name */
b34976b6 727 FALSE, /* partial_inplace */
5bd4f169
AM
728 0, /* src_mask */
729 0xffff, /* dst_mask */
b34976b6 730 FALSE), /* pcrel_offset */
5bd4f169
AM
731
732 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
733 the symbol. */
734 HOWTO (R_PPC64_PLT16_HA, /* type */
735 16, /* rightshift */
736 1, /* size (0 = byte, 1 = short, 2 = long) */
737 16, /* bitsize */
b34976b6 738 FALSE, /* pc_relative */
5bd4f169 739 0, /* bitpos */
f9c6b907 740 complain_overflow_signed, /* complain_on_overflow */
805fc799 741 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 742 "R_PPC64_PLT16_HA", /* name */
b34976b6 743 FALSE, /* partial_inplace */
5bd4f169
AM
744 0, /* src_mask */
745 0xffff, /* dst_mask */
b34976b6 746 FALSE), /* pcrel_offset */
5bd4f169 747
c061c2d8 748 /* 16-bit section relative relocation. */
5bd4f169
AM
749 HOWTO (R_PPC64_SECTOFF, /* type */
750 0, /* rightshift */
c061c2d8
AM
751 1, /* size (0 = byte, 1 = short, 2 = long) */
752 16, /* bitsize */
b34976b6 753 FALSE, /* pc_relative */
5bd4f169 754 0, /* bitpos */
b80eed39 755 complain_overflow_signed, /* complain_on_overflow */
805fc799 756 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 757 "R_PPC64_SECTOFF", /* name */
b34976b6 758 FALSE, /* partial_inplace */
5bd4f169 759 0, /* src_mask */
c061c2d8 760 0xffff, /* dst_mask */
b34976b6 761 FALSE), /* pcrel_offset */
5bd4f169 762
c061c2d8 763 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
764 HOWTO (R_PPC64_SECTOFF_LO, /* type */
765 0, /* rightshift */
766 1, /* size (0 = byte, 1 = short, 2 = long) */
767 16, /* bitsize */
b34976b6 768 FALSE, /* pc_relative */
5bd4f169
AM
769 0, /* bitpos */
770 complain_overflow_dont, /* complain_on_overflow */
805fc799 771 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 772 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 773 FALSE, /* partial_inplace */
5bd4f169
AM
774 0, /* src_mask */
775 0xffff, /* dst_mask */
b34976b6 776 FALSE), /* pcrel_offset */
5bd4f169
AM
777
778 /* 16-bit upper half section relative relocation. */
779 HOWTO (R_PPC64_SECTOFF_HI, /* type */
780 16, /* rightshift */
781 1, /* size (0 = byte, 1 = short, 2 = long) */
782 16, /* bitsize */
b34976b6 783 FALSE, /* pc_relative */
5bd4f169 784 0, /* bitpos */
f9c6b907 785 complain_overflow_signed, /* complain_on_overflow */
805fc799 786 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 787 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 788 FALSE, /* partial_inplace */
5bd4f169
AM
789 0, /* src_mask */
790 0xffff, /* dst_mask */
b34976b6 791 FALSE), /* pcrel_offset */
5bd4f169
AM
792
793 /* 16-bit upper half adjusted section relative relocation. */
794 HOWTO (R_PPC64_SECTOFF_HA, /* type */
795 16, /* rightshift */
796 1, /* size (0 = byte, 1 = short, 2 = long) */
797 16, /* bitsize */
b34976b6 798 FALSE, /* pc_relative */
5bd4f169 799 0, /* bitpos */
f9c6b907 800 complain_overflow_signed, /* complain_on_overflow */
805fc799 801 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 802 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 803 FALSE, /* partial_inplace */
5bd4f169
AM
804 0, /* src_mask */
805 0xffff, /* dst_mask */
b34976b6 806 FALSE), /* pcrel_offset */
5bd4f169 807
04c9666a
AM
808 /* Like R_PPC64_REL24 without touching the two least significant bits. */
809 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
810 2, /* rightshift */
811 2, /* size (0 = byte, 1 = short, 2 = long) */
812 30, /* bitsize */
b34976b6 813 TRUE, /* pc_relative */
5bd4f169
AM
814 0, /* bitpos */
815 complain_overflow_dont, /* complain_on_overflow */
816 bfd_elf_generic_reloc, /* special_function */
04c9666a 817 "R_PPC64_REL30", /* name */
b34976b6 818 FALSE, /* partial_inplace */
d006db6c 819 0, /* src_mask */
5bd4f169 820 0xfffffffc, /* dst_mask */
b34976b6 821 TRUE), /* pcrel_offset */
5bd4f169
AM
822
823 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
824
825 /* A standard 64-bit relocation. */
826 HOWTO (R_PPC64_ADDR64, /* type */
827 0, /* rightshift */
828 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
829 64, /* bitsize */
b34976b6 830 FALSE, /* pc_relative */
5bd4f169
AM
831 0, /* bitpos */
832 complain_overflow_dont, /* complain_on_overflow */
833 bfd_elf_generic_reloc, /* special_function */
834 "R_PPC64_ADDR64", /* name */
b34976b6 835 FALSE, /* partial_inplace */
5bd4f169 836 0, /* src_mask */
f5e87a1d 837 ONES (64), /* dst_mask */
b34976b6 838 FALSE), /* pcrel_offset */
5bd4f169
AM
839
840 /* The bits 32-47 of an address. */
841 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
842 32, /* rightshift */
843 1, /* size (0 = byte, 1 = short, 2 = long) */
844 16, /* bitsize */
b34976b6 845 FALSE, /* pc_relative */
5bd4f169
AM
846 0, /* bitpos */
847 complain_overflow_dont, /* complain_on_overflow */
848 bfd_elf_generic_reloc, /* special_function */
849 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 850 FALSE, /* partial_inplace */
5bd4f169
AM
851 0, /* src_mask */
852 0xffff, /* dst_mask */
b34976b6 853 FALSE), /* pcrel_offset */
5bd4f169
AM
854
855 /* The bits 32-47 of an address, plus 1 if the contents of the low
856 16 bits, treated as a signed number, is negative. */
857 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
858 32, /* rightshift */
859 1, /* size (0 = byte, 1 = short, 2 = long) */
860 16, /* bitsize */
b34976b6 861 FALSE, /* pc_relative */
5bd4f169
AM
862 0, /* bitpos */
863 complain_overflow_dont, /* complain_on_overflow */
805fc799 864 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 865 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 866 FALSE, /* partial_inplace */
5bd4f169
AM
867 0, /* src_mask */
868 0xffff, /* dst_mask */
b34976b6 869 FALSE), /* pcrel_offset */
5bd4f169
AM
870
871 /* The bits 48-63 of an address. */
872 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
873 48, /* rightshift */
874 1, /* size (0 = byte, 1 = short, 2 = long) */
875 16, /* bitsize */
b34976b6 876 FALSE, /* pc_relative */
5bd4f169
AM
877 0, /* bitpos */
878 complain_overflow_dont, /* complain_on_overflow */
879 bfd_elf_generic_reloc, /* special_function */
880 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 881 FALSE, /* partial_inplace */
5bd4f169
AM
882 0, /* src_mask */
883 0xffff, /* dst_mask */
b34976b6 884 FALSE), /* pcrel_offset */
5bd4f169
AM
885
886 /* The bits 48-63 of an address, plus 1 if the contents of the low
887 16 bits, treated as a signed number, is negative. */
888 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
889 48, /* rightshift */
890 1, /* size (0 = byte, 1 = short, 2 = long) */
891 16, /* bitsize */
b34976b6 892 FALSE, /* pc_relative */
5bd4f169
AM
893 0, /* bitpos */
894 complain_overflow_dont, /* complain_on_overflow */
805fc799 895 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 896 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 897 FALSE, /* partial_inplace */
5bd4f169
AM
898 0, /* src_mask */
899 0xffff, /* dst_mask */
b34976b6 900 FALSE), /* pcrel_offset */
5bd4f169
AM
901
902 /* Like ADDR64, but may be unaligned. */
903 HOWTO (R_PPC64_UADDR64, /* type */
904 0, /* rightshift */
905 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
906 64, /* bitsize */
b34976b6 907 FALSE, /* pc_relative */
5bd4f169
AM
908 0, /* bitpos */
909 complain_overflow_dont, /* complain_on_overflow */
910 bfd_elf_generic_reloc, /* special_function */
911 "R_PPC64_UADDR64", /* name */
b34976b6 912 FALSE, /* partial_inplace */
5bd4f169 913 0, /* src_mask */
f5e87a1d 914 ONES (64), /* dst_mask */
b34976b6 915 FALSE), /* pcrel_offset */
5bd4f169
AM
916
917 /* 64-bit relative relocation. */
918 HOWTO (R_PPC64_REL64, /* type */
919 0, /* rightshift */
920 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
921 64, /* bitsize */
b34976b6 922 TRUE, /* pc_relative */
5bd4f169
AM
923 0, /* bitpos */
924 complain_overflow_dont, /* complain_on_overflow */
925 bfd_elf_generic_reloc, /* special_function */
926 "R_PPC64_REL64", /* name */
b34976b6 927 FALSE, /* partial_inplace */
5bd4f169 928 0, /* src_mask */
f5e87a1d 929 ONES (64), /* dst_mask */
b34976b6 930 TRUE), /* pcrel_offset */
5bd4f169 931
cedb70c5 932 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
933 HOWTO (R_PPC64_PLT64, /* type */
934 0, /* rightshift */
935 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
936 64, /* bitsize */
b34976b6 937 FALSE, /* pc_relative */
5bd4f169
AM
938 0, /* bitpos */
939 complain_overflow_dont, /* complain_on_overflow */
805fc799 940 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 941 "R_PPC64_PLT64", /* name */
b34976b6 942 FALSE, /* partial_inplace */
5bd4f169 943 0, /* src_mask */
f5e87a1d 944 ONES (64), /* dst_mask */
b34976b6 945 FALSE), /* pcrel_offset */
5bd4f169
AM
946
947 /* 64-bit PC relative relocation to the symbol's procedure linkage
948 table. */
949 /* FIXME: R_PPC64_PLTREL64 not supported. */
950 HOWTO (R_PPC64_PLTREL64, /* type */
951 0, /* rightshift */
952 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
953 64, /* bitsize */
b34976b6 954 TRUE, /* pc_relative */
5bd4f169
AM
955 0, /* bitpos */
956 complain_overflow_dont, /* complain_on_overflow */
805fc799 957 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 958 "R_PPC64_PLTREL64", /* name */
b34976b6 959 FALSE, /* partial_inplace */
5bd4f169 960 0, /* src_mask */
f5e87a1d 961 ONES (64), /* dst_mask */
b34976b6 962 TRUE), /* pcrel_offset */
5bd4f169
AM
963
964 /* 16 bit TOC-relative relocation. */
965
966 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
967 HOWTO (R_PPC64_TOC16, /* type */
968 0, /* rightshift */
969 1, /* size (0 = byte, 1 = short, 2 = long) */
970 16, /* bitsize */
b34976b6 971 FALSE, /* pc_relative */
5bd4f169
AM
972 0, /* bitpos */
973 complain_overflow_signed, /* complain_on_overflow */
805fc799 974 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 975 "R_PPC64_TOC16", /* name */
b34976b6 976 FALSE, /* partial_inplace */
5bd4f169
AM
977 0, /* src_mask */
978 0xffff, /* dst_mask */
b34976b6 979 FALSE), /* pcrel_offset */
5bd4f169
AM
980
981 /* 16 bit TOC-relative relocation without overflow. */
982
983 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
984 HOWTO (R_PPC64_TOC16_LO, /* type */
985 0, /* rightshift */
986 1, /* size (0 = byte, 1 = short, 2 = long) */
987 16, /* bitsize */
b34976b6 988 FALSE, /* pc_relative */
5bd4f169
AM
989 0, /* bitpos */
990 complain_overflow_dont, /* complain_on_overflow */
805fc799 991 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 992 "R_PPC64_TOC16_LO", /* name */
b34976b6 993 FALSE, /* partial_inplace */
5bd4f169
AM
994 0, /* src_mask */
995 0xffff, /* dst_mask */
b34976b6 996 FALSE), /* pcrel_offset */
5bd4f169
AM
997
998 /* 16 bit TOC-relative relocation, high 16 bits. */
999
1000 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1001 HOWTO (R_PPC64_TOC16_HI, /* type */
1002 16, /* rightshift */
1003 1, /* size (0 = byte, 1 = short, 2 = long) */
1004 16, /* bitsize */
b34976b6 1005 FALSE, /* pc_relative */
5bd4f169 1006 0, /* bitpos */
f9c6b907 1007 complain_overflow_signed, /* complain_on_overflow */
805fc799 1008 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 1009 "R_PPC64_TOC16_HI", /* name */
b34976b6 1010 FALSE, /* partial_inplace */
5bd4f169
AM
1011 0, /* src_mask */
1012 0xffff, /* dst_mask */
b34976b6 1013 FALSE), /* pcrel_offset */
5bd4f169
AM
1014
1015 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1016 contents of the low 16 bits, treated as a signed number, is
1017 negative. */
1018
1019 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1020 HOWTO (R_PPC64_TOC16_HA, /* type */
1021 16, /* rightshift */
1022 1, /* size (0 = byte, 1 = short, 2 = long) */
1023 16, /* bitsize */
b34976b6 1024 FALSE, /* pc_relative */
5bd4f169 1025 0, /* bitpos */
f9c6b907 1026 complain_overflow_signed, /* complain_on_overflow */
805fc799 1027 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 1028 "R_PPC64_TOC16_HA", /* name */
b34976b6 1029 FALSE, /* partial_inplace */
5bd4f169
AM
1030 0, /* src_mask */
1031 0xffff, /* dst_mask */
b34976b6 1032 FALSE), /* pcrel_offset */
5bd4f169
AM
1033
1034 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1035
1036 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1037 HOWTO (R_PPC64_TOC, /* type */
1038 0, /* rightshift */
1039 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1040 64, /* bitsize */
b34976b6 1041 FALSE, /* pc_relative */
5bd4f169 1042 0, /* bitpos */
b80eed39 1043 complain_overflow_dont, /* complain_on_overflow */
805fc799 1044 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1045 "R_PPC64_TOC", /* name */
b34976b6 1046 FALSE, /* partial_inplace */
5bd4f169 1047 0, /* src_mask */
f5e87a1d 1048 ONES (64), /* dst_mask */
b34976b6 1049 FALSE), /* pcrel_offset */
5bd4f169
AM
1050
1051 /* Like R_PPC64_GOT16, but also informs the link editor that the
1052 value to relocate may (!) refer to a PLT entry which the link
1053 editor (a) may replace with the symbol value. If the link editor
1054 is unable to fully resolve the symbol, it may (b) create a PLT
1055 entry and store the address to the new PLT entry in the GOT.
1056 This permits lazy resolution of function symbols at run time.
1057 The link editor may also skip all of this and just (c) emit a
1058 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1059 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1060 HOWTO (R_PPC64_PLTGOT16, /* type */
1061 0, /* rightshift */
1062 1, /* size (0 = byte, 1 = short, 2 = long) */
1063 16, /* bitsize */
b34976b6 1064 FALSE, /* pc_relative */
5bd4f169
AM
1065 0, /* bitpos */
1066 complain_overflow_signed, /* complain_on_overflow */
805fc799 1067 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1068 "R_PPC64_PLTGOT16", /* name */
1069 FALSE, /* partial_inplace */
1070 0, /* src_mask */
1071 0xffff, /* dst_mask */
1072 FALSE), /* pcrel_offset */
1073
1074 /* Like R_PPC64_PLTGOT16, but without overflow. */
1075 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1076 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1077 0, /* rightshift */
1078 1, /* size (0 = byte, 1 = short, 2 = long) */
1079 16, /* bitsize */
1080 FALSE, /* pc_relative */
1081 0, /* bitpos */
1082 complain_overflow_dont, /* complain_on_overflow */
1083 ppc64_elf_unhandled_reloc, /* special_function */
1084 "R_PPC64_PLTGOT16_LO", /* name */
1085 FALSE, /* partial_inplace */
1086 0, /* src_mask */
1087 0xffff, /* dst_mask */
1088 FALSE), /* pcrel_offset */
1089
1090 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1091 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1092 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1093 16, /* rightshift */
1094 1, /* size (0 = byte, 1 = short, 2 = long) */
1095 16, /* bitsize */
1096 FALSE, /* pc_relative */
1097 0, /* bitpos */
f9c6b907 1098 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1099 ppc64_elf_unhandled_reloc, /* special_function */
1100 "R_PPC64_PLTGOT16_HI", /* name */
1101 FALSE, /* partial_inplace */
1102 0, /* src_mask */
1103 0xffff, /* dst_mask */
1104 FALSE), /* pcrel_offset */
1105
1106 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1107 1 if the contents of the low 16 bits, treated as a signed number,
1108 is negative. */
1109 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1110 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1111 16, /* rightshift */
1112 1, /* size (0 = byte, 1 = short, 2 = long) */
1113 16, /* bitsize */
1114 FALSE, /* pc_relative */
1115 0, /* bitpos */
f9c6b907 1116 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1117 ppc64_elf_unhandled_reloc, /* special_function */
1118 "R_PPC64_PLTGOT16_HA", /* name */
1119 FALSE, /* partial_inplace */
1120 0, /* src_mask */
1121 0xffff, /* dst_mask */
1122 FALSE), /* pcrel_offset */
1123
1124 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1125 HOWTO (R_PPC64_ADDR16_DS, /* type */
1126 0, /* rightshift */
1127 1, /* size (0 = byte, 1 = short, 2 = long) */
1128 16, /* bitsize */
1129 FALSE, /* pc_relative */
1130 0, /* bitpos */
b80eed39 1131 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1132 bfd_elf_generic_reloc, /* special_function */
1133 "R_PPC64_ADDR16_DS", /* name */
1134 FALSE, /* partial_inplace */
1135 0, /* src_mask */
1136 0xfffc, /* dst_mask */
1137 FALSE), /* pcrel_offset */
1138
1139 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1140 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1141 0, /* rightshift */
1142 1, /* size (0 = byte, 1 = short, 2 = long) */
1143 16, /* bitsize */
1144 FALSE, /* pc_relative */
1145 0, /* bitpos */
1146 complain_overflow_dont,/* complain_on_overflow */
1147 bfd_elf_generic_reloc, /* special_function */
1148 "R_PPC64_ADDR16_LO_DS",/* name */
1149 FALSE, /* partial_inplace */
1150 0, /* src_mask */
1151 0xfffc, /* dst_mask */
1152 FALSE), /* pcrel_offset */
1153
1154 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1155 HOWTO (R_PPC64_GOT16_DS, /* type */
1156 0, /* rightshift */
1157 1, /* size (0 = byte, 1 = short, 2 = long) */
1158 16, /* bitsize */
1159 FALSE, /* pc_relative */
1160 0, /* bitpos */
1161 complain_overflow_signed, /* complain_on_overflow */
1162 ppc64_elf_unhandled_reloc, /* special_function */
1163 "R_PPC64_GOT16_DS", /* name */
1164 FALSE, /* partial_inplace */
1165 0, /* src_mask */
1166 0xfffc, /* dst_mask */
1167 FALSE), /* pcrel_offset */
1168
1169 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1170 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1171 0, /* rightshift */
1172 1, /* size (0 = byte, 1 = short, 2 = long) */
1173 16, /* bitsize */
1174 FALSE, /* pc_relative */
1175 0, /* bitpos */
1176 complain_overflow_dont, /* complain_on_overflow */
1177 ppc64_elf_unhandled_reloc, /* special_function */
1178 "R_PPC64_GOT16_LO_DS", /* name */
1179 FALSE, /* partial_inplace */
1180 0, /* src_mask */
1181 0xfffc, /* dst_mask */
1182 FALSE), /* pcrel_offset */
1183
1184 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1185 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1186 0, /* rightshift */
1187 1, /* size (0 = byte, 1 = short, 2 = long) */
1188 16, /* bitsize */
1189 FALSE, /* pc_relative */
1190 0, /* bitpos */
1191 complain_overflow_dont, /* complain_on_overflow */
1192 ppc64_elf_unhandled_reloc, /* special_function */
1193 "R_PPC64_PLT16_LO_DS", /* name */
1194 FALSE, /* partial_inplace */
1195 0, /* src_mask */
1196 0xfffc, /* dst_mask */
1197 FALSE), /* pcrel_offset */
1198
1199 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1200 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1201 0, /* rightshift */
1202 1, /* size (0 = byte, 1 = short, 2 = long) */
1203 16, /* bitsize */
1204 FALSE, /* pc_relative */
1205 0, /* bitpos */
b80eed39 1206 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1207 ppc64_elf_sectoff_reloc, /* special_function */
1208 "R_PPC64_SECTOFF_DS", /* name */
1209 FALSE, /* partial_inplace */
1210 0, /* src_mask */
1211 0xfffc, /* dst_mask */
1212 FALSE), /* pcrel_offset */
1213
1214 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1215 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1216 0, /* rightshift */
1217 1, /* size (0 = byte, 1 = short, 2 = long) */
1218 16, /* bitsize */
1219 FALSE, /* pc_relative */
1220 0, /* bitpos */
1221 complain_overflow_dont, /* complain_on_overflow */
1222 ppc64_elf_sectoff_reloc, /* special_function */
1223 "R_PPC64_SECTOFF_LO_DS",/* name */
1224 FALSE, /* partial_inplace */
1225 0, /* src_mask */
1226 0xfffc, /* dst_mask */
1227 FALSE), /* pcrel_offset */
1228
1229 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1230 HOWTO (R_PPC64_TOC16_DS, /* type */
1231 0, /* rightshift */
1232 1, /* size (0 = byte, 1 = short, 2 = long) */
1233 16, /* bitsize */
1234 FALSE, /* pc_relative */
1235 0, /* bitpos */
1236 complain_overflow_signed, /* complain_on_overflow */
1237 ppc64_elf_toc_reloc, /* special_function */
1238 "R_PPC64_TOC16_DS", /* name */
1239 FALSE, /* partial_inplace */
1240 0, /* src_mask */
1241 0xfffc, /* dst_mask */
1242 FALSE), /* pcrel_offset */
1243
1244 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1245 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1246 0, /* rightshift */
1247 1, /* size (0 = byte, 1 = short, 2 = long) */
1248 16, /* bitsize */
1249 FALSE, /* pc_relative */
1250 0, /* bitpos */
1251 complain_overflow_dont, /* complain_on_overflow */
1252 ppc64_elf_toc_reloc, /* special_function */
1253 "R_PPC64_TOC16_LO_DS", /* name */
1254 FALSE, /* partial_inplace */
1255 0, /* src_mask */
1256 0xfffc, /* dst_mask */
1257 FALSE), /* pcrel_offset */
1258
1259 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1260 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1261 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1262 0, /* rightshift */
1263 1, /* size (0 = byte, 1 = short, 2 = long) */
1264 16, /* bitsize */
1265 FALSE, /* pc_relative */
1266 0, /* bitpos */
1267 complain_overflow_signed, /* complain_on_overflow */
1268 ppc64_elf_unhandled_reloc, /* special_function */
1269 "R_PPC64_PLTGOT16_DS", /* name */
1270 FALSE, /* partial_inplace */
1271 0, /* src_mask */
1272 0xfffc, /* dst_mask */
1273 FALSE), /* pcrel_offset */
1274
1275 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1276 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1277 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1278 0, /* rightshift */
1279 1, /* size (0 = byte, 1 = short, 2 = long) */
1280 16, /* bitsize */
1281 FALSE, /* pc_relative */
1282 0, /* bitpos */
1283 complain_overflow_dont, /* complain_on_overflow */
1284 ppc64_elf_unhandled_reloc, /* special_function */
1285 "R_PPC64_PLTGOT16_LO_DS",/* name */
1286 FALSE, /* partial_inplace */
1287 0, /* src_mask */
1288 0xfffc, /* dst_mask */
1289 FALSE), /* pcrel_offset */
1290
727fc41e 1291 /* Marker relocs for TLS. */
411e1bfb
AM
1292 HOWTO (R_PPC64_TLS,
1293 0, /* rightshift */
1294 2, /* size (0 = byte, 1 = short, 2 = long) */
1295 32, /* bitsize */
1296 FALSE, /* pc_relative */
1297 0, /* bitpos */
1298 complain_overflow_dont, /* complain_on_overflow */
1299 bfd_elf_generic_reloc, /* special_function */
1300 "R_PPC64_TLS", /* name */
1301 FALSE, /* partial_inplace */
1302 0, /* src_mask */
1303 0, /* dst_mask */
1304 FALSE), /* pcrel_offset */
1305
727fc41e
AM
1306 HOWTO (R_PPC64_TLSGD,
1307 0, /* rightshift */
1308 2, /* size (0 = byte, 1 = short, 2 = long) */
1309 32, /* bitsize */
1310 FALSE, /* pc_relative */
1311 0, /* bitpos */
1312 complain_overflow_dont, /* complain_on_overflow */
1313 bfd_elf_generic_reloc, /* special_function */
1314 "R_PPC64_TLSGD", /* name */
1315 FALSE, /* partial_inplace */
1316 0, /* src_mask */
1317 0, /* dst_mask */
1318 FALSE), /* pcrel_offset */
1319
1320 HOWTO (R_PPC64_TLSLD,
1321 0, /* rightshift */
1322 2, /* size (0 = byte, 1 = short, 2 = long) */
1323 32, /* bitsize */
1324 FALSE, /* pc_relative */
1325 0, /* bitpos */
1326 complain_overflow_dont, /* complain_on_overflow */
1327 bfd_elf_generic_reloc, /* special_function */
1328 "R_PPC64_TLSLD", /* name */
1329 FALSE, /* partial_inplace */
1330 0, /* src_mask */
1331 0, /* dst_mask */
1332 FALSE), /* pcrel_offset */
1333
3b421ab3
AM
1334 HOWTO (R_PPC64_TOCSAVE,
1335 0, /* rightshift */
1336 2, /* size (0 = byte, 1 = short, 2 = long) */
1337 32, /* bitsize */
1338 FALSE, /* pc_relative */
1339 0, /* bitpos */
1340 complain_overflow_dont, /* complain_on_overflow */
1341 bfd_elf_generic_reloc, /* special_function */
1342 "R_PPC64_TOCSAVE", /* name */
1343 FALSE, /* partial_inplace */
1344 0, /* src_mask */
1345 0, /* dst_mask */
1346 FALSE), /* pcrel_offset */
1347
411e1bfb
AM
1348 /* Computes the load module index of the load module that contains the
1349 definition of its TLS sym. */
1350 HOWTO (R_PPC64_DTPMOD64,
1351 0, /* rightshift */
1352 4, /* size (0 = byte, 1 = short, 2 = long) */
1353 64, /* bitsize */
1354 FALSE, /* pc_relative */
1355 0, /* bitpos */
1356 complain_overflow_dont, /* complain_on_overflow */
1357 ppc64_elf_unhandled_reloc, /* special_function */
1358 "R_PPC64_DTPMOD64", /* name */
1359 FALSE, /* partial_inplace */
1360 0, /* src_mask */
1361 ONES (64), /* dst_mask */
1362 FALSE), /* pcrel_offset */
1363
1364 /* Computes a dtv-relative displacement, the difference between the value
1365 of sym+add and the base address of the thread-local storage block that
1366 contains the definition of sym, minus 0x8000. */
1367 HOWTO (R_PPC64_DTPREL64,
1368 0, /* rightshift */
1369 4, /* size (0 = byte, 1 = short, 2 = long) */
1370 64, /* bitsize */
1371 FALSE, /* pc_relative */
1372 0, /* bitpos */
1373 complain_overflow_dont, /* complain_on_overflow */
1374 ppc64_elf_unhandled_reloc, /* special_function */
1375 "R_PPC64_DTPREL64", /* name */
1376 FALSE, /* partial_inplace */
1377 0, /* src_mask */
1378 ONES (64), /* dst_mask */
1379 FALSE), /* pcrel_offset */
1380
1381 /* A 16 bit dtprel reloc. */
1382 HOWTO (R_PPC64_DTPREL16,
1383 0, /* rightshift */
1384 1, /* size (0 = byte, 1 = short, 2 = long) */
1385 16, /* bitsize */
1386 FALSE, /* pc_relative */
1387 0, /* bitpos */
1388 complain_overflow_signed, /* complain_on_overflow */
1389 ppc64_elf_unhandled_reloc, /* special_function */
1390 "R_PPC64_DTPREL16", /* name */
1391 FALSE, /* partial_inplace */
1392 0, /* src_mask */
1393 0xffff, /* dst_mask */
1394 FALSE), /* pcrel_offset */
1395
1396 /* Like DTPREL16, but no overflow. */
1397 HOWTO (R_PPC64_DTPREL16_LO,
1398 0, /* rightshift */
1399 1, /* size (0 = byte, 1 = short, 2 = long) */
1400 16, /* bitsize */
1401 FALSE, /* pc_relative */
1402 0, /* bitpos */
1403 complain_overflow_dont, /* complain_on_overflow */
1404 ppc64_elf_unhandled_reloc, /* special_function */
1405 "R_PPC64_DTPREL16_LO", /* name */
1406 FALSE, /* partial_inplace */
1407 0, /* src_mask */
1408 0xffff, /* dst_mask */
1409 FALSE), /* pcrel_offset */
1410
1411 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1412 HOWTO (R_PPC64_DTPREL16_HI,
1413 16, /* rightshift */
1414 1, /* size (0 = byte, 1 = short, 2 = long) */
1415 16, /* bitsize */
1416 FALSE, /* pc_relative */
1417 0, /* bitpos */
f9c6b907 1418 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1419 ppc64_elf_unhandled_reloc, /* special_function */
1420 "R_PPC64_DTPREL16_HI", /* name */
1421 FALSE, /* partial_inplace */
1422 0, /* src_mask */
1423 0xffff, /* dst_mask */
1424 FALSE), /* pcrel_offset */
1425
1426 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1427 HOWTO (R_PPC64_DTPREL16_HA,
1428 16, /* rightshift */
1429 1, /* size (0 = byte, 1 = short, 2 = long) */
1430 16, /* bitsize */
1431 FALSE, /* pc_relative */
1432 0, /* bitpos */
f9c6b907 1433 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1434 ppc64_elf_unhandled_reloc, /* special_function */
1435 "R_PPC64_DTPREL16_HA", /* name */
1436 FALSE, /* partial_inplace */
1437 0, /* src_mask */
1438 0xffff, /* dst_mask */
1439 FALSE), /* pcrel_offset */
1440
1441 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1442 HOWTO (R_PPC64_DTPREL16_HIGHER,
1443 32, /* rightshift */
1444 1, /* size (0 = byte, 1 = short, 2 = long) */
1445 16, /* bitsize */
1446 FALSE, /* pc_relative */
1447 0, /* bitpos */
1448 complain_overflow_dont, /* complain_on_overflow */
1449 ppc64_elf_unhandled_reloc, /* special_function */
1450 "R_PPC64_DTPREL16_HIGHER", /* name */
1451 FALSE, /* partial_inplace */
1452 0, /* src_mask */
1453 0xffff, /* dst_mask */
1454 FALSE), /* pcrel_offset */
1455
1456 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1457 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1458 32, /* rightshift */
1459 1, /* size (0 = byte, 1 = short, 2 = long) */
1460 16, /* bitsize */
1461 FALSE, /* pc_relative */
1462 0, /* bitpos */
1463 complain_overflow_dont, /* complain_on_overflow */
1464 ppc64_elf_unhandled_reloc, /* special_function */
1465 "R_PPC64_DTPREL16_HIGHERA", /* name */
1466 FALSE, /* partial_inplace */
1467 0, /* src_mask */
1468 0xffff, /* dst_mask */
1469 FALSE), /* pcrel_offset */
1470
1471 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1472 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1473 48, /* rightshift */
1474 1, /* size (0 = byte, 1 = short, 2 = long) */
1475 16, /* bitsize */
1476 FALSE, /* pc_relative */
1477 0, /* bitpos */
1478 complain_overflow_dont, /* complain_on_overflow */
1479 ppc64_elf_unhandled_reloc, /* special_function */
1480 "R_PPC64_DTPREL16_HIGHEST", /* name */
1481 FALSE, /* partial_inplace */
1482 0, /* src_mask */
1483 0xffff, /* dst_mask */
1484 FALSE), /* pcrel_offset */
1485
1486 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1487 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1488 48, /* rightshift */
1489 1, /* size (0 = byte, 1 = short, 2 = long) */
1490 16, /* bitsize */
1491 FALSE, /* pc_relative */
1492 0, /* bitpos */
1493 complain_overflow_dont, /* complain_on_overflow */
1494 ppc64_elf_unhandled_reloc, /* special_function */
1495 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1496 FALSE, /* partial_inplace */
1497 0, /* src_mask */
1498 0xffff, /* dst_mask */
1499 FALSE), /* pcrel_offset */
1500
1501 /* Like DTPREL16, but for insns with a DS field. */
1502 HOWTO (R_PPC64_DTPREL16_DS,
1503 0, /* rightshift */
1504 1, /* size (0 = byte, 1 = short, 2 = long) */
1505 16, /* bitsize */
1506 FALSE, /* pc_relative */
1507 0, /* bitpos */
1508 complain_overflow_signed, /* complain_on_overflow */
1509 ppc64_elf_unhandled_reloc, /* special_function */
1510 "R_PPC64_DTPREL16_DS", /* name */
1511 FALSE, /* partial_inplace */
1512 0, /* src_mask */
1513 0xfffc, /* dst_mask */
1514 FALSE), /* pcrel_offset */
1515
1516 /* Like DTPREL16_DS, but no overflow. */
1517 HOWTO (R_PPC64_DTPREL16_LO_DS,
1518 0, /* rightshift */
1519 1, /* size (0 = byte, 1 = short, 2 = long) */
1520 16, /* bitsize */
1521 FALSE, /* pc_relative */
1522 0, /* bitpos */
1523 complain_overflow_dont, /* complain_on_overflow */
1524 ppc64_elf_unhandled_reloc, /* special_function */
1525 "R_PPC64_DTPREL16_LO_DS", /* name */
1526 FALSE, /* partial_inplace */
1527 0, /* src_mask */
1528 0xfffc, /* dst_mask */
1529 FALSE), /* pcrel_offset */
1530
1531 /* Computes a tp-relative displacement, the difference between the value of
1532 sym+add and the value of the thread pointer (r13). */
1533 HOWTO (R_PPC64_TPREL64,
1534 0, /* rightshift */
1535 4, /* size (0 = byte, 1 = short, 2 = long) */
1536 64, /* bitsize */
1537 FALSE, /* pc_relative */
1538 0, /* bitpos */
1539 complain_overflow_dont, /* complain_on_overflow */
1540 ppc64_elf_unhandled_reloc, /* special_function */
1541 "R_PPC64_TPREL64", /* name */
1542 FALSE, /* partial_inplace */
1543 0, /* src_mask */
1544 ONES (64), /* dst_mask */
1545 FALSE), /* pcrel_offset */
1546
1547 /* A 16 bit tprel reloc. */
1548 HOWTO (R_PPC64_TPREL16,
1549 0, /* rightshift */
1550 1, /* size (0 = byte, 1 = short, 2 = long) */
1551 16, /* bitsize */
1552 FALSE, /* pc_relative */
1553 0, /* bitpos */
1554 complain_overflow_signed, /* complain_on_overflow */
1555 ppc64_elf_unhandled_reloc, /* special_function */
1556 "R_PPC64_TPREL16", /* name */
1557 FALSE, /* partial_inplace */
1558 0, /* src_mask */
1559 0xffff, /* dst_mask */
1560 FALSE), /* pcrel_offset */
1561
1562 /* Like TPREL16, but no overflow. */
1563 HOWTO (R_PPC64_TPREL16_LO,
1564 0, /* rightshift */
1565 1, /* size (0 = byte, 1 = short, 2 = long) */
1566 16, /* bitsize */
1567 FALSE, /* pc_relative */
1568 0, /* bitpos */
1569 complain_overflow_dont, /* complain_on_overflow */
1570 ppc64_elf_unhandled_reloc, /* special_function */
1571 "R_PPC64_TPREL16_LO", /* name */
1572 FALSE, /* partial_inplace */
1573 0, /* src_mask */
1574 0xffff, /* dst_mask */
1575 FALSE), /* pcrel_offset */
1576
1577 /* Like TPREL16_LO, but next higher group of 16 bits. */
1578 HOWTO (R_PPC64_TPREL16_HI,
1579 16, /* rightshift */
1580 1, /* size (0 = byte, 1 = short, 2 = long) */
1581 16, /* bitsize */
1582 FALSE, /* pc_relative */
1583 0, /* bitpos */
f9c6b907 1584 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1585 ppc64_elf_unhandled_reloc, /* special_function */
1586 "R_PPC64_TPREL16_HI", /* name */
1587 FALSE, /* partial_inplace */
1588 0, /* src_mask */
1589 0xffff, /* dst_mask */
1590 FALSE), /* pcrel_offset */
1591
1592 /* Like TPREL16_HI, but adjust for low 16 bits. */
1593 HOWTO (R_PPC64_TPREL16_HA,
1594 16, /* rightshift */
1595 1, /* size (0 = byte, 1 = short, 2 = long) */
1596 16, /* bitsize */
1597 FALSE, /* pc_relative */
1598 0, /* bitpos */
f9c6b907 1599 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1600 ppc64_elf_unhandled_reloc, /* special_function */
1601 "R_PPC64_TPREL16_HA", /* name */
1602 FALSE, /* partial_inplace */
1603 0, /* src_mask */
1604 0xffff, /* dst_mask */
1605 FALSE), /* pcrel_offset */
1606
1607 /* Like TPREL16_HI, but next higher group of 16 bits. */
1608 HOWTO (R_PPC64_TPREL16_HIGHER,
1609 32, /* rightshift */
1610 1, /* size (0 = byte, 1 = short, 2 = long) */
1611 16, /* bitsize */
1612 FALSE, /* pc_relative */
1613 0, /* bitpos */
1614 complain_overflow_dont, /* complain_on_overflow */
1615 ppc64_elf_unhandled_reloc, /* special_function */
1616 "R_PPC64_TPREL16_HIGHER", /* name */
1617 FALSE, /* partial_inplace */
1618 0, /* src_mask */
1619 0xffff, /* dst_mask */
1620 FALSE), /* pcrel_offset */
1621
1622 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1623 HOWTO (R_PPC64_TPREL16_HIGHERA,
1624 32, /* rightshift */
1625 1, /* size (0 = byte, 1 = short, 2 = long) */
1626 16, /* bitsize */
1627 FALSE, /* pc_relative */
1628 0, /* bitpos */
1629 complain_overflow_dont, /* complain_on_overflow */
1630 ppc64_elf_unhandled_reloc, /* special_function */
1631 "R_PPC64_TPREL16_HIGHERA", /* name */
1632 FALSE, /* partial_inplace */
1633 0, /* src_mask */
1634 0xffff, /* dst_mask */
1635 FALSE), /* pcrel_offset */
1636
1637 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1638 HOWTO (R_PPC64_TPREL16_HIGHEST,
1639 48, /* rightshift */
1640 1, /* size (0 = byte, 1 = short, 2 = long) */
1641 16, /* bitsize */
1642 FALSE, /* pc_relative */
1643 0, /* bitpos */
1644 complain_overflow_dont, /* complain_on_overflow */
1645 ppc64_elf_unhandled_reloc, /* special_function */
1646 "R_PPC64_TPREL16_HIGHEST", /* name */
1647 FALSE, /* partial_inplace */
1648 0, /* src_mask */
1649 0xffff, /* dst_mask */
1650 FALSE), /* pcrel_offset */
1651
1652 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1653 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1654 48, /* rightshift */
1655 1, /* size (0 = byte, 1 = short, 2 = long) */
1656 16, /* bitsize */
1657 FALSE, /* pc_relative */
1658 0, /* bitpos */
1659 complain_overflow_dont, /* complain_on_overflow */
1660 ppc64_elf_unhandled_reloc, /* special_function */
1661 "R_PPC64_TPREL16_HIGHESTA", /* name */
1662 FALSE, /* partial_inplace */
1663 0, /* src_mask */
1664 0xffff, /* dst_mask */
1665 FALSE), /* pcrel_offset */
1666
1667 /* Like TPREL16, but for insns with a DS field. */
1668 HOWTO (R_PPC64_TPREL16_DS,
1669 0, /* rightshift */
1670 1, /* size (0 = byte, 1 = short, 2 = long) */
1671 16, /* bitsize */
1672 FALSE, /* pc_relative */
1673 0, /* bitpos */
1674 complain_overflow_signed, /* complain_on_overflow */
1675 ppc64_elf_unhandled_reloc, /* special_function */
1676 "R_PPC64_TPREL16_DS", /* name */
1677 FALSE, /* partial_inplace */
1678 0, /* src_mask */
1679 0xfffc, /* dst_mask */
1680 FALSE), /* pcrel_offset */
1681
1682 /* Like TPREL16_DS, but no overflow. */
1683 HOWTO (R_PPC64_TPREL16_LO_DS,
1684 0, /* rightshift */
1685 1, /* size (0 = byte, 1 = short, 2 = long) */
1686 16, /* bitsize */
1687 FALSE, /* pc_relative */
1688 0, /* bitpos */
1689 complain_overflow_dont, /* complain_on_overflow */
1690 ppc64_elf_unhandled_reloc, /* special_function */
1691 "R_PPC64_TPREL16_LO_DS", /* name */
1692 FALSE, /* partial_inplace */
1693 0, /* src_mask */
1694 0xfffc, /* dst_mask */
1695 FALSE), /* pcrel_offset */
1696
1697 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1698 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1699 to the first entry relative to the TOC base (r2). */
1700 HOWTO (R_PPC64_GOT_TLSGD16,
1701 0, /* rightshift */
1702 1, /* size (0 = byte, 1 = short, 2 = long) */
1703 16, /* bitsize */
1704 FALSE, /* pc_relative */
1705 0, /* bitpos */
1706 complain_overflow_signed, /* complain_on_overflow */
1707 ppc64_elf_unhandled_reloc, /* special_function */
1708 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1709 FALSE, /* partial_inplace */
5bd4f169
AM
1710 0, /* src_mask */
1711 0xffff, /* dst_mask */
b34976b6 1712 FALSE), /* pcrel_offset */
5bd4f169 1713
411e1bfb
AM
1714 /* Like GOT_TLSGD16, but no overflow. */
1715 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1716 0, /* rightshift */
1717 1, /* size (0 = byte, 1 = short, 2 = long) */
1718 16, /* bitsize */
b34976b6 1719 FALSE, /* pc_relative */
5bd4f169
AM
1720 0, /* bitpos */
1721 complain_overflow_dont, /* complain_on_overflow */
805fc799 1722 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1723 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1724 FALSE, /* partial_inplace */
5bd4f169
AM
1725 0, /* src_mask */
1726 0xffff, /* dst_mask */
b34976b6 1727 FALSE), /* pcrel_offset */
5bd4f169 1728
411e1bfb
AM
1729 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1730 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1731 16, /* rightshift */
1732 1, /* size (0 = byte, 1 = short, 2 = long) */
1733 16, /* bitsize */
b34976b6 1734 FALSE, /* pc_relative */
5bd4f169 1735 0, /* bitpos */
f9c6b907 1736 complain_overflow_signed, /* complain_on_overflow */
805fc799 1737 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1738 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1739 FALSE, /* partial_inplace */
5bd4f169
AM
1740 0, /* src_mask */
1741 0xffff, /* dst_mask */
b34976b6 1742 FALSE), /* pcrel_offset */
5bd4f169 1743
411e1bfb
AM
1744 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1745 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1746 16, /* rightshift */
1747 1, /* size (0 = byte, 1 = short, 2 = long) */
1748 16, /* bitsize */
b34976b6 1749 FALSE, /* pc_relative */
5bd4f169 1750 0, /* bitpos */
f9c6b907 1751 complain_overflow_signed, /* complain_on_overflow */
805fc799 1752 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1753 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1754 FALSE, /* partial_inplace */
5bd4f169
AM
1755 0, /* src_mask */
1756 0xffff, /* dst_mask */
b34976b6 1757 FALSE), /* pcrel_offset */
5bd4f169 1758
411e1bfb
AM
1759 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1760 with values (sym+add)@dtpmod and zero, and computes the offset to the
1761 first entry relative to the TOC base (r2). */
1762 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1763 0, /* rightshift */
1764 1, /* size (0 = byte, 1 = short, 2 = long) */
1765 16, /* bitsize */
b34976b6 1766 FALSE, /* pc_relative */
5bd4f169 1767 0, /* bitpos */
411e1bfb
AM
1768 complain_overflow_signed, /* complain_on_overflow */
1769 ppc64_elf_unhandled_reloc, /* special_function */
1770 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1771 FALSE, /* partial_inplace */
d006db6c 1772 0, /* src_mask */
411e1bfb 1773 0xffff, /* dst_mask */
b34976b6 1774 FALSE), /* pcrel_offset */
5bd4f169 1775
411e1bfb
AM
1776 /* Like GOT_TLSLD16, but no overflow. */
1777 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1778 0, /* rightshift */
1779 1, /* size (0 = byte, 1 = short, 2 = long) */
1780 16, /* bitsize */
b34976b6 1781 FALSE, /* pc_relative */
5bd4f169 1782 0, /* bitpos */
411e1bfb
AM
1783 complain_overflow_dont, /* complain_on_overflow */
1784 ppc64_elf_unhandled_reloc, /* special_function */
1785 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1786 FALSE, /* partial_inplace */
d006db6c 1787 0, /* src_mask */
411e1bfb 1788 0xffff, /* dst_mask */
b34976b6 1789 FALSE), /* pcrel_offset */
5bd4f169 1790
411e1bfb
AM
1791 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1792 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1793 16, /* rightshift */
5bd4f169
AM
1794 1, /* size (0 = byte, 1 = short, 2 = long) */
1795 16, /* bitsize */
b34976b6 1796 FALSE, /* pc_relative */
5bd4f169 1797 0, /* bitpos */
f9c6b907 1798 complain_overflow_signed, /* complain_on_overflow */
805fc799 1799 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1800 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1801 FALSE, /* partial_inplace */
d006db6c 1802 0, /* src_mask */
411e1bfb 1803 0xffff, /* dst_mask */
b34976b6 1804 FALSE), /* pcrel_offset */
5bd4f169 1805
411e1bfb
AM
1806 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1807 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1808 16, /* rightshift */
5bd4f169
AM
1809 1, /* size (0 = byte, 1 = short, 2 = long) */
1810 16, /* bitsize */
b34976b6 1811 FALSE, /* pc_relative */
5bd4f169 1812 0, /* bitpos */
f9c6b907 1813 complain_overflow_signed, /* complain_on_overflow */
805fc799 1814 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1815 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1816 FALSE, /* partial_inplace */
d006db6c 1817 0, /* src_mask */
411e1bfb 1818 0xffff, /* dst_mask */
b34976b6 1819 FALSE), /* pcrel_offset */
5bd4f169 1820
411e1bfb
AM
1821 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1822 the offset to the entry relative to the TOC base (r2). */
1823 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1824 0, /* rightshift */
1825 1, /* size (0 = byte, 1 = short, 2 = long) */
1826 16, /* bitsize */
b34976b6 1827 FALSE, /* pc_relative */
5bd4f169 1828 0, /* bitpos */
411e1bfb 1829 complain_overflow_signed, /* complain_on_overflow */
805fc799 1830 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1831 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1832 FALSE, /* partial_inplace */
d006db6c 1833 0, /* src_mask */
5bd4f169 1834 0xfffc, /* dst_mask */
b34976b6 1835 FALSE), /* pcrel_offset */
5bd4f169 1836
411e1bfb
AM
1837 /* Like GOT_DTPREL16_DS, but no overflow. */
1838 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1839 0, /* rightshift */
c061c2d8
AM
1840 1, /* size (0 = byte, 1 = short, 2 = long) */
1841 16, /* bitsize */
b34976b6 1842 FALSE, /* pc_relative */
5bd4f169 1843 0, /* bitpos */
411e1bfb
AM
1844 complain_overflow_dont, /* complain_on_overflow */
1845 ppc64_elf_unhandled_reloc, /* special_function */
1846 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1847 FALSE, /* partial_inplace */
d006db6c 1848 0, /* src_mask */
c061c2d8 1849 0xfffc, /* dst_mask */
b34976b6 1850 FALSE), /* pcrel_offset */
5bd4f169 1851
411e1bfb
AM
1852 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1853 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1854 16, /* rightshift */
5bd4f169
AM
1855 1, /* size (0 = byte, 1 = short, 2 = long) */
1856 16, /* bitsize */
b34976b6 1857 FALSE, /* pc_relative */
5bd4f169 1858 0, /* bitpos */
f9c6b907 1859 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1860 ppc64_elf_unhandled_reloc, /* special_function */
1861 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1862 FALSE, /* partial_inplace */
d006db6c 1863 0, /* src_mask */
411e1bfb 1864 0xffff, /* dst_mask */
b34976b6 1865 FALSE), /* pcrel_offset */
5bd4f169 1866
411e1bfb
AM
1867 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1868 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1869 16, /* rightshift */
1870 1, /* size (0 = byte, 1 = short, 2 = long) */
1871 16, /* bitsize */
1872 FALSE, /* pc_relative */
1873 0, /* bitpos */
f9c6b907 1874 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1875 ppc64_elf_unhandled_reloc, /* special_function */
1876 "R_PPC64_GOT_DTPREL16_HA", /* name */
1877 FALSE, /* partial_inplace */
1878 0, /* src_mask */
1879 0xffff, /* dst_mask */
1880 FALSE), /* pcrel_offset */
1881
1882 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1883 offset to the entry relative to the TOC base (r2). */
1884 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1885 0, /* rightshift */
1886 1, /* size (0 = byte, 1 = short, 2 = long) */
1887 16, /* bitsize */
b34976b6 1888 FALSE, /* pc_relative */
5bd4f169
AM
1889 0, /* bitpos */
1890 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1891 ppc64_elf_unhandled_reloc, /* special_function */
1892 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1893 FALSE, /* partial_inplace */
d006db6c 1894 0, /* src_mask */
ad8e1ba5 1895 0xfffc, /* dst_mask */
b34976b6 1896 FALSE), /* pcrel_offset */
5bd4f169 1897
411e1bfb
AM
1898 /* Like GOT_TPREL16_DS, but no overflow. */
1899 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1900 0, /* rightshift */
1901 1, /* size (0 = byte, 1 = short, 2 = long) */
1902 16, /* bitsize */
b34976b6 1903 FALSE, /* pc_relative */
5bd4f169
AM
1904 0, /* bitpos */
1905 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1906 ppc64_elf_unhandled_reloc, /* special_function */
1907 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1908 FALSE, /* partial_inplace */
d006db6c 1909 0, /* src_mask */
ad8e1ba5 1910 0xfffc, /* dst_mask */
b34976b6 1911 FALSE), /* pcrel_offset */
5bd4f169 1912
411e1bfb
AM
1913 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1914 HOWTO (R_PPC64_GOT_TPREL16_HI,
1915 16, /* rightshift */
5bd4f169
AM
1916 1, /* size (0 = byte, 1 = short, 2 = long) */
1917 16, /* bitsize */
b34976b6 1918 FALSE, /* pc_relative */
5bd4f169 1919 0, /* bitpos */
f9c6b907 1920 complain_overflow_signed, /* complain_on_overflow */
805fc799 1921 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1922 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1923 FALSE, /* partial_inplace */
d006db6c 1924 0, /* src_mask */
411e1bfb 1925 0xffff, /* dst_mask */
b34976b6 1926 FALSE), /* pcrel_offset */
5bd4f169 1927
411e1bfb
AM
1928 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1929 HOWTO (R_PPC64_GOT_TPREL16_HA,
1930 16, /* rightshift */
5bd4f169
AM
1931 1, /* size (0 = byte, 1 = short, 2 = long) */
1932 16, /* bitsize */
b34976b6 1933 FALSE, /* pc_relative */
5bd4f169 1934 0, /* bitpos */
f9c6b907 1935 complain_overflow_signed, /* complain_on_overflow */
805fc799 1936 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1937 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1938 FALSE, /* partial_inplace */
d006db6c 1939 0, /* src_mask */
411e1bfb 1940 0xffff, /* dst_mask */
b34976b6 1941 FALSE), /* pcrel_offset */
5bd4f169 1942
25f23106
AM
1943 HOWTO (R_PPC64_JMP_IREL, /* type */
1944 0, /* rightshift */
1945 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1946 0, /* bitsize */
1947 FALSE, /* pc_relative */
1948 0, /* bitpos */
1949 complain_overflow_dont, /* complain_on_overflow */
1950 ppc64_elf_unhandled_reloc, /* special_function */
1951 "R_PPC64_JMP_IREL", /* name */
1952 FALSE, /* partial_inplace */
1953 0, /* src_mask */
1954 0, /* dst_mask */
1955 FALSE), /* pcrel_offset */
1956
e054468f
AM
1957 HOWTO (R_PPC64_IRELATIVE, /* type */
1958 0, /* rightshift */
1959 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1960 64, /* bitsize */
1961 FALSE, /* pc_relative */
1962 0, /* bitpos */
1963 complain_overflow_dont, /* complain_on_overflow */
1964 bfd_elf_generic_reloc, /* special_function */
1965 "R_PPC64_IRELATIVE", /* name */
1966 FALSE, /* partial_inplace */
1967 0, /* src_mask */
1968 ONES (64), /* dst_mask */
1969 FALSE), /* pcrel_offset */
1970
25f23106
AM
1971 /* A 16 bit relative relocation. */
1972 HOWTO (R_PPC64_REL16, /* type */
1973 0, /* rightshift */
1974 1, /* size (0 = byte, 1 = short, 2 = long) */
1975 16, /* bitsize */
1976 TRUE, /* pc_relative */
1977 0, /* bitpos */
b80eed39 1978 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1979 bfd_elf_generic_reloc, /* special_function */
1980 "R_PPC64_REL16", /* name */
1981 FALSE, /* partial_inplace */
1982 0, /* src_mask */
1983 0xffff, /* dst_mask */
1984 TRUE), /* pcrel_offset */
1985
1986 /* A 16 bit relative relocation without overflow. */
1987 HOWTO (R_PPC64_REL16_LO, /* type */
1988 0, /* rightshift */
1989 1, /* size (0 = byte, 1 = short, 2 = long) */
1990 16, /* bitsize */
1991 TRUE, /* pc_relative */
1992 0, /* bitpos */
1993 complain_overflow_dont,/* complain_on_overflow */
1994 bfd_elf_generic_reloc, /* special_function */
1995 "R_PPC64_REL16_LO", /* name */
1996 FALSE, /* partial_inplace */
1997 0, /* src_mask */
1998 0xffff, /* dst_mask */
1999 TRUE), /* pcrel_offset */
2000
2001 /* The high order 16 bits of a relative address. */
2002 HOWTO (R_PPC64_REL16_HI, /* type */
2003 16, /* rightshift */
2004 1, /* size (0 = byte, 1 = short, 2 = long) */
2005 16, /* bitsize */
2006 TRUE, /* pc_relative */
2007 0, /* bitpos */
f9c6b907 2008 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2009 bfd_elf_generic_reloc, /* special_function */
2010 "R_PPC64_REL16_HI", /* name */
2011 FALSE, /* partial_inplace */
2012 0, /* src_mask */
2013 0xffff, /* dst_mask */
2014 TRUE), /* pcrel_offset */
2015
2016 /* The high order 16 bits of a relative address, plus 1 if the contents of
2017 the low 16 bits, treated as a signed number, is negative. */
2018 HOWTO (R_PPC64_REL16_HA, /* type */
2019 16, /* rightshift */
2020 1, /* size (0 = byte, 1 = short, 2 = long) */
2021 16, /* bitsize */
2022 TRUE, /* pc_relative */
2023 0, /* bitpos */
f9c6b907 2024 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
2025 ppc64_elf_ha_reloc, /* special_function */
2026 "R_PPC64_REL16_HA", /* name */
2027 FALSE, /* partial_inplace */
2028 0, /* src_mask */
2029 0xffff, /* dst_mask */
2030 TRUE), /* pcrel_offset */
2031
a680de9a
PB
2032 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2033 HOWTO (R_PPC64_REL16DX_HA, /* type */
2034 16, /* rightshift */
2035 2, /* size (0 = byte, 1 = short, 2 = long) */
2036 16, /* bitsize */
2037 TRUE, /* pc_relative */
2038 0, /* bitpos */
2039 complain_overflow_signed, /* complain_on_overflow */
2040 ppc64_elf_ha_reloc, /* special_function */
2041 "R_PPC64_REL16DX_HA", /* name */
2042 FALSE, /* partial_inplace */
2043 0, /* src_mask */
2044 0x1fffc1, /* dst_mask */
2045 TRUE), /* pcrel_offset */
2046
f9c6b907
AM
2047 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2048 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2049 16, /* rightshift */
2050 1, /* size (0 = byte, 1 = short, 2 = long) */
2051 16, /* bitsize */
2052 FALSE, /* pc_relative */
2053 0, /* bitpos */
2054 complain_overflow_dont, /* complain_on_overflow */
2055 bfd_elf_generic_reloc, /* special_function */
2056 "R_PPC64_ADDR16_HIGH", /* name */
2057 FALSE, /* partial_inplace */
2058 0, /* src_mask */
2059 0xffff, /* dst_mask */
2060 FALSE), /* pcrel_offset */
2061
2062 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2063 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2064 16, /* rightshift */
2065 1, /* size (0 = byte, 1 = short, 2 = long) */
2066 16, /* bitsize */
2067 FALSE, /* pc_relative */
2068 0, /* bitpos */
2069 complain_overflow_dont, /* complain_on_overflow */
2070 ppc64_elf_ha_reloc, /* special_function */
2071 "R_PPC64_ADDR16_HIGHA", /* name */
2072 FALSE, /* partial_inplace */
2073 0, /* src_mask */
2074 0xffff, /* dst_mask */
2075 FALSE), /* pcrel_offset */
2076
2077 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2078 HOWTO (R_PPC64_DTPREL16_HIGH,
2079 16, /* rightshift */
2080 1, /* size (0 = byte, 1 = short, 2 = long) */
2081 16, /* bitsize */
2082 FALSE, /* pc_relative */
2083 0, /* bitpos */
2084 complain_overflow_dont, /* complain_on_overflow */
2085 ppc64_elf_unhandled_reloc, /* special_function */
2086 "R_PPC64_DTPREL16_HIGH", /* name */
2087 FALSE, /* partial_inplace */
2088 0, /* src_mask */
2089 0xffff, /* dst_mask */
2090 FALSE), /* pcrel_offset */
2091
2092 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2093 HOWTO (R_PPC64_DTPREL16_HIGHA,
2094 16, /* rightshift */
2095 1, /* size (0 = byte, 1 = short, 2 = long) */
2096 16, /* bitsize */
2097 FALSE, /* pc_relative */
2098 0, /* bitpos */
2099 complain_overflow_dont, /* complain_on_overflow */
2100 ppc64_elf_unhandled_reloc, /* special_function */
2101 "R_PPC64_DTPREL16_HIGHA", /* name */
2102 FALSE, /* partial_inplace */
2103 0, /* src_mask */
2104 0xffff, /* dst_mask */
2105 FALSE), /* pcrel_offset */
2106
2107 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2108 HOWTO (R_PPC64_TPREL16_HIGH,
2109 16, /* rightshift */
2110 1, /* size (0 = byte, 1 = short, 2 = long) */
2111 16, /* bitsize */
2112 FALSE, /* pc_relative */
2113 0, /* bitpos */
2114 complain_overflow_dont, /* complain_on_overflow */
2115 ppc64_elf_unhandled_reloc, /* special_function */
2116 "R_PPC64_TPREL16_HIGH", /* name */
2117 FALSE, /* partial_inplace */
2118 0, /* src_mask */
2119 0xffff, /* dst_mask */
2120 FALSE), /* pcrel_offset */
2121
2122 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2123 HOWTO (R_PPC64_TPREL16_HIGHA,
2124 16, /* rightshift */
2125 1, /* size (0 = byte, 1 = short, 2 = long) */
2126 16, /* bitsize */
2127 FALSE, /* pc_relative */
2128 0, /* bitpos */
2129 complain_overflow_dont, /* complain_on_overflow */
2130 ppc64_elf_unhandled_reloc, /* special_function */
2131 "R_PPC64_TPREL16_HIGHA", /* name */
2132 FALSE, /* partial_inplace */
2133 0, /* src_mask */
2134 0xffff, /* dst_mask */
2135 FALSE), /* pcrel_offset */
2136
006589cf
AM
2137 /* Marker reloc on ELFv2 large-model function entry. */
2138 HOWTO (R_PPC64_ENTRY,
2139 0, /* rightshift */
2140 2, /* size (0 = byte, 1 = short, 2 = long) */
2141 32, /* bitsize */
2142 FALSE, /* pc_relative */
2143 0, /* bitpos */
2144 complain_overflow_dont, /* complain_on_overflow */
2145 bfd_elf_generic_reloc, /* special_function */
2146 "R_PPC64_ENTRY", /* name */
2147 FALSE, /* partial_inplace */
2148 0, /* src_mask */
2149 0, /* dst_mask */
2150 FALSE), /* pcrel_offset */
2151
45965137
AM
2152 /* Like ADDR64, but use local entry point of function. */
2153 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2154 0, /* rightshift */
2155 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2156 64, /* bitsize */
2157 FALSE, /* pc_relative */
2158 0, /* bitpos */
2159 complain_overflow_dont, /* complain_on_overflow */
2160 bfd_elf_generic_reloc, /* special_function */
2161 "R_PPC64_ADDR64_LOCAL", /* name */
2162 FALSE, /* partial_inplace */
2163 0, /* src_mask */
2164 ONES (64), /* dst_mask */
2165 FALSE), /* pcrel_offset */
2166
5bd4f169
AM
2167 /* GNU extension to record C++ vtable hierarchy. */
2168 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2169 0, /* rightshift */
2170 0, /* size (0 = byte, 1 = short, 2 = long) */
2171 0, /* bitsize */
b34976b6 2172 FALSE, /* pc_relative */
5bd4f169
AM
2173 0, /* bitpos */
2174 complain_overflow_dont, /* complain_on_overflow */
2175 NULL, /* special_function */
2176 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2177 FALSE, /* partial_inplace */
5bd4f169
AM
2178 0, /* src_mask */
2179 0, /* dst_mask */
b34976b6 2180 FALSE), /* pcrel_offset */
5bd4f169
AM
2181
2182 /* GNU extension to record C++ vtable member usage. */
2183 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2184 0, /* rightshift */
2185 0, /* size (0 = byte, 1 = short, 2 = long) */
2186 0, /* bitsize */
b34976b6 2187 FALSE, /* pc_relative */
5bd4f169
AM
2188 0, /* bitpos */
2189 complain_overflow_dont, /* complain_on_overflow */
2190 NULL, /* special_function */
2191 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2192 FALSE, /* partial_inplace */
5bd4f169
AM
2193 0, /* src_mask */
2194 0, /* dst_mask */
b34976b6 2195 FALSE), /* pcrel_offset */
5bd4f169
AM
2196};
2197
2198\f
2199/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2200 be done. */
2201
2202static void
4ce794b7 2203ppc_howto_init (void)
5bd4f169
AM
2204{
2205 unsigned int i, type;
2206
a4b6fadd 2207 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
2208 {
2209 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 2210 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
2211 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2212 }
2213}
2214
2215static reloc_howto_type *
4ce794b7
AM
2216ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2217 bfd_reloc_code_real_type code)
5bd4f169 2218{
411e1bfb 2219 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2220
2221 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2222 /* Initialize howto table if needed. */
2223 ppc_howto_init ();
2224
4ce794b7 2225 switch (code)
5bd4f169
AM
2226 {
2227 default:
4ce794b7 2228 return NULL;
5bd4f169 2229
411e1bfb
AM
2230 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2231 break;
2232 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2233 break;
2234 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2235 break;
2236 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2237 break;
2238 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2239 break;
2240 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2241 break;
f9c6b907
AM
2242 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2243 break;
411e1bfb 2244 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2245 break;
f9c6b907
AM
2246 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2247 break;
411e1bfb 2248 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2249 break;
411e1bfb 2250 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2251 break;
411e1bfb 2252 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2253 break;
411e1bfb 2254 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2255 break;
411e1bfb 2256 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2257 break;
411e1bfb 2258 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2259 break;
411e1bfb 2260 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2261 break;
411e1bfb 2262 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2263 break;
411e1bfb 2264 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2265 break;
411e1bfb 2266 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2267 break;
411e1bfb 2268 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2269 break;
411e1bfb 2270 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2271 break;
411e1bfb 2272 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2273 break;
411e1bfb 2274 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2275 break;
411e1bfb 2276 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2277 break;
411e1bfb 2278 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2279 break;
411e1bfb 2280 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2281 break;
411e1bfb 2282 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2283 break;
411e1bfb 2284 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2285 break;
411e1bfb 2286 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2287 break;
411e1bfb 2288 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2289 break;
411e1bfb 2290 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2291 break;
411e1bfb 2292 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2293 break;
411e1bfb 2294 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2295 break;
411e1bfb 2296 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2297 break;
411e1bfb 2298 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2299 break;
411e1bfb 2300 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2301 break;
411e1bfb 2302 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2303 break;
411e1bfb 2304 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2305 break;
411e1bfb 2306 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2307 break;
411e1bfb 2308 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2309 break;
411e1bfb 2310 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2311 break;
411e1bfb 2312 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2313 break;
411e1bfb 2314 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2315 break;
411e1bfb 2316 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2317 break;
411e1bfb 2318 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2319 break;
411e1bfb 2320 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2321 break;
411e1bfb 2322 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2323 break;
411e1bfb 2324 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2325 break;
411e1bfb 2326 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2327 break;
411e1bfb 2328 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2329 break;
411e1bfb 2330 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2331 break;
411e1bfb 2332 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2333 break;
411e1bfb 2334 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2335 break;
411e1bfb 2336 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2337 break;
411e1bfb 2338 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2339 break;
411e1bfb 2340 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2341 break;
411e1bfb 2342 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2343 break;
411e1bfb 2344 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2345 break;
411e1bfb 2346 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2347 break;
411e1bfb 2348 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2349 break;
411e1bfb 2350 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2351 break;
411e1bfb 2352 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2353 break;
727fc41e
AM
2354 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2355 break;
2356 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2357 break;
411e1bfb 2358 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2359 break;
411e1bfb 2360 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2361 break;
411e1bfb 2362 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2363 break;
411e1bfb 2364 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2365 break;
f9c6b907
AM
2366 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2367 break;
411e1bfb 2368 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2369 break;
f9c6b907
AM
2370 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2371 break;
411e1bfb 2372 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2373 break;
411e1bfb
AM
2374 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2375 break;
2376 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2377 break;
2378 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2379 break;
f9c6b907
AM
2380 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2381 break;
411e1bfb
AM
2382 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2383 break;
f9c6b907
AM
2384 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2385 break;
411e1bfb
AM
2386 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2387 break;
2388 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2389 break;
2390 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2391 break;
2392 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2393 break;
2394 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2395 break;
2396 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2397 break;
2398 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2399 break;
2400 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2401 break;
2402 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2403 break;
2404 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2405 break;
2406 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2407 break;
2408 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2409 break;
2410 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2411 break;
2412 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2413 break;
2414 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2415 break;
2416 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2417 break;
2418 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2419 break;
2420 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2421 break;
2422 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2423 break;
2424 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2425 break;
2426 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2427 break;
2428 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2429 break;
2430 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2431 break;
2432 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2433 break;
2434 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2435 break;
2436 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2437 break;
2438 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2439 break;
2440 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2441 break;
2442 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2443 break;
25f23106
AM
2444 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2445 break;
2446 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2447 break;
2448 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2449 break;
2450 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2451 break;
a680de9a
PB
2452 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2453 break;
006589cf
AM
2454 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2455 break;
45965137
AM
2456 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2457 break;
411e1bfb
AM
2458 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2459 break;
2460 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2461 break;
2462 }
2463
4ce794b7 2464 return ppc64_elf_howto_table[r];
5bd4f169
AM
2465};
2466
157090f7
AM
2467static reloc_howto_type *
2468ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2469 const char *r_name)
2470{
2471 unsigned int i;
2472
a4b6fadd 2473 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
2474 if (ppc64_elf_howto_raw[i].name != NULL
2475 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2476 return &ppc64_elf_howto_raw[i];
2477
2478 return NULL;
2479}
2480
5bd4f169
AM
2481/* Set the howto pointer for a PowerPC ELF reloc. */
2482
2483static void
4ce794b7
AM
2484ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2485 Elf_Internal_Rela *dst)
5bd4f169 2486{
65f38f15
AM
2487 unsigned int type;
2488
ef60b7ff 2489 /* Initialize howto table if needed. */
5bd4f169 2490 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2491 ppc_howto_init ();
2492
65f38f15 2493 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 2494 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 2495 {
695344c0 2496 /* xgettext:c-format */
4eca0228
AM
2497 _bfd_error_handler (_("%B: invalid relocation type %d"),
2498 abfd, (int) type);
3ec2b351 2499 type = R_PPC64_NONE;
d0fb9a8d 2500 }
65f38f15 2501 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2502}
2503
04c9666a 2504/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2505
2506static bfd_reloc_status_type
4ce794b7
AM
2507ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2508 void *data, asection *input_section,
2509 bfd *output_bfd, char **error_message)
5bd4f169 2510{
a680de9a
PB
2511 enum elf_ppc64_reloc_type r_type;
2512 long insn;
2513 bfd_size_type octets;
2514 bfd_vma value;
2515
805fc799
AM
2516 /* If this is a relocatable link (output_bfd test tells us), just
2517 call the generic function. Any adjustment will be done at final
2518 link time. */
2519 if (output_bfd != NULL)
cedb70c5 2520 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2521 input_section, output_bfd, error_message);
2522
2523 /* Adjust the addend for sign extension of the low 16 bits.
2524 We won't actually be using the low 16 bits, so trashing them
2525 doesn't matter. */
2526 reloc_entry->addend += 0x8000;
a680de9a
PB
2527 r_type = reloc_entry->howto->type;
2528 if (r_type != R_PPC64_REL16DX_HA)
2529 return bfd_reloc_continue;
2530
2531 value = 0;
2532 if (!bfd_is_com_section (symbol->section))
2533 value = symbol->value;
2534 value += (reloc_entry->addend
2535 + symbol->section->output_offset
2536 + symbol->section->output_section->vma);
2537 value -= (reloc_entry->address
2538 + input_section->output_offset
2539 + input_section->output_section->vma);
2540 value = (bfd_signed_vma) value >> 16;
2541
2542 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2543 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2544 insn &= ~0x1fffc1;
2545 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2546 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2547 if (value + 0x8000 > 0xffff)
2548 return bfd_reloc_overflow;
2549 return bfd_reloc_ok;
805fc799 2550}
5bd4f169 2551
2441e016
AM
2552static bfd_reloc_status_type
2553ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2554 void *data, asection *input_section,
2555 bfd *output_bfd, char **error_message)
2556{
2557 if (output_bfd != NULL)
2558 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2559 input_section, output_bfd, error_message);
2560
699733f6
AM
2561 if (strcmp (symbol->section->name, ".opd") == 0
2562 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2563 {
2564 bfd_vma dest = opd_entry_value (symbol->section,
2565 symbol->value + reloc_entry->addend,
aef36ac1 2566 NULL, NULL, FALSE);
2441e016
AM
2567 if (dest != (bfd_vma) -1)
2568 reloc_entry->addend = dest - (symbol->value
2569 + symbol->section->output_section->vma
2570 + symbol->section->output_offset);
2571 }
810d4e75
AM
2572 else
2573 {
2574 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2575
2576 if (symbol->section->owner != abfd
9f284bf9 2577 && symbol->section->owner != NULL
810d4e75
AM
2578 && abiversion (symbol->section->owner) >= 2)
2579 {
2580 unsigned int i;
2581
2582 for (i = 0; i < symbol->section->owner->symcount; ++i)
2583 {
2584 asymbol *symdef = symbol->section->owner->outsymbols[i];
2585
2586 if (strcmp (symdef->name, symbol->name) == 0)
2587 {
2588 elfsym = (elf_symbol_type *) symdef;
2589 break;
2590 }
2591 }
2592 }
2593 reloc_entry->addend
2594 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2595 }
2441e016
AM
2596 return bfd_reloc_continue;
2597}
2598
805fc799 2599static bfd_reloc_status_type
4ce794b7
AM
2600ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2601 void *data, asection *input_section,
2602 bfd *output_bfd, char **error_message)
805fc799
AM
2603{
2604 long insn;
04c9666a 2605 enum elf_ppc64_reloc_type r_type;
805fc799 2606 bfd_size_type octets;
794e51c0
AM
2607 /* Assume 'at' branch hints. */
2608 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2609
2610 /* If this is a relocatable link (output_bfd test tells us), just
2611 call the generic function. Any adjustment will be done at final
2612 link time. */
5bd4f169 2613 if (output_bfd != NULL)
cedb70c5 2614 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2615 input_section, output_bfd, error_message);
2616
2617 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2618 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2619 insn &= ~(0x01 << 21);
4ce794b7 2620 r_type = reloc_entry->howto->type;
805fc799
AM
2621 if (r_type == R_PPC64_ADDR14_BRTAKEN
2622 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2623 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2624
794e51c0 2625 if (is_isa_v2)
5bd4f169 2626 {
805fc799
AM
2627 /* Set 'a' bit. This is 0b00010 in BO field for branch
2628 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2629 for branch on CTR insns (BO == 1a00t or 1a01t). */
2630 if ((insn & (0x14 << 21)) == (0x04 << 21))
2631 insn |= 0x02 << 21;
2632 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2633 insn |= 0x08 << 21;
2634 else
2441e016 2635 goto out;
5bd4f169 2636 }
805fc799
AM
2637 else
2638 {
2639 bfd_vma target = 0;
2640 bfd_vma from;
5bd4f169 2641
805fc799
AM
2642 if (!bfd_is_com_section (symbol->section))
2643 target = symbol->value;
2644 target += symbol->section->output_section->vma;
2645 target += symbol->section->output_offset;
2646 target += reloc_entry->addend;
5bd4f169 2647
805fc799
AM
2648 from = (reloc_entry->address
2649 + input_section->output_offset
2650 + input_section->output_section->vma);
5bd4f169 2651
805fc799
AM
2652 /* Invert 'y' bit if not the default. */
2653 if ((bfd_signed_vma) (target - from) < 0)
2654 insn ^= 0x01 << 21;
2655 }
4ce794b7 2656 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2657 out:
2658 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2659 input_section, output_bfd, error_message);
805fc799 2660}
5bd4f169 2661
805fc799 2662static bfd_reloc_status_type
4ce794b7
AM
2663ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2664 void *data, asection *input_section,
2665 bfd *output_bfd, char **error_message)
805fc799
AM
2666{
2667 /* If this is a relocatable link (output_bfd test tells us), just
2668 call the generic function. Any adjustment will be done at final
2669 link time. */
2670 if (output_bfd != NULL)
cedb70c5 2671 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2672 input_section, output_bfd, error_message);
5bd4f169 2673
805fc799
AM
2674 /* Subtract the symbol section base address. */
2675 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2676 return bfd_reloc_continue;
2677}
2678
805fc799 2679static bfd_reloc_status_type
4ce794b7
AM
2680ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2681 void *data, asection *input_section,
2682 bfd *output_bfd, char **error_message)
805fc799
AM
2683{
2684 /* If this is a relocatable link (output_bfd test tells us), just
2685 call the generic function. Any adjustment will be done at final
2686 link time. */
2687 if (output_bfd != NULL)
cedb70c5 2688 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2689 input_section, output_bfd, error_message);
2690
2691 /* Subtract the symbol section base address. */
2692 reloc_entry->addend -= symbol->section->output_section->vma;
2693
2694 /* Adjust the addend for sign extension of the low 16 bits. */
2695 reloc_entry->addend += 0x8000;
2696 return bfd_reloc_continue;
2697}
2698
2699static bfd_reloc_status_type
4ce794b7
AM
2700ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2701 void *data, asection *input_section,
2702 bfd *output_bfd, char **error_message)
805fc799
AM
2703{
2704 bfd_vma TOCstart;
2705
2706 /* If this is a relocatable link (output_bfd test tells us), just
2707 call the generic function. Any adjustment will be done at final
2708 link time. */
2709 if (output_bfd != NULL)
cedb70c5 2710 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2711 input_section, output_bfd, error_message);
2712
2713 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2714 if (TOCstart == 0)
1c865ab2 2715 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2716
2717 /* Subtract the TOC base address. */
2718 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2719 return bfd_reloc_continue;
2720}
2721
2722static bfd_reloc_status_type
4ce794b7
AM
2723ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2724 void *data, asection *input_section,
2725 bfd *output_bfd, char **error_message)
805fc799
AM
2726{
2727 bfd_vma TOCstart;
2728
2729 /* If this is a relocatable link (output_bfd test tells us), just
2730 call the generic function. Any adjustment will be done at final
2731 link time. */
2732 if (output_bfd != NULL)
cedb70c5 2733 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2734 input_section, output_bfd, error_message);
2735
2736 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2737 if (TOCstart == 0)
1c865ab2 2738 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2739
2740 /* Subtract the TOC base address. */
2741 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2742
2743 /* Adjust the addend for sign extension of the low 16 bits. */
2744 reloc_entry->addend += 0x8000;
2745 return bfd_reloc_continue;
2746}
2747
2748static bfd_reloc_status_type
4ce794b7
AM
2749ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2750 void *data, asection *input_section,
2751 bfd *output_bfd, char **error_message)
805fc799
AM
2752{
2753 bfd_vma TOCstart;
2754 bfd_size_type octets;
2755
2756 /* If this is a relocatable link (output_bfd test tells us), just
2757 call the generic function. Any adjustment will be done at final
2758 link time. */
2759 if (output_bfd != NULL)
cedb70c5 2760 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2761 input_section, output_bfd, error_message);
2762
2763 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2764 if (TOCstart == 0)
1c865ab2 2765 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2766
2767 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2768 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2769 return bfd_reloc_ok;
2770}
2771
2772static bfd_reloc_status_type
4ce794b7
AM
2773ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2774 void *data, asection *input_section,
2775 bfd *output_bfd, char **error_message)
805fc799
AM
2776{
2777 /* If this is a relocatable link (output_bfd test tells us), just
2778 call the generic function. Any adjustment will be done at final
2779 link time. */
2780 if (output_bfd != NULL)
cedb70c5 2781 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2782 input_section, output_bfd, error_message);
2783
2784 if (error_message != NULL)
2785 {
2786 static char buf[60];
2787 sprintf (buf, "generic linker can't handle %s",
2788 reloc_entry->howto->name);
2789 *error_message = buf;
2790 }
2791 return bfd_reloc_dangerous;
2792}
2793
927be08e
AM
2794/* Track GOT entries needed for a given symbol. We might need more
2795 than one got entry per symbol. */
2796struct got_entry
2797{
2798 struct got_entry *next;
2799
2800 /* The symbol addend that we'll be placing in the GOT. */
2801 bfd_vma addend;
2802
2803 /* Unlike other ELF targets, we use separate GOT entries for the same
2804 symbol referenced from different input files. This is to support
2805 automatic multiple TOC/GOT sections, where the TOC base can vary
2806 from one input file to another. After partitioning into TOC groups
2807 we merge entries within the group.
2808
2809 Point to the BFD owning this GOT entry. */
2810 bfd *owner;
2811
2812 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2813 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2814 unsigned char tls_type;
927be08e
AM
2815
2816 /* Non-zero if got.ent points to real entry. */
f961d9dd 2817 unsigned char is_indirect;
927be08e
AM
2818
2819 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2820 union
2821 {
2822 bfd_signed_vma refcount;
2823 bfd_vma offset;
2824 struct got_entry *ent;
2825 } got;
2826};
2827
2828/* The same for PLT. */
2829struct plt_entry
2830{
2831 struct plt_entry *next;
2832
2833 bfd_vma addend;
2834
2835 union
2836 {
2837 bfd_signed_vma refcount;
2838 bfd_vma offset;
2839 } plt;
2840};
2841
e717da7e
AM
2842struct ppc64_elf_obj_tdata
2843{
2844 struct elf_obj_tdata elf;
2845
2846 /* Shortcuts to dynamic linker sections. */
2847 asection *got;
2848 asection *relgot;
2849
b3fac117
AM
2850 /* Used during garbage collection. We attach global symbols defined
2851 on removed .opd entries to this section so that the sym is removed. */
2852 asection *deleted_section;
81688140 2853
927be08e 2854 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2855 sections means we potentially need one of these for each input bfd. */
927be08e 2856 struct got_entry tlsld_got;
8860955f 2857
729eabd5
AM
2858 union {
2859 /* A copy of relocs before they are modified for --emit-relocs. */
2860 Elf_Internal_Rela *relocs;
2861
2862 /* Section contents. */
2863 bfd_byte *contents;
2864 } opd;
d77c8a4b
AM
2865
2866 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2867 the reloc to be in the range -32768 to 32767. */
98528052
AM
2868 unsigned int has_small_toc_reloc : 1;
2869
560c8763
AM
2870 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2871 instruction not one we handle. */
2872 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2873};
2874
2875#define ppc64_elf_tdata(bfd) \
2876 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2877
2878#define ppc64_tlsld_got(bfd) \
2879 (&ppc64_elf_tdata (bfd)->tlsld_got)
2880
0c8d6e5c
AM
2881#define is_ppc64_elf(bfd) \
2882 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2883 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2884
e717da7e
AM
2885/* Override the generic function because we store some extras. */
2886
2887static bfd_boolean
2888ppc64_elf_mkobject (bfd *abfd)
2889{
0ffa91dd 2890 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2891 PPC64_ELF_DATA);
e717da7e
AM
2892}
2893
feee612b 2894/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 2895 default is 32 bit. Also select arch based on apuinfo. */
feee612b 2896
b34976b6 2897static bfd_boolean
4ce794b7 2898ppc64_elf_object_p (bfd *abfd)
feee612b 2899{
14b57c7c
AM
2900 if (!abfd->arch_info->the_default)
2901 return TRUE;
2902
2903 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
2904 {
2905 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2906
2907 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2908 {
2909 /* Relies on arch after 32 bit default being 64 bit default. */
2910 abfd->arch_info = abfd->arch_info->next;
2911 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2912 }
2913 }
14b57c7c 2914 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
2915}
2916
d37c89e5
AM
2917/* Support for core dump NOTE sections. */
2918
2919static bfd_boolean
2920ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2921{
eea6121a 2922 size_t offset, size;
d37c89e5
AM
2923
2924 if (note->descsz != 504)
2925 return FALSE;
2926
2927 /* pr_cursig */
228e534f 2928 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2929
2930 /* pr_pid */
228e534f 2931 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2932
2933 /* pr_reg */
2934 offset = 112;
eea6121a 2935 size = 384;
d37c89e5
AM
2936
2937 /* Make a ".reg/999" section. */
2938 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2939 size, note->descpos + offset);
d37c89e5
AM
2940}
2941
2942static bfd_boolean
2943ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2944{
2945 if (note->descsz != 136)
2946 return FALSE;
2947
228e534f 2948 elf_tdata (abfd)->core->pid
bc989cdc 2949 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2950 elf_tdata (abfd)->core->program
d37c89e5 2951 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2952 elf_tdata (abfd)->core->command
d37c89e5
AM
2953 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2954
2955 return TRUE;
2956}
2957
183e98be
AM
2958static char *
2959ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2960 ...)
2961{
2962 switch (note_type)
2963 {
2964 default:
2965 return NULL;
2966
2967 case NT_PRPSINFO:
2968 {
2969 char data[136];
2970 va_list ap;
2971
2972 va_start (ap, note_type);
75cd47ed 2973 memset (data, 0, sizeof (data));
183e98be
AM
2974 strncpy (data + 40, va_arg (ap, const char *), 16);
2975 strncpy (data + 56, va_arg (ap, const char *), 80);
2976 va_end (ap);
2977 return elfcore_write_note (abfd, buf, bufsiz,
2978 "CORE", note_type, data, sizeof (data));
2979 }
2980
2981 case NT_PRSTATUS:
2982 {
2983 char data[504];
2984 va_list ap;
2985 long pid;
2986 int cursig;
2987 const void *greg;
2988
2989 va_start (ap, note_type);
2990 memset (data, 0, 112);
2991 pid = va_arg (ap, long);
2992 bfd_put_32 (abfd, pid, data + 32);
2993 cursig = va_arg (ap, int);
2994 bfd_put_16 (abfd, cursig, data + 12);
2995 greg = va_arg (ap, const void *);
2996 memcpy (data + 112, greg, 384);
2997 memset (data + 496, 0, 8);
2998 va_end (ap);
2999 return elfcore_write_note (abfd, buf, bufsiz,
3000 "CORE", note_type, data, sizeof (data));
3001 }
3002 }
3003}
3004
5d35169e
AM
3005/* Add extra PPC sections. */
3006
b35d266b 3007static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 3008{
0112cd26
NC
3009 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3010 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3011 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3012 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3013 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3014 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3015 { NULL, 0, 0, 0, 0 }
5d35169e
AM
3016};
3017
7c8fe5c4
AM
3018enum _ppc64_sec_type {
3019 sec_normal = 0,
3020 sec_opd = 1,
3021 sec_toc = 2
3022};
3023
f0abc2a1
AM
3024struct _ppc64_elf_section_data
3025{
3026 struct bfd_elf_section_data elf;
411e1bfb 3027
f0abc2a1
AM
3028 union
3029 {
51aecdc5
AM
3030 /* An array with one entry for each opd function descriptor,
3031 and some spares since opd entries may be either 16 or 24 bytes. */
3032#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
3033 struct _opd_sec_data
3034 {
3035 /* Points to the function code section for local opd entries. */
3036 asection **func_sec;
3037
3038 /* After editing .opd, adjust references to opd local syms. */
3039 long *adjust;
3040 } opd;
7c8fe5c4 3041
3a71aa26
AM
3042 /* An array for toc sections, indexed by offset/8. */
3043 struct _toc_sec_data
3044 {
3045 /* Specifies the relocation symbol index used at a given toc offset. */
3046 unsigned *symndx;
3047
3048 /* And the relocation addend. */
3049 bfd_vma *add;
3050 } toc;
7c8fe5c4
AM
3051 } u;
3052
3053 enum _ppc64_sec_type sec_type:2;
411e1bfb 3054
7c8fe5c4
AM
3055 /* Flag set when small branches are detected. Used to
3056 select suitable defaults for the stub group size. */
3057 unsigned int has_14bit_branch:1;
f0abc2a1
AM
3058};
3059
3060#define ppc64_elf_section_data(sec) \
411e1bfb 3061 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
3062
3063static bfd_boolean
4ce794b7 3064ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 3065{
f592407e
AM
3066 if (!sec->used_by_bfd)
3067 {
3068 struct _ppc64_elf_section_data *sdata;
3069 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 3070
f592407e
AM
3071 sdata = bfd_zalloc (abfd, amt);
3072 if (sdata == NULL)
3073 return FALSE;
3074 sec->used_by_bfd = sdata;
3075 }
f0abc2a1
AM
3076
3077 return _bfd_elf_new_section_hook (abfd, sec);
3078}
4025353c 3079
74f0fb50 3080static struct _opd_sec_data *
4025353c
AM
3081get_opd_info (asection * sec)
3082{
3083 if (sec != NULL
3084 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 3085 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 3086 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
3087 return NULL;
3088}
90e3cdf2
JJ
3089\f
3090/* Parameters for the qsort hook. */
90e3cdf2 3091static bfd_boolean synthetic_relocatable;
cd285db5 3092static asection *synthetic_opd;
90e3cdf2 3093
699733f6 3094/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
3095
3096static int
3097compare_symbols (const void *ap, const void *bp)
3098{
3099 const asymbol *a = * (const asymbol **) ap;
3100 const asymbol *b = * (const asymbol **) bp;
3101
699733f6
AM
3102 /* Section symbols first. */
3103 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 3104 return -1;
699733f6 3105 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
3106 return 1;
3107
699733f6 3108 /* then .opd symbols. */
cd285db5
AM
3109 if (synthetic_opd != NULL)
3110 {
3111 if (strcmp (a->section->name, ".opd") == 0
3112 && strcmp (b->section->name, ".opd") != 0)
3113 return -1;
3114 if (strcmp (a->section->name, ".opd") != 0
3115 && strcmp (b->section->name, ".opd") == 0)
3116 return 1;
3117 }
90e3cdf2 3118
699733f6 3119 /* then other code symbols. */
90e3cdf2
JJ
3120 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3121 == (SEC_CODE | SEC_ALLOC)
3122 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3123 != (SEC_CODE | SEC_ALLOC))
3124 return -1;
3125
3126 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3127 != (SEC_CODE | SEC_ALLOC)
3128 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3129 == (SEC_CODE | SEC_ALLOC))
3130 return 1;
3131
3132 if (synthetic_relocatable)
3133 {
3134 if (a->section->id < b->section->id)
3135 return -1;
3136
3137 if (a->section->id > b->section->id)
3138 return 1;
3139 }
3140
3141 if (a->value + a->section->vma < b->value + b->section->vma)
3142 return -1;
3143
3144 if (a->value + a->section->vma > b->value + b->section->vma)
3145 return 1;
3146
4d35a0aa
AM
3147 /* For syms with the same value, prefer strong dynamic global function
3148 syms over other syms. */
3149 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3150 return -1;
3151
3152 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3153 return 1;
3154
3155 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3156 return -1;
3157
3158 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3159 return 1;
3160
3161 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3162 return -1;
3163
3164 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3165 return 1;
3166
3167 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3168 return -1;
3169
3170 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3171 return 1;
3172
90e3cdf2
JJ
3173 return 0;
3174}
3175
699733f6 3176/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3177
699733f6 3178static asymbol *
7292b3ac 3179sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
90e3cdf2 3180{
699733f6 3181 long mid;
90e3cdf2 3182
7292b3ac 3183 if (id == (unsigned) -1)
699733f6
AM
3184 {
3185 while (lo < hi)
3186 {
3187 mid = (lo + hi) >> 1;
3188 if (syms[mid]->value + syms[mid]->section->vma < value)
3189 lo = mid + 1;
3190 else if (syms[mid]->value + syms[mid]->section->vma > value)
3191 hi = mid;
3192 else
3193 return syms[mid];
3194 }
3195 }
3196 else
3197 {
3198 while (lo < hi)
3199 {
3200 mid = (lo + hi) >> 1;
3201 if (syms[mid]->section->id < id)
3202 lo = mid + 1;
3203 else if (syms[mid]->section->id > id)
3204 hi = mid;
3205 else if (syms[mid]->value < value)
3206 lo = mid + 1;
3207 else if (syms[mid]->value > value)
3208 hi = mid;
3209 else
3210 return syms[mid];
3211 }
3212 }
3213 return NULL;
90e3cdf2
JJ
3214}
3215
468392fb
AM
3216static bfd_boolean
3217section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3218{
3219 bfd_vma vma = *(bfd_vma *) ptr;
3220 return ((section->flags & SEC_ALLOC) != 0
3221 && section->vma <= vma
3222 && vma < section->vma + section->size);
3223}
3224
699733f6 3225/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
3226 entry syms. Also generate @plt symbols for the glink branch table.
3227 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
3228
3229static long
a7535cf3
AM
3230ppc64_elf_get_synthetic_symtab (bfd *abfd,
3231 long static_count, asymbol **static_syms,
3232 long dyn_count, asymbol **dyn_syms,
c9727e01 3233 asymbol **ret)
90e3cdf2
JJ
3234{
3235 asymbol *s;
699733f6
AM
3236 long i;
3237 long count;
90e3cdf2 3238 char *names;
a7535cf3 3239 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3240 asection *opd = NULL;
90e3cdf2 3241 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3242 asymbol **syms;
ee67d69a 3243 int abi = abiversion (abfd);
90e3cdf2
JJ
3244
3245 *ret = NULL;
3246
ee67d69a
AM
3247 if (abi < 2)
3248 {
3249 opd = bfd_get_section_by_name (abfd, ".opd");
3250 if (opd == NULL && abi == 1)
3251 return 0;
3252 }
90e3cdf2 3253
a7535cf3 3254 symcount = static_count;
c9727e01 3255 if (!relocatable)
a7535cf3 3256 symcount += dyn_count;
90e3cdf2 3257 if (symcount == 0)
c9727e01 3258 return 0;
90e3cdf2 3259
a7535cf3
AM
3260 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3261 if (syms == NULL)
7356fed5 3262 return -1;
a7535cf3
AM
3263
3264 if (!relocatable && static_count != 0 && dyn_count != 0)
3265 {
3266 /* Use both symbol tables. */
3267 memcpy (syms, static_syms, static_count * sizeof (*syms));
3268 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3269 }
3270 else if (!relocatable && static_count == 0)
3271 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3272 else
3273 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3274
90e3cdf2 3275 synthetic_relocatable = relocatable;
cd285db5 3276 synthetic_opd = opd;
595da8c5 3277 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3278
c9727e01
AM
3279 if (!relocatable && symcount > 1)
3280 {
3281 long j;
3282 /* Trim duplicate syms, since we may have merged the normal and
3283 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3284 different values, so trim any with the same value. */
c9727e01
AM
3285 for (i = 1, j = 1; i < symcount; ++i)
3286 if (syms[i - 1]->value + syms[i - 1]->section->vma
3287 != syms[i]->value + syms[i]->section->vma)
3288 syms[j++] = syms[i];
3289 symcount = j;
3290 }
3291
699733f6 3292 i = 0;
cd285db5
AM
3293 /* Note that here and in compare_symbols we can't compare opd and
3294 sym->section directly. With separate debug info files, the
3295 symbols will be extracted from the debug file while abfd passed
3296 to this function is the real binary. */
3297 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3298 ++i;
3299 codesecsym = i;
90e3cdf2 3300
699733f6
AM
3301 for (; i < symcount; ++i)
3302 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3303 != (SEC_CODE | SEC_ALLOC))
3304 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3305 break;
3306 codesecsymend = i;
90e3cdf2 3307
699733f6
AM
3308 for (; i < symcount; ++i)
3309 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3310 break;
3311 secsymend = i;
90e3cdf2 3312
cd285db5
AM
3313 if (opd != NULL)
3314 for (; i < symcount; ++i)
3315 if (strcmp (syms[i]->section->name, ".opd") != 0)
3316 break;
699733f6 3317 opdsymend = i;
90e3cdf2 3318
699733f6
AM
3319 for (; i < symcount; ++i)
3320 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3321 != (SEC_CODE | SEC_ALLOC))
3322 break;
3323 symcount = i;
3324
c9727e01 3325 count = 0;
90e3cdf2 3326
699733f6 3327 if (relocatable)
90e3cdf2 3328 {
699733f6
AM
3329 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3330 arelent *r;
3331 size_t size;
3332 long relcount;
90e3cdf2 3333
468392fb
AM
3334 if (opdsymend == secsymend)
3335 goto done;
3336
699733f6 3337 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3338 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3339 if (relcount == 0)
c9727e01 3340 goto done;
90e3cdf2 3341
7356fed5
AM
3342 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3343 {
3344 count = -1;
3345 goto done;
3346 }
3347
699733f6 3348 size = 0;
595da8c5 3349 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3350 {
3351 asymbol *sym;
90e3cdf2 3352
595da8c5 3353 while (r < opd->relocation + relcount
699733f6
AM
3354 && r->address < syms[i]->value + opd->vma)
3355 ++r;
90e3cdf2 3356
595da8c5 3357 if (r == opd->relocation + relcount)
699733f6 3358 break;
90e3cdf2 3359
699733f6
AM
3360 if (r->address != syms[i]->value + opd->vma)
3361 continue;
90e3cdf2 3362
699733f6
AM
3363 if (r->howto->type != R_PPC64_ADDR64)
3364 continue;
90e3cdf2 3365
699733f6
AM
3366 sym = *r->sym_ptr_ptr;
3367 if (!sym_exists_at (syms, opdsymend, symcount,
3368 sym->section->id, sym->value + r->addend))
3369 {
3370 ++count;
3371 size += sizeof (asymbol);
3372 size += strlen (syms[i]->name) + 2;
3373 }
3374 }
90e3cdf2 3375
c4b0b099
AM
3376 if (size == 0)
3377 goto done;
699733f6
AM
3378 s = *ret = bfd_malloc (size);
3379 if (s == NULL)
3380 {
7356fed5 3381 count = -1;
c9727e01 3382 goto done;
699733f6 3383 }
90e3cdf2 3384
699733f6 3385 names = (char *) (s + count);
90e3cdf2 3386
595da8c5 3387 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3388 {
699733f6 3389 asymbol *sym;
90e3cdf2 3390
595da8c5 3391 while (r < opd->relocation + relcount
699733f6
AM
3392 && r->address < syms[i]->value + opd->vma)
3393 ++r;
90e3cdf2 3394
595da8c5 3395 if (r == opd->relocation + relcount)
699733f6
AM
3396 break;
3397
3398 if (r->address != syms[i]->value + opd->vma)
3399 continue;
3400
3401 if (r->howto->type != R_PPC64_ADDR64)
3402 continue;
90e3cdf2 3403
699733f6
AM
3404 sym = *r->sym_ptr_ptr;
3405 if (!sym_exists_at (syms, opdsymend, symcount,
3406 sym->section->id, sym->value + r->addend))
3407 {
3408 size_t len;
3409
3410 *s = *syms[i];
6ba2a415 3411 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3412 s->section = sym->section;
3413 s->value = sym->value + r->addend;
3414 s->name = names;
3415 *names++ = '.';
3416 len = strlen (syms[i]->name);
3417 memcpy (names, syms[i]->name, len + 1);
3418 names += len + 1;
6f610d07
UW
3419 /* Have udata.p point back to the original symbol this
3420 synthetic symbol was derived from. */
3421 s->udata.p = syms[i];
699733f6
AM
3422 s++;
3423 }
3424 }
3425 }
3426 else
90e3cdf2 3427 {
468392fb 3428 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3429 bfd_byte *contents = NULL;
699733f6 3430 size_t size;
468392fb
AM
3431 long plt_count = 0;
3432 bfd_vma glink_vma = 0, resolv_vma = 0;
3433 asection *dynamic, *glink = NULL, *relplt = NULL;
3434 arelent *p;
90e3cdf2 3435
ee67d69a 3436 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3437 {
c4b0b099
AM
3438 free_contents_and_exit_err:
3439 count = -1;
ee67d69a 3440 free_contents_and_exit:
699733f6 3441 if (contents)
ee67d69a 3442 free (contents);
c9727e01 3443 goto done;
699733f6 3444 }
90e3cdf2 3445
699733f6
AM
3446 size = 0;
3447 for (i = secsymend; i < opdsymend; ++i)
3448 {
3449 bfd_vma ent;
90e3cdf2 3450
5ef11c02
AM
3451 /* Ignore bogus symbols. */
3452 if (syms[i]->value > opd->size - 8)
3453 continue;
3454
699733f6
AM
3455 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3456 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3457 {
3458 ++count;
3459 size += sizeof (asymbol);
3460 size += strlen (syms[i]->name) + 2;
3461 }
3462 }
90e3cdf2 3463
468392fb 3464 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3465 if (dyn_count != 0
3466 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3467 {
3468 bfd_byte *dynbuf, *extdyn, *extdynend;
3469 size_t extdynsize;
3470 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3471
3472 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 3473 goto free_contents_and_exit_err;
468392fb
AM
3474
3475 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3476 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3477
3478 extdyn = dynbuf;
3479 extdynend = extdyn + dynamic->size;
3480 for (; extdyn < extdynend; extdyn += extdynsize)
3481 {
3482 Elf_Internal_Dyn dyn;
3483 (*swap_dyn_in) (abfd, extdyn, &dyn);
3484
3485 if (dyn.d_tag == DT_NULL)
3486 break;
3487
3488 if (dyn.d_tag == DT_PPC64_GLINK)
3489 {
b9e5796b
AM
3490 /* The first glink stub starts at offset 32; see
3491 comment in ppc64_elf_finish_dynamic_sections. */
3492 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
468392fb
AM
3493 /* The .glink section usually does not survive the final
3494 link; search for the section (usually .text) where the
3495 glink stubs now reside. */
3496 glink = bfd_sections_find_if (abfd, section_covers_vma,
3497 &glink_vma);
3498 break;
3499 }
3500 }
3501
3502 free (dynbuf);
3503 }
3504
3505 if (glink != NULL)
3506 {
3507 /* Determine __glink trampoline by reading the relative branch
3508 from the first glink stub. */
3509 bfd_byte buf[4];
b9e5796b
AM
3510 unsigned int off = 0;
3511
3512 while (bfd_get_section_contents (abfd, glink, buf,
3513 glink_vma + off - glink->vma, 4))
468392fb
AM
3514 {
3515 unsigned int insn = bfd_get_32 (abfd, buf);
3516 insn ^= B_DOT;
3517 if ((insn & ~0x3fffffc) == 0)
b9e5796b
AM
3518 {
3519 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3520 break;
3521 }
3522 off += 4;
3523 if (off > 4)
3524 break;
468392fb
AM
3525 }
3526
3527 if (resolv_vma)
3528 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3529
066ee829
AM
3530 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3531 if (relplt != NULL)
3532 {
3533 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3534 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
c4b0b099 3535 goto free_contents_and_exit_err;
68ffbac6 3536
066ee829
AM
3537 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3538 size += plt_count * sizeof (asymbol);
468392fb 3539
066ee829
AM
3540 p = relplt->relocation;
3541 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3542 {
3543 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3544 if (p->addend != 0)
3545 size += sizeof ("+0x") - 1 + 16;
3546 }
066ee829 3547 }
468392fb
AM
3548 }
3549
c4b0b099
AM
3550 if (size == 0)
3551 goto free_contents_and_exit;
699733f6
AM
3552 s = *ret = bfd_malloc (size);
3553 if (s == NULL)
c4b0b099 3554 goto free_contents_and_exit_err;
90e3cdf2 3555
468392fb 3556 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3557
699733f6 3558 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3559 {
699733f6 3560 bfd_vma ent;
90e3cdf2 3561
5ef11c02
AM
3562 if (syms[i]->value > opd->size - 8)
3563 continue;
3564
699733f6
AM
3565 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3566 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3567 {
c9727e01 3568 long lo, hi;
699733f6 3569 size_t len;
c9727e01 3570 asection *sec = abfd->sections;
90e3cdf2 3571
699733f6
AM
3572 *s = *syms[i];
3573 lo = codesecsym;
3574 hi = codesecsymend;
3575 while (lo < hi)
3576 {
c9727e01 3577 long mid = (lo + hi) >> 1;
699733f6
AM
3578 if (syms[mid]->section->vma < ent)
3579 lo = mid + 1;
3580 else if (syms[mid]->section->vma > ent)
3581 hi = mid;
3582 else
c9727e01
AM
3583 {
3584 sec = syms[mid]->section;
3585 break;
3586 }
699733f6
AM
3587 }
3588
c9727e01 3589 if (lo >= hi && lo > codesecsym)
699733f6 3590 sec = syms[lo - 1]->section;
699733f6
AM
3591
3592 for (; sec != NULL; sec = sec->next)
3593 {
3594 if (sec->vma > ent)
3595 break;
63524580
JK
3596 /* SEC_LOAD may not be set if SEC is from a separate debug
3597 info file. */
3598 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3599 break;
3600 if ((sec->flags & SEC_CODE) != 0)
3601 s->section = sec;
3602 }
6ba2a415 3603 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3604 s->value = ent - s->section->vma;
3605 s->name = names;
3606 *names++ = '.';
3607 len = strlen (syms[i]->name);
3608 memcpy (names, syms[i]->name, len + 1);
3609 names += len + 1;
6f610d07
UW
3610 /* Have udata.p point back to the original symbol this
3611 synthetic symbol was derived from. */
3612 s->udata.p = syms[i];
699733f6 3613 s++;
90e3cdf2 3614 }
90e3cdf2 3615 }
699733f6 3616 free (contents);
468392fb
AM
3617
3618 if (glink != NULL && relplt != NULL)
3619 {
3620 if (resolv_vma)
3621 {
3622 /* Add a symbol for the main glink trampoline. */
86a4952b 3623 memset (s, 0, sizeof *s);
468392fb 3624 s->the_bfd = abfd;
6ba2a415 3625 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3626 s->section = glink;
3627 s->value = resolv_vma - glink->vma;
3628 s->name = names;
3629 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3630 names += sizeof ("__glink_PLTresolve");
3631 s++;
3632 count++;
3633 }
3634
3635 /* FIXME: It would be very much nicer to put sym@plt on the
3636 stub rather than on the glink branch table entry. The
3637 objdump disassembler would then use a sensible symbol
3638 name on plt calls. The difficulty in doing so is
3639 a) finding the stubs, and,
3640 b) matching stubs against plt entries, and,
3641 c) there can be multiple stubs for a given plt entry.
3642
3643 Solving (a) could be done by code scanning, but older
3644 ppc64 binaries used different stubs to current code.
3645 (b) is the tricky one since you need to known the toc
3646 pointer for at least one function that uses a pic stub to
3647 be able to calculate the plt address referenced.
3648 (c) means gdb would need to set multiple breakpoints (or
3649 find the glink branch itself) when setting breakpoints
3650 for pending shared library loads. */
3651 p = relplt->relocation;
3652 for (i = 0; i < plt_count; i++, p++)
3653 {
3654 size_t len;
3655
3656 *s = **p->sym_ptr_ptr;
3657 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3658 we are defining a symbol, ensure one of them is set. */
3659 if ((s->flags & BSF_LOCAL) == 0)
3660 s->flags |= BSF_GLOBAL;
6ba2a415 3661 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3662 s->section = glink;
3663 s->value = glink_vma - glink->vma;
3664 s->name = names;
3665 s->udata.p = NULL;
3666 len = strlen ((*p->sym_ptr_ptr)->name);
3667 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3668 names += len;
e054468f
AM
3669 if (p->addend != 0)
3670 {
3671 memcpy (names, "+0x", sizeof ("+0x") - 1);
3672 names += sizeof ("+0x") - 1;
3673 bfd_sprintf_vma (abfd, names, p->addend);
3674 names += strlen (names);
3675 }
468392fb
AM
3676 memcpy (names, "@plt", sizeof ("@plt"));
3677 names += sizeof ("@plt");
3678 s++;
b9e5796b
AM
3679 if (abi < 2)
3680 {
3681 glink_vma += 8;
3682 if (i >= 0x8000)
3683 glink_vma += 4;
3684 }
3685 else
468392fb
AM
3686 glink_vma += 4;
3687 }
3688 count += plt_count;
3689 }
90e3cdf2
JJ
3690 }
3691
c9727e01 3692 done:
a7535cf3 3693 free (syms);
90e3cdf2
JJ
3694 return count;
3695}
5bd4f169 3696\f
65f38f15
AM
3697/* The following functions are specific to the ELF linker, while
3698 functions above are used generally. Those named ppc64_elf_* are
3699 called by the main ELF linker code. They appear in this file more
3700 or less in the order in which they are called. eg.
3701 ppc64_elf_check_relocs is called early in the link process,
3702 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3703 called.
3704
3705 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3706 functions have both a function code symbol and a function descriptor
3707 symbol. A call to foo in a relocatable object file looks like:
3708
3709 . .text
3710 . x:
3711 . bl .foo
3712 . nop
3713
3714 The function definition in another object file might be:
3715
3716 . .section .opd
3717 . foo: .quad .foo
3718 . .quad .TOC.@tocbase
3719 . .quad 0
3720 .
3721 . .text
3722 . .foo: blr
3723
3724 When the linker resolves the call during a static link, the branch
3725 unsurprisingly just goes to .foo and the .opd information is unused.
3726 If the function definition is in a shared library, things are a little
3727 different: The call goes via a plt call stub, the opd information gets
3728 copied to the plt, and the linker patches the nop.
3729
3730 . x:
3731 . bl .foo_stub
3732 . ld 2,40(1)
3733 .
3734 .
3735 . .foo_stub:
71a39c98
AM
3736 . std 2,40(1) # in practice, the call stub
3737 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3738 . addi 11,11,Lfoo@toc@l # this is the general idea
3739 . ld 12,0(11)
3740 . ld 2,8(11)
3741 . mtctr 12
3742 . ld 11,16(11)
e86ce104
AM
3743 . bctr
3744 .
3745 . .section .plt
3746 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3747
3748 The "reloc ()" notation is supposed to indicate that the linker emits
3749 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3750 copying.
3751
3752 What are the difficulties here? Well, firstly, the relocations
3753 examined by the linker in check_relocs are against the function code
3754 sym .foo, while the dynamic relocation in the plt is emitted against
3755 the function descriptor symbol, foo. Somewhere along the line, we need
3756 to carefully copy dynamic link information from one symbol to the other.
3757 Secondly, the generic part of the elf linker will make .foo a dynamic
3758 symbol as is normal for most other backends. We need foo dynamic
3759 instead, at least for an application final link. However, when
3760 creating a shared library containing foo, we need to have both symbols
3761 dynamic so that references to .foo are satisfied during the early
3762 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3763 definition from some other object, eg. a static library.
3764
3765 Update: As of August 2004, we support a new convention. Function
3766 calls may use the function descriptor symbol, ie. "bl foo". This
3767 behaves exactly as "bl .foo". */
65f38f15 3768
1d483afe 3769/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3770 selects those that must be copied when linking a shared library,
3771 even when the symbol is local. */
65f38f15 3772
1d483afe
AM
3773static int
3774must_be_dyn_reloc (struct bfd_link_info *info,
3775 enum elf_ppc64_reloc_type r_type)
3776{
3777 switch (r_type)
3778 {
3779 default:
3780 return 1;
3781
3782 case R_PPC64_REL32:
3783 case R_PPC64_REL64:
3784 case R_PPC64_REL30:
3785 return 0;
3786
3787 case R_PPC64_TPREL16:
3788 case R_PPC64_TPREL16_LO:
3789 case R_PPC64_TPREL16_HI:
3790 case R_PPC64_TPREL16_HA:
3791 case R_PPC64_TPREL16_DS:
3792 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3793 case R_PPC64_TPREL16_HIGH:
3794 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3795 case R_PPC64_TPREL16_HIGHER:
3796 case R_PPC64_TPREL16_HIGHERA:
3797 case R_PPC64_TPREL16_HIGHEST:
3798 case R_PPC64_TPREL16_HIGHESTA:
3799 case R_PPC64_TPREL64:
0e1862bb 3800 return !bfd_link_executable (info);
1d483afe
AM
3801 }
3802}
65f38f15 3803
f4656909
AM
3804/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3805 copying dynamic variables from a shared lib into an app's dynbss
3806 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3807 shared lib. With code that gcc generates, it's vital that this be
3808 enabled; In the PowerPC64 ABI, the address of a function is actually
3809 the address of a function descriptor, which resides in the .opd
3810 section. gcc uses the descriptor directly rather than going via the
3811 GOT as some other ABI's do, which means that initialized function
3812 pointers must reference the descriptor. Thus, a function pointer
3813 initialized to the address of a function in a shared library will
3814 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3815 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3816 presents a problem as a plt entry for that function is also
3817 initialized from the function descriptor symbol and the copy reloc
3818 may not be initialized first. */
a23b6845 3819#define ELIMINATE_COPY_RELOCS 1
f4656909 3820
721956f4 3821/* Section name for stubs is the associated section name plus this
29942be8
NC
3822 string. */
3823#define STUB_SUFFIX ".stub"
721956f4
AM
3824
3825/* Linker stubs.
3826 ppc_stub_long_branch:
3827 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3828 destination, but a 24 bit branch in a stub section will reach.
3829 . b dest
3830
3831 ppc_stub_plt_branch:
3832 Similar to the above, but a 24 bit branch in the stub section won't
3833 reach its destination.
71a39c98
AM
3834 . addis %r11,%r2,xxx@toc@ha
3835 . ld %r12,xxx@toc@l(%r11)
3836 . mtctr %r12
721956f4
AM
3837 . bctr
3838
3839 ppc_stub_plt_call:
2c66dc6c
AM
3840 Used to call a function in a shared library. If it so happens that
3841 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3842 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3843 . std %r2,40(%r1)
71a39c98
AM
3844 . addis %r11,%r2,xxx@toc@ha
3845 . ld %r12,xxx+0@toc@l(%r11)
3846 . mtctr %r12
3847 . ld %r2,xxx+8@toc@l(%r11)
3848 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3849 . bctr
ad8e1ba5
AM
3850
3851 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3852 code to adjust the value and save r2 to support multiple toc sections.
3853 A ppc_stub_long_branch with an r2 offset looks like:
3854 . std %r2,40(%r1)
3855 . addis %r2,%r2,off@ha
3856 . addi %r2,%r2,off@l
3857 . b dest
3858
3859 A ppc_stub_plt_branch with an r2 offset looks like:
3860 . std %r2,40(%r1)
71a39c98
AM
3861 . addis %r11,%r2,xxx@toc@ha
3862 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3863 . addis %r2,%r2,off@ha
3864 . addi %r2,%r2,off@l
71a39c98 3865 . mtctr %r12
ad8e1ba5 3866 . bctr
ac2df442
AM
3867
3868 In cases where the "addis" instruction would add zero, the "addis" is
3869 omitted and following instructions modified slightly in some cases.
721956f4
AM
3870*/
3871
3872enum ppc_stub_type {
3873 ppc_stub_none,
3874 ppc_stub_long_branch,
ad8e1ba5 3875 ppc_stub_long_branch_r2off,
721956f4 3876 ppc_stub_plt_branch,
ad8e1ba5 3877 ppc_stub_plt_branch_r2off,
794e51c0 3878 ppc_stub_plt_call,
7341d5e2 3879 ppc_stub_plt_call_r2save,
a4b6fadd
AM
3880 ppc_stub_global_entry,
3881 ppc_stub_save_res
721956f4
AM
3882};
3883
6f20ed8a
AM
3884/* Information on stub grouping. */
3885struct map_stub
3886{
3887 /* The stub section. */
3888 asection *stub_sec;
3889 /* This is the section to which stubs in the group will be attached. */
3890 asection *link_sec;
a4b6fadd
AM
3891 /* Next group. */
3892 struct map_stub *next;
3893 /* Whether to emit a copy of register save/restore functions in this
3894 group. */
3895 int needs_save_res;
6f20ed8a
AM
3896};
3897
721956f4
AM
3898struct ppc_stub_hash_entry {
3899
3900 /* Base hash table entry structure. */
3901 struct bfd_hash_entry root;
3902
ad8e1ba5
AM
3903 enum ppc_stub_type stub_type;
3904
6f20ed8a
AM
3905 /* Group information. */
3906 struct map_stub *group;
721956f4
AM
3907
3908 /* Offset within stub_sec of the beginning of this stub. */
3909 bfd_vma stub_offset;
3910
3911 /* Given the symbol's value and its section we can determine its final
3912 value when building the stubs (so the stub knows where to jump. */
3913 bfd_vma target_value;
3914 asection *target_section;
3915
721956f4
AM
3916 /* The symbol table entry, if any, that this was derived from. */
3917 struct ppc_link_hash_entry *h;
e054468f 3918 struct plt_entry *plt_ent;
721956f4 3919
6911b7dc
AM
3920 /* Symbol st_other. */
3921 unsigned char other;
721956f4
AM
3922};
3923
3924struct ppc_branch_hash_entry {
3925
3926 /* Base hash table entry structure. */
3927 struct bfd_hash_entry root;
3928
c456f082 3929 /* Offset within branch lookup table. */
721956f4
AM
3930 unsigned int offset;
3931
3932 /* Generation marker. */
3933 unsigned int iter;
3934};
65f38f15 3935
19e08130
AM
3936/* Used to track dynamic relocations for local symbols. */
3937struct ppc_dyn_relocs
3938{
3939 struct ppc_dyn_relocs *next;
3940
3941 /* The input section of the reloc. */
3942 asection *sec;
3943
3944 /* Total number of relocs copied for the input section. */
3945 unsigned int count : 31;
3946
3947 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3948 unsigned int ifunc : 1;
3949};
3950
65f38f15
AM
3951struct ppc_link_hash_entry
3952{
3953 struct elf_link_hash_entry elf;
3954
b3fac117
AM
3955 union {
3956 /* A pointer to the most recently used stub hash entry against this
3957 symbol. */
3958 struct ppc_stub_hash_entry *stub_cache;
3959
3960 /* A pointer to the next symbol starting with a '.' */
3961 struct ppc_link_hash_entry *next_dot_sym;
3962 } u;
721956f4 3963
65f38f15 3964 /* Track dynamic relocs copied for this symbol. */
6061a67d 3965 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3966
d311bc8b
AM
3967 /* Chain of aliases referring to a weakdef. */
3968 struct ppc_link_hash_entry *weakref;
3969
721956f4 3970 /* Link between function code and descriptor symbols. */
34814b9f 3971 struct ppc_link_hash_entry *oh;
721956f4 3972
e86ce104
AM
3973 /* Flag function code and descriptor symbols. */
3974 unsigned int is_func:1;
3975 unsigned int is_func_descriptor:1;
908b32fc 3976 unsigned int fake:1;
411e1bfb 3977
c5614fa4
AM
3978 /* Whether global opd/toc sym has been adjusted or not.
3979 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3980 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3981 unsigned int adjust_done:1;
3982
a4b6fadd
AM
3983 /* Set if this is an out-of-line register save/restore function,
3984 with non-standard calling convention. */
3985 unsigned int save_res:1;
3986
411e1bfb 3987 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3988 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3989 corresponding relocs are encountered during check_relocs.
3990 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3991 indicate the corresponding GOT entry type is not needed.
3992 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3993 a TPREL one. We use a separate flag rather than setting TPREL
3994 just for convenience in distinguishing the two cases. */
3995#define TLS_GD 1 /* GD reloc. */
3996#define TLS_LD 2 /* LD reloc. */
3997#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3998#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3999#define TLS_TLS 16 /* Any TLS reloc. */
4000#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4001#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 4002#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 4003 unsigned char tls_mask;
65f38f15
AM
4004};
4005
4006/* ppc64 ELF linker hash table. */
4007
4008struct ppc_link_hash_table
4009{
4010 struct elf_link_hash_table elf;
4011
721956f4
AM
4012 /* The stub hash table. */
4013 struct bfd_hash_table stub_hash_table;
4014
4015 /* Another hash table for plt_branch stubs. */
4016 struct bfd_hash_table branch_hash_table;
4017
3b421ab3
AM
4018 /* Hash table for function prologue tocsave. */
4019 htab_t tocsave_htab;
4020
e7d1c40c
AM
4021 /* Various options and other info passed from the linker. */
4022 struct ppc64_elf_params *params;
721956f4 4023
6f20ed8a
AM
4024 /* The size of sec_info below. */
4025 unsigned int sec_info_arr_size;
4026
4027 /* Per-section array of extra section info. Done this way rather
4028 than as part of ppc64_elf_section_data so we have the info for
4029 non-ppc64 sections. */
4030 struct
4031 {
4032 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 4033 bfd_vma toc_off;
6f20ed8a
AM
4034
4035 union
4036 {
4037 /* The section group that this section belongs to. */
4038 struct map_stub *group;
4039 /* A temp section list pointer. */
4040 asection *list;
4041 } u;
4042 } *sec_info;
721956f4 4043
a4b6fadd
AM
4044 /* Linked list of groups. */
4045 struct map_stub *group;
4046
ad8e1ba5
AM
4047 /* Temp used when calculating TOC pointers. */
4048 bfd_vma toc_curr;
bf102f86
AM
4049 bfd *toc_bfd;
4050 asection *toc_first_sec;
ad8e1ba5 4051
b3fac117
AM
4052 /* Used when adding symbols. */
4053 struct ppc_link_hash_entry *dot_syms;
4054
33e44f2e 4055 /* Shortcuts to get to dynamic linker sections. */
4ce794b7
AM
4056 asection *dynbss;
4057 asection *relbss;
4058 asection *glink;
82bd7b59 4059 asection *sfpr;
4ce794b7
AM
4060 asection *brlt;
4061 asection *relbrlt;
58d180e8 4062 asection *glink_eh_frame;
ec338859 4063
8387904d
AM
4064 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4065 struct ppc_link_hash_entry *tls_get_addr;
4066 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4067
927be08e
AM
4068 /* The size of reliplt used by got entry relocs. */
4069 bfd_size_type got_reli_size;
4070
9b5ecbd0 4071 /* Statistics. */
7341d5e2 4072 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4073
ee75fd95
AM
4074 /* Number of stubs against global syms. */
4075 unsigned long stub_globals;
4076
ee67d69a
AM
4077 /* Set if we're linking code with function descriptors. */
4078 unsigned int opd_abi:1;
4079
4c52953f 4080 /* Support for multiple toc sections. */
33c0ec9d 4081 unsigned int do_multi_toc:1;
4c52953f 4082 unsigned int multi_toc_needed:1;
927be08e 4083 unsigned int second_toc_pass:1;
67f0cbdb 4084 unsigned int do_toc_opt:1;
4c52953f 4085
5d1634d7 4086 /* Set on error. */
99877b66 4087 unsigned int stub_error:1;
721956f4 4088
8c5b4e52
AM
4089 /* Whether func_desc_adjust needs to be run over symbols. */
4090 unsigned int need_func_desc_adj:1;
721956f4
AM
4091
4092 /* Incremented every time we size stubs. */
4093 unsigned int stub_iteration;
5d1634d7 4094
87d72d41
AM
4095 /* Small local sym cache. */
4096 struct sym_cache sym_cache;
65f38f15
AM
4097};
4098
4c52953f
AM
4099/* Rename some of the generic section flags to better document how they
4100 are used here. */
b0dddeec
AM
4101
4102/* Nonzero if this section has TLS related relocations. */
4103#define has_tls_reloc sec_flg0
4104
4105/* Nonzero if this section has a call to __tls_get_addr. */
4106#define has_tls_get_addr_call sec_flg1
4107
4108/* Nonzero if this section has any toc or got relocs. */
4109#define has_toc_reloc sec_flg2
4110
4111/* Nonzero if this section has a call to another section that uses
4112 the toc or got. */
d77c8a4b 4113#define makes_toc_func_call sec_flg3
b0dddeec
AM
4114
4115/* Recursion protection when determining above flag. */
d77c8a4b 4116#define call_check_in_progress sec_flg4
70cc837d 4117#define call_check_done sec_flg5
4c52953f 4118
65f38f15
AM
4119/* Get the ppc64 ELF linker hash table from a link_info structure. */
4120
4121#define ppc_hash_table(p) \
4dfe6ac6
NC
4122 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4123 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4124
721956f4
AM
4125#define ppc_stub_hash_lookup(table, string, create, copy) \
4126 ((struct ppc_stub_hash_entry *) \
4127 bfd_hash_lookup ((table), (string), (create), (copy)))
4128
4129#define ppc_branch_hash_lookup(table, string, create, copy) \
4130 ((struct ppc_branch_hash_entry *) \
4131 bfd_hash_lookup ((table), (string), (create), (copy)))
4132
4133/* Create an entry in the stub hash table. */
4134
4135static struct bfd_hash_entry *
4ce794b7
AM
4136stub_hash_newfunc (struct bfd_hash_entry *entry,
4137 struct bfd_hash_table *table,
4138 const char *string)
721956f4
AM
4139{
4140 /* Allocate the structure if it has not already been allocated by a
4141 subclass. */
4142 if (entry == NULL)
4143 {
4144 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4145 if (entry == NULL)
4146 return entry;
4147 }
4148
4149 /* Call the allocation method of the superclass. */
4150 entry = bfd_hash_newfunc (entry, table, string);
4151 if (entry != NULL)
4152 {
4153 struct ppc_stub_hash_entry *eh;
4154
4155 /* Initialize the local fields. */
4156 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4157 eh->stub_type = ppc_stub_none;
6f20ed8a 4158 eh->group = NULL;
721956f4
AM
4159 eh->stub_offset = 0;
4160 eh->target_value = 0;
4161 eh->target_section = NULL;
721956f4 4162 eh->h = NULL;
6911b7dc 4163 eh->plt_ent = NULL;
6911b7dc 4164 eh->other = 0;
721956f4
AM
4165 }
4166
4167 return entry;
4168}
4169
4170/* Create an entry in the branch hash table. */
4171
4172static struct bfd_hash_entry *
4ce794b7
AM
4173branch_hash_newfunc (struct bfd_hash_entry *entry,
4174 struct bfd_hash_table *table,
4175 const char *string)
721956f4
AM
4176{
4177 /* Allocate the structure if it has not already been allocated by a
4178 subclass. */
4179 if (entry == NULL)
4180 {
4181 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4182 if (entry == NULL)
4183 return entry;
4184 }
4185
4186 /* Call the allocation method of the superclass. */
4187 entry = bfd_hash_newfunc (entry, table, string);
4188 if (entry != NULL)
4189 {
4190 struct ppc_branch_hash_entry *eh;
4191
4192 /* Initialize the local fields. */
4193 eh = (struct ppc_branch_hash_entry *) entry;
4194 eh->offset = 0;
4195 eh->iter = 0;
4196 }
4197
4198 return entry;
4199}
4200
65f38f15
AM
4201/* Create an entry in a ppc64 ELF linker hash table. */
4202
4203static struct bfd_hash_entry *
4ce794b7
AM
4204link_hash_newfunc (struct bfd_hash_entry *entry,
4205 struct bfd_hash_table *table,
4206 const char *string)
65f38f15
AM
4207{
4208 /* Allocate the structure if it has not already been allocated by a
4209 subclass. */
4210 if (entry == NULL)
4211 {
4212 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4213 if (entry == NULL)
4214 return entry;
4215 }
4216
4217 /* Call the allocation method of the superclass. */
4218 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4219 if (entry != NULL)
4220 {
4221 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4222
b3fac117 4223 memset (&eh->u.stub_cache, 0,
908b32fc 4224 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4225 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4226
4227 /* When making function calls, old ABI code references function entry
4228 points (dot symbols), while new ABI code references the function
4229 descriptor symbol. We need to make any combination of reference and
4230 definition work together, without breaking archive linking.
4231
4232 For a defined function "foo" and an undefined call to "bar":
4233 An old object defines "foo" and ".foo", references ".bar" (possibly
4234 "bar" too).
4235 A new object defines "foo" and references "bar".
4236
4237 A new object thus has no problem with its undefined symbols being
4238 satisfied by definitions in an old object. On the other hand, the
4239 old object won't have ".bar" satisfied by a new object.
4240
4241 Keep a list of newly added dot-symbols. */
4242
4243 if (string[0] == '.')
4244 {
4245 struct ppc_link_hash_table *htab;
4246
4247 htab = (struct ppc_link_hash_table *) table;
4248 eh->u.next_dot_sym = htab->dot_syms;
4249 htab->dot_syms = eh;
4250 }
65f38f15
AM
4251 }
4252
4253 return entry;
4254}
4255
3b421ab3
AM
4256struct tocsave_entry {
4257 asection *sec;
4258 bfd_vma offset;
4259};
4260
4261static hashval_t
4262tocsave_htab_hash (const void *p)
4263{
4264 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4265 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4266}
4267
4268static int
4269tocsave_htab_eq (const void *p1, const void *p2)
4270{
4271 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4272 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4273 return e1->sec == e2->sec && e1->offset == e2->offset;
4274}
4275
68faa637
AM
4276/* Destroy a ppc64 ELF linker hash table. */
4277
4278static void
d495ab0d 4279ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4280{
d495ab0d 4281 struct ppc_link_hash_table *htab;
68faa637 4282
d495ab0d 4283 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4284 if (htab->tocsave_htab)
4285 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4286 bfd_hash_table_free (&htab->branch_hash_table);
4287 bfd_hash_table_free (&htab->stub_hash_table);
4288 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4289}
4290
65f38f15
AM
4291/* Create a ppc64 ELF linker hash table. */
4292
4293static struct bfd_link_hash_table *
4ce794b7 4294ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4295{
4296 struct ppc_link_hash_table *htab;
4297 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4298
4ce794b7 4299 htab = bfd_zmalloc (amt);
65f38f15
AM
4300 if (htab == NULL)
4301 return NULL;
4302
66eb6687 4303 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4304 sizeof (struct ppc_link_hash_entry),
4305 PPC64_ELF_DATA))
65f38f15 4306 {
e2d34d7d 4307 free (htab);
65f38f15
AM
4308 return NULL;
4309 }
4310
721956f4 4311 /* Init the stub hash table too. */
66eb6687
AM
4312 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4313 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4314 {
d495ab0d 4315 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4316 return NULL;
4317 }
721956f4
AM
4318
4319 /* And the branch hash table. */
66eb6687
AM
4320 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4321 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4322 {
4323 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4324 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4325 return NULL;
4326 }
721956f4 4327
3b421ab3
AM
4328 htab->tocsave_htab = htab_try_create (1024,
4329 tocsave_htab_hash,
4330 tocsave_htab_eq,
4331 NULL);
4332 if (htab->tocsave_htab == NULL)
2915c55b 4333 {
d495ab0d 4334 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4335 return NULL;
4336 }
d495ab0d 4337 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4338
3254fd24
AM
4339 /* Initializing two fields of the union is just cosmetic. We really
4340 only care about glist, but when compiled on a 32-bit host the
4341 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4342 debugger inspection of these fields look nicer. */
a6aa5195
AM
4343 htab->elf.init_got_refcount.refcount = 0;
4344 htab->elf.init_got_refcount.glist = NULL;
4345 htab->elf.init_plt_refcount.refcount = 0;
4346 htab->elf.init_plt_refcount.glist = NULL;
4347 htab->elf.init_got_offset.offset = 0;
4348 htab->elf.init_got_offset.glist = NULL;
4349 htab->elf.init_plt_offset.offset = 0;
4350 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4351
65f38f15
AM
4352 return &htab->elf.root;
4353}
4354
bfeb4a28
AM
4355/* Create sections for linker generated code. */
4356
4357static bfd_boolean
4358create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4359{
4360 struct ppc_link_hash_table *htab;
4361 flagword flags;
4362
4363 htab = ppc_hash_table (info);
4364
bfeb4a28
AM
4365 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4366 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4367 if (htab->params->save_restore_funcs)
4368 {
4369 /* Create .sfpr for code to save and restore fp regs. */
4370 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4371 flags);
4372 if (htab->sfpr == NULL
4373 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4374 return FALSE;
4375 }
4376
4377 if (bfd_link_relocatable (info))
4378 return TRUE;
bfeb4a28
AM
4379
4380 /* Create .glink for lazy dynamic linking support. */
4381 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4382 flags);
4383 if (htab->glink == NULL
4384 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4385 return FALSE;
4386
4387 if (!info->no_ld_generated_unwind_info)
4388 {
4389 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4390 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4391 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4392 ".eh_frame",
4393 flags);
4394 if (htab->glink_eh_frame == NULL
4395 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4396 return FALSE;
4397 }
4398
4399 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4400 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4401 if (htab->elf.iplt == NULL
4402 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4403 return FALSE;
4404
4405 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4406 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4407 htab->elf.irelplt
4408 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4409 if (htab->elf.irelplt == NULL
4410 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4411 return FALSE;
4412
4413 /* Create branch lookup table for plt_branch stubs. */
4414 flags = (SEC_ALLOC | SEC_LOAD
4415 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4416 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4417 flags);
4418 if (htab->brlt == NULL
4419 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4420 return FALSE;
4421
0e1862bb 4422 if (!bfd_link_pic (info))
bfeb4a28
AM
4423 return TRUE;
4424
4425 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4426 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4427 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4428 ".rela.branch_lt",
4429 flags);
4430 if (htab->relbrlt == NULL
4431 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4432 return FALSE;
4433
4434 return TRUE;
4435}
4436
e717da7e
AM
4437/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4438
bfeb4a28 4439bfd_boolean
e7d1c40c
AM
4440ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4441 struct ppc64_elf_params *params)
e717da7e
AM
4442{
4443 struct ppc_link_hash_table *htab;
4444
e7d1c40c 4445 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4446
4447/* Always hook our dynamic sections into the first bfd, which is the
4448 linker created stub bfd. This ensures that the GOT header is at
4449 the start of the output TOC section. */
4450 htab = ppc_hash_table (info);
e7d1c40c
AM
4451 htab->elf.dynobj = params->stub_bfd;
4452 htab->params = params;
bfeb4a28 4453
bfeb4a28 4454 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4455}
4456
721956f4
AM
4457/* Build a name for an entry in the stub hash table. */
4458
4459static char *
4ce794b7
AM
4460ppc_stub_name (const asection *input_section,
4461 const asection *sym_sec,
4462 const struct ppc_link_hash_entry *h,
4463 const Elf_Internal_Rela *rel)
721956f4
AM
4464{
4465 char *stub_name;
bcaa2f82 4466 ssize_t len;
721956f4
AM
4467
4468 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4469 offsets from a sym as a branch target? In fact, we could
4470 probably assume the addend is always zero. */
4471 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4472
4473 if (h)
4474 {
4475 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4476 stub_name = bfd_malloc (len);
46de2a7c
AM
4477 if (stub_name == NULL)
4478 return stub_name;
4479
bcaa2f82
AM
4480 len = sprintf (stub_name, "%08x.%s+%x",
4481 input_section->id & 0xffffffff,
4482 h->elf.root.root.string,
4483 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4484 }
4485 else
4486 {
ad8e1ba5 4487 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4488 stub_name = bfd_malloc (len);
46de2a7c
AM
4489 if (stub_name == NULL)
4490 return stub_name;
4491
bcaa2f82
AM
4492 len = sprintf (stub_name, "%08x.%x:%x+%x",
4493 input_section->id & 0xffffffff,
4494 sym_sec->id & 0xffffffff,
4495 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4496 (int) rel->r_addend & 0xffffffff);
721956f4 4497 }
bcaa2f82 4498 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4499 stub_name[len - 2] = 0;
721956f4
AM
4500 return stub_name;
4501}
4502
4503/* Look up an entry in the stub hash. Stub entries are cached because
4504 creating the stub name takes a bit of time. */
4505
4506static struct ppc_stub_hash_entry *
4ce794b7
AM
4507ppc_get_stub_entry (const asection *input_section,
4508 const asection *sym_sec,
039b3fef 4509 struct ppc_link_hash_entry *h,
4ce794b7
AM
4510 const Elf_Internal_Rela *rel,
4511 struct ppc_link_hash_table *htab)
721956f4
AM
4512{
4513 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4514 struct map_stub *group;
721956f4
AM
4515
4516 /* If this input section is part of a group of sections sharing one
4517 stub section, then use the id of the first section in the group.
4518 Stub names need to include a section id, as there may well be
4519 more than one stub used to reach say, printf, and we need to
4520 distinguish between them. */
6f20ed8a 4521 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4522 if (group == NULL)
4523 return NULL;
721956f4 4524
b3fac117
AM
4525 if (h != NULL && h->u.stub_cache != NULL
4526 && h->u.stub_cache->h == h
6f20ed8a 4527 && h->u.stub_cache->group == group)
721956f4 4528 {
b3fac117 4529 stub_entry = h->u.stub_cache;
721956f4
AM
4530 }
4531 else
4532 {
4533 char *stub_name;
4534
6f20ed8a 4535 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4536 if (stub_name == NULL)
4537 return NULL;
4538
4539 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4540 stub_name, FALSE, FALSE);
721956f4 4541 if (h != NULL)
b3fac117 4542 h->u.stub_cache = stub_entry;
721956f4
AM
4543
4544 free (stub_name);
4545 }
4546
4547 return stub_entry;
4548}
4549
4550/* Add a new stub entry to the stub hash. Not all fields of the new
4551 stub entry are initialised. */
4552
4553static struct ppc_stub_hash_entry *
4ce794b7
AM
4554ppc_add_stub (const char *stub_name,
4555 asection *section,
25f53a85 4556 struct bfd_link_info *info)
721956f4 4557{
25f53a85 4558 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4559 struct map_stub *group;
721956f4
AM
4560 asection *link_sec;
4561 asection *stub_sec;
4562 struct ppc_stub_hash_entry *stub_entry;
4563
6f20ed8a
AM
4564 group = htab->sec_info[section->id].u.group;
4565 link_sec = group->link_sec;
4566 stub_sec = group->stub_sec;
721956f4
AM
4567 if (stub_sec == NULL)
4568 {
6f20ed8a
AM
4569 size_t namelen;
4570 bfd_size_type len;
4571 char *s_name;
721956f4 4572
6f20ed8a
AM
4573 namelen = strlen (link_sec->name);
4574 len = namelen + sizeof (STUB_SUFFIX);
4575 s_name = bfd_alloc (htab->params->stub_bfd, len);
4576 if (s_name == NULL)
4577 return NULL;
721956f4 4578
6f20ed8a
AM
4579 memcpy (s_name, link_sec->name, namelen);
4580 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4581 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4582 if (stub_sec == NULL)
4583 return NULL;
4584 group->stub_sec = stub_sec;
721956f4
AM
4585 }
4586
4587 /* Enter this entry into the linker stub hash table. */
4588 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4589 TRUE, FALSE);
721956f4
AM
4590 if (stub_entry == NULL)
4591 {
695344c0 4592 /* xgettext:c-format */
8de848d8 4593 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4594 section->owner, stub_name);
721956f4
AM
4595 return NULL;
4596 }
4597
6f20ed8a 4598 stub_entry->group = group;
721956f4 4599 stub_entry->stub_offset = 0;
721956f4
AM
4600 return stub_entry;
4601}
4602
e717da7e
AM
4603/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4604 not already done. */
65f38f15 4605
b34976b6 4606static bfd_boolean
e717da7e 4607create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4608{
e717da7e
AM
4609 asection *got, *relgot;
4610 flagword flags;
4611 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4612
0c8d6e5c 4613 if (!is_ppc64_elf (abfd))
0ffa91dd 4614 return FALSE;
4dfe6ac6
NC
4615 if (htab == NULL)
4616 return FALSE;
0ffa91dd 4617
33e44f2e
AM
4618 if (!htab->elf.sgot
4619 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4620 return FALSE;
e717da7e
AM
4621
4622 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4623 | SEC_LINKER_CREATED);
4624
c456f082 4625 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4626 if (!got
e717da7e
AM
4627 || !bfd_set_section_alignment (abfd, got, 3))
4628 return FALSE;
65f38f15 4629
c456f082
AM
4630 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4631 flags | SEC_READONLY);
e717da7e 4632 if (!relgot
e717da7e 4633 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4634 return FALSE;
e717da7e
AM
4635
4636 ppc64_elf_tdata (abfd)->got = got;
4637 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4638 return TRUE;
65f38f15 4639}
5bd4f169 4640
82bd7b59 4641/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4642
b34976b6 4643static bfd_boolean
4ce794b7 4644ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4645{
65f38f15 4646 struct ppc_link_hash_table *htab;
5bd4f169 4647
65f38f15 4648 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4649 return FALSE;
65f38f15 4650
e717da7e 4651 htab = ppc_hash_table (info);
4dfe6ac6
NC
4652 if (htab == NULL)
4653 return FALSE;
4654
3d4d4302 4655 htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
0e1862bb 4656 if (!bfd_link_pic (info))
3d4d4302 4657 htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
65f38f15 4658
33e44f2e 4659 if (!htab->elf.sgot || !htab->elf.splt || !htab->elf.srelplt || !htab->dynbss
0e1862bb 4660 || (!bfd_link_pic (info) && !htab->relbss))
65f38f15
AM
4661 abort ();
4662
b34976b6 4663 return TRUE;
5bd4f169
AM
4664}
4665
b31867b6
AM
4666/* Follow indirect and warning symbol links. */
4667
4668static inline struct bfd_link_hash_entry *
4669follow_link (struct bfd_link_hash_entry *h)
4670{
4671 while (h->type == bfd_link_hash_indirect
4672 || h->type == bfd_link_hash_warning)
4673 h = h->u.i.link;
4674 return h;
4675}
4676
4677static inline struct elf_link_hash_entry *
4678elf_follow_link (struct elf_link_hash_entry *h)
4679{
4680 return (struct elf_link_hash_entry *) follow_link (&h->root);
4681}
4682
4683static inline struct ppc_link_hash_entry *
4684ppc_follow_link (struct ppc_link_hash_entry *h)
4685{
4686 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4687}
4688
40d16e0b
AM
4689/* Merge PLT info on FROM with that on TO. */
4690
4691static void
4692move_plt_plist (struct ppc_link_hash_entry *from,
4693 struct ppc_link_hash_entry *to)
4694{
4695 if (from->elf.plt.plist != NULL)
4696 {
4697 if (to->elf.plt.plist != NULL)
4698 {
4699 struct plt_entry **entp;
4700 struct plt_entry *ent;
4701
4702 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4703 {
4704 struct plt_entry *dent;
4705
4706 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4707 if (dent->addend == ent->addend)
4708 {
4709 dent->plt.refcount += ent->plt.refcount;
4710 *entp = ent->next;
4711 break;
4712 }
4713 if (dent == NULL)
4714 entp = &ent->next;
4715 }
4716 *entp = to->elf.plt.plist;
4717 }
4718
4719 to->elf.plt.plist = from->elf.plt.plist;
4720 from->elf.plt.plist = NULL;
4721 }
4722}
4723
65f38f15
AM
4724/* Copy the extra info we tack onto an elf_link_hash_entry. */
4725
4726static void
fcfa13d2
AM
4727ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4728 struct elf_link_hash_entry *dir,
4729 struct elf_link_hash_entry *ind)
65f38f15
AM
4730{
4731 struct ppc_link_hash_entry *edir, *eind;
4732
4733 edir = (struct ppc_link_hash_entry *) dir;
4734 eind = (struct ppc_link_hash_entry *) ind;
4735
c79d6685
AM
4736 edir->is_func |= eind->is_func;
4737 edir->is_func_descriptor |= eind->is_func_descriptor;
4738 edir->tls_mask |= eind->tls_mask;
4739 if (eind->oh != NULL)
4740 edir->oh = ppc_follow_link (eind->oh);
4741
474436e6
AM
4742 if (edir->elf.versioned != versioned_hidden)
4743 {
4744 /* If called to transfer flags for a weakdef during processing
4745 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4746 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4747 if (!(ELIMINATE_COPY_RELOCS
4748 && eind->elf.root.type != bfd_link_hash_indirect
4749 && edir->elf.dynamic_adjusted))
4750 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4751
4752 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4753 edir->elf.ref_regular |= eind->elf.ref_regular;
4754 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4755 edir->elf.needs_plt |= eind->elf.needs_plt;
4756 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4757 }
c79d6685 4758
d311bc8b
AM
4759 /* If we were called to copy over info for a weak sym, don't copy
4760 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4761 in order to simplify readonly_dynrelocs and save a field in the
4762 symbol hash entry, but that means dyn_relocs can't be used in any
4763 tests about a specific symbol, or affect other symbol flags which
4764 are then tested.
4765 Chain weakdefs so we can get from the weakdef back to an alias.
4766 The list is circular so that we don't need to use u.weakdef as
4767 well as this list to look at all aliases. */
4768 if (eind->elf.root.type != bfd_link_hash_indirect)
4769 {
4770 struct ppc_link_hash_entry *cur, *add, *next;
4771
4772 add = eind;
4773 do
4774 {
4775 cur = edir->weakref;
4776 if (cur != NULL)
4777 {
4778 do
4779 {
4780 /* We can be called twice for the same symbols.
4781 Don't make multiple loops. */
4782 if (cur == add)
4783 return;
4784 cur = cur->weakref;
4785 } while (cur != edir);
4786 }
4787 next = add->weakref;
4788 if (cur != add)
4789 {
4790 add->weakref = edir->weakref != NULL ? edir->weakref : edir;
4791 edir->weakref = add;
4792 }
4793 add = next;
4794 } while (add != NULL && add != eind);
4795 return;
4796 }
4797
411e1bfb 4798 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4799 if (eind->dyn_relocs != NULL)
65f38f15 4800 {
bbd7ec4a
AM
4801 if (edir->dyn_relocs != NULL)
4802 {
6061a67d
AM
4803 struct elf_dyn_relocs **pp;
4804 struct elf_dyn_relocs *p;
bbd7ec4a 4805
fcfa13d2 4806 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4807 list. Merge any entries against the same section. */
4808 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4809 {
6061a67d 4810 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4811
4812 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4813 if (q->sec == p->sec)
4814 {
4815 q->pc_count += p->pc_count;
4816 q->count += p->count;
4817 *pp = p->next;
4818 break;
4819 }
4820 if (q == NULL)
4821 pp = &p->next;
4822 }
4823 *pp = edir->dyn_relocs;
4824 }
4825
65f38f15
AM
4826 edir->dyn_relocs = eind->dyn_relocs;
4827 eind->dyn_relocs = NULL;
4828 }
65f38f15 4829
81848ca0
AM
4830 /* Copy over got entries that we may have already seen to the
4831 symbol which just became indirect. */
411e1bfb
AM
4832 if (eind->elf.got.glist != NULL)
4833 {
4834 if (edir->elf.got.glist != NULL)
4835 {
4836 struct got_entry **entp;
4837 struct got_entry *ent;
4838
4839 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4840 {
4841 struct got_entry *dent;
4842
4843 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4844 if (dent->addend == ent->addend
e717da7e 4845 && dent->owner == ent->owner
411e1bfb
AM
4846 && dent->tls_type == ent->tls_type)
4847 {
4848 dent->got.refcount += ent->got.refcount;
4849 *entp = ent->next;
4850 break;
4851 }
4852 if (dent == NULL)
4853 entp = &ent->next;
4854 }
4855 *entp = edir->elf.got.glist;
4856 }
4857
4858 edir->elf.got.glist = eind->elf.got.glist;
4859 eind->elf.got.glist = NULL;
4860 }
4861
4862 /* And plt entries. */
40d16e0b 4863 move_plt_plist (eind, edir);
411e1bfb 4864
fcfa13d2 4865 if (eind->elf.dynindx != -1)
411e1bfb 4866 {
fcfa13d2
AM
4867 if (edir->elf.dynindx != -1)
4868 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4869 edir->elf.dynstr_index);
411e1bfb
AM
4870 edir->elf.dynindx = eind->elf.dynindx;
4871 edir->elf.dynstr_index = eind->elf.dynstr_index;
4872 eind->elf.dynindx = -1;
4873 eind->elf.dynstr_index = 0;
4874 }
411e1bfb
AM
4875}
4876
8387904d
AM
4877/* Find the function descriptor hash entry from the given function code
4878 hash entry FH. Link the entries via their OH fields. */
4879
4880static struct ppc_link_hash_entry *
b31867b6 4881lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4882{
4883 struct ppc_link_hash_entry *fdh = fh->oh;
4884
4885 if (fdh == NULL)
4886 {
4887 const char *fd_name = fh->elf.root.root.string + 1;
4888
4889 fdh = (struct ppc_link_hash_entry *)
4890 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4891 if (fdh == NULL)
4892 return fdh;
4893
4894 fdh->is_func_descriptor = 1;
4895 fdh->oh = fh;
4896 fh->is_func = 1;
4897 fh->oh = fdh;
8387904d
AM
4898 }
4899
8c5b4e52
AM
4900 fdh = ppc_follow_link (fdh);
4901 fdh->is_func_descriptor = 1;
4902 fdh->oh = fh;
4903 return fdh;
8387904d
AM
4904}
4905
8c5b4e52 4906/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4907
4908static struct ppc_link_hash_entry *
4909make_fdh (struct bfd_link_info *info,
908b32fc 4910 struct ppc_link_hash_entry *fh)
bb700d78 4911{
8c5b4e52
AM
4912 bfd *abfd = fh->elf.root.u.undef.abfd;
4913 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4914 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4915 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4916 ? BSF_WEAK
4917 : BSF_GLOBAL);
4918
4919 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4920 fh->elf.root.root.string + 1,
4921 flags, bfd_und_section_ptr, 0,
4922 NULL, FALSE, FALSE, &bh))
bb700d78
AM
4923 return NULL;
4924
4925 fdh = (struct ppc_link_hash_entry *) bh;
4926 fdh->elf.non_elf = 0;
908b32fc
AM
4927 fdh->fake = 1;
4928 fdh->is_func_descriptor = 1;
4929 fdh->oh = fh;
4930 fh->is_func = 1;
4931 fh->oh = fdh;
bb700d78
AM
4932 return fdh;
4933}
4934
8387904d
AM
4935/* Fix function descriptor symbols defined in .opd sections to be
4936 function type. */
555cd476
AM
4937
4938static bfd_boolean
c16153ae 4939ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4940 struct bfd_link_info *info,
555cd476 4941 Elf_Internal_Sym *isym,
6911b7dc 4942 const char **name,
555cd476
AM
4943 flagword *flags ATTRIBUTE_UNUSED,
4944 asection **sec,
b53dfeb2 4945 bfd_vma *value)
555cd476 4946{
a43942db 4947 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
f1885d1e
AM
4948 && (ibfd->flags & DYNAMIC) == 0
4949 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
a43942db 4950 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
f64b2e8d 4951
b53dfeb2 4952 if (*sec != NULL
f1885d1e 4953 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4954 {
4955 asection *code_sec;
4956
4957 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4958 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4959 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4960
4961 /* If the symbol is a function defined in .opd, and the function
4962 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4963 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4964 && (*sec)->reloc_count != 0
4965 && opd_entry_value (*sec, *value, &code_sec, NULL,
4966 FALSE) != (bfd_vma) -1
4967 && discarded_section (code_sec))
4968 {
4969 *sec = bfd_und_section_ptr;
4970 isym->st_shndx = SHN_UNDEF;
4971 }
4972 }
dbd1e97e
AM
4973 else if (*sec != NULL
4974 && strcmp ((*sec)->name, ".toc") == 0
4975 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4976 {
4977 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4978 if (htab != NULL)
4979 htab->params->object_in_toc = 1;
4980 }
433817dd 4981
6911b7dc
AM
4982 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4983 {
4984 if (abiversion (ibfd) == 0)
4985 set_abiversion (ibfd, 2);
4986 else if (abiversion (ibfd) == 1)
4987 {
4988 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4989 " for ABI version 1\n"), name);
4990 bfd_set_error (bfd_error_bad_value);
4991 return FALSE;
4992 }
4993 }
4994
555cd476
AM
4995 return TRUE;
4996}
4997
6911b7dc
AM
4998/* Merge non-visibility st_other attributes: local entry point. */
4999
5000static void
5001ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5002 const Elf_Internal_Sym *isym,
5003 bfd_boolean definition,
5004 bfd_boolean dynamic)
5005{
5006 if (definition && !dynamic)
5007 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5008 | ELF_ST_VISIBILITY (h->other));
5009}
5010
8c5b4e52
AM
5011/* Hook called on merging a symbol. We use this to clear "fake" since
5012 we now have a real symbol. */
5013
5014static bfd_boolean
5015ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5016 const Elf_Internal_Sym *isym ATTRIBUTE_UNUSED,
5017 asection **psec ATTRIBUTE_UNUSED,
5018 bfd_boolean newdef ATTRIBUTE_UNUSED,
5019 bfd_boolean olddef ATTRIBUTE_UNUSED,
5020 bfd *oldbfd ATTRIBUTE_UNUSED,
5021 const asection *oldsec ATTRIBUTE_UNUSED)
5022{
5023 ((struct ppc_link_hash_entry *) h)->fake = 0;
5024 return TRUE;
5025}
5026
8387904d 5027/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5028 inclusion of a new ABI object archive that defines "bar".
5029 NAME is a symbol defined in an archive. Return a symbol in the hash
5030 table that might be satisfied by the archive symbols. */
8387904d
AM
5031
5032static struct elf_link_hash_entry *
5033ppc64_elf_archive_symbol_lookup (bfd *abfd,
5034 struct bfd_link_info *info,
5035 const char *name)
5036{
5037 struct elf_link_hash_entry *h;
5038 char *dot_name;
5039 size_t len;
5040
5041 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5042 if (h != NULL
5043 /* Don't return this sym if it is a fake function descriptor
5044 created by add_symbol_adjust. */
8c5b4e52 5045 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
5046 return h;
5047
5048 if (name[0] == '.')
5049 return h;
5050
5051 len = strlen (name);
5052 dot_name = bfd_alloc (abfd, len + 2);
5053 if (dot_name == NULL)
5054 return (struct elf_link_hash_entry *) 0 - 1;
5055 dot_name[0] = '.';
5056 memcpy (dot_name + 1, name, len + 1);
5057 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5058 bfd_release (abfd, dot_name);
5059 return h;
5060}
5061
5062/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5063 new ABI object defines "bar". Well, at least, undefined dot symbols
5064 are made weak. This stops later archive searches from including an
5065 object if we already have a function descriptor definition. It also
35b0ce59
AM
5066 prevents the linker complaining about undefined symbols.
5067 We also check and correct mismatched symbol visibility here. The
5068 most restrictive visibility of the function descriptor and the
5069 function entry symbol is used. */
8387904d
AM
5070
5071static bfd_boolean
b3fac117 5072add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5073{
8387904d
AM
5074 struct ppc_link_hash_table *htab;
5075 struct ppc_link_hash_entry *fdh;
5076
b3fac117
AM
5077 if (eh->elf.root.type == bfd_link_hash_warning)
5078 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5079
8400d40d
AM
5080 if (eh->elf.root.type == bfd_link_hash_indirect)
5081 return TRUE;
5082
b3fac117
AM
5083 if (eh->elf.root.root.string[0] != '.')
5084 abort ();
8387904d 5085
b3fac117 5086 htab = ppc_hash_table (info);
4dfe6ac6
NC
5087 if (htab == NULL)
5088 return FALSE;
5089
b31867b6 5090 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
5091 if (fdh == NULL
5092 && !bfd_link_relocatable (info)
5093 && (eh->elf.root.type == bfd_link_hash_undefined
5094 || eh->elf.root.type == bfd_link_hash_undefweak)
5095 && eh->elf.ref_regular)
5096 {
5097 /* Make an undefined function descriptor sym, in order to
5098 pull in an --as-needed shared lib. Archives are handled
5099 elsewhere. */
5100 fdh = make_fdh (info, eh);
5101 if (fdh == NULL)
5102 return FALSE;
bb700d78 5103 }
8c5b4e52
AM
5104
5105 if (fdh != NULL)
8387904d 5106 {
35b0ce59
AM
5107 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5108 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
5109
5110 /* Make both descriptor and entry symbol have the most
5111 constraining visibility of either symbol. */
35b0ce59
AM
5112 if (entry_vis < descr_vis)
5113 fdh->elf.other += entry_vis - descr_vis;
5114 else if (entry_vis > descr_vis)
5115 eh->elf.other += descr_vis - entry_vis;
5116
8c5b4e52
AM
5117 /* Propagate reference flags from entry symbol to function
5118 descriptor symbol. */
5119 fdh->elf.root.non_ir_ref |= eh->elf.root.non_ir_ref;
5120 fdh->elf.ref_regular |= eh->elf.ref_regular;
5121 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5122
5123 if (!fdh->elf.forced_local
5124 && fdh->elf.dynindx == -1
5125 && fdh->elf.versioned != versioned_hidden
5126 && (bfd_link_dll (info)
5127 || fdh->elf.def_dynamic
5128 || fdh->elf.ref_dynamic)
5129 && (eh->elf.ref_regular
5130 || eh->elf.def_regular))
5131 {
5132 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5133 return FALSE;
35b0ce59 5134 }
8387904d 5135 }
99877b66 5136
8387904d
AM
5137 return TRUE;
5138}
5139
f6c7c3e8
AM
5140/* Set up opd section info and abiversion for IBFD, and process list
5141 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5142
8387904d 5143static bfd_boolean
f6c7c3e8 5144ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5145{
99877b66 5146 struct ppc_link_hash_table *htab;
b3fac117 5147 struct ppc_link_hash_entry **p, *eh;
459609d6 5148 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5149
459609d6 5150 if (opd != NULL && opd->size != 0)
b3fac117 5151 {
459609d6
AM
5152 if (abiversion (ibfd) == 0)
5153 set_abiversion (ibfd, 1);
8a2058b5 5154 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5155 {
695344c0 5156 /* xgettext:c-format */
459609d6
AM
5157 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5158 " version %d\n"),
5159 ibfd, abiversion (ibfd));
5160 bfd_set_error (bfd_error_bad_value);
5161 return FALSE;
f6c7c3e8
AM
5162 }
5163
459609d6
AM
5164 if ((ibfd->flags & DYNAMIC) == 0
5165 && (opd->flags & SEC_RELOC) != 0
5166 && opd->reloc_count != 0
5167 && !bfd_is_abs_section (opd->output_section))
b3fac117 5168 {
459609d6
AM
5169 /* Garbage collection needs some extra help with .opd sections.
5170 We don't want to necessarily keep everything referenced by
5171 relocs in .opd, as that would keep all functions. Instead,
5172 if we reference an .opd symbol (a function descriptor), we
5173 want to keep the function code symbol's section. This is
5174 easy for global symbols, but for local syms we need to keep
5175 information about the associated function section. */
5176 bfd_size_type amt;
5177 asection **opd_sym_map;
5178
51aecdc5 5179 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
459609d6
AM
5180 opd_sym_map = bfd_zalloc (ibfd, amt);
5181 if (opd_sym_map == NULL)
b3fac117 5182 return FALSE;
459609d6
AM
5183 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5184 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5185 ppc64_elf_section_data (opd)->sec_type = sec_opd;
b3fac117
AM
5186 }
5187 }
5188
459609d6
AM
5189 if (!is_ppc64_elf (info->output_bfd))
5190 return TRUE;
5191 htab = ppc_hash_table (info);
5192 if (htab == NULL)
5193 return FALSE;
5194
5195 /* For input files without an explicit abiversion in e_flags
5196 we should have flagged any with symbol st_other bits set
5197 as ELFv1 and above flagged those with .opd as ELFv2.
5198 Set the output abiversion if not yet set, and for any input
5199 still ambiguous, take its abiversion from the output.
5200 Differences in ABI are reported later. */
5201 if (abiversion (info->output_bfd) == 0)
5202 set_abiversion (info->output_bfd, abiversion (ibfd));
5203 else if (abiversion (ibfd) == 0)
5204 set_abiversion (ibfd, abiversion (info->output_bfd));
5205
5206 p = &htab->dot_syms;
5207 while ((eh = *p) != NULL)
5208 {
5209 *p = NULL;
5210 if (&eh->elf == htab->elf.hgot)
5211 ;
5212 else if (htab->elf.hgot == NULL
5213 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5214 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
5215 else if (abiversion (ibfd) <= 1)
5216 {
5217 htab->need_func_desc_adj = 1;
5218 if (!add_symbol_adjust (eh, info))
5219 return FALSE;
5220 }
459609d6
AM
5221 p = &eh->u.next_dot_sym;
5222 }
b3fac117 5223 return TRUE;
8387904d
AM
5224}
5225
97fed1c9
JJ
5226/* Undo hash table changes when an --as-needed input file is determined
5227 not to be needed. */
5228
5229static bfd_boolean
e5034e59
AM
5230ppc64_elf_notice_as_needed (bfd *ibfd,
5231 struct bfd_link_info *info,
5232 enum notice_asneeded_action act)
97fed1c9 5233{
e5034e59
AM
5234 if (act == notice_not_needed)
5235 {
5236 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5237
e5034e59
AM
5238 if (htab == NULL)
5239 return FALSE;
4dfe6ac6 5240
e5034e59
AM
5241 htab->dot_syms = NULL;
5242 }
5243 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5244}
5245
aa374f67
AM
5246/* If --just-symbols against a final linked binary, then assume we need
5247 toc adjusting stubs when calling functions defined there. */
5248
5249static void
5250ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5251{
5252 if ((sec->flags & SEC_CODE) != 0
5253 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5254 && is_ppc64_elf (sec->owner))
5255 {
2c3f079f
AM
5256 if (abiversion (sec->owner) >= 2
5257 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5258 sec->has_toc_reloc = 1;
5259 }
5260 _bfd_elf_link_just_syms (sec, info);
5261}
5262
e054468f 5263static struct plt_entry **
4ce794b7
AM
5264update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5265 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5266{
5267 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5268 struct plt_entry **local_plt;
f961d9dd 5269 unsigned char *local_got_tls_masks;
411e1bfb
AM
5270
5271 if (local_got_ents == NULL)
5272 {
5273 bfd_size_type size = symtab_hdr->sh_info;
5274
e054468f
AM
5275 size *= (sizeof (*local_got_ents)
5276 + sizeof (*local_plt)
5277 + sizeof (*local_got_tls_masks));
4ce794b7 5278 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5279 if (local_got_ents == NULL)
e054468f 5280 return NULL;
411e1bfb
AM
5281 elf_local_got_ents (abfd) = local_got_ents;
5282 }
5283
e054468f 5284 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5285 {
5286 struct got_entry *ent;
5287
5288 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5289 if (ent->addend == r_addend
5290 && ent->owner == abfd
5291 && ent->tls_type == tls_type)
411e1bfb
AM
5292 break;
5293 if (ent == NULL)
5294 {
5295 bfd_size_type amt = sizeof (*ent);
4ce794b7 5296 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5297 if (ent == NULL)
5298 return FALSE;
5299 ent->next = local_got_ents[r_symndx];
5300 ent->addend = r_addend;
e717da7e 5301 ent->owner = abfd;
411e1bfb 5302 ent->tls_type = tls_type;
927be08e 5303 ent->is_indirect = FALSE;
411e1bfb
AM
5304 ent->got.refcount = 0;
5305 local_got_ents[r_symndx] = ent;
5306 }
5307 ent->got.refcount += 1;
5308 }
5309
e054468f 5310 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5311 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5312 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5313
5314 return local_plt + r_symndx;
65f38f15
AM
5315}
5316
411e1bfb 5317static bfd_boolean
e054468f 5318update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5319{
411e1bfb 5320 struct plt_entry *ent;
1e2f5b6e 5321
e054468f 5322 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5323 if (ent->addend == addend)
5324 break;
5325 if (ent == NULL)
1e2f5b6e 5326 {
411e1bfb 5327 bfd_size_type amt = sizeof (*ent);
4ce794b7 5328 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5329 if (ent == NULL)
5330 return FALSE;
e054468f 5331 ent->next = *plist;
411e1bfb
AM
5332 ent->addend = addend;
5333 ent->plt.refcount = 0;
e054468f 5334 *plist = ent;
1e2f5b6e 5335 }
411e1bfb 5336 ent->plt.refcount += 1;
b34976b6 5337 return TRUE;
1e2f5b6e
AM
5338}
5339
e054468f
AM
5340static bfd_boolean
5341is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5342{
5343 return (r_type == R_PPC64_REL24
5344 || r_type == R_PPC64_REL14
5345 || r_type == R_PPC64_REL14_BRTAKEN
5346 || r_type == R_PPC64_REL14_BRNTAKEN
5347 || r_type == R_PPC64_ADDR24
5348 || r_type == R_PPC64_ADDR14
5349 || r_type == R_PPC64_ADDR14_BRTAKEN
5350 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5351}
5352
5bd4f169 5353/* Look through the relocs for a section during the first phase, and
65f38f15 5354 calculate needed space in the global offset table, procedure
5d1634d7 5355 linkage table, and dynamic reloc sections. */
5bd4f169 5356
b34976b6 5357static bfd_boolean
4ce794b7
AM
5358ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5359 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5360{
65f38f15 5361 struct ppc_link_hash_table *htab;
5bd4f169 5362 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5363 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5364 const Elf_Internal_Rela *rel;
5365 const Elf_Internal_Rela *rel_end;
5bd4f169 5366 asection *sreloc;
1e2f5b6e 5367 asection **opd_sym_map;
3a71aa26 5368 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 5369
0e1862bb 5370 if (bfd_link_relocatable (info))
b34976b6 5371 return TRUE;
5bd4f169 5372
680a3378
AM
5373 /* Don't do anything special with non-loaded, non-alloced sections.
5374 In particular, any relocs in such sections should not affect GOT
5375 and PLT reference counting (ie. we don't allow them to create GOT
5376 or PLT entries), there's no possibility or desire to optimize TLS
5377 relocs, and there's not much point in propagating relocs to shared
5378 libs that the dynamic linker won't relocate. */
5379 if ((sec->flags & SEC_ALLOC) == 0)
5380 return TRUE;
5381
0c8d6e5c 5382 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5383
65f38f15 5384 htab = ppc_hash_table (info);
4dfe6ac6
NC
5385 if (htab == NULL)
5386 return FALSE;
5387
3a71aa26
AM
5388 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5389 FALSE, FALSE, TRUE);
5390 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5391 FALSE, FALSE, TRUE);
0ffa91dd 5392 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5393 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5394 sreloc = NULL;
1e2f5b6e 5395 opd_sym_map = NULL;
f6c7c3e8
AM
5396 if (ppc64_elf_section_data (sec) != NULL
5397 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5398 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5bd4f169
AM
5399
5400 rel_end = relocs + sec->reloc_count;
5401 for (rel = relocs; rel < rel_end; rel++)
5402 {
5403 unsigned long r_symndx;
5404 struct elf_link_hash_entry *h;
04c9666a 5405 enum elf_ppc64_reloc_type r_type;
727fc41e 5406 int tls_type;
7c8fe5c4 5407 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5408 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5409
5410 r_symndx = ELF64_R_SYM (rel->r_info);
5411 if (r_symndx < symtab_hdr->sh_info)
5412 h = NULL;
5413 else
973a3492 5414 {
8c5b4e52
AM
5415 struct ppc_link_hash_entry *eh;
5416
973a3492 5417 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5418 h = elf_follow_link (h);
8c5b4e52 5419 eh = (struct ppc_link_hash_entry *) h;
81fbe831
AM
5420
5421 /* PR15323, ref flags aren't set for references in the same
5422 object. */
5423 h->root.non_ir_ref = 1;
8c5b4e52
AM
5424 if (eh->is_func && eh->oh != NULL)
5425 eh->oh->elf.root.non_ir_ref = 1;
1c865ab2
AM
5426
5427 if (h == htab->elf.hgot)
5428 sec->has_toc_reloc = 1;
973a3492 5429 }
5bd4f169 5430
727fc41e 5431 tls_type = 0;
e054468f 5432 ifunc = NULL;
25f23106
AM
5433 if (h != NULL)
5434 {
5435 if (h->type == STT_GNU_IFUNC)
5436 {
5437 h->needs_plt = 1;
5438 ifunc = &h->plt.plist;
5439 }
5440 }
5441 else
5442 {
5443 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5444 abfd, r_symndx);
5445 if (isym == NULL)
5446 return FALSE;
5447
5448 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5449 {
5450 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5451 rel->r_addend, PLT_IFUNC);
5452 if (ifunc == NULL)
5453 return FALSE;
5454 }
5455 }
727fc41e 5456
cbf95972 5457 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5458 switch (r_type)
5bd4f169 5459 {
727fc41e
AM
5460 case R_PPC64_TLSGD:
5461 case R_PPC64_TLSLD:
5462 /* These special tls relocs tie a call to __tls_get_addr with
5463 its parameter symbol. */
5464 break;
5465
411e1bfb
AM
5466 case R_PPC64_GOT_TLSLD16:
5467 case R_PPC64_GOT_TLSLD16_LO:
5468 case R_PPC64_GOT_TLSLD16_HI:
5469 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5470 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5471 goto dogottls;
5472
5473 case R_PPC64_GOT_TLSGD16:
5474 case R_PPC64_GOT_TLSGD16_LO:
5475 case R_PPC64_GOT_TLSGD16_HI:
5476 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5477 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5478 goto dogottls;
5479
5480 case R_PPC64_GOT_TPREL16_DS:
5481 case R_PPC64_GOT_TPREL16_LO_DS:
5482 case R_PPC64_GOT_TPREL16_HI:
5483 case R_PPC64_GOT_TPREL16_HA:
0e1862bb 5484 if (bfd_link_pic (info))
411e1bfb
AM
5485 info->flags |= DF_STATIC_TLS;
5486 tls_type = TLS_TLS | TLS_TPREL;
5487 goto dogottls;
5488
5489 case R_PPC64_GOT_DTPREL16_DS:
5490 case R_PPC64_GOT_DTPREL16_LO_DS:
5491 case R_PPC64_GOT_DTPREL16_HI:
5492 case R_PPC64_GOT_DTPREL16_HA:
5493 tls_type = TLS_TLS | TLS_DTPREL;
5494 dogottls:
5495 sec->has_tls_reloc = 1;
1a0670f3 5496 /* Fall through */
411e1bfb 5497
5bd4f169 5498 case R_PPC64_GOT16:
5bd4f169 5499 case R_PPC64_GOT16_DS:
65f38f15
AM
5500 case R_PPC64_GOT16_HA:
5501 case R_PPC64_GOT16_HI:
5502 case R_PPC64_GOT16_LO:
5bd4f169 5503 case R_PPC64_GOT16_LO_DS:
65f38f15 5504 /* This symbol requires a global offset table entry. */
4c52953f 5505 sec->has_toc_reloc = 1;
33c0ec9d
AM
5506 if (r_type == R_PPC64_GOT_TLSLD16
5507 || r_type == R_PPC64_GOT_TLSGD16
5508 || r_type == R_PPC64_GOT_TPREL16_DS
5509 || r_type == R_PPC64_GOT_DTPREL16_DS
5510 || r_type == R_PPC64_GOT16
5511 || r_type == R_PPC64_GOT16_DS)
5512 {
5513 htab->do_multi_toc = 1;
d77c8a4b 5514 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5515 }
5516
e717da7e
AM
5517 if (ppc64_elf_tdata (abfd)->got == NULL
5518 && !create_got_section (abfd, info))
b34976b6 5519 return FALSE;
5bd4f169
AM
5520
5521 if (h != NULL)
5522 {
411e1bfb
AM
5523 struct ppc_link_hash_entry *eh;
5524 struct got_entry *ent;
65f38f15 5525
411e1bfb
AM
5526 eh = (struct ppc_link_hash_entry *) h;
5527 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5528 if (ent->addend == rel->r_addend
e717da7e 5529 && ent->owner == abfd
411e1bfb
AM
5530 && ent->tls_type == tls_type)
5531 break;
5532 if (ent == NULL)
5bd4f169 5533 {
411e1bfb 5534 bfd_size_type amt = sizeof (*ent);
4ce794b7 5535 ent = bfd_alloc (abfd, amt);
411e1bfb 5536 if (ent == NULL)
b34976b6 5537 return FALSE;
411e1bfb
AM
5538 ent->next = eh->elf.got.glist;
5539 ent->addend = rel->r_addend;
e717da7e 5540 ent->owner = abfd;
411e1bfb 5541 ent->tls_type = tls_type;
927be08e 5542 ent->is_indirect = FALSE;
411e1bfb
AM
5543 ent->got.refcount = 0;
5544 eh->elf.got.glist = ent;
5bd4f169 5545 }
411e1bfb 5546 ent->got.refcount += 1;
e7b938ca 5547 eh->tls_mask |= tls_type;
5bd4f169 5548 }
411e1bfb
AM
5549 else
5550 /* This is a global offset table entry for a local symbol. */
5551 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5552 rel->r_addend, tls_type))
5553 return FALSE;
a345bc8d
AM
5554
5555 /* We may also need a plt entry if the symbol turns out to be
5556 an ifunc. */
0e1862bb 5557 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5558 {
5559 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5560 return FALSE;
5561 }
5bd4f169
AM
5562 break;
5563
5bd4f169 5564 case R_PPC64_PLT16_HA:
65f38f15
AM
5565 case R_PPC64_PLT16_HI:
5566 case R_PPC64_PLT16_LO:
5567 case R_PPC64_PLT32:
5568 case R_PPC64_PLT64:
cbf95972
AM
5569 /* This symbol requires a procedure linkage table entry. */
5570 plt_list = ifunc;
5571 if (h != NULL)
e054468f 5572 {
e054468f
AM
5573 h->needs_plt = 1;
5574 if (h->root.root.string[0] == '.'
5575 && h->root.root.string[1] != '\0')
5576 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972
AM
5577 plt_list = &h->plt.plist;
5578 }
5579 if (plt_list == NULL)
5580 {
5581 /* It does not make sense to have a procedure linkage
5582 table entry for a non-ifunc local symbol. */
5583 info->callbacks->einfo
695344c0 5584 /* xgettext:c-format */
cbf95972
AM
5585 (_("%P: %H: %s reloc against local symbol\n"),
5586 abfd, sec, rel->r_offset,
5587 ppc64_elf_howto_table[r_type]->name);
5588 bfd_set_error (bfd_error_bad_value);
5589 return FALSE;
e054468f 5590 }
cbf95972
AM
5591 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5592 return FALSE;
5bd4f169
AM
5593 break;
5594
5595 /* The following relocations don't need to propagate the
5596 relocation if linking a shared object since they are
5597 section relative. */
5598 case R_PPC64_SECTOFF:
5599 case R_PPC64_SECTOFF_LO:
5600 case R_PPC64_SECTOFF_HI:
5601 case R_PPC64_SECTOFF_HA:
5602 case R_PPC64_SECTOFF_DS:
5603 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5604 case R_PPC64_DTPREL16:
5605 case R_PPC64_DTPREL16_LO:
5606 case R_PPC64_DTPREL16_HI:
5607 case R_PPC64_DTPREL16_HA:
5608 case R_PPC64_DTPREL16_DS:
5609 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5610 case R_PPC64_DTPREL16_HIGH:
5611 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5612 case R_PPC64_DTPREL16_HIGHER:
5613 case R_PPC64_DTPREL16_HIGHERA:
5614 case R_PPC64_DTPREL16_HIGHEST:
5615 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5616 break;
5617
ad8e1ba5 5618 /* Nor do these. */
25f23106
AM
5619 case R_PPC64_REL16:
5620 case R_PPC64_REL16_LO:
5621 case R_PPC64_REL16_HI:
5622 case R_PPC64_REL16_HA:
a680de9a 5623 case R_PPC64_REL16DX_HA:
25f23106
AM
5624 break;
5625
45965137
AM
5626 /* Not supported as a dynamic relocation. */
5627 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5628 if (bfd_link_pic (info))
45965137
AM
5629 {
5630 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5631 ppc_howto_init ();
695344c0 5632 /* xgettext:c-format */
45965137
AM
5633 info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5634 "in shared libraries and PIEs.\n"),
5635 abfd, sec, rel->r_offset,
5636 ppc64_elf_howto_table[r_type]->name);
5637 bfd_set_error (bfd_error_bad_value);
5638 return FALSE;
5639 }
5640 break;
5641
ad8e1ba5 5642 case R_PPC64_TOC16:
33c0ec9d
AM
5643 case R_PPC64_TOC16_DS:
5644 htab->do_multi_toc = 1;
d77c8a4b 5645 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5646 /* Fall through. */
ad8e1ba5
AM
5647 case R_PPC64_TOC16_LO:
5648 case R_PPC64_TOC16_HI:
5649 case R_PPC64_TOC16_HA:
ad8e1ba5 5650 case R_PPC64_TOC16_LO_DS:
4c52953f 5651 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5652 break;
5653
006589cf
AM
5654 /* Marker reloc. */
5655 case R_PPC64_ENTRY:
5656 break;
5657
5bd4f169
AM
5658 /* This relocation describes the C++ object vtable hierarchy.
5659 Reconstruct it for later use during GC. */
5660 case R_PPC64_GNU_VTINHERIT:
c152c796 5661 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5662 return FALSE;
5bd4f169
AM
5663 break;
5664
5665 /* This relocation describes which C++ vtable entries are actually
5666 used. Record for later use during GC. */
5667 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5668 BFD_ASSERT (h != NULL);
5669 if (h != NULL
5670 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5671 return FALSE;
5bd4f169
AM
5672 break;
5673
721956f4
AM
5674 case R_PPC64_REL14:
5675 case R_PPC64_REL14_BRTAKEN:
5676 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5677 {
5678 asection *dest = NULL;
5679
5680 /* Heuristic: If jumping outside our section, chances are
5681 we are going to need a stub. */
5682 if (h != NULL)
5683 {
5684 /* If the sym is weak it may be overridden later, so
5685 don't assume we know where a weak sym lives. */
5686 if (h->root.type == bfd_link_hash_defined)
5687 dest = h->root.u.def.section;
5688 }
5689 else
87d72d41
AM
5690 {
5691 Elf_Internal_Sym *isym;
5692
5693 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5694 abfd, r_symndx);
5695 if (isym == NULL)
5696 return FALSE;
5697
5698 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5699 }
5700
220c76dd 5701 if (dest != sec)
7c8fe5c4 5702 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5703 }
721956f4
AM
5704 /* Fall through. */
5705
5d1634d7 5706 case R_PPC64_REL24:
cbf95972
AM
5707 plt_list = ifunc;
5708 if (h != NULL)
5d1634d7 5709 {
e054468f
AM
5710 h->needs_plt = 1;
5711 if (h->root.root.string[0] == '.'
5712 && h->root.root.string[1] != '\0')
5713 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5714
3a71aa26 5715 if (h == tga || h == dottga)
cbf95972
AM
5716 {
5717 sec->has_tls_reloc = 1;
5718 if (rel != relocs
5719 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5720 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5721 /* We have a new-style __tls_get_addr call with
5722 a marker reloc. */
5723 ;
5724 else
5725 /* Mark this section as having an old-style call. */
5726 sec->has_tls_get_addr_call = 1;
5727 }
5728 plt_list = &h->plt.plist;
411e1bfb 5729 }
cbf95972
AM
5730
5731 /* We may need a .plt entry if the function this reloc
5732 refers to is in a shared lib. */
5733 if (plt_list
5734 && !update_plt_info (abfd, plt_list, rel->r_addend))
5735 return FALSE;
411e1bfb
AM
5736 break;
5737
cbf95972
AM
5738 case R_PPC64_ADDR14:
5739 case R_PPC64_ADDR14_BRNTAKEN:
5740 case R_PPC64_ADDR14_BRTAKEN:
5741 case R_PPC64_ADDR24:
5742 goto dodyn;
5743
411e1bfb
AM
5744 case R_PPC64_TPREL64:
5745 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
0e1862bb 5746 if (bfd_link_pic (info))
411e1bfb
AM
5747 info->flags |= DF_STATIC_TLS;
5748 goto dotlstoc;
5749
5750 case R_PPC64_DTPMOD64:
5751 if (rel + 1 < rel_end
5752 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5753 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5754 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5755 else
951fd09b 5756 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5757 goto dotlstoc;
5758
5759 case R_PPC64_DTPREL64:
5760 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5761 if (rel != relocs
5762 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5763 && rel[-1].r_offset == rel->r_offset - 8)
5764 /* This is the second reloc of a dtpmod, dtprel pair.
5765 Don't mark with TLS_DTPREL. */
5766 goto dodyn;
5767
5768 dotlstoc:
5769 sec->has_tls_reloc = 1;
5770 if (h != NULL)
5771 {
5772 struct ppc_link_hash_entry *eh;
5773 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5774 eh->tls_mask |= tls_type;
411e1bfb
AM
5775 }
5776 else
5777 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5778 rel->r_addend, tls_type))
5779 return FALSE;
5780
7c8fe5c4
AM
5781 ppc64_sec = ppc64_elf_section_data (sec);
5782 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5783 {
3a71aa26
AM
5784 bfd_size_type amt;
5785
e7b938ca 5786 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5787 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5788 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5789 if (ppc64_sec->u.toc.symndx == NULL)
5790 return FALSE;
5791 amt = sec->size * sizeof (bfd_vma) / 8;
5792 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5793 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5794 return FALSE;
7c8fe5c4
AM
5795 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5796 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5797 }
5798 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5799 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5800 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5801
5802 /* Mark the second slot of a GD or LD entry.
5803 -1 to indicate GD and -2 to indicate LD. */
5804 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5805 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5806 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5807 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5808 goto dodyn;
5809
5810 case R_PPC64_TPREL16:
5811 case R_PPC64_TPREL16_LO:
5812 case R_PPC64_TPREL16_HI:
5813 case R_PPC64_TPREL16_HA:
5814 case R_PPC64_TPREL16_DS:
5815 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5816 case R_PPC64_TPREL16_HIGH:
5817 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5818 case R_PPC64_TPREL16_HIGHER:
5819 case R_PPC64_TPREL16_HIGHERA:
5820 case R_PPC64_TPREL16_HIGHEST:
5821 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 5822 if (bfd_link_pic (info))
411e1bfb 5823 {
afb93314 5824 info->flags |= DF_STATIC_TLS;
411e1bfb 5825 goto dodyn;
5d1634d7
AM
5826 }
5827 break;
5828
e86ce104 5829 case R_PPC64_ADDR64:
1e2f5b6e 5830 if (opd_sym_map != NULL
1e2f5b6e 5831 && rel + 1 < rel_end
4ce794b7 5832 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5833 {
8387904d 5834 if (h != NULL)
8c5b4e52 5835 ((struct ppc_link_hash_entry *) h)->is_func = 1;
8387904d
AM
5836 else
5837 {
5838 asection *s;
87d72d41 5839 Elf_Internal_Sym *isym;
1e2f5b6e 5840
87d72d41
AM
5841 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5842 abfd, r_symndx);
5843 if (isym == NULL)
8387904d 5844 return FALSE;
87d72d41
AM
5845
5846 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5847 if (s != NULL && s != sec)
51aecdc5 5848 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
8387904d 5849 }
1e2f5b6e 5850 }
e86ce104
AM
5851 /* Fall through. */
5852
65f38f15
AM
5853 case R_PPC64_ADDR16:
5854 case R_PPC64_ADDR16_DS:
5855 case R_PPC64_ADDR16_HA:
5856 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5857 case R_PPC64_ADDR16_HIGH:
5858 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5859 case R_PPC64_ADDR16_HIGHER:
5860 case R_PPC64_ADDR16_HIGHERA:
5861 case R_PPC64_ADDR16_HIGHEST:
5862 case R_PPC64_ADDR16_HIGHESTA:
5863 case R_PPC64_ADDR16_LO:
5864 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5865 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5866 && rel->r_addend == 0)
5867 {
5868 /* We may need a .plt entry if this reloc refers to a
5869 function in a shared lib. */
5870 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5871 return FALSE;
5872 h->pointer_equality_needed = 1;
5873 }
5874 /* Fall through. */
5875
5876 case R_PPC64_REL30:
5877 case R_PPC64_REL32:
5878 case R_PPC64_REL64:
65f38f15 5879 case R_PPC64_ADDR32:
65f38f15
AM
5880 case R_PPC64_UADDR16:
5881 case R_PPC64_UADDR32:
5882 case R_PPC64_UADDR64:
5bd4f169 5883 case R_PPC64_TOC:
0e1862bb 5884 if (h != NULL && !bfd_link_pic (info))
81848ca0 5885 /* We may need a copy reloc. */
f5385ebf 5886 h->non_got_ref = 1;
81848ca0 5887
41bd81ab 5888 /* Don't propagate .opd relocs. */
1e2f5b6e 5889 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5890 break;
e86ce104 5891
65f38f15
AM
5892 /* If we are creating a shared library, and this is a reloc
5893 against a global symbol, or a non PC relative reloc
5894 against a local symbol, then we need to copy the reloc
5895 into the shared library. However, if we are linking with
5896 -Bsymbolic, we do not need to copy a reloc against a
5897 global symbol which is defined in an object we are
5898 including in the link (i.e., DEF_REGULAR is set). At
5899 this point we have not seen all the input files, so it is
5900 possible that DEF_REGULAR is not set now but will be set
5901 later (it is never cleared). In case of a weak definition,
5902 DEF_REGULAR may be cleared later by a strong definition in
5903 a shared library. We account for that possibility below by
f4656909 5904 storing information in the dyn_relocs field of the hash
65f38f15
AM
5905 table entry. A similar situation occurs when creating
5906 shared libraries and symbol visibility changes render the
5907 symbol local.
5908
5909 If on the other hand, we are creating an executable, we
5910 may need to keep relocations for symbols satisfied by a
5911 dynamic library if we manage to avoid copy relocs for the
5912 symbol. */
411e1bfb 5913 dodyn:
0e1862bb 5914 if ((bfd_link_pic (info)
1d483afe 5915 && (must_be_dyn_reloc (info, r_type)
65f38f15 5916 || (h != NULL
198f1157 5917 && (!SYMBOLIC_BIND (info, h)
65f38f15 5918 || h->root.type == bfd_link_hash_defweak
f5385ebf 5919 || !h->def_regular))))
f4656909 5920 || (ELIMINATE_COPY_RELOCS
0e1862bb 5921 && !bfd_link_pic (info)
65f38f15
AM
5922 && h != NULL
5923 && (h->root.type == bfd_link_hash_defweak
25f23106 5924 || !h->def_regular))
0e1862bb 5925 || (!bfd_link_pic (info)
25f23106 5926 && ifunc != NULL))
5bd4f169 5927 {
65f38f15
AM
5928 /* We must copy these reloc types into the output file.
5929 Create a reloc section in dynobj and make room for
5930 this reloc. */
5bd4f169
AM
5931 if (sreloc == NULL)
5932 {
83bac4b0
NC
5933 sreloc = _bfd_elf_make_dynamic_reloc_section
5934 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5935
5bd4f169 5936 if (sreloc == NULL)
83bac4b0 5937 return FALSE;
5bd4f169
AM
5938 }
5939
65f38f15
AM
5940 /* If this is a global symbol, we count the number of
5941 relocations we need for this symbol. */
5942 if (h != NULL)
5943 {
19e08130
AM
5944 struct elf_dyn_relocs *p;
5945 struct elf_dyn_relocs **head;
5946
ec338859 5947 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5948 p = *head;
5949 if (p == NULL || p->sec != sec)
5950 {
5951 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5952 if (p == NULL)
5953 return FALSE;
5954 p->next = *head;
5955 *head = p;
5956 p->sec = sec;
5957 p->count = 0;
5958 p->pc_count = 0;
5959 }
5960 p->count += 1;
5961 if (!must_be_dyn_reloc (info, r_type))
5962 p->pc_count += 1;
65f38f15
AM
5963 }
5964 else
5965 {
ec338859
AM
5966 /* Track dynamic relocs needed for local syms too.
5967 We really need local syms available to do this
5968 easily. Oh well. */
19e08130
AM
5969 struct ppc_dyn_relocs *p;
5970 struct ppc_dyn_relocs **head;
5971 bfd_boolean is_ifunc;
ec338859 5972 asection *s;
6edfbbad 5973 void *vpp;
87d72d41 5974 Elf_Internal_Sym *isym;
6edfbbad 5975
87d72d41
AM
5976 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5977 abfd, r_symndx);
5978 if (isym == NULL)
b34976b6 5979 return FALSE;
ec338859 5980
87d72d41
AM
5981 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5982 if (s == NULL)
5983 s = sec;
5984
6edfbbad 5985 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5986 head = (struct ppc_dyn_relocs **) vpp;
5987 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5988 p = *head;
5989 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5990 p = p->next;
5991 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5992 {
5993 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5994 if (p == NULL)
5995 return FALSE;
5996 p->next = *head;
5997 *head = p;
5998 p->sec = sec;
5999 p->ifunc = is_ifunc;
6000 p->count = 0;
6001 }
6002 p->count += 1;
ec338859 6003 }
65f38f15 6004 }
5bd4f169 6005 break;
65f38f15
AM
6006
6007 default:
96e0dda4 6008 break;
5bd4f169
AM
6009 }
6010 }
6011
b34976b6 6012 return TRUE;
5bd4f169
AM
6013}
6014
ee67d69a
AM
6015/* Merge backend specific data from an object file to the output
6016 object file when linking. */
6017
6018static bfd_boolean
50e03d47 6019ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 6020{
50e03d47 6021 bfd *obfd = info->output_bfd;
ee67d69a
AM
6022 unsigned long iflags, oflags;
6023
6024 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6025 return TRUE;
6026
6027 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6028 return TRUE;
6029
50e03d47 6030 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6031 return FALSE;
6032
6033 iflags = elf_elfheader (ibfd)->e_flags;
6034 oflags = elf_elfheader (obfd)->e_flags;
6035
f6c7c3e8 6036 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6037 {
4eca0228 6038 _bfd_error_handler
695344c0 6039 /* xgettext:c-format */
ee67d69a
AM
6040 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6041 bfd_set_error (bfd_error_bad_value);
6042 return FALSE;
6043 }
f6c7c3e8 6044 else if (iflags != oflags && iflags != 0)
ee67d69a 6045 {
4eca0228 6046 _bfd_error_handler
695344c0 6047 /* xgettext:c-format */
ee67d69a
AM
6048 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6049 ibfd, iflags, oflags);
6050 bfd_set_error (bfd_error_bad_value);
6051 return FALSE;
6052 }
6053
50e03d47 6054 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
005d79fd 6055
ee67d69a 6056 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 6057 _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6058
6059 return TRUE;
6060}
6061
6062static bfd_boolean
6063ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6064{
6065 /* Print normal ELF private data. */
6066 _bfd_elf_print_private_bfd_data (abfd, ptr);
6067
6068 if (elf_elfheader (abfd)->e_flags != 0)
6069 {
6070 FILE *file = ptr;
6071
ee67d69a
AM
6072 fprintf (file, _("private flags = 0x%lx:"),
6073 elf_elfheader (abfd)->e_flags);
6074
6075 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6076 fprintf (file, _(" [abiv%ld]"),
6077 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6078 fputc ('\n', file);
6079 }
6080
6081 return TRUE;
6082}
6083
8387904d 6084/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6085 of the code entry point, and its section, which must be in the same
6086 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6087
6088static bfd_vma
6089opd_entry_value (asection *opd_sec,
6090 bfd_vma offset,
6091 asection **code_sec,
aef36ac1
AM
6092 bfd_vma *code_off,
6093 bfd_boolean in_code_sec)
8387904d
AM
6094{
6095 bfd *opd_bfd = opd_sec->owner;
8860955f 6096 Elf_Internal_Rela *relocs;
8387904d 6097 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6098 bfd_vma val;
8387904d 6099
9f296da3
AM
6100 /* No relocs implies we are linking a --just-symbols object, or looking
6101 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6102 if (opd_sec->reloc_count == 0)
6103 {
729eabd5 6104 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6105
729eabd5
AM
6106 if (contents == NULL)
6107 {
6108 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6109 return (bfd_vma) -1;
6110 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6111 }
ee1e4ede 6112
dbb3fbbb 6113 /* PR 17512: file: 64b9dfbb. */
451dfd38 6114 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6115 return (bfd_vma) -1;
6116
729eabd5 6117 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6118 if (code_sec != NULL)
6119 {
6120 asection *sec, *likely = NULL;
ee1e4ede 6121
aef36ac1 6122 if (in_code_sec)
4b85d634 6123 {
aef36ac1
AM
6124 sec = *code_sec;
6125 if (sec->vma <= val
6126 && val < sec->vma + sec->size)
6127 likely = sec;
6128 else
6129 val = -1;
6130 }
6131 else
6132 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6133 if (sec->vma <= val
6134 && (sec->flags & SEC_LOAD) != 0
6135 && (sec->flags & SEC_ALLOC) != 0)
6136 likely = sec;
6137 if (likely != NULL)
6138 {
6139 *code_sec = likely;
6140 if (code_off != NULL)
6141 *code_off = val - likely->vma;
4b85d634
AM
6142 }
6143 }
aef36ac1 6144 return val;
4b85d634
AM
6145 }
6146
0c8d6e5c 6147 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6148
729eabd5 6149 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6150 if (relocs == NULL)
6151 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6152 /* PR 17512: file: df8e1fd6. */
6153 if (relocs == NULL)
6154 return (bfd_vma) -1;
645ea6a9 6155
8387904d 6156 /* Go find the opd reloc at the sym address. */
8860955f 6157 lo = relocs;
8387904d 6158 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6159 val = (bfd_vma) -1;
8387904d
AM
6160 while (lo < hi)
6161 {
6162 look = lo + (hi - lo) / 2;
6163 if (look->r_offset < offset)
6164 lo = look + 1;
6165 else if (look->r_offset > offset)
6166 hi = look;
6167 else
6168 {
0ffa91dd
NC
6169 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6170
8387904d
AM
6171 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6172 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6173 {
6174 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6175 asection *sec = NULL;
8387904d 6176
b53dfeb2
AM
6177 if (symndx >= symtab_hdr->sh_info
6178 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6179 {
6180 struct elf_link_hash_entry **sym_hashes;
6181 struct elf_link_hash_entry *rh;
6182
6183 sym_hashes = elf_sym_hashes (opd_bfd);
6184 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6185 if (rh != NULL)
6186 {
6187 rh = elf_follow_link (rh);
bb854a36
AM
6188 if (rh->root.type != bfd_link_hash_defined
6189 && rh->root.type != bfd_link_hash_defweak)
6190 break;
6191 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6192 {
bb854a36
AM
6193 val = rh->root.u.def.value;
6194 sec = rh->root.u.def.section;
b53dfeb2
AM
6195 }
6196 }
6197 }
6198
6199 if (sec == NULL)
6200 {
6201 Elf_Internal_Sym *sym;
6202
6203 if (symndx < symtab_hdr->sh_info)
6204 {
6205 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6206 if (sym == NULL)
6207 {
6208 size_t symcnt = symtab_hdr->sh_info;
6209 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6210 symcnt, 0,
6211 NULL, NULL, NULL);
6212 if (sym == NULL)
6213 break;
6214 symtab_hdr->contents = (bfd_byte *) sym;
6215 }
6216 sym += symndx;
128205bb
AM
6217 }
6218 else
6219 {
b53dfeb2
AM
6220 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6221 1, symndx,
6222 NULL, NULL, NULL);
128205bb
AM
6223 if (sym == NULL)
6224 break;
128205bb 6225 }
b53dfeb2
AM
6226 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6227 if (sec == NULL)
6228 break;
6229 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6230 val = sym->st_value;
8387904d 6231 }
b53dfeb2 6232
8387904d
AM
6233 val += look->r_addend;
6234 if (code_off != NULL)
6235 *code_off = val;
6236 if (code_sec != NULL)
aef36ac1
AM
6237 {
6238 if (in_code_sec && *code_sec != sec)
6239 return -1;
6240 else
6241 *code_sec = sec;
6242 }
b53dfeb2 6243 if (sec->output_section != NULL)
8387904d 6244 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6245 }
6246 break;
6247 }
6248 }
645ea6a9 6249
645ea6a9 6250 return val;
8387904d
AM
6251}
6252
aef36ac1
AM
6253/* If the ELF symbol SYM might be a function in SEC, return the
6254 function size and set *CODE_OFF to the function's entry point,
6255 otherwise return zero. */
9f296da3 6256
aef36ac1
AM
6257static bfd_size_type
6258ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6259 bfd_vma *code_off)
9f296da3 6260{
aef36ac1
AM
6261 bfd_size_type size;
6262
6263 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6264 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6265 return 0;
6266
6267 size = 0;
6268 if (!(sym->flags & BSF_SYNTHETIC))
6269 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6270
6271 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6272 {
b07bca4e
AM
6273 struct _opd_sec_data *opd = get_opd_info (sym->section);
6274 bfd_vma symval = sym->value;
6275
6276 if (opd != NULL
6277 && opd->adjust != NULL
6278 && elf_section_data (sym->section)->relocs != NULL)
6279 {
6280 /* opd_entry_value will use cached relocs that have been
6281 adjusted, but with raw symbols. That means both local
6282 and global symbols need adjusting. */
6283 long adjust = opd->adjust[OPD_NDX (symval)];
6284 if (adjust == -1)
6285 return 0;
6286 symval += adjust;
6287 }
6288
6289 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6290 &sec, code_off, TRUE) == (bfd_vma) -1)
6291 return 0;
6292 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6293 symbol. This size has nothing to do with the code size of the
6294 function, which is what we're supposed to return, but the
6295 code size isn't available without looking up the dot-sym.
6296 However, doing that would be a waste of time particularly
6297 since elf_find_function will look at the dot-sym anyway.
6298 Now, elf_find_function will keep the largest size of any
6299 function sym found at the code address of interest, so return
6300 1 here to avoid it incorrectly caching a larger function size
6301 for a small function. This does mean we return the wrong
6302 size for a new-ABI function of size 24, but all that does is
6303 disable caching for such functions. */
6304 if (size == 24)
6305 size = 1;
9f296da3 6306 }
aef36ac1
AM
6307 else
6308 {
6309 if (sym->section != sec)
6310 return 0;
6311 *code_off = sym->value;
6312 }
6313 if (size == 0)
6314 size = 1;
6315 return size;
9f296da3
AM
6316}
6317
854b41e7
AM
6318/* Return true if symbol is defined in a regular object file. */
6319
6320static bfd_boolean
6321is_static_defined (struct elf_link_hash_entry *h)
6322{
6323 return ((h->root.type == bfd_link_hash_defined
6324 || h->root.type == bfd_link_hash_defweak)
6325 && h->root.u.def.section != NULL
6326 && h->root.u.def.section->output_section != NULL);
6327}
6328
b31867b6
AM
6329/* If FDH is a function descriptor symbol, return the associated code
6330 entry symbol if it is defined. Return NULL otherwise. */
6331
6332static struct ppc_link_hash_entry *
6333defined_code_entry (struct ppc_link_hash_entry *fdh)
6334{
6335 if (fdh->is_func_descriptor)
6336 {
6337 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6338 if (fh->elf.root.type == bfd_link_hash_defined
6339 || fh->elf.root.type == bfd_link_hash_defweak)
6340 return fh;
6341 }
6342 return NULL;
6343}
6344
6345/* If FH is a function code entry symbol, return the associated
6346 function descriptor symbol if it is defined. Return NULL otherwise. */
6347
6348static struct ppc_link_hash_entry *
6349defined_func_desc (struct ppc_link_hash_entry *fh)
6350{
6351 if (fh->oh != NULL
6352 && fh->oh->is_func_descriptor)
6353 {
6354 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6355 if (fdh->elf.root.type == bfd_link_hash_defined
6356 || fdh->elf.root.type == bfd_link_hash_defweak)
6357 return fdh;
6358 }
6359 return NULL;
6360}
6361
8c5b4e52
AM
6362static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6363
6364/* Garbage collect sections, after first dealing with dot-symbols. */
6365
6366static bfd_boolean
6367ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6368{
6369 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6370
6371 if (htab != NULL && htab->need_func_desc_adj)
6372 {
6373 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6374 htab->need_func_desc_adj = 0;
6375 }
6376 return bfd_elf_gc_sections (abfd, info);
6377}
6378
74f0fb50
AM
6379/* Mark all our entry sym sections, both opd and code section. */
6380
6381static void
6382ppc64_elf_gc_keep (struct bfd_link_info *info)
6383{
6384 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6385 struct bfd_sym_chain *sym;
6386
4dfe6ac6
NC
6387 if (htab == NULL)
6388 return;
6389
74f0fb50
AM
6390 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6391 {
b31867b6 6392 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6393 asection *sec;
6394
6395 eh = (struct ppc_link_hash_entry *)
b31867b6 6396 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6397 if (eh == NULL)
6398 continue;
6399 if (eh->elf.root.type != bfd_link_hash_defined
6400 && eh->elf.root.type != bfd_link_hash_defweak)
6401 continue;
6402
b31867b6
AM
6403 fh = defined_code_entry (eh);
6404 if (fh != NULL)
74f0fb50 6405 {
b31867b6 6406 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6407 sec->flags |= SEC_KEEP;
6408 }
6409 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6410 && opd_entry_value (eh->elf.root.u.def.section,
6411 eh->elf.root.u.def.value,
aef36ac1 6412 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6413 sec->flags |= SEC_KEEP;
6414
6415 sec = eh->elf.root.u.def.section;
6416 sec->flags |= SEC_KEEP;
6417 }
6418}
6419
64d03ab5
AM
6420/* Mark sections containing dynamically referenced symbols. When
6421 building shared libraries, we must assume that any visible symbol is
6422 referenced. */
6423
6424static bfd_boolean
6425ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6426{
6427 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6428 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6429 struct ppc_link_hash_entry *fdh;
b407645f 6430 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6431
64d03ab5 6432 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6433 fdh = defined_func_desc (eh);
6434 if (fdh != NULL)
6435 eh = fdh;
64d03ab5
AM
6436
6437 if ((eh->elf.root.type == bfd_link_hash_defined
6438 || eh->elf.root.type == bfd_link_hash_defweak)
6439 && (eh->elf.ref_dynamic
1c9177d9 6440 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6441 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6442 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6443 && (!bfd_link_executable (info)
b407645f
AM
6444 || info->export_dynamic
6445 || (eh->elf.dynamic
6446 && d != NULL
6447 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
4c58e0d8
AM
6448 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6449 || !bfd_hide_sym_by_version (info->version_info,
6450 eh->elf.root.root.string)))))
64d03ab5
AM
6451 {
6452 asection *code_sec;
b31867b6 6453 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6454
6455 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6456
6457 /* Function descriptor syms cause the associated
6458 function code sym section to be marked. */
b31867b6
AM
6459 fh = defined_code_entry (eh);
6460 if (fh != NULL)
6461 {
6462 code_sec = fh->elf.root.u.def.section;
6463 code_sec->flags |= SEC_KEEP;
6464 }
64d03ab5
AM
6465 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6466 && opd_entry_value (eh->elf.root.u.def.section,
6467 eh->elf.root.u.def.value,
aef36ac1 6468 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6469 code_sec->flags |= SEC_KEEP;
6470 }
6471
6472 return TRUE;
6473}
6474
5bd4f169
AM
6475/* Return the section that should be marked against GC for a given
6476 relocation. */
6477
6478static asection *
4ce794b7 6479ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6480 struct bfd_link_info *info,
4ce794b7
AM
6481 Elf_Internal_Rela *rel,
6482 struct elf_link_hash_entry *h,
6483 Elf_Internal_Sym *sym)
5bd4f169 6484{
ccfa59ea
AM
6485 asection *rsec;
6486
ccfa59ea
AM
6487 /* Syms return NULL if we're marking .opd, so we avoid marking all
6488 function sections, as all functions are referenced in .opd. */
6489 rsec = NULL;
6490 if (get_opd_info (sec) != NULL)
6491 return rsec;
1e2f5b6e 6492
5bd4f169
AM
6493 if (h != NULL)
6494 {
04c9666a 6495 enum elf_ppc64_reloc_type r_type;
b31867b6 6496 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6497
4ce794b7 6498 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6499 switch (r_type)
5bd4f169
AM
6500 {
6501 case R_PPC64_GNU_VTINHERIT:
6502 case R_PPC64_GNU_VTENTRY:
6503 break;
6504
6505 default:
6506 switch (h->root.type)
6507 {
6508 case bfd_link_hash_defined:
6509 case bfd_link_hash_defweak:
ccfa59ea 6510 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6511 fdh = defined_func_desc (eh);
6512 if (fdh != NULL)
8c5b4e52
AM
6513 {
6514 /* -mcall-aixdesc code references the dot-symbol on
6515 a call reloc. Mark the function descriptor too
6516 against garbage collection. */
6517 fdh->elf.mark = 1;
6518 if (fdh->elf.u.weakdef != NULL)
6519 fdh->elf.u.weakdef->mark = 1;
6520 eh = fdh;
6521 }
1e2f5b6e
AM
6522
6523 /* Function descriptor syms cause the associated
6524 function code sym section to be marked. */
b31867b6
AM
6525 fh = defined_code_entry (eh);
6526 if (fh != NULL)
ccfa59ea
AM
6527 {
6528 /* They also mark their opd section. */
74f0fb50 6529 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6530
b31867b6 6531 rsec = fh->elf.root.u.def.section;
ccfa59ea 6532 }
8387904d
AM
6533 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6534 && opd_entry_value (eh->elf.root.u.def.section,
6535 eh->elf.root.u.def.value,
aef36ac1 6536 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6537 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6538 else
1e2f5b6e
AM
6539 rsec = h->root.u.def.section;
6540 break;
5bd4f169
AM
6541
6542 case bfd_link_hash_common:
1e2f5b6e
AM
6543 rsec = h->root.u.c.p->section;
6544 break;
5bd4f169
AM
6545
6546 default:
fb34365b 6547 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6548 }
6549 }
6550 }
6551 else
6552 {
74f0fb50 6553 struct _opd_sec_data *opd;
1e2f5b6e
AM
6554
6555 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6556 opd = get_opd_info (rsec);
6557 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6558 {
74f0fb50 6559 rsec->gc_mark = 1;
ccfa59ea 6560
51aecdc5 6561 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6562 }
5bd4f169
AM
6563 }
6564
1e2f5b6e 6565 return rsec;
5bd4f169
AM
6566}
6567
65f38f15
AM
6568/* Update the .got, .plt. and dynamic reloc reference counts for the
6569 section being removed. */
5bd4f169 6570
b34976b6 6571static bfd_boolean
4ce794b7
AM
6572ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6573 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 6574{
411e1bfb 6575 struct ppc_link_hash_table *htab;
5bd4f169
AM
6576 Elf_Internal_Shdr *symtab_hdr;
6577 struct elf_link_hash_entry **sym_hashes;
411e1bfb 6578 struct got_entry **local_got_ents;
5bd4f169 6579 const Elf_Internal_Rela *rel, *relend;
5bd4f169 6580
0e1862bb 6581 if (bfd_link_relocatable (info))
7dda2462
TG
6582 return TRUE;
6583
680a3378
AM
6584 if ((sec->flags & SEC_ALLOC) == 0)
6585 return TRUE;
6586
ec338859
AM
6587 elf_section_data (sec)->local_dynrel = NULL;
6588
411e1bfb 6589 htab = ppc_hash_table (info);
4dfe6ac6
NC
6590 if (htab == NULL)
6591 return FALSE;
6592
0ffa91dd 6593 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 6594 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 6595 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
6596
6597 relend = relocs + sec->reloc_count;
6598 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
6599 {
6600 unsigned long r_symndx;
04c9666a 6601 enum elf_ppc64_reloc_type r_type;
58ac9f71 6602 struct elf_link_hash_entry *h = NULL;
cbf95972 6603 struct plt_entry **plt_list;
f961d9dd 6604 unsigned char tls_type = 0;
5bd4f169 6605
a33d1f77 6606 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 6607 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
6608 if (r_symndx >= symtab_hdr->sh_info)
6609 {
6610 struct ppc_link_hash_entry *eh;
6061a67d
AM
6611 struct elf_dyn_relocs **pp;
6612 struct elf_dyn_relocs *p;
58ac9f71
AM
6613
6614 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6615 h = elf_follow_link (h);
58ac9f71
AM
6616 eh = (struct ppc_link_hash_entry *) h;
6617
6618 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6619 if (p->sec == sec)
6620 {
6621 /* Everything must go for SEC. */
6622 *pp = p->next;
6623 break;
6624 }
6625 }
6626
a33d1f77
AM
6627 switch (r_type)
6628 {
411e1bfb
AM
6629 case R_PPC64_GOT_TLSLD16:
6630 case R_PPC64_GOT_TLSLD16_LO:
6631 case R_PPC64_GOT_TLSLD16_HI:
6632 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 6633 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6634 goto dogot;
6635
6636 case R_PPC64_GOT_TLSGD16:
6637 case R_PPC64_GOT_TLSGD16_LO:
6638 case R_PPC64_GOT_TLSGD16_HI:
6639 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6640 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6641 goto dogot;
6642
6643 case R_PPC64_GOT_TPREL16_DS:
6644 case R_PPC64_GOT_TPREL16_LO_DS:
6645 case R_PPC64_GOT_TPREL16_HI:
6646 case R_PPC64_GOT_TPREL16_HA:
6647 tls_type = TLS_TLS | TLS_TPREL;
6648 goto dogot;
6649
6650 case R_PPC64_GOT_DTPREL16_DS:
6651 case R_PPC64_GOT_DTPREL16_LO_DS:
6652 case R_PPC64_GOT_DTPREL16_HI:
6653 case R_PPC64_GOT_DTPREL16_HA:
6654 tls_type = TLS_TLS | TLS_DTPREL;
6655 goto dogot;
6656
a33d1f77
AM
6657 case R_PPC64_GOT16:
6658 case R_PPC64_GOT16_DS:
6659 case R_PPC64_GOT16_HA:
6660 case R_PPC64_GOT16_HI:
6661 case R_PPC64_GOT16_LO:
6662 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
6663 dogot:
6664 {
6665 struct got_entry *ent;
6666
58ac9f71
AM
6667 if (h != NULL)
6668 ent = h->got.glist;
411e1bfb
AM
6669 else
6670 ent = local_got_ents[r_symndx];
6671
6672 for (; ent != NULL; ent = ent->next)
6673 if (ent->addend == rel->r_addend
e717da7e 6674 && ent->owner == abfd
411e1bfb
AM
6675 && ent->tls_type == tls_type)
6676 break;
6677 if (ent == NULL)
6678 abort ();
6679 if (ent->got.refcount > 0)
6680 ent->got.refcount -= 1;
6681 }
a33d1f77 6682 break;
65f38f15 6683
a33d1f77
AM
6684 case R_PPC64_PLT16_HA:
6685 case R_PPC64_PLT16_HI:
6686 case R_PPC64_PLT16_LO:
6687 case R_PPC64_PLT32:
6688 case R_PPC64_PLT64:
721956f4
AM
6689 case R_PPC64_REL14:
6690 case R_PPC64_REL14_BRNTAKEN:
6691 case R_PPC64_REL14_BRTAKEN:
5d1634d7 6692 case R_PPC64_REL24:
cbf95972 6693 plt_list = NULL;
58ac9f71 6694 if (h != NULL)
cbf95972
AM
6695 plt_list = &h->plt.plist;
6696 else if (local_got_ents != NULL)
6697 {
6698 struct plt_entry **local_plt = (struct plt_entry **)
6699 (local_got_ents + symtab_hdr->sh_info);
6700 unsigned char *local_got_tls_masks = (unsigned char *)
6701 (local_plt + symtab_hdr->sh_info);
6702 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6703 plt_list = local_plt + r_symndx;
6704 }
6705 if (plt_list)
5d1634d7 6706 {
411e1bfb
AM
6707 struct plt_entry *ent;
6708
cbf95972 6709 for (ent = *plt_list; ent != NULL; ent = ent->next)
411e1bfb
AM
6710 if (ent->addend == rel->r_addend)
6711 break;
7c6c1722 6712 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 6713 ent->plt.refcount -= 1;
5d1634d7 6714 }
e86ce104 6715 break;
5d1634d7 6716
a33d1f77
AM
6717 default:
6718 break;
6719 }
6720 }
b34976b6 6721 return TRUE;
5bd4f169
AM
6722}
6723
deb0e272
AM
6724/* The maximum size of .sfpr. */
6725#define SFPR_MAX (218*4)
6726
6727struct sfpr_def_parms
6728{
699733f6
AM
6729 const char name[12];
6730 unsigned char lo, hi;
deb0e272
AM
6731 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6732 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6733};
6734
a4b6fadd
AM
6735/* Auto-generate _save*, _rest* functions in .sfpr.
6736 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6737 instead. */
deb0e272 6738
4dfe6ac6 6739static bfd_boolean
a4b6fadd
AM
6740sfpr_define (struct bfd_link_info *info,
6741 const struct sfpr_def_parms *parm,
6742 asection *stub_sec)
deb0e272
AM
6743{
6744 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6745 unsigned int i;
6746 size_t len = strlen (parm->name);
6747 bfd_boolean writing = FALSE;
699733f6 6748 char sym[16];
deb0e272 6749
4dfe6ac6
NC
6750 if (htab == NULL)
6751 return FALSE;
6752
deb0e272
AM
6753 memcpy (sym, parm->name, len);
6754 sym[len + 2] = 0;
6755
6756 for (i = parm->lo; i <= parm->hi; i++)
6757 {
a4b6fadd 6758 struct ppc_link_hash_entry *h;
deb0e272
AM
6759
6760 sym[len + 0] = i / 10 + '0';
6761 sym[len + 1] = i % 10 + '0';
a4b6fadd 6762 h = (struct ppc_link_hash_entry *)
b32547cd 6763 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6764 if (stub_sec != NULL)
deb0e272 6765 {
a4b6fadd
AM
6766 if (h != NULL
6767 && h->elf.root.type == bfd_link_hash_defined
6768 && h->elf.root.u.def.section == htab->sfpr)
6769 {
6770 struct elf_link_hash_entry *s;
6771 char buf[32];
6772 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6773 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6774 if (s == NULL)
6775 return FALSE;
6776 if (s->root.type == bfd_link_hash_new
6777 || (s->root.type = bfd_link_hash_defined
6778 && s->root.u.def.section == stub_sec))
6779 {
6780 s->root.type = bfd_link_hash_defined;
6781 s->root.u.def.section = stub_sec;
6782 s->root.u.def.value = (stub_sec->size
6783 + h->elf.root.u.def.value);
6784 s->ref_regular = 1;
6785 s->def_regular = 1;
6786 s->ref_regular_nonweak = 1;
6787 s->forced_local = 1;
6788 s->non_elf = 0;
6789 s->root.linker_def = 1;
6790 }
6791 }
6792 continue;
6793 }
6794 if (h != NULL)
6795 {
6796 h->save_res = 1;
6797 if (!h->elf.def_regular)
deb0e272 6798 {
a4b6fadd
AM
6799 h->elf.root.type = bfd_link_hash_defined;
6800 h->elf.root.u.def.section = htab->sfpr;
6801 h->elf.root.u.def.value = htab->sfpr->size;
6802 h->elf.type = STT_FUNC;
6803 h->elf.def_regular = 1;
b32547cd 6804 h->elf.non_elf = 0;
a4b6fadd
AM
6805 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6806 writing = TRUE;
deb0e272 6807 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6808 {
6809 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6810 if (htab->sfpr->contents == NULL)
6811 return FALSE;
6812 }
deb0e272
AM
6813 }
6814 }
6815 if (writing)
6816 {
6817 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6818 if (i != parm->hi)
6819 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6820 else
6821 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6822 htab->sfpr->size = p - htab->sfpr->contents;
6823 }
6824 }
6825
6826 return TRUE;
6827}
6828
6829static bfd_byte *
6830savegpr0 (bfd *abfd, bfd_byte *p, int r)
6831{
6832 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6833 return p + 4;
6834}
6835
6836static bfd_byte *
6837savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6838{
6839 p = savegpr0 (abfd, p, r);
a078d95a 6840 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6841 p = p + 4;
6842 bfd_put_32 (abfd, BLR, p);
6843 return p + 4;
6844}
6845
6846static bfd_byte *
6847restgpr0 (bfd *abfd, bfd_byte *p, int r)
6848{
6849 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6850 return p + 4;
6851}
6852
6853static bfd_byte *
6854restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6855{
a078d95a 6856 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6857 p = p + 4;
6858 p = restgpr0 (abfd, p, r);
6859 bfd_put_32 (abfd, MTLR_R0, p);
6860 p = p + 4;
6861 if (r == 29)
6862 {
6863 p = restgpr0 (abfd, p, 30);
6864 p = restgpr0 (abfd, p, 31);
6865 }
6866 bfd_put_32 (abfd, BLR, p);
6867 return p + 4;
6868}
6869
6870static bfd_byte *
6871savegpr1 (bfd *abfd, bfd_byte *p, int r)
6872{
6873 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6874 return p + 4;
6875}
6876
6877static bfd_byte *
6878savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6879{
6880 p = savegpr1 (abfd, p, r);
6881 bfd_put_32 (abfd, BLR, p);
6882 return p + 4;
6883}
6884
6885static bfd_byte *
6886restgpr1 (bfd *abfd, bfd_byte *p, int r)
6887{
6888 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6889 return p + 4;
6890}
6891
6892static bfd_byte *
6893restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6894{
6895 p = restgpr1 (abfd, p, r);
6896 bfd_put_32 (abfd, BLR, p);
6897 return p + 4;
6898}
6899
6900static bfd_byte *
6901savefpr (bfd *abfd, bfd_byte *p, int r)
6902{
6903 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6904 return p + 4;
6905}
6906
6907static bfd_byte *
6908savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6909{
6910 p = savefpr (abfd, p, r);
a078d95a 6911 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6912 p = p + 4;
6913 bfd_put_32 (abfd, BLR, p);
6914 return p + 4;
6915}
6916
6917static bfd_byte *
6918restfpr (bfd *abfd, bfd_byte *p, int r)
6919{
6920 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6921 return p + 4;
6922}
6923
6924static bfd_byte *
6925restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6926{
a078d95a 6927 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6928 p = p + 4;
6929 p = restfpr (abfd, p, r);
6930 bfd_put_32 (abfd, MTLR_R0, p);
6931 p = p + 4;
6932 if (r == 29)
6933 {
6934 p = restfpr (abfd, p, 30);
6935 p = restfpr (abfd, p, 31);
6936 }
6937 bfd_put_32 (abfd, BLR, p);
6938 return p + 4;
6939}
6940
6941static bfd_byte *
6942savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6943{
6944 p = savefpr (abfd, p, r);
6945 bfd_put_32 (abfd, BLR, p);
6946 return p + 4;
6947}
6948
6949static bfd_byte *
6950restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6951{
6952 p = restfpr (abfd, p, r);
6953 bfd_put_32 (abfd, BLR, p);
6954 return p + 4;
6955}
6956
6957static bfd_byte *
6958savevr (bfd *abfd, bfd_byte *p, int r)
6959{
6960 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6961 p = p + 4;
6962 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6963 return p + 4;
6964}
6965
6966static bfd_byte *
6967savevr_tail (bfd *abfd, bfd_byte *p, int r)
6968{
6969 p = savevr (abfd, p, r);
6970 bfd_put_32 (abfd, BLR, p);
6971 return p + 4;
6972}
6973
6974static bfd_byte *
6975restvr (bfd *abfd, bfd_byte *p, int r)
6976{
6977 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6978 p = p + 4;
6979 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6980 return p + 4;
6981}
6982
6983static bfd_byte *
6984restvr_tail (bfd *abfd, bfd_byte *p, int r)
6985{
6986 p = restvr (abfd, p, r);
6987 bfd_put_32 (abfd, BLR, p);
6988 return p + 4;
6989}
6990
e86ce104
AM
6991/* Called via elf_link_hash_traverse to transfer dynamic linking
6992 information on function code symbol entries to their corresponding
6993 function descriptor symbol entries. */
deb0e272 6994
b34976b6 6995static bfd_boolean
4ce794b7 6996func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6997{
e86ce104 6998 struct bfd_link_info *info;
65f38f15 6999 struct ppc_link_hash_table *htab;
50bc7936
AM
7000 struct ppc_link_hash_entry *fh;
7001 struct ppc_link_hash_entry *fdh;
7002 bfd_boolean force_local;
5bd4f169 7003
50bc7936
AM
7004 fh = (struct ppc_link_hash_entry *) h;
7005 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 7006 return TRUE;
e86ce104 7007
8c5b4e52
AM
7008 if (!fh->is_func)
7009 return TRUE;
7010
7011 if (fh->elf.root.root.string[0] != '.'
7012 || fh->elf.root.root.string[1] == '\0')
7013 return TRUE;
7014
4ce794b7 7015 info = inf;
65f38f15 7016 htab = ppc_hash_table (info);
4dfe6ac6
NC
7017 if (htab == NULL)
7018 return FALSE;
5bd4f169 7019
8c5b4e52
AM
7020 /* Find the corresponding function descriptor symbol. */
7021 fdh = lookup_fdh (fh, htab);
7022
c09bdfe5
AM
7023 /* Resolve undefined references to dot-symbols as the value
7024 in the function descriptor, if we have one in a regular object.
7025 This is to satisfy cases like ".quad .foo". Calls to functions
7026 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
7027 if ((fh->elf.root.type == bfd_link_hash_undefined
7028 || fh->elf.root.type == bfd_link_hash_undefweak)
7029 && (fdh->elf.root.type == bfd_link_hash_defined
7030 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
7031 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7032 && opd_entry_value (fdh->elf.root.u.def.section,
7033 fdh->elf.root.u.def.value,
c09bdfe5 7034 &fh->elf.root.u.def.section,
aef36ac1 7035 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 7036 {
b31867b6 7037 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 7038 fh->elf.forced_local = 1;
b31867b6
AM
7039 fh->elf.def_regular = fdh->elf.def_regular;
7040 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
7041 }
7042
8c5b4e52
AM
7043 if (!fh->elf.dynamic)
7044 {
7045 struct plt_entry *ent;
5bd4f169 7046
8c5b4e52
AM
7047 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7048 if (ent->plt.refcount > 0)
7049 break;
7050 if (ent == NULL)
7051 return TRUE;
7052 }
5bd4f169 7053
8c5b4e52 7054 /* Create a descriptor as undefined if necessary. */
50bc7936 7055 if (fdh == NULL
0e1862bb 7056 && !bfd_link_executable (info)
50bc7936
AM
7057 && (fh->elf.root.type == bfd_link_hash_undefined
7058 || fh->elf.root.type == bfd_link_hash_undefweak))
7059 {
908b32fc 7060 fdh = make_fdh (info, fh);
bb700d78
AM
7061 if (fdh == NULL)
7062 return FALSE;
50bc7936 7063 }
648cca2c 7064
8c5b4e52 7065 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
7066 if (fdh != NULL
7067 && fdh->fake
8c5b4e52
AM
7068 && (fh->elf.root.type == bfd_link_hash_defined
7069 || fh->elf.root.type == bfd_link_hash_defweak))
7070 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 7071
8c5b4e52
AM
7072 /* Transfer dynamic linking information to the function descriptor. */
7073 if (fdh != NULL)
7074 {
f5385ebf
AM
7075 fdh->elf.ref_regular |= fh->elf.ref_regular;
7076 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7077 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7078 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
7079 fdh->elf.dynamic |= fh->elf.dynamic;
7080 fdh->elf.needs_plt |= (fh->elf.needs_plt
7081 || fh->elf.type == STT_FUNC
7082 || fh->elf.type == STT_GNU_IFUNC);
7083 move_plt_plist (fh, fdh);
7084
7085 if (!fdh->elf.forced_local
7086 && fh->elf.dynindx != -1)
7087 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7088 return FALSE;
e86ce104
AM
7089 }
7090
50bc7936
AM
7091 /* Now that the info is on the function descriptor, clear the
7092 function code sym info. Any function code syms for which we
7093 don't have a definition in a regular file, we force local.
7094 This prevents a shared library from exporting syms that have
7095 been imported from another library. Function code syms that
7096 are really in the library we must leave global to prevent the
7097 linker dragging in a definition from a static library. */
93f3fa99
AM
7098 force_local = (!fh->elf.def_regular
7099 || fdh == NULL
7100 || !fdh->elf.def_regular
7101 || fdh->elf.forced_local);
50bc7936
AM
7102 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7103
b34976b6 7104 return TRUE;
e86ce104 7105}
40b8271b 7106
a4b6fadd
AM
7107static const struct sfpr_def_parms save_res_funcs[] =
7108 {
7109 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7110 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7111 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7112 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7113 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7114 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7115 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7116 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7117 { "._savef", 14, 31, savefpr, savefpr1_tail },
7118 { "._restf", 14, 31, restfpr, restfpr1_tail },
7119 { "_savevr_", 20, 31, savevr, savevr_tail },
7120 { "_restvr_", 20, 31, restvr, restvr_tail }
7121 };
7122
e86ce104 7123/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
7124 this hook to a) provide some gcc support functions, and b) transfer
7125 dynamic linking information gathered so far on function code symbol
7126 entries, to their corresponding function descriptor symbol entries. */
deb0e272 7127
b34976b6 7128static bfd_boolean
4ce794b7
AM
7129ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7130 struct bfd_link_info *info)
e86ce104
AM
7131{
7132 struct ppc_link_hash_table *htab;
7133
7134 htab = ppc_hash_table (info);
4dfe6ac6
NC
7135 if (htab == NULL)
7136 return FALSE;
7137
b32547cd
AM
7138 /* Provide any missing _save* and _rest* functions. */
7139 if (htab->sfpr != NULL)
7140 {
7141 unsigned int i;
7142
7143 htab->sfpr->size = 0;
7144 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7145 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7146 return FALSE;
7147 if (htab->sfpr->size == 0)
7148 htab->sfpr->flags |= SEC_EXCLUDE;
7149 }
7150
7151 if (bfd_link_relocatable (info))
7152 return TRUE;
7153
7154 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7155 {
7156 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7157 /* Make .TOC. defined so as to prevent it being made dynamic.
7158 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7159 if (!htab->elf.hgot->def_regular
7160 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7161 {
7162 htab->elf.hgot->root.type = bfd_link_hash_defined;
7163 htab->elf.hgot->root.u.def.value = 0;
7164 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7165 htab->elf.hgot->def_regular = 1;
7166 htab->elf.hgot->root.linker_def = 1;
7167 }
dba6fa9b 7168 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7169 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7170 | STV_HIDDEN);
7171 }
c66bb0ee 7172
8c5b4e52
AM
7173 if (htab->need_func_desc_adj)
7174 {
7175 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7176 htab->need_func_desc_adj = 0;
7177 }
805fc799 7178
b34976b6 7179 return TRUE;
e86ce104
AM
7180}
7181
8a2058b5
AM
7182/* Return true if we have dynamic relocs against H that apply to
7183 read-only sections. */
a345bc8d
AM
7184
7185static bfd_boolean
7186readonly_dynrelocs (struct elf_link_hash_entry *h)
7187{
7188 struct ppc_link_hash_entry *eh;
7189 struct elf_dyn_relocs *p;
7190
7191 eh = (struct ppc_link_hash_entry *) h;
7192 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7193 {
7194 asection *s = p->sec->output_section;
7195
7196 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7197 return TRUE;
7198 }
7199 return FALSE;
7200}
7201
d311bc8b
AM
7202/* Return true if we have dynamic relocs against H or any of its weak
7203 aliases, that apply to read-only sections. */
7204
7205static bfd_boolean
7206alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7207{
7208 struct ppc_link_hash_entry *eh;
7209
7210 eh = (struct ppc_link_hash_entry *) h;
7211 do
7212 {
7213 if (readonly_dynrelocs (&eh->elf))
7214 return TRUE;
7215 eh = eh->weakref;
7216 } while (eh != NULL && &eh->elf != h);
7217
7218 return FALSE;
7219}
8a2058b5 7220
8a9e8e72
AM
7221/* Return whether EH has pc-relative dynamic relocs. */
7222
7223static bfd_boolean
7224pc_dynrelocs (struct ppc_link_hash_entry *eh)
7225{
7226 struct elf_dyn_relocs *p;
7227
7228 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7229 if (p->pc_count != 0)
7230 return TRUE;
7231 return FALSE;
7232}
7233
8a2058b5
AM
7234/* Return true if a global entry stub will be created for H. Valid
7235 for ELFv2 before plt entries have been allocated. */
7236
7237static bfd_boolean
7238global_entry_stub (struct elf_link_hash_entry *h)
7239{
7240 struct plt_entry *pent;
7241
7242 if (!h->pointer_equality_needed
7243 || h->def_regular)
7244 return FALSE;
7245
7246 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7247 if (pent->plt.refcount > 0
7248 && pent->addend == 0)
7249 return TRUE;
7250
7251 return FALSE;
7252}
7253
e86ce104
AM
7254/* Adjust a symbol defined by a dynamic object and referenced by a
7255 regular object. The current definition is in some section of the
7256 dynamic object, but we're not including those sections. We have to
7257 change the definition to something the rest of the link can
7258 understand. */
7259
b34976b6 7260static bfd_boolean
4ce794b7
AM
7261ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7262 struct elf_link_hash_entry *h)
e86ce104
AM
7263{
7264 struct ppc_link_hash_table *htab;
e86ce104 7265 asection *s;
e86ce104
AM
7266
7267 htab = ppc_hash_table (info);
4dfe6ac6
NC
7268 if (htab == NULL)
7269 return FALSE;
e86ce104
AM
7270
7271 /* Deal with function syms. */
7272 if (h->type == STT_FUNC
e054468f 7273 || h->type == STT_GNU_IFUNC
f5385ebf 7274 || h->needs_plt)
e86ce104
AM
7275 {
7276 /* Clear procedure linkage table information for any symbol that
7277 won't need a .plt entry. */
411e1bfb
AM
7278 struct plt_entry *ent;
7279 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7280 if (ent->plt.refcount > 0)
7281 break;
8387904d 7282 if (ent == NULL
e054468f
AM
7283 || (h->type != STT_GNU_IFUNC
7284 && (SYMBOL_CALLS_LOCAL (info, h)
7285 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
a4b6fadd
AM
7286 && h->root.type == bfd_link_hash_undefweak)))
7287 || ((struct ppc_link_hash_entry *) h)->save_res)
40b8271b 7288 {
411e1bfb 7289 h->plt.plist = NULL;
f5385ebf 7290 h->needs_plt = 0;
d1eca1e4 7291 h->pointer_equality_needed = 0;
40b8271b 7292 }
8a2058b5 7293 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7294 {
d1eca1e4
AM
7295 /* Taking a function's address in a read/write section
7296 doesn't require us to define the function symbol in the
7297 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7298 be used instead. The reason we prefer a few more dynamic
7299 relocs is that calling via a global entry stub costs a
7300 few more instructions, and pointer_equality_needed causes
7301 extra work in ld.so when resolving these symbols. */
7302 if (global_entry_stub (h)
d311bc8b 7303 && !alias_readonly_dynrelocs (h))
d1eca1e4
AM
7304 {
7305 h->pointer_equality_needed = 0;
8a2058b5
AM
7306 /* After adjust_dynamic_symbol, non_got_ref set in
7307 the non-pic case means that dyn_relocs for this
7308 symbol should be discarded. */
d1eca1e4
AM
7309 h->non_got_ref = 0;
7310 }
7311
a345bc8d
AM
7312 /* If making a plt entry, then we don't need copy relocs. */
7313 return TRUE;
7314 }
5bd4f169 7315 }
bbd7ec4a 7316 else
411e1bfb 7317 h->plt.plist = NULL;
5bd4f169
AM
7318
7319 /* If this is a weak symbol, and there is a real definition, the
7320 processor independent code will have arranged for us to see the
7321 real definition first, and we can just use the same value. */
f6e332e6 7322 if (h->u.weakdef != NULL)
5bd4f169 7323 {
f6e332e6
AM
7324 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7325 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7326 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7327 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 7328 if (ELIMINATE_COPY_RELOCS)
f6e332e6 7329 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 7330 return TRUE;
5bd4f169
AM
7331 }
7332
5bd4f169
AM
7333 /* If we are creating a shared library, we must presume that the
7334 only references to the symbol are via the global offset table.
7335 For such cases we need not do anything here; the relocations will
7336 be handled correctly by relocate_section. */
0e1862bb 7337 if (bfd_link_pic (info))
b34976b6 7338 return TRUE;
5bd4f169 7339
65f38f15
AM
7340 /* If there are no references to this symbol that do not use the
7341 GOT, we don't need to generate a copy reloc. */
f5385ebf 7342 if (!h->non_got_ref)
b34976b6 7343 return TRUE;
65f38f15 7344
b186458a 7345 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7346 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7347
d93d1c80
AM
7348 /* If -z nocopyreloc was given, don't generate them either. */
7349 || info->nocopyreloc
a127494f 7350
d93d1c80
AM
7351 /* If we didn't find any dynamic relocs in read-only sections, then
7352 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7353 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7354
d93d1c80
AM
7355 /* Protected variables do not work with .dynbss. The copy in
7356 .dynbss won't be used by the shared library with the protected
7357 definition for the variable. Text relocations are preferable
7358 to an incorrect program. */
7359 || h->protected_def)
a127494f
AM
7360 {
7361 h->non_got_ref = 0;
7362 return TRUE;
7363 }
7364
5d35169e 7365 if (h->plt.plist != NULL)
97b639ba
AM
7366 {
7367 /* We should never get here, but unfortunately there are versions
7368 of gcc out there that improperly (for this ABI) put initialized
7369 function pointers, vtable refs and suchlike in read-only
7370 sections. Allow them to proceed, but warn that this might
7371 break at runtime. */
25f53a85 7372 info->callbacks->einfo
bc30df16 7373 (_("%P: copy reloc against `%T' requires lazy plt linking; "
25f53a85 7374 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7375 h->root.root.string);
7376 }
5d35169e
AM
7377
7378 /* This is a reference to a symbol defined by a dynamic object which
7379 is not a function. */
7380
5bd4f169
AM
7381 /* We must allocate the symbol in our .dynbss section, which will
7382 become part of the .bss section of the executable. There will be
7383 an entry for this symbol in the .dynsym section. The dynamic
7384 object will contain position independent code, so all references
7385 from the dynamic object to this symbol will go through the global
7386 offset table. The dynamic linker will use the .dynsym entry to
7387 determine the address it must put in the global offset table, so
7388 both the dynamic object and the regular object will refer to the
7389 same memory location for the variable. */
5bd4f169 7390
04c9666a
AM
7391 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7392 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
7393 runtime process image. We need to remember the offset into the
7394 .rela.bss section we are going to use. */
1d7e9d18 7395 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7396 {
eea6121a 7397 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 7398 h->needs_copy = 1;
5bd4f169
AM
7399 }
7400
4ce794b7 7401 s = htab->dynbss;
5bd4f169 7402
6cabe1ea 7403 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7404}
7405
e86ce104
AM
7406/* If given a function descriptor symbol, hide both the function code
7407 sym and the descriptor. */
7408static void
4ce794b7
AM
7409ppc64_elf_hide_symbol (struct bfd_link_info *info,
7410 struct elf_link_hash_entry *h,
7411 bfd_boolean force_local)
e86ce104 7412{
34814b9f 7413 struct ppc_link_hash_entry *eh;
e86ce104
AM
7414 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7415
34814b9f
AM
7416 eh = (struct ppc_link_hash_entry *) h;
7417 if (eh->is_func_descriptor)
e86ce104 7418 {
34814b9f 7419 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7420
721956f4 7421 if (fh == NULL)
d1329ca3
AM
7422 {
7423 const char *p, *q;
b8ac2841 7424 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
7425 char save;
7426
7427 /* We aren't supposed to use alloca in BFD because on
7428 systems which do not have alloca the version in libiberty
7429 calls xmalloc, which might cause the program to crash
7430 when it runs out of memory. This function doesn't have a
7431 return status, so there's no way to gracefully return an
7432 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7433 accessed; It's either a string in an ELF string table,
7434 or allocated in an objalloc structure. */
d1329ca3 7435
34814b9f 7436 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7437 save = *p;
7438 *(char *) p = '.';
34814b9f 7439 fh = (struct ppc_link_hash_entry *)
b8ac2841 7440 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7441 *(char *) p = save;
7442
7443 /* Unfortunately, if it so happens that the string we were
7444 looking for was allocated immediately before this string,
7445 then we overwrote the string terminator. That's the only
7446 reason the lookup should fail. */
7447 if (fh == NULL)
7448 {
34814b9f
AM
7449 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7450 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7451 --q, --p;
34814b9f
AM
7452 if (q < eh->elf.root.root.string && *p == '.')
7453 fh = (struct ppc_link_hash_entry *)
b8ac2841 7454 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7455 }
7456 if (fh != NULL)
7457 {
34814b9f
AM
7458 eh->oh = fh;
7459 fh->oh = eh;
d1329ca3
AM
7460 }
7461 }
e86ce104 7462 if (fh != NULL)
34814b9f 7463 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7464 }
7465}
7466
411e1bfb 7467static bfd_boolean
8843416a
AM
7468get_sym_h (struct elf_link_hash_entry **hp,
7469 Elf_Internal_Sym **symp,
7470 asection **symsecp,
f961d9dd 7471 unsigned char **tls_maskp,
8843416a
AM
7472 Elf_Internal_Sym **locsymsp,
7473 unsigned long r_symndx,
7474 bfd *ibfd)
411e1bfb 7475{
0ffa91dd 7476 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7477
7478 if (r_symndx >= symtab_hdr->sh_info)
7479 {
7480 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7481 struct elf_link_hash_entry *h;
7482
7483 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7484 h = elf_follow_link (h);
411e1bfb
AM
7485
7486 if (hp != NULL)
7487 *hp = h;
7488
7489 if (symp != NULL)
7490 *symp = NULL;
7491
7492 if (symsecp != NULL)
7493 {
7494 asection *symsec = NULL;
7495 if (h->root.type == bfd_link_hash_defined
7496 || h->root.type == bfd_link_hash_defweak)
7497 symsec = h->root.u.def.section;
7498 *symsecp = symsec;
7499 }
7500
e7b938ca 7501 if (tls_maskp != NULL)
411e1bfb
AM
7502 {
7503 struct ppc_link_hash_entry *eh;
7504
7505 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7506 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7507 }
7508 }
7509 else
7510 {
7511 Elf_Internal_Sym *sym;
7512 Elf_Internal_Sym *locsyms = *locsymsp;
7513
7514 if (locsyms == NULL)
7515 {
7516 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7517 if (locsyms == NULL)
7518 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7519 symtab_hdr->sh_info,
7520 0, NULL, NULL, NULL);
7521 if (locsyms == NULL)
7522 return FALSE;
7523 *locsymsp = locsyms;
7524 }
7525 sym = locsyms + r_symndx;
7526
7527 if (hp != NULL)
7528 *hp = NULL;
7529
7530 if (symp != NULL)
7531 *symp = sym;
7532
7533 if (symsecp != NULL)
cb33740c 7534 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7535
e7b938ca 7536 if (tls_maskp != NULL)
411e1bfb
AM
7537 {
7538 struct got_entry **lgot_ents;
f961d9dd 7539 unsigned char *tls_mask;
411e1bfb 7540
e7b938ca 7541 tls_mask = NULL;
411e1bfb
AM
7542 lgot_ents = elf_local_got_ents (ibfd);
7543 if (lgot_ents != NULL)
7544 {
e054468f
AM
7545 struct plt_entry **local_plt = (struct plt_entry **)
7546 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7547 unsigned char *lgot_masks = (unsigned char *)
e054468f 7548 (local_plt + symtab_hdr->sh_info);
e7b938ca 7549 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7550 }
e7b938ca 7551 *tls_maskp = tls_mask;
411e1bfb
AM
7552 }
7553 }
7554 return TRUE;
7555}
7556
e7b938ca 7557/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7558 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7559 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7560
7561static int
f961d9dd 7562get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7563 unsigned long *toc_symndx,
7564 bfd_vma *toc_addend,
0d4792f7 7565 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7566 const Elf_Internal_Rela *rel,
7567 bfd *ibfd)
411e1bfb
AM
7568{
7569 unsigned long r_symndx;
0d4792f7 7570 int next_r;
411e1bfb
AM
7571 struct elf_link_hash_entry *h;
7572 Elf_Internal_Sym *sym;
7573 asection *sec;
7574 bfd_vma off;
7575
7576 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7577 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7578 return 0;
411e1bfb 7579
e7b938ca 7580 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7581 || sec == NULL
6bee8834 7582 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7583 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7584 return 1;
411e1bfb
AM
7585
7586 /* Look inside a TOC section too. */
7587 if (h != NULL)
7588 {
7589 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7590 off = h->root.u.def.value;
7591 }
7592 else
7593 off = sym->st_value;
7594 off += rel->r_addend;
7595 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7596 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7597 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7598 if (toc_symndx != NULL)
7599 *toc_symndx = r_symndx;
3a71aa26
AM
7600 if (toc_addend != NULL)
7601 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7602 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7603 return 0;
854b41e7 7604 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7605 && (next_r == -1 || next_r == -2))
7606 return 1 - next_r;
951fd09b 7607 return 1;
411e1bfb
AM
7608}
7609
3b421ab3
AM
7610/* Find (or create) an entry in the tocsave hash table. */
7611
7612static struct tocsave_entry *
7613tocsave_find (struct ppc_link_hash_table *htab,
7614 enum insert_option insert,
7615 Elf_Internal_Sym **local_syms,
7616 const Elf_Internal_Rela *irela,
7617 bfd *ibfd)
7618{
7619 unsigned long r_indx;
7620 struct elf_link_hash_entry *h;
7621 Elf_Internal_Sym *sym;
7622 struct tocsave_entry ent, *p;
7623 hashval_t hash;
7624 struct tocsave_entry **slot;
7625
7626 r_indx = ELF64_R_SYM (irela->r_info);
7627 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7628 return NULL;
7629 if (ent.sec == NULL || ent.sec->output_section == NULL)
7630 {
4eca0228 7631 _bfd_error_handler
3b421ab3
AM
7632 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7633 return NULL;
7634 }
7635
7636 if (h != NULL)
7637 ent.offset = h->root.u.def.value;
7638 else
7639 ent.offset = sym->st_value;
7640 ent.offset += irela->r_addend;
7641
7642 hash = tocsave_htab_hash (&ent);
7643 slot = ((struct tocsave_entry **)
7644 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7645 if (slot == NULL)
7646 return NULL;
7647
7648 if (*slot == NULL)
7649 {
7650 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7651 if (p == NULL)
7652 return NULL;
7653 *p = ent;
7654 *slot = p;
7655 }
7656 return *slot;
7657}
7658
754021d0 7659/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7660 code for the old ABI, these will already have been done. */
754021d0
AM
7661
7662static bfd_boolean
7663adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7664{
7665 struct ppc_link_hash_entry *eh;
7666 asection *sym_sec;
74f0fb50 7667 struct _opd_sec_data *opd;
754021d0
AM
7668
7669 if (h->root.type == bfd_link_hash_indirect)
7670 return TRUE;
7671
754021d0
AM
7672 if (h->root.type != bfd_link_hash_defined
7673 && h->root.type != bfd_link_hash_defweak)
7674 return TRUE;
7675
7676 eh = (struct ppc_link_hash_entry *) h;
7677 if (eh->adjust_done)
7678 return TRUE;
7679
7680 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7681 opd = get_opd_info (sym_sec);
7682 if (opd != NULL && opd->adjust != NULL)
754021d0 7683 {
51aecdc5 7684 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7685 if (adjust == -1)
7686 {
7687 /* This entry has been deleted. */
b3fac117 7688 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7689 if (dsec == NULL)
7690 {
7691 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7692 if (discarded_section (dsec))
81688140 7693 {
b3fac117 7694 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7695 break;
7696 }
7697 }
4025353c 7698 eh->elf.root.u.def.value = 0;
81688140 7699 eh->elf.root.u.def.section = dsec;
4025353c
AM
7700 }
7701 else
7702 eh->elf.root.u.def.value += adjust;
754021d0
AM
7703 eh->adjust_done = 1;
7704 }
7705 return TRUE;
7706}
7707
8c1d1bb8 7708/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7709 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7710 have already been determined. */
7711
7712static bfd_boolean
7713dec_dynrel_count (bfd_vma r_info,
7714 asection *sec,
7715 struct bfd_link_info *info,
7716 Elf_Internal_Sym **local_syms,
7717 struct elf_link_hash_entry *h,
19e08130 7718 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7719{
7720 enum elf_ppc64_reloc_type r_type;
19e08130 7721 asection *sym_sec = NULL;
8c1d1bb8
AM
7722
7723 /* Can this reloc be dynamic? This switch, and later tests here
7724 should be kept in sync with the code in check_relocs. */
7725 r_type = ELF64_R_TYPE (r_info);
7726 switch (r_type)
7727 {
7728 default:
7729 return TRUE;
7730
7731 case R_PPC64_TPREL16:
7732 case R_PPC64_TPREL16_LO:
7733 case R_PPC64_TPREL16_HI:
7734 case R_PPC64_TPREL16_HA:
7735 case R_PPC64_TPREL16_DS:
7736 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7737 case R_PPC64_TPREL16_HIGH:
7738 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7739 case R_PPC64_TPREL16_HIGHER:
7740 case R_PPC64_TPREL16_HIGHERA:
7741 case R_PPC64_TPREL16_HIGHEST:
7742 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 7743 if (!bfd_link_pic (info))
8c1d1bb8
AM
7744 return TRUE;
7745
7746 case R_PPC64_TPREL64:
7747 case R_PPC64_DTPMOD64:
7748 case R_PPC64_DTPREL64:
7749 case R_PPC64_ADDR64:
7750 case R_PPC64_REL30:
7751 case R_PPC64_REL32:
7752 case R_PPC64_REL64:
7753 case R_PPC64_ADDR14:
7754 case R_PPC64_ADDR14_BRNTAKEN:
7755 case R_PPC64_ADDR14_BRTAKEN:
7756 case R_PPC64_ADDR16:
7757 case R_PPC64_ADDR16_DS:
7758 case R_PPC64_ADDR16_HA:
7759 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7760 case R_PPC64_ADDR16_HIGH:
7761 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7762 case R_PPC64_ADDR16_HIGHER:
7763 case R_PPC64_ADDR16_HIGHERA:
7764 case R_PPC64_ADDR16_HIGHEST:
7765 case R_PPC64_ADDR16_HIGHESTA:
7766 case R_PPC64_ADDR16_LO:
7767 case R_PPC64_ADDR16_LO_DS:
7768 case R_PPC64_ADDR24:
7769 case R_PPC64_ADDR32:
7770 case R_PPC64_UADDR16:
7771 case R_PPC64_UADDR32:
7772 case R_PPC64_UADDR64:
7773 case R_PPC64_TOC:
7774 break;
7775 }
7776
7777 if (local_syms != NULL)
7778 {
7779 unsigned long r_symndx;
8c1d1bb8
AM
7780 bfd *ibfd = sec->owner;
7781
7782 r_symndx = ELF64_R_SYM (r_info);
7783 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7784 return FALSE;
7785 }
7786
0e1862bb 7787 if ((bfd_link_pic (info)
1d483afe 7788 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7789 || (h != NULL
198f1157 7790 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7791 || h->root.type == bfd_link_hash_defweak
7792 || !h->def_regular))))
7793 || (ELIMINATE_COPY_RELOCS
0e1862bb 7794 && !bfd_link_pic (info)
8c1d1bb8
AM
7795 && h != NULL
7796 && (h->root.type == bfd_link_hash_defweak
7797 || !h->def_regular)))
7798 ;
7799 else
7800 return TRUE;
7801
7802 if (h != NULL)
6edfbbad 7803 {
19e08130
AM
7804 struct elf_dyn_relocs *p;
7805 struct elf_dyn_relocs **pp;
7806 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7807
7808 /* elf_gc_sweep may have already removed all dyn relocs associated
7809 with local syms for a given section. Also, symbol flags are
7810 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7811 report a dynreloc miscount. */
7812 if (*pp == NULL && info->gc_sections)
7813 return TRUE;
7814
7815 while ((p = *pp) != NULL)
60124e18 7816 {
19e08130
AM
7817 if (p->sec == sec)
7818 {
7819 if (!must_be_dyn_reloc (info, r_type))
7820 p->pc_count -= 1;
7821 p->count -= 1;
7822 if (p->count == 0)
7823 *pp = p->next;
7824 return TRUE;
7825 }
7826 pp = &p->next;
60124e18 7827 }
6edfbbad 7828 }
19e08130
AM
7829 else
7830 {
7831 struct ppc_dyn_relocs *p;
7832 struct ppc_dyn_relocs **pp;
7833 void *vpp;
7834 bfd_boolean is_ifunc;
8c1d1bb8 7835
19e08130
AM
7836 if (local_syms == NULL)
7837 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7838 if (sym_sec == NULL)
7839 sym_sec = sec;
c57da1a7 7840
19e08130
AM
7841 vpp = &elf_section_data (sym_sec)->local_dynrel;
7842 pp = (struct ppc_dyn_relocs **) vpp;
7843
7844 if (*pp == NULL && info->gc_sections)
7845 return TRUE;
7846
7847 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7848 while ((p = *pp) != NULL)
8c1d1bb8 7849 {
19e08130
AM
7850 if (p->sec == sec && p->ifunc == is_ifunc)
7851 {
7852 p->count -= 1;
7853 if (p->count == 0)
7854 *pp = p->next;
7855 return TRUE;
7856 }
7857 pp = &p->next;
8c1d1bb8 7858 }
8c1d1bb8
AM
7859 }
7860
695344c0 7861 /* xgettext:c-format */
8de848d8 7862 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7863 sec->owner, sec);
8c1d1bb8
AM
7864 bfd_set_error (bfd_error_bad_value);
7865 return FALSE;
7866}
7867
754021d0
AM
7868/* Remove unused Official Procedure Descriptor entries. Currently we
7869 only remove those associated with functions in discarded link-once
7870 sections, or weakly defined functions that have been overridden. It
7871 would be possible to remove many more entries for statically linked
7872 applications. */
7873
b34976b6 7874bfd_boolean
e7d1c40c 7875ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7876{
7877 bfd *ibfd;
754021d0 7878 bfd_boolean some_edited = FALSE;
3f764659 7879 asection *need_pad = NULL;
e7d1c40c
AM
7880 struct ppc_link_hash_table *htab;
7881
7882 htab = ppc_hash_table (info);
7883 if (htab == NULL)
7884 return FALSE;
1e2f5b6e 7885
c72f2fb2 7886 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7887 {
7888 asection *sec;
7889 Elf_Internal_Rela *relstart, *rel, *relend;
7890 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7891 Elf_Internal_Sym *local_syms;
74f0fb50 7892 struct _opd_sec_data *opd;
51aecdc5 7893 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7894 bfd_size_type cnt_16b = 0;
1e2f5b6e 7895
854b41e7
AM
7896 if (!is_ppc64_elf (ibfd))
7897 continue;
7898
1e2f5b6e 7899 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7900 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7901 continue;
7902
dbaa2011 7903 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7904 continue;
7905
1e2f5b6e
AM
7906 if (sec->output_section == bfd_abs_section_ptr)
7907 continue;
7908
7909 /* Look through the section relocs. */
7910 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7911 continue;
7912
6cdc0ccc 7913 local_syms = NULL;
0ffa91dd 7914 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7915
7916 /* Read the relocations. */
4ce794b7 7917 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7918 info->keep_memory);
1e2f5b6e 7919 if (relstart == NULL)
b34976b6 7920 return FALSE;
1e2f5b6e
AM
7921
7922 /* First run through the relocs to check they are sane, and to
7923 determine whether we need to edit this opd section. */
b34976b6 7924 need_edit = FALSE;
51aecdc5 7925 broken = FALSE;
3f764659 7926 need_pad = sec;
1e2f5b6e 7927 relend = relstart + sec->reloc_count;
50bc7936 7928 for (rel = relstart; rel < relend; )
1e2f5b6e 7929 {
04c9666a 7930 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7931 unsigned long r_symndx;
7932 asection *sym_sec;
7933 struct elf_link_hash_entry *h;
7934 Elf_Internal_Sym *sym;
51aecdc5 7935 bfd_vma offset;
1e2f5b6e 7936
51aecdc5 7937 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7938 only interested in the reloc pointing to a function entry
7939 point. */
51aecdc5
AM
7940 offset = rel->r_offset;
7941 if (rel + 1 == relend
7942 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7943 {
7944 /* If someone messes with .opd alignment then after a
7945 "ld -r" we might have padding in the middle of .opd.
7946 Also, there's nothing to prevent someone putting
7947 something silly in .opd with the assembler. No .opd
b34976b6 7948 optimization for them! */
3f764659 7949 broken_opd:
4eca0228 7950 _bfd_error_handler
d003868e 7951 (_("%B: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7952 broken = TRUE;
1e2f5b6e
AM
7953 break;
7954 }
7955
50bc7936
AM
7956 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7957 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7958 {
4eca0228 7959 _bfd_error_handler
695344c0 7960 /* xgettext:c-format */
d003868e
AM
7961 (_("%B: unexpected reloc type %u in .opd section"),
7962 ibfd, r_type);
51aecdc5 7963 broken = TRUE;
50bc7936
AM
7964 break;
7965 }
7966
1e2f5b6e 7967 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7968 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7969 r_symndx, ibfd))
50bc7936 7970 goto error_ret;
1e2f5b6e
AM
7971
7972 if (sym_sec == NULL || sym_sec->owner == NULL)
7973 {
411e1bfb
AM
7974 const char *sym_name;
7975 if (h != NULL)
7976 sym_name = h->root.root.string;
7977 else
26c61ae5
L
7978 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7979 sym_sec);
411e1bfb 7980
4eca0228 7981 _bfd_error_handler
695344c0 7982 /* xgettext:c-format */
d003868e
AM
7983 (_("%B: undefined sym `%s' in .opd section"),
7984 ibfd, sym_name);
51aecdc5 7985 broken = TRUE;
1e2f5b6e
AM
7986 break;
7987 }
7988
51020317
AM
7989 /* opd entries are always for functions defined in the
7990 current input bfd. If the symbol isn't defined in the
7991 input bfd, then we won't be using the function in this
7992 bfd; It must be defined in a linkonce section in another
7993 bfd, or is weak. It's also possible that we are
7994 discarding the function due to a linker script /DISCARD/,
7995 which we test for via the output_section. */
7996 if (sym_sec->owner != ibfd
7997 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7998 need_edit = TRUE;
1e2f5b6e 7999
50bc7936 8000 rel += 2;
51aecdc5
AM
8001 if (rel + 1 == relend
8002 || (rel + 2 < relend
8003 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8004 ++rel;
8005
8006 if (rel == relend)
3f764659
JJ
8007 {
8008 if (sec->size == offset + 24)
8009 {
8010 need_pad = NULL;
8011 break;
8012 }
51aecdc5 8013 if (sec->size == offset + 16)
3f764659
JJ
8014 {
8015 cnt_16b++;
8016 break;
8017 }
8018 goto broken_opd;
8019 }
3f764659
JJ
8020 else if (rel + 1 < relend
8021 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8022 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8023 {
51aecdc5
AM
8024 if (rel[0].r_offset == offset + 16)
8025 cnt_16b++;
8026 else if (rel[0].r_offset != offset + 24)
8027 goto broken_opd;
3f764659
JJ
8028 }
8029 else
8030 goto broken_opd;
1e2f5b6e
AM
8031 }
8032
e7d1c40c 8033 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 8034
51aecdc5 8035 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
8036 {
8037 Elf_Internal_Rela *write_rel;
d4730f92 8038 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 8039 bfd_byte *rptr, *wptr;
983bddc8 8040 bfd_byte *new_contents;
74f0fb50
AM
8041 bfd_size_type amt;
8042
983bddc8 8043 new_contents = NULL;
51aecdc5 8044 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 8045 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 8046 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
8047 if (opd->adjust == NULL)
8048 return FALSE;
8049 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
8050
8051 /* This seems a waste of time as input .opd sections are all
8052 zeros as generated by gcc, but I suppose there's no reason
8053 this will always be so. We might start putting something in
8054 the third word of .opd entries. */
8055 if ((sec->flags & SEC_IN_MEMORY) == 0)
8056 {
eea6121a
AM
8057 bfd_byte *loc;
8058 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 8059 {
eea6121a
AM
8060 if (loc != NULL)
8061 free (loc);
50bc7936 8062 error_ret:
6cdc0ccc
AM
8063 if (local_syms != NULL
8064 && symtab_hdr->contents != (unsigned char *) local_syms)
8065 free (local_syms);
6cdc0ccc
AM
8066 if (elf_section_data (sec)->relocs != relstart)
8067 free (relstart);
b34976b6 8068 return FALSE;
6cdc0ccc 8069 }
1e2f5b6e
AM
8070 sec->contents = loc;
8071 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8072 }
8073
8074 elf_section_data (sec)->relocs = relstart;
8075
3f764659 8076 new_contents = sec->contents;
3f764659
JJ
8077 if (add_aux_fields)
8078 {
8079 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8080 if (new_contents == NULL)
8081 return FALSE;
51aecdc5 8082 need_pad = NULL;
3f764659 8083 }
b4f4e59f
AM
8084 wptr = new_contents;
8085 rptr = sec->contents;
1e2f5b6e 8086 write_rel = relstart;
51aecdc5 8087 for (rel = relstart; rel < relend; )
1e2f5b6e 8088 {
50bc7936
AM
8089 unsigned long r_symndx;
8090 asection *sym_sec;
8091 struct elf_link_hash_entry *h;
51aecdc5 8092 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 8093 Elf_Internal_Sym *sym;
51aecdc5
AM
8094 long opd_ent_size;
8095 Elf_Internal_Rela *next_rel;
8096 bfd_boolean skip;
50bc7936
AM
8097
8098 r_symndx = ELF64_R_SYM (rel->r_info);
8099 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 8100 r_symndx, ibfd))
50bc7936
AM
8101 goto error_ret;
8102
51aecdc5
AM
8103 next_rel = rel + 2;
8104 if (next_rel + 1 == relend
8105 || (next_rel + 2 < relend
8106 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8107 ++next_rel;
8108
8109 /* See if the .opd entry is full 24 byte or
8110 16 byte (with fd_aux entry overlapped with next
8111 fd_func). */
8112 opd_ent_size = 24;
8113 if (next_rel == relend)
1e2f5b6e 8114 {
51aecdc5 8115 if (sec->size == rel->r_offset + 16)
3f764659 8116 opd_ent_size = 16;
51aecdc5
AM
8117 }
8118 else if (next_rel->r_offset == rel->r_offset + 16)
8119 opd_ent_size = 16;
3f764659 8120
51aecdc5
AM
8121 if (h != NULL
8122 && h->root.root.string[0] == '.')
8123 {
8c5b4e52
AM
8124 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8125 if (fdh != NULL)
8126 {
8127 fdh = ppc_follow_link (fdh);
8128 if (fdh->elf.root.type != bfd_link_hash_defined
8129 && fdh->elf.root.type != bfd_link_hash_defweak)
8130 fdh = NULL;
8131 }
51aecdc5 8132 }
1e2f5b6e 8133
51aecdc5
AM
8134 skip = (sym_sec->owner != ibfd
8135 || sym_sec->output_section == bfd_abs_section_ptr);
8136 if (skip)
8137 {
8138 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8139 {
51aecdc5
AM
8140 /* Arrange for the function descriptor sym
8141 to be dropped. */
8142 fdh->elf.root.u.def.value = 0;
8143 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8144 }
51aecdc5 8145 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8146
0e1862bb 8147 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8148 rel = next_rel;
8149 else
8150 while (1)
8151 {
8152 if (!dec_dynrel_count (rel->r_info, sec, info,
8153 NULL, h, sym))
8154 goto error_ret;
754021d0 8155
51aecdc5
AM
8156 if (++rel == next_rel)
8157 break;
1e2f5b6e 8158
51aecdc5
AM
8159 r_symndx = ELF64_R_SYM (rel->r_info);
8160 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8161 r_symndx, ibfd))
8162 goto error_ret;
8163 }
50bc7936
AM
8164 }
8165 else
1e2f5b6e 8166 {
51aecdc5
AM
8167 /* We'll be keeping this opd entry. */
8168 long adjust;
8169
8170 if (fdh != NULL)
8171 {
8172 /* Redefine the function descriptor symbol to
8173 this location in the opd section. It is
8174 necessary to update the value here rather
8175 than using an array of adjustments as we do
8176 for local symbols, because various places
8177 in the generic ELF code use the value
8178 stored in u.def.value. */
8179 fdh->elf.root.u.def.value = wptr - new_contents;
8180 fdh->adjust_done = 1;
8181 }
8182
8183 /* Local syms are a bit tricky. We could
8184 tweak them as they can be cached, but
8185 we'd need to look through the local syms
8186 for the function descriptor sym which we
8187 don't have at the moment. So keep an
8188 array of adjustments. */
8189 adjust = (wptr - new_contents) - (rptr - sec->contents);
8190 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8191
8192 if (wptr != rptr)
8193 memcpy (wptr, rptr, opd_ent_size);
8194 wptr += opd_ent_size;
8195 if (add_aux_fields && opd_ent_size == 16)
8196 {
8197 memset (wptr, '\0', 8);
8198 wptr += 8;
8199 }
8200
50bc7936 8201 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8202 new opd entries. */
8203 for ( ; rel != next_rel; ++rel)
8204 {
8205 rel->r_offset += adjust;
8206 if (write_rel != rel)
8207 memcpy (write_rel, rel, sizeof (*rel));
8208 ++write_rel;
8209 }
1e2f5b6e 8210 }
51aecdc5
AM
8211
8212 rptr += opd_ent_size;
1e2f5b6e
AM
8213 }
8214
3f764659 8215 sec->size = wptr - new_contents;
1e2f5b6e 8216 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8217 if (add_aux_fields)
8218 {
8219 free (sec->contents);
8220 sec->contents = new_contents;
8221 }
8222
05bf9422 8223 /* Fudge the header size too, as this is used later in
cdcf6e38 8224 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8225 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8226 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8227 some_edited = TRUE;
1e2f5b6e 8228 }
6cdc0ccc 8229 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8230 free (relstart);
6cdc0ccc 8231
411e1bfb
AM
8232 if (local_syms != NULL
8233 && symtab_hdr->contents != (unsigned char *) local_syms)
8234 {
8235 if (!info->keep_memory)
8236 free (local_syms);
8237 else
8238 symtab_hdr->contents = (unsigned char *) local_syms;
8239 }
8240 }
8241
754021d0
AM
8242 if (some_edited)
8243 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8244
3f764659
JJ
8245 /* If we are doing a final link and the last .opd entry is just 16 byte
8246 long, add a 8 byte padding after it. */
0e1862bb 8247 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8248 {
8249 bfd_byte *p;
8250
8251 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8252 {
8253 BFD_ASSERT (need_pad->size > 0);
8254
8255 p = bfd_malloc (need_pad->size + 8);
8256 if (p == NULL)
8257 return FALSE;
699733f6 8258
3f764659
JJ
8259 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8260 p, 0, need_pad->size))
8261 return FALSE;
8262
8263 need_pad->contents = p;
8264 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8265 }
8266 else
8267 {
8268 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8269 if (p == NULL)
8270 return FALSE;
8271
8272 need_pad->contents = p;
8273 }
8274
8275 memset (need_pad->contents + need_pad->size, 0, 8);
8276 need_pad->size += 8;
8277 }
8278
411e1bfb
AM
8279 return TRUE;
8280}
8281
e1918d23 8282/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8283
e1918d23 8284asection *
e7d1c40c 8285ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8286{
411e1bfb
AM
8287 struct ppc_link_hash_table *htab;
8288
411e1bfb 8289 htab = ppc_hash_table (info);
4dfe6ac6
NC
8290 if (htab == NULL)
8291 return NULL;
8292
ee67d69a
AM
8293 if (abiversion (info->output_bfd) == 1)
8294 htab->opd_abi = 1;
8295
e7d1c40c 8296 if (htab->params->no_multi_toc)
33c0ec9d
AM
8297 htab->do_multi_toc = 0;
8298 else if (!htab->do_multi_toc)
e7d1c40c 8299 htab->params->no_multi_toc = 1;
33c0ec9d 8300
3a71aa26
AM
8301 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8302 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8303 FALSE, FALSE, TRUE));
a7f2871e
AM
8304 /* Move dynamic linking info to the function descriptor sym. */
8305 if (htab->tls_get_addr != NULL)
8306 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8307 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8308 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8309 FALSE, FALSE, TRUE));
7c9cf415 8310 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8311 {
8312 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8313
8314 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8315 FALSE, FALSE, TRUE);
8316 if (opt != NULL)
8317 func_desc_adjust (opt, info);
8318 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8319 FALSE, FALSE, TRUE);
8320 if (opt_fd != NULL
8321 && (opt_fd->root.type == bfd_link_hash_defined
8322 || opt_fd->root.type == bfd_link_hash_defweak))
8323 {
8324 /* If glibc supports an optimized __tls_get_addr call stub,
8325 signalled by the presence of __tls_get_addr_opt, and we'll
8326 be calling __tls_get_addr via a plt call stub, then
8327 make __tls_get_addr point to __tls_get_addr_opt. */
8328 tga_fd = &htab->tls_get_addr_fd->elf;
8329 if (htab->elf.dynamic_sections_created
8330 && tga_fd != NULL
8331 && (tga_fd->type == STT_FUNC
8332 || tga_fd->needs_plt)
8333 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8334 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8335 && tga_fd->root.type == bfd_link_hash_undefweak)))
8336 {
8337 struct plt_entry *ent;
8338
8339 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8340 if (ent->plt.refcount > 0)
8341 break;
8342 if (ent != NULL)
8343 {
8344 tga_fd->root.type = bfd_link_hash_indirect;
8345 tga_fd->root.u.i.link = &opt_fd->root;
8346 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
3bd43ebc 8347 opt_fd->forced_local = 0;
a7f2871e
AM
8348 if (opt_fd->dynindx != -1)
8349 {
8350 /* Use __tls_get_addr_opt in dynamic relocations. */
8351 opt_fd->dynindx = -1;
8352 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8353 opt_fd->dynstr_index);
8354 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8355 return NULL;
a7f2871e
AM
8356 }
8357 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8358 tga = &htab->tls_get_addr->elf;
8359 if (opt != NULL && tga != NULL)
8360 {
8361 tga->root.type = bfd_link_hash_indirect;
8362 tga->root.u.i.link = &opt->root;
8363 ppc64_elf_copy_indirect_symbol (info, opt, tga);
3bd43ebc 8364 opt->forced_local = 0;
a7f2871e
AM
8365 _bfd_elf_link_hash_hide_symbol (info, opt,
8366 tga->forced_local);
8367 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8368 }
8369 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8370 htab->tls_get_addr_fd->is_func_descriptor = 1;
8371 if (htab->tls_get_addr != NULL)
8372 {
8373 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8374 htab->tls_get_addr->is_func = 1;
8375 }
8376 }
8377 }
8378 }
7c9cf415
AM
8379 else if (htab->params->tls_get_addr_opt < 0)
8380 htab->params->tls_get_addr_opt = 0;
a7f2871e 8381 }
33c0ec9d 8382 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8383}
8387904d 8384
3a71aa26
AM
8385/* Return TRUE iff REL is a branch reloc with a global symbol matching
8386 HASH1 or HASH2. */
8387904d 8387
3a71aa26
AM
8388static bfd_boolean
8389branch_reloc_hash_match (const bfd *ibfd,
8390 const Elf_Internal_Rela *rel,
8391 const struct ppc_link_hash_entry *hash1,
8392 const struct ppc_link_hash_entry *hash2)
8393{
8394 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8395 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8396 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8397
e054468f 8398 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8399 {
3a71aa26
AM
8400 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8401 struct elf_link_hash_entry *h;
8387904d 8402
3a71aa26 8403 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8404 h = elf_follow_link (h);
3a71aa26
AM
8405 if (h == &hash1->elf || h == &hash2->elf)
8406 return TRUE;
a48ebf4d 8407 }
3a71aa26 8408 return FALSE;
951fd09b 8409}
411e1bfb 8410
951fd09b
AM
8411/* Run through all the TLS relocs looking for optimization
8412 opportunities. The linker has been hacked (see ppc64elf.em) to do
8413 a preliminary section layout so that we know the TLS segment
8414 offsets. We can't optimize earlier because some optimizations need
8415 to know the tp offset, and we need to optimize before allocating
8416 dynamic relocations. */
8417
8418bfd_boolean
33c0ec9d 8419ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8420{
8421 bfd *ibfd;
8422 asection *sec;
8423 struct ppc_link_hash_table *htab;
663a1470 8424 unsigned char *toc_ref;
102890f0 8425 int pass;
951fd09b 8426
3cbc1e5e 8427 if (!bfd_link_executable (info))
411e1bfb
AM
8428 return TRUE;
8429
951fd09b 8430 htab = ppc_hash_table (info);
4dfe6ac6
NC
8431 if (htab == NULL)
8432 return FALSE;
8433
663a1470
AM
8434 /* Make two passes over the relocs. On the first pass, mark toc
8435 entries involved with tls relocs, and check that tls relocs
8436 involved in setting up a tls_get_addr call are indeed followed by
8437 such a call. If they are not, we can't do any tls optimization.
8438 On the second pass twiddle tls_mask flags to notify
8439 relocate_section that optimization can be done, and adjust got
8440 and plt refcounts. */
8441 toc_ref = NULL;
8442 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8443 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8444 {
8445 Elf_Internal_Sym *locsyms = NULL;
8446 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8447
102890f0
AM
8448 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8449 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8450 {
8451 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8452 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8453
102890f0
AM
8454 /* Read the relocations. */
8455 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8456 info->keep_memory);
8457 if (relstart == NULL)
2915c55b
JK
8458 {
8459 free (toc_ref);
8460 return FALSE;
8461 }
411e1bfb 8462
102890f0
AM
8463 relend = relstart + sec->reloc_count;
8464 for (rel = relstart; rel < relend; rel++)
8465 {
8466 enum elf_ppc64_reloc_type r_type;
8467 unsigned long r_symndx;
8468 struct elf_link_hash_entry *h;
8469 Elf_Internal_Sym *sym;
8470 asection *sym_sec;
f961d9dd
AM
8471 unsigned char *tls_mask;
8472 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8473 bfd_vma value;
8474 bfd_boolean ok_tprel, is_local;
8475 long toc_ref_index = 0;
8476 int expecting_tls_get_addr = 0;
663a1470 8477 bfd_boolean ret = FALSE;
411e1bfb 8478
102890f0
AM
8479 r_symndx = ELF64_R_SYM (rel->r_info);
8480 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8481 r_symndx, ibfd))
8482 {
8483 err_free_rel:
8484 if (elf_section_data (sec)->relocs != relstart)
8485 free (relstart);
8486 if (toc_ref != NULL)
8487 free (toc_ref);
8488 if (locsyms != NULL
0ffa91dd 8489 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8490 != (unsigned char *) locsyms))
8491 free (locsyms);
663a1470 8492 return ret;
102890f0 8493 }
411e1bfb 8494
102890f0
AM
8495 if (h != NULL)
8496 {
766bc656
AM
8497 if (h->root.type == bfd_link_hash_defined
8498 || h->root.type == bfd_link_hash_defweak)
8499 value = h->root.u.def.value;
8500 else if (h->root.type == bfd_link_hash_undefweak)
8501 value = 0;
8502 else
663a1470
AM
8503 {
8504 found_tls_get_addr_arg = 0;
8505 continue;
8506 }
102890f0
AM
8507 }
8508 else
8509 /* Symbols referenced by TLS relocs must be of type
8510 STT_TLS. So no need for .opd local sym adjust. */
8511 value = sym->st_value;
8512
8513 ok_tprel = FALSE;
8514 is_local = FALSE;
8515 if (h == NULL
8516 || !h->def_dynamic)
8517 {
8518 is_local = TRUE;
766bc656
AM
8519 if (h != NULL
8520 && h->root.type == bfd_link_hash_undefweak)
8521 ok_tprel = TRUE;
c27b8c2a
AM
8522 else if (sym_sec != NULL
8523 && sym_sec->output_section != NULL)
766bc656
AM
8524 {
8525 value += sym_sec->output_offset;
8526 value += sym_sec->output_section->vma;
8527 value -= htab->elf.tls_sec->vma;
8528 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8529 < (bfd_vma) 1 << 32);
8530 }
102890f0 8531 }
951fd09b 8532
102890f0 8533 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8534 /* If this section has old-style __tls_get_addr calls
8535 without marker relocs, then check that each
8536 __tls_get_addr call reloc is preceded by a reloc
8537 that conceivably belongs to the __tls_get_addr arg
8538 setup insn. If we don't find matching arg setup
8539 relocs, don't do any tls optimization. */
8540 if (pass == 0
8541 && sec->has_tls_get_addr_call
8542 && h != NULL
8543 && (h == &htab->tls_get_addr->elf
8544 || h == &htab->tls_get_addr_fd->elf)
8545 && !found_tls_get_addr_arg
8546 && is_branch_reloc (r_type))
8547 {
25f53a85 8548 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8549 "TLS optimization disabled\n"),
8550 ibfd, sec, rel->r_offset);
8551 ret = TRUE;
8552 goto err_free_rel;
8553 }
8554
8555 found_tls_get_addr_arg = 0;
102890f0
AM
8556 switch (r_type)
8557 {
8558 case R_PPC64_GOT_TLSLD16:
8559 case R_PPC64_GOT_TLSLD16_LO:
8560 expecting_tls_get_addr = 1;
663a1470 8561 found_tls_get_addr_arg = 1;
1a0670f3 8562 /* Fall through. */
102890f0
AM
8563
8564 case R_PPC64_GOT_TLSLD16_HI:
8565 case R_PPC64_GOT_TLSLD16_HA:
8566 /* These relocs should never be against a symbol
8567 defined in a shared lib. Leave them alone if
8568 that turns out to be the case. */
8569 if (!is_local)
8570 continue;
411e1bfb 8571
102890f0 8572 /* LD -> LE */
411e1bfb 8573 tls_set = 0;
102890f0
AM
8574 tls_clear = TLS_LD;
8575 tls_type = TLS_TLS | TLS_LD;
8576 break;
411e1bfb 8577
102890f0
AM
8578 case R_PPC64_GOT_TLSGD16:
8579 case R_PPC64_GOT_TLSGD16_LO:
8580 expecting_tls_get_addr = 1;
663a1470 8581 found_tls_get_addr_arg = 1;
1a0670f3 8582 /* Fall through. */
102890f0
AM
8583
8584 case R_PPC64_GOT_TLSGD16_HI:
8585 case R_PPC64_GOT_TLSGD16_HA:
8586 if (ok_tprel)
8587 /* GD -> LE */
411e1bfb 8588 tls_set = 0;
102890f0
AM
8589 else
8590 /* GD -> IE */
8591 tls_set = TLS_TLS | TLS_TPRELGD;
8592 tls_clear = TLS_GD;
8593 tls_type = TLS_TLS | TLS_GD;
8594 break;
8595
8596 case R_PPC64_GOT_TPREL16_DS:
8597 case R_PPC64_GOT_TPREL16_LO_DS:
8598 case R_PPC64_GOT_TPREL16_HI:
8599 case R_PPC64_GOT_TPREL16_HA:
8600 if (ok_tprel)
8601 {
8602 /* IE -> LE */
8603 tls_set = 0;
8604 tls_clear = TLS_TPREL;
8605 tls_type = TLS_TLS | TLS_TPREL;
8606 break;
8607 }
411e1bfb
AM
8608 continue;
8609
727fc41e
AM
8610 case R_PPC64_TLSGD:
8611 case R_PPC64_TLSLD:
663a1470 8612 found_tls_get_addr_arg = 1;
1a0670f3 8613 /* Fall through. */
663a1470
AM
8614
8615 case R_PPC64_TLS:
8616 case R_PPC64_TOC16:
8617 case R_PPC64_TOC16_LO:
102890f0
AM
8618 if (sym_sec == NULL || sym_sec != toc)
8619 continue;
8620
8621 /* Mark this toc entry as referenced by a TLS
8622 code sequence. We can do that now in the
8623 case of R_PPC64_TLS, and after checking for
8624 tls_get_addr for the TOC16 relocs. */
8625 if (toc_ref == NULL)
663a1470
AM
8626 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8627 if (toc_ref == NULL)
8628 goto err_free_rel;
8629
102890f0
AM
8630 if (h != NULL)
8631 value = h->root.u.def.value;
8632 else
8633 value = sym->st_value;
8634 value += rel->r_addend;
73242275
AM
8635 if (value % 8 != 0)
8636 continue;
8637 BFD_ASSERT (value < toc->size
8638 && toc->output_offset % 8 == 0);
663a1470 8639 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8640 if (r_type == R_PPC64_TLS
8641 || r_type == R_PPC64_TLSGD
8642 || r_type == R_PPC64_TLSLD)
102890f0
AM
8643 {
8644 toc_ref[toc_ref_index] = 1;
8645 continue;
8646 }
8647
8648 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8649 continue;
8650
8651 tls_set = 0;
8652 tls_clear = 0;
8653 expecting_tls_get_addr = 2;
8654 break;
8655
8656 case R_PPC64_TPREL64:
8657 if (pass == 0
8658 || sec != toc
8659 || toc_ref == NULL
663a1470 8660 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8661 continue;
8662 if (ok_tprel)
8663 {
8664 /* IE -> LE */
8665 tls_set = TLS_EXPLICIT;
8666 tls_clear = TLS_TPREL;
8667 break;
8668 }
8669 continue;
8670
8671 case R_PPC64_DTPMOD64:
8672 if (pass == 0
8673 || sec != toc
8674 || toc_ref == NULL
663a1470 8675 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8676 continue;
8677 if (rel + 1 < relend
8678 && (rel[1].r_info
8679 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8680 && rel[1].r_offset == rel->r_offset + 8)
8681 {
8682 if (ok_tprel)
8683 /* GD -> LE */
8684 tls_set = TLS_EXPLICIT | TLS_GD;
8685 else
8686 /* GD -> IE */
8687 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8688 tls_clear = TLS_GD;
8689 }
8690 else
8691 {
8692 if (!is_local)
8693 continue;
8694
8695 /* LD -> LE */
8696 tls_set = TLS_EXPLICIT;
8697 tls_clear = TLS_LD;
8698 }
8699 break;
8700
8701 default:
8702 continue;
8703 }
8704
8705 if (pass == 0)
8706 {
727fc41e
AM
8707 if (!expecting_tls_get_addr
8708 || !sec->has_tls_get_addr_call)
102890f0
AM
8709 continue;
8710
3a71aa26
AM
8711 if (rel + 1 < relend
8712 && branch_reloc_hash_match (ibfd, rel + 1,
8713 htab->tls_get_addr,
8714 htab->tls_get_addr_fd))
102890f0 8715 {
3a71aa26 8716 if (expecting_tls_get_addr == 2)
102890f0 8717 {
3a71aa26 8718 /* Check for toc tls entries. */
f961d9dd 8719 unsigned char *toc_tls;
3a71aa26
AM
8720 int retval;
8721
8722 retval = get_tls_mask (&toc_tls, NULL, NULL,
8723 &locsyms,
8724 rel, ibfd);
8725 if (retval == 0)
8726 goto err_free_rel;
663a1470
AM
8727 if (toc_tls != NULL)
8728 {
8729 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8730 found_tls_get_addr_arg = 1;
8731 if (retval > 1)
8732 toc_ref[toc_ref_index] = 1;
8733 }
102890f0 8734 }
3a71aa26 8735 continue;
102890f0
AM
8736 }
8737
8738 if (expecting_tls_get_addr != 1)
8739 continue;
8740
8741 /* Uh oh, we didn't find the expected call. We
8742 could just mark this symbol to exclude it
8743 from tls optimization but it's safer to skip
663a1470 8744 the entire optimization. */
695344c0 8745 /* xgettext:c-format */
25f53a85 8746 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8747 "TLS optimization disabled\n"),
8748 ibfd, sec, rel->r_offset);
8749 ret = TRUE;
8750 goto err_free_rel;
102890f0
AM
8751 }
8752
85f7a9cb 8753 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8754 {
8755 struct plt_entry *ent;
8756 for (ent = htab->tls_get_addr->elf.plt.plist;
8757 ent != NULL;
8758 ent = ent->next)
8759 if (ent->addend == 0)
411e1bfb 8760 {
102890f0 8761 if (ent->plt.refcount > 0)
30038c59 8762 {
102890f0
AM
8763 ent->plt.refcount -= 1;
8764 expecting_tls_get_addr = 0;
30038c59 8765 }
102890f0 8766 break;
411e1bfb 8767 }
102890f0 8768 }
411e1bfb 8769
85f7a9cb 8770 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8771 {
8772 struct plt_entry *ent;
8773 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8774 ent != NULL;
8775 ent = ent->next)
8776 if (ent->addend == 0)
411e1bfb 8777 {
102890f0
AM
8778 if (ent->plt.refcount > 0)
8779 ent->plt.refcount -= 1;
8780 break;
411e1bfb 8781 }
102890f0 8782 }
411e1bfb 8783
102890f0 8784 if (tls_clear == 0)
30038c59
AM
8785 continue;
8786
102890f0
AM
8787 if ((tls_set & TLS_EXPLICIT) == 0)
8788 {
8789 struct got_entry *ent;
411e1bfb 8790
102890f0
AM
8791 /* Adjust got entry for this reloc. */
8792 if (h != NULL)
8793 ent = h->got.glist;
8794 else
8795 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8796
102890f0
AM
8797 for (; ent != NULL; ent = ent->next)
8798 if (ent->addend == rel->r_addend
8799 && ent->owner == ibfd
8800 && ent->tls_type == tls_type)
8801 break;
8802 if (ent == NULL)
8803 abort ();
411e1bfb 8804
102890f0
AM
8805 if (tls_set == 0)
8806 {
8807 /* We managed to get rid of a got entry. */
8808 if (ent->got.refcount > 0)
8809 ent->got.refcount -= 1;
8810 }
8811 }
8812 else
8813 {
8814 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8815 we'll lose one or two dyn relocs. */
8816 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8817 NULL, h, sym))
102890f0 8818 return FALSE;
411e1bfb 8819
102890f0
AM
8820 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8821 {
8822 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8823 NULL, h, sym))
102890f0
AM
8824 return FALSE;
8825 }
8826 }
411e1bfb 8827
102890f0
AM
8828 *tls_mask |= tls_set;
8829 *tls_mask &= ~tls_clear;
8830 }
8c1d1bb8 8831
102890f0
AM
8832 if (elf_section_data (sec)->relocs != relstart)
8833 free (relstart);
8834 }
411e1bfb 8835
663a1470
AM
8836 if (locsyms != NULL
8837 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8838 {
8839 if (!info->keep_memory)
8840 free (locsyms);
8841 else
8842 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8843 }
8844 }
411e1bfb 8845
663a1470
AM
8846 if (toc_ref != NULL)
8847 free (toc_ref);
b34976b6 8848 return TRUE;
1e2f5b6e 8849}
b34976b6 8850
c5614fa4
AM
8851/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8852 the values of any global symbols in a toc section that has been
8853 edited. Globals in toc sections should be a rarity, so this function
8854 sets a flag if any are found in toc sections other than the one just
8855 edited, so that futher hash table traversals can be avoided. */
8856
8857struct adjust_toc_info
8858{
8859 asection *toc;
8860 unsigned long *skip;
8861 bfd_boolean global_toc_syms;
8862};
8863
ba761f19
AM
8864enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8865
c5614fa4
AM
8866static bfd_boolean
8867adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8868{
8869 struct ppc_link_hash_entry *eh;
8870 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8871 unsigned long i;
c5614fa4 8872
c5614fa4
AM
8873 if (h->root.type != bfd_link_hash_defined
8874 && h->root.type != bfd_link_hash_defweak)
8875 return TRUE;
8876
8877 eh = (struct ppc_link_hash_entry *) h;
8878 if (eh->adjust_done)
8879 return TRUE;
8880
8881 if (eh->elf.root.u.def.section == toc_inf->toc)
8882 {
854b41e7
AM
8883 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8884 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8885 else
854b41e7
AM
8886 i = eh->elf.root.u.def.value >> 3;
8887
ba761f19 8888 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8889 {
4eca0228 8890 _bfd_error_handler
854b41e7
AM
8891 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8892 do
8893 ++i;
ba761f19 8894 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8895 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8896 }
854b41e7
AM
8897
8898 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8899 eh->adjust_done = 1;
8900 }
8901 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8902 toc_inf->global_toc_syms = TRUE;
8903
8904 return TRUE;
8905}
8906
39eeab25
AM
8907/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8908 on a _LO variety toc/got reloc. */
560c8763
AM
8909
8910static bfd_boolean
39eeab25 8911ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8912{
39eeab25
AM
8913 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8914 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8915 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8916 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8917 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8918 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8919 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8920 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8921 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8922 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8923 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8924 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8925 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8926 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8927 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8928 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8929 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8930 /* Exclude lfqu by testing reloc. If relocs are ever
8931 defined for the reduced D field in psq_lu then those
8932 will need testing too. */
8933 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8934 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8935 && (insn & 1) == 0)
8936 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8937 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8938 /* Exclude stfqu. psq_stu as above for psq_lu. */
8939 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8940 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8941 && (insn & 1) == 0));
560c8763
AM
8942}
8943
c5614fa4
AM
8944/* Examine all relocs referencing .toc sections in order to remove
8945 unused .toc entries. */
8946
8947bfd_boolean
33c0ec9d 8948ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8949{
8950 bfd *ibfd;
8951 struct adjust_toc_info toc_inf;
67f0cbdb 8952 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8953
67f0cbdb 8954 htab->do_toc_opt = 1;
c5614fa4 8955 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8956 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8957 {
8958 asection *toc, *sec;
8959 Elf_Internal_Shdr *symtab_hdr;
8960 Elf_Internal_Sym *local_syms;
425b145b 8961 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8962 unsigned long *skip, *drop;
8963 unsigned char *used;
8964 unsigned char *keep, last, some_unused;
8965
854b41e7
AM
8966 if (!is_ppc64_elf (ibfd))
8967 continue;
8968
c5614fa4
AM
8969 toc = bfd_get_section_by_name (ibfd, ".toc");
8970 if (toc == NULL
92b7a70f 8971 || toc->size == 0
dbaa2011
AM
8972 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8973 || discarded_section (toc))
c5614fa4
AM
8974 continue;
8975
425b145b 8976 toc_relocs = NULL;
c5614fa4 8977 local_syms = NULL;
0ffa91dd 8978 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8979
8980 /* Look at sections dropped from the final link. */
8981 skip = NULL;
8982 relstart = NULL;
8983 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8984 {
8985 if (sec->reloc_count == 0
dbaa2011 8986 || !discarded_section (sec)
c5614fa4
AM
8987 || get_opd_info (sec)
8988 || (sec->flags & SEC_ALLOC) == 0
8989 || (sec->flags & SEC_DEBUGGING) != 0)
8990 continue;
8991
8992 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8993 if (relstart == NULL)
8994 goto error_ret;
8995
8996 /* Run through the relocs to see which toc entries might be
8997 unused. */
8998 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8999 {
9000 enum elf_ppc64_reloc_type r_type;
9001 unsigned long r_symndx;
9002 asection *sym_sec;
9003 struct elf_link_hash_entry *h;
9004 Elf_Internal_Sym *sym;
9005 bfd_vma val;
9006
9007 r_type = ELF64_R_TYPE (rel->r_info);
9008 switch (r_type)
9009 {
9010 default:
9011 continue;
9012
9013 case R_PPC64_TOC16:
9014 case R_PPC64_TOC16_LO:
9015 case R_PPC64_TOC16_HI:
9016 case R_PPC64_TOC16_HA:
9017 case R_PPC64_TOC16_DS:
9018 case R_PPC64_TOC16_LO_DS:
9019 break;
9020 }
9021
9022 r_symndx = ELF64_R_SYM (rel->r_info);
9023 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9024 r_symndx, ibfd))
9025 goto error_ret;
9026
9027 if (sym_sec != toc)
9028 continue;
9029
9030 if (h != NULL)
9031 val = h->root.u.def.value;
9032 else
9033 val = sym->st_value;
9034 val += rel->r_addend;
9035
9036 if (val >= toc->size)
9037 continue;
9038
9039 /* Anything in the toc ought to be aligned to 8 bytes.
9040 If not, don't mark as unused. */
9041 if (val & 7)
9042 continue;
9043
9044 if (skip == NULL)
9045 {
854b41e7 9046 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9047 if (skip == NULL)
9048 goto error_ret;
9049 }
9050
ba761f19 9051 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9052 }
9053
9054 if (elf_section_data (sec)->relocs != relstart)
9055 free (relstart);
9056 }
9057
ba761f19
AM
9058 /* For largetoc loads of address constants, we can convert
9059 . addis rx,2,addr@got@ha
9060 . ld ry,addr@got@l(rx)
9061 to
9062 . addis rx,2,addr@toc@ha
9063 . addi ry,rx,addr@toc@l
9064 when addr is within 2G of the toc pointer. This then means
9065 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9066
ba761f19
AM
9067 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9068 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9069 && toc->reloc_count != 0)
9070 {
9071 /* Read toc relocs. */
425b145b
AM
9072 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9073 info->keep_memory);
9074 if (toc_relocs == NULL)
ba761f19
AM
9075 goto error_ret;
9076
425b145b 9077 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9078 {
9079 enum elf_ppc64_reloc_type r_type;
9080 unsigned long r_symndx;
9081 asection *sym_sec;
9082 struct elf_link_hash_entry *h;
9083 Elf_Internal_Sym *sym;
9084 bfd_vma val, addr;
9085
9086 r_type = ELF64_R_TYPE (rel->r_info);
9087 if (r_type != R_PPC64_ADDR64)
9088 continue;
9089
9090 r_symndx = ELF64_R_SYM (rel->r_info);
9091 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9092 r_symndx, ibfd))
9093 goto error_ret;
9094
425b145b 9095 if (sym_sec == NULL
c27b8c2a 9096 || sym_sec->output_section == NULL
dbaa2011 9097 || discarded_section (sym_sec))
425b145b
AM
9098 continue;
9099
afe397ea 9100 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9101 continue;
9102
9103 if (h != NULL)
bddc25c9
AM
9104 {
9105 if (h->type == STT_GNU_IFUNC)
9106 continue;
9107 val = h->root.u.def.value;
9108 }
ba761f19 9109 else
bddc25c9
AM
9110 {
9111 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9112 continue;
9113 val = sym->st_value;
9114 }
ba761f19
AM
9115 val += rel->r_addend;
9116 val += sym_sec->output_section->vma + sym_sec->output_offset;
9117
9118 /* We don't yet know the exact toc pointer value, but we
9119 know it will be somewhere in the toc section. Don't
9120 optimize if the difference from any possible toc
9121 pointer is outside [ff..f80008000, 7fff7fff]. */
9122 addr = toc->output_section->vma + TOC_BASE_OFF;
9123 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9124 continue;
9125
9126 addr = toc->output_section->vma + toc->output_section->rawsize;
9127 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9128 continue;
9129
9130 if (skip == NULL)
9131 {
9132 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9133 if (skip == NULL)
9134 goto error_ret;
9135 }
9136
9137 skip[rel->r_offset >> 3]
425b145b 9138 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9139 }
ba761f19
AM
9140 }
9141
c5614fa4
AM
9142 if (skip == NULL)
9143 continue;
9144
9145 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9146 if (used == NULL)
9147 {
9148 error_ret:
9149 if (local_syms != NULL
9150 && symtab_hdr->contents != (unsigned char *) local_syms)
9151 free (local_syms);
9152 if (sec != NULL
9153 && relstart != NULL
9154 && elf_section_data (sec)->relocs != relstart)
9155 free (relstart);
425b145b
AM
9156 if (toc_relocs != NULL
9157 && elf_section_data (toc)->relocs != toc_relocs)
9158 free (toc_relocs);
c5614fa4
AM
9159 if (skip != NULL)
9160 free (skip);
9161 return FALSE;
9162 }
9163
30038c59
AM
9164 /* Now check all kept sections that might reference the toc.
9165 Check the toc itself last. */
9166 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9167 : ibfd->sections);
c5614fa4 9168 sec != NULL;
c5614fa4 9169 sec = (sec == toc ? NULL
c5614fa4 9170 : sec->next == NULL ? toc
30038c59 9171 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9172 : sec->next))
9173 {
9174 int repeat;
9175
9176 if (sec->reloc_count == 0
dbaa2011 9177 || discarded_section (sec)
c5614fa4
AM
9178 || get_opd_info (sec)
9179 || (sec->flags & SEC_ALLOC) == 0
9180 || (sec->flags & SEC_DEBUGGING) != 0)
9181 continue;
9182
854b41e7
AM
9183 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9184 info->keep_memory);
c5614fa4 9185 if (relstart == NULL)
2915c55b
JK
9186 {
9187 free (used);
9188 goto error_ret;
9189 }
c5614fa4
AM
9190
9191 /* Mark toc entries referenced as used. */
c5614fa4 9192 do
d4f1ee75
AM
9193 {
9194 repeat = 0;
9195 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9196 {
9197 enum elf_ppc64_reloc_type r_type;
9198 unsigned long r_symndx;
9199 asection *sym_sec;
9200 struct elf_link_hash_entry *h;
9201 Elf_Internal_Sym *sym;
9202 bfd_vma val;
9203 enum {no_check, check_lo, check_ha} insn_check;
98528052 9204
d4f1ee75
AM
9205 r_type = ELF64_R_TYPE (rel->r_info);
9206 switch (r_type)
9207 {
9208 default:
9209 insn_check = no_check;
9210 break;
98528052 9211
d4f1ee75
AM
9212 case R_PPC64_GOT_TLSLD16_HA:
9213 case R_PPC64_GOT_TLSGD16_HA:
9214 case R_PPC64_GOT_TPREL16_HA:
9215 case R_PPC64_GOT_DTPREL16_HA:
9216 case R_PPC64_GOT16_HA:
9217 case R_PPC64_TOC16_HA:
9218 insn_check = check_ha;
9219 break;
98528052 9220
d4f1ee75
AM
9221 case R_PPC64_GOT_TLSLD16_LO:
9222 case R_PPC64_GOT_TLSGD16_LO:
9223 case R_PPC64_GOT_TPREL16_LO_DS:
9224 case R_PPC64_GOT_DTPREL16_LO_DS:
9225 case R_PPC64_GOT16_LO:
9226 case R_PPC64_GOT16_LO_DS:
9227 case R_PPC64_TOC16_LO:
9228 case R_PPC64_TOC16_LO_DS:
9229 insn_check = check_lo;
9230 break;
9231 }
560c8763 9232
d4f1ee75
AM
9233 if (insn_check != no_check)
9234 {
9235 bfd_vma off = rel->r_offset & ~3;
9236 unsigned char buf[4];
9237 unsigned int insn;
c5614fa4 9238
d4f1ee75
AM
9239 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9240 {
9241 free (used);
9242 goto error_ret;
9243 }
9244 insn = bfd_get_32 (ibfd, buf);
9245 if (insn_check == check_lo
39eeab25 9246 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
9247 : ((insn & ((0x3f << 26) | 0x1f << 16))
9248 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9249 {
9250 char str[12];
9251
9252 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9253 sprintf (str, "%#08x", insn);
9254 info->callbacks->einfo
695344c0 9255 /* xgettext:c-format */
d4f1ee75
AM
9256 (_("%P: %H: toc optimization is not supported for"
9257 " %s instruction.\n"),
9258 ibfd, sec, rel->r_offset & ~3, str);
9259 }
9260 }
c5614fa4 9261
d4f1ee75
AM
9262 switch (r_type)
9263 {
9264 case R_PPC64_TOC16:
9265 case R_PPC64_TOC16_LO:
9266 case R_PPC64_TOC16_HI:
9267 case R_PPC64_TOC16_HA:
9268 case R_PPC64_TOC16_DS:
9269 case R_PPC64_TOC16_LO_DS:
9270 /* In case we're taking addresses of toc entries. */
9271 case R_PPC64_ADDR64:
9272 break;
c5614fa4 9273
d4f1ee75
AM
9274 default:
9275 continue;
9276 }
c5614fa4 9277
d4f1ee75
AM
9278 r_symndx = ELF64_R_SYM (rel->r_info);
9279 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9280 r_symndx, ibfd))
9281 {
9282 free (used);
9283 goto error_ret;
9284 }
c5614fa4 9285
d4f1ee75
AM
9286 if (sym_sec != toc)
9287 continue;
c5614fa4 9288
d4f1ee75
AM
9289 if (h != NULL)
9290 val = h->root.u.def.value;
9291 else
9292 val = sym->st_value;
9293 val += rel->r_addend;
ba761f19 9294
d4f1ee75
AM
9295 if (val >= toc->size)
9296 continue;
ba761f19 9297
d4f1ee75
AM
9298 if ((skip[val >> 3] & can_optimize) != 0)
9299 {
9300 bfd_vma off;
9301 unsigned char opc;
9302
9303 switch (r_type)
9304 {
9305 case R_PPC64_TOC16_HA:
ba761f19 9306 break;
ba761f19 9307
d4f1ee75
AM
9308 case R_PPC64_TOC16_LO_DS:
9309 off = rel->r_offset;
9310 off += (bfd_big_endian (ibfd) ? -2 : 3);
9311 if (!bfd_get_section_contents (ibfd, sec, &opc,
9312 off, 1))
9313 {
9314 free (used);
9315 goto error_ret;
9316 }
9317 if ((opc & (0x3f << 2)) == (58u << 2))
9318 break;
1a0670f3 9319 /* Fall through. */
ba761f19 9320
d4f1ee75
AM
9321 default:
9322 /* Wrong sort of reloc, or not a ld. We may
9323 as well clear ref_from_discarded too. */
9324 skip[val >> 3] = 0;
9325 }
9326 }
9327
9328 if (sec != toc)
9329 used[val >> 3] = 1;
9330 /* For the toc section, we only mark as used if this
9331 entry itself isn't unused. */
9332 else if ((used[rel->r_offset >> 3]
9333 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9334 && !used[val >> 3])
9335 {
9336 /* Do all the relocs again, to catch reference
9337 chains. */
9338 repeat = 1;
9339 used[val >> 3] = 1;
9340 }
9341 }
9342 }
c5614fa4 9343 while (repeat);
854b41e7
AM
9344
9345 if (elf_section_data (sec)->relocs != relstart)
9346 free (relstart);
c5614fa4
AM
9347 }
9348
9349 /* Merge the used and skip arrays. Assume that TOC
9350 doublewords not appearing as either used or unused belong
9351 to to an entry more than one doubleword in size. */
9352 for (drop = skip, keep = used, last = 0, some_unused = 0;
9353 drop < skip + (toc->size + 7) / 8;
9354 ++drop, ++keep)
9355 {
9356 if (*keep)
9357 {
ba761f19
AM
9358 *drop &= ~ref_from_discarded;
9359 if ((*drop & can_optimize) != 0)
9360 some_unused = 1;
c5614fa4
AM
9361 last = 0;
9362 }
b140b010 9363 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9364 {
9365 some_unused = 1;
ba761f19 9366 last = ref_from_discarded;
c5614fa4
AM
9367 }
9368 else
9369 *drop = last;
9370 }
9371
9372 free (used);
9373
9374 if (some_unused)
9375 {
9376 bfd_byte *contents, *src;
9377 unsigned long off;
d62b3684 9378 Elf_Internal_Sym *sym;
ba761f19 9379 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9380
9381 /* Shuffle the toc contents, and at the same time convert the
9382 skip array from booleans into offsets. */
9383 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9384 goto error_ret;
9385
9386 elf_section_data (toc)->this_hdr.contents = contents;
9387
9388 for (src = contents, off = 0, drop = skip;
9389 src < contents + toc->size;
9390 src += 8, ++drop)
9391 {
ba761f19
AM
9392 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9393 off += 8;
c5614fa4
AM
9394 else if (off != 0)
9395 {
9396 *drop = off;
9397 memcpy (src - off, src, 8);
9398 }
9399 }
854b41e7 9400 *drop = off;
c5614fa4
AM
9401 toc->rawsize = toc->size;
9402 toc->size = src - contents - off;
9403
ba761f19
AM
9404 /* Adjust addends for relocs against the toc section sym,
9405 and optimize any accesses we can. */
c5614fa4
AM
9406 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9407 {
9408 if (sec->reloc_count == 0
dbaa2011 9409 || discarded_section (sec))
c5614fa4
AM
9410 continue;
9411
9412 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9413 info->keep_memory);
c5614fa4
AM
9414 if (relstart == NULL)
9415 goto error_ret;
9416
9417 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9418 {
9419 enum elf_ppc64_reloc_type r_type;
9420 unsigned long r_symndx;
9421 asection *sym_sec;
9422 struct elf_link_hash_entry *h;
854b41e7 9423 bfd_vma val;
c5614fa4
AM
9424
9425 r_type = ELF64_R_TYPE (rel->r_info);
9426 switch (r_type)
9427 {
9428 default:
9429 continue;
9430
9431 case R_PPC64_TOC16:
9432 case R_PPC64_TOC16_LO:
9433 case R_PPC64_TOC16_HI:
9434 case R_PPC64_TOC16_HA:
9435 case R_PPC64_TOC16_DS:
9436 case R_PPC64_TOC16_LO_DS:
9437 case R_PPC64_ADDR64:
9438 break;
9439 }
9440
9441 r_symndx = ELF64_R_SYM (rel->r_info);
9442 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9443 r_symndx, ibfd))
9444 goto error_ret;
9445
ba761f19 9446 if (sym_sec != toc)
c5614fa4
AM
9447 continue;
9448
ba761f19
AM
9449 if (h != NULL)
9450 val = h->root.u.def.value;
9451 else
9452 {
9453 val = sym->st_value;
9454 if (val != 0)
9455 local_toc_syms = TRUE;
9456 }
9457
9458 val += rel->r_addend;
854b41e7
AM
9459
9460 if (val > toc->rawsize)
9461 val = toc->rawsize;
ba761f19
AM
9462 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9463 continue;
9464 else if ((skip[val >> 3] & can_optimize) != 0)
9465 {
9466 Elf_Internal_Rela *tocrel
425b145b 9467 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9468 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9469
9470 switch (r_type)
9471 {
9472 case R_PPC64_TOC16_HA:
9473 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9474 break;
9475
9476 case R_PPC64_TOC16_LO_DS:
9477 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9478 break;
9479
9480 default:
28942f62
AM
9481 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9482 ppc_howto_init ();
b140b010 9483 info->callbacks->einfo
695344c0 9484 /* xgettext:c-format */
bc30df16 9485 (_("%P: %H: %s references "
b140b010
AM
9486 "optimized away TOC entry\n"),
9487 ibfd, sec, rel->r_offset,
9488 ppc64_elf_howto_table[r_type]->name);
9489 bfd_set_error (bfd_error_bad_value);
9490 goto error_ret;
ba761f19
AM
9491 }
9492 rel->r_addend = tocrel->r_addend;
9493 elf_section_data (sec)->relocs = relstart;
9494 continue;
9495 }
9496
9497 if (h != NULL || sym->st_value != 0)
9498 continue;
854b41e7
AM
9499
9500 rel->r_addend -= skip[val >> 3];
9501 elf_section_data (sec)->relocs = relstart;
c5614fa4 9502 }
854b41e7
AM
9503
9504 if (elf_section_data (sec)->relocs != relstart)
9505 free (relstart);
c5614fa4
AM
9506 }
9507
9508 /* We shouldn't have local or global symbols defined in the TOC,
9509 but handle them anyway. */
df22d223
AM
9510 if (local_syms != NULL)
9511 for (sym = local_syms;
9512 sym < local_syms + symtab_hdr->sh_info;
9513 ++sym)
9514 if (sym->st_value != 0
9515 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9516 {
9517 unsigned long i;
854b41e7 9518
df22d223
AM
9519 if (sym->st_value > toc->rawsize)
9520 i = toc->rawsize >> 3;
9521 else
9522 i = sym->st_value >> 3;
854b41e7 9523
df22d223
AM
9524 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9525 {
9526 if (local_toc_syms)
4eca0228 9527 _bfd_error_handler
df22d223
AM
9528 (_("%s defined on removed toc entry"),
9529 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9530 do
9531 ++i;
9532 while ((skip[i] & (ref_from_discarded | can_optimize)));
9533 sym->st_value = (bfd_vma) i << 3;
9534 }
d62b3684 9535
df22d223
AM
9536 sym->st_value -= skip[i];
9537 symtab_hdr->contents = (unsigned char *) local_syms;
9538 }
c5614fa4 9539
854b41e7 9540 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9541 if (toc_inf.global_toc_syms)
9542 {
9543 toc_inf.toc = toc;
9544 toc_inf.skip = skip;
9545 toc_inf.global_toc_syms = FALSE;
9546 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9547 &toc_inf);
9548 }
854b41e7
AM
9549
9550 if (toc->reloc_count != 0)
9551 {
d4730f92 9552 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9553 Elf_Internal_Rela *wrel;
9554 bfd_size_type sz;
9555
854b41e7 9556 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9557 if (toc_relocs == NULL)
9558 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9559 info->keep_memory);
9560 if (toc_relocs == NULL)
9561 goto error_ret;
9562
425b145b
AM
9563 wrel = toc_relocs;
9564 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9565 if ((skip[rel->r_offset >> 3]
9566 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9567 {
9568 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9569 wrel->r_info = rel->r_info;
9570 wrel->r_addend = rel->r_addend;
9571 ++wrel;
9572 }
9573 else if (!dec_dynrel_count (rel->r_info, toc, info,
9574 &local_syms, NULL, NULL))
9575 goto error_ret;
9576
425b145b
AM
9577 elf_section_data (toc)->relocs = toc_relocs;
9578 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9579 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9580 sz = rel_hdr->sh_entsize;
9581 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9582 }
c5614fa4 9583 }
28be611c
AM
9584 else if (toc_relocs != NULL
9585 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9586 free (toc_relocs);
c5614fa4
AM
9587
9588 if (local_syms != NULL
9589 && symtab_hdr->contents != (unsigned char *) local_syms)
9590 {
9591 if (!info->keep_memory)
9592 free (local_syms);
9593 else
9594 symtab_hdr->contents = (unsigned char *) local_syms;
9595 }
9596 free (skip);
9597 }
9598
9599 return TRUE;
9600}
9601
1bbe0902
AM
9602/* Return true iff input section I references the TOC using
9603 instructions limited to +/-32k offsets. */
9604
9605bfd_boolean
9606ppc64_elf_has_small_toc_reloc (asection *i)
9607{
9608 return (is_ppc64_elf (i->owner)
9609 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9610}
9611
927be08e
AM
9612/* Allocate space for one GOT entry. */
9613
9614static void
9615allocate_got (struct elf_link_hash_entry *h,
9616 struct bfd_link_info *info,
9617 struct got_entry *gent)
9618{
9619 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9620 bfd_boolean dyn;
9621 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9622 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9623 ? 16 : 8);
9624 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9625 ? 2 : 1) * sizeof (Elf64_External_Rela);
9626 asection *got = ppc64_elf_tdata (gent->owner)->got;
9627
9628 gent->got.offset = got->size;
9629 got->size += entsize;
9630
9631 dyn = htab->elf.dynamic_sections_created;
19e08130 9632 if (h->type == STT_GNU_IFUNC)
927be08e 9633 {
33e44f2e 9634 htab->elf.irelplt->size += rentsize;
19e08130 9635 htab->got_reli_size += rentsize;
927be08e 9636 }
0e1862bb 9637 else if ((bfd_link_pic (info)
19e08130
AM
9638 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9639 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9640 || h->root.type != bfd_link_hash_undefweak))
927be08e 9641 {
19e08130 9642 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9643 relgot->size += rentsize;
927be08e
AM
9644 }
9645}
9646
7865406b
AM
9647/* This function merges got entries in the same toc group. */
9648
9649static void
9650merge_got_entries (struct got_entry **pent)
9651{
9652 struct got_entry *ent, *ent2;
9653
9654 for (ent = *pent; ent != NULL; ent = ent->next)
9655 if (!ent->is_indirect)
9656 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9657 if (!ent2->is_indirect
9658 && ent2->addend == ent->addend
9659 && ent2->tls_type == ent->tls_type
9660 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9661 {
9662 ent2->is_indirect = TRUE;
9663 ent2->got.ent = ent;
9664 }
9665}
9666
65f38f15
AM
9667/* Allocate space in .plt, .got and associated reloc sections for
9668 dynamic relocs. */
5bd4f169 9669
b34976b6 9670static bfd_boolean
4ce794b7 9671allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9672{
65f38f15
AM
9673 struct bfd_link_info *info;
9674 struct ppc_link_hash_table *htab;
5bd4f169 9675 asection *s;
65f38f15 9676 struct ppc_link_hash_entry *eh;
0b8bcf0d 9677 struct got_entry **pgent, *gent;
5bd4f169 9678
e92d460e 9679 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9680 return TRUE;
5bd4f169 9681
65f38f15
AM
9682 info = (struct bfd_link_info *) inf;
9683 htab = ppc_hash_table (info);
4dfe6ac6
NC
9684 if (htab == NULL)
9685 return FALSE;
5bd4f169 9686
951fd09b
AM
9687 eh = (struct ppc_link_hash_entry *) h;
9688 /* Run through the TLS GD got entries first if we're changing them
9689 to TPREL. */
e7b938ca 9690 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9691 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9692 if (gent->got.refcount > 0
9693 && (gent->tls_type & TLS_GD) != 0)
9694 {
9695 /* This was a GD entry that has been converted to TPREL. If
9696 there happens to be a TPREL entry we can use that one. */
9697 struct got_entry *ent;
9698 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9699 if (ent->got.refcount > 0
9700 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9701 && ent->addend == gent->addend
9702 && ent->owner == gent->owner)
951fd09b
AM
9703 {
9704 gent->got.refcount = 0;
9705 break;
9706 }
9707
9708 /* If not, then we'll be using our own TPREL entry. */
9709 if (gent->got.refcount != 0)
9710 gent->tls_type = TLS_TLS | TLS_TPREL;
9711 }
9712
7865406b
AM
9713 /* Remove any list entry that won't generate a word in the GOT before
9714 we call merge_got_entries. Otherwise we risk merging to empty
9715 entries. */
0b8bcf0d
AM
9716 pgent = &h->got.glist;
9717 while ((gent = *pgent) != NULL)
411e1bfb 9718 if (gent->got.refcount > 0)
7865406b
AM
9719 {
9720 if ((gent->tls_type & TLS_LD) != 0
9721 && !h->def_dynamic)
9722 {
9723 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9724 *pgent = gent->next;
9725 }
9726 else
9727 pgent = &gent->next;
9728 }
9729 else
9730 *pgent = gent->next;
9731
9732 if (!htab->do_multi_toc)
9733 merge_got_entries (&h->got.glist);
9734
9735 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9736 if (!gent->is_indirect)
411e1bfb
AM
9737 {
9738 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
9739 Undefined weak syms won't yet be marked as dynamic,
9740 nor will all TLS symbols. */
411e1bfb 9741 if (h->dynindx == -1
b099ab9f 9742 && !h->forced_local
25f23106 9743 && h->type != STT_GNU_IFUNC
b099ab9f 9744 && htab->elf.dynamic_sections_created)
411e1bfb 9745 {
c152c796 9746 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
9747 return FALSE;
9748 }
65f38f15 9749
0c8d6e5c 9750 if (!is_ppc64_elf (gent->owner))
927be08e 9751 abort ();
0ffa91dd 9752
927be08e 9753 allocate_got (h, info, gent);
411e1bfb 9754 }
65f38f15 9755
8a2058b5
AM
9756 if (!htab->elf.dynamic_sections_created
9757 && h->type != STT_GNU_IFUNC)
9758 eh->dyn_relocs = NULL;
9759
9760 if (eh->dyn_relocs != NULL)
65f38f15 9761 {
8a2058b5
AM
9762 struct elf_dyn_relocs *p, **pp;
9763
57e7d118
AM
9764 /* In the shared -Bsymbolic case, discard space allocated for
9765 dynamic pc-relative relocs against symbols which turn out to
9766 be defined in regular objects. For the normal shared case,
9767 discard space for relocs that have become local due to symbol
9768 visibility changes. */
9769
9770 if (bfd_link_pic (info))
65f38f15 9771 {
57e7d118
AM
9772 /* Relocs that use pc_count are those that appear on a call
9773 insn, or certain REL relocs (see must_be_dyn_reloc) that
9774 can be generated via assembly. We want calls to
9775 protected symbols to resolve directly to the function
9776 rather than going via the plt. If people want function
9777 pointer comparisons to work as expected then they should
9778 avoid writing weird assembly. */
9779 if (SYMBOL_CALLS_LOCAL (info, h))
9780 {
57e7d118
AM
9781 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9782 {
9783 p->count -= p->pc_count;
9784 p->pc_count = 0;
9785 if (p->count == 0)
9786 *pp = p->next;
9787 else
9788 pp = &p->next;
9789 }
9790 }
65f38f15 9791
57e7d118
AM
9792 /* Also discard relocs on undefined weak syms with
9793 non-default visibility. */
9794 if (eh->dyn_relocs != NULL
9795 && h->root.type == bfd_link_hash_undefweak)
5bd4f169 9796 {
57e7d118
AM
9797 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9798 eh->dyn_relocs = NULL;
9799
9800 /* Make sure this symbol is output as a dynamic symbol.
9801 Undefined weak syms won't yet be marked as dynamic. */
9802 else if (h->dynindx == -1
9803 && !h->forced_local)
9804 {
9805 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9806 return FALSE;
9807 }
5bd4f169 9808 }
65f38f15 9809 }
57e7d118 9810 else if (h->type == STT_GNU_IFUNC)
dfbb6ac9 9811 {
8a2058b5
AM
9812 /* A plt entry is always created when making direct calls to
9813 an ifunc, even when building a static executable, but
9814 that doesn't cover all cases. We may have only an ifunc
9815 initialised function pointer for a given ifunc symbol.
9816
9817 For ELFv2, dynamic relocations are not required when
9818 generating a global entry PLT stub. */
9819 if (abiversion (info->output_bfd) >= 2)
9820 {
9821 if (global_entry_stub (h))
9822 eh->dyn_relocs = NULL;
9823 }
9824
9825 /* For ELFv1 we have function descriptors. Descriptors need
9826 to be treated like PLT entries and thus have dynamic
9827 relocations. One exception is when the function
9828 descriptor is copied into .dynbss (which should only
9829 happen with ancient versions of gcc). */
9830 else if (h->needs_copy)
dfbb6ac9 9831 eh->dyn_relocs = NULL;
57e7d118
AM
9832 }
9833 else if (ELIMINATE_COPY_RELOCS)
9834 {
8a2058b5 9835 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9836 symbols which turn out to need copy relocs or are not
9837 dynamic. */
dfbb6ac9 9838
8a2058b5
AM
9839 /* First make sure this symbol is output as a dynamic symbol.
9840 Undefined weak syms won't yet be marked as dynamic. */
9841 if (h->root.type == bfd_link_hash_undefweak
9842 && !h->non_got_ref
9843 && !h->def_regular
9844 && h->dynindx == -1
9845 && !h->forced_local
9846 && !bfd_elf_link_record_dynamic_symbol (info, h))
9847 return FALSE;
57e7d118 9848
8a2058b5
AM
9849 if (h->non_got_ref
9850 || h->def_regular
9851 || h->dynindx == -1)
9852 eh->dyn_relocs = NULL;
57e7d118
AM
9853 }
9854
9855 /* Finally, allocate space. */
9856 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9857 {
9858 asection *sreloc = elf_section_data (p->sec)->sreloc;
9859 if (eh->elf.type == STT_GNU_IFUNC)
9860 sreloc = htab->elf.irelplt;
9861 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9862 }
65f38f15 9863 }
57e7d118
AM
9864
9865 if ((htab->elf.dynamic_sections_created
9866 && h->dynindx != -1
9867 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
9868 || h->type == STT_GNU_IFUNC)
65f38f15 9869 {
57e7d118
AM
9870 struct plt_entry *pent;
9871 bfd_boolean doneone = FALSE;
9872 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9873 if (pent->plt.refcount > 0)
9874 {
9875 if (!htab->elf.dynamic_sections_created
9876 || h->dynindx == -1)
9877 {
9878 s = htab->elf.iplt;
9879 pent->plt.offset = s->size;
9880 s->size += PLT_ENTRY_SIZE (htab);
9881 s = htab->elf.irelplt;
9882 }
9883 else
9884 {
9885 /* If this is the first .plt entry, make room for the special
9886 first entry. */
9887 s = htab->elf.splt;
9888 if (s->size == 0)
9889 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9890
57e7d118 9891 pent->plt.offset = s->size;
65f38f15 9892
57e7d118
AM
9893 /* Make room for this entry. */
9894 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9895
57e7d118
AM
9896 /* Make room for the .glink code. */
9897 s = htab->glink;
9898 if (s->size == 0)
9899 s->size += GLINK_CALL_STUB_SIZE;
9900 if (htab->opd_abi)
9901 {
9902 /* We need bigger stubs past index 32767. */
9903 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9904 s->size += 4;
9905 s->size += 2*4;
9906 }
9907 else
9908 s->size += 4;
65f38f15 9909
57e7d118
AM
9910 /* We also need to make an entry in the .rela.plt section. */
9911 s = htab->elf.srelplt;
9912 }
9913 s->size += sizeof (Elf64_External_Rela);
9914 doneone = TRUE;
9915 }
9916 else
9917 pent->plt.offset = (bfd_vma) -1;
9918 if (!doneone)
9919 {
9920 h->plt.plist = NULL;
9921 h->needs_plt = 0;
9922 }
65f38f15 9923 }
57e7d118 9924 else
65f38f15 9925 {
57e7d118
AM
9926 h->plt.plist = NULL;
9927 h->needs_plt = 0;
65f38f15
AM
9928 }
9929
b34976b6 9930 return TRUE;
65f38f15
AM
9931}
9932
a345bc8d
AM
9933/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9934 to set up space for global entry stubs. These are put in glink,
9935 after the branch table. */
65f38f15 9936
b34976b6 9937static bfd_boolean
a345bc8d 9938size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9939{
a345bc8d
AM
9940 struct bfd_link_info *info;
9941 struct ppc_link_hash_table *htab;
9942 struct plt_entry *pent;
9943 asection *s;
65f38f15 9944
a345bc8d
AM
9945 if (h->root.type == bfd_link_hash_indirect)
9946 return TRUE;
65f38f15 9947
a345bc8d
AM
9948 if (!h->pointer_equality_needed)
9949 return TRUE;
65f38f15 9950
a345bc8d
AM
9951 if (h->def_regular)
9952 return TRUE;
65f38f15 9953
a345bc8d
AM
9954 info = inf;
9955 htab = ppc_hash_table (info);
9956 if (htab == NULL)
9957 return FALSE;
9958
9959 s = htab->glink;
9960 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9961 if (pent->plt.offset != (bfd_vma) -1
9962 && pent->addend == 0)
9963 {
afe397ea
AM
9964 /* For ELFv2, if this symbol is not defined in a regular file
9965 and we are not generating a shared library or pie, then we
9966 need to define the symbol in the executable on a call stub.
9967 This is to avoid text relocations. */
a345bc8d 9968 s->size = (s->size + 15) & -16;
8a2058b5 9969 h->root.type = bfd_link_hash_defined;
afe397ea
AM
9970 h->root.u.def.section = s;
9971 h->root.u.def.value = s->size;
a345bc8d
AM
9972 s->size += 16;
9973 break;
9974 }
9975 return TRUE;
9976}
9977
9978/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9979 read-only sections. */
9980
9981static bfd_boolean
9982maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9983{
9984 if (h->root.type == bfd_link_hash_indirect)
9985 return TRUE;
9986
9987 if (readonly_dynrelocs (h))
9988 {
9989 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9990
9991 /* Not an error, just cut short the traversal. */
9992 return FALSE;
65f38f15 9993 }
b34976b6 9994 return TRUE;
65f38f15
AM
9995}
9996
9997/* Set the sizes of the dynamic sections. */
9998
b34976b6 9999static bfd_boolean
ee67d69a 10000ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 10001 struct bfd_link_info *info)
65f38f15
AM
10002{
10003 struct ppc_link_hash_table *htab;
10004 bfd *dynobj;
10005 asection *s;
b34976b6 10006 bfd_boolean relocs;
65f38f15 10007 bfd *ibfd;
7865406b 10008 struct got_entry *first_tlsld;
65f38f15
AM
10009
10010 htab = ppc_hash_table (info);
4dfe6ac6
NC
10011 if (htab == NULL)
10012 return FALSE;
10013
65f38f15
AM
10014 dynobj = htab->elf.dynobj;
10015 if (dynobj == NULL)
10016 abort ();
10017
10018 if (htab->elf.dynamic_sections_created)
10019 {
10020 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10021 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10022 {
3d4d4302 10023 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10024 if (s == NULL)
10025 abort ();
eea6121a 10026 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10027 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10028 }
10029 }
10030
10031 /* Set up .got offsets for local syms, and space for local dynamic
10032 relocs. */
c72f2fb2 10033 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10034 {
411e1bfb
AM
10035 struct got_entry **lgot_ents;
10036 struct got_entry **end_lgot_ents;
e054468f
AM
10037 struct plt_entry **local_plt;
10038 struct plt_entry **end_local_plt;
f961d9dd 10039 unsigned char *lgot_masks;
65f38f15
AM
10040 bfd_size_type locsymcount;
10041 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10042
0c8d6e5c 10043 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10044 continue;
10045
10046 for (s = ibfd->sections; s != NULL; s = s->next)
10047 {
19e08130 10048 struct ppc_dyn_relocs *p;
65f38f15 10049
6edfbbad 10050 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10051 {
ec338859
AM
10052 if (!bfd_is_abs_section (p->sec)
10053 && bfd_is_abs_section (p->sec->output_section))
10054 {
10055 /* Input section has been discarded, either because
10056 it is a copy of a linkonce section or due to
10057 linker script /DISCARD/, so we'll be discarding
10058 the relocs too. */
10059 }
248866a8 10060 else if (p->count != 0)
ec338859 10061 {
19e08130
AM
10062 asection *srel = elf_section_data (p->sec)->sreloc;
10063 if (p->ifunc)
33e44f2e 10064 srel = htab->elf.irelplt;
eea6121a 10065 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10066 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10067 info->flags |= DF_TEXTREL;
ec338859 10068 }
65f38f15
AM
10069 }
10070 }
10071
411e1bfb
AM
10072 lgot_ents = elf_local_got_ents (ibfd);
10073 if (!lgot_ents)
65f38f15
AM
10074 continue;
10075
0ffa91dd 10076 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10077 locsymcount = symtab_hdr->sh_info;
411e1bfb 10078 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10079 local_plt = (struct plt_entry **) end_lgot_ents;
10080 end_local_plt = local_plt + locsymcount;
f961d9dd 10081 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10082 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10083 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10084 {
0b8bcf0d 10085 struct got_entry **pent, *ent;
411e1bfb 10086
0b8bcf0d
AM
10087 pent = lgot_ents;
10088 while ((ent = *pent) != NULL)
411e1bfb
AM
10089 if (ent->got.refcount > 0)
10090 {
e7b938ca 10091 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10092 {
927be08e 10093 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10094 *pent = ent->next;
411e1bfb
AM
10095 }
10096 else
10097 {
19e08130
AM
10098 unsigned int ent_size = 8;
10099 unsigned int rel_size = sizeof (Elf64_External_Rela);
10100
eea6121a 10101 ent->got.offset = s->size;
e7b938ca 10102 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10103 {
19e08130
AM
10104 ent_size *= 2;
10105 rel_size *= 2;
10106 }
10107 s->size += ent_size;
10108 if ((*lgot_masks & PLT_IFUNC) != 0)
10109 {
33e44f2e 10110 htab->elf.irelplt->size += rel_size;
19e08130
AM
10111 htab->got_reli_size += rel_size;
10112 }
0e1862bb 10113 else if (bfd_link_pic (info))
19e08130
AM
10114 {
10115 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10116 srel->size += rel_size;
927be08e 10117 }
0b8bcf0d 10118 pent = &ent->next;
411e1bfb
AM
10119 }
10120 }
10121 else
0b8bcf0d 10122 *pent = ent->next;
65f38f15 10123 }
e054468f
AM
10124
10125 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10126 for (; local_plt < end_local_plt; ++local_plt)
10127 {
10128 struct plt_entry *ent;
10129
10130 for (ent = *local_plt; ent != NULL; ent = ent->next)
10131 if (ent->plt.refcount > 0)
10132 {
33e44f2e 10133 s = htab->elf.iplt;
e054468f 10134 ent->plt.offset = s->size;
b9e5796b 10135 s->size += PLT_ENTRY_SIZE (htab);
e054468f 10136
33e44f2e 10137 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
10138 }
10139 else
10140 ent->plt.offset = (bfd_vma) -1;
10141 }
65f38f15
AM
10142 }
10143
10144 /* Allocate global sym .plt and .got entries, and space for global
10145 sym dynamic relocs. */
4ce794b7 10146 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d
AM
10147 /* Stash the end of glink branch table. */
10148 if (htab->glink != NULL)
10149 htab->glink->rawsize = htab->glink->size;
10150
0e1862bb 10151 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10152 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10153
7865406b 10154 first_tlsld = NULL;
c72f2fb2 10155 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10156 {
7865406b
AM
10157 struct got_entry *ent;
10158
0c8d6e5c 10159 if (!is_ppc64_elf (ibfd))
102890f0
AM
10160 continue;
10161
7865406b
AM
10162 ent = ppc64_tlsld_got (ibfd);
10163 if (ent->got.refcount > 0)
102890f0 10164 {
7865406b 10165 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10166 {
7865406b
AM
10167 ent->is_indirect = TRUE;
10168 ent->got.ent = first_tlsld;
10169 }
10170 else
10171 {
10172 if (first_tlsld == NULL)
10173 first_tlsld = ent;
10174 s = ppc64_elf_tdata (ibfd)->got;
10175 ent->got.offset = s->size;
10176 ent->owner = ibfd;
10177 s->size += 16;
0e1862bb 10178 if (bfd_link_pic (info))
7865406b
AM
10179 {
10180 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10181 srel->size += sizeof (Elf64_External_Rela);
10182 }
102890f0
AM
10183 }
10184 }
10185 else
7865406b 10186 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10187 }
10188
65f38f15
AM
10189 /* We now have determined the sizes of the various dynamic sections.
10190 Allocate memory for them. */
b34976b6 10191 relocs = FALSE;
65f38f15
AM
10192 for (s = dynobj->sections; s != NULL; s = s->next)
10193 {
10194 if ((s->flags & SEC_LINKER_CREATED) == 0)
10195 continue;
10196
4ce794b7 10197 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10198 /* These haven't been allocated yet; don't strip. */
10199 continue;
33e44f2e
AM
10200 else if (s == htab->elf.sgot
10201 || s == htab->elf.splt
10202 || s == htab->elf.iplt
c456f082
AM
10203 || s == htab->glink
10204 || s == htab->dynbss)
65f38f15
AM
10205 {
10206 /* Strip this section if we don't need it; see the
10207 comment below. */
5bd4f169 10208 }
58d180e8
AM
10209 else if (s == htab->glink_eh_frame)
10210 {
10211 if (!bfd_is_abs_section (s->output_section))
10212 /* Not sized yet. */
10213 continue;
10214 }
70cc837d 10215 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10216 {
c456f082 10217 if (s->size != 0)
5bd4f169 10218 {
33e44f2e 10219 if (s != htab->elf.srelplt)
b34976b6 10220 relocs = TRUE;
5bd4f169
AM
10221
10222 /* We use the reloc_count field as a counter if we need
10223 to copy relocs into the output file. */
10224 s->reloc_count = 0;
10225 }
10226 }
65f38f15 10227 else
5bd4f169
AM
10228 {
10229 /* It's not one of our sections, so don't allocate space. */
10230 continue;
10231 }
10232
eea6121a 10233 if (s->size == 0)
5bd4f169 10234 {
c456f082
AM
10235 /* If we don't need this section, strip it from the
10236 output file. This is mostly to handle .rela.bss and
10237 .rela.plt. We must create both sections in
10238 create_dynamic_sections, because they must be created
10239 before the linker maps input sections to output
10240 sections. The linker does that before
10241 adjust_dynamic_symbol is called, and it is that
10242 function which decides whether anything needs to go
10243 into these sections. */
8423293d 10244 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10245 continue;
10246 }
10247
c456f082 10248 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10249 continue;
10250
65f38f15
AM
10251 /* Allocate memory for the section contents. We use bfd_zalloc
10252 here in case unused entries are not reclaimed before the
10253 section's contents are written out. This should not happen,
411e1bfb
AM
10254 but this way if it does we get a R_PPC64_NONE reloc in .rela
10255 sections instead of garbage.
10256 We also rely on the section contents being zero when writing
10257 the GOT. */
eea6121a 10258 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10259 if (s->contents == NULL)
b34976b6 10260 return FALSE;
5bd4f169
AM
10261 }
10262
c72f2fb2 10263 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10264 {
0c8d6e5c 10265 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10266 continue;
10267
e717da7e 10268 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10269 if (s != NULL && s != htab->elf.sgot)
e717da7e 10270 {
eea6121a 10271 if (s->size == 0)
8423293d 10272 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10273 else
10274 {
eea6121a 10275 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10276 if (s->contents == NULL)
10277 return FALSE;
10278 }
10279 }
10280 s = ppc64_elf_tdata (ibfd)->relgot;
10281 if (s != NULL)
10282 {
eea6121a 10283 if (s->size == 0)
8423293d 10284 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10285 else
10286 {
eea6121a 10287 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10288 if (s->contents == NULL)
10289 return FALSE;
10290 relocs = TRUE;
10291 s->reloc_count = 0;
10292 }
10293 }
10294 }
10295
e86ce104 10296 if (htab->elf.dynamic_sections_created)
5bd4f169 10297 {
e8910a83
AM
10298 bfd_boolean tls_opt;
10299
5bd4f169
AM
10300 /* Add some entries to the .dynamic section. We fill in the
10301 values later, in ppc64_elf_finish_dynamic_sections, but we
10302 must add the entries now so that we get the correct size for
10303 the .dynamic section. The DT_DEBUG entry is filled in by the
10304 dynamic linker and used by the debugger. */
dc810e39 10305#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10306 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10307
0e1862bb 10308 if (bfd_link_executable (info))
5bd4f169 10309 {
dc810e39 10310 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10311 return FALSE;
5bd4f169
AM
10312 }
10313
33e44f2e 10314 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10315 {
dc810e39
AM
10316 if (!add_dynamic_entry (DT_PLTGOT, 0)
10317 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10318 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10319 || !add_dynamic_entry (DT_JMPREL, 0)
10320 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10321 return FALSE;
5bd4f169
AM
10322 }
10323
ee67d69a 10324 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10325 {
10326 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10327 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10328 return FALSE;
19397422
AM
10329 }
10330
7c9cf415 10331 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10332 && htab->tls_get_addr_fd != NULL
10333 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10334 if (tls_opt || !htab->opd_abi)
10335 {
10336 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10337 return FALSE;
10338 }
a7f2871e 10339
5bd4f169
AM
10340 if (relocs)
10341 {
dc810e39
AM
10342 if (!add_dynamic_entry (DT_RELA, 0)
10343 || !add_dynamic_entry (DT_RELASZ, 0)
10344 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10345 return FALSE;
5bd4f169 10346
65f38f15
AM
10347 /* If any dynamic relocs apply to a read-only section,
10348 then we need a DT_TEXTREL entry. */
248866a8 10349 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10350 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10351
65f38f15 10352 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10353 {
65f38f15 10354 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10355 return FALSE;
5bd4f169 10356 }
5bd4f169 10357 }
5bd4f169 10358 }
65f38f15 10359#undef add_dynamic_entry
5bd4f169 10360
b34976b6 10361 return TRUE;
5bd4f169
AM
10362}
10363
a345bc8d
AM
10364/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10365
10366static bfd_boolean
10367ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10368{
10369 if (h->plt.plist != NULL
10370 && !h->def_regular
10371 && !h->pointer_equality_needed)
10372 return FALSE;
10373
10374 return _bfd_elf_hash_symbol (h);
10375}
10376
721956f4 10377/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10378
4ce794b7
AM
10379static inline enum ppc_stub_type
10380ppc_type_of_stub (asection *input_sec,
10381 const Elf_Internal_Rela *rel,
10382 struct ppc_link_hash_entry **hash,
e054468f 10383 struct plt_entry **plt_ent,
6911b7dc
AM
10384 bfd_vma destination,
10385 unsigned long local_off)
5bd4f169 10386{
721956f4
AM
10387 struct ppc_link_hash_entry *h = *hash;
10388 bfd_vma location;
10389 bfd_vma branch_offset;
10390 bfd_vma max_branch_offset;
4ce794b7 10391 enum elf_ppc64_reloc_type r_type;
5bd4f169 10392
721956f4
AM
10393 if (h != NULL)
10394 {
e054468f 10395 struct plt_entry *ent;
7fe2b9a6 10396 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10397 if (h->oh != NULL
10398 && h->oh->is_func_descriptor)
7b8f6675
AM
10399 {
10400 fdh = ppc_follow_link (h->oh);
10401 *hash = fdh;
10402 }
8387904d 10403
e054468f
AM
10404 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10405 if (ent->addend == rel->r_addend
10406 && ent->plt.offset != (bfd_vma) -1)
10407 {
e054468f
AM
10408 *plt_ent = ent;
10409 return ppc_stub_plt_call;
10410 }
5bd4f169 10411
7fe2b9a6
AM
10412 /* Here, we know we don't have a plt entry. If we don't have a
10413 either a defined function descriptor or a defined entry symbol
10414 in a regular object file, then it is pointless trying to make
10415 any other type of stub. */
854b41e7
AM
10416 if (!is_static_defined (&fdh->elf)
10417 && !is_static_defined (&h->elf))
721956f4 10418 return ppc_stub_none;
5d1634d7 10419 }
e054468f
AM
10420 else if (elf_local_got_ents (input_sec->owner) != NULL)
10421 {
10422 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10423 struct plt_entry **local_plt = (struct plt_entry **)
10424 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10425 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10426
10427 if (local_plt[r_symndx] != NULL)
10428 {
10429 struct plt_entry *ent;
10430
10431 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10432 if (ent->addend == rel->r_addend
10433 && ent->plt.offset != (bfd_vma) -1)
10434 {
10435 *plt_ent = ent;
10436 return ppc_stub_plt_call;
10437 }
10438 }
10439 }
5d1634d7 10440
721956f4
AM
10441 /* Determine where the call point is. */
10442 location = (input_sec->output_offset
10443 + input_sec->output_section->vma
10444 + rel->r_offset);
5d1634d7 10445
721956f4
AM
10446 branch_offset = destination - location;
10447 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10448
721956f4
AM
10449 /* Determine if a long branch stub is needed. */
10450 max_branch_offset = 1 << 25;
4ce794b7 10451 if (r_type != R_PPC64_REL24)
721956f4 10452 max_branch_offset = 1 << 15;
5d1634d7 10453
6911b7dc 10454 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10455 /* We need a stub. Figure out whether a long_branch or plt_branch
10456 is needed later. */
10457 return ppc_stub_long_branch;
5d1634d7 10458
721956f4 10459 return ppc_stub_none;
5d1634d7
AM
10460}
10461
794e51c0
AM
10462/* With power7 weakly ordered memory model, it is possible for ld.so
10463 to update a plt entry in one thread and have another thread see a
10464 stale zero toc entry. To avoid this we need some sort of acquire
10465 barrier in the call stub. One solution is to make the load of the
10466 toc word seem to appear to depend on the load of the function entry
10467 word. Another solution is to test for r2 being zero, and branch to
10468 the appropriate glink entry if so.
10469
10470 . fake dep barrier compare
71a39c98
AM
10471 . ld 12,xxx(2) ld 12,xxx(2)
10472 . mtctr 12 mtctr 12
10473 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10474 . add 2,2,11 cmpldi 2,0
10475 . ld 2,xxx+8(2) bnectr+
10476 . bctr b <glink_entry>
10477
10478 The solution involving the compare turns out to be faster, so
10479 that's what we use unless the branch won't reach. */
10480
10481#define ALWAYS_USE_FAKE_DEP 0
10482#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10483
5d1634d7
AM
10484#define PPC_LO(v) ((v) & 0xffff)
10485#define PPC_HI(v) (((v) >> 16) & 0xffff)
10486#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10487
794e51c0
AM
10488static inline unsigned int
10489plt_stub_size (struct ppc_link_hash_table *htab,
10490 struct ppc_stub_hash_entry *stub_entry,
10491 bfd_vma off)
10492{
b9e5796b
AM
10493 unsigned size = 12;
10494
10495 if (ALWAYS_EMIT_R2SAVE
10496 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10497 size += 4;
10498 if (PPC_HA (off) != 0)
794e51c0 10499 size += 4;
b9e5796b
AM
10500 if (htab->opd_abi)
10501 {
10502 size += 4;
e7d1c40c 10503 if (htab->params->plt_static_chain)
b9e5796b 10504 size += 4;
bd4d2eaa
AM
10505 if (htab->params->plt_thread_safe
10506 && htab->elf.dynamic_sections_created
10507 && stub_entry->h != NULL
10508 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10509 size += 8;
e7d1c40c 10510 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10511 size += 4;
10512 }
794e51c0
AM
10513 if (stub_entry->h != NULL
10514 && (stub_entry->h == htab->tls_get_addr_fd
10515 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10516 && htab->params->tls_get_addr_opt)
794e51c0
AM
10517 size += 13 * 4;
10518 return size;
10519}
10520
10521/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10522 then return the padding needed to do so. */
10523static inline unsigned int
10524plt_stub_pad (struct ppc_link_hash_table *htab,
10525 struct ppc_stub_hash_entry *stub_entry,
10526 bfd_vma plt_off)
10527{
e7d1c40c 10528 int stub_align = 1 << htab->params->plt_stub_align;
794e51c0 10529 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10530 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0
AM
10531
10532 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10533 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10534 return stub_align - (stub_off & (stub_align - 1));
10535 return 0;
10536}
10537
10538/* Build a .plt call stub. */
10539
10540static inline bfd_byte *
10541build_plt_stub (struct ppc_link_hash_table *htab,
10542 struct ppc_stub_hash_entry *stub_entry,
10543 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10544{
e7d1c40c 10545 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10546 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10547 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10548 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10549 && htab->elf.dynamic_sections_created
10550 && stub_entry->h != NULL
10551 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10552 bfd_boolean use_fake_dep = plt_thread_safe;
10553 bfd_vma cmp_branch_off = 0;
10554
10555 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10556 && plt_load_toc
794e51c0 10557 && plt_thread_safe
bd4d2eaa
AM
10558 && !((stub_entry->h == htab->tls_get_addr_fd
10559 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10560 && htab->params->tls_get_addr_opt))
794e51c0
AM
10561 {
10562 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10563 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10564 / PLT_ENTRY_SIZE (htab));
794e51c0
AM
10565 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10566 bfd_vma to, from;
10567
68d62958
AM
10568 if (pltindex > 32768)
10569 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10570 to = (glinkoff
10571 + htab->glink->output_offset
10572 + htab->glink->output_section->vma);
6f20ed8a 10573 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10574 + 4 * (ALWAYS_EMIT_R2SAVE
10575 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10576 + 4 * (PPC_HA (offset) != 0)
10577 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10578 != PPC_HA (offset))
10579 + 4 * (plt_static_chain != 0)
10580 + 20
6f20ed8a
AM
10581 + stub_entry->group->stub_sec->output_offset
10582 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10583 cmp_branch_off = to - from;
10584 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10585 }
10586
ac2df442
AM
10587 if (PPC_HA (offset) != 0)
10588 {
176a0d42
AM
10589 if (r != NULL)
10590 {
794e51c0
AM
10591 if (ALWAYS_EMIT_R2SAVE
10592 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10593 r[0].r_offset += 4;
176a0d42 10594 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10595 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10596 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10597 r[1].r_addend = r[0].r_addend;
b9e5796b 10598 if (plt_load_toc)
176a0d42 10599 {
b9e5796b 10600 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10601 {
b9e5796b
AM
10602 r[2].r_offset = r[1].r_offset + 4;
10603 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10604 r[2].r_addend = r[0].r_addend;
10605 }
10606 else
10607 {
10608 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10609 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10610 r[2].r_addend = r[0].r_addend + 8;
10611 if (plt_static_chain)
10612 {
10613 r[3].r_offset = r[2].r_offset + 4;
10614 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10615 r[3].r_addend = r[0].r_addend + 16;
10616 }
c7131b65 10617 }
176a0d42
AM
10618 }
10619 }
794e51c0
AM
10620 if (ALWAYS_EMIT_R2SAVE
10621 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10622 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10623 if (plt_load_toc)
10624 {
10625 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10626 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10627 }
10628 else
10629 {
10630 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10631 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10632 }
b9e5796b
AM
10633 if (plt_load_toc
10634 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10635 {
71a39c98 10636 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10637 offset = 0;
10638 }
71a39c98 10639 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10640 if (plt_load_toc)
794e51c0 10641 {
b9e5796b
AM
10642 if (use_fake_dep)
10643 {
10644 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10645 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10646 }
10647 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10648 if (plt_static_chain)
10649 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10650 }
ac2df442
AM
10651 }
10652 else
10653 {
176a0d42
AM
10654 if (r != NULL)
10655 {
794e51c0
AM
10656 if (ALWAYS_EMIT_R2SAVE
10657 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10658 r[0].r_offset += 4;
176a0d42 10659 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10660 if (plt_load_toc)
176a0d42 10661 {
b9e5796b 10662 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10663 {
b9e5796b
AM
10664 r[1].r_offset = r[0].r_offset + 4;
10665 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10666 r[1].r_addend = r[0].r_addend;
10667 }
10668 else
10669 {
10670 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10671 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10672 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10673 if (plt_static_chain)
10674 {
10675 r[2].r_offset = r[1].r_offset + 4;
10676 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10677 r[2].r_addend = r[0].r_addend + 8;
10678 }
c7131b65 10679 }
176a0d42
AM
10680 }
10681 }
794e51c0
AM
10682 if (ALWAYS_EMIT_R2SAVE
10683 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10684 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10685 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10686 if (plt_load_toc
10687 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10688 {
10689 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10690 offset = 0;
10691 }
71a39c98 10692 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10693 if (plt_load_toc)
794e51c0 10694 {
b9e5796b
AM
10695 if (use_fake_dep)
10696 {
10697 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10698 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10699 }
10700 if (plt_static_chain)
10701 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10702 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10703 }
ac2df442 10704 }
b9e5796b 10705 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10706 {
10707 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10708 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10709 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10710 }
10711 else
10712 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10713 return p;
10714}
10715
a7f2871e
AM
10716/* Build a special .plt call stub for __tls_get_addr. */
10717
10718#define LD_R11_0R3 0xe9630000
10719#define LD_R12_0R3 0xe9830000
10720#define MR_R0_R3 0x7c601b78
10721#define CMPDI_R11_0 0x2c2b0000
10722#define ADD_R3_R12_R13 0x7c6c6a14
10723#define BEQLR 0x4d820020
10724#define MR_R3_R0 0x7c030378
a7f2871e
AM
10725#define STD_R11_0R1 0xf9610000
10726#define BCTRL 0x4e800421
10727#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10728#define MTLR_R11 0x7d6803a6
10729
10730static inline bfd_byte *
794e51c0
AM
10731build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10732 struct ppc_stub_hash_entry *stub_entry,
10733 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10734{
e7d1c40c 10735 bfd *obfd = htab->params->stub_bfd;
794e51c0 10736
a7f2871e
AM
10737 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10738 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10739 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10740 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10741 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10742 bfd_put_32 (obfd, BEQLR, p), p += 4;
10743 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10744 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10745 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10746
10747 if (r != NULL)
10748 r[0].r_offset += 9 * 4;
794e51c0 10749 p = build_plt_stub (htab, stub_entry, p, offset, r);
a7f2871e
AM
10750 bfd_put_32 (obfd, BCTRL, p - 4);
10751
a078d95a 10752 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10753 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10754 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10755 bfd_put_32 (obfd, BLR, p), p += 4;
10756
10757 return p;
10758}
10759
176a0d42
AM
10760static Elf_Internal_Rela *
10761get_relocs (asection *sec, int count)
10762{
10763 Elf_Internal_Rela *relocs;
10764 struct bfd_elf_section_data *elfsec_data;
10765
10766 elfsec_data = elf_section_data (sec);
10767 relocs = elfsec_data->relocs;
10768 if (relocs == NULL)
10769 {
10770 bfd_size_type relsize;
10771 relsize = sec->reloc_count * sizeof (*relocs);
10772 relocs = bfd_alloc (sec->owner, relsize);
10773 if (relocs == NULL)
10774 return NULL;
10775 elfsec_data->relocs = relocs;
d4730f92
BS
10776 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10777 sizeof (Elf_Internal_Shdr));
10778 if (elfsec_data->rela.hdr == NULL)
10779 return NULL;
10780 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10781 * sizeof (Elf64_External_Rela));
10782 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10783 sec->reloc_count = 0;
10784 }
10785 relocs += sec->reloc_count;
10786 sec->reloc_count += count;
10787 return relocs;
10788}
10789
aa374f67 10790static bfd_vma
25f53a85 10791get_r2off (struct bfd_link_info *info,
aa374f67
AM
10792 struct ppc_stub_hash_entry *stub_entry)
10793{
25f53a85 10794 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10795 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10796
10797 if (r2off == 0)
10798 {
10799 /* Support linking -R objects. Get the toc pointer from the
10800 opd entry. */
10801 char buf[8];
b9e5796b
AM
10802 if (!htab->opd_abi)
10803 return r2off;
aa374f67
AM
10804 asection *opd = stub_entry->h->elf.root.u.def.section;
10805 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10806
10807 if (strcmp (opd->name, ".opd") != 0
10808 || opd->reloc_count != 0)
10809 {
bc30df16 10810 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
25f53a85 10811 stub_entry->h->elf.root.root.string);
aa374f67 10812 bfd_set_error (bfd_error_bad_value);
a7c49797 10813 return (bfd_vma) -1;
aa374f67
AM
10814 }
10815 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10816 return (bfd_vma) -1;
aa374f67 10817 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10818 r2off -= elf_gp (info->output_bfd);
aa374f67 10819 }
6f20ed8a 10820 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10821 return r2off;
10822}
10823
b34976b6 10824static bfd_boolean
4ce794b7 10825ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10826{
721956f4
AM
10827 struct ppc_stub_hash_entry *stub_entry;
10828 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10829 struct bfd_link_info *info;
10830 struct ppc_link_hash_table *htab;
721956f4
AM
10831 bfd_byte *loc;
10832 bfd_byte *p;
ee75fd95 10833 bfd_vma dest, off;
721956f4 10834 int size;
176a0d42 10835 Elf_Internal_Rela *r;
e054468f 10836 asection *plt;
5d1634d7 10837
721956f4
AM
10838 /* Massage our args to the form they really have. */
10839 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10840 info = in_arg;
5d1634d7 10841
5d1634d7 10842 htab = ppc_hash_table (info);
4dfe6ac6
NC
10843 if (htab == NULL)
10844 return FALSE;
5d1634d7 10845
721956f4 10846 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
10847 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10848 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10849
4ce794b7 10850 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10851 switch (stub_entry->stub_type)
5d1634d7 10852 {
721956f4 10853 case ppc_stub_long_branch:
ad8e1ba5 10854 case ppc_stub_long_branch_r2off:
721956f4 10855 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10856 dest = (stub_entry->target_value
10857 + stub_entry->target_section->output_offset
10858 + stub_entry->target_section->output_section->vma);
10859 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10860 off = dest;
5d1634d7 10861
721956f4
AM
10862 /* And this is where we are coming from. */
10863 off -= (stub_entry->stub_offset
6f20ed8a
AM
10864 + stub_entry->group->stub_sec->output_offset
10865 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 10866
ac2df442
AM
10867 size = 4;
10868 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10869 {
25f53a85 10870 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10871
a7c49797 10872 if (r2off == (bfd_vma) -1)
aa374f67
AM
10873 {
10874 htab->stub_error = TRUE;
10875 return FALSE;
10876 }
e7d1c40c 10877 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10878 loc += 4;
a7c49797 10879 size = 8;
ac2df442
AM
10880 if (PPC_HA (r2off) != 0)
10881 {
e7d1c40c
AM
10882 bfd_put_32 (htab->params->stub_bfd,
10883 ADDIS_R2_R2 | PPC_HA (r2off), loc);
ac2df442 10884 loc += 4;
a7c49797
AM
10885 size += 4;
10886 }
10887 if (PPC_LO (r2off) != 0)
10888 {
10889 bfd_put_32 (htab->params->stub_bfd,
10890 ADDI_R2_R2 | PPC_LO (r2off), loc);
10891 loc += 4;
10892 size += 4;
ac2df442 10893 }
ac2df442 10894 off -= size - 4;
ad8e1ba5 10895 }
e7d1c40c 10896 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 10897
5c3dead3
AM
10898 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10899 {
bc30df16
AM
10900 info->callbacks->einfo
10901 (_("%P: long branch stub `%s' offset overflow\n"),
10902 stub_entry->root.string);
5c3dead3
AM
10903 htab->stub_error = TRUE;
10904 return FALSE;
10905 }
ee75fd95
AM
10906
10907 if (info->emitrelocations)
10908 {
6f20ed8a 10909 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10910 if (r == NULL)
10911 return FALSE;
6f20ed8a 10912 r->r_offset = loc - stub_entry->group->stub_sec->contents;
ee75fd95
AM
10913 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10914 r->r_addend = dest;
10915 if (stub_entry->h != NULL)
10916 {
10917 struct elf_link_hash_entry **hashes;
10918 unsigned long symndx;
10919 struct ppc_link_hash_entry *h;
10920
e7d1c40c 10921 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10922 if (hashes == NULL)
10923 {
10924 bfd_size_type hsize;
10925
10926 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10927 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10928 if (hashes == NULL)
10929 return FALSE;
e7d1c40c 10930 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10931 htab->stub_globals = 1;
10932 }
10933 symndx = htab->stub_globals++;
10934 h = stub_entry->h;
10935 hashes[symndx] = &h->elf;
10936 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10937 if (h->oh != NULL && h->oh->is_func)
b31867b6 10938 h = ppc_follow_link (h->oh);
ee75fd95
AM
10939 if (h->elf.root.u.def.section != stub_entry->target_section)
10940 /* H is an opd symbol. The addend must be zero. */
10941 r->r_addend = 0;
10942 else
10943 {
10944 off = (h->elf.root.u.def.value
10945 + h->elf.root.u.def.section->output_offset
10946 + h->elf.root.u.def.section->output_section->vma);
10947 r->r_addend -= off;
10948 }
10949 }
10950 }
721956f4 10951 break;
e86ce104 10952
721956f4 10953 case ppc_stub_plt_branch:
ad8e1ba5 10954 case ppc_stub_plt_branch_r2off:
721956f4
AM
10955 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10956 stub_entry->root.string + 9,
b34976b6 10957 FALSE, FALSE);
721956f4
AM
10958 if (br_entry == NULL)
10959 {
8de848d8 10960 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 10961 stub_entry->root.string);
b34976b6
AM
10962 htab->stub_error = TRUE;
10963 return FALSE;
721956f4
AM
10964 }
10965
176a0d42
AM
10966 dest = (stub_entry->target_value
10967 + stub_entry->target_section->output_offset
10968 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10969 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10970 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10971
176a0d42 10972 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10973 htab->brlt->contents + br_entry->offset);
721956f4 10974
f94498ff 10975 if (br_entry->iter == htab->stub_iteration)
721956f4 10976 {
f94498ff 10977 br_entry->iter = 0;
84f5d08e 10978
f94498ff 10979 if (htab->relbrlt != NULL)
84f5d08e 10980 {
f94498ff
AM
10981 /* Create a reloc for the branch lookup table entry. */
10982 Elf_Internal_Rela rela;
10983 bfd_byte *rl;
10984
10985 rela.r_offset = (br_entry->offset
10986 + htab->brlt->output_offset
10987 + htab->brlt->output_section->vma);
10988 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10989 rela.r_addend = dest;
f94498ff
AM
10990
10991 rl = htab->relbrlt->contents;
10992 rl += (htab->relbrlt->reloc_count++
10993 * sizeof (Elf64_External_Rela));
10994 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10995 }
10996 else if (info->emitrelocations)
10997 {
176a0d42
AM
10998 r = get_relocs (htab->brlt, 1);
10999 if (r == NULL)
11000 return FALSE;
11001 /* brlt, being SEC_LINKER_CREATED does not go through the
11002 normal reloc processing. Symbols and offsets are not
11003 translated from input file to output file form, so
11004 set up the offset per the output file. */
f94498ff
AM
11005 r->r_offset = (br_entry->offset
11006 + htab->brlt->output_offset
11007 + htab->brlt->output_section->vma);
11008 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 11009 r->r_addend = dest;
84f5d08e 11010 }
84f5d08e 11011 }
721956f4 11012
176a0d42
AM
11013 dest = (br_entry->offset
11014 + htab->brlt->output_offset
11015 + htab->brlt->output_section->vma);
11016
11017 off = (dest
4ce794b7 11018 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 11019 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11020
ad8e1ba5 11021 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11022 {
25f53a85 11023 info->callbacks->einfo
bc30df16 11024 (_("%P: linkage table error against `%T'\n"),
721956f4 11025 stub_entry->root.string);
5d1634d7 11026 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11027 htab->stub_error = TRUE;
11028 return FALSE;
5d1634d7 11029 }
41bd81ab 11030
176a0d42
AM
11031 if (info->emitrelocations)
11032 {
6f20ed8a 11033 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11034 if (r == NULL)
11035 return FALSE;
6f20ed8a 11036 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11037 if (bfd_big_endian (info->output_bfd))
11038 r[0].r_offset += 2;
00f412ee 11039 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11040 r[0].r_offset += 4;
11041 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11042 r[0].r_addend = dest;
11043 if (PPC_HA (off) != 0)
11044 {
11045 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11046 r[1].r_offset = r[0].r_offset + 4;
11047 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11048 r[1].r_addend = r[0].r_addend;
11049 }
11050 }
11051
00f412ee 11052 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11053 {
176a0d42 11054 if (PPC_HA (off) != 0)
ac2df442
AM
11055 {
11056 size = 16;
e7d1c40c 11057 bfd_put_32 (htab->params->stub_bfd,
397998fc 11058 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 11059 loc += 4;
e7d1c40c 11060 bfd_put_32 (htab->params->stub_bfd,
397998fc 11061 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
11062 }
11063 else
11064 {
11065 size = 12;
e7d1c40c
AM
11066 bfd_put_32 (htab->params->stub_bfd,
11067 LD_R12_0R2 | PPC_LO (off), loc);
ac2df442 11068 }
ad8e1ba5
AM
11069 }
11070 else
11071 {
25f53a85 11072 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11073
a7c49797 11074 if (r2off == (bfd_vma) -1)
aa374f67
AM
11075 {
11076 htab->stub_error = TRUE;
11077 return FALSE;
11078 }
ad8e1ba5 11079
e7d1c40c 11080 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 11081 loc += 4;
00f412ee 11082 size = 16;
176a0d42 11083 if (PPC_HA (off) != 0)
ac2df442
AM
11084 {
11085 size += 4;
e7d1c40c 11086 bfd_put_32 (htab->params->stub_bfd,
397998fc 11087 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 11088 loc += 4;
e7d1c40c 11089 bfd_put_32 (htab->params->stub_bfd,
397998fc 11090 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
11091 }
11092 else
e7d1c40c 11093 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442
AM
11094
11095 if (PPC_HA (r2off) != 0)
11096 {
11097 size += 4;
00f412ee 11098 loc += 4;
e7d1c40c
AM
11099 bfd_put_32 (htab->params->stub_bfd,
11100 ADDIS_R2_R2 | PPC_HA (r2off), loc);
00f412ee
AM
11101 }
11102 if (PPC_LO (r2off) != 0)
11103 {
11104 size += 4;
ac2df442 11105 loc += 4;
e7d1c40c
AM
11106 bfd_put_32 (htab->params->stub_bfd,
11107 ADDI_R2_R2 | PPC_LO (r2off), loc);
ac2df442 11108 }
ad8e1ba5
AM
11109 }
11110 loc += 4;
e7d1c40c 11111 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
ad8e1ba5 11112 loc += 4;
e7d1c40c 11113 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
721956f4 11114 break;
5d1634d7 11115
721956f4 11116 case ppc_stub_plt_call:
794e51c0 11117 case ppc_stub_plt_call_r2save:
e054468f 11118 if (stub_entry->h != NULL
b31867b6
AM
11119 && stub_entry->h->is_func_descriptor
11120 && stub_entry->h->oh != NULL)
c862ae31 11121 {
b31867b6
AM
11122 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11123
11124 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11125 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11126 if (fh->elf.root.type == bfd_link_hash_undefined
11127 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11128 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11129 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11130 }
11131
721956f4 11132 /* Now build the stub. */
e054468f 11133 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 11134 if (dest >= (bfd_vma) -2)
721956f4
AM
11135 abort ();
11136
33e44f2e 11137 plt = htab->elf.splt;
25f23106
AM
11138 if (!htab->elf.dynamic_sections_created
11139 || stub_entry->h == NULL
11140 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11141 plt = htab->elf.iplt;
e054468f
AM
11142
11143 dest += plt->output_offset + plt->output_section->vma;
11144
11145 if (stub_entry->h == NULL
11146 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11147 {
11148 Elf_Internal_Rela rela;
11149 bfd_byte *rl;
11150
11151 rela.r_offset = dest;
ee67d69a
AM
11152 if (htab->opd_abi)
11153 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11154 else
11155 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
11156 rela.r_addend = (stub_entry->target_value
11157 + stub_entry->target_section->output_offset
11158 + stub_entry->target_section->output_section->vma);
11159
33e44f2e
AM
11160 rl = (htab->elf.irelplt->contents
11161 + (htab->elf.irelplt->reloc_count++
25f23106
AM
11162 * sizeof (Elf64_External_Rela)));
11163 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
11164 stub_entry->plt_ent->plt.offset |= 1;
11165 }
176a0d42
AM
11166
11167 off = (dest
e054468f 11168 - elf_gp (plt->output_section->owner)
6f20ed8a 11169 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11170
ad8e1ba5 11171 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11172 {
25f53a85 11173 info->callbacks->einfo
695344c0 11174 /* xgettext:c-format */
bc30df16 11175 (_("%P: linkage table error against `%T'\n"),
e054468f
AM
11176 stub_entry->h != NULL
11177 ? stub_entry->h->elf.root.root.string
11178 : "<local sym>");
721956f4 11179 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11180 htab->stub_error = TRUE;
11181 return FALSE;
721956f4
AM
11182 }
11183
e7d1c40c 11184 if (htab->params->plt_stub_align != 0)
794e51c0
AM
11185 {
11186 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11187
6f20ed8a
AM
11188 stub_entry->group->stub_sec->size += pad;
11189 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
11190 loc += pad;
11191 }
11192
176a0d42
AM
11193 r = NULL;
11194 if (info->emitrelocations)
11195 {
6f20ed8a 11196 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11197 ((PPC_HA (off) != 0)
11198 + (htab->opd_abi
e7d1c40c 11199 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11200 && PPC_HA (off + 16) == PPC_HA (off))
11201 : 1)));
176a0d42
AM
11202 if (r == NULL)
11203 return FALSE;
6f20ed8a 11204 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11205 if (bfd_big_endian (info->output_bfd))
11206 r[0].r_offset += 2;
176a0d42
AM
11207 r[0].r_addend = dest;
11208 }
a7f2871e
AM
11209 if (stub_entry->h != NULL
11210 && (stub_entry->h == htab->tls_get_addr_fd
11211 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11212 && htab->params->tls_get_addr_opt)
794e51c0 11213 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11214 else
794e51c0 11215 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11216 size = p - loc;
11217 break;
11218
a4b6fadd
AM
11219 case ppc_stub_save_res:
11220 return TRUE;
11221
721956f4
AM
11222 default:
11223 BFD_FAIL ();
b34976b6 11224 return FALSE;
721956f4
AM
11225 }
11226
6f20ed8a 11227 stub_entry->group->stub_sec->size += size;
97b639ba 11228
e7d1c40c 11229 if (htab->params->emit_stub_syms)
97b639ba
AM
11230 {
11231 struct elf_link_hash_entry *h;
ee75fd95
AM
11232 size_t len1, len2;
11233 char *name;
11234 const char *const stub_str[] = { "long_branch",
11235 "long_branch_r2off",
11236 "plt_branch",
11237 "plt_branch_r2off",
794e51c0 11238 "plt_call",
ee75fd95
AM
11239 "plt_call" };
11240
11241 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11242 len2 = strlen (stub_entry->root.string);
11243 name = bfd_malloc (len1 + len2 + 2);
11244 if (name == NULL)
11245 return FALSE;
11246 memcpy (name, stub_entry->root.string, 9);
11247 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11248 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11249 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11250 if (h == NULL)
11251 return FALSE;
11252 if (h->root.type == bfd_link_hash_new)
11253 {
11254 h->root.type = bfd_link_hash_defined;
6f20ed8a 11255 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11256 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11257 h->ref_regular = 1;
11258 h->def_regular = 1;
11259 h->ref_regular_nonweak = 1;
11260 h->forced_local = 1;
11261 h->non_elf = 0;
2ec55de3 11262 h->root.linker_def = 1;
97b639ba
AM
11263 }
11264 }
11265
b34976b6 11266 return TRUE;
721956f4
AM
11267}
11268
11269/* As above, but don't actually build the stub. Just bump offset so
11270 we know stub section sizes, and select plt_branch stubs where
11271 long_branch stubs won't do. */
11272
b34976b6 11273static bfd_boolean
4ce794b7 11274ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11275{
11276 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11277 struct bfd_link_info *info;
721956f4
AM
11278 struct ppc_link_hash_table *htab;
11279 bfd_vma off;
11280 int size;
11281
11282 /* Massage our args to the form they really have. */
11283 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11284 info = in_arg;
11285
11286 htab = ppc_hash_table (info);
4dfe6ac6
NC
11287 if (htab == NULL)
11288 return FALSE;
721956f4 11289
a4b6fadd
AM
11290 if (stub_entry->h != NULL
11291 && stub_entry->h->save_res
11292 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11293 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11294 {
11295 /* Don't make stubs to out-of-line register save/restore
11296 functions. Instead, emit copies of the functions. */
11297 stub_entry->group->needs_save_res = 1;
11298 stub_entry->stub_type = ppc_stub_save_res;
11299 return TRUE;
11300 }
11301
794e51c0
AM
11302 if (stub_entry->stub_type == ppc_stub_plt_call
11303 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11304 {
e054468f
AM
11305 asection *plt;
11306 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11307 if (off >= (bfd_vma) -2)
411e1bfb 11308 abort ();
33e44f2e 11309 plt = htab->elf.splt;
25f23106
AM
11310 if (!htab->elf.dynamic_sections_created
11311 || stub_entry->h == NULL
11312 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11313 plt = htab->elf.iplt;
e054468f
AM
11314 off += (plt->output_offset
11315 + plt->output_section->vma
11316 - elf_gp (plt->output_section->owner)
6f20ed8a 11317 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11318
794e51c0 11319 size = plt_stub_size (htab, stub_entry, off);
e7d1c40c 11320 if (htab->params->plt_stub_align)
794e51c0 11321 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11322 if (info->emitrelocations)
11323 {
6f20ed8a 11324 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11325 += ((PPC_HA (off) != 0)
11326 + (htab->opd_abi
e7d1c40c 11327 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11328 && PPC_HA (off + 16) == PPC_HA (off))
11329 : 1));
6f20ed8a 11330 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11331 }
721956f4
AM
11332 }
11333 else
11334 {
ad8e1ba5
AM
11335 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11336 variants. */
ac2df442 11337 bfd_vma r2off = 0;
6911b7dc 11338 bfd_vma local_off = 0;
ac2df442 11339
721956f4
AM
11340 off = (stub_entry->target_value
11341 + stub_entry->target_section->output_offset
11342 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11343 off -= (stub_entry->group->stub_sec->size
11344 + stub_entry->group->stub_sec->output_offset
11345 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11346
ad8e1ba5
AM
11347 /* Reset the stub type from the plt variant in case we now
11348 can reach with a shorter stub. */
11349 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11350 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11351
11352 size = 4;
11353 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11354 {
25f53a85 11355 r2off = get_r2off (info, stub_entry);
a7c49797 11356 if (r2off == (bfd_vma) -1)
aa374f67
AM
11357 {
11358 htab->stub_error = TRUE;
11359 return FALSE;
11360 }
a7c49797 11361 size = 8;
ac2df442 11362 if (PPC_HA (r2off) != 0)
a7c49797
AM
11363 size += 4;
11364 if (PPC_LO (r2off) != 0)
11365 size += 4;
ac2df442 11366 off -= size - 4;
ad8e1ba5
AM
11367 }
11368
6911b7dc
AM
11369 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11370
b9e5796b
AM
11371 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11372 Do the same for -R objects without function descriptors. */
11373 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11374 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11375 && r2off == 0
11376 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11377 {
11378 struct ppc_branch_hash_entry *br_entry;
11379
11380 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11381 stub_entry->root.string + 9,
b34976b6 11382 TRUE, FALSE);
721956f4
AM
11383 if (br_entry == NULL)
11384 {
8de848d8 11385 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 11386 stub_entry->root.string);
b34976b6
AM
11387 htab->stub_error = TRUE;
11388 return FALSE;
721956f4
AM
11389 }
11390
11391 if (br_entry->iter != htab->stub_iteration)
11392 {
11393 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11394 br_entry->offset = htab->brlt->size;
11395 htab->brlt->size += 8;
63bc6f6c 11396
ee75fd95 11397 if (htab->relbrlt != NULL)
eea6121a 11398 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11399 else if (info->emitrelocations)
11400 {
11401 htab->brlt->reloc_count += 1;
11402 htab->brlt->flags |= SEC_RELOC;
11403 }
721956f4 11404 }
ad8e1ba5
AM
11405
11406 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11407 off = (br_entry->offset
11408 + htab->brlt->output_offset
11409 + htab->brlt->output_section->vma
11410 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 11411 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11412
176a0d42
AM
11413 if (info->emitrelocations)
11414 {
6f20ed8a
AM
11415 stub_entry->group->stub_sec->reloc_count
11416 += 1 + (PPC_HA (off) != 0);
11417 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11418 }
11419
00f412ee 11420 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11421 {
11422 size = 12;
176a0d42 11423 if (PPC_HA (off) != 0)
ac2df442
AM
11424 size = 16;
11425 }
11426 else
11427 {
00f412ee 11428 size = 16;
176a0d42 11429 if (PPC_HA (off) != 0)
ac2df442
AM
11430 size += 4;
11431
11432 if (PPC_HA (r2off) != 0)
11433 size += 4;
00f412ee
AM
11434 if (PPC_LO (r2off) != 0)
11435 size += 4;
ac2df442 11436 }
721956f4 11437 }
84f5d08e
AM
11438 else if (info->emitrelocations)
11439 {
6f20ed8a
AM
11440 stub_entry->group->stub_sec->reloc_count += 1;
11441 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11442 }
721956f4
AM
11443 }
11444
6f20ed8a 11445 stub_entry->group->stub_sec->size += size;
b34976b6 11446 return TRUE;
721956f4
AM
11447}
11448
11449/* Set up various things so that we can make a list of input sections
11450 for each output section included in the link. Returns -1 on error,
cedb70c5 11451 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11452
11453int
e7d1c40c 11454ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11455{
6f20ed8a 11456 unsigned int id;
721956f4
AM
11457 bfd_size_type amt;
11458 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11459
4dfe6ac6
NC
11460 if (htab == NULL)
11461 return -1;
4c52953f 11462
6f20ed8a
AM
11463 htab->sec_info_arr_size = bfd_get_next_section_id ();
11464 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11465 htab->sec_info = bfd_zmalloc (amt);
11466 if (htab->sec_info == NULL)
721956f4
AM
11467 return -1;
11468
3d6f9012
AM
11469 /* Set toc_off for com, und, abs and ind sections. */
11470 for (id = 0; id < 3; id++)
6f20ed8a 11471 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11472
721956f4
AM
11473 return 1;
11474}
11475
927be08e
AM
11476/* Set up for first pass at multitoc partitioning. */
11477
11478void
11479ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11480{
11481 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11482
1c865ab2 11483 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11484 htab->toc_bfd = NULL;
11485 htab->toc_first_sec = NULL;
11486}
11487
e717da7e
AM
11488/* The linker repeatedly calls this function for each TOC input section
11489 and linker generated GOT section. Group input bfds such that the toc
927be08e 11490 within a group is less than 64k in size. */
ad8e1ba5 11491
927be08e 11492bfd_boolean
4ce794b7 11493ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11494{
11495 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11496 bfd_vma addr, off, limit;
ad8e1ba5 11497
4dfe6ac6
NC
11498 if (htab == NULL)
11499 return FALSE;
11500
927be08e 11501 if (!htab->second_toc_pass)
4c52953f 11502 {
927be08e 11503 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11504 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11505
11506 if (new_bfd)
bf102f86
AM
11507 {
11508 htab->toc_bfd = isec->owner;
11509 htab->toc_first_sec = isec;
11510 }
927be08e 11511
bf102f86
AM
11512 addr = isec->output_offset + isec->output_section->vma;
11513 off = addr - htab->toc_curr;
d77c8a4b
AM
11514 limit = 0x80008000;
11515 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11516 limit = 0x10000;
11517 if (off + isec->size > limit)
bf102f86
AM
11518 {
11519 addr = (htab->toc_first_sec->output_offset
11520 + htab->toc_first_sec->output_section->vma);
11521 htab->toc_curr = addr;
a27e685f 11522 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11523 }
99877b66 11524
927be08e
AM
11525 /* toc_curr is the base address of this toc group. Set elf_gp
11526 for the input section to be the offset relative to the
11527 output toc base plus 0x8000. Making the input elf_gp an
11528 offset allows us to move the toc as a whole without
11529 recalculating input elf_gp. */
11530 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11531 off += TOC_BASE_OFF;
11532
11533 /* Die if someone uses a linker script that doesn't keep input
11534 file .toc and .got together. */
a4fd3de5
AM
11535 if (new_bfd
11536 && elf_gp (isec->owner) != 0
927be08e
AM
11537 && elf_gp (isec->owner) != off)
11538 return FALSE;
11539
11540 elf_gp (isec->owner) = off;
11541 return TRUE;
4c52953f 11542 }
927be08e
AM
11543
11544 /* During the second pass toc_first_sec points to the start of
11545 a toc group, and toc_curr is used to track the old elf_gp.
11546 We use toc_bfd to ensure we only look at each bfd once. */
11547 if (htab->toc_bfd == isec->owner)
11548 return TRUE;
11549 htab->toc_bfd = isec->owner;
11550
11551 if (htab->toc_first_sec == NULL
11552 || htab->toc_curr != elf_gp (isec->owner))
11553 {
11554 htab->toc_curr = elf_gp (isec->owner);
11555 htab->toc_first_sec = isec;
11556 }
11557 addr = (htab->toc_first_sec->output_offset
11558 + htab->toc_first_sec->output_section->vma);
11559 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11560 elf_gp (isec->owner) = off;
11561
11562 return TRUE;
ad8e1ba5
AM
11563}
11564
927be08e
AM
11565/* Called via elf_link_hash_traverse to merge GOT entries for global
11566 symbol H. */
11567
11568static bfd_boolean
11569merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11570{
11571 if (h->root.type == bfd_link_hash_indirect)
11572 return TRUE;
11573
927be08e
AM
11574 merge_got_entries (&h->got.glist);
11575
11576 return TRUE;
11577}
11578
11579/* Called via elf_link_hash_traverse to allocate GOT entries for global
11580 symbol H. */
11581
11582static bfd_boolean
11583reallocate_got (struct elf_link_hash_entry *h, void *inf)
11584{
11585 struct got_entry *gent;
11586
11587 if (h->root.type == bfd_link_hash_indirect)
11588 return TRUE;
11589
927be08e
AM
11590 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11591 if (!gent->is_indirect)
11592 allocate_got (h, (struct bfd_link_info *) inf, gent);
11593 return TRUE;
11594}
11595
11596/* Called on the first multitoc pass after the last call to
11597 ppc64_elf_next_toc_section. This function removes duplicate GOT
11598 entries. */
11599
11600bfd_boolean
11601ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11602{
11603 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11604 struct bfd *ibfd, *ibfd2;
11605 bfd_boolean done_something;
11606
11607 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11608
7865406b
AM
11609 if (!htab->do_multi_toc)
11610 return FALSE;
11611
d0fae19d 11612 /* Merge global sym got entries within a toc group. */
927be08e
AM
11613 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11614
11615 /* And tlsld_got. */
c72f2fb2 11616 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11617 {
11618 struct got_entry *ent, *ent2;
11619
11620 if (!is_ppc64_elf (ibfd))
11621 continue;
11622
11623 ent = ppc64_tlsld_got (ibfd);
11624 if (!ent->is_indirect
11625 && ent->got.offset != (bfd_vma) -1)
11626 {
c72f2fb2 11627 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11628 {
11629 if (!is_ppc64_elf (ibfd2))
11630 continue;
11631
11632 ent2 = ppc64_tlsld_got (ibfd2);
11633 if (!ent2->is_indirect
11634 && ent2->got.offset != (bfd_vma) -1
11635 && elf_gp (ibfd2) == elf_gp (ibfd))
11636 {
11637 ent2->is_indirect = TRUE;
11638 ent2->got.ent = ent;
11639 }
11640 }
11641 }
11642 }
11643
11644 /* Zap sizes of got sections. */
33e44f2e
AM
11645 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11646 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11647 htab->got_reli_size = 0;
11648
c72f2fb2 11649 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11650 {
11651 asection *got, *relgot;
11652
11653 if (!is_ppc64_elf (ibfd))
11654 continue;
11655
11656 got = ppc64_elf_tdata (ibfd)->got;
11657 if (got != NULL)
11658 {
11659 got->rawsize = got->size;
11660 got->size = 0;
11661 relgot = ppc64_elf_tdata (ibfd)->relgot;
11662 relgot->rawsize = relgot->size;
11663 relgot->size = 0;
11664 }
11665 }
11666
11667 /* Now reallocate the got, local syms first. We don't need to
11668 allocate section contents again since we never increase size. */
c72f2fb2 11669 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11670 {
11671 struct got_entry **lgot_ents;
11672 struct got_entry **end_lgot_ents;
11673 struct plt_entry **local_plt;
11674 struct plt_entry **end_local_plt;
f961d9dd 11675 unsigned char *lgot_masks;
927be08e
AM
11676 bfd_size_type locsymcount;
11677 Elf_Internal_Shdr *symtab_hdr;
19e08130 11678 asection *s;
927be08e
AM
11679
11680 if (!is_ppc64_elf (ibfd))
11681 continue;
11682
11683 lgot_ents = elf_local_got_ents (ibfd);
11684 if (!lgot_ents)
11685 continue;
11686
11687 symtab_hdr = &elf_symtab_hdr (ibfd);
11688 locsymcount = symtab_hdr->sh_info;
11689 end_lgot_ents = lgot_ents + locsymcount;
11690 local_plt = (struct plt_entry **) end_lgot_ents;
11691 end_local_plt = local_plt + locsymcount;
f961d9dd 11692 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11693 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11694 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11695 {
11696 struct got_entry *ent;
11697
11698 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11699 {
19e08130
AM
11700 unsigned int ent_size = 8;
11701 unsigned int rel_size = sizeof (Elf64_External_Rela);
11702
d0fae19d
AM
11703 ent->got.offset = s->size;
11704 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11705 {
19e08130
AM
11706 ent_size *= 2;
11707 rel_size *= 2;
11708 }
11709 s->size += ent_size;
11710 if ((*lgot_masks & PLT_IFUNC) != 0)
11711 {
33e44f2e 11712 htab->elf.irelplt->size += rel_size;
19e08130
AM
11713 htab->got_reli_size += rel_size;
11714 }
0e1862bb 11715 else if (bfd_link_pic (info))
19e08130
AM
11716 {
11717 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11718 srel->size += rel_size;
d0fae19d
AM
11719 }
11720 }
927be08e
AM
11721 }
11722 }
11723
11724 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11725
c72f2fb2 11726 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11727 {
11728 struct got_entry *ent;
11729
11730 if (!is_ppc64_elf (ibfd))
11731 continue;
11732
11733 ent = ppc64_tlsld_got (ibfd);
11734 if (!ent->is_indirect
11735 && ent->got.offset != (bfd_vma) -1)
11736 {
11737 asection *s = ppc64_elf_tdata (ibfd)->got;
11738 ent->got.offset = s->size;
11739 s->size += 16;
0e1862bb 11740 if (bfd_link_pic (info))
927be08e
AM
11741 {
11742 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11743 srel->size += sizeof (Elf64_External_Rela);
11744 }
11745 }
11746 }
11747
33e44f2e 11748 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11749 if (!done_something)
c72f2fb2 11750 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11751 {
11752 asection *got;
11753
11754 if (!is_ppc64_elf (ibfd))
11755 continue;
11756
11757 got = ppc64_elf_tdata (ibfd)->got;
11758 if (got != NULL)
11759 {
11760 done_something = got->rawsize != got->size;
11761 if (done_something)
11762 break;
11763 }
11764 }
11765
11766 if (done_something)
e7d1c40c 11767 (*htab->params->layout_sections_again) ();
927be08e
AM
11768
11769 /* Set up for second pass over toc sections to recalculate elf_gp
11770 on input sections. */
11771 htab->toc_bfd = NULL;
11772 htab->toc_first_sec = NULL;
11773 htab->second_toc_pass = TRUE;
11774 return done_something;
11775}
11776
11777/* Called after second pass of multitoc partitioning. */
11778
11779void
11780ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11781{
11782 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11783
11784 /* After the second pass, toc_curr tracks the TOC offset used
11785 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11786 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11787}
11788
9b5ecbd0
AM
11789/* No toc references were found in ISEC. If the code in ISEC makes no
11790 calls, then there's no need to use toc adjusting stubs when branching
11791 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11792 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11793 needed, and 2 if a cyclical call-graph was found but no other reason
11794 for a stub was detected. If called from the top level, a return of
11795 2 means the same as a return of 0. */
9b5ecbd0
AM
11796
11797static int
4ce794b7 11798toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11799{
9b5ecbd0 11800 int ret;
70cc837d
AM
11801
11802 /* Mark this section as checked. */
11803 isec->call_check_done = 1;
9b5ecbd0 11804
772119ce
AM
11805 /* We know none of our code bearing sections will need toc stubs. */
11806 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11807 return 0;
11808
eea6121a 11809 if (isec->size == 0)
082c50f8
AM
11810 return 0;
11811
4c52953f
AM
11812 if (isec->output_section == NULL)
11813 return 0;
11814
4c52953f 11815 ret = 0;
70cc837d 11816 if (isec->reloc_count != 0)
9b5ecbd0 11817 {
70cc837d
AM
11818 Elf_Internal_Rela *relstart, *rel;
11819 Elf_Internal_Sym *local_syms;
11820 struct ppc_link_hash_table *htab;
2917689a 11821
70cc837d
AM
11822 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11823 info->keep_memory);
11824 if (relstart == NULL)
11825 return -1;
90aecf7a 11826
70cc837d
AM
11827 /* Look for branches to outside of this section. */
11828 local_syms = NULL;
11829 htab = ppc_hash_table (info);
11830 if (htab == NULL)
11831 return -1;
4c52953f 11832
70cc837d 11833 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11834 {
70cc837d
AM
11835 enum elf_ppc64_reloc_type r_type;
11836 unsigned long r_symndx;
11837 struct elf_link_hash_entry *h;
11838 struct ppc_link_hash_entry *eh;
11839 Elf_Internal_Sym *sym;
11840 asection *sym_sec;
11841 struct _opd_sec_data *opd;
11842 bfd_vma sym_value;
11843 bfd_vma dest;
11844
11845 r_type = ELF64_R_TYPE (rel->r_info);
11846 if (r_type != R_PPC64_REL24
11847 && r_type != R_PPC64_REL14
11848 && r_type != R_PPC64_REL14_BRTAKEN
11849 && r_type != R_PPC64_REL14_BRNTAKEN)
11850 continue;
4c52953f 11851
70cc837d
AM
11852 r_symndx = ELF64_R_SYM (rel->r_info);
11853 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11854 isec->owner))
4c52953f 11855 {
70cc837d
AM
11856 ret = -1;
11857 break;
11858 }
4c52953f 11859
70cc837d
AM
11860 /* Calls to dynamic lib functions go through a plt call stub
11861 that uses r2. */
11862 eh = (struct ppc_link_hash_entry *) h;
11863 if (eh != NULL
11864 && (eh->elf.plt.plist != NULL
11865 || (eh->oh != NULL
11866 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11867 {
11868 ret = 1;
11869 break;
4c52953f
AM
11870 }
11871
70cc837d
AM
11872 if (sym_sec == NULL)
11873 /* Ignore other undefined symbols. */
4c52953f 11874 continue;
4c52953f 11875
70cc837d
AM
11876 /* Assume branches to other sections not included in the
11877 link need stubs too, to cover -R and absolute syms. */
11878 if (sym_sec->output_section == NULL)
11879 {
11880 ret = 1;
11881 break;
11882 }
4c52953f 11883
70cc837d
AM
11884 if (h == NULL)
11885 sym_value = sym->st_value;
11886 else
11887 {
11888 if (h->root.type != bfd_link_hash_defined
11889 && h->root.type != bfd_link_hash_defweak)
11890 abort ();
11891 sym_value = h->root.u.def.value;
11892 }
11893 sym_value += rel->r_addend;
4c52953f 11894
70cc837d
AM
11895 /* If this branch reloc uses an opd sym, find the code section. */
11896 opd = get_opd_info (sym_sec);
11897 if (opd != NULL)
11898 {
11899 if (h == NULL && opd->adjust != NULL)
11900 {
11901 long adjust;
4c52953f 11902
92a9c616 11903 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11904 if (adjust == -1)
11905 /* Assume deleted functions won't ever be called. */
11906 continue;
11907 sym_value += adjust;
11908 }
4c52953f 11909
aef36ac1
AM
11910 dest = opd_entry_value (sym_sec, sym_value,
11911 &sym_sec, NULL, FALSE);
70cc837d
AM
11912 if (dest == (bfd_vma) -1)
11913 continue;
11914 }
11915 else
11916 dest = (sym_value
11917 + sym_sec->output_offset
11918 + sym_sec->output_section->vma);
4c52953f 11919
70cc837d
AM
11920 /* Ignore branch to self. */
11921 if (sym_sec == isec)
11922 continue;
4c52953f 11923
70cc837d
AM
11924 /* If the called function uses the toc, we need a stub. */
11925 if (sym_sec->has_toc_reloc
11926 || sym_sec->makes_toc_func_call)
4c52953f 11927 {
70cc837d 11928 ret = 1;
4c52953f
AM
11929 break;
11930 }
70cc837d
AM
11931
11932 /* Assume any branch that needs a long branch stub might in fact
11933 need a plt_branch stub. A plt_branch stub uses r2. */
11934 else if (dest - (isec->output_offset
11935 + isec->output_section->vma
6911b7dc
AM
11936 + rel->r_offset) + (1 << 25)
11937 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11938 ? h->other
11939 : sym->st_other))
4c52953f 11940 {
70cc837d
AM
11941 ret = 1;
11942 break;
11943 }
11944
11945 /* If calling back to a section in the process of being
11946 tested, we can't say for sure that no toc adjusting stubs
11947 are needed, so don't return zero. */
11948 else if (sym_sec->call_check_in_progress)
11949 ret = 2;
11950
11951 /* Branches to another section that itself doesn't have any TOC
11952 references are OK. Recursively call ourselves to check. */
11953 else if (!sym_sec->call_check_done)
11954 {
11955 int recur;
11956
11957 /* Mark current section as indeterminate, so that other
11958 sections that call back to current won't be marked as
11959 known. */
11960 isec->call_check_in_progress = 1;
11961 recur = toc_adjusting_stub_needed (info, sym_sec);
11962 isec->call_check_in_progress = 0;
11963
4c52953f
AM
11964 if (recur != 0)
11965 {
70cc837d
AM
11966 ret = recur;
11967 if (recur != 2)
11968 break;
4c52953f
AM
11969 }
11970 }
4c52953f 11971 }
70cc837d
AM
11972
11973 if (local_syms != NULL
11974 && (elf_symtab_hdr (isec->owner).contents
11975 != (unsigned char *) local_syms))
11976 free (local_syms);
11977 if (elf_section_data (isec)->relocs != relstart)
11978 free (relstart);
9b5ecbd0
AM
11979 }
11980
70cc837d
AM
11981 if ((ret & 1) == 0
11982 && isec->map_head.s != NULL
11983 && (strcmp (isec->output_section->name, ".init") == 0
11984 || strcmp (isec->output_section->name, ".fini") == 0))
11985 {
11986 if (isec->map_head.s->has_toc_reloc
11987 || isec->map_head.s->makes_toc_func_call)
11988 ret = 1;
11989 else if (!isec->map_head.s->call_check_done)
11990 {
11991 int recur;
11992 isec->call_check_in_progress = 1;
11993 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11994 isec->call_check_in_progress = 0;
11995 if (recur != 0)
11996 ret = recur;
11997 }
11998 }
11999
12000 if (ret == 1)
12001 isec->makes_toc_func_call = 1;
4c52953f 12002
9b5ecbd0
AM
12003 return ret;
12004}
12005
721956f4
AM
12006/* The linker repeatedly calls this function for each input section,
12007 in the order that input sections are linked into output sections.
12008 Build lists of input sections to determine groupings between which
12009 we may insert linker stubs. */
12010
9b5ecbd0 12011bfd_boolean
4ce794b7 12012ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12013{
12014 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12015
4dfe6ac6
NC
12016 if (htab == NULL)
12017 return FALSE;
12018
734b6cf9 12019 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12020 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12021 {
3d6f9012
AM
12022 /* This happens to make the list in reverse order,
12023 which is what we want. */
6f20ed8a
AM
12024 htab->sec_info[isec->id].u.list
12025 = htab->sec_info[isec->output_section->id].u.list;
12026 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12027 }
ad8e1ba5 12028
4c52953f 12029 if (htab->multi_toc_needed)
9b5ecbd0 12030 {
8b974ba3
AM
12031 /* Analyse sections that aren't already flagged as needing a
12032 valid toc pointer. Exclude .fixup for the linux kernel.
12033 .fixup contains branches, but only back to the function that
12034 hit an exception. */
12035 if (!(isec->has_toc_reloc
12036 || (isec->flags & SEC_CODE) == 0
12037 || strcmp (isec->name, ".fixup") == 0
12038 || isec->call_check_done))
12039 {
12040 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12041 return FALSE;
8b974ba3
AM
12042 }
12043 /* Make all sections use the TOC assigned for this object file.
12044 This will be wrong for pasted sections; We fix that in
12045 check_pasted_section(). */
12046 if (elf_gp (isec->owner) != 0)
12047 htab->toc_curr = elf_gp (isec->owner);
12048 }
12049
6f20ed8a 12050 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12051 return TRUE;
721956f4
AM
12052}
12053
70cc837d
AM
12054/* Check that all .init and .fini sections use the same toc, if they
12055 have toc relocs. */
12056
12057static bfd_boolean
12058check_pasted_section (struct bfd_link_info *info, const char *name)
12059{
12060 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12061
12062 if (o != NULL)
12063 {
12064 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12065 bfd_vma toc_off = 0;
12066 asection *i;
12067
12068 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12069 if (i->has_toc_reloc)
12070 {
12071 if (toc_off == 0)
6f20ed8a
AM
12072 toc_off = htab->sec_info[i->id].toc_off;
12073 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12074 return FALSE;
12075 }
6683a28d
AM
12076
12077 if (toc_off == 0)
12078 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12079 if (i->makes_toc_func_call)
12080 {
6f20ed8a 12081 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12082 break;
12083 }
12084
70cc837d
AM
12085 /* Make sure the whole pasted function uses the same toc offset. */
12086 if (toc_off != 0)
12087 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12088 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12089 }
12090 return TRUE;
12091}
12092
12093bfd_boolean
12094ppc64_elf_check_init_fini (struct bfd_link_info *info)
12095{
12096 return (check_pasted_section (info, ".init")
12097 & check_pasted_section (info, ".fini"));
12098}
12099
721956f4
AM
12100/* See whether we can group stub sections together. Grouping stub
12101 sections may result in fewer stubs. More importantly, we need to
12102 put all .init* and .fini* stubs at the beginning of the .init or
12103 .fini output sections respectively, because glibc splits the
12104 _init and _fini functions into multiple parts. Putting a stub in
12105 the middle of a function is not a good idea. */
12106
6f20ed8a
AM
12107static bfd_boolean
12108group_sections (struct bfd_link_info *info,
4ce794b7
AM
12109 bfd_size_type stub_group_size,
12110 bfd_boolean stubs_always_before_branch)
721956f4 12111{
6f20ed8a
AM
12112 struct ppc_link_hash_table *htab;
12113 asection *osec;
7c8fe5c4
AM
12114 bfd_boolean suppress_size_errors;
12115
6f20ed8a
AM
12116 htab = ppc_hash_table (info);
12117 if (htab == NULL)
12118 return FALSE;
12119
7c8fe5c4 12120 suppress_size_errors = FALSE;
7c8fe5c4
AM
12121 if (stub_group_size == 1)
12122 {
12123 /* Default values. */
12124 if (stubs_always_before_branch)
09f92717 12125 stub_group_size = 0x1e00000;
7c8fe5c4 12126 else
09f92717 12127 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12128 suppress_size_errors = TRUE;
12129 }
12130
6f20ed8a 12131 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12132 {
6f20ed8a
AM
12133 asection *tail;
12134
12135 if (osec->id >= htab->sec_info_arr_size)
12136 continue;
12137
12138 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12139 while (tail != NULL)
721956f4 12140 {
734b6cf9
AM
12141 asection *curr;
12142 asection *prev;
12143 bfd_size_type total;
12144 bfd_boolean big_sec;
12145 bfd_vma curr_toc;
6f20ed8a 12146 struct map_stub *group;
09f92717 12147 bfd_size_type group_size;
734b6cf9
AM
12148
12149 curr = tail;
eea6121a 12150 total = tail->size;
09f92717
AM
12151 group_size = (ppc64_elf_section_data (tail) != NULL
12152 && ppc64_elf_section_data (tail)->has_14bit_branch
12153 ? stub_group_size >> 10 : stub_group_size);
12154
12155 big_sec = total > group_size;
7c8fe5c4 12156 if (big_sec && !suppress_size_errors)
695344c0 12157 /* xgettext:c-format */
4eca0228
AM
12158 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12159 tail->owner, tail);
6f20ed8a 12160 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12161
6f20ed8a 12162 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12163 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12164 < (ppc64_elf_section_data (prev) != NULL
12165 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12166 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12167 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12168 curr = prev;
12169
12170 /* OK, the size from the start of CURR to the end is less
09f92717 12171 than group_size and thus can be handled by one stub
734b6cf9 12172 section. (or the tail section is itself larger than
09f92717
AM
12173 group_size, in which case we may be toast.) We should
12174 really be keeping track of the total size of stubs added
12175 here, as stubs contribute to the final output section
12176 size. That's a little tricky, and this way will only
12177 break if stubs added make the total size more than 2^25,
12178 ie. for the default stub_group_size, if stubs total more
12179 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12180 group = bfd_alloc (curr->owner, sizeof (*group));
12181 if (group == NULL)
12182 return FALSE;
12183 group->link_sec = curr;
12184 group->stub_sec = NULL;
a4b6fadd
AM
12185 group->needs_save_res = 0;
12186 group->next = htab->group;
12187 htab->group = group;
734b6cf9 12188 do
721956f4 12189 {
6f20ed8a 12190 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12191 /* Set up this stub group. */
6f20ed8a 12192 htab->sec_info[tail->id].u.group = group;
721956f4 12193 }
734b6cf9
AM
12194 while (tail != curr && (tail = prev) != NULL);
12195
09f92717 12196 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12197 bytes before the stub section can be handled by it too.
12198 Don't do this if we have a really large section after the
12199 stubs, as adding more stubs increases the chance that
12200 branches may not reach into the stub section. */
12201 if (!stubs_always_before_branch && !big_sec)
12202 {
12203 total = 0;
12204 while (prev != NULL
12205 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12206 < (ppc64_elf_section_data (prev) != NULL
12207 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12208 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12209 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12210 {
12211 tail = prev;
6f20ed8a
AM
12212 prev = htab->sec_info[tail->id].u.list;
12213 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12214 }
12215 }
12216 tail = prev;
721956f4
AM
12217 }
12218 }
6f20ed8a 12219 return TRUE;
721956f4
AM
12220}
12221
58d180e8
AM
12222static const unsigned char glink_eh_frame_cie[] =
12223{
12224 0, 0, 0, 16, /* length. */
12225 0, 0, 0, 0, /* id. */
12226 1, /* CIE version. */
12227 'z', 'R', 0, /* Augmentation string. */
12228 4, /* Code alignment. */
12229 0x78, /* Data alignment. */
12230 65, /* RA reg. */
12231 1, /* Augmentation size. */
12232 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
da44f4e5
AM
12233 DW_CFA_def_cfa, 1, 0, /* def_cfa: r1 offset 0. */
12234 0, 0, 0, 0
58d180e8
AM
12235};
12236
d969d15f
AM
12237/* Stripping output sections is normally done before dynamic section
12238 symbols have been allocated. This function is called later, and
12239 handles cases like htab->brlt which is mapped to its own output
12240 section. */
12241
12242static void
12243maybe_strip_output (struct bfd_link_info *info, asection *isec)
12244{
12245 if (isec->size == 0
12246 && isec->output_section->size == 0
53d8967a 12247 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12248 && !bfd_section_removed_from_list (info->output_bfd,
12249 isec->output_section)
12250 && elf_section_data (isec->output_section)->dynindx == 0)
12251 {
12252 isec->output_section->flags |= SEC_EXCLUDE;
12253 bfd_section_list_remove (info->output_bfd, isec->output_section);
12254 info->output_bfd->section_count--;
12255 }
12256}
12257
721956f4
AM
12258/* Determine and set the size of the stub section for a final link.
12259
12260 The basic idea here is to examine all the relocations looking for
12261 PC-relative calls to a target that is unreachable with a "bl"
12262 instruction. */
12263
b34976b6 12264bfd_boolean
e7d1c40c 12265ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12266{
12267 bfd_size_type stub_group_size;
b34976b6 12268 bfd_boolean stubs_always_before_branch;
721956f4
AM
12269 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12270
4dfe6ac6
NC
12271 if (htab == NULL)
12272 return FALSE;
12273
0e1862bb 12274 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12275 htab->params->plt_thread_safe = 1;
b9e5796b 12276 if (!htab->opd_abi)
e7d1c40c
AM
12277 htab->params->plt_thread_safe = 0;
12278 else if (htab->params->plt_thread_safe == -1)
794e51c0 12279 {
e2458743 12280 static const char *const thread_starter[] =
794e51c0
AM
12281 {
12282 "pthread_create",
12283 /* libstdc++ */
12284 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12285 /* librt */
12286 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12287 "mq_notify", "create_timer",
12288 /* libanl */
12289 "getaddrinfo_a",
12290 /* libgomp */
2300b5a1 12291 "GOMP_parallel",
794e51c0 12292 "GOMP_parallel_start",
2300b5a1 12293 "GOMP_parallel_loop_static",
794e51c0 12294 "GOMP_parallel_loop_static_start",
2300b5a1 12295 "GOMP_parallel_loop_dynamic",
794e51c0 12296 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12297 "GOMP_parallel_loop_guided",
794e51c0 12298 "GOMP_parallel_loop_guided_start",
2300b5a1 12299 "GOMP_parallel_loop_runtime",
794e51c0 12300 "GOMP_parallel_loop_runtime_start",
2300b5a1 12301 "GOMP_parallel_sections",
68ffbac6 12302 "GOMP_parallel_sections_start",
f9dffbf0
AM
12303 /* libgo */
12304 "__go_go",
794e51c0
AM
12305 };
12306 unsigned i;
12307
a4b6fadd 12308 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12309 {
12310 struct elf_link_hash_entry *h;
12311 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12312 FALSE, FALSE, TRUE);
e7d1c40c
AM
12313 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12314 if (htab->params->plt_thread_safe)
794e51c0
AM
12315 break;
12316 }
12317 }
e7d1c40c
AM
12318 stubs_always_before_branch = htab->params->group_size < 0;
12319 if (htab->params->group_size < 0)
12320 stub_group_size = -htab->params->group_size;
721956f4 12321 else
e7d1c40c 12322 stub_group_size = htab->params->group_size;
721956f4 12323
6f20ed8a
AM
12324 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12325 return FALSE;
721956f4 12326
c9301e31
AM
12327#define STUB_SHRINK_ITER 20
12328 /* Loop until no stubs added. After iteration 20 of this loop we may
12329 exit on a stub section shrinking. This is to break out of a
12330 pathological case where adding stubs on one iteration decreases
12331 section gaps (perhaps due to alignment), which then requires
12332 fewer or smaller stubs on the next iteration. */
12333
721956f4
AM
12334 while (1)
12335 {
12336 bfd *input_bfd;
12337 unsigned int bfd_indx;
a4b6fadd 12338 struct map_stub *group;
721956f4 12339 asection *stub_sec;
721956f4
AM
12340
12341 htab->stub_iteration += 1;
721956f4
AM
12342
12343 for (input_bfd = info->input_bfds, bfd_indx = 0;
12344 input_bfd != NULL;
c72f2fb2 12345 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12346 {
12347 Elf_Internal_Shdr *symtab_hdr;
12348 asection *section;
6cdc0ccc 12349 Elf_Internal_Sym *local_syms = NULL;
721956f4 12350
0c8d6e5c 12351 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12352 continue;
12353
721956f4 12354 /* We'll need the symbol table in a second. */
0ffa91dd 12355 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12356 if (symtab_hdr->sh_info == 0)
12357 continue;
12358
721956f4
AM
12359 /* Walk over each section attached to the input bfd. */
12360 for (section = input_bfd->sections;
12361 section != NULL;
12362 section = section->next)
12363 {
721956f4 12364 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12365
12366 /* If there aren't any relocs, then there's nothing more
12367 to do. */
12368 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12369 || (section->flags & SEC_ALLOC) == 0
12370 || (section->flags & SEC_LOAD) == 0
12371 || (section->flags & SEC_CODE) == 0
721956f4
AM
12372 || section->reloc_count == 0)
12373 continue;
12374
12375 /* If this section is a link-once section that will be
12376 discarded, then don't create any stubs. */
12377 if (section->output_section == NULL
927be08e 12378 || section->output_section->owner != info->output_bfd)
721956f4
AM
12379 continue;
12380
1e2f5b6e
AM
12381 /* Get the relocs. */
12382 internal_relocs
4ce794b7 12383 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12384 info->keep_memory);
721956f4 12385 if (internal_relocs == NULL)
1e2f5b6e 12386 goto error_ret_free_local;
721956f4
AM
12387
12388 /* Now examine each relocation. */
12389 irela = internal_relocs;
12390 irelaend = irela + section->reloc_count;
12391 for (; irela < irelaend; irela++)
12392 {
4ce794b7
AM
12393 enum elf_ppc64_reloc_type r_type;
12394 unsigned int r_indx;
721956f4
AM
12395 enum ppc_stub_type stub_type;
12396 struct ppc_stub_hash_entry *stub_entry;
8387904d 12397 asection *sym_sec, *code_sec;
e054468f 12398 bfd_vma sym_value, code_value;
721956f4 12399 bfd_vma destination;
6911b7dc 12400 unsigned long local_off;
8843416a 12401 bfd_boolean ok_dest;
721956f4 12402 struct ppc_link_hash_entry *hash;
8387904d 12403 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12404 struct elf_link_hash_entry *h;
12405 Elf_Internal_Sym *sym;
721956f4
AM
12406 char *stub_name;
12407 const asection *id_sec;
74f0fb50 12408 struct _opd_sec_data *opd;
e054468f 12409 struct plt_entry *plt_ent;
721956f4
AM
12410
12411 r_type = ELF64_R_TYPE (irela->r_info);
12412 r_indx = ELF64_R_SYM (irela->r_info);
12413
4ce794b7 12414 if (r_type >= R_PPC64_max)
721956f4
AM
12415 {
12416 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12417 goto error_ret_free_internal;
721956f4
AM
12418 }
12419
12420 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12421 if (r_type != R_PPC64_REL24
12422 && r_type != R_PPC64_REL14
12423 && r_type != R_PPC64_REL14_BRTAKEN
12424 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12425 continue;
12426
12427 /* Now determine the call target, its name, value,
12428 section. */
411e1bfb
AM
12429 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12430 r_indx, input_bfd))
12431 goto error_ret_free_internal;
12432 hash = (struct ppc_link_hash_entry *) h;
12433
8843416a 12434 ok_dest = FALSE;
8387904d 12435 fdh = NULL;
7fe2b9a6 12436 sym_value = 0;
411e1bfb 12437 if (hash == NULL)
721956f4 12438 {
411e1bfb 12439 sym_value = sym->st_value;
c27b8c2a
AM
12440 if (sym_sec != NULL
12441 && sym_sec->output_section != NULL)
12442 ok_dest = TRUE;
721956f4 12443 }
7fe2b9a6
AM
12444 else if (hash->elf.root.type == bfd_link_hash_defined
12445 || hash->elf.root.type == bfd_link_hash_defweak)
12446 {
12447 sym_value = hash->elf.root.u.def.value;
12448 if (sym_sec->output_section != NULL)
12449 ok_dest = TRUE;
12450 }
12451 else if (hash->elf.root.type == bfd_link_hash_undefweak
12452 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12453 {
99877b66 12454 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12455 use the func descriptor sym instead if it is
12456 defined. */
ceb1f1ef 12457 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12458 && hash->oh != NULL)
8387904d 12459 {
8c5b4e52 12460 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12461 if (fdh->elf.root.type == bfd_link_hash_defined
12462 || fdh->elf.root.type == bfd_link_hash_defweak)
12463 {
12464 sym_sec = fdh->elf.root.u.def.section;
12465 sym_value = fdh->elf.root.u.def.value;
12466 if (sym_sec->output_section != NULL)
12467 ok_dest = TRUE;
12468 }
99877b66
AM
12469 else
12470 fdh = NULL;
8387904d 12471 }
7fe2b9a6
AM
12472 }
12473 else
12474 {
12475 bfd_set_error (bfd_error_bad_value);
12476 goto error_ret_free_internal;
721956f4
AM
12477 }
12478
8843416a 12479 destination = 0;
6911b7dc 12480 local_off = 0;
8843416a
AM
12481 if (ok_dest)
12482 {
12483 sym_value += irela->r_addend;
12484 destination = (sym_value
12485 + sym_sec->output_offset
12486 + sym_sec->output_section->vma);
6911b7dc
AM
12487 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12488 ? hash->elf.other
12489 : sym->st_other);
8843416a
AM
12490 }
12491
8387904d 12492 code_sec = sym_sec;
e054468f 12493 code_value = sym_value;
74f0fb50
AM
12494 opd = get_opd_info (sym_sec);
12495 if (opd != NULL)
8387904d
AM
12496 {
12497 bfd_vma dest;
12498
74f0fb50 12499 if (hash == NULL && opd->adjust != NULL)
8387904d 12500 {
51aecdc5 12501 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12502 if (adjust == -1)
12503 continue;
e054468f 12504 code_value += adjust;
8387904d
AM
12505 sym_value += adjust;
12506 }
12507 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12508 &code_sec, &code_value, FALSE);
8387904d
AM
12509 if (dest != (bfd_vma) -1)
12510 {
12511 destination = dest;
12512 if (fdh != NULL)
12513 {
12514 /* Fixup old ABI sym to point at code
12515 entry. */
99877b66 12516 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12517 hash->elf.root.u.def.section = code_sec;
e054468f 12518 hash->elf.root.u.def.value = code_value;
8387904d
AM
12519 }
12520 }
12521 }
12522
721956f4 12523 /* Determine what (if any) linker stub is needed. */
e054468f 12524 plt_ent = NULL;
721956f4 12525 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12526 &plt_ent, destination,
12527 local_off);
ad8e1ba5
AM
12528
12529 if (stub_type != ppc_stub_plt_call)
12530 {
12531 /* Check whether we need a TOC adjusting stub.
12532 Since the linker pastes together pieces from
12533 different object files when creating the
12534 _init and _fini functions, it may be that a
12535 call to what looks like a local sym is in
12536 fact a call needing a TOC adjustment. */
8387904d
AM
12537 if (code_sec != NULL
12538 && code_sec->output_section != NULL
6f20ed8a
AM
12539 && (htab->sec_info[code_sec->id].toc_off
12540 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12541 && (code_sec->has_toc_reloc
12542 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12543 stub_type = ppc_stub_long_branch_r2off;
12544 }
12545
721956f4
AM
12546 if (stub_type == ppc_stub_none)
12547 continue;
12548
411e1bfb
AM
12549 /* __tls_get_addr calls might be eliminated. */
12550 if (stub_type != ppc_stub_plt_call
12551 && hash != NULL
8387904d
AM
12552 && (hash == htab->tls_get_addr
12553 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12554 && section->has_tls_reloc
12555 && irela != internal_relocs)
12556 {
12557 /* Get tls info. */
f961d9dd 12558 unsigned char *tls_mask;
411e1bfb 12559
3a71aa26 12560 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12561 irela - 1, input_bfd))
12562 goto error_ret_free_internal;
e7b938ca 12563 if (*tls_mask != 0)
411e1bfb
AM
12564 continue;
12565 }
12566
3b421ab3
AM
12567 if (stub_type == ppc_stub_plt_call
12568 && irela + 1 < irelaend
12569 && irela[1].r_offset == irela->r_offset + 4
794e51c0
AM
12570 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12571 {
12572 if (!tocsave_find (htab, INSERT,
12573 &local_syms, irela + 1, input_bfd))
12574 goto error_ret_free_internal;
12575 }
12576 else if (stub_type == ppc_stub_plt_call)
12577 stub_type = ppc_stub_plt_call_r2save;
3b421ab3 12578
721956f4 12579 /* Support for grouping stub sections. */
6f20ed8a 12580 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12581
12582 /* Get the name of this stub. */
12583 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12584 if (!stub_name)
12585 goto error_ret_free_internal;
12586
12587 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12588 stub_name, FALSE, FALSE);
721956f4
AM
12589 if (stub_entry != NULL)
12590 {
12591 /* The proper stub has already been created. */
12592 free (stub_name);
794e51c0
AM
12593 if (stub_type == ppc_stub_plt_call_r2save)
12594 stub_entry->stub_type = stub_type;
721956f4
AM
12595 continue;
12596 }
12597
25f53a85 12598 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12599 if (stub_entry == NULL)
12600 {
12601 free (stub_name);
6cdc0ccc
AM
12602 error_ret_free_internal:
12603 if (elf_section_data (section)->relocs == NULL)
12604 free (internal_relocs);
12605 error_ret_free_local:
12606 if (local_syms != NULL
12607 && (symtab_hdr->contents
12608 != (unsigned char *) local_syms))
12609 free (local_syms);
b34976b6 12610 return FALSE;
721956f4
AM
12611 }
12612
ad8e1ba5 12613 stub_entry->stub_type = stub_type;
794e51c0
AM
12614 if (stub_type != ppc_stub_plt_call
12615 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12616 {
12617 stub_entry->target_value = code_value;
12618 stub_entry->target_section = code_sec;
12619 }
12620 else
12621 {
12622 stub_entry->target_value = sym_value;
12623 stub_entry->target_section = sym_sec;
12624 }
721956f4 12625 stub_entry->h = hash;
e054468f 12626 stub_entry->plt_ent = plt_ent;
6911b7dc 12627 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12628
12629 if (stub_entry->h != NULL)
12630 htab->stub_globals += 1;
721956f4
AM
12631 }
12632
12633 /* We're done with the internal relocs, free them. */
6cdc0ccc 12634 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12635 free (internal_relocs);
721956f4 12636 }
6cdc0ccc
AM
12637
12638 if (local_syms != NULL
12639 && symtab_hdr->contents != (unsigned char *) local_syms)
12640 {
12641 if (!info->keep_memory)
12642 free (local_syms);
12643 else
12644 symtab_hdr->contents = (unsigned char *) local_syms;
12645 }
721956f4
AM
12646 }
12647
5c3dead3 12648 /* We may have added some stubs. Find out the new size of the
721956f4 12649 stub sections. */
e7d1c40c 12650 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
12651 stub_sec != NULL;
12652 stub_sec = stub_sec->next)
e717da7e 12653 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 12654 {
ea3d7d1c
AM
12655 if (htab->stub_iteration <= STUB_SHRINK_ITER
12656 || stub_sec->rawsize < stub_sec->size)
12657 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12658 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12659 stub_sec->size = 0;
12660 stub_sec->reloc_count = 0;
84f5d08e 12661 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12662 }
eea6121a
AM
12663
12664 htab->brlt->size = 0;
84f5d08e
AM
12665 htab->brlt->reloc_count = 0;
12666 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12667 if (htab->relbrlt != NULL)
eea6121a 12668 htab->relbrlt->size = 0;
721956f4 12669
63bc6f6c 12670 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12671
a4b6fadd
AM
12672 for (group = htab->group; group != NULL; group = group->next)
12673 if (group->needs_save_res)
12674 group->stub_sec->size += htab->sfpr->size;
12675
176a0d42
AM
12676 if (info->emitrelocations
12677 && htab->glink != NULL && htab->glink->size != 0)
12678 {
12679 htab->glink->reloc_count = 1;
12680 htab->glink->flags |= SEC_RELOC;
12681 }
12682
58d180e8
AM
12683 if (htab->glink_eh_frame != NULL
12684 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
9a2a56cc 12685 && htab->glink_eh_frame->output_section->size != 0)
58d180e8 12686 {
4bbe044a 12687 size_t size = 0, align;
58d180e8 12688
e7d1c40c 12689 for (stub_sec = htab->params->stub_bfd->sections;
58d180e8
AM
12690 stub_sec != NULL;
12691 stub_sec = stub_sec->next)
12692 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
da44f4e5 12693 size += 24;
58d180e8
AM
12694 if (htab->glink != NULL && htab->glink->size != 0)
12695 size += 24;
12696 if (size != 0)
12697 size += sizeof (glink_eh_frame_cie);
4bbe044a
AM
12698 align = 1;
12699 align <<= htab->glink_eh_frame->output_section->alignment_power;
12700 align -= 1;
12701 size = (size + align) & ~align;
58d180e8
AM
12702 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12703 htab->glink_eh_frame->size = size;
12704 }
12705
e7d1c40c
AM
12706 if (htab->params->plt_stub_align != 0)
12707 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
12708 stub_sec != NULL;
12709 stub_sec = stub_sec->next)
12710 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
12711 stub_sec->size = ((stub_sec->size
12712 + (1 << htab->params->plt_stub_align) - 1)
29f628db 12713 & -(1 << htab->params->plt_stub_align));
794e51c0 12714
e7d1c40c 12715 for (stub_sec = htab->params->stub_bfd->sections;
5c3dead3
AM
12716 stub_sec != NULL;
12717 stub_sec = stub_sec->next)
12718 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
c9301e31
AM
12719 && stub_sec->rawsize != stub_sec->size
12720 && (htab->stub_iteration <= STUB_SHRINK_ITER
12721 || stub_sec->rawsize < stub_sec->size))
5c3dead3
AM
12722 break;
12723
58d180e8
AM
12724 if (stub_sec == NULL
12725 && (htab->glink_eh_frame == NULL
12726 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12727 break;
12728
721956f4 12729 /* Ask the linker to do its stuff. */
e7d1c40c 12730 (*htab->params->layout_sections_again) ();
721956f4
AM
12731 }
12732
da44f4e5
AM
12733 if (htab->glink_eh_frame != NULL
12734 && htab->glink_eh_frame->size != 0)
12735 {
12736 bfd_vma val;
12737 bfd_byte *p, *last_fde;
12738 size_t last_fde_len, size, align, pad;
12739 asection *stub_sec;
12740
12741 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12742 if (p == NULL)
12743 return FALSE;
12744 htab->glink_eh_frame->contents = p;
12745 last_fde = p;
12746
12747 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12748 /* CIE length (rewrite in case little-endian). */
12749 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12750 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12751 p += sizeof (glink_eh_frame_cie);
12752
12753 for (stub_sec = htab->params->stub_bfd->sections;
12754 stub_sec != NULL;
12755 stub_sec = stub_sec->next)
12756 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12757 {
12758 last_fde = p;
12759 last_fde_len = 20;
12760 /* FDE length. */
12761 bfd_put_32 (htab->elf.dynobj, 20, p);
12762 p += 4;
12763 /* CIE pointer. */
12764 val = p - htab->glink_eh_frame->contents;
12765 bfd_put_32 (htab->elf.dynobj, val, p);
12766 p += 4;
12767 /* Offset to stub section, written later. */
12768 p += 4;
12769 /* stub section size. */
12770 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12771 p += 4;
12772 /* Augmentation. */
12773 p += 1;
12774 /* Pad. */
12775 p += 7;
12776 }
12777 if (htab->glink != NULL && htab->glink->size != 0)
12778 {
12779 last_fde = p;
12780 last_fde_len = 20;
12781 /* FDE length. */
12782 bfd_put_32 (htab->elf.dynobj, 20, p);
12783 p += 4;
12784 /* CIE pointer. */
12785 val = p - htab->glink_eh_frame->contents;
12786 bfd_put_32 (htab->elf.dynobj, val, p);
12787 p += 4;
12788 /* Offset to .glink, written later. */
12789 p += 4;
12790 /* .glink size. */
12791 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12792 p += 4;
12793 /* Augmentation. */
12794 p += 1;
12795
12796 *p++ = DW_CFA_advance_loc + 1;
12797 *p++ = DW_CFA_register;
12798 *p++ = 65;
9f08fa5c 12799 *p++ = htab->opd_abi ? 12 : 0;
da44f4e5
AM
12800 *p++ = DW_CFA_advance_loc + 4;
12801 *p++ = DW_CFA_restore_extended;
12802 *p++ = 65;
12803 }
12804 /* Subsume any padding into the last FDE if user .eh_frame
12805 sections are aligned more than glink_eh_frame. Otherwise any
12806 zero padding will be seen as a terminator. */
12807 size = p - htab->glink_eh_frame->contents;
12808 align = 1;
12809 align <<= htab->glink_eh_frame->output_section->alignment_power;
12810 align -= 1;
12811 pad = ((size + align) & ~align) - size;
12812 htab->glink_eh_frame->size = size + pad;
12813 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12814 }
12815
d969d15f
AM
12816 maybe_strip_output (info, htab->brlt);
12817 if (htab->glink_eh_frame != NULL)
12818 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12819
b34976b6 12820 return TRUE;
721956f4
AM
12821}
12822
12823/* Called after we have determined section placement. If sections
805fc799 12824 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12825
805fc799 12826bfd_vma
1c865ab2 12827ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12828{
805fc799 12829 asection *s;
a27e685f 12830 bfd_vma TOCstart, adjust;
721956f4 12831
43417696
AM
12832 if (info != NULL)
12833 {
12834 struct elf_link_hash_entry *h;
12835 struct elf_link_hash_table *htab = elf_hash_table (info);
12836
12837 if (is_elf_hash_table (htab)
12838 && htab->hgot != NULL)
12839 h = htab->hgot;
12840 else
12841 {
12842 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12843 if (is_elf_hash_table (htab))
12844 htab->hgot = h;
12845 }
12846 if (h != NULL
12847 && h->root.type == bfd_link_hash_defined
12848 && !h->root.linker_def
12849 && (!is_elf_hash_table (htab)
12850 || h->def_regular))
12851 {
12852 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12853 + h->root.u.def.section->output_offset
12854 + h->root.u.def.section->output_section->vma);
12855 _bfd_set_gp_value (obfd, TOCstart);
12856 return TOCstart;
12857 }
12858 }
12859
805fc799
AM
12860 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12861 order. The TOC starts where the first of these sections starts. */
12862 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12863 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12864 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12865 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12866 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12867 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12868 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12869 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12870 {
12871 /* This may happen for
12872 o references to TOC base (SYM@toc / TOC[tc0]) without a
12873 .toc directive
12874 o bad linker script
12875 o --gc-sections and empty TOC sections
12876
12877 FIXME: Warn user? */
12878
12879 /* Look for a likely section. We probably won't even be
12880 using TOCstart. */
12881 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12882 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12883 | SEC_EXCLUDE))
805fc799
AM
12884 == (SEC_ALLOC | SEC_SMALL_DATA))
12885 break;
721956f4 12886 if (s == NULL)
805fc799 12887 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12888 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12889 == (SEC_ALLOC | SEC_SMALL_DATA))
12890 break;
721956f4 12891 if (s == NULL)
805fc799 12892 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12893 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12894 == SEC_ALLOC)
805fc799 12895 break;
721956f4 12896 if (s == NULL)
805fc799 12897 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12898 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12899 break;
12900 }
721956f4 12901
805fc799
AM
12902 TOCstart = 0;
12903 if (s != NULL)
12904 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12905
a27e685f
AM
12906 /* Force alignment. */
12907 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12908 TOCstart -= adjust;
1c865ab2
AM
12909 _bfd_set_gp_value (obfd, TOCstart);
12910
810d4e75 12911 if (info != NULL && s != NULL)
1c865ab2
AM
12912 {
12913 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12914
810d4e75
AM
12915 if (htab != NULL)
12916 {
12917 if (htab->elf.hgot != NULL)
12918 {
a27e685f 12919 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12920 htab->elf.hgot->root.u.def.section = s;
12921 }
12922 }
12923 else
1c865ab2 12924 {
810d4e75
AM
12925 struct bfd_link_hash_entry *bh = NULL;
12926 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12927 s, TOC_BASE_OFF - adjust,
12928 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12929 }
12930 }
805fc799 12931 return TOCstart;
721956f4
AM
12932}
12933
a345bc8d
AM
12934/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12935 write out any global entry stubs. */
12936
12937static bfd_boolean
12938build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12939{
12940 struct bfd_link_info *info;
12941 struct ppc_link_hash_table *htab;
12942 struct plt_entry *pent;
12943 asection *s;
12944
12945 if (h->root.type == bfd_link_hash_indirect)
12946 return TRUE;
12947
12948 if (!h->pointer_equality_needed)
12949 return TRUE;
12950
12951 if (h->def_regular)
12952 return TRUE;
12953
12954 info = inf;
12955 htab = ppc_hash_table (info);
12956 if (htab == NULL)
12957 return FALSE;
12958
12959 s = htab->glink;
12960 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12961 if (pent->plt.offset != (bfd_vma) -1
12962 && pent->addend == 0)
12963 {
12964 bfd_byte *p;
12965 asection *plt;
12966 bfd_vma off;
12967
a345bc8d 12968 p = s->contents + h->root.u.def.value;
33e44f2e 12969 plt = htab->elf.splt;
a345bc8d
AM
12970 if (!htab->elf.dynamic_sections_created
12971 || h->dynindx == -1)
33e44f2e 12972 plt = htab->elf.iplt;
a345bc8d
AM
12973 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12974 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12975
12976 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12977 {
12978 info->callbacks->einfo
12979 (_("%P: linkage table error against `%T'\n"),
12980 h->root.root.string);
12981 bfd_set_error (bfd_error_bad_value);
12982 htab->stub_error = TRUE;
12983 }
12984
7341d5e2
AM
12985 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12986 if (htab->params->emit_stub_syms)
12987 {
12988 size_t len = strlen (h->root.root.string);
12989 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12990
12991 if (name == NULL)
12992 return FALSE;
12993
12994 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12995 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12996 if (h == NULL)
12997 return FALSE;
12998 if (h->root.type == bfd_link_hash_new)
12999 {
13000 h->root.type = bfd_link_hash_defined;
13001 h->root.u.def.section = s;
13002 h->root.u.def.value = p - s->contents;
13003 h->ref_regular = 1;
13004 h->def_regular = 1;
13005 h->ref_regular_nonweak = 1;
13006 h->forced_local = 1;
13007 h->non_elf = 0;
2ec55de3 13008 h->root.linker_def = 1;
7341d5e2
AM
13009 }
13010 }
13011
a345bc8d
AM
13012 if (PPC_HA (off) != 0)
13013 {
13014 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13015 p += 4;
13016 }
13017 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13018 p += 4;
13019 bfd_put_32 (s->owner, MTCTR_R12, p);
13020 p += 4;
13021 bfd_put_32 (s->owner, BCTR, p);
13022 break;
13023 }
13024 return TRUE;
13025}
13026
721956f4
AM
13027/* Build all the stubs associated with the current output file.
13028 The stubs are kept in a hash table attached to the main linker
13029 hash table. This function is called via gldelf64ppc_finish. */
13030
b34976b6 13031bfd_boolean
e7d1c40c 13032ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13033 char **stats)
5d1634d7
AM
13034{
13035 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13036 struct map_stub *group;
721956f4 13037 asection *stub_sec;
5d1634d7 13038 bfd_byte *p;
e717da7e 13039 int stub_sec_count = 0;
5d1634d7 13040
4dfe6ac6
NC
13041 if (htab == NULL)
13042 return FALSE;
13043
eea6121a 13044 /* Allocate memory to hold the linker stubs. */
e7d1c40c 13045 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
13046 stub_sec != NULL;
13047 stub_sec = stub_sec->next)
eea6121a
AM
13048 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
13049 && stub_sec->size != 0)
e717da7e 13050 {
e7d1c40c 13051 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
13052 if (stub_sec->contents == NULL)
13053 return FALSE;
eea6121a 13054 stub_sec->size = 0;
e717da7e 13055 }
5d1634d7 13056
23eb7e01 13057 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13058 {
9f951329 13059 unsigned int indx;
ad8e1ba5 13060 bfd_vma plt0;
9f951329 13061
721956f4 13062 /* Build the .glink plt call stub. */
e7d1c40c 13063 if (htab->params->emit_stub_syms)
97b639ba
AM
13064 {
13065 struct elf_link_hash_entry *h;
468392fb
AM
13066 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13067 TRUE, FALSE, FALSE);
97b639ba
AM
13068 if (h == NULL)
13069 return FALSE;
13070 if (h->root.type == bfd_link_hash_new)
13071 {
13072 h->root.type = bfd_link_hash_defined;
13073 h->root.u.def.section = htab->glink;
ee4bf8d2 13074 h->root.u.def.value = 8;
f5385ebf
AM
13075 h->ref_regular = 1;
13076 h->def_regular = 1;
13077 h->ref_regular_nonweak = 1;
13078 h->forced_local = 1;
13079 h->non_elf = 0;
2ec55de3 13080 h->root.linker_def = 1;
97b639ba
AM
13081 }
13082 }
33e44f2e
AM
13083 plt0 = (htab->elf.splt->output_section->vma
13084 + htab->elf.splt->output_offset
13085 - 16);
176a0d42
AM
13086 if (info->emitrelocations)
13087 {
13088 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13089 if (r == NULL)
13090 return FALSE;
13091 r->r_offset = (htab->glink->output_offset
13092 + htab->glink->output_section->vma);
13093 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13094 r->r_addend = plt0;
13095 }
4ce794b7 13096 p = htab->glink->contents;
176a0d42 13097 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13098 bfd_put_64 (htab->glink->owner, plt0, p);
13099 p += 8;
b9e5796b
AM
13100 if (htab->opd_abi)
13101 {
13102 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13103 p += 4;
13104 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13105 p += 4;
13106 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13107 p += 4;
13108 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13109 p += 4;
13110 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13111 p += 4;
13112 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13113 p += 4;
13114 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13115 p += 4;
13116 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13117 p += 4;
13118 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13119 p += 4;
13120 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13121 p += 4;
13122 }
13123 else
13124 {
13125 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13126 p += 4;
13127 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13128 p += 4;
13129 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13130 p += 4;
13131 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13132 p += 4;
13133 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13134 p += 4;
13135 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13136 p += 4;
13137 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13138 p += 4;
13139 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13140 p += 4;
13141 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13142 p += 4;
13143 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13144 p += 4;
13145 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13146 p += 4;
13147 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13148 p += 4;
13149 }
4ce794b7 13150 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 13151 p += 4;
ee4bf8d2
AM
13152 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13153 {
13154 bfd_put_32 (htab->glink->owner, NOP, p);
13155 p += 4;
13156 }
ad8e1ba5 13157
9f951329
AM
13158 /* Build the .glink lazy link call stubs. */
13159 indx = 0;
a345bc8d 13160 while (p < htab->glink->contents + htab->glink->rawsize)
9f951329 13161 {
b9e5796b 13162 if (htab->opd_abi)
9f951329 13163 {
b9e5796b
AM
13164 if (indx < 0x8000)
13165 {
13166 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13167 p += 4;
13168 }
13169 else
13170 {
13171 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13172 p += 4;
13173 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13174 p);
13175 p += 4;
13176 }
9f951329 13177 }
4ce794b7 13178 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13179 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13180 indx++;
9f951329
AM
13181 p += 4;
13182 }
a345bc8d
AM
13183
13184 /* Build .glink global entry stubs. */
13185 if (htab->glink->size > htab->glink->rawsize)
afe397ea 13186 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
5d1634d7 13187 }
5d1634d7 13188
7341d5e2 13189 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13190 {
4ce794b7 13191 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13192 htab->brlt->size);
4ce794b7 13193 if (htab->brlt->contents == NULL)
b34976b6 13194 return FALSE;
721956f4 13195 }
ee75fd95 13196 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13197 {
13198 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13199 htab->relbrlt->size);
63bc6f6c
AM
13200 if (htab->relbrlt->contents == NULL)
13201 return FALSE;
13202 }
5d1634d7 13203
721956f4
AM
13204 /* Build the stubs as directed by the stub hash table. */
13205 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13206
a4b6fadd
AM
13207 for (group = htab->group; group != NULL; group = group->next)
13208 if (group->needs_save_res)
13209 {
13210 stub_sec = group->stub_sec;
13211 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13212 htab->sfpr->size);
13213 if (htab->params->emit_stub_syms)
13214 {
13215 unsigned int i;
13216
13217 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13218 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13219 return FALSE;
13220 }
13221 stub_sec->size += htab->sfpr->size;
13222 }
13223
aa8a7074
AM
13224 if (htab->relbrlt != NULL)
13225 htab->relbrlt->reloc_count = 0;
13226
e7d1c40c
AM
13227 if (htab->params->plt_stub_align != 0)
13228 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
13229 stub_sec != NULL;
13230 stub_sec = stub_sec->next)
13231 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
13232 stub_sec->size = ((stub_sec->size
13233 + (1 << htab->params->plt_stub_align) - 1)
29f628db 13234 & -(1 << htab->params->plt_stub_align));
794e51c0 13235
e7d1c40c 13236 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
13237 stub_sec != NULL;
13238 stub_sec = stub_sec->next)
e717da7e
AM
13239 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13240 {
13241 stub_sec_count += 1;
c9301e31
AM
13242 if (stub_sec->rawsize != stub_sec->size
13243 && (htab->stub_iteration <= STUB_SHRINK_ITER
13244 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13245 break;
13246 }
5d1634d7 13247
da44f4e5
AM
13248 /* Note that the glink_eh_frame check here is not only testing that
13249 the generated size matched the calculated size but also that
13250 bfd_elf_discard_info didn't make any changes to the section. */
721956f4 13251 if (stub_sec != NULL
58d180e8
AM
13252 || (htab->glink_eh_frame != NULL
13253 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 13254 {
b34976b6 13255 htab->stub_error = TRUE;
8de848d8 13256 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 13257 }
721956f4 13258
d2a300cf
AM
13259 if (htab->stub_error)
13260 return FALSE;
13261
13262 if (stats != NULL)
13263 {
13264 *stats = bfd_malloc (500);
13265 if (*stats == NULL)
13266 return FALSE;
13267
ee75fd95 13268 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
13269 " branch %lu\n"
13270 " toc adjust %lu\n"
13271 " long branch %lu\n"
13272 " long toc adj %lu\n"
794e51c0 13273 " plt call %lu\n"
7341d5e2
AM
13274 " plt call toc %lu\n"
13275 " global entry %lu"),
e717da7e 13276 stub_sec_count,
ee75fd95 13277 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
13278 htab->stub_count[ppc_stub_long_branch - 1],
13279 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13280 htab->stub_count[ppc_stub_plt_branch - 1],
13281 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13282 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13283 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13284 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13285 }
13286 return TRUE;
5bd4f169
AM
13287}
13288
60124e18
AM
13289/* What to do when ld finds relocations against symbols defined in
13290 discarded sections. */
13291
13292static unsigned int
13293ppc64_elf_action_discarded (asection *sec)
13294{
13295 if (strcmp (".opd", sec->name) == 0)
13296 return 0;
13297
13298 if (strcmp (".toc", sec->name) == 0)
13299 return 0;
13300
bce50a28
JJ
13301 if (strcmp (".toc1", sec->name) == 0)
13302 return 0;
13303
60124e18
AM
13304 return _bfd_elf_default_action_discarded (sec);
13305}
13306
5bd4f169
AM
13307/* The RELOCATE_SECTION function is called by the ELF backend linker
13308 to handle the relocations for a section.
13309
13310 The relocs are always passed as Rela structures; if the section
13311 actually uses Rel structures, the r_addend field will always be
13312 zero.
13313
13314 This function is responsible for adjust the section contents as
13315 necessary, and (if using Rela relocs and generating a
1049f94e 13316 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13317 necessary.
13318
13319 This function does not have to worry about setting the reloc
13320 address or the reloc symbol index.
13321
13322 LOCAL_SYMS is a pointer to the swapped in local symbols.
13323
13324 LOCAL_SECTIONS is an array giving the section in the input file
13325 corresponding to the st_shndx field of each local symbol.
13326
13327 The global hash table entry for the global symbols can be found
13328 via elf_sym_hashes (input_bfd).
13329
1049f94e 13330 When generating relocatable output, this function must handle
5bd4f169
AM
13331 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13332 going to be the section symbol corresponding to the output
13333 section, which means that the addend must be adjusted
13334 accordingly. */
13335
b34976b6 13336static bfd_boolean
4ce794b7
AM
13337ppc64_elf_relocate_section (bfd *output_bfd,
13338 struct bfd_link_info *info,
13339 bfd *input_bfd,
13340 asection *input_section,
13341 bfd_byte *contents,
13342 Elf_Internal_Rela *relocs,
13343 Elf_Internal_Sym *local_syms,
13344 asection **local_sections)
5bd4f169 13345{
65f38f15 13346 struct ppc_link_hash_table *htab;
5bd4f169
AM
13347 Elf_Internal_Shdr *symtab_hdr;
13348 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13349 Elf_Internal_Rela *rel;
c316a17c 13350 Elf_Internal_Rela *wrel;
5bd4f169 13351 Elf_Internal_Rela *relend;
411e1bfb
AM
13352 Elf_Internal_Rela outrel;
13353 bfd_byte *loc;
411e1bfb 13354 struct got_entry **local_got_ents;
5bd4f169 13355 bfd_vma TOCstart;
b34976b6
AM
13356 bfd_boolean ret = TRUE;
13357 bfd_boolean is_opd;
794e51c0
AM
13358 /* Assume 'at' branch hints. */
13359 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13360 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13361
65f38f15 13362 /* Initialize howto table if needed. */
5bd4f169 13363 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13364 ppc_howto_init ();
13365
65f38f15 13366 htab = ppc_hash_table (info);
4dfe6ac6
NC
13367 if (htab == NULL)
13368 return FALSE;
ee75fd95
AM
13369
13370 /* Don't relocate stub sections. */
e7d1c40c 13371 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13372 return TRUE;
13373
0c8d6e5c 13374 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13375
411e1bfb 13376 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13377 TOCstart = elf_gp (output_bfd);
0ffa91dd 13378 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13379 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13380 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13381
c316a17c 13382 rel = wrel = relocs;
5bd4f169 13383 relend = relocs + input_section->reloc_count;
c316a17c 13384 for (; rel < relend; wrel++, rel++)
5bd4f169 13385 {
04c9666a 13386 enum elf_ppc64_reloc_type r_type;
31c76678 13387 bfd_vma addend;
5bd4f169
AM
13388 bfd_reloc_status_type r;
13389 Elf_Internal_Sym *sym;
13390 asection *sec;
039b3fef
AM
13391 struct elf_link_hash_entry *h_elf;
13392 struct ppc_link_hash_entry *h;
13393 struct ppc_link_hash_entry *fdh;
5bd4f169 13394 const char *sym_name;
0d4792f7 13395 unsigned long r_symndx, toc_symndx;
3a71aa26 13396 bfd_vma toc_addend;
f961d9dd
AM
13397 unsigned char tls_mask, tls_gd, tls_type;
13398 unsigned char sym_type;
5bd4f169 13399 bfd_vma relocation;
b34976b6
AM
13400 bfd_boolean unresolved_reloc;
13401 bfd_boolean warned;
bc30df16 13402 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13403 unsigned int insn;
e11840f9 13404 unsigned int mask;
721956f4
AM
13405 struct ppc_stub_hash_entry *stub_entry;
13406 bfd_vma max_br_offset;
13407 bfd_vma from;
c316a17c 13408 Elf_Internal_Rela orig_rel;
b80eed39
AM
13409 reloc_howto_type *howto;
13410 struct reloc_howto_struct alt_howto;
5bd4f169 13411
c316a17c
AM
13412 again:
13413 orig_rel = *rel;
13414
4ce794b7 13415 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13416 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13417
13418 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13419 symbol of the previous ADDR64 reloc. The symbol gives us the
13420 proper TOC base to use. */
13421 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13422 && wrel != relocs
13423 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13424 && is_opd)
c316a17c 13425 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13426
4ce794b7
AM
13427 sym = NULL;
13428 sec = NULL;
039b3fef 13429 h_elf = NULL;
4ce794b7 13430 sym_name = NULL;
b34976b6
AM
13431 unresolved_reloc = FALSE;
13432 warned = FALSE;
65f38f15 13433
0b13192e 13434 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13435 {
13436 /* It's a local symbol. */
74f0fb50 13437 struct _opd_sec_data *opd;
4025353c 13438
5bd4f169
AM
13439 sym = local_syms + r_symndx;
13440 sec = local_sections[r_symndx];
26c61ae5 13441 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13442 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13443 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13444 opd = get_opd_info (sec);
13445 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13446 {
51aecdc5
AM
13447 long adjust = opd->adjust[OPD_NDX (sym->st_value
13448 + rel->r_addend)];
4025353c
AM
13449 if (adjust == -1)
13450 relocation = 0;
13451 else
4cc603a5
AM
13452 {
13453 /* If this is a relocation against the opd section sym
13454 and we have edited .opd, adjust the reloc addend so
13455 that ld -r and ld --emit-relocs output is correct.
13456 If it is a reloc against some other .opd symbol,
13457 then the symbol value will be adjusted later. */
13458 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13459 rel->r_addend += adjust;
13460 else
13461 relocation += adjust;
13462 }
1e2f5b6e 13463 }
5bd4f169
AM
13464 }
13465 else
13466 {
62d887d4
L
13467 bfd_boolean ignored;
13468
b2a8e766
AM
13469 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13470 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13471 h_elf, sec, relocation,
62d887d4 13472 unresolved_reloc, warned, ignored);
039b3fef
AM
13473 sym_name = h_elf->root.root.string;
13474 sym_type = h_elf->type;
b69fdb4e
AM
13475 if (sec != NULL
13476 && sec->owner == output_bfd
13477 && strcmp (sec->name, ".opd") == 0)
13478 {
13479 /* This is a symbol defined in a linker script. All
13480 such are defined in output sections, even those
13481 defined by simple assignment from a symbol defined in
13482 an input section. Transfer the symbol to an
13483 appropriate input .opd section, so that a branch to
13484 this symbol will be mapped to the location specified
13485 by the opd entry. */
13486 struct bfd_link_order *lo;
13487 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13488 if (lo->type == bfd_indirect_link_order)
13489 {
13490 asection *isec = lo->u.indirect.section;
13491 if (h_elf->root.u.def.value >= isec->output_offset
13492 && h_elf->root.u.def.value < (isec->output_offset
13493 + isec->size))
13494 {
13495 h_elf->root.u.def.value -= isec->output_offset;
13496 h_elf->root.u.def.section = isec;
13497 sec = isec;
13498 break;
13499 }
13500 }
13501 }
5bd4f169 13502 }
039b3fef 13503 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13504
dbaa2011 13505 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13506 {
13507 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13508 input_bfd, input_section,
13509 contents + rel->r_offset);
13510 wrel->r_offset = rel->r_offset;
13511 wrel->r_info = 0;
13512 wrel->r_addend = 0;
13513
13514 /* For ld -r, remove relocations in debug sections against
13515 sections defined in discarded sections. Not done for
13516 non-debug to preserve relocs in .eh_frame which the
13517 eh_frame editing code expects to be present. */
13518 if (bfd_link_relocatable (info)
13519 && (input_section->flags & SEC_DEBUGGING))
13520 wrel--;
13521
13522 continue;
13523 }
ab96bf03 13524
0e1862bb 13525 if (bfd_link_relocatable (info))
c316a17c 13526 goto copy_reloc;
ab96bf03 13527
f40da81b
AM
13528 if (h != NULL && &h->elf == htab->elf.hgot)
13529 {
6f20ed8a 13530 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13531 sec = bfd_abs_section_ptr;
13532 unresolved_reloc = FALSE;
13533 }
13534
951fd09b
AM
13535 /* TLS optimizations. Replace instruction sequences and relocs
13536 based on information we collected in tls_optimize. We edit
13537 RELOCS so that --emit-relocs will output something sensible
13538 for the final instruction stream. */
13539 tls_mask = 0;
13540 tls_gd = 0;
0d4792f7 13541 toc_symndx = 0;
727fc41e
AM
13542 if (h != NULL)
13543 tls_mask = h->tls_mask;
13544 else if (local_got_ents != NULL)
411e1bfb 13545 {
e054468f
AM
13546 struct plt_entry **local_plt = (struct plt_entry **)
13547 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13548 unsigned char *lgot_masks = (unsigned char *)
e054468f 13549 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13550 tls_mask = lgot_masks[r_symndx];
13551 }
13552 if (tls_mask == 0
13553 && (r_type == R_PPC64_TLS
13554 || r_type == R_PPC64_TLSGD
13555 || r_type == R_PPC64_TLSLD))
13556 {
13557 /* Check for toc tls entries. */
f961d9dd 13558 unsigned char *toc_tls;
0d4792f7 13559
727fc41e
AM
13560 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13561 &local_syms, rel, input_bfd))
13562 return FALSE;
0d4792f7 13563
727fc41e
AM
13564 if (toc_tls)
13565 tls_mask = *toc_tls;
0d4792f7
AM
13566 }
13567
13568 /* Check that tls relocs are used with tls syms, and non-tls
13569 relocs are used with non-tls syms. */
cf35638d 13570 if (r_symndx != STN_UNDEF
0d4792f7
AM
13571 && r_type != R_PPC64_NONE
13572 && (h == NULL
039b3fef
AM
13573 || h->elf.root.type == bfd_link_hash_defined
13574 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13575 && (IS_PPC64_TLS_RELOC (r_type)
13576 != (sym_type == STT_TLS
13577 || (sym_type == STT_SECTION
13578 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13579 {
727fc41e
AM
13580 if (tls_mask != 0
13581 && (r_type == R_PPC64_TLS
13582 || r_type == R_PPC64_TLSGD
13583 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13584 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13585 ;
13586 else
25f53a85 13587 info->callbacks->einfo
1d483afe 13588 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 13589 /* xgettext:c-format */
bc30df16 13590 ? _("%P: %H: %s used with TLS symbol `%T'\n")
695344c0 13591 /* xgettext:c-format */
bc30df16 13592 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
25f53a85 13593 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13594 ppc64_elf_howto_table[r_type]->name,
13595 sym_name);
411e1bfb
AM
13596 }
13597
13598 /* Ensure reloc mapping code below stays sane. */
13599 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13600 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13601 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13602 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13603 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13604 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13605 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13606 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13607 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13608 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13609 abort ();
0d4792f7 13610
411e1bfb
AM
13611 switch (r_type)
13612 {
13613 default:
411e1bfb
AM
13614 break;
13615
ba761f19 13616 case R_PPC64_LO_DS_OPT:
95f0d0d2 13617 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
13618 if ((insn & (0x3f << 26)) != 58u << 26)
13619 abort ();
13620 insn += (14u << 26) - (58u << 26);
95f0d0d2 13621 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
13622 r_type = R_PPC64_TOC16_LO;
13623 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13624 break;
13625
411e1bfb
AM
13626 case R_PPC64_TOC16:
13627 case R_PPC64_TOC16_LO:
13628 case R_PPC64_TOC16_DS:
13629 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13630 {
13631 /* Check for toc tls entries. */
f961d9dd 13632 unsigned char *toc_tls;
951fd09b 13633 int retval;
411e1bfb 13634
3a71aa26
AM
13635 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13636 &local_syms, rel, input_bfd);
951fd09b 13637 if (retval == 0)
411e1bfb
AM
13638 return FALSE;
13639
13640 if (toc_tls)
13641 {
951fd09b 13642 tls_mask = *toc_tls;
411e1bfb
AM
13643 if (r_type == R_PPC64_TOC16_DS
13644 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
13645 {
13646 if (tls_mask != 0
13647 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13648 goto toctprel;
13649 }
411e1bfb 13650 else
951fd09b
AM
13651 {
13652 /* If we found a GD reloc pair, then we might be
13653 doing a GD->IE transition. */
13654 if (retval == 2)
13655 {
13656 tls_gd = TLS_TPRELGD;
13657 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13658 goto tls_ldgd_opt;
951fd09b
AM
13659 }
13660 else if (retval == 3)
13661 {
13662 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 13663 goto tls_ldgd_opt;
951fd09b
AM
13664 }
13665 }
411e1bfb
AM
13666 }
13667 }
13668 break;
13669
9d6ded02
AM
13670 case R_PPC64_GOT_TPREL16_HI:
13671 case R_PPC64_GOT_TPREL16_HA:
13672 if (tls_mask != 0
13673 && (tls_mask & TLS_TPREL) == 0)
13674 {
13675 rel->r_offset -= d_offset;
95f0d0d2 13676 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
13677 r_type = R_PPC64_NONE;
13678 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13679 }
13680 break;
13681
411e1bfb
AM
13682 case R_PPC64_GOT_TPREL16_DS:
13683 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13684 if (tls_mask != 0
13685 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13686 {
81407a69 13687 toctprel:
95f0d0d2 13688 insn = bfd_get_32 (input_bfd,
c316a17c 13689 contents + rel->r_offset - d_offset);
411e1bfb
AM
13690 insn &= 31 << 21;
13691 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 13692 bfd_put_32 (input_bfd, insn,
c316a17c 13693 contents + rel->r_offset - d_offset);
411e1bfb 13694 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13695 if (toc_symndx != 0)
13696 {
13697 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13698 rel->r_addend = toc_addend;
0d4792f7
AM
13699 /* We changed the symbol. Start over in order to
13700 get h, sym, sec etc. right. */
c316a17c 13701 goto again;
0d4792f7
AM
13702 }
13703 else
13704 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13705 }
13706 break;
13707
13708 case R_PPC64_TLS:
951fd09b
AM
13709 if (tls_mask != 0
13710 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13711 {
95f0d0d2 13712 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
13713 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13714 if (insn == 0)
411e1bfb 13715 abort ();
95f0d0d2 13716 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 13717 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13718 PPC64_TPREL16_LO which is at low-order half-word. */
13719 rel->r_offset += d_offset;
0d4792f7
AM
13720 r_type = R_PPC64_TPREL16_LO;
13721 if (toc_symndx != 0)
13722 {
13723 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13724 rel->r_addend = toc_addend;
0d4792f7
AM
13725 /* We changed the symbol. Start over in order to
13726 get h, sym, sec etc. right. */
c316a17c 13727 goto again;
0d4792f7
AM
13728 }
13729 else
13730 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13731 }
13732 break;
13733
411e1bfb
AM
13734 case R_PPC64_GOT_TLSGD16_HI:
13735 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13736 tls_gd = TLS_TPRELGD;
13737 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13738 goto tls_gdld_hi;
13739 break;
13740
411e1bfb
AM
13741 case R_PPC64_GOT_TLSLD16_HI:
13742 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13743 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13744 {
951fd09b
AM
13745 tls_gdld_hi:
13746 if ((tls_mask & tls_gd) != 0)
13747 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13748 + R_PPC64_GOT_TPREL16_DS);
13749 else
411e1bfb 13750 {
4fe5ca5b 13751 rel->r_offset -= d_offset;
95f0d0d2 13752 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 13753 r_type = R_PPC64_NONE;
411e1bfb 13754 }
951fd09b 13755 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13756 }
13757 break;
13758
951fd09b
AM
13759 case R_PPC64_GOT_TLSGD16:
13760 case R_PPC64_GOT_TLSGD16_LO:
13761 tls_gd = TLS_TPRELGD;
13762 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13763 goto tls_ldgd_opt;
951fd09b 13764 break;
411e1bfb 13765
951fd09b
AM
13766 case R_PPC64_GOT_TLSLD16:
13767 case R_PPC64_GOT_TLSLD16_LO:
13768 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13769 {
3a71aa26 13770 unsigned int insn1, insn2, insn3;
102890f0
AM
13771 bfd_vma offset;
13772
13773 tls_ldgd_opt:
727fc41e
AM
13774 offset = (bfd_vma) -1;
13775 /* If not using the newer R_PPC64_TLSGD/LD to mark
13776 __tls_get_addr calls, we must trust that the call
13777 stays with its arg setup insns, ie. that the next
13778 reloc is the __tls_get_addr call associated with
13779 the current reloc. Edit both insns. */
13780 if (input_section->has_tls_get_addr_call
13781 && rel + 1 < relend
13782 && branch_reloc_hash_match (input_bfd, rel + 1,
13783 htab->tls_get_addr,
13784 htab->tls_get_addr_fd))
13785 offset = rel[1].r_offset;
b86ac8e3
AM
13786 /* We read the low GOT_TLS (or TOC16) insn because we
13787 need to keep the destination reg. It may be
13788 something other than the usual r3, and moved to r3
13789 before the call by intervening code. */
95f0d0d2 13790 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 13791 contents + rel->r_offset - d_offset);
102890f0 13792 if ((tls_mask & tls_gd) != 0)
411e1bfb 13793 {
102890f0 13794 /* IE */
b86ac8e3 13795 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13796 insn1 |= 58 << 26; /* ld */
13797 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13798 if (offset != (bfd_vma) -1)
f58d5a2d 13799 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13800 if ((tls_mask & TLS_EXPLICIT) == 0)
13801 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13802 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13803 else
102890f0
AM
13804 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13805 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13806 }
13807 else
13808 {
13809 /* LE */
b86ac8e3
AM
13810 insn1 &= 0x1f << 21;
13811 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13812 insn2 = 0x38630000; /* addi 3,3,0 */
13813 if (tls_gd == 0)
951fd09b 13814 {
102890f0 13815 /* Was an LD reloc. */
1d483afe
AM
13816 if (toc_symndx)
13817 sec = local_sections[toc_symndx];
13818 for (r_symndx = 0;
13819 r_symndx < symtab_hdr->sh_info;
13820 r_symndx++)
13821 if (local_sections[r_symndx] == sec)
13822 break;
13823 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13824 r_symndx = STN_UNDEF;
102890f0 13825 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13826 if (r_symndx != STN_UNDEF)
1d483afe
AM
13827 rel->r_addend -= (local_syms[r_symndx].st_value
13828 + sec->output_offset
13829 + sec->output_section->vma);
951fd09b 13830 }
102890f0 13831 else if (toc_symndx != 0)
3a71aa26
AM
13832 {
13833 r_symndx = toc_symndx;
13834 rel->r_addend = toc_addend;
13835 }
102890f0
AM
13836 r_type = R_PPC64_TPREL16_HA;
13837 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13838 if (offset != (bfd_vma) -1)
13839 {
13840 rel[1].r_info = ELF64_R_INFO (r_symndx,
13841 R_PPC64_TPREL16_LO);
13842 rel[1].r_offset = offset + d_offset;
13843 rel[1].r_addend = rel->r_addend;
13844 }
102890f0 13845 }
95f0d0d2 13846 bfd_put_32 (input_bfd, insn1,
3a71aa26 13847 contents + rel->r_offset - d_offset);
727fc41e
AM
13848 if (offset != (bfd_vma) -1)
13849 {
95f0d0d2 13850 insn3 = bfd_get_32 (input_bfd,
727fc41e
AM
13851 contents + offset + 4);
13852 if (insn3 == NOP
13853 || insn3 == CROR_151515 || insn3 == CROR_313131)
13854 {
13855 rel[1].r_offset += 4;
95f0d0d2 13856 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
727fc41e
AM
13857 insn2 = NOP;
13858 }
95f0d0d2 13859 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
13860 }
13861 if ((tls_mask & tls_gd) == 0
13862 && (tls_gd == 0 || toc_symndx != 0))
13863 {
13864 /* We changed the symbol. Start over in order
13865 to get h, sym, sec etc. right. */
c316a17c 13866 goto again;
727fc41e
AM
13867 }
13868 }
13869 break;
13870
13871 case R_PPC64_TLSGD:
13872 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13873 {
13874 unsigned int insn2, insn3;
13875 bfd_vma offset = rel->r_offset;
13876
13877 if ((tls_mask & TLS_TPRELGD) != 0)
13878 {
13879 /* IE */
13880 r_type = R_PPC64_NONE;
13881 insn2 = 0x7c636a14; /* add 3,3,13 */
13882 }
13883 else
13884 {
13885 /* LE */
13886 if (toc_symndx != 0)
13887 {
13888 r_symndx = toc_symndx;
13889 rel->r_addend = toc_addend;
13890 }
13891 r_type = R_PPC64_TPREL16_LO;
13892 rel->r_offset = offset + d_offset;
13893 insn2 = 0x38630000; /* addi 3,3,0 */
13894 }
13895 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13896 /* Zap the reloc on the _tls_get_addr call too. */
13897 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13898 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 13899 insn3 = bfd_get_32 (input_bfd,
3a71aa26 13900 contents + offset + 4);
102890f0
AM
13901 if (insn3 == NOP
13902 || insn3 == CROR_151515 || insn3 == CROR_313131)
13903 {
727fc41e 13904 rel->r_offset += 4;
95f0d0d2 13905 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
3a71aa26 13906 insn2 = NOP;
102890f0 13907 }
95f0d0d2 13908 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 13909 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13910 goto again;
411e1bfb 13911 }
411e1bfb
AM
13912 break;
13913
727fc41e
AM
13914 case R_PPC64_TLSLD:
13915 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13916 {
13917 unsigned int insn2, insn3;
13918 bfd_vma offset = rel->r_offset;
13919
13920 if (toc_symndx)
13921 sec = local_sections[toc_symndx];
13922 for (r_symndx = 0;
13923 r_symndx < symtab_hdr->sh_info;
13924 r_symndx++)
13925 if (local_sections[r_symndx] == sec)
13926 break;
13927 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13928 r_symndx = STN_UNDEF;
727fc41e 13929 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13930 if (r_symndx != STN_UNDEF)
727fc41e
AM
13931 rel->r_addend -= (local_syms[r_symndx].st_value
13932 + sec->output_offset
13933 + sec->output_section->vma);
13934
13935 r_type = R_PPC64_TPREL16_LO;
13936 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13937 rel->r_offset = offset + d_offset;
13938 /* Zap the reloc on the _tls_get_addr call too. */
13939 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13940 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 13941 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 13942 insn3 = bfd_get_32 (input_bfd,
727fc41e
AM
13943 contents + offset + 4);
13944 if (insn3 == NOP
13945 || insn3 == CROR_151515 || insn3 == CROR_313131)
13946 {
13947 rel->r_offset += 4;
95f0d0d2 13948 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
727fc41e
AM
13949 insn2 = NOP;
13950 }
95f0d0d2 13951 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 13952 goto again;
727fc41e
AM
13953 }
13954 break;
13955
411e1bfb 13956 case R_PPC64_DTPMOD64:
951fd09b
AM
13957 if (rel + 1 < relend
13958 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13959 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13960 {
951fd09b
AM
13961 if ((tls_mask & TLS_GD) == 0)
13962 {
13963 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13964 if ((tls_mask & TLS_TPRELGD) != 0)
13965 r_type = R_PPC64_TPREL64;
13966 else
13967 {
4ce794b7 13968 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13969 r_type = R_PPC64_NONE;
13970 }
13971 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13972 }
13973 }
13974 else
13975 {
13976 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13977 {
4ce794b7 13978 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13979 r_type = R_PPC64_NONE;
951fd09b 13980 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13981 }
411e1bfb
AM
13982 }
13983 break;
13984
13985 case R_PPC64_TPREL64:
951fd09b 13986 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13987 {
13988 r_type = R_PPC64_NONE;
13989 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13990 }
13991 break;
52a82034 13992
006589cf
AM
13993 case R_PPC64_ENTRY:
13994 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13995 if (!bfd_link_pic (info)
13996 && !info->traditional_format
13997 && relocation + 0x80008000 <= 0xffffffff)
13998 {
13999 unsigned int insn1, insn2;
14000
14001 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14002 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14003 if ((insn1 & ~0xfffc) == LD_R2_0R12
14004 && insn2 == ADD_R2_R2_R12)
14005 {
95f0d0d2 14006 bfd_put_32 (input_bfd,
006589cf
AM
14007 LIS_R2 + PPC_HA (relocation),
14008 contents + rel->r_offset);
95f0d0d2 14009 bfd_put_32 (input_bfd,
006589cf
AM
14010 ADDI_R2_R2 + PPC_LO (relocation),
14011 contents + rel->r_offset + 4);
14012 }
14013 }
14014 else
14015 {
14016 relocation -= (rel->r_offset
14017 + input_section->output_offset
14018 + input_section->output_section->vma);
14019 if (relocation + 0x80008000 <= 0xffffffff)
14020 {
14021 unsigned int insn1, insn2;
14022
14023 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14024 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14025 if ((insn1 & ~0xfffc) == LD_R2_0R12
14026 && insn2 == ADD_R2_R2_R12)
14027 {
95f0d0d2 14028 bfd_put_32 (input_bfd,
006589cf
AM
14029 ADDIS_R2_R12 + PPC_HA (relocation),
14030 contents + rel->r_offset);
95f0d0d2 14031 bfd_put_32 (input_bfd,
006589cf
AM
14032 ADDI_R2_R2 + PPC_LO (relocation),
14033 contents + rel->r_offset + 4);
14034 }
14035 }
14036 }
14037 break;
14038
52a82034
AM
14039 case R_PPC64_REL16_HA:
14040 /* If we are generating a non-PIC executable, edit
14041 . 0: addis 2,12,.TOC.-0b@ha
14042 . addi 2,2,.TOC.-0b@l
14043 used by ELFv2 global entry points to set up r2, to
14044 . lis 2,.TOC.@ha
14045 . addi 2,2,.TOC.@l
14046 if .TOC. is in range. */
0e1862bb 14047 if (!bfd_link_pic (info)
810d4e75 14048 && !info->traditional_format
006589cf 14049 && !htab->opd_abi
4f038ee5 14050 && rel->r_addend == d_offset
52a82034
AM
14051 && h != NULL && &h->elf == htab->elf.hgot
14052 && rel + 1 < relend
14053 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14054 && rel[1].r_offset == rel->r_offset + 4
14055 && rel[1].r_addend == rel->r_addend + 4
14056 && relocation + 0x80008000 <= 0xffffffff)
14057 {
14058 unsigned int insn1, insn2;
14059 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14060 insn1 = bfd_get_32 (input_bfd, contents + offset);
14061 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14062 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14063 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14064 {
14065 r_type = R_PPC64_ADDR16_HA;
14066 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14067 rel->r_addend -= d_offset;
14068 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14069 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14070 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14071 }
14072 }
14073 break;
411e1bfb
AM
14074 }
14075
14076 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14077 insn = 0;
b25116a9
AM
14078 max_br_offset = 1 << 25;
14079 addend = rel->r_addend;
bc30df16 14080 reloc_dest = DEST_NORMAL;
65f38f15 14081 switch (r_type)
5bd4f169
AM
14082 {
14083 default:
65f38f15 14084 break;
5bd4f169 14085
3b421ab3
AM
14086 case R_PPC64_TOCSAVE:
14087 if (relocation + addend == (rel->r_offset
14088 + input_section->output_offset
14089 + input_section->output_section->vma)
14090 && tocsave_find (htab, NO_INSERT,
14091 &local_syms, rel, input_bfd))
14092 {
14093 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14094 if (insn == NOP
14095 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14096 bfd_put_32 (input_bfd,
14097 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14098 contents + rel->r_offset);
14099 }
14100 break;
14101
65f38f15
AM
14102 /* Branch taken prediction relocations. */
14103 case R_PPC64_ADDR14_BRTAKEN:
14104 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14105 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14106 /* Fall through. */
65f38f15 14107
86c76c7b 14108 /* Branch not taken prediction relocations. */
65f38f15
AM
14109 case R_PPC64_ADDR14_BRNTAKEN:
14110 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14111 insn |= bfd_get_32 (input_bfd,
411e1bfb 14112 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14113 /* Fall through. */
86c76c7b 14114
b25116a9
AM
14115 case R_PPC64_REL14:
14116 max_br_offset = 1 << 15;
1a0670f3 14117 /* Fall through. */
5bd4f169 14118
65f38f15 14119 case R_PPC64_REL24:
ad8e1ba5
AM
14120 /* Calls to functions with a different TOC, such as calls to
14121 shared objects, need to alter the TOC pointer. This is
14122 done using a linkage stub. A REL24 branching to these
14123 linkage stubs needs to be followed by a nop, as the nop
14124 will be replaced with an instruction to restore the TOC
14125 base pointer. */
8387904d 14126 fdh = h;
b31867b6
AM
14127 if (h != NULL
14128 && h->oh != NULL
14129 && h->oh->is_func_descriptor)
14130 fdh = ppc_follow_link (h->oh);
31c76678
DK
14131 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14132 htab);
6abec6bc 14133 if (stub_entry != NULL
ad8e1ba5 14134 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 14135 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
14136 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14137 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 14138 {
b25116a9 14139 bfd_boolean can_plt_call = FALSE;
721956f4 14140
ba8ca3e7
AM
14141 /* All of these stubs will modify r2, so there must be a
14142 branch and link followed by a nop. The nop is
14143 replaced by an insn to restore r2. */
eea6121a 14144 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14145 {
ba8ca3e7
AM
14146 unsigned long br;
14147
14148 br = bfd_get_32 (input_bfd,
14149 contents + rel->r_offset);
14150 if ((br & 1) != 0)
41bd81ab 14151 {
ba8ca3e7
AM
14152 unsigned long nop;
14153
14154 nop = bfd_get_32 (input_bfd,
14155 contents + rel->r_offset + 4);
14156 if (nop == NOP
14157 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 14158 {
ba8ca3e7
AM
14159 if (h != NULL
14160 && (h == htab->tls_get_addr_fd
14161 || h == htab->tls_get_addr)
7c9cf415 14162 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14163 {
14164 /* Special stub used, leave nop alone. */
14165 }
14166 else
a078d95a
AM
14167 bfd_put_32 (input_bfd,
14168 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14169 contents + rel->r_offset + 4);
14170 can_plt_call = TRUE;
a7f2871e 14171 }
41bd81ab 14172 }
5bd4f169 14173 }
721956f4 14174
ba8ca3e7 14175 if (!can_plt_call && h != NULL)
721956f4 14176 {
ba8ca3e7
AM
14177 const char *name = h->elf.root.root.string;
14178
14179 if (*name == '.')
14180 ++name;
14181
14182 if (strncmp (name, "__libc_start_main", 17) == 0
14183 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14184 {
ba8ca3e7
AM
14185 /* Allow crt1 branch to go via a toc adjusting
14186 stub. Other calls that never return could do
14187 the same, if we could detect such. */
b25116a9 14188 can_plt_call = TRUE;
6ab189d5 14189 }
ba8ca3e7
AM
14190 }
14191
14192 if (!can_plt_call)
14193 {
14194 /* g++ as of 20130507 emits self-calls without a
14195 following nop. This is arguably wrong since we
14196 have conflicting information. On the one hand a
14197 global symbol and on the other a local call
14198 sequence, but don't error for this special case.
14199 It isn't possible to cheaply verify we have
14200 exactly such a call. Allow all calls to the same
14201 section. */
14202 asection *code_sec = sec;
14203
14204 if (get_opd_info (sec) != NULL)
ad8e1ba5 14205 {
ba8ca3e7
AM
14206 bfd_vma off = (relocation + addend
14207 - sec->output_section->vma
14208 - sec->output_offset);
bc30df16 14209
ba8ca3e7 14210 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14211 }
ba8ca3e7
AM
14212 if (code_sec == input_section)
14213 can_plt_call = TRUE;
14214 }
14215
14216 if (!can_plt_call)
14217 {
4805fc55
AM
14218 if (stub_entry->stub_type == ppc_stub_plt_call
14219 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14220 info->callbacks->einfo
695344c0 14221 /* xgettext:c-format */
4805fc55
AM
14222 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14223 "recompile with -fPIC\n"),
14224 input_bfd, input_section, rel->r_offset, sym_name);
14225 else
14226 info->callbacks->einfo
695344c0 14227 /* xgettext:c-format */
4805fc55
AM
14228 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14229 "(-mcmodel=small toc adjust stub)\n"),
14230 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14231
14232 bfd_set_error (bfd_error_bad_value);
14233 ret = FALSE;
721956f4
AM
14234 }
14235
b25116a9 14236 if (can_plt_call
794e51c0
AM
14237 && (stub_entry->stub_type == ppc_stub_plt_call
14238 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14239 unresolved_reloc = FALSE;
14240 }
14241
6abec6bc
AM
14242 if ((stub_entry == NULL
14243 || stub_entry->stub_type == ppc_stub_long_branch
14244 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14245 && get_opd_info (sec) != NULL)
14246 {
14247 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14248 bfd_vma off = (relocation + addend
14249 - sec->output_section->vma
14250 - sec->output_offset);
aef36ac1 14251 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14252 if (dest != (bfd_vma) -1)
14253 {
14254 relocation = dest;
14255 addend = 0;
bc30df16 14256 reloc_dest = DEST_OPD;
8387904d
AM
14257 }
14258 }
14259
b25116a9
AM
14260 /* If the branch is out of reach we ought to have a long
14261 branch stub. */
14262 from = (rel->r_offset
14263 + input_section->output_offset
14264 + input_section->output_section->vma);
14265
6911b7dc
AM
14266 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14267 ? fdh->elf.other
14268 : sym->st_other);
14269
6abec6bc
AM
14270 if (stub_entry != NULL
14271 && (stub_entry->stub_type == ppc_stub_long_branch
14272 || stub_entry->stub_type == ppc_stub_plt_branch)
14273 && (r_type == R_PPC64_ADDR14_BRTAKEN
14274 || r_type == R_PPC64_ADDR14_BRNTAKEN
14275 || (relocation + addend - from + max_br_offset
14276 < 2 * max_br_offset)))
14277 /* Don't use the stub if this branch is in range. */
14278 stub_entry = NULL;
b25116a9
AM
14279
14280 if (stub_entry != NULL)
14281 {
14282 /* Munge up the value and addend so that we call the stub
14283 rather than the procedure directly. */
a4b6fadd
AM
14284 asection *stub_sec = stub_entry->group->stub_sec;
14285
14286 if (stub_entry->stub_type == ppc_stub_save_res)
14287 relocation += (stub_sec->output_offset
14288 + stub_sec->output_section->vma
14289 + stub_sec->size - htab->sfpr->size
14290 - htab->sfpr->output_offset
14291 - htab->sfpr->output_section->vma);
14292 else
14293 relocation = (stub_entry->stub_offset
14294 + stub_sec->output_offset
14295 + stub_sec->output_section->vma);
b25116a9 14296 addend = 0;
bc30df16 14297 reloc_dest = DEST_STUB;
3b421ab3 14298
794e51c0
AM
14299 if ((stub_entry->stub_type == ppc_stub_plt_call
14300 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14301 && (ALWAYS_EMIT_R2SAVE
14302 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14303 && rel + 1 < relend
14304 && rel[1].r_offset == rel->r_offset + 4
14305 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14306 relocation += 4;
b25116a9
AM
14307 }
14308
14309 if (insn != 0)
14310 {
794e51c0 14311 if (is_isa_v2)
721956f4 14312 {
b25116a9
AM
14313 /* Set 'a' bit. This is 0b00010 in BO field for branch
14314 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14315 for branch on CTR insns (BO == 1a00t or 1a01t). */
14316 if ((insn & (0x14 << 21)) == (0x04 << 21))
14317 insn |= 0x02 << 21;
14318 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14319 insn |= 0x08 << 21;
14320 else
14321 break;
14322 }
14323 else
14324 {
14325 /* Invert 'y' bit if not the default. */
4cc603a5 14326 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14327 insn ^= 0x01 << 21;
721956f4 14328 }
b25116a9 14329
95f0d0d2 14330 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14331 }
e86ce104 14332
06da1e8e
AM
14333 /* NOP out calls to undefined weak functions.
14334 We can thus call a weak function without first
14335 checking whether the function is defined. */
b25116a9 14336 else if (h != NULL
039b3fef 14337 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14338 && h->elf.dynindx == -1
b25116a9
AM
14339 && r_type == R_PPC64_REL24
14340 && relocation == 0
4cc603a5 14341 && addend == 0)
e86ce104 14342 {
95f0d0d2 14343 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14344 goto copy_reloc;
e86ce104 14345 }
65f38f15
AM
14346 break;
14347 }
5bd4f169 14348
65f38f15 14349 /* Set `addend'. */
411e1bfb 14350 tls_type = 0;
65f38f15
AM
14351 switch (r_type)
14352 {
14353 default:
25f53a85 14354 info->callbacks->einfo
695344c0 14355 /* xgettext:c-format */
bc30df16 14356 (_("%P: %B: unknown relocation type %d for `%T'\n"),
d003868e 14357 input_bfd, (int) r_type, sym_name);
5bd4f169 14358
65f38f15 14359 bfd_set_error (bfd_error_bad_value);
b34976b6 14360 ret = FALSE;
c316a17c 14361 goto copy_reloc;
5bd4f169 14362
65f38f15 14363 case R_PPC64_NONE:
411e1bfb 14364 case R_PPC64_TLS:
727fc41e
AM
14365 case R_PPC64_TLSGD:
14366 case R_PPC64_TLSLD:
3b421ab3 14367 case R_PPC64_TOCSAVE:
04c9666a
AM
14368 case R_PPC64_GNU_VTINHERIT:
14369 case R_PPC64_GNU_VTENTRY:
006589cf 14370 case R_PPC64_ENTRY:
c316a17c 14371 goto copy_reloc;
5bd4f169
AM
14372
14373 /* GOT16 relocations. Like an ADDR16 using the symbol's
14374 address in the GOT as relocation value instead of the
411e1bfb 14375 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14376 symbol and put the symbol value there. */
411e1bfb
AM
14377 case R_PPC64_GOT_TLSGD16:
14378 case R_PPC64_GOT_TLSGD16_LO:
14379 case R_PPC64_GOT_TLSGD16_HI:
14380 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14381 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14382 goto dogot;
14383
14384 case R_PPC64_GOT_TLSLD16:
14385 case R_PPC64_GOT_TLSLD16_LO:
14386 case R_PPC64_GOT_TLSLD16_HI:
14387 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14388 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14389 goto dogot;
14390
14391 case R_PPC64_GOT_TPREL16_DS:
14392 case R_PPC64_GOT_TPREL16_LO_DS:
14393 case R_PPC64_GOT_TPREL16_HI:
14394 case R_PPC64_GOT_TPREL16_HA:
14395 tls_type = TLS_TLS | TLS_TPREL;
14396 goto dogot;
14397
14398 case R_PPC64_GOT_DTPREL16_DS:
14399 case R_PPC64_GOT_DTPREL16_LO_DS:
14400 case R_PPC64_GOT_DTPREL16_HI:
14401 case R_PPC64_GOT_DTPREL16_HA:
14402 tls_type = TLS_TLS | TLS_DTPREL;
14403 goto dogot;
14404
65f38f15
AM
14405 case R_PPC64_GOT16:
14406 case R_PPC64_GOT16_LO:
14407 case R_PPC64_GOT16_HI:
14408 case R_PPC64_GOT16_HA:
14409 case R_PPC64_GOT16_DS:
14410 case R_PPC64_GOT16_LO_DS:
411e1bfb 14411 dogot:
5bd4f169
AM
14412 {
14413 /* Relocation is to the entry for this symbol in the global
14414 offset table. */
e717da7e 14415 asection *got;
d881513a 14416 bfd_vma *offp;
5bd4f169 14417 bfd_vma off;
d881513a 14418 unsigned long indx = 0;
927be08e 14419 struct got_entry *ent;
65f38f15 14420
d881513a
AM
14421 if (tls_type == (TLS_TLS | TLS_LD)
14422 && (h == NULL
f5385ebf 14423 || !h->elf.def_dynamic))
927be08e 14424 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14425 else
5bd4f169 14426 {
5bd4f169 14427
d881513a
AM
14428 if (h != NULL)
14429 {
14430 bfd_boolean dyn = htab->elf.dynamic_sections_created;
0e1862bb 14431 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
039b3fef 14432 &h->elf)
0e1862bb 14433 || (bfd_link_pic (info)
afe397ea 14434 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
d881513a
AM
14435 /* This is actually a static link, or it is a
14436 -Bsymbolic link and the symbol is defined
14437 locally, or the symbol was forced to be local
14438 because of a version file. */
14439 ;
14440 else
14441 {
14acf4dc 14442 BFD_ASSERT (h->elf.dynindx != -1);
039b3fef 14443 indx = h->elf.dynindx;
d881513a
AM
14444 unresolved_reloc = FALSE;
14445 }
039b3fef 14446 ent = h->elf.got.glist;
d881513a 14447 }
411e1bfb 14448 else
5bd4f169 14449 {
d881513a
AM
14450 if (local_got_ents == NULL)
14451 abort ();
14452 ent = local_got_ents[r_symndx];
5bd4f169 14453 }
d881513a
AM
14454
14455 for (; ent != NULL; ent = ent->next)
31c76678 14456 if (ent->addend == orig_rel.r_addend
e717da7e 14457 && ent->owner == input_bfd
d881513a
AM
14458 && ent->tls_type == tls_type)
14459 break;
5bd4f169 14460 }
411e1bfb 14461
927be08e
AM
14462 if (ent == NULL)
14463 abort ();
14464 if (ent->is_indirect)
14465 ent = ent->got.ent;
14466 offp = &ent->got.offset;
14467 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14468 if (got == NULL)
14469 abort ();
14470
411e1bfb
AM
14471 /* The offset must always be a multiple of 8. We use the
14472 least significant bit to record whether we have already
14473 processed this entry. */
d881513a 14474 off = *offp;
411e1bfb
AM
14475 if ((off & 1) != 0)
14476 off &= ~1;
5bd4f169
AM
14477 else
14478 {
411e1bfb
AM
14479 /* Generate relocs for the dynamic linker, except in
14480 the case of TLSLD where we'll use one entry per
14481 module. */
25f23106
AM
14482 asection *relgot;
14483 bfd_boolean ifunc;
e717da7e 14484
d881513a 14485 *offp = off | 1;
25f23106
AM
14486 relgot = NULL;
14487 ifunc = (h != NULL
14488 ? h->elf.type == STT_GNU_IFUNC
14489 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14490 if (ifunc)
33e44f2e 14491 relgot = htab->elf.irelplt;
0e1862bb 14492 else if ((bfd_link_pic (info) || indx != 0)
19e08130
AM
14493 && (h == NULL
14494 || (tls_type == (TLS_TLS | TLS_LD)
14495 && !h->elf.def_dynamic)
14496 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14497 || h->elf.root.type != bfd_link_hash_undefweak))
14498 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14499 if (relgot != NULL)
5bd4f169 14500 {
e717da7e
AM
14501 outrel.r_offset = (got->output_section->vma
14502 + got->output_offset
411e1bfb 14503 + off);
4cc603a5 14504 outrel.r_addend = addend;
d881513a 14505 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14506 {
411e1bfb 14507 outrel.r_addend = 0;
e515b051 14508 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14509 if (tls_type == (TLS_TLS | TLS_GD))
14510 {
e717da7e
AM
14511 loc = relgot->contents;
14512 loc += (relgot->reloc_count++
d881513a
AM
14513 * sizeof (Elf64_External_Rela));
14514 bfd_elf64_swap_reloca_out (output_bfd,
14515 &outrel, loc);
e515b051 14516 outrel.r_offset += 8;
4cc603a5 14517 outrel.r_addend = addend;
d881513a
AM
14518 outrel.r_info
14519 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14520 }
411e1bfb 14521 }
951fd09b 14522 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14523 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14524 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14525 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14526 else if (indx != 0)
14527 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14528 else
81407a69 14529 {
25f23106
AM
14530 if (ifunc)
14531 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14532 else
14533 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14534
14535 /* Write the .got section contents for the sake
14536 of prelink. */
e717da7e 14537 loc = got->contents + off;
23fbd6fa
JJ
14538 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14539 loc);
81407a69 14540 }
81407a69
AM
14541
14542 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14543 {
14544 outrel.r_addend += relocation;
14545 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14546 {
14547 if (htab->elf.tls_sec == NULL)
14548 outrel.r_addend = 0;
14549 else
14550 outrel.r_addend -= htab->elf.tls_sec->vma;
14551 }
e515b051 14552 }
e717da7e
AM
14553 loc = relgot->contents;
14554 loc += (relgot->reloc_count++
411e1bfb
AM
14555 * sizeof (Elf64_External_Rela));
14556 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14557 }
14558
ad8e1ba5 14559 /* Init the .got section contents here if we're not
81407a69 14560 emitting a reloc. */
d881513a 14561 else
411e1bfb 14562 {
4cc603a5 14563 relocation += addend;
7b609f53
AM
14564 if (tls_type == (TLS_TLS | TLS_LD))
14565 relocation = 1;
14566 else if (tls_type != 0)
411e1bfb 14567 {
989f9879
AM
14568 if (htab->elf.tls_sec == NULL)
14569 relocation = 0;
14570 else
14571 {
14572 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14573 if (tls_type == (TLS_TLS | TLS_TPREL))
14574 relocation += DTP_OFFSET - TP_OFFSET;
14575 }
5bd4f169 14576
7b609f53
AM
14577 if (tls_type == (TLS_TLS | TLS_GD))
14578 {
14579 bfd_put_64 (output_bfd, relocation,
e717da7e 14580 got->contents + off + 8);
7b609f53
AM
14581 relocation = 1;
14582 }
411e1bfb 14583 }
7b609f53 14584
411e1bfb 14585 bfd_put_64 (output_bfd, relocation,
e717da7e 14586 got->contents + off);
5bd4f169
AM
14587 }
14588 }
14589
65f38f15
AM
14590 if (off >= (bfd_vma) -2)
14591 abort ();
14592
bf102f86 14593 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14594 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14595 }
65f38f15
AM
14596 break;
14597
14598 case R_PPC64_PLT16_HA:
14599 case R_PPC64_PLT16_HI:
14600 case R_PPC64_PLT16_LO:
14601 case R_PPC64_PLT32:
14602 case R_PPC64_PLT64:
14603 /* Relocation is to the entry for this symbol in the
14604 procedure linkage table. */
cbf95972
AM
14605 {
14606 struct plt_entry **plt_list = NULL;
14607 if (h != NULL)
14608 plt_list = &h->elf.plt.plist;
14609 else if (local_got_ents != NULL)
14610 {
14611 struct plt_entry **local_plt = (struct plt_entry **)
14612 (local_got_ents + symtab_hdr->sh_info);
14613 unsigned char *local_got_tls_masks = (unsigned char *)
14614 (local_plt + symtab_hdr->sh_info);
14615 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14616 plt_list = local_plt + r_symndx;
14617 }
14618 if (plt_list)
14619 {
14620 struct plt_entry *ent;
65f38f15 14621
cbf95972
AM
14622 for (ent = *plt_list; ent != NULL; ent = ent->next)
14623 if (ent->plt.offset != (bfd_vma) -1
14624 && ent->addend == orig_rel.r_addend)
14625 {
14626 asection *plt;
14627
14628 plt = htab->elf.splt;
14629 if (!htab->elf.dynamic_sections_created
14630 || h == NULL
14631 || h->elf.dynindx == -1)
14632 plt = htab->elf.iplt;
14633 relocation = (plt->output_section->vma
14634 + plt->output_offset
14635 + ent->plt.offset);
14636 addend = 0;
14637 unresolved_reloc = FALSE;
14638 break;
14639 }
14640 }
14641 }
65f38f15 14642 break;
5bd4f169 14643
0b13192e
AM
14644 case R_PPC64_TOC:
14645 /* Relocation value is TOC base. */
14646 relocation = TOCstart;
cf35638d 14647 if (r_symndx == STN_UNDEF)
6f20ed8a 14648 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14649 else if (unresolved_reloc)
14650 ;
6f20ed8a
AM
14651 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14652 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14653 else
14654 unresolved_reloc = TRUE;
ab96bf03 14655 goto dodyn;
0b13192e 14656
5bd4f169
AM
14657 /* TOC16 relocs. We want the offset relative to the TOC base,
14658 which is the address of the start of the TOC plus 0x8000.
14659 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14660 in this order. */
65f38f15
AM
14661 case R_PPC64_TOC16:
14662 case R_PPC64_TOC16_LO:
14663 case R_PPC64_TOC16_HI:
14664 case R_PPC64_TOC16_DS:
14665 case R_PPC64_TOC16_LO_DS:
14666 case R_PPC64_TOC16_HA:
6f20ed8a 14667 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14668 break;
14669
14670 /* Relocate against the beginning of the section. */
65f38f15
AM
14671 case R_PPC64_SECTOFF:
14672 case R_PPC64_SECTOFF_LO:
14673 case R_PPC64_SECTOFF_HI:
14674 case R_PPC64_SECTOFF_DS:
14675 case R_PPC64_SECTOFF_LO_DS:
14676 case R_PPC64_SECTOFF_HA:
4ce794b7 14677 if (sec != NULL)
65f38f15 14678 addend -= sec->output_section->vma;
5bd4f169
AM
14679 break;
14680
25f23106
AM
14681 case R_PPC64_REL16:
14682 case R_PPC64_REL16_LO:
14683 case R_PPC64_REL16_HI:
14684 case R_PPC64_REL16_HA:
a680de9a 14685 case R_PPC64_REL16DX_HA:
25f23106
AM
14686 break;
14687
721956f4
AM
14688 case R_PPC64_REL14:
14689 case R_PPC64_REL14_BRNTAKEN:
14690 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
14691 case R_PPC64_REL24:
14692 break;
14693
411e1bfb
AM
14694 case R_PPC64_TPREL16:
14695 case R_PPC64_TPREL16_LO:
14696 case R_PPC64_TPREL16_HI:
14697 case R_PPC64_TPREL16_HA:
14698 case R_PPC64_TPREL16_DS:
14699 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14700 case R_PPC64_TPREL16_HIGH:
14701 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14702 case R_PPC64_TPREL16_HIGHER:
14703 case R_PPC64_TPREL16_HIGHERA:
14704 case R_PPC64_TPREL16_HIGHEST:
14705 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14706 if (h != NULL
14707 && h->elf.root.type == bfd_link_hash_undefweak
14708 && h->elf.dynindx == -1)
14709 {
14710 /* Make this relocation against an undefined weak symbol
14711 resolve to zero. This is really just a tweak, since
14712 code using weak externs ought to check that they are
14713 defined before using them. */
14714 bfd_byte *p = contents + rel->r_offset - d_offset;
14715
95f0d0d2 14716 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
14717 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14718 if (insn != 0)
95f0d0d2 14719 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
14720 break;
14721 }
989f9879
AM
14722 if (htab->elf.tls_sec != NULL)
14723 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
0e1862bb 14724 if (bfd_link_pic (info))
411e1bfb
AM
14725 /* The TPREL16 relocs shouldn't really be used in shared
14726 libs as they will result in DT_TEXTREL being set, but
14727 support them anyway. */
14728 goto dodyn;
14729 break;
14730
14731 case R_PPC64_DTPREL16:
14732 case R_PPC64_DTPREL16_LO:
14733 case R_PPC64_DTPREL16_HI:
14734 case R_PPC64_DTPREL16_HA:
14735 case R_PPC64_DTPREL16_DS:
14736 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14737 case R_PPC64_DTPREL16_HIGH:
14738 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14739 case R_PPC64_DTPREL16_HIGHER:
14740 case R_PPC64_DTPREL16_HIGHERA:
14741 case R_PPC64_DTPREL16_HIGHEST:
14742 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14743 if (htab->elf.tls_sec != NULL)
14744 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14745 break;
14746
45965137
AM
14747 case R_PPC64_ADDR64_LOCAL:
14748 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14749 ? h->elf.other
14750 : sym->st_other);
14751 break;
14752
e515b051
AM
14753 case R_PPC64_DTPMOD64:
14754 relocation = 1;
14755 addend = 0;
14756 goto dodyn;
14757
411e1bfb 14758 case R_PPC64_TPREL64:
989f9879
AM
14759 if (htab->elf.tls_sec != NULL)
14760 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14761 goto dodyn;
14762
14763 case R_PPC64_DTPREL64:
989f9879
AM
14764 if (htab->elf.tls_sec != NULL)
14765 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 14766 /* Fall through. */
411e1bfb 14767
65f38f15
AM
14768 /* Relocations that may need to be propagated if this is a
14769 dynamic object. */
04c9666a 14770 case R_PPC64_REL30:
65f38f15
AM
14771 case R_PPC64_REL32:
14772 case R_PPC64_REL64:
14773 case R_PPC64_ADDR14:
14774 case R_PPC64_ADDR14_BRNTAKEN:
14775 case R_PPC64_ADDR14_BRTAKEN:
14776 case R_PPC64_ADDR16:
14777 case R_PPC64_ADDR16_DS:
14778 case R_PPC64_ADDR16_HA:
14779 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14780 case R_PPC64_ADDR16_HIGH:
14781 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14782 case R_PPC64_ADDR16_HIGHER:
14783 case R_PPC64_ADDR16_HIGHERA:
14784 case R_PPC64_ADDR16_HIGHEST:
14785 case R_PPC64_ADDR16_HIGHESTA:
14786 case R_PPC64_ADDR16_LO:
14787 case R_PPC64_ADDR16_LO_DS:
14788 case R_PPC64_ADDR24:
65f38f15
AM
14789 case R_PPC64_ADDR32:
14790 case R_PPC64_ADDR64:
14791 case R_PPC64_UADDR16:
14792 case R_PPC64_UADDR32:
14793 case R_PPC64_UADDR64:
411e1bfb 14794 dodyn:
5d1634d7 14795 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14796 break;
14797
41bd81ab
AM
14798 if (NO_OPD_RELOCS && is_opd)
14799 break;
14800
8a9e8e72
AM
14801 if (bfd_link_pic (info)
14802 ? ((h != NULL && pc_dynrelocs (h))
14803 || must_be_dyn_reloc (info, r_type))
14804 : (h != NULL
14805 ? h->dyn_relocs != NULL
d311bc8b 14806 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 14807 {
b34976b6 14808 bfd_boolean skip, relocate;
65f38f15 14809 asection *sreloc;
1cf1f670 14810 bfd_vma out_off;
65f38f15
AM
14811
14812 /* When generating a dynamic object, these relocations
14813 are copied into the output file to be resolved at run
14814 time. */
14815
b34976b6
AM
14816 skip = FALSE;
14817 relocate = FALSE;
65f38f15 14818
1cf1f670
AM
14819 out_off = _bfd_elf_section_offset (output_bfd, info,
14820 input_section, rel->r_offset);
14821 if (out_off == (bfd_vma) -1)
b34976b6 14822 skip = TRUE;
1cf1f670 14823 else if (out_off == (bfd_vma) -2)
b34976b6 14824 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14825 out_off += (input_section->output_section->vma
14826 + input_section->output_offset);
14827 outrel.r_offset = out_off;
411e1bfb 14828 outrel.r_addend = rel->r_addend;
65f38f15 14829
1cf1f670
AM
14830 /* Optimize unaligned reloc use. */
14831 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14832 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14833 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14834 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14835 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14836 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14837 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14838 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14839 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14840
65f38f15 14841 if (skip)
0bb2d96a 14842 memset (&outrel, 0, sizeof outrel);
afe397ea 14843 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14844 && !is_opd
14845 && r_type != R_PPC64_TOC)
14acf4dc
MR
14846 {
14847 BFD_ASSERT (h->elf.dynindx != -1);
14848 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14849 }
65f38f15
AM
14850 else
14851 {
41bd81ab
AM
14852 /* This symbol is local, or marked to become local,
14853 or this is an opd section reloc which must point
14854 at a local function. */
65f38f15 14855 outrel.r_addend += relocation;
e86ce104 14856 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14857 {
3fad3c7c 14858 if (is_opd && h != NULL)
afbe61cf
AM
14859 {
14860 /* Lie about opd entries. This case occurs
14861 when building shared libraries and we
14862 reference a function in another shared
3fad3c7c
AM
14863 lib. The same thing happens for a weak
14864 definition in an application that's
14865 overridden by a strong definition in a
14866 shared lib. (I believe this is a generic
14867 bug in binutils handling of weak syms.)
14868 In these cases we won't use the opd
1e2f5b6e 14869 entry in this lib. */
b34976b6 14870 unresolved_reloc = FALSE;
afbe61cf 14871 }
25f23106
AM
14872 if (!is_opd
14873 && r_type == R_PPC64_ADDR64
14874 && (h != NULL
14875 ? h->elf.type == STT_GNU_IFUNC
14876 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14877 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14878 else
14879 {
14880 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14881
25f23106
AM
14882 /* We need to relocate .opd contents for ld.so.
14883 Prelink also wants simple and consistent rules
14884 for relocs. This make all RELATIVE relocs have
14885 *r_offset equal to r_addend. */
14886 relocate = TRUE;
14887 }
65f38f15
AM
14888 }
14889 else
14890 {
14891 long indx = 0;
14892
25f23106
AM
14893 if (h != NULL
14894 ? h->elf.type == STT_GNU_IFUNC
14895 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14896 {
25f53a85 14897 info->callbacks->einfo
695344c0 14898 /* xgettext:c-format */
bc30df16
AM
14899 (_("%P: %H: %s for indirect "
14900 "function `%T' unsupported\n"),
25f53a85 14901 input_bfd, input_section, rel->r_offset,
25f23106
AM
14902 ppc64_elf_howto_table[r_type]->name,
14903 sym_name);
14904 ret = FALSE;
14905 }
cf35638d 14906 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14907 ;
14908 else if (sec == NULL || sec->owner == NULL)
14909 {
14910 bfd_set_error (bfd_error_bad_value);
b34976b6 14911 return FALSE;
65f38f15
AM
14912 }
14913 else
14914 {
14915 asection *osec;
14916
14917 osec = sec->output_section;
14918 indx = elf_section_data (osec)->dynindx;
14919
74541ad4
AM
14920 if (indx == 0)
14921 {
14922 if ((osec->flags & SEC_READONLY) == 0
14923 && htab->elf.data_index_section != NULL)
14924 osec = htab->elf.data_index_section;
14925 else
14926 osec = htab->elf.text_index_section;
14927 indx = elf_section_data (osec)->dynindx;
14928 }
14929 BFD_ASSERT (indx != 0);
14930
65f38f15
AM
14931 /* We are turning this relocation into one
14932 against a section symbol, so subtract out
14933 the output section's address but not the
14934 offset of the input section in the output
14935 section. */
14936 outrel.r_addend -= osec->vma;
14937 }
14938
14939 outrel.r_info = ELF64_R_INFO (indx, r_type);
14940 }
14941 }
14942
14943 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14944 if (h != NULL
14945 ? h->elf.type == STT_GNU_IFUNC
14946 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
33e44f2e 14947 sreloc = htab->elf.irelplt;
65f38f15
AM
14948 if (sreloc == NULL)
14949 abort ();
14950
dfbb6ac9
AM
14951 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14952 >= sreloc->size)
14953 abort ();
947216bf
AM
14954 loc = sreloc->contents;
14955 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14956 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14957
14958 /* If this reloc is against an external symbol, it will
14959 be computed at runtime, so there's no need to do
81407a69
AM
14960 anything now. However, for the sake of prelink ensure
14961 that the section contents are a known value. */
65f38f15 14962 if (! relocate)
81407a69
AM
14963 {
14964 unresolved_reloc = FALSE;
14965 /* The value chosen here is quite arbitrary as ld.so
14966 ignores section contents except for the special
14967 case of .opd where the contents might be accessed
14968 before relocation. Choose zero, as that won't
14969 cause reloc overflow. */
14970 relocation = 0;
14971 addend = 0;
14972 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14973 to improve backward compatibility with older
14974 versions of ld. */
14975 if (r_type == R_PPC64_ADDR64)
14976 addend = outrel.r_addend;
14977 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 14978 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
14979 addend = (input_section->output_section->vma
14980 + input_section->output_offset
14981 + rel->r_offset);
81407a69 14982 }
65f38f15 14983 }
5bd4f169
AM
14984 break;
14985
65f38f15
AM
14986 case R_PPC64_COPY:
14987 case R_PPC64_GLOB_DAT:
14988 case R_PPC64_JMP_SLOT:
25f23106 14989 case R_PPC64_JMP_IREL:
65f38f15
AM
14990 case R_PPC64_RELATIVE:
14991 /* We shouldn't ever see these dynamic relocs in relocatable
14992 files. */
ae9a127f 14993 /* Fall through. */
65f38f15
AM
14994
14995 case R_PPC64_PLTGOT16:
14996 case R_PPC64_PLTGOT16_DS:
14997 case R_PPC64_PLTGOT16_HA:
14998 case R_PPC64_PLTGOT16_HI:
14999 case R_PPC64_PLTGOT16_LO:
15000 case R_PPC64_PLTGOT16_LO_DS:
15001 case R_PPC64_PLTREL32:
15002 case R_PPC64_PLTREL64:
15003 /* These ones haven't been implemented yet. */
15004
25f53a85 15005 info->callbacks->einfo
695344c0 15006 /* xgettext:c-format */
bc30df16 15007 (_("%P: %B: %s is not supported for `%T'\n"),
d003868e 15008 input_bfd,
4ce794b7 15009 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
15010
15011 bfd_set_error (bfd_error_invalid_operation);
b34976b6 15012 ret = FALSE;
c316a17c 15013 goto copy_reloc;
65f38f15 15014 }
5bd4f169 15015
67f0cbdb
AM
15016 /* Multi-instruction sequences that access the TOC can be
15017 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15018 to nop; addi rb,r2,x; */
15019 switch (r_type)
15020 {
15021 default:
15022 break;
15023
15024 case R_PPC64_GOT_TLSLD16_HI:
15025 case R_PPC64_GOT_TLSGD16_HI:
15026 case R_PPC64_GOT_TPREL16_HI:
15027 case R_PPC64_GOT_DTPREL16_HI:
15028 case R_PPC64_GOT16_HI:
15029 case R_PPC64_TOC16_HI:
15030 /* These relocs would only be useful if building up an
15031 offset to later add to r2, perhaps in an indexed
15032 addressing mode instruction. Don't try to optimize.
15033 Unfortunately, the possibility of someone building up an
15034 offset like this or even with the HA relocs, means that
15035 we need to check the high insn when optimizing the low
15036 insn. */
15037 break;
15038
15039 case R_PPC64_GOT_TLSLD16_HA:
15040 case R_PPC64_GOT_TLSGD16_HA:
15041 case R_PPC64_GOT_TPREL16_HA:
15042 case R_PPC64_GOT_DTPREL16_HA:
15043 case R_PPC64_GOT16_HA:
15044 case R_PPC64_TOC16_HA:
98528052 15045 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15046 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
15047 {
15048 bfd_byte *p = contents + (rel->r_offset & ~3);
15049 bfd_put_32 (input_bfd, NOP, p);
15050 }
67f0cbdb
AM
15051 break;
15052
15053 case R_PPC64_GOT_TLSLD16_LO:
15054 case R_PPC64_GOT_TLSGD16_LO:
15055 case R_PPC64_GOT_TPREL16_LO_DS:
15056 case R_PPC64_GOT_DTPREL16_LO_DS:
15057 case R_PPC64_GOT16_LO:
15058 case R_PPC64_GOT16_LO_DS:
15059 case R_PPC64_TOC16_LO:
15060 case R_PPC64_TOC16_LO_DS:
98528052 15061 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15062 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15063 {
15064 bfd_byte *p = contents + (rel->r_offset & ~3);
15065 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15066 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15067 {
15068 /* Transform addic to addi when we change reg. */
15069 insn &= ~((0x3f << 26) | (0x1f << 16));
15070 insn |= (14u << 26) | (2 << 16);
15071 }
15072 else
67f0cbdb 15073 {
98528052
AM
15074 insn &= ~(0x1f << 16);
15075 insn |= 2 << 16;
67f0cbdb 15076 }
560c8763 15077 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15078 }
15079 break;
15080 }
15081
65f38f15 15082 /* Do any further special processing. */
b80eed39 15083 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15
AM
15084 switch (r_type)
15085 {
15086 default:
15087 break;
15088
25f23106 15089 case R_PPC64_REL16_HA:
a680de9a 15090 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15091 case R_PPC64_ADDR16_HA:
15092 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15093 case R_PPC64_ADDR16_HIGHERA:
15094 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15095 case R_PPC64_TOC16_HA:
15096 case R_PPC64_SECTOFF_HA:
411e1bfb 15097 case R_PPC64_TPREL16_HA:
f9c6b907 15098 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15099 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15100 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15101 case R_PPC64_DTPREL16_HA:
15102 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15103 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15104 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15105 /* It's just possible that this symbol is a weak symbol
15106 that's not actually defined anywhere. In that case,
15107 'sec' would be NULL, and we should leave the symbol
15108 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15109 if (sec == NULL)
15110 break;
1a0670f3 15111 /* Fall through. */
5c5f6e17
AM
15112
15113 case R_PPC64_GOT16_HA:
15114 case R_PPC64_PLTGOT16_HA:
15115 case R_PPC64_PLT16_HA:
15116 case R_PPC64_GOT_TLSGD16_HA:
15117 case R_PPC64_GOT_TLSLD16_HA:
15118 case R_PPC64_GOT_TPREL16_HA:
15119 case R_PPC64_GOT_DTPREL16_HA:
15120 /* Add 0x10000 if sign bit in 0:15 is set.
15121 Bits 0:15 are not used. */
15122 addend += 0x8000;
65f38f15
AM
15123 break;
15124
15125 case R_PPC64_ADDR16_DS:
15126 case R_PPC64_ADDR16_LO_DS:
15127 case R_PPC64_GOT16_DS:
15128 case R_PPC64_GOT16_LO_DS:
15129 case R_PPC64_PLT16_LO_DS:
15130 case R_PPC64_SECTOFF_DS:
15131 case R_PPC64_SECTOFF_LO_DS:
15132 case R_PPC64_TOC16_DS:
15133 case R_PPC64_TOC16_LO_DS:
15134 case R_PPC64_PLTGOT16_DS:
15135 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15136 case R_PPC64_GOT_TPREL16_DS:
15137 case R_PPC64_GOT_TPREL16_LO_DS:
15138 case R_PPC64_GOT_DTPREL16_DS:
15139 case R_PPC64_GOT_DTPREL16_LO_DS:
15140 case R_PPC64_TPREL16_DS:
15141 case R_PPC64_TPREL16_LO_DS:
15142 case R_PPC64_DTPREL16_DS:
15143 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15144 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15145 mask = 3;
a680de9a
PB
15146 /* If this reloc is against an lq, lxv, or stxv insn, then
15147 the value must be a multiple of 16. This is somewhat of
15148 a hack, but the "correct" way to do this by defining _DQ
15149 forms of all the _DS relocs bloats all reloc switches in
15150 this file. It doesn't make much sense to use these
15151 relocs in data, so testing the insn should be safe. */
15152 if ((insn & (0x3f << 26)) == (56u << 26)
15153 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15154 mask = 15;
a680de9a
PB
15155 relocation += addend;
15156 addend = insn & (mask ^ 3);
15157 if ((relocation & mask) != 0)
65f38f15 15158 {
a680de9a 15159 relocation ^= relocation & mask;
25f53a85 15160 info->callbacks->einfo
695344c0 15161 /* xgettext:c-format */
8de848d8 15162 (_("%P: %H: error: %s not a multiple of %u\n"),
25f53a85 15163 input_bfd, input_section, rel->r_offset,
b80eed39 15164 howto->name,
adadcc0c 15165 mask + 1);
65f38f15 15166 bfd_set_error (bfd_error_bad_value);
b34976b6 15167 ret = FALSE;
c316a17c 15168 goto copy_reloc;
65f38f15
AM
15169 }
15170 break;
5bd4f169
AM
15171 }
15172
239e1f3a
AM
15173 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15174 because such sections are not SEC_ALLOC and thus ld.so will
15175 not process them. */
65f38f15 15176 if (unresolved_reloc
239e1f3a 15177 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15178 && h->elf.def_dynamic)
15179 && _bfd_elf_section_offset (output_bfd, info, input_section,
15180 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15181 {
25f53a85 15182 info->callbacks->einfo
695344c0 15183 /* xgettext:c-format */
bc30df16 15184 (_("%P: %H: unresolvable %s against `%T'\n"),
25f53a85 15185 input_bfd, input_section, rel->r_offset,
b80eed39 15186 howto->name,
039b3fef 15187 h->elf.root.root.string);
b34976b6 15188 ret = FALSE;
9c07fe7c 15189 }
5bd4f169 15190
b80eed39
AM
15191 /* 16-bit fields in insns mostly have signed values, but a
15192 few insns have 16-bit unsigned values. Really, we should
15193 have different reloc types. */
15194 if (howto->complain_on_overflow != complain_overflow_dont
15195 && howto->dst_mask == 0xffff
15196 && (input_section->flags & SEC_CODE) != 0)
15197 {
15198 enum complain_overflow complain = complain_overflow_signed;
15199
15200 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15201 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15202 complain = complain_overflow_bitfield;
15203 else if (howto->rightshift == 0
15204 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15205 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15206 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15207 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15208 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15209 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15210 complain = complain_overflow_unsigned;
15211 if (howto->complain_on_overflow != complain)
15212 {
15213 alt_howto = *howto;
15214 alt_howto.complain_on_overflow = complain;
15215 howto = &alt_howto;
15216 }
15217 }
15218
a680de9a
PB
15219 if (r_type == R_PPC64_REL16DX_HA)
15220 {
15221 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15222 if (rel->r_offset + 4 > input_section->size)
15223 r = bfd_reloc_outofrange;
15224 else
15225 {
15226 relocation += addend;
15227 relocation -= (rel->r_offset
15228 + input_section->output_offset
15229 + input_section->output_section->vma);
15230 relocation = (bfd_signed_vma) relocation >> 16;
15231 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15232 insn &= ~0x1fffc1;
15233 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15234 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15235 r = bfd_reloc_ok;
15236 if (relocation + 0x8000 > 0xffff)
15237 r = bfd_reloc_overflow;
15238 }
15239 }
15240 else
15241 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15242 rel->r_offset, relocation, addend);
5bd4f169 15243
ef60b7ff 15244 if (r != bfd_reloc_ok)
5bd4f169 15245 {
bc30df16 15246 char *more_info = NULL;
b80eed39 15247 const char *reloc_name = howto->name;
bc30df16
AM
15248
15249 if (reloc_dest != DEST_NORMAL)
15250 {
15251 more_info = bfd_malloc (strlen (reloc_name) + 8);
15252 if (more_info != NULL)
15253 {
15254 strcpy (more_info, reloc_name);
15255 strcat (more_info, (reloc_dest == DEST_OPD
15256 ? " (OPD)" : " (stub)"));
15257 reloc_name = more_info;
15258 }
15259 }
15260
cd27b276 15261 if (r == bfd_reloc_overflow)
5bd4f169 15262 {
8131c122
AM
15263 /* On code like "if (foo) foo();" don't report overflow
15264 on a branch to zero when foo is undefined. */
15265 if (!warned
15266 && (reloc_dest == DEST_STUB
15267 || !(h != NULL
15268 && (h->elf.root.type == bfd_link_hash_undefweak
15269 || h->elf.root.type == bfd_link_hash_undefined)
15270 && is_branch_reloc (r_type))))
1a72702b
AM
15271 info->callbacks->reloc_overflow (info, &h->elf.root,
15272 sym_name, reloc_name,
15273 orig_rel.r_addend,
15274 input_bfd, input_section,
15275 rel->r_offset);
ef60b7ff
AM
15276 }
15277 else
15278 {
25f53a85 15279 info->callbacks->einfo
695344c0 15280 /* xgettext:c-format */
bc30df16 15281 (_("%P: %H: %s against `%T': error %d\n"),
25f53a85 15282 input_bfd, input_section, rel->r_offset,
bc30df16 15283 reloc_name, sym_name, (int) r);
b34976b6 15284 ret = FALSE;
ef60b7ff 15285 }
bc30df16
AM
15286 if (more_info != NULL)
15287 free (more_info);
5bd4f169 15288 }
c316a17c
AM
15289 copy_reloc:
15290 if (wrel != rel)
15291 *wrel = *rel;
15292 }
15293
15294 if (wrel != rel)
15295 {
15296 Elf_Internal_Shdr *rel_hdr;
15297 size_t deleted = rel - wrel;
15298
15299 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15300 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15301 if (rel_hdr->sh_size == 0)
15302 {
15303 /* It is too late to remove an empty reloc section. Leave
15304 one NONE reloc.
15305 ??? What is wrong with an empty section??? */
15306 rel_hdr->sh_size = rel_hdr->sh_entsize;
15307 deleted -= 1;
15308 }
15309 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15310 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15311 input_section->reloc_count -= deleted;
5bd4f169
AM
15312 }
15313
645ea6a9
AM
15314 /* If we're emitting relocations, then shortly after this function
15315 returns, reloc offsets and addends for this section will be
15316 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15317 file rather than the input. Save a copy of the relocs for
15318 opd_entry_value. */
0e1862bb 15319 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15320 {
15321 bfd_size_type amt;
15322 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15323 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15324 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15325 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15326 if (rel == NULL)
15327 return FALSE;
15328 memcpy (rel, relocs, amt);
15329 }
5bd4f169
AM
15330 return ret;
15331}
15332
754021d0
AM
15333/* Adjust the value of any local symbols in opd sections. */
15334
6e0b88f1 15335static int
754021d0
AM
15336ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15337 const char *name ATTRIBUTE_UNUSED,
15338 Elf_Internal_Sym *elfsym,
15339 asection *input_sec,
15340 struct elf_link_hash_entry *h)
15341{
74f0fb50
AM
15342 struct _opd_sec_data *opd;
15343 long adjust;
754021d0
AM
15344 bfd_vma value;
15345
4025353c 15346 if (h != NULL)
6e0b88f1 15347 return 1;
4025353c 15348
74f0fb50
AM
15349 opd = get_opd_info (input_sec);
15350 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15351 return 1;
754021d0
AM
15352
15353 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15354 if (!bfd_link_relocatable (info))
754021d0
AM
15355 value -= input_sec->output_section->vma;
15356
51aecdc5 15357 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15358 if (adjust == -1)
6e0b88f1
AM
15359 return 2;
15360
15361 elfsym->st_value += adjust;
15362 return 1;
754021d0
AM
15363}
15364
5bd4f169
AM
15365/* Finish up dynamic symbol handling. We set the contents of various
15366 dynamic sections here. */
15367
b34976b6 15368static bfd_boolean
4ce794b7
AM
15369ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15370 struct bfd_link_info *info,
15371 struct elf_link_hash_entry *h,
ab6dce23 15372 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5bd4f169 15373{
65f38f15 15374 struct ppc_link_hash_table *htab;
8387904d
AM
15375 struct plt_entry *ent;
15376 Elf_Internal_Rela rela;
15377 bfd_byte *loc;
5bd4f169 15378
65f38f15 15379 htab = ppc_hash_table (info);
4dfe6ac6
NC
15380 if (htab == NULL)
15381 return FALSE;
5bd4f169 15382
8387904d
AM
15383 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15384 if (ent->plt.offset != (bfd_vma) -1)
15385 {
15386 /* This symbol has an entry in the procedure linkage
15387 table. Set it up. */
e054468f
AM
15388 if (!htab->elf.dynamic_sections_created
15389 || h->dynindx == -1)
15390 {
15391 BFD_ASSERT (h->type == STT_GNU_IFUNC
15392 && h->def_regular
15393 && (h->root.type == bfd_link_hash_defined
15394 || h->root.type == bfd_link_hash_defweak));
33e44f2e
AM
15395 rela.r_offset = (htab->elf.iplt->output_section->vma
15396 + htab->elf.iplt->output_offset
25f23106 15397 + ent->plt.offset);
ee67d69a
AM
15398 if (htab->opd_abi)
15399 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15400 else
15401 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
15402 rela.r_addend = (h->root.u.def.value
15403 + h->root.u.def.section->output_offset
15404 + h->root.u.def.section->output_section->vma
15405 + ent->addend);
33e44f2e
AM
15406 loc = (htab->elf.irelplt->contents
15407 + (htab->elf.irelplt->reloc_count++
25f23106 15408 * sizeof (Elf64_External_Rela)));
e054468f
AM
15409 }
15410 else
15411 {
33e44f2e
AM
15412 rela.r_offset = (htab->elf.splt->output_section->vma
15413 + htab->elf.splt->output_offset
25f23106 15414 + ent->plt.offset);
e054468f
AM
15415 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15416 rela.r_addend = ent->addend;
33e44f2e 15417 loc = (htab->elf.srelplt->contents
b9e5796b
AM
15418 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15419 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
e054468f 15420 }
8387904d 15421 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a345bc8d
AM
15422
15423 if (!htab->opd_abi)
15424 {
15425 if (!h->def_regular)
15426 {
15427 /* Mark the symbol as undefined, rather than as
15428 defined in glink. Leave the value if there were
15429 any relocations where pointer equality matters
15430 (this is a clue for the dynamic linker, to make
15431 function pointer comparisons work between an
15432 application and shared library), otherwise set it
15433 to zero. */
15434 sym->st_shndx = SHN_UNDEF;
15435 if (!h->pointer_equality_needed)
15436 sym->st_value = 0;
15437 else if (!h->ref_regular_nonweak)
15438 {
15439 /* This breaks function pointer comparisons, but
15440 that is better than breaking tests for a NULL
15441 function pointer. */
15442 sym->st_value = 0;
15443 }
15444 }
15445 }
8387904d 15446 }
5bd4f169 15447
f5385ebf 15448 if (h->needs_copy)
5bd4f169 15449 {
65f38f15 15450 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 15451
65f38f15
AM
15452 if (h->dynindx == -1
15453 || (h->root.type != bfd_link_hash_defined
15454 && h->root.type != bfd_link_hash_defweak)
4ce794b7 15455 || htab->relbss == NULL)
65f38f15 15456 abort ();
5bd4f169
AM
15457
15458 rela.r_offset = (h->root.u.def.value
15459 + h->root.u.def.section->output_section->vma
15460 + h->root.u.def.section->output_offset);
15461 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15462 rela.r_addend = 0;
4ce794b7
AM
15463 loc = htab->relbss->contents;
15464 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15465 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15466 }
15467
b34976b6 15468 return TRUE;
5bd4f169
AM
15469}
15470
65f38f15
AM
15471/* Used to decide how to sort relocs in an optimal manner for the
15472 dynamic linker, before writing them out. */
15473
15474static enum elf_reloc_type_class
7e612e98
AM
15475ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15476 const asection *rel_sec,
15477 const Elf_Internal_Rela *rela)
65f38f15 15478{
04c9666a 15479 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15480 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15481
33e44f2e 15482 if (rel_sec == htab->elf.irelplt)
7e612e98 15483 return reloc_class_ifunc;
a33d1f77 15484
4ce794b7 15485 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15486 switch (r_type)
65f38f15
AM
15487 {
15488 case R_PPC64_RELATIVE:
15489 return reloc_class_relative;
15490 case R_PPC64_JMP_SLOT:
15491 return reloc_class_plt;
15492 case R_PPC64_COPY:
15493 return reloc_class_copy;
15494 default:
15495 return reloc_class_normal;
15496 }
15497}
15498
5bd4f169
AM
15499/* Finish up the dynamic sections. */
15500
b34976b6 15501static bfd_boolean
4ce794b7
AM
15502ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15503 struct bfd_link_info *info)
5bd4f169 15504{
65f38f15
AM
15505 struct ppc_link_hash_table *htab;
15506 bfd *dynobj;
5bd4f169 15507 asection *sdyn;
5bd4f169 15508
65f38f15 15509 htab = ppc_hash_table (info);
4dfe6ac6
NC
15510 if (htab == NULL)
15511 return FALSE;
15512
65f38f15 15513 dynobj = htab->elf.dynobj;
3d4d4302 15514 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15515
65f38f15 15516 if (htab->elf.dynamic_sections_created)
5bd4f169 15517 {
5bd4f169
AM
15518 Elf64_External_Dyn *dyncon, *dynconend;
15519
33e44f2e 15520 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15521 abort ();
5bd4f169
AM
15522
15523 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15524 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15525 for (; dyncon < dynconend; dyncon++)
15526 {
15527 Elf_Internal_Dyn dyn;
19397422 15528 asection *s;
5bd4f169
AM
15529
15530 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15531
15532 switch (dyn.d_tag)
15533 {
65f38f15
AM
15534 default:
15535 continue;
5bd4f169 15536
5d1634d7 15537 case DT_PPC64_GLINK:
4ce794b7 15538 s = htab->glink;
6348e046 15539 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15540 /* We stupidly defined DT_PPC64_GLINK to be the start
15541 of glink rather than the first entry point, which is
15542 what ld.so needs, and now have a bigger stub to
15543 support automatic multiple TOCs. */
b9e5796b 15544 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
5d1634d7
AM
15545 break;
15546
19397422
AM
15547 case DT_PPC64_OPD:
15548 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15549 if (s == NULL)
15550 continue;
15551 dyn.d_un.d_ptr = s->vma;
19397422
AM
15552 break;
15553
e8910a83
AM
15554 case DT_PPC64_OPT:
15555 if (htab->do_multi_toc && htab->multi_toc_needed)
15556 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15557 break;
15558
19397422
AM
15559 case DT_PPC64_OPDSZ:
15560 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15561 if (s == NULL)
15562 continue;
eea6121a 15563 dyn.d_un.d_val = s->size;
19397422
AM
15564 break;
15565
65f38f15 15566 case DT_PLTGOT:
33e44f2e 15567 s = htab->elf.splt;
6348e046 15568 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15569 break;
15570
15571 case DT_JMPREL:
33e44f2e 15572 s = htab->elf.srelplt;
6348e046 15573 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15574 break;
5bd4f169 15575
65f38f15 15576 case DT_PLTRELSZ:
33e44f2e 15577 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 15578 break;
5bd4f169 15579 }
5bd4f169 15580
65f38f15 15581 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15582 }
5bd4f169
AM
15583 }
15584
33e44f2e 15585 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
5d1634d7
AM
15586 {
15587 /* Fill in the first entry in the global offset table.
15588 We use it to hold the link-time TOCbase. */
15589 bfd_put_64 (output_bfd,
60ee0d4a 15590 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15591 htab->elf.sgot->contents);
5d1634d7
AM
15592
15593 /* Set .got entry size. */
33e44f2e 15594 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
15595 }
15596
33e44f2e 15597 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5d1634d7
AM
15598 {
15599 /* Set .plt entry size. */
33e44f2e 15600 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15601 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15602 }
15603
84f5d08e
AM
15604 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15605 brlt ourselves if emitrelocations. */
15606 if (htab->brlt != NULL
15607 && htab->brlt->reloc_count != 0
15608 && !_bfd_elf_link_output_relocs (output_bfd,
15609 htab->brlt,
d4730f92 15610 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15611 elf_section_data (htab->brlt)->relocs,
15612 NULL))
15613 return FALSE;
15614
176a0d42
AM
15615 if (htab->glink != NULL
15616 && htab->glink->reloc_count != 0
15617 && !_bfd_elf_link_output_relocs (output_bfd,
15618 htab->glink,
d4730f92 15619 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15620 elf_section_data (htab->glink)->relocs,
15621 NULL))
15622 return FALSE;
15623
58d180e8 15624 if (htab->glink_eh_frame != NULL
da44f4e5
AM
15625 && htab->glink_eh_frame->size != 0)
15626 {
15627 bfd_vma val;
15628 bfd_byte *p;
15629 asection *stub_sec;
15630
15631 p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15632 for (stub_sec = htab->params->stub_bfd->sections;
15633 stub_sec != NULL;
15634 stub_sec = stub_sec->next)
15635 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15636 {
15637 /* FDE length. */
15638 p += 4;
15639 /* CIE pointer. */
15640 p += 4;
15641 /* Offset to stub section. */
15642 val = (stub_sec->output_section->vma
15643 + stub_sec->output_offset);
15644 val -= (htab->glink_eh_frame->output_section->vma
15645 + htab->glink_eh_frame->output_offset
15646 + (p - htab->glink_eh_frame->contents));
15647 if (val + 0x80000000 > 0xffffffff)
15648 {
15649 info->callbacks->einfo
15650 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15651 stub_sec->name);
15652 return FALSE;
15653 }
15654 bfd_put_32 (dynobj, val, p);
15655 p += 4;
15656 /* stub section size. */
15657 p += 4;
15658 /* Augmentation. */
15659 p += 1;
15660 /* Pad. */
15661 p += 7;
15662 }
15663 if (htab->glink != NULL && htab->glink->size != 0)
15664 {
15665 /* FDE length. */
15666 p += 4;
15667 /* CIE pointer. */
15668 p += 4;
15669 /* Offset to .glink. */
15670 val = (htab->glink->output_section->vma
15671 + htab->glink->output_offset
15672 + 8);
15673 val -= (htab->glink_eh_frame->output_section->vma
15674 + htab->glink_eh_frame->output_offset
15675 + (p - htab->glink_eh_frame->contents));
15676 if (val + 0x80000000 > 0xffffffff)
15677 {
15678 info->callbacks->einfo
15679 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15680 htab->glink->name);
15681 return FALSE;
15682 }
15683 bfd_put_32 (dynobj, val, p);
15684 p += 4;
15685 /* .glink size. */
15686 p += 4;
15687 /* Augmentation. */
15688 p += 1;
15689 /* Ops. */
15690 p += 7;
15691 }
15692
15693 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15694 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15695 htab->glink_eh_frame,
15696 htab->glink_eh_frame->contents))
15697 return FALSE;
15698 }
58d180e8 15699
e717da7e 15700 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15701 since we didn't add them to DYNOBJ. We know dynobj is the first
15702 bfd. */
c72f2fb2 15703 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15704 {
15705 asection *s;
7b53ace3 15706
0c8d6e5c 15707 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15708 continue;
15709
e717da7e
AM
15710 s = ppc64_elf_tdata (dynobj)->got;
15711 if (s != NULL
eea6121a 15712 && s->size != 0
e717da7e
AM
15713 && s->output_section != bfd_abs_section_ptr
15714 && !bfd_set_section_contents (output_bfd, s->output_section,
15715 s->contents, s->output_offset,
eea6121a 15716 s->size))
e717da7e
AM
15717 return FALSE;
15718 s = ppc64_elf_tdata (dynobj)->relgot;
15719 if (s != NULL
eea6121a 15720 && s->size != 0
e717da7e
AM
15721 && s->output_section != bfd_abs_section_ptr
15722 && !bfd_set_section_contents (output_bfd, s->output_section,
15723 s->contents, s->output_offset,
eea6121a 15724 s->size))
e717da7e
AM
15725 return FALSE;
15726 }
f6c52c13 15727
b34976b6 15728 return TRUE;
5bd4f169
AM
15729}
15730
5bd4f169 15731#include "elf64-target.h"
7b8e7dad
AM
15732
15733/* FreeBSD support */
15734
15735#undef TARGET_LITTLE_SYM
15736#undef TARGET_LITTLE_NAME
15737
15738#undef TARGET_BIG_SYM
6d00b590 15739#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15740#undef TARGET_BIG_NAME
15741#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15742
15743#undef ELF_OSABI
15744#define ELF_OSABI ELFOSABI_FREEBSD
15745
15746#undef elf64_bed
15747#define elf64_bed elf64_powerpc_fbsd_bed
15748
15749#include "elf64-target.h"
15750
This page took 2.301155 seconds and 4 git commands to generate.