Put .dynbss and .rel.bss shortcuts in main elf hash table
[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
9d19e4fd 96#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 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 4056 asection *glink;
82bd7b59 4057 asection *sfpr;
4ce794b7
AM
4058 asection *brlt;
4059 asection *relbrlt;
58d180e8 4060 asection *glink_eh_frame;
ec338859 4061
8387904d
AM
4062 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4063 struct ppc_link_hash_entry *tls_get_addr;
4064 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 4065
927be08e
AM
4066 /* The size of reliplt used by got entry relocs. */
4067 bfd_size_type got_reli_size;
4068
9b5ecbd0 4069 /* Statistics. */
7341d5e2 4070 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 4071
ee75fd95
AM
4072 /* Number of stubs against global syms. */
4073 unsigned long stub_globals;
4074
ee67d69a
AM
4075 /* Set if we're linking code with function descriptors. */
4076 unsigned int opd_abi:1;
4077
4c52953f 4078 /* Support for multiple toc sections. */
33c0ec9d 4079 unsigned int do_multi_toc:1;
4c52953f 4080 unsigned int multi_toc_needed:1;
927be08e 4081 unsigned int second_toc_pass:1;
67f0cbdb 4082 unsigned int do_toc_opt:1;
4c52953f 4083
5d1634d7 4084 /* Set on error. */
99877b66 4085 unsigned int stub_error:1;
721956f4 4086
8c5b4e52
AM
4087 /* Whether func_desc_adjust needs to be run over symbols. */
4088 unsigned int need_func_desc_adj:1;
721956f4
AM
4089
4090 /* Incremented every time we size stubs. */
4091 unsigned int stub_iteration;
5d1634d7 4092
87d72d41
AM
4093 /* Small local sym cache. */
4094 struct sym_cache sym_cache;
65f38f15
AM
4095};
4096
4c52953f
AM
4097/* Rename some of the generic section flags to better document how they
4098 are used here. */
b0dddeec
AM
4099
4100/* Nonzero if this section has TLS related relocations. */
4101#define has_tls_reloc sec_flg0
4102
4103/* Nonzero if this section has a call to __tls_get_addr. */
4104#define has_tls_get_addr_call sec_flg1
4105
4106/* Nonzero if this section has any toc or got relocs. */
4107#define has_toc_reloc sec_flg2
4108
4109/* Nonzero if this section has a call to another section that uses
4110 the toc or got. */
d77c8a4b 4111#define makes_toc_func_call sec_flg3
b0dddeec
AM
4112
4113/* Recursion protection when determining above flag. */
d77c8a4b 4114#define call_check_in_progress sec_flg4
70cc837d 4115#define call_check_done sec_flg5
4c52953f 4116
65f38f15
AM
4117/* Get the ppc64 ELF linker hash table from a link_info structure. */
4118
4119#define ppc_hash_table(p) \
4dfe6ac6
NC
4120 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4121 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 4122
721956f4
AM
4123#define ppc_stub_hash_lookup(table, string, create, copy) \
4124 ((struct ppc_stub_hash_entry *) \
4125 bfd_hash_lookup ((table), (string), (create), (copy)))
4126
4127#define ppc_branch_hash_lookup(table, string, create, copy) \
4128 ((struct ppc_branch_hash_entry *) \
4129 bfd_hash_lookup ((table), (string), (create), (copy)))
4130
4131/* Create an entry in the stub hash table. */
4132
4133static struct bfd_hash_entry *
4ce794b7
AM
4134stub_hash_newfunc (struct bfd_hash_entry *entry,
4135 struct bfd_hash_table *table,
4136 const char *string)
721956f4
AM
4137{
4138 /* Allocate the structure if it has not already been allocated by a
4139 subclass. */
4140 if (entry == NULL)
4141 {
4142 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4143 if (entry == NULL)
4144 return entry;
4145 }
4146
4147 /* Call the allocation method of the superclass. */
4148 entry = bfd_hash_newfunc (entry, table, string);
4149 if (entry != NULL)
4150 {
4151 struct ppc_stub_hash_entry *eh;
4152
4153 /* Initialize the local fields. */
4154 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4155 eh->stub_type = ppc_stub_none;
6f20ed8a 4156 eh->group = NULL;
721956f4
AM
4157 eh->stub_offset = 0;
4158 eh->target_value = 0;
4159 eh->target_section = NULL;
721956f4 4160 eh->h = NULL;
6911b7dc 4161 eh->plt_ent = NULL;
6911b7dc 4162 eh->other = 0;
721956f4
AM
4163 }
4164
4165 return entry;
4166}
4167
4168/* Create an entry in the branch hash table. */
4169
4170static struct bfd_hash_entry *
4ce794b7
AM
4171branch_hash_newfunc (struct bfd_hash_entry *entry,
4172 struct bfd_hash_table *table,
4173 const char *string)
721956f4
AM
4174{
4175 /* Allocate the structure if it has not already been allocated by a
4176 subclass. */
4177 if (entry == NULL)
4178 {
4179 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4180 if (entry == NULL)
4181 return entry;
4182 }
4183
4184 /* Call the allocation method of the superclass. */
4185 entry = bfd_hash_newfunc (entry, table, string);
4186 if (entry != NULL)
4187 {
4188 struct ppc_branch_hash_entry *eh;
4189
4190 /* Initialize the local fields. */
4191 eh = (struct ppc_branch_hash_entry *) entry;
4192 eh->offset = 0;
4193 eh->iter = 0;
4194 }
4195
4196 return entry;
4197}
4198
65f38f15
AM
4199/* Create an entry in a ppc64 ELF linker hash table. */
4200
4201static struct bfd_hash_entry *
4ce794b7
AM
4202link_hash_newfunc (struct bfd_hash_entry *entry,
4203 struct bfd_hash_table *table,
4204 const char *string)
65f38f15
AM
4205{
4206 /* Allocate the structure if it has not already been allocated by a
4207 subclass. */
4208 if (entry == NULL)
4209 {
4210 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4211 if (entry == NULL)
4212 return entry;
4213 }
4214
4215 /* Call the allocation method of the superclass. */
4216 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4217 if (entry != NULL)
4218 {
4219 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4220
b3fac117 4221 memset (&eh->u.stub_cache, 0,
908b32fc 4222 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4223 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4224
4225 /* When making function calls, old ABI code references function entry
4226 points (dot symbols), while new ABI code references the function
4227 descriptor symbol. We need to make any combination of reference and
4228 definition work together, without breaking archive linking.
4229
4230 For a defined function "foo" and an undefined call to "bar":
4231 An old object defines "foo" and ".foo", references ".bar" (possibly
4232 "bar" too).
4233 A new object defines "foo" and references "bar".
4234
4235 A new object thus has no problem with its undefined symbols being
4236 satisfied by definitions in an old object. On the other hand, the
4237 old object won't have ".bar" satisfied by a new object.
4238
4239 Keep a list of newly added dot-symbols. */
4240
4241 if (string[0] == '.')
4242 {
4243 struct ppc_link_hash_table *htab;
4244
4245 htab = (struct ppc_link_hash_table *) table;
4246 eh->u.next_dot_sym = htab->dot_syms;
4247 htab->dot_syms = eh;
4248 }
65f38f15
AM
4249 }
4250
4251 return entry;
4252}
4253
3b421ab3
AM
4254struct tocsave_entry {
4255 asection *sec;
4256 bfd_vma offset;
4257};
4258
4259static hashval_t
4260tocsave_htab_hash (const void *p)
4261{
4262 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4263 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4264}
4265
4266static int
4267tocsave_htab_eq (const void *p1, const void *p2)
4268{
4269 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4270 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4271 return e1->sec == e2->sec && e1->offset == e2->offset;
4272}
4273
68faa637
AM
4274/* Destroy a ppc64 ELF linker hash table. */
4275
4276static void
d495ab0d 4277ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 4278{
d495ab0d 4279 struct ppc_link_hash_table *htab;
68faa637 4280
d495ab0d 4281 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
4282 if (htab->tocsave_htab)
4283 htab_delete (htab->tocsave_htab);
d495ab0d
AM
4284 bfd_hash_table_free (&htab->branch_hash_table);
4285 bfd_hash_table_free (&htab->stub_hash_table);
4286 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
4287}
4288
65f38f15
AM
4289/* Create a ppc64 ELF linker hash table. */
4290
4291static struct bfd_link_hash_table *
4ce794b7 4292ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4293{
4294 struct ppc_link_hash_table *htab;
4295 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4296
4ce794b7 4297 htab = bfd_zmalloc (amt);
65f38f15
AM
4298 if (htab == NULL)
4299 return NULL;
4300
66eb6687 4301 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4302 sizeof (struct ppc_link_hash_entry),
4303 PPC64_ELF_DATA))
65f38f15 4304 {
e2d34d7d 4305 free (htab);
65f38f15
AM
4306 return NULL;
4307 }
4308
721956f4 4309 /* Init the stub hash table too. */
66eb6687
AM
4310 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4311 sizeof (struct ppc_stub_hash_entry)))
2915c55b 4312 {
d495ab0d 4313 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4314 return NULL;
4315 }
721956f4
AM
4316
4317 /* And the branch hash table. */
66eb6687
AM
4318 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4319 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
4320 {
4321 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 4322 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
4323 return NULL;
4324 }
721956f4 4325
3b421ab3
AM
4326 htab->tocsave_htab = htab_try_create (1024,
4327 tocsave_htab_hash,
4328 tocsave_htab_eq,
4329 NULL);
4330 if (htab->tocsave_htab == NULL)
2915c55b 4331 {
d495ab0d 4332 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
4333 return NULL;
4334 }
d495ab0d 4335 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 4336
3254fd24
AM
4337 /* Initializing two fields of the union is just cosmetic. We really
4338 only care about glist, but when compiled on a 32-bit host the
4339 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4340 debugger inspection of these fields look nicer. */
a6aa5195
AM
4341 htab->elf.init_got_refcount.refcount = 0;
4342 htab->elf.init_got_refcount.glist = NULL;
4343 htab->elf.init_plt_refcount.refcount = 0;
4344 htab->elf.init_plt_refcount.glist = NULL;
4345 htab->elf.init_got_offset.offset = 0;
4346 htab->elf.init_got_offset.glist = NULL;
4347 htab->elf.init_plt_offset.offset = 0;
4348 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4349
65f38f15
AM
4350 return &htab->elf.root;
4351}
4352
bfeb4a28
AM
4353/* Create sections for linker generated code. */
4354
4355static bfd_boolean
4356create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4357{
4358 struct ppc_link_hash_table *htab;
4359 flagword flags;
4360
4361 htab = ppc_hash_table (info);
4362
bfeb4a28
AM
4363 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4364 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
4365 if (htab->params->save_restore_funcs)
4366 {
4367 /* Create .sfpr for code to save and restore fp regs. */
4368 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4369 flags);
4370 if (htab->sfpr == NULL
4371 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4372 return FALSE;
4373 }
4374
4375 if (bfd_link_relocatable (info))
4376 return TRUE;
bfeb4a28
AM
4377
4378 /* Create .glink for lazy dynamic linking support. */
4379 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4380 flags);
4381 if (htab->glink == NULL
4382 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4383 return FALSE;
4384
4385 if (!info->no_ld_generated_unwind_info)
4386 {
4387 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4388 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4389 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4390 ".eh_frame",
4391 flags);
4392 if (htab->glink_eh_frame == NULL
4393 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4394 return FALSE;
4395 }
4396
4397 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
4398 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4399 if (htab->elf.iplt == NULL
4400 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
bfeb4a28
AM
4401 return FALSE;
4402
4403 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4404 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
4405 htab->elf.irelplt
4406 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4407 if (htab->elf.irelplt == NULL
4408 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
bfeb4a28
AM
4409 return FALSE;
4410
4411 /* Create branch lookup table for plt_branch stubs. */
4412 flags = (SEC_ALLOC | SEC_LOAD
4413 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4414 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4415 flags);
4416 if (htab->brlt == NULL
4417 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4418 return FALSE;
4419
0e1862bb 4420 if (!bfd_link_pic (info))
bfeb4a28
AM
4421 return TRUE;
4422
4423 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4424 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4425 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4426 ".rela.branch_lt",
4427 flags);
4428 if (htab->relbrlt == NULL
4429 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4430 return FALSE;
4431
4432 return TRUE;
4433}
4434
e717da7e
AM
4435/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4436
bfeb4a28 4437bfd_boolean
e7d1c40c
AM
4438ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4439 struct ppc64_elf_params *params)
e717da7e
AM
4440{
4441 struct ppc_link_hash_table *htab;
4442
e7d1c40c 4443 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
4444
4445/* Always hook our dynamic sections into the first bfd, which is the
4446 linker created stub bfd. This ensures that the GOT header is at
4447 the start of the output TOC section. */
4448 htab = ppc_hash_table (info);
e7d1c40c
AM
4449 htab->elf.dynobj = params->stub_bfd;
4450 htab->params = params;
bfeb4a28 4451
bfeb4a28 4452 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4453}
4454
721956f4
AM
4455/* Build a name for an entry in the stub hash table. */
4456
4457static char *
4ce794b7
AM
4458ppc_stub_name (const asection *input_section,
4459 const asection *sym_sec,
4460 const struct ppc_link_hash_entry *h,
4461 const Elf_Internal_Rela *rel)
721956f4
AM
4462{
4463 char *stub_name;
bcaa2f82 4464 ssize_t len;
721956f4
AM
4465
4466 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4467 offsets from a sym as a branch target? In fact, we could
4468 probably assume the addend is always zero. */
4469 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4470
4471 if (h)
4472 {
4473 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4474 stub_name = bfd_malloc (len);
46de2a7c
AM
4475 if (stub_name == NULL)
4476 return stub_name;
4477
bcaa2f82
AM
4478 len = sprintf (stub_name, "%08x.%s+%x",
4479 input_section->id & 0xffffffff,
4480 h->elf.root.root.string,
4481 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4482 }
4483 else
4484 {
ad8e1ba5 4485 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4486 stub_name = bfd_malloc (len);
46de2a7c
AM
4487 if (stub_name == NULL)
4488 return stub_name;
4489
bcaa2f82
AM
4490 len = sprintf (stub_name, "%08x.%x:%x+%x",
4491 input_section->id & 0xffffffff,
4492 sym_sec->id & 0xffffffff,
4493 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4494 (int) rel->r_addend & 0xffffffff);
721956f4 4495 }
bcaa2f82 4496 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4497 stub_name[len - 2] = 0;
721956f4
AM
4498 return stub_name;
4499}
4500
4501/* Look up an entry in the stub hash. Stub entries are cached because
4502 creating the stub name takes a bit of time. */
4503
4504static struct ppc_stub_hash_entry *
4ce794b7
AM
4505ppc_get_stub_entry (const asection *input_section,
4506 const asection *sym_sec,
039b3fef 4507 struct ppc_link_hash_entry *h,
4ce794b7
AM
4508 const Elf_Internal_Rela *rel,
4509 struct ppc_link_hash_table *htab)
721956f4
AM
4510{
4511 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 4512 struct map_stub *group;
721956f4
AM
4513
4514 /* If this input section is part of a group of sections sharing one
4515 stub section, then use the id of the first section in the group.
4516 Stub names need to include a section id, as there may well be
4517 more than one stub used to reach say, printf, and we need to
4518 distinguish between them. */
6f20ed8a 4519 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
4520 if (group == NULL)
4521 return NULL;
721956f4 4522
b3fac117
AM
4523 if (h != NULL && h->u.stub_cache != NULL
4524 && h->u.stub_cache->h == h
6f20ed8a 4525 && h->u.stub_cache->group == group)
721956f4 4526 {
b3fac117 4527 stub_entry = h->u.stub_cache;
721956f4
AM
4528 }
4529 else
4530 {
4531 char *stub_name;
4532
6f20ed8a 4533 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
4534 if (stub_name == NULL)
4535 return NULL;
4536
4537 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4538 stub_name, FALSE, FALSE);
721956f4 4539 if (h != NULL)
b3fac117 4540 h->u.stub_cache = stub_entry;
721956f4
AM
4541
4542 free (stub_name);
4543 }
4544
4545 return stub_entry;
4546}
4547
4548/* Add a new stub entry to the stub hash. Not all fields of the new
4549 stub entry are initialised. */
4550
4551static struct ppc_stub_hash_entry *
4ce794b7
AM
4552ppc_add_stub (const char *stub_name,
4553 asection *section,
25f53a85 4554 struct bfd_link_info *info)
721956f4 4555{
25f53a85 4556 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 4557 struct map_stub *group;
721956f4
AM
4558 asection *link_sec;
4559 asection *stub_sec;
4560 struct ppc_stub_hash_entry *stub_entry;
4561
6f20ed8a
AM
4562 group = htab->sec_info[section->id].u.group;
4563 link_sec = group->link_sec;
4564 stub_sec = group->stub_sec;
721956f4
AM
4565 if (stub_sec == NULL)
4566 {
6f20ed8a
AM
4567 size_t namelen;
4568 bfd_size_type len;
4569 char *s_name;
721956f4 4570
6f20ed8a
AM
4571 namelen = strlen (link_sec->name);
4572 len = namelen + sizeof (STUB_SUFFIX);
4573 s_name = bfd_alloc (htab->params->stub_bfd, len);
4574 if (s_name == NULL)
4575 return NULL;
721956f4 4576
6f20ed8a
AM
4577 memcpy (s_name, link_sec->name, namelen);
4578 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4579 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4580 if (stub_sec == NULL)
4581 return NULL;
4582 group->stub_sec = stub_sec;
721956f4
AM
4583 }
4584
4585 /* Enter this entry into the linker stub hash table. */
4586 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4587 TRUE, FALSE);
721956f4
AM
4588 if (stub_entry == NULL)
4589 {
695344c0 4590 /* xgettext:c-format */
8de848d8 4591 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4592 section->owner, stub_name);
721956f4
AM
4593 return NULL;
4594 }
4595
6f20ed8a 4596 stub_entry->group = group;
721956f4 4597 stub_entry->stub_offset = 0;
721956f4
AM
4598 return stub_entry;
4599}
4600
e717da7e
AM
4601/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4602 not already done. */
65f38f15 4603
b34976b6 4604static bfd_boolean
e717da7e 4605create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4606{
e717da7e
AM
4607 asection *got, *relgot;
4608 flagword flags;
4609 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4610
0c8d6e5c 4611 if (!is_ppc64_elf (abfd))
0ffa91dd 4612 return FALSE;
4dfe6ac6
NC
4613 if (htab == NULL)
4614 return FALSE;
0ffa91dd 4615
33e44f2e
AM
4616 if (!htab->elf.sgot
4617 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4618 return FALSE;
e717da7e
AM
4619
4620 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4621 | SEC_LINKER_CREATED);
4622
c456f082 4623 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4624 if (!got
e717da7e
AM
4625 || !bfd_set_section_alignment (abfd, got, 3))
4626 return FALSE;
65f38f15 4627
c456f082
AM
4628 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4629 flags | SEC_READONLY);
e717da7e 4630 if (!relgot
e717da7e 4631 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4632 return FALSE;
e717da7e
AM
4633
4634 ppc64_elf_tdata (abfd)->got = got;
4635 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4636 return TRUE;
65f38f15 4637}
5bd4f169 4638
b31867b6
AM
4639/* Follow indirect and warning symbol links. */
4640
4641static inline struct bfd_link_hash_entry *
4642follow_link (struct bfd_link_hash_entry *h)
4643{
4644 while (h->type == bfd_link_hash_indirect
4645 || h->type == bfd_link_hash_warning)
4646 h = h->u.i.link;
4647 return h;
4648}
4649
4650static inline struct elf_link_hash_entry *
4651elf_follow_link (struct elf_link_hash_entry *h)
4652{
4653 return (struct elf_link_hash_entry *) follow_link (&h->root);
4654}
4655
4656static inline struct ppc_link_hash_entry *
4657ppc_follow_link (struct ppc_link_hash_entry *h)
4658{
4659 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4660}
4661
40d16e0b
AM
4662/* Merge PLT info on FROM with that on TO. */
4663
4664static void
4665move_plt_plist (struct ppc_link_hash_entry *from,
4666 struct ppc_link_hash_entry *to)
4667{
4668 if (from->elf.plt.plist != NULL)
4669 {
4670 if (to->elf.plt.plist != NULL)
4671 {
4672 struct plt_entry **entp;
4673 struct plt_entry *ent;
4674
4675 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4676 {
4677 struct plt_entry *dent;
4678
4679 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4680 if (dent->addend == ent->addend)
4681 {
4682 dent->plt.refcount += ent->plt.refcount;
4683 *entp = ent->next;
4684 break;
4685 }
4686 if (dent == NULL)
4687 entp = &ent->next;
4688 }
4689 *entp = to->elf.plt.plist;
4690 }
4691
4692 to->elf.plt.plist = from->elf.plt.plist;
4693 from->elf.plt.plist = NULL;
4694 }
4695}
4696
65f38f15
AM
4697/* Copy the extra info we tack onto an elf_link_hash_entry. */
4698
4699static void
fcfa13d2
AM
4700ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4701 struct elf_link_hash_entry *dir,
4702 struct elf_link_hash_entry *ind)
65f38f15
AM
4703{
4704 struct ppc_link_hash_entry *edir, *eind;
4705
4706 edir = (struct ppc_link_hash_entry *) dir;
4707 eind = (struct ppc_link_hash_entry *) ind;
4708
c79d6685
AM
4709 edir->is_func |= eind->is_func;
4710 edir->is_func_descriptor |= eind->is_func_descriptor;
4711 edir->tls_mask |= eind->tls_mask;
4712 if (eind->oh != NULL)
4713 edir->oh = ppc_follow_link (eind->oh);
4714
474436e6
AM
4715 if (edir->elf.versioned != versioned_hidden)
4716 {
4717 /* If called to transfer flags for a weakdef during processing
4718 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4719 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4720 if (!(ELIMINATE_COPY_RELOCS
4721 && eind->elf.root.type != bfd_link_hash_indirect
4722 && edir->elf.dynamic_adjusted))
4723 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4724
4725 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4726 edir->elf.ref_regular |= eind->elf.ref_regular;
4727 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4728 edir->elf.needs_plt |= eind->elf.needs_plt;
4729 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4730 }
c79d6685 4731
d311bc8b
AM
4732 /* If we were called to copy over info for a weak sym, don't copy
4733 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4734 in order to simplify readonly_dynrelocs and save a field in the
4735 symbol hash entry, but that means dyn_relocs can't be used in any
4736 tests about a specific symbol, or affect other symbol flags which
4737 are then tested.
4738 Chain weakdefs so we can get from the weakdef back to an alias.
4739 The list is circular so that we don't need to use u.weakdef as
4740 well as this list to look at all aliases. */
4741 if (eind->elf.root.type != bfd_link_hash_indirect)
4742 {
4743 struct ppc_link_hash_entry *cur, *add, *next;
4744
4745 add = eind;
4746 do
4747 {
4748 cur = edir->weakref;
4749 if (cur != NULL)
4750 {
4751 do
4752 {
4753 /* We can be called twice for the same symbols.
4754 Don't make multiple loops. */
4755 if (cur == add)
4756 return;
4757 cur = cur->weakref;
4758 } while (cur != edir);
4759 }
4760 next = add->weakref;
4761 if (cur != add)
4762 {
4763 add->weakref = edir->weakref != NULL ? edir->weakref : edir;
4764 edir->weakref = add;
4765 }
4766 add = next;
4767 } while (add != NULL && add != eind);
4768 return;
4769 }
4770
411e1bfb 4771 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4772 if (eind->dyn_relocs != NULL)
65f38f15 4773 {
bbd7ec4a
AM
4774 if (edir->dyn_relocs != NULL)
4775 {
6061a67d
AM
4776 struct elf_dyn_relocs **pp;
4777 struct elf_dyn_relocs *p;
bbd7ec4a 4778
fcfa13d2 4779 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4780 list. Merge any entries against the same section. */
4781 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4782 {
6061a67d 4783 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4784
4785 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4786 if (q->sec == p->sec)
4787 {
4788 q->pc_count += p->pc_count;
4789 q->count += p->count;
4790 *pp = p->next;
4791 break;
4792 }
4793 if (q == NULL)
4794 pp = &p->next;
4795 }
4796 *pp = edir->dyn_relocs;
4797 }
4798
65f38f15
AM
4799 edir->dyn_relocs = eind->dyn_relocs;
4800 eind->dyn_relocs = NULL;
4801 }
65f38f15 4802
81848ca0
AM
4803 /* Copy over got entries that we may have already seen to the
4804 symbol which just became indirect. */
411e1bfb
AM
4805 if (eind->elf.got.glist != NULL)
4806 {
4807 if (edir->elf.got.glist != NULL)
4808 {
4809 struct got_entry **entp;
4810 struct got_entry *ent;
4811
4812 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4813 {
4814 struct got_entry *dent;
4815
4816 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4817 if (dent->addend == ent->addend
e717da7e 4818 && dent->owner == ent->owner
411e1bfb
AM
4819 && dent->tls_type == ent->tls_type)
4820 {
4821 dent->got.refcount += ent->got.refcount;
4822 *entp = ent->next;
4823 break;
4824 }
4825 if (dent == NULL)
4826 entp = &ent->next;
4827 }
4828 *entp = edir->elf.got.glist;
4829 }
4830
4831 edir->elf.got.glist = eind->elf.got.glist;
4832 eind->elf.got.glist = NULL;
4833 }
4834
4835 /* And plt entries. */
40d16e0b 4836 move_plt_plist (eind, edir);
411e1bfb 4837
fcfa13d2 4838 if (eind->elf.dynindx != -1)
411e1bfb 4839 {
fcfa13d2
AM
4840 if (edir->elf.dynindx != -1)
4841 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4842 edir->elf.dynstr_index);
411e1bfb
AM
4843 edir->elf.dynindx = eind->elf.dynindx;
4844 edir->elf.dynstr_index = eind->elf.dynstr_index;
4845 eind->elf.dynindx = -1;
4846 eind->elf.dynstr_index = 0;
4847 }
411e1bfb
AM
4848}
4849
8387904d
AM
4850/* Find the function descriptor hash entry from the given function code
4851 hash entry FH. Link the entries via their OH fields. */
4852
4853static struct ppc_link_hash_entry *
b31867b6 4854lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4855{
4856 struct ppc_link_hash_entry *fdh = fh->oh;
4857
4858 if (fdh == NULL)
4859 {
4860 const char *fd_name = fh->elf.root.root.string + 1;
4861
4862 fdh = (struct ppc_link_hash_entry *)
4863 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4864 if (fdh == NULL)
4865 return fdh;
4866
4867 fdh->is_func_descriptor = 1;
4868 fdh->oh = fh;
4869 fh->is_func = 1;
4870 fh->oh = fdh;
8387904d
AM
4871 }
4872
8c5b4e52
AM
4873 fdh = ppc_follow_link (fdh);
4874 fdh->is_func_descriptor = 1;
4875 fdh->oh = fh;
4876 return fdh;
8387904d
AM
4877}
4878
8c5b4e52 4879/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4880
4881static struct ppc_link_hash_entry *
4882make_fdh (struct bfd_link_info *info,
908b32fc 4883 struct ppc_link_hash_entry *fh)
bb700d78 4884{
8c5b4e52
AM
4885 bfd *abfd = fh->elf.root.u.undef.abfd;
4886 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4887 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4888 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4889 ? BSF_WEAK
4890 : BSF_GLOBAL);
4891
4892 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4893 fh->elf.root.root.string + 1,
4894 flags, bfd_und_section_ptr, 0,
4895 NULL, FALSE, FALSE, &bh))
bb700d78
AM
4896 return NULL;
4897
4898 fdh = (struct ppc_link_hash_entry *) bh;
4899 fdh->elf.non_elf = 0;
908b32fc
AM
4900 fdh->fake = 1;
4901 fdh->is_func_descriptor = 1;
4902 fdh->oh = fh;
4903 fh->is_func = 1;
4904 fh->oh = fdh;
bb700d78
AM
4905 return fdh;
4906}
4907
8387904d
AM
4908/* Fix function descriptor symbols defined in .opd sections to be
4909 function type. */
555cd476
AM
4910
4911static bfd_boolean
c16153ae 4912ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4913 struct bfd_link_info *info,
555cd476 4914 Elf_Internal_Sym *isym,
6911b7dc 4915 const char **name,
555cd476
AM
4916 flagword *flags ATTRIBUTE_UNUSED,
4917 asection **sec,
b53dfeb2 4918 bfd_vma *value)
555cd476 4919{
a43942db 4920 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
f1885d1e
AM
4921 && (ibfd->flags & DYNAMIC) == 0
4922 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
a43942db 4923 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
f64b2e8d 4924
b53dfeb2 4925 if (*sec != NULL
f1885d1e 4926 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4927 {
4928 asection *code_sec;
4929
4930 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4931 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4932 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4933
4934 /* If the symbol is a function defined in .opd, and the function
4935 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4936 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4937 && (*sec)->reloc_count != 0
4938 && opd_entry_value (*sec, *value, &code_sec, NULL,
4939 FALSE) != (bfd_vma) -1
4940 && discarded_section (code_sec))
4941 {
4942 *sec = bfd_und_section_ptr;
4943 isym->st_shndx = SHN_UNDEF;
4944 }
4945 }
dbd1e97e
AM
4946 else if (*sec != NULL
4947 && strcmp ((*sec)->name, ".toc") == 0
4948 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4949 {
4950 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4951 if (htab != NULL)
4952 htab->params->object_in_toc = 1;
4953 }
433817dd 4954
6911b7dc
AM
4955 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4956 {
4957 if (abiversion (ibfd) == 0)
4958 set_abiversion (ibfd, 2);
4959 else if (abiversion (ibfd) == 1)
4960 {
4961 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4962 " for ABI version 1\n"), name);
4963 bfd_set_error (bfd_error_bad_value);
4964 return FALSE;
4965 }
4966 }
4967
555cd476
AM
4968 return TRUE;
4969}
4970
6911b7dc
AM
4971/* Merge non-visibility st_other attributes: local entry point. */
4972
4973static void
4974ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4975 const Elf_Internal_Sym *isym,
4976 bfd_boolean definition,
4977 bfd_boolean dynamic)
4978{
4979 if (definition && !dynamic)
4980 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4981 | ELF_ST_VISIBILITY (h->other));
4982}
4983
8c5b4e52
AM
4984/* Hook called on merging a symbol. We use this to clear "fake" since
4985 we now have a real symbol. */
4986
4987static bfd_boolean
4988ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4989 const Elf_Internal_Sym *isym ATTRIBUTE_UNUSED,
4990 asection **psec ATTRIBUTE_UNUSED,
4991 bfd_boolean newdef ATTRIBUTE_UNUSED,
4992 bfd_boolean olddef ATTRIBUTE_UNUSED,
4993 bfd *oldbfd ATTRIBUTE_UNUSED,
4994 const asection *oldsec ATTRIBUTE_UNUSED)
4995{
4996 ((struct ppc_link_hash_entry *) h)->fake = 0;
4997 return TRUE;
4998}
4999
8387904d 5000/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
5001 inclusion of a new ABI object archive that defines "bar".
5002 NAME is a symbol defined in an archive. Return a symbol in the hash
5003 table that might be satisfied by the archive symbols. */
8387904d
AM
5004
5005static struct elf_link_hash_entry *
5006ppc64_elf_archive_symbol_lookup (bfd *abfd,
5007 struct bfd_link_info *info,
5008 const char *name)
5009{
5010 struct elf_link_hash_entry *h;
5011 char *dot_name;
5012 size_t len;
5013
5014 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
5015 if (h != NULL
5016 /* Don't return this sym if it is a fake function descriptor
5017 created by add_symbol_adjust. */
8c5b4e52 5018 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
5019 return h;
5020
5021 if (name[0] == '.')
5022 return h;
5023
5024 len = strlen (name);
5025 dot_name = bfd_alloc (abfd, len + 2);
5026 if (dot_name == NULL)
5027 return (struct elf_link_hash_entry *) 0 - 1;
5028 dot_name[0] = '.';
5029 memcpy (dot_name + 1, name, len + 1);
5030 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5031 bfd_release (abfd, dot_name);
5032 return h;
5033}
5034
5035/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
5036 new ABI object defines "bar". Well, at least, undefined dot symbols
5037 are made weak. This stops later archive searches from including an
5038 object if we already have a function descriptor definition. It also
35b0ce59
AM
5039 prevents the linker complaining about undefined symbols.
5040 We also check and correct mismatched symbol visibility here. The
5041 most restrictive visibility of the function descriptor and the
5042 function entry symbol is used. */
8387904d
AM
5043
5044static bfd_boolean
b3fac117 5045add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 5046{
8387904d
AM
5047 struct ppc_link_hash_table *htab;
5048 struct ppc_link_hash_entry *fdh;
5049
b3fac117
AM
5050 if (eh->elf.root.type == bfd_link_hash_warning)
5051 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 5052
8400d40d
AM
5053 if (eh->elf.root.type == bfd_link_hash_indirect)
5054 return TRUE;
5055
b3fac117
AM
5056 if (eh->elf.root.root.string[0] != '.')
5057 abort ();
8387904d 5058
b3fac117 5059 htab = ppc_hash_table (info);
4dfe6ac6
NC
5060 if (htab == NULL)
5061 return FALSE;
5062
b31867b6 5063 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
5064 if (fdh == NULL
5065 && !bfd_link_relocatable (info)
5066 && (eh->elf.root.type == bfd_link_hash_undefined
5067 || eh->elf.root.type == bfd_link_hash_undefweak)
5068 && eh->elf.ref_regular)
5069 {
5070 /* Make an undefined function descriptor sym, in order to
5071 pull in an --as-needed shared lib. Archives are handled
5072 elsewhere. */
5073 fdh = make_fdh (info, eh);
5074 if (fdh == NULL)
5075 return FALSE;
bb700d78 5076 }
8c5b4e52
AM
5077
5078 if (fdh != NULL)
8387904d 5079 {
35b0ce59
AM
5080 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5081 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
5082
5083 /* Make both descriptor and entry symbol have the most
5084 constraining visibility of either symbol. */
35b0ce59
AM
5085 if (entry_vis < descr_vis)
5086 fdh->elf.other += entry_vis - descr_vis;
5087 else if (entry_vis > descr_vis)
5088 eh->elf.other += descr_vis - entry_vis;
5089
8c5b4e52
AM
5090 /* Propagate reference flags from entry symbol to function
5091 descriptor symbol. */
5092 fdh->elf.root.non_ir_ref |= eh->elf.root.non_ir_ref;
5093 fdh->elf.ref_regular |= eh->elf.ref_regular;
5094 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5095
5096 if (!fdh->elf.forced_local
5097 && fdh->elf.dynindx == -1
5098 && fdh->elf.versioned != versioned_hidden
5099 && (bfd_link_dll (info)
5100 || fdh->elf.def_dynamic
5101 || fdh->elf.ref_dynamic)
5102 && (eh->elf.ref_regular
5103 || eh->elf.def_regular))
5104 {
5105 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5106 return FALSE;
35b0ce59 5107 }
8387904d 5108 }
99877b66 5109
8387904d
AM
5110 return TRUE;
5111}
5112
f6c7c3e8
AM
5113/* Set up opd section info and abiversion for IBFD, and process list
5114 of dot-symbols we made in link_hash_newfunc. */
b3fac117 5115
8387904d 5116static bfd_boolean
f6c7c3e8 5117ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 5118{
99877b66 5119 struct ppc_link_hash_table *htab;
b3fac117 5120 struct ppc_link_hash_entry **p, *eh;
459609d6 5121 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 5122
459609d6 5123 if (opd != NULL && opd->size != 0)
b3fac117 5124 {
459609d6
AM
5125 if (abiversion (ibfd) == 0)
5126 set_abiversion (ibfd, 1);
8a2058b5 5127 else if (abiversion (ibfd) >= 2)
f6c7c3e8 5128 {
695344c0 5129 /* xgettext:c-format */
459609d6
AM
5130 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5131 " version %d\n"),
5132 ibfd, abiversion (ibfd));
5133 bfd_set_error (bfd_error_bad_value);
5134 return FALSE;
f6c7c3e8
AM
5135 }
5136
459609d6
AM
5137 if ((ibfd->flags & DYNAMIC) == 0
5138 && (opd->flags & SEC_RELOC) != 0
5139 && opd->reloc_count != 0
5140 && !bfd_is_abs_section (opd->output_section))
b3fac117 5141 {
459609d6
AM
5142 /* Garbage collection needs some extra help with .opd sections.
5143 We don't want to necessarily keep everything referenced by
5144 relocs in .opd, as that would keep all functions. Instead,
5145 if we reference an .opd symbol (a function descriptor), we
5146 want to keep the function code symbol's section. This is
5147 easy for global symbols, but for local syms we need to keep
5148 information about the associated function section. */
5149 bfd_size_type amt;
5150 asection **opd_sym_map;
5151
51aecdc5 5152 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
459609d6
AM
5153 opd_sym_map = bfd_zalloc (ibfd, amt);
5154 if (opd_sym_map == NULL)
b3fac117 5155 return FALSE;
459609d6
AM
5156 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5157 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5158 ppc64_elf_section_data (opd)->sec_type = sec_opd;
b3fac117
AM
5159 }
5160 }
5161
459609d6
AM
5162 if (!is_ppc64_elf (info->output_bfd))
5163 return TRUE;
5164 htab = ppc_hash_table (info);
5165 if (htab == NULL)
5166 return FALSE;
5167
5168 /* For input files without an explicit abiversion in e_flags
5169 we should have flagged any with symbol st_other bits set
5170 as ELFv1 and above flagged those with .opd as ELFv2.
5171 Set the output abiversion if not yet set, and for any input
5172 still ambiguous, take its abiversion from the output.
5173 Differences in ABI are reported later. */
5174 if (abiversion (info->output_bfd) == 0)
5175 set_abiversion (info->output_bfd, abiversion (ibfd));
5176 else if (abiversion (ibfd) == 0)
5177 set_abiversion (ibfd, abiversion (info->output_bfd));
5178
5179 p = &htab->dot_syms;
5180 while ((eh = *p) != NULL)
5181 {
5182 *p = NULL;
5183 if (&eh->elf == htab->elf.hgot)
5184 ;
5185 else if (htab->elf.hgot == NULL
5186 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5187 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
5188 else if (abiversion (ibfd) <= 1)
5189 {
5190 htab->need_func_desc_adj = 1;
5191 if (!add_symbol_adjust (eh, info))
5192 return FALSE;
5193 }
459609d6
AM
5194 p = &eh->u.next_dot_sym;
5195 }
b3fac117 5196 return TRUE;
8387904d
AM
5197}
5198
97fed1c9
JJ
5199/* Undo hash table changes when an --as-needed input file is determined
5200 not to be needed. */
5201
5202static bfd_boolean
e5034e59
AM
5203ppc64_elf_notice_as_needed (bfd *ibfd,
5204 struct bfd_link_info *info,
5205 enum notice_asneeded_action act)
97fed1c9 5206{
e5034e59
AM
5207 if (act == notice_not_needed)
5208 {
5209 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 5210
e5034e59
AM
5211 if (htab == NULL)
5212 return FALSE;
4dfe6ac6 5213
e5034e59
AM
5214 htab->dot_syms = NULL;
5215 }
5216 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
5217}
5218
aa374f67
AM
5219/* If --just-symbols against a final linked binary, then assume we need
5220 toc adjusting stubs when calling functions defined there. */
5221
5222static void
5223ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5224{
5225 if ((sec->flags & SEC_CODE) != 0
5226 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5227 && is_ppc64_elf (sec->owner))
5228 {
2c3f079f
AM
5229 if (abiversion (sec->owner) >= 2
5230 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
5231 sec->has_toc_reloc = 1;
5232 }
5233 _bfd_elf_link_just_syms (sec, info);
5234}
5235
e054468f 5236static struct plt_entry **
4ce794b7
AM
5237update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5238 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5239{
5240 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5241 struct plt_entry **local_plt;
f961d9dd 5242 unsigned char *local_got_tls_masks;
411e1bfb
AM
5243
5244 if (local_got_ents == NULL)
5245 {
5246 bfd_size_type size = symtab_hdr->sh_info;
5247
e054468f
AM
5248 size *= (sizeof (*local_got_ents)
5249 + sizeof (*local_plt)
5250 + sizeof (*local_got_tls_masks));
4ce794b7 5251 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5252 if (local_got_ents == NULL)
e054468f 5253 return NULL;
411e1bfb
AM
5254 elf_local_got_ents (abfd) = local_got_ents;
5255 }
5256
e054468f 5257 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5258 {
5259 struct got_entry *ent;
5260
5261 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5262 if (ent->addend == r_addend
5263 && ent->owner == abfd
5264 && ent->tls_type == tls_type)
411e1bfb
AM
5265 break;
5266 if (ent == NULL)
5267 {
5268 bfd_size_type amt = sizeof (*ent);
4ce794b7 5269 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5270 if (ent == NULL)
5271 return FALSE;
5272 ent->next = local_got_ents[r_symndx];
5273 ent->addend = r_addend;
e717da7e 5274 ent->owner = abfd;
411e1bfb 5275 ent->tls_type = tls_type;
927be08e 5276 ent->is_indirect = FALSE;
411e1bfb
AM
5277 ent->got.refcount = 0;
5278 local_got_ents[r_symndx] = ent;
5279 }
5280 ent->got.refcount += 1;
5281 }
5282
e054468f 5283 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5284 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5285 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5286
5287 return local_plt + r_symndx;
65f38f15
AM
5288}
5289
411e1bfb 5290static bfd_boolean
e054468f 5291update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5292{
411e1bfb 5293 struct plt_entry *ent;
1e2f5b6e 5294
e054468f 5295 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5296 if (ent->addend == addend)
5297 break;
5298 if (ent == NULL)
1e2f5b6e 5299 {
411e1bfb 5300 bfd_size_type amt = sizeof (*ent);
4ce794b7 5301 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5302 if (ent == NULL)
5303 return FALSE;
e054468f 5304 ent->next = *plist;
411e1bfb
AM
5305 ent->addend = addend;
5306 ent->plt.refcount = 0;
e054468f 5307 *plist = ent;
1e2f5b6e 5308 }
411e1bfb 5309 ent->plt.refcount += 1;
b34976b6 5310 return TRUE;
1e2f5b6e
AM
5311}
5312
e054468f
AM
5313static bfd_boolean
5314is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5315{
5316 return (r_type == R_PPC64_REL24
5317 || r_type == R_PPC64_REL14
5318 || r_type == R_PPC64_REL14_BRTAKEN
5319 || r_type == R_PPC64_REL14_BRNTAKEN
5320 || r_type == R_PPC64_ADDR24
5321 || r_type == R_PPC64_ADDR14
5322 || r_type == R_PPC64_ADDR14_BRTAKEN
5323 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5324}
5325
5bd4f169 5326/* Look through the relocs for a section during the first phase, and
65f38f15 5327 calculate needed space in the global offset table, procedure
5d1634d7 5328 linkage table, and dynamic reloc sections. */
5bd4f169 5329
b34976b6 5330static bfd_boolean
4ce794b7
AM
5331ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5332 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5333{
65f38f15 5334 struct ppc_link_hash_table *htab;
5bd4f169 5335 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5336 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5337 const Elf_Internal_Rela *rel;
5338 const Elf_Internal_Rela *rel_end;
5bd4f169 5339 asection *sreloc;
1e2f5b6e 5340 asection **opd_sym_map;
3a71aa26 5341 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 5342
0e1862bb 5343 if (bfd_link_relocatable (info))
b34976b6 5344 return TRUE;
5bd4f169 5345
680a3378
AM
5346 /* Don't do anything special with non-loaded, non-alloced sections.
5347 In particular, any relocs in such sections should not affect GOT
5348 and PLT reference counting (ie. we don't allow them to create GOT
5349 or PLT entries), there's no possibility or desire to optimize TLS
5350 relocs, and there's not much point in propagating relocs to shared
5351 libs that the dynamic linker won't relocate. */
5352 if ((sec->flags & SEC_ALLOC) == 0)
5353 return TRUE;
5354
0c8d6e5c 5355 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5356
65f38f15 5357 htab = ppc_hash_table (info);
4dfe6ac6
NC
5358 if (htab == NULL)
5359 return FALSE;
5360
3a71aa26
AM
5361 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5362 FALSE, FALSE, TRUE);
5363 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5364 FALSE, FALSE, TRUE);
0ffa91dd 5365 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5366 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5367 sreloc = NULL;
1e2f5b6e 5368 opd_sym_map = NULL;
f6c7c3e8
AM
5369 if (ppc64_elf_section_data (sec) != NULL
5370 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5371 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5bd4f169
AM
5372
5373 rel_end = relocs + sec->reloc_count;
5374 for (rel = relocs; rel < rel_end; rel++)
5375 {
5376 unsigned long r_symndx;
5377 struct elf_link_hash_entry *h;
04c9666a 5378 enum elf_ppc64_reloc_type r_type;
727fc41e 5379 int tls_type;
7c8fe5c4 5380 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 5381 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
5382
5383 r_symndx = ELF64_R_SYM (rel->r_info);
5384 if (r_symndx < symtab_hdr->sh_info)
5385 h = NULL;
5386 else
973a3492 5387 {
8c5b4e52
AM
5388 struct ppc_link_hash_entry *eh;
5389
973a3492 5390 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5391 h = elf_follow_link (h);
8c5b4e52 5392 eh = (struct ppc_link_hash_entry *) h;
81fbe831
AM
5393
5394 /* PR15323, ref flags aren't set for references in the same
5395 object. */
5396 h->root.non_ir_ref = 1;
8c5b4e52
AM
5397 if (eh->is_func && eh->oh != NULL)
5398 eh->oh->elf.root.non_ir_ref = 1;
1c865ab2
AM
5399
5400 if (h == htab->elf.hgot)
5401 sec->has_toc_reloc = 1;
973a3492 5402 }
5bd4f169 5403
727fc41e 5404 tls_type = 0;
e054468f 5405 ifunc = NULL;
25f23106
AM
5406 if (h != NULL)
5407 {
5408 if (h->type == STT_GNU_IFUNC)
5409 {
5410 h->needs_plt = 1;
5411 ifunc = &h->plt.plist;
5412 }
5413 }
5414 else
5415 {
5416 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5417 abfd, r_symndx);
5418 if (isym == NULL)
5419 return FALSE;
5420
5421 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5422 {
5423 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5424 rel->r_addend, PLT_IFUNC);
5425 if (ifunc == NULL)
5426 return FALSE;
5427 }
5428 }
727fc41e 5429
cbf95972 5430 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5431 switch (r_type)
5bd4f169 5432 {
727fc41e
AM
5433 case R_PPC64_TLSGD:
5434 case R_PPC64_TLSLD:
5435 /* These special tls relocs tie a call to __tls_get_addr with
5436 its parameter symbol. */
5437 break;
5438
411e1bfb
AM
5439 case R_PPC64_GOT_TLSLD16:
5440 case R_PPC64_GOT_TLSLD16_LO:
5441 case R_PPC64_GOT_TLSLD16_HI:
5442 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5443 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5444 goto dogottls;
5445
5446 case R_PPC64_GOT_TLSGD16:
5447 case R_PPC64_GOT_TLSGD16_LO:
5448 case R_PPC64_GOT_TLSGD16_HI:
5449 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5450 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5451 goto dogottls;
5452
5453 case R_PPC64_GOT_TPREL16_DS:
5454 case R_PPC64_GOT_TPREL16_LO_DS:
5455 case R_PPC64_GOT_TPREL16_HI:
5456 case R_PPC64_GOT_TPREL16_HA:
0e1862bb 5457 if (bfd_link_pic (info))
411e1bfb
AM
5458 info->flags |= DF_STATIC_TLS;
5459 tls_type = TLS_TLS | TLS_TPREL;
5460 goto dogottls;
5461
5462 case R_PPC64_GOT_DTPREL16_DS:
5463 case R_PPC64_GOT_DTPREL16_LO_DS:
5464 case R_PPC64_GOT_DTPREL16_HI:
5465 case R_PPC64_GOT_DTPREL16_HA:
5466 tls_type = TLS_TLS | TLS_DTPREL;
5467 dogottls:
5468 sec->has_tls_reloc = 1;
1a0670f3 5469 /* Fall through */
411e1bfb 5470
5bd4f169 5471 case R_PPC64_GOT16:
5bd4f169 5472 case R_PPC64_GOT16_DS:
65f38f15
AM
5473 case R_PPC64_GOT16_HA:
5474 case R_PPC64_GOT16_HI:
5475 case R_PPC64_GOT16_LO:
5bd4f169 5476 case R_PPC64_GOT16_LO_DS:
65f38f15 5477 /* This symbol requires a global offset table entry. */
4c52953f 5478 sec->has_toc_reloc = 1;
33c0ec9d
AM
5479 if (r_type == R_PPC64_GOT_TLSLD16
5480 || r_type == R_PPC64_GOT_TLSGD16
5481 || r_type == R_PPC64_GOT_TPREL16_DS
5482 || r_type == R_PPC64_GOT_DTPREL16_DS
5483 || r_type == R_PPC64_GOT16
5484 || r_type == R_PPC64_GOT16_DS)
5485 {
5486 htab->do_multi_toc = 1;
d77c8a4b 5487 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5488 }
5489
e717da7e
AM
5490 if (ppc64_elf_tdata (abfd)->got == NULL
5491 && !create_got_section (abfd, info))
b34976b6 5492 return FALSE;
5bd4f169
AM
5493
5494 if (h != NULL)
5495 {
411e1bfb
AM
5496 struct ppc_link_hash_entry *eh;
5497 struct got_entry *ent;
65f38f15 5498
411e1bfb
AM
5499 eh = (struct ppc_link_hash_entry *) h;
5500 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5501 if (ent->addend == rel->r_addend
e717da7e 5502 && ent->owner == abfd
411e1bfb
AM
5503 && ent->tls_type == tls_type)
5504 break;
5505 if (ent == NULL)
5bd4f169 5506 {
411e1bfb 5507 bfd_size_type amt = sizeof (*ent);
4ce794b7 5508 ent = bfd_alloc (abfd, amt);
411e1bfb 5509 if (ent == NULL)
b34976b6 5510 return FALSE;
411e1bfb
AM
5511 ent->next = eh->elf.got.glist;
5512 ent->addend = rel->r_addend;
e717da7e 5513 ent->owner = abfd;
411e1bfb 5514 ent->tls_type = tls_type;
927be08e 5515 ent->is_indirect = FALSE;
411e1bfb
AM
5516 ent->got.refcount = 0;
5517 eh->elf.got.glist = ent;
5bd4f169 5518 }
411e1bfb 5519 ent->got.refcount += 1;
e7b938ca 5520 eh->tls_mask |= tls_type;
5bd4f169 5521 }
411e1bfb
AM
5522 else
5523 /* This is a global offset table entry for a local symbol. */
5524 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5525 rel->r_addend, tls_type))
5526 return FALSE;
a345bc8d
AM
5527
5528 /* We may also need a plt entry if the symbol turns out to be
5529 an ifunc. */
0e1862bb 5530 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
a345bc8d
AM
5531 {
5532 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5533 return FALSE;
5534 }
5bd4f169
AM
5535 break;
5536
5bd4f169 5537 case R_PPC64_PLT16_HA:
65f38f15
AM
5538 case R_PPC64_PLT16_HI:
5539 case R_PPC64_PLT16_LO:
5540 case R_PPC64_PLT32:
5541 case R_PPC64_PLT64:
cbf95972
AM
5542 /* This symbol requires a procedure linkage table entry. */
5543 plt_list = ifunc;
5544 if (h != NULL)
e054468f 5545 {
e054468f
AM
5546 h->needs_plt = 1;
5547 if (h->root.root.string[0] == '.'
5548 && h->root.root.string[1] != '\0')
5549 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972
AM
5550 plt_list = &h->plt.plist;
5551 }
5552 if (plt_list == NULL)
5553 {
5554 /* It does not make sense to have a procedure linkage
5555 table entry for a non-ifunc local symbol. */
5556 info->callbacks->einfo
695344c0 5557 /* xgettext:c-format */
cbf95972
AM
5558 (_("%P: %H: %s reloc against local symbol\n"),
5559 abfd, sec, rel->r_offset,
5560 ppc64_elf_howto_table[r_type]->name);
5561 bfd_set_error (bfd_error_bad_value);
5562 return FALSE;
e054468f 5563 }
cbf95972
AM
5564 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5565 return FALSE;
5bd4f169
AM
5566 break;
5567
5568 /* The following relocations don't need to propagate the
5569 relocation if linking a shared object since they are
5570 section relative. */
5571 case R_PPC64_SECTOFF:
5572 case R_PPC64_SECTOFF_LO:
5573 case R_PPC64_SECTOFF_HI:
5574 case R_PPC64_SECTOFF_HA:
5575 case R_PPC64_SECTOFF_DS:
5576 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5577 case R_PPC64_DTPREL16:
5578 case R_PPC64_DTPREL16_LO:
5579 case R_PPC64_DTPREL16_HI:
5580 case R_PPC64_DTPREL16_HA:
5581 case R_PPC64_DTPREL16_DS:
5582 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5583 case R_PPC64_DTPREL16_HIGH:
5584 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5585 case R_PPC64_DTPREL16_HIGHER:
5586 case R_PPC64_DTPREL16_HIGHERA:
5587 case R_PPC64_DTPREL16_HIGHEST:
5588 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5589 break;
5590
ad8e1ba5 5591 /* Nor do these. */
25f23106
AM
5592 case R_PPC64_REL16:
5593 case R_PPC64_REL16_LO:
5594 case R_PPC64_REL16_HI:
5595 case R_PPC64_REL16_HA:
a680de9a 5596 case R_PPC64_REL16DX_HA:
25f23106
AM
5597 break;
5598
45965137
AM
5599 /* Not supported as a dynamic relocation. */
5600 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5601 if (bfd_link_pic (info))
45965137
AM
5602 {
5603 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5604 ppc_howto_init ();
695344c0 5605 /* xgettext:c-format */
45965137
AM
5606 info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5607 "in shared libraries and PIEs.\n"),
5608 abfd, sec, rel->r_offset,
5609 ppc64_elf_howto_table[r_type]->name);
5610 bfd_set_error (bfd_error_bad_value);
5611 return FALSE;
5612 }
5613 break;
5614
ad8e1ba5 5615 case R_PPC64_TOC16:
33c0ec9d
AM
5616 case R_PPC64_TOC16_DS:
5617 htab->do_multi_toc = 1;
d77c8a4b 5618 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5619 /* Fall through. */
ad8e1ba5
AM
5620 case R_PPC64_TOC16_LO:
5621 case R_PPC64_TOC16_HI:
5622 case R_PPC64_TOC16_HA:
ad8e1ba5 5623 case R_PPC64_TOC16_LO_DS:
4c52953f 5624 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5625 break;
5626
006589cf
AM
5627 /* Marker reloc. */
5628 case R_PPC64_ENTRY:
5629 break;
5630
5bd4f169
AM
5631 /* This relocation describes the C++ object vtable hierarchy.
5632 Reconstruct it for later use during GC. */
5633 case R_PPC64_GNU_VTINHERIT:
c152c796 5634 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5635 return FALSE;
5bd4f169
AM
5636 break;
5637
5638 /* This relocation describes which C++ vtable entries are actually
5639 used. Record for later use during GC. */
5640 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5641 BFD_ASSERT (h != NULL);
5642 if (h != NULL
5643 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5644 return FALSE;
5bd4f169
AM
5645 break;
5646
721956f4
AM
5647 case R_PPC64_REL14:
5648 case R_PPC64_REL14_BRTAKEN:
5649 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5650 {
5651 asection *dest = NULL;
5652
5653 /* Heuristic: If jumping outside our section, chances are
5654 we are going to need a stub. */
5655 if (h != NULL)
5656 {
5657 /* If the sym is weak it may be overridden later, so
5658 don't assume we know where a weak sym lives. */
5659 if (h->root.type == bfd_link_hash_defined)
5660 dest = h->root.u.def.section;
5661 }
5662 else
87d72d41
AM
5663 {
5664 Elf_Internal_Sym *isym;
5665
5666 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5667 abfd, r_symndx);
5668 if (isym == NULL)
5669 return FALSE;
5670
5671 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5672 }
5673
220c76dd 5674 if (dest != sec)
7c8fe5c4 5675 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5676 }
721956f4
AM
5677 /* Fall through. */
5678
5d1634d7 5679 case R_PPC64_REL24:
cbf95972
AM
5680 plt_list = ifunc;
5681 if (h != NULL)
5d1634d7 5682 {
e054468f
AM
5683 h->needs_plt = 1;
5684 if (h->root.root.string[0] == '.'
5685 && h->root.root.string[1] != '\0')
5686 ((struct ppc_link_hash_entry *) h)->is_func = 1;
cbf95972 5687
3a71aa26 5688 if (h == tga || h == dottga)
cbf95972
AM
5689 {
5690 sec->has_tls_reloc = 1;
5691 if (rel != relocs
5692 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5693 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5694 /* We have a new-style __tls_get_addr call with
5695 a marker reloc. */
5696 ;
5697 else
5698 /* Mark this section as having an old-style call. */
5699 sec->has_tls_get_addr_call = 1;
5700 }
5701 plt_list = &h->plt.plist;
411e1bfb 5702 }
cbf95972
AM
5703
5704 /* We may need a .plt entry if the function this reloc
5705 refers to is in a shared lib. */
5706 if (plt_list
5707 && !update_plt_info (abfd, plt_list, rel->r_addend))
5708 return FALSE;
411e1bfb
AM
5709 break;
5710
cbf95972
AM
5711 case R_PPC64_ADDR14:
5712 case R_PPC64_ADDR14_BRNTAKEN:
5713 case R_PPC64_ADDR14_BRTAKEN:
5714 case R_PPC64_ADDR24:
5715 goto dodyn;
5716
411e1bfb
AM
5717 case R_PPC64_TPREL64:
5718 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
0e1862bb 5719 if (bfd_link_pic (info))
411e1bfb
AM
5720 info->flags |= DF_STATIC_TLS;
5721 goto dotlstoc;
5722
5723 case R_PPC64_DTPMOD64:
5724 if (rel + 1 < rel_end
5725 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5726 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5727 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5728 else
951fd09b 5729 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5730 goto dotlstoc;
5731
5732 case R_PPC64_DTPREL64:
5733 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5734 if (rel != relocs
5735 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5736 && rel[-1].r_offset == rel->r_offset - 8)
5737 /* This is the second reloc of a dtpmod, dtprel pair.
5738 Don't mark with TLS_DTPREL. */
5739 goto dodyn;
5740
5741 dotlstoc:
5742 sec->has_tls_reloc = 1;
5743 if (h != NULL)
5744 {
5745 struct ppc_link_hash_entry *eh;
5746 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5747 eh->tls_mask |= tls_type;
411e1bfb
AM
5748 }
5749 else
5750 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5751 rel->r_addend, tls_type))
5752 return FALSE;
5753
7c8fe5c4
AM
5754 ppc64_sec = ppc64_elf_section_data (sec);
5755 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5756 {
3a71aa26
AM
5757 bfd_size_type amt;
5758
e7b938ca 5759 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5760 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5761 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5762 if (ppc64_sec->u.toc.symndx == NULL)
5763 return FALSE;
5764 amt = sec->size * sizeof (bfd_vma) / 8;
5765 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5766 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5767 return FALSE;
7c8fe5c4
AM
5768 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5769 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5770 }
5771 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5772 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5773 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5774
5775 /* Mark the second slot of a GD or LD entry.
5776 -1 to indicate GD and -2 to indicate LD. */
5777 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5778 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5779 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5780 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5781 goto dodyn;
5782
5783 case R_PPC64_TPREL16:
5784 case R_PPC64_TPREL16_LO:
5785 case R_PPC64_TPREL16_HI:
5786 case R_PPC64_TPREL16_HA:
5787 case R_PPC64_TPREL16_DS:
5788 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5789 case R_PPC64_TPREL16_HIGH:
5790 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5791 case R_PPC64_TPREL16_HIGHER:
5792 case R_PPC64_TPREL16_HIGHERA:
5793 case R_PPC64_TPREL16_HIGHEST:
5794 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 5795 if (bfd_link_pic (info))
411e1bfb 5796 {
afb93314 5797 info->flags |= DF_STATIC_TLS;
411e1bfb 5798 goto dodyn;
5d1634d7
AM
5799 }
5800 break;
5801
e86ce104 5802 case R_PPC64_ADDR64:
1e2f5b6e 5803 if (opd_sym_map != NULL
1e2f5b6e 5804 && rel + 1 < rel_end
4ce794b7 5805 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5806 {
8387904d 5807 if (h != NULL)
8c5b4e52 5808 ((struct ppc_link_hash_entry *) h)->is_func = 1;
8387904d
AM
5809 else
5810 {
5811 asection *s;
87d72d41 5812 Elf_Internal_Sym *isym;
1e2f5b6e 5813
87d72d41
AM
5814 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5815 abfd, r_symndx);
5816 if (isym == NULL)
8387904d 5817 return FALSE;
87d72d41
AM
5818
5819 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5820 if (s != NULL && s != sec)
51aecdc5 5821 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
8387904d 5822 }
1e2f5b6e 5823 }
e86ce104
AM
5824 /* Fall through. */
5825
65f38f15
AM
5826 case R_PPC64_ADDR16:
5827 case R_PPC64_ADDR16_DS:
5828 case R_PPC64_ADDR16_HA:
5829 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5830 case R_PPC64_ADDR16_HIGH:
5831 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5832 case R_PPC64_ADDR16_HIGHER:
5833 case R_PPC64_ADDR16_HIGHERA:
5834 case R_PPC64_ADDR16_HIGHEST:
5835 case R_PPC64_ADDR16_HIGHESTA:
5836 case R_PPC64_ADDR16_LO:
5837 case R_PPC64_ADDR16_LO_DS:
0e1862bb 5838 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5839 && rel->r_addend == 0)
5840 {
5841 /* We may need a .plt entry if this reloc refers to a
5842 function in a shared lib. */
5843 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5844 return FALSE;
5845 h->pointer_equality_needed = 1;
5846 }
5847 /* Fall through. */
5848
5849 case R_PPC64_REL30:
5850 case R_PPC64_REL32:
5851 case R_PPC64_REL64:
65f38f15 5852 case R_PPC64_ADDR32:
65f38f15
AM
5853 case R_PPC64_UADDR16:
5854 case R_PPC64_UADDR32:
5855 case R_PPC64_UADDR64:
5bd4f169 5856 case R_PPC64_TOC:
0e1862bb 5857 if (h != NULL && !bfd_link_pic (info))
81848ca0 5858 /* We may need a copy reloc. */
f5385ebf 5859 h->non_got_ref = 1;
81848ca0 5860
41bd81ab 5861 /* Don't propagate .opd relocs. */
1e2f5b6e 5862 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5863 break;
e86ce104 5864
65f38f15
AM
5865 /* If we are creating a shared library, and this is a reloc
5866 against a global symbol, or a non PC relative reloc
5867 against a local symbol, then we need to copy the reloc
5868 into the shared library. However, if we are linking with
5869 -Bsymbolic, we do not need to copy a reloc against a
5870 global symbol which is defined in an object we are
5871 including in the link (i.e., DEF_REGULAR is set). At
5872 this point we have not seen all the input files, so it is
5873 possible that DEF_REGULAR is not set now but will be set
5874 later (it is never cleared). In case of a weak definition,
5875 DEF_REGULAR may be cleared later by a strong definition in
5876 a shared library. We account for that possibility below by
f4656909 5877 storing information in the dyn_relocs field of the hash
65f38f15
AM
5878 table entry. A similar situation occurs when creating
5879 shared libraries and symbol visibility changes render the
5880 symbol local.
5881
5882 If on the other hand, we are creating an executable, we
5883 may need to keep relocations for symbols satisfied by a
5884 dynamic library if we manage to avoid copy relocs for the
5885 symbol. */
411e1bfb 5886 dodyn:
0e1862bb 5887 if ((bfd_link_pic (info)
1d483afe 5888 && (must_be_dyn_reloc (info, r_type)
65f38f15 5889 || (h != NULL
198f1157 5890 && (!SYMBOLIC_BIND (info, h)
65f38f15 5891 || h->root.type == bfd_link_hash_defweak
f5385ebf 5892 || !h->def_regular))))
f4656909 5893 || (ELIMINATE_COPY_RELOCS
0e1862bb 5894 && !bfd_link_pic (info)
65f38f15
AM
5895 && h != NULL
5896 && (h->root.type == bfd_link_hash_defweak
25f23106 5897 || !h->def_regular))
0e1862bb 5898 || (!bfd_link_pic (info)
25f23106 5899 && ifunc != NULL))
5bd4f169 5900 {
65f38f15
AM
5901 /* We must copy these reloc types into the output file.
5902 Create a reloc section in dynobj and make room for
5903 this reloc. */
5bd4f169
AM
5904 if (sreloc == NULL)
5905 {
83bac4b0
NC
5906 sreloc = _bfd_elf_make_dynamic_reloc_section
5907 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5908
5bd4f169 5909 if (sreloc == NULL)
83bac4b0 5910 return FALSE;
5bd4f169
AM
5911 }
5912
65f38f15
AM
5913 /* If this is a global symbol, we count the number of
5914 relocations we need for this symbol. */
5915 if (h != NULL)
5916 {
19e08130
AM
5917 struct elf_dyn_relocs *p;
5918 struct elf_dyn_relocs **head;
5919
ec338859 5920 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5921 p = *head;
5922 if (p == NULL || p->sec != sec)
5923 {
5924 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5925 if (p == NULL)
5926 return FALSE;
5927 p->next = *head;
5928 *head = p;
5929 p->sec = sec;
5930 p->count = 0;
5931 p->pc_count = 0;
5932 }
5933 p->count += 1;
5934 if (!must_be_dyn_reloc (info, r_type))
5935 p->pc_count += 1;
65f38f15
AM
5936 }
5937 else
5938 {
ec338859
AM
5939 /* Track dynamic relocs needed for local syms too.
5940 We really need local syms available to do this
5941 easily. Oh well. */
19e08130
AM
5942 struct ppc_dyn_relocs *p;
5943 struct ppc_dyn_relocs **head;
5944 bfd_boolean is_ifunc;
ec338859 5945 asection *s;
6edfbbad 5946 void *vpp;
87d72d41 5947 Elf_Internal_Sym *isym;
6edfbbad 5948
87d72d41
AM
5949 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5950 abfd, r_symndx);
5951 if (isym == NULL)
b34976b6 5952 return FALSE;
ec338859 5953
87d72d41
AM
5954 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5955 if (s == NULL)
5956 s = sec;
5957
6edfbbad 5958 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5959 head = (struct ppc_dyn_relocs **) vpp;
5960 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5961 p = *head;
5962 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5963 p = p->next;
5964 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5965 {
5966 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5967 if (p == NULL)
5968 return FALSE;
5969 p->next = *head;
5970 *head = p;
5971 p->sec = sec;
5972 p->ifunc = is_ifunc;
5973 p->count = 0;
5974 }
5975 p->count += 1;
ec338859 5976 }
65f38f15 5977 }
5bd4f169 5978 break;
65f38f15
AM
5979
5980 default:
96e0dda4 5981 break;
5bd4f169
AM
5982 }
5983 }
5984
b34976b6 5985 return TRUE;
5bd4f169
AM
5986}
5987
ee67d69a
AM
5988/* Merge backend specific data from an object file to the output
5989 object file when linking. */
5990
5991static bfd_boolean
50e03d47 5992ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5993{
50e03d47 5994 bfd *obfd = info->output_bfd;
ee67d69a
AM
5995 unsigned long iflags, oflags;
5996
5997 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5998 return TRUE;
5999
6000 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6001 return TRUE;
6002
50e03d47 6003 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
6004 return FALSE;
6005
6006 iflags = elf_elfheader (ibfd)->e_flags;
6007 oflags = elf_elfheader (obfd)->e_flags;
6008
f6c7c3e8 6009 if (iflags & ~EF_PPC64_ABI)
ee67d69a 6010 {
4eca0228 6011 _bfd_error_handler
695344c0 6012 /* xgettext:c-format */
ee67d69a
AM
6013 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6014 bfd_set_error (bfd_error_bad_value);
6015 return FALSE;
6016 }
f6c7c3e8 6017 else if (iflags != oflags && iflags != 0)
ee67d69a 6018 {
4eca0228 6019 _bfd_error_handler
695344c0 6020 /* xgettext:c-format */
ee67d69a
AM
6021 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6022 ibfd, iflags, oflags);
6023 bfd_set_error (bfd_error_bad_value);
6024 return FALSE;
6025 }
6026
50e03d47 6027 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
005d79fd 6028
ee67d69a 6029 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 6030 _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
6031
6032 return TRUE;
6033}
6034
6035static bfd_boolean
6036ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6037{
6038 /* Print normal ELF private data. */
6039 _bfd_elf_print_private_bfd_data (abfd, ptr);
6040
6041 if (elf_elfheader (abfd)->e_flags != 0)
6042 {
6043 FILE *file = ptr;
6044
ee67d69a
AM
6045 fprintf (file, _("private flags = 0x%lx:"),
6046 elf_elfheader (abfd)->e_flags);
6047
6048 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6049 fprintf (file, _(" [abiv%ld]"),
6050 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6051 fputc ('\n', file);
6052 }
6053
6054 return TRUE;
6055}
6056
8387904d 6057/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
6058 of the code entry point, and its section, which must be in the same
6059 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
6060
6061static bfd_vma
6062opd_entry_value (asection *opd_sec,
6063 bfd_vma offset,
6064 asection **code_sec,
aef36ac1
AM
6065 bfd_vma *code_off,
6066 bfd_boolean in_code_sec)
8387904d
AM
6067{
6068 bfd *opd_bfd = opd_sec->owner;
8860955f 6069 Elf_Internal_Rela *relocs;
8387904d 6070 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 6071 bfd_vma val;
8387904d 6072
9f296da3
AM
6073 /* No relocs implies we are linking a --just-symbols object, or looking
6074 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
6075 if (opd_sec->reloc_count == 0)
6076 {
729eabd5 6077 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 6078
729eabd5
AM
6079 if (contents == NULL)
6080 {
6081 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6082 return (bfd_vma) -1;
6083 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6084 }
ee1e4ede 6085
dbb3fbbb 6086 /* PR 17512: file: 64b9dfbb. */
451dfd38 6087 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
6088 return (bfd_vma) -1;
6089
729eabd5 6090 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
6091 if (code_sec != NULL)
6092 {
6093 asection *sec, *likely = NULL;
ee1e4ede 6094
aef36ac1 6095 if (in_code_sec)
4b85d634 6096 {
aef36ac1
AM
6097 sec = *code_sec;
6098 if (sec->vma <= val
6099 && val < sec->vma + sec->size)
6100 likely = sec;
6101 else
6102 val = -1;
6103 }
6104 else
6105 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6106 if (sec->vma <= val
6107 && (sec->flags & SEC_LOAD) != 0
6108 && (sec->flags & SEC_ALLOC) != 0)
6109 likely = sec;
6110 if (likely != NULL)
6111 {
6112 *code_sec = likely;
6113 if (code_off != NULL)
6114 *code_off = val - likely->vma;
4b85d634
AM
6115 }
6116 }
aef36ac1 6117 return val;
4b85d634
AM
6118 }
6119
0c8d6e5c 6120 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 6121
729eabd5 6122 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
6123 if (relocs == NULL)
6124 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
6125 /* PR 17512: file: df8e1fd6. */
6126 if (relocs == NULL)
6127 return (bfd_vma) -1;
645ea6a9 6128
8387904d 6129 /* Go find the opd reloc at the sym address. */
8860955f 6130 lo = relocs;
8387904d 6131 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 6132 val = (bfd_vma) -1;
8387904d
AM
6133 while (lo < hi)
6134 {
6135 look = lo + (hi - lo) / 2;
6136 if (look->r_offset < offset)
6137 lo = look + 1;
6138 else if (look->r_offset > offset)
6139 hi = look;
6140 else
6141 {
0ffa91dd
NC
6142 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6143
8387904d
AM
6144 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6145 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6146 {
6147 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 6148 asection *sec = NULL;
8387904d 6149
b53dfeb2
AM
6150 if (symndx >= symtab_hdr->sh_info
6151 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
6152 {
6153 struct elf_link_hash_entry **sym_hashes;
6154 struct elf_link_hash_entry *rh;
6155
6156 sym_hashes = elf_sym_hashes (opd_bfd);
6157 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
6158 if (rh != NULL)
6159 {
6160 rh = elf_follow_link (rh);
bb854a36
AM
6161 if (rh->root.type != bfd_link_hash_defined
6162 && rh->root.type != bfd_link_hash_defweak)
6163 break;
6164 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 6165 {
bb854a36
AM
6166 val = rh->root.u.def.value;
6167 sec = rh->root.u.def.section;
b53dfeb2
AM
6168 }
6169 }
6170 }
6171
6172 if (sec == NULL)
6173 {
6174 Elf_Internal_Sym *sym;
6175
6176 if (symndx < symtab_hdr->sh_info)
6177 {
6178 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6179 if (sym == NULL)
6180 {
6181 size_t symcnt = symtab_hdr->sh_info;
6182 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6183 symcnt, 0,
6184 NULL, NULL, NULL);
6185 if (sym == NULL)
6186 break;
6187 symtab_hdr->contents = (bfd_byte *) sym;
6188 }
6189 sym += symndx;
128205bb
AM
6190 }
6191 else
6192 {
b53dfeb2
AM
6193 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6194 1, symndx,
6195 NULL, NULL, NULL);
128205bb
AM
6196 if (sym == NULL)
6197 break;
128205bb 6198 }
b53dfeb2
AM
6199 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6200 if (sec == NULL)
6201 break;
6202 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6203 val = sym->st_value;
8387904d 6204 }
b53dfeb2 6205
8387904d
AM
6206 val += look->r_addend;
6207 if (code_off != NULL)
6208 *code_off = val;
6209 if (code_sec != NULL)
aef36ac1
AM
6210 {
6211 if (in_code_sec && *code_sec != sec)
6212 return -1;
6213 else
6214 *code_sec = sec;
6215 }
b53dfeb2 6216 if (sec->output_section != NULL)
8387904d 6217 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
6218 }
6219 break;
6220 }
6221 }
645ea6a9 6222
645ea6a9 6223 return val;
8387904d
AM
6224}
6225
aef36ac1
AM
6226/* If the ELF symbol SYM might be a function in SEC, return the
6227 function size and set *CODE_OFF to the function's entry point,
6228 otherwise return zero. */
9f296da3 6229
aef36ac1
AM
6230static bfd_size_type
6231ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6232 bfd_vma *code_off)
9f296da3 6233{
aef36ac1
AM
6234 bfd_size_type size;
6235
6236 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6237 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6238 return 0;
6239
6240 size = 0;
6241 if (!(sym->flags & BSF_SYNTHETIC))
6242 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6243
6244 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 6245 {
b07bca4e
AM
6246 struct _opd_sec_data *opd = get_opd_info (sym->section);
6247 bfd_vma symval = sym->value;
6248
6249 if (opd != NULL
6250 && opd->adjust != NULL
6251 && elf_section_data (sym->section)->relocs != NULL)
6252 {
6253 /* opd_entry_value will use cached relocs that have been
6254 adjusted, but with raw symbols. That means both local
6255 and global symbols need adjusting. */
6256 long adjust = opd->adjust[OPD_NDX (symval)];
6257 if (adjust == -1)
6258 return 0;
6259 symval += adjust;
6260 }
6261
6262 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
6263 &sec, code_off, TRUE) == (bfd_vma) -1)
6264 return 0;
6265 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6266 symbol. This size has nothing to do with the code size of the
6267 function, which is what we're supposed to return, but the
6268 code size isn't available without looking up the dot-sym.
6269 However, doing that would be a waste of time particularly
6270 since elf_find_function will look at the dot-sym anyway.
6271 Now, elf_find_function will keep the largest size of any
6272 function sym found at the code address of interest, so return
6273 1 here to avoid it incorrectly caching a larger function size
6274 for a small function. This does mean we return the wrong
6275 size for a new-ABI function of size 24, but all that does is
6276 disable caching for such functions. */
6277 if (size == 24)
6278 size = 1;
9f296da3 6279 }
aef36ac1
AM
6280 else
6281 {
6282 if (sym->section != sec)
6283 return 0;
6284 *code_off = sym->value;
6285 }
6286 if (size == 0)
6287 size = 1;
6288 return size;
9f296da3
AM
6289}
6290
854b41e7
AM
6291/* Return true if symbol is defined in a regular object file. */
6292
6293static bfd_boolean
6294is_static_defined (struct elf_link_hash_entry *h)
6295{
6296 return ((h->root.type == bfd_link_hash_defined
6297 || h->root.type == bfd_link_hash_defweak)
6298 && h->root.u.def.section != NULL
6299 && h->root.u.def.section->output_section != NULL);
6300}
6301
b31867b6
AM
6302/* If FDH is a function descriptor symbol, return the associated code
6303 entry symbol if it is defined. Return NULL otherwise. */
6304
6305static struct ppc_link_hash_entry *
6306defined_code_entry (struct ppc_link_hash_entry *fdh)
6307{
6308 if (fdh->is_func_descriptor)
6309 {
6310 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6311 if (fh->elf.root.type == bfd_link_hash_defined
6312 || fh->elf.root.type == bfd_link_hash_defweak)
6313 return fh;
6314 }
6315 return NULL;
6316}
6317
6318/* If FH is a function code entry symbol, return the associated
6319 function descriptor symbol if it is defined. Return NULL otherwise. */
6320
6321static struct ppc_link_hash_entry *
6322defined_func_desc (struct ppc_link_hash_entry *fh)
6323{
6324 if (fh->oh != NULL
6325 && fh->oh->is_func_descriptor)
6326 {
6327 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6328 if (fdh->elf.root.type == bfd_link_hash_defined
6329 || fdh->elf.root.type == bfd_link_hash_defweak)
6330 return fdh;
6331 }
6332 return NULL;
6333}
6334
8c5b4e52
AM
6335static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6336
6337/* Garbage collect sections, after first dealing with dot-symbols. */
6338
6339static bfd_boolean
6340ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6341{
6342 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6343
6344 if (htab != NULL && htab->need_func_desc_adj)
6345 {
6346 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6347 htab->need_func_desc_adj = 0;
6348 }
6349 return bfd_elf_gc_sections (abfd, info);
6350}
6351
74f0fb50
AM
6352/* Mark all our entry sym sections, both opd and code section. */
6353
6354static void
6355ppc64_elf_gc_keep (struct bfd_link_info *info)
6356{
6357 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6358 struct bfd_sym_chain *sym;
6359
4dfe6ac6
NC
6360 if (htab == NULL)
6361 return;
6362
74f0fb50
AM
6363 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6364 {
b31867b6 6365 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6366 asection *sec;
6367
6368 eh = (struct ppc_link_hash_entry *)
b31867b6 6369 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6370 if (eh == NULL)
6371 continue;
6372 if (eh->elf.root.type != bfd_link_hash_defined
6373 && eh->elf.root.type != bfd_link_hash_defweak)
6374 continue;
6375
b31867b6
AM
6376 fh = defined_code_entry (eh);
6377 if (fh != NULL)
74f0fb50 6378 {
b31867b6 6379 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6380 sec->flags |= SEC_KEEP;
6381 }
6382 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6383 && opd_entry_value (eh->elf.root.u.def.section,
6384 eh->elf.root.u.def.value,
aef36ac1 6385 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6386 sec->flags |= SEC_KEEP;
6387
6388 sec = eh->elf.root.u.def.section;
6389 sec->flags |= SEC_KEEP;
6390 }
6391}
6392
64d03ab5
AM
6393/* Mark sections containing dynamically referenced symbols. When
6394 building shared libraries, we must assume that any visible symbol is
6395 referenced. */
6396
6397static bfd_boolean
6398ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6399{
6400 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6401 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6402 struct ppc_link_hash_entry *fdh;
b407645f 6403 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 6404
64d03ab5 6405 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6406 fdh = defined_func_desc (eh);
6407 if (fdh != NULL)
6408 eh = fdh;
64d03ab5
AM
6409
6410 if ((eh->elf.root.type == bfd_link_hash_defined
6411 || eh->elf.root.type == bfd_link_hash_defweak)
6412 && (eh->elf.ref_dynamic
1c9177d9 6413 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 6414 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 6415 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 6416 && (!bfd_link_executable (info)
b407645f
AM
6417 || info->export_dynamic
6418 || (eh->elf.dynamic
6419 && d != NULL
6420 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
4c58e0d8
AM
6421 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6422 || !bfd_hide_sym_by_version (info->version_info,
6423 eh->elf.root.root.string)))))
64d03ab5
AM
6424 {
6425 asection *code_sec;
b31867b6 6426 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6427
6428 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6429
6430 /* Function descriptor syms cause the associated
6431 function code sym section to be marked. */
b31867b6
AM
6432 fh = defined_code_entry (eh);
6433 if (fh != NULL)
6434 {
6435 code_sec = fh->elf.root.u.def.section;
6436 code_sec->flags |= SEC_KEEP;
6437 }
64d03ab5
AM
6438 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6439 && opd_entry_value (eh->elf.root.u.def.section,
6440 eh->elf.root.u.def.value,
aef36ac1 6441 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6442 code_sec->flags |= SEC_KEEP;
6443 }
6444
6445 return TRUE;
6446}
6447
5bd4f169
AM
6448/* Return the section that should be marked against GC for a given
6449 relocation. */
6450
6451static asection *
4ce794b7 6452ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6453 struct bfd_link_info *info,
4ce794b7
AM
6454 Elf_Internal_Rela *rel,
6455 struct elf_link_hash_entry *h,
6456 Elf_Internal_Sym *sym)
5bd4f169 6457{
ccfa59ea
AM
6458 asection *rsec;
6459
ccfa59ea
AM
6460 /* Syms return NULL if we're marking .opd, so we avoid marking all
6461 function sections, as all functions are referenced in .opd. */
6462 rsec = NULL;
6463 if (get_opd_info (sec) != NULL)
6464 return rsec;
1e2f5b6e 6465
5bd4f169
AM
6466 if (h != NULL)
6467 {
04c9666a 6468 enum elf_ppc64_reloc_type r_type;
b31867b6 6469 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6470
4ce794b7 6471 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6472 switch (r_type)
5bd4f169
AM
6473 {
6474 case R_PPC64_GNU_VTINHERIT:
6475 case R_PPC64_GNU_VTENTRY:
6476 break;
6477
6478 default:
6479 switch (h->root.type)
6480 {
6481 case bfd_link_hash_defined:
6482 case bfd_link_hash_defweak:
ccfa59ea 6483 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6484 fdh = defined_func_desc (eh);
6485 if (fdh != NULL)
8c5b4e52
AM
6486 {
6487 /* -mcall-aixdesc code references the dot-symbol on
6488 a call reloc. Mark the function descriptor too
6489 against garbage collection. */
6490 fdh->elf.mark = 1;
6491 if (fdh->elf.u.weakdef != NULL)
6492 fdh->elf.u.weakdef->mark = 1;
6493 eh = fdh;
6494 }
1e2f5b6e
AM
6495
6496 /* Function descriptor syms cause the associated
6497 function code sym section to be marked. */
b31867b6
AM
6498 fh = defined_code_entry (eh);
6499 if (fh != NULL)
ccfa59ea
AM
6500 {
6501 /* They also mark their opd section. */
74f0fb50 6502 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6503
b31867b6 6504 rsec = fh->elf.root.u.def.section;
ccfa59ea 6505 }
8387904d
AM
6506 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6507 && opd_entry_value (eh->elf.root.u.def.section,
6508 eh->elf.root.u.def.value,
aef36ac1 6509 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6510 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6511 else
1e2f5b6e
AM
6512 rsec = h->root.u.def.section;
6513 break;
5bd4f169
AM
6514
6515 case bfd_link_hash_common:
1e2f5b6e
AM
6516 rsec = h->root.u.c.p->section;
6517 break;
5bd4f169
AM
6518
6519 default:
fb34365b 6520 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6521 }
6522 }
6523 }
6524 else
6525 {
74f0fb50 6526 struct _opd_sec_data *opd;
1e2f5b6e
AM
6527
6528 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6529 opd = get_opd_info (rsec);
6530 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6531 {
74f0fb50 6532 rsec->gc_mark = 1;
ccfa59ea 6533
51aecdc5 6534 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6535 }
5bd4f169
AM
6536 }
6537
1e2f5b6e 6538 return rsec;
5bd4f169
AM
6539}
6540
65f38f15
AM
6541/* Update the .got, .plt. and dynamic reloc reference counts for the
6542 section being removed. */
5bd4f169 6543
b34976b6 6544static bfd_boolean
4ce794b7
AM
6545ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6546 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 6547{
411e1bfb 6548 struct ppc_link_hash_table *htab;
5bd4f169
AM
6549 Elf_Internal_Shdr *symtab_hdr;
6550 struct elf_link_hash_entry **sym_hashes;
411e1bfb 6551 struct got_entry **local_got_ents;
5bd4f169 6552 const Elf_Internal_Rela *rel, *relend;
5bd4f169 6553
0e1862bb 6554 if (bfd_link_relocatable (info))
7dda2462
TG
6555 return TRUE;
6556
680a3378
AM
6557 if ((sec->flags & SEC_ALLOC) == 0)
6558 return TRUE;
6559
ec338859
AM
6560 elf_section_data (sec)->local_dynrel = NULL;
6561
411e1bfb 6562 htab = ppc_hash_table (info);
4dfe6ac6
NC
6563 if (htab == NULL)
6564 return FALSE;
6565
0ffa91dd 6566 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 6567 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 6568 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
6569
6570 relend = relocs + sec->reloc_count;
6571 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
6572 {
6573 unsigned long r_symndx;
04c9666a 6574 enum elf_ppc64_reloc_type r_type;
58ac9f71 6575 struct elf_link_hash_entry *h = NULL;
cbf95972 6576 struct plt_entry **plt_list;
f961d9dd 6577 unsigned char tls_type = 0;
5bd4f169 6578
a33d1f77 6579 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 6580 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
6581 if (r_symndx >= symtab_hdr->sh_info)
6582 {
6583 struct ppc_link_hash_entry *eh;
6061a67d
AM
6584 struct elf_dyn_relocs **pp;
6585 struct elf_dyn_relocs *p;
58ac9f71
AM
6586
6587 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6588 h = elf_follow_link (h);
58ac9f71
AM
6589 eh = (struct ppc_link_hash_entry *) h;
6590
6591 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6592 if (p->sec == sec)
6593 {
6594 /* Everything must go for SEC. */
6595 *pp = p->next;
6596 break;
6597 }
6598 }
6599
a33d1f77
AM
6600 switch (r_type)
6601 {
411e1bfb
AM
6602 case R_PPC64_GOT_TLSLD16:
6603 case R_PPC64_GOT_TLSLD16_LO:
6604 case R_PPC64_GOT_TLSLD16_HI:
6605 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 6606 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6607 goto dogot;
6608
6609 case R_PPC64_GOT_TLSGD16:
6610 case R_PPC64_GOT_TLSGD16_LO:
6611 case R_PPC64_GOT_TLSGD16_HI:
6612 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6613 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6614 goto dogot;
6615
6616 case R_PPC64_GOT_TPREL16_DS:
6617 case R_PPC64_GOT_TPREL16_LO_DS:
6618 case R_PPC64_GOT_TPREL16_HI:
6619 case R_PPC64_GOT_TPREL16_HA:
6620 tls_type = TLS_TLS | TLS_TPREL;
6621 goto dogot;
6622
6623 case R_PPC64_GOT_DTPREL16_DS:
6624 case R_PPC64_GOT_DTPREL16_LO_DS:
6625 case R_PPC64_GOT_DTPREL16_HI:
6626 case R_PPC64_GOT_DTPREL16_HA:
6627 tls_type = TLS_TLS | TLS_DTPREL;
6628 goto dogot;
6629
a33d1f77
AM
6630 case R_PPC64_GOT16:
6631 case R_PPC64_GOT16_DS:
6632 case R_PPC64_GOT16_HA:
6633 case R_PPC64_GOT16_HI:
6634 case R_PPC64_GOT16_LO:
6635 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
6636 dogot:
6637 {
6638 struct got_entry *ent;
6639
58ac9f71
AM
6640 if (h != NULL)
6641 ent = h->got.glist;
411e1bfb
AM
6642 else
6643 ent = local_got_ents[r_symndx];
6644
6645 for (; ent != NULL; ent = ent->next)
6646 if (ent->addend == rel->r_addend
e717da7e 6647 && ent->owner == abfd
411e1bfb
AM
6648 && ent->tls_type == tls_type)
6649 break;
6650 if (ent == NULL)
6651 abort ();
6652 if (ent->got.refcount > 0)
6653 ent->got.refcount -= 1;
6654 }
a33d1f77 6655 break;
65f38f15 6656
a33d1f77
AM
6657 case R_PPC64_PLT16_HA:
6658 case R_PPC64_PLT16_HI:
6659 case R_PPC64_PLT16_LO:
6660 case R_PPC64_PLT32:
6661 case R_PPC64_PLT64:
721956f4
AM
6662 case R_PPC64_REL14:
6663 case R_PPC64_REL14_BRNTAKEN:
6664 case R_PPC64_REL14_BRTAKEN:
5d1634d7 6665 case R_PPC64_REL24:
cbf95972 6666 plt_list = NULL;
58ac9f71 6667 if (h != NULL)
cbf95972
AM
6668 plt_list = &h->plt.plist;
6669 else if (local_got_ents != NULL)
6670 {
6671 struct plt_entry **local_plt = (struct plt_entry **)
6672 (local_got_ents + symtab_hdr->sh_info);
6673 unsigned char *local_got_tls_masks = (unsigned char *)
6674 (local_plt + symtab_hdr->sh_info);
6675 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6676 plt_list = local_plt + r_symndx;
6677 }
6678 if (plt_list)
5d1634d7 6679 {
411e1bfb
AM
6680 struct plt_entry *ent;
6681
cbf95972 6682 for (ent = *plt_list; ent != NULL; ent = ent->next)
411e1bfb
AM
6683 if (ent->addend == rel->r_addend)
6684 break;
7c6c1722 6685 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 6686 ent->plt.refcount -= 1;
5d1634d7 6687 }
e86ce104 6688 break;
5d1634d7 6689
a33d1f77
AM
6690 default:
6691 break;
6692 }
6693 }
b34976b6 6694 return TRUE;
5bd4f169
AM
6695}
6696
deb0e272
AM
6697/* The maximum size of .sfpr. */
6698#define SFPR_MAX (218*4)
6699
6700struct sfpr_def_parms
6701{
699733f6
AM
6702 const char name[12];
6703 unsigned char lo, hi;
deb0e272
AM
6704 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6705 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6706};
6707
a4b6fadd
AM
6708/* Auto-generate _save*, _rest* functions in .sfpr.
6709 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6710 instead. */
deb0e272 6711
4dfe6ac6 6712static bfd_boolean
a4b6fadd
AM
6713sfpr_define (struct bfd_link_info *info,
6714 const struct sfpr_def_parms *parm,
6715 asection *stub_sec)
deb0e272
AM
6716{
6717 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6718 unsigned int i;
6719 size_t len = strlen (parm->name);
6720 bfd_boolean writing = FALSE;
699733f6 6721 char sym[16];
deb0e272 6722
4dfe6ac6
NC
6723 if (htab == NULL)
6724 return FALSE;
6725
deb0e272
AM
6726 memcpy (sym, parm->name, len);
6727 sym[len + 2] = 0;
6728
6729 for (i = parm->lo; i <= parm->hi; i++)
6730 {
a4b6fadd 6731 struct ppc_link_hash_entry *h;
deb0e272
AM
6732
6733 sym[len + 0] = i / 10 + '0';
6734 sym[len + 1] = i % 10 + '0';
a4b6fadd 6735 h = (struct ppc_link_hash_entry *)
b32547cd 6736 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
a4b6fadd 6737 if (stub_sec != NULL)
deb0e272 6738 {
a4b6fadd
AM
6739 if (h != NULL
6740 && h->elf.root.type == bfd_link_hash_defined
6741 && h->elf.root.u.def.section == htab->sfpr)
6742 {
6743 struct elf_link_hash_entry *s;
6744 char buf[32];
6745 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6746 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6747 if (s == NULL)
6748 return FALSE;
6749 if (s->root.type == bfd_link_hash_new
6750 || (s->root.type = bfd_link_hash_defined
6751 && s->root.u.def.section == stub_sec))
6752 {
6753 s->root.type = bfd_link_hash_defined;
6754 s->root.u.def.section = stub_sec;
6755 s->root.u.def.value = (stub_sec->size
6756 + h->elf.root.u.def.value);
6757 s->ref_regular = 1;
6758 s->def_regular = 1;
6759 s->ref_regular_nonweak = 1;
6760 s->forced_local = 1;
6761 s->non_elf = 0;
6762 s->root.linker_def = 1;
6763 }
6764 }
6765 continue;
6766 }
6767 if (h != NULL)
6768 {
6769 h->save_res = 1;
6770 if (!h->elf.def_regular)
deb0e272 6771 {
a4b6fadd
AM
6772 h->elf.root.type = bfd_link_hash_defined;
6773 h->elf.root.u.def.section = htab->sfpr;
6774 h->elf.root.u.def.value = htab->sfpr->size;
6775 h->elf.type = STT_FUNC;
6776 h->elf.def_regular = 1;
b32547cd 6777 h->elf.non_elf = 0;
a4b6fadd
AM
6778 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6779 writing = TRUE;
deb0e272 6780 if (htab->sfpr->contents == NULL)
a4b6fadd
AM
6781 {
6782 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6783 if (htab->sfpr->contents == NULL)
6784 return FALSE;
6785 }
deb0e272
AM
6786 }
6787 }
6788 if (writing)
6789 {
6790 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6791 if (i != parm->hi)
6792 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6793 else
6794 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6795 htab->sfpr->size = p - htab->sfpr->contents;
6796 }
6797 }
6798
6799 return TRUE;
6800}
6801
6802static bfd_byte *
6803savegpr0 (bfd *abfd, bfd_byte *p, int r)
6804{
6805 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6806 return p + 4;
6807}
6808
6809static bfd_byte *
6810savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6811{
6812 p = savegpr0 (abfd, p, r);
a078d95a 6813 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6814 p = p + 4;
6815 bfd_put_32 (abfd, BLR, p);
6816 return p + 4;
6817}
6818
6819static bfd_byte *
6820restgpr0 (bfd *abfd, bfd_byte *p, int r)
6821{
6822 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6823 return p + 4;
6824}
6825
6826static bfd_byte *
6827restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6828{
a078d95a 6829 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6830 p = p + 4;
6831 p = restgpr0 (abfd, p, r);
6832 bfd_put_32 (abfd, MTLR_R0, p);
6833 p = p + 4;
6834 if (r == 29)
6835 {
6836 p = restgpr0 (abfd, p, 30);
6837 p = restgpr0 (abfd, p, 31);
6838 }
6839 bfd_put_32 (abfd, BLR, p);
6840 return p + 4;
6841}
6842
6843static bfd_byte *
6844savegpr1 (bfd *abfd, bfd_byte *p, int r)
6845{
6846 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6847 return p + 4;
6848}
6849
6850static bfd_byte *
6851savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6852{
6853 p = savegpr1 (abfd, p, r);
6854 bfd_put_32 (abfd, BLR, p);
6855 return p + 4;
6856}
6857
6858static bfd_byte *
6859restgpr1 (bfd *abfd, bfd_byte *p, int r)
6860{
6861 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6862 return p + 4;
6863}
6864
6865static bfd_byte *
6866restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6867{
6868 p = restgpr1 (abfd, p, r);
6869 bfd_put_32 (abfd, BLR, p);
6870 return p + 4;
6871}
6872
6873static bfd_byte *
6874savefpr (bfd *abfd, bfd_byte *p, int r)
6875{
6876 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6877 return p + 4;
6878}
6879
6880static bfd_byte *
6881savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6882{
6883 p = savefpr (abfd, p, r);
a078d95a 6884 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6885 p = p + 4;
6886 bfd_put_32 (abfd, BLR, p);
6887 return p + 4;
6888}
6889
6890static bfd_byte *
6891restfpr (bfd *abfd, bfd_byte *p, int r)
6892{
6893 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6894 return p + 4;
6895}
6896
6897static bfd_byte *
6898restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6899{
a078d95a 6900 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6901 p = p + 4;
6902 p = restfpr (abfd, p, r);
6903 bfd_put_32 (abfd, MTLR_R0, p);
6904 p = p + 4;
6905 if (r == 29)
6906 {
6907 p = restfpr (abfd, p, 30);
6908 p = restfpr (abfd, p, 31);
6909 }
6910 bfd_put_32 (abfd, BLR, p);
6911 return p + 4;
6912}
6913
6914static bfd_byte *
6915savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6916{
6917 p = savefpr (abfd, p, r);
6918 bfd_put_32 (abfd, BLR, p);
6919 return p + 4;
6920}
6921
6922static bfd_byte *
6923restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6924{
6925 p = restfpr (abfd, p, r);
6926 bfd_put_32 (abfd, BLR, p);
6927 return p + 4;
6928}
6929
6930static bfd_byte *
6931savevr (bfd *abfd, bfd_byte *p, int r)
6932{
6933 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6934 p = p + 4;
6935 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6936 return p + 4;
6937}
6938
6939static bfd_byte *
6940savevr_tail (bfd *abfd, bfd_byte *p, int r)
6941{
6942 p = savevr (abfd, p, r);
6943 bfd_put_32 (abfd, BLR, p);
6944 return p + 4;
6945}
6946
6947static bfd_byte *
6948restvr (bfd *abfd, bfd_byte *p, int r)
6949{
6950 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6951 p = p + 4;
6952 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6953 return p + 4;
6954}
6955
6956static bfd_byte *
6957restvr_tail (bfd *abfd, bfd_byte *p, int r)
6958{
6959 p = restvr (abfd, p, r);
6960 bfd_put_32 (abfd, BLR, p);
6961 return p + 4;
6962}
6963
e86ce104
AM
6964/* Called via elf_link_hash_traverse to transfer dynamic linking
6965 information on function code symbol entries to their corresponding
6966 function descriptor symbol entries. */
deb0e272 6967
b34976b6 6968static bfd_boolean
4ce794b7 6969func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6970{
e86ce104 6971 struct bfd_link_info *info;
65f38f15 6972 struct ppc_link_hash_table *htab;
50bc7936
AM
6973 struct ppc_link_hash_entry *fh;
6974 struct ppc_link_hash_entry *fdh;
6975 bfd_boolean force_local;
5bd4f169 6976
50bc7936
AM
6977 fh = (struct ppc_link_hash_entry *) h;
6978 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6979 return TRUE;
e86ce104 6980
8c5b4e52
AM
6981 if (!fh->is_func)
6982 return TRUE;
6983
6984 if (fh->elf.root.root.string[0] != '.'
6985 || fh->elf.root.root.string[1] == '\0')
6986 return TRUE;
6987
4ce794b7 6988 info = inf;
65f38f15 6989 htab = ppc_hash_table (info);
4dfe6ac6
NC
6990 if (htab == NULL)
6991 return FALSE;
5bd4f169 6992
8c5b4e52
AM
6993 /* Find the corresponding function descriptor symbol. */
6994 fdh = lookup_fdh (fh, htab);
6995
c09bdfe5
AM
6996 /* Resolve undefined references to dot-symbols as the value
6997 in the function descriptor, if we have one in a regular object.
6998 This is to satisfy cases like ".quad .foo". Calls to functions
6999 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
7000 if ((fh->elf.root.type == bfd_link_hash_undefined
7001 || fh->elf.root.type == bfd_link_hash_undefweak)
7002 && (fdh->elf.root.type == bfd_link_hash_defined
7003 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
7004 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7005 && opd_entry_value (fdh->elf.root.u.def.section,
7006 fdh->elf.root.u.def.value,
c09bdfe5 7007 &fh->elf.root.u.def.section,
aef36ac1 7008 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 7009 {
b31867b6 7010 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 7011 fh->elf.forced_local = 1;
b31867b6
AM
7012 fh->elf.def_regular = fdh->elf.def_regular;
7013 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
7014 }
7015
8c5b4e52
AM
7016 if (!fh->elf.dynamic)
7017 {
7018 struct plt_entry *ent;
5bd4f169 7019
8c5b4e52
AM
7020 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7021 if (ent->plt.refcount > 0)
7022 break;
7023 if (ent == NULL)
7024 return TRUE;
7025 }
5bd4f169 7026
8c5b4e52 7027 /* Create a descriptor as undefined if necessary. */
50bc7936 7028 if (fdh == NULL
0e1862bb 7029 && !bfd_link_executable (info)
50bc7936
AM
7030 && (fh->elf.root.type == bfd_link_hash_undefined
7031 || fh->elf.root.type == bfd_link_hash_undefweak))
7032 {
908b32fc 7033 fdh = make_fdh (info, fh);
bb700d78
AM
7034 if (fdh == NULL)
7035 return FALSE;
50bc7936 7036 }
648cca2c 7037
8c5b4e52 7038 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
7039 if (fdh != NULL
7040 && fdh->fake
8c5b4e52
AM
7041 && (fh->elf.root.type == bfd_link_hash_defined
7042 || fh->elf.root.type == bfd_link_hash_defweak))
7043 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 7044
8c5b4e52
AM
7045 /* Transfer dynamic linking information to the function descriptor. */
7046 if (fdh != NULL)
7047 {
f5385ebf
AM
7048 fdh->elf.ref_regular |= fh->elf.ref_regular;
7049 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7050 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7051 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
7052 fdh->elf.dynamic |= fh->elf.dynamic;
7053 fdh->elf.needs_plt |= (fh->elf.needs_plt
7054 || fh->elf.type == STT_FUNC
7055 || fh->elf.type == STT_GNU_IFUNC);
7056 move_plt_plist (fh, fdh);
7057
7058 if (!fdh->elf.forced_local
7059 && fh->elf.dynindx != -1)
7060 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7061 return FALSE;
e86ce104
AM
7062 }
7063
50bc7936
AM
7064 /* Now that the info is on the function descriptor, clear the
7065 function code sym info. Any function code syms for which we
7066 don't have a definition in a regular file, we force local.
7067 This prevents a shared library from exporting syms that have
7068 been imported from another library. Function code syms that
7069 are really in the library we must leave global to prevent the
7070 linker dragging in a definition from a static library. */
93f3fa99
AM
7071 force_local = (!fh->elf.def_regular
7072 || fdh == NULL
7073 || !fdh->elf.def_regular
7074 || fdh->elf.forced_local);
50bc7936
AM
7075 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7076
b34976b6 7077 return TRUE;
e86ce104 7078}
40b8271b 7079
a4b6fadd
AM
7080static const struct sfpr_def_parms save_res_funcs[] =
7081 {
7082 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7083 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7084 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7085 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7086 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7087 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7088 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7089 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7090 { "._savef", 14, 31, savefpr, savefpr1_tail },
7091 { "._restf", 14, 31, restfpr, restfpr1_tail },
7092 { "_savevr_", 20, 31, savevr, savevr_tail },
7093 { "_restvr_", 20, 31, restvr, restvr_tail }
7094 };
7095
e86ce104 7096/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
7097 this hook to a) provide some gcc support functions, and b) transfer
7098 dynamic linking information gathered so far on function code symbol
7099 entries, to their corresponding function descriptor symbol entries. */
deb0e272 7100
b34976b6 7101static bfd_boolean
4ce794b7
AM
7102ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7103 struct bfd_link_info *info)
e86ce104
AM
7104{
7105 struct ppc_link_hash_table *htab;
7106
7107 htab = ppc_hash_table (info);
4dfe6ac6
NC
7108 if (htab == NULL)
7109 return FALSE;
7110
b32547cd
AM
7111 /* Provide any missing _save* and _rest* functions. */
7112 if (htab->sfpr != NULL)
7113 {
7114 unsigned int i;
7115
7116 htab->sfpr->size = 0;
7117 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7118 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7119 return FALSE;
7120 if (htab->sfpr->size == 0)
7121 htab->sfpr->flags |= SEC_EXCLUDE;
7122 }
7123
7124 if (bfd_link_relocatable (info))
7125 return TRUE;
7126
7127 if (htab->elf.hgot != NULL)
dba6fa9b
AM
7128 {
7129 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7130 /* Make .TOC. defined so as to prevent it being made dynamic.
7131 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
7132 if (!htab->elf.hgot->def_regular
7133 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7134 {
7135 htab->elf.hgot->root.type = bfd_link_hash_defined;
7136 htab->elf.hgot->root.u.def.value = 0;
7137 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7138 htab->elf.hgot->def_regular = 1;
7139 htab->elf.hgot->root.linker_def = 1;
7140 }
dba6fa9b 7141 htab->elf.hgot->type = STT_OBJECT;
dba6fa9b
AM
7142 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7143 | STV_HIDDEN);
7144 }
c66bb0ee 7145
8c5b4e52
AM
7146 if (htab->need_func_desc_adj)
7147 {
7148 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7149 htab->need_func_desc_adj = 0;
7150 }
805fc799 7151
b34976b6 7152 return TRUE;
e86ce104
AM
7153}
7154
8a2058b5
AM
7155/* Return true if we have dynamic relocs against H that apply to
7156 read-only sections. */
a345bc8d
AM
7157
7158static bfd_boolean
7159readonly_dynrelocs (struct elf_link_hash_entry *h)
7160{
7161 struct ppc_link_hash_entry *eh;
7162 struct elf_dyn_relocs *p;
7163
7164 eh = (struct ppc_link_hash_entry *) h;
7165 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7166 {
7167 asection *s = p->sec->output_section;
7168
7169 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7170 return TRUE;
7171 }
7172 return FALSE;
7173}
7174
d311bc8b
AM
7175/* Return true if we have dynamic relocs against H or any of its weak
7176 aliases, that apply to read-only sections. */
7177
7178static bfd_boolean
7179alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7180{
7181 struct ppc_link_hash_entry *eh;
7182
7183 eh = (struct ppc_link_hash_entry *) h;
7184 do
7185 {
7186 if (readonly_dynrelocs (&eh->elf))
7187 return TRUE;
7188 eh = eh->weakref;
7189 } while (eh != NULL && &eh->elf != h);
7190
7191 return FALSE;
7192}
8a2058b5 7193
8a9e8e72
AM
7194/* Return whether EH has pc-relative dynamic relocs. */
7195
7196static bfd_boolean
7197pc_dynrelocs (struct ppc_link_hash_entry *eh)
7198{
7199 struct elf_dyn_relocs *p;
7200
7201 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7202 if (p->pc_count != 0)
7203 return TRUE;
7204 return FALSE;
7205}
7206
8a2058b5
AM
7207/* Return true if a global entry stub will be created for H. Valid
7208 for ELFv2 before plt entries have been allocated. */
7209
7210static bfd_boolean
7211global_entry_stub (struct elf_link_hash_entry *h)
7212{
7213 struct plt_entry *pent;
7214
7215 if (!h->pointer_equality_needed
7216 || h->def_regular)
7217 return FALSE;
7218
7219 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7220 if (pent->plt.refcount > 0
7221 && pent->addend == 0)
7222 return TRUE;
7223
7224 return FALSE;
7225}
7226
e86ce104
AM
7227/* Adjust a symbol defined by a dynamic object and referenced by a
7228 regular object. The current definition is in some section of the
7229 dynamic object, but we're not including those sections. We have to
7230 change the definition to something the rest of the link can
7231 understand. */
7232
b34976b6 7233static bfd_boolean
4ce794b7
AM
7234ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7235 struct elf_link_hash_entry *h)
e86ce104
AM
7236{
7237 struct ppc_link_hash_table *htab;
e86ce104 7238 asection *s;
e86ce104
AM
7239
7240 htab = ppc_hash_table (info);
4dfe6ac6
NC
7241 if (htab == NULL)
7242 return FALSE;
e86ce104
AM
7243
7244 /* Deal with function syms. */
7245 if (h->type == STT_FUNC
e054468f 7246 || h->type == STT_GNU_IFUNC
f5385ebf 7247 || h->needs_plt)
e86ce104
AM
7248 {
7249 /* Clear procedure linkage table information for any symbol that
7250 won't need a .plt entry. */
411e1bfb
AM
7251 struct plt_entry *ent;
7252 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7253 if (ent->plt.refcount > 0)
7254 break;
8387904d 7255 if (ent == NULL
e054468f
AM
7256 || (h->type != STT_GNU_IFUNC
7257 && (SYMBOL_CALLS_LOCAL (info, h)
7258 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
a4b6fadd
AM
7259 && h->root.type == bfd_link_hash_undefweak)))
7260 || ((struct ppc_link_hash_entry *) h)->save_res)
40b8271b 7261 {
411e1bfb 7262 h->plt.plist = NULL;
f5385ebf 7263 h->needs_plt = 0;
d1eca1e4 7264 h->pointer_equality_needed = 0;
40b8271b 7265 }
8a2058b5 7266 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 7267 {
d1eca1e4
AM
7268 /* Taking a function's address in a read/write section
7269 doesn't require us to define the function symbol in the
7270 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
7271 be used instead. The reason we prefer a few more dynamic
7272 relocs is that calling via a global entry stub costs a
7273 few more instructions, and pointer_equality_needed causes
7274 extra work in ld.so when resolving these symbols. */
7275 if (global_entry_stub (h)
d311bc8b 7276 && !alias_readonly_dynrelocs (h))
d1eca1e4
AM
7277 {
7278 h->pointer_equality_needed = 0;
8a2058b5
AM
7279 /* After adjust_dynamic_symbol, non_got_ref set in
7280 the non-pic case means that dyn_relocs for this
7281 symbol should be discarded. */
d1eca1e4
AM
7282 h->non_got_ref = 0;
7283 }
7284
a345bc8d
AM
7285 /* If making a plt entry, then we don't need copy relocs. */
7286 return TRUE;
7287 }
5bd4f169 7288 }
bbd7ec4a 7289 else
411e1bfb 7290 h->plt.plist = NULL;
5bd4f169
AM
7291
7292 /* If this is a weak symbol, and there is a real definition, the
7293 processor independent code will have arranged for us to see the
7294 real definition first, and we can just use the same value. */
f6e332e6 7295 if (h->u.weakdef != NULL)
5bd4f169 7296 {
f6e332e6
AM
7297 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7298 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7299 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7300 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 7301 if (ELIMINATE_COPY_RELOCS)
f6e332e6 7302 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 7303 return TRUE;
5bd4f169
AM
7304 }
7305
5bd4f169
AM
7306 /* If we are creating a shared library, we must presume that the
7307 only references to the symbol are via the global offset table.
7308 For such cases we need not do anything here; the relocations will
7309 be handled correctly by relocate_section. */
0e1862bb 7310 if (bfd_link_pic (info))
b34976b6 7311 return TRUE;
5bd4f169 7312
65f38f15
AM
7313 /* If there are no references to this symbol that do not use the
7314 GOT, we don't need to generate a copy reloc. */
f5385ebf 7315 if (!h->non_got_ref)
b34976b6 7316 return TRUE;
65f38f15 7317
b186458a 7318 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 7319 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 7320
d93d1c80
AM
7321 /* If -z nocopyreloc was given, don't generate them either. */
7322 || info->nocopyreloc
a127494f 7323
d93d1c80
AM
7324 /* If we didn't find any dynamic relocs in read-only sections, then
7325 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
d311bc8b 7326 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
65f38f15 7327
d93d1c80
AM
7328 /* Protected variables do not work with .dynbss. The copy in
7329 .dynbss won't be used by the shared library with the protected
7330 definition for the variable. Text relocations are preferable
7331 to an incorrect program. */
7332 || h->protected_def)
a127494f
AM
7333 {
7334 h->non_got_ref = 0;
7335 return TRUE;
7336 }
7337
5d35169e 7338 if (h->plt.plist != NULL)
97b639ba
AM
7339 {
7340 /* We should never get here, but unfortunately there are versions
7341 of gcc out there that improperly (for this ABI) put initialized
7342 function pointers, vtable refs and suchlike in read-only
7343 sections. Allow them to proceed, but warn that this might
7344 break at runtime. */
25f53a85 7345 info->callbacks->einfo
bc30df16 7346 (_("%P: copy reloc against `%T' requires lazy plt linking; "
25f53a85 7347 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
7348 h->root.root.string);
7349 }
5d35169e
AM
7350
7351 /* This is a reference to a symbol defined by a dynamic object which
7352 is not a function. */
7353
5bd4f169
AM
7354 /* We must allocate the symbol in our .dynbss section, which will
7355 become part of the .bss section of the executable. There will be
7356 an entry for this symbol in the .dynsym section. The dynamic
7357 object will contain position independent code, so all references
7358 from the dynamic object to this symbol will go through the global
7359 offset table. The dynamic linker will use the .dynsym entry to
7360 determine the address it must put in the global offset table, so
7361 both the dynamic object and the regular object will refer to the
7362 same memory location for the variable. */
5bd4f169 7363
04c9666a
AM
7364 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7365 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
7366 runtime process image. We need to remember the offset into the
7367 .rela.bss section we are going to use. */
1d7e9d18 7368 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 7369 {
9d19e4fd 7370 htab->elf.srelbss->size += sizeof (Elf64_External_Rela);
f5385ebf 7371 h->needs_copy = 1;
5bd4f169
AM
7372 }
7373
9d19e4fd 7374 s = htab->elf.sdynbss;
5bd4f169 7375
6cabe1ea 7376 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
7377}
7378
e86ce104
AM
7379/* If given a function descriptor symbol, hide both the function code
7380 sym and the descriptor. */
7381static void
4ce794b7
AM
7382ppc64_elf_hide_symbol (struct bfd_link_info *info,
7383 struct elf_link_hash_entry *h,
7384 bfd_boolean force_local)
e86ce104 7385{
34814b9f 7386 struct ppc_link_hash_entry *eh;
e86ce104
AM
7387 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7388
34814b9f
AM
7389 eh = (struct ppc_link_hash_entry *) h;
7390 if (eh->is_func_descriptor)
e86ce104 7391 {
34814b9f 7392 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 7393
721956f4 7394 if (fh == NULL)
d1329ca3
AM
7395 {
7396 const char *p, *q;
b8ac2841 7397 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
7398 char save;
7399
7400 /* We aren't supposed to use alloca in BFD because on
7401 systems which do not have alloca the version in libiberty
7402 calls xmalloc, which might cause the program to crash
7403 when it runs out of memory. This function doesn't have a
7404 return status, so there's no way to gracefully return an
7405 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
7406 accessed; It's either a string in an ELF string table,
7407 or allocated in an objalloc structure. */
d1329ca3 7408
34814b9f 7409 p = eh->elf.root.root.string - 1;
d1329ca3
AM
7410 save = *p;
7411 *(char *) p = '.';
34814b9f 7412 fh = (struct ppc_link_hash_entry *)
b8ac2841 7413 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7414 *(char *) p = save;
7415
7416 /* Unfortunately, if it so happens that the string we were
7417 looking for was allocated immediately before this string,
7418 then we overwrote the string terminator. That's the only
7419 reason the lookup should fail. */
7420 if (fh == NULL)
7421 {
34814b9f
AM
7422 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7423 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7424 --q, --p;
34814b9f
AM
7425 if (q < eh->elf.root.root.string && *p == '.')
7426 fh = (struct ppc_link_hash_entry *)
b8ac2841 7427 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7428 }
7429 if (fh != NULL)
7430 {
34814b9f
AM
7431 eh->oh = fh;
7432 fh->oh = eh;
d1329ca3
AM
7433 }
7434 }
e86ce104 7435 if (fh != NULL)
34814b9f 7436 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7437 }
7438}
7439
411e1bfb 7440static bfd_boolean
8843416a
AM
7441get_sym_h (struct elf_link_hash_entry **hp,
7442 Elf_Internal_Sym **symp,
7443 asection **symsecp,
f961d9dd 7444 unsigned char **tls_maskp,
8843416a
AM
7445 Elf_Internal_Sym **locsymsp,
7446 unsigned long r_symndx,
7447 bfd *ibfd)
411e1bfb 7448{
0ffa91dd 7449 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7450
7451 if (r_symndx >= symtab_hdr->sh_info)
7452 {
7453 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7454 struct elf_link_hash_entry *h;
7455
7456 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7457 h = elf_follow_link (h);
411e1bfb
AM
7458
7459 if (hp != NULL)
7460 *hp = h;
7461
7462 if (symp != NULL)
7463 *symp = NULL;
7464
7465 if (symsecp != NULL)
7466 {
7467 asection *symsec = NULL;
7468 if (h->root.type == bfd_link_hash_defined
7469 || h->root.type == bfd_link_hash_defweak)
7470 symsec = h->root.u.def.section;
7471 *symsecp = symsec;
7472 }
7473
e7b938ca 7474 if (tls_maskp != NULL)
411e1bfb
AM
7475 {
7476 struct ppc_link_hash_entry *eh;
7477
7478 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7479 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7480 }
7481 }
7482 else
7483 {
7484 Elf_Internal_Sym *sym;
7485 Elf_Internal_Sym *locsyms = *locsymsp;
7486
7487 if (locsyms == NULL)
7488 {
7489 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7490 if (locsyms == NULL)
7491 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7492 symtab_hdr->sh_info,
7493 0, NULL, NULL, NULL);
7494 if (locsyms == NULL)
7495 return FALSE;
7496 *locsymsp = locsyms;
7497 }
7498 sym = locsyms + r_symndx;
7499
7500 if (hp != NULL)
7501 *hp = NULL;
7502
7503 if (symp != NULL)
7504 *symp = sym;
7505
7506 if (symsecp != NULL)
cb33740c 7507 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7508
e7b938ca 7509 if (tls_maskp != NULL)
411e1bfb
AM
7510 {
7511 struct got_entry **lgot_ents;
f961d9dd 7512 unsigned char *tls_mask;
411e1bfb 7513
e7b938ca 7514 tls_mask = NULL;
411e1bfb
AM
7515 lgot_ents = elf_local_got_ents (ibfd);
7516 if (lgot_ents != NULL)
7517 {
e054468f
AM
7518 struct plt_entry **local_plt = (struct plt_entry **)
7519 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7520 unsigned char *lgot_masks = (unsigned char *)
e054468f 7521 (local_plt + symtab_hdr->sh_info);
e7b938ca 7522 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7523 }
e7b938ca 7524 *tls_maskp = tls_mask;
411e1bfb
AM
7525 }
7526 }
7527 return TRUE;
7528}
7529
e7b938ca 7530/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7531 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7532 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7533
7534static int
f961d9dd 7535get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7536 unsigned long *toc_symndx,
7537 bfd_vma *toc_addend,
0d4792f7 7538 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7539 const Elf_Internal_Rela *rel,
7540 bfd *ibfd)
411e1bfb
AM
7541{
7542 unsigned long r_symndx;
0d4792f7 7543 int next_r;
411e1bfb
AM
7544 struct elf_link_hash_entry *h;
7545 Elf_Internal_Sym *sym;
7546 asection *sec;
7547 bfd_vma off;
7548
7549 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7550 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7551 return 0;
411e1bfb 7552
e7b938ca 7553 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7554 || sec == NULL
6bee8834 7555 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7556 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7557 return 1;
411e1bfb
AM
7558
7559 /* Look inside a TOC section too. */
7560 if (h != NULL)
7561 {
7562 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7563 off = h->root.u.def.value;
7564 }
7565 else
7566 off = sym->st_value;
7567 off += rel->r_addend;
7568 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7569 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7570 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7571 if (toc_symndx != NULL)
7572 *toc_symndx = r_symndx;
3a71aa26
AM
7573 if (toc_addend != NULL)
7574 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7575 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7576 return 0;
854b41e7 7577 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7578 && (next_r == -1 || next_r == -2))
7579 return 1 - next_r;
951fd09b 7580 return 1;
411e1bfb
AM
7581}
7582
3b421ab3
AM
7583/* Find (or create) an entry in the tocsave hash table. */
7584
7585static struct tocsave_entry *
7586tocsave_find (struct ppc_link_hash_table *htab,
7587 enum insert_option insert,
7588 Elf_Internal_Sym **local_syms,
7589 const Elf_Internal_Rela *irela,
7590 bfd *ibfd)
7591{
7592 unsigned long r_indx;
7593 struct elf_link_hash_entry *h;
7594 Elf_Internal_Sym *sym;
7595 struct tocsave_entry ent, *p;
7596 hashval_t hash;
7597 struct tocsave_entry **slot;
7598
7599 r_indx = ELF64_R_SYM (irela->r_info);
7600 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7601 return NULL;
7602 if (ent.sec == NULL || ent.sec->output_section == NULL)
7603 {
4eca0228 7604 _bfd_error_handler
3b421ab3
AM
7605 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7606 return NULL;
7607 }
7608
7609 if (h != NULL)
7610 ent.offset = h->root.u.def.value;
7611 else
7612 ent.offset = sym->st_value;
7613 ent.offset += irela->r_addend;
7614
7615 hash = tocsave_htab_hash (&ent);
7616 slot = ((struct tocsave_entry **)
7617 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7618 if (slot == NULL)
7619 return NULL;
7620
7621 if (*slot == NULL)
7622 {
7623 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7624 if (p == NULL)
7625 return NULL;
7626 *p = ent;
7627 *slot = p;
7628 }
7629 return *slot;
7630}
7631
754021d0 7632/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7633 code for the old ABI, these will already have been done. */
754021d0
AM
7634
7635static bfd_boolean
7636adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7637{
7638 struct ppc_link_hash_entry *eh;
7639 asection *sym_sec;
74f0fb50 7640 struct _opd_sec_data *opd;
754021d0
AM
7641
7642 if (h->root.type == bfd_link_hash_indirect)
7643 return TRUE;
7644
754021d0
AM
7645 if (h->root.type != bfd_link_hash_defined
7646 && h->root.type != bfd_link_hash_defweak)
7647 return TRUE;
7648
7649 eh = (struct ppc_link_hash_entry *) h;
7650 if (eh->adjust_done)
7651 return TRUE;
7652
7653 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7654 opd = get_opd_info (sym_sec);
7655 if (opd != NULL && opd->adjust != NULL)
754021d0 7656 {
51aecdc5 7657 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7658 if (adjust == -1)
7659 {
7660 /* This entry has been deleted. */
b3fac117 7661 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7662 if (dsec == NULL)
7663 {
7664 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7665 if (discarded_section (dsec))
81688140 7666 {
b3fac117 7667 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7668 break;
7669 }
7670 }
4025353c 7671 eh->elf.root.u.def.value = 0;
81688140 7672 eh->elf.root.u.def.section = dsec;
4025353c
AM
7673 }
7674 else
7675 eh->elf.root.u.def.value += adjust;
754021d0
AM
7676 eh->adjust_done = 1;
7677 }
7678 return TRUE;
7679}
7680
8c1d1bb8 7681/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7682 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7683 have already been determined. */
7684
7685static bfd_boolean
7686dec_dynrel_count (bfd_vma r_info,
7687 asection *sec,
7688 struct bfd_link_info *info,
7689 Elf_Internal_Sym **local_syms,
7690 struct elf_link_hash_entry *h,
19e08130 7691 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7692{
7693 enum elf_ppc64_reloc_type r_type;
19e08130 7694 asection *sym_sec = NULL;
8c1d1bb8
AM
7695
7696 /* Can this reloc be dynamic? This switch, and later tests here
7697 should be kept in sync with the code in check_relocs. */
7698 r_type = ELF64_R_TYPE (r_info);
7699 switch (r_type)
7700 {
7701 default:
7702 return TRUE;
7703
7704 case R_PPC64_TPREL16:
7705 case R_PPC64_TPREL16_LO:
7706 case R_PPC64_TPREL16_HI:
7707 case R_PPC64_TPREL16_HA:
7708 case R_PPC64_TPREL16_DS:
7709 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7710 case R_PPC64_TPREL16_HIGH:
7711 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7712 case R_PPC64_TPREL16_HIGHER:
7713 case R_PPC64_TPREL16_HIGHERA:
7714 case R_PPC64_TPREL16_HIGHEST:
7715 case R_PPC64_TPREL16_HIGHESTA:
0e1862bb 7716 if (!bfd_link_pic (info))
8c1d1bb8
AM
7717 return TRUE;
7718
7719 case R_PPC64_TPREL64:
7720 case R_PPC64_DTPMOD64:
7721 case R_PPC64_DTPREL64:
7722 case R_PPC64_ADDR64:
7723 case R_PPC64_REL30:
7724 case R_PPC64_REL32:
7725 case R_PPC64_REL64:
7726 case R_PPC64_ADDR14:
7727 case R_PPC64_ADDR14_BRNTAKEN:
7728 case R_PPC64_ADDR14_BRTAKEN:
7729 case R_PPC64_ADDR16:
7730 case R_PPC64_ADDR16_DS:
7731 case R_PPC64_ADDR16_HA:
7732 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7733 case R_PPC64_ADDR16_HIGH:
7734 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7735 case R_PPC64_ADDR16_HIGHER:
7736 case R_PPC64_ADDR16_HIGHERA:
7737 case R_PPC64_ADDR16_HIGHEST:
7738 case R_PPC64_ADDR16_HIGHESTA:
7739 case R_PPC64_ADDR16_LO:
7740 case R_PPC64_ADDR16_LO_DS:
7741 case R_PPC64_ADDR24:
7742 case R_PPC64_ADDR32:
7743 case R_PPC64_UADDR16:
7744 case R_PPC64_UADDR32:
7745 case R_PPC64_UADDR64:
7746 case R_PPC64_TOC:
7747 break;
7748 }
7749
7750 if (local_syms != NULL)
7751 {
7752 unsigned long r_symndx;
8c1d1bb8
AM
7753 bfd *ibfd = sec->owner;
7754
7755 r_symndx = ELF64_R_SYM (r_info);
7756 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7757 return FALSE;
7758 }
7759
0e1862bb 7760 if ((bfd_link_pic (info)
1d483afe 7761 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7762 || (h != NULL
198f1157 7763 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7764 || h->root.type == bfd_link_hash_defweak
7765 || !h->def_regular))))
7766 || (ELIMINATE_COPY_RELOCS
0e1862bb 7767 && !bfd_link_pic (info)
8c1d1bb8
AM
7768 && h != NULL
7769 && (h->root.type == bfd_link_hash_defweak
7770 || !h->def_regular)))
7771 ;
7772 else
7773 return TRUE;
7774
7775 if (h != NULL)
6edfbbad 7776 {
19e08130
AM
7777 struct elf_dyn_relocs *p;
7778 struct elf_dyn_relocs **pp;
7779 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7780
7781 /* elf_gc_sweep may have already removed all dyn relocs associated
7782 with local syms for a given section. Also, symbol flags are
7783 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7784 report a dynreloc miscount. */
7785 if (*pp == NULL && info->gc_sections)
7786 return TRUE;
7787
7788 while ((p = *pp) != NULL)
60124e18 7789 {
19e08130
AM
7790 if (p->sec == sec)
7791 {
7792 if (!must_be_dyn_reloc (info, r_type))
7793 p->pc_count -= 1;
7794 p->count -= 1;
7795 if (p->count == 0)
7796 *pp = p->next;
7797 return TRUE;
7798 }
7799 pp = &p->next;
60124e18 7800 }
6edfbbad 7801 }
19e08130
AM
7802 else
7803 {
7804 struct ppc_dyn_relocs *p;
7805 struct ppc_dyn_relocs **pp;
7806 void *vpp;
7807 bfd_boolean is_ifunc;
8c1d1bb8 7808
19e08130
AM
7809 if (local_syms == NULL)
7810 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7811 if (sym_sec == NULL)
7812 sym_sec = sec;
c57da1a7 7813
19e08130
AM
7814 vpp = &elf_section_data (sym_sec)->local_dynrel;
7815 pp = (struct ppc_dyn_relocs **) vpp;
7816
7817 if (*pp == NULL && info->gc_sections)
7818 return TRUE;
7819
7820 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7821 while ((p = *pp) != NULL)
8c1d1bb8 7822 {
19e08130
AM
7823 if (p->sec == sec && p->ifunc == is_ifunc)
7824 {
7825 p->count -= 1;
7826 if (p->count == 0)
7827 *pp = p->next;
7828 return TRUE;
7829 }
7830 pp = &p->next;
8c1d1bb8 7831 }
8c1d1bb8
AM
7832 }
7833
695344c0 7834 /* xgettext:c-format */
8de848d8 7835 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7836 sec->owner, sec);
8c1d1bb8
AM
7837 bfd_set_error (bfd_error_bad_value);
7838 return FALSE;
7839}
7840
754021d0
AM
7841/* Remove unused Official Procedure Descriptor entries. Currently we
7842 only remove those associated with functions in discarded link-once
7843 sections, or weakly defined functions that have been overridden. It
7844 would be possible to remove many more entries for statically linked
7845 applications. */
7846
b34976b6 7847bfd_boolean
e7d1c40c 7848ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7849{
7850 bfd *ibfd;
754021d0 7851 bfd_boolean some_edited = FALSE;
3f764659 7852 asection *need_pad = NULL;
e7d1c40c
AM
7853 struct ppc_link_hash_table *htab;
7854
7855 htab = ppc_hash_table (info);
7856 if (htab == NULL)
7857 return FALSE;
1e2f5b6e 7858
c72f2fb2 7859 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7860 {
7861 asection *sec;
7862 Elf_Internal_Rela *relstart, *rel, *relend;
7863 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7864 Elf_Internal_Sym *local_syms;
74f0fb50 7865 struct _opd_sec_data *opd;
51aecdc5 7866 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7867 bfd_size_type cnt_16b = 0;
1e2f5b6e 7868
854b41e7
AM
7869 if (!is_ppc64_elf (ibfd))
7870 continue;
7871
1e2f5b6e 7872 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7873 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7874 continue;
7875
dbaa2011 7876 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7877 continue;
7878
1e2f5b6e
AM
7879 if (sec->output_section == bfd_abs_section_ptr)
7880 continue;
7881
7882 /* Look through the section relocs. */
7883 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7884 continue;
7885
6cdc0ccc 7886 local_syms = NULL;
0ffa91dd 7887 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7888
7889 /* Read the relocations. */
4ce794b7 7890 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7891 info->keep_memory);
1e2f5b6e 7892 if (relstart == NULL)
b34976b6 7893 return FALSE;
1e2f5b6e
AM
7894
7895 /* First run through the relocs to check they are sane, and to
7896 determine whether we need to edit this opd section. */
b34976b6 7897 need_edit = FALSE;
51aecdc5 7898 broken = FALSE;
3f764659 7899 need_pad = sec;
1e2f5b6e 7900 relend = relstart + sec->reloc_count;
50bc7936 7901 for (rel = relstart; rel < relend; )
1e2f5b6e 7902 {
04c9666a 7903 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7904 unsigned long r_symndx;
7905 asection *sym_sec;
7906 struct elf_link_hash_entry *h;
7907 Elf_Internal_Sym *sym;
51aecdc5 7908 bfd_vma offset;
1e2f5b6e 7909
51aecdc5 7910 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7911 only interested in the reloc pointing to a function entry
7912 point. */
51aecdc5
AM
7913 offset = rel->r_offset;
7914 if (rel + 1 == relend
7915 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7916 {
7917 /* If someone messes with .opd alignment then after a
7918 "ld -r" we might have padding in the middle of .opd.
7919 Also, there's nothing to prevent someone putting
7920 something silly in .opd with the assembler. No .opd
b34976b6 7921 optimization for them! */
3f764659 7922 broken_opd:
4eca0228 7923 _bfd_error_handler
d003868e 7924 (_("%B: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7925 broken = TRUE;
1e2f5b6e
AM
7926 break;
7927 }
7928
50bc7936
AM
7929 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7930 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7931 {
4eca0228 7932 _bfd_error_handler
695344c0 7933 /* xgettext:c-format */
d003868e
AM
7934 (_("%B: unexpected reloc type %u in .opd section"),
7935 ibfd, r_type);
51aecdc5 7936 broken = TRUE;
50bc7936
AM
7937 break;
7938 }
7939
1e2f5b6e 7940 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7941 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7942 r_symndx, ibfd))
50bc7936 7943 goto error_ret;
1e2f5b6e
AM
7944
7945 if (sym_sec == NULL || sym_sec->owner == NULL)
7946 {
411e1bfb
AM
7947 const char *sym_name;
7948 if (h != NULL)
7949 sym_name = h->root.root.string;
7950 else
26c61ae5
L
7951 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7952 sym_sec);
411e1bfb 7953
4eca0228 7954 _bfd_error_handler
695344c0 7955 /* xgettext:c-format */
d003868e
AM
7956 (_("%B: undefined sym `%s' in .opd section"),
7957 ibfd, sym_name);
51aecdc5 7958 broken = TRUE;
1e2f5b6e
AM
7959 break;
7960 }
7961
51020317
AM
7962 /* opd entries are always for functions defined in the
7963 current input bfd. If the symbol isn't defined in the
7964 input bfd, then we won't be using the function in this
7965 bfd; It must be defined in a linkonce section in another
7966 bfd, or is weak. It's also possible that we are
7967 discarding the function due to a linker script /DISCARD/,
7968 which we test for via the output_section. */
7969 if (sym_sec->owner != ibfd
7970 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7971 need_edit = TRUE;
1e2f5b6e 7972
50bc7936 7973 rel += 2;
51aecdc5
AM
7974 if (rel + 1 == relend
7975 || (rel + 2 < relend
7976 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7977 ++rel;
7978
7979 if (rel == relend)
3f764659
JJ
7980 {
7981 if (sec->size == offset + 24)
7982 {
7983 need_pad = NULL;
7984 break;
7985 }
51aecdc5 7986 if (sec->size == offset + 16)
3f764659
JJ
7987 {
7988 cnt_16b++;
7989 break;
7990 }
7991 goto broken_opd;
7992 }
3f764659
JJ
7993 else if (rel + 1 < relend
7994 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7995 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7996 {
51aecdc5
AM
7997 if (rel[0].r_offset == offset + 16)
7998 cnt_16b++;
7999 else if (rel[0].r_offset != offset + 24)
8000 goto broken_opd;
3f764659
JJ
8001 }
8002 else
8003 goto broken_opd;
1e2f5b6e
AM
8004 }
8005
e7d1c40c 8006 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 8007
51aecdc5 8008 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
8009 {
8010 Elf_Internal_Rela *write_rel;
d4730f92 8011 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 8012 bfd_byte *rptr, *wptr;
983bddc8 8013 bfd_byte *new_contents;
74f0fb50
AM
8014 bfd_size_type amt;
8015
983bddc8 8016 new_contents = NULL;
51aecdc5 8017 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 8018 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 8019 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
8020 if (opd->adjust == NULL)
8021 return FALSE;
8022 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
8023
8024 /* This seems a waste of time as input .opd sections are all
8025 zeros as generated by gcc, but I suppose there's no reason
8026 this will always be so. We might start putting something in
8027 the third word of .opd entries. */
8028 if ((sec->flags & SEC_IN_MEMORY) == 0)
8029 {
eea6121a
AM
8030 bfd_byte *loc;
8031 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 8032 {
eea6121a
AM
8033 if (loc != NULL)
8034 free (loc);
50bc7936 8035 error_ret:
6cdc0ccc
AM
8036 if (local_syms != NULL
8037 && symtab_hdr->contents != (unsigned char *) local_syms)
8038 free (local_syms);
6cdc0ccc
AM
8039 if (elf_section_data (sec)->relocs != relstart)
8040 free (relstart);
b34976b6 8041 return FALSE;
6cdc0ccc 8042 }
1e2f5b6e
AM
8043 sec->contents = loc;
8044 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8045 }
8046
8047 elf_section_data (sec)->relocs = relstart;
8048
3f764659 8049 new_contents = sec->contents;
3f764659
JJ
8050 if (add_aux_fields)
8051 {
8052 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8053 if (new_contents == NULL)
8054 return FALSE;
51aecdc5 8055 need_pad = NULL;
3f764659 8056 }
b4f4e59f
AM
8057 wptr = new_contents;
8058 rptr = sec->contents;
1e2f5b6e 8059 write_rel = relstart;
51aecdc5 8060 for (rel = relstart; rel < relend; )
1e2f5b6e 8061 {
50bc7936
AM
8062 unsigned long r_symndx;
8063 asection *sym_sec;
8064 struct elf_link_hash_entry *h;
51aecdc5 8065 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 8066 Elf_Internal_Sym *sym;
51aecdc5
AM
8067 long opd_ent_size;
8068 Elf_Internal_Rela *next_rel;
8069 bfd_boolean skip;
50bc7936
AM
8070
8071 r_symndx = ELF64_R_SYM (rel->r_info);
8072 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 8073 r_symndx, ibfd))
50bc7936
AM
8074 goto error_ret;
8075
51aecdc5
AM
8076 next_rel = rel + 2;
8077 if (next_rel + 1 == relend
8078 || (next_rel + 2 < relend
8079 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8080 ++next_rel;
8081
8082 /* See if the .opd entry is full 24 byte or
8083 16 byte (with fd_aux entry overlapped with next
8084 fd_func). */
8085 opd_ent_size = 24;
8086 if (next_rel == relend)
1e2f5b6e 8087 {
51aecdc5 8088 if (sec->size == rel->r_offset + 16)
3f764659 8089 opd_ent_size = 16;
51aecdc5
AM
8090 }
8091 else if (next_rel->r_offset == rel->r_offset + 16)
8092 opd_ent_size = 16;
3f764659 8093
51aecdc5
AM
8094 if (h != NULL
8095 && h->root.root.string[0] == '.')
8096 {
8c5b4e52
AM
8097 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8098 if (fdh != NULL)
8099 {
8100 fdh = ppc_follow_link (fdh);
8101 if (fdh->elf.root.type != bfd_link_hash_defined
8102 && fdh->elf.root.type != bfd_link_hash_defweak)
8103 fdh = NULL;
8104 }
51aecdc5 8105 }
1e2f5b6e 8106
51aecdc5
AM
8107 skip = (sym_sec->owner != ibfd
8108 || sym_sec->output_section == bfd_abs_section_ptr);
8109 if (skip)
8110 {
8111 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 8112 {
51aecdc5
AM
8113 /* Arrange for the function descriptor sym
8114 to be dropped. */
8115 fdh->elf.root.u.def.value = 0;
8116 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 8117 }
51aecdc5 8118 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 8119
0e1862bb 8120 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
8121 rel = next_rel;
8122 else
8123 while (1)
8124 {
8125 if (!dec_dynrel_count (rel->r_info, sec, info,
8126 NULL, h, sym))
8127 goto error_ret;
754021d0 8128
51aecdc5
AM
8129 if (++rel == next_rel)
8130 break;
1e2f5b6e 8131
51aecdc5
AM
8132 r_symndx = ELF64_R_SYM (rel->r_info);
8133 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8134 r_symndx, ibfd))
8135 goto error_ret;
8136 }
50bc7936
AM
8137 }
8138 else
1e2f5b6e 8139 {
51aecdc5
AM
8140 /* We'll be keeping this opd entry. */
8141 long adjust;
8142
8143 if (fdh != NULL)
8144 {
8145 /* Redefine the function descriptor symbol to
8146 this location in the opd section. It is
8147 necessary to update the value here rather
8148 than using an array of adjustments as we do
8149 for local symbols, because various places
8150 in the generic ELF code use the value
8151 stored in u.def.value. */
8152 fdh->elf.root.u.def.value = wptr - new_contents;
8153 fdh->adjust_done = 1;
8154 }
8155
8156 /* Local syms are a bit tricky. We could
8157 tweak them as they can be cached, but
8158 we'd need to look through the local syms
8159 for the function descriptor sym which we
8160 don't have at the moment. So keep an
8161 array of adjustments. */
8162 adjust = (wptr - new_contents) - (rptr - sec->contents);
8163 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8164
8165 if (wptr != rptr)
8166 memcpy (wptr, rptr, opd_ent_size);
8167 wptr += opd_ent_size;
8168 if (add_aux_fields && opd_ent_size == 16)
8169 {
8170 memset (wptr, '\0', 8);
8171 wptr += 8;
8172 }
8173
50bc7936 8174 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
8175 new opd entries. */
8176 for ( ; rel != next_rel; ++rel)
8177 {
8178 rel->r_offset += adjust;
8179 if (write_rel != rel)
8180 memcpy (write_rel, rel, sizeof (*rel));
8181 ++write_rel;
8182 }
1e2f5b6e 8183 }
51aecdc5
AM
8184
8185 rptr += opd_ent_size;
1e2f5b6e
AM
8186 }
8187
3f764659 8188 sec->size = wptr - new_contents;
1e2f5b6e 8189 sec->reloc_count = write_rel - relstart;
3f764659
JJ
8190 if (add_aux_fields)
8191 {
8192 free (sec->contents);
8193 sec->contents = new_contents;
8194 }
8195
05bf9422 8196 /* Fudge the header size too, as this is used later in
cdcf6e38 8197 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
8198 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8199 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 8200 some_edited = TRUE;
1e2f5b6e 8201 }
6cdc0ccc 8202 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 8203 free (relstart);
6cdc0ccc 8204
411e1bfb
AM
8205 if (local_syms != NULL
8206 && symtab_hdr->contents != (unsigned char *) local_syms)
8207 {
8208 if (!info->keep_memory)
8209 free (local_syms);
8210 else
8211 symtab_hdr->contents = (unsigned char *) local_syms;
8212 }
8213 }
8214
754021d0
AM
8215 if (some_edited)
8216 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8217
3f764659
JJ
8218 /* If we are doing a final link and the last .opd entry is just 16 byte
8219 long, add a 8 byte padding after it. */
0e1862bb 8220 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
8221 {
8222 bfd_byte *p;
8223
8224 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8225 {
8226 BFD_ASSERT (need_pad->size > 0);
8227
8228 p = bfd_malloc (need_pad->size + 8);
8229 if (p == NULL)
8230 return FALSE;
699733f6 8231
3f764659
JJ
8232 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8233 p, 0, need_pad->size))
8234 return FALSE;
8235
8236 need_pad->contents = p;
8237 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8238 }
8239 else
8240 {
8241 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8242 if (p == NULL)
8243 return FALSE;
8244
8245 need_pad->contents = p;
8246 }
8247
8248 memset (need_pad->contents + need_pad->size, 0, 8);
8249 need_pad->size += 8;
8250 }
8251
411e1bfb
AM
8252 return TRUE;
8253}
8254
e1918d23 8255/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 8256
e1918d23 8257asection *
e7d1c40c 8258ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 8259{
411e1bfb
AM
8260 struct ppc_link_hash_table *htab;
8261
411e1bfb 8262 htab = ppc_hash_table (info);
4dfe6ac6
NC
8263 if (htab == NULL)
8264 return NULL;
8265
ee67d69a
AM
8266 if (abiversion (info->output_bfd) == 1)
8267 htab->opd_abi = 1;
8268
e7d1c40c 8269 if (htab->params->no_multi_toc)
33c0ec9d
AM
8270 htab->do_multi_toc = 0;
8271 else if (!htab->do_multi_toc)
e7d1c40c 8272 htab->params->no_multi_toc = 1;
33c0ec9d 8273
3a71aa26
AM
8274 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8275 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8276 FALSE, FALSE, TRUE));
a7f2871e
AM
8277 /* Move dynamic linking info to the function descriptor sym. */
8278 if (htab->tls_get_addr != NULL)
8279 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
8280 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8281 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8282 FALSE, FALSE, TRUE));
7c9cf415 8283 if (htab->params->tls_get_addr_opt)
a7f2871e
AM
8284 {
8285 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8286
8287 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8288 FALSE, FALSE, TRUE);
8289 if (opt != NULL)
8290 func_desc_adjust (opt, info);
8291 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8292 FALSE, FALSE, TRUE);
8293 if (opt_fd != NULL
8294 && (opt_fd->root.type == bfd_link_hash_defined
8295 || opt_fd->root.type == bfd_link_hash_defweak))
8296 {
8297 /* If glibc supports an optimized __tls_get_addr call stub,
8298 signalled by the presence of __tls_get_addr_opt, and we'll
8299 be calling __tls_get_addr via a plt call stub, then
8300 make __tls_get_addr point to __tls_get_addr_opt. */
8301 tga_fd = &htab->tls_get_addr_fd->elf;
8302 if (htab->elf.dynamic_sections_created
8303 && tga_fd != NULL
8304 && (tga_fd->type == STT_FUNC
8305 || tga_fd->needs_plt)
8306 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8307 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8308 && tga_fd->root.type == bfd_link_hash_undefweak)))
8309 {
8310 struct plt_entry *ent;
8311
8312 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8313 if (ent->plt.refcount > 0)
8314 break;
8315 if (ent != NULL)
8316 {
8317 tga_fd->root.type = bfd_link_hash_indirect;
8318 tga_fd->root.u.i.link = &opt_fd->root;
8319 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
3bd43ebc 8320 opt_fd->forced_local = 0;
a7f2871e
AM
8321 if (opt_fd->dynindx != -1)
8322 {
8323 /* Use __tls_get_addr_opt in dynamic relocations. */
8324 opt_fd->dynindx = -1;
8325 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8326 opt_fd->dynstr_index);
8327 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 8328 return NULL;
a7f2871e
AM
8329 }
8330 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8331 tga = &htab->tls_get_addr->elf;
8332 if (opt != NULL && tga != NULL)
8333 {
8334 tga->root.type = bfd_link_hash_indirect;
8335 tga->root.u.i.link = &opt->root;
8336 ppc64_elf_copy_indirect_symbol (info, opt, tga);
3bd43ebc 8337 opt->forced_local = 0;
a7f2871e
AM
8338 _bfd_elf_link_hash_hide_symbol (info, opt,
8339 tga->forced_local);
8340 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8341 }
8342 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8343 htab->tls_get_addr_fd->is_func_descriptor = 1;
8344 if (htab->tls_get_addr != NULL)
8345 {
8346 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8347 htab->tls_get_addr->is_func = 1;
8348 }
8349 }
8350 }
8351 }
7c9cf415
AM
8352 else if (htab->params->tls_get_addr_opt < 0)
8353 htab->params->tls_get_addr_opt = 0;
a7f2871e 8354 }
33c0ec9d 8355 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 8356}
8387904d 8357
3a71aa26
AM
8358/* Return TRUE iff REL is a branch reloc with a global symbol matching
8359 HASH1 or HASH2. */
8387904d 8360
3a71aa26
AM
8361static bfd_boolean
8362branch_reloc_hash_match (const bfd *ibfd,
8363 const Elf_Internal_Rela *rel,
8364 const struct ppc_link_hash_entry *hash1,
8365 const struct ppc_link_hash_entry *hash2)
8366{
8367 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8368 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8369 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8370
e054468f 8371 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8372 {
3a71aa26
AM
8373 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8374 struct elf_link_hash_entry *h;
8387904d 8375
3a71aa26 8376 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8377 h = elf_follow_link (h);
3a71aa26
AM
8378 if (h == &hash1->elf || h == &hash2->elf)
8379 return TRUE;
a48ebf4d 8380 }
3a71aa26 8381 return FALSE;
951fd09b 8382}
411e1bfb 8383
951fd09b
AM
8384/* Run through all the TLS relocs looking for optimization
8385 opportunities. The linker has been hacked (see ppc64elf.em) to do
8386 a preliminary section layout so that we know the TLS segment
8387 offsets. We can't optimize earlier because some optimizations need
8388 to know the tp offset, and we need to optimize before allocating
8389 dynamic relocations. */
8390
8391bfd_boolean
33c0ec9d 8392ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8393{
8394 bfd *ibfd;
8395 asection *sec;
8396 struct ppc_link_hash_table *htab;
663a1470 8397 unsigned char *toc_ref;
102890f0 8398 int pass;
951fd09b 8399
3cbc1e5e 8400 if (!bfd_link_executable (info))
411e1bfb
AM
8401 return TRUE;
8402
951fd09b 8403 htab = ppc_hash_table (info);
4dfe6ac6
NC
8404 if (htab == NULL)
8405 return FALSE;
8406
663a1470
AM
8407 /* Make two passes over the relocs. On the first pass, mark toc
8408 entries involved with tls relocs, and check that tls relocs
8409 involved in setting up a tls_get_addr call are indeed followed by
8410 such a call. If they are not, we can't do any tls optimization.
8411 On the second pass twiddle tls_mask flags to notify
8412 relocate_section that optimization can be done, and adjust got
8413 and plt refcounts. */
8414 toc_ref = NULL;
8415 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8416 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8417 {
8418 Elf_Internal_Sym *locsyms = NULL;
8419 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8420
102890f0
AM
8421 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8422 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8423 {
8424 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8425 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8426
102890f0
AM
8427 /* Read the relocations. */
8428 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8429 info->keep_memory);
8430 if (relstart == NULL)
2915c55b
JK
8431 {
8432 free (toc_ref);
8433 return FALSE;
8434 }
411e1bfb 8435
102890f0
AM
8436 relend = relstart + sec->reloc_count;
8437 for (rel = relstart; rel < relend; rel++)
8438 {
8439 enum elf_ppc64_reloc_type r_type;
8440 unsigned long r_symndx;
8441 struct elf_link_hash_entry *h;
8442 Elf_Internal_Sym *sym;
8443 asection *sym_sec;
f961d9dd
AM
8444 unsigned char *tls_mask;
8445 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8446 bfd_vma value;
8447 bfd_boolean ok_tprel, is_local;
8448 long toc_ref_index = 0;
8449 int expecting_tls_get_addr = 0;
663a1470 8450 bfd_boolean ret = FALSE;
411e1bfb 8451
102890f0
AM
8452 r_symndx = ELF64_R_SYM (rel->r_info);
8453 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8454 r_symndx, ibfd))
8455 {
8456 err_free_rel:
8457 if (elf_section_data (sec)->relocs != relstart)
8458 free (relstart);
8459 if (toc_ref != NULL)
8460 free (toc_ref);
8461 if (locsyms != NULL
0ffa91dd 8462 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8463 != (unsigned char *) locsyms))
8464 free (locsyms);
663a1470 8465 return ret;
102890f0 8466 }
411e1bfb 8467
102890f0
AM
8468 if (h != NULL)
8469 {
766bc656
AM
8470 if (h->root.type == bfd_link_hash_defined
8471 || h->root.type == bfd_link_hash_defweak)
8472 value = h->root.u.def.value;
8473 else if (h->root.type == bfd_link_hash_undefweak)
8474 value = 0;
8475 else
663a1470
AM
8476 {
8477 found_tls_get_addr_arg = 0;
8478 continue;
8479 }
102890f0
AM
8480 }
8481 else
8482 /* Symbols referenced by TLS relocs must be of type
8483 STT_TLS. So no need for .opd local sym adjust. */
8484 value = sym->st_value;
8485
8486 ok_tprel = FALSE;
8487 is_local = FALSE;
8488 if (h == NULL
8489 || !h->def_dynamic)
8490 {
8491 is_local = TRUE;
766bc656
AM
8492 if (h != NULL
8493 && h->root.type == bfd_link_hash_undefweak)
8494 ok_tprel = TRUE;
c27b8c2a
AM
8495 else if (sym_sec != NULL
8496 && sym_sec->output_section != NULL)
766bc656
AM
8497 {
8498 value += sym_sec->output_offset;
8499 value += sym_sec->output_section->vma;
8500 value -= htab->elf.tls_sec->vma;
8501 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8502 < (bfd_vma) 1 << 32);
8503 }
102890f0 8504 }
951fd09b 8505
102890f0 8506 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8507 /* If this section has old-style __tls_get_addr calls
8508 without marker relocs, then check that each
8509 __tls_get_addr call reloc is preceded by a reloc
8510 that conceivably belongs to the __tls_get_addr arg
8511 setup insn. If we don't find matching arg setup
8512 relocs, don't do any tls optimization. */
8513 if (pass == 0
8514 && sec->has_tls_get_addr_call
8515 && h != NULL
8516 && (h == &htab->tls_get_addr->elf
8517 || h == &htab->tls_get_addr_fd->elf)
8518 && !found_tls_get_addr_arg
8519 && is_branch_reloc (r_type))
8520 {
25f53a85 8521 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8522 "TLS optimization disabled\n"),
8523 ibfd, sec, rel->r_offset);
8524 ret = TRUE;
8525 goto err_free_rel;
8526 }
8527
8528 found_tls_get_addr_arg = 0;
102890f0
AM
8529 switch (r_type)
8530 {
8531 case R_PPC64_GOT_TLSLD16:
8532 case R_PPC64_GOT_TLSLD16_LO:
8533 expecting_tls_get_addr = 1;
663a1470 8534 found_tls_get_addr_arg = 1;
1a0670f3 8535 /* Fall through. */
102890f0
AM
8536
8537 case R_PPC64_GOT_TLSLD16_HI:
8538 case R_PPC64_GOT_TLSLD16_HA:
8539 /* These relocs should never be against a symbol
8540 defined in a shared lib. Leave them alone if
8541 that turns out to be the case. */
8542 if (!is_local)
8543 continue;
411e1bfb 8544
102890f0 8545 /* LD -> LE */
411e1bfb 8546 tls_set = 0;
102890f0
AM
8547 tls_clear = TLS_LD;
8548 tls_type = TLS_TLS | TLS_LD;
8549 break;
411e1bfb 8550
102890f0
AM
8551 case R_PPC64_GOT_TLSGD16:
8552 case R_PPC64_GOT_TLSGD16_LO:
8553 expecting_tls_get_addr = 1;
663a1470 8554 found_tls_get_addr_arg = 1;
1a0670f3 8555 /* Fall through. */
102890f0
AM
8556
8557 case R_PPC64_GOT_TLSGD16_HI:
8558 case R_PPC64_GOT_TLSGD16_HA:
8559 if (ok_tprel)
8560 /* GD -> LE */
411e1bfb 8561 tls_set = 0;
102890f0
AM
8562 else
8563 /* GD -> IE */
8564 tls_set = TLS_TLS | TLS_TPRELGD;
8565 tls_clear = TLS_GD;
8566 tls_type = TLS_TLS | TLS_GD;
8567 break;
8568
8569 case R_PPC64_GOT_TPREL16_DS:
8570 case R_PPC64_GOT_TPREL16_LO_DS:
8571 case R_PPC64_GOT_TPREL16_HI:
8572 case R_PPC64_GOT_TPREL16_HA:
8573 if (ok_tprel)
8574 {
8575 /* IE -> LE */
8576 tls_set = 0;
8577 tls_clear = TLS_TPREL;
8578 tls_type = TLS_TLS | TLS_TPREL;
8579 break;
8580 }
411e1bfb
AM
8581 continue;
8582
727fc41e
AM
8583 case R_PPC64_TLSGD:
8584 case R_PPC64_TLSLD:
663a1470 8585 found_tls_get_addr_arg = 1;
1a0670f3 8586 /* Fall through. */
663a1470
AM
8587
8588 case R_PPC64_TLS:
8589 case R_PPC64_TOC16:
8590 case R_PPC64_TOC16_LO:
102890f0
AM
8591 if (sym_sec == NULL || sym_sec != toc)
8592 continue;
8593
8594 /* Mark this toc entry as referenced by a TLS
8595 code sequence. We can do that now in the
8596 case of R_PPC64_TLS, and after checking for
8597 tls_get_addr for the TOC16 relocs. */
8598 if (toc_ref == NULL)
663a1470
AM
8599 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8600 if (toc_ref == NULL)
8601 goto err_free_rel;
8602
102890f0
AM
8603 if (h != NULL)
8604 value = h->root.u.def.value;
8605 else
8606 value = sym->st_value;
8607 value += rel->r_addend;
73242275
AM
8608 if (value % 8 != 0)
8609 continue;
8610 BFD_ASSERT (value < toc->size
8611 && toc->output_offset % 8 == 0);
663a1470 8612 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8613 if (r_type == R_PPC64_TLS
8614 || r_type == R_PPC64_TLSGD
8615 || r_type == R_PPC64_TLSLD)
102890f0
AM
8616 {
8617 toc_ref[toc_ref_index] = 1;
8618 continue;
8619 }
8620
8621 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8622 continue;
8623
8624 tls_set = 0;
8625 tls_clear = 0;
8626 expecting_tls_get_addr = 2;
8627 break;
8628
8629 case R_PPC64_TPREL64:
8630 if (pass == 0
8631 || sec != toc
8632 || toc_ref == NULL
663a1470 8633 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8634 continue;
8635 if (ok_tprel)
8636 {
8637 /* IE -> LE */
8638 tls_set = TLS_EXPLICIT;
8639 tls_clear = TLS_TPREL;
8640 break;
8641 }
8642 continue;
8643
8644 case R_PPC64_DTPMOD64:
8645 if (pass == 0
8646 || sec != toc
8647 || toc_ref == NULL
663a1470 8648 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8649 continue;
8650 if (rel + 1 < relend
8651 && (rel[1].r_info
8652 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8653 && rel[1].r_offset == rel->r_offset + 8)
8654 {
8655 if (ok_tprel)
8656 /* GD -> LE */
8657 tls_set = TLS_EXPLICIT | TLS_GD;
8658 else
8659 /* GD -> IE */
8660 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8661 tls_clear = TLS_GD;
8662 }
8663 else
8664 {
8665 if (!is_local)
8666 continue;
8667
8668 /* LD -> LE */
8669 tls_set = TLS_EXPLICIT;
8670 tls_clear = TLS_LD;
8671 }
8672 break;
8673
8674 default:
8675 continue;
8676 }
8677
8678 if (pass == 0)
8679 {
727fc41e
AM
8680 if (!expecting_tls_get_addr
8681 || !sec->has_tls_get_addr_call)
102890f0
AM
8682 continue;
8683
3a71aa26
AM
8684 if (rel + 1 < relend
8685 && branch_reloc_hash_match (ibfd, rel + 1,
8686 htab->tls_get_addr,
8687 htab->tls_get_addr_fd))
102890f0 8688 {
3a71aa26 8689 if (expecting_tls_get_addr == 2)
102890f0 8690 {
3a71aa26 8691 /* Check for toc tls entries. */
f961d9dd 8692 unsigned char *toc_tls;
3a71aa26
AM
8693 int retval;
8694
8695 retval = get_tls_mask (&toc_tls, NULL, NULL,
8696 &locsyms,
8697 rel, ibfd);
8698 if (retval == 0)
8699 goto err_free_rel;
663a1470
AM
8700 if (toc_tls != NULL)
8701 {
8702 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8703 found_tls_get_addr_arg = 1;
8704 if (retval > 1)
8705 toc_ref[toc_ref_index] = 1;
8706 }
102890f0 8707 }
3a71aa26 8708 continue;
102890f0
AM
8709 }
8710
8711 if (expecting_tls_get_addr != 1)
8712 continue;
8713
8714 /* Uh oh, we didn't find the expected call. We
8715 could just mark this symbol to exclude it
8716 from tls optimization but it's safer to skip
663a1470 8717 the entire optimization. */
695344c0 8718 /* xgettext:c-format */
25f53a85 8719 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8720 "TLS optimization disabled\n"),
8721 ibfd, sec, rel->r_offset);
8722 ret = TRUE;
8723 goto err_free_rel;
102890f0
AM
8724 }
8725
85f7a9cb 8726 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8727 {
8728 struct plt_entry *ent;
8729 for (ent = htab->tls_get_addr->elf.plt.plist;
8730 ent != NULL;
8731 ent = ent->next)
8732 if (ent->addend == 0)
411e1bfb 8733 {
102890f0 8734 if (ent->plt.refcount > 0)
30038c59 8735 {
102890f0
AM
8736 ent->plt.refcount -= 1;
8737 expecting_tls_get_addr = 0;
30038c59 8738 }
102890f0 8739 break;
411e1bfb 8740 }
102890f0 8741 }
411e1bfb 8742
85f7a9cb 8743 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8744 {
8745 struct plt_entry *ent;
8746 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8747 ent != NULL;
8748 ent = ent->next)
8749 if (ent->addend == 0)
411e1bfb 8750 {
102890f0
AM
8751 if (ent->plt.refcount > 0)
8752 ent->plt.refcount -= 1;
8753 break;
411e1bfb 8754 }
102890f0 8755 }
411e1bfb 8756
102890f0 8757 if (tls_clear == 0)
30038c59
AM
8758 continue;
8759
102890f0
AM
8760 if ((tls_set & TLS_EXPLICIT) == 0)
8761 {
8762 struct got_entry *ent;
411e1bfb 8763
102890f0
AM
8764 /* Adjust got entry for this reloc. */
8765 if (h != NULL)
8766 ent = h->got.glist;
8767 else
8768 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8769
102890f0
AM
8770 for (; ent != NULL; ent = ent->next)
8771 if (ent->addend == rel->r_addend
8772 && ent->owner == ibfd
8773 && ent->tls_type == tls_type)
8774 break;
8775 if (ent == NULL)
8776 abort ();
411e1bfb 8777
102890f0
AM
8778 if (tls_set == 0)
8779 {
8780 /* We managed to get rid of a got entry. */
8781 if (ent->got.refcount > 0)
8782 ent->got.refcount -= 1;
8783 }
8784 }
8785 else
8786 {
8787 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8788 we'll lose one or two dyn relocs. */
8789 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8790 NULL, h, sym))
102890f0 8791 return FALSE;
411e1bfb 8792
102890f0
AM
8793 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8794 {
8795 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8796 NULL, h, sym))
102890f0
AM
8797 return FALSE;
8798 }
8799 }
411e1bfb 8800
102890f0
AM
8801 *tls_mask |= tls_set;
8802 *tls_mask &= ~tls_clear;
8803 }
8c1d1bb8 8804
102890f0
AM
8805 if (elf_section_data (sec)->relocs != relstart)
8806 free (relstart);
8807 }
411e1bfb 8808
663a1470
AM
8809 if (locsyms != NULL
8810 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8811 {
8812 if (!info->keep_memory)
8813 free (locsyms);
8814 else
8815 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8816 }
8817 }
411e1bfb 8818
663a1470
AM
8819 if (toc_ref != NULL)
8820 free (toc_ref);
b34976b6 8821 return TRUE;
1e2f5b6e 8822}
b34976b6 8823
c5614fa4
AM
8824/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8825 the values of any global symbols in a toc section that has been
8826 edited. Globals in toc sections should be a rarity, so this function
8827 sets a flag if any are found in toc sections other than the one just
8828 edited, so that futher hash table traversals can be avoided. */
8829
8830struct adjust_toc_info
8831{
8832 asection *toc;
8833 unsigned long *skip;
8834 bfd_boolean global_toc_syms;
8835};
8836
ba761f19
AM
8837enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8838
c5614fa4
AM
8839static bfd_boolean
8840adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8841{
8842 struct ppc_link_hash_entry *eh;
8843 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8844 unsigned long i;
c5614fa4 8845
c5614fa4
AM
8846 if (h->root.type != bfd_link_hash_defined
8847 && h->root.type != bfd_link_hash_defweak)
8848 return TRUE;
8849
8850 eh = (struct ppc_link_hash_entry *) h;
8851 if (eh->adjust_done)
8852 return TRUE;
8853
8854 if (eh->elf.root.u.def.section == toc_inf->toc)
8855 {
854b41e7
AM
8856 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8857 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8858 else
854b41e7
AM
8859 i = eh->elf.root.u.def.value >> 3;
8860
ba761f19 8861 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8862 {
4eca0228 8863 _bfd_error_handler
854b41e7
AM
8864 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8865 do
8866 ++i;
ba761f19 8867 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8868 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8869 }
854b41e7
AM
8870
8871 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8872 eh->adjust_done = 1;
8873 }
8874 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8875 toc_inf->global_toc_syms = TRUE;
8876
8877 return TRUE;
8878}
8879
39eeab25
AM
8880/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8881 on a _LO variety toc/got reloc. */
560c8763
AM
8882
8883static bfd_boolean
39eeab25 8884ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8885{
39eeab25
AM
8886 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8887 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
560c8763
AM
8888 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8889 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8890 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8891 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8892 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8893 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8894 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8895 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8896 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8897 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8898 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8899 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8900 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
39eeab25
AM
8901 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8902 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8903 /* Exclude lfqu by testing reloc. If relocs are ever
8904 defined for the reduced D field in psq_lu then those
8905 will need testing too. */
8906 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8907 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8908 && (insn & 1) == 0)
8909 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8910 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8911 /* Exclude stfqu. psq_stu as above for psq_lu. */
8912 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8913 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8914 && (insn & 1) == 0));
560c8763
AM
8915}
8916
c5614fa4
AM
8917/* Examine all relocs referencing .toc sections in order to remove
8918 unused .toc entries. */
8919
8920bfd_boolean
33c0ec9d 8921ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8922{
8923 bfd *ibfd;
8924 struct adjust_toc_info toc_inf;
67f0cbdb 8925 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8926
67f0cbdb 8927 htab->do_toc_opt = 1;
c5614fa4 8928 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8929 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8930 {
8931 asection *toc, *sec;
8932 Elf_Internal_Shdr *symtab_hdr;
8933 Elf_Internal_Sym *local_syms;
425b145b 8934 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8935 unsigned long *skip, *drop;
8936 unsigned char *used;
8937 unsigned char *keep, last, some_unused;
8938
854b41e7
AM
8939 if (!is_ppc64_elf (ibfd))
8940 continue;
8941
c5614fa4
AM
8942 toc = bfd_get_section_by_name (ibfd, ".toc");
8943 if (toc == NULL
92b7a70f 8944 || toc->size == 0
dbaa2011
AM
8945 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8946 || discarded_section (toc))
c5614fa4
AM
8947 continue;
8948
425b145b 8949 toc_relocs = NULL;
c5614fa4 8950 local_syms = NULL;
0ffa91dd 8951 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8952
8953 /* Look at sections dropped from the final link. */
8954 skip = NULL;
8955 relstart = NULL;
8956 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8957 {
8958 if (sec->reloc_count == 0
dbaa2011 8959 || !discarded_section (sec)
c5614fa4
AM
8960 || get_opd_info (sec)
8961 || (sec->flags & SEC_ALLOC) == 0
8962 || (sec->flags & SEC_DEBUGGING) != 0)
8963 continue;
8964
8965 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8966 if (relstart == NULL)
8967 goto error_ret;
8968
8969 /* Run through the relocs to see which toc entries might be
8970 unused. */
8971 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8972 {
8973 enum elf_ppc64_reloc_type r_type;
8974 unsigned long r_symndx;
8975 asection *sym_sec;
8976 struct elf_link_hash_entry *h;
8977 Elf_Internal_Sym *sym;
8978 bfd_vma val;
8979
8980 r_type = ELF64_R_TYPE (rel->r_info);
8981 switch (r_type)
8982 {
8983 default:
8984 continue;
8985
8986 case R_PPC64_TOC16:
8987 case R_PPC64_TOC16_LO:
8988 case R_PPC64_TOC16_HI:
8989 case R_PPC64_TOC16_HA:
8990 case R_PPC64_TOC16_DS:
8991 case R_PPC64_TOC16_LO_DS:
8992 break;
8993 }
8994
8995 r_symndx = ELF64_R_SYM (rel->r_info);
8996 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8997 r_symndx, ibfd))
8998 goto error_ret;
8999
9000 if (sym_sec != toc)
9001 continue;
9002
9003 if (h != NULL)
9004 val = h->root.u.def.value;
9005 else
9006 val = sym->st_value;
9007 val += rel->r_addend;
9008
9009 if (val >= toc->size)
9010 continue;
9011
9012 /* Anything in the toc ought to be aligned to 8 bytes.
9013 If not, don't mark as unused. */
9014 if (val & 7)
9015 continue;
9016
9017 if (skip == NULL)
9018 {
854b41e7 9019 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9020 if (skip == NULL)
9021 goto error_ret;
9022 }
9023
ba761f19 9024 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9025 }
9026
9027 if (elf_section_data (sec)->relocs != relstart)
9028 free (relstart);
9029 }
9030
ba761f19
AM
9031 /* For largetoc loads of address constants, we can convert
9032 . addis rx,2,addr@got@ha
9033 . ld ry,addr@got@l(rx)
9034 to
9035 . addis rx,2,addr@toc@ha
9036 . addi ry,rx,addr@toc@l
9037 when addr is within 2G of the toc pointer. This then means
9038 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9039
ba761f19
AM
9040 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9041 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9042 && toc->reloc_count != 0)
9043 {
9044 /* Read toc relocs. */
425b145b
AM
9045 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9046 info->keep_memory);
9047 if (toc_relocs == NULL)
ba761f19
AM
9048 goto error_ret;
9049
425b145b 9050 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9051 {
9052 enum elf_ppc64_reloc_type r_type;
9053 unsigned long r_symndx;
9054 asection *sym_sec;
9055 struct elf_link_hash_entry *h;
9056 Elf_Internal_Sym *sym;
9057 bfd_vma val, addr;
9058
9059 r_type = ELF64_R_TYPE (rel->r_info);
9060 if (r_type != R_PPC64_ADDR64)
9061 continue;
9062
9063 r_symndx = ELF64_R_SYM (rel->r_info);
9064 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9065 r_symndx, ibfd))
9066 goto error_ret;
9067
425b145b 9068 if (sym_sec == NULL
c27b8c2a 9069 || sym_sec->output_section == NULL
dbaa2011 9070 || discarded_section (sym_sec))
425b145b
AM
9071 continue;
9072
afe397ea 9073 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9074 continue;
9075
9076 if (h != NULL)
bddc25c9
AM
9077 {
9078 if (h->type == STT_GNU_IFUNC)
9079 continue;
9080 val = h->root.u.def.value;
9081 }
ba761f19 9082 else
bddc25c9
AM
9083 {
9084 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9085 continue;
9086 val = sym->st_value;
9087 }
ba761f19
AM
9088 val += rel->r_addend;
9089 val += sym_sec->output_section->vma + sym_sec->output_offset;
9090
9091 /* We don't yet know the exact toc pointer value, but we
9092 know it will be somewhere in the toc section. Don't
9093 optimize if the difference from any possible toc
9094 pointer is outside [ff..f80008000, 7fff7fff]. */
9095 addr = toc->output_section->vma + TOC_BASE_OFF;
9096 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9097 continue;
9098
9099 addr = toc->output_section->vma + toc->output_section->rawsize;
9100 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9101 continue;
9102
9103 if (skip == NULL)
9104 {
9105 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9106 if (skip == NULL)
9107 goto error_ret;
9108 }
9109
9110 skip[rel->r_offset >> 3]
425b145b 9111 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9112 }
ba761f19
AM
9113 }
9114
c5614fa4
AM
9115 if (skip == NULL)
9116 continue;
9117
9118 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9119 if (used == NULL)
9120 {
9121 error_ret:
9122 if (local_syms != NULL
9123 && symtab_hdr->contents != (unsigned char *) local_syms)
9124 free (local_syms);
9125 if (sec != NULL
9126 && relstart != NULL
9127 && elf_section_data (sec)->relocs != relstart)
9128 free (relstart);
425b145b
AM
9129 if (toc_relocs != NULL
9130 && elf_section_data (toc)->relocs != toc_relocs)
9131 free (toc_relocs);
c5614fa4
AM
9132 if (skip != NULL)
9133 free (skip);
9134 return FALSE;
9135 }
9136
30038c59
AM
9137 /* Now check all kept sections that might reference the toc.
9138 Check the toc itself last. */
9139 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9140 : ibfd->sections);
c5614fa4 9141 sec != NULL;
c5614fa4 9142 sec = (sec == toc ? NULL
c5614fa4 9143 : sec->next == NULL ? toc
30038c59 9144 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9145 : sec->next))
9146 {
9147 int repeat;
9148
9149 if (sec->reloc_count == 0
dbaa2011 9150 || discarded_section (sec)
c5614fa4
AM
9151 || get_opd_info (sec)
9152 || (sec->flags & SEC_ALLOC) == 0
9153 || (sec->flags & SEC_DEBUGGING) != 0)
9154 continue;
9155
854b41e7
AM
9156 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9157 info->keep_memory);
c5614fa4 9158 if (relstart == NULL)
2915c55b
JK
9159 {
9160 free (used);
9161 goto error_ret;
9162 }
c5614fa4
AM
9163
9164 /* Mark toc entries referenced as used. */
c5614fa4 9165 do
d4f1ee75
AM
9166 {
9167 repeat = 0;
9168 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9169 {
9170 enum elf_ppc64_reloc_type r_type;
9171 unsigned long r_symndx;
9172 asection *sym_sec;
9173 struct elf_link_hash_entry *h;
9174 Elf_Internal_Sym *sym;
9175 bfd_vma val;
9176 enum {no_check, check_lo, check_ha} insn_check;
98528052 9177
d4f1ee75
AM
9178 r_type = ELF64_R_TYPE (rel->r_info);
9179 switch (r_type)
9180 {
9181 default:
9182 insn_check = no_check;
9183 break;
98528052 9184
d4f1ee75
AM
9185 case R_PPC64_GOT_TLSLD16_HA:
9186 case R_PPC64_GOT_TLSGD16_HA:
9187 case R_PPC64_GOT_TPREL16_HA:
9188 case R_PPC64_GOT_DTPREL16_HA:
9189 case R_PPC64_GOT16_HA:
9190 case R_PPC64_TOC16_HA:
9191 insn_check = check_ha;
9192 break;
98528052 9193
d4f1ee75
AM
9194 case R_PPC64_GOT_TLSLD16_LO:
9195 case R_PPC64_GOT_TLSGD16_LO:
9196 case R_PPC64_GOT_TPREL16_LO_DS:
9197 case R_PPC64_GOT_DTPREL16_LO_DS:
9198 case R_PPC64_GOT16_LO:
9199 case R_PPC64_GOT16_LO_DS:
9200 case R_PPC64_TOC16_LO:
9201 case R_PPC64_TOC16_LO_DS:
9202 insn_check = check_lo;
9203 break;
9204 }
560c8763 9205
d4f1ee75
AM
9206 if (insn_check != no_check)
9207 {
9208 bfd_vma off = rel->r_offset & ~3;
9209 unsigned char buf[4];
9210 unsigned int insn;
c5614fa4 9211
d4f1ee75
AM
9212 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9213 {
9214 free (used);
9215 goto error_ret;
9216 }
9217 insn = bfd_get_32 (ibfd, buf);
9218 if (insn_check == check_lo
39eeab25 9219 ? !ok_lo_toc_insn (insn, r_type)
d4f1ee75
AM
9220 : ((insn & ((0x3f << 26) | 0x1f << 16))
9221 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9222 {
9223 char str[12];
9224
9225 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9226 sprintf (str, "%#08x", insn);
9227 info->callbacks->einfo
695344c0 9228 /* xgettext:c-format */
d4f1ee75
AM
9229 (_("%P: %H: toc optimization is not supported for"
9230 " %s instruction.\n"),
9231 ibfd, sec, rel->r_offset & ~3, str);
9232 }
9233 }
c5614fa4 9234
d4f1ee75
AM
9235 switch (r_type)
9236 {
9237 case R_PPC64_TOC16:
9238 case R_PPC64_TOC16_LO:
9239 case R_PPC64_TOC16_HI:
9240 case R_PPC64_TOC16_HA:
9241 case R_PPC64_TOC16_DS:
9242 case R_PPC64_TOC16_LO_DS:
9243 /* In case we're taking addresses of toc entries. */
9244 case R_PPC64_ADDR64:
9245 break;
c5614fa4 9246
d4f1ee75
AM
9247 default:
9248 continue;
9249 }
c5614fa4 9250
d4f1ee75
AM
9251 r_symndx = ELF64_R_SYM (rel->r_info);
9252 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9253 r_symndx, ibfd))
9254 {
9255 free (used);
9256 goto error_ret;
9257 }
c5614fa4 9258
d4f1ee75
AM
9259 if (sym_sec != toc)
9260 continue;
c5614fa4 9261
d4f1ee75
AM
9262 if (h != NULL)
9263 val = h->root.u.def.value;
9264 else
9265 val = sym->st_value;
9266 val += rel->r_addend;
ba761f19 9267
d4f1ee75
AM
9268 if (val >= toc->size)
9269 continue;
ba761f19 9270
d4f1ee75
AM
9271 if ((skip[val >> 3] & can_optimize) != 0)
9272 {
9273 bfd_vma off;
9274 unsigned char opc;
9275
9276 switch (r_type)
9277 {
9278 case R_PPC64_TOC16_HA:
ba761f19 9279 break;
ba761f19 9280
d4f1ee75
AM
9281 case R_PPC64_TOC16_LO_DS:
9282 off = rel->r_offset;
9283 off += (bfd_big_endian (ibfd) ? -2 : 3);
9284 if (!bfd_get_section_contents (ibfd, sec, &opc,
9285 off, 1))
9286 {
9287 free (used);
9288 goto error_ret;
9289 }
9290 if ((opc & (0x3f << 2)) == (58u << 2))
9291 break;
1a0670f3 9292 /* Fall through. */
ba761f19 9293
d4f1ee75
AM
9294 default:
9295 /* Wrong sort of reloc, or not a ld. We may
9296 as well clear ref_from_discarded too. */
9297 skip[val >> 3] = 0;
9298 }
9299 }
9300
9301 if (sec != toc)
9302 used[val >> 3] = 1;
9303 /* For the toc section, we only mark as used if this
9304 entry itself isn't unused. */
9305 else if ((used[rel->r_offset >> 3]
9306 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9307 && !used[val >> 3])
9308 {
9309 /* Do all the relocs again, to catch reference
9310 chains. */
9311 repeat = 1;
9312 used[val >> 3] = 1;
9313 }
9314 }
9315 }
c5614fa4 9316 while (repeat);
854b41e7
AM
9317
9318 if (elf_section_data (sec)->relocs != relstart)
9319 free (relstart);
c5614fa4
AM
9320 }
9321
9322 /* Merge the used and skip arrays. Assume that TOC
9323 doublewords not appearing as either used or unused belong
9324 to to an entry more than one doubleword in size. */
9325 for (drop = skip, keep = used, last = 0, some_unused = 0;
9326 drop < skip + (toc->size + 7) / 8;
9327 ++drop, ++keep)
9328 {
9329 if (*keep)
9330 {
ba761f19
AM
9331 *drop &= ~ref_from_discarded;
9332 if ((*drop & can_optimize) != 0)
9333 some_unused = 1;
c5614fa4
AM
9334 last = 0;
9335 }
b140b010 9336 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9337 {
9338 some_unused = 1;
ba761f19 9339 last = ref_from_discarded;
c5614fa4
AM
9340 }
9341 else
9342 *drop = last;
9343 }
9344
9345 free (used);
9346
9347 if (some_unused)
9348 {
9349 bfd_byte *contents, *src;
9350 unsigned long off;
d62b3684 9351 Elf_Internal_Sym *sym;
ba761f19 9352 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9353
9354 /* Shuffle the toc contents, and at the same time convert the
9355 skip array from booleans into offsets. */
9356 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9357 goto error_ret;
9358
9359 elf_section_data (toc)->this_hdr.contents = contents;
9360
9361 for (src = contents, off = 0, drop = skip;
9362 src < contents + toc->size;
9363 src += 8, ++drop)
9364 {
ba761f19
AM
9365 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9366 off += 8;
c5614fa4
AM
9367 else if (off != 0)
9368 {
9369 *drop = off;
9370 memcpy (src - off, src, 8);
9371 }
9372 }
854b41e7 9373 *drop = off;
c5614fa4
AM
9374 toc->rawsize = toc->size;
9375 toc->size = src - contents - off;
9376
ba761f19
AM
9377 /* Adjust addends for relocs against the toc section sym,
9378 and optimize any accesses we can. */
c5614fa4
AM
9379 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9380 {
9381 if (sec->reloc_count == 0
dbaa2011 9382 || discarded_section (sec))
c5614fa4
AM
9383 continue;
9384
9385 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9386 info->keep_memory);
c5614fa4
AM
9387 if (relstart == NULL)
9388 goto error_ret;
9389
9390 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9391 {
9392 enum elf_ppc64_reloc_type r_type;
9393 unsigned long r_symndx;
9394 asection *sym_sec;
9395 struct elf_link_hash_entry *h;
854b41e7 9396 bfd_vma val;
c5614fa4
AM
9397
9398 r_type = ELF64_R_TYPE (rel->r_info);
9399 switch (r_type)
9400 {
9401 default:
9402 continue;
9403
9404 case R_PPC64_TOC16:
9405 case R_PPC64_TOC16_LO:
9406 case R_PPC64_TOC16_HI:
9407 case R_PPC64_TOC16_HA:
9408 case R_PPC64_TOC16_DS:
9409 case R_PPC64_TOC16_LO_DS:
9410 case R_PPC64_ADDR64:
9411 break;
9412 }
9413
9414 r_symndx = ELF64_R_SYM (rel->r_info);
9415 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9416 r_symndx, ibfd))
9417 goto error_ret;
9418
ba761f19 9419 if (sym_sec != toc)
c5614fa4
AM
9420 continue;
9421
ba761f19
AM
9422 if (h != NULL)
9423 val = h->root.u.def.value;
9424 else
9425 {
9426 val = sym->st_value;
9427 if (val != 0)
9428 local_toc_syms = TRUE;
9429 }
9430
9431 val += rel->r_addend;
854b41e7
AM
9432
9433 if (val > toc->rawsize)
9434 val = toc->rawsize;
ba761f19
AM
9435 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9436 continue;
9437 else if ((skip[val >> 3] & can_optimize) != 0)
9438 {
9439 Elf_Internal_Rela *tocrel
425b145b 9440 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9441 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9442
9443 switch (r_type)
9444 {
9445 case R_PPC64_TOC16_HA:
9446 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9447 break;
9448
9449 case R_PPC64_TOC16_LO_DS:
9450 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9451 break;
9452
9453 default:
28942f62
AM
9454 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9455 ppc_howto_init ();
b140b010 9456 info->callbacks->einfo
695344c0 9457 /* xgettext:c-format */
bc30df16 9458 (_("%P: %H: %s references "
b140b010
AM
9459 "optimized away TOC entry\n"),
9460 ibfd, sec, rel->r_offset,
9461 ppc64_elf_howto_table[r_type]->name);
9462 bfd_set_error (bfd_error_bad_value);
9463 goto error_ret;
ba761f19
AM
9464 }
9465 rel->r_addend = tocrel->r_addend;
9466 elf_section_data (sec)->relocs = relstart;
9467 continue;
9468 }
9469
9470 if (h != NULL || sym->st_value != 0)
9471 continue;
854b41e7
AM
9472
9473 rel->r_addend -= skip[val >> 3];
9474 elf_section_data (sec)->relocs = relstart;
c5614fa4 9475 }
854b41e7
AM
9476
9477 if (elf_section_data (sec)->relocs != relstart)
9478 free (relstart);
c5614fa4
AM
9479 }
9480
9481 /* We shouldn't have local or global symbols defined in the TOC,
9482 but handle them anyway. */
df22d223
AM
9483 if (local_syms != NULL)
9484 for (sym = local_syms;
9485 sym < local_syms + symtab_hdr->sh_info;
9486 ++sym)
9487 if (sym->st_value != 0
9488 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9489 {
9490 unsigned long i;
854b41e7 9491
df22d223
AM
9492 if (sym->st_value > toc->rawsize)
9493 i = toc->rawsize >> 3;
9494 else
9495 i = sym->st_value >> 3;
854b41e7 9496
df22d223
AM
9497 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9498 {
9499 if (local_toc_syms)
4eca0228 9500 _bfd_error_handler
df22d223
AM
9501 (_("%s defined on removed toc entry"),
9502 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9503 do
9504 ++i;
9505 while ((skip[i] & (ref_from_discarded | can_optimize)));
9506 sym->st_value = (bfd_vma) i << 3;
9507 }
d62b3684 9508
df22d223
AM
9509 sym->st_value -= skip[i];
9510 symtab_hdr->contents = (unsigned char *) local_syms;
9511 }
c5614fa4 9512
854b41e7 9513 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9514 if (toc_inf.global_toc_syms)
9515 {
9516 toc_inf.toc = toc;
9517 toc_inf.skip = skip;
9518 toc_inf.global_toc_syms = FALSE;
9519 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9520 &toc_inf);
9521 }
854b41e7
AM
9522
9523 if (toc->reloc_count != 0)
9524 {
d4730f92 9525 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9526 Elf_Internal_Rela *wrel;
9527 bfd_size_type sz;
9528
854b41e7 9529 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9530 if (toc_relocs == NULL)
9531 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9532 info->keep_memory);
9533 if (toc_relocs == NULL)
9534 goto error_ret;
9535
425b145b
AM
9536 wrel = toc_relocs;
9537 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9538 if ((skip[rel->r_offset >> 3]
9539 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9540 {
9541 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9542 wrel->r_info = rel->r_info;
9543 wrel->r_addend = rel->r_addend;
9544 ++wrel;
9545 }
9546 else if (!dec_dynrel_count (rel->r_info, toc, info,
9547 &local_syms, NULL, NULL))
9548 goto error_ret;
9549
425b145b
AM
9550 elf_section_data (toc)->relocs = toc_relocs;
9551 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9552 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9553 sz = rel_hdr->sh_entsize;
9554 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9555 }
c5614fa4 9556 }
28be611c
AM
9557 else if (toc_relocs != NULL
9558 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9559 free (toc_relocs);
c5614fa4
AM
9560
9561 if (local_syms != NULL
9562 && symtab_hdr->contents != (unsigned char *) local_syms)
9563 {
9564 if (!info->keep_memory)
9565 free (local_syms);
9566 else
9567 symtab_hdr->contents = (unsigned char *) local_syms;
9568 }
9569 free (skip);
9570 }
9571
9572 return TRUE;
9573}
9574
1bbe0902
AM
9575/* Return true iff input section I references the TOC using
9576 instructions limited to +/-32k offsets. */
9577
9578bfd_boolean
9579ppc64_elf_has_small_toc_reloc (asection *i)
9580{
9581 return (is_ppc64_elf (i->owner)
9582 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9583}
9584
927be08e
AM
9585/* Allocate space for one GOT entry. */
9586
9587static void
9588allocate_got (struct elf_link_hash_entry *h,
9589 struct bfd_link_info *info,
9590 struct got_entry *gent)
9591{
9592 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9593 bfd_boolean dyn;
9594 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9595 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9596 ? 16 : 8);
9597 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9598 ? 2 : 1) * sizeof (Elf64_External_Rela);
9599 asection *got = ppc64_elf_tdata (gent->owner)->got;
9600
9601 gent->got.offset = got->size;
9602 got->size += entsize;
9603
9604 dyn = htab->elf.dynamic_sections_created;
19e08130 9605 if (h->type == STT_GNU_IFUNC)
927be08e 9606 {
33e44f2e 9607 htab->elf.irelplt->size += rentsize;
19e08130 9608 htab->got_reli_size += rentsize;
927be08e 9609 }
0e1862bb 9610 else if ((bfd_link_pic (info)
19e08130
AM
9611 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9612 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9613 || h->root.type != bfd_link_hash_undefweak))
927be08e 9614 {
19e08130 9615 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9616 relgot->size += rentsize;
927be08e
AM
9617 }
9618}
9619
7865406b
AM
9620/* This function merges got entries in the same toc group. */
9621
9622static void
9623merge_got_entries (struct got_entry **pent)
9624{
9625 struct got_entry *ent, *ent2;
9626
9627 for (ent = *pent; ent != NULL; ent = ent->next)
9628 if (!ent->is_indirect)
9629 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9630 if (!ent2->is_indirect
9631 && ent2->addend == ent->addend
9632 && ent2->tls_type == ent->tls_type
9633 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9634 {
9635 ent2->is_indirect = TRUE;
9636 ent2->got.ent = ent;
9637 }
9638}
9639
65f38f15
AM
9640/* Allocate space in .plt, .got and associated reloc sections for
9641 dynamic relocs. */
5bd4f169 9642
b34976b6 9643static bfd_boolean
4ce794b7 9644allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9645{
65f38f15
AM
9646 struct bfd_link_info *info;
9647 struct ppc_link_hash_table *htab;
5bd4f169 9648 asection *s;
65f38f15 9649 struct ppc_link_hash_entry *eh;
0b8bcf0d 9650 struct got_entry **pgent, *gent;
5bd4f169 9651
e92d460e 9652 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9653 return TRUE;
5bd4f169 9654
65f38f15
AM
9655 info = (struct bfd_link_info *) inf;
9656 htab = ppc_hash_table (info);
4dfe6ac6
NC
9657 if (htab == NULL)
9658 return FALSE;
5bd4f169 9659
951fd09b
AM
9660 eh = (struct ppc_link_hash_entry *) h;
9661 /* Run through the TLS GD got entries first if we're changing them
9662 to TPREL. */
e7b938ca 9663 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9664 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9665 if (gent->got.refcount > 0
9666 && (gent->tls_type & TLS_GD) != 0)
9667 {
9668 /* This was a GD entry that has been converted to TPREL. If
9669 there happens to be a TPREL entry we can use that one. */
9670 struct got_entry *ent;
9671 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9672 if (ent->got.refcount > 0
9673 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9674 && ent->addend == gent->addend
9675 && ent->owner == gent->owner)
951fd09b
AM
9676 {
9677 gent->got.refcount = 0;
9678 break;
9679 }
9680
9681 /* If not, then we'll be using our own TPREL entry. */
9682 if (gent->got.refcount != 0)
9683 gent->tls_type = TLS_TLS | TLS_TPREL;
9684 }
9685
7865406b
AM
9686 /* Remove any list entry that won't generate a word in the GOT before
9687 we call merge_got_entries. Otherwise we risk merging to empty
9688 entries. */
0b8bcf0d
AM
9689 pgent = &h->got.glist;
9690 while ((gent = *pgent) != NULL)
411e1bfb 9691 if (gent->got.refcount > 0)
7865406b
AM
9692 {
9693 if ((gent->tls_type & TLS_LD) != 0
9694 && !h->def_dynamic)
9695 {
9696 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9697 *pgent = gent->next;
9698 }
9699 else
9700 pgent = &gent->next;
9701 }
9702 else
9703 *pgent = gent->next;
9704
9705 if (!htab->do_multi_toc)
9706 merge_got_entries (&h->got.glist);
9707
9708 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9709 if (!gent->is_indirect)
411e1bfb
AM
9710 {
9711 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
9712 Undefined weak syms won't yet be marked as dynamic,
9713 nor will all TLS symbols. */
411e1bfb 9714 if (h->dynindx == -1
b099ab9f 9715 && !h->forced_local
25f23106 9716 && h->type != STT_GNU_IFUNC
b099ab9f 9717 && htab->elf.dynamic_sections_created)
411e1bfb 9718 {
c152c796 9719 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
9720 return FALSE;
9721 }
65f38f15 9722
0c8d6e5c 9723 if (!is_ppc64_elf (gent->owner))
927be08e 9724 abort ();
0ffa91dd 9725
927be08e 9726 allocate_got (h, info, gent);
411e1bfb 9727 }
65f38f15 9728
8a2058b5
AM
9729 if (!htab->elf.dynamic_sections_created
9730 && h->type != STT_GNU_IFUNC)
9731 eh->dyn_relocs = NULL;
9732
9733 if (eh->dyn_relocs != NULL)
65f38f15 9734 {
8a2058b5
AM
9735 struct elf_dyn_relocs *p, **pp;
9736
57e7d118
AM
9737 /* In the shared -Bsymbolic case, discard space allocated for
9738 dynamic pc-relative relocs against symbols which turn out to
9739 be defined in regular objects. For the normal shared case,
9740 discard space for relocs that have become local due to symbol
9741 visibility changes. */
9742
9743 if (bfd_link_pic (info))
65f38f15 9744 {
57e7d118
AM
9745 /* Relocs that use pc_count are those that appear on a call
9746 insn, or certain REL relocs (see must_be_dyn_reloc) that
9747 can be generated via assembly. We want calls to
9748 protected symbols to resolve directly to the function
9749 rather than going via the plt. If people want function
9750 pointer comparisons to work as expected then they should
9751 avoid writing weird assembly. */
9752 if (SYMBOL_CALLS_LOCAL (info, h))
9753 {
57e7d118
AM
9754 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9755 {
9756 p->count -= p->pc_count;
9757 p->pc_count = 0;
9758 if (p->count == 0)
9759 *pp = p->next;
9760 else
9761 pp = &p->next;
9762 }
9763 }
65f38f15 9764
57e7d118
AM
9765 /* Also discard relocs on undefined weak syms with
9766 non-default visibility. */
9767 if (eh->dyn_relocs != NULL
9768 && h->root.type == bfd_link_hash_undefweak)
5bd4f169 9769 {
57e7d118
AM
9770 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9771 eh->dyn_relocs = NULL;
9772
9773 /* Make sure this symbol is output as a dynamic symbol.
9774 Undefined weak syms won't yet be marked as dynamic. */
9775 else if (h->dynindx == -1
9776 && !h->forced_local)
9777 {
9778 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9779 return FALSE;
9780 }
5bd4f169 9781 }
65f38f15 9782 }
57e7d118 9783 else if (h->type == STT_GNU_IFUNC)
dfbb6ac9 9784 {
8a2058b5
AM
9785 /* A plt entry is always created when making direct calls to
9786 an ifunc, even when building a static executable, but
9787 that doesn't cover all cases. We may have only an ifunc
9788 initialised function pointer for a given ifunc symbol.
9789
9790 For ELFv2, dynamic relocations are not required when
9791 generating a global entry PLT stub. */
9792 if (abiversion (info->output_bfd) >= 2)
9793 {
9794 if (global_entry_stub (h))
9795 eh->dyn_relocs = NULL;
9796 }
9797
9798 /* For ELFv1 we have function descriptors. Descriptors need
9799 to be treated like PLT entries and thus have dynamic
9800 relocations. One exception is when the function
9801 descriptor is copied into .dynbss (which should only
9802 happen with ancient versions of gcc). */
9803 else if (h->needs_copy)
dfbb6ac9 9804 eh->dyn_relocs = NULL;
57e7d118
AM
9805 }
9806 else if (ELIMINATE_COPY_RELOCS)
9807 {
8a2058b5 9808 /* For the non-pic case, discard space for relocs against
57e7d118
AM
9809 symbols which turn out to need copy relocs or are not
9810 dynamic. */
dfbb6ac9 9811
8a2058b5
AM
9812 /* First make sure this symbol is output as a dynamic symbol.
9813 Undefined weak syms won't yet be marked as dynamic. */
9814 if (h->root.type == bfd_link_hash_undefweak
9815 && !h->non_got_ref
9816 && !h->def_regular
9817 && h->dynindx == -1
9818 && !h->forced_local
9819 && !bfd_elf_link_record_dynamic_symbol (info, h))
9820 return FALSE;
57e7d118 9821
8a2058b5
AM
9822 if (h->non_got_ref
9823 || h->def_regular
9824 || h->dynindx == -1)
9825 eh->dyn_relocs = NULL;
57e7d118
AM
9826 }
9827
9828 /* Finally, allocate space. */
9829 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9830 {
9831 asection *sreloc = elf_section_data (p->sec)->sreloc;
9832 if (eh->elf.type == STT_GNU_IFUNC)
9833 sreloc = htab->elf.irelplt;
9834 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9835 }
65f38f15 9836 }
57e7d118
AM
9837
9838 if ((htab->elf.dynamic_sections_created
9839 && h->dynindx != -1
9840 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
9841 || h->type == STT_GNU_IFUNC)
65f38f15 9842 {
57e7d118
AM
9843 struct plt_entry *pent;
9844 bfd_boolean doneone = FALSE;
9845 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9846 if (pent->plt.refcount > 0)
9847 {
9848 if (!htab->elf.dynamic_sections_created
9849 || h->dynindx == -1)
9850 {
9851 s = htab->elf.iplt;
9852 pent->plt.offset = s->size;
9853 s->size += PLT_ENTRY_SIZE (htab);
9854 s = htab->elf.irelplt;
9855 }
9856 else
9857 {
9858 /* If this is the first .plt entry, make room for the special
9859 first entry. */
9860 s = htab->elf.splt;
9861 if (s->size == 0)
9862 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9863
57e7d118 9864 pent->plt.offset = s->size;
65f38f15 9865
57e7d118
AM
9866 /* Make room for this entry. */
9867 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9868
57e7d118
AM
9869 /* Make room for the .glink code. */
9870 s = htab->glink;
9871 if (s->size == 0)
9872 s->size += GLINK_CALL_STUB_SIZE;
9873 if (htab->opd_abi)
9874 {
9875 /* We need bigger stubs past index 32767. */
9876 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9877 s->size += 4;
9878 s->size += 2*4;
9879 }
9880 else
9881 s->size += 4;
65f38f15 9882
57e7d118
AM
9883 /* We also need to make an entry in the .rela.plt section. */
9884 s = htab->elf.srelplt;
9885 }
9886 s->size += sizeof (Elf64_External_Rela);
9887 doneone = TRUE;
9888 }
9889 else
9890 pent->plt.offset = (bfd_vma) -1;
9891 if (!doneone)
9892 {
9893 h->plt.plist = NULL;
9894 h->needs_plt = 0;
9895 }
65f38f15 9896 }
57e7d118 9897 else
65f38f15 9898 {
57e7d118
AM
9899 h->plt.plist = NULL;
9900 h->needs_plt = 0;
65f38f15
AM
9901 }
9902
b34976b6 9903 return TRUE;
65f38f15
AM
9904}
9905
a345bc8d
AM
9906/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9907 to set up space for global entry stubs. These are put in glink,
9908 after the branch table. */
65f38f15 9909
b34976b6 9910static bfd_boolean
a345bc8d 9911size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9912{
a345bc8d
AM
9913 struct bfd_link_info *info;
9914 struct ppc_link_hash_table *htab;
9915 struct plt_entry *pent;
9916 asection *s;
65f38f15 9917
a345bc8d
AM
9918 if (h->root.type == bfd_link_hash_indirect)
9919 return TRUE;
65f38f15 9920
a345bc8d
AM
9921 if (!h->pointer_equality_needed)
9922 return TRUE;
65f38f15 9923
a345bc8d
AM
9924 if (h->def_regular)
9925 return TRUE;
65f38f15 9926
a345bc8d
AM
9927 info = inf;
9928 htab = ppc_hash_table (info);
9929 if (htab == NULL)
9930 return FALSE;
9931
9932 s = htab->glink;
9933 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9934 if (pent->plt.offset != (bfd_vma) -1
9935 && pent->addend == 0)
9936 {
afe397ea
AM
9937 /* For ELFv2, if this symbol is not defined in a regular file
9938 and we are not generating a shared library or pie, then we
9939 need to define the symbol in the executable on a call stub.
9940 This is to avoid text relocations. */
a345bc8d 9941 s->size = (s->size + 15) & -16;
8a2058b5 9942 h->root.type = bfd_link_hash_defined;
afe397ea
AM
9943 h->root.u.def.section = s;
9944 h->root.u.def.value = s->size;
a345bc8d
AM
9945 s->size += 16;
9946 break;
9947 }
9948 return TRUE;
9949}
9950
9951/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9952 read-only sections. */
9953
9954static bfd_boolean
9955maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9956{
9957 if (h->root.type == bfd_link_hash_indirect)
9958 return TRUE;
9959
9960 if (readonly_dynrelocs (h))
9961 {
9962 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9963
9964 /* Not an error, just cut short the traversal. */
9965 return FALSE;
65f38f15 9966 }
b34976b6 9967 return TRUE;
65f38f15
AM
9968}
9969
9970/* Set the sizes of the dynamic sections. */
9971
b34976b6 9972static bfd_boolean
ee67d69a 9973ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9974 struct bfd_link_info *info)
65f38f15
AM
9975{
9976 struct ppc_link_hash_table *htab;
9977 bfd *dynobj;
9978 asection *s;
b34976b6 9979 bfd_boolean relocs;
65f38f15 9980 bfd *ibfd;
7865406b 9981 struct got_entry *first_tlsld;
65f38f15
AM
9982
9983 htab = ppc_hash_table (info);
4dfe6ac6
NC
9984 if (htab == NULL)
9985 return FALSE;
9986
65f38f15
AM
9987 dynobj = htab->elf.dynobj;
9988 if (dynobj == NULL)
9989 abort ();
9990
9991 if (htab->elf.dynamic_sections_created)
9992 {
9993 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9994 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 9995 {
3d4d4302 9996 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9997 if (s == NULL)
9998 abort ();
eea6121a 9999 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10000 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10001 }
10002 }
10003
10004 /* Set up .got offsets for local syms, and space for local dynamic
10005 relocs. */
c72f2fb2 10006 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10007 {
411e1bfb
AM
10008 struct got_entry **lgot_ents;
10009 struct got_entry **end_lgot_ents;
e054468f
AM
10010 struct plt_entry **local_plt;
10011 struct plt_entry **end_local_plt;
f961d9dd 10012 unsigned char *lgot_masks;
65f38f15
AM
10013 bfd_size_type locsymcount;
10014 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10015
0c8d6e5c 10016 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10017 continue;
10018
10019 for (s = ibfd->sections; s != NULL; s = s->next)
10020 {
19e08130 10021 struct ppc_dyn_relocs *p;
65f38f15 10022
6edfbbad 10023 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10024 {
ec338859
AM
10025 if (!bfd_is_abs_section (p->sec)
10026 && bfd_is_abs_section (p->sec->output_section))
10027 {
10028 /* Input section has been discarded, either because
10029 it is a copy of a linkonce section or due to
10030 linker script /DISCARD/, so we'll be discarding
10031 the relocs too. */
10032 }
248866a8 10033 else if (p->count != 0)
ec338859 10034 {
19e08130
AM
10035 asection *srel = elf_section_data (p->sec)->sreloc;
10036 if (p->ifunc)
33e44f2e 10037 srel = htab->elf.irelplt;
eea6121a 10038 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10039 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10040 info->flags |= DF_TEXTREL;
ec338859 10041 }
65f38f15
AM
10042 }
10043 }
10044
411e1bfb
AM
10045 lgot_ents = elf_local_got_ents (ibfd);
10046 if (!lgot_ents)
65f38f15
AM
10047 continue;
10048
0ffa91dd 10049 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10050 locsymcount = symtab_hdr->sh_info;
411e1bfb 10051 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10052 local_plt = (struct plt_entry **) end_lgot_ents;
10053 end_local_plt = local_plt + locsymcount;
f961d9dd 10054 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10055 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10056 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10057 {
0b8bcf0d 10058 struct got_entry **pent, *ent;
411e1bfb 10059
0b8bcf0d
AM
10060 pent = lgot_ents;
10061 while ((ent = *pent) != NULL)
411e1bfb
AM
10062 if (ent->got.refcount > 0)
10063 {
e7b938ca 10064 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10065 {
927be08e 10066 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10067 *pent = ent->next;
411e1bfb
AM
10068 }
10069 else
10070 {
19e08130
AM
10071 unsigned int ent_size = 8;
10072 unsigned int rel_size = sizeof (Elf64_External_Rela);
10073
eea6121a 10074 ent->got.offset = s->size;
e7b938ca 10075 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10076 {
19e08130
AM
10077 ent_size *= 2;
10078 rel_size *= 2;
10079 }
10080 s->size += ent_size;
10081 if ((*lgot_masks & PLT_IFUNC) != 0)
10082 {
33e44f2e 10083 htab->elf.irelplt->size += rel_size;
19e08130
AM
10084 htab->got_reli_size += rel_size;
10085 }
0e1862bb 10086 else if (bfd_link_pic (info))
19e08130
AM
10087 {
10088 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10089 srel->size += rel_size;
927be08e 10090 }
0b8bcf0d 10091 pent = &ent->next;
411e1bfb
AM
10092 }
10093 }
10094 else
0b8bcf0d 10095 *pent = ent->next;
65f38f15 10096 }
e054468f
AM
10097
10098 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10099 for (; local_plt < end_local_plt; ++local_plt)
10100 {
10101 struct plt_entry *ent;
10102
10103 for (ent = *local_plt; ent != NULL; ent = ent->next)
10104 if (ent->plt.refcount > 0)
10105 {
33e44f2e 10106 s = htab->elf.iplt;
e054468f 10107 ent->plt.offset = s->size;
b9e5796b 10108 s->size += PLT_ENTRY_SIZE (htab);
e054468f 10109
33e44f2e 10110 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
10111 }
10112 else
10113 ent->plt.offset = (bfd_vma) -1;
10114 }
65f38f15
AM
10115 }
10116
10117 /* Allocate global sym .plt and .got entries, and space for global
10118 sym dynamic relocs. */
4ce794b7 10119 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d
AM
10120 /* Stash the end of glink branch table. */
10121 if (htab->glink != NULL)
10122 htab->glink->rawsize = htab->glink->size;
10123
0e1862bb 10124 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10125 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10126
7865406b 10127 first_tlsld = NULL;
c72f2fb2 10128 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10129 {
7865406b
AM
10130 struct got_entry *ent;
10131
0c8d6e5c 10132 if (!is_ppc64_elf (ibfd))
102890f0
AM
10133 continue;
10134
7865406b
AM
10135 ent = ppc64_tlsld_got (ibfd);
10136 if (ent->got.refcount > 0)
102890f0 10137 {
7865406b 10138 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10139 {
7865406b
AM
10140 ent->is_indirect = TRUE;
10141 ent->got.ent = first_tlsld;
10142 }
10143 else
10144 {
10145 if (first_tlsld == NULL)
10146 first_tlsld = ent;
10147 s = ppc64_elf_tdata (ibfd)->got;
10148 ent->got.offset = s->size;
10149 ent->owner = ibfd;
10150 s->size += 16;
0e1862bb 10151 if (bfd_link_pic (info))
7865406b
AM
10152 {
10153 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10154 srel->size += sizeof (Elf64_External_Rela);
10155 }
102890f0
AM
10156 }
10157 }
10158 else
7865406b 10159 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10160 }
10161
65f38f15
AM
10162 /* We now have determined the sizes of the various dynamic sections.
10163 Allocate memory for them. */
b34976b6 10164 relocs = FALSE;
65f38f15
AM
10165 for (s = dynobj->sections; s != NULL; s = s->next)
10166 {
10167 if ((s->flags & SEC_LINKER_CREATED) == 0)
10168 continue;
10169
4ce794b7 10170 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10171 /* These haven't been allocated yet; don't strip. */
10172 continue;
33e44f2e
AM
10173 else if (s == htab->elf.sgot
10174 || s == htab->elf.splt
10175 || s == htab->elf.iplt
c456f082 10176 || s == htab->glink
9d19e4fd 10177 || s == htab->elf.sdynbss)
65f38f15
AM
10178 {
10179 /* Strip this section if we don't need it; see the
10180 comment below. */
5bd4f169 10181 }
58d180e8
AM
10182 else if (s == htab->glink_eh_frame)
10183 {
10184 if (!bfd_is_abs_section (s->output_section))
10185 /* Not sized yet. */
10186 continue;
10187 }
70cc837d 10188 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10189 {
c456f082 10190 if (s->size != 0)
5bd4f169 10191 {
33e44f2e 10192 if (s != htab->elf.srelplt)
b34976b6 10193 relocs = TRUE;
5bd4f169
AM
10194
10195 /* We use the reloc_count field as a counter if we need
10196 to copy relocs into the output file. */
10197 s->reloc_count = 0;
10198 }
10199 }
65f38f15 10200 else
5bd4f169
AM
10201 {
10202 /* It's not one of our sections, so don't allocate space. */
10203 continue;
10204 }
10205
eea6121a 10206 if (s->size == 0)
5bd4f169 10207 {
c456f082
AM
10208 /* If we don't need this section, strip it from the
10209 output file. This is mostly to handle .rela.bss and
10210 .rela.plt. We must create both sections in
10211 create_dynamic_sections, because they must be created
10212 before the linker maps input sections to output
10213 sections. The linker does that before
10214 adjust_dynamic_symbol is called, and it is that
10215 function which decides whether anything needs to go
10216 into these sections. */
8423293d 10217 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10218 continue;
10219 }
10220
c456f082 10221 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10222 continue;
10223
65f38f15
AM
10224 /* Allocate memory for the section contents. We use bfd_zalloc
10225 here in case unused entries are not reclaimed before the
10226 section's contents are written out. This should not happen,
411e1bfb
AM
10227 but this way if it does we get a R_PPC64_NONE reloc in .rela
10228 sections instead of garbage.
10229 We also rely on the section contents being zero when writing
10230 the GOT. */
eea6121a 10231 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10232 if (s->contents == NULL)
b34976b6 10233 return FALSE;
5bd4f169
AM
10234 }
10235
c72f2fb2 10236 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10237 {
0c8d6e5c 10238 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10239 continue;
10240
e717da7e 10241 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10242 if (s != NULL && s != htab->elf.sgot)
e717da7e 10243 {
eea6121a 10244 if (s->size == 0)
8423293d 10245 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10246 else
10247 {
eea6121a 10248 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10249 if (s->contents == NULL)
10250 return FALSE;
10251 }
10252 }
10253 s = ppc64_elf_tdata (ibfd)->relgot;
10254 if (s != NULL)
10255 {
eea6121a 10256 if (s->size == 0)
8423293d 10257 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10258 else
10259 {
eea6121a 10260 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10261 if (s->contents == NULL)
10262 return FALSE;
10263 relocs = TRUE;
10264 s->reloc_count = 0;
10265 }
10266 }
10267 }
10268
e86ce104 10269 if (htab->elf.dynamic_sections_created)
5bd4f169 10270 {
e8910a83
AM
10271 bfd_boolean tls_opt;
10272
5bd4f169
AM
10273 /* Add some entries to the .dynamic section. We fill in the
10274 values later, in ppc64_elf_finish_dynamic_sections, but we
10275 must add the entries now so that we get the correct size for
10276 the .dynamic section. The DT_DEBUG entry is filled in by the
10277 dynamic linker and used by the debugger. */
dc810e39 10278#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10279 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10280
0e1862bb 10281 if (bfd_link_executable (info))
5bd4f169 10282 {
dc810e39 10283 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10284 return FALSE;
5bd4f169
AM
10285 }
10286
33e44f2e 10287 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10288 {
dc810e39
AM
10289 if (!add_dynamic_entry (DT_PLTGOT, 0)
10290 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10291 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10292 || !add_dynamic_entry (DT_JMPREL, 0)
10293 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10294 return FALSE;
5bd4f169
AM
10295 }
10296
ee67d69a 10297 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10298 {
10299 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10300 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10301 return FALSE;
19397422
AM
10302 }
10303
7c9cf415 10304 tls_opt = (htab->params->tls_get_addr_opt
e8910a83
AM
10305 && htab->tls_get_addr_fd != NULL
10306 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10307 if (tls_opt || !htab->opd_abi)
10308 {
10309 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10310 return FALSE;
10311 }
a7f2871e 10312
5bd4f169
AM
10313 if (relocs)
10314 {
dc810e39
AM
10315 if (!add_dynamic_entry (DT_RELA, 0)
10316 || !add_dynamic_entry (DT_RELASZ, 0)
10317 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10318 return FALSE;
5bd4f169 10319
65f38f15
AM
10320 /* If any dynamic relocs apply to a read-only section,
10321 then we need a DT_TEXTREL entry. */
248866a8 10322 if ((info->flags & DF_TEXTREL) == 0)
a345bc8d 10323 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
5bd4f169 10324
65f38f15 10325 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10326 {
65f38f15 10327 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10328 return FALSE;
5bd4f169 10329 }
5bd4f169 10330 }
5bd4f169 10331 }
65f38f15 10332#undef add_dynamic_entry
5bd4f169 10333
b34976b6 10334 return TRUE;
5bd4f169
AM
10335}
10336
a345bc8d
AM
10337/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10338
10339static bfd_boolean
10340ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10341{
10342 if (h->plt.plist != NULL
10343 && !h->def_regular
10344 && !h->pointer_equality_needed)
10345 return FALSE;
10346
10347 return _bfd_elf_hash_symbol (h);
10348}
10349
721956f4 10350/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10351
4ce794b7
AM
10352static inline enum ppc_stub_type
10353ppc_type_of_stub (asection *input_sec,
10354 const Elf_Internal_Rela *rel,
10355 struct ppc_link_hash_entry **hash,
e054468f 10356 struct plt_entry **plt_ent,
6911b7dc
AM
10357 bfd_vma destination,
10358 unsigned long local_off)
5bd4f169 10359{
721956f4
AM
10360 struct ppc_link_hash_entry *h = *hash;
10361 bfd_vma location;
10362 bfd_vma branch_offset;
10363 bfd_vma max_branch_offset;
4ce794b7 10364 enum elf_ppc64_reloc_type r_type;
5bd4f169 10365
721956f4
AM
10366 if (h != NULL)
10367 {
e054468f 10368 struct plt_entry *ent;
7fe2b9a6 10369 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10370 if (h->oh != NULL
10371 && h->oh->is_func_descriptor)
7b8f6675
AM
10372 {
10373 fdh = ppc_follow_link (h->oh);
10374 *hash = fdh;
10375 }
8387904d 10376
e054468f
AM
10377 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10378 if (ent->addend == rel->r_addend
10379 && ent->plt.offset != (bfd_vma) -1)
10380 {
e054468f
AM
10381 *plt_ent = ent;
10382 return ppc_stub_plt_call;
10383 }
5bd4f169 10384
7fe2b9a6
AM
10385 /* Here, we know we don't have a plt entry. If we don't have a
10386 either a defined function descriptor or a defined entry symbol
10387 in a regular object file, then it is pointless trying to make
10388 any other type of stub. */
854b41e7
AM
10389 if (!is_static_defined (&fdh->elf)
10390 && !is_static_defined (&h->elf))
721956f4 10391 return ppc_stub_none;
5d1634d7 10392 }
e054468f
AM
10393 else if (elf_local_got_ents (input_sec->owner) != NULL)
10394 {
10395 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10396 struct plt_entry **local_plt = (struct plt_entry **)
10397 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10398 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10399
10400 if (local_plt[r_symndx] != NULL)
10401 {
10402 struct plt_entry *ent;
10403
10404 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10405 if (ent->addend == rel->r_addend
10406 && ent->plt.offset != (bfd_vma) -1)
10407 {
10408 *plt_ent = ent;
10409 return ppc_stub_plt_call;
10410 }
10411 }
10412 }
5d1634d7 10413
721956f4
AM
10414 /* Determine where the call point is. */
10415 location = (input_sec->output_offset
10416 + input_sec->output_section->vma
10417 + rel->r_offset);
5d1634d7 10418
721956f4
AM
10419 branch_offset = destination - location;
10420 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10421
721956f4
AM
10422 /* Determine if a long branch stub is needed. */
10423 max_branch_offset = 1 << 25;
4ce794b7 10424 if (r_type != R_PPC64_REL24)
721956f4 10425 max_branch_offset = 1 << 15;
5d1634d7 10426
6911b7dc 10427 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10428 /* We need a stub. Figure out whether a long_branch or plt_branch
10429 is needed later. */
10430 return ppc_stub_long_branch;
5d1634d7 10431
721956f4 10432 return ppc_stub_none;
5d1634d7
AM
10433}
10434
794e51c0
AM
10435/* With power7 weakly ordered memory model, it is possible for ld.so
10436 to update a plt entry in one thread and have another thread see a
10437 stale zero toc entry. To avoid this we need some sort of acquire
10438 barrier in the call stub. One solution is to make the load of the
10439 toc word seem to appear to depend on the load of the function entry
10440 word. Another solution is to test for r2 being zero, and branch to
10441 the appropriate glink entry if so.
10442
10443 . fake dep barrier compare
71a39c98
AM
10444 . ld 12,xxx(2) ld 12,xxx(2)
10445 . mtctr 12 mtctr 12
10446 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10447 . add 2,2,11 cmpldi 2,0
10448 . ld 2,xxx+8(2) bnectr+
10449 . bctr b <glink_entry>
10450
10451 The solution involving the compare turns out to be faster, so
10452 that's what we use unless the branch won't reach. */
10453
10454#define ALWAYS_USE_FAKE_DEP 0
10455#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10456
5d1634d7
AM
10457#define PPC_LO(v) ((v) & 0xffff)
10458#define PPC_HI(v) (((v) >> 16) & 0xffff)
10459#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10460
794e51c0
AM
10461static inline unsigned int
10462plt_stub_size (struct ppc_link_hash_table *htab,
10463 struct ppc_stub_hash_entry *stub_entry,
10464 bfd_vma off)
10465{
b9e5796b
AM
10466 unsigned size = 12;
10467
10468 if (ALWAYS_EMIT_R2SAVE
10469 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10470 size += 4;
10471 if (PPC_HA (off) != 0)
794e51c0 10472 size += 4;
b9e5796b
AM
10473 if (htab->opd_abi)
10474 {
10475 size += 4;
e7d1c40c 10476 if (htab->params->plt_static_chain)
b9e5796b 10477 size += 4;
bd4d2eaa
AM
10478 if (htab->params->plt_thread_safe
10479 && htab->elf.dynamic_sections_created
10480 && stub_entry->h != NULL
10481 && stub_entry->h->elf.dynindx != -1)
b9e5796b 10482 size += 8;
e7d1c40c 10483 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
b9e5796b
AM
10484 size += 4;
10485 }
794e51c0
AM
10486 if (stub_entry->h != NULL
10487 && (stub_entry->h == htab->tls_get_addr_fd
10488 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10489 && htab->params->tls_get_addr_opt)
794e51c0
AM
10490 size += 13 * 4;
10491 return size;
10492}
10493
10494/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10495 then return the padding needed to do so. */
10496static inline unsigned int
10497plt_stub_pad (struct ppc_link_hash_table *htab,
10498 struct ppc_stub_hash_entry *stub_entry,
10499 bfd_vma plt_off)
10500{
e7d1c40c 10501 int stub_align = 1 << htab->params->plt_stub_align;
794e51c0 10502 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
6f20ed8a 10503 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0
AM
10504
10505 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10506 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10507 return stub_align - (stub_off & (stub_align - 1));
10508 return 0;
10509}
10510
10511/* Build a .plt call stub. */
10512
10513static inline bfd_byte *
10514build_plt_stub (struct ppc_link_hash_table *htab,
10515 struct ppc_stub_hash_entry *stub_entry,
10516 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10517{
e7d1c40c 10518 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10519 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10520 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10521 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10522 && htab->elf.dynamic_sections_created
10523 && stub_entry->h != NULL
10524 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10525 bfd_boolean use_fake_dep = plt_thread_safe;
10526 bfd_vma cmp_branch_off = 0;
10527
10528 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10529 && plt_load_toc
794e51c0 10530 && plt_thread_safe
bd4d2eaa
AM
10531 && !((stub_entry->h == htab->tls_get_addr_fd
10532 || stub_entry->h == htab->tls_get_addr)
7c9cf415 10533 && htab->params->tls_get_addr_opt))
794e51c0
AM
10534 {
10535 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
10536 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10537 / PLT_ENTRY_SIZE (htab));
794e51c0
AM
10538 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10539 bfd_vma to, from;
10540
68d62958
AM
10541 if (pltindex > 32768)
10542 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10543 to = (glinkoff
10544 + htab->glink->output_offset
10545 + htab->glink->output_section->vma);
6f20ed8a 10546 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
10547 + 4 * (ALWAYS_EMIT_R2SAVE
10548 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10549 + 4 * (PPC_HA (offset) != 0)
10550 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10551 != PPC_HA (offset))
10552 + 4 * (plt_static_chain != 0)
10553 + 20
6f20ed8a
AM
10554 + stub_entry->group->stub_sec->output_offset
10555 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
10556 cmp_branch_off = to - from;
10557 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10558 }
10559
ac2df442
AM
10560 if (PPC_HA (offset) != 0)
10561 {
176a0d42
AM
10562 if (r != NULL)
10563 {
794e51c0
AM
10564 if (ALWAYS_EMIT_R2SAVE
10565 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10566 r[0].r_offset += 4;
176a0d42 10567 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10568 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10569 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10570 r[1].r_addend = r[0].r_addend;
b9e5796b 10571 if (plt_load_toc)
176a0d42 10572 {
b9e5796b 10573 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10574 {
b9e5796b
AM
10575 r[2].r_offset = r[1].r_offset + 4;
10576 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10577 r[2].r_addend = r[0].r_addend;
10578 }
10579 else
10580 {
10581 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10582 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10583 r[2].r_addend = r[0].r_addend + 8;
10584 if (plt_static_chain)
10585 {
10586 r[3].r_offset = r[2].r_offset + 4;
10587 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10588 r[3].r_addend = r[0].r_addend + 16;
10589 }
c7131b65 10590 }
176a0d42
AM
10591 }
10592 }
794e51c0
AM
10593 if (ALWAYS_EMIT_R2SAVE
10594 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10595 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
10596 if (plt_load_toc)
10597 {
10598 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10599 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10600 }
10601 else
10602 {
10603 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10604 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10605 }
b9e5796b
AM
10606 if (plt_load_toc
10607 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10608 {
71a39c98 10609 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10610 offset = 0;
10611 }
71a39c98 10612 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10613 if (plt_load_toc)
794e51c0 10614 {
b9e5796b
AM
10615 if (use_fake_dep)
10616 {
10617 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10618 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10619 }
10620 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10621 if (plt_static_chain)
10622 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 10623 }
ac2df442
AM
10624 }
10625 else
10626 {
176a0d42
AM
10627 if (r != NULL)
10628 {
794e51c0
AM
10629 if (ALWAYS_EMIT_R2SAVE
10630 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10631 r[0].r_offset += 4;
176a0d42 10632 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 10633 if (plt_load_toc)
176a0d42 10634 {
b9e5796b 10635 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 10636 {
b9e5796b
AM
10637 r[1].r_offset = r[0].r_offset + 4;
10638 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10639 r[1].r_addend = r[0].r_addend;
10640 }
10641 else
10642 {
10643 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10644 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10645 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10646 if (plt_static_chain)
10647 {
10648 r[2].r_offset = r[1].r_offset + 4;
10649 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10650 r[2].r_addend = r[0].r_addend + 8;
10651 }
c7131b65 10652 }
176a0d42
AM
10653 }
10654 }
794e51c0
AM
10655 if (ALWAYS_EMIT_R2SAVE
10656 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 10657 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 10658 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
10659 if (plt_load_toc
10660 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10661 {
10662 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10663 offset = 0;
10664 }
71a39c98 10665 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 10666 if (plt_load_toc)
794e51c0 10667 {
b9e5796b
AM
10668 if (use_fake_dep)
10669 {
10670 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10671 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10672 }
10673 if (plt_static_chain)
10674 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10675 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 10676 }
ac2df442 10677 }
b9e5796b 10678 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
10679 {
10680 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10681 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10682 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10683 }
10684 else
10685 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10686 return p;
10687}
10688
a7f2871e
AM
10689/* Build a special .plt call stub for __tls_get_addr. */
10690
10691#define LD_R11_0R3 0xe9630000
10692#define LD_R12_0R3 0xe9830000
10693#define MR_R0_R3 0x7c601b78
10694#define CMPDI_R11_0 0x2c2b0000
10695#define ADD_R3_R12_R13 0x7c6c6a14
10696#define BEQLR 0x4d820020
10697#define MR_R3_R0 0x7c030378
a7f2871e
AM
10698#define STD_R11_0R1 0xf9610000
10699#define BCTRL 0x4e800421
10700#define LD_R11_0R1 0xe9610000
a7f2871e
AM
10701#define MTLR_R11 0x7d6803a6
10702
10703static inline bfd_byte *
794e51c0
AM
10704build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10705 struct ppc_stub_hash_entry *stub_entry,
10706 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10707{
e7d1c40c 10708 bfd *obfd = htab->params->stub_bfd;
794e51c0 10709
a7f2871e
AM
10710 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10711 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10712 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10713 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10714 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10715 bfd_put_32 (obfd, BEQLR, p), p += 4;
10716 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10717 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
a078d95a 10718 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10719
10720 if (r != NULL)
10721 r[0].r_offset += 9 * 4;
794e51c0 10722 p = build_plt_stub (htab, stub_entry, p, offset, r);
a7f2871e
AM
10723 bfd_put_32 (obfd, BCTRL, p - 4);
10724
a078d95a 10725 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
bd4d2eaa 10726 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
a7f2871e
AM
10727 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10728 bfd_put_32 (obfd, BLR, p), p += 4;
10729
10730 return p;
10731}
10732
176a0d42
AM
10733static Elf_Internal_Rela *
10734get_relocs (asection *sec, int count)
10735{
10736 Elf_Internal_Rela *relocs;
10737 struct bfd_elf_section_data *elfsec_data;
10738
10739 elfsec_data = elf_section_data (sec);
10740 relocs = elfsec_data->relocs;
10741 if (relocs == NULL)
10742 {
10743 bfd_size_type relsize;
10744 relsize = sec->reloc_count * sizeof (*relocs);
10745 relocs = bfd_alloc (sec->owner, relsize);
10746 if (relocs == NULL)
10747 return NULL;
10748 elfsec_data->relocs = relocs;
d4730f92
BS
10749 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10750 sizeof (Elf_Internal_Shdr));
10751 if (elfsec_data->rela.hdr == NULL)
10752 return NULL;
10753 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10754 * sizeof (Elf64_External_Rela));
10755 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10756 sec->reloc_count = 0;
10757 }
10758 relocs += sec->reloc_count;
10759 sec->reloc_count += count;
10760 return relocs;
10761}
10762
aa374f67 10763static bfd_vma
25f53a85 10764get_r2off (struct bfd_link_info *info,
aa374f67
AM
10765 struct ppc_stub_hash_entry *stub_entry)
10766{
25f53a85 10767 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 10768 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
10769
10770 if (r2off == 0)
10771 {
10772 /* Support linking -R objects. Get the toc pointer from the
10773 opd entry. */
10774 char buf[8];
b9e5796b
AM
10775 if (!htab->opd_abi)
10776 return r2off;
aa374f67
AM
10777 asection *opd = stub_entry->h->elf.root.u.def.section;
10778 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10779
10780 if (strcmp (opd->name, ".opd") != 0
10781 || opd->reloc_count != 0)
10782 {
bc30df16 10783 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
25f53a85 10784 stub_entry->h->elf.root.root.string);
aa374f67 10785 bfd_set_error (bfd_error_bad_value);
a7c49797 10786 return (bfd_vma) -1;
aa374f67
AM
10787 }
10788 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 10789 return (bfd_vma) -1;
aa374f67 10790 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10791 r2off -= elf_gp (info->output_bfd);
aa374f67 10792 }
6f20ed8a 10793 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
10794 return r2off;
10795}
10796
b34976b6 10797static bfd_boolean
4ce794b7 10798ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10799{
721956f4
AM
10800 struct ppc_stub_hash_entry *stub_entry;
10801 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10802 struct bfd_link_info *info;
10803 struct ppc_link_hash_table *htab;
721956f4
AM
10804 bfd_byte *loc;
10805 bfd_byte *p;
ee75fd95 10806 bfd_vma dest, off;
721956f4 10807 int size;
176a0d42 10808 Elf_Internal_Rela *r;
e054468f 10809 asection *plt;
5d1634d7 10810
721956f4
AM
10811 /* Massage our args to the form they really have. */
10812 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10813 info = in_arg;
5d1634d7 10814
5d1634d7 10815 htab = ppc_hash_table (info);
4dfe6ac6
NC
10816 if (htab == NULL)
10817 return FALSE;
5d1634d7 10818
721956f4 10819 /* Make a note of the offset within the stubs for this entry. */
6f20ed8a
AM
10820 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10821 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 10822
4ce794b7 10823 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10824 switch (stub_entry->stub_type)
5d1634d7 10825 {
721956f4 10826 case ppc_stub_long_branch:
ad8e1ba5 10827 case ppc_stub_long_branch_r2off:
721956f4 10828 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10829 dest = (stub_entry->target_value
10830 + stub_entry->target_section->output_offset
10831 + stub_entry->target_section->output_section->vma);
10832 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10833 off = dest;
5d1634d7 10834
721956f4
AM
10835 /* And this is where we are coming from. */
10836 off -= (stub_entry->stub_offset
6f20ed8a
AM
10837 + stub_entry->group->stub_sec->output_offset
10838 + stub_entry->group->stub_sec->output_section->vma);
e86ce104 10839
ac2df442
AM
10840 size = 4;
10841 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10842 {
25f53a85 10843 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10844
a7c49797 10845 if (r2off == (bfd_vma) -1)
aa374f67
AM
10846 {
10847 htab->stub_error = TRUE;
10848 return FALSE;
10849 }
e7d1c40c 10850 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 10851 loc += 4;
a7c49797 10852 size = 8;
ac2df442
AM
10853 if (PPC_HA (r2off) != 0)
10854 {
e7d1c40c
AM
10855 bfd_put_32 (htab->params->stub_bfd,
10856 ADDIS_R2_R2 | PPC_HA (r2off), loc);
ac2df442 10857 loc += 4;
a7c49797
AM
10858 size += 4;
10859 }
10860 if (PPC_LO (r2off) != 0)
10861 {
10862 bfd_put_32 (htab->params->stub_bfd,
10863 ADDI_R2_R2 | PPC_LO (r2off), loc);
10864 loc += 4;
10865 size += 4;
ac2df442 10866 }
ac2df442 10867 off -= size - 4;
ad8e1ba5 10868 }
e7d1c40c 10869 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 10870
5c3dead3
AM
10871 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10872 {
bc30df16
AM
10873 info->callbacks->einfo
10874 (_("%P: long branch stub `%s' offset overflow\n"),
10875 stub_entry->root.string);
5c3dead3
AM
10876 htab->stub_error = TRUE;
10877 return FALSE;
10878 }
ee75fd95
AM
10879
10880 if (info->emitrelocations)
10881 {
6f20ed8a 10882 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
10883 if (r == NULL)
10884 return FALSE;
6f20ed8a 10885 r->r_offset = loc - stub_entry->group->stub_sec->contents;
ee75fd95
AM
10886 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10887 r->r_addend = dest;
10888 if (stub_entry->h != NULL)
10889 {
10890 struct elf_link_hash_entry **hashes;
10891 unsigned long symndx;
10892 struct ppc_link_hash_entry *h;
10893
e7d1c40c 10894 hashes = elf_sym_hashes (htab->params->stub_bfd);
ee75fd95
AM
10895 if (hashes == NULL)
10896 {
10897 bfd_size_type hsize;
10898
10899 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
e7d1c40c 10900 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
ee75fd95
AM
10901 if (hashes == NULL)
10902 return FALSE;
e7d1c40c 10903 elf_sym_hashes (htab->params->stub_bfd) = hashes;
ee75fd95
AM
10904 htab->stub_globals = 1;
10905 }
10906 symndx = htab->stub_globals++;
10907 h = stub_entry->h;
10908 hashes[symndx] = &h->elf;
10909 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10910 if (h->oh != NULL && h->oh->is_func)
b31867b6 10911 h = ppc_follow_link (h->oh);
ee75fd95
AM
10912 if (h->elf.root.u.def.section != stub_entry->target_section)
10913 /* H is an opd symbol. The addend must be zero. */
10914 r->r_addend = 0;
10915 else
10916 {
10917 off = (h->elf.root.u.def.value
10918 + h->elf.root.u.def.section->output_offset
10919 + h->elf.root.u.def.section->output_section->vma);
10920 r->r_addend -= off;
10921 }
10922 }
10923 }
721956f4 10924 break;
e86ce104 10925
721956f4 10926 case ppc_stub_plt_branch:
ad8e1ba5 10927 case ppc_stub_plt_branch_r2off:
721956f4
AM
10928 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10929 stub_entry->root.string + 9,
b34976b6 10930 FALSE, FALSE);
721956f4
AM
10931 if (br_entry == NULL)
10932 {
8de848d8 10933 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 10934 stub_entry->root.string);
b34976b6
AM
10935 htab->stub_error = TRUE;
10936 return FALSE;
721956f4
AM
10937 }
10938
176a0d42
AM
10939 dest = (stub_entry->target_value
10940 + stub_entry->target_section->output_offset
10941 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10942 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10943 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10944
176a0d42 10945 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10946 htab->brlt->contents + br_entry->offset);
721956f4 10947
f94498ff 10948 if (br_entry->iter == htab->stub_iteration)
721956f4 10949 {
f94498ff 10950 br_entry->iter = 0;
84f5d08e 10951
f94498ff 10952 if (htab->relbrlt != NULL)
84f5d08e 10953 {
f94498ff
AM
10954 /* Create a reloc for the branch lookup table entry. */
10955 Elf_Internal_Rela rela;
10956 bfd_byte *rl;
10957
10958 rela.r_offset = (br_entry->offset
10959 + htab->brlt->output_offset
10960 + htab->brlt->output_section->vma);
10961 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10962 rela.r_addend = dest;
f94498ff
AM
10963
10964 rl = htab->relbrlt->contents;
10965 rl += (htab->relbrlt->reloc_count++
10966 * sizeof (Elf64_External_Rela));
10967 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10968 }
10969 else if (info->emitrelocations)
10970 {
176a0d42
AM
10971 r = get_relocs (htab->brlt, 1);
10972 if (r == NULL)
10973 return FALSE;
10974 /* brlt, being SEC_LINKER_CREATED does not go through the
10975 normal reloc processing. Symbols and offsets are not
10976 translated from input file to output file form, so
10977 set up the offset per the output file. */
f94498ff
AM
10978 r->r_offset = (br_entry->offset
10979 + htab->brlt->output_offset
10980 + htab->brlt->output_section->vma);
10981 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10982 r->r_addend = dest;
84f5d08e 10983 }
84f5d08e 10984 }
721956f4 10985
176a0d42
AM
10986 dest = (br_entry->offset
10987 + htab->brlt->output_offset
10988 + htab->brlt->output_section->vma);
10989
10990 off = (dest
4ce794b7 10991 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 10992 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 10993
ad8e1ba5 10994 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10995 {
25f53a85 10996 info->callbacks->einfo
bc30df16 10997 (_("%P: linkage table error against `%T'\n"),
721956f4 10998 stub_entry->root.string);
5d1634d7 10999 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11000 htab->stub_error = TRUE;
11001 return FALSE;
5d1634d7 11002 }
41bd81ab 11003
176a0d42
AM
11004 if (info->emitrelocations)
11005 {
6f20ed8a 11006 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11007 if (r == NULL)
11008 return FALSE;
6f20ed8a 11009 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11010 if (bfd_big_endian (info->output_bfd))
11011 r[0].r_offset += 2;
00f412ee 11012 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11013 r[0].r_offset += 4;
11014 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11015 r[0].r_addend = dest;
11016 if (PPC_HA (off) != 0)
11017 {
11018 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11019 r[1].r_offset = r[0].r_offset + 4;
11020 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11021 r[1].r_addend = r[0].r_addend;
11022 }
11023 }
11024
00f412ee 11025 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11026 {
176a0d42 11027 if (PPC_HA (off) != 0)
ac2df442
AM
11028 {
11029 size = 16;
e7d1c40c 11030 bfd_put_32 (htab->params->stub_bfd,
397998fc 11031 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 11032 loc += 4;
e7d1c40c 11033 bfd_put_32 (htab->params->stub_bfd,
397998fc 11034 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
11035 }
11036 else
11037 {
11038 size = 12;
e7d1c40c
AM
11039 bfd_put_32 (htab->params->stub_bfd,
11040 LD_R12_0R2 | PPC_LO (off), loc);
ac2df442 11041 }
ad8e1ba5
AM
11042 }
11043 else
11044 {
25f53a85 11045 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11046
a7c49797 11047 if (r2off == (bfd_vma) -1)
aa374f67
AM
11048 {
11049 htab->stub_error = TRUE;
11050 return FALSE;
11051 }
ad8e1ba5 11052
e7d1c40c 11053 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
ad8e1ba5 11054 loc += 4;
00f412ee 11055 size = 16;
176a0d42 11056 if (PPC_HA (off) != 0)
ac2df442
AM
11057 {
11058 size += 4;
e7d1c40c 11059 bfd_put_32 (htab->params->stub_bfd,
397998fc 11060 ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 11061 loc += 4;
e7d1c40c 11062 bfd_put_32 (htab->params->stub_bfd,
397998fc 11063 LD_R12_0R12 | PPC_LO (off), loc);
ac2df442
AM
11064 }
11065 else
e7d1c40c 11066 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442
AM
11067
11068 if (PPC_HA (r2off) != 0)
11069 {
11070 size += 4;
00f412ee 11071 loc += 4;
e7d1c40c
AM
11072 bfd_put_32 (htab->params->stub_bfd,
11073 ADDIS_R2_R2 | PPC_HA (r2off), loc);
00f412ee
AM
11074 }
11075 if (PPC_LO (r2off) != 0)
11076 {
11077 size += 4;
ac2df442 11078 loc += 4;
e7d1c40c
AM
11079 bfd_put_32 (htab->params->stub_bfd,
11080 ADDI_R2_R2 | PPC_LO (r2off), loc);
ac2df442 11081 }
ad8e1ba5
AM
11082 }
11083 loc += 4;
e7d1c40c 11084 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
ad8e1ba5 11085 loc += 4;
e7d1c40c 11086 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
721956f4 11087 break;
5d1634d7 11088
721956f4 11089 case ppc_stub_plt_call:
794e51c0 11090 case ppc_stub_plt_call_r2save:
e054468f 11091 if (stub_entry->h != NULL
b31867b6
AM
11092 && stub_entry->h->is_func_descriptor
11093 && stub_entry->h->oh != NULL)
c862ae31 11094 {
b31867b6
AM
11095 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11096
11097 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11098 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11099 if (fh->elf.root.type == bfd_link_hash_undefined
11100 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11101 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11102 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11103 }
11104
721956f4 11105 /* Now build the stub. */
e054468f 11106 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 11107 if (dest >= (bfd_vma) -2)
721956f4
AM
11108 abort ();
11109
33e44f2e 11110 plt = htab->elf.splt;
25f23106
AM
11111 if (!htab->elf.dynamic_sections_created
11112 || stub_entry->h == NULL
11113 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11114 plt = htab->elf.iplt;
e054468f
AM
11115
11116 dest += plt->output_offset + plt->output_section->vma;
11117
11118 if (stub_entry->h == NULL
11119 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11120 {
11121 Elf_Internal_Rela rela;
11122 bfd_byte *rl;
11123
11124 rela.r_offset = dest;
ee67d69a
AM
11125 if (htab->opd_abi)
11126 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11127 else
11128 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
11129 rela.r_addend = (stub_entry->target_value
11130 + stub_entry->target_section->output_offset
11131 + stub_entry->target_section->output_section->vma);
11132
33e44f2e
AM
11133 rl = (htab->elf.irelplt->contents
11134 + (htab->elf.irelplt->reloc_count++
25f23106
AM
11135 * sizeof (Elf64_External_Rela)));
11136 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
11137 stub_entry->plt_ent->plt.offset |= 1;
11138 }
176a0d42
AM
11139
11140 off = (dest
e054468f 11141 - elf_gp (plt->output_section->owner)
6f20ed8a 11142 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11143
ad8e1ba5 11144 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11145 {
25f53a85 11146 info->callbacks->einfo
695344c0 11147 /* xgettext:c-format */
bc30df16 11148 (_("%P: linkage table error against `%T'\n"),
e054468f
AM
11149 stub_entry->h != NULL
11150 ? stub_entry->h->elf.root.root.string
11151 : "<local sym>");
721956f4 11152 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11153 htab->stub_error = TRUE;
11154 return FALSE;
721956f4
AM
11155 }
11156
e7d1c40c 11157 if (htab->params->plt_stub_align != 0)
794e51c0
AM
11158 {
11159 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11160
6f20ed8a
AM
11161 stub_entry->group->stub_sec->size += pad;
11162 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
794e51c0
AM
11163 loc += pad;
11164 }
11165
176a0d42
AM
11166 r = NULL;
11167 if (info->emitrelocations)
11168 {
6f20ed8a 11169 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11170 ((PPC_HA (off) != 0)
11171 + (htab->opd_abi
e7d1c40c 11172 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11173 && PPC_HA (off + 16) == PPC_HA (off))
11174 : 1)));
176a0d42
AM
11175 if (r == NULL)
11176 return FALSE;
6f20ed8a 11177 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11178 if (bfd_big_endian (info->output_bfd))
11179 r[0].r_offset += 2;
176a0d42
AM
11180 r[0].r_addend = dest;
11181 }
a7f2871e
AM
11182 if (stub_entry->h != NULL
11183 && (stub_entry->h == htab->tls_get_addr_fd
11184 || stub_entry->h == htab->tls_get_addr)
7c9cf415 11185 && htab->params->tls_get_addr_opt)
794e51c0 11186 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 11187 else
794e51c0 11188 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
11189 size = p - loc;
11190 break;
11191
a4b6fadd
AM
11192 case ppc_stub_save_res:
11193 return TRUE;
11194
721956f4
AM
11195 default:
11196 BFD_FAIL ();
b34976b6 11197 return FALSE;
721956f4
AM
11198 }
11199
6f20ed8a 11200 stub_entry->group->stub_sec->size += size;
97b639ba 11201
e7d1c40c 11202 if (htab->params->emit_stub_syms)
97b639ba
AM
11203 {
11204 struct elf_link_hash_entry *h;
ee75fd95
AM
11205 size_t len1, len2;
11206 char *name;
11207 const char *const stub_str[] = { "long_branch",
11208 "long_branch_r2off",
11209 "plt_branch",
11210 "plt_branch_r2off",
794e51c0 11211 "plt_call",
ee75fd95
AM
11212 "plt_call" };
11213
11214 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11215 len2 = strlen (stub_entry->root.string);
11216 name = bfd_malloc (len1 + len2 + 2);
11217 if (name == NULL)
11218 return FALSE;
11219 memcpy (name, stub_entry->root.string, 9);
11220 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11221 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11222 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11223 if (h == NULL)
11224 return FALSE;
11225 if (h->root.type == bfd_link_hash_new)
11226 {
11227 h->root.type = bfd_link_hash_defined;
6f20ed8a 11228 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11229 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11230 h->ref_regular = 1;
11231 h->def_regular = 1;
11232 h->ref_regular_nonweak = 1;
11233 h->forced_local = 1;
11234 h->non_elf = 0;
2ec55de3 11235 h->root.linker_def = 1;
97b639ba
AM
11236 }
11237 }
11238
b34976b6 11239 return TRUE;
721956f4
AM
11240}
11241
11242/* As above, but don't actually build the stub. Just bump offset so
11243 we know stub section sizes, and select plt_branch stubs where
11244 long_branch stubs won't do. */
11245
b34976b6 11246static bfd_boolean
4ce794b7 11247ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11248{
11249 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11250 struct bfd_link_info *info;
721956f4
AM
11251 struct ppc_link_hash_table *htab;
11252 bfd_vma off;
11253 int size;
11254
11255 /* Massage our args to the form they really have. */
11256 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11257 info = in_arg;
11258
11259 htab = ppc_hash_table (info);
4dfe6ac6
NC
11260 if (htab == NULL)
11261 return FALSE;
721956f4 11262
a4b6fadd
AM
11263 if (stub_entry->h != NULL
11264 && stub_entry->h->save_res
11265 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11266 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11267 {
11268 /* Don't make stubs to out-of-line register save/restore
11269 functions. Instead, emit copies of the functions. */
11270 stub_entry->group->needs_save_res = 1;
11271 stub_entry->stub_type = ppc_stub_save_res;
11272 return TRUE;
11273 }
11274
794e51c0
AM
11275 if (stub_entry->stub_type == ppc_stub_plt_call
11276 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 11277 {
e054468f
AM
11278 asection *plt;
11279 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 11280 if (off >= (bfd_vma) -2)
411e1bfb 11281 abort ();
33e44f2e 11282 plt = htab->elf.splt;
25f23106
AM
11283 if (!htab->elf.dynamic_sections_created
11284 || stub_entry->h == NULL
11285 || stub_entry->h->elf.dynindx == -1)
33e44f2e 11286 plt = htab->elf.iplt;
e054468f
AM
11287 off += (plt->output_offset
11288 + plt->output_section->vma
11289 - elf_gp (plt->output_section->owner)
6f20ed8a 11290 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
721956f4 11291
794e51c0 11292 size = plt_stub_size (htab, stub_entry, off);
e7d1c40c 11293 if (htab->params->plt_stub_align)
794e51c0 11294 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
11295 if (info->emitrelocations)
11296 {
6f20ed8a 11297 stub_entry->group->stub_sec->reloc_count
b9e5796b
AM
11298 += ((PPC_HA (off) != 0)
11299 + (htab->opd_abi
e7d1c40c 11300 ? 2 + (htab->params->plt_static_chain
b9e5796b
AM
11301 && PPC_HA (off + 16) == PPC_HA (off))
11302 : 1));
6f20ed8a 11303 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42 11304 }
721956f4
AM
11305 }
11306 else
11307 {
ad8e1ba5
AM
11308 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11309 variants. */
ac2df442 11310 bfd_vma r2off = 0;
6911b7dc 11311 bfd_vma local_off = 0;
ac2df442 11312
721956f4
AM
11313 off = (stub_entry->target_value
11314 + stub_entry->target_section->output_offset
11315 + stub_entry->target_section->output_section->vma);
6f20ed8a
AM
11316 off -= (stub_entry->group->stub_sec->size
11317 + stub_entry->group->stub_sec->output_offset
11318 + stub_entry->group->stub_sec->output_section->vma);
721956f4 11319
ad8e1ba5
AM
11320 /* Reset the stub type from the plt variant in case we now
11321 can reach with a shorter stub. */
11322 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11323 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11324
11325 size = 4;
11326 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11327 {
25f53a85 11328 r2off = get_r2off (info, stub_entry);
a7c49797 11329 if (r2off == (bfd_vma) -1)
aa374f67
AM
11330 {
11331 htab->stub_error = TRUE;
11332 return FALSE;
11333 }
a7c49797 11334 size = 8;
ac2df442 11335 if (PPC_HA (r2off) != 0)
a7c49797
AM
11336 size += 4;
11337 if (PPC_LO (r2off) != 0)
11338 size += 4;
ac2df442 11339 off -= size - 4;
ad8e1ba5
AM
11340 }
11341
6911b7dc
AM
11342 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11343
b9e5796b
AM
11344 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11345 Do the same for -R objects without function descriptors. */
11346 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11347 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
a7c49797
AM
11348 && r2off == 0
11349 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
721956f4
AM
11350 {
11351 struct ppc_branch_hash_entry *br_entry;
11352
11353 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11354 stub_entry->root.string + 9,
b34976b6 11355 TRUE, FALSE);
721956f4
AM
11356 if (br_entry == NULL)
11357 {
8de848d8 11358 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 11359 stub_entry->root.string);
b34976b6
AM
11360 htab->stub_error = TRUE;
11361 return FALSE;
721956f4
AM
11362 }
11363
11364 if (br_entry->iter != htab->stub_iteration)
11365 {
11366 br_entry->iter = htab->stub_iteration;
eea6121a
AM
11367 br_entry->offset = htab->brlt->size;
11368 htab->brlt->size += 8;
63bc6f6c 11369
ee75fd95 11370 if (htab->relbrlt != NULL)
eea6121a 11371 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
11372 else if (info->emitrelocations)
11373 {
11374 htab->brlt->reloc_count += 1;
11375 htab->brlt->flags |= SEC_RELOC;
11376 }
721956f4 11377 }
ad8e1ba5
AM
11378
11379 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
11380 off = (br_entry->offset
11381 + htab->brlt->output_offset
11382 + htab->brlt->output_section->vma
11383 - elf_gp (htab->brlt->output_section->owner)
6f20ed8a 11384 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
ac2df442 11385
176a0d42
AM
11386 if (info->emitrelocations)
11387 {
6f20ed8a
AM
11388 stub_entry->group->stub_sec->reloc_count
11389 += 1 + (PPC_HA (off) != 0);
11390 stub_entry->group->stub_sec->flags |= SEC_RELOC;
176a0d42
AM
11391 }
11392
00f412ee 11393 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ac2df442
AM
11394 {
11395 size = 12;
176a0d42 11396 if (PPC_HA (off) != 0)
ac2df442
AM
11397 size = 16;
11398 }
11399 else
11400 {
00f412ee 11401 size = 16;
176a0d42 11402 if (PPC_HA (off) != 0)
ac2df442
AM
11403 size += 4;
11404
11405 if (PPC_HA (r2off) != 0)
11406 size += 4;
00f412ee
AM
11407 if (PPC_LO (r2off) != 0)
11408 size += 4;
ac2df442 11409 }
721956f4 11410 }
84f5d08e
AM
11411 else if (info->emitrelocations)
11412 {
6f20ed8a
AM
11413 stub_entry->group->stub_sec->reloc_count += 1;
11414 stub_entry->group->stub_sec->flags |= SEC_RELOC;
84f5d08e 11415 }
721956f4
AM
11416 }
11417
6f20ed8a 11418 stub_entry->group->stub_sec->size += size;
b34976b6 11419 return TRUE;
721956f4
AM
11420}
11421
11422/* Set up various things so that we can make a list of input sections
11423 for each output section included in the link. Returns -1 on error,
cedb70c5 11424 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
11425
11426int
e7d1c40c 11427ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 11428{
6f20ed8a 11429 unsigned int id;
721956f4
AM
11430 bfd_size_type amt;
11431 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11432
4dfe6ac6
NC
11433 if (htab == NULL)
11434 return -1;
4c52953f 11435
6f20ed8a
AM
11436 htab->sec_info_arr_size = bfd_get_next_section_id ();
11437 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11438 htab->sec_info = bfd_zmalloc (amt);
11439 if (htab->sec_info == NULL)
721956f4
AM
11440 return -1;
11441
3d6f9012
AM
11442 /* Set toc_off for com, und, abs and ind sections. */
11443 for (id = 0; id < 3; id++)
6f20ed8a 11444 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 11445
721956f4
AM
11446 return 1;
11447}
11448
927be08e
AM
11449/* Set up for first pass at multitoc partitioning. */
11450
11451void
11452ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11453{
11454 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11455
1c865ab2 11456 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
11457 htab->toc_bfd = NULL;
11458 htab->toc_first_sec = NULL;
11459}
11460
e717da7e
AM
11461/* The linker repeatedly calls this function for each TOC input section
11462 and linker generated GOT section. Group input bfds such that the toc
927be08e 11463 within a group is less than 64k in size. */
ad8e1ba5 11464
927be08e 11465bfd_boolean
4ce794b7 11466ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
11467{
11468 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 11469 bfd_vma addr, off, limit;
ad8e1ba5 11470
4dfe6ac6
NC
11471 if (htab == NULL)
11472 return FALSE;
11473
927be08e 11474 if (!htab->second_toc_pass)
4c52953f 11475 {
927be08e 11476 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
11477 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11478
11479 if (new_bfd)
bf102f86
AM
11480 {
11481 htab->toc_bfd = isec->owner;
11482 htab->toc_first_sec = isec;
11483 }
927be08e 11484
bf102f86
AM
11485 addr = isec->output_offset + isec->output_section->vma;
11486 off = addr - htab->toc_curr;
d77c8a4b
AM
11487 limit = 0x80008000;
11488 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11489 limit = 0x10000;
11490 if (off + isec->size > limit)
bf102f86
AM
11491 {
11492 addr = (htab->toc_first_sec->output_offset
11493 + htab->toc_first_sec->output_section->vma);
11494 htab->toc_curr = addr;
a27e685f 11495 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 11496 }
99877b66 11497
927be08e
AM
11498 /* toc_curr is the base address of this toc group. Set elf_gp
11499 for the input section to be the offset relative to the
11500 output toc base plus 0x8000. Making the input elf_gp an
11501 offset allows us to move the toc as a whole without
11502 recalculating input elf_gp. */
11503 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11504 off += TOC_BASE_OFF;
11505
11506 /* Die if someone uses a linker script that doesn't keep input
11507 file .toc and .got together. */
a4fd3de5
AM
11508 if (new_bfd
11509 && elf_gp (isec->owner) != 0
927be08e
AM
11510 && elf_gp (isec->owner) != off)
11511 return FALSE;
11512
11513 elf_gp (isec->owner) = off;
11514 return TRUE;
4c52953f 11515 }
927be08e
AM
11516
11517 /* During the second pass toc_first_sec points to the start of
11518 a toc group, and toc_curr is used to track the old elf_gp.
11519 We use toc_bfd to ensure we only look at each bfd once. */
11520 if (htab->toc_bfd == isec->owner)
11521 return TRUE;
11522 htab->toc_bfd = isec->owner;
11523
11524 if (htab->toc_first_sec == NULL
11525 || htab->toc_curr != elf_gp (isec->owner))
11526 {
11527 htab->toc_curr = elf_gp (isec->owner);
11528 htab->toc_first_sec = isec;
11529 }
11530 addr = (htab->toc_first_sec->output_offset
11531 + htab->toc_first_sec->output_section->vma);
11532 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11533 elf_gp (isec->owner) = off;
11534
11535 return TRUE;
ad8e1ba5
AM
11536}
11537
927be08e
AM
11538/* Called via elf_link_hash_traverse to merge GOT entries for global
11539 symbol H. */
11540
11541static bfd_boolean
11542merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11543{
11544 if (h->root.type == bfd_link_hash_indirect)
11545 return TRUE;
11546
927be08e
AM
11547 merge_got_entries (&h->got.glist);
11548
11549 return TRUE;
11550}
11551
11552/* Called via elf_link_hash_traverse to allocate GOT entries for global
11553 symbol H. */
11554
11555static bfd_boolean
11556reallocate_got (struct elf_link_hash_entry *h, void *inf)
11557{
11558 struct got_entry *gent;
11559
11560 if (h->root.type == bfd_link_hash_indirect)
11561 return TRUE;
11562
927be08e
AM
11563 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11564 if (!gent->is_indirect)
11565 allocate_got (h, (struct bfd_link_info *) inf, gent);
11566 return TRUE;
11567}
11568
11569/* Called on the first multitoc pass after the last call to
11570 ppc64_elf_next_toc_section. This function removes duplicate GOT
11571 entries. */
11572
11573bfd_boolean
11574ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11575{
11576 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11577 struct bfd *ibfd, *ibfd2;
11578 bfd_boolean done_something;
11579
11580 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11581
7865406b
AM
11582 if (!htab->do_multi_toc)
11583 return FALSE;
11584
d0fae19d 11585 /* Merge global sym got entries within a toc group. */
927be08e
AM
11586 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11587
11588 /* And tlsld_got. */
c72f2fb2 11589 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11590 {
11591 struct got_entry *ent, *ent2;
11592
11593 if (!is_ppc64_elf (ibfd))
11594 continue;
11595
11596 ent = ppc64_tlsld_got (ibfd);
11597 if (!ent->is_indirect
11598 && ent->got.offset != (bfd_vma) -1)
11599 {
c72f2fb2 11600 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
11601 {
11602 if (!is_ppc64_elf (ibfd2))
11603 continue;
11604
11605 ent2 = ppc64_tlsld_got (ibfd2);
11606 if (!ent2->is_indirect
11607 && ent2->got.offset != (bfd_vma) -1
11608 && elf_gp (ibfd2) == elf_gp (ibfd))
11609 {
11610 ent2->is_indirect = TRUE;
11611 ent2->got.ent = ent;
11612 }
11613 }
11614 }
11615 }
11616
11617 /* Zap sizes of got sections. */
33e44f2e
AM
11618 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11619 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
11620 htab->got_reli_size = 0;
11621
c72f2fb2 11622 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11623 {
11624 asection *got, *relgot;
11625
11626 if (!is_ppc64_elf (ibfd))
11627 continue;
11628
11629 got = ppc64_elf_tdata (ibfd)->got;
11630 if (got != NULL)
11631 {
11632 got->rawsize = got->size;
11633 got->size = 0;
11634 relgot = ppc64_elf_tdata (ibfd)->relgot;
11635 relgot->rawsize = relgot->size;
11636 relgot->size = 0;
11637 }
11638 }
11639
11640 /* Now reallocate the got, local syms first. We don't need to
11641 allocate section contents again since we never increase size. */
c72f2fb2 11642 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11643 {
11644 struct got_entry **lgot_ents;
11645 struct got_entry **end_lgot_ents;
11646 struct plt_entry **local_plt;
11647 struct plt_entry **end_local_plt;
f961d9dd 11648 unsigned char *lgot_masks;
927be08e
AM
11649 bfd_size_type locsymcount;
11650 Elf_Internal_Shdr *symtab_hdr;
19e08130 11651 asection *s;
927be08e
AM
11652
11653 if (!is_ppc64_elf (ibfd))
11654 continue;
11655
11656 lgot_ents = elf_local_got_ents (ibfd);
11657 if (!lgot_ents)
11658 continue;
11659
11660 symtab_hdr = &elf_symtab_hdr (ibfd);
11661 locsymcount = symtab_hdr->sh_info;
11662 end_lgot_ents = lgot_ents + locsymcount;
11663 local_plt = (struct plt_entry **) end_lgot_ents;
11664 end_local_plt = local_plt + locsymcount;
f961d9dd 11665 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11666 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11667 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11668 {
11669 struct got_entry *ent;
11670
11671 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11672 {
19e08130
AM
11673 unsigned int ent_size = 8;
11674 unsigned int rel_size = sizeof (Elf64_External_Rela);
11675
d0fae19d
AM
11676 ent->got.offset = s->size;
11677 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11678 {
19e08130
AM
11679 ent_size *= 2;
11680 rel_size *= 2;
11681 }
11682 s->size += ent_size;
11683 if ((*lgot_masks & PLT_IFUNC) != 0)
11684 {
33e44f2e 11685 htab->elf.irelplt->size += rel_size;
19e08130
AM
11686 htab->got_reli_size += rel_size;
11687 }
0e1862bb 11688 else if (bfd_link_pic (info))
19e08130
AM
11689 {
11690 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11691 srel->size += rel_size;
d0fae19d
AM
11692 }
11693 }
927be08e
AM
11694 }
11695 }
11696
11697 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11698
c72f2fb2 11699 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11700 {
11701 struct got_entry *ent;
11702
11703 if (!is_ppc64_elf (ibfd))
11704 continue;
11705
11706 ent = ppc64_tlsld_got (ibfd);
11707 if (!ent->is_indirect
11708 && ent->got.offset != (bfd_vma) -1)
11709 {
11710 asection *s = ppc64_elf_tdata (ibfd)->got;
11711 ent->got.offset = s->size;
11712 s->size += 16;
0e1862bb 11713 if (bfd_link_pic (info))
927be08e
AM
11714 {
11715 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11716 srel->size += sizeof (Elf64_External_Rela);
11717 }
11718 }
11719 }
11720
33e44f2e 11721 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 11722 if (!done_something)
c72f2fb2 11723 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
11724 {
11725 asection *got;
11726
11727 if (!is_ppc64_elf (ibfd))
11728 continue;
11729
11730 got = ppc64_elf_tdata (ibfd)->got;
11731 if (got != NULL)
11732 {
11733 done_something = got->rawsize != got->size;
11734 if (done_something)
11735 break;
11736 }
11737 }
11738
11739 if (done_something)
e7d1c40c 11740 (*htab->params->layout_sections_again) ();
927be08e
AM
11741
11742 /* Set up for second pass over toc sections to recalculate elf_gp
11743 on input sections. */
11744 htab->toc_bfd = NULL;
11745 htab->toc_first_sec = NULL;
11746 htab->second_toc_pass = TRUE;
11747 return done_something;
11748}
11749
11750/* Called after second pass of multitoc partitioning. */
11751
11752void
11753ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11754{
11755 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11756
11757 /* After the second pass, toc_curr tracks the TOC offset used
11758 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11759 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11760}
11761
9b5ecbd0
AM
11762/* No toc references were found in ISEC. If the code in ISEC makes no
11763 calls, then there's no need to use toc adjusting stubs when branching
11764 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11765 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11766 needed, and 2 if a cyclical call-graph was found but no other reason
11767 for a stub was detected. If called from the top level, a return of
11768 2 means the same as a return of 0. */
9b5ecbd0
AM
11769
11770static int
4ce794b7 11771toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11772{
9b5ecbd0 11773 int ret;
70cc837d
AM
11774
11775 /* Mark this section as checked. */
11776 isec->call_check_done = 1;
9b5ecbd0 11777
772119ce
AM
11778 /* We know none of our code bearing sections will need toc stubs. */
11779 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11780 return 0;
11781
eea6121a 11782 if (isec->size == 0)
082c50f8
AM
11783 return 0;
11784
4c52953f
AM
11785 if (isec->output_section == NULL)
11786 return 0;
11787
4c52953f 11788 ret = 0;
70cc837d 11789 if (isec->reloc_count != 0)
9b5ecbd0 11790 {
70cc837d
AM
11791 Elf_Internal_Rela *relstart, *rel;
11792 Elf_Internal_Sym *local_syms;
11793 struct ppc_link_hash_table *htab;
2917689a 11794
70cc837d
AM
11795 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11796 info->keep_memory);
11797 if (relstart == NULL)
11798 return -1;
90aecf7a 11799
70cc837d
AM
11800 /* Look for branches to outside of this section. */
11801 local_syms = NULL;
11802 htab = ppc_hash_table (info);
11803 if (htab == NULL)
11804 return -1;
4c52953f 11805
70cc837d 11806 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11807 {
70cc837d
AM
11808 enum elf_ppc64_reloc_type r_type;
11809 unsigned long r_symndx;
11810 struct elf_link_hash_entry *h;
11811 struct ppc_link_hash_entry *eh;
11812 Elf_Internal_Sym *sym;
11813 asection *sym_sec;
11814 struct _opd_sec_data *opd;
11815 bfd_vma sym_value;
11816 bfd_vma dest;
11817
11818 r_type = ELF64_R_TYPE (rel->r_info);
11819 if (r_type != R_PPC64_REL24
11820 && r_type != R_PPC64_REL14
11821 && r_type != R_PPC64_REL14_BRTAKEN
11822 && r_type != R_PPC64_REL14_BRNTAKEN)
11823 continue;
4c52953f 11824
70cc837d
AM
11825 r_symndx = ELF64_R_SYM (rel->r_info);
11826 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11827 isec->owner))
4c52953f 11828 {
70cc837d
AM
11829 ret = -1;
11830 break;
11831 }
4c52953f 11832
70cc837d
AM
11833 /* Calls to dynamic lib functions go through a plt call stub
11834 that uses r2. */
11835 eh = (struct ppc_link_hash_entry *) h;
11836 if (eh != NULL
11837 && (eh->elf.plt.plist != NULL
11838 || (eh->oh != NULL
11839 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11840 {
11841 ret = 1;
11842 break;
4c52953f
AM
11843 }
11844
70cc837d
AM
11845 if (sym_sec == NULL)
11846 /* Ignore other undefined symbols. */
4c52953f 11847 continue;
4c52953f 11848
70cc837d
AM
11849 /* Assume branches to other sections not included in the
11850 link need stubs too, to cover -R and absolute syms. */
11851 if (sym_sec->output_section == NULL)
11852 {
11853 ret = 1;
11854 break;
11855 }
4c52953f 11856
70cc837d
AM
11857 if (h == NULL)
11858 sym_value = sym->st_value;
11859 else
11860 {
11861 if (h->root.type != bfd_link_hash_defined
11862 && h->root.type != bfd_link_hash_defweak)
11863 abort ();
11864 sym_value = h->root.u.def.value;
11865 }
11866 sym_value += rel->r_addend;
4c52953f 11867
70cc837d
AM
11868 /* If this branch reloc uses an opd sym, find the code section. */
11869 opd = get_opd_info (sym_sec);
11870 if (opd != NULL)
11871 {
11872 if (h == NULL && opd->adjust != NULL)
11873 {
11874 long adjust;
4c52953f 11875
92a9c616 11876 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
11877 if (adjust == -1)
11878 /* Assume deleted functions won't ever be called. */
11879 continue;
11880 sym_value += adjust;
11881 }
4c52953f 11882
aef36ac1
AM
11883 dest = opd_entry_value (sym_sec, sym_value,
11884 &sym_sec, NULL, FALSE);
70cc837d
AM
11885 if (dest == (bfd_vma) -1)
11886 continue;
11887 }
11888 else
11889 dest = (sym_value
11890 + sym_sec->output_offset
11891 + sym_sec->output_section->vma);
4c52953f 11892
70cc837d
AM
11893 /* Ignore branch to self. */
11894 if (sym_sec == isec)
11895 continue;
4c52953f 11896
70cc837d
AM
11897 /* If the called function uses the toc, we need a stub. */
11898 if (sym_sec->has_toc_reloc
11899 || sym_sec->makes_toc_func_call)
4c52953f 11900 {
70cc837d 11901 ret = 1;
4c52953f
AM
11902 break;
11903 }
70cc837d
AM
11904
11905 /* Assume any branch that needs a long branch stub might in fact
11906 need a plt_branch stub. A plt_branch stub uses r2. */
11907 else if (dest - (isec->output_offset
11908 + isec->output_section->vma
6911b7dc
AM
11909 + rel->r_offset) + (1 << 25)
11910 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11911 ? h->other
11912 : sym->st_other))
4c52953f 11913 {
70cc837d
AM
11914 ret = 1;
11915 break;
11916 }
11917
11918 /* If calling back to a section in the process of being
11919 tested, we can't say for sure that no toc adjusting stubs
11920 are needed, so don't return zero. */
11921 else if (sym_sec->call_check_in_progress)
11922 ret = 2;
11923
11924 /* Branches to another section that itself doesn't have any TOC
11925 references are OK. Recursively call ourselves to check. */
11926 else if (!sym_sec->call_check_done)
11927 {
11928 int recur;
11929
11930 /* Mark current section as indeterminate, so that other
11931 sections that call back to current won't be marked as
11932 known. */
11933 isec->call_check_in_progress = 1;
11934 recur = toc_adjusting_stub_needed (info, sym_sec);
11935 isec->call_check_in_progress = 0;
11936
4c52953f
AM
11937 if (recur != 0)
11938 {
70cc837d
AM
11939 ret = recur;
11940 if (recur != 2)
11941 break;
4c52953f
AM
11942 }
11943 }
4c52953f 11944 }
70cc837d
AM
11945
11946 if (local_syms != NULL
11947 && (elf_symtab_hdr (isec->owner).contents
11948 != (unsigned char *) local_syms))
11949 free (local_syms);
11950 if (elf_section_data (isec)->relocs != relstart)
11951 free (relstart);
9b5ecbd0
AM
11952 }
11953
70cc837d
AM
11954 if ((ret & 1) == 0
11955 && isec->map_head.s != NULL
11956 && (strcmp (isec->output_section->name, ".init") == 0
11957 || strcmp (isec->output_section->name, ".fini") == 0))
11958 {
11959 if (isec->map_head.s->has_toc_reloc
11960 || isec->map_head.s->makes_toc_func_call)
11961 ret = 1;
11962 else if (!isec->map_head.s->call_check_done)
11963 {
11964 int recur;
11965 isec->call_check_in_progress = 1;
11966 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11967 isec->call_check_in_progress = 0;
11968 if (recur != 0)
11969 ret = recur;
11970 }
11971 }
11972
11973 if (ret == 1)
11974 isec->makes_toc_func_call = 1;
4c52953f 11975
9b5ecbd0
AM
11976 return ret;
11977}
11978
721956f4
AM
11979/* The linker repeatedly calls this function for each input section,
11980 in the order that input sections are linked into output sections.
11981 Build lists of input sections to determine groupings between which
11982 we may insert linker stubs. */
11983
9b5ecbd0 11984bfd_boolean
4ce794b7 11985ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11986{
11987 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11988
4dfe6ac6
NC
11989 if (htab == NULL)
11990 return FALSE;
11991
734b6cf9 11992 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 11993 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 11994 {
3d6f9012
AM
11995 /* This happens to make the list in reverse order,
11996 which is what we want. */
6f20ed8a
AM
11997 htab->sec_info[isec->id].u.list
11998 = htab->sec_info[isec->output_section->id].u.list;
11999 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12000 }
ad8e1ba5 12001
4c52953f 12002 if (htab->multi_toc_needed)
9b5ecbd0 12003 {
8b974ba3
AM
12004 /* Analyse sections that aren't already flagged as needing a
12005 valid toc pointer. Exclude .fixup for the linux kernel.
12006 .fixup contains branches, but only back to the function that
12007 hit an exception. */
12008 if (!(isec->has_toc_reloc
12009 || (isec->flags & SEC_CODE) == 0
12010 || strcmp (isec->name, ".fixup") == 0
12011 || isec->call_check_done))
12012 {
12013 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12014 return FALSE;
8b974ba3
AM
12015 }
12016 /* Make all sections use the TOC assigned for this object file.
12017 This will be wrong for pasted sections; We fix that in
12018 check_pasted_section(). */
12019 if (elf_gp (isec->owner) != 0)
12020 htab->toc_curr = elf_gp (isec->owner);
12021 }
12022
6f20ed8a 12023 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12024 return TRUE;
721956f4
AM
12025}
12026
70cc837d
AM
12027/* Check that all .init and .fini sections use the same toc, if they
12028 have toc relocs. */
12029
12030static bfd_boolean
12031check_pasted_section (struct bfd_link_info *info, const char *name)
12032{
12033 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12034
12035 if (o != NULL)
12036 {
12037 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12038 bfd_vma toc_off = 0;
12039 asection *i;
12040
12041 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12042 if (i->has_toc_reloc)
12043 {
12044 if (toc_off == 0)
6f20ed8a
AM
12045 toc_off = htab->sec_info[i->id].toc_off;
12046 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
12047 return FALSE;
12048 }
6683a28d
AM
12049
12050 if (toc_off == 0)
12051 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12052 if (i->makes_toc_func_call)
12053 {
6f20ed8a 12054 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
12055 break;
12056 }
12057
70cc837d
AM
12058 /* Make sure the whole pasted function uses the same toc offset. */
12059 if (toc_off != 0)
12060 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 12061 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
12062 }
12063 return TRUE;
12064}
12065
12066bfd_boolean
12067ppc64_elf_check_init_fini (struct bfd_link_info *info)
12068{
12069 return (check_pasted_section (info, ".init")
12070 & check_pasted_section (info, ".fini"));
12071}
12072
721956f4
AM
12073/* See whether we can group stub sections together. Grouping stub
12074 sections may result in fewer stubs. More importantly, we need to
12075 put all .init* and .fini* stubs at the beginning of the .init or
12076 .fini output sections respectively, because glibc splits the
12077 _init and _fini functions into multiple parts. Putting a stub in
12078 the middle of a function is not a good idea. */
12079
6f20ed8a
AM
12080static bfd_boolean
12081group_sections (struct bfd_link_info *info,
4ce794b7
AM
12082 bfd_size_type stub_group_size,
12083 bfd_boolean stubs_always_before_branch)
721956f4 12084{
6f20ed8a
AM
12085 struct ppc_link_hash_table *htab;
12086 asection *osec;
7c8fe5c4
AM
12087 bfd_boolean suppress_size_errors;
12088
6f20ed8a
AM
12089 htab = ppc_hash_table (info);
12090 if (htab == NULL)
12091 return FALSE;
12092
7c8fe5c4 12093 suppress_size_errors = FALSE;
7c8fe5c4
AM
12094 if (stub_group_size == 1)
12095 {
12096 /* Default values. */
12097 if (stubs_always_before_branch)
09f92717 12098 stub_group_size = 0x1e00000;
7c8fe5c4 12099 else
09f92717 12100 stub_group_size = 0x1c00000;
7c8fe5c4
AM
12101 suppress_size_errors = TRUE;
12102 }
12103
6f20ed8a 12104 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 12105 {
6f20ed8a
AM
12106 asection *tail;
12107
12108 if (osec->id >= htab->sec_info_arr_size)
12109 continue;
12110
12111 tail = htab->sec_info[osec->id].u.list;
734b6cf9 12112 while (tail != NULL)
721956f4 12113 {
734b6cf9
AM
12114 asection *curr;
12115 asection *prev;
12116 bfd_size_type total;
12117 bfd_boolean big_sec;
12118 bfd_vma curr_toc;
6f20ed8a 12119 struct map_stub *group;
09f92717 12120 bfd_size_type group_size;
734b6cf9
AM
12121
12122 curr = tail;
eea6121a 12123 total = tail->size;
09f92717
AM
12124 group_size = (ppc64_elf_section_data (tail) != NULL
12125 && ppc64_elf_section_data (tail)->has_14bit_branch
12126 ? stub_group_size >> 10 : stub_group_size);
12127
12128 big_sec = total > group_size;
7c8fe5c4 12129 if (big_sec && !suppress_size_errors)
695344c0 12130 /* xgettext:c-format */
4eca0228
AM
12131 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12132 tail->owner, tail);
6f20ed8a 12133 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 12134
6f20ed8a 12135 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 12136 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
12137 < (ppc64_elf_section_data (prev) != NULL
12138 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12139 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12140 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12141 curr = prev;
12142
12143 /* OK, the size from the start of CURR to the end is less
09f92717 12144 than group_size and thus can be handled by one stub
734b6cf9 12145 section. (or the tail section is itself larger than
09f92717
AM
12146 group_size, in which case we may be toast.) We should
12147 really be keeping track of the total size of stubs added
12148 here, as stubs contribute to the final output section
12149 size. That's a little tricky, and this way will only
12150 break if stubs added make the total size more than 2^25,
12151 ie. for the default stub_group_size, if stubs total more
12152 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
12153 group = bfd_alloc (curr->owner, sizeof (*group));
12154 if (group == NULL)
12155 return FALSE;
12156 group->link_sec = curr;
12157 group->stub_sec = NULL;
a4b6fadd
AM
12158 group->needs_save_res = 0;
12159 group->next = htab->group;
12160 htab->group = group;
734b6cf9 12161 do
721956f4 12162 {
6f20ed8a 12163 prev = htab->sec_info[tail->id].u.list;
734b6cf9 12164 /* Set up this stub group. */
6f20ed8a 12165 htab->sec_info[tail->id].u.group = group;
721956f4 12166 }
734b6cf9
AM
12167 while (tail != curr && (tail = prev) != NULL);
12168
09f92717 12169 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
12170 bytes before the stub section can be handled by it too.
12171 Don't do this if we have a really large section after the
12172 stubs, as adding more stubs increases the chance that
12173 branches may not reach into the stub section. */
12174 if (!stubs_always_before_branch && !big_sec)
12175 {
12176 total = 0;
12177 while (prev != NULL
12178 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
12179 < (ppc64_elf_section_data (prev) != NULL
12180 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 12181 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 12182 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
12183 {
12184 tail = prev;
6f20ed8a
AM
12185 prev = htab->sec_info[tail->id].u.list;
12186 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
12187 }
12188 }
12189 tail = prev;
721956f4
AM
12190 }
12191 }
6f20ed8a 12192 return TRUE;
721956f4
AM
12193}
12194
58d180e8
AM
12195static const unsigned char glink_eh_frame_cie[] =
12196{
12197 0, 0, 0, 16, /* length. */
12198 0, 0, 0, 0, /* id. */
12199 1, /* CIE version. */
12200 'z', 'R', 0, /* Augmentation string. */
12201 4, /* Code alignment. */
12202 0x78, /* Data alignment. */
12203 65, /* RA reg. */
12204 1, /* Augmentation size. */
12205 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
da44f4e5
AM
12206 DW_CFA_def_cfa, 1, 0, /* def_cfa: r1 offset 0. */
12207 0, 0, 0, 0
58d180e8
AM
12208};
12209
d969d15f
AM
12210/* Stripping output sections is normally done before dynamic section
12211 symbols have been allocated. This function is called later, and
12212 handles cases like htab->brlt which is mapped to its own output
12213 section. */
12214
12215static void
12216maybe_strip_output (struct bfd_link_info *info, asection *isec)
12217{
12218 if (isec->size == 0
12219 && isec->output_section->size == 0
53d8967a 12220 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
12221 && !bfd_section_removed_from_list (info->output_bfd,
12222 isec->output_section)
12223 && elf_section_data (isec->output_section)->dynindx == 0)
12224 {
12225 isec->output_section->flags |= SEC_EXCLUDE;
12226 bfd_section_list_remove (info->output_bfd, isec->output_section);
12227 info->output_bfd->section_count--;
12228 }
12229}
12230
721956f4
AM
12231/* Determine and set the size of the stub section for a final link.
12232
12233 The basic idea here is to examine all the relocations looking for
12234 PC-relative calls to a target that is unreachable with a "bl"
12235 instruction. */
12236
b34976b6 12237bfd_boolean
e7d1c40c 12238ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
12239{
12240 bfd_size_type stub_group_size;
b34976b6 12241 bfd_boolean stubs_always_before_branch;
721956f4
AM
12242 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12243
4dfe6ac6
NC
12244 if (htab == NULL)
12245 return FALSE;
12246
0e1862bb 12247 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 12248 htab->params->plt_thread_safe = 1;
b9e5796b 12249 if (!htab->opd_abi)
e7d1c40c
AM
12250 htab->params->plt_thread_safe = 0;
12251 else if (htab->params->plt_thread_safe == -1)
794e51c0 12252 {
e2458743 12253 static const char *const thread_starter[] =
794e51c0
AM
12254 {
12255 "pthread_create",
12256 /* libstdc++ */
12257 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12258 /* librt */
12259 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12260 "mq_notify", "create_timer",
12261 /* libanl */
12262 "getaddrinfo_a",
12263 /* libgomp */
2300b5a1 12264 "GOMP_parallel",
794e51c0 12265 "GOMP_parallel_start",
2300b5a1 12266 "GOMP_parallel_loop_static",
794e51c0 12267 "GOMP_parallel_loop_static_start",
2300b5a1 12268 "GOMP_parallel_loop_dynamic",
794e51c0 12269 "GOMP_parallel_loop_dynamic_start",
2300b5a1 12270 "GOMP_parallel_loop_guided",
794e51c0 12271 "GOMP_parallel_loop_guided_start",
2300b5a1 12272 "GOMP_parallel_loop_runtime",
794e51c0 12273 "GOMP_parallel_loop_runtime_start",
2300b5a1 12274 "GOMP_parallel_sections",
68ffbac6 12275 "GOMP_parallel_sections_start",
f9dffbf0
AM
12276 /* libgo */
12277 "__go_go",
794e51c0
AM
12278 };
12279 unsigned i;
12280
a4b6fadd 12281 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
12282 {
12283 struct elf_link_hash_entry *h;
12284 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12285 FALSE, FALSE, TRUE);
e7d1c40c
AM
12286 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12287 if (htab->params->plt_thread_safe)
794e51c0
AM
12288 break;
12289 }
12290 }
e7d1c40c
AM
12291 stubs_always_before_branch = htab->params->group_size < 0;
12292 if (htab->params->group_size < 0)
12293 stub_group_size = -htab->params->group_size;
721956f4 12294 else
e7d1c40c 12295 stub_group_size = htab->params->group_size;
721956f4 12296
6f20ed8a
AM
12297 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12298 return FALSE;
721956f4 12299
c9301e31
AM
12300#define STUB_SHRINK_ITER 20
12301 /* Loop until no stubs added. After iteration 20 of this loop we may
12302 exit on a stub section shrinking. This is to break out of a
12303 pathological case where adding stubs on one iteration decreases
12304 section gaps (perhaps due to alignment), which then requires
12305 fewer or smaller stubs on the next iteration. */
12306
721956f4
AM
12307 while (1)
12308 {
12309 bfd *input_bfd;
12310 unsigned int bfd_indx;
a4b6fadd 12311 struct map_stub *group;
721956f4 12312 asection *stub_sec;
721956f4
AM
12313
12314 htab->stub_iteration += 1;
721956f4
AM
12315
12316 for (input_bfd = info->input_bfds, bfd_indx = 0;
12317 input_bfd != NULL;
c72f2fb2 12318 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
12319 {
12320 Elf_Internal_Shdr *symtab_hdr;
12321 asection *section;
6cdc0ccc 12322 Elf_Internal_Sym *local_syms = NULL;
721956f4 12323
0c8d6e5c 12324 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
12325 continue;
12326
721956f4 12327 /* We'll need the symbol table in a second. */
0ffa91dd 12328 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
12329 if (symtab_hdr->sh_info == 0)
12330 continue;
12331
721956f4
AM
12332 /* Walk over each section attached to the input bfd. */
12333 for (section = input_bfd->sections;
12334 section != NULL;
12335 section = section->next)
12336 {
721956f4 12337 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
12338
12339 /* If there aren't any relocs, then there's nothing more
12340 to do. */
12341 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
12342 || (section->flags & SEC_ALLOC) == 0
12343 || (section->flags & SEC_LOAD) == 0
12344 || (section->flags & SEC_CODE) == 0
721956f4
AM
12345 || section->reloc_count == 0)
12346 continue;
12347
12348 /* If this section is a link-once section that will be
12349 discarded, then don't create any stubs. */
12350 if (section->output_section == NULL
927be08e 12351 || section->output_section->owner != info->output_bfd)
721956f4
AM
12352 continue;
12353
1e2f5b6e
AM
12354 /* Get the relocs. */
12355 internal_relocs
4ce794b7 12356 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 12357 info->keep_memory);
721956f4 12358 if (internal_relocs == NULL)
1e2f5b6e 12359 goto error_ret_free_local;
721956f4
AM
12360
12361 /* Now examine each relocation. */
12362 irela = internal_relocs;
12363 irelaend = irela + section->reloc_count;
12364 for (; irela < irelaend; irela++)
12365 {
4ce794b7
AM
12366 enum elf_ppc64_reloc_type r_type;
12367 unsigned int r_indx;
721956f4
AM
12368 enum ppc_stub_type stub_type;
12369 struct ppc_stub_hash_entry *stub_entry;
8387904d 12370 asection *sym_sec, *code_sec;
e054468f 12371 bfd_vma sym_value, code_value;
721956f4 12372 bfd_vma destination;
6911b7dc 12373 unsigned long local_off;
8843416a 12374 bfd_boolean ok_dest;
721956f4 12375 struct ppc_link_hash_entry *hash;
8387904d 12376 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
12377 struct elf_link_hash_entry *h;
12378 Elf_Internal_Sym *sym;
721956f4
AM
12379 char *stub_name;
12380 const asection *id_sec;
74f0fb50 12381 struct _opd_sec_data *opd;
e054468f 12382 struct plt_entry *plt_ent;
721956f4
AM
12383
12384 r_type = ELF64_R_TYPE (irela->r_info);
12385 r_indx = ELF64_R_SYM (irela->r_info);
12386
4ce794b7 12387 if (r_type >= R_PPC64_max)
721956f4
AM
12388 {
12389 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 12390 goto error_ret_free_internal;
721956f4
AM
12391 }
12392
12393 /* Only look for stubs on branch instructions. */
4ce794b7
AM
12394 if (r_type != R_PPC64_REL24
12395 && r_type != R_PPC64_REL14
12396 && r_type != R_PPC64_REL14_BRTAKEN
12397 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
12398 continue;
12399
12400 /* Now determine the call target, its name, value,
12401 section. */
411e1bfb
AM
12402 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12403 r_indx, input_bfd))
12404 goto error_ret_free_internal;
12405 hash = (struct ppc_link_hash_entry *) h;
12406
8843416a 12407 ok_dest = FALSE;
8387904d 12408 fdh = NULL;
7fe2b9a6 12409 sym_value = 0;
411e1bfb 12410 if (hash == NULL)
721956f4 12411 {
411e1bfb 12412 sym_value = sym->st_value;
c27b8c2a
AM
12413 if (sym_sec != NULL
12414 && sym_sec->output_section != NULL)
12415 ok_dest = TRUE;
721956f4 12416 }
7fe2b9a6
AM
12417 else if (hash->elf.root.type == bfd_link_hash_defined
12418 || hash->elf.root.type == bfd_link_hash_defweak)
12419 {
12420 sym_value = hash->elf.root.u.def.value;
12421 if (sym_sec->output_section != NULL)
12422 ok_dest = TRUE;
12423 }
12424 else if (hash->elf.root.type == bfd_link_hash_undefweak
12425 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 12426 {
99877b66 12427 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
12428 use the func descriptor sym instead if it is
12429 defined. */
ceb1f1ef 12430 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 12431 && hash->oh != NULL)
8387904d 12432 {
8c5b4e52 12433 fdh = ppc_follow_link (hash->oh);
8387904d
AM
12434 if (fdh->elf.root.type == bfd_link_hash_defined
12435 || fdh->elf.root.type == bfd_link_hash_defweak)
12436 {
12437 sym_sec = fdh->elf.root.u.def.section;
12438 sym_value = fdh->elf.root.u.def.value;
12439 if (sym_sec->output_section != NULL)
12440 ok_dest = TRUE;
12441 }
99877b66
AM
12442 else
12443 fdh = NULL;
8387904d 12444 }
7fe2b9a6
AM
12445 }
12446 else
12447 {
12448 bfd_set_error (bfd_error_bad_value);
12449 goto error_ret_free_internal;
721956f4
AM
12450 }
12451
8843416a 12452 destination = 0;
6911b7dc 12453 local_off = 0;
8843416a
AM
12454 if (ok_dest)
12455 {
12456 sym_value += irela->r_addend;
12457 destination = (sym_value
12458 + sym_sec->output_offset
12459 + sym_sec->output_section->vma);
6911b7dc
AM
12460 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12461 ? hash->elf.other
12462 : sym->st_other);
8843416a
AM
12463 }
12464
8387904d 12465 code_sec = sym_sec;
e054468f 12466 code_value = sym_value;
74f0fb50
AM
12467 opd = get_opd_info (sym_sec);
12468 if (opd != NULL)
8387904d
AM
12469 {
12470 bfd_vma dest;
12471
74f0fb50 12472 if (hash == NULL && opd->adjust != NULL)
8387904d 12473 {
51aecdc5 12474 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
12475 if (adjust == -1)
12476 continue;
e054468f 12477 code_value += adjust;
8387904d
AM
12478 sym_value += adjust;
12479 }
12480 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 12481 &code_sec, &code_value, FALSE);
8387904d
AM
12482 if (dest != (bfd_vma) -1)
12483 {
12484 destination = dest;
12485 if (fdh != NULL)
12486 {
12487 /* Fixup old ABI sym to point at code
12488 entry. */
99877b66 12489 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 12490 hash->elf.root.u.def.section = code_sec;
e054468f 12491 hash->elf.root.u.def.value = code_value;
8387904d
AM
12492 }
12493 }
12494 }
12495
721956f4 12496 /* Determine what (if any) linker stub is needed. */
e054468f 12497 plt_ent = NULL;
721956f4 12498 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
12499 &plt_ent, destination,
12500 local_off);
ad8e1ba5
AM
12501
12502 if (stub_type != ppc_stub_plt_call)
12503 {
12504 /* Check whether we need a TOC adjusting stub.
12505 Since the linker pastes together pieces from
12506 different object files when creating the
12507 _init and _fini functions, it may be that a
12508 call to what looks like a local sym is in
12509 fact a call needing a TOC adjustment. */
8387904d
AM
12510 if (code_sec != NULL
12511 && code_sec->output_section != NULL
6f20ed8a
AM
12512 && (htab->sec_info[code_sec->id].toc_off
12513 != htab->sec_info[section->id].toc_off)
4c52953f
AM
12514 && (code_sec->has_toc_reloc
12515 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
12516 stub_type = ppc_stub_long_branch_r2off;
12517 }
12518
721956f4
AM
12519 if (stub_type == ppc_stub_none)
12520 continue;
12521
411e1bfb
AM
12522 /* __tls_get_addr calls might be eliminated. */
12523 if (stub_type != ppc_stub_plt_call
12524 && hash != NULL
8387904d
AM
12525 && (hash == htab->tls_get_addr
12526 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
12527 && section->has_tls_reloc
12528 && irela != internal_relocs)
12529 {
12530 /* Get tls info. */
f961d9dd 12531 unsigned char *tls_mask;
411e1bfb 12532
3a71aa26 12533 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
12534 irela - 1, input_bfd))
12535 goto error_ret_free_internal;
e7b938ca 12536 if (*tls_mask != 0)
411e1bfb
AM
12537 continue;
12538 }
12539
3b421ab3
AM
12540 if (stub_type == ppc_stub_plt_call
12541 && irela + 1 < irelaend
12542 && irela[1].r_offset == irela->r_offset + 4
794e51c0
AM
12543 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12544 {
12545 if (!tocsave_find (htab, INSERT,
12546 &local_syms, irela + 1, input_bfd))
12547 goto error_ret_free_internal;
12548 }
12549 else if (stub_type == ppc_stub_plt_call)
12550 stub_type = ppc_stub_plt_call_r2save;
3b421ab3 12551
721956f4 12552 /* Support for grouping stub sections. */
6f20ed8a 12553 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
12554
12555 /* Get the name of this stub. */
12556 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12557 if (!stub_name)
12558 goto error_ret_free_internal;
12559
12560 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 12561 stub_name, FALSE, FALSE);
721956f4
AM
12562 if (stub_entry != NULL)
12563 {
12564 /* The proper stub has already been created. */
12565 free (stub_name);
794e51c0
AM
12566 if (stub_type == ppc_stub_plt_call_r2save)
12567 stub_entry->stub_type = stub_type;
721956f4
AM
12568 continue;
12569 }
12570
25f53a85 12571 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12572 if (stub_entry == NULL)
12573 {
12574 free (stub_name);
6cdc0ccc
AM
12575 error_ret_free_internal:
12576 if (elf_section_data (section)->relocs == NULL)
12577 free (internal_relocs);
12578 error_ret_free_local:
12579 if (local_syms != NULL
12580 && (symtab_hdr->contents
12581 != (unsigned char *) local_syms))
12582 free (local_syms);
b34976b6 12583 return FALSE;
721956f4
AM
12584 }
12585
ad8e1ba5 12586 stub_entry->stub_type = stub_type;
794e51c0
AM
12587 if (stub_type != ppc_stub_plt_call
12588 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12589 {
12590 stub_entry->target_value = code_value;
12591 stub_entry->target_section = code_sec;
12592 }
12593 else
12594 {
12595 stub_entry->target_value = sym_value;
12596 stub_entry->target_section = sym_sec;
12597 }
721956f4 12598 stub_entry->h = hash;
e054468f 12599 stub_entry->plt_ent = plt_ent;
6911b7dc 12600 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12601
12602 if (stub_entry->h != NULL)
12603 htab->stub_globals += 1;
721956f4
AM
12604 }
12605
12606 /* We're done with the internal relocs, free them. */
6cdc0ccc 12607 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12608 free (internal_relocs);
721956f4 12609 }
6cdc0ccc
AM
12610
12611 if (local_syms != NULL
12612 && symtab_hdr->contents != (unsigned char *) local_syms)
12613 {
12614 if (!info->keep_memory)
12615 free (local_syms);
12616 else
12617 symtab_hdr->contents = (unsigned char *) local_syms;
12618 }
721956f4
AM
12619 }
12620
5c3dead3 12621 /* We may have added some stubs. Find out the new size of the
721956f4 12622 stub sections. */
e7d1c40c 12623 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
12624 stub_sec != NULL;
12625 stub_sec = stub_sec->next)
e717da7e 12626 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 12627 {
ea3d7d1c
AM
12628 if (htab->stub_iteration <= STUB_SHRINK_ITER
12629 || stub_sec->rawsize < stub_sec->size)
12630 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12631 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12632 stub_sec->size = 0;
12633 stub_sec->reloc_count = 0;
84f5d08e 12634 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12635 }
eea6121a
AM
12636
12637 htab->brlt->size = 0;
84f5d08e
AM
12638 htab->brlt->reloc_count = 0;
12639 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12640 if (htab->relbrlt != NULL)
eea6121a 12641 htab->relbrlt->size = 0;
721956f4 12642
63bc6f6c 12643 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12644
a4b6fadd
AM
12645 for (group = htab->group; group != NULL; group = group->next)
12646 if (group->needs_save_res)
12647 group->stub_sec->size += htab->sfpr->size;
12648
176a0d42
AM
12649 if (info->emitrelocations
12650 && htab->glink != NULL && htab->glink->size != 0)
12651 {
12652 htab->glink->reloc_count = 1;
12653 htab->glink->flags |= SEC_RELOC;
12654 }
12655
58d180e8
AM
12656 if (htab->glink_eh_frame != NULL
12657 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
9a2a56cc 12658 && htab->glink_eh_frame->output_section->size != 0)
58d180e8 12659 {
4bbe044a 12660 size_t size = 0, align;
58d180e8 12661
e7d1c40c 12662 for (stub_sec = htab->params->stub_bfd->sections;
58d180e8
AM
12663 stub_sec != NULL;
12664 stub_sec = stub_sec->next)
12665 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
da44f4e5 12666 size += 24;
58d180e8
AM
12667 if (htab->glink != NULL && htab->glink->size != 0)
12668 size += 24;
12669 if (size != 0)
12670 size += sizeof (glink_eh_frame_cie);
4bbe044a
AM
12671 align = 1;
12672 align <<= htab->glink_eh_frame->output_section->alignment_power;
12673 align -= 1;
12674 size = (size + align) & ~align;
58d180e8
AM
12675 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12676 htab->glink_eh_frame->size = size;
12677 }
12678
e7d1c40c
AM
12679 if (htab->params->plt_stub_align != 0)
12680 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
12681 stub_sec != NULL;
12682 stub_sec = stub_sec->next)
12683 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
12684 stub_sec->size = ((stub_sec->size
12685 + (1 << htab->params->plt_stub_align) - 1)
29f628db 12686 & -(1 << htab->params->plt_stub_align));
794e51c0 12687
e7d1c40c 12688 for (stub_sec = htab->params->stub_bfd->sections;
5c3dead3
AM
12689 stub_sec != NULL;
12690 stub_sec = stub_sec->next)
12691 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
c9301e31
AM
12692 && stub_sec->rawsize != stub_sec->size
12693 && (htab->stub_iteration <= STUB_SHRINK_ITER
12694 || stub_sec->rawsize < stub_sec->size))
5c3dead3
AM
12695 break;
12696
58d180e8
AM
12697 if (stub_sec == NULL
12698 && (htab->glink_eh_frame == NULL
12699 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12700 break;
12701
721956f4 12702 /* Ask the linker to do its stuff. */
e7d1c40c 12703 (*htab->params->layout_sections_again) ();
721956f4
AM
12704 }
12705
da44f4e5
AM
12706 if (htab->glink_eh_frame != NULL
12707 && htab->glink_eh_frame->size != 0)
12708 {
12709 bfd_vma val;
12710 bfd_byte *p, *last_fde;
12711 size_t last_fde_len, size, align, pad;
12712 asection *stub_sec;
12713
12714 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12715 if (p == NULL)
12716 return FALSE;
12717 htab->glink_eh_frame->contents = p;
12718 last_fde = p;
12719
12720 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12721 /* CIE length (rewrite in case little-endian). */
12722 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12723 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12724 p += sizeof (glink_eh_frame_cie);
12725
12726 for (stub_sec = htab->params->stub_bfd->sections;
12727 stub_sec != NULL;
12728 stub_sec = stub_sec->next)
12729 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12730 {
12731 last_fde = p;
12732 last_fde_len = 20;
12733 /* FDE length. */
12734 bfd_put_32 (htab->elf.dynobj, 20, p);
12735 p += 4;
12736 /* CIE pointer. */
12737 val = p - htab->glink_eh_frame->contents;
12738 bfd_put_32 (htab->elf.dynobj, val, p);
12739 p += 4;
12740 /* Offset to stub section, written later. */
12741 p += 4;
12742 /* stub section size. */
12743 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12744 p += 4;
12745 /* Augmentation. */
12746 p += 1;
12747 /* Pad. */
12748 p += 7;
12749 }
12750 if (htab->glink != NULL && htab->glink->size != 0)
12751 {
12752 last_fde = p;
12753 last_fde_len = 20;
12754 /* FDE length. */
12755 bfd_put_32 (htab->elf.dynobj, 20, p);
12756 p += 4;
12757 /* CIE pointer. */
12758 val = p - htab->glink_eh_frame->contents;
12759 bfd_put_32 (htab->elf.dynobj, val, p);
12760 p += 4;
12761 /* Offset to .glink, written later. */
12762 p += 4;
12763 /* .glink size. */
12764 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12765 p += 4;
12766 /* Augmentation. */
12767 p += 1;
12768
12769 *p++ = DW_CFA_advance_loc + 1;
12770 *p++ = DW_CFA_register;
12771 *p++ = 65;
9f08fa5c 12772 *p++ = htab->opd_abi ? 12 : 0;
da44f4e5
AM
12773 *p++ = DW_CFA_advance_loc + 4;
12774 *p++ = DW_CFA_restore_extended;
12775 *p++ = 65;
12776 }
12777 /* Subsume any padding into the last FDE if user .eh_frame
12778 sections are aligned more than glink_eh_frame. Otherwise any
12779 zero padding will be seen as a terminator. */
12780 size = p - htab->glink_eh_frame->contents;
12781 align = 1;
12782 align <<= htab->glink_eh_frame->output_section->alignment_power;
12783 align -= 1;
12784 pad = ((size + align) & ~align) - size;
12785 htab->glink_eh_frame->size = size + pad;
12786 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12787 }
12788
d969d15f
AM
12789 maybe_strip_output (info, htab->brlt);
12790 if (htab->glink_eh_frame != NULL)
12791 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12792
b34976b6 12793 return TRUE;
721956f4
AM
12794}
12795
12796/* Called after we have determined section placement. If sections
805fc799 12797 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12798
805fc799 12799bfd_vma
1c865ab2 12800ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12801{
805fc799 12802 asection *s;
a27e685f 12803 bfd_vma TOCstart, adjust;
721956f4 12804
43417696
AM
12805 if (info != NULL)
12806 {
12807 struct elf_link_hash_entry *h;
12808 struct elf_link_hash_table *htab = elf_hash_table (info);
12809
12810 if (is_elf_hash_table (htab)
12811 && htab->hgot != NULL)
12812 h = htab->hgot;
12813 else
12814 {
12815 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12816 if (is_elf_hash_table (htab))
12817 htab->hgot = h;
12818 }
12819 if (h != NULL
12820 && h->root.type == bfd_link_hash_defined
12821 && !h->root.linker_def
12822 && (!is_elf_hash_table (htab)
12823 || h->def_regular))
12824 {
12825 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12826 + h->root.u.def.section->output_offset
12827 + h->root.u.def.section->output_section->vma);
12828 _bfd_set_gp_value (obfd, TOCstart);
12829 return TOCstart;
12830 }
12831 }
12832
805fc799
AM
12833 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12834 order. The TOC starts where the first of these sections starts. */
12835 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12836 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12837 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12838 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12839 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12840 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12841 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12842 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12843 {
12844 /* This may happen for
12845 o references to TOC base (SYM@toc / TOC[tc0]) without a
12846 .toc directive
12847 o bad linker script
12848 o --gc-sections and empty TOC sections
12849
12850 FIXME: Warn user? */
12851
12852 /* Look for a likely section. We probably won't even be
12853 using TOCstart. */
12854 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12855 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12856 | SEC_EXCLUDE))
805fc799
AM
12857 == (SEC_ALLOC | SEC_SMALL_DATA))
12858 break;
721956f4 12859 if (s == NULL)
805fc799 12860 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12861 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12862 == (SEC_ALLOC | SEC_SMALL_DATA))
12863 break;
721956f4 12864 if (s == NULL)
805fc799 12865 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12866 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12867 == SEC_ALLOC)
805fc799 12868 break;
721956f4 12869 if (s == NULL)
805fc799 12870 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12871 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12872 break;
12873 }
721956f4 12874
805fc799
AM
12875 TOCstart = 0;
12876 if (s != NULL)
12877 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12878
a27e685f
AM
12879 /* Force alignment. */
12880 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12881 TOCstart -= adjust;
1c865ab2
AM
12882 _bfd_set_gp_value (obfd, TOCstart);
12883
810d4e75 12884 if (info != NULL && s != NULL)
1c865ab2
AM
12885 {
12886 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12887
810d4e75
AM
12888 if (htab != NULL)
12889 {
12890 if (htab->elf.hgot != NULL)
12891 {
a27e685f 12892 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
12893 htab->elf.hgot->root.u.def.section = s;
12894 }
12895 }
12896 else
1c865ab2 12897 {
810d4e75
AM
12898 struct bfd_link_hash_entry *bh = NULL;
12899 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
12900 s, TOC_BASE_OFF - adjust,
12901 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
12902 }
12903 }
805fc799 12904 return TOCstart;
721956f4
AM
12905}
12906
a345bc8d
AM
12907/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12908 write out any global entry stubs. */
12909
12910static bfd_boolean
12911build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12912{
12913 struct bfd_link_info *info;
12914 struct ppc_link_hash_table *htab;
12915 struct plt_entry *pent;
12916 asection *s;
12917
12918 if (h->root.type == bfd_link_hash_indirect)
12919 return TRUE;
12920
12921 if (!h->pointer_equality_needed)
12922 return TRUE;
12923
12924 if (h->def_regular)
12925 return TRUE;
12926
12927 info = inf;
12928 htab = ppc_hash_table (info);
12929 if (htab == NULL)
12930 return FALSE;
12931
12932 s = htab->glink;
12933 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12934 if (pent->plt.offset != (bfd_vma) -1
12935 && pent->addend == 0)
12936 {
12937 bfd_byte *p;
12938 asection *plt;
12939 bfd_vma off;
12940
a345bc8d 12941 p = s->contents + h->root.u.def.value;
33e44f2e 12942 plt = htab->elf.splt;
a345bc8d
AM
12943 if (!htab->elf.dynamic_sections_created
12944 || h->dynindx == -1)
33e44f2e 12945 plt = htab->elf.iplt;
a345bc8d
AM
12946 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12947 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12948
12949 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12950 {
12951 info->callbacks->einfo
12952 (_("%P: linkage table error against `%T'\n"),
12953 h->root.root.string);
12954 bfd_set_error (bfd_error_bad_value);
12955 htab->stub_error = TRUE;
12956 }
12957
7341d5e2
AM
12958 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12959 if (htab->params->emit_stub_syms)
12960 {
12961 size_t len = strlen (h->root.root.string);
12962 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12963
12964 if (name == NULL)
12965 return FALSE;
12966
12967 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12968 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12969 if (h == NULL)
12970 return FALSE;
12971 if (h->root.type == bfd_link_hash_new)
12972 {
12973 h->root.type = bfd_link_hash_defined;
12974 h->root.u.def.section = s;
12975 h->root.u.def.value = p - s->contents;
12976 h->ref_regular = 1;
12977 h->def_regular = 1;
12978 h->ref_regular_nonweak = 1;
12979 h->forced_local = 1;
12980 h->non_elf = 0;
2ec55de3 12981 h->root.linker_def = 1;
7341d5e2
AM
12982 }
12983 }
12984
a345bc8d
AM
12985 if (PPC_HA (off) != 0)
12986 {
12987 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12988 p += 4;
12989 }
12990 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12991 p += 4;
12992 bfd_put_32 (s->owner, MTCTR_R12, p);
12993 p += 4;
12994 bfd_put_32 (s->owner, BCTR, p);
12995 break;
12996 }
12997 return TRUE;
12998}
12999
721956f4
AM
13000/* Build all the stubs associated with the current output file.
13001 The stubs are kept in a hash table attached to the main linker
13002 hash table. This function is called via gldelf64ppc_finish. */
13003
b34976b6 13004bfd_boolean
e7d1c40c 13005ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 13006 char **stats)
5d1634d7
AM
13007{
13008 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 13009 struct map_stub *group;
721956f4 13010 asection *stub_sec;
5d1634d7 13011 bfd_byte *p;
e717da7e 13012 int stub_sec_count = 0;
5d1634d7 13013
4dfe6ac6
NC
13014 if (htab == NULL)
13015 return FALSE;
13016
eea6121a 13017 /* Allocate memory to hold the linker stubs. */
e7d1c40c 13018 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
13019 stub_sec != NULL;
13020 stub_sec = stub_sec->next)
eea6121a
AM
13021 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
13022 && stub_sec->size != 0)
e717da7e 13023 {
e7d1c40c 13024 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
eea6121a
AM
13025 if (stub_sec->contents == NULL)
13026 return FALSE;
eea6121a 13027 stub_sec->size = 0;
e717da7e 13028 }
5d1634d7 13029
23eb7e01 13030 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 13031 {
9f951329 13032 unsigned int indx;
ad8e1ba5 13033 bfd_vma plt0;
9f951329 13034
721956f4 13035 /* Build the .glink plt call stub. */
e7d1c40c 13036 if (htab->params->emit_stub_syms)
97b639ba
AM
13037 {
13038 struct elf_link_hash_entry *h;
468392fb
AM
13039 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13040 TRUE, FALSE, FALSE);
97b639ba
AM
13041 if (h == NULL)
13042 return FALSE;
13043 if (h->root.type == bfd_link_hash_new)
13044 {
13045 h->root.type = bfd_link_hash_defined;
13046 h->root.u.def.section = htab->glink;
ee4bf8d2 13047 h->root.u.def.value = 8;
f5385ebf
AM
13048 h->ref_regular = 1;
13049 h->def_regular = 1;
13050 h->ref_regular_nonweak = 1;
13051 h->forced_local = 1;
13052 h->non_elf = 0;
2ec55de3 13053 h->root.linker_def = 1;
97b639ba
AM
13054 }
13055 }
33e44f2e
AM
13056 plt0 = (htab->elf.splt->output_section->vma
13057 + htab->elf.splt->output_offset
13058 - 16);
176a0d42
AM
13059 if (info->emitrelocations)
13060 {
13061 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13062 if (r == NULL)
13063 return FALSE;
13064 r->r_offset = (htab->glink->output_offset
13065 + htab->glink->output_section->vma);
13066 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13067 r->r_addend = plt0;
13068 }
4ce794b7 13069 p = htab->glink->contents;
176a0d42 13070 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
13071 bfd_put_64 (htab->glink->owner, plt0, p);
13072 p += 8;
b9e5796b
AM
13073 if (htab->opd_abi)
13074 {
13075 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13076 p += 4;
13077 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13078 p += 4;
13079 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13080 p += 4;
13081 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13082 p += 4;
13083 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13084 p += 4;
13085 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13086 p += 4;
13087 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13088 p += 4;
13089 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13090 p += 4;
13091 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13092 p += 4;
13093 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13094 p += 4;
13095 }
13096 else
13097 {
13098 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13099 p += 4;
13100 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13101 p += 4;
13102 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13103 p += 4;
13104 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13105 p += 4;
13106 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13107 p += 4;
13108 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13109 p += 4;
13110 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13111 p += 4;
13112 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), 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, SRDI_R0_R0_2, 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 | 8, p);
13121 p += 4;
13122 }
4ce794b7 13123 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 13124 p += 4;
ee4bf8d2
AM
13125 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13126 {
13127 bfd_put_32 (htab->glink->owner, NOP, p);
13128 p += 4;
13129 }
ad8e1ba5 13130
9f951329
AM
13131 /* Build the .glink lazy link call stubs. */
13132 indx = 0;
a345bc8d 13133 while (p < htab->glink->contents + htab->glink->rawsize)
9f951329 13134 {
b9e5796b 13135 if (htab->opd_abi)
9f951329 13136 {
b9e5796b
AM
13137 if (indx < 0x8000)
13138 {
13139 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13140 p += 4;
13141 }
13142 else
13143 {
13144 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13145 p += 4;
13146 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13147 p);
13148 p += 4;
13149 }
9f951329 13150 }
4ce794b7 13151 bfd_put_32 (htab->glink->owner,
ee4bf8d2 13152 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 13153 indx++;
9f951329
AM
13154 p += 4;
13155 }
a345bc8d
AM
13156
13157 /* Build .glink global entry stubs. */
13158 if (htab->glink->size > htab->glink->rawsize)
afe397ea 13159 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
5d1634d7 13160 }
5d1634d7 13161
7341d5e2 13162 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 13163 {
4ce794b7 13164 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 13165 htab->brlt->size);
4ce794b7 13166 if (htab->brlt->contents == NULL)
b34976b6 13167 return FALSE;
721956f4 13168 }
ee75fd95 13169 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
13170 {
13171 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 13172 htab->relbrlt->size);
63bc6f6c
AM
13173 if (htab->relbrlt->contents == NULL)
13174 return FALSE;
13175 }
5d1634d7 13176
721956f4
AM
13177 /* Build the stubs as directed by the stub hash table. */
13178 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 13179
a4b6fadd
AM
13180 for (group = htab->group; group != NULL; group = group->next)
13181 if (group->needs_save_res)
13182 {
13183 stub_sec = group->stub_sec;
13184 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13185 htab->sfpr->size);
13186 if (htab->params->emit_stub_syms)
13187 {
13188 unsigned int i;
13189
13190 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13191 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13192 return FALSE;
13193 }
13194 stub_sec->size += htab->sfpr->size;
13195 }
13196
aa8a7074
AM
13197 if (htab->relbrlt != NULL)
13198 htab->relbrlt->reloc_count = 0;
13199
e7d1c40c
AM
13200 if (htab->params->plt_stub_align != 0)
13201 for (stub_sec = htab->params->stub_bfd->sections;
794e51c0
AM
13202 stub_sec != NULL;
13203 stub_sec = stub_sec->next)
13204 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
e7d1c40c
AM
13205 stub_sec->size = ((stub_sec->size
13206 + (1 << htab->params->plt_stub_align) - 1)
29f628db 13207 & -(1 << htab->params->plt_stub_align));
794e51c0 13208
e7d1c40c 13209 for (stub_sec = htab->params->stub_bfd->sections;
721956f4
AM
13210 stub_sec != NULL;
13211 stub_sec = stub_sec->next)
e717da7e
AM
13212 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13213 {
13214 stub_sec_count += 1;
c9301e31
AM
13215 if (stub_sec->rawsize != stub_sec->size
13216 && (htab->stub_iteration <= STUB_SHRINK_ITER
13217 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
13218 break;
13219 }
5d1634d7 13220
da44f4e5
AM
13221 /* Note that the glink_eh_frame check here is not only testing that
13222 the generated size matched the calculated size but also that
13223 bfd_elf_discard_info didn't make any changes to the section. */
721956f4 13224 if (stub_sec != NULL
58d180e8
AM
13225 || (htab->glink_eh_frame != NULL
13226 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 13227 {
b34976b6 13228 htab->stub_error = TRUE;
8de848d8 13229 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 13230 }
721956f4 13231
d2a300cf
AM
13232 if (htab->stub_error)
13233 return FALSE;
13234
13235 if (stats != NULL)
13236 {
13237 *stats = bfd_malloc (500);
13238 if (*stats == NULL)
13239 return FALSE;
13240
ee75fd95 13241 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
13242 " branch %lu\n"
13243 " toc adjust %lu\n"
13244 " long branch %lu\n"
13245 " long toc adj %lu\n"
794e51c0 13246 " plt call %lu\n"
7341d5e2
AM
13247 " plt call toc %lu\n"
13248 " global entry %lu"),
e717da7e 13249 stub_sec_count,
ee75fd95 13250 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
13251 htab->stub_count[ppc_stub_long_branch - 1],
13252 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13253 htab->stub_count[ppc_stub_plt_branch - 1],
13254 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0 13255 htab->stub_count[ppc_stub_plt_call - 1],
7341d5e2
AM
13256 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13257 htab->stub_count[ppc_stub_global_entry - 1]);
d2a300cf
AM
13258 }
13259 return TRUE;
5bd4f169
AM
13260}
13261
60124e18
AM
13262/* What to do when ld finds relocations against symbols defined in
13263 discarded sections. */
13264
13265static unsigned int
13266ppc64_elf_action_discarded (asection *sec)
13267{
13268 if (strcmp (".opd", sec->name) == 0)
13269 return 0;
13270
13271 if (strcmp (".toc", sec->name) == 0)
13272 return 0;
13273
bce50a28
JJ
13274 if (strcmp (".toc1", sec->name) == 0)
13275 return 0;
13276
60124e18
AM
13277 return _bfd_elf_default_action_discarded (sec);
13278}
13279
5bd4f169
AM
13280/* The RELOCATE_SECTION function is called by the ELF backend linker
13281 to handle the relocations for a section.
13282
13283 The relocs are always passed as Rela structures; if the section
13284 actually uses Rel structures, the r_addend field will always be
13285 zero.
13286
13287 This function is responsible for adjust the section contents as
13288 necessary, and (if using Rela relocs and generating a
1049f94e 13289 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
13290 necessary.
13291
13292 This function does not have to worry about setting the reloc
13293 address or the reloc symbol index.
13294
13295 LOCAL_SYMS is a pointer to the swapped in local symbols.
13296
13297 LOCAL_SECTIONS is an array giving the section in the input file
13298 corresponding to the st_shndx field of each local symbol.
13299
13300 The global hash table entry for the global symbols can be found
13301 via elf_sym_hashes (input_bfd).
13302
1049f94e 13303 When generating relocatable output, this function must handle
5bd4f169
AM
13304 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13305 going to be the section symbol corresponding to the output
13306 section, which means that the addend must be adjusted
13307 accordingly. */
13308
b34976b6 13309static bfd_boolean
4ce794b7
AM
13310ppc64_elf_relocate_section (bfd *output_bfd,
13311 struct bfd_link_info *info,
13312 bfd *input_bfd,
13313 asection *input_section,
13314 bfd_byte *contents,
13315 Elf_Internal_Rela *relocs,
13316 Elf_Internal_Sym *local_syms,
13317 asection **local_sections)
5bd4f169 13318{
65f38f15 13319 struct ppc_link_hash_table *htab;
5bd4f169
AM
13320 Elf_Internal_Shdr *symtab_hdr;
13321 struct elf_link_hash_entry **sym_hashes;
5bd4f169 13322 Elf_Internal_Rela *rel;
c316a17c 13323 Elf_Internal_Rela *wrel;
5bd4f169 13324 Elf_Internal_Rela *relend;
411e1bfb
AM
13325 Elf_Internal_Rela outrel;
13326 bfd_byte *loc;
411e1bfb 13327 struct got_entry **local_got_ents;
5bd4f169 13328 bfd_vma TOCstart;
b34976b6
AM
13329 bfd_boolean ret = TRUE;
13330 bfd_boolean is_opd;
794e51c0
AM
13331 /* Assume 'at' branch hints. */
13332 bfd_boolean is_isa_v2 = TRUE;
95f0d0d2 13333 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 13334
65f38f15 13335 /* Initialize howto table if needed. */
5bd4f169 13336 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
13337 ppc_howto_init ();
13338
65f38f15 13339 htab = ppc_hash_table (info);
4dfe6ac6
NC
13340 if (htab == NULL)
13341 return FALSE;
ee75fd95
AM
13342
13343 /* Don't relocate stub sections. */
e7d1c40c 13344 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
13345 return TRUE;
13346
0c8d6e5c 13347 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 13348
411e1bfb 13349 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 13350 TOCstart = elf_gp (output_bfd);
0ffa91dd 13351 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 13352 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 13353 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 13354
c316a17c 13355 rel = wrel = relocs;
5bd4f169 13356 relend = relocs + input_section->reloc_count;
c316a17c 13357 for (; rel < relend; wrel++, rel++)
5bd4f169 13358 {
04c9666a 13359 enum elf_ppc64_reloc_type r_type;
31c76678 13360 bfd_vma addend;
5bd4f169
AM
13361 bfd_reloc_status_type r;
13362 Elf_Internal_Sym *sym;
13363 asection *sec;
039b3fef
AM
13364 struct elf_link_hash_entry *h_elf;
13365 struct ppc_link_hash_entry *h;
13366 struct ppc_link_hash_entry *fdh;
5bd4f169 13367 const char *sym_name;
0d4792f7 13368 unsigned long r_symndx, toc_symndx;
3a71aa26 13369 bfd_vma toc_addend;
f961d9dd
AM
13370 unsigned char tls_mask, tls_gd, tls_type;
13371 unsigned char sym_type;
5bd4f169 13372 bfd_vma relocation;
b34976b6
AM
13373 bfd_boolean unresolved_reloc;
13374 bfd_boolean warned;
bc30df16 13375 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 13376 unsigned int insn;
e11840f9 13377 unsigned int mask;
721956f4
AM
13378 struct ppc_stub_hash_entry *stub_entry;
13379 bfd_vma max_br_offset;
13380 bfd_vma from;
c316a17c 13381 Elf_Internal_Rela orig_rel;
b80eed39
AM
13382 reloc_howto_type *howto;
13383 struct reloc_howto_struct alt_howto;
5bd4f169 13384
c316a17c
AM
13385 again:
13386 orig_rel = *rel;
13387
4ce794b7 13388 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 13389 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
13390
13391 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13392 symbol of the previous ADDR64 reloc. The symbol gives us the
13393 proper TOC base to use. */
13394 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
13395 && wrel != relocs
13396 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 13397 && is_opd)
c316a17c 13398 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 13399
4ce794b7
AM
13400 sym = NULL;
13401 sec = NULL;
039b3fef 13402 h_elf = NULL;
4ce794b7 13403 sym_name = NULL;
b34976b6
AM
13404 unresolved_reloc = FALSE;
13405 warned = FALSE;
65f38f15 13406
0b13192e 13407 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
13408 {
13409 /* It's a local symbol. */
74f0fb50 13410 struct _opd_sec_data *opd;
4025353c 13411
5bd4f169
AM
13412 sym = local_syms + r_symndx;
13413 sec = local_sections[r_symndx];
26c61ae5 13414 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 13415 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 13416 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
13417 opd = get_opd_info (sec);
13418 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 13419 {
51aecdc5
AM
13420 long adjust = opd->adjust[OPD_NDX (sym->st_value
13421 + rel->r_addend)];
4025353c
AM
13422 if (adjust == -1)
13423 relocation = 0;
13424 else
4cc603a5
AM
13425 {
13426 /* If this is a relocation against the opd section sym
13427 and we have edited .opd, adjust the reloc addend so
13428 that ld -r and ld --emit-relocs output is correct.
13429 If it is a reloc against some other .opd symbol,
13430 then the symbol value will be adjusted later. */
13431 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13432 rel->r_addend += adjust;
13433 else
13434 relocation += adjust;
13435 }
1e2f5b6e 13436 }
5bd4f169
AM
13437 }
13438 else
13439 {
62d887d4
L
13440 bfd_boolean ignored;
13441
b2a8e766
AM
13442 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13443 r_symndx, symtab_hdr, sym_hashes,
039b3fef 13444 h_elf, sec, relocation,
62d887d4 13445 unresolved_reloc, warned, ignored);
039b3fef
AM
13446 sym_name = h_elf->root.root.string;
13447 sym_type = h_elf->type;
b69fdb4e
AM
13448 if (sec != NULL
13449 && sec->owner == output_bfd
13450 && strcmp (sec->name, ".opd") == 0)
13451 {
13452 /* This is a symbol defined in a linker script. All
13453 such are defined in output sections, even those
13454 defined by simple assignment from a symbol defined in
13455 an input section. Transfer the symbol to an
13456 appropriate input .opd section, so that a branch to
13457 this symbol will be mapped to the location specified
13458 by the opd entry. */
13459 struct bfd_link_order *lo;
13460 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13461 if (lo->type == bfd_indirect_link_order)
13462 {
13463 asection *isec = lo->u.indirect.section;
13464 if (h_elf->root.u.def.value >= isec->output_offset
13465 && h_elf->root.u.def.value < (isec->output_offset
13466 + isec->size))
13467 {
13468 h_elf->root.u.def.value -= isec->output_offset;
13469 h_elf->root.u.def.section = isec;
13470 sec = isec;
13471 break;
13472 }
13473 }
13474 }
5bd4f169 13475 }
039b3fef 13476 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 13477
dbaa2011 13478 if (sec != NULL && discarded_section (sec))
c316a17c
AM
13479 {
13480 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13481 input_bfd, input_section,
13482 contents + rel->r_offset);
13483 wrel->r_offset = rel->r_offset;
13484 wrel->r_info = 0;
13485 wrel->r_addend = 0;
13486
13487 /* For ld -r, remove relocations in debug sections against
13488 sections defined in discarded sections. Not done for
13489 non-debug to preserve relocs in .eh_frame which the
13490 eh_frame editing code expects to be present. */
13491 if (bfd_link_relocatable (info)
13492 && (input_section->flags & SEC_DEBUGGING))
13493 wrel--;
13494
13495 continue;
13496 }
ab96bf03 13497
0e1862bb 13498 if (bfd_link_relocatable (info))
c316a17c 13499 goto copy_reloc;
ab96bf03 13500
f40da81b
AM
13501 if (h != NULL && &h->elf == htab->elf.hgot)
13502 {
6f20ed8a 13503 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
13504 sec = bfd_abs_section_ptr;
13505 unresolved_reloc = FALSE;
13506 }
13507
951fd09b
AM
13508 /* TLS optimizations. Replace instruction sequences and relocs
13509 based on information we collected in tls_optimize. We edit
13510 RELOCS so that --emit-relocs will output something sensible
13511 for the final instruction stream. */
13512 tls_mask = 0;
13513 tls_gd = 0;
0d4792f7 13514 toc_symndx = 0;
727fc41e
AM
13515 if (h != NULL)
13516 tls_mask = h->tls_mask;
13517 else if (local_got_ents != NULL)
411e1bfb 13518 {
e054468f
AM
13519 struct plt_entry **local_plt = (struct plt_entry **)
13520 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 13521 unsigned char *lgot_masks = (unsigned char *)
e054468f 13522 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
13523 tls_mask = lgot_masks[r_symndx];
13524 }
13525 if (tls_mask == 0
13526 && (r_type == R_PPC64_TLS
13527 || r_type == R_PPC64_TLSGD
13528 || r_type == R_PPC64_TLSLD))
13529 {
13530 /* Check for toc tls entries. */
f961d9dd 13531 unsigned char *toc_tls;
0d4792f7 13532
727fc41e
AM
13533 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13534 &local_syms, rel, input_bfd))
13535 return FALSE;
0d4792f7 13536
727fc41e
AM
13537 if (toc_tls)
13538 tls_mask = *toc_tls;
0d4792f7
AM
13539 }
13540
13541 /* Check that tls relocs are used with tls syms, and non-tls
13542 relocs are used with non-tls syms. */
cf35638d 13543 if (r_symndx != STN_UNDEF
0d4792f7
AM
13544 && r_type != R_PPC64_NONE
13545 && (h == NULL
039b3fef
AM
13546 || h->elf.root.type == bfd_link_hash_defined
13547 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
13548 && (IS_PPC64_TLS_RELOC (r_type)
13549 != (sym_type == STT_TLS
13550 || (sym_type == STT_SECTION
13551 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 13552 {
727fc41e
AM
13553 if (tls_mask != 0
13554 && (r_type == R_PPC64_TLS
13555 || r_type == R_PPC64_TLSGD
13556 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
13557 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13558 ;
13559 else
25f53a85 13560 info->callbacks->einfo
1d483afe 13561 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 13562 /* xgettext:c-format */
bc30df16 13563 ? _("%P: %H: %s used with TLS symbol `%T'\n")
695344c0 13564 /* xgettext:c-format */
bc30df16 13565 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
25f53a85 13566 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
13567 ppc64_elf_howto_table[r_type]->name,
13568 sym_name);
411e1bfb
AM
13569 }
13570
13571 /* Ensure reloc mapping code below stays sane. */
13572 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13573 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13574 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13575 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13576 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13577 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13578 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13579 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13580 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13581 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13582 abort ();
0d4792f7 13583
411e1bfb
AM
13584 switch (r_type)
13585 {
13586 default:
411e1bfb
AM
13587 break;
13588
ba761f19 13589 case R_PPC64_LO_DS_OPT:
95f0d0d2 13590 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
ba761f19
AM
13591 if ((insn & (0x3f << 26)) != 58u << 26)
13592 abort ();
13593 insn += (14u << 26) - (58u << 26);
95f0d0d2 13594 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
13595 r_type = R_PPC64_TOC16_LO;
13596 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13597 break;
13598
411e1bfb
AM
13599 case R_PPC64_TOC16:
13600 case R_PPC64_TOC16_LO:
13601 case R_PPC64_TOC16_DS:
13602 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
13603 {
13604 /* Check for toc tls entries. */
f961d9dd 13605 unsigned char *toc_tls;
951fd09b 13606 int retval;
411e1bfb 13607
3a71aa26
AM
13608 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13609 &local_syms, rel, input_bfd);
951fd09b 13610 if (retval == 0)
411e1bfb
AM
13611 return FALSE;
13612
13613 if (toc_tls)
13614 {
951fd09b 13615 tls_mask = *toc_tls;
411e1bfb
AM
13616 if (r_type == R_PPC64_TOC16_DS
13617 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
13618 {
13619 if (tls_mask != 0
13620 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13621 goto toctprel;
13622 }
411e1bfb 13623 else
951fd09b
AM
13624 {
13625 /* If we found a GD reloc pair, then we might be
13626 doing a GD->IE transition. */
13627 if (retval == 2)
13628 {
13629 tls_gd = TLS_TPRELGD;
13630 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13631 goto tls_ldgd_opt;
951fd09b
AM
13632 }
13633 else if (retval == 3)
13634 {
13635 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 13636 goto tls_ldgd_opt;
951fd09b
AM
13637 }
13638 }
411e1bfb
AM
13639 }
13640 }
13641 break;
13642
9d6ded02
AM
13643 case R_PPC64_GOT_TPREL16_HI:
13644 case R_PPC64_GOT_TPREL16_HA:
13645 if (tls_mask != 0
13646 && (tls_mask & TLS_TPREL) == 0)
13647 {
13648 rel->r_offset -= d_offset;
95f0d0d2 13649 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
13650 r_type = R_PPC64_NONE;
13651 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13652 }
13653 break;
13654
411e1bfb
AM
13655 case R_PPC64_GOT_TPREL16_DS:
13656 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
13657 if (tls_mask != 0
13658 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13659 {
81407a69 13660 toctprel:
95f0d0d2 13661 insn = bfd_get_32 (input_bfd,
c316a17c 13662 contents + rel->r_offset - d_offset);
411e1bfb
AM
13663 insn &= 31 << 21;
13664 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 13665 bfd_put_32 (input_bfd, insn,
c316a17c 13666 contents + rel->r_offset - d_offset);
411e1bfb 13667 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
13668 if (toc_symndx != 0)
13669 {
13670 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13671 rel->r_addend = toc_addend;
0d4792f7
AM
13672 /* We changed the symbol. Start over in order to
13673 get h, sym, sec etc. right. */
c316a17c 13674 goto again;
0d4792f7
AM
13675 }
13676 else
13677 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13678 }
13679 break;
13680
13681 case R_PPC64_TLS:
951fd09b
AM
13682 if (tls_mask != 0
13683 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 13684 {
95f0d0d2 13685 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2d0f3896
AM
13686 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13687 if (insn == 0)
411e1bfb 13688 abort ();
95f0d0d2 13689 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
411e1bfb 13690 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
13691 PPC64_TPREL16_LO which is at low-order half-word. */
13692 rel->r_offset += d_offset;
0d4792f7
AM
13693 r_type = R_PPC64_TPREL16_LO;
13694 if (toc_symndx != 0)
13695 {
13696 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 13697 rel->r_addend = toc_addend;
0d4792f7
AM
13698 /* We changed the symbol. Start over in order to
13699 get h, sym, sec etc. right. */
c316a17c 13700 goto again;
0d4792f7
AM
13701 }
13702 else
13703 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13704 }
13705 break;
13706
411e1bfb
AM
13707 case R_PPC64_GOT_TLSGD16_HI:
13708 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
13709 tls_gd = TLS_TPRELGD;
13710 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13711 goto tls_gdld_hi;
13712 break;
13713
411e1bfb
AM
13714 case R_PPC64_GOT_TLSLD16_HI:
13715 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13716 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 13717 {
951fd09b
AM
13718 tls_gdld_hi:
13719 if ((tls_mask & tls_gd) != 0)
13720 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13721 + R_PPC64_GOT_TPREL16_DS);
13722 else
411e1bfb 13723 {
4fe5ca5b 13724 rel->r_offset -= d_offset;
95f0d0d2 13725 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 13726 r_type = R_PPC64_NONE;
411e1bfb 13727 }
951fd09b 13728 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
13729 }
13730 break;
13731
951fd09b
AM
13732 case R_PPC64_GOT_TLSGD16:
13733 case R_PPC64_GOT_TLSGD16_LO:
13734 tls_gd = TLS_TPRELGD;
13735 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13736 goto tls_ldgd_opt;
951fd09b 13737 break;
411e1bfb 13738
951fd09b
AM
13739 case R_PPC64_GOT_TLSLD16:
13740 case R_PPC64_GOT_TLSLD16_LO:
13741 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13742 {
3a71aa26 13743 unsigned int insn1, insn2, insn3;
102890f0
AM
13744 bfd_vma offset;
13745
13746 tls_ldgd_opt:
727fc41e
AM
13747 offset = (bfd_vma) -1;
13748 /* If not using the newer R_PPC64_TLSGD/LD to mark
13749 __tls_get_addr calls, we must trust that the call
13750 stays with its arg setup insns, ie. that the next
13751 reloc is the __tls_get_addr call associated with
13752 the current reloc. Edit both insns. */
13753 if (input_section->has_tls_get_addr_call
13754 && rel + 1 < relend
13755 && branch_reloc_hash_match (input_bfd, rel + 1,
13756 htab->tls_get_addr,
13757 htab->tls_get_addr_fd))
13758 offset = rel[1].r_offset;
b86ac8e3
AM
13759 /* We read the low GOT_TLS (or TOC16) insn because we
13760 need to keep the destination reg. It may be
13761 something other than the usual r3, and moved to r3
13762 before the call by intervening code. */
95f0d0d2 13763 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 13764 contents + rel->r_offset - d_offset);
102890f0 13765 if ((tls_mask & tls_gd) != 0)
411e1bfb 13766 {
102890f0 13767 /* IE */
b86ac8e3 13768 insn1 &= (0x1f << 21) | (0x1f << 16);
102890f0
AM
13769 insn1 |= 58 << 26; /* ld */
13770 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13771 if (offset != (bfd_vma) -1)
f58d5a2d 13772 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13773 if ((tls_mask & TLS_EXPLICIT) == 0)
13774 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13775 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13776 else
102890f0
AM
13777 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13778 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13779 }
13780 else
13781 {
13782 /* LE */
b86ac8e3
AM
13783 insn1 &= 0x1f << 21;
13784 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
13785 insn2 = 0x38630000; /* addi 3,3,0 */
13786 if (tls_gd == 0)
951fd09b 13787 {
102890f0 13788 /* Was an LD reloc. */
1d483afe
AM
13789 if (toc_symndx)
13790 sec = local_sections[toc_symndx];
13791 for (r_symndx = 0;
13792 r_symndx < symtab_hdr->sh_info;
13793 r_symndx++)
13794 if (local_sections[r_symndx] == sec)
13795 break;
13796 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13797 r_symndx = STN_UNDEF;
102890f0 13798 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13799 if (r_symndx != STN_UNDEF)
1d483afe
AM
13800 rel->r_addend -= (local_syms[r_symndx].st_value
13801 + sec->output_offset
13802 + sec->output_section->vma);
951fd09b 13803 }
102890f0 13804 else if (toc_symndx != 0)
3a71aa26
AM
13805 {
13806 r_symndx = toc_symndx;
13807 rel->r_addend = toc_addend;
13808 }
102890f0
AM
13809 r_type = R_PPC64_TPREL16_HA;
13810 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13811 if (offset != (bfd_vma) -1)
13812 {
13813 rel[1].r_info = ELF64_R_INFO (r_symndx,
13814 R_PPC64_TPREL16_LO);
13815 rel[1].r_offset = offset + d_offset;
13816 rel[1].r_addend = rel->r_addend;
13817 }
102890f0 13818 }
95f0d0d2 13819 bfd_put_32 (input_bfd, insn1,
3a71aa26 13820 contents + rel->r_offset - d_offset);
727fc41e
AM
13821 if (offset != (bfd_vma) -1)
13822 {
95f0d0d2 13823 insn3 = bfd_get_32 (input_bfd,
727fc41e
AM
13824 contents + offset + 4);
13825 if (insn3 == NOP
13826 || insn3 == CROR_151515 || insn3 == CROR_313131)
13827 {
13828 rel[1].r_offset += 4;
95f0d0d2 13829 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
727fc41e
AM
13830 insn2 = NOP;
13831 }
95f0d0d2 13832 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e
AM
13833 }
13834 if ((tls_mask & tls_gd) == 0
13835 && (tls_gd == 0 || toc_symndx != 0))
13836 {
13837 /* We changed the symbol. Start over in order
13838 to get h, sym, sec etc. right. */
c316a17c 13839 goto again;
727fc41e
AM
13840 }
13841 }
13842 break;
13843
13844 case R_PPC64_TLSGD:
13845 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13846 {
13847 unsigned int insn2, insn3;
13848 bfd_vma offset = rel->r_offset;
13849
13850 if ((tls_mask & TLS_TPRELGD) != 0)
13851 {
13852 /* IE */
13853 r_type = R_PPC64_NONE;
13854 insn2 = 0x7c636a14; /* add 3,3,13 */
13855 }
13856 else
13857 {
13858 /* LE */
13859 if (toc_symndx != 0)
13860 {
13861 r_symndx = toc_symndx;
13862 rel->r_addend = toc_addend;
13863 }
13864 r_type = R_PPC64_TPREL16_LO;
13865 rel->r_offset = offset + d_offset;
13866 insn2 = 0x38630000; /* addi 3,3,0 */
13867 }
13868 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13869 /* Zap the reloc on the _tls_get_addr call too. */
13870 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13871 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 13872 insn3 = bfd_get_32 (input_bfd,
3a71aa26 13873 contents + offset + 4);
102890f0
AM
13874 if (insn3 == NOP
13875 || insn3 == CROR_151515 || insn3 == CROR_313131)
13876 {
727fc41e 13877 rel->r_offset += 4;
95f0d0d2 13878 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
3a71aa26 13879 insn2 = NOP;
102890f0 13880 }
95f0d0d2 13881 bfd_put_32 (input_bfd, insn2, contents + offset);
727fc41e 13882 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
c316a17c 13883 goto again;
411e1bfb 13884 }
411e1bfb
AM
13885 break;
13886
727fc41e
AM
13887 case R_PPC64_TLSLD:
13888 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13889 {
13890 unsigned int insn2, insn3;
13891 bfd_vma offset = rel->r_offset;
13892
13893 if (toc_symndx)
13894 sec = local_sections[toc_symndx];
13895 for (r_symndx = 0;
13896 r_symndx < symtab_hdr->sh_info;
13897 r_symndx++)
13898 if (local_sections[r_symndx] == sec)
13899 break;
13900 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13901 r_symndx = STN_UNDEF;
727fc41e 13902 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13903 if (r_symndx != STN_UNDEF)
727fc41e
AM
13904 rel->r_addend -= (local_syms[r_symndx].st_value
13905 + sec->output_offset
13906 + sec->output_section->vma);
13907
13908 r_type = R_PPC64_TPREL16_LO;
13909 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13910 rel->r_offset = offset + d_offset;
13911 /* Zap the reloc on the _tls_get_addr call too. */
13912 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13913 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e 13914 insn2 = 0x38630000; /* addi 3,3,0 */
95f0d0d2 13915 insn3 = bfd_get_32 (input_bfd,
727fc41e
AM
13916 contents + offset + 4);
13917 if (insn3 == NOP
13918 || insn3 == CROR_151515 || insn3 == CROR_313131)
13919 {
13920 rel->r_offset += 4;
95f0d0d2 13921 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
727fc41e
AM
13922 insn2 = NOP;
13923 }
95f0d0d2 13924 bfd_put_32 (input_bfd, insn2, contents + offset);
c316a17c 13925 goto again;
727fc41e
AM
13926 }
13927 break;
13928
411e1bfb 13929 case R_PPC64_DTPMOD64:
951fd09b
AM
13930 if (rel + 1 < relend
13931 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13932 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13933 {
951fd09b
AM
13934 if ((tls_mask & TLS_GD) == 0)
13935 {
13936 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13937 if ((tls_mask & TLS_TPRELGD) != 0)
13938 r_type = R_PPC64_TPREL64;
13939 else
13940 {
4ce794b7 13941 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13942 r_type = R_PPC64_NONE;
13943 }
13944 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13945 }
13946 }
13947 else
13948 {
13949 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13950 {
4ce794b7 13951 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13952 r_type = R_PPC64_NONE;
951fd09b 13953 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13954 }
411e1bfb
AM
13955 }
13956 break;
13957
13958 case R_PPC64_TPREL64:
951fd09b 13959 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13960 {
13961 r_type = R_PPC64_NONE;
13962 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13963 }
13964 break;
52a82034 13965
006589cf
AM
13966 case R_PPC64_ENTRY:
13967 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13968 if (!bfd_link_pic (info)
13969 && !info->traditional_format
13970 && relocation + 0x80008000 <= 0xffffffff)
13971 {
13972 unsigned int insn1, insn2;
13973
13974 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13975 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13976 if ((insn1 & ~0xfffc) == LD_R2_0R12
13977 && insn2 == ADD_R2_R2_R12)
13978 {
95f0d0d2 13979 bfd_put_32 (input_bfd,
006589cf
AM
13980 LIS_R2 + PPC_HA (relocation),
13981 contents + rel->r_offset);
95f0d0d2 13982 bfd_put_32 (input_bfd,
006589cf
AM
13983 ADDI_R2_R2 + PPC_LO (relocation),
13984 contents + rel->r_offset + 4);
13985 }
13986 }
13987 else
13988 {
13989 relocation -= (rel->r_offset
13990 + input_section->output_offset
13991 + input_section->output_section->vma);
13992 if (relocation + 0x80008000 <= 0xffffffff)
13993 {
13994 unsigned int insn1, insn2;
13995
13996 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13997 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13998 if ((insn1 & ~0xfffc) == LD_R2_0R12
13999 && insn2 == ADD_R2_R2_R12)
14000 {
95f0d0d2 14001 bfd_put_32 (input_bfd,
006589cf
AM
14002 ADDIS_R2_R12 + PPC_HA (relocation),
14003 contents + rel->r_offset);
95f0d0d2 14004 bfd_put_32 (input_bfd,
006589cf
AM
14005 ADDI_R2_R2 + PPC_LO (relocation),
14006 contents + rel->r_offset + 4);
14007 }
14008 }
14009 }
14010 break;
14011
52a82034
AM
14012 case R_PPC64_REL16_HA:
14013 /* If we are generating a non-PIC executable, edit
14014 . 0: addis 2,12,.TOC.-0b@ha
14015 . addi 2,2,.TOC.-0b@l
14016 used by ELFv2 global entry points to set up r2, to
14017 . lis 2,.TOC.@ha
14018 . addi 2,2,.TOC.@l
14019 if .TOC. is in range. */
0e1862bb 14020 if (!bfd_link_pic (info)
810d4e75 14021 && !info->traditional_format
006589cf 14022 && !htab->opd_abi
4f038ee5 14023 && rel->r_addend == d_offset
52a82034
AM
14024 && h != NULL && &h->elf == htab->elf.hgot
14025 && rel + 1 < relend
14026 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14027 && rel[1].r_offset == rel->r_offset + 4
14028 && rel[1].r_addend == rel->r_addend + 4
14029 && relocation + 0x80008000 <= 0xffffffff)
14030 {
14031 unsigned int insn1, insn2;
14032 bfd_vma offset = rel->r_offset - d_offset;
95f0d0d2
AM
14033 insn1 = bfd_get_32 (input_bfd, contents + offset);
14034 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
14035 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14036 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
14037 {
14038 r_type = R_PPC64_ADDR16_HA;
14039 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14040 rel->r_addend -= d_offset;
14041 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14042 rel[1].r_addend -= d_offset + 4;
95f0d0d2 14043 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
14044 }
14045 }
14046 break;
411e1bfb
AM
14047 }
14048
14049 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 14050 insn = 0;
b25116a9
AM
14051 max_br_offset = 1 << 25;
14052 addend = rel->r_addend;
bc30df16 14053 reloc_dest = DEST_NORMAL;
65f38f15 14054 switch (r_type)
5bd4f169
AM
14055 {
14056 default:
65f38f15 14057 break;
5bd4f169 14058
3b421ab3
AM
14059 case R_PPC64_TOCSAVE:
14060 if (relocation + addend == (rel->r_offset
14061 + input_section->output_offset
14062 + input_section->output_section->vma)
14063 && tocsave_find (htab, NO_INSERT,
14064 &local_syms, rel, input_bfd))
14065 {
14066 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14067 if (insn == NOP
14068 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
14069 bfd_put_32 (input_bfd,
14070 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
14071 contents + rel->r_offset);
14072 }
14073 break;
14074
65f38f15
AM
14075 /* Branch taken prediction relocations. */
14076 case R_PPC64_ADDR14_BRTAKEN:
14077 case R_PPC64_REL14_BRTAKEN:
cedb70c5 14078 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 14079 /* Fall through. */
65f38f15 14080
86c76c7b 14081 /* Branch not taken prediction relocations. */
65f38f15
AM
14082 case R_PPC64_ADDR14_BRNTAKEN:
14083 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 14084 insn |= bfd_get_32 (input_bfd,
411e1bfb 14085 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 14086 /* Fall through. */
86c76c7b 14087
b25116a9
AM
14088 case R_PPC64_REL14:
14089 max_br_offset = 1 << 15;
1a0670f3 14090 /* Fall through. */
5bd4f169 14091
65f38f15 14092 case R_PPC64_REL24:
ad8e1ba5
AM
14093 /* Calls to functions with a different TOC, such as calls to
14094 shared objects, need to alter the TOC pointer. This is
14095 done using a linkage stub. A REL24 branching to these
14096 linkage stubs needs to be followed by a nop, as the nop
14097 will be replaced with an instruction to restore the TOC
14098 base pointer. */
8387904d 14099 fdh = h;
b31867b6
AM
14100 if (h != NULL
14101 && h->oh != NULL
14102 && h->oh->is_func_descriptor)
14103 fdh = ppc_follow_link (h->oh);
31c76678
DK
14104 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14105 htab);
6abec6bc 14106 if (stub_entry != NULL
ad8e1ba5 14107 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 14108 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
14109 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14110 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 14111 {
b25116a9 14112 bfd_boolean can_plt_call = FALSE;
721956f4 14113
ba8ca3e7
AM
14114 /* All of these stubs will modify r2, so there must be a
14115 branch and link followed by a nop. The nop is
14116 replaced by an insn to restore r2. */
eea6121a 14117 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 14118 {
ba8ca3e7
AM
14119 unsigned long br;
14120
14121 br = bfd_get_32 (input_bfd,
14122 contents + rel->r_offset);
14123 if ((br & 1) != 0)
41bd81ab 14124 {
ba8ca3e7
AM
14125 unsigned long nop;
14126
14127 nop = bfd_get_32 (input_bfd,
14128 contents + rel->r_offset + 4);
14129 if (nop == NOP
14130 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 14131 {
ba8ca3e7
AM
14132 if (h != NULL
14133 && (h == htab->tls_get_addr_fd
14134 || h == htab->tls_get_addr)
7c9cf415 14135 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
14136 {
14137 /* Special stub used, leave nop alone. */
14138 }
14139 else
a078d95a
AM
14140 bfd_put_32 (input_bfd,
14141 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
14142 contents + rel->r_offset + 4);
14143 can_plt_call = TRUE;
a7f2871e 14144 }
41bd81ab 14145 }
5bd4f169 14146 }
721956f4 14147
ba8ca3e7 14148 if (!can_plt_call && h != NULL)
721956f4 14149 {
ba8ca3e7
AM
14150 const char *name = h->elf.root.root.string;
14151
14152 if (*name == '.')
14153 ++name;
14154
14155 if (strncmp (name, "__libc_start_main", 17) == 0
14156 && (name[17] == 0 || name[17] == '@'))
6ab189d5 14157 {
ba8ca3e7
AM
14158 /* Allow crt1 branch to go via a toc adjusting
14159 stub. Other calls that never return could do
14160 the same, if we could detect such. */
b25116a9 14161 can_plt_call = TRUE;
6ab189d5 14162 }
ba8ca3e7
AM
14163 }
14164
14165 if (!can_plt_call)
14166 {
14167 /* g++ as of 20130507 emits self-calls without a
14168 following nop. This is arguably wrong since we
14169 have conflicting information. On the one hand a
14170 global symbol and on the other a local call
14171 sequence, but don't error for this special case.
14172 It isn't possible to cheaply verify we have
14173 exactly such a call. Allow all calls to the same
14174 section. */
14175 asection *code_sec = sec;
14176
14177 if (get_opd_info (sec) != NULL)
ad8e1ba5 14178 {
ba8ca3e7
AM
14179 bfd_vma off = (relocation + addend
14180 - sec->output_section->vma
14181 - sec->output_offset);
bc30df16 14182
ba8ca3e7 14183 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 14184 }
ba8ca3e7
AM
14185 if (code_sec == input_section)
14186 can_plt_call = TRUE;
14187 }
14188
14189 if (!can_plt_call)
14190 {
4805fc55
AM
14191 if (stub_entry->stub_type == ppc_stub_plt_call
14192 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14193 info->callbacks->einfo
695344c0 14194 /* xgettext:c-format */
4805fc55
AM
14195 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14196 "recompile with -fPIC\n"),
14197 input_bfd, input_section, rel->r_offset, sym_name);
14198 else
14199 info->callbacks->einfo
695344c0 14200 /* xgettext:c-format */
4805fc55
AM
14201 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14202 "(-mcmodel=small toc adjust stub)\n"),
14203 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
14204
14205 bfd_set_error (bfd_error_bad_value);
14206 ret = FALSE;
721956f4
AM
14207 }
14208
b25116a9 14209 if (can_plt_call
794e51c0
AM
14210 && (stub_entry->stub_type == ppc_stub_plt_call
14211 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
14212 unresolved_reloc = FALSE;
14213 }
14214
6abec6bc
AM
14215 if ((stub_entry == NULL
14216 || stub_entry->stub_type == ppc_stub_long_branch
14217 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
14218 && get_opd_info (sec) != NULL)
14219 {
14220 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
14221 bfd_vma off = (relocation + addend
14222 - sec->output_section->vma
14223 - sec->output_offset);
aef36ac1 14224 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
14225 if (dest != (bfd_vma) -1)
14226 {
14227 relocation = dest;
14228 addend = 0;
bc30df16 14229 reloc_dest = DEST_OPD;
8387904d
AM
14230 }
14231 }
14232
b25116a9
AM
14233 /* If the branch is out of reach we ought to have a long
14234 branch stub. */
14235 from = (rel->r_offset
14236 + input_section->output_offset
14237 + input_section->output_section->vma);
14238
6911b7dc
AM
14239 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14240 ? fdh->elf.other
14241 : sym->st_other);
14242
6abec6bc
AM
14243 if (stub_entry != NULL
14244 && (stub_entry->stub_type == ppc_stub_long_branch
14245 || stub_entry->stub_type == ppc_stub_plt_branch)
14246 && (r_type == R_PPC64_ADDR14_BRTAKEN
14247 || r_type == R_PPC64_ADDR14_BRNTAKEN
14248 || (relocation + addend - from + max_br_offset
14249 < 2 * max_br_offset)))
14250 /* Don't use the stub if this branch is in range. */
14251 stub_entry = NULL;
b25116a9
AM
14252
14253 if (stub_entry != NULL)
14254 {
14255 /* Munge up the value and addend so that we call the stub
14256 rather than the procedure directly. */
a4b6fadd
AM
14257 asection *stub_sec = stub_entry->group->stub_sec;
14258
14259 if (stub_entry->stub_type == ppc_stub_save_res)
14260 relocation += (stub_sec->output_offset
14261 + stub_sec->output_section->vma
14262 + stub_sec->size - htab->sfpr->size
14263 - htab->sfpr->output_offset
14264 - htab->sfpr->output_section->vma);
14265 else
14266 relocation = (stub_entry->stub_offset
14267 + stub_sec->output_offset
14268 + stub_sec->output_section->vma);
b25116a9 14269 addend = 0;
bc30df16 14270 reloc_dest = DEST_STUB;
3b421ab3 14271
794e51c0
AM
14272 if ((stub_entry->stub_type == ppc_stub_plt_call
14273 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14274 && (ALWAYS_EMIT_R2SAVE
14275 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
14276 && rel + 1 < relend
14277 && rel[1].r_offset == rel->r_offset + 4
14278 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14279 relocation += 4;
b25116a9
AM
14280 }
14281
14282 if (insn != 0)
14283 {
794e51c0 14284 if (is_isa_v2)
721956f4 14285 {
b25116a9
AM
14286 /* Set 'a' bit. This is 0b00010 in BO field for branch
14287 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14288 for branch on CTR insns (BO == 1a00t or 1a01t). */
14289 if ((insn & (0x14 << 21)) == (0x04 << 21))
14290 insn |= 0x02 << 21;
14291 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14292 insn |= 0x08 << 21;
14293 else
14294 break;
14295 }
14296 else
14297 {
14298 /* Invert 'y' bit if not the default. */
4cc603a5 14299 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 14300 insn ^= 0x01 << 21;
721956f4 14301 }
b25116a9 14302
95f0d0d2 14303 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 14304 }
e86ce104 14305
06da1e8e
AM
14306 /* NOP out calls to undefined weak functions.
14307 We can thus call a weak function without first
14308 checking whether the function is defined. */
b25116a9 14309 else if (h != NULL
039b3fef 14310 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 14311 && h->elf.dynindx == -1
b25116a9
AM
14312 && r_type == R_PPC64_REL24
14313 && relocation == 0
4cc603a5 14314 && addend == 0)
e86ce104 14315 {
95f0d0d2 14316 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 14317 goto copy_reloc;
e86ce104 14318 }
65f38f15
AM
14319 break;
14320 }
5bd4f169 14321
65f38f15 14322 /* Set `addend'. */
411e1bfb 14323 tls_type = 0;
65f38f15
AM
14324 switch (r_type)
14325 {
14326 default:
25f53a85 14327 info->callbacks->einfo
695344c0 14328 /* xgettext:c-format */
bc30df16 14329 (_("%P: %B: unknown relocation type %d for `%T'\n"),
d003868e 14330 input_bfd, (int) r_type, sym_name);
5bd4f169 14331
65f38f15 14332 bfd_set_error (bfd_error_bad_value);
b34976b6 14333 ret = FALSE;
c316a17c 14334 goto copy_reloc;
5bd4f169 14335
65f38f15 14336 case R_PPC64_NONE:
411e1bfb 14337 case R_PPC64_TLS:
727fc41e
AM
14338 case R_PPC64_TLSGD:
14339 case R_PPC64_TLSLD:
3b421ab3 14340 case R_PPC64_TOCSAVE:
04c9666a
AM
14341 case R_PPC64_GNU_VTINHERIT:
14342 case R_PPC64_GNU_VTENTRY:
006589cf 14343 case R_PPC64_ENTRY:
c316a17c 14344 goto copy_reloc;
5bd4f169
AM
14345
14346 /* GOT16 relocations. Like an ADDR16 using the symbol's
14347 address in the GOT as relocation value instead of the
411e1bfb 14348 symbol's value itself. Also, create a GOT entry for the
5bd4f169 14349 symbol and put the symbol value there. */
411e1bfb
AM
14350 case R_PPC64_GOT_TLSGD16:
14351 case R_PPC64_GOT_TLSGD16_LO:
14352 case R_PPC64_GOT_TLSGD16_HI:
14353 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 14354 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
14355 goto dogot;
14356
14357 case R_PPC64_GOT_TLSLD16:
14358 case R_PPC64_GOT_TLSLD16_LO:
14359 case R_PPC64_GOT_TLSLD16_HI:
14360 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 14361 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
14362 goto dogot;
14363
14364 case R_PPC64_GOT_TPREL16_DS:
14365 case R_PPC64_GOT_TPREL16_LO_DS:
14366 case R_PPC64_GOT_TPREL16_HI:
14367 case R_PPC64_GOT_TPREL16_HA:
14368 tls_type = TLS_TLS | TLS_TPREL;
14369 goto dogot;
14370
14371 case R_PPC64_GOT_DTPREL16_DS:
14372 case R_PPC64_GOT_DTPREL16_LO_DS:
14373 case R_PPC64_GOT_DTPREL16_HI:
14374 case R_PPC64_GOT_DTPREL16_HA:
14375 tls_type = TLS_TLS | TLS_DTPREL;
14376 goto dogot;
14377
65f38f15
AM
14378 case R_PPC64_GOT16:
14379 case R_PPC64_GOT16_LO:
14380 case R_PPC64_GOT16_HI:
14381 case R_PPC64_GOT16_HA:
14382 case R_PPC64_GOT16_DS:
14383 case R_PPC64_GOT16_LO_DS:
411e1bfb 14384 dogot:
5bd4f169
AM
14385 {
14386 /* Relocation is to the entry for this symbol in the global
14387 offset table. */
e717da7e 14388 asection *got;
d881513a 14389 bfd_vma *offp;
5bd4f169 14390 bfd_vma off;
d881513a 14391 unsigned long indx = 0;
927be08e 14392 struct got_entry *ent;
65f38f15 14393
d881513a
AM
14394 if (tls_type == (TLS_TLS | TLS_LD)
14395 && (h == NULL
f5385ebf 14396 || !h->elf.def_dynamic))
927be08e 14397 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 14398 else
5bd4f169 14399 {
5bd4f169 14400
d881513a
AM
14401 if (h != NULL)
14402 {
14403 bfd_boolean dyn = htab->elf.dynamic_sections_created;
0e1862bb 14404 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
039b3fef 14405 &h->elf)
0e1862bb 14406 || (bfd_link_pic (info)
afe397ea 14407 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
d881513a
AM
14408 /* This is actually a static link, or it is a
14409 -Bsymbolic link and the symbol is defined
14410 locally, or the symbol was forced to be local
14411 because of a version file. */
14412 ;
14413 else
14414 {
14acf4dc 14415 BFD_ASSERT (h->elf.dynindx != -1);
039b3fef 14416 indx = h->elf.dynindx;
d881513a
AM
14417 unresolved_reloc = FALSE;
14418 }
039b3fef 14419 ent = h->elf.got.glist;
d881513a 14420 }
411e1bfb 14421 else
5bd4f169 14422 {
d881513a
AM
14423 if (local_got_ents == NULL)
14424 abort ();
14425 ent = local_got_ents[r_symndx];
5bd4f169 14426 }
d881513a
AM
14427
14428 for (; ent != NULL; ent = ent->next)
31c76678 14429 if (ent->addend == orig_rel.r_addend
e717da7e 14430 && ent->owner == input_bfd
d881513a
AM
14431 && ent->tls_type == tls_type)
14432 break;
5bd4f169 14433 }
411e1bfb 14434
927be08e
AM
14435 if (ent == NULL)
14436 abort ();
14437 if (ent->is_indirect)
14438 ent = ent->got.ent;
14439 offp = &ent->got.offset;
14440 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
14441 if (got == NULL)
14442 abort ();
14443
411e1bfb
AM
14444 /* The offset must always be a multiple of 8. We use the
14445 least significant bit to record whether we have already
14446 processed this entry. */
d881513a 14447 off = *offp;
411e1bfb
AM
14448 if ((off & 1) != 0)
14449 off &= ~1;
5bd4f169
AM
14450 else
14451 {
411e1bfb
AM
14452 /* Generate relocs for the dynamic linker, except in
14453 the case of TLSLD where we'll use one entry per
14454 module. */
25f23106
AM
14455 asection *relgot;
14456 bfd_boolean ifunc;
e717da7e 14457
d881513a 14458 *offp = off | 1;
25f23106
AM
14459 relgot = NULL;
14460 ifunc = (h != NULL
14461 ? h->elf.type == STT_GNU_IFUNC
14462 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 14463 if (ifunc)
33e44f2e 14464 relgot = htab->elf.irelplt;
0e1862bb 14465 else if ((bfd_link_pic (info) || indx != 0)
19e08130
AM
14466 && (h == NULL
14467 || (tls_type == (TLS_TLS | TLS_LD)
14468 && !h->elf.def_dynamic)
14469 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14470 || h->elf.root.type != bfd_link_hash_undefweak))
14471 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 14472 if (relgot != NULL)
5bd4f169 14473 {
e717da7e
AM
14474 outrel.r_offset = (got->output_section->vma
14475 + got->output_offset
411e1bfb 14476 + off);
4cc603a5 14477 outrel.r_addend = addend;
d881513a 14478 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 14479 {
411e1bfb 14480 outrel.r_addend = 0;
e515b051 14481 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
14482 if (tls_type == (TLS_TLS | TLS_GD))
14483 {
e717da7e
AM
14484 loc = relgot->contents;
14485 loc += (relgot->reloc_count++
d881513a
AM
14486 * sizeof (Elf64_External_Rela));
14487 bfd_elf64_swap_reloca_out (output_bfd,
14488 &outrel, loc);
e515b051 14489 outrel.r_offset += 8;
4cc603a5 14490 outrel.r_addend = addend;
d881513a
AM
14491 outrel.r_info
14492 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 14493 }
411e1bfb 14494 }
951fd09b 14495 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 14496 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 14497 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 14498 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
14499 else if (indx != 0)
14500 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14501 else
81407a69 14502 {
25f23106
AM
14503 if (ifunc)
14504 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14505 else
14506 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
14507
14508 /* Write the .got section contents for the sake
14509 of prelink. */
e717da7e 14510 loc = got->contents + off;
23fbd6fa
JJ
14511 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14512 loc);
81407a69 14513 }
81407a69
AM
14514
14515 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
14516 {
14517 outrel.r_addend += relocation;
14518 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
14519 {
14520 if (htab->elf.tls_sec == NULL)
14521 outrel.r_addend = 0;
14522 else
14523 outrel.r_addend -= htab->elf.tls_sec->vma;
14524 }
e515b051 14525 }
e717da7e
AM
14526 loc = relgot->contents;
14527 loc += (relgot->reloc_count++
411e1bfb
AM
14528 * sizeof (Elf64_External_Rela));
14529 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14530 }
14531
ad8e1ba5 14532 /* Init the .got section contents here if we're not
81407a69 14533 emitting a reloc. */
d881513a 14534 else
411e1bfb 14535 {
4cc603a5 14536 relocation += addend;
7b609f53
AM
14537 if (tls_type == (TLS_TLS | TLS_LD))
14538 relocation = 1;
14539 else if (tls_type != 0)
411e1bfb 14540 {
989f9879
AM
14541 if (htab->elf.tls_sec == NULL)
14542 relocation = 0;
14543 else
14544 {
14545 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14546 if (tls_type == (TLS_TLS | TLS_TPREL))
14547 relocation += DTP_OFFSET - TP_OFFSET;
14548 }
5bd4f169 14549
7b609f53
AM
14550 if (tls_type == (TLS_TLS | TLS_GD))
14551 {
14552 bfd_put_64 (output_bfd, relocation,
e717da7e 14553 got->contents + off + 8);
7b609f53
AM
14554 relocation = 1;
14555 }
411e1bfb 14556 }
7b609f53 14557
411e1bfb 14558 bfd_put_64 (output_bfd, relocation,
e717da7e 14559 got->contents + off);
5bd4f169
AM
14560 }
14561 }
14562
65f38f15
AM
14563 if (off >= (bfd_vma) -2)
14564 abort ();
14565
bf102f86 14566 relocation = got->output_section->vma + got->output_offset + off;
6f20ed8a 14567 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 14568 }
65f38f15
AM
14569 break;
14570
14571 case R_PPC64_PLT16_HA:
14572 case R_PPC64_PLT16_HI:
14573 case R_PPC64_PLT16_LO:
14574 case R_PPC64_PLT32:
14575 case R_PPC64_PLT64:
14576 /* Relocation is to the entry for this symbol in the
14577 procedure linkage table. */
cbf95972
AM
14578 {
14579 struct plt_entry **plt_list = NULL;
14580 if (h != NULL)
14581 plt_list = &h->elf.plt.plist;
14582 else if (local_got_ents != NULL)
14583 {
14584 struct plt_entry **local_plt = (struct plt_entry **)
14585 (local_got_ents + symtab_hdr->sh_info);
14586 unsigned char *local_got_tls_masks = (unsigned char *)
14587 (local_plt + symtab_hdr->sh_info);
14588 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14589 plt_list = local_plt + r_symndx;
14590 }
14591 if (plt_list)
14592 {
14593 struct plt_entry *ent;
65f38f15 14594
cbf95972
AM
14595 for (ent = *plt_list; ent != NULL; ent = ent->next)
14596 if (ent->plt.offset != (bfd_vma) -1
14597 && ent->addend == orig_rel.r_addend)
14598 {
14599 asection *plt;
14600
14601 plt = htab->elf.splt;
14602 if (!htab->elf.dynamic_sections_created
14603 || h == NULL
14604 || h->elf.dynindx == -1)
14605 plt = htab->elf.iplt;
14606 relocation = (plt->output_section->vma
14607 + plt->output_offset
14608 + ent->plt.offset);
14609 addend = 0;
14610 unresolved_reloc = FALSE;
14611 break;
14612 }
14613 }
14614 }
65f38f15 14615 break;
5bd4f169 14616
0b13192e
AM
14617 case R_PPC64_TOC:
14618 /* Relocation value is TOC base. */
14619 relocation = TOCstart;
cf35638d 14620 if (r_symndx == STN_UNDEF)
6f20ed8a 14621 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
14622 else if (unresolved_reloc)
14623 ;
6f20ed8a
AM
14624 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14625 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
14626 else
14627 unresolved_reloc = TRUE;
ab96bf03 14628 goto dodyn;
0b13192e 14629
5bd4f169
AM
14630 /* TOC16 relocs. We want the offset relative to the TOC base,
14631 which is the address of the start of the TOC plus 0x8000.
14632 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14633 in this order. */
65f38f15
AM
14634 case R_PPC64_TOC16:
14635 case R_PPC64_TOC16_LO:
14636 case R_PPC64_TOC16_HI:
14637 case R_PPC64_TOC16_DS:
14638 case R_PPC64_TOC16_LO_DS:
14639 case R_PPC64_TOC16_HA:
6f20ed8a 14640 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
5bd4f169
AM
14641 break;
14642
14643 /* Relocate against the beginning of the section. */
65f38f15
AM
14644 case R_PPC64_SECTOFF:
14645 case R_PPC64_SECTOFF_LO:
14646 case R_PPC64_SECTOFF_HI:
14647 case R_PPC64_SECTOFF_DS:
14648 case R_PPC64_SECTOFF_LO_DS:
14649 case R_PPC64_SECTOFF_HA:
4ce794b7 14650 if (sec != NULL)
65f38f15 14651 addend -= sec->output_section->vma;
5bd4f169
AM
14652 break;
14653
25f23106
AM
14654 case R_PPC64_REL16:
14655 case R_PPC64_REL16_LO:
14656 case R_PPC64_REL16_HI:
14657 case R_PPC64_REL16_HA:
a680de9a 14658 case R_PPC64_REL16DX_HA:
25f23106
AM
14659 break;
14660
721956f4
AM
14661 case R_PPC64_REL14:
14662 case R_PPC64_REL14_BRNTAKEN:
14663 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
14664 case R_PPC64_REL24:
14665 break;
14666
411e1bfb
AM
14667 case R_PPC64_TPREL16:
14668 case R_PPC64_TPREL16_LO:
14669 case R_PPC64_TPREL16_HI:
14670 case R_PPC64_TPREL16_HA:
14671 case R_PPC64_TPREL16_DS:
14672 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
14673 case R_PPC64_TPREL16_HIGH:
14674 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
14675 case R_PPC64_TPREL16_HIGHER:
14676 case R_PPC64_TPREL16_HIGHERA:
14677 case R_PPC64_TPREL16_HIGHEST:
14678 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
14679 if (h != NULL
14680 && h->elf.root.type == bfd_link_hash_undefweak
14681 && h->elf.dynindx == -1)
14682 {
14683 /* Make this relocation against an undefined weak symbol
14684 resolve to zero. This is really just a tweak, since
14685 code using weak externs ought to check that they are
14686 defined before using them. */
14687 bfd_byte *p = contents + rel->r_offset - d_offset;
14688
95f0d0d2 14689 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
14690 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14691 if (insn != 0)
95f0d0d2 14692 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
14693 break;
14694 }
989f9879
AM
14695 if (htab->elf.tls_sec != NULL)
14696 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
0e1862bb 14697 if (bfd_link_pic (info))
411e1bfb
AM
14698 /* The TPREL16 relocs shouldn't really be used in shared
14699 libs as they will result in DT_TEXTREL being set, but
14700 support them anyway. */
14701 goto dodyn;
14702 break;
14703
14704 case R_PPC64_DTPREL16:
14705 case R_PPC64_DTPREL16_LO:
14706 case R_PPC64_DTPREL16_HI:
14707 case R_PPC64_DTPREL16_HA:
14708 case R_PPC64_DTPREL16_DS:
14709 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
14710 case R_PPC64_DTPREL16_HIGH:
14711 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
14712 case R_PPC64_DTPREL16_HIGHER:
14713 case R_PPC64_DTPREL16_HIGHERA:
14714 case R_PPC64_DTPREL16_HIGHEST:
14715 case R_PPC64_DTPREL16_HIGHESTA:
989f9879
AM
14716 if (htab->elf.tls_sec != NULL)
14717 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
14718 break;
14719
45965137
AM
14720 case R_PPC64_ADDR64_LOCAL:
14721 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14722 ? h->elf.other
14723 : sym->st_other);
14724 break;
14725
e515b051
AM
14726 case R_PPC64_DTPMOD64:
14727 relocation = 1;
14728 addend = 0;
14729 goto dodyn;
14730
411e1bfb 14731 case R_PPC64_TPREL64:
989f9879
AM
14732 if (htab->elf.tls_sec != NULL)
14733 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
14734 goto dodyn;
14735
14736 case R_PPC64_DTPREL64:
989f9879
AM
14737 if (htab->elf.tls_sec != NULL)
14738 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 14739 /* Fall through. */
411e1bfb 14740
65f38f15
AM
14741 /* Relocations that may need to be propagated if this is a
14742 dynamic object. */
04c9666a 14743 case R_PPC64_REL30:
65f38f15
AM
14744 case R_PPC64_REL32:
14745 case R_PPC64_REL64:
14746 case R_PPC64_ADDR14:
14747 case R_PPC64_ADDR14_BRNTAKEN:
14748 case R_PPC64_ADDR14_BRTAKEN:
14749 case R_PPC64_ADDR16:
14750 case R_PPC64_ADDR16_DS:
14751 case R_PPC64_ADDR16_HA:
14752 case R_PPC64_ADDR16_HI:
f9c6b907
AM
14753 case R_PPC64_ADDR16_HIGH:
14754 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14755 case R_PPC64_ADDR16_HIGHER:
14756 case R_PPC64_ADDR16_HIGHERA:
14757 case R_PPC64_ADDR16_HIGHEST:
14758 case R_PPC64_ADDR16_HIGHESTA:
14759 case R_PPC64_ADDR16_LO:
14760 case R_PPC64_ADDR16_LO_DS:
14761 case R_PPC64_ADDR24:
65f38f15
AM
14762 case R_PPC64_ADDR32:
14763 case R_PPC64_ADDR64:
14764 case R_PPC64_UADDR16:
14765 case R_PPC64_UADDR32:
14766 case R_PPC64_UADDR64:
411e1bfb 14767 dodyn:
5d1634d7 14768 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
14769 break;
14770
41bd81ab
AM
14771 if (NO_OPD_RELOCS && is_opd)
14772 break;
14773
8a9e8e72
AM
14774 if (bfd_link_pic (info)
14775 ? ((h != NULL && pc_dynrelocs (h))
14776 || must_be_dyn_reloc (info, r_type))
14777 : (h != NULL
14778 ? h->dyn_relocs != NULL
d311bc8b 14779 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 14780 {
b34976b6 14781 bfd_boolean skip, relocate;
65f38f15 14782 asection *sreloc;
1cf1f670 14783 bfd_vma out_off;
65f38f15
AM
14784
14785 /* When generating a dynamic object, these relocations
14786 are copied into the output file to be resolved at run
14787 time. */
14788
b34976b6
AM
14789 skip = FALSE;
14790 relocate = FALSE;
65f38f15 14791
1cf1f670
AM
14792 out_off = _bfd_elf_section_offset (output_bfd, info,
14793 input_section, rel->r_offset);
14794 if (out_off == (bfd_vma) -1)
b34976b6 14795 skip = TRUE;
1cf1f670 14796 else if (out_off == (bfd_vma) -2)
b34976b6 14797 skip = TRUE, relocate = TRUE;
1cf1f670
AM
14798 out_off += (input_section->output_section->vma
14799 + input_section->output_offset);
14800 outrel.r_offset = out_off;
411e1bfb 14801 outrel.r_addend = rel->r_addend;
65f38f15 14802
1cf1f670
AM
14803 /* Optimize unaligned reloc use. */
14804 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14805 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14806 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14807 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14808 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14809 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14810 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14811 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14812 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14813
65f38f15 14814 if (skip)
0bb2d96a 14815 memset (&outrel, 0, sizeof outrel);
afe397ea 14816 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
14817 && !is_opd
14818 && r_type != R_PPC64_TOC)
14acf4dc
MR
14819 {
14820 BFD_ASSERT (h->elf.dynindx != -1);
14821 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14822 }
65f38f15
AM
14823 else
14824 {
41bd81ab
AM
14825 /* This symbol is local, or marked to become local,
14826 or this is an opd section reloc which must point
14827 at a local function. */
65f38f15 14828 outrel.r_addend += relocation;
e86ce104 14829 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 14830 {
3fad3c7c 14831 if (is_opd && h != NULL)
afbe61cf
AM
14832 {
14833 /* Lie about opd entries. This case occurs
14834 when building shared libraries and we
14835 reference a function in another shared
3fad3c7c
AM
14836 lib. The same thing happens for a weak
14837 definition in an application that's
14838 overridden by a strong definition in a
14839 shared lib. (I believe this is a generic
14840 bug in binutils handling of weak syms.)
14841 In these cases we won't use the opd
1e2f5b6e 14842 entry in this lib. */
b34976b6 14843 unresolved_reloc = FALSE;
afbe61cf 14844 }
25f23106
AM
14845 if (!is_opd
14846 && r_type == R_PPC64_ADDR64
14847 && (h != NULL
14848 ? h->elf.type == STT_GNU_IFUNC
14849 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14850 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14851 else
14852 {
14853 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 14854
25f23106
AM
14855 /* We need to relocate .opd contents for ld.so.
14856 Prelink also wants simple and consistent rules
14857 for relocs. This make all RELATIVE relocs have
14858 *r_offset equal to r_addend. */
14859 relocate = TRUE;
14860 }
65f38f15
AM
14861 }
14862 else
14863 {
14864 long indx = 0;
14865
25f23106
AM
14866 if (h != NULL
14867 ? h->elf.type == STT_GNU_IFUNC
14868 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14869 {
25f53a85 14870 info->callbacks->einfo
695344c0 14871 /* xgettext:c-format */
bc30df16
AM
14872 (_("%P: %H: %s for indirect "
14873 "function `%T' unsupported\n"),
25f53a85 14874 input_bfd, input_section, rel->r_offset,
25f23106
AM
14875 ppc64_elf_howto_table[r_type]->name,
14876 sym_name);
14877 ret = FALSE;
14878 }
cf35638d 14879 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14880 ;
14881 else if (sec == NULL || sec->owner == NULL)
14882 {
14883 bfd_set_error (bfd_error_bad_value);
b34976b6 14884 return FALSE;
65f38f15
AM
14885 }
14886 else
14887 {
14888 asection *osec;
14889
14890 osec = sec->output_section;
14891 indx = elf_section_data (osec)->dynindx;
14892
74541ad4
AM
14893 if (indx == 0)
14894 {
14895 if ((osec->flags & SEC_READONLY) == 0
14896 && htab->elf.data_index_section != NULL)
14897 osec = htab->elf.data_index_section;
14898 else
14899 osec = htab->elf.text_index_section;
14900 indx = elf_section_data (osec)->dynindx;
14901 }
14902 BFD_ASSERT (indx != 0);
14903
65f38f15
AM
14904 /* We are turning this relocation into one
14905 against a section symbol, so subtract out
14906 the output section's address but not the
14907 offset of the input section in the output
14908 section. */
14909 outrel.r_addend -= osec->vma;
14910 }
14911
14912 outrel.r_info = ELF64_R_INFO (indx, r_type);
14913 }
14914 }
14915
14916 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14917 if (h != NULL
14918 ? h->elf.type == STT_GNU_IFUNC
14919 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
33e44f2e 14920 sreloc = htab->elf.irelplt;
65f38f15
AM
14921 if (sreloc == NULL)
14922 abort ();
14923
dfbb6ac9
AM
14924 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14925 >= sreloc->size)
14926 abort ();
947216bf
AM
14927 loc = sreloc->contents;
14928 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14929 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14930
14931 /* If this reloc is against an external symbol, it will
14932 be computed at runtime, so there's no need to do
81407a69
AM
14933 anything now. However, for the sake of prelink ensure
14934 that the section contents are a known value. */
65f38f15 14935 if (! relocate)
81407a69
AM
14936 {
14937 unresolved_reloc = FALSE;
14938 /* The value chosen here is quite arbitrary as ld.so
14939 ignores section contents except for the special
14940 case of .opd where the contents might be accessed
14941 before relocation. Choose zero, as that won't
14942 cause reloc overflow. */
14943 relocation = 0;
14944 addend = 0;
14945 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14946 to improve backward compatibility with older
14947 versions of ld. */
14948 if (r_type == R_PPC64_ADDR64)
14949 addend = outrel.r_addend;
14950 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 14951 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
14952 addend = (input_section->output_section->vma
14953 + input_section->output_offset
14954 + rel->r_offset);
81407a69 14955 }
65f38f15 14956 }
5bd4f169
AM
14957 break;
14958
65f38f15
AM
14959 case R_PPC64_COPY:
14960 case R_PPC64_GLOB_DAT:
14961 case R_PPC64_JMP_SLOT:
25f23106 14962 case R_PPC64_JMP_IREL:
65f38f15
AM
14963 case R_PPC64_RELATIVE:
14964 /* We shouldn't ever see these dynamic relocs in relocatable
14965 files. */
ae9a127f 14966 /* Fall through. */
65f38f15
AM
14967
14968 case R_PPC64_PLTGOT16:
14969 case R_PPC64_PLTGOT16_DS:
14970 case R_PPC64_PLTGOT16_HA:
14971 case R_PPC64_PLTGOT16_HI:
14972 case R_PPC64_PLTGOT16_LO:
14973 case R_PPC64_PLTGOT16_LO_DS:
14974 case R_PPC64_PLTREL32:
14975 case R_PPC64_PLTREL64:
14976 /* These ones haven't been implemented yet. */
14977
25f53a85 14978 info->callbacks->einfo
695344c0 14979 /* xgettext:c-format */
bc30df16 14980 (_("%P: %B: %s is not supported for `%T'\n"),
d003868e 14981 input_bfd,
4ce794b7 14982 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
14983
14984 bfd_set_error (bfd_error_invalid_operation);
b34976b6 14985 ret = FALSE;
c316a17c 14986 goto copy_reloc;
65f38f15 14987 }
5bd4f169 14988
67f0cbdb
AM
14989 /* Multi-instruction sequences that access the TOC can be
14990 optimized, eg. addis ra,r2,0; addi rb,ra,x;
14991 to nop; addi rb,r2,x; */
14992 switch (r_type)
14993 {
14994 default:
14995 break;
14996
14997 case R_PPC64_GOT_TLSLD16_HI:
14998 case R_PPC64_GOT_TLSGD16_HI:
14999 case R_PPC64_GOT_TPREL16_HI:
15000 case R_PPC64_GOT_DTPREL16_HI:
15001 case R_PPC64_GOT16_HI:
15002 case R_PPC64_TOC16_HI:
15003 /* These relocs would only be useful if building up an
15004 offset to later add to r2, perhaps in an indexed
15005 addressing mode instruction. Don't try to optimize.
15006 Unfortunately, the possibility of someone building up an
15007 offset like this or even with the HA relocs, means that
15008 we need to check the high insn when optimizing the low
15009 insn. */
15010 break;
15011
15012 case R_PPC64_GOT_TLSLD16_HA:
15013 case R_PPC64_GOT_TLSGD16_HA:
15014 case R_PPC64_GOT_TPREL16_HA:
15015 case R_PPC64_GOT_DTPREL16_HA:
15016 case R_PPC64_GOT16_HA:
15017 case R_PPC64_TOC16_HA:
98528052 15018 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15019 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
15020 {
15021 bfd_byte *p = contents + (rel->r_offset & ~3);
15022 bfd_put_32 (input_bfd, NOP, p);
15023 }
67f0cbdb
AM
15024 break;
15025
15026 case R_PPC64_GOT_TLSLD16_LO:
15027 case R_PPC64_GOT_TLSGD16_LO:
15028 case R_PPC64_GOT_TPREL16_LO_DS:
15029 case R_PPC64_GOT_DTPREL16_LO_DS:
15030 case R_PPC64_GOT16_LO:
15031 case R_PPC64_GOT16_LO_DS:
15032 case R_PPC64_TOC16_LO:
15033 case R_PPC64_TOC16_LO_DS:
98528052 15034 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 15035 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
15036 {
15037 bfd_byte *p = contents + (rel->r_offset & ~3);
15038 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
15039 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15040 {
15041 /* Transform addic to addi when we change reg. */
15042 insn &= ~((0x3f << 26) | (0x1f << 16));
15043 insn |= (14u << 26) | (2 << 16);
15044 }
15045 else
67f0cbdb 15046 {
98528052
AM
15047 insn &= ~(0x1f << 16);
15048 insn |= 2 << 16;
67f0cbdb 15049 }
560c8763 15050 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
15051 }
15052 break;
15053 }
15054
65f38f15 15055 /* Do any further special processing. */
b80eed39 15056 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15
AM
15057 switch (r_type)
15058 {
15059 default:
15060 break;
15061
25f23106 15062 case R_PPC64_REL16_HA:
a680de9a 15063 case R_PPC64_REL16DX_HA:
f9c6b907
AM
15064 case R_PPC64_ADDR16_HA:
15065 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
15066 case R_PPC64_ADDR16_HIGHERA:
15067 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
15068 case R_PPC64_TOC16_HA:
15069 case R_PPC64_SECTOFF_HA:
411e1bfb 15070 case R_PPC64_TPREL16_HA:
f9c6b907 15071 case R_PPC64_TPREL16_HIGHA:
411e1bfb 15072 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 15073 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
15074 case R_PPC64_DTPREL16_HA:
15075 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 15076 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 15077 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
15078 /* It's just possible that this symbol is a weak symbol
15079 that's not actually defined anywhere. In that case,
15080 'sec' would be NULL, and we should leave the symbol
15081 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
15082 if (sec == NULL)
15083 break;
1a0670f3 15084 /* Fall through. */
5c5f6e17
AM
15085
15086 case R_PPC64_GOT16_HA:
15087 case R_PPC64_PLTGOT16_HA:
15088 case R_PPC64_PLT16_HA:
15089 case R_PPC64_GOT_TLSGD16_HA:
15090 case R_PPC64_GOT_TLSLD16_HA:
15091 case R_PPC64_GOT_TPREL16_HA:
15092 case R_PPC64_GOT_DTPREL16_HA:
15093 /* Add 0x10000 if sign bit in 0:15 is set.
15094 Bits 0:15 are not used. */
15095 addend += 0x8000;
65f38f15
AM
15096 break;
15097
15098 case R_PPC64_ADDR16_DS:
15099 case R_PPC64_ADDR16_LO_DS:
15100 case R_PPC64_GOT16_DS:
15101 case R_PPC64_GOT16_LO_DS:
15102 case R_PPC64_PLT16_LO_DS:
15103 case R_PPC64_SECTOFF_DS:
15104 case R_PPC64_SECTOFF_LO_DS:
15105 case R_PPC64_TOC16_DS:
15106 case R_PPC64_TOC16_LO_DS:
15107 case R_PPC64_PLTGOT16_DS:
15108 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
15109 case R_PPC64_GOT_TPREL16_DS:
15110 case R_PPC64_GOT_TPREL16_LO_DS:
15111 case R_PPC64_GOT_DTPREL16_DS:
15112 case R_PPC64_GOT_DTPREL16_LO_DS:
15113 case R_PPC64_TPREL16_DS:
15114 case R_PPC64_TPREL16_LO_DS:
15115 case R_PPC64_DTPREL16_DS:
15116 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
15117 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15118 mask = 3;
a680de9a
PB
15119 /* If this reloc is against an lq, lxv, or stxv insn, then
15120 the value must be a multiple of 16. This is somewhat of
15121 a hack, but the "correct" way to do this by defining _DQ
15122 forms of all the _DS relocs bloats all reloc switches in
15123 this file. It doesn't make much sense to use these
15124 relocs in data, so testing the insn should be safe. */
15125 if ((insn & (0x3f << 26)) == (56u << 26)
15126 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 15127 mask = 15;
a680de9a
PB
15128 relocation += addend;
15129 addend = insn & (mask ^ 3);
15130 if ((relocation & mask) != 0)
65f38f15 15131 {
a680de9a 15132 relocation ^= relocation & mask;
25f53a85 15133 info->callbacks->einfo
695344c0 15134 /* xgettext:c-format */
8de848d8 15135 (_("%P: %H: error: %s not a multiple of %u\n"),
25f53a85 15136 input_bfd, input_section, rel->r_offset,
b80eed39 15137 howto->name,
adadcc0c 15138 mask + 1);
65f38f15 15139 bfd_set_error (bfd_error_bad_value);
b34976b6 15140 ret = FALSE;
c316a17c 15141 goto copy_reloc;
65f38f15
AM
15142 }
15143 break;
5bd4f169
AM
15144 }
15145
239e1f3a
AM
15146 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15147 because such sections are not SEC_ALLOC and thus ld.so will
15148 not process them. */
65f38f15 15149 if (unresolved_reloc
239e1f3a 15150 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
15151 && h->elf.def_dynamic)
15152 && _bfd_elf_section_offset (output_bfd, info, input_section,
15153 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 15154 {
25f53a85 15155 info->callbacks->einfo
695344c0 15156 /* xgettext:c-format */
bc30df16 15157 (_("%P: %H: unresolvable %s against `%T'\n"),
25f53a85 15158 input_bfd, input_section, rel->r_offset,
b80eed39 15159 howto->name,
039b3fef 15160 h->elf.root.root.string);
b34976b6 15161 ret = FALSE;
9c07fe7c 15162 }
5bd4f169 15163
b80eed39
AM
15164 /* 16-bit fields in insns mostly have signed values, but a
15165 few insns have 16-bit unsigned values. Really, we should
15166 have different reloc types. */
15167 if (howto->complain_on_overflow != complain_overflow_dont
15168 && howto->dst_mask == 0xffff
15169 && (input_section->flags & SEC_CODE) != 0)
15170 {
15171 enum complain_overflow complain = complain_overflow_signed;
15172
15173 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
a47622ac
AM
15174 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15175 complain = complain_overflow_bitfield;
15176 else if (howto->rightshift == 0
15177 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15178 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15179 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15180 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15181 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15182 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
15183 complain = complain_overflow_unsigned;
15184 if (howto->complain_on_overflow != complain)
15185 {
15186 alt_howto = *howto;
15187 alt_howto.complain_on_overflow = complain;
15188 howto = &alt_howto;
15189 }
15190 }
15191
a680de9a
PB
15192 if (r_type == R_PPC64_REL16DX_HA)
15193 {
15194 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15195 if (rel->r_offset + 4 > input_section->size)
15196 r = bfd_reloc_outofrange;
15197 else
15198 {
15199 relocation += addend;
15200 relocation -= (rel->r_offset
15201 + input_section->output_offset
15202 + input_section->output_section->vma);
15203 relocation = (bfd_signed_vma) relocation >> 16;
15204 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15205 insn &= ~0x1fffc1;
15206 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15207 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15208 r = bfd_reloc_ok;
15209 if (relocation + 0x8000 > 0xffff)
15210 r = bfd_reloc_overflow;
15211 }
15212 }
15213 else
15214 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15215 rel->r_offset, relocation, addend);
5bd4f169 15216
ef60b7ff 15217 if (r != bfd_reloc_ok)
5bd4f169 15218 {
bc30df16 15219 char *more_info = NULL;
b80eed39 15220 const char *reloc_name = howto->name;
bc30df16
AM
15221
15222 if (reloc_dest != DEST_NORMAL)
15223 {
15224 more_info = bfd_malloc (strlen (reloc_name) + 8);
15225 if (more_info != NULL)
15226 {
15227 strcpy (more_info, reloc_name);
15228 strcat (more_info, (reloc_dest == DEST_OPD
15229 ? " (OPD)" : " (stub)"));
15230 reloc_name = more_info;
15231 }
15232 }
15233
cd27b276 15234 if (r == bfd_reloc_overflow)
5bd4f169 15235 {
8131c122
AM
15236 /* On code like "if (foo) foo();" don't report overflow
15237 on a branch to zero when foo is undefined. */
15238 if (!warned
15239 && (reloc_dest == DEST_STUB
15240 || !(h != NULL
15241 && (h->elf.root.type == bfd_link_hash_undefweak
15242 || h->elf.root.type == bfd_link_hash_undefined)
15243 && is_branch_reloc (r_type))))
1a72702b
AM
15244 info->callbacks->reloc_overflow (info, &h->elf.root,
15245 sym_name, reloc_name,
15246 orig_rel.r_addend,
15247 input_bfd, input_section,
15248 rel->r_offset);
ef60b7ff
AM
15249 }
15250 else
15251 {
25f53a85 15252 info->callbacks->einfo
695344c0 15253 /* xgettext:c-format */
bc30df16 15254 (_("%P: %H: %s against `%T': error %d\n"),
25f53a85 15255 input_bfd, input_section, rel->r_offset,
bc30df16 15256 reloc_name, sym_name, (int) r);
b34976b6 15257 ret = FALSE;
ef60b7ff 15258 }
bc30df16
AM
15259 if (more_info != NULL)
15260 free (more_info);
5bd4f169 15261 }
c316a17c
AM
15262 copy_reloc:
15263 if (wrel != rel)
15264 *wrel = *rel;
15265 }
15266
15267 if (wrel != rel)
15268 {
15269 Elf_Internal_Shdr *rel_hdr;
15270 size_t deleted = rel - wrel;
15271
15272 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15273 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15274 if (rel_hdr->sh_size == 0)
15275 {
15276 /* It is too late to remove an empty reloc section. Leave
15277 one NONE reloc.
15278 ??? What is wrong with an empty section??? */
15279 rel_hdr->sh_size = rel_hdr->sh_entsize;
15280 deleted -= 1;
15281 }
15282 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15283 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15284 input_section->reloc_count -= deleted;
5bd4f169
AM
15285 }
15286
645ea6a9
AM
15287 /* If we're emitting relocations, then shortly after this function
15288 returns, reloc offsets and addends for this section will be
15289 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
15290 file rather than the input. Save a copy of the relocs for
15291 opd_entry_value. */
0e1862bb 15292 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
15293 {
15294 bfd_size_type amt;
15295 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15296 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
15297 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15298 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
15299 if (rel == NULL)
15300 return FALSE;
15301 memcpy (rel, relocs, amt);
15302 }
5bd4f169
AM
15303 return ret;
15304}
15305
754021d0
AM
15306/* Adjust the value of any local symbols in opd sections. */
15307
6e0b88f1 15308static int
754021d0
AM
15309ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15310 const char *name ATTRIBUTE_UNUSED,
15311 Elf_Internal_Sym *elfsym,
15312 asection *input_sec,
15313 struct elf_link_hash_entry *h)
15314{
74f0fb50
AM
15315 struct _opd_sec_data *opd;
15316 long adjust;
754021d0
AM
15317 bfd_vma value;
15318
4025353c 15319 if (h != NULL)
6e0b88f1 15320 return 1;
4025353c 15321
74f0fb50
AM
15322 opd = get_opd_info (input_sec);
15323 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 15324 return 1;
754021d0
AM
15325
15326 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 15327 if (!bfd_link_relocatable (info))
754021d0
AM
15328 value -= input_sec->output_section->vma;
15329
51aecdc5 15330 adjust = opd->adjust[OPD_NDX (value)];
4025353c 15331 if (adjust == -1)
6e0b88f1
AM
15332 return 2;
15333
15334 elfsym->st_value += adjust;
15335 return 1;
754021d0
AM
15336}
15337
5bd4f169
AM
15338/* Finish up dynamic symbol handling. We set the contents of various
15339 dynamic sections here. */
15340
b34976b6 15341static bfd_boolean
4ce794b7
AM
15342ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15343 struct bfd_link_info *info,
15344 struct elf_link_hash_entry *h,
ab6dce23 15345 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5bd4f169 15346{
65f38f15 15347 struct ppc_link_hash_table *htab;
8387904d
AM
15348 struct plt_entry *ent;
15349 Elf_Internal_Rela rela;
15350 bfd_byte *loc;
5bd4f169 15351
65f38f15 15352 htab = ppc_hash_table (info);
4dfe6ac6
NC
15353 if (htab == NULL)
15354 return FALSE;
5bd4f169 15355
8387904d
AM
15356 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15357 if (ent->plt.offset != (bfd_vma) -1)
15358 {
15359 /* This symbol has an entry in the procedure linkage
15360 table. Set it up. */
e054468f
AM
15361 if (!htab->elf.dynamic_sections_created
15362 || h->dynindx == -1)
15363 {
15364 BFD_ASSERT (h->type == STT_GNU_IFUNC
15365 && h->def_regular
15366 && (h->root.type == bfd_link_hash_defined
15367 || h->root.type == bfd_link_hash_defweak));
33e44f2e
AM
15368 rela.r_offset = (htab->elf.iplt->output_section->vma
15369 + htab->elf.iplt->output_offset
25f23106 15370 + ent->plt.offset);
ee67d69a
AM
15371 if (htab->opd_abi)
15372 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15373 else
15374 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
15375 rela.r_addend = (h->root.u.def.value
15376 + h->root.u.def.section->output_offset
15377 + h->root.u.def.section->output_section->vma
15378 + ent->addend);
33e44f2e
AM
15379 loc = (htab->elf.irelplt->contents
15380 + (htab->elf.irelplt->reloc_count++
25f23106 15381 * sizeof (Elf64_External_Rela)));
e054468f
AM
15382 }
15383 else
15384 {
33e44f2e
AM
15385 rela.r_offset = (htab->elf.splt->output_section->vma
15386 + htab->elf.splt->output_offset
25f23106 15387 + ent->plt.offset);
e054468f
AM
15388 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15389 rela.r_addend = ent->addend;
33e44f2e 15390 loc = (htab->elf.srelplt->contents
b9e5796b
AM
15391 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15392 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
e054468f 15393 }
8387904d 15394 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
a345bc8d
AM
15395
15396 if (!htab->opd_abi)
15397 {
15398 if (!h->def_regular)
15399 {
15400 /* Mark the symbol as undefined, rather than as
15401 defined in glink. Leave the value if there were
15402 any relocations where pointer equality matters
15403 (this is a clue for the dynamic linker, to make
15404 function pointer comparisons work between an
15405 application and shared library), otherwise set it
15406 to zero. */
15407 sym->st_shndx = SHN_UNDEF;
15408 if (!h->pointer_equality_needed)
15409 sym->st_value = 0;
15410 else if (!h->ref_regular_nonweak)
15411 {
15412 /* This breaks function pointer comparisons, but
15413 that is better than breaking tests for a NULL
15414 function pointer. */
15415 sym->st_value = 0;
15416 }
15417 }
15418 }
8387904d 15419 }
5bd4f169 15420
f5385ebf 15421 if (h->needs_copy)
5bd4f169 15422 {
65f38f15 15423 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 15424
65f38f15
AM
15425 if (h->dynindx == -1
15426 || (h->root.type != bfd_link_hash_defined
15427 && h->root.type != bfd_link_hash_defweak)
9d19e4fd 15428 || htab->elf.srelbss == NULL)
65f38f15 15429 abort ();
5bd4f169
AM
15430
15431 rela.r_offset = (h->root.u.def.value
15432 + h->root.u.def.section->output_section->vma
15433 + h->root.u.def.section->output_offset);
15434 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15435 rela.r_addend = 0;
9d19e4fd
AM
15436 loc = htab->elf.srelbss->contents;
15437 loc += htab->elf.srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 15438 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
15439 }
15440
b34976b6 15441 return TRUE;
5bd4f169
AM
15442}
15443
65f38f15
AM
15444/* Used to decide how to sort relocs in an optimal manner for the
15445 dynamic linker, before writing them out. */
15446
15447static enum elf_reloc_type_class
7e612e98
AM
15448ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15449 const asection *rel_sec,
15450 const Elf_Internal_Rela *rela)
65f38f15 15451{
04c9666a 15452 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
15453 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15454
33e44f2e 15455 if (rel_sec == htab->elf.irelplt)
7e612e98 15456 return reloc_class_ifunc;
a33d1f77 15457
4ce794b7 15458 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 15459 switch (r_type)
65f38f15
AM
15460 {
15461 case R_PPC64_RELATIVE:
15462 return reloc_class_relative;
15463 case R_PPC64_JMP_SLOT:
15464 return reloc_class_plt;
15465 case R_PPC64_COPY:
15466 return reloc_class_copy;
15467 default:
15468 return reloc_class_normal;
15469 }
15470}
15471
5bd4f169
AM
15472/* Finish up the dynamic sections. */
15473
b34976b6 15474static bfd_boolean
4ce794b7
AM
15475ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15476 struct bfd_link_info *info)
5bd4f169 15477{
65f38f15
AM
15478 struct ppc_link_hash_table *htab;
15479 bfd *dynobj;
5bd4f169 15480 asection *sdyn;
5bd4f169 15481
65f38f15 15482 htab = ppc_hash_table (info);
4dfe6ac6
NC
15483 if (htab == NULL)
15484 return FALSE;
15485
65f38f15 15486 dynobj = htab->elf.dynobj;
3d4d4302 15487 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 15488
65f38f15 15489 if (htab->elf.dynamic_sections_created)
5bd4f169 15490 {
5bd4f169
AM
15491 Elf64_External_Dyn *dyncon, *dynconend;
15492
33e44f2e 15493 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 15494 abort ();
5bd4f169
AM
15495
15496 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 15497 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
15498 for (; dyncon < dynconend; dyncon++)
15499 {
15500 Elf_Internal_Dyn dyn;
19397422 15501 asection *s;
5bd4f169
AM
15502
15503 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15504
15505 switch (dyn.d_tag)
15506 {
65f38f15
AM
15507 default:
15508 continue;
5bd4f169 15509
5d1634d7 15510 case DT_PPC64_GLINK:
4ce794b7 15511 s = htab->glink;
6348e046 15512 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
15513 /* We stupidly defined DT_PPC64_GLINK to be the start
15514 of glink rather than the first entry point, which is
15515 what ld.so needs, and now have a bigger stub to
15516 support automatic multiple TOCs. */
b9e5796b 15517 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
5d1634d7
AM
15518 break;
15519
19397422
AM
15520 case DT_PPC64_OPD:
15521 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15522 if (s == NULL)
15523 continue;
15524 dyn.d_un.d_ptr = s->vma;
19397422
AM
15525 break;
15526
e8910a83
AM
15527 case DT_PPC64_OPT:
15528 if (htab->do_multi_toc && htab->multi_toc_needed)
15529 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15530 break;
15531
19397422
AM
15532 case DT_PPC64_OPDSZ:
15533 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
15534 if (s == NULL)
15535 continue;
eea6121a 15536 dyn.d_un.d_val = s->size;
19397422
AM
15537 break;
15538
65f38f15 15539 case DT_PLTGOT:
33e44f2e 15540 s = htab->elf.splt;
6348e046 15541 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
15542 break;
15543
15544 case DT_JMPREL:
33e44f2e 15545 s = htab->elf.srelplt;
6348e046 15546 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 15547 break;
5bd4f169 15548
65f38f15 15549 case DT_PLTRELSZ:
33e44f2e 15550 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 15551 break;
5bd4f169 15552 }
5bd4f169 15553
65f38f15 15554 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 15555 }
5bd4f169
AM
15556 }
15557
33e44f2e 15558 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
5d1634d7
AM
15559 {
15560 /* Fill in the first entry in the global offset table.
15561 We use it to hold the link-time TOCbase. */
15562 bfd_put_64 (output_bfd,
60ee0d4a 15563 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 15564 htab->elf.sgot->contents);
5d1634d7
AM
15565
15566 /* Set .got entry size. */
33e44f2e 15567 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
15568 }
15569
33e44f2e 15570 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5d1634d7
AM
15571 {
15572 /* Set .plt entry size. */
33e44f2e 15573 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 15574 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
15575 }
15576
84f5d08e
AM
15577 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15578 brlt ourselves if emitrelocations. */
15579 if (htab->brlt != NULL
15580 && htab->brlt->reloc_count != 0
15581 && !_bfd_elf_link_output_relocs (output_bfd,
15582 htab->brlt,
d4730f92 15583 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
15584 elf_section_data (htab->brlt)->relocs,
15585 NULL))
15586 return FALSE;
15587
176a0d42
AM
15588 if (htab->glink != NULL
15589 && htab->glink->reloc_count != 0
15590 && !_bfd_elf_link_output_relocs (output_bfd,
15591 htab->glink,
d4730f92 15592 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
15593 elf_section_data (htab->glink)->relocs,
15594 NULL))
15595 return FALSE;
15596
58d180e8 15597 if (htab->glink_eh_frame != NULL
da44f4e5
AM
15598 && htab->glink_eh_frame->size != 0)
15599 {
15600 bfd_vma val;
15601 bfd_byte *p;
15602 asection *stub_sec;
15603
15604 p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15605 for (stub_sec = htab->params->stub_bfd->sections;
15606 stub_sec != NULL;
15607 stub_sec = stub_sec->next)
15608 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15609 {
15610 /* FDE length. */
15611 p += 4;
15612 /* CIE pointer. */
15613 p += 4;
15614 /* Offset to stub section. */
15615 val = (stub_sec->output_section->vma
15616 + stub_sec->output_offset);
15617 val -= (htab->glink_eh_frame->output_section->vma
15618 + htab->glink_eh_frame->output_offset
15619 + (p - htab->glink_eh_frame->contents));
15620 if (val + 0x80000000 > 0xffffffff)
15621 {
15622 info->callbacks->einfo
15623 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15624 stub_sec->name);
15625 return FALSE;
15626 }
15627 bfd_put_32 (dynobj, val, p);
15628 p += 4;
15629 /* stub section size. */
15630 p += 4;
15631 /* Augmentation. */
15632 p += 1;
15633 /* Pad. */
15634 p += 7;
15635 }
15636 if (htab->glink != NULL && htab->glink->size != 0)
15637 {
15638 /* FDE length. */
15639 p += 4;
15640 /* CIE pointer. */
15641 p += 4;
15642 /* Offset to .glink. */
15643 val = (htab->glink->output_section->vma
15644 + htab->glink->output_offset
15645 + 8);
15646 val -= (htab->glink_eh_frame->output_section->vma
15647 + htab->glink_eh_frame->output_offset
15648 + (p - htab->glink_eh_frame->contents));
15649 if (val + 0x80000000 > 0xffffffff)
15650 {
15651 info->callbacks->einfo
15652 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15653 htab->glink->name);
15654 return FALSE;
15655 }
15656 bfd_put_32 (dynobj, val, p);
15657 p += 4;
15658 /* .glink size. */
15659 p += 4;
15660 /* Augmentation. */
15661 p += 1;
15662 /* Ops. */
15663 p += 7;
15664 }
15665
15666 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15667 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15668 htab->glink_eh_frame,
15669 htab->glink_eh_frame->contents))
15670 return FALSE;
15671 }
58d180e8 15672
e717da7e 15673 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
15674 since we didn't add them to DYNOBJ. We know dynobj is the first
15675 bfd. */
c72f2fb2 15676 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
15677 {
15678 asection *s;
7b53ace3 15679
0c8d6e5c 15680 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
15681 continue;
15682
e717da7e
AM
15683 s = ppc64_elf_tdata (dynobj)->got;
15684 if (s != NULL
eea6121a 15685 && s->size != 0
e717da7e
AM
15686 && s->output_section != bfd_abs_section_ptr
15687 && !bfd_set_section_contents (output_bfd, s->output_section,
15688 s->contents, s->output_offset,
eea6121a 15689 s->size))
e717da7e
AM
15690 return FALSE;
15691 s = ppc64_elf_tdata (dynobj)->relgot;
15692 if (s != NULL
eea6121a 15693 && s->size != 0
e717da7e
AM
15694 && s->output_section != bfd_abs_section_ptr
15695 && !bfd_set_section_contents (output_bfd, s->output_section,
15696 s->contents, s->output_offset,
eea6121a 15697 s->size))
e717da7e
AM
15698 return FALSE;
15699 }
f6c52c13 15700
b34976b6 15701 return TRUE;
5bd4f169
AM
15702}
15703
5bd4f169 15704#include "elf64-target.h"
7b8e7dad
AM
15705
15706/* FreeBSD support */
15707
15708#undef TARGET_LITTLE_SYM
15709#undef TARGET_LITTLE_NAME
15710
15711#undef TARGET_BIG_SYM
6d00b590 15712#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
15713#undef TARGET_BIG_NAME
15714#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15715
15716#undef ELF_OSABI
15717#define ELF_OSABI ELFOSABI_FREEBSD
15718
15719#undef elf64_bed
15720#define elf64_bed elf64_powerpc_fbsd_bed
15721
15722#include "elf64-target.h"
15723
This page took 3.306565 seconds and 4 git commands to generate.